driver-bifury.c 9.7 KB

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