driver-bitfury.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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_ATMEL_RESET);
  35. if (!err)
  36. err = usb_transfer(bitfury, 0x21, 0x22, 0, 0, C_ATMEL_OPEN);
  37. if (!err) {
  38. err = usb_transfer_data(bitfury, 0x21, 0x20, 0x0000, 0, buf,
  39. BF1MSGSIZE, C_ATMEL_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. /* This does not artificially raise hashrate, it simply allows the
  126. * hashrate to adapt quickly on starting. */
  127. info->total_nonces = 1;
  128. if (!bitfury_open(bitfury))
  129. goto out_close;
  130. /* Send getinfo request */
  131. if (!bitfury_getinfo(bitfury, info))
  132. goto out_close;
  133. /* Send reset request */
  134. if (!bitfury_reset(bitfury))
  135. goto out_close;
  136. bitfury_identify(bitfury);
  137. bitfury_empty_buffer(bitfury);
  138. if (!add_cgpu(bitfury))
  139. quit(1, "Failed to add_cgpu in bitfury_detect_one");
  140. update_usb_stats(bitfury);
  141. applog(LOG_INFO, "%s %d: Successfully initialised %s",
  142. bitfury->drv->name, bitfury->device_id, bitfury->device_path);
  143. return true;
  144. out_close:
  145. bitfury_close(bitfury);
  146. usb_uninit(bitfury);
  147. out:
  148. bitfury = usb_free_cgpu(bitfury);
  149. return false;
  150. }
  151. static void bitfury_detect(bool __maybe_unused hotplug)
  152. {
  153. usb_detect(&bitfury_drv, bitfury_detect_one);
  154. }
  155. static uint32_t decnonce(uint32_t in)
  156. {
  157. uint32_t out;
  158. /* First part load */
  159. out = (in & 0xFF) << 24; in >>= 8;
  160. /* Byte reversal */
  161. in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
  162. in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
  163. in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
  164. out |= (in >> 2)&0x3FFFFF;
  165. /* Extraction */
  166. if (in & 1) out |= (1 << 23);
  167. if (in & 2) out |= (1 << 22);
  168. out -= 0x800004;
  169. return out;
  170. }
  171. #define BT_OFFSETS 3
  172. const uint32_t bf_offsets[] = {-0x800000, 0, -0x400000};
  173. static bool bitfury_checkresults(struct thr_info *thr, struct work *work, uint32_t nonce)
  174. {
  175. int i;
  176. for (i = 0; i < BT_OFFSETS; i++) {
  177. if (test_nonce(work, nonce + bf_offsets[i])) {
  178. submit_tested_work(thr, work);
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. static int64_t bitfury_scanwork(struct thr_info *thr)
  185. {
  186. struct cgpu_info *bitfury = thr->cgpu;
  187. struct bitfury_info *info = bitfury->device_data;
  188. struct timeval tv_now;
  189. struct work *work;
  190. double nonce_rate;
  191. int64_t ret = 0;
  192. int amount, i;
  193. char buf[45];
  194. int ms_diff;
  195. work = get_work(thr, thr->id);
  196. if (unlikely(thr->work_restart)) {
  197. free_work(work);
  198. return 0;
  199. }
  200. buf[0] = 'W';
  201. memcpy(buf + 1, work->midstate, 32);
  202. memcpy(buf + 33, work->data + 64, 12);
  203. /* New results may spill out from the latest work, making us drop out
  204. * too early so read whatever we get for the first half nonce and then
  205. * look for the results to prev work. */
  206. cgtime(&tv_now);
  207. ms_diff = 600 - ms_tdiff(&tv_now, &info->tv_start);
  208. if (ms_diff > 0) {
  209. usb_read_timeout_cancellable(bitfury, info->buf, 512, &amount, ms_diff, C_BF1_GETRES);
  210. info->tot += amount;
  211. }
  212. if (unlikely(thr->work_restart))
  213. goto cascade;
  214. /* Now look for the bulk of the previous work results, they will come
  215. * in a batch following the first data. */
  216. cgtime(&tv_now);
  217. ms_diff = BF1WAIT - ms_tdiff(&tv_now, &info->tv_start);
  218. if (unlikely(ms_diff < 10))
  219. ms_diff = 10;
  220. usb_read_once_timeout_cancellable(bitfury, info->buf + info->tot, BF1MSGSIZE,
  221. &amount, ms_diff, C_BF1_GETRES);
  222. info->tot += amount;
  223. while (amount) {
  224. usb_read_once_timeout(bitfury, info->buf + info->tot, 512, &amount, 10, C_BF1_GETRES);
  225. info->tot += amount;
  226. };
  227. if (unlikely(thr->work_restart))
  228. goto cascade;
  229. /* Send work */
  230. usb_write(bitfury, buf, 45, &amount, C_BF1_REQWORK);
  231. cgtime(&info->tv_start);
  232. /* Get response acknowledging work */
  233. usb_read(bitfury, buf, BF1MSGSIZE, &amount, C_BF1_GETWORK);
  234. /* Only happens on startup */
  235. if (unlikely(!info->prevwork[BF1ARRAY_SIZE]))
  236. goto cascade;
  237. /* Search for what work the nonce matches in order of likelihood. Last
  238. * entry is end of result marker. */
  239. for (i = 0; i < info->tot - BF1MSGSIZE; i += BF1MSGSIZE) {
  240. uint32_t nonce;
  241. int j;
  242. /* Ignore state & switched data in results for now. */
  243. memcpy(&nonce, info->buf + i + 3, 4);
  244. nonce = decnonce(nonce);
  245. for (j = 0; j < BF1ARRAY_SIZE; j++) {
  246. if (bitfury_checkresults(thr, info->prevwork[j], nonce)) {
  247. info->nonces++;
  248. break;
  249. }
  250. }
  251. }
  252. info->tot = 0;
  253. free_work(info->prevwork[BF1ARRAY_SIZE]);
  254. cascade:
  255. for (i = BF1ARRAY_SIZE; i > 0; i--)
  256. info->prevwork[i] = info->prevwork[i - 1];
  257. info->prevwork[0] = work;
  258. info->cycles++;
  259. info->total_nonces += info->nonces;
  260. info->saved_nonces += info->nonces;
  261. info->nonces = 0;
  262. nonce_rate = (double)info->total_nonces / (double)info->cycles;
  263. if (info->saved_nonces >= nonce_rate) {
  264. info->saved_nonces -= nonce_rate;
  265. ret = (double)0xffffffff * nonce_rate;
  266. }
  267. if (unlikely(bitfury->usbinfo.nodev)) {
  268. applog(LOG_WARNING, "%s %d: Device disappeared, disabling thread",
  269. bitfury->drv->name, bitfury->device_id);
  270. ret = -1;
  271. }
  272. return ret;
  273. }
  274. static struct api_data *bitfury_api_stats(struct cgpu_info *cgpu)
  275. {
  276. struct bitfury_info *info = cgpu->device_data;
  277. struct api_data *root = NULL;
  278. double nonce_rate;
  279. char serial[16];
  280. int version;
  281. version = info->version;
  282. root = api_add_int(root, "Version", &version, true);
  283. root = api_add_string(root, "Product", info->product, false);
  284. sprintf(serial, "%08x", info->serial);
  285. root = api_add_string(root, "Serial", serial, true);
  286. nonce_rate = (double)info->total_nonces / (double)info->cycles;
  287. root = api_add_double(root, "NonceRate", &nonce_rate, true);
  288. return root;
  289. }
  290. static void bitfury_init(struct cgpu_info *bitfury)
  291. {
  292. bitfury_close(bitfury);
  293. bitfury_open(bitfury);
  294. bitfury_reset(bitfury);
  295. }
  296. static void bitfury_shutdown(struct thr_info *thr)
  297. {
  298. struct cgpu_info *bitfury = thr->cgpu;
  299. bitfury_close(bitfury);
  300. }
  301. /* Currently hardcoded to BF1 devices */
  302. struct device_drv bitfury_drv = {
  303. .drv_id = DRIVER_bitfury,
  304. .dname = "bitfury",
  305. .name = "BF1",
  306. .drv_detect = bitfury_detect,
  307. .hash_work = &hash_driver_work,
  308. .scanwork = bitfury_scanwork,
  309. .get_api_stats = bitfury_api_stats,
  310. .reinit_device = bitfury_init,
  311. .thread_shutdown = bitfury_shutdown,
  312. .identify_device = bitfury_identify
  313. };