driver-hashfast.c 30 KB

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