driver-bigpic.c 9.7 KB

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