driver-rockminer.c 16 KB

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