driver-hashfast.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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. uint32_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)
  94. {
  95. char hex[(bufsz * 2) + 1];
  96. bin2hex(hex, buf, bufsz);
  97. if (rv < 0)
  98. applog(LOG_DEBUG, "%s fd=%d: SEND (%s) => %d",
  99. "hashfast", fd, hex, (int)rv);
  100. else
  101. if (rv < bufsz)
  102. applog(LOG_DEBUG, "%s fd=%d: SEND %.*s(%s)",
  103. "hashfast", fd, rv * 2, hex, &hex[rv * 2]);
  104. else
  105. if (rv > bufsz)
  106. applog(LOG_DEBUG, "%s fd=%d: SEND %s => +%d",
  107. "hashfast", fd, hex, (int)(rv - bufsz));
  108. else
  109. applog(LOG_DEBUG, "%s fd=%d: SEND %s",
  110. "hashfast", fd, hex);
  111. }
  112. return rv;
  113. }
  114. static
  115. ssize_t hashfast_read(const int fd, void * const buf, size_t bufsz)
  116. {
  117. const ssize_t rv = serial_read(fd, buf, bufsz);
  118. if (opt_debug && opt_dev_protocol && rv)
  119. {
  120. char hex[(rv * 2) + 1];
  121. bin2hex(hex, buf, rv);
  122. applog(LOG_DEBUG, "%s fd=%d: RECV %s",
  123. "hashfast", fd, hex);
  124. }
  125. return rv;
  126. }
  127. static
  128. 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)
  129. {
  130. buf[0] = '\xaa';
  131. buf[1] = opcode;
  132. buf[2] = chipaddr;
  133. buf[3] = coreaddr;
  134. buf[4] = hdata & 0xff;
  135. buf[5] = hdata >> 8;
  136. if (datalen > 1020 || datalen % 4)
  137. return false;
  138. buf[6] = datalen / 4;
  139. buf[7] = crc8ccitt(&buf[1], 6);
  140. return true;
  141. }
  142. static
  143. 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)
  144. {
  145. if (!hashfast_prepare_msg(buf, opcode, chipaddr, coreaddr, hdata, datalen))
  146. return false;
  147. const size_t buflen = HASHFAST_HEADER_SIZE + datalen;
  148. return (buflen == hashfast_write(fd, buf, buflen));
  149. }
  150. static
  151. bool hashfast_parse_msg(const int fd, struct hashfast_parsed_msg * const out_msg)
  152. {
  153. uint8_t buf[HASHFAST_HEADER_SIZE];
  154. startover:
  155. if (HASHFAST_HEADER_SIZE != hashfast_read(fd, buf, HASHFAST_HEADER_SIZE))
  156. return false;
  157. uint8_t *p = memchr(buf, '\xaa', HASHFAST_HEADER_SIZE);
  158. if (p != buf)
  159. {
  160. ignoresome:
  161. if (!p)
  162. goto startover;
  163. int moreneeded = p - buf;
  164. int alreadyhave = HASHFAST_HEADER_SIZE - moreneeded;
  165. memmove(buf, p, alreadyhave);
  166. if (moreneeded != hashfast_read(fd, &buf[alreadyhave], moreneeded))
  167. return false;
  168. }
  169. const uint8_t correct_crc8 = crc8ccitt(&buf[1], 6);
  170. if (buf[7] != correct_crc8)
  171. {
  172. p = memchr(&buf[1], '\xaa', HASHFAST_HEADER_SIZE - 1);
  173. goto ignoresome;
  174. }
  175. out_msg->opcode = buf[1];
  176. out_msg->chipaddr = buf[2];
  177. out_msg->coreaddr = buf[3];
  178. out_msg->hdata = (uint16_t)buf[4] | ((uint16_t)buf[5] << 8);
  179. out_msg->datalen = buf[6] * 4;
  180. return (out_msg->datalen == hashfast_read(fd, &out_msg->data[0], out_msg->datalen));
  181. }
  182. static
  183. bool hashfast_lowl_match(const struct lowlevel_device_info * const info)
  184. {
  185. if (!lowlevel_match_id(info, &lowl_vcom, 0, 0))
  186. return false;
  187. return (info->manufacturer && strstr(info->manufacturer, "HashFast"));
  188. }
  189. static
  190. bool hashfast_detect_one(const char * const devpath)
  191. {
  192. uint16_t clock = 550;
  193. uint8_t buf[HASHFAST_HEADER_SIZE];
  194. const int fd = serial_open(devpath, 0, 100, true);
  195. if (fd == -1)
  196. {
  197. applog(LOG_DEBUG, "%s: Failed to open %s", __func__, devpath);
  198. return false;
  199. }
  200. struct hashfast_parsed_msg * const pmsg = malloc(sizeof(*pmsg));
  201. hashfast_send_msg(fd, buf, HFOP_USB_INIT, 0, 0, clock, 0);
  202. do {
  203. if (!hashfast_parse_msg(fd, pmsg))
  204. {
  205. applog(LOG_DEBUG, "%s: Failed to parse response on %s",
  206. __func__, devpath);
  207. serial_close(fd);
  208. goto err;
  209. }
  210. } while (pmsg->opcode != HFOP_USB_INIT);
  211. serial_close(fd);
  212. const int expectlen = 0x20 + (pmsg->chipaddr * pmsg->coreaddr) / 8;
  213. if (pmsg->datalen < expectlen)
  214. {
  215. applog(LOG_DEBUG, "%s: USB_INIT response too short on %s (%d < %d)",
  216. __func__, devpath, (int)pmsg->datalen, expectlen);
  217. goto err;
  218. }
  219. if (pmsg->data[8] != 0)
  220. {
  221. applog(LOG_DEBUG, "%s: USB_INIT failed on %s (err=%d)",
  222. __func__, devpath, pmsg->data[8]);
  223. goto err;
  224. }
  225. if (serial_claim_v(devpath, &hashfast_ums_drv))
  226. return false;
  227. struct cgpu_info * const cgpu = malloc(sizeof(*cgpu));
  228. *cgpu = (struct cgpu_info){
  229. .drv = &hashfast_ums_drv,
  230. .device_path = strdup(devpath),
  231. .deven = DEV_ENABLED,
  232. .procs = (pmsg->chipaddr * pmsg->coreaddr),
  233. .threads = 1,
  234. .device_data = pmsg,
  235. .cutofftemp = 100,
  236. };
  237. return add_cgpu(cgpu);
  238. err:
  239. free(pmsg);
  240. return false;
  241. }
  242. static
  243. bool hashfast_lowl_probe(const struct lowlevel_device_info * const info)
  244. {
  245. return vcom_lowl_probe_wrapper(info, hashfast_detect_one);
  246. }
  247. struct hashfast_dev_state {
  248. uint8_t cores_per_chip;
  249. int fd;
  250. struct hashfast_chip_state *chipstates;
  251. };
  252. struct hashfast_chip_state {
  253. struct cgpu_info **coreprocs;
  254. hashfast_isn_t last_isn;
  255. float voltages[HASHFAST_MAX_VOLTAGES];
  256. };
  257. struct hashfast_core_state {
  258. uint8_t chipaddr;
  259. uint8_t coreaddr;
  260. int next_device_id;
  261. uint8_t last_seq;
  262. hashfast_isn_t last_isn;
  263. hashfast_isn_t last2_isn;
  264. bool has_pending;
  265. unsigned queued;
  266. };
  267. static
  268. bool hashfast_init(struct thr_info * const master_thr)
  269. {
  270. struct cgpu_info * const dev = master_thr->cgpu, *proc;
  271. struct hashfast_parsed_msg * const pmsg = dev->device_data;
  272. struct hashfast_dev_state * const devstate = malloc(sizeof(*devstate));
  273. struct hashfast_chip_state * const chipstates = malloc(sizeof(*chipstates) * pmsg->chipaddr), *chipstate;
  274. struct hashfast_core_state * const corestates = malloc(sizeof(*corestates) * dev->procs), *cs;
  275. int i;
  276. *devstate = (struct hashfast_dev_state){
  277. .chipstates = chipstates,
  278. .cores_per_chip = pmsg->coreaddr,
  279. .fd = serial_open(dev->device_path, 0, 1, true),
  280. };
  281. for (i = 0; i < pmsg->chipaddr; ++i)
  282. {
  283. chipstate = &chipstates[i];
  284. *chipstate = (struct hashfast_chip_state){
  285. .coreprocs = malloc(sizeof(struct cgpu_info *) * pmsg->coreaddr),
  286. };
  287. }
  288. for ((i = 0), (proc = dev); proc; ++i, (proc = proc->next_proc))
  289. {
  290. struct thr_info * const thr = proc->thr[0];
  291. const bool core_is_working = pmsg->data[0x20 + (i / 8)] & (1 << (i % 8));
  292. if (!core_is_working)
  293. proc->deven = DEV_RECOVER_DRV;
  294. proc->device_data = devstate;
  295. thr->cgpu_data = cs = &corestates[i];
  296. *cs = (struct hashfast_core_state){
  297. .chipaddr = i / pmsg->coreaddr,
  298. .coreaddr = i % pmsg->coreaddr,
  299. };
  300. chipstates[cs->chipaddr].coreprocs[cs->coreaddr] = proc;
  301. }
  302. free(pmsg);
  303. // TODO: actual clock = [12,13]
  304. timer_set_now(&master_thr->tv_poll);
  305. return true;
  306. }
  307. static
  308. bool hashfast_queue_append(struct thr_info * const thr, struct work * const work)
  309. {
  310. struct cgpu_info * const proc = thr->cgpu;
  311. struct hashfast_dev_state * const devstate = proc->device_data;
  312. const int fd = devstate->fd;
  313. struct hashfast_core_state * const cs = thr->cgpu_data;
  314. struct hashfast_chip_state * const chipstate = &devstate->chipstates[cs->chipaddr];
  315. const size_t cmdlen = HASHFAST_HEADER_SIZE + HASHFAST_HASH_SIZE;
  316. uint8_t cmd[cmdlen];
  317. uint8_t * const hashdata = &cmd[HASHFAST_HEADER_SIZE];
  318. hashfast_isn_t isn;
  319. uint8_t seq;
  320. if (cs->has_pending)
  321. {
  322. thr->queue_full = true;
  323. return false;
  324. }
  325. isn = ++chipstate->last_isn;
  326. seq = ++cs->last_seq;
  327. work->device_id = seq;
  328. cs->last2_isn = cs->last_isn;
  329. cs->last_isn = isn;
  330. hashfast_prepare_msg(cmd, HFOP_HASH, cs->chipaddr, cs->coreaddr, (cs->coreaddr << 8) | seq, 56);
  331. memcpy(&hashdata[ 0], work->midstate, 0x20);
  332. memcpy(&hashdata[0x20], &work->data[64], 0xc);
  333. memset(&hashdata[0x2c], '\0', 0xa); // starting_nonce, nonce_loops, ntime_loops
  334. hashdata[0x36] = 32; // search target (number of zero bits)
  335. hashdata[0x37] = 0;
  336. cs->has_pending = true;
  337. if (cmdlen != hashfast_write(fd, cmd, cmdlen))
  338. return false;
  339. DL_APPEND(thr->work, work);
  340. if (cs->queued > HASHFAST_QUEUE_MEMORY)
  341. {
  342. struct work * const old_work = thr->work;
  343. DL_DELETE(thr->work, old_work);
  344. free_work(old_work);
  345. }
  346. else
  347. ++cs->queued;
  348. return true;
  349. }
  350. static
  351. void hashfast_queue_flush(struct thr_info * const thr)
  352. {
  353. struct cgpu_info * const proc = thr->cgpu;
  354. struct hashfast_dev_state * const devstate = proc->device_data;
  355. const int fd = devstate->fd;
  356. struct hashfast_core_state * const cs = thr->cgpu_data;
  357. uint8_t cmd[HASHFAST_HEADER_SIZE];
  358. uint16_t hdata = 2;
  359. if ((!thr->work) || stale_work(thr->work->prev, true))
  360. {
  361. applog(LOG_DEBUG, "%"PRIpreprv": Flushing both active and pending work",
  362. proc->proc_repr);
  363. hdata |= 1;
  364. }
  365. else
  366. applog(LOG_DEBUG, "%"PRIpreprv": Flushing pending work",
  367. proc->proc_repr);
  368. hashfast_send_msg(fd, cmd, HFOP_ABORT, cs->chipaddr, cs->coreaddr, hdata, 0);
  369. }
  370. static
  371. struct cgpu_info *hashfast_find_proc(struct thr_info * const master_thr, int chipaddr, int coreaddr)
  372. {
  373. struct cgpu_info *proc = master_thr->cgpu;
  374. struct hashfast_dev_state * const devstate = proc->device_data;
  375. if (coreaddr >= devstate->cores_per_chip)
  376. return NULL;
  377. const unsigned chip_count = proc->procs / devstate->cores_per_chip;
  378. if (chipaddr >= chip_count)
  379. return NULL;
  380. struct hashfast_chip_state * const chipstate = &devstate->chipstates[chipaddr];
  381. return chipstate->coreprocs[coreaddr];
  382. }
  383. static
  384. hashfast_isn_t hashfast_get_isn(struct hashfast_chip_state * const chipstate, uint16_t hfseq)
  385. {
  386. const uint8_t coreaddr = hfseq >> 8;
  387. const uint8_t seq = hfseq & 0xff;
  388. struct cgpu_info * const proc = chipstate->coreprocs[coreaddr];
  389. struct thr_info * const thr = proc->thr[0];
  390. struct hashfast_core_state * const cs = thr->cgpu_data;
  391. if (cs->last_seq == seq)
  392. return cs->last_isn;
  393. if (cs->last_seq == (uint8_t)(seq + 1))
  394. return cs->last2_isn;
  395. return 0;
  396. }
  397. static
  398. void hashfast_submit_nonce(struct thr_info * const thr, struct work * const work, const uint32_t nonce, const bool searched)
  399. {
  400. struct cgpu_info * const proc = thr->cgpu;
  401. struct hashfast_core_state * const cs = thr->cgpu_data;
  402. applog(LOG_DEBUG, "%"PRIpreprv": Found nonce for seq %02x (last=%02x): %08lx%s",
  403. proc->proc_repr, (unsigned)work->device_id, (unsigned)cs->last_seq,
  404. (unsigned long)nonce, searched ? " (searched)" : "");
  405. submit_nonce(thr, work, nonce);
  406. }
  407. static
  408. bool hashfast_poll_msg(struct thr_info * const master_thr)
  409. {
  410. struct cgpu_info * const dev = master_thr->cgpu;
  411. struct hashfast_dev_state * const devstate = dev->device_data;
  412. const int fd = devstate->fd;
  413. struct hashfast_parsed_msg msg;
  414. if (!hashfast_parse_msg(fd, &msg))
  415. return false;
  416. switch (msg.opcode)
  417. {
  418. case HFOP_NONCE:
  419. {
  420. const uint8_t *data = msg.data;
  421. for (int i = msg.datalen / 8; i; --i, (data = &data[8]))
  422. {
  423. const uint32_t nonce = (data[0] << 0)
  424. | (data[1] << 8)
  425. | (data[2] << 16)
  426. | (data[3] << 24);
  427. const uint8_t seq = data[4];
  428. const uint8_t coreaddr = data[5];
  429. // uint32_t ntime = data[6] | ((data[7] & 0xf) << 8);
  430. const bool search = data[7] & 0x10;
  431. struct cgpu_info * const proc = hashfast_find_proc(master_thr, msg.chipaddr, coreaddr);
  432. if (unlikely(!proc))
  433. {
  434. applog(LOG_ERR, "%s: Unknown chip/core address %u/%u",
  435. dev->dev_repr, (unsigned)msg.chipaddr, (unsigned)coreaddr);
  436. inc_hw_errors_only(master_thr);
  437. continue;
  438. }
  439. struct thr_info * const thr = proc->thr[0];
  440. struct hashfast_core_state * const cs = thr->cgpu_data;
  441. struct work *work;
  442. DL_SEARCH_SCALAR(thr->work, work, device_id, seq);
  443. if (unlikely(!work))
  444. {
  445. applog(LOG_WARNING, "%"PRIpreprv": Unknown seq %02x (last=%02x)",
  446. proc->proc_repr, (unsigned)seq, (unsigned)cs->last_seq);
  447. inc_hw_errors2(thr, NULL, &nonce);
  448. continue;
  449. }
  450. unsigned nonces_found = 1;
  451. hashfast_submit_nonce(thr, work, nonce, false);
  452. if (search)
  453. {
  454. for (int noffset = 1; noffset <= 0x80; ++noffset)
  455. {
  456. const uint32_t nonce2 = nonce + noffset;
  457. if (test_nonce(work, nonce2, false))
  458. {
  459. hashfast_submit_nonce(thr, work, nonce2, true);
  460. ++nonces_found;
  461. }
  462. }
  463. if (!nonces_found)
  464. {
  465. inc_hw_errors_only(thr);
  466. applog(LOG_WARNING, "%"PRIpreprv": search=1, but failed to turn up any additional solutions",
  467. proc->proc_repr);
  468. }
  469. }
  470. hashes_done2(thr, 0x100000000 * nonces_found, NULL);
  471. }
  472. break;
  473. }
  474. case HFOP_STATUS:
  475. {
  476. const uint8_t *data = &msg.data[8];
  477. struct cgpu_info *proc = hashfast_find_proc(master_thr, msg.chipaddr, 0);
  478. if (unlikely(!proc))
  479. {
  480. applog(LOG_ERR, "%s: Unknown chip address %u",
  481. dev->dev_repr, (unsigned)msg.chipaddr);
  482. inc_hw_errors_only(master_thr);
  483. break;
  484. }
  485. struct hashfast_chip_state * const chipstate = &devstate->chipstates[msg.chipaddr];
  486. hashfast_isn_t isn = hashfast_get_isn(chipstate, msg.hdata);
  487. const float temp = hashfast_temperature_conv(&msg.data[0]);
  488. for (int i = 0; i < HASHFAST_MAX_VOLTAGES; ++i)
  489. chipstate->voltages[i] = hashfast_voltage_conv(msg.data[2 + i]);
  490. int cores_uptodate, cores_active, cores_pending, cores_transitioned;
  491. cores_uptodate = cores_active = cores_pending = cores_transitioned = 0;
  492. for (int i = 0; i < devstate->cores_per_chip; ++i, (proc = proc->next_proc))
  493. {
  494. struct thr_info * const thr = proc->thr[0];
  495. struct hashfast_core_state * const cs = thr->cgpu_data;
  496. const uint8_t bits = data[i / 4] >> (2 * (i % 4));
  497. const bool has_active = bits & 1;
  498. const bool has_pending = bits & 2;
  499. bool try_transition = true;
  500. proc->temp = temp;
  501. if (cs->last_isn <= isn)
  502. ++cores_uptodate;
  503. else
  504. try_transition = false;
  505. if (has_active)
  506. ++cores_active;
  507. if (has_pending)
  508. ++cores_pending;
  509. else
  510. if (try_transition)
  511. {
  512. ++cores_transitioned;
  513. cs->has_pending = false;
  514. thr->queue_full = false;
  515. }
  516. }
  517. 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",
  518. dev->dev_repr, (unsigned)msg.chipaddr, (unsigned)msg.hdata, isn,
  519. devstate->cores_per_chip, cores_uptodate,
  520. cores_active, cores_pending, cores_transitioned);
  521. break;
  522. }
  523. }
  524. return true;
  525. }
  526. static
  527. void hashfast_poll(struct thr_info * const master_thr)
  528. {
  529. struct cgpu_info * const dev = master_thr->cgpu;
  530. struct timeval tv_timeout;
  531. timer_set_delay_from_now(&tv_timeout, 10000);
  532. while (true)
  533. {
  534. if (!hashfast_poll_msg(master_thr))
  535. {
  536. applog(LOG_DEBUG, "%s poll: No more messages", dev->dev_repr);
  537. break;
  538. }
  539. if (timer_passed(&tv_timeout, NULL))
  540. {
  541. applog(LOG_DEBUG, "%s poll: 10ms timeout met", dev->dev_repr);
  542. break;
  543. }
  544. }
  545. timer_set_delay_from_now(&master_thr->tv_poll, 100000);
  546. }
  547. static
  548. struct api_data *hashfast_api_stats(struct cgpu_info * const proc)
  549. {
  550. struct hashfast_dev_state * const devstate = proc->device_data;
  551. struct thr_info * const thr = proc->thr[0];
  552. struct hashfast_core_state * const cs = thr->cgpu_data;
  553. struct hashfast_chip_state * const chipstate = &devstate->chipstates[cs->chipaddr];
  554. struct api_data *root = NULL;
  555. char key[] = "VoltageNN";
  556. for (int i = 0; i < HASHFAST_MAX_VOLTAGES; ++i)
  557. {
  558. snprintf(&key[7], 3, "%d", i);
  559. if (chipstate->voltages[i])
  560. root = api_add_volts(root, key, &chipstate->voltages[i], false);
  561. }
  562. return root;
  563. }
  564. #ifdef HAVE_CURSES
  565. static
  566. void hashfast_wlogprint_status(struct cgpu_info * const proc)
  567. {
  568. struct hashfast_dev_state * const devstate = proc->device_data;
  569. struct thr_info * const thr = proc->thr[0];
  570. struct hashfast_core_state * const cs = thr->cgpu_data;
  571. struct hashfast_chip_state * const chipstate = &devstate->chipstates[cs->chipaddr];
  572. {
  573. // -> "NNN.xxx / NNN.xxx / NNN.xxx"
  574. size_t sz = (HASHFAST_MAX_VOLTAGES * 10) + 1;
  575. char buf[sz];
  576. char *s = buf;
  577. int rv = 0;
  578. for (int i = 0; i < HASHFAST_MAX_VOLTAGES; ++i)
  579. {
  580. const float voltage = chipstate->voltages[i];
  581. if (!voltage)
  582. continue;
  583. _SNP("%.3f / ", voltage);
  584. }
  585. if (rv >= 3 && s[-2] == '/')
  586. {
  587. s[-3] = '\0';
  588. wlogprint("Voltages: %s\n", buf);
  589. }
  590. }
  591. }
  592. #endif
  593. struct device_drv hashfast_ums_drv = {
  594. .dname = "hashfast_ums",
  595. .name = "HFA",
  596. .lowl_match = hashfast_lowl_match,
  597. .lowl_probe = hashfast_lowl_probe,
  598. .thread_init = hashfast_init,
  599. .minerloop = minerloop_queue,
  600. .queue_append = hashfast_queue_append,
  601. .queue_flush = hashfast_queue_flush,
  602. .poll = hashfast_poll,
  603. .get_api_stats = hashfast_api_stats,
  604. #ifdef HAVE_CURSES
  605. .proc_wlogprint_status = hashfast_wlogprint_status,
  606. #endif
  607. };