driver-twinfury.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. * Twin Bitfury USB miner with two 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-twinfury.h"
  23. #include <stdio.h>
  24. #include <pthread.h>
  25. #include <termios.h>
  26. BFG_REGISTER_DRIVER(twinfury_drv)
  27. static const uint8_t PREAMBLE[] = { 0xDE, 0xAD, 0xBE, 0xEF };
  28. //------------------------------------------------------------------------------
  29. static bool twinfury_send_command(int fd, uint8_t *tx, uint16_t tx_size)
  30. {
  31. if(4 != write(fd, PREAMBLE, 4))
  32. {
  33. return false;
  34. }
  35. if(tx_size != write(fd, tx, tx_size))
  36. {
  37. return false;
  38. }
  39. tcflush(fd, TCIOFLUSH);
  40. return true;
  41. }
  42. //------------------------------------------------------------------------------
  43. static int16_t twinfury_wait_response(int fd, uint8_t *rx, uint16_t rx_size)
  44. {
  45. int16_t rx_len;
  46. int timeout = 20;
  47. while(timeout > 0)
  48. {
  49. rx_len = serial_read(fd, rx, rx_size);
  50. if(rx_len > 0)
  51. break;
  52. timeout--;
  53. }
  54. if(unlikely(timeout == 0))
  55. {
  56. return -1;
  57. }
  58. return rx_len;
  59. }
  60. //------------------------------------------------------------------------------
  61. static bool twinfury_detect_custom(const char *devpath, struct device_drv *api, struct twinfury_info *info)
  62. {
  63. int fd = serial_open(devpath, info->baud, 1, true);
  64. if(fd < 0)
  65. {
  66. return false;
  67. }
  68. char buf[1024];
  69. int16_t len;
  70. applog(LOG_DEBUG, "%s: Probing for Twinfury device %s", twinfury_drv.dname, devpath);
  71. serial_read(fd, buf, sizeof(buf));
  72. if (!twinfury_send_command(fd, "I", 1))
  73. {
  74. applog(LOG_ERR, "%s: Failed writing id request to %s",
  75. twinfury_drv.dname, devpath);
  76. serial_close(fd);
  77. return false;
  78. }
  79. len = twinfury_wait_response(fd, buf, sizeof(buf));
  80. if(len != 29)
  81. {
  82. applog(LOG_ERR, "%s: Not a valid response from device (%d)", twinfury_drv.dname, len);
  83. serial_close(fd);
  84. return false;
  85. }
  86. info->id.version = buf[1];
  87. memcpy(info->id.product, buf+2, 16);
  88. bin2hex(info->id.serial, buf+18, 11);
  89. applog(LOG_DEBUG, "%s: %s: %d, %s %s",
  90. twinfury_drv.dname,
  91. devpath,
  92. info->id.version, info->id.product,
  93. info->id.serial);
  94. char buf_state[sizeof(struct twinfury_state)+1];
  95. if (!twinfury_send_command(fd, "R", 1))
  96. {
  97. applog(LOG_ERR, "%s: Failed writing reset request to %s",
  98. twinfury_drv.dname, devpath);
  99. serial_close(fd);
  100. return false;
  101. }
  102. len = 0;
  103. while(len == 0)
  104. {
  105. len = serial_read(fd, buf, sizeof(buf_state));
  106. cgsleep_ms(100);
  107. }
  108. serial_close(fd);
  109. if(len != 8)
  110. {
  111. applog(LOG_ERR, "%s: %s not responding to reset: %d",
  112. twinfury_drv.dname,
  113. devpath, len);
  114. return false;
  115. }
  116. if (serial_claim_v(devpath, api))
  117. return false;
  118. struct cgpu_info *bigpic;
  119. bigpic = calloc(1, sizeof(struct cgpu_info));
  120. bigpic->drv = api;
  121. bigpic->device_path = strdup(devpath);
  122. bigpic->device_fd = -1;
  123. bigpic->threads = 1;
  124. bigpic->procs = 2;
  125. add_cgpu(bigpic);
  126. applog(LOG_INFO, "Found %"PRIpreprv" at %s", bigpic->proc_repr, devpath);
  127. applog(LOG_DEBUG, "%"PRIpreprv": Init: baud=%d",
  128. bigpic->proc_repr, info->baud);
  129. bigpic->device_data = info;
  130. return true;
  131. }
  132. //------------------------------------------------------------------------------
  133. static bool twinfury_detect_one(const char *devpath)
  134. {
  135. struct twinfury_info *info = calloc(1, sizeof(struct twinfury_info));
  136. if (unlikely(!info))
  137. quit(1, "Failed to malloc bigpicInfo");
  138. info->baud = BPM_BAUD;
  139. if (!twinfury_detect_custom(devpath, &twinfury_drv, info))
  140. {
  141. free(info);
  142. return false;
  143. }
  144. return true;
  145. }
  146. //------------------------------------------------------------------------------
  147. static bool twinfury_lowl_match(const struct lowlevel_device_info * const info)
  148. {
  149. return lowlevel_match_product(info, "Twinfury");
  150. }
  151. //------------------------------------------------------------------------------
  152. static bool twinfury_lowl_probe(const struct lowlevel_device_info * const info)
  153. {
  154. return vcom_lowl_probe_wrapper(info, twinfury_detect_one);
  155. }
  156. //------------------------------------------------------------------------------
  157. static bool twinfury_init(struct thr_info *thr)
  158. {
  159. struct cgpu_info * const cgpu = thr->cgpu;
  160. struct twinfury_info *info = (struct twinfury_info *)cgpu->device_data;
  161. struct cgpu_info *proc;
  162. int i=0;
  163. applog(LOG_DEBUG, "%"PRIpreprv": init", cgpu->proc_repr);
  164. for(i=1, proc = cgpu->next_proc; proc; proc = proc->next_proc, i++)
  165. {
  166. struct twinfury_info *data = calloc(1, sizeof(struct twinfury_info));
  167. proc->device_data = data;
  168. data->tx_buffer[0] = 'W';
  169. data->tx_buffer[1] = i;
  170. }
  171. int fd = serial_open(cgpu->device_path, info->baud, 1, true);
  172. if (unlikely(-1 == fd))
  173. {
  174. applog(LOG_ERR, "%"PRIpreprv": Failed to open %s",
  175. cgpu->proc_repr, cgpu->device_path);
  176. return false;
  177. }
  178. cgpu->device_fd = fd;
  179. cgpu->dev_serial = info->id.serial;
  180. applog(LOG_INFO, "%"PRIpreprv": Opened %s", cgpu->proc_repr, cgpu->device_path);
  181. info->tx_buffer[0] = 'W';
  182. info->tx_buffer[1] = 0x00;
  183. timer_set_now(&thr->tv_poll);
  184. return true;
  185. }
  186. //------------------------------------------------------------------------------
  187. static bool twinfury_process_results(struct cgpu_info * const proc)
  188. {
  189. struct twinfury_info *device = proc->device_data;
  190. uint8_t *rx_buffer = device->rx_buffer;
  191. int16_t rx_len = device->rx_len;
  192. struct work *work = proc->thr[0]->work;
  193. if(rx_len == 0 || rx_len == -1)
  194. {
  195. return false;
  196. }
  197. if(rx_buffer[3] == 0)
  198. {
  199. return false;
  200. }
  201. if(!work)
  202. {
  203. return true;
  204. }
  205. uint32_t m7 = *((uint32_t *)&work->data[64]);
  206. uint32_t ntime = *((uint32_t *)&work->data[68]);
  207. uint32_t nbits = *((uint32_t *)&work->data[72]);
  208. int j=0;
  209. for(j=0; j<rx_len; j+= 8)
  210. {
  211. struct twinfury_state state;
  212. state.chip = rx_buffer[j + 1];
  213. state.state = rx_buffer[j + 2];
  214. state.switched = rx_buffer[j + 3];
  215. memcpy(&state.nonce, rx_buffer + j + 4, 4);
  216. uint32_t nonce = bitfury_decnonce(state.nonce);
  217. if((nonce & 0xFFC00000) != 0xdf800000)
  218. {
  219. applog(LOG_DEBUG, "%"PRIpreprv": Len: %lu Cmd: %c Chip: %d State: %c Switched: %d Nonce: %08lx",
  220. proc->proc_repr,
  221. (unsigned long)rx_len, rx_buffer[j], state.chip, state.state, state.switched, (unsigned long)nonce);
  222. if (bitfury_fudge_nonce(work->midstate, m7, ntime, nbits, &nonce))
  223. submit_nonce(proc->thr[0], work, nonce);
  224. else
  225. inc_hw_errors(proc->thr[0], work, nonce);
  226. }
  227. }
  228. return true;
  229. }
  230. //------------------------------------------------------------------------------
  231. int64_t twinfury_job_process_results(struct thr_info *thr, struct work *work, bool stopping)
  232. {
  233. // Bitfury chips process only 768/1024 of the nonce range
  234. return 0xbd000000;
  235. }
  236. //------------------------------------------------------------------------------
  237. static
  238. bool twinfury_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  239. {
  240. struct cgpu_info *board = thr->cgpu;
  241. struct twinfury_info *info = (struct twinfury_info *)board->device_data;
  242. memcpy(&info->tx_buffer[ 2], work->midstate, 32);
  243. memcpy(&info->tx_buffer[34], &work->data[64], 12);
  244. work->blk.nonce = 0xffffffff;
  245. return true;
  246. }
  247. //------------------------------------------------------------------------------
  248. static
  249. void twinfury_poll(struct thr_info *thr)
  250. {
  251. struct cgpu_info * const dev = thr->cgpu;
  252. struct cgpu_info *proc;
  253. uint8_t n_chips = 0;
  254. uint8_t timeout = 10;
  255. uint8_t buffer[2] = { 'Q', 0 };
  256. uint8_t response[8];
  257. uint8_t i=0;
  258. if(dev->flash_led)
  259. {
  260. char buf[] = "L";
  261. dev->flash_led = 0;
  262. if(!twinfury_send_command(dev->device_fd, buf, 1))
  263. applog(LOG_ERR, "%"PRIpreprv": Failed writing flash LED", proc->proc_repr);
  264. if(1 != twinfury_wait_response(dev->device_fd, buf, 1))
  265. applog(LOG_ERR, "%"PRIpreprv": Waiting for response timed out (Flash LED)", proc->proc_repr);
  266. }
  267. for(proc = thr->cgpu; proc; proc = proc->next_proc, n_chips++)
  268. {
  269. struct twinfury_info *info = (struct twinfury_info *)proc->device_data;
  270. buffer[1] = n_chips;
  271. if(!twinfury_send_command(dev->device_fd, buffer, 2))
  272. {
  273. applog(LOG_ERR, "%"PRIpreprv": Failed writing work task", proc->proc_repr);
  274. dev_error(dev, REASON_DEV_COMMS_ERROR);
  275. return;
  276. }
  277. info->rx_len = twinfury_wait_response(dev->device_fd, info->rx_buffer, sizeof(info->rx_buffer));
  278. if(unlikely(info->rx_len == -1))
  279. {
  280. applog(LOG_ERR, "%"PRIpreprv": Query timeout", proc->proc_repr);
  281. }
  282. if(twinfury_process_results(proc) == true)
  283. {
  284. struct thr_info *proc_thr = proc->thr[0];
  285. mt_job_transition(proc_thr);
  286. // TODO: Delay morework until right before it's needed
  287. timer_set_now(&proc_thr->tv_morework);
  288. job_start_complete(proc_thr);
  289. }
  290. }
  291. buffer[0] = 'T';
  292. if(twinfury_send_command(dev->device_fd, buffer, 1))
  293. {
  294. if(8 == twinfury_wait_response(dev->device_fd, response, 8))
  295. {
  296. if(response[0] == buffer[0])
  297. {
  298. uint16_t temp = response[4] | (response[5] << 8);
  299. char hex[93];
  300. bin2hex(hex, response, 8);
  301. applog(LOG_DEBUG, "%"PRIpreprv": TEMP: %s",
  302. dev->dev_repr, hex);
  303. dev->temp = (float)temp / 10.0;
  304. applog(LOG_DEBUG, "%"PRIpreprv": Temperature: %f", dev->dev_repr, dev->temp);
  305. }
  306. }
  307. else
  308. {
  309. applog(LOG_DEBUG, "%"PRIpreprv": No temperature response", dev->dev_repr);
  310. }
  311. }
  312. timer_set_delay_from_now(&thr->tv_poll, 250000);
  313. }
  314. //------------------------------------------------------------------------------
  315. static
  316. void twinfury_job_start(struct thr_info *thr)
  317. {
  318. struct cgpu_info *board = thr->cgpu;
  319. struct twinfury_info *info = (struct twinfury_info *)board->device_data;
  320. int timeout = 50;
  321. int device_fd = thr->cgpu->device->device_fd;
  322. uint8_t buffer[8];
  323. int16_t len;
  324. if (opt_dev_protocol && opt_debug)
  325. {
  326. char hex[93];
  327. bin2hex(hex, info->tx_buffer, 46);
  328. applog(LOG_DEBUG, "%"PRIpreprv": SEND: %s",
  329. board->proc_repr, hex);
  330. }
  331. if(!twinfury_send_command(device_fd, info->tx_buffer, 46))
  332. {
  333. applog(LOG_ERR, "%"PRIpreprv": Failed writing work task", board->proc_repr);
  334. dev_error(board, REASON_DEV_COMMS_ERROR);
  335. job_start_abort(thr, true);
  336. return;
  337. }
  338. len = twinfury_wait_response(device_fd, buffer, 8);
  339. if(unlikely(len == -1))
  340. {
  341. applog(LOG_ERR, "%"PRIpreprv": Work send timeout.", board->proc_repr);
  342. }
  343. }
  344. //------------------------------------------------------------------------------
  345. static void twinfury_shutdown(struct thr_info *thr)
  346. {
  347. struct cgpu_info *cgpu = thr->cgpu;
  348. serial_close(cgpu->device_fd);
  349. }
  350. //------------------------------------------------------------------------------
  351. static bool twinfury_identify(struct cgpu_info *cgpu)
  352. {
  353. cgpu->flash_led = 1;
  354. return true;
  355. }
  356. //------------------------------------------------------------------------------
  357. struct device_drv twinfury_drv = {
  358. .dname = "Twinfury",
  359. .name = "TBF",
  360. .probe_priority = -111,
  361. .lowl_match = twinfury_lowl_match,
  362. .lowl_probe = twinfury_lowl_probe,
  363. .identify_device = twinfury_identify,
  364. .thread_init = twinfury_init,
  365. .minerloop = minerloop_async,
  366. .job_prepare = twinfury_job_prepare,
  367. .job_start = twinfury_job_start,
  368. .poll = twinfury_poll,
  369. .job_process_results = twinfury_job_process_results,
  370. .thread_shutdown = twinfury_shutdown,
  371. };