driver-bf1.c 9.8 KB

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