driver-hashfast.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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 <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <utlist.h>
  16. #include "deviceapi.h"
  17. #include "logging.h"
  18. #include "lowlevel.h"
  19. #include "lowl-vcom.h"
  20. #include "util.h"
  21. BFG_REGISTER_DRIVER(hashfast_ums_drv)
  22. #define HASHFAST_QUEUE_MEMORY 0x20
  23. #define HASHFAST_ALL_CHIPS 0xff
  24. #define HASHFAST_ALL_CORES 0xff
  25. #define HASHFAST_HEADER_SIZE 8
  26. #define HASHFAST_MAX_DATA 0x3fc
  27. #define HASHFAST_HASH_SIZE (0x20 + 0xc + 4 + 4 + 2 + 1 + 1)
  28. #define HASHFAST_MAX_VOLTAGES 4
  29. enum hashfast_opcode {
  30. HFOP_NULL = 0,
  31. HFOP_ROOT = 1,
  32. HFOP_RESET = 2,
  33. HFOP_PLL_CONFIG = 3,
  34. HFOP_ADDRESS = 4,
  35. HFOP_READDRESS = 5,
  36. HFOP_HIGHEST = 6,
  37. HFOP_BAUD = 7,
  38. HFOP_UNROOT = 8,
  39. HFOP_HASH = 9,
  40. HFOP_NONCE = 0x0a,
  41. HFOP_ABORT = 0x0b,
  42. HFOP_STATUS = 0x0c,
  43. HFOP_GPIO = 0x0d,
  44. HFOP_CONFIG = 0x0e,
  45. HFOP_STATISTICS = 0x0f,
  46. HFOP_GROUP = 0x10,
  47. HFOP_CLOCKGATE = 0x11,
  48. HFOP_USB_INIT = 0x80,
  49. HFOP_GET_TRACE = 0x81,
  50. HFOP_LOOPBACK_USB = 0x82,
  51. HFOP_LOOPBACK_UART = 0x83,
  52. HFOP_DFU = 0x84,
  53. HFOP_USB_SHUTDOWN = 0x85,
  54. HFOP_DIE_STATUS = 0x86,
  55. HFOP_GWQ_STATUS = 0x87,
  56. HFOP_WORK_RESTART = 0x88,
  57. HFOP_USB_STATS1 = 0x89,
  58. HFOP_USB_GWQSTATS = 0x8a,
  59. HFOP_USB_NOTICE = 0x8b,
  60. HFOP_USB_DEBUG = 0xff,
  61. };
  62. typedef unsigned long hashfast_isn_t;
  63. static inline
  64. float hashfast_temperature_conv(const uint8_t * const data)
  65. {
  66. // Temperature is 12-bit fraction ranging between -61.5 C and ~178.5 C
  67. int32_t tempdata = ((uint32_t)data[1] << 8) | data[0];
  68. tempdata &= 0xfff;
  69. tempdata *= 240;
  70. tempdata -= 251904; // 61.5 * 4096
  71. float temp = tempdata;
  72. temp /= 4096.;
  73. return temp;
  74. }
  75. static inline
  76. float hashfast_voltage_conv(const uint8_t vdata)
  77. {
  78. // Voltage is 8-bit fraction ranging between 0 V and ~1.2 V
  79. return (float)vdata / 256. * 1.2;
  80. }
  81. struct hashfast_parsed_msg {
  82. uint8_t opcode;
  83. uint8_t chipaddr;
  84. uint8_t coreaddr;
  85. uint16_t hdata;
  86. uint8_t data[HASHFAST_MAX_DATA];
  87. size_t datalen;
  88. };
  89. static
  90. ssize_t hashfast_write(const int fd, void * const buf, size_t bufsz)
  91. {
  92. const ssize_t rv = write(fd, buf, bufsz);
  93. if ((opt_debug && opt_dev_protocol) || unlikely(rv != bufsz))
  94. {
  95. const int e = errno;
  96. char hex[(bufsz * 2) + 1];
  97. bin2hex(hex, buf, bufsz);
  98. if (rv < 0)
  99. applog(LOG_WARNING, "%s fd=%d: SEND (%s) => %d errno=%d(%s)",
  100. "hashfast", fd, hex, (int)rv, e, bfg_strerror(e, BST_ERRNO));
  101. else
  102. if (rv < bufsz)
  103. applog(LOG_WARNING, "%s fd=%d: SEND %.*s(%s)",
  104. "hashfast", fd, (int)(rv * 2), hex, &hex[rv * 2]);
  105. else
  106. if (rv > bufsz)
  107. applog(LOG_WARNING, "%s fd=%d: SEND %s => +%d",
  108. "hashfast", fd, hex, (int)(rv - bufsz));
  109. else
  110. applog(LOG_DEBUG, "%s fd=%d: SEND %s",
  111. "hashfast", fd, hex);
  112. }
  113. return rv;
  114. }
  115. static
  116. ssize_t hashfast_read(const int fd, void * const buf, size_t bufsz)
  117. {
  118. const ssize_t rv = serial_read(fd, buf, bufsz);
  119. if (opt_debug && opt_dev_protocol && rv)
  120. {
  121. char hex[(rv * 2) + 1];
  122. bin2hex(hex, buf, rv);
  123. applog(LOG_DEBUG, "%s fd=%d: RECV %s",
  124. "hashfast", fd, hex);
  125. }
  126. return rv;
  127. }
  128. static
  129. bool hashfast_prepare_msg(uint8_t * const buf, const uint8_t opcode, const uint8_t chipaddr, const uint8_t coreaddr, const uint16_t hdata, const size_t datalen)
  130. {
  131. buf[0] = '\xaa';
  132. buf[1] = opcode;
  133. buf[2] = chipaddr;
  134. buf[3] = coreaddr;
  135. buf[4] = hdata & 0xff;
  136. buf[5] = hdata >> 8;
  137. if (datalen > 1020 || datalen % 4)
  138. return false;
  139. buf[6] = datalen / 4;
  140. buf[7] = crc8ccitt(&buf[1], 6);
  141. return true;
  142. }
  143. static
  144. bool hashfast_send_msg(const int fd, uint8_t * const buf, const uint8_t opcode, const uint8_t chipaddr, const uint8_t coreaddr, const uint16_t hdata, const size_t datalen)
  145. {
  146. if (!hashfast_prepare_msg(buf, opcode, chipaddr, coreaddr, hdata, datalen))
  147. return false;
  148. const size_t buflen = HASHFAST_HEADER_SIZE + datalen;
  149. return (buflen == hashfast_write(fd, buf, buflen));
  150. }
  151. static
  152. bool hashfast_parse_msg(const int fd, struct hashfast_parsed_msg * const out_msg)
  153. {
  154. uint8_t buf[HASHFAST_HEADER_SIZE];
  155. startover:
  156. if (HASHFAST_HEADER_SIZE != hashfast_read(fd, buf, HASHFAST_HEADER_SIZE))
  157. return false;
  158. uint8_t *p = memchr(buf, '\xaa', HASHFAST_HEADER_SIZE);
  159. if (p != buf)
  160. {
  161. ignoresome:
  162. if (!p)
  163. goto startover;
  164. int moreneeded = p - buf;
  165. int alreadyhave = HASHFAST_HEADER_SIZE - moreneeded;
  166. memmove(buf, p, alreadyhave);
  167. if (moreneeded != hashfast_read(fd, &buf[alreadyhave], moreneeded))
  168. return false;
  169. }
  170. const uint8_t correct_crc8 = crc8ccitt(&buf[1], 6);
  171. if (buf[7] != correct_crc8)
  172. {
  173. p = memchr(&buf[1], '\xaa', HASHFAST_HEADER_SIZE - 1);
  174. goto ignoresome;
  175. }
  176. out_msg->opcode = buf[1];
  177. out_msg->chipaddr = buf[2];
  178. out_msg->coreaddr = buf[3];
  179. out_msg->hdata = (uint16_t)buf[4] | ((uint16_t)buf[5] << 8);
  180. out_msg->datalen = buf[6] * 4;
  181. return (out_msg->datalen == hashfast_read(fd, &out_msg->data[0], out_msg->datalen));
  182. }
  183. static
  184. bool hashfast_lowl_match(const struct lowlevel_device_info * const info)
  185. {
  186. if (lowlevel_match_product(info, "GoldenNonce"))
  187. return true; UDEV
  188. return (info->manufacturer && strstr(info->manufacturer, "HashFast"));
  189. }
  190. static
  191. const char *hashfast_set_clock(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  192. {
  193. uint16_t * const clockp = proc->device_data;
  194. *clockp = atoi(newvalue);
  195. return NULL;
  196. }
  197. static const struct bfg_set_device_definition hashfast_set_device_funcs_probe[] = {
  198. {"clock", hashfast_set_clock, "clock frequency (can only be set at startup, with --set-device)"},
  199. {NULL},
  200. };
  201. static
  202. bool hashfast_detect_one(const char * const devpath)
  203. {
  204. uint16_t clock = 550;
  205. uint8_t buf[HASHFAST_HEADER_SIZE];
  206. const int fd = serial_open(devpath, 0, 100, true);
  207. if (fd == -1)
  208. {
  209. applog(LOG_DEBUG, "%s: Failed to open %s", __func__, devpath);
  210. return false;
  211. }
  212. struct hashfast_parsed_msg * const pmsg = malloc(sizeof(*pmsg));
  213. drv_set_defaults(&hashfast_ums_drv, hashfast_set_device_funcs_probe, &clock, devpath, detectone_meta_info.serial, 1);
  214. hashfast_send_msg(fd, buf, HFOP_USB_INIT, 0, 0, clock, 0);
  215. do {
  216. if (!hashfast_parse_msg(fd, pmsg))
  217. {
  218. applog(LOG_DEBUG, "%s: Failed to parse response on %s",
  219. __func__, devpath);
  220. serial_close(fd);
  221. goto err;
  222. }
  223. } while (pmsg->opcode != HFOP_USB_INIT);
  224. serial_close(fd);
  225. const int expectlen = 0x20 + (pmsg->chipaddr * pmsg->coreaddr) / 8;
  226. if (pmsg->datalen < expectlen)
  227. {
  228. applog(LOG_DEBUG, "%s: USB_INIT response too short on %s (%d < %d)",
  229. __func__, devpath, (int)pmsg->datalen, expectlen);
  230. goto err;
  231. }
  232. if (pmsg->data[8] != 0)
  233. {
  234. applog(LOG_DEBUG, "%s: USB_INIT failed on %s (err=%d)",
  235. __func__, devpath, pmsg->data[8]);
  236. goto err;
  237. }
  238. if (serial_claim_v(devpath, &hashfast_ums_drv))
  239. return false;
  240. struct cgpu_info * const cgpu = malloc(sizeof(*cgpu));
  241. *cgpu = (struct cgpu_info){
  242. .drv = &hashfast_ums_drv,
  243. .device_path = strdup(devpath),
  244. .deven = DEV_ENABLED,
  245. .procs = (pmsg->chipaddr * pmsg->coreaddr),
  246. .threads = 1,
  247. .device_data = pmsg,
  248. .cutofftemp = 100,
  249. };
  250. return add_cgpu(cgpu);
  251. err:
  252. free(pmsg);
  253. return false;
  254. }
  255. static
  256. bool hashfast_lowl_probe(const struct lowlevel_device_info * const info)
  257. {
  258. return vcom_lowl_probe_wrapper(info, hashfast_detect_one);
  259. }
  260. struct hashfast_dev_state {
  261. uint8_t cores_per_chip;
  262. int fd;
  263. struct hashfast_chip_state *chipstates;
  264. };
  265. struct hashfast_chip_state {
  266. struct cgpu_info **coreprocs;
  267. hashfast_isn_t last_isn;
  268. float voltages[HASHFAST_MAX_VOLTAGES];
  269. };
  270. struct hashfast_core_state {
  271. uint8_t chipaddr;
  272. uint8_t coreaddr;
  273. int next_device_id;
  274. uint8_t last_seq;
  275. hashfast_isn_t last_isn;
  276. hashfast_isn_t last2_isn;
  277. bool has_pending;
  278. unsigned queued;
  279. };
  280. static
  281. bool hashfast_init(struct thr_info * const master_thr)
  282. {
  283. struct cgpu_info * const dev = master_thr->cgpu, *proc;
  284. struct hashfast_parsed_msg * const pmsg = dev->device_data;
  285. struct hashfast_dev_state * const devstate = malloc(sizeof(*devstate));
  286. struct hashfast_chip_state * const chipstates = malloc(sizeof(*chipstates) * pmsg->chipaddr), *chipstate;
  287. struct hashfast_core_state * const corestates = malloc(sizeof(*corestates) * dev->procs), *cs;
  288. int i;
  289. *devstate = (struct hashfast_dev_state){
  290. .chipstates = chipstates,
  291. .cores_per_chip = pmsg->coreaddr,
  292. .fd = serial_open(dev->device_path, 0, 1, true),
  293. };
  294. for (i = 0; i < pmsg->chipaddr; ++i)
  295. {
  296. chipstate = &chipstates[i];
  297. *chipstate = (struct hashfast_chip_state){
  298. .coreprocs = malloc(sizeof(struct cgpu_info *) * pmsg->coreaddr),
  299. };
  300. }
  301. for ((i = 0), (proc = dev); proc; ++i, (proc = proc->next_proc))
  302. {
  303. struct thr_info * const thr = proc->thr[0];
  304. const bool core_is_working = pmsg->data[0x20 + (i / 8)] & (1 << (i % 8));
  305. if (!core_is_working)
  306. proc->deven = DEV_RECOVER_DRV;
  307. proc->device_data = devstate;
  308. thr->cgpu_data = cs = &corestates[i];
  309. *cs = (struct hashfast_core_state){
  310. .chipaddr = i / pmsg->coreaddr,
  311. .coreaddr = i % pmsg->coreaddr,
  312. };
  313. chipstates[cs->chipaddr].coreprocs[cs->coreaddr] = proc;
  314. }
  315. free(pmsg);
  316. // TODO: actual clock = [12,13]
  317. for_each_managed_proc(proc, dev)
  318. {
  319. proc->status = LIFE_INIT2;
  320. }
  321. timer_set_now(&master_thr->tv_poll);
  322. return true;
  323. }
  324. static
  325. bool hashfast_queue_append(struct thr_info * const thr, struct work * const work)
  326. {
  327. struct cgpu_info * const proc = thr->cgpu;
  328. struct hashfast_dev_state * const devstate = proc->device_data;
  329. const int fd = devstate->fd;
  330. struct hashfast_core_state * const cs = thr->cgpu_data;
  331. struct hashfast_chip_state * const chipstate = &devstate->chipstates[cs->chipaddr];
  332. const size_t cmdlen = HASHFAST_HEADER_SIZE + HASHFAST_HASH_SIZE;
  333. uint8_t cmd[cmdlen];
  334. uint8_t * const hashdata = &cmd[HASHFAST_HEADER_SIZE];
  335. hashfast_isn_t isn;
  336. uint8_t seq;
  337. if (cs->has_pending)
  338. {
  339. thr->queue_full = true;
  340. return false;
  341. }
  342. isn = ++chipstate->last_isn;
  343. seq = ++cs->last_seq;
  344. work->device_id = seq;
  345. cs->last2_isn = cs->last_isn;
  346. cs->last_isn = isn;
  347. hashfast_prepare_msg(cmd, HFOP_HASH, cs->chipaddr, cs->coreaddr, (cs->coreaddr << 8) | seq, 56);
  348. memcpy(&hashdata[ 0], work->midstate, 0x20);
  349. memcpy(&hashdata[0x20], &work->data[64], 0xc);
  350. memset(&hashdata[0x2c], '\0', 0xa); // starting_nonce, nonce_loops, ntime_loops
  351. hashdata[0x36] = 32; // search target (number of zero bits)
  352. hashdata[0x37] = 0;
  353. cs->has_pending = true;
  354. if (cmdlen != hashfast_write(fd, cmd, cmdlen))
  355. return false;
  356. DL_APPEND(thr->work, work);
  357. if (cs->queued > HASHFAST_QUEUE_MEMORY)
  358. {
  359. struct work * const old_work = thr->work;
  360. DL_DELETE(thr->work, old_work);
  361. free_work(old_work);
  362. }
  363. else
  364. ++cs->queued;
  365. return true;
  366. }
  367. static
  368. void hashfast_queue_flush(struct thr_info * const thr)
  369. {
  370. struct cgpu_info * const proc = thr->cgpu;
  371. struct hashfast_dev_state * const devstate = proc->device_data;
  372. const int fd = devstate->fd;
  373. struct hashfast_core_state * const cs = thr->cgpu_data;
  374. uint8_t cmd[HASHFAST_HEADER_SIZE];
  375. uint16_t hdata = 2;
  376. if ((!thr->work) || stale_work(thr->work->prev, true))
  377. {
  378. applog(LOG_DEBUG, "%"PRIpreprv": Flushing both active and pending work",
  379. proc->proc_repr);
  380. hdata |= 1;
  381. }
  382. else
  383. applog(LOG_DEBUG, "%"PRIpreprv": Flushing pending work",
  384. proc->proc_repr);
  385. hashfast_send_msg(fd, cmd, HFOP_ABORT, cs->chipaddr, cs->coreaddr, hdata, 0);
  386. }
  387. static
  388. struct cgpu_info *hashfast_find_proc(struct thr_info * const master_thr, int chipaddr, int coreaddr)
  389. {
  390. struct cgpu_info *proc = master_thr->cgpu;
  391. struct hashfast_dev_state * const devstate = proc->device_data;
  392. if (coreaddr >= devstate->cores_per_chip)
  393. return NULL;
  394. const unsigned chip_count = proc->procs / devstate->cores_per_chip;
  395. if (chipaddr >= chip_count)
  396. return NULL;
  397. struct hashfast_chip_state * const chipstate = &devstate->chipstates[chipaddr];
  398. return chipstate->coreprocs[coreaddr];
  399. }
  400. static
  401. hashfast_isn_t hashfast_get_isn(struct hashfast_chip_state * const chipstate, uint16_t hfseq)
  402. {
  403. const uint8_t coreaddr = hfseq >> 8;
  404. const uint8_t seq = hfseq & 0xff;
  405. struct cgpu_info * const proc = chipstate->coreprocs[coreaddr];
  406. struct thr_info * const thr = proc->thr[0];
  407. struct hashfast_core_state * const cs = thr->cgpu_data;
  408. if (cs->last_seq == seq)
  409. return cs->last_isn;
  410. if (cs->last_seq == (uint8_t)(seq + 1))
  411. return cs->last2_isn;
  412. return 0;
  413. }
  414. static
  415. void hashfast_submit_nonce(struct thr_info * const thr, struct work * const work, const uint32_t nonce, const bool searched)
  416. {
  417. struct cgpu_info * const proc = thr->cgpu;
  418. struct hashfast_core_state * const cs = thr->cgpu_data;
  419. applog(LOG_DEBUG, "%"PRIpreprv": Found nonce for seq %02x (last=%02x): %08lx%s",
  420. proc->proc_repr, (unsigned)work->device_id, (unsigned)cs->last_seq,
  421. (unsigned long)nonce, searched ? " (searched)" : "");
  422. submit_nonce(thr, work, nonce);
  423. }
  424. static
  425. bool hashfast_poll_msg(struct thr_info * const master_thr)
  426. {
  427. struct cgpu_info * const dev = master_thr->cgpu;
  428. struct hashfast_dev_state * const devstate = dev->device_data;
  429. const int fd = devstate->fd;
  430. struct hashfast_parsed_msg msg;
  431. if (!hashfast_parse_msg(fd, &msg))
  432. return false;
  433. switch (msg.opcode)
  434. {
  435. case HFOP_NONCE:
  436. {
  437. const uint8_t *data = msg.data;
  438. for (int i = msg.datalen / 8; i; --i, (data = &data[8]))
  439. {
  440. const uint32_t nonce = (data[0] << 0)
  441. | (data[1] << 8)
  442. | (data[2] << 16)
  443. | (data[3] << 24);
  444. const uint8_t seq = data[4];
  445. const uint8_t coreaddr = data[5];
  446. // uint32_t ntime = data[6] | ((data[7] & 0xf) << 8);
  447. const bool search = data[7] & 0x10;
  448. struct cgpu_info * const proc = hashfast_find_proc(master_thr, msg.chipaddr, coreaddr);
  449. if (unlikely(!proc))
  450. {
  451. applog(LOG_ERR, "%s: Unknown chip/core address %u/%u",
  452. dev->dev_repr, (unsigned)msg.chipaddr, (unsigned)coreaddr);
  453. inc_hw_errors_only(master_thr);
  454. continue;
  455. }
  456. struct thr_info * const thr = proc->thr[0];
  457. struct hashfast_core_state * const cs = thr->cgpu_data;
  458. struct work *work;
  459. DL_SEARCH_SCALAR(thr->work, work, device_id, seq);
  460. if (unlikely(!work))
  461. {
  462. applog(LOG_WARNING, "%"PRIpreprv": Unknown seq %02x (last=%02x)",
  463. proc->proc_repr, (unsigned)seq, (unsigned)cs->last_seq);
  464. inc_hw_errors2(thr, NULL, &nonce);
  465. continue;
  466. }
  467. unsigned nonces_found = 1;
  468. hashfast_submit_nonce(thr, work, nonce, false);
  469. if (search)
  470. {
  471. for (int noffset = 1; noffset <= 0x80; ++noffset)
  472. {
  473. const uint32_t nonce2 = nonce + noffset;
  474. if (test_nonce(work, nonce2, false))
  475. {
  476. hashfast_submit_nonce(thr, work, nonce2, true);
  477. ++nonces_found;
  478. }
  479. }
  480. if (!nonces_found)
  481. {
  482. inc_hw_errors_only(thr);
  483. applog(LOG_WARNING, "%"PRIpreprv": search=1, but failed to turn up any additional solutions",
  484. proc->proc_repr);
  485. }
  486. }
  487. hashes_done2(thr, 0x100000000 * nonces_found, NULL);
  488. }
  489. break;
  490. }
  491. case HFOP_STATUS:
  492. {
  493. const uint8_t *data = &msg.data[8];
  494. struct cgpu_info *proc = hashfast_find_proc(master_thr, msg.chipaddr, 0);
  495. if (unlikely(!proc))
  496. {
  497. applog(LOG_ERR, "%s: Unknown chip address %u",
  498. dev->dev_repr, (unsigned)msg.chipaddr);
  499. inc_hw_errors_only(master_thr);
  500. break;
  501. }
  502. struct hashfast_chip_state * const chipstate = &devstate->chipstates[msg.chipaddr];
  503. hashfast_isn_t isn = hashfast_get_isn(chipstate, msg.hdata);
  504. const float temp = hashfast_temperature_conv(&msg.data[0]);
  505. for (int i = 0; i < HASHFAST_MAX_VOLTAGES; ++i)
  506. chipstate->voltages[i] = hashfast_voltage_conv(msg.data[2 + i]);
  507. int cores_uptodate, cores_active, cores_pending, cores_transitioned;
  508. cores_uptodate = cores_active = cores_pending = cores_transitioned = 0;
  509. for (int i = 0; i < devstate->cores_per_chip; ++i, (proc = proc->next_proc))
  510. {
  511. struct thr_info * const thr = proc->thr[0];
  512. struct hashfast_core_state * const cs = thr->cgpu_data;
  513. const uint8_t bits = data[i / 4] >> (2 * (i % 4));
  514. const bool has_active = bits & 1;
  515. const bool has_pending = bits & 2;
  516. bool try_transition = true;
  517. proc->temp = temp;
  518. if (cs->last_isn <= isn)
  519. ++cores_uptodate;
  520. else
  521. try_transition = false;
  522. if (has_active)
  523. ++cores_active;
  524. if (has_pending)
  525. ++cores_pending;
  526. else
  527. if (try_transition)
  528. {
  529. ++cores_transitioned;
  530. cs->has_pending = false;
  531. thr->queue_full = false;
  532. }
  533. }
  534. applog(LOG_DEBUG, "%s: STATUS from chipaddr=0x%02x with hdata=0x%04x (isn=0x%lx): total=%d uptodate=%d active=%d pending=%d transitioned=%d",
  535. dev->dev_repr, (unsigned)msg.chipaddr, (unsigned)msg.hdata, isn,
  536. devstate->cores_per_chip, cores_uptodate,
  537. cores_active, cores_pending, cores_transitioned);
  538. break;
  539. }
  540. }
  541. return true;
  542. }
  543. static
  544. void hashfast_poll(struct thr_info * const master_thr)
  545. {
  546. struct cgpu_info * const dev = master_thr->cgpu;
  547. struct timeval tv_timeout;
  548. timer_set_delay_from_now(&tv_timeout, 10000);
  549. while (true)
  550. {
  551. if (!hashfast_poll_msg(master_thr))
  552. {
  553. applog(LOG_DEBUG, "%s poll: No more messages", dev->dev_repr);
  554. break;
  555. }
  556. if (timer_passed(&tv_timeout, NULL))
  557. {
  558. applog(LOG_DEBUG, "%s poll: 10ms timeout met", dev->dev_repr);
  559. break;
  560. }
  561. }
  562. timer_set_delay_from_now(&master_thr->tv_poll, 100000);
  563. }
  564. static
  565. struct api_data *hashfast_api_stats(struct cgpu_info * const proc)
  566. {
  567. struct hashfast_dev_state * const devstate = proc->device_data;
  568. struct thr_info * const thr = proc->thr[0];
  569. struct hashfast_core_state * const cs = thr->cgpu_data;
  570. struct hashfast_chip_state * const chipstate = &devstate->chipstates[cs->chipaddr];
  571. struct api_data *root = NULL;
  572. char key[] = "VoltageNN";
  573. for (int i = 0; i < HASHFAST_MAX_VOLTAGES; ++i)
  574. {
  575. snprintf(&key[7], 3, "%d", i);
  576. if (chipstate->voltages[i])
  577. root = api_add_volts(root, key, &chipstate->voltages[i], false);
  578. }
  579. return root;
  580. }
  581. #ifdef HAVE_CURSES
  582. static
  583. void hashfast_wlogprint_status(struct cgpu_info * const proc)
  584. {
  585. struct hashfast_dev_state * const devstate = proc->device_data;
  586. struct thr_info * const thr = proc->thr[0];
  587. struct hashfast_core_state * const cs = thr->cgpu_data;
  588. struct hashfast_chip_state * const chipstate = &devstate->chipstates[cs->chipaddr];
  589. {
  590. // -> "NNN.xxx / NNN.xxx / NNN.xxx"
  591. size_t sz = (HASHFAST_MAX_VOLTAGES * 10) + 1;
  592. char buf[sz];
  593. char *s = buf;
  594. int rv = 0;
  595. for (int i = 0; i < HASHFAST_MAX_VOLTAGES; ++i)
  596. {
  597. const float voltage = chipstate->voltages[i];
  598. if (!voltage)
  599. continue;
  600. _SNP("%.3f / ", voltage);
  601. }
  602. if (rv >= 3 && s[-2] == '/')
  603. {
  604. s[-3] = '\0';
  605. wlogprint("Voltages: %s\n", buf);
  606. }
  607. }
  608. }
  609. #endif
  610. struct device_drv hashfast_ums_drv = {
  611. .dname = "hashfast_ums",
  612. .name = "HFA",
  613. .lowl_match = hashfast_lowl_match,
  614. .lowl_probe = hashfast_lowl_probe,
  615. .thread_init = hashfast_init,
  616. .minerloop = minerloop_queue,
  617. .queue_append = hashfast_queue_append,
  618. .queue_flush = hashfast_queue_flush,
  619. .poll = hashfast_poll,
  620. .get_api_stats = hashfast_api_stats,
  621. #ifdef HAVE_CURSES
  622. .proc_wlogprint_status = hashfast_wlogprint_status,
  623. #endif
  624. };