driver-bitfury.c 9.0 KB

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