driver-hashfast.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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. uint8_t 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 = hfa_cmds[opcode].cmd;
  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_INFO, "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 void hfa_send_shutdown(struct cgpu_info *hashfast)
  249. {
  250. hfa_send_frame(hashfast, HF_USB_CMD(OP_USB_SHUTDOWN), 0, NULL, 0);
  251. }
  252. static void hfa_clear_readbuf(struct cgpu_info *hashfast)
  253. {
  254. int amount, ret;
  255. char buf[512];
  256. do {
  257. ret = usb_read(hashfast, buf, 512, &amount, C_HF_CLEAR_READ);
  258. } while (!ret || amount);
  259. }
  260. static bool hfa_detect_common(struct cgpu_info *hashfast)
  261. {
  262. struct hashfast_info *info;
  263. bool ret;
  264. info = calloc(sizeof(struct hashfast_info), 1);
  265. if (!info)
  266. quit(1, "Failed to calloc hashfast_info in hfa_detect_common");
  267. hashfast->device_data = info;
  268. hfa_clear_readbuf(hashfast);
  269. /* hashfast_reset should fill in details for info */
  270. ret = hfa_reset(hashfast, info);
  271. if (!ret) {
  272. hfa_send_shutdown(hashfast);
  273. hfa_clear_readbuf(hashfast);
  274. free(info);
  275. hashfast->device_data = NULL;
  276. return false;
  277. }
  278. // The per-die status array
  279. info->die_status = calloc(info->asic_count, sizeof(struct hf_g1_die_data));
  280. if (unlikely(!(info->die_status)))
  281. quit(1, "Failed to calloc die_status");
  282. // The per-die statistics array
  283. info->die_statistics = calloc(info->asic_count, sizeof(struct hf_long_statistics));
  284. if (unlikely(!(info->die_statistics)))
  285. quit(1, "Failed to calloc die_statistics");
  286. info->works = calloc(sizeof(struct work *), info->num_sequence);
  287. if (!info->works)
  288. quit(1, "Failed to calloc info works in hfa_detect_common");
  289. return true;
  290. }
  291. static void hfa_initialise(struct cgpu_info *hashfast)
  292. {
  293. if (hashfast->usbinfo.nodev)
  294. return;
  295. usb_buffer_enable(hashfast);
  296. // FIXME Do necessary initialising here
  297. }
  298. static bool hfa_detect_one_usb(libusb_device *dev, struct usb_find_devices *found)
  299. {
  300. struct cgpu_info *hashfast;
  301. hashfast = usb_alloc_cgpu(&hashfast_drv, HASHFAST_MINER_THREADS);
  302. if (!hashfast)
  303. quit(1, "Failed to usb_alloc_cgpu hashfast");
  304. if (!usb_init(hashfast, dev, found)) {
  305. hashfast = usb_free_cgpu(hashfast);
  306. return false;
  307. }
  308. hashfast->usbdev->usb_type = USB_TYPE_STD;
  309. hfa_initialise(hashfast);
  310. add_cgpu(hashfast);
  311. return hfa_detect_common(hashfast);
  312. }
  313. static void hfa_detect(bool hotplug)
  314. {
  315. /* Set up the CRC tables only once. */
  316. if (!hotplug)
  317. hfa_init_crc8();
  318. usb_detect(&hashfast_drv, hfa_detect_one_usb);
  319. }
  320. static bool hfa_get_packet(struct cgpu_info *hashfast, struct hf_header *h)
  321. {
  322. uint8_t hcrc;
  323. bool ret;
  324. ret = hfa_get_header(hashfast, h, &hcrc);
  325. if (unlikely(!ret))
  326. goto out;
  327. if (unlikely(h->crc8 != hcrc)) {
  328. applog(LOG_WARNING, "HFA %d: Bad CRC %d vs %d, attempting to process anyway",
  329. hashfast->device_id, h->crc8, hcrc);
  330. }
  331. if (h->data_length > 0)
  332. ret = hfa_get_data(hashfast, (char *)(h + 1), h->data_length);
  333. if (unlikely(!ret)) {
  334. applog(LOG_WARNING, "HFA %d: Failed to get data associated with header",
  335. hashfast->device_id);
  336. }
  337. out:
  338. return ret;
  339. }
  340. static void hfa_parse_gwq_status(struct cgpu_info *hashfast, struct hashfast_info *info,
  341. struct hf_header *h)
  342. {
  343. struct hf_gwq_data *g = (struct hf_gwq_data *)(h + 1);
  344. struct work *work;
  345. applog(LOG_DEBUG, "HFA %d: OP_GWQ_STATUS, device_head %4d tail %4d my tail %4d shed %3d inflight %4d",
  346. hashfast->device_id, g->sequence_head, g->sequence_tail, info->hash_sequence_tail,
  347. g->shed_count, SEQUENCE_DISTANCE(info->hash_sequence_head,g->sequence_tail));
  348. mutex_lock(&info->lock);
  349. info->hash_count += g->hash_count;
  350. info->device_sequence_head = g->sequence_head;
  351. info->device_sequence_tail = g->sequence_tail;
  352. info->shed_count = g->shed_count;
  353. /* Free any work that is no longer required */
  354. while (info->device_sequence_tail != info->hash_sequence_tail) {
  355. if (++info->hash_sequence_tail >= info->num_sequence)
  356. info->hash_sequence_tail = 0;
  357. if (unlikely(!(work = info->works[info->hash_sequence_tail]))) {
  358. applog(LOG_ERR, "HFA %d: Bad work sequence tail",
  359. hashfast->device_id);
  360. hashfast->shutdown = true;
  361. break;
  362. }
  363. applog(LOG_DEBUG, "HFA %d: Completing work on hash_sequence_tail %d",
  364. hashfast->device_id, info->hash_sequence_tail);
  365. free_work(work);
  366. info->works[info->hash_sequence_tail] = NULL;
  367. }
  368. mutex_unlock(&info->lock);
  369. }
  370. static void hfa_update_die_status(struct cgpu_info *hashfast, struct hashfast_info *info,
  371. struct hf_header *h)
  372. {
  373. struct hf_g1_die_data *d = (struct hf_g1_die_data *)(h + 1), *ds;
  374. int num_included = (h->data_length * 4) / sizeof(struct hf_g1_die_data);
  375. int i, j;
  376. float die_temperature;
  377. float core_voltage[6];
  378. if (info->device_type == HFD_G1) {
  379. // Copy in the data. They're numbered sequentially from the starting point
  380. ds = info->die_status + h->chip_address;
  381. for (i = 0; i < num_included; i++)
  382. memcpy(ds++, d++, sizeof(struct hf_g1_die_data));
  383. for (i = 0, d = &info->die_status[h->chip_address]; i < num_included; i++, d++) {
  384. die_temperature = GN_DIE_TEMPERATURE(d->die.die_temperature);
  385. for (j = 0; j < 6; j++)
  386. core_voltage[j] = GN_CORE_VOLTAGE(d->die.core_voltage[j]);
  387. applog(LOG_DEBUG, "HFA %d: die %2d: OP_DIE_STATUS Die temp %.2fC vdd's %.2f %.2f %.2f %.2f %.2f %.2f",
  388. hashfast->device_id, h->chip_address + i, die_temperature,
  389. core_voltage[0], core_voltage[1], core_voltage[2],
  390. core_voltage[3], core_voltage[4], core_voltage[5]);
  391. // XXX Convert board phase currents, voltage, temperature
  392. }
  393. }
  394. }
  395. static void search_for_extra_nonce(struct thr_info *thr, struct work *work,
  396. struct hf_candidate_nonce *n)
  397. {
  398. uint32_t nonce = n->nonce;
  399. int i;
  400. /* No function to test with ntime offsets yet */
  401. if (n->ntime)
  402. return;
  403. for (i = 0; i < 128; i++, nonce++) {
  404. /* We could break out of this early if nonce wraps or if we
  405. * find one correct nonce since the chance of more is extremely
  406. * low but this function will be hit so infrequently we may as
  407. * well test the entire range with the least code. */
  408. if (test_nonce(work, nonce))
  409. submit_tested_work(thr, work);
  410. }
  411. }
  412. static void hfa_parse_nonce(struct thr_info *thr, struct cgpu_info *hashfast,
  413. struct hashfast_info *info, struct hf_header *h)
  414. {
  415. struct hf_candidate_nonce *n = (struct hf_candidate_nonce *)(h + 1);
  416. int i, num_nonces = h->data_length / U32SIZE(sizeof(struct hf_candidate_nonce));
  417. applog(LOG_DEBUG, "HFA %d: OP_NONCE: %2d:, num_nonces %d hdata 0x%04x",
  418. hashfast->device_id, h->chip_address, num_nonces, h->hdata);
  419. for (i = 0; i < num_nonces; i++, n++) {
  420. struct work *work;
  421. applog(LOG_DEBUG, "HFA %d: OP_NONCE: %2d: %2d: search %1d ntime %2d sequence %4d nonce 0x%08x",
  422. hashfast->device_id, h->chip_address, i, n->search, n->ntime, n->sequence, n->nonce);
  423. // Find the job from the sequence number
  424. mutex_lock(&info->lock);
  425. work = info->works[n->sequence];
  426. mutex_unlock(&info->lock);
  427. if (unlikely(!work)) {
  428. info->no_matching_work++;
  429. applog(LOG_INFO, "HFA %d: No matching work!", hashfast->device_id);
  430. } else {
  431. applog(LOG_DEBUG, "HFA %d: OP_NONCE: sequence %d: submitting nonce 0x%08x ntime %d",
  432. hashfast->device_id, n->sequence, n->nonce, n->ntime);
  433. if ((n->nonce & 0xffff0000) == 0x42420000) // XXX REMOVE THIS
  434. break; // XXX PHONEY EMULATOR NONCE
  435. submit_noffset_nonce(thr, work, n->nonce, n->ntime); // XXX Return value from submit_nonce is error if set
  436. if (unlikely(n->search)) {
  437. /* This tells us there is another share in the
  438. * next 128 nonces */
  439. applog(LOG_DEBUG, "HFA %d: OP_NONCE: SEARCH PROXIMITY EVENT FOUND",
  440. hashfast->device_id);
  441. search_for_extra_nonce(thr, work, n);
  442. }
  443. }
  444. }
  445. }
  446. static void hfa_update_die_statistics(struct hashfast_info *info, struct hf_header *h)
  447. {
  448. struct hf_statistics *s = (struct hf_statistics *)(h + 1);
  449. struct hf_long_statistics *l;
  450. // Accumulate the data
  451. l = info->die_statistics + h->chip_address;
  452. l->rx_header_crc += s->rx_header_crc;
  453. l->rx_body_crc += s->rx_body_crc;
  454. l->rx_header_timeouts += s->rx_header_timeouts;
  455. l->rx_body_timeouts += s->rx_body_timeouts;
  456. l->core_nonce_fifo_full += s->core_nonce_fifo_full;
  457. l->array_nonce_fifo_full += s->array_nonce_fifo_full;
  458. l->stats_overrun += s->stats_overrun;
  459. }
  460. static void hfa_update_stats1(struct cgpu_info *hashfast, struct hashfast_info *info,
  461. struct hf_header *h)
  462. {
  463. struct hf_long_usb_stats1 *s1 = &info->stats1;
  464. struct hf_usb_stats1 *sd = (struct hf_usb_stats1 *)(h + 1);
  465. s1->usb_rx_preambles += sd->usb_rx_preambles;
  466. s1->usb_rx_receive_byte_errors += sd->usb_rx_receive_byte_errors;
  467. s1->usb_rx_bad_hcrc += sd->usb_rx_bad_hcrc;
  468. s1->usb_tx_attempts += sd->usb_tx_attempts;
  469. s1->usb_tx_packets += sd->usb_tx_packets;
  470. s1->usb_tx_timeouts += sd->usb_tx_timeouts;
  471. s1->usb_tx_incompletes += sd->usb_tx_incompletes;
  472. s1->usb_tx_endpointstalled += sd->usb_tx_endpointstalled;
  473. s1->usb_tx_disconnected += sd->usb_tx_disconnected;
  474. s1->usb_tx_suspended += sd->usb_tx_suspended;
  475. #if 0
  476. /* We don't care about UART stats so they're not in our struct */
  477. s1->uart_tx_queue_dma += sd->uart_tx_queue_dma;
  478. s1->uart_tx_interrupts += sd->uart_tx_interrupts;
  479. s1->uart_rx_preamble_ints += sd->uart_rx_preamble_ints;
  480. s1->uart_rx_missed_preamble_ints += sd->uart_rx_missed_preamble_ints;
  481. s1->uart_rx_header_done += sd->uart_rx_header_done;
  482. s1->uart_rx_data_done += sd->uart_rx_data_done;
  483. s1->uart_rx_bad_hcrc += sd->uart_rx_bad_hcrc;
  484. s1->uart_rx_bad_dma += sd->uart_rx_bad_dma;
  485. s1->uart_rx_short_dma += sd->uart_rx_short_dma;
  486. s1->uart_rx_buffers_full += sd->uart_rx_buffers_full;
  487. #endif
  488. if (sd->max_tx_buffers > s1->max_tx_buffers)
  489. s1->max_tx_buffers = sd->max_tx_buffers;
  490. if (sd->max_rx_buffers > s1->max_rx_buffers)
  491. s1->max_rx_buffers = sd->max_rx_buffers;
  492. applog(LOG_DEBUG, "HFA %d: OP_USB_STATS1:", hashfast->device_id);
  493. applog(LOG_DEBUG, " usb_rx_preambles: %6d", sd->usb_rx_preambles);
  494. applog(LOG_DEBUG, " usb_rx_receive_byte_errors: %6d", sd->usb_rx_receive_byte_errors);
  495. applog(LOG_DEBUG, " usb_rx_bad_hcrc: %6d", sd->usb_rx_bad_hcrc);
  496. applog(LOG_DEBUG, " usb_tx_attempts: %6d", sd->usb_tx_attempts);
  497. applog(LOG_DEBUG, " usb_tx_packets: %6d", sd->usb_tx_packets);
  498. applog(LOG_DEBUG, " usb_tx_timeouts: %6d", sd->usb_tx_timeouts);
  499. applog(LOG_DEBUG, " usb_tx_incompletes: %6d", sd->usb_tx_incompletes);
  500. applog(LOG_DEBUG, " usb_tx_endpointstalled: %6d", sd->usb_tx_endpointstalled);
  501. applog(LOG_DEBUG, " usb_tx_disconnected: %6d", sd->usb_tx_disconnected);
  502. applog(LOG_DEBUG, " usb_tx_suspended: %6d", sd->usb_tx_suspended);
  503. #if 0
  504. applog(LOG_DEBUG, " uart_tx_queue_dma: %6d", sd->uart_tx_queue_dma);
  505. applog(LOG_DEBUG, " uart_tx_interrupts: %6d", sd->uart_tx_interrupts);
  506. applog(LOG_DEBUG, " uart_rx_preamble_ints: %6d", sd->uart_rx_preamble_ints);
  507. applog(LOG_DEBUG, " uart_rx_missed_preamble_ints: %6d", sd->uart_rx_missed_preamble_ints);
  508. applog(LOG_DEBUG, " uart_rx_header_done: %6d", sd->uart_rx_header_done);
  509. applog(LOG_DEBUG, " uart_rx_data_done: %6d", sd->uart_rx_data_done);
  510. applog(LOG_DEBUG, " uart_rx_bad_hcrc: %6d", sd->uart_rx_bad_hcrc);
  511. applog(LOG_DEBUG, " uart_rx_bad_dma: %6d", sd->uart_rx_bad_dma);
  512. applog(LOG_DEBUG, " uart_rx_short_dma: %6d", sd->uart_rx_short_dma);
  513. applog(LOG_DEBUG, " uart_rx_buffers_full: %6d", sd->uart_rx_buffers_full);
  514. #endif
  515. applog(LOG_DEBUG, " max_tx_buffers: %6d", sd->max_tx_buffers);
  516. applog(LOG_DEBUG, " max_rx_buffers: %6d", sd->max_rx_buffers);
  517. }
  518. static void *hfa_read(void *arg)
  519. {
  520. struct thr_info *thr = (struct thr_info *)arg;
  521. struct cgpu_info *hashfast = thr->cgpu;
  522. struct hashfast_info *info = hashfast->device_data;
  523. char threadname[24];
  524. snprintf(threadname, 24, "hfa_read/%d", hashfast->device_id);
  525. RenameThread(threadname);
  526. while (likely(!hashfast->shutdown)) {
  527. char buf[512];
  528. struct hf_header *h = (struct hf_header *)buf;
  529. bool ret = hfa_get_packet(hashfast, h);
  530. if (unlikely(!ret))
  531. continue;
  532. switch (h->operation_code) {
  533. case OP_GWQ_STATUS:
  534. hfa_parse_gwq_status(hashfast, info, h);
  535. break;
  536. case OP_DIE_STATUS:
  537. hfa_update_die_status(hashfast, info, h);
  538. break;
  539. case OP_NONCE:
  540. hfa_parse_nonce(thr, hashfast, info, h);
  541. break;
  542. case OP_STATISTICS:
  543. hfa_update_die_statistics(info, h);
  544. break;
  545. case OP_USB_STATS1:
  546. hfa_update_stats1(hashfast, info, h);
  547. break;
  548. default:
  549. applog(LOG_WARNING, "HFA %d: Unhandled operation code %d",
  550. hashfast->device_id, h->operation_code);
  551. break;
  552. }
  553. }
  554. return NULL;
  555. }
  556. static bool hfa_prepare(struct thr_info *thr)
  557. {
  558. struct cgpu_info *hashfast = thr->cgpu;
  559. struct hashfast_info *info = hashfast->device_data;
  560. struct timeval now;
  561. mutex_init(&info->lock);
  562. if (pthread_create(&info->read_thr, NULL, hfa_read, (void *)thr))
  563. quit(1, "Failed to pthread_create read thr in hfa_prepare");
  564. cgtime(&now);
  565. get_datestamp(hashfast->init, sizeof(hashfast->init), &now);
  566. return true;
  567. }
  568. /* Figure out how many jobs to send. */
  569. static int __hfa_jobs(struct hashfast_info *info)
  570. {
  571. return info->usb_init_base.inflight_target - HF_SEQUENCE_DISTANCE(info->hash_sequence, info->device_sequence_tail);
  572. }
  573. static int hfa_jobs(struct hashfast_info *info)
  574. {
  575. int ret;
  576. mutex_lock(&info->lock);
  577. ret = __hfa_jobs(info);
  578. mutex_unlock(&info->lock);
  579. return ret;
  580. }
  581. static int64_t hfa_scanwork(struct thr_info *thr)
  582. {
  583. struct cgpu_info *hashfast = thr->cgpu;
  584. struct hashfast_info *info = hashfast->device_data;
  585. int64_t hashes;
  586. int jobs, ret;
  587. if (unlikely(thr->work_restart)) {
  588. restart:
  589. ret = hfa_send_frame(hashfast, HF_USB_CMD(OP_WORK_RESTART), 0, (uint8_t *)NULL, 0);
  590. if (unlikely(!ret)) {
  591. ret = hfa_reset(hashfast, info);
  592. if (unlikely(!ret)) {
  593. applog(LOG_ERR, "HFA %d: Failed to reset after write failure, disabling",
  594. hashfast->device_id);
  595. return -1;
  596. }
  597. }
  598. }
  599. jobs = hfa_jobs(info);
  600. if (!jobs) {
  601. ret = restart_wait(thr, 100);
  602. if (unlikely(!ret))
  603. goto restart;
  604. jobs = hfa_jobs(info);
  605. }
  606. while (jobs > 0) {
  607. struct hf_hash_usb op_hash_data;
  608. struct work *work;
  609. uint64_t intdiff;
  610. int i, sequence;
  611. uint32_t *p;
  612. /* This is a blocking function if there's no work */
  613. work = get_work(thr, thr->id);
  614. /* Assemble the data frame and send the OP_HASH packet */
  615. memcpy(op_hash_data.midstate, work->midstate, sizeof(op_hash_data.midstate));
  616. memcpy(op_hash_data.merkle_residual, work->data + 64, 4);
  617. p = (uint32_t *)(work->data + 64 + 4);
  618. op_hash_data.timestamp = *p++;
  619. op_hash_data.bits = *p++;
  620. op_hash_data.nonce_loops = 0;
  621. /* Set the number of leading zeroes to look for based on diff.
  622. * Diff 1 = 32, Diff 2 = 33, Diff 4 = 34 etc. */
  623. intdiff = (uint64_t)work->device_diff;
  624. for (i = 31; intdiff; i++, intdiff >>= 1);
  625. op_hash_data.search_difficulty = i;
  626. if ((sequence = info->hash_sequence + 1) >= info->num_sequence)
  627. sequence = 0;
  628. ret = hfa_send_frame(hashfast, OP_HASH, sequence, (uint8_t *)&op_hash_data, sizeof(op_hash_data));
  629. if (unlikely(!ret)) {
  630. ret = hfa_reset(hashfast, info);
  631. if (unlikely(!ret)) {
  632. applog(LOG_ERR, "HFA %d: Failed to reset after write failure, disabling",
  633. hashfast->device_id);
  634. return -1;
  635. }
  636. }
  637. mutex_lock(&info->lock);
  638. info->hash_sequence = sequence;
  639. *(info->works + info->hash_sequence) = work;
  640. jobs = __hfa_jobs(info);
  641. mutex_unlock(&info->lock);
  642. applog(LOG_DEBUG, "HFA %d: OP_HASH sequence %d search_difficulty %d work_difficulty %g",
  643. hashfast->device_id, info->hash_sequence, op_hash_data.search_difficulty, work->work_difficulty);
  644. }
  645. mutex_lock(&info->lock);
  646. hashes = info->hash_count;
  647. info->hash_count = 0;
  648. mutex_unlock(&info->lock);
  649. return hashes;
  650. }
  651. static struct api_data *hfa_api_stats(struct cgpu_info *cgpu)
  652. {
  653. struct hashfast_info *info = cgpu->device_data;
  654. struct hf_long_usb_stats1 *s1;
  655. struct api_data *root = NULL;
  656. struct hf_usb_init_base *db;
  657. int varint, i;
  658. char buf[64];
  659. root = api_add_int(root, "asic count", &info->asic_count, false);
  660. root = api_add_int(root, "core count", &info->core_count, false);
  661. db = &info->usb_init_base;
  662. sprintf(buf, "%d.%d", (db->firmware_rev >> 8) & 0xff, db->firmware_rev & 0xff);
  663. root = api_add_string(root, "firmware rev", buf, true);
  664. sprintf(buf, "%d.%d", (db->hardware_rev >> 8) & 0xff, db->hardware_rev & 0xff);
  665. root = api_add_string(root, "hardware rev", buf, true);
  666. varint = db->serial_number;
  667. root = api_add_int(root, "serial number", &varint, true);
  668. varint = db->hash_clockrate;
  669. root = api_add_int(root, "hash clockrate", &varint, true);
  670. varint = db->inflight_target;
  671. root = api_add_int(root, "inflight target", &varint, true);
  672. varint = db->sequence_modulus;
  673. root = api_add_int(root, "sequence modules", &varint, true);
  674. s1 = &info->stats1;
  675. root = api_add_uint64(root, "rx preambles", &s1->usb_rx_preambles, false);
  676. root = api_add_uint64(root, "rx rcv byte err", &s1->usb_rx_receive_byte_errors, false);
  677. root = api_add_uint64(root, "rx bad hcrc", &s1->usb_rx_bad_hcrc, false);
  678. root = api_add_uint64(root, "tx attempts", &s1->usb_tx_attempts, false);
  679. root = api_add_uint64(root, "tx packets", &s1->usb_tx_packets, false);
  680. root = api_add_uint64(root, "tx incompletes", &s1->usb_tx_incompletes, false);
  681. root = api_add_uint64(root, "tx ep stalled", &s1->usb_tx_endpointstalled, false);
  682. root = api_add_uint64(root, "tx disconnect", &s1->usb_tx_disconnected, false);
  683. root = api_add_uint64(root, "tx suspend", &s1->usb_tx_suspended, false);
  684. varint = s1->max_tx_buffers;
  685. root = api_add_int(root, "max tx buf", &varint, true);
  686. varint = s1->max_rx_buffers;
  687. root = api_add_int(root, "max rx buf", &varint, true);
  688. for (i = 0; i < info->asic_count; i++) {
  689. struct hf_long_statistics *l = &info->die_statistics[i];
  690. struct hf_g1_die_data *d = &info->die_status[i];
  691. double die_temp, core_voltage;
  692. int j;
  693. root = api_add_int(root, "Core", &i, true);
  694. die_temp = GN_DIE_TEMPERATURE(d->die.die_temperature);
  695. root = api_add_double(root, "die temperature", &die_temp, true);
  696. for (j = 0; j < 6; j++) {
  697. core_voltage = GN_CORE_VOLTAGE(d->die.core_voltage[j]);
  698. sprintf(buf, "%d: %.2f", j, core_voltage);
  699. root = api_add_string(root, "core voltage", buf, true);
  700. }
  701. root = api_add_uint64(root, "rx header crc", &l->rx_header_crc, false);
  702. root = api_add_uint64(root, "rx body crc", &l->rx_body_crc, false);
  703. root = api_add_uint64(root, "rx header to", &l->rx_header_timeouts, false);
  704. root = api_add_uint64(root, "rx body to", &l->rx_body_timeouts, false);
  705. root = api_add_uint64(root, "cn fifo full", &l->core_nonce_fifo_full, false);
  706. root = api_add_uint64(root, "an fifo full", &l->array_nonce_fifo_full, false);
  707. root = api_add_uint64(root, "stats overrun", &l->stats_overrun, false);
  708. }
  709. return root;
  710. }
  711. static void hfa_init(struct cgpu_info __maybe_unused *hashfast)
  712. {
  713. }
  714. static void hfa_free_all_work(struct hashfast_info *info)
  715. {
  716. while (info->device_sequence_tail != info->hash_sequence_head) {
  717. struct work *work;
  718. if (++info->hash_sequence_tail >= info->num_sequence)
  719. info->hash_sequence_tail = 0;
  720. if (unlikely(!(work = info->works[info->hash_sequence_tail])))
  721. break;
  722. free_work(work);
  723. info->works[info->hash_sequence_tail] = NULL;
  724. }
  725. }
  726. static void hfa_shutdown(struct thr_info *thr)
  727. {
  728. struct cgpu_info *hashfast = thr->cgpu;
  729. struct hashfast_info *info = hashfast->device_data;
  730. hfa_send_shutdown(hashfast);
  731. pthread_join(info->read_thr, NULL);
  732. hfa_free_all_work(info);
  733. hfa_clear_readbuf(hashfast);
  734. usb_buffer_disable(hashfast);
  735. free(info->works);
  736. free(info->die_statistics);
  737. free(info->die_status);
  738. free(info);
  739. }
  740. struct device_drv hashfast_drv = {
  741. .drv_id = DRIVER_hashfast,
  742. .dname = "Hashfast",
  743. .name = "HFA",
  744. .max_diff = 256.0, // Limit max diff to get some nonces back regardless
  745. .drv_detect = hfa_detect,
  746. .thread_prepare = hfa_prepare,
  747. .hash_work = &hash_driver_work,
  748. .scanwork = hfa_scanwork,
  749. .get_api_stats = hfa_api_stats,
  750. .reinit_device = hfa_init,
  751. .thread_shutdown = hfa_shutdown,
  752. };