driver-bitfury.c 7.4 KB

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