driver-littlefury.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * Copyright 2013-2014 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 <string.h>
  13. #include <unistd.h>
  14. #include "deviceapi.h"
  15. #include "driver-bitfury.h"
  16. #include "libbitfury.h"
  17. #include "logging.h"
  18. #include "lowlevel.h"
  19. #include "lowl-spi.h"
  20. #include "lowl-vcom.h"
  21. #include "miner.h"
  22. #include "util.h"
  23. enum littlefury_opcode {
  24. LFOP_VERSION = 0,
  25. LFOP_SPI = 1,
  26. LFOP_REGVOLT = 2,
  27. LFOP_REGINFO = 3,
  28. LFOP_REGPWR = 4,
  29. LFOP_TEMP = 5,
  30. LFOP_LED = 6,
  31. LFOP_ADC = 7,
  32. };
  33. BFG_REGISTER_DRIVER(littlefury_drv)
  34. static
  35. ssize_t keep_reading(int prio, int fd, void *buf, size_t count)
  36. {
  37. ssize_t r, rv = 0;
  38. while (count)
  39. {
  40. r = read(fd, buf, count);
  41. if (unlikely(r <= 0))
  42. {
  43. applog(prio, "Read of fd %d returned %d", fd, (int)r);
  44. return rv ?: r;
  45. }
  46. rv += r;
  47. count -= r;
  48. buf += r;
  49. }
  50. return rv;
  51. }
  52. static
  53. bool bitfury_do_packet(int prio, const char *repr, const int fd, void * const buf, uint16_t * const bufsz, const uint8_t op, const void * const payload, const uint16_t payloadsz)
  54. {
  55. uint16_t crc;
  56. size_t sz;
  57. ssize_t r;
  58. uint8_t pkt[0x407];
  59. bool b;
  60. {
  61. sz = 2 + 1 + 2 + payloadsz + 2;
  62. pkt[0] = 0xab;
  63. pkt[1] = 0xcd;
  64. pkt[2] = op;
  65. pkt[3] = payloadsz >> 8;
  66. pkt[4] = payloadsz & 0xff;
  67. if (payloadsz)
  68. memcpy(&pkt[5], payload, payloadsz);
  69. crc = crc16ffff(&pkt[2], 3 + (size_t)payloadsz);
  70. pkt[sz - 2] = crc >> 8;
  71. pkt[sz - 1] = crc & 0xff;
  72. if (unlikely(opt_dev_protocol))
  73. {
  74. char hex[(sz * 2) + 1];
  75. bin2hex(hex, pkt, sz);
  76. applog(LOG_DEBUG, "%s: DEVPROTO: SEND %s", repr, hex);
  77. }
  78. r = write(fd, pkt, sz);
  79. if (sz != r)
  80. {
  81. applog(prio, "%s: Failed to write packet (%d bytes succeeded)", repr, (int)r);
  82. return false;
  83. }
  84. }
  85. {
  86. r = keep_reading(prio, fd, pkt, 5);
  87. if (5 != r || pkt[0] != 0xab || pkt[1] != 0xcd || pkt[2] != op)
  88. {
  89. char hex[(r * 2) + 1];
  90. bin2hex(hex, pkt, r);
  91. applog(prio, "%s: DEVPROTO: RECV %s", repr, hex);
  92. applog(prio, "%s: Failed to read correct packet header", repr);
  93. return false;
  94. }
  95. sz = (((unsigned)pkt[3] << 8) | pkt[4]) + 2;
  96. r = keep_reading(prio, fd, &pkt[5], sz);
  97. if (sz != r)
  98. {
  99. r += 5;
  100. char hex[(r * 2) + 1];
  101. bin2hex(hex, pkt, r);
  102. applog(prio, "%s: DEVPROTO: RECV %s", repr, hex);
  103. applog(prio, "%s: Failed to read packet payload (len=%d)", repr, (int)sz);
  104. return false;
  105. }
  106. crc = (pkt[sz + 3] << 8) | pkt[sz + 4];
  107. b = (crc != crc16ffff(&pkt[2], sz + 1));
  108. if (unlikely(opt_dev_protocol || b))
  109. {
  110. char hex[((sz + 5) * 2) + 1];
  111. bin2hex(hex, pkt, sz + 5);
  112. applog(b ? prio : LOG_DEBUG, "%s: DEVPROTO: RECV %s", repr, hex);
  113. if (b)
  114. {
  115. applog(prio, "%s: Packet checksum mismatch", repr);
  116. return false;
  117. }
  118. }
  119. sz -= 2;
  120. memcpy(buf, &pkt[5], (*bufsz < sz ? *bufsz : sz));
  121. *bufsz = sz;
  122. }
  123. return true;
  124. }
  125. static
  126. bool littlefury_set_power(const int loglev, const char * const repr, const int fd, const bool power)
  127. {
  128. const uint8_t pflg = (power ? '\1' : '\0');
  129. uint8_t buf[1] = { pflg };
  130. uint16_t bufsz = 1;
  131. return bitfury_do_packet(loglev, repr, fd, buf, &bufsz, LFOP_REGPWR, buf, 1) && bufsz && (buf[0] == pflg);
  132. }
  133. static
  134. bool littlefury_txrx(struct spi_port *port)
  135. {
  136. const struct cgpu_info * const cgpu = port->cgpu;
  137. const void *wrbuf = spi_gettxbuf(port);
  138. void *rdbuf = spi_getrxbuf(port);
  139. size_t bufsz = spi_getbufsz(port);
  140. uint16_t rbufsz, xfer;
  141. const int logprio = port->logprio;
  142. const char * const repr = port->repr;
  143. const int fd = cgpu->device->device_fd;
  144. if (unlikely(fd == -1))
  145. return false;
  146. rbufsz = 1;
  147. if (!bitfury_do_packet(logprio, repr, fd, rdbuf, &rbufsz, LFOP_SPI, NULL, 0))
  148. {
  149. littlefury_set_power(LOG_DEBUG, cgpu->dev_repr, fd, false);
  150. serial_close(fd);
  151. cgpu->device->device_fd = -1;
  152. return false;
  153. }
  154. while (bufsz)
  155. {
  156. xfer = (bufsz > 1024) ? 1024 : bufsz;
  157. rbufsz = xfer;
  158. if (!bitfury_do_packet(logprio, repr, fd, rdbuf, &rbufsz, LFOP_SPI, wrbuf, xfer))
  159. return false;
  160. if (rbufsz < xfer)
  161. {
  162. applog(port->logprio, "%s: SPI: Got fewer bytes back than sent (%d < %d)",
  163. repr, rbufsz, xfer);
  164. return false;
  165. }
  166. bufsz -= xfer;
  167. rdbuf += xfer;
  168. wrbuf += xfer;
  169. }
  170. return true;
  171. }
  172. static
  173. bool littlefury_lowl_match(const struct lowlevel_device_info * const info)
  174. {
  175. return lowlevel_match_product(info, "LittleFury");
  176. }
  177. static
  178. int littlefury_chip_count(struct cgpu_info * const info)
  179. {
  180. /* Do not allocate spi_port on the stack! OS X, at least, has a 512 KB default stack size for secondary threads */
  181. struct spi_port *spi = malloc(sizeof(*spi));
  182. spi->txrx = littlefury_txrx;
  183. spi->cgpu = info;
  184. spi->repr = littlefury_drv.dname;
  185. spi->logprio = LOG_DEBUG;
  186. const int chip_count = libbitfury_detectChips1(spi);
  187. free(spi);
  188. return chip_count;
  189. }
  190. static
  191. bool littlefury_detect_one(const char *devpath)
  192. {
  193. int fd, chips;
  194. uint8_t buf[255];
  195. uint16_t bufsz;
  196. struct cgpu_info dummy;
  197. char *devname = NULL;
  198. fd = serial_open(devpath, 0, 10, true);
  199. applog(LOG_DEBUG, "%s: %s %s",
  200. littlefury_drv.dname,
  201. ((fd == -1) ? "Failed to open" : "Successfully opened"),
  202. devpath);
  203. if (unlikely(fd == -1))
  204. goto err;
  205. bufsz = sizeof(buf);
  206. if (!bitfury_do_packet(LOG_DEBUG, littlefury_drv.dname, fd, buf, &bufsz, LFOP_VERSION, NULL, 0))
  207. goto err;
  208. if (bufsz < 4)
  209. {
  210. applog(LOG_DEBUG, "%s: Incomplete version response", littlefury_drv.dname);
  211. goto err;
  212. }
  213. devname = malloc(bufsz - 3);
  214. memcpy(devname, (char*)&buf[4], bufsz - 4);
  215. devname[bufsz - 4] = '\0';
  216. applog(LOG_DEBUG, "%s: Identified %s %d.%d.%d (features %02x)",
  217. littlefury_drv.dname, devname, buf[0], buf[1], buf[2], buf[3]);
  218. if (!littlefury_set_power(LOG_DEBUG, littlefury_drv.dname, fd, true))
  219. applog(LOG_WARNING, "%s: Unable to power on chip(s) for %s",
  220. littlefury_drv.dname, devpath);
  221. dummy.device = &dummy;
  222. dummy.device_fd = fd;
  223. chips = littlefury_chip_count(&dummy);
  224. if (!chips) {
  225. applog(LOG_WARNING, "%s: No Bitfury chips detected on %s",
  226. littlefury_drv.dname, devpath);
  227. goto err;
  228. } else {
  229. applog(LOG_DEBUG, "%s: %d chips detected",
  230. littlefury_drv.dname, chips);
  231. }
  232. littlefury_set_power(LOG_DEBUG, littlefury_drv.dname, fd, false);
  233. if (serial_claim_v(devpath, &littlefury_drv))
  234. goto err;
  235. serial_close(fd);
  236. struct cgpu_info *cgpu;
  237. cgpu = malloc(sizeof(*cgpu));
  238. *cgpu = (struct cgpu_info){
  239. .drv = &littlefury_drv,
  240. .set_device_funcs = bitfury_set_device_funcs,
  241. .device_path = strdup(devpath),
  242. .deven = DEV_ENABLED,
  243. .procs = chips,
  244. .threads = 1,
  245. .name = devname,
  246. .cutofftemp = 85,
  247. };
  248. // NOTE: Xcode's clang has a bug where it cannot find fields inside anonymous unions (more details in fpgautils)
  249. cgpu->device_fd = -1;
  250. return add_cgpu(cgpu);
  251. err:
  252. if (fd != -1)
  253. serial_close(fd);
  254. free(devname);
  255. return false;
  256. }
  257. static
  258. bool littlefury_lowl_probe(const struct lowlevel_device_info * const info)
  259. {
  260. return vcom_lowl_probe_wrapper(info, littlefury_detect_one);
  261. }
  262. struct littlefury_state {
  263. int chips_enabled;
  264. bool powered;
  265. };
  266. static
  267. bool littlefury_thread_init(struct thr_info *thr)
  268. {
  269. struct cgpu_info * const cgpu = thr->cgpu;
  270. struct cgpu_info *proc;
  271. struct spi_port *spi;
  272. struct bitfury_device *bitfury;
  273. struct littlefury_state * const lfstate = malloc(sizeof(*lfstate));
  274. int i = 0;
  275. *lfstate = (struct littlefury_state){
  276. .chips_enabled = 0,
  277. };
  278. for (proc = cgpu; proc; proc = proc->next_proc)
  279. {
  280. struct thr_info * const proc_thr = proc->thr[0];
  281. spi = malloc(sizeof(*spi));
  282. /* Be careful, read lowl-spi.h comments for warnings */
  283. memset(spi, 0, sizeof(*spi));
  284. spi->txrx = littlefury_txrx;
  285. spi->cgpu = proc;
  286. spi->repr = proc->proc_repr;
  287. spi->logprio = LOG_ERR;
  288. bitfury = malloc(sizeof(*bitfury));
  289. *bitfury = (struct bitfury_device){
  290. .spi = spi,
  291. .fasync = i++,
  292. };
  293. proc->device_data = bitfury;
  294. bitfury->osc6_bits = 50;
  295. proc_thr->cgpu_data = lfstate;
  296. ++lfstate->chips_enabled;
  297. }
  298. timer_set_now(&thr->tv_poll);
  299. cgpu->status = LIFE_INIT2;
  300. return true;
  301. }
  302. static void littlefury_common_error(struct cgpu_info *, enum dev_reason);
  303. static
  304. bool littlefury_power_on(struct cgpu_info * const dev)
  305. {
  306. struct thr_info * const master_thr = dev->thr[0];
  307. struct littlefury_state * const lfstate = master_thr->cgpu_data;
  308. applog(LOG_DEBUG, "%s: Turning power on", dev->dev_repr);
  309. if (!littlefury_set_power(LOG_WARNING, dev->dev_repr, dev->device_fd, true))
  310. {
  311. applog(LOG_ERR, "%s: Unable to power on chip(s)", dev->dev_repr);
  312. littlefury_common_error(dev, REASON_THREAD_FAIL_INIT);
  313. serial_close(dev->device_fd);
  314. dev->device_fd = -1;
  315. lfstate->powered = false;
  316. return false;
  317. }
  318. lfstate->powered = true;
  319. return true;
  320. }
  321. static
  322. void littlefury_chip_init(struct cgpu_info * const proc)
  323. {
  324. struct bitfury_device * const bitfury = proc->device_data;
  325. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  326. bitfury_init_chip(proc);
  327. }
  328. static
  329. void littlefury_disable(struct thr_info * const thr)
  330. {
  331. struct cgpu_info *proc = thr->cgpu;
  332. struct cgpu_info * const dev = proc->device;
  333. struct littlefury_state * const lfstate = thr->cgpu_data;
  334. bitfury_disable(thr);
  335. // If all chips disabled, kill power and close device
  336. if (!--lfstate->chips_enabled)
  337. {
  338. applog(LOG_DEBUG, "%s: 0 chips enabled, turning off power", dev->dev_repr);
  339. lfstate->powered = false;
  340. if (!littlefury_set_power(LOG_ERR, dev->dev_repr, dev->device_fd, false))
  341. applog(LOG_WARNING, "%s: Unable to power off chip(s)", dev->dev_repr);
  342. serial_close(dev->device_fd);
  343. dev->device_fd = -1;
  344. timer_unset(&dev->thr[0]->tv_poll);
  345. }
  346. else
  347. applog(LOG_DEBUG, "%s: %d chips enabled, power remains on", dev->dev_repr, lfstate->chips_enabled);
  348. }
  349. static
  350. void littlefury_enable(struct thr_info * const thr)
  351. {
  352. struct cgpu_info *proc = thr->cgpu;
  353. struct cgpu_info * const dev = proc->device;
  354. struct thr_info * const master_thr = dev->thr[0];
  355. struct littlefury_state * const lfstate = thr->cgpu_data;
  356. ++lfstate->chips_enabled;
  357. if (dev->device_fd != -1 && !lfstate->powered)
  358. littlefury_power_on(dev);
  359. if (dev->device_fd != -1)
  360. {
  361. struct bitfury_device * const bitfury = proc->device_data;
  362. bitfury_send_reinit(bitfury->spi, bitfury->slot, bitfury->fasync, bitfury->osc6_bits);
  363. bitfury_init_chip(proc);
  364. }
  365. if (!timer_isset(&master_thr->tv_poll))
  366. timer_set_now(&master_thr->tv_poll);
  367. }
  368. static void littlefury_shutdown(struct thr_info *thr)
  369. {
  370. struct cgpu_info * const cgpu = thr->cgpu;
  371. const int fd = cgpu->device->device_fd;
  372. bitfury_shutdown(thr);
  373. if (!littlefury_set_power(LOG_ERR, cgpu->dev_repr, fd, false))
  374. applog(LOG_WARNING, "%s: Unable to power off chip(s)", cgpu->dev_repr);
  375. }
  376. static
  377. void littlefury_common_error(struct cgpu_info * const dev, const enum dev_reason reason)
  378. {
  379. for (struct cgpu_info *proc = dev; proc; proc = proc->next_proc)
  380. {
  381. struct thr_info * const thr = proc->thr[0];
  382. dev_error(proc, reason);
  383. inc_hw_errors_only(thr);
  384. }
  385. }
  386. static
  387. void littlefury_poll(struct thr_info * const master_thr)
  388. {
  389. struct cgpu_info * const dev = master_thr->cgpu, *proc;
  390. struct littlefury_state * const lfstate = master_thr->cgpu_data;
  391. int fd = dev->device_fd;
  392. if (unlikely(fd == -1))
  393. {
  394. fd = serial_open(dev->device_path, 0, 10, true);
  395. if (unlikely(fd == -1))
  396. {
  397. applog(LOG_ERR, "%s: Failed to open %s",
  398. dev->dev_repr, dev->device_path);
  399. littlefury_common_error(dev, REASON_THREAD_FAIL_INIT);
  400. return;
  401. }
  402. dev->device_fd = fd;
  403. lfstate->powered = false;
  404. }
  405. if (unlikely(!lfstate->powered))
  406. {
  407. if (!littlefury_power_on(dev))
  408. return;
  409. for (proc = dev; proc; proc = proc->next_proc)
  410. {
  411. if (proc->deven != DEV_ENABLED || proc->thr[0]->pause)
  412. continue;
  413. littlefury_chip_init(proc);
  414. }
  415. }
  416. return bitfury_do_io(master_thr);
  417. }
  418. static
  419. void littlefury_reinit(struct cgpu_info * const proc)
  420. {
  421. timer_set_now(&proc->thr[0]->tv_poll);
  422. }
  423. struct device_drv littlefury_drv = {
  424. .dname = "littlefury",
  425. .name = "LFY",
  426. .lowl_match = littlefury_lowl_match,
  427. .lowl_probe = littlefury_lowl_probe,
  428. .thread_init = littlefury_thread_init,
  429. .thread_disable = littlefury_disable,
  430. .thread_enable = littlefury_enable,
  431. .reinit_device = littlefury_reinit,
  432. .thread_shutdown = littlefury_shutdown,
  433. .minerloop = minerloop_async,
  434. .job_prepare = bitfury_job_prepare,
  435. .job_start = bitfury_noop_job_start,
  436. .poll = littlefury_poll,
  437. .job_process_results = bitfury_job_process_results,
  438. .get_api_extra_device_detail = bitfury_api_device_detail,
  439. .get_api_extra_device_status = bitfury_api_device_status,
  440. #ifdef HAVE_CURSES
  441. .proc_wlogprint_status = bitfury_wlogprint_status,
  442. .proc_tui_wlogprint_choices = bitfury_tui_wlogprint_choices,
  443. .proc_tui_handle_choice = bitfury_tui_handle_choice,
  444. #endif
  445. };