driver-twinfury.c 11 KB

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