driver-bifury.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. int major, minor, hwrev, chips;
  73. struct cgpu_info *cgpu;
  74. const int fd = serial_open(devpath, 0, 10, true);
  75. applog(LOG_DEBUG, "%s: %s %s",
  76. bifury_drv.dname,
  77. ((fd == -1) ? "Failed to open" : "Successfully opened"),
  78. devpath);
  79. if (unlikely(fd == -1))
  80. return false;
  81. while (read(fd, buf, sizeof(buf)) == sizeof(buf))
  82. {}
  83. if (8 != write(fd, "version\n", 8))
  84. {
  85. applog(LOG_DEBUG, "%s: Error sending version request", bifury_drv.dname);
  86. goto err;
  87. }
  88. if (read(fd, buf, sizeof(buf)) < 1 || strncmp("version ", buf, 8))
  89. {
  90. applog(LOG_DEBUG, "%s: Wrong response to version request: %s",
  91. bifury_drv.dname, buf);
  92. goto err;
  93. }
  94. serial_close(fd);
  95. major = strtol(&buf[8], &p, 10);
  96. if (p == &buf[8] || p[0] != '.')
  97. goto parseerr;
  98. minor = strtol(&p[1], &q, 10);
  99. if (p == q || strncmp(" rev ", q, 5))
  100. goto parseerr;
  101. hwrev = strtol(&q[5], &p, 10);
  102. if (p == q || strncmp(" chips ", p, 7))
  103. goto parseerr;
  104. chips = strtol(&p[7], &q, 10);
  105. if (p == q || chips < 1)
  106. goto parseerr;
  107. applog(LOG_DEBUG, "%s: Found firmware %d.%d on hardware rev %d with %d chips",
  108. bifury_drv.dname, major, minor, hwrev, chips);
  109. cgpu = malloc(sizeof(*cgpu));
  110. *cgpu = (struct cgpu_info){
  111. .drv = &bifury_drv,
  112. .device_path = strdup(devpath),
  113. .deven = DEV_ENABLED,
  114. .procs = chips,
  115. .threads = 1,
  116. // .cutofftemp = TODO,
  117. };
  118. // NOTE: Xcode's clang has a bug where it cannot find fields inside anonymous unions (more details in fpgautils)
  119. cgpu->device_fd = -1;
  120. return add_cgpu(cgpu);
  121. parseerr:
  122. applog(LOG_DEBUG, "%s: Error parsing version response", bifury_drv.dname);
  123. err:
  124. serial_close(fd);
  125. return false;
  126. }
  127. static
  128. bool bifury_lowl_probe(const struct lowlevel_device_info * const info)
  129. {
  130. return vcom_lowl_probe_wrapper(info, bifury_detect_one);
  131. }
  132. static
  133. bool bifury_set_queue_full(const struct cgpu_info * const dev, int needwork)
  134. {
  135. struct bifury_state * const state = dev->device_data;
  136. struct thr_info * const master_thr = dev->thr[0];
  137. const int fd = dev->device_fd;
  138. if (needwork != -1)
  139. state->needwork = needwork;
  140. const bool full = (fd == -1 || !state->needwork);
  141. if (full == master_thr->queue_full)
  142. return full;
  143. for (const struct cgpu_info *proc = dev; proc; proc = proc->next_proc)
  144. {
  145. struct thr_info * const thr = proc->thr[0];
  146. thr->queue_full = full;
  147. }
  148. return full;
  149. }
  150. static
  151. bool bifury_thread_init(struct thr_info *master_thr)
  152. {
  153. struct cgpu_info * const dev = master_thr->cgpu, *proc;
  154. struct bifury_state * const state = malloc(sizeof(*state));
  155. if (!state)
  156. return false;
  157. *state = (struct bifury_state){
  158. .buf = BYTES_INIT,
  159. };
  160. for (proc = dev; proc; proc = proc->next_proc)
  161. {
  162. proc->device_data = state;
  163. proc->status = LIFE_INIT2;
  164. }
  165. bifury_set_queue_full(dev, 0);
  166. timer_set_now(&master_thr->tv_poll);
  167. return true;
  168. }
  169. static
  170. void bifury_reinit(struct cgpu_info * const proc)
  171. {
  172. timer_set_now(&proc->thr[0]->tv_poll);
  173. }
  174. static
  175. void bifury_common_error(struct cgpu_info * const dev, const enum dev_reason reason)
  176. {
  177. for (struct cgpu_info *proc = dev; proc; proc = proc->next_proc)
  178. {
  179. struct thr_info * const thr = proc->thr[0];
  180. dev_error(proc, reason);
  181. inc_hw_errors_only(thr);
  182. }
  183. }
  184. static
  185. bool bifury_queue_append(struct thr_info * const thr, struct work * const work)
  186. {
  187. const struct cgpu_info * const dev = thr->cgpu->device;
  188. struct bifury_state * const state = dev->device_data;
  189. if (bifury_set_queue_full(dev, -1))
  190. return false;
  191. struct thr_info * const master_thr = dev->thr[0];
  192. char buf[5 + 0x98 + 1 + 8 + 1];
  193. memcpy(buf, "work ", 5);
  194. bin2hex(&buf[5], work->data, 0x4c);
  195. work->device_id = ++state->last_work_id;
  196. sprintf(&buf[5 + 0x98], " %08x", work->device_id);
  197. buf[5 + 0x98 + 1 + 8] = '\n';
  198. if (sizeof(buf) != bifury_write(dev, buf, sizeof(buf)))
  199. {
  200. applog(LOG_ERR, "%s: Failed to send work", dev->dev_repr);
  201. return false;
  202. }
  203. HASH_ADD(hh, master_thr->work_list, device_id, sizeof(work->device_id), work);
  204. bifury_set_queue_full(dev, state->needwork - 1);
  205. return true;
  206. }
  207. static
  208. void bifury_queue_flush(struct thr_info * const thr)
  209. {
  210. const struct cgpu_info *dev = thr->cgpu;
  211. if (dev != dev->device)
  212. return;
  213. const int fd = dev->device_fd;
  214. if (fd != -1)
  215. bifury_write(dev, "flush\n", 6);
  216. bifury_set_queue_full(dev, dev->procs);
  217. }
  218. static
  219. void bifury_handle_cmd(struct cgpu_info * const dev, const char * const cmd)
  220. {
  221. struct thr_info * const master_thr = dev->thr[0];
  222. struct bifury_state * const state = dev->device_data;
  223. struct cgpu_info *proc;
  224. struct thr_info *thr;
  225. struct work *work;
  226. char *p;
  227. if (!strncmp(cmd, "submit ", 7))
  228. {
  229. // submit <nonce> <jobid> <timestamp> <chip>
  230. uint32_t nonce = strtoll(&cmd[7], &p, 0x10);
  231. const uint32_t jobid = strtoll(&p[1], &p, 0x10);
  232. strtoll(&p[1], &p, 0x10); // Ignore timestamp for now
  233. const int chip = atoi(&p[1]);
  234. nonce = le32toh(nonce);
  235. proc = dev;
  236. for (int i = 0; i < chip; ++i)
  237. proc = proc->next_proc ?: proc;
  238. thr = proc->thr[0];
  239. HASH_FIND(hh, master_thr->work_list, &jobid, sizeof(jobid), work);
  240. if (work)
  241. submit_nonce(thr, work, nonce);
  242. else
  243. if (!jobid)
  244. applog(LOG_DEBUG, "%s: Dummy submit ignored", dev->dev_repr);
  245. else
  246. inc_hw_errors2(thr, NULL, &nonce);
  247. }
  248. else
  249. if (!strncmp(cmd, "temp ", 5))
  250. {
  251. const int decicelsius = atoi(&cmd[5]);
  252. if (decicelsius)
  253. {
  254. const float celsius = 0.1 * (float)decicelsius;
  255. for (proc = dev; proc; proc = proc->next_proc)
  256. proc->temp = celsius;
  257. }
  258. }
  259. else
  260. if (!strncmp(cmd, "jobs ", 5))
  261. {
  262. // jobs <n> ...
  263. // TODO
  264. }
  265. else
  266. if (!strncmp(cmd, "needwork ", 9))
  267. {
  268. const int needwork = atoi(&cmd[9]);
  269. bifury_set_queue_full(dev, needwork);
  270. applog(LOG_DEBUG, "%s: needwork=%d", dev->dev_repr, state->needwork);
  271. }
  272. }
  273. static
  274. void bifury_poll(struct thr_info * const master_thr)
  275. {
  276. struct cgpu_info * const dev = master_thr->cgpu;
  277. struct bifury_state * const state = dev->device_data;
  278. int fd = dev->device_fd;
  279. char *cmd;
  280. if (unlikely(fd == -1))
  281. {
  282. fd = serial_open(dev->device_path, 0, 1, true);
  283. if (unlikely(fd == -1))
  284. {
  285. applog(LOG_ERR, "%s: Failed to open %s",
  286. dev->dev_repr, dev->device_path);
  287. bifury_common_error(dev, REASON_THREAD_FAIL_INIT);
  288. return;
  289. }
  290. dev->device_fd = fd;
  291. if (sizeof(bifury_init_cmds)-1 != bifury_write(dev, bifury_init_cmds, sizeof(bifury_init_cmds)-1))
  292. {
  293. applog(LOG_ERR, "%s: Failed to send configuration", dev->dev_repr);
  294. bifury_common_error(dev, REASON_THREAD_FAIL_INIT);
  295. serial_close(fd);
  296. dev->device_fd = -1;
  297. return;
  298. }
  299. bifury_set_queue_full(dev, dev->procs);
  300. }
  301. while ( (cmd = bifury_readln(fd, &state->buf)) )
  302. {
  303. if (opt_dev_protocol)
  304. applog(LOG_DEBUG, "%s: DEVPROTO: RECV %s", dev->dev_repr, cmd);
  305. bifury_handle_cmd(dev, cmd);
  306. free(cmd);
  307. }
  308. }
  309. struct device_drv bifury_drv = {
  310. .dname = "bifury",
  311. .name = "BIF",
  312. .lowl_match = bifury_lowl_match,
  313. .lowl_probe = bifury_lowl_probe,
  314. .thread_init = bifury_thread_init,
  315. .reinit_device = bifury_reinit,
  316. .minerloop = minerloop_queue,
  317. .queue_append = bifury_queue_append,
  318. .queue_flush = bifury_queue_flush,
  319. .poll = bifury_poll,
  320. };