driver-bf1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright 2013 DI Andreas Auer
  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. /*
  10. * Bitfury BF1 USB miner with Bitfury ASIC
  11. */
  12. #include "config.h"
  13. #include "miner.h"
  14. #include "fpgautils.h"
  15. #include "logging.h"
  16. #include "deviceapi.h"
  17. #include "sha2.h"
  18. #include "driver-bf1.h"
  19. #include <stdio.h>
  20. struct device_drv bf1_drv;
  21. //------------------------------------------------------------------------------
  22. uint32_t bf1_decnonce(uint32_t in)
  23. {
  24. uint32_t out;
  25. /* First part load */
  26. out = (in & 0xFF) << 24; in >>= 8;
  27. /* Byte reversal */
  28. in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
  29. in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
  30. in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
  31. out |= (in >> 2)&0x3FFFFF;
  32. /* Extraction */
  33. if (in & 1) out |= (1 << 23);
  34. if (in & 2) out |= (1 << 22);
  35. out -= 0x800004;
  36. return out;
  37. }
  38. //------------------------------------------------------------------------------
  39. int bf1_rehash(unsigned char *midstate, unsigned m7, unsigned ntime, unsigned nbits, unsigned nnonce)
  40. {
  41. uint8_t in[16];
  42. uint32_t *in32 = (uint32_t *)in;
  43. char *hex;
  44. uint32_t *mid32 = (uint32_t *)midstate;
  45. uint32_t out32[8];
  46. uint8_t *out = (uint8_t *) out32;
  47. sha2_context ctx;
  48. memset( &ctx, 0, sizeof(sha2_context));
  49. memcpy(ctx.state, mid32, 8*4);
  50. ctx.total[0] = 64;
  51. ctx.total[1] = 0;
  52. nnonce = bswap_32(nnonce);
  53. in32[0] = bswap_32(m7);
  54. in32[1] = bswap_32(ntime);
  55. in32[2] = bswap_32(nbits);
  56. in32[3] = nnonce;
  57. sha2_update(&ctx, in, 16);
  58. sha2_finish(&ctx, out);
  59. sha2(out, 32, out);
  60. if (out32[7] == 0)
  61. {
  62. hex = bin2hex(out, 32);
  63. applog(LOG_INFO, "! MS0: %08x, m7: %08x, ntime: %08x, nbits: %08x, nnonce: %08x\n\t\t\t out: %s\n", mid32[0], m7, ntime, nbits, nnonce, hex);
  64. return 1;
  65. }
  66. #if 0
  67. else
  68. {
  69. applog(LOG_INFO, "DATA: %08X %08X %08X %08X", m7, ntime, nbits, nnonce);
  70. }
  71. #endif
  72. return 0;
  73. }
  74. //------------------------------------------------------------------------------
  75. static bool bf1_detect_custom(const char *devpath, struct device_drv *api, struct BF1Info *info)
  76. {
  77. int fd = serial_open(devpath, info->baud, 1, true);
  78. if(fd < 0)
  79. {
  80. return false;
  81. }
  82. char buf[sizeof(struct BF1Identity)+1];
  83. int len;
  84. write(fd, "I", 1);
  85. len = serial_read(fd, buf, sizeof(buf));
  86. if(len != 14)
  87. {
  88. serial_close(fd);
  89. return false;
  90. }
  91. info->id.version = buf[1];
  92. memcpy(info->id.product, buf+2, 8);
  93. memcpy(&info->id.serial, buf+10, 4);
  94. applog(LOG_DEBUG, "%d, %s %08x", info->id.version, info->id.product, info->id.serial);
  95. char buf_state[sizeof(struct BF1State)+1];
  96. len = 0;
  97. write(fd, "R", 1);
  98. while(len == 0)
  99. {
  100. len = serial_read(fd, buf, sizeof(buf_state));
  101. nmsleep(100);
  102. }
  103. serial_close(fd);
  104. if(len != 7)
  105. {
  106. applog(LOG_ERR, "Not responding to reset: %d", len);
  107. return false;
  108. }
  109. if(serial_claim(devpath, api))
  110. {
  111. const char *claimedby = serial_claim(devpath, api)->dname;
  112. applog(LOG_DEBUG, "Bitfury BF1 device %s already claimed by other driver: %s", devpath, claimedby);
  113. return false;
  114. }
  115. /* We have a real MiniMiner ONE! */
  116. struct cgpu_info *bf1;
  117. bf1 = calloc(1, sizeof(struct cgpu_info));
  118. bf1->drv = api;
  119. bf1->device_path = strdup(devpath);
  120. bf1->device_fd = -1;
  121. bf1->threads = 1;
  122. add_cgpu(bf1);
  123. applog(LOG_INFO, "Found %"PRIpreprv" at %s", bf1->proc_repr, devpath);
  124. applog(LOG_DEBUG, "%"PRIpreprv": Init: baud=%d",
  125. bf1->proc_repr, info->baud);
  126. bf1->device_data = info;
  127. return true;
  128. }
  129. //------------------------------------------------------------------------------
  130. static bool bf1_detect_one(const char *devpath)
  131. {
  132. struct BF1Info *info = calloc(1, sizeof(struct BF1Info));
  133. if (unlikely(!info))
  134. quit(1, "Failed to malloc bf1Info");
  135. applog(LOG_INFO, "Detect Bitfury BF1 device: %s", devpath);
  136. info->baud = BF1_BAUD;
  137. if (!bf1_detect_custom(devpath, &bf1_drv, info))
  138. {
  139. free(info);
  140. return false;
  141. }
  142. return true;
  143. }
  144. //------------------------------------------------------------------------------
  145. static int bf1_detect_auto(void)
  146. {
  147. applog(LOG_INFO, "Autodetect Bitfury BF1 devices");
  148. return serial_autodetect(bf1_detect_one, "Bitfury_BF1");
  149. }
  150. //------------------------------------------------------------------------------
  151. static void bf1_detect()
  152. {
  153. applog(LOG_INFO, "Searching for Bitfury BF1 devices");
  154. //serial_detect(&bf1_drv, bf1_detect_one);
  155. serial_detect_auto(&bf1_drv, bf1_detect_one, bf1_detect_auto);
  156. }
  157. //------------------------------------------------------------------------------
  158. static bool bf1_init(struct thr_info *thr)
  159. {
  160. applog(LOG_INFO, "Bitfury BF1 init");
  161. struct cgpu_info *bf1 = thr->cgpu;
  162. struct BF1Info *info = (struct BF1Info *)bf1->device_data;
  163. int fd = serial_open(bf1->device_path, info->baud, 1, true);
  164. if (unlikely(-1 == fd))
  165. {
  166. applog(LOG_ERR, "Failed to open Bitfury BF1 on %s", bf1->device_path);
  167. return false;
  168. }
  169. bf1->device_fd = fd;
  170. applog(LOG_INFO, "Opened Bitfury BF1 on %s", bf1->device_path);
  171. struct timeval tv_now;
  172. gettimeofday(&tv_now, NULL);
  173. timer_set_delay(&thr->tv_poll, &tv_now, 1000000);
  174. info->work = 0;
  175. info->prev_work[0] = 0;
  176. info->prev_work[1] = 0;
  177. return true;
  178. }
  179. //------------------------------------------------------------------------------
  180. static bool duplicate(uint32_t *results, uint32_t size, uint32_t test_nonce)
  181. {
  182. for(uint32_t i=0; i<size; i++)
  183. {
  184. if(results[i] == test_nonce)
  185. {
  186. return true;
  187. }
  188. }
  189. return false;
  190. }
  191. //------------------------------------------------------------------------------
  192. static void bf1_process_results(struct thr_info *thr, struct work *work)
  193. {
  194. struct cgpu_info *board = thr->cgpu;
  195. struct BF1Info *info = (struct BF1Info *)board->device_data;
  196. uint32_t results[16*6];
  197. uint32_t num_results;
  198. uint32_t m7 = *((uint32_t *)&work->data[64]);
  199. uint32_t ntime = *((uint32_t *)&work->data[68]);
  200. uint32_t nbits = *((uint32_t *)&work->data[72]);
  201. int32_t nonces = (info->rx_len / 7) - 1;
  202. num_results = 0;
  203. for(int i=0; i<info->rx_len; i+=7)
  204. {
  205. struct BF1State state;
  206. state.state = info->rx_buffer[i + 1];
  207. state.switched = info->rx_buffer[i + 2];
  208. memcpy(&state.nonce, info->rx_buffer + i + 3, 4);
  209. if(duplicate(results, num_results, state.nonce))
  210. {
  211. continue;
  212. }
  213. uint32_t nonce = bf1_decnonce(state.nonce);
  214. results[num_results++] = state.nonce;
  215. //applog(LOG_INFO, "Len: %d Cmd: %c State: %c Switched: %d Nonce: %08X", info->rx_len, info->rx_buffer[i], state->state, state->switched, nonce);
  216. if(bf1_rehash(work->midstate, m7, ntime, nbits, nonce))
  217. {
  218. submit_nonce(thr, work, nonce);
  219. nonces--;
  220. continue;
  221. }
  222. if(bf1_rehash(work->midstate, m7, ntime, nbits, nonce-0x400000))
  223. {
  224. submit_nonce(thr, work, nonce-0x400000);
  225. nonces--;
  226. continue;
  227. }
  228. if(bf1_rehash(work->midstate, m7, ntime, nbits, nonce-0x800000))
  229. {
  230. submit_nonce(thr, work, nonce-0x800000);
  231. nonces--;
  232. continue;
  233. }
  234. if(bf1_rehash(work->midstate, m7, ntime, nbits, nonce+0x2800000))
  235. {
  236. submit_nonce(thr, work, nonce+0x2800000);
  237. nonces--;
  238. continue;
  239. }
  240. if(bf1_rehash(work->midstate, m7, ntime, nbits, nonce+0x2C00000))
  241. {
  242. submit_nonce(thr, work, nonce+0x2C00000);
  243. nonces--;
  244. continue;
  245. }
  246. if(bf1_rehash(work->midstate, m7, ntime, nbits, nonce+0x400000))
  247. {
  248. submit_nonce(thr, work, nonce+0x400000);
  249. nonces--;
  250. continue;
  251. }
  252. }
  253. for(int i=0; i<nonces; i++)
  254. inc_hw_errors(thr);
  255. }
  256. //------------------------------------------------------------------------------
  257. static int64_t bf1_scanwork(struct thr_info *thr)
  258. {
  259. struct cgpu_info *board = thr->cgpu;
  260. struct BF1Info *info = (struct BF1Info *)board->device_data;
  261. uint32_t hashes = 0;
  262. if(!info->work)
  263. {
  264. info->work = get_queued(board);
  265. if(info->work == NULL)
  266. return 0;
  267. }
  268. uint8_t sendbuf[45];
  269. sendbuf[0] = 'W';
  270. memcpy(sendbuf + 1, info->work->midstate, 32);
  271. memcpy(sendbuf + 33, info->work->data + 64, 12);
  272. write(board->device_fd, sendbuf, sizeof(sendbuf));
  273. applog(LOG_INFO, "Work Task sending: %d", info->work->id);
  274. while(1)
  275. {
  276. uint8_t buffer[7];
  277. int len;
  278. len = serial_read(board->device_fd, buffer, 7);
  279. if(len > 0)
  280. break;
  281. }
  282. applog(LOG_INFO, "Work Task sent");
  283. while(1)
  284. {
  285. info->rx_len = serial_read(board->device_fd, info->rx_buffer, sizeof(info->rx_buffer));
  286. if(info->rx_len > 0)
  287. break;
  288. }
  289. applog(LOG_INFO, "Work Task accepted");
  290. applog(LOG_INFO, "Nonces sent back: %d", info->rx_len / 7);
  291. /*
  292. if(info->prev_work[1])
  293. {
  294. applog(LOG_INFO, "PREV[1]");
  295. bf1_process_results(thr, info->prev_work[1]);
  296. work_completed(board, info->prev_work[1]);
  297. info->prev_work[1] = 0;
  298. }
  299. */
  300. if(info->prev_work[0])
  301. {
  302. applog(LOG_INFO, "PREV[0]");
  303. bf1_process_results(thr, info->prev_work[0]);
  304. }
  305. info->prev_work[1] = info->prev_work[0];
  306. info->prev_work[0] = info->work;
  307. info->work = 0;
  308. //hashes = 0xffffffff;
  309. hashes = 0xBD000000;
  310. applog(LOG_INFO, "WORK completed");
  311. return hashes;
  312. }
  313. //------------------------------------------------------------------------------
  314. static void bf1_poll(struct thr_info *thr)
  315. {
  316. /*
  317. struct cgpu_info *board = thr->cgpu;
  318. uint8_t rx_buf[128];
  319. int len = 0;
  320. len = serial_read(board->device_fd, rx_buf, sizeof(rx_buf));
  321. applog(LOG_INFO, "POLL: serial read: %d", len);
  322. */
  323. struct timeval tv_now;
  324. gettimeofday(&tv_now, NULL);
  325. timer_set_delay(&thr->tv_poll, &tv_now, 1000000);
  326. }
  327. //------------------------------------------------------------------------------
  328. static void bf1_statline_before(char *buf, struct cgpu_info *cgpu)
  329. {
  330. char before[] = " ";
  331. if(cgpu->device_data)
  332. {
  333. struct BF1Info *info = (struct BF1Info *)cgpu->device_data;
  334. int len = strlen(info->id.product);
  335. memcpy(before, info->id.product, len);
  336. sprintf(before + len + 1, "%08X", info->id.serial);
  337. }
  338. tailsprintf(buf, "%s | ", &before[0]);
  339. }
  340. //------------------------------------------------------------------------------
  341. static void bf1_shutdown(struct thr_info *thr)
  342. {
  343. struct cgpu_info *cgpu = thr->cgpu;
  344. serial_close(cgpu->device_fd);
  345. }
  346. //------------------------------------------------------------------------------
  347. static bool bf1_identify(struct cgpu_info *cgpu)
  348. {
  349. char buf[] = "L";
  350. write(cgpu->device_fd, buf, sizeof(buf));
  351. return true;
  352. }
  353. //------------------------------------------------------------------------------
  354. struct device_drv bf1_drv = {
  355. .dname = "Bitfury BF1",
  356. .name = "BF1",
  357. .minerloop = hash_queued_work,
  358. .drv_detect = bf1_detect,
  359. .get_statline_before = bf1_statline_before,
  360. .identify_device = bf1_identify,
  361. .thread_init = bf1_init,
  362. .thread_shutdown = bf1_shutdown,
  363. .poll = bf1_poll,
  364. .scanwork = bf1_scanwork,
  365. };