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