driver-bf1.c 11 KB

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