driver-bf2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Copyright 2013 Andreas Auer
  3. * Copyright 2013 Luke Dashjr
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. /*
  11. * Big Picture Mining USB miner with Bitfury ASIC
  12. */
  13. #include "config.h"
  14. #include "miner.h"
  15. #include "logging.h"
  16. #include "util.h"
  17. #include "libbitfury.h"
  18. #include "lowlevel.h"
  19. #include "lowl-vcom.h"
  20. #include "deviceapi.h"
  21. #include "sha2.h"
  22. #include "driver-bf2.h"
  23. #include <stdio.h>
  24. #include <pthread.h>
  25. #include <termios.h>
  26. BFG_REGISTER_DRIVER(bf2_drv)
  27. //------------------------------------------------------------------------------
  28. static bool bf2_detect_custom(const char *devpath, struct device_drv *api, struct bf2_info *info)
  29. {
  30. int fd = serial_open(devpath, info->baud, 1, true);
  31. if(fd < 0)
  32. {
  33. return false;
  34. }
  35. char buf[1024];
  36. int len;
  37. serial_read(fd, buf, sizeof(buf));
  38. if (1 != write(fd, "I", 1))
  39. {
  40. applog(LOG_ERR, "%s: Failed writing id request to %s",
  41. bf2_drv.dname, devpath);
  42. return false;
  43. }
  44. len = serial_read(fd, buf, sizeof(buf));
  45. if(len != 21)
  46. {
  47. serial_close(fd);
  48. return false;
  49. }
  50. info->id.version = buf[1];
  51. memcpy(info->id.product, buf+2, 8);
  52. memcpy(&info->id.serial, buf+10, 4);
  53. applog(LOG_DEBUG, "%s: %s: %d, %s %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  54. bf2_drv.dname,
  55. devpath,
  56. info->id.version, info->id.product,
  57. info->id.serial[0],
  58. info->id.serial[1],
  59. info->id.serial[2],
  60. info->id.serial[3],
  61. info->id.serial[4],
  62. info->id.serial[5],
  63. info->id.serial[6],
  64. info->id.serial[7],
  65. info->id.serial[8],
  66. info->id.serial[9],
  67. info->id.serial[10]
  68. );
  69. char buf_state[sizeof(struct bf2_state)+1];
  70. len = 0;
  71. if (1 != write(fd, "R", 1))
  72. {
  73. applog(LOG_ERR, "%s: Failed writing reset request to %s",
  74. bf2_drv.dname, devpath);
  75. return false;
  76. }
  77. while(len == 0)
  78. {
  79. len = serial_read(fd, buf, sizeof(buf_state));
  80. cgsleep_ms(100);
  81. }
  82. serial_close(fd);
  83. if(len != 8)
  84. {
  85. applog(LOG_ERR, "%s: %s not responding to reset: %d",
  86. bf2_drv.dname,
  87. devpath, len);
  88. return false;
  89. }
  90. if (serial_claim_v(devpath, api))
  91. return false;
  92. struct cgpu_info *bigpic;
  93. bigpic = calloc(1, sizeof(struct cgpu_info));
  94. bigpic->drv = api;
  95. bigpic->device_path = strdup(devpath);
  96. bigpic->device_fd = -1;
  97. bigpic->threads = 1;
  98. bigpic->procs = 2;
  99. add_cgpu(bigpic);
  100. applog(LOG_INFO, "Found %"PRIpreprv" at %s", bigpic->proc_repr, devpath);
  101. applog(LOG_DEBUG, "%"PRIpreprv": Init: baud=%d",
  102. bigpic->proc_repr, info->baud);
  103. bigpic->device_data = info;
  104. return true;
  105. }
  106. //------------------------------------------------------------------------------
  107. static bool bf2_detect_one(const char *devpath)
  108. {
  109. struct bf2_info *info = calloc(1, sizeof(struct bf2_info));
  110. if (unlikely(!info))
  111. quit(1, "Failed to malloc bigpicInfo");
  112. info->baud = BPM_BAUD;
  113. if (!bf2_detect_custom(devpath, &bf2_drv, info))
  114. {
  115. free(info);
  116. return false;
  117. }
  118. return true;
  119. }
  120. //------------------------------------------------------------------------------
  121. static int bf2_detect_auto(void)
  122. {
  123. return serial_autodetect(bf2_detect_one, "Bitfury", "BF2");
  124. }
  125. //------------------------------------------------------------------------------
  126. static void bf2_detect()
  127. {
  128. serial_detect_auto(&bf2_drv, bf2_detect_one, bf2_detect_auto);
  129. }
  130. //------------------------------------------------------------------------------
  131. static bool bf2_init(struct thr_info *thr)
  132. {
  133. struct cgpu_info * const cgpu = thr->cgpu;
  134. struct bf2_info *info = (struct bf2_info *)cgpu->device_data;
  135. struct cgpu_info *proc;
  136. int i=0;
  137. applog(LOG_DEBUG, "%"PRIpreprv": init", cgpu->proc_repr);
  138. for(i=1, proc = cgpu->next_proc; proc; proc = proc->next_proc, i++)
  139. {
  140. struct bf2_info *data = calloc(1, sizeof(struct bf2_info));
  141. proc->device_data = data;
  142. data->tx_buffer[0] = 'W';
  143. data->tx_buffer[1] = i;
  144. }
  145. int fd = serial_open(cgpu->device_path, info->baud, 1, true);
  146. if (unlikely(-1 == fd))
  147. {
  148. applog(LOG_ERR, "%"PRIpreprv": Failed to open %s",
  149. cgpu->proc_repr, cgpu->device_path);
  150. return false;
  151. }
  152. cgpu->device_fd = fd;
  153. applog(LOG_INFO, "%"PRIpreprv": Opened %s", cgpu->proc_repr, cgpu->device_path);
  154. info->tx_buffer[0] = 'W';
  155. info->tx_buffer[1] = 0x00;
  156. mutex_init(&cgpu->device_mutex);
  157. timer_set_now(&thr->tv_poll);
  158. return true;
  159. }
  160. //------------------------------------------------------------------------------
  161. static bool bf2_process_results(struct cgpu_info * const proc)
  162. {
  163. struct bf2_info *device = proc->device_data;
  164. uint8_t *rx_buffer = device->rx_buffer;
  165. uint32_t rx_len = device->rx_len;
  166. struct work *work = proc->thr[0]->work;
  167. if(rx_buffer[3] == 0)
  168. {
  169. return false;
  170. }
  171. if(!work)
  172. {
  173. return true;
  174. }
  175. uint32_t m7 = *((uint32_t *)&work->data[64]);
  176. uint32_t ntime = *((uint32_t *)&work->data[68]);
  177. uint32_t nbits = *((uint32_t *)&work->data[72]);
  178. int j=0;
  179. for(j=0; j<rx_len; j+= 8)
  180. {
  181. struct bf2_state state;
  182. state.chip = rx_buffer[j + 1];
  183. state.state = rx_buffer[j + 2];
  184. state.switched = rx_buffer[j + 3];
  185. memcpy(&state.nonce, rx_buffer + j + 4, 4);
  186. uint32_t nonce = bitfury_decnonce(state.nonce);
  187. if((nonce & 0xFFC00000) != 0xdf800000)
  188. {
  189. applog(LOG_DEBUG, "%"PRIpreprv": Len: %lu Cmd: %c Chip: %d State: %c Switched: %d Nonce: %08lx",
  190. proc->proc_repr,
  191. (unsigned long)rx_len, rx_buffer[j], state.chip, state.state, state.switched, (unsigned long)nonce);
  192. if (bitfury_fudge_nonce(work->midstate, m7, ntime, nbits, &nonce))
  193. submit_nonce(proc->thr[0], work, nonce);
  194. else
  195. inc_hw_errors(proc->thr[0], work, nonce);
  196. }
  197. }
  198. return true;
  199. }
  200. //------------------------------------------------------------------------------
  201. static bool bf2_send_command(int fd, uint8_t *tx, uint16_t tx_size)
  202. {
  203. if(tx_size != write(fd, tx, tx_size))
  204. {
  205. return false;
  206. }
  207. tcflush(fd, TCIOFLUSH);
  208. return true;
  209. }
  210. //------------------------------------------------------------------------------
  211. static uint16_t bf2_wait_response(int fd, uint8_t *rx, uint16_t rx_size)
  212. {
  213. uint16_t rx_len;
  214. int timeout = 20;
  215. while(timeout > 0)
  216. {
  217. rx_len = serial_read(fd, rx, rx_size);
  218. if(rx_len > 0)
  219. break;
  220. timeout--;
  221. }
  222. if(unlikely(timeout == 0))
  223. {
  224. return 0;
  225. }
  226. return rx_len;
  227. }
  228. //------------------------------------------------------------------------------
  229. int64_t bf2_job_process_results(struct thr_info *thr, struct work *work, bool stopping)
  230. {
  231. // Bitfury chips process only 768/1024 of the nonce range
  232. return 0xbd000000;
  233. }
  234. //------------------------------------------------------------------------------
  235. static
  236. bool bf2_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  237. {
  238. struct cgpu_info *board = thr->cgpu;
  239. struct bf2_info *info = (struct bf2_info *)board->device_data;
  240. memcpy(&info->tx_buffer[ 2], work->midstate, 32);
  241. memcpy(&info->tx_buffer[34], &work->data[64], 12);
  242. work->blk.nonce = 0xffffffff;
  243. return true;
  244. }
  245. //------------------------------------------------------------------------------
  246. static
  247. void bf2_poll(struct thr_info *thr)
  248. {
  249. struct cgpu_info * const dev = thr->cgpu;
  250. struct cgpu_info *proc;
  251. uint8_t n_chips = 0;
  252. uint8_t timeout = 10;
  253. uint8_t buffer[2] = { 'Q', 0 };
  254. uint8_t response[8];
  255. uint8_t i=0;
  256. mutex_lock(&dev->device_mutex);
  257. for(proc = thr->cgpu; proc; proc = proc->next_proc, n_chips++)
  258. {
  259. struct bf2_info *info = (struct bf2_info *)proc->device_data;
  260. buffer[1] = n_chips;
  261. if(2 != write(dev->device_fd, buffer, 2))
  262. {
  263. applog(LOG_ERR, "%"PRIpreprv": Failed writing work task", proc->proc_repr);
  264. dev_error(dev, REASON_DEV_COMMS_ERROR);
  265. return;
  266. }
  267. timeout = 20;
  268. while(timeout > 0)
  269. {
  270. info->rx_len = serial_read(dev->device_fd, info->rx_buffer, sizeof(info->rx_buffer));
  271. if(info->rx_len > 0)
  272. break;
  273. timeout--;
  274. }
  275. if(unlikely(timeout == 0))
  276. {
  277. applog(LOG_ERR, "%"PRIpreprv": Query timeout", proc->proc_repr);
  278. }
  279. if(bf2_process_results(proc) == true)
  280. {
  281. struct thr_info *proc_thr = proc->thr[0];
  282. mt_job_transition(proc_thr);
  283. // TODO: Delay morework until right before it's needed
  284. timer_set_now(&proc_thr->tv_morework);
  285. job_start_complete(proc_thr);
  286. }
  287. }
  288. buffer[0] = 'T';
  289. if(bf2_send_command(dev->device_fd, buffer, 1))
  290. {
  291. if(8 == bf2_wait_response(dev->device_fd, response, 8))
  292. {
  293. if(response[0] == buffer[0])
  294. {
  295. uint16_t temp = response[4] | (response[5] << 8);
  296. char hex[93];
  297. bin2hex(hex, response, 8);
  298. applog(LOG_DEBUG, "%"PRIpreprv": TEMP: %s",
  299. dev->dev_repr, hex);
  300. dev->temp = (float)temp / 10.0;
  301. applog(LOG_DEBUG, "%"PRIpreprv": Temperature: %f", dev->dev_repr, dev->temp);
  302. }
  303. }
  304. else
  305. {
  306. applog(LOG_DEBUG, "%"PRIpreprv": No temperature response", dev->dev_repr);
  307. }
  308. }
  309. mutex_unlock(&dev->device_mutex);
  310. timer_set_delay_from_now(&thr->tv_poll, 250000);
  311. }
  312. //------------------------------------------------------------------------------
  313. static
  314. void bf2_job_start(struct thr_info *thr)
  315. {
  316. struct cgpu_info *board = thr->cgpu;
  317. struct bf2_info *info = (struct bf2_info *)board->device_data;
  318. int timeout = 50;
  319. int device_fd = thr->cgpu->device->device_fd;
  320. if (opt_dev_protocol && opt_debug)
  321. {
  322. char hex[93];
  323. bin2hex(hex, info->tx_buffer, 46);
  324. applog(LOG_DEBUG, "%"PRIpreprv": SEND: %s",
  325. board->proc_repr, hex);
  326. }
  327. mutex_lock(&board->device_mutex);
  328. if (46 != write(device_fd, info->tx_buffer, 46))
  329. {
  330. applog(LOG_ERR, "%"PRIpreprv": Failed writing work task", board->proc_repr);
  331. dev_error(board, REASON_DEV_COMMS_ERROR);
  332. job_start_abort(thr, true);
  333. return;
  334. }
  335. while(timeout > 0)
  336. {
  337. uint8_t buffer[8];
  338. int len;
  339. len = serial_read(device_fd, buffer, 8);
  340. if(len > 0)
  341. break;
  342. timeout--;
  343. }
  344. mutex_unlock(&board->device_mutex);
  345. if(unlikely(timeout == 0))
  346. {
  347. applog(LOG_ERR, "%"PRIpreprv": Timeout.", board->proc_repr);
  348. }
  349. }
  350. //------------------------------------------------------------------------------
  351. static void bf2_shutdown(struct thr_info *thr)
  352. {
  353. struct cgpu_info *cgpu = thr->cgpu;
  354. mutex_lock(&cgpu->device_mutex);
  355. serial_close(cgpu->device_fd);
  356. mutex_unlock(&cgpu->device_mutex);
  357. }
  358. //------------------------------------------------------------------------------
  359. static bool bf2_identify(struct cgpu_info *cgpu)
  360. {
  361. char buf[] = "L";
  362. mutex_lock(&cgpu->device_mutex);
  363. if (1 != write(cgpu->device_fd, buf, 1))
  364. return false;
  365. mutex_unlock(&cgpu->device_mutex);
  366. return true;
  367. }
  368. //------------------------------------------------------------------------------
  369. struct device_drv bf2_drv = {
  370. .dname = "Twin Bitfury",
  371. .name = "TBF",
  372. .drv_detect = bf2_detect,
  373. .identify_device = bf2_identify,
  374. .thread_init = bf2_init,
  375. .minerloop = minerloop_async,
  376. .job_prepare = bf2_job_prepare,
  377. .job_start = bf2_job_start,
  378. .poll = bf2_poll,
  379. .job_process_results = bf2_job_process_results,
  380. .thread_shutdown = bf2_shutdown,
  381. };