driver-bitfury.c 8.5 KB

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