driver-hashfast.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. /*
  2. * Copyright 2013 Con Kolivas <kernel@kolivas.org>
  3. * Copyright 2013 Hashfast Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <stdbool.h>
  12. #include "usbutils.h"
  13. #include "driver-hashfast.h"
  14. ////////////////////////////////////////////////////////////////////////////////
  15. // Support for the CRC's used in header (CRC-8) and packet body (CRC-32)
  16. ////////////////////////////////////////////////////////////////////////////////
  17. #define GP8 0x107 /* x^8 + x^2 + x + 1 */
  18. #define DI8 0x07
  19. static unsigned char crc8_table[256]; /* CRC-8 table */
  20. static void hfa_init_crc8(void)
  21. {
  22. int i,j;
  23. unsigned char crc;
  24. for (i = 0; i < 256; i++) {
  25. crc = i;
  26. for (j = 0; j < 8; j++)
  27. crc = (crc << 1) ^ ((crc & 0x80) ? DI8 : 0);
  28. crc8_table[i] = crc & 0xFF;
  29. }
  30. }
  31. static unsigned char hfa_crc8(unsigned char *h)
  32. {
  33. int i;
  34. unsigned char crc;
  35. h++; // Preamble not included
  36. for (i = 1, crc = 0xff; i < 7; i++)
  37. crc = crc8_table[crc ^ *h++];
  38. return crc;
  39. }
  40. struct hfa_cmd {
  41. int cmd;
  42. char *cmd_name;
  43. enum usb_cmds usb_cmd;
  44. };
  45. /* Entries in this array need to align with the actual op values specified
  46. * in hf_protocol.h */
  47. #define C_NULL C_MAX
  48. static const struct hfa_cmd hfa_cmds[] = {
  49. {OP_NULL, "OP_NULL", C_NULL}, // 0
  50. {OP_ROOT, "OP_ROOT", C_NULL},
  51. {OP_RESET, "OP_RESET", C_HF_RESET},
  52. {OP_PLL_CONFIG, "OP_PLL_CONFIG", C_HF_PLL_CONFIG},
  53. {OP_ADDRESS, "OP_ADDRESS", C_HF_ADDRESS},
  54. {OP_READDRESS, "OP_READDRESS", C_NULL},
  55. {OP_HIGHEST, "OP_HIGHEST", C_NULL},
  56. {OP_BAUD, "OP_BAUD", C_HF_BAUD},
  57. {OP_UNROOT, "OP_UNROOT", C_NULL}, // 8
  58. {OP_HASH, "OP_HASH", C_HF_HASH},
  59. {OP_NONCE, "OP_NONCE", C_HF_NONCE},
  60. {OP_ABORT, "OP_ABORT", C_HF_ABORT},
  61. {OP_STATUS, "OP_STATUS", C_HF_STATUS},
  62. {OP_GPIO, "OP_GPIO", C_NULL},
  63. {OP_CONFIG, "OP_CONFIG", C_HF_CONFIG},
  64. {OP_STATISTICS, "OP_STATISTICS", C_HF_STATISTICS},
  65. {OP_GROUP, "OP_GROUP", C_NULL}, // 16
  66. {OP_CLOCKGATE, "OP_CLOCKGATE", C_HF_CLOCKGATE},
  67. {OP_USB_INIT, "OP_USB_INIT", C_HF_USB_INIT}, // 18
  68. {OP_GET_TRACE, "OP_GET_TRACE", C_NULL},
  69. {OP_LOOPBACK_USB, "OP_LOOPBACK_USB", C_NULL},
  70. {OP_LOOPBACK_UART, "OP_LOOPBACK_UART", C_NULL},
  71. {OP_DFU, "OP_DFU", C_NULL},
  72. {OP_USB_SHUTDOWN, "OP_USB_SHUTDOWN", C_NULL},
  73. {OP_DIE_STATUS, "OP_DIE_STATUS", C_HF_DIE_STATUS}, // 24
  74. {OP_GWQ_STATUS, "OP_GWQ_STATUS", C_HF_GWQ_STATUS},
  75. {OP_WORK_RESTART, "OP_WORK_RESTART", C_HF_WORK_RESTART},
  76. {OP_USB_STATS1, "OP_USB_STATS1", C_NULL},
  77. {OP_USB_GWQSTATS, "OP_USB_GWQSTATS", C_HF_GWQSTATS}
  78. };
  79. #define HF_USB_CMD_OFFSET (128 - 18)
  80. #define HF_USB_CMD(X) (X - HF_USB_CMD_OFFSET)
  81. /* Send an arbitrary frame, consisting of an 8 byte header and an optional
  82. * packet body. */
  83. static bool hfa_send_frame(struct cgpu_info *hashfast, uint8_t opcode, uint16_t hdata,
  84. uint8_t *data, int len)
  85. {
  86. int tx_length, ret, amount, id = hashfast->device_id;
  87. uint8_t packet[256];
  88. struct hf_header *p = (struct hf_header *)packet;
  89. p->preamble = HF_PREAMBLE;
  90. p->operation_code = opcode;
  91. p->chip_address = HF_GWQ_ADDRESS;
  92. p->core_address = 0;
  93. p->hdata = htole16(hdata);
  94. p->data_length = len / 4;
  95. p->crc8 = hfa_crc8(packet);
  96. if (len)
  97. memcpy(&packet[sizeof(struct hf_header)], data, len);
  98. tx_length = sizeof(struct hf_header) + len;
  99. ret = usb_write(hashfast, (char *)packet, tx_length, &amount,
  100. hfa_cmds[opcode].usb_cmd);
  101. if (unlikely(ret < 0 || amount != tx_length)) {
  102. applog(LOG_ERR, "HFA %d: hfa_send_frame: USB Send error, ret %d amount %d vs. tx_length %d",
  103. id, ret, amount, tx_length);
  104. return false;
  105. }
  106. return true;
  107. }
  108. static bool hfa_send_header(struct cgpu_info *hashfast, struct hf_header *h, int cmd)
  109. {
  110. int amount, ret, len;
  111. len = sizeof(*h);
  112. ret = usb_write(hashfast, (char *)h, len, &amount, hfa_cmds[cmd].usb_cmd);
  113. if (ret < 0 || amount != len) {
  114. applog(LOG_WARNING, "HFA%d: send_header: %s USB Send error, ret %d amount %d vs. length %d",
  115. hashfast->device_id, hfa_cmds[cmd].cmd_name, ret, amount, len);
  116. return false;
  117. }
  118. return true;
  119. }
  120. static bool hfa_get_header(struct cgpu_info *hashfast, struct hf_header *h, uint8_t *computed_crc)
  121. {
  122. int amount, ret, orig_len, len, ofs = 0, reads = 0;
  123. char buf[512];
  124. char *header;
  125. /* Read for up to 200ms till we find the first occurrence of HF_PREAMBLE
  126. * though it should be the first byte unless we get woefully out of
  127. * sync. */
  128. orig_len = len = sizeof(*h);
  129. do {
  130. if (++reads > 20)
  131. return false;
  132. ret = usb_read_timeout(hashfast, buf + ofs, len, &amount, 10, C_HF_GETHEADER);
  133. if (unlikely(ret && ret != LIBUSB_ERROR_TIMEOUT))
  134. return false;
  135. ofs += amount;
  136. header = memchr(buf, HF_PREAMBLE, ofs);
  137. if (header)
  138. len -= ofs - (header - buf);
  139. } while (len);
  140. memcpy(h, header, orig_len);
  141. *computed_crc = hfa_crc8((uint8_t *)h);
  142. return true;
  143. }
  144. static bool hfa_get_data(struct cgpu_info *hashfast, char *buf, int len4)
  145. {
  146. int amount, ret, len = len4 * 4;
  147. ret = usb_read(hashfast, buf, len, &amount, C_HF_GETDATA);
  148. if (ret)
  149. return false;
  150. if (amount != len) {
  151. applog(LOG_WARNING, "HFA %d: get_data: Strange amount returned %d vs. expected %d",
  152. hashfast->device_id, amount, len);
  153. return false;
  154. }
  155. return true;
  156. }
  157. static bool hfa_reset(struct cgpu_info *hashfast, struct hashfast_info *info)
  158. {
  159. struct hf_usb_init_header usb_init, *hu = &usb_init;
  160. struct hf_usb_init_base *db;
  161. char buf[1024];
  162. struct hf_header *h = (struct hf_header *)buf;
  163. uint8_t hcrc;
  164. bool ret;
  165. int i;
  166. info->hash_clock_rate = 550; // Hash clock rate in Mhz
  167. // Assemble the USB_INIT request
  168. memset(hu, 0, sizeof(*hu));
  169. hu->preamble = HF_PREAMBLE;
  170. hu->operation_code = OP_USB_INIT;
  171. hu->protocol = PROTOCOL_GLOBAL_WORK_QUEUE; // Protocol to use
  172. hu->hash_clock = info->hash_clock_rate; // Hash clock rate in Mhz
  173. hu->crc8 = hfa_crc8((uint8_t *)hu);
  174. applog(LOG_WARNING, "HFA%d: Sending OP_USB_INIT with GWQ protocol specified",
  175. hashfast->device_id);
  176. if (!hfa_send_header(hashfast, (struct hf_header *)hu, HF_USB_CMD(OP_USB_INIT)))
  177. return false;
  178. // Check for the correct response.
  179. // We extend the normal timeout - a complete device initialization, including
  180. // bringing power supplies up from standby, etc., can take over a second.
  181. for (i = 0; i < 30; i++) {
  182. ret = hfa_get_header(hashfast, h, &hcrc);
  183. if (ret)
  184. break;
  185. }
  186. if (!ret) {
  187. applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed!", hashfast->device_id);
  188. return false;
  189. }
  190. if (h->crc8 != hcrc) {
  191. applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed! CRC mismatch", hashfast->device_id);
  192. return false;
  193. }
  194. if (h->operation_code != OP_USB_INIT) {
  195. applog(LOG_WARNING, "HFA %d: OP_USB_INIT: Tossing packet, valid but unexpected type", hashfast->device_id);
  196. hfa_get_data(hashfast, buf, h->data_length);
  197. return false;
  198. }
  199. applog(LOG_DEBUG, "HFA %d: Good reply to OP_USB_INIT", hashfast->device_id);
  200. applog(LOG_DEBUG, "HFA %d: OP_USB_INIT: %d die in chain, %d cores, device_type %d, refclk %d Mhz",
  201. hashfast->device_id, h->chip_address, h->core_address, h->hdata & 0xff, (h->hdata >> 8) & 0xff);
  202. // Save device configuration
  203. info->asic_count = h->chip_address;
  204. info->core_count = h->core_address;
  205. info->device_type = (uint8_t)h->hdata;
  206. info->ref_frequency = (uint8_t)(h->hdata>>8);
  207. info->hash_sequence = 0;
  208. info->hash_sequence_tail = 0;
  209. info->device_sequence_tail = 0;
  210. // Size in bytes of the core bitmap in bytes
  211. info->core_bitmap_size = (((info->asic_count * info->core_count) + 31) / 32) * 4;
  212. // Get the usb_init_base structure
  213. if (!hfa_get_data(hashfast, (char *)&info->usb_init_base, U32SIZE(info->usb_init_base))) {
  214. applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed! Failure to get usb_init_base data",
  215. hashfast->device_id);
  216. return false;
  217. }
  218. db = &info->usb_init_base;
  219. applog(LOG_INFO, "HFA %d: firmware_rev: %d.%d", hashfast->device_id,
  220. (db->firmware_rev >> 8) & 0xff, db->firmware_rev & 0xff);
  221. applog(LOG_INFO, "HFA %d: hardware_rev: %d.%d", hashfast->device_id,
  222. (db->hardware_rev >> 8) & 0xff, db->hardware_rev & 0xff);
  223. applog(LOG_INFO, "HFA %d: serial number: %d", hashfast->device_id,
  224. db->serial_number);
  225. applog(LOG_INFO, "HFA %d: hash clockrate: %d Mhz", hashfast->device_id,
  226. db->hash_clockrate);
  227. applog(LOG_INFO, "HFA %d: inflight_target: %d", hashfast->device_id,
  228. db->inflight_target);
  229. applog(LOG_INFO, "HFA %d: sequence_modulus: %d", hashfast->device_id,
  230. db->sequence_modulus);
  231. info->num_sequence = db->sequence_modulus;
  232. // Now a copy of the config data used
  233. if (!hfa_get_data(hashfast, (char *)&info->config_data, U32SIZE(info->config_data))) {
  234. applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed! Failure to get config_data",
  235. hashfast->device_id);
  236. return false;
  237. }
  238. // Now the core bitmap
  239. info->core_bitmap = malloc(info->core_bitmap_size);
  240. if (!info->core_bitmap)
  241. quit(1, "Failed to malloc info core bitmap in hfa_reset");
  242. if (!hfa_get_data(hashfast, (char *)info->core_bitmap, info->core_bitmap_size / 4)) {
  243. applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed! Failure to get core_bitmap", hashfast->device_id);
  244. return false;
  245. }
  246. return true;
  247. }
  248. static bool hfa_detect_common(struct cgpu_info *hashfast)
  249. {
  250. struct hashfast_info *info;
  251. bool ret;
  252. info = calloc(sizeof(struct hashfast_info), 1);
  253. if (!info)
  254. quit(1, "Failed to calloc hashfast_info in hfa_detect_common");
  255. hashfast->device_data = info;
  256. /* hashfast_reset should fill in details for info */
  257. ret = hfa_reset(hashfast, info);
  258. if (!ret) {
  259. free(info);
  260. hashfast->device_data = NULL;
  261. return false;
  262. }
  263. // The per-die status array
  264. info->die_status = calloc(info->asic_count, sizeof(struct hf_g1_die_data));
  265. if (unlikely(!(info->die_status)))
  266. quit(1, "Failed to calloc die_status");
  267. // The per-die statistics array
  268. info->die_statistics = calloc(info->asic_count, sizeof(struct hf_long_statistics));
  269. if (unlikely(!(info->die_statistics)))
  270. quit(1, "Failed to calloc die_statistics");
  271. info->works = calloc(sizeof(struct work *), info->num_sequence);
  272. if (!info->works)
  273. quit(1, "Failed to calloc info works in hfa_detect_common");
  274. return true;
  275. }
  276. static void hfa_initialise(struct cgpu_info *hashfast)
  277. {
  278. if (hashfast->usbinfo.nodev)
  279. return;
  280. // FIXME Do necessary initialising here
  281. }
  282. static bool hfa_detect_one_usb(libusb_device *dev, struct usb_find_devices *found)
  283. {
  284. struct cgpu_info *hashfast;
  285. hashfast = usb_alloc_cgpu(&hashfast_drv, HASHFAST_MINER_THREADS);
  286. if (!hashfast)
  287. quit(1, "Failed to usb_alloc_cgpu hashfast");
  288. if (!usb_init(hashfast, dev, found)) {
  289. hashfast = usb_free_cgpu(hashfast);
  290. return false;
  291. }
  292. hashfast->usbdev->usb_type = USB_TYPE_STD;
  293. hfa_initialise(hashfast);
  294. add_cgpu(hashfast);
  295. return hfa_detect_common(hashfast);
  296. }
  297. static void hfa_detect(bool hotplug)
  298. {
  299. /* Set up the CRC tables only once. */
  300. if (!hotplug)
  301. hfa_init_crc8();
  302. usb_detect(&hashfast_drv, hfa_detect_one_usb);
  303. }
  304. static bool hfa_get_packet(struct cgpu_info *hashfast, struct hf_header *h)
  305. {
  306. uint8_t hcrc;
  307. bool ret;
  308. ret = hfa_get_header(hashfast, h, &hcrc);
  309. if (unlikely(!ret))
  310. goto out;
  311. if (unlikely(h->crc8 != hcrc)) {
  312. applog(LOG_WARNING, "HFA %d: Bad CRC %d vs %d, attempting to process anyway",
  313. hashfast->device_id, h->crc8, hcrc);
  314. }
  315. if (h->data_length > 0)
  316. ret = hfa_get_data(hashfast, (char *)(h + 1), h->data_length);
  317. if (unlikely(!ret)) {
  318. applog(LOG_WARNING, "HFA %d: Failed to get data associated with header",
  319. hashfast->device_id);
  320. }
  321. out:
  322. return ret;
  323. }
  324. static void hfa_parse_gwq_status(struct cgpu_info *hashfast, struct hashfast_info *info,
  325. struct hf_header *h)
  326. {
  327. struct hf_gwq_data *g = (struct hf_gwq_data *)(h + 1);
  328. struct work *work;
  329. applog(LOG_DEBUG, "HFA %d: OP_GWQ_STATUS, device_head %4d tail %4d my tail %4d shed %3d inflight %4d",
  330. hashfast->device_id, g->sequence_head, g->sequence_tail, info->hash_sequence_tail,
  331. g->shed_count, SEQUENCE_DISTANCE(info->hash_sequence_head,g->sequence_tail));
  332. mutex_lock(&info->lock);
  333. info->hash_count += g->hash_count;
  334. info->device_sequence_head = g->sequence_head;
  335. info->device_sequence_tail = g->sequence_tail;
  336. info->shed_count = g->shed_count;
  337. /* Free any work that is no longer required */
  338. while (info->device_sequence_tail != info->hash_sequence_tail) {
  339. if (++info->hash_sequence_tail >= info->num_sequence)
  340. info->hash_sequence_tail = 0;
  341. if (unlikely(!(work = info->works[info->hash_sequence_tail]))) {
  342. applog(LOG_ERR, "HFA %d: Bad work sequence tail",
  343. hashfast->device_id);
  344. hashfast->shutdown = true;
  345. break;
  346. }
  347. applog(LOG_DEBUG, "HFA %d: Completing work on hash_sequence_tail %d",
  348. hashfast->device_id, info->hash_sequence_tail);
  349. free_work(work);
  350. info->works[info->hash_sequence_tail] = NULL;
  351. }
  352. mutex_unlock(&info->lock);
  353. }
  354. static void hfa_update_die_status(struct cgpu_info *hashfast, struct hashfast_info *info,
  355. struct hf_header *h)
  356. {
  357. struct hf_g1_die_data *d = (struct hf_g1_die_data *)(h + 1), *ds;
  358. int num_included = (h->data_length * 4) / sizeof(struct hf_g1_die_data);
  359. int i, j;
  360. float die_temperature;
  361. float core_voltage[6];
  362. if (info->device_type == HFD_G1) {
  363. // Copy in the data. They're numbered sequentially from the starting point
  364. ds = info->die_status + h->chip_address;
  365. for (i = 0; i < num_included; i++)
  366. memcpy(ds++, d++, sizeof(struct hf_g1_die_data));
  367. for (i = 0, d = &info->die_status[h->chip_address]; i < num_included; i++, d++) {
  368. die_temperature = GN_DIE_TEMPERATURE(d->die.die_temperature);
  369. for (j = 0; j < 6; j++)
  370. core_voltage[j] = GN_CORE_VOLTAGE(d->die.core_voltage[j]);
  371. applog(LOG_DEBUG, "HF%d: die %2d: OP_DIE_STATUS Die temp %.2fC vdd's %.2f %.2f %.2f %.2f %.2f %.2f",
  372. hashfast->device_id, h->chip_address + i, die_temperature,
  373. core_voltage[0], core_voltage[1], core_voltage[2],
  374. core_voltage[3], core_voltage[4], core_voltage[5]);
  375. // XXX Convert board phase currents, voltage, temperature
  376. }
  377. }
  378. }
  379. static void search_for_extra_nonce(struct thr_info *thr, struct work *work,
  380. struct hf_candidate_nonce *n)
  381. {
  382. uint32_t nonce = n->nonce;
  383. int i;
  384. /* No function to test with ntime offsets yet */
  385. if (n->ntime)
  386. return;
  387. for (i = 0; i < 128; i++, nonce++) {
  388. /* We could break out of this early if nonce wraps or if we
  389. * find one correct nonce since the chance of more is extremely
  390. * low but this function will be hit so infrequently we may as
  391. * well test the entire range with the least code. */
  392. if (test_nonce(work, nonce))
  393. submit_tested_work(thr, work);
  394. }
  395. }
  396. static void hfa_parse_nonce(struct thr_info *thr, struct cgpu_info *hashfast,
  397. struct hashfast_info *info, struct hf_header *h)
  398. {
  399. struct hf_candidate_nonce *n = (struct hf_candidate_nonce *)(h + 1);
  400. int i, num_nonces = h->data_length / U32SIZE(sizeof(struct hf_candidate_nonce));
  401. applog(LOG_DEBUG, "HFA %d: OP_NONCE: %2d:, num_nonces %d hdata 0x%04x",
  402. hashfast->device_id, h->chip_address, num_nonces, h->hdata);
  403. for (i = 0; i < num_nonces; i++, n++) {
  404. struct work *work;
  405. applog(LOG_DEBUG, "HFA %d: OP_NONCE: %2d: %2d: search %1d ntime %2d sequence %4d nonce 0x%08x",
  406. hashfast->device_id, h->chip_address, i, n->search, n->ntime, n->sequence, n->nonce);
  407. // Find the job from the sequence number
  408. mutex_lock(&info->lock);
  409. work = info->works[n->sequence];
  410. mutex_unlock(&info->lock);
  411. if (unlikely(!work)) {
  412. info->no_matching_work++;
  413. applog(LOG_INFO, "HFA %d: No matching work!", hashfast->device_id);
  414. } else {
  415. applog(LOG_DEBUG, "HFA %d: OP_NONCE: sequence %d: submitting nonce 0x%08x ntime %d",
  416. hashfast->device_id, n->sequence, n->nonce, n->ntime);
  417. if ((n->nonce & 0xffff0000) == 0x42420000) // XXX REMOVE THIS
  418. break; // XXX PHONEY EMULATOR NONCE
  419. submit_noffset_nonce(thr, work, n->nonce, n->ntime); // XXX Return value from submit_nonce is error if set
  420. if (unlikely(n->search)) {
  421. /* This tells us there is another share in the
  422. * next 128 nonces */
  423. applog(LOG_DEBUG, "HFA %d: OP_NONCE: SEARCH PROXIMITY EVENT FOUND",
  424. hashfast->device_id);
  425. search_for_extra_nonce(thr, work, n);
  426. }
  427. }
  428. }
  429. }
  430. static void hfa_update_die_statistics(struct hashfast_info *info, struct hf_header *h)
  431. {
  432. struct hf_statistics *s = (struct hf_statistics *)(h + 1);
  433. struct hf_long_statistics *l;
  434. // Accumulate the data
  435. l = info->die_statistics + h->chip_address;
  436. l->rx_header_crc += s->rx_header_crc;
  437. l->rx_body_crc += s->rx_body_crc;
  438. l->rx_header_timeouts += s->rx_header_timeouts;
  439. l->rx_body_timeouts += s->rx_body_timeouts;
  440. l->core_nonce_fifo_full += s->core_nonce_fifo_full;
  441. l->array_nonce_fifo_full += s->array_nonce_fifo_full;
  442. l->stats_overrun += s->stats_overrun;
  443. }
  444. static void hfa_update_stats1(struct cgpu_info *hashfast, struct hashfast_info *info,
  445. struct hf_header *h)
  446. {
  447. struct hf_long_usb_stats1 *s1 = &info->stats1;
  448. struct hf_usb_stats1 *sd = (struct hf_usb_stats1 *)(h + 1);
  449. s1->usb_rx_preambles += sd->usb_rx_preambles;
  450. s1->usb_rx_receive_byte_errors += sd->usb_rx_receive_byte_errors;
  451. s1->usb_rx_bad_hcrc += sd->usb_rx_bad_hcrc;
  452. s1->usb_tx_attempts += sd->usb_tx_attempts;
  453. s1->usb_tx_packets += sd->usb_tx_packets;
  454. s1->usb_tx_timeouts += sd->usb_tx_timeouts;
  455. s1->usb_tx_incompletes += sd->usb_tx_incompletes;
  456. s1->usb_tx_endpointstalled += sd->usb_tx_endpointstalled;
  457. s1->usb_tx_disconnected += sd->usb_tx_disconnected;
  458. s1->usb_tx_suspended += sd->usb_tx_suspended;
  459. #if 0
  460. /* We don't care about UART stats so they're not in our struct */
  461. s1->uart_tx_queue_dma += sd->uart_tx_queue_dma;
  462. s1->uart_tx_interrupts += sd->uart_tx_interrupts;
  463. s1->uart_rx_preamble_ints += sd->uart_rx_preamble_ints;
  464. s1->uart_rx_missed_preamble_ints += sd->uart_rx_missed_preamble_ints;
  465. s1->uart_rx_header_done += sd->uart_rx_header_done;
  466. s1->uart_rx_data_done += sd->uart_rx_data_done;
  467. s1->uart_rx_bad_hcrc += sd->uart_rx_bad_hcrc;
  468. s1->uart_rx_bad_dma += sd->uart_rx_bad_dma;
  469. s1->uart_rx_short_dma += sd->uart_rx_short_dma;
  470. s1->uart_rx_buffers_full += sd->uart_rx_buffers_full;
  471. #endif
  472. if (sd->max_tx_buffers > s1->max_tx_buffers)
  473. s1->max_tx_buffers = sd->max_tx_buffers;
  474. if (sd->max_rx_buffers > s1->max_rx_buffers)
  475. s1->max_rx_buffers = sd->max_rx_buffers;
  476. applog(LOG_DEBUG, "HFA %d: OP_USB_STATS1:", hashfast->device_id);
  477. applog(LOG_DEBUG, " usb_rx_preambles: %6d", sd->usb_rx_preambles);
  478. applog(LOG_DEBUG, " usb_rx_receive_byte_errors: %6d", sd->usb_rx_receive_byte_errors);
  479. applog(LOG_DEBUG, " usb_rx_bad_hcrc: %6d", sd->usb_rx_bad_hcrc);
  480. applog(LOG_DEBUG, " usb_tx_attempts: %6d", sd->usb_tx_attempts);
  481. applog(LOG_DEBUG, " usb_tx_packets: %6d", sd->usb_tx_packets);
  482. applog(LOG_DEBUG, " usb_tx_timeouts: %6d", sd->usb_tx_timeouts);
  483. applog(LOG_DEBUG, " usb_tx_incompletes: %6d", sd->usb_tx_incompletes);
  484. applog(LOG_DEBUG, " usb_tx_endpointstalled: %6d", sd->usb_tx_endpointstalled);
  485. applog(LOG_DEBUG, " usb_tx_disconnected: %6d", sd->usb_tx_disconnected);
  486. applog(LOG_DEBUG, " usb_tx_suspended: %6d", sd->usb_tx_suspended);
  487. #if 0
  488. applog(LOG_DEBUG, " uart_tx_queue_dma: %6d", sd->uart_tx_queue_dma);
  489. applog(LOG_DEBUG, " uart_tx_interrupts: %6d", sd->uart_tx_interrupts);
  490. applog(LOG_DEBUG, " uart_rx_preamble_ints: %6d", sd->uart_rx_preamble_ints);
  491. applog(LOG_DEBUG, " uart_rx_missed_preamble_ints: %6d", sd->uart_rx_missed_preamble_ints);
  492. applog(LOG_DEBUG, " uart_rx_header_done: %6d", sd->uart_rx_header_done);
  493. applog(LOG_DEBUG, " uart_rx_data_done: %6d", sd->uart_rx_data_done);
  494. applog(LOG_DEBUG, " uart_rx_bad_hcrc: %6d", sd->uart_rx_bad_hcrc);
  495. applog(LOG_DEBUG, " uart_rx_bad_dma: %6d", sd->uart_rx_bad_dma);
  496. applog(LOG_DEBUG, " uart_rx_short_dma: %6d", sd->uart_rx_short_dma);
  497. applog(LOG_DEBUG, " uart_rx_buffers_full: %6d", sd->uart_rx_buffers_full);
  498. #endif
  499. applog(LOG_DEBUG, " max_tx_buffers: %6d", sd->max_tx_buffers);
  500. applog(LOG_DEBUG, " max_rx_buffers: %6d", sd->max_rx_buffers);
  501. }
  502. static void *hfa_read(void *arg)
  503. {
  504. struct thr_info *thr = (struct thr_info *)arg;
  505. struct cgpu_info *hashfast = thr->cgpu;
  506. struct hashfast_info *info = hashfast->device_data;
  507. char threadname[24];
  508. snprintf(threadname, 24, "hfa_read/%d", hashfast->device_id);
  509. RenameThread(threadname);
  510. while (likely(!hashfast->shutdown)) {
  511. char buf[512];
  512. struct hf_header *h = (struct hf_header *)buf;
  513. bool ret = hfa_get_packet(hashfast, h);
  514. if (unlikely(!ret))
  515. continue;
  516. switch (h->operation_code) {
  517. case OP_GWQ_STATUS:
  518. hfa_parse_gwq_status(hashfast, info, h);
  519. break;
  520. case OP_DIE_STATUS:
  521. hfa_update_die_status(hashfast, info, h);
  522. break;
  523. case OP_NONCE:
  524. hfa_parse_nonce(thr, hashfast, info, h);
  525. break;
  526. case OP_STATISTICS:
  527. hfa_update_die_statistics(info, h);
  528. break;
  529. case OP_USB_STATS1:
  530. hfa_update_stats1(hashfast, info, h);
  531. break;
  532. default:
  533. applog(LOG_WARNING, "HFA %d: Unhandled operation code %d",
  534. hashfast->device_id, h->operation_code);
  535. break;
  536. }
  537. }
  538. return NULL;
  539. }
  540. static bool hfa_prepare(struct thr_info *thr)
  541. {
  542. struct cgpu_info *hashfast = thr->cgpu;
  543. struct hashfast_info *info = hashfast->device_data;
  544. struct timeval now;
  545. mutex_init(&info->lock);
  546. if (pthread_create(&info->read_thr, NULL, hfa_read, (void *)thr))
  547. quit(1, "Failed to pthread_create read thr in hfa_prepare");
  548. cgtime(&now);
  549. get_datestamp(hashfast->init, sizeof(hashfast->init), &now);
  550. return true;
  551. }
  552. /* Figure out how many jobs to send. */
  553. static int __hfa_jobs(struct hashfast_info *info)
  554. {
  555. return info->usb_init_base.inflight_target - HF_SEQUENCE_DISTANCE(info->hash_sequence, info->device_sequence_tail);
  556. }
  557. static int hfa_jobs(struct hashfast_info *info)
  558. {
  559. int ret;
  560. mutex_lock(&info->lock);
  561. ret = __hfa_jobs(info);
  562. mutex_unlock(&info->lock);
  563. return ret;
  564. }
  565. static int64_t hfa_scanwork(struct thr_info *thr)
  566. {
  567. struct cgpu_info *hashfast = thr->cgpu;
  568. struct hashfast_info *info = hashfast->device_data;
  569. int64_t hashes;
  570. int jobs, ret;
  571. if (unlikely(thr->work_restart)) {
  572. restart:
  573. ret = hfa_send_frame(hashfast, OP_WORK_RESTART, 0, (uint8_t *)NULL, 0);
  574. if (unlikely(!ret)) {
  575. ret = hfa_reset(hashfast, info);
  576. if (unlikely(!ret)) {
  577. applog(LOG_ERR, "HFA %d: Failed to reset after write failure, disabling",
  578. hashfast->device_id);
  579. return -1;
  580. }
  581. }
  582. }
  583. jobs = hfa_jobs(info);
  584. if (!jobs) {
  585. ret = restart_wait(thr, 100);
  586. if (unlikely(!ret))
  587. goto restart;
  588. jobs = hfa_jobs(info);
  589. }
  590. while (jobs > 0) {
  591. struct hf_hash_usb op_hash_data;
  592. struct work *work;
  593. uint64_t intdiff;
  594. int i, sequence;
  595. uint32_t *p;
  596. /* This is a blocking function if there's no work */
  597. work = get_work(thr, thr->id);
  598. /* Assemble the data frame and send the OP_HASH packet */
  599. memcpy(op_hash_data.midstate, work->midstate, sizeof(op_hash_data.midstate));
  600. memcpy(op_hash_data.merkle_residual, work->data + 64, 4);
  601. p = (uint32_t *)(work->data + 64 + 4);
  602. op_hash_data.timestamp = *p++;
  603. op_hash_data.bits = *p++;
  604. op_hash_data.nonce_loops = 0;
  605. /* Set the number of leading zeroes to look for based on diff.
  606. * Diff 1 = 32, Diff 2 = 33, Diff 4 = 34 etc. */
  607. intdiff = (uint64_t)work->device_diff;
  608. for (i = 31; intdiff; i++, intdiff >>= 1);
  609. op_hash_data.search_difficulty = i;
  610. if ((sequence = info->hash_sequence + 1) >= info->num_sequence)
  611. sequence = 0;
  612. ret = hfa_send_frame(hashfast, OP_HASH, sequence, (uint8_t *)&op_hash_data, sizeof(op_hash_data));
  613. if (unlikely(!ret)) {
  614. ret = hfa_reset(hashfast, info);
  615. if (unlikely(!ret)) {
  616. applog(LOG_ERR, "HFA %d: Failed to reset after write failure, disabling",
  617. hashfast->device_id);
  618. return -1;
  619. }
  620. }
  621. mutex_lock(&info->lock);
  622. info->hash_sequence = sequence;
  623. *(info->works + info->hash_sequence) = work;
  624. jobs = __hfa_jobs(info);
  625. mutex_unlock(&info->lock);
  626. applog(LOG_DEBUG, "HFA %d: OP_HASH sequence %d search_difficulty %d work_difficulty %g",
  627. hashfast->device_id, info->hash_sequence, op_hash_data.search_difficulty, work->work_difficulty);
  628. }
  629. mutex_lock(&info->lock);
  630. hashes = info->hash_count;
  631. info->hash_count = 0;
  632. mutex_unlock(&info->lock);
  633. return hashes;
  634. }
  635. static struct api_data *hfa_api_stats(struct cgpu_info __maybe_unused *cgpu)
  636. {
  637. return NULL;
  638. }
  639. static void hfa_init(struct cgpu_info *hashfast)
  640. {
  641. usb_buffer_enable(hashfast);
  642. }
  643. static void hfa_free_all_work(struct hashfast_info *info)
  644. {
  645. while (info->device_sequence_tail != info->hash_sequence_head) {
  646. struct work *work;
  647. if (++info->hash_sequence_tail >= info->num_sequence)
  648. info->hash_sequence_tail = 0;
  649. if (unlikely(!(work = info->works[info->hash_sequence_tail])))
  650. break;
  651. free_work(work);
  652. info->works[info->hash_sequence_tail] = NULL;
  653. }
  654. }
  655. static void hfa_shutdown(struct thr_info *thr)
  656. {
  657. struct cgpu_info *hashfast = thr->cgpu;
  658. struct hashfast_info *info = hashfast->device_data;
  659. hfa_send_frame(hashfast, HF_USB_CMD(OP_USB_SHUTDOWN), 0, NULL, 0);
  660. pthread_join(info->read_thr, NULL);
  661. hfa_free_all_work(info);
  662. }
  663. struct device_drv hashfast_drv = {
  664. .drv_id = DRIVER_hashfast,
  665. .dname = "Hashfast",
  666. .name = "HFA",
  667. .max_diff = 256.0, // Limit max diff to get some nonces back regardless
  668. .drv_detect = hfa_detect,
  669. .thread_prepare = hfa_prepare,
  670. .hash_work = &hash_driver_work,
  671. .scanwork = hfa_scanwork,
  672. .get_api_stats = hfa_api_stats,
  673. .reinit_device = hfa_init,
  674. .thread_shutdown = hfa_shutdown,
  675. };