driver-hashfast.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 uint32_t crc32_table[256]; /* CRC-32 table */
  21. void hf_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 __maybe_unused hf_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. #define DI32 0x04c11db7L
  42. void hf_init_crc32(void)
  43. {
  44. uint32_t i, j;
  45. uint32_t crc;
  46. for (i = 0; i < 256; i++){
  47. crc = i << 24;
  48. for (j = 0; j < 8; j++) {
  49. if (crc & 0x80000000L)
  50. crc = (crc << 1) ^ DI32;
  51. else
  52. crc = (crc << 1);
  53. }
  54. crc32_table[i] = crc;
  55. }
  56. }
  57. static uint32_t __maybe_unused hf_crc32(unsigned char *p, int len, int plug_in)
  58. {
  59. uint32_t crc = 0xffffffffU, crc_sav;
  60. uint32_t i;
  61. while (len--) {
  62. i = ((crc >> 24) ^ *p++) & 0xff;
  63. crc = (crc << 8) ^ crc32_table[i];
  64. }
  65. crc_sav = crc;
  66. applog(LOG_DEBUG, "hf_crc32: crc is 0x%08x", crc);
  67. if (plug_in) {
  68. for (i = 0; i < 4; i++, crc >>= 8)
  69. *p++ = crc & 0xff;
  70. }
  71. return crc_sav;
  72. }
  73. static hf_info_t **hashfast_infos;
  74. struct device_drv hashfast_drv;
  75. struct hf_cmd {
  76. int cmd;
  77. char *cmd_name;
  78. enum usb_cmds usb_cmd;
  79. };
  80. static const struct hf_cmd hf_cmds[] = {
  81. {OP_NULL, "OP_NULL", C_NULL},
  82. {OP_ROOT, "OP_ROOT", C_NULL},
  83. {OP_RESET, "OP_RESET", C_HF_RESET},
  84. {OP_PLL_CONFIG, "OP_PLL_CONFIG", C_HF_PLL_CONFIG},
  85. {OP_ADDRESS, "OP_ADDRESS", C_HF_ADDRESS},
  86. {OP_READDRESS, "OP_READDRESS", C_NULL},
  87. {OP_HIGHEST, "OP_HIGHEST", C_NULL},
  88. {OP_BAUD, "OP_BAUD", C_HF_BAUD},
  89. {OP_UNROOT, "OP_UNROOT", C_NULL},
  90. {OP_HASH, "OP_HASH", C_HF_HASH},
  91. {OP_NONCE, "OP_NONCE", C_HF_NONCE},
  92. {OP_ABORT, "OP_ABORT", C_HF_ABORT},
  93. {OP_STATUS, "OP_STATUS", C_HF_STATUS},
  94. {OP_GPIO, "OP_GPIO", C_NULL},
  95. {OP_CONFIG, "OP_CONFIG", C_HF_CONFIG},
  96. {OP_STATISTICS, "OP_STATISTICS", C_HF_STATISTICS},
  97. {OP_GROUP, "OP_GROUP", C_NULL},
  98. {OP_CLOCKGATE, "OP_CLOCKGATE", C_HF_CLOCKGATE}
  99. };
  100. /* Send an arbitrary frame, consisting of an 8 byte header and an optional
  101. * packet body. */
  102. static int __maybe_unused hashfast_send_frame(struct cgpu_info *hashfast, uint8_t opcode,
  103. uint8_t chip, uint8_t core, uint16_t hdata,
  104. uint8_t *data, int len)
  105. {
  106. int tx_length, ret, amount, id = hashfast->device_id;
  107. uint8_t packet[256];
  108. struct hf_header *p = (struct hf_header *)packet;
  109. p->preamble = HF_PREAMBLE;
  110. p->operation_code = opcode;
  111. p->chip_address = chip;
  112. p->core_address = core;
  113. p->hdata = htole16(hdata);
  114. p->data_length = len / 4;
  115. p->crc8 = hf_crc8(packet);
  116. tx_length = sizeof(struct hf_header);
  117. if (len) {
  118. memcpy(&packet[sizeof(struct hf_header)], data, len);
  119. hf_crc32(&packet[sizeof(struct hf_header)], len, 1);
  120. tx_length += len + 4;
  121. }
  122. ret = usb_write(hashfast, (char *)packet, tx_length, &amount,
  123. hf_cmds[opcode].usb_cmd);
  124. if (ret < 0 || amount != tx_length) {
  125. applog(LOG_ERR, "HF%d: hashfast_send_frame: USB Send error, ret %d amount %d vs. tx_length %d",
  126. id, ret, amount, tx_length);
  127. return 1;
  128. }
  129. return 0;
  130. }
  131. static int hashfast_reset(struct cgpu_info __maybe_unused *hashfast)
  132. {
  133. return 0;
  134. }
  135. static bool hashfast_detect_common(struct cgpu_info *hashfast, int baud)
  136. {
  137. hf_core_t **c, *core;
  138. hf_info_t *info;
  139. hf_job_t *j;
  140. int i, k, ret;
  141. hashfast_infos = realloc(hashfast_infos, sizeof(hf_info_t *) * (total_devices + 1));
  142. if (unlikely(!hashfast_infos))
  143. quit(1, "Failed to realloc hashfast_infos in hashfast_detect_common");
  144. // Assume success, allocate info ahead of reset, so reset can fill fields in
  145. info = calloc(sizeof(hf_info_t), 1);
  146. if (unlikely(!info))
  147. quit(1, "Failed to calloc info in hashfast_detect_common");
  148. hashfast_infos[hashfast->device_id] = info;
  149. info->tacho_enable = 1;
  150. info->miner_count = 1;
  151. info->max_search_difficulty = 12;
  152. info->baud_rate = baud;
  153. ret = hashfast_reset(hashfast);
  154. if (unlikely(ret)) {
  155. free(info);
  156. hashfast_infos[hashfast->device_id] = NULL;
  157. return false;
  158. }
  159. /* 1 Pending, 1 active for each */
  160. info->inflight_target = info->asic_count * info->core_count *2;
  161. switch (info->device_type) {
  162. default:
  163. case HFD_G1:
  164. /* Implies hash_loops = 0 for full nonce range */
  165. break;
  166. case HFD_ExpressAGX:
  167. /* ExpressAGX */
  168. info->hash_loops = 1 << 26;
  169. break;
  170. case HFD_VC709:
  171. /* Virtex 7
  172. * Adjust according to fast or slow configuration */
  173. if (info->core_count > 5)
  174. info->hash_loops = 1 << 26;
  175. else
  176. info->hash_loops = 1 << 30;
  177. break;
  178. }
  179. applog(LOG_INFO, "Hashfast Detect: chips %d cores %d inflight_target %d entries",
  180. info->asic_count, info->core_count, info->inflight_target);
  181. /* Initialize list heads */
  182. info->active.next = &info->active;
  183. info->active.prev = &info->active;
  184. info->inactive.next = &info->inactive;
  185. info->inactive.prev = &info->inactive;
  186. /* Allocate core data structures */
  187. info->cores = calloc(info->asic_count, sizeof(hf_core_t *));
  188. if (unlikely(!info->cores))
  189. quit(1, "Failed to calloc info cores in hashfast_detect_common");
  190. c = info->cores;
  191. for (i = 0; i < info->asic_count; i++) {
  192. *c = calloc(info->core_count, sizeof(hf_core_t));
  193. if (unlikely(!*c))
  194. quit(1, "Failed to calloc hf_core_t in hashfast_detect_common");
  195. for (k = 0, core = *c; k < info->core_count; k++, core++)
  196. core->enabled = 1;
  197. c++;
  198. }
  199. /* Now allocate enough structures to hold all the in-flight work
  200. * 2 per core - one active and one pending. These go on the inactive
  201. * queue, and get used/recycled as required. */
  202. for (i = 0; i < info->asic_count * info->core_count * 2; i++) {
  203. j = calloc(sizeof(hf_job_t), 1);
  204. if (unlikely(!j))
  205. quit(1, "Failed to calloc hf_job_t in hashfast_detect_common");
  206. list_add(&info->inactive, &j->l);
  207. }
  208. info->inactive_count = info->asic_count * info->core_count * 2;
  209. applog(LOG_INFO, "Hashfast Detect: Allocated %d job entries",
  210. info->inflight_target);
  211. // Finally, allocate enough space to hold the work array.
  212. info->max_work = info->inflight_target;
  213. info->num_sequence = 1024;
  214. info->work = calloc(info->max_work, sizeof(hf_work_t));
  215. if (unlikely(!info->work))
  216. quit(1, "Failed to calloc info work in hashfast_detect_common");
  217. applog(LOG_INFO, "Hashfast Detect: Allocated space for %d work entries", info->inflight_target);
  218. return true;
  219. }
  220. static void hashfast_usb_initialise(struct cgpu_info *hashfast)
  221. {
  222. if (hashfast->usbinfo.nodev)
  223. return;
  224. // FIXME Do necessary initialising here
  225. }
  226. static bool hashfast_detect_one_usb(libusb_device *dev, struct usb_find_devices *found)
  227. {
  228. struct cgpu_info *hashfast;
  229. int baud = DEFAULT_BAUD_RATE;
  230. hashfast = usb_alloc_cgpu(&hashfast_drv, HASHFAST_MINER_THREADS);
  231. if (!hashfast)
  232. return false;
  233. if (!usb_init(hashfast, dev, found)) {
  234. free(hashfast->device_data);
  235. hashfast->device_data = NULL;
  236. hashfast = usb_free_cgpu(hashfast);
  237. return false;
  238. }
  239. hashfast->usbdev->usb_type = USB_TYPE_STD;
  240. hashfast->usbdev->PrefPacketSize = HASHFAST_USB_PACKETSIZE;
  241. hashfast_usb_initialise(hashfast);
  242. add_cgpu(hashfast);
  243. return hashfast_detect_common(hashfast, baud);
  244. }
  245. static void hashfast_detect(void)
  246. {
  247. usb_detect(&hashfast_drv, hashfast_detect_one_usb);
  248. }
  249. static bool hashfast_prepare(struct thr_info __maybe_unused *thr)
  250. {
  251. return true;
  252. }
  253. static bool hashfast_fill(struct cgpu_info __maybe_unused *hashfast)
  254. {
  255. return true;
  256. }
  257. static int64_t hashfast_scanhash(struct thr_info __maybe_unused *thr)
  258. {
  259. return 0;
  260. }
  261. static struct api_data *hashfast_api_stats(struct cgpu_info __maybe_unused *cgpu)
  262. {
  263. return NULL;
  264. }
  265. static void hashfast_init(struct cgpu_info *hashfast)
  266. {
  267. usb_buffer_enable(hashfast);
  268. }
  269. static void hashfast_shutdown(struct thr_info __maybe_unused *thr)
  270. {
  271. }
  272. struct device_drv hashfast_drv = {
  273. .drv_id = DRIVER_HASHFAST,
  274. .dname = "Hashfast",
  275. .name = "HFA",
  276. .drv_detect = hashfast_detect,
  277. .thread_prepare = hashfast_prepare,
  278. .hash_work = hash_queued_work,
  279. .queue_full = hashfast_fill,
  280. .scanwork = hashfast_scanhash,
  281. .get_api_stats = hashfast_api_stats,
  282. .reinit_device = hashfast_init,
  283. .thread_shutdown = hashfast_shutdown,
  284. };