driver-bitfury.c 8.9 KB

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