driver-hashfast.c 32 KB

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