driver-hashfast.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. static int hashfast_reset(struct cgpu_info __maybe_unused *hashfast)
  101. {
  102. return 0;
  103. }
  104. static bool hashfast_detect_common(struct cgpu_info *hashfast, int baud)
  105. {
  106. hf_core_t **c, *core;
  107. hf_info_t *info;
  108. hf_job_t *j;
  109. int i, k, ret;
  110. hashfast_infos = realloc(hashfast_infos, sizeof(hf_info_t *) * (total_devices + 1));
  111. if (unlikely(!hashfast_infos))
  112. quit(1, "Failed to realloc hashfast_infos in hashfast_detect_common");
  113. // Assume success, allocate info ahead of reset, so reset can fill fields in
  114. info = calloc(sizeof(hf_info_t), 1);
  115. if (unlikely(!info))
  116. quit(1, "Failed to calloc info in hashfast_detect_common");
  117. hashfast_infos[hashfast->device_id] = info;
  118. info->tacho_enable = 1;
  119. info->miner_count = 1;
  120. info->max_search_difficulty = 12;
  121. info->baud_rate = baud;
  122. ret = hashfast_reset(hashfast);
  123. if (unlikely(ret)) {
  124. free(info);
  125. hashfast_infos[hashfast->device_id] = NULL;
  126. return false;
  127. }
  128. /* 1 Pending, 1 active for each */
  129. info->inflight_target = info->asic_count * info->core_count *2;
  130. switch (info->device_type) {
  131. default:
  132. case HFD_G1:
  133. /* Implies hash_loops = 0 for full nonce range */
  134. break;
  135. case HFD_ExpressAGX:
  136. /* ExpressAGX */
  137. info->hash_loops = 1 << 26;
  138. break;
  139. case HFD_VC709:
  140. /* Virtex 7
  141. * Adjust according to fast or slow configuration */
  142. if (info->core_count > 5)
  143. info->hash_loops = 1 << 26;
  144. else
  145. info->hash_loops = 1 << 30;
  146. break;
  147. }
  148. applog(LOG_INFO, "Hashfast Detect: chips %d cores %d inflight_target %d entries",
  149. info->asic_count, info->core_count, info->inflight_target);
  150. /* Initialize list heads */
  151. info->active.next = &info->active;
  152. info->active.prev = &info->active;
  153. info->inactive.next = &info->inactive;
  154. info->inactive.prev = &info->inactive;
  155. /* Allocate core data structures */
  156. info->cores = calloc(info->asic_count, sizeof(hf_core_t *));
  157. if (unlikely(!info->cores))
  158. quit(1, "Failed to calloc info cores in hashfast_detect_common");
  159. c = info->cores;
  160. for (i = 0; i < info->asic_count; i++) {
  161. *c = calloc(info->core_count, sizeof(hf_core_t));
  162. if (unlikely(!*c))
  163. quit(1, "Failed to calloc hf_core_t in hashfast_detect_common");
  164. for (k = 0, core = *c; k < info->core_count; k++, core++)
  165. core->enabled = 1;
  166. c++;
  167. }
  168. /* Now allocate enough structures to hold all the in-flight work
  169. * 2 per core - one active and one pending. These go on the inactive
  170. * queue, and get used/recycled as required. */
  171. for (i = 0; i < info->asic_count * info->core_count * 2; i++) {
  172. j = calloc(sizeof(hf_job_t), 1);
  173. if (unlikely(!j))
  174. quit(1, "Failed to calloc hf_job_t in hashfast_detect_common");
  175. list_add(&info->inactive, &j->l);
  176. }
  177. info->inactive_count = info->asic_count * info->core_count * 2;
  178. applog(LOG_INFO, "Hashfast Detect: Allocated %d job entries",
  179. info->inflight_target);
  180. // Finally, allocate enough space to hold the work array.
  181. info->max_work = info->inflight_target;
  182. info->num_sequence = 1024;
  183. info->work = calloc(info->max_work, sizeof(hf_work_t));
  184. if (unlikely(!info->work))
  185. quit(1, "Failed to calloc info work in hashfast_detect_common");
  186. applog(LOG_INFO, "Hashfast Detect: Allocated space for %d work entries", info->inflight_target);
  187. return true;
  188. }
  189. static void hashfast_usb_initialise(struct cgpu_info *hashfast)
  190. {
  191. if (hashfast->usbinfo.nodev)
  192. return;
  193. // FIXME Do necessary initialising here
  194. }
  195. static bool hashfast_detect_one_usb(libusb_device *dev, struct usb_find_devices *found)
  196. {
  197. struct cgpu_info *hashfast;
  198. int baud = DEFAULT_BAUD_RATE;
  199. hashfast = usb_alloc_cgpu(&hashfast_drv, HASHFAST_MINER_THREADS);
  200. if (!hashfast)
  201. return false;
  202. if (!usb_init(hashfast, dev, found)) {
  203. free(hashfast->device_data);
  204. hashfast->device_data = NULL;
  205. hashfast = usb_free_cgpu(hashfast);
  206. return false;
  207. }
  208. hashfast->usbdev->usb_type = USB_TYPE_STD;
  209. hashfast->usbdev->PrefPacketSize = HASHFAST_USB_PACKETSIZE;
  210. hashfast_usb_initialise(hashfast);
  211. add_cgpu(hashfast);
  212. return hashfast_detect_common(hashfast, baud);
  213. }
  214. static void hashfast_detect(void)
  215. {
  216. usb_detect(&hashfast_drv, hashfast_detect_one_usb);
  217. }
  218. static bool hashfast_prepare(struct thr_info __maybe_unused *thr)
  219. {
  220. return true;
  221. }
  222. static bool hashfast_fill(struct cgpu_info __maybe_unused *hashfast)
  223. {
  224. return true;
  225. }
  226. static int64_t hashfast_scanhash(struct thr_info __maybe_unused *thr)
  227. {
  228. return 0;
  229. }
  230. static struct api_data *hashfast_api_stats(struct cgpu_info __maybe_unused *cgpu)
  231. {
  232. return NULL;
  233. }
  234. static void hashfast_init(struct cgpu_info __maybe_unused *hashfast)
  235. {
  236. }
  237. static void hashfast_shutdown(struct thr_info __maybe_unused *thr)
  238. {
  239. }
  240. struct device_drv hashfast_drv = {
  241. .drv_id = DRIVER_HASHFAST,
  242. .dname = "Hashfast",
  243. .name = "HFA",
  244. .drv_detect = hashfast_detect,
  245. .thread_prepare = hashfast_prepare,
  246. .hash_work = hash_queued_work,
  247. .queue_full = hashfast_fill,
  248. .scanwork = hashfast_scanhash,
  249. .get_api_stats = hashfast_api_stats,
  250. .reinit_device = hashfast_init,
  251. .thread_shutdown = hashfast_shutdown,
  252. };