driver-hashfast.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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 hf_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 __maybe_unused hf_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 hf_cmd {
  41. int cmd;
  42. char *cmd_name;
  43. enum usb_cmds usb_cmd;
  44. };
  45. /* Entries in this array need to align with the actual op values specified
  46. * in hf_protocol.h */
  47. #define C_NULL C_MAX
  48. static const struct hf_cmd hf_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 hashfast_send_frame(struct cgpu_info *hashfast, uint8_t opcode,
  84. uint16_t hdata, uint8_t *data, int len)
  85. {
  86. int tx_length, ret, amount, id = hashfast->device_id;
  87. uint8_t packet[256];
  88. struct hf_header *p = (struct hf_header *)packet;
  89. p->preamble = HF_PREAMBLE;
  90. p->operation_code = opcode;
  91. p->chip_address = HF_GWQ_ADDRESS;
  92. p->core_address = 0;
  93. p->hdata = htole16(hdata);
  94. p->data_length = len / 4;
  95. p->crc8 = hf_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. hf_cmds[opcode].usb_cmd);
  101. if (unlikely(ret < 0 || amount != tx_length)) {
  102. applog(LOG_ERR, "HFA %d: hashfast_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 hashfast_send_header(struct cgpu_info *hashfast, struct hf_header *h,
  109. int cmd)
  110. {
  111. int amount, ret, len;
  112. len = sizeof(*h);
  113. ret = usb_write(hashfast, (char *)h, len, &amount, hf_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, hf_cmds[cmd].cmd_name, ret, amount, len);
  117. return false;
  118. }
  119. return true;
  120. }
  121. static bool hashfast_get_header(struct cgpu_info *hashfast, struct hf_header *h,
  122. uint8_t *computed_crc)
  123. {
  124. int amount, ret, orig_len, len, ofs = 0, reads = 0;
  125. char buf[512];
  126. char *header;
  127. /* Read for up to 200ms till we find the first occurrence of HF_PREAMBLE
  128. * though it should be the first byte unless we get woefully out of
  129. * sync. */
  130. orig_len = len = sizeof(*h);
  131. do {
  132. if (++reads > 20)
  133. return false;
  134. ret = usb_read_timeout(hashfast, buf + ofs, len, &amount, 10, C_HF_GETHEADER);
  135. if (unlikely(ret && ret != LIBUSB_ERROR_TIMEOUT))
  136. return false;
  137. ofs += amount;
  138. header = memchr(buf, HF_PREAMBLE, ofs);
  139. if (header)
  140. len -= ofs - (header - buf);
  141. } while (len);
  142. memcpy(h, header, orig_len);
  143. *computed_crc = hf_crc8((uint8_t *)h);
  144. return true;
  145. }
  146. static bool hashfast_get_data(struct cgpu_info *hashfast, char *buf, int len4)
  147. {
  148. int amount, ret, len = len4 * 4;
  149. ret = usb_read(hashfast, buf, len, &amount, C_HF_GETDATA);
  150. if (ret)
  151. return false;
  152. if (amount != len) {
  153. applog(LOG_WARNING, "HFA %d: get_data: Strange amount returned %d vs. expected %d",
  154. hashfast->device_id, amount, len);
  155. return false;
  156. }
  157. return true;
  158. }
  159. static bool hashfast_reset(struct cgpu_info *hashfast, struct hashfast_info *info)
  160. {
  161. struct hf_usb_init_header usb_init, *hu = &usb_init;
  162. struct hf_usb_init_base *db;
  163. char buf[1024];
  164. struct hf_header *h = (struct hf_header *)buf;
  165. uint8_t hcrc;
  166. bool ret;
  167. int i;
  168. info->hash_clock_rate = 550; // Hash clock rate in Mhz
  169. // Assemble the USB_INIT request
  170. memset(hu, 0, sizeof(*hu));
  171. hu->preamble = HF_PREAMBLE;
  172. hu->operation_code = OP_USB_INIT;
  173. hu->protocol = PROTOCOL_GLOBAL_WORK_QUEUE; // Protocol to use
  174. hu->hash_clock = info->hash_clock_rate; // Hash clock rate in Mhz
  175. hu->crc8 = hf_crc8((uint8_t *)hu);
  176. applog(LOG_WARNING, "HFA%d: Sending OP_USB_INIT with GWQ protocol specified",
  177. hashfast->device_id);
  178. if (!hashfast_send_header(hashfast, (struct hf_header *)hu, HF_USB_CMD(OP_USB_INIT)))
  179. return false;
  180. // Check for the correct response.
  181. // We extend the normal timeout - a complete device initialization, including
  182. // bringing power supplies up from standby, etc., can take over a second.
  183. for (i = 0; i < 30; i++) {
  184. ret = hashfast_get_header(hashfast, h, &hcrc);
  185. if (ret)
  186. break;
  187. }
  188. if (!ret) {
  189. applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed!", hashfast->device_id);
  190. return false;
  191. }
  192. if (h->crc8 != hcrc) {
  193. applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed! CRC mismatch", hashfast->device_id);
  194. return false;
  195. }
  196. if (h->operation_code != OP_USB_INIT) {
  197. applog(LOG_WARNING, "HFA %d: OP_USB_INIT: Tossing packet, valid but unexpected type", hashfast->device_id);
  198. hashfast_get_data(hashfast, buf, h->data_length);
  199. return false;
  200. }
  201. applog(LOG_DEBUG, "HFA %d: Good reply to OP_USB_INIT", hashfast->device_id);
  202. applog(LOG_DEBUG, "HFA %d: OP_USB_INIT: %d die in chain, %d cores, device_type %d, refclk %d Mhz",
  203. hashfast->device_id, h->chip_address, h->core_address, h->hdata & 0xff, (h->hdata >> 8) & 0xff);
  204. // Save device configuration
  205. info->asic_count = h->chip_address;
  206. info->core_count = h->core_address;
  207. info->device_type = (uint8_t)h->hdata;
  208. info->ref_frequency = (uint8_t)(h->hdata>>8);
  209. info->hash_sequence = 0;
  210. info->hash_sequence_tail = 0;
  211. info->device_sequence_tail = 0;
  212. // Size in bytes of the core bitmap in bytes
  213. info->core_bitmap_size = (((info->asic_count * info->core_count) + 31) / 32) * 4;
  214. // Get the usb_init_base structure
  215. if (!hashfast_get_data(hashfast, (char *)&info->usb_init_base, U32SIZE(info->usb_init_base))) {
  216. applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed! Failure to get usb_init_base data",
  217. hashfast->device_id);
  218. return false;
  219. }
  220. db = &info->usb_init_base;
  221. applog(LOG_INFO, "HFA %d: firmware_rev: %d.%d", hashfast->device_id,
  222. (db->firmware_rev >> 8) & 0xff, db->firmware_rev & 0xff);
  223. applog(LOG_INFO, "HFA %d: hardware_rev: %d.%d", hashfast->device_id,
  224. (db->hardware_rev >> 8) & 0xff, db->hardware_rev & 0xff);
  225. applog(LOG_INFO, "HFA %d: serial number: %d", hashfast->device_id,
  226. db->serial_number);
  227. applog(LOG_INFO, "HFA %d: hash clockrate: %d Mhz", hashfast->device_id,
  228. db->hash_clockrate);
  229. applog(LOG_INFO, "HFA %d: inflight_target: %d", hashfast->device_id,
  230. db->inflight_target);
  231. // Now a copy of the config data used
  232. if (!hashfast_get_data(hashfast, (char *)&info->config_data, U32SIZE(info->config_data))) {
  233. applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed! Failure to get config_data",
  234. hashfast->device_id);
  235. return false;
  236. }
  237. // Now the core bitmap
  238. info->core_bitmap = malloc(info->core_bitmap_size);
  239. if (!info->core_bitmap)
  240. quit(1, "Failed to malloc info core bitmap in hashfast_reset");
  241. if (!hashfast_get_data(hashfast, (char *)info->core_bitmap, info->core_bitmap_size / 4)) {
  242. applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed! Failure to get core_bitmap", hashfast->device_id);
  243. return false;
  244. }
  245. return true;
  246. }
  247. static bool hashfast_detect_common(struct cgpu_info *hashfast)
  248. {
  249. struct hashfast_info *info;
  250. bool ret;
  251. info = calloc(sizeof(struct hashfast_info), 1);
  252. if (!info)
  253. quit(1, "Failed to calloc hashfast_info in hashfast_detect_common");
  254. hashfast->device_data = info;
  255. /* hashfast_reset should fill in details for info */
  256. ret = hashfast_reset(hashfast, info);
  257. if (!ret) {
  258. free(info);
  259. hashfast->device_data = NULL;
  260. return false;
  261. }
  262. // The per-die status array
  263. info->die_status = calloc(info->asic_count, sizeof(struct hf_g1_die_data));
  264. if (unlikely(!(info->die_status)))
  265. quit(1, "Failed to calloc die_status");
  266. // The per-die statistics array
  267. info->die_statistics = calloc(info->asic_count, sizeof(struct hf_long_statistics));
  268. if (unlikely(!(info->die_statistics)))
  269. quit(1, "Failed to calloc die_statistics");
  270. info->works = calloc(sizeof(struct work *), HF_NUM_SEQUENCE);
  271. if (!info->works)
  272. quit(1, "Failed to calloc info works in hashfast_detect_common");
  273. return true;
  274. }
  275. static void hashfast_initialise(struct cgpu_info *hashfast)
  276. {
  277. if (hashfast->usbinfo.nodev)
  278. return;
  279. // FIXME Do necessary initialising here
  280. }
  281. static bool hashfast_detect_one_usb(libusb_device *dev, struct usb_find_devices *found)
  282. {
  283. struct cgpu_info *hashfast;
  284. hashfast = usb_alloc_cgpu(&hashfast_drv, HASHFAST_MINER_THREADS);
  285. if (!hashfast)
  286. quit(1, "Failed to usb_alloc_cgpu hashfast");
  287. if (!usb_init(hashfast, dev, found)) {
  288. hashfast = usb_free_cgpu(hashfast);
  289. return false;
  290. }
  291. hashfast->usbdev->usb_type = USB_TYPE_STD;
  292. hashfast_initialise(hashfast);
  293. add_cgpu(hashfast);
  294. return hashfast_detect_common(hashfast);
  295. }
  296. static void hashfast_detect(bool hotplug)
  297. {
  298. /* Set up the CRC tables only once. */
  299. if (!hotplug)
  300. hf_init_crc8();
  301. usb_detect(&hashfast_drv, hashfast_detect_one_usb);
  302. }
  303. static void *hf_read(void *arg)
  304. {
  305. struct thr_info *thr = (struct thr_info *)arg;
  306. struct cgpu_info *hashfast = thr->cgpu;
  307. struct hashfast_info *info = hashfast->device_data;
  308. while (likely(!hashfast->shutdown)) {
  309. }
  310. return NULL;
  311. }
  312. static bool hashfast_prepare(struct thr_info *thr)
  313. {
  314. struct cgpu_info *hashfast = thr->cgpu;
  315. struct hashfast_info *info = hashfast->device_data;
  316. struct timeval now;
  317. mutex_init(&info->lock);
  318. if (pthread_create(&info->read_thr, NULL, hf_read, (void *)thr))
  319. quit(1, "Failed to pthread_create read thr in hashfast_prepare");
  320. cgtime(&now);
  321. get_datestamp(hashfast->init, sizeof(hashfast->init), &now);
  322. return true;
  323. }
  324. /* Figure out how many jobs to send. */
  325. static int __hashfast_jobs(struct hashfast_info *info)
  326. {
  327. return info->usb_init_base.inflight_target - GWQ_SEQUENCE_DISTANCE(info->hash_sequence, info->device_sequence_tail);
  328. }
  329. static int hashfast_jobs(struct hashfast_info *info)
  330. {
  331. int ret;
  332. mutex_lock(&info->lock);
  333. ret = __hashfast_jobs(info);
  334. mutex_unlock(&info->lock);
  335. return ret;
  336. }
  337. static int64_t hashfast_scanwork(struct thr_info *thr)
  338. {
  339. struct cgpu_info *hashfast = thr->cgpu;
  340. struct hashfast_info *info = hashfast->device_data;
  341. int64_t hashes;
  342. int jobs, ret;
  343. if (unlikely(thr->work_restart)) {
  344. restart:
  345. ret = hashfast_send_frame(hashfast, OP_WORK_RESTART, 0, (uint8_t *)NULL, 0);
  346. if (unlikely(!ret)) {
  347. ret = hashfast_reset(hashfast, info);
  348. if (unlikely(!ret)) {
  349. applog(LOG_ERR, "HFA %d: Failed to reset after write failure, disabling",
  350. hashfast->device_id);
  351. return -1;
  352. }
  353. }
  354. }
  355. jobs = hashfast_jobs(info);
  356. if (!jobs) {
  357. ret = restart_wait(thr, 100);
  358. if (unlikely(!ret))
  359. goto restart;
  360. jobs = hashfast_jobs(info);
  361. }
  362. while (jobs > 0) {
  363. struct hf_hash_usb op_hash_data;
  364. struct work *work;
  365. uint64_t intdiff;
  366. int i, sequence;
  367. uint32_t *p;
  368. /* This is a blocking function if there's no work */
  369. work = get_work(thr, thr->id);
  370. /* Assemble the data frame and send the OP_HASH packet */
  371. memcpy(op_hash_data.midstate, work->midstate, sizeof(op_hash_data.midstate));
  372. memcpy(op_hash_data.merkle_residual, work->data + 64, 4);
  373. p = (uint32_t *)(work->data + 64 + 4);
  374. op_hash_data.timestamp = *p++;
  375. op_hash_data.bits = *p++;
  376. op_hash_data.nonce_loops = 0;
  377. /* Set the number of leading zeroes to look for based on diff.
  378. * Diff 1 = 32, Diff 2 = 33, Diff 4 = 34 etc. */
  379. intdiff = (uint64_t)work->device_diff;
  380. for (i = 31; intdiff; i++, intdiff >>= 1);
  381. op_hash_data.search_difficulty = i;
  382. if ((sequence = info->hash_sequence + 1) >= HF_NUM_SEQUENCE)
  383. sequence = 0;
  384. ret = hashfast_send_frame(hashfast, OP_HASH, sequence, (uint8_t *)&op_hash_data, sizeof(op_hash_data));
  385. if (unlikely(!ret)) {
  386. ret = hashfast_reset(hashfast, info);
  387. if (unlikely(!ret)) {
  388. applog(LOG_ERR, "HFA %d: Failed to reset after write failure, disabling",
  389. hashfast->device_id);
  390. return -1;
  391. }
  392. }
  393. mutex_lock(&info->lock);
  394. info->hash_sequence = sequence;
  395. *(info->works + info->hash_sequence) = work;
  396. jobs = __hashfast_jobs(info);
  397. mutex_unlock(&info->lock);
  398. applog(LOG_DEBUG, "HFA %d: OP_HASH sequence %d search_difficulty %d work_difficulty %g",
  399. hashfast->device_id, info->hash_sequence, op_hash_data.search_difficulty, work->work_difficulty);
  400. }
  401. mutex_lock(&info->lock);
  402. hashes = info->hash_count;
  403. info->hash_count = 0;
  404. mutex_unlock(&info->lock);
  405. return hashes;
  406. }
  407. static struct api_data *hashfast_api_stats(struct cgpu_info __maybe_unused *cgpu)
  408. {
  409. return NULL;
  410. }
  411. static void hashfast_init(struct cgpu_info *hashfast)
  412. {
  413. usb_buffer_enable(hashfast);
  414. }
  415. static void hashfast_shutdown(struct thr_info *thr)
  416. {
  417. struct cgpu_info *hashfast = thr->cgpu;
  418. struct hashfast_info *info = hashfast->device_data;
  419. pthread_join(info->read_thr, NULL);
  420. }
  421. struct device_drv hashfast_drv = {
  422. .drv_id = DRIVER_hashfast,
  423. .dname = "Hashfast",
  424. .name = "HFA",
  425. .drv_detect = hashfast_detect,
  426. .thread_prepare = hashfast_prepare,
  427. .hash_work = &hash_driver_work,
  428. .scanwork = hashfast_scanwork,
  429. .get_api_stats = hashfast_api_stats,
  430. .reinit_device = hashfast_init,
  431. .thread_shutdown = hashfast_shutdown,
  432. };