driver-bf1.c 11 KB

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