driver-twinfury.c 11 KB

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