driver-bigpic.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. * Big Picture Mining 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-bigpic.h"
  19. #include <stdio.h>
  20. struct device_drv bigpic_drv;
  21. //------------------------------------------------------------------------------
  22. uint32_t bigpic_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 bigpic_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_DEBUG, "! MS0: %08x, m7: %08x, ntime: %08x, nbits: %08x, nnonce: %08x", mid32[0], m7, ntime, nbits, nnonce);
  64. applog(LOG_DEBUG, " out: %s", hex);
  65. return 1;
  66. }
  67. return 0;
  68. }
  69. //------------------------------------------------------------------------------
  70. static bool bigpic_detect_custom(const char *devpath, struct device_drv *api, struct bigpic_info *info)
  71. {
  72. int fd = serial_open(devpath, info->baud, 1, true);
  73. if(fd < 0)
  74. {
  75. return false;
  76. }
  77. char buf[sizeof(struct bigpic_identity)+1];
  78. int len;
  79. write(fd, "I", 1);
  80. len = serial_read(fd, buf, sizeof(buf));
  81. if(len != 14)
  82. {
  83. serial_close(fd);
  84. return false;
  85. }
  86. info->id.version = buf[1];
  87. memcpy(info->id.product, buf+2, 8);
  88. memcpy(&info->id.serial, buf+10, 4);
  89. applog(LOG_DEBUG, "%s: %s: %d, %s %08x",
  90. bigpic_drv.dname,
  91. devpath,
  92. info->id.version, info->id.product, info->id.serial);
  93. char buf_state[sizeof(struct bigpic_state)+1];
  94. len = 0;
  95. write(fd, "R", 1);
  96. while(len == 0)
  97. {
  98. len = serial_read(fd, buf, sizeof(buf_state));
  99. cgsleep_ms(100);
  100. }
  101. serial_close(fd);
  102. if(len != 7)
  103. {
  104. applog(LOG_ERR, "%s: %s not responding to reset: %d",
  105. bigpic_drv.dname,
  106. devpath, len);
  107. return false;
  108. }
  109. if (serial_claim_v(devpath, api))
  110. return false;
  111. struct cgpu_info *bigpic;
  112. bigpic = calloc(1, sizeof(struct cgpu_info));
  113. bigpic->drv = api;
  114. bigpic->device_path = strdup(devpath);
  115. bigpic->device_fd = -1;
  116. bigpic->threads = 1;
  117. add_cgpu(bigpic);
  118. applog(LOG_INFO, "Found %"PRIpreprv" at %s", bigpic->proc_repr, devpath);
  119. applog(LOG_DEBUG, "%"PRIpreprv": Init: baud=%d",
  120. bigpic->proc_repr, info->baud);
  121. bigpic->device_data = info;
  122. return true;
  123. }
  124. //------------------------------------------------------------------------------
  125. static bool bigpic_detect_one(const char *devpath)
  126. {
  127. struct bigpic_info *info = calloc(1, sizeof(struct bigpic_info));
  128. if (unlikely(!info))
  129. quit(1, "Failed to malloc bigpicInfo");
  130. info->baud = BPM_BAUD;
  131. if (!bigpic_detect_custom(devpath, &bigpic_drv, info))
  132. {
  133. free(info);
  134. return false;
  135. }
  136. return true;
  137. }
  138. //------------------------------------------------------------------------------
  139. static int bigpic_detect_auto(void)
  140. {
  141. return serial_autodetect(bigpic_detect_one, "Bitfury_BF1");
  142. }
  143. //------------------------------------------------------------------------------
  144. static void bigpic_detect()
  145. {
  146. serial_detect_auto(&bigpic_drv, bigpic_detect_one, bigpic_detect_auto);
  147. }
  148. //------------------------------------------------------------------------------
  149. static bool bigpic_init(struct thr_info *thr)
  150. {
  151. struct cgpu_info *bigpic = thr->cgpu;
  152. struct bigpic_info *info = (struct bigpic_info *)bigpic->device_data;
  153. applog(LOG_DEBUG, "%"PRIpreprv": init", bigpic->proc_repr);
  154. int fd = serial_open(bigpic->device_path, info->baud, 1, true);
  155. if (unlikely(-1 == fd))
  156. {
  157. applog(LOG_ERR, "%"PRIpreprv": Failed to open %s",
  158. bigpic->proc_repr, bigpic->device_path);
  159. return false;
  160. }
  161. bigpic->device_fd = fd;
  162. applog(LOG_INFO, "%"PRIpreprv": Opened %s", bigpic->proc_repr, bigpic->device_path);
  163. struct timeval tv_now;
  164. gettimeofday(&tv_now, NULL);
  165. timer_set_delay(&thr->tv_poll, &tv_now, 1000000);
  166. info->work = 0;
  167. info->prev_work[0] = 0;
  168. info->prev_work[1] = 0;
  169. return true;
  170. }
  171. //------------------------------------------------------------------------------
  172. static bool duplicate(uint32_t *results, uint32_t size, uint32_t test_nonce)
  173. {
  174. for(uint32_t i=0; i<size; i++)
  175. {
  176. if(results[i] == test_nonce)
  177. {
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. //------------------------------------------------------------------------------
  184. static void bigpic_process_results(struct thr_info *thr, struct work *work)
  185. {
  186. struct cgpu_info *board = thr->cgpu;
  187. struct bigpic_info *info = (struct bigpic_info *)board->device_data;
  188. uint32_t results[16*6];
  189. uint32_t num_results;
  190. uint32_t m7 = *((uint32_t *)&work->data[64]);
  191. uint32_t ntime = *((uint32_t *)&work->data[68]);
  192. uint32_t nbits = *((uint32_t *)&work->data[72]);
  193. int32_t nonces = (info->rx_len / 7) - 1;
  194. num_results = 0;
  195. for(int i=0; i<info->rx_len; i+=7)
  196. {
  197. struct bigpic_state state;
  198. state.state = info->rx_buffer[i + 1];
  199. state.switched = info->rx_buffer[i + 2];
  200. memcpy(&state.nonce, info->rx_buffer + i + 3, 4);
  201. if(duplicate(results, num_results, state.nonce))
  202. {
  203. continue;
  204. }
  205. uint32_t nonce = bigpic_decnonce(state.nonce);
  206. results[num_results++] = state.nonce;
  207. //applog(LOG_DEBUG, "%"PRIpreprv": Len: %d Cmd: %c State: %c Switched: %d Nonce: %08X", board->proc_repr, info->rx_len, info->rx_buffer[i], state->state, state->switched, nonce);
  208. if(bigpic_rehash(work->midstate, m7, ntime, nbits, nonce))
  209. {
  210. submit_nonce(thr, work, nonce);
  211. nonces--;
  212. continue;
  213. }
  214. if(bigpic_rehash(work->midstate, m7, ntime, nbits, nonce-0x400000))
  215. {
  216. submit_nonce(thr, work, nonce-0x400000);
  217. nonces--;
  218. continue;
  219. }
  220. if(bigpic_rehash(work->midstate, m7, ntime, nbits, nonce-0x800000))
  221. {
  222. submit_nonce(thr, work, nonce-0x800000);
  223. nonces--;
  224. continue;
  225. }
  226. if(bigpic_rehash(work->midstate, m7, ntime, nbits, nonce+0x2800000))
  227. {
  228. submit_nonce(thr, work, nonce+0x2800000);
  229. nonces--;
  230. continue;
  231. }
  232. if(bigpic_rehash(work->midstate, m7, ntime, nbits, nonce+0x2C00000))
  233. {
  234. submit_nonce(thr, work, nonce+0x2C00000);
  235. nonces--;
  236. continue;
  237. }
  238. if(bigpic_rehash(work->midstate, m7, ntime, nbits, nonce+0x400000))
  239. {
  240. submit_nonce(thr, work, nonce+0x400000);
  241. nonces--;
  242. continue;
  243. }
  244. inc_hw_errors(thr, work, nonce);
  245. }
  246. }
  247. //------------------------------------------------------------------------------
  248. static int64_t bigpic_scanwork(struct thr_info *thr)
  249. {
  250. struct cgpu_info *board = thr->cgpu;
  251. struct bigpic_info *info = (struct bigpic_info *)board->device_data;
  252. uint32_t hashes = 0;
  253. if(!info->work)
  254. {
  255. info->work = get_queued(board);
  256. if(info->work == NULL)
  257. return 0;
  258. }
  259. uint8_t sendbuf[45];
  260. sendbuf[0] = 'W';
  261. memcpy(sendbuf + 1, info->work->midstate, 32);
  262. memcpy(sendbuf + 33, info->work->data + 64, 12);
  263. write(board->device_fd, sendbuf, sizeof(sendbuf));
  264. applog(LOG_DEBUG, "%"PRIpreprv": Work Task sending: %d",
  265. board->proc_repr, info->work->id);
  266. while(1)
  267. {
  268. uint8_t buffer[7];
  269. int len;
  270. len = serial_read(board->device_fd, buffer, 7);
  271. if(len > 0)
  272. break;
  273. }
  274. applog(LOG_DEBUG, "%"PRIpreprv": Work Task sent", board->proc_repr);
  275. while(1)
  276. {
  277. info->rx_len = serial_read(board->device_fd, info->rx_buffer, sizeof(info->rx_buffer));
  278. if(info->rx_len > 0)
  279. break;
  280. }
  281. applog(LOG_DEBUG, "%"PRIpreprv": Work Task accepted", board->proc_repr);
  282. applog(LOG_DEBUG, "%"PRIpreprv": Nonces sent back: %d",
  283. board->proc_repr, info->rx_len / 7);
  284. /*
  285. if(info->prev_work[1])
  286. {
  287. applog(LOG_DEBUG, "%"PRIpreprv": PREV[1]", board->proc_repr);
  288. bigpic_process_results(thr, info->prev_work[1]);
  289. work_completed(board, info->prev_work[1]);
  290. info->prev_work[1] = 0;
  291. }
  292. */
  293. if(info->prev_work[0])
  294. {
  295. applog(LOG_DEBUG, "%"PRIpreprv": PREV[0]", board->proc_repr);
  296. bigpic_process_results(thr, info->prev_work[0]);
  297. }
  298. info->prev_work[1] = info->prev_work[0];
  299. info->prev_work[0] = info->work;
  300. info->work = 0;
  301. //hashes = 0xffffffff;
  302. hashes = 0xBD000000;
  303. applog(LOG_DEBUG, "%"PRIpreprv": WORK completed", board->proc_repr);
  304. return hashes;
  305. }
  306. //------------------------------------------------------------------------------
  307. static void bigpic_poll(struct thr_info *thr)
  308. {
  309. /*
  310. struct cgpu_info *board = thr->cgpu;
  311. uint8_t rx_buf[128];
  312. int len = 0;
  313. len = serial_read(board->device_fd, rx_buf, sizeof(rx_buf));
  314. applog(LOG_DEBUG, "%"PRIpreprv": POLL: serial read: %d", board->proc_repr, len);
  315. */
  316. struct timeval tv_now;
  317. gettimeofday(&tv_now, NULL);
  318. timer_set_delay(&thr->tv_poll, &tv_now, 1000000);
  319. }
  320. //------------------------------------------------------------------------------
  321. static void bigpic_shutdown(struct thr_info *thr)
  322. {
  323. struct cgpu_info *cgpu = thr->cgpu;
  324. serial_close(cgpu->device_fd);
  325. }
  326. //------------------------------------------------------------------------------
  327. static bool bigpic_identify(struct cgpu_info *cgpu)
  328. {
  329. char buf[] = "L";
  330. write(cgpu->device_fd, buf, sizeof(buf));
  331. return true;
  332. }
  333. //------------------------------------------------------------------------------
  334. struct device_drv bigpic_drv = {
  335. .dname = "bigpic",
  336. .name = "BPM",
  337. .minerloop = hash_queued_work,
  338. .drv_detect = bigpic_detect,
  339. .identify_device = bigpic_identify,
  340. .thread_init = bigpic_init,
  341. .thread_shutdown = bigpic_shutdown,
  342. .poll = bigpic_poll,
  343. .scanwork = bigpic_scanwork,
  344. };