driver-drillbit.c 11 KB

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