driver-bifury.c 8.5 KB

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