driver-bifury.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 "fpgautils.h"
  19. #include "logging.h"
  20. #include "lowlevel.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 size_t 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. };
  65. static
  66. bool bifury_lowl_match(const struct lowlevel_device_info * const info)
  67. {
  68. return lowlevel_match_product(info, "bi\xe2\x80\xa2""fury");
  69. }
  70. static
  71. bool bifury_detect_one(const char * const devpath)
  72. {
  73. char buf[0x40], *p, *q;
  74. bytes_t reply = BYTES_INIT;
  75. int major, minor, hwrev, chips;
  76. struct cgpu_info *cgpu;
  77. struct timeval tv_timeout;
  78. const int fd = serial_open(devpath, 0, 10, true);
  79. applog(LOG_DEBUG, "%s: %s %s",
  80. bifury_drv.dname,
  81. ((fd == -1) ? "Failed to open" : "Successfully opened"),
  82. devpath);
  83. if (unlikely(fd == -1))
  84. return false;
  85. while (read(fd, buf, sizeof(buf)) == sizeof(buf))
  86. {}
  87. if (opt_dev_protocol)
  88. applog(LOG_DEBUG, "%s fd=%d: DEVPROTO: SEND %s", bifury_drv.dname, fd, "version");
  89. if (8 != write(fd, "version\n", 8))
  90. {
  91. applog(LOG_DEBUG, "%s: Error sending version request", bifury_drv.dname);
  92. goto err;
  93. }
  94. timer_set_delay_from_now(&tv_timeout, 1000000);
  95. while (true)
  96. {
  97. p = bifury_readln(fd, &reply);
  98. if (p)
  99. {
  100. if (opt_dev_protocol)
  101. applog(LOG_DEBUG, "%s fd=%d: DEVPROTO: RECV %s",
  102. bifury_drv.dname, fd, p);
  103. if (!strncmp("version ", p, 8))
  104. break;
  105. free(p);
  106. }
  107. if (timer_passed(&tv_timeout, NULL))
  108. {
  109. applog(LOG_DEBUG, "%s: Timed out waiting for response to version request",
  110. bifury_drv.dname);
  111. goto err;
  112. }
  113. }
  114. bytes_free(&reply);
  115. serial_close(fd);
  116. major = strtol(&p[8], &p, 10);
  117. if (p == &buf[8] || p[0] != '.')
  118. goto parseerr;
  119. minor = strtol(&p[1], &q, 10);
  120. if (p == q || strncmp(" rev ", q, 5))
  121. goto parseerr;
  122. hwrev = strtol(&q[5], &p, 10);
  123. if (p == q || strncmp(" chips ", p, 7))
  124. goto parseerr;
  125. chips = strtol(&p[7], &q, 10);
  126. if (p == q || chips < 1)
  127. goto parseerr;
  128. applog(LOG_DEBUG, "%s: Found firmware %d.%d on hardware rev %d with %d chips",
  129. bifury_drv.dname, major, minor, hwrev, chips);
  130. cgpu = malloc(sizeof(*cgpu));
  131. *cgpu = (struct cgpu_info){
  132. .drv = &bifury_drv,
  133. .device_path = strdup(devpath),
  134. .deven = DEV_ENABLED,
  135. .procs = chips,
  136. .threads = 1,
  137. // .cutofftemp = TODO,
  138. };
  139. // NOTE: Xcode's clang has a bug where it cannot find fields inside anonymous unions (more details in fpgautils)
  140. cgpu->device_fd = -1;
  141. return add_cgpu(cgpu);
  142. parseerr:
  143. applog(LOG_DEBUG, "%s: Error parsing version response", bifury_drv.dname);
  144. err:
  145. bytes_free(&reply);
  146. serial_close(fd);
  147. return false;
  148. }
  149. static
  150. bool bifury_lowl_probe(const struct lowlevel_device_info * const info)
  151. {
  152. return vcom_lowl_probe_wrapper(info, bifury_detect_one);
  153. }
  154. static
  155. bool bifury_set_queue_full(const struct cgpu_info * const dev, int needwork)
  156. {
  157. struct bifury_state * const state = dev->device_data;
  158. struct thr_info * const master_thr = dev->thr[0];
  159. const int fd = dev->device_fd;
  160. if (needwork != -1)
  161. state->needwork = needwork;
  162. const bool full = (fd == -1 || !state->needwork);
  163. if (full == master_thr->queue_full)
  164. return full;
  165. for (const struct cgpu_info *proc = dev; proc; proc = proc->next_proc)
  166. {
  167. struct thr_info * const thr = proc->thr[0];
  168. thr->queue_full = full;
  169. }
  170. return full;
  171. }
  172. static
  173. bool bifury_thread_init(struct thr_info *master_thr)
  174. {
  175. struct cgpu_info * const dev = master_thr->cgpu, *proc;
  176. struct bifury_state * const state = malloc(sizeof(*state));
  177. if (!state)
  178. return false;
  179. *state = (struct bifury_state){
  180. .buf = BYTES_INIT,
  181. };
  182. for (proc = dev; proc; proc = proc->next_proc)
  183. {
  184. proc->device_data = state;
  185. proc->status = LIFE_INIT2;
  186. }
  187. bifury_set_queue_full(dev, 0);
  188. timer_set_now(&master_thr->tv_poll);
  189. return true;
  190. }
  191. static
  192. void bifury_reinit(struct cgpu_info * const proc)
  193. {
  194. timer_set_now(&proc->thr[0]->tv_poll);
  195. }
  196. static
  197. void bifury_common_error(struct cgpu_info * const dev, const enum dev_reason reason)
  198. {
  199. for (struct cgpu_info *proc = dev; proc; proc = proc->next_proc)
  200. {
  201. struct thr_info * const thr = proc->thr[0];
  202. dev_error(proc, reason);
  203. inc_hw_errors_only(thr);
  204. }
  205. }
  206. static
  207. bool bifury_queue_append(struct thr_info * const thr, struct work *work)
  208. {
  209. const struct cgpu_info * const dev = thr->cgpu->device;
  210. struct bifury_state * const state = dev->device_data;
  211. if (bifury_set_queue_full(dev, -1))
  212. return false;
  213. struct thr_info * const master_thr = dev->thr[0];
  214. char buf[5 + 0x98 + 1 + 8 + 1];
  215. memcpy(buf, "work ", 5);
  216. bin2hex(&buf[5], work->data, 0x4c);
  217. work->device_id = ++state->last_work_id;
  218. sprintf(&buf[5 + 0x98], " %08x", work->device_id);
  219. buf[5 + 0x98 + 1 + 8] = '\n';
  220. if (sizeof(buf) != bifury_write(dev, buf, sizeof(buf)))
  221. {
  222. applog(LOG_ERR, "%s: Failed to send work", dev->dev_repr);
  223. return false;
  224. }
  225. HASH_ADD(hh, master_thr->work_list, device_id, sizeof(work->device_id), work);
  226. int prunequeue = HASH_COUNT(master_thr->work_list) - BIFURY_MAX_QUEUED;
  227. if (prunequeue > 0)
  228. {
  229. struct work *tmp;
  230. applog(LOG_DEBUG, "%s: Pruning %d old work item%s",
  231. dev->dev_repr, prunequeue, prunequeue == 1 ? "" : "s");
  232. HASH_ITER(hh, master_thr->work_list, work, tmp)
  233. {
  234. HASH_DEL(master_thr->work_list, work);
  235. free_work(work);
  236. if (--prunequeue < 1)
  237. break;
  238. }
  239. }
  240. bifury_set_queue_full(dev, state->needwork - 1);
  241. return true;
  242. }
  243. static
  244. void bifury_queue_flush(struct thr_info * const thr)
  245. {
  246. const struct cgpu_info *dev = thr->cgpu;
  247. if (dev != dev->device)
  248. return;
  249. const int fd = dev->device_fd;
  250. if (fd != -1)
  251. bifury_write(dev, "flush\n", 6);
  252. bifury_set_queue_full(dev, dev->procs);
  253. }
  254. static
  255. const struct cgpu_info *device_proc_by_id(const struct cgpu_info * const dev, int procid)
  256. {
  257. const struct cgpu_info *proc = dev;
  258. for (int i = 0; i < procid; ++i)
  259. {
  260. proc = proc->next_proc;
  261. if (unlikely(!proc))
  262. return NULL;
  263. }
  264. return proc;
  265. }
  266. static
  267. void bifury_handle_cmd(struct cgpu_info * const dev, const char * const cmd)
  268. {
  269. struct thr_info * const master_thr = dev->thr[0];
  270. struct bifury_state * const state = dev->device_data;
  271. struct thr_info *thr;
  272. struct work *work;
  273. char *p;
  274. if (!strncmp(cmd, "submit ", 7))
  275. {
  276. // submit <nonce> <jobid> <timestamp> <chip>
  277. uint32_t nonce = strtoll(&cmd[7], &p, 0x10);
  278. const uint32_t jobid = strtoll(&p[1], &p, 0x10);
  279. const uint32_t ntime = strtoll(&p[1], &p, 0x10);
  280. const int chip = atoi(&p[1]);
  281. nonce = le32toh(nonce);
  282. const struct cgpu_info * const proc = device_proc_by_id(dev, chip);
  283. thr = proc->thr[0];
  284. HASH_FIND(hh, master_thr->work_list, &jobid, sizeof(jobid), work);
  285. if (work)
  286. {
  287. const uint32_t work_ntime = be32toh(*(uint32_t*)&work->data[68]);
  288. submit_noffset_nonce(thr, work, nonce, ntime - work_ntime);
  289. }
  290. else
  291. if (!jobid)
  292. applog(LOG_DEBUG, "%s: Dummy submit ignored", dev->dev_repr);
  293. else
  294. inc_hw_errors2(thr, NULL, &nonce);
  295. if (!state->has_needwork)
  296. bifury_set_queue_full(dev, state->needwork + 2);
  297. }
  298. else
  299. if (!strncmp(cmd, "temp ", 5))
  300. {
  301. struct cgpu_info *proc;
  302. const int decicelsius = atoi(&cmd[5]);
  303. if (decicelsius)
  304. {
  305. const float celsius = 0.1 * (float)decicelsius;
  306. for (proc = dev; proc; proc = proc->next_proc)
  307. proc->temp = celsius;
  308. }
  309. }
  310. else
  311. if (!strncmp(cmd, "job ", 4))
  312. {
  313. // job <jobid> <chip>
  314. const uint32_t jobid = strtoll(&cmd[4], &p, 0x10);
  315. const int chip = atoi(&p[1]);
  316. const struct cgpu_info * const proc = device_proc_by_id(dev, chip);
  317. thr = proc->thr[0];
  318. hashes_done2(thr, 0xbd000000, NULL);
  319. HASH_FIND(hh, master_thr->work_list, &jobid, sizeof(jobid), work);
  320. if (work)
  321. {
  322. HASH_DEL(master_thr->work_list, work);
  323. free_work(work);
  324. }
  325. }
  326. else
  327. if (!strncmp(cmd, "needwork ", 9))
  328. {
  329. const int needwork = atoi(&cmd[9]);
  330. state->has_needwork = true;
  331. bifury_set_queue_full(dev, needwork);
  332. applog(LOG_DEBUG, "%s: needwork=%d", dev->dev_repr, state->needwork);
  333. }
  334. }
  335. static
  336. void bifury_poll(struct thr_info * const master_thr)
  337. {
  338. struct cgpu_info * const dev = master_thr->cgpu;
  339. struct bifury_state * const state = dev->device_data;
  340. int fd = dev->device_fd;
  341. char *cmd;
  342. if (unlikely(fd == -1))
  343. {
  344. fd = serial_open(dev->device_path, 0, 1, true);
  345. if (unlikely(fd == -1))
  346. {
  347. applog(LOG_ERR, "%s: Failed to open %s",
  348. dev->dev_repr, dev->device_path);
  349. bifury_common_error(dev, REASON_THREAD_FAIL_INIT);
  350. return;
  351. }
  352. dev->device_fd = fd;
  353. if (sizeof(bifury_init_cmds)-1 != bifury_write(dev, bifury_init_cmds, sizeof(bifury_init_cmds)-1))
  354. {
  355. applog(LOG_ERR, "%s: Failed to send configuration", dev->dev_repr);
  356. bifury_common_error(dev, REASON_THREAD_FAIL_INIT);
  357. serial_close(fd);
  358. dev->device_fd = -1;
  359. return;
  360. }
  361. bifury_set_queue_full(dev, dev->procs * 2);
  362. }
  363. while ( (cmd = bifury_readln(fd, &state->buf)) )
  364. {
  365. if (opt_dev_protocol)
  366. applog(LOG_DEBUG, "%s: DEVPROTO: RECV %s", dev->dev_repr, cmd);
  367. bifury_handle_cmd(dev, cmd);
  368. free(cmd);
  369. }
  370. }
  371. struct device_drv bifury_drv = {
  372. .dname = "bifury",
  373. .name = "BIF",
  374. .lowl_match = bifury_lowl_match,
  375. .lowl_probe = bifury_lowl_probe,
  376. .thread_init = bifury_thread_init,
  377. .reinit_device = bifury_reinit,
  378. .minerloop = minerloop_queue,
  379. .queue_append = bifury_queue_append,
  380. .queue_flush = bifury_queue_flush,
  381. .poll = bifury_poll,
  382. };