driver-bifury.c 8.4 KB

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