driver-bitfury.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright 2013 Con Kolivas
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include "miner.h"
  11. #include "driver-bitfury.h"
  12. #include "sha2.h"
  13. /* Wait longer 1/3 longer than it would take for a full nonce range */
  14. #define BF1WAIT 1600
  15. struct device_drv bitfury_drv;
  16. static void bitfury_empty_buffer(struct cgpu_info *bitfury)
  17. {
  18. char buf[512];
  19. int amount;
  20. do {
  21. usb_read_ii(bitfury, 1, buf, 512, &amount, C_BF1_FLUSH);
  22. } while (amount);
  23. }
  24. static void bitfury_open(struct cgpu_info *bitfury)
  25. {
  26. char buf[8];
  27. int amount;
  28. usb_read_ii(bitfury, 0, buf, 8, &amount, C_BF1_IFLUSH);
  29. /* Magic open sequence */
  30. usb_transfer(bitfury, 0x21, 0x22, 0x0003, 0, C_BF1_OPEN);
  31. bitfury_empty_buffer(bitfury);
  32. }
  33. static void bitfury_close(struct cgpu_info *bitfury)
  34. {
  35. bitfury_empty_buffer(bitfury);
  36. /* Magic close sequence */
  37. usb_transfer(bitfury, 0x21, 0x22, 0, 0, C_BF1_CLOSE);
  38. bitfury_empty_buffer(bitfury);
  39. usb_transfer(bitfury, 0x23, 0x08, 0x9053, 1, C_BF1_CLOSE);
  40. bitfury_empty_buffer(bitfury);
  41. }
  42. static void bitfury_identify(struct cgpu_info *bitfury)
  43. {
  44. int amount;
  45. usb_write_ii(bitfury, 1, "L", 1, &amount, C_BF1_IDENTIFY);
  46. }
  47. static bool bitfury_getinfo(struct cgpu_info *bitfury, struct bitfury_info *info)
  48. {
  49. char buf[512];
  50. int amount;
  51. usb_write_ii(bitfury, 1, "I", 1, &amount, C_BF1_REQINFO);
  52. usb_read_ii(bitfury, 1, buf, 14, &amount, C_BF1_GETINFO);
  53. if (amount != 14) {
  54. applog(LOG_INFO, "%s %d: Getinfo received %d bytes",
  55. bitfury->drv->name, bitfury->device_id, amount);
  56. return false;
  57. }
  58. info->version = buf[1];
  59. memcpy(&info->product, buf + 2, 8);
  60. memcpy(&info->serial, buf + 10, 4);
  61. applog(LOG_INFO, "%s %d: Getinfo returned version %d, product %s serial %08x", bitfury->drv->name,
  62. bitfury->device_id, info->version, info->product, info->serial);
  63. bitfury_empty_buffer(bitfury);
  64. return true;
  65. }
  66. static bool bitfury_reset(struct cgpu_info *bitfury)
  67. {
  68. char buf[512];
  69. int amount;
  70. usb_write_ii(bitfury, 1, "R", 1, &amount, C_BF1_REQRESET);
  71. usb_read_ii_timeout(bitfury, 1, buf, 7, &amount, BF1WAIT, C_BF1_GETRESET);
  72. if (amount != 7) {
  73. applog(LOG_INFO, "%s %d: Getreset received %d bytes",
  74. bitfury->drv->name, bitfury->device_id, amount);
  75. return false;
  76. }
  77. applog(LOG_INFO, "%s %d: Getreset returned %s", bitfury->drv->name,
  78. bitfury->device_id, buf);
  79. bitfury_empty_buffer(bitfury);
  80. return true;
  81. }
  82. static bool bitfury_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  83. {
  84. struct cgpu_info *bitfury;
  85. struct bitfury_info *info;
  86. bitfury = usb_alloc_cgpu(&bitfury_drv, 1);
  87. if (!usb_init(bitfury, dev, found)) {
  88. bitfury = usb_free_cgpu(bitfury);
  89. goto out;
  90. }
  91. applog(LOG_INFO, "%s %d: Found at %s", bitfury->drv->name,
  92. bitfury->device_id, bitfury->device_path);
  93. info = calloc(sizeof(struct bitfury_info), 1);
  94. if (!info)
  95. quit(1, "Failed to calloc info in bitfury_detect_one");
  96. bitfury->device_data = info;
  97. usb_buffer_enable(bitfury);
  98. bitfury_open(bitfury);
  99. /* Send getinfo request */
  100. if (!bitfury_getinfo(bitfury, info))
  101. goto out_close;
  102. /* Send reset request */
  103. if (!bitfury_reset(bitfury))
  104. goto out_close;
  105. bitfury_identify(bitfury);
  106. bitfury_empty_buffer(bitfury);
  107. if (!add_cgpu(bitfury))
  108. goto out_close;
  109. update_usb_stats(bitfury);
  110. applog(LOG_INFO, "%s %d: Found at %s",
  111. bitfury->drv->name, bitfury->device_id, bitfury->device_path);
  112. return true;
  113. out_close:
  114. bitfury_close(bitfury);
  115. out:
  116. return false;
  117. }
  118. static void bitfury_detect(void)
  119. {
  120. usb_detect(&bitfury_drv, bitfury_detect_one);
  121. }
  122. static uint32_t decnonce(uint32_t in)
  123. {
  124. uint32_t out;
  125. /* First part load */
  126. out = (in & 0xFF) << 24; in >>= 8;
  127. /* Byte reversal */
  128. in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
  129. in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
  130. in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
  131. out |= (in >> 2)&0x3FFFFF;
  132. /* Extraction */
  133. if (in & 1) out |= (1 << 23);
  134. if (in & 2) out |= (1 << 22);
  135. out -= 0x800004;
  136. return out;
  137. }
  138. #define BT_OFFSETS 3
  139. const uint32_t bf_offsets[] = {0, -0x400000, -0x800000};
  140. static bool bitfury_checkresults(struct thr_info *thr, struct work *work, uint32_t nonce)
  141. {
  142. int i;
  143. for (i = 0; i < BT_OFFSETS; i++) {
  144. if (test_nonce(work, nonce + bf_offsets[i])) {
  145. submit_tested_work(thr, work);
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. static int64_t bitfury_scanhash(struct thr_info *thr, struct work *work,
  152. int64_t __maybe_unused max_nonce)
  153. {
  154. struct cgpu_info *bitfury = thr->cgpu;
  155. struct bitfury_info *info = bitfury->device_data;
  156. struct timeval tv_now;
  157. int amount, i;
  158. char buf[45];
  159. int ms_diff;
  160. buf[0] = 'W';
  161. memcpy(buf + 1, work->midstate, 32);
  162. memcpy(buf + 33, work->data + 64, 12);
  163. /* New results may spill out from the latest work, making us drop out
  164. * too early so read whatever we get for the first half nonce and then
  165. * look for the results to prev work. */
  166. cgtime(&tv_now);
  167. ms_diff = 600 - ms_tdiff(&tv_now, &info->tv_start);
  168. if (ms_diff > 0) {
  169. usb_read_ii_timeout(bitfury, 1, info->buf, 512, &amount, ms_diff, C_BF1_GETRES);
  170. info->tot += amount;
  171. }
  172. if (unlikely(thr->work_restart))
  173. goto cascade;
  174. /* Now look for the bulk of the previous work results, they will come
  175. * in a batch following the first data. */
  176. cgtime(&tv_now);
  177. ms_diff = BF1WAIT - ms_tdiff(&tv_now, &info->tv_start);
  178. if (unlikely(ms_diff < 10))
  179. ms_diff = 10;
  180. usb_read_ii_once_timeout(bitfury, 1, info->buf + info->tot, 7, &amount, ms_diff, C_BF1_GETRES);
  181. info->tot += amount;
  182. while (amount) {
  183. usb_read_ii_once_timeout(bitfury, 1, info->buf + info->tot, 512, &amount, 10, C_BF1_GETRES);
  184. info->tot += amount;
  185. };
  186. if (unlikely(thr->work_restart))
  187. goto cascade;
  188. /* Send work */
  189. usb_write_ii(bitfury, 1, buf, 45, &amount, C_BF1_REQWORK);
  190. cgtime(&info->tv_start);
  191. /* Get response acknowledging work */
  192. usb_read_ii(bitfury, 1, buf, 7, &amount, C_BF1_GETWORK);
  193. /* Only happens on startup */
  194. if (unlikely(!info->prevwork[BF1ARRAY_SIZE]))
  195. goto cascade;
  196. /* Search for what work the nonce matches in order of likelihood. Last
  197. * entry is end of result marker. */
  198. for (i = 0; i < info->tot - 7; i += 7) {
  199. uint32_t nonce;
  200. int j;
  201. /* Ignore state & switched data in results for now. */
  202. memcpy(&nonce, info->buf + i + 3, 4);
  203. nonce = decnonce(nonce);
  204. for (j = 0; j < BF1ARRAY_SIZE; j++) {
  205. if (bitfury_checkresults(thr, info->prevwork[j], nonce)) {
  206. info->nonces++;
  207. break;
  208. }
  209. }
  210. }
  211. info->tot = 0;
  212. free_work(info->prevwork[BF1ARRAY_SIZE]);
  213. cascade:
  214. for (i = BF1ARRAY_SIZE; i > 0; i--)
  215. info->prevwork[i] = info->prevwork[i - 1];
  216. info->prevwork[0] = copy_work(work);
  217. work->blk.nonce = 0xffffffff;
  218. if (info->nonces) {
  219. info->nonces--;
  220. return (int64_t)0xffffffff;
  221. }
  222. return 0;
  223. }
  224. static struct api_data *bitfury_api_stats(struct cgpu_info *cgpu)
  225. {
  226. struct bitfury_info *info = cgpu->device_data;
  227. struct api_data *root = NULL;
  228. char serial[16];
  229. int version;
  230. version = info->version;
  231. root = api_add_int(root, "Version", &version, true);
  232. root = api_add_string(root, "Product", info->product, false);
  233. sprintf(serial, "%08x", info->serial);
  234. root = api_add_string(root, "Serial", serial, true);
  235. return root;
  236. }
  237. static void bitfury_init(struct cgpu_info *bitfury)
  238. {
  239. bitfury_close(bitfury);
  240. bitfury_open(bitfury);
  241. bitfury_reset(bitfury);
  242. }
  243. static void bitfury_shutdown(struct thr_info *thr)
  244. {
  245. struct cgpu_info *bitfury = thr->cgpu;
  246. bitfury_close(bitfury);
  247. }
  248. /* Currently hardcoded to BF1 devices */
  249. struct device_drv bitfury_drv = {
  250. .drv_id = DRIVER_BITFURY,
  251. .dname = "bitfury",
  252. .name = "BF1",
  253. .drv_detect = bitfury_detect,
  254. .scanhash = bitfury_scanhash,
  255. .get_api_stats = bitfury_api_stats,
  256. .reinit_device = bitfury_init,
  257. .thread_shutdown = bitfury_shutdown,
  258. .identify_device = bitfury_identify
  259. };