driver-bitfury.c 9.6 KB

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