driver-bitfury.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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_ii(bitfury, 1, buf, 512, &amount, C_BF1_FLUSH);
  22. } while (amount);
  23. }
  24. static void bitfury_empty_intbuf(struct cgpu_info *bitfury)
  25. {
  26. char buf[8];
  27. int amount;
  28. usb_read_ii(bitfury, 0, buf, 8, &amount, C_BF1_IFLUSH);
  29. }
  30. static bool bitfury_open(struct cgpu_info *bitfury)
  31. {
  32. int err;
  33. bitfury_empty_intbuf(bitfury);
  34. /* Magic open sequence */
  35. err = usb_transfer(bitfury, 0x21, 0x22, 0x0003, 0, C_BF1_OPEN);
  36. if (!err)
  37. bitfury_empty_buffer(bitfury);
  38. return !err;
  39. }
  40. static void bitfury_close(struct cgpu_info *bitfury)
  41. {
  42. bitfury_empty_buffer(bitfury);
  43. /* Magic close sequence */
  44. usb_transfer(bitfury, 0x21, 0x22, 0, 0, C_BF1_CLOSE);
  45. bitfury_empty_intbuf(bitfury);
  46. bitfury_empty_buffer(bitfury);
  47. usb_transfer(bitfury, 0x23, 0x08, 0x9053, 1, C_BF1_CLOSE);
  48. bitfury_empty_buffer(bitfury);
  49. }
  50. static void bitfury_identify(struct cgpu_info *bitfury)
  51. {
  52. int amount;
  53. usb_write_ii(bitfury, 1, "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_ii(bitfury, 1, "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_ii(bitfury, 1, 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_ii(bitfury, 1, "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_ii_timeout(bitfury, 1, buf, 7, &amount, BF1WAIT, C_BF1_GETRESET);
  95. if (err) {
  96. applog(LOG_INFO, "%s %d: Failed to read GETRESET",
  97. bitfury->drv->name, bitfury->device_id);
  98. return false;
  99. }
  100. if (amount != 7) {
  101. applog(LOG_INFO, "%s %d: Getreset received %d bytes instead of 7",
  102. bitfury->drv->name, bitfury->device_id, amount);
  103. return false;
  104. }
  105. applog(LOG_DEBUG, "%s %d: Getreset returned %s", bitfury->drv->name,
  106. bitfury->device_id, buf);
  107. bitfury_empty_buffer(bitfury);
  108. return true;
  109. }
  110. static bool bitfury_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  111. {
  112. struct cgpu_info *bitfury;
  113. struct bitfury_info *info;
  114. bitfury = usb_alloc_cgpu(&bitfury_drv, 1);
  115. if (!usb_init(bitfury, dev, found))
  116. goto out;
  117. applog(LOG_INFO, "%s %d: Found at %s", bitfury->drv->name,
  118. bitfury->device_id, bitfury->device_path);
  119. info = calloc(sizeof(struct bitfury_info), 1);
  120. if (!info)
  121. quit(1, "Failed to calloc info in bitfury_detect_one");
  122. bitfury->device_data = info;
  123. usb_buffer_enable(bitfury);
  124. if (!bitfury_open(bitfury)) {
  125. applog(LOG_INFO, "%s %d: Failed to open", bitfury->drv->name,
  126. bitfury->device_id);
  127. goto out_close;
  128. }
  129. /* Send getinfo request */
  130. if (!bitfury_getinfo(bitfury, info))
  131. goto out_close;
  132. /* Send reset request */
  133. if (!bitfury_reset(bitfury))
  134. goto out_close;
  135. bitfury_identify(bitfury);
  136. bitfury_empty_buffer(bitfury);
  137. if (!add_cgpu(bitfury))
  138. goto out_close;
  139. update_usb_stats(bitfury);
  140. applog(LOG_INFO, "%s %d: Found at %s",
  141. bitfury->drv->name, bitfury->device_id, bitfury->device_path);
  142. return true;
  143. out_close:
  144. bitfury_close(bitfury);
  145. usb_uninit(bitfury);
  146. out:
  147. bitfury = usb_free_cgpu(bitfury);
  148. return false;
  149. }
  150. static void bitfury_detect(void)
  151. {
  152. usb_detect(&bitfury_drv, bitfury_detect_one);
  153. }
  154. static uint32_t decnonce(uint32_t in)
  155. {
  156. uint32_t out;
  157. /* First part load */
  158. out = (in & 0xFF) << 24; in >>= 8;
  159. /* Byte reversal */
  160. in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
  161. in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
  162. in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
  163. out |= (in >> 2)&0x3FFFFF;
  164. /* Extraction */
  165. if (in & 1) out |= (1 << 23);
  166. if (in & 2) out |= (1 << 22);
  167. out -= 0x800004;
  168. return out;
  169. }
  170. #define BT_OFFSETS 3
  171. const uint32_t bf_offsets[] = {0, -0x400000, -0x800000};
  172. static bool bitfury_checkresults(struct thr_info *thr, struct work *work, uint32_t nonce)
  173. {
  174. int i;
  175. for (i = 0; i < BT_OFFSETS; i++) {
  176. if (test_nonce(work, nonce + bf_offsets[i])) {
  177. submit_tested_work(thr, work);
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. static int64_t bitfury_scanhash(struct thr_info *thr, struct work *work,
  184. int64_t __maybe_unused max_nonce)
  185. {
  186. struct cgpu_info *bitfury = thr->cgpu;
  187. struct bitfury_info *info = bitfury->device_data;
  188. struct timeval tv_now;
  189. int amount, i;
  190. char buf[45];
  191. int ms_diff;
  192. buf[0] = 'W';
  193. memcpy(buf + 1, work->midstate, 32);
  194. memcpy(buf + 33, work->data + 64, 12);
  195. /* New results may spill out from the latest work, making us drop out
  196. * too early so read whatever we get for the first half nonce and then
  197. * look for the results to prev work. */
  198. cgtime(&tv_now);
  199. ms_diff = 600 - ms_tdiff(&tv_now, &info->tv_start);
  200. if (ms_diff > 0) {
  201. usb_read_ii_timeout(bitfury, 1, info->buf, 512, &amount, ms_diff, C_BF1_GETRES);
  202. info->tot += amount;
  203. }
  204. if (unlikely(thr->work_restart))
  205. goto cascade;
  206. /* Now look for the bulk of the previous work results, they will come
  207. * in a batch following the first data. */
  208. cgtime(&tv_now);
  209. ms_diff = BF1WAIT - ms_tdiff(&tv_now, &info->tv_start);
  210. if (unlikely(ms_diff < 10))
  211. ms_diff = 10;
  212. usb_read_ii_once_timeout(bitfury, 1, info->buf + info->tot, 7, &amount, ms_diff, C_BF1_GETRES);
  213. info->tot += amount;
  214. while (amount) {
  215. usb_read_ii_once_timeout(bitfury, 1, 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_ii(bitfury, 1, buf, 45, &amount, C_BF1_REQWORK);
  222. cgtime(&info->tv_start);
  223. /* Get response acknowledging work */
  224. usb_read_ii(bitfury, 1, buf, 7, &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 - 7; i += 7) {
  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. return 0;
  255. }
  256. static struct api_data *bitfury_api_stats(struct cgpu_info *cgpu)
  257. {
  258. struct bitfury_info *info = cgpu->device_data;
  259. struct api_data *root = NULL;
  260. char serial[16];
  261. int version;
  262. version = info->version;
  263. root = api_add_int(root, "Version", &version, true);
  264. root = api_add_string(root, "Product", info->product, false);
  265. sprintf(serial, "%08x", info->serial);
  266. root = api_add_string(root, "Serial", serial, true);
  267. return root;
  268. }
  269. static void bitfury_init(struct cgpu_info *bitfury)
  270. {
  271. bitfury_close(bitfury);
  272. bitfury_open(bitfury);
  273. bitfury_reset(bitfury);
  274. }
  275. static void bitfury_shutdown(struct thr_info *thr)
  276. {
  277. struct cgpu_info *bitfury = thr->cgpu;
  278. bitfury_close(bitfury);
  279. }
  280. /* Currently hardcoded to BF1 devices */
  281. struct device_drv bitfury_drv = {
  282. .drv_id = DRIVER_BITFURY,
  283. .dname = "bitfury",
  284. .name = "BF1",
  285. .drv_detect = bitfury_detect,
  286. .scanhash = bitfury_scanhash,
  287. .get_api_stats = bitfury_api_stats,
  288. .reinit_device = bitfury_init,
  289. .thread_shutdown = bitfury_shutdown,
  290. .identify_device = bitfury_identify
  291. };