driver-bifury.c 16 KB

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