driver-bifury.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /*
  2. * Copyright 2013 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 <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <uthash.h>
  17. #include "deviceapi.h"
  18. #include "logging.h"
  19. #include "lowlevel.h"
  20. #include "lowl-vcom.h"
  21. #include "miner.h"
  22. #include "util.h"
  23. #define BIFURY_MAX_QUEUED 0x10
  24. BFG_REGISTER_DRIVER(bifury_drv)
  25. const char bifury_init_cmds[] = "flush\ntarget ffffffff\nmaxroll 0\n";
  26. static
  27. ssize_t bifury_write(const struct cgpu_info * const dev, const void * const buf, const size_t count)
  28. {
  29. const int fd = dev->device_fd;
  30. if (opt_dev_protocol)
  31. {
  32. const int psz = (((const char*)buf)[count-1] == '\n') ? (count - 1) : count;
  33. applog(LOG_DEBUG, "%s: DEVPROTO: SEND %.*s", dev->dev_repr, psz, (const char*)buf);
  34. }
  35. return write(fd, buf, count);
  36. }
  37. static
  38. void *bifury_readln(int fd, bytes_t *leftover)
  39. {
  40. uint8_t buf[0x40];
  41. ssize_t r;
  42. parse:
  43. if ( (r = bytes_find(leftover, '\n')) >= 0)
  44. {
  45. uint8_t *ret = malloc(r+1);
  46. if (r)
  47. memcpy(ret, bytes_buf(leftover), r);
  48. ret[r] = '\0';
  49. bytes_shift(leftover, r + 1);
  50. return ret;
  51. }
  52. if ( (r = read(fd, buf, sizeof(buf))) > 0)
  53. {
  54. bytes_append(leftover, buf, r);
  55. goto parse;
  56. }
  57. return NULL;
  58. }
  59. struct bifury_state {
  60. bytes_t buf;
  61. uint32_t last_work_id;
  62. int needwork;
  63. bool has_needwork;
  64. uint8_t *osc6_bits;
  65. bool send_clock;
  66. };
  67. static
  68. bool bifury_lowl_match(const struct lowlevel_device_info * const info)
  69. {
  70. return lowlevel_match_product(info, "bi\xe2\x80\xa2""fury");
  71. }
  72. static
  73. bool bifury_detect_one(const char * const devpath)
  74. {
  75. char buf[0x40], *p, *q, *s;
  76. bytes_t reply = BYTES_INIT;
  77. int major, minor, hwrev, chips;
  78. struct cgpu_info *cgpu;
  79. struct timeval tv_timeout;
  80. const int fd = serial_open(devpath, 0, 10, true);
  81. applog(LOG_DEBUG, "%s: %s %s",
  82. bifury_drv.dname,
  83. ((fd == -1) ? "Failed to open" : "Successfully opened"),
  84. devpath);
  85. if (unlikely(fd == -1))
  86. return false;
  87. while (read(fd, buf, sizeof(buf)) == sizeof(buf))
  88. {}
  89. if (opt_dev_protocol)
  90. applog(LOG_DEBUG, "%s fd=%d: DEVPROTO: SEND %s", bifury_drv.dname, fd, "version");
  91. if (8 != write(fd, "version\n", 8))
  92. {
  93. applog(LOG_DEBUG, "%s: Error sending version request", bifury_drv.dname);
  94. goto err;
  95. }
  96. timer_set_delay_from_now(&tv_timeout, 1000000);
  97. while (true)
  98. {
  99. p = bifury_readln(fd, &reply);
  100. if (p)
  101. {
  102. if (opt_dev_protocol)
  103. applog(LOG_DEBUG, "%s fd=%d: DEVPROTO: RECV %s",
  104. bifury_drv.dname, fd, p);
  105. if (!strncmp("version ", p, 8))
  106. break;
  107. free(p);
  108. }
  109. if (timer_passed(&tv_timeout, NULL))
  110. {
  111. applog(LOG_DEBUG, "%s: Timed out waiting for response to version request",
  112. bifury_drv.dname);
  113. goto err;
  114. }
  115. }
  116. bytes_free(&reply);
  117. serial_close(fd);
  118. s = p;
  119. major = strtol(&p[8], &p, 10);
  120. if (p == &buf[8] || p[0] != '.')
  121. goto parseerr;
  122. minor = strtol(&p[1], &q, 10);
  123. if (p == q || strncmp(" rev ", q, 5))
  124. goto parseerr;
  125. hwrev = strtol(&q[5], &p, 10);
  126. if (p == q || strncmp(" chips ", p, 7))
  127. goto parseerr;
  128. chips = strtol(&p[7], &q, 10);
  129. if (p == q || chips < 1)
  130. goto parseerr;
  131. free(s);
  132. applog(LOG_DEBUG, "%s: Found firmware %d.%d on hardware rev %d with %d chips",
  133. bifury_drv.dname, major, minor, hwrev, chips);
  134. cgpu = malloc(sizeof(*cgpu));
  135. *cgpu = (struct cgpu_info){
  136. .drv = &bifury_drv,
  137. .device_path = strdup(devpath),
  138. .deven = DEV_ENABLED,
  139. .procs = chips,
  140. .threads = 1,
  141. .cutofftemp = 75,
  142. };
  143. // NOTE: Xcode's clang has a bug where it cannot find fields inside anonymous unions (more details in fpgautils)
  144. cgpu->device_fd = -1;
  145. return add_cgpu(cgpu);
  146. parseerr:
  147. applog(LOG_DEBUG, "%s: Error parsing version response", bifury_drv.dname);
  148. free(s);
  149. return false;
  150. err:
  151. bytes_free(&reply);
  152. serial_close(fd);
  153. return false;
  154. }
  155. static
  156. bool bifury_lowl_probe(const struct lowlevel_device_info * const info)
  157. {
  158. return vcom_lowl_probe_wrapper(info, bifury_detect_one);
  159. }
  160. static
  161. bool bifury_set_queue_full(const struct cgpu_info * const dev, int needwork)
  162. {
  163. struct bifury_state * const state = dev->device_data;
  164. struct thr_info * const master_thr = dev->thr[0];
  165. const int fd = dev->device_fd;
  166. if (needwork != -1)
  167. state->needwork = needwork;
  168. const bool full = (fd == -1 || !state->needwork);
  169. if (full == master_thr->queue_full)
  170. return full;
  171. for (const struct cgpu_info *proc = dev; proc; proc = proc->next_proc)
  172. {
  173. struct thr_info * const thr = proc->thr[0];
  174. thr->queue_full = full;
  175. }
  176. return full;
  177. }
  178. void bifury_send_clock(const struct cgpu_info * const dev)
  179. {
  180. struct bifury_state * const state = dev->device_data;
  181. const struct cgpu_info *proc;
  182. size_t clockbufsz = 5 + (3 * dev->procs) + 1 + 1;
  183. char clockbuf[clockbufsz];
  184. strcpy(clockbuf, "clock");
  185. proc = dev;
  186. for (int i = 0; i < dev->procs; ++i, (proc = proc->next_proc))
  187. {
  188. const struct thr_info * const thr = proc->thr[0];
  189. int clk;
  190. if (proc->deven == DEV_ENABLED && !thr->pause)
  191. clk = state->osc6_bits[i];
  192. else
  193. clk = 0;
  194. tailsprintf(clockbuf, clockbufsz, " %d", clk);
  195. }
  196. tailsprintf(clockbuf, clockbufsz, "\n");
  197. --clockbufsz;
  198. if (clockbufsz != bifury_write(dev, clockbuf, clockbufsz))
  199. {
  200. state->send_clock = true;
  201. applog(LOG_ERR, "%s: Failed to send clock assignments",
  202. dev->dev_repr);
  203. }
  204. else
  205. state->send_clock = false;
  206. }
  207. static
  208. bool bifury_thread_init(struct thr_info *master_thr)
  209. {
  210. struct cgpu_info * const dev = master_thr->cgpu, *proc;
  211. struct bifury_state * const state = malloc(sizeof(*state));
  212. if (!state)
  213. return false;
  214. *state = (struct bifury_state){
  215. .buf = BYTES_INIT,
  216. .osc6_bits = malloc(sizeof(*state->osc6_bits) * dev->procs),
  217. };
  218. for (int i = 0; i < dev->procs; ++i)
  219. state->osc6_bits[i] = 54;
  220. for (proc = dev; proc; proc = proc->next_proc)
  221. {
  222. proc->device_data = state;
  223. proc->status = LIFE_INIT2;
  224. }
  225. bifury_set_queue_full(dev, 0);
  226. timer_set_now(&master_thr->tv_poll);
  227. return true;
  228. }
  229. static
  230. void bifury_reinit(struct cgpu_info * const proc)
  231. {
  232. timer_set_now(&proc->thr[0]->tv_poll);
  233. }
  234. void bifury_trigger_send_clock(struct thr_info * const thr)
  235. {
  236. struct cgpu_info * const proc = thr->cgpu;
  237. struct bifury_state * const state = proc->device_data;
  238. state->send_clock = true;
  239. }
  240. static
  241. void bifury_common_error(struct cgpu_info * const dev, const enum dev_reason reason)
  242. {
  243. for (struct cgpu_info *proc = dev; proc; proc = proc->next_proc)
  244. {
  245. struct thr_info * const thr = proc->thr[0];
  246. dev_error(proc, reason);
  247. inc_hw_errors_only(thr);
  248. }
  249. }
  250. static
  251. bool bifury_queue_append(struct thr_info * const thr, struct work *work)
  252. {
  253. const struct cgpu_info * const dev = thr->cgpu->device;
  254. struct bifury_state * const state = dev->device_data;
  255. if (bifury_set_queue_full(dev, -1))
  256. return false;
  257. struct thr_info * const master_thr = dev->thr[0];
  258. char buf[5 + 0x98 + 1 + 8 + 1];
  259. memcpy(buf, "work ", 5);
  260. bin2hex(&buf[5], work->data, 0x4c);
  261. work->device_id = ++state->last_work_id;
  262. sprintf(&buf[5 + 0x98], " %08x", work->device_id);
  263. buf[5 + 0x98 + 1 + 8] = '\n';
  264. if (sizeof(buf) != bifury_write(dev, buf, sizeof(buf)))
  265. {
  266. applog(LOG_ERR, "%s: Failed to send work", dev->dev_repr);
  267. return false;
  268. }
  269. HASH_ADD(hh, master_thr->work_list, device_id, sizeof(work->device_id), work);
  270. int prunequeue = HASH_COUNT(master_thr->work_list) - BIFURY_MAX_QUEUED;
  271. if (prunequeue > 0)
  272. {
  273. struct work *tmp;
  274. applog(LOG_DEBUG, "%s: Pruning %d old work item%s",
  275. dev->dev_repr, prunequeue, prunequeue == 1 ? "" : "s");
  276. HASH_ITER(hh, master_thr->work_list, work, tmp)
  277. {
  278. HASH_DEL(master_thr->work_list, work);
  279. free_work(work);
  280. if (--prunequeue < 1)
  281. break;
  282. }
  283. }
  284. bifury_set_queue_full(dev, state->needwork - 1);
  285. return true;
  286. }
  287. static
  288. void bifury_queue_flush(struct thr_info * const thr)
  289. {
  290. const struct cgpu_info *dev = thr->cgpu;
  291. if (dev != dev->device)
  292. return;
  293. const int fd = dev->device_fd;
  294. if (fd != -1)
  295. bifury_write(dev, "flush\n", 6);
  296. bifury_set_queue_full(dev, dev->procs);
  297. }
  298. static
  299. const struct cgpu_info *device_proc_by_id(const struct cgpu_info * const dev, int procid)
  300. {
  301. const struct cgpu_info *proc = dev;
  302. for (int i = 0; i < procid; ++i)
  303. {
  304. proc = proc->next_proc;
  305. if (unlikely(!proc))
  306. return NULL;
  307. }
  308. return proc;
  309. }
  310. static
  311. void bifury_handle_cmd(struct cgpu_info * const dev, const char * const cmd)
  312. {
  313. struct thr_info * const master_thr = dev->thr[0];
  314. struct bifury_state * const state = dev->device_data;
  315. struct thr_info *thr;
  316. struct work *work;
  317. char *p;
  318. if (!strncmp(cmd, "submit ", 7))
  319. {
  320. // submit <nonce> <jobid> <timestamp> <chip>
  321. uint32_t nonce = strtoll(&cmd[7], &p, 0x10);
  322. const uint32_t jobid = strtoll(&p[1], &p, 0x10);
  323. const uint32_t ntime = strtoll(&p[1], &p, 0x10);
  324. const int chip = atoi(&p[1]);
  325. nonce = le32toh(nonce);
  326. const struct cgpu_info * const proc = device_proc_by_id(dev, chip);
  327. thr = proc->thr[0];
  328. HASH_FIND(hh, master_thr->work_list, &jobid, sizeof(jobid), work);
  329. if (work)
  330. {
  331. const uint32_t work_ntime = be32toh(*(uint32_t*)&work->data[68]);
  332. submit_noffset_nonce(thr, work, nonce, ntime - work_ntime);
  333. }
  334. else
  335. if (!jobid)
  336. applog(LOG_DEBUG, "%s: Dummy submit ignored", dev->dev_repr);
  337. else
  338. inc_hw_errors2(thr, NULL, &nonce);
  339. if (!state->has_needwork)
  340. bifury_set_queue_full(dev, state->needwork + 2);
  341. }
  342. else
  343. if (!strncmp(cmd, "temp ", 5))
  344. {
  345. struct cgpu_info *proc;
  346. const int decicelsius = atoi(&cmd[5]);
  347. if (decicelsius)
  348. {
  349. const float celsius = 0.1 * (float)decicelsius;
  350. for (proc = dev; proc; proc = proc->next_proc)
  351. proc->temp = celsius;
  352. }
  353. }
  354. else
  355. if (!strncmp(cmd, "job ", 4))
  356. {
  357. // job <jobid> <timestamp> <chip>
  358. const uint32_t jobid = strtoll(&cmd[4], &p, 0x10);
  359. strtoll(&p[1], &p, 0x10);
  360. const int chip = atoi(&p[1]);
  361. const struct cgpu_info * const proc = device_proc_by_id(dev, chip);
  362. HASH_FIND(hh, master_thr->work_list, &jobid, sizeof(jobid), work);
  363. if (likely(work))
  364. {
  365. if (likely(proc))
  366. {
  367. thr = proc->thr[0];
  368. hashes_done2(thr, 0xbd000000, NULL);
  369. }
  370. else
  371. applog(LOG_DEBUG, "%s: Unknown chip id: %s",
  372. dev->dev_repr, cmd);
  373. HASH_DEL(master_thr->work_list, work);
  374. free_work(work);
  375. }
  376. else
  377. applog(LOG_WARNING, "%s: Unknown job id: %s",
  378. dev->dev_repr, cmd);
  379. }
  380. else
  381. if (!strncmp(cmd, "hwerror ", 8))
  382. {
  383. const int chip = atoi(&cmd[8]);
  384. const struct cgpu_info * const proc = device_proc_by_id(dev, chip);
  385. thr = proc->thr[0];
  386. inc_hw_errors2(thr, NULL, UNKNOWN_NONCE);
  387. }
  388. else
  389. if (!strncmp(cmd, "needwork ", 9))
  390. {
  391. const int needwork = atoi(&cmd[9]);
  392. state->has_needwork = true;
  393. bifury_set_queue_full(dev, needwork);
  394. applog(LOG_DEBUG, "%s: needwork=%d", dev->dev_repr, state->needwork);
  395. }
  396. }
  397. static
  398. void bifury_poll(struct thr_info * const master_thr)
  399. {
  400. struct cgpu_info * const dev = master_thr->cgpu;
  401. struct bifury_state * const state = dev->device_data;
  402. int fd = dev->device_fd;
  403. char *cmd;
  404. if (unlikely(fd == -1))
  405. {
  406. fd = serial_open(dev->device_path, 0, 1, true);
  407. if (unlikely(fd == -1))
  408. {
  409. applog(LOG_ERR, "%s: Failed to open %s",
  410. dev->dev_repr, dev->device_path);
  411. bifury_common_error(dev, REASON_THREAD_FAIL_INIT);
  412. return;
  413. }
  414. dev->device_fd = fd;
  415. if (sizeof(bifury_init_cmds)-1 != bifury_write(dev, bifury_init_cmds, sizeof(bifury_init_cmds)-1))
  416. {
  417. applog(LOG_ERR, "%s: Failed to send configuration", dev->dev_repr);
  418. bifury_common_error(dev, REASON_THREAD_FAIL_INIT);
  419. serial_close(fd);
  420. dev->device_fd = -1;
  421. return;
  422. }
  423. bifury_set_queue_full(dev, dev->procs * 2);
  424. state->send_clock = true;
  425. }
  426. if (state->send_clock)
  427. bifury_send_clock(dev);
  428. while ( (cmd = bifury_readln(fd, &state->buf)) )
  429. {
  430. if (opt_dev_protocol)
  431. applog(LOG_DEBUG, "%s: DEVPROTO: RECV %s", dev->dev_repr, cmd);
  432. bifury_handle_cmd(dev, cmd);
  433. free(cmd);
  434. }
  435. }
  436. static
  437. struct api_data *bifury_api_device_status(struct cgpu_info * const proc)
  438. {
  439. struct bifury_state * const state = proc->device_data;
  440. struct api_data *root = NULL;
  441. int osc6_bits = state->osc6_bits[proc->proc_id];
  442. root = api_add_int(root, "Clock Bits", &osc6_bits, true);
  443. return root;
  444. }
  445. char *bifury_set_device(struct cgpu_info * const proc, char * const option, char * const setting, char * const replybuf)
  446. {
  447. struct bifury_state * const state = proc->device_data;
  448. if (!strcasecmp(option, "help"))
  449. {
  450. sprintf(replybuf, "osc6_bits: range 33-63 (slow to fast)");
  451. return replybuf;
  452. }
  453. if (!strcasecmp(option, "osc6_bits"))
  454. {
  455. if (!setting || !*setting)
  456. {
  457. sprintf(replybuf, "missing setting");
  458. return replybuf;
  459. }
  460. const uint8_t val = atoi(setting);
  461. if (val < 33 || val > 63)
  462. {
  463. sprintf(replybuf, "invalid setting");
  464. return replybuf;
  465. }
  466. state->osc6_bits[proc->proc_id] = val;
  467. state->send_clock = true;
  468. return NULL;
  469. }
  470. sprintf(replybuf, "Unknown option: %s", option);
  471. return replybuf;
  472. }
  473. #ifdef HAVE_CURSES
  474. void bifury_tui_wlogprint_choices(struct cgpu_info * const proc)
  475. {
  476. wlogprint("[O]scillator bits ");
  477. }
  478. const char *bifury_tui_handle_choice(struct cgpu_info * const proc, const int input)
  479. {
  480. struct bifury_state * const state = proc->device_data;
  481. switch (input)
  482. {
  483. case 'o': case 'O':
  484. {
  485. const int val = curses_int("Set oscillator bits (range 33-63; slow to fast)");
  486. if (val < 33 || val > 63)
  487. return "Invalid oscillator bits\n";
  488. state->osc6_bits[proc->proc_id] = val;
  489. state->send_clock = true;
  490. return "Oscillator bits changing\n";
  491. }
  492. }
  493. return NULL;
  494. }
  495. void bifury_wlogprint_status(struct cgpu_info * const proc)
  496. {
  497. const struct bifury_state * const state = proc->device_data;
  498. const int osc6_bits = state->osc6_bits[proc->proc_id];
  499. wlogprint("Oscillator bits: %d\n", osc6_bits);
  500. }
  501. #endif
  502. struct device_drv bifury_drv = {
  503. .dname = "bifury",
  504. .name = "BIF",
  505. .lowl_match = bifury_lowl_match,
  506. .lowl_probe = bifury_lowl_probe,
  507. .thread_init = bifury_thread_init,
  508. .reinit_device = bifury_reinit,
  509. .thread_disable = bifury_trigger_send_clock,
  510. .thread_enable = bifury_trigger_send_clock,
  511. .minerloop = minerloop_queue,
  512. .queue_append = bifury_queue_append,
  513. .queue_flush = bifury_queue_flush,
  514. .poll = bifury_poll,
  515. .get_api_extra_device_status = bifury_api_device_status,
  516. .set_device = bifury_set_device,
  517. #ifdef HAVE_CURSES
  518. .proc_wlogprint_status = bifury_wlogprint_status,
  519. .proc_tui_wlogprint_choices = bifury_tui_wlogprint_choices,
  520. .proc_tui_handle_choice = bifury_tui_handle_choice,
  521. #endif
  522. };