driver-drillbit.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Copyright 2013 Luke Dashjr
  3. * Copyright 2013 Angus Gratton
  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. #include <stdbool.h>
  11. #include <stdint.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "deviceapi.h"
  15. #include "logging.h"
  16. #include "lowlevel.h"
  17. #include "lowl-vcom.h"
  18. BFG_REGISTER_DRIVER(drillbit_drv)
  19. #define DRILLBIT_MIN_VERSION 2
  20. #define DRILLBIT_MAX_VERSION 3
  21. #define DRILLBIT_MAX_WORK_RESULTS 0x400
  22. #define DRILLBIT_MAX_RESULT_NONCES 0x10
  23. enum drillbit_capability {
  24. DBC_TEMP = 1,
  25. DBC_EXT_CLOCK = 2,
  26. };
  27. enum drillbit_voltagecfg {
  28. DBV_650mV = 0,
  29. DBV_750mV = 2,
  30. DBV_850mV = 1,
  31. DBV_950mV = 3,
  32. };
  33. struct drillbit_board {
  34. enum drillbit_voltagecfg core_voltage_cfg;
  35. unsigned clock_level;
  36. bool clock_div2;
  37. bool use_ext_clock;
  38. unsigned ext_clock_freq;
  39. };
  40. static
  41. bool drillbit_lowl_match(const struct lowlevel_device_info * const info)
  42. {
  43. if (!lowlevel_match_id(info, &lowl_vcom, 0, 0))
  44. return false;
  45. return (info->manufacturer && strstr(info->manufacturer, "Drillbit"));
  46. }
  47. static
  48. bool drillbit_detect_one(const char * const devpath)
  49. {
  50. uint8_t buf[0x10];
  51. const int fd = serial_open(devpath, 0, 1, true);
  52. if (fd == -1)
  53. applogr(false, LOG_DEBUG, "%s: %s: Failed to open", __func__, devpath);
  54. if (1 != write(fd, "I", 1))
  55. {
  56. applog(LOG_DEBUG, "%s: %s: Error writing 'I'", __func__, devpath);
  57. err:
  58. serial_close(fd);
  59. return false;
  60. }
  61. if (sizeof(buf) != serial_read(fd, buf, sizeof(buf)))
  62. {
  63. applog(LOG_DEBUG, "%s: %s: Short read in response to 'I'",
  64. __func__, devpath);
  65. goto err;
  66. }
  67. serial_close(fd);
  68. const unsigned protover = buf[0];
  69. const unsigned long serialno = (uint32_t)buf[9] | ((uint32_t)buf[0xa] << 8) | ((uint32_t)buf[0xb] << 16) | ((uint32_t)buf[0xc] << 24);
  70. char * const product = (void*)&buf[1];
  71. buf[9] = '\0'; // Ensure it is null-terminated (clobbers serial, but we already parsed it)
  72. unsigned chips = buf[0xd];
  73. uint16_t caps = (uint16_t)buf[0xe] | ((uint16_t)buf[0xf] << 8);
  74. if (!product[0])
  75. applogr(false, LOG_DEBUG, "%s: %s: Null product name", __func__, devpath);
  76. if (!serialno)
  77. applogr(false, LOG_DEBUG, "%s: %s: Serial number is zero", __func__, devpath);
  78. if (!chips)
  79. applogr(false, LOG_DEBUG, "%s: %s: No chips found", __func__, devpath);
  80. int loglev = LOG_WARNING;
  81. if (!strcmp(product, "DRILLBIT"))
  82. {
  83. // Hack: first production firmwares all described themselves as DRILLBIT, so fill in the gaps
  84. if (chips == 1)
  85. strcpy(product, "Thumb");
  86. else
  87. strcpy(product, "Eight");
  88. }
  89. else
  90. if (chips == 8 && !strcmp(product, "Eight"))
  91. {} // Known device
  92. else
  93. if (chips == 1 && !strcmp(product, "Thumb"))
  94. {} // Known device
  95. else
  96. loglev = LOG_DEBUG;
  97. if (protover < DRILLBIT_MIN_VERSION || (loglev == LOG_DEBUG && protover > DRILLBIT_MAX_VERSION))
  98. applogr(false, loglev, "%s: %s: Unknown device protocol version %u.",
  99. __func__, devpath, protover);
  100. if (protover > DRILLBIT_MAX_VERSION)
  101. applogr(false, loglev, "%s: %s: Device firmware uses newer Drillbit protocol %u. We only support up to %u. Find a newer BFGMiner!",
  102. __func__, devpath, protover, (unsigned)DRILLBIT_MAX_VERSION);
  103. if (protover == 2 && chips == 1)
  104. // Production firmware Thumbs don't set any capability bits, so fill in the EXT_CLOCK one
  105. caps |= DBC_EXT_CLOCK;
  106. char *serno = malloc(9);
  107. snprintf(serno, 9, "%08lx", serialno);
  108. if (chips > 0x100)
  109. {
  110. applog(LOG_WARNING, "%s: %s: %u chips reported, but driver only supports up to 256",
  111. __func__, devpath, chips);
  112. chips = 0x100;
  113. }
  114. struct cgpu_info * const cgpu = malloc(sizeof(*cgpu));
  115. *cgpu = (struct cgpu_info){
  116. .drv = &drillbit_drv,
  117. .device_path = strdup(devpath),
  118. .dev_product = strdup(product),
  119. .dev_serial = serno,
  120. .deven = DEV_ENABLED,
  121. .procs = chips,
  122. .threads = 1,
  123. //.device_data = ,
  124. };
  125. return add_cgpu(cgpu);
  126. }
  127. static
  128. bool drillbit_lowl_probe(const struct lowlevel_device_info * const info)
  129. {
  130. return vcom_lowl_probe_wrapper(info, drillbit_detect_one);
  131. }
  132. static
  133. bool drillbit_check_response(const char * const repr, const int fd, struct cgpu_info * const dev, const char expect)
  134. {
  135. uint8_t ack;
  136. if (1 != serial_read(fd, &ack, 1))
  137. applogr(false, LOG_ERR, "%s: Short read in response to '%c'",
  138. repr, expect);
  139. if (ack != expect)
  140. applogr(false, LOG_ERR, "%s: Wrong response to '%c': %u",
  141. dev->dev_repr, expect, (unsigned)ack);
  142. return true;
  143. }
  144. static
  145. bool drillbit_reset(struct cgpu_info * const dev)
  146. {
  147. const int fd = dev->device_fd;
  148. if (unlikely(fd == -1))
  149. return false;
  150. if (1 != write(fd, "R", 1))
  151. applogr(false, LOG_ERR, "%s: Error writing reset command", dev->dev_repr);
  152. return drillbit_check_response(dev->dev_repr, fd, dev, 'R');
  153. }
  154. static
  155. bool drillbit_send_config(struct cgpu_info * const dev)
  156. {
  157. const int fd = dev->device_fd;
  158. if (unlikely(fd == -1))
  159. return false;
  160. const struct drillbit_board * const board = dev->device_data;
  161. const uint8_t buf[7] = {'C', board->core_voltage_cfg, board->clock_level, (board->clock_div2 ? 1 : 0), (board->use_ext_clock ? 1 : 0), board->ext_clock_freq};
  162. if (sizeof(buf) != write(fd, buf, sizeof(buf)))
  163. applogr(false, LOG_ERR, "%s: Error sending config", dev->dev_repr);
  164. return drillbit_check_response(dev->dev_repr, fd, dev, 'C');
  165. }
  166. static
  167. bool drillbit_init(struct thr_info * const master_thr)
  168. {
  169. struct cgpu_info * const dev = master_thr->cgpu;
  170. const int fd = serial_open(dev->device_path, 0, 10, true);
  171. if (fd == -1)
  172. return false;
  173. struct drillbit_board * const board = malloc(sizeof(*board));
  174. dev->device_fd = fd;
  175. dev->device_data = board;
  176. *board = (struct drillbit_board){
  177. .core_voltage_cfg = DBV_850mV,
  178. .clock_level = 40,
  179. .clock_div2 = false,
  180. .use_ext_clock = false,
  181. .ext_clock_freq = 200,
  182. };
  183. if (!(drillbit_reset(dev) && drillbit_send_config(dev)))
  184. {
  185. serial_close(fd);
  186. return false;
  187. }
  188. timer_set_delay_from_now(&master_thr->tv_poll, 10000);
  189. return true;
  190. }
  191. static
  192. bool drillbit_job_prepare(struct thr_info * const thr, struct work * const work, __maybe_unused const uint64_t max_nonce)
  193. {
  194. struct cgpu_info * const proc = thr->cgpu;
  195. const int chipid = proc->proc_id;
  196. struct cgpu_info * const dev = proc->device;
  197. const int fd = dev->device_fd;
  198. uint8_t buf[0x31];
  199. buf[0] = 'W';
  200. buf[1] = chipid;
  201. buf[2] = 0; // high bits of chipid
  202. memcpy(&buf[3], work->midstate, 0x20);
  203. memcpy(&buf[0x23], &work->data[0x40], 0xc);
  204. if (sizeof(buf) != write(fd, buf, sizeof(buf)))
  205. applogr(false, LOG_ERR, "%"PRIpreprv": Error sending work %d",
  206. proc->proc_repr, work->id);
  207. if (!drillbit_check_response(proc->proc_repr, fd, dev, 'W'))
  208. applogr(false, LOG_ERR, "%"PRIpreprv": Error queuing work %d",
  209. proc->proc_repr, work->id);
  210. applog(LOG_DEBUG, "%"PRIpreprv": Queued work %d",
  211. proc->proc_repr, work->id);
  212. work->blk.nonce = 0xffffffff;
  213. return true;
  214. }
  215. static
  216. void drillbit_first_job_start(struct thr_info __maybe_unused * const thr)
  217. {
  218. struct cgpu_info * const proc = thr->cgpu;
  219. if (unlikely(!thr->work))
  220. {
  221. applog(LOG_DEBUG, "%"PRIpreprv": No current work, assuming immediate start",
  222. proc->proc_repr);
  223. mt_job_transition(thr);
  224. job_start_complete(thr);
  225. timer_set_now(&thr->tv_morework);
  226. }
  227. }
  228. static
  229. int64_t drillbit_job_process_results(struct thr_info *thr, struct work *work, bool stopping)
  230. {
  231. return 0xbd000000;
  232. }
  233. static
  234. struct cgpu_info *drillbit_find_proc(struct cgpu_info * const dev, int chipid)
  235. {
  236. struct cgpu_info *proc = dev;
  237. for (int i = 0; i < chipid; ++i)
  238. {
  239. proc = proc->next_proc;
  240. if (unlikely(!proc))
  241. return NULL;
  242. }
  243. return proc;
  244. }
  245. static
  246. bool bitfury_fudge_nonce2(struct work * const work, uint32_t * const nonce_p)
  247. {
  248. if (!work)
  249. return false;
  250. const uint32_t m7 = *((uint32_t *)&work->data[64]);
  251. const uint32_t ntime = *((uint32_t *)&work->data[68]);
  252. const uint32_t nbits = *((uint32_t *)&work->data[72]);
  253. return bitfury_fudge_nonce(work->midstate, m7, ntime, nbits, nonce_p);
  254. }
  255. static
  256. bool drillbit_get_work_results(struct cgpu_info * const dev)
  257. {
  258. const int fd = dev->device_fd;
  259. if (fd == -1)
  260. return false;
  261. uint8_t buf[4 + (4 * DRILLBIT_MAX_RESULT_NONCES)];
  262. uint32_t total;
  263. int i, j;
  264. if (1 != write(fd, "E", 1))
  265. applogr(false, LOG_ERR, "%s: Error sending request for work results", dev->dev_repr);
  266. if (sizeof(total) != serial_read(fd, &total, sizeof(total)))
  267. applogr(false, LOG_ERR, "%s: Short read in response to 'E'", dev->dev_repr);
  268. total = le32toh(total);
  269. if (total > DRILLBIT_MAX_WORK_RESULTS)
  270. applogr(false, LOG_ERR, "%s: Impossible number of total work: %lu",
  271. dev->dev_repr, (unsigned long)total);
  272. for (i = 0; i < total; ++i)
  273. {
  274. if (sizeof(buf) != serial_read(fd, buf, sizeof(buf)))
  275. applogr(false, LOG_ERR, "%s: Short read on %dth total work",
  276. dev->dev_repr, i);
  277. const int chipid = buf[0];
  278. struct cgpu_info * const proc = drillbit_find_proc(dev, chipid);
  279. struct thr_info * const thr = proc->thr[0];
  280. if (unlikely(!proc))
  281. {
  282. applog(LOG_ERR, "%s: Unknown chip id %d", dev->dev_repr, chipid);
  283. continue;
  284. }
  285. const bool is_idle = buf[3];
  286. int nonces = buf[2];
  287. if (nonces > DRILLBIT_MAX_RESULT_NONCES)
  288. {
  289. applog(LOG_ERR, "%"PRIpreprv": More than %d nonces claimed, impossible",
  290. proc->proc_repr, (int)DRILLBIT_MAX_RESULT_NONCES);
  291. nonces = DRILLBIT_MAX_RESULT_NONCES;
  292. }
  293. applog(LOG_DEBUG, "%"PRIpreprv": Handling completion of %d nonces. is_idle=%d work=%p next_work=%p",
  294. proc->proc_repr, nonces, is_idle, thr->work, thr->next_work);
  295. const uint32_t *nonce_p = (void*)&buf[4];
  296. for (j = 0; j < nonces; ++j, ++nonce_p)
  297. {
  298. uint32_t nonce = bitfury_decnonce(*nonce_p);
  299. if (bitfury_fudge_nonce2(thr->work, &nonce))
  300. submit_nonce(thr, thr->work, nonce);
  301. else
  302. if (bitfury_fudge_nonce2(thr->next_work, &nonce))
  303. {
  304. applog(LOG_DEBUG, "%"PRIpreprv": Result for next work, transitioning",
  305. proc->proc_repr);
  306. submit_nonce(thr, thr->next_work, nonce);
  307. mt_job_transition(thr);
  308. job_start_complete(thr);
  309. }
  310. else
  311. if (bitfury_fudge_nonce2(thr->prev_work, &nonce))
  312. {
  313. applog(LOG_DEBUG, "%"PRIpreprv": Result for PREVIOUS work",
  314. proc->proc_repr);
  315. submit_nonce(thr, thr->prev_work, nonce);
  316. }
  317. else
  318. inc_hw_errors(thr, thr->work, nonce);
  319. }
  320. if (is_idle && thr->next_work)
  321. {
  322. applog(LOG_DEBUG, "%"PRIpreprv": Chip went idle without any results for next work",
  323. proc->proc_repr);
  324. mt_job_transition(thr);
  325. job_start_complete(thr);
  326. }
  327. if (!thr->next_work)
  328. timer_set_now(&thr->tv_morework);
  329. }
  330. return true;
  331. }
  332. static
  333. void drillbit_poll(struct thr_info * const master_thr)
  334. {
  335. struct cgpu_info * const dev = master_thr->cgpu;
  336. drillbit_get_work_results(dev);
  337. timer_set_delay_from_now(&master_thr->tv_poll, 10000);
  338. }
  339. static
  340. bool drillbit_get_stats(struct cgpu_info * const dev)
  341. {
  342. if (dev != dev->device)
  343. return true;
  344. const int fd = dev->device_fd;
  345. if (fd == -1)
  346. return false;
  347. if (1 != write(fd, "T", 1))
  348. applogr(false, LOG_ERR, "%s: Error requesting temperature", dev->dev_repr);
  349. uint8_t buf[2];
  350. if (sizeof(buf) != serial_read(fd, buf, sizeof(buf)))
  351. applogr(false, LOG_ERR, "%s: Short read in response to 'T'", dev->dev_repr);
  352. float temp = ((uint16_t)buf[0]) | ((uint16_t)buf[1] << 8);
  353. temp /= 10.;
  354. for (struct cgpu_info *proc = dev; proc; proc = proc->next_proc)
  355. proc->temp = temp;
  356. return true;
  357. }
  358. struct device_drv drillbit_drv = {
  359. .dname = "drillbit",
  360. .name = "DRB",
  361. .lowl_match = drillbit_lowl_match,
  362. .lowl_probe = drillbit_lowl_probe,
  363. .thread_init = drillbit_init,
  364. .minerloop = minerloop_async,
  365. .job_prepare = drillbit_job_prepare,
  366. .job_start = drillbit_first_job_start,
  367. .job_process_results = drillbit_job_process_results,
  368. .poll = drillbit_poll,
  369. .get_stats = drillbit_get_stats,
  370. };