driver-rockminer.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * Copyright 2014 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <stdbool.h>
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include "deviceapi.h"
  15. #include "lowlevel.h"
  16. #include "lowl-vcom.h"
  17. #include "miner.h"
  18. #define ROCKMINER_MIN_FREQ_MHZ 200
  19. #define ROCKMINER_DEF_FREQ_MHZ 270
  20. #define ROCKMINER_MAX_FREQ_MHZ 290
  21. #define ROCKMINER_POLL_US 0
  22. #define ROCKMINER_RETRY_US 5000000
  23. #define ROCKMINER_MIDTASK_TIMEOUT_US 500000
  24. #define ROCKMINER_MIDTASK_RETRY_US 1000000
  25. #define ROCKMINER_MAX_CHIPS 64
  26. #define ROCKMINER_WORK_REQ_SIZE 0x40
  27. #define ROCKMINER_REPLY_SIZE 8
  28. enum rockminer_replies {
  29. ROCKMINER_REPLY_NONCE_FOUND = 0,
  30. ROCKMINER_REPLY_TASK_COMPLETE = 1,
  31. ROCKMINER_REPLY_GET_TASK = 2,
  32. };
  33. BFG_REGISTER_DRIVER(rockminer_drv)
  34. static const struct bfg_set_device_definition rockminer_set_device_funcs[];
  35. struct rockminer_chip_data {
  36. uint8_t next_work_req[ROCKMINER_WORK_REQ_SIZE];
  37. struct work *works[2];
  38. uint8_t last_taskid;
  39. struct timeval tv_midtask_timeout;
  40. };
  41. static
  42. void rockminer_job_buf_init(uint8_t * const buf, const uint8_t chipid)
  43. {
  44. memset(&buf[0x20], 0, 0x10);
  45. buf[0x30] = 0xaa;
  46. // 0x31 is frequency, filled in elsewhere
  47. buf[0x32] = chipid;
  48. buf[0x33] = 0x55;
  49. }
  50. static
  51. void rockminer_job_buf_set_freq(uint8_t * const buf, const unsigned short freq)
  52. {
  53. buf[0x31] = (freq / 10) - 1;
  54. }
  55. static
  56. bool rockminer_lowl_match(const struct lowlevel_device_info * const info)
  57. {
  58. return lowlevel_match_product(info, "R-BOX miner") || lowlevel_match_product(info, "RX-BOX miner");
  59. }
  60. static const uint8_t golden_midstate[] = {
  61. 0x4a, 0x54, 0x8f, 0xe4, 0x71, 0xfa, 0x3a, 0x9a,
  62. 0x13, 0x71, 0x14, 0x45, 0x56, 0xc3, 0xf6, 0x4d,
  63. 0x25, 0x00, 0xb4, 0x82, 0x60, 0x08, 0xfe, 0x4b,
  64. 0xbf, 0x76, 0x98, 0xc9, 0x4e, 0xba, 0x79, 0x46,
  65. };
  66. static const uint8_t golden_datatail[] = {
  67. 0xce, 0x22, 0xa7, 0x2f,
  68. 0x4f, 0x67, 0x26, 0x14, 0x1a, 0x0b, 0x32, 0x87,
  69. };
  70. static const uint8_t golden_result[] = {
  71. 0x00, 0x01, 0x87, 0xa2,
  72. };
  73. int8_t rockminer_bisect_chips(const int fd, uint8_t * const buf)
  74. {
  75. static const int max_concurrent_tests = 4;
  76. int concurrent_tests = max_concurrent_tests;
  77. uint8_t tests[max_concurrent_tests];
  78. uint8_t reply[ROCKMINER_REPLY_SIZE];
  79. uint8_t minvalid = 0, maxvalid = ROCKMINER_MAX_CHIPS - 1;
  80. uint8_t pertest;
  81. char msg[0x10];
  82. ssize_t rsz;
  83. do {
  84. pertest = (maxvalid + 1 - minvalid) / concurrent_tests;
  85. if (!pertest)
  86. pertest = 1;
  87. msg[0] = '\0';
  88. for (int i = 0; i < concurrent_tests; ++i)
  89. {
  90. uint8_t chipid = (minvalid + pertest * (i + 1)) - 1;
  91. if (chipid > maxvalid)
  92. {
  93. concurrent_tests = i;
  94. break;
  95. }
  96. tests[i] = chipid;
  97. buf[0x32] = chipid;
  98. if (write(fd, buf, ROCKMINER_WORK_REQ_SIZE) != ROCKMINER_WORK_REQ_SIZE)
  99. applogr(-1, LOG_DEBUG, "%s(%d): Error sending request for chip %d", __func__, fd, chipid);
  100. tailsprintf(msg, sizeof(msg), "%d ", chipid);
  101. }
  102. msg[strlen(msg)-1] = '\0';
  103. applog(LOG_DEBUG, "%s(%d): Testing chips %s (within range %d-%d)", __func__, fd, msg, minvalid, maxvalid);
  104. while ( (rsz = read(fd, reply, sizeof(reply))) == sizeof(reply))
  105. {
  106. const uint8_t chipid = reply[5] & 0x3f;
  107. if (chipid > minvalid)
  108. {
  109. applog(LOG_DEBUG, "%s(%d): Saw chip %d", __func__, fd, chipid);
  110. minvalid = chipid;
  111. if (minvalid >= tests[concurrent_tests-1])
  112. break;
  113. }
  114. }
  115. for (int i = concurrent_tests; i--; )
  116. {
  117. if (tests[i] > minvalid)
  118. {
  119. applog(LOG_DEBUG, "%s(%d): Didn't see chip %d", __func__, fd, tests[i]);
  120. maxvalid = tests[i] - 1;
  121. }
  122. else
  123. break;
  124. }
  125. } while (minvalid != maxvalid);
  126. return maxvalid + 1;
  127. }
  128. static
  129. bool rockminer_detect_one(const char * const devpath)
  130. {
  131. int fd, chips;
  132. uint8_t buf[ROCKMINER_WORK_REQ_SIZE], reply[ROCKMINER_REPLY_SIZE];
  133. ssize_t rsz;
  134. fd = serial_open(devpath, 0, 1, true);
  135. if (fd < 0)
  136. return_via_applog(err, , LOG_DEBUG, "%s: %s %s", rockminer_drv.dname, "Failed to open", devpath);
  137. applog(LOG_DEBUG, "%s: %s %s", rockminer_drv.dname, "Successfully opened", devpath);
  138. rockminer_job_buf_init(buf, 0);
  139. rockminer_job_buf_set_freq(buf, ROCKMINER_MIN_FREQ_MHZ);
  140. memcpy(&buf[ 0], golden_midstate, 0x20);
  141. memcpy(&buf[0x34], golden_datatail, 0xc);
  142. if (write(fd, buf, sizeof(buf)) != sizeof(buf))
  143. return_via_applog(err, , LOG_DEBUG, "%s: %s %s", rockminer_drv.dname, "Error sending request to ", devpath);
  144. while (true)
  145. {
  146. rsz = read(fd, reply, sizeof(reply));
  147. if (rsz != sizeof(reply))
  148. return_via_applog(err, , LOG_DEBUG, "%s: Short read from %s (%d)", rockminer_drv.dname, devpath, rsz);
  149. if ((!memcmp(reply, golden_result, sizeof(golden_result))) && (reply[4] & 0xf) == ROCKMINER_REPLY_NONCE_FOUND)
  150. break;
  151. }
  152. applog(LOG_DEBUG, "%s: Found chip 0 on %s, probing for total chip count", rockminer_drv.dname, devpath);
  153. chips = rockminer_bisect_chips(fd, buf);
  154. applog(LOG_DEBUG, "%s: Identified %d chips on %s", rockminer_drv.dname, chips, devpath);
  155. if (serial_claim_v(devpath, &rockminer_drv))
  156. goto err;
  157. serial_close(fd);
  158. struct cgpu_info * const cgpu = malloc(sizeof(*cgpu));
  159. *cgpu = (struct cgpu_info){
  160. .drv = &rockminer_drv,
  161. .set_device_funcs = rockminer_set_device_funcs,
  162. .device_path = strdup(devpath),
  163. .deven = DEV_ENABLED,
  164. .procs = chips,
  165. .threads = 1,
  166. };
  167. // NOTE: Xcode's clang has a bug where it cannot find fields inside anonymous unions (more details in fpgautils)
  168. cgpu->device_fd = -1;
  169. return add_cgpu(cgpu);
  170. err:
  171. if (fd >= 0)
  172. serial_close(fd);
  173. return false;
  174. }
  175. static
  176. bool rockminer_lowl_probe(const struct lowlevel_device_info * const info)
  177. {
  178. return vcom_lowl_probe_wrapper(info, rockminer_detect_one);
  179. }
  180. static
  181. bool rockminer_init(struct thr_info * const master_thr)
  182. {
  183. struct cgpu_info * const dev = master_thr->cgpu;
  184. for_each_managed_proc(proc, dev)
  185. {
  186. struct thr_info * const thr = proc->thr[0];
  187. struct rockminer_chip_data * const chip = malloc(sizeof(*chip));
  188. thr->cgpu_data = chip;
  189. *chip = (struct rockminer_chip_data){
  190. .last_taskid = 0,
  191. };
  192. rockminer_job_buf_init(chip->next_work_req, proc->proc_id);
  193. rockminer_job_buf_set_freq(chip->next_work_req, ROCKMINER_DEF_FREQ_MHZ);
  194. }
  195. timer_set_now(&master_thr->tv_poll);
  196. return true;
  197. }
  198. static
  199. void rockminer_dead(struct cgpu_info * const dev)
  200. {
  201. serial_close(dev->device_fd);
  202. dev->device_fd = -1;
  203. for_each_managed_proc(proc, dev)
  204. {
  205. struct thr_info * const thr = proc->thr[0];
  206. thr->queue_full = true;
  207. }
  208. }
  209. static
  210. bool rockminer_send_work(struct thr_info * const thr)
  211. {
  212. struct cgpu_info * const proc = thr->cgpu;
  213. struct cgpu_info * const dev = proc->device;
  214. struct rockminer_chip_data * const chip = thr->cgpu_data;
  215. const int fd = dev->device_fd;
  216. return (write(fd, chip->next_work_req, sizeof(chip->next_work_req)) == sizeof(chip->next_work_req));
  217. }
  218. static
  219. bool rockminer_queue_append(struct thr_info * const thr, struct work * const work)
  220. {
  221. struct cgpu_info * const proc = thr->cgpu;
  222. struct cgpu_info * const dev = proc->device;
  223. struct rockminer_chip_data * const chip = thr->cgpu_data;
  224. const int fd = dev->device_fd;
  225. thr->queue_full = true;
  226. if (fd < 0)
  227. return false;
  228. memcpy(&chip->next_work_req[ 0], work->midstate, 0x20);
  229. memcpy(&chip->next_work_req[0x34], &work->data[0x40], 0xc);
  230. if (!rockminer_send_work(thr))
  231. {
  232. rockminer_dead(dev);
  233. inc_hw_errors_only(thr);
  234. applogr(false, LOG_ERR, "%"PRIpreprv": Failed to send work", proc->proc_repr);
  235. }
  236. chip->last_taskid = chip->last_taskid ? 0 : 1;
  237. if (chip->works[chip->last_taskid])
  238. free_work(chip->works[chip->last_taskid]);
  239. chip->works[chip->last_taskid] = work;
  240. applog(LOG_DEBUG, "%"PRIpreprv": Work %d queued as task %d", proc->proc_repr, work->id, chip->last_taskid);
  241. return true;
  242. }
  243. static
  244. void rockminer_queue_flush(__maybe_unused struct thr_info * const thr)
  245. {
  246. }
  247. static
  248. void rockminer_poll(struct thr_info * const master_thr)
  249. {
  250. struct cgpu_info * const dev = master_thr->cgpu;
  251. int fd = dev->device_fd;
  252. uint8_t reply[ROCKMINER_REPLY_SIZE];
  253. ssize_t rsz;
  254. if (fd < 0)
  255. {
  256. fd = serial_open(dev->device_path, 0, 1, true);
  257. if (fd < 0)
  258. {
  259. timer_set_delay_from_now(&master_thr->tv_poll, ROCKMINER_RETRY_US);
  260. for_each_managed_proc(proc, dev)
  261. {
  262. struct thr_info * const thr = proc->thr[0];
  263. inc_hw_errors_only(thr);
  264. }
  265. applogr(, LOG_ERR, "%s: Failed to open %s", dev->dev_repr, dev->device_path);
  266. }
  267. dev->device_fd = fd;
  268. for_each_managed_proc(proc, dev)
  269. {
  270. struct thr_info * const thr = proc->thr[0];
  271. struct rockminer_chip_data * const chip = thr->cgpu_data;
  272. thr->queue_full = false;
  273. timer_unset(&chip->tv_midtask_timeout);
  274. }
  275. }
  276. while ( (rsz = read(fd, reply, sizeof(reply))) == sizeof(reply))
  277. {
  278. // const uint8_t status = reply[4] >> 4;
  279. const enum rockminer_replies cmd = reply[4] & 0xf;
  280. // const uint8_t prodid = reply[5] >> 6;
  281. const uint8_t chipid = reply[5] & 0x3f;
  282. const uint8_t taskid = reply[6] & 1;
  283. const uint8_t temp = reply[7];
  284. struct cgpu_info * const proc = device_proc_by_id(dev, chipid);
  285. if (unlikely(!proc))
  286. {
  287. for_each_managed_proc(proc, dev)
  288. {
  289. struct thr_info * const thr = proc->thr[0];
  290. inc_hw_errors_only(thr);
  291. }
  292. applog(LOG_ERR, "%s: Chip id %d out of range", dev->dev_repr, chipid);
  293. continue;
  294. }
  295. struct thr_info * const thr = proc->thr[0];
  296. struct rockminer_chip_data * const chip = thr->cgpu_data;
  297. if (temp != 128)
  298. proc->temp = temp;
  299. switch (cmd) {
  300. case ROCKMINER_REPLY_NONCE_FOUND:
  301. {
  302. const uint32_t nonce = upk_u32be(reply, 0);
  303. struct work *work;
  304. if (chip->works[taskid] && test_nonce(chip->works[taskid], nonce, false))
  305. {}
  306. else
  307. if (chip->works[taskid ? 0 : 1] && test_nonce(chip->works[taskid ? 0 : 1], nonce, false))
  308. {
  309. applog(LOG_DEBUG, "%"PRIpreprv": We have task ids inverted; fixing", proc->proc_repr);
  310. work = chip->works[0];
  311. chip->works[0] = chip->works[1];
  312. chip->works[1] = work;
  313. chip->last_taskid = chip->last_taskid ? 0 : 1;
  314. }
  315. work = chip->works[taskid];
  316. submit_nonce(thr, work, nonce);
  317. break;
  318. }
  319. case ROCKMINER_REPLY_TASK_COMPLETE:
  320. applog(LOG_DEBUG, "%"PRIpreprv": Task %d completed", proc->proc_repr, taskid);
  321. hashes_done2(thr, 0x100000000, NULL);
  322. timer_set_delay_from_now(&chip->tv_midtask_timeout, ROCKMINER_MIDTASK_TIMEOUT_US);
  323. break;
  324. case ROCKMINER_REPLY_GET_TASK:
  325. applog(LOG_DEBUG, "%"PRIpreprv": Task %d requested", proc->proc_repr, taskid);
  326. thr->queue_full = false;
  327. timer_unset(&chip->tv_midtask_timeout);
  328. break;
  329. }
  330. }
  331. if (rsz < 0)
  332. rockminer_dead(dev);
  333. struct timeval tv_now;
  334. timer_set_now(&tv_now);
  335. for_each_managed_proc(proc, dev)
  336. {
  337. struct thr_info * const thr = proc->thr[0];
  338. struct rockminer_chip_data * const chip = thr->cgpu_data;
  339. if (timer_passed(&chip->tv_midtask_timeout, &tv_now))
  340. {
  341. // A task completed, but no request followed
  342. // This means it missed our last task send, so we need to resend it
  343. applog(LOG_WARNING, "%"PRIpreprv": No task request? Probably lost, resending task %d", proc->proc_repr, chip->last_taskid);
  344. inc_hw_errors_only(thr);
  345. timer_set_delay(&chip->tv_midtask_timeout, &tv_now, ROCKMINER_MIDTASK_RETRY_US);
  346. if (!rockminer_send_work(thr))
  347. {
  348. rockminer_dead(dev);
  349. timer_set_delay_from_now(&master_thr->tv_poll, ROCKMINER_RETRY_US);
  350. inc_hw_errors_only(thr);
  351. applogr(, LOG_ERR, "%"PRIpreprv": Failed to resend work", proc->proc_repr);
  352. }
  353. }
  354. }
  355. timer_set_delay_from_now(&master_thr->tv_poll, ROCKMINER_POLL_US);
  356. }
  357. static
  358. const char *rockminer_set_clock(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  359. {
  360. struct thr_info * const thr = proc->thr[0];
  361. struct rockminer_chip_data * const chip = thr->cgpu_data;
  362. const int val = atoi(newvalue);
  363. if (val < ROCKMINER_MIN_FREQ_MHZ || val > ROCKMINER_MAX_FREQ_MHZ)
  364. return "Invalid clock speed";
  365. applog(LOG_DEBUG, "%"PRIpreprv": Changing clock frequency for future jobs to %d MHz", proc->proc_repr, val);
  366. rockminer_job_buf_set_freq(chip->next_work_req, val);
  367. return NULL;
  368. }
  369. static const struct bfg_set_device_definition rockminer_set_device_funcs[] = {
  370. {"clock", rockminer_set_clock, "clock frequency"},
  371. {NULL}
  372. };
  373. static
  374. int rockminer_get_clock(struct cgpu_info * const proc)
  375. {
  376. struct thr_info * const thr = proc->thr[0];
  377. struct rockminer_chip_data * const chip = thr->cgpu_data;
  378. return ((int)chip->next_work_req[0x31] + 1) * 10;
  379. }
  380. static
  381. struct api_data *rockminer_get_extra_device_status(struct cgpu_info * const proc)
  382. {
  383. struct api_data *root = NULL;
  384. double d = rockminer_get_clock(proc);
  385. root = api_add_freq(root, "Frequency", &d, true);
  386. return root;
  387. }
  388. #ifdef HAVE_CURSES
  389. static
  390. void rockminer_tui_wlogprint_choices(struct cgpu_info * const proc)
  391. {
  392. wlogprint("[C]lock speed ");
  393. }
  394. static
  395. const char *rockminer_tui_handle_choice(struct cgpu_info * const proc, const int input)
  396. {
  397. static char buf[0x100]; // Static for replies
  398. switch (input)
  399. {
  400. case 'c': case 'C':
  401. {
  402. sprintf(buf, "Set clock speed (range %d-%d, multiple of 10)", ROCKMINER_MIN_FREQ_MHZ, ROCKMINER_MAX_FREQ_MHZ);
  403. char * const val = curses_input(buf);
  404. const char * const msg = rockminer_set_clock(proc, "clock", val ?: "", NULL, NULL);
  405. free(val);
  406. if (msg)
  407. {
  408. snprintf(buf, sizeof(buf), "%s\n", msg);
  409. return buf;
  410. }
  411. return "Clock speed changed\n";
  412. }
  413. }
  414. return NULL;
  415. }
  416. static
  417. void rockminer_wlogprint_status(struct cgpu_info * const proc)
  418. {
  419. wlogprint("Clock speed: %d\n", rockminer_get_clock(proc));
  420. }
  421. #endif
  422. struct device_drv rockminer_drv = {
  423. .dname = "rockminer",
  424. .name = "RKM",
  425. .lowl_match = rockminer_lowl_match,
  426. .lowl_probe = rockminer_lowl_probe,
  427. .thread_init = rockminer_init,
  428. .minerloop = minerloop_queue,
  429. .queue_append = rockminer_queue_append,
  430. .queue_flush = rockminer_queue_flush,
  431. .poll = rockminer_poll,
  432. .get_api_extra_device_status = rockminer_get_extra_device_status,
  433. #ifdef HAVE_CURSES
  434. .proc_wlogprint_status = rockminer_wlogprint_status,
  435. .proc_tui_wlogprint_choices = rockminer_tui_wlogprint_choices,
  436. .proc_tui_handle_choice = rockminer_tui_handle_choice,
  437. #endif
  438. };