driver-hashfast.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 int __maybe_unused hashfast_send_frame(struct cgpu_info *hashfast, uint8_t opcode,
  84. uint8_t chip, uint8_t core, 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 = opcode;
  92. p->chip_address = chip;
  93. p->core_address = core;
  94. p->hdata = htole16(hdata);
  95. p->data_length = len / 4;
  96. p->crc8 = hf_crc8(packet);
  97. if (len)
  98. memcpy(&packet[sizeof(struct hf_header)], data, len);
  99. tx_length = sizeof(struct hf_header) + len;
  100. tx_length = sizeof(struct hf_header);
  101. ret = usb_write(hashfast, (char *)packet, tx_length, &amount,
  102. hf_cmds[opcode].usb_cmd);
  103. if (ret < 0 || amount != tx_length) {
  104. applog(LOG_ERR, "HF%d: hashfast_send_frame: USB Send error, ret %d amount %d vs. tx_length %d",
  105. id, ret, amount, tx_length);
  106. return 1;
  107. }
  108. return 0;
  109. }
  110. static bool hashfast_send_header(struct cgpu_info *hashfast, struct hf_header *h,
  111. int cmd)
  112. {
  113. int amount, ret, len;
  114. len = sizeof(*h);
  115. ret = usb_write(hashfast, (char *)h, len, &amount, hf_cmds[cmd].usb_cmd);
  116. if (ret < 0 || amount != len) {
  117. applog(LOG_WARNING, "HFA%d: send_header: %s USB Send error, ret %d amount %d vs. length %d",
  118. hashfast->device_id, hf_cmds[cmd].cmd_name, ret, amount, len);
  119. return false;
  120. }
  121. return true;
  122. }
  123. static bool hashfast_get_header(struct cgpu_info *hashfast, struct hf_header *h,
  124. uint8_t *computed_crc)
  125. {
  126. int amount, ret, orig_len, len, ofs = 0, reads = 0;
  127. char buf[512];
  128. char *header;
  129. /* Read for up to 200ms till we find the first occurrence of HF_PREAMBLE
  130. * though it should be the first byte unless we get woefully out of
  131. * sync. */
  132. orig_len = len = sizeof(*h);
  133. do {
  134. if (++reads > 20)
  135. return false;
  136. ret = usb_read_timeout(hashfast, buf + ofs, len, &amount, 10, C_HF_GETHEADER);
  137. if (unlikely(ret && ret != LIBUSB_ERROR_TIMEOUT))
  138. return false;
  139. ofs += amount;
  140. header = memchr(buf, HF_PREAMBLE, ofs);
  141. if (header)
  142. len -= ofs - (header - buf);
  143. } while (len);
  144. memcpy(h, header, orig_len);
  145. *computed_crc = hf_crc8((uint8_t *)h);
  146. return true;
  147. }
  148. static bool hashfast_reset(struct cgpu_info *hashfast, struct hashfast_info *info)
  149. {
  150. struct hf_usb_init_header usb_init, *hu = &usb_init;
  151. struct hf_usb_init_base *db;
  152. uint8_t buf[1024];
  153. struct hf_header *h = (struct hf_header *)buf;
  154. uint8_t hcrc;
  155. uint8_t addr;
  156. uint16_t hdata;
  157. bool ret;
  158. int i;
  159. info->hash_clock_rate = 550; // Hash clock rate in Mhz
  160. // Assemble the USB_INIT request
  161. memset(hu, 0, sizeof(*hu));
  162. hu->preamble = HF_PREAMBLE;
  163. hu->operation_code = OP_USB_INIT;
  164. hu->protocol = PROTOCOL_GLOBAL_WORK_QUEUE; // Protocol to use
  165. hu->hash_clock = info->hash_clock_rate; // Hash clock rate in Mhz
  166. hu->crc8 = hf_crc8((uint8_t *)hu);
  167. applog(LOG_WARNING, "HFA%d: Sending OP_USB_INIT with GWQ protocol specified",
  168. hashfast->device_id);
  169. if (!hashfast_send_header(hashfast, (struct hf_header *)hu, HF_USB_CMD(OP_USB_INIT)))
  170. return false;
  171. // Check for the correct response.
  172. // We extend the normal timeout - a complete device initialization, including
  173. // bringing power supplies up from standby, etc., can take over a second.
  174. for (i = 0; i < 30; i++) {
  175. ret = hashfast_get_header(hashfast, h, &hcrc);
  176. if (ret)
  177. break;
  178. }
  179. if (!ret) {
  180. applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed!", hashfast->device_id);
  181. return false;
  182. }
  183. if (h->crc8 != hcrc) {
  184. applog(LOG_WARNING, "HFA %d: OP_USB_INIT failed! CRC mismatch", hashfast->device_id);
  185. return false;
  186. }
  187. if (h->operation_code != OP_USB_INIT) {
  188. applog(LOG_WARNING, "HFA %d: OP_USB_INIT: Tossing packet, valid but unexpected type", hashfast->device_id);
  189. return false;
  190. }
  191. return true;
  192. }
  193. static bool hashfast_detect_common(struct cgpu_info *hashfast)
  194. {
  195. struct hashfast_info *info;
  196. bool ret;
  197. info = calloc(sizeof(struct hashfast_info), 1);
  198. if (!info)
  199. quit(1, "Failed to calloc hashfast_info in hashfast_detect_common");
  200. hashfast->device_data = info;
  201. /* hashfast_reset should fill in details for info */
  202. ret = hashfast_reset(hashfast, info);
  203. if (!ret) {
  204. free(info);
  205. return false;
  206. }
  207. info->works = calloc(sizeof(struct work *), HF_NUM_SEQUENCE);
  208. if (!info->works)
  209. quit(1, "Failed to calloc info works in hashfast_detect_common");
  210. return true;
  211. }
  212. static void hashfast_usb_initialise(struct cgpu_info *hashfast)
  213. {
  214. if (hashfast->usbinfo.nodev)
  215. return;
  216. // FIXME Do necessary initialising here
  217. }
  218. static bool hashfast_detect_one_usb(libusb_device *dev, struct usb_find_devices *found)
  219. {
  220. struct cgpu_info *hashfast;
  221. hashfast = usb_alloc_cgpu(&hashfast_drv, HASHFAST_MINER_THREADS);
  222. if (!hashfast)
  223. quit(1, "Failed to usb_alloc_cgpu hashfast");
  224. if (!usb_init(hashfast, dev, found)) {
  225. free(hashfast->device_data);
  226. hashfast->device_data = NULL;
  227. hashfast = usb_free_cgpu(hashfast);
  228. return false;
  229. }
  230. hashfast->usbdev->usb_type = USB_TYPE_STD;
  231. hashfast_usb_initialise(hashfast);
  232. add_cgpu(hashfast);
  233. return hashfast_detect_common(hashfast);
  234. }
  235. static void hashfast_detect(bool hotplug)
  236. {
  237. /* Set up the CRC tables only once. */
  238. if (!hotplug)
  239. hf_init_crc8();
  240. usb_detect(&hashfast_drv, hashfast_detect_one_usb);
  241. }
  242. static bool hashfast_prepare(struct thr_info __maybe_unused *thr)
  243. {
  244. return true;
  245. }
  246. static int64_t hashfast_scanwork(struct thr_info __maybe_unused *thr)
  247. {
  248. return 0;
  249. }
  250. static struct api_data *hashfast_api_stats(struct cgpu_info __maybe_unused *cgpu)
  251. {
  252. return NULL;
  253. }
  254. static void hashfast_init(struct cgpu_info *hashfast)
  255. {
  256. usb_buffer_enable(hashfast);
  257. }
  258. static void hashfast_shutdown(struct thr_info __maybe_unused *thr)
  259. {
  260. }
  261. struct device_drv hashfast_drv = {
  262. .drv_id = DRIVER_hashfast,
  263. .dname = "Hashfast",
  264. .name = "HFA",
  265. .drv_detect = hashfast_detect,
  266. .thread_prepare = hashfast_prepare,
  267. .hash_work = &hash_driver_work,
  268. .scanwork = hashfast_scanwork,
  269. .get_api_stats = hashfast_api_stats,
  270. .reinit_device = hashfast_init,
  271. .thread_shutdown = hashfast_shutdown,
  272. };