driver-twinfury.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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. applog(LOG_INFO, "%"PRIpreprv": Opened %s", cgpu->proc_repr, cgpu->device_path);
  143. info->tx_buffer[0] = 'W';
  144. info->tx_buffer[1] = 0x00;
  145. mutex_init(&cgpu->device_mutex);
  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_buffer[3] == 0)
  157. {
  158. return false;
  159. }
  160. if(!work)
  161. {
  162. return true;
  163. }
  164. uint32_t m7 = *((uint32_t *)&work->data[64]);
  165. uint32_t ntime = *((uint32_t *)&work->data[68]);
  166. uint32_t nbits = *((uint32_t *)&work->data[72]);
  167. int j=0;
  168. for(j=0; j<rx_len; j+= 8)
  169. {
  170. struct twinfury_state state;
  171. state.chip = rx_buffer[j + 1];
  172. state.state = rx_buffer[j + 2];
  173. state.switched = rx_buffer[j + 3];
  174. memcpy(&state.nonce, rx_buffer + j + 4, 4);
  175. uint32_t nonce = bitfury_decnonce(state.nonce);
  176. if((nonce & 0xFFC00000) != 0xdf800000)
  177. {
  178. applog(LOG_DEBUG, "%"PRIpreprv": Len: %lu Cmd: %c Chip: %d State: %c Switched: %d Nonce: %08lx",
  179. proc->proc_repr,
  180. (unsigned long)rx_len, rx_buffer[j], state.chip, state.state, state.switched, (unsigned long)nonce);
  181. if (bitfury_fudge_nonce(work->midstate, m7, ntime, nbits, &nonce))
  182. submit_nonce(proc->thr[0], work, nonce);
  183. else
  184. inc_hw_errors(proc->thr[0], work, nonce);
  185. }
  186. }
  187. return true;
  188. }
  189. //------------------------------------------------------------------------------
  190. static bool twinfury_send_command(int fd, uint8_t *tx, uint16_t tx_size)
  191. {
  192. if(tx_size != write(fd, tx, tx_size))
  193. {
  194. return false;
  195. }
  196. tcflush(fd, TCIOFLUSH);
  197. return true;
  198. }
  199. //------------------------------------------------------------------------------
  200. static uint16_t twinfury_wait_response(int fd, uint8_t *rx, uint16_t rx_size)
  201. {
  202. uint16_t rx_len;
  203. int timeout = 20;
  204. while(timeout > 0)
  205. {
  206. rx_len = serial_read(fd, rx, rx_size);
  207. if(rx_len > 0)
  208. break;
  209. timeout--;
  210. }
  211. if(unlikely(timeout == 0))
  212. {
  213. return 0;
  214. }
  215. return rx_len;
  216. }
  217. //------------------------------------------------------------------------------
  218. int64_t twinfury_job_process_results(struct thr_info *thr, struct work *work, bool stopping)
  219. {
  220. // Bitfury chips process only 768/1024 of the nonce range
  221. return 0xbd000000;
  222. }
  223. //------------------------------------------------------------------------------
  224. static
  225. bool twinfury_job_prepare(struct thr_info *thr, struct work *work, __maybe_unused uint64_t max_nonce)
  226. {
  227. struct cgpu_info *board = thr->cgpu;
  228. struct twinfury_info *info = (struct twinfury_info *)board->device_data;
  229. memcpy(&info->tx_buffer[ 2], work->midstate, 32);
  230. memcpy(&info->tx_buffer[34], &work->data[64], 12);
  231. work->blk.nonce = 0xffffffff;
  232. return true;
  233. }
  234. //------------------------------------------------------------------------------
  235. static
  236. void twinfury_poll(struct thr_info *thr)
  237. {
  238. struct cgpu_info * const dev = thr->cgpu;
  239. struct cgpu_info *proc;
  240. uint8_t n_chips = 0;
  241. uint8_t timeout = 10;
  242. uint8_t buffer[2] = { 'Q', 0 };
  243. uint8_t response[8];
  244. uint8_t i=0;
  245. mutex_lock(&dev->device_mutex);
  246. for(proc = thr->cgpu; proc; proc = proc->next_proc, n_chips++)
  247. {
  248. struct twinfury_info *info = (struct twinfury_info *)proc->device_data;
  249. buffer[1] = n_chips;
  250. if(2 != write(dev->device_fd, buffer, 2))
  251. {
  252. applog(LOG_ERR, "%"PRIpreprv": Failed writing work task", proc->proc_repr);
  253. dev_error(dev, REASON_DEV_COMMS_ERROR);
  254. return;
  255. }
  256. timeout = 20;
  257. while(timeout > 0)
  258. {
  259. info->rx_len = serial_read(dev->device_fd, info->rx_buffer, sizeof(info->rx_buffer));
  260. if(info->rx_len > 0)
  261. break;
  262. timeout--;
  263. }
  264. if(unlikely(timeout == 0))
  265. {
  266. applog(LOG_ERR, "%"PRIpreprv": Query timeout", proc->proc_repr);
  267. }
  268. if(twinfury_process_results(proc) == true)
  269. {
  270. struct thr_info *proc_thr = proc->thr[0];
  271. mt_job_transition(proc_thr);
  272. // TODO: Delay morework until right before it's needed
  273. timer_set_now(&proc_thr->tv_morework);
  274. job_start_complete(proc_thr);
  275. }
  276. }
  277. buffer[0] = 'T';
  278. if(twinfury_send_command(dev->device_fd, buffer, 1))
  279. {
  280. if(8 == twinfury_wait_response(dev->device_fd, response, 8))
  281. {
  282. if(response[0] == buffer[0])
  283. {
  284. uint16_t temp = response[4] | (response[5] << 8);
  285. char hex[93];
  286. bin2hex(hex, response, 8);
  287. applog(LOG_DEBUG, "%"PRIpreprv": TEMP: %s",
  288. dev->dev_repr, hex);
  289. dev->temp = (float)temp / 10.0;
  290. applog(LOG_DEBUG, "%"PRIpreprv": Temperature: %f", dev->dev_repr, dev->temp);
  291. }
  292. }
  293. else
  294. {
  295. applog(LOG_DEBUG, "%"PRIpreprv": No temperature response", dev->dev_repr);
  296. }
  297. }
  298. mutex_unlock(&dev->device_mutex);
  299. timer_set_delay_from_now(&thr->tv_poll, 250000);
  300. }
  301. //------------------------------------------------------------------------------
  302. static
  303. void twinfury_job_start(struct thr_info *thr)
  304. {
  305. struct cgpu_info *board = thr->cgpu;
  306. struct twinfury_info *info = (struct twinfury_info *)board->device_data;
  307. int timeout = 50;
  308. int device_fd = thr->cgpu->device->device_fd;
  309. if (opt_dev_protocol && opt_debug)
  310. {
  311. char hex[93];
  312. bin2hex(hex, info->tx_buffer, 46);
  313. applog(LOG_DEBUG, "%"PRIpreprv": SEND: %s",
  314. board->proc_repr, hex);
  315. }
  316. mutex_lock(&board->device_mutex);
  317. if (46 != write(device_fd, info->tx_buffer, 46))
  318. {
  319. applog(LOG_ERR, "%"PRIpreprv": Failed writing work task", board->proc_repr);
  320. dev_error(board, REASON_DEV_COMMS_ERROR);
  321. job_start_abort(thr, true);
  322. return;
  323. }
  324. while(timeout > 0)
  325. {
  326. uint8_t buffer[8];
  327. int len;
  328. len = serial_read(device_fd, buffer, 8);
  329. if(len > 0)
  330. break;
  331. timeout--;
  332. }
  333. mutex_unlock(&board->device_mutex);
  334. if(unlikely(timeout == 0))
  335. {
  336. applog(LOG_ERR, "%"PRIpreprv": Timeout.", board->proc_repr);
  337. }
  338. }
  339. //------------------------------------------------------------------------------
  340. static void twinfury_shutdown(struct thr_info *thr)
  341. {
  342. struct cgpu_info *cgpu = thr->cgpu;
  343. mutex_lock(&cgpu->device_mutex);
  344. serial_close(cgpu->device_fd);
  345. mutex_unlock(&cgpu->device_mutex);
  346. }
  347. //------------------------------------------------------------------------------
  348. static bool twinfury_identify(struct cgpu_info *cgpu)
  349. {
  350. char buf[] = "L";
  351. mutex_lock(&cgpu->device_mutex);
  352. if (1 != write(cgpu->device_fd, buf, 1))
  353. return false;
  354. mutex_unlock(&cgpu->device_mutex);
  355. return true;
  356. }
  357. //------------------------------------------------------------------------------
  358. struct device_drv twinfury_drv = {
  359. .dname = "Twinfury",
  360. .name = "TBF",
  361. .drv_detect = twinfury_detect,
  362. .identify_device = twinfury_identify,
  363. .thread_init = twinfury_init,
  364. .minerloop = minerloop_async,
  365. .job_prepare = twinfury_job_prepare,
  366. .job_start = twinfury_job_start,
  367. .poll = twinfury_poll,
  368. .job_process_results = twinfury_job_process_results,
  369. .thread_shutdown = twinfury_shutdown,
  370. };