driver-bitfury.c 8.4 KB

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