driver-bitfury.c 8.0 KB

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