driver-hashfast.c 8.0 KB

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