driver-bitfury.c 7.5 KB

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