driver-cointerra.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * Copyright 2014 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <math.h>
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "compat.h"
  15. #include "deviceapi.h"
  16. #include "logging.h"
  17. #include "lowlevel.h"
  18. #include "lowl-usb.h"
  19. #include "miner.h"
  20. #include "util.h"
  21. #define COINTERRA_LATEST_RESULT_USECS (10 * 1000000)
  22. #define COINTERRA_DESIRED_ROLL 60
  23. #define COINTERRA_MAX_NONCE_DIFF 0x20
  24. #define COINTERRA_EP_R (LIBUSB_ENDPOINT_IN | 1)
  25. #define COINTERRA_EP_W (LIBUSB_ENDPOINT_OUT | 1)
  26. #define COINTERRA_USB_TIMEOUT 100
  27. #define COINTERRA_USB_POLL_TIMEOUT 1
  28. #define COINTERRA_PACKET_SIZE 0x40
  29. #define COINTERRA_START_SEQ 0x5a,0x5a
  30. #define COINTERRA_MSG_SIZE (COINTERRA_PACKET_SIZE - sizeof(cointerra_startseq))
  31. #define COINTERRA_MSGBODY_SIZE (COINTERRA_MSG_SIZE - 1)
  32. BFG_REGISTER_DRIVER(cointerra_drv)
  33. enum cointerra_msg_type_out {
  34. CMTO_RESET = 1,
  35. CMTO_WORK = 2,
  36. CMTO_REQUEST = 4,
  37. CMTO_HWERR = 5,
  38. CMTO_LEDCTL = 6,
  39. CMTO_HASHRATE = 7,
  40. CMTO_GET_INFO = 0x21,
  41. };
  42. enum cointerra_msg_type_in {
  43. CMTI_WORKREQ = 1,
  44. CMTI_MATCH = 2,
  45. CMTI_WORKDONE = 3,
  46. CMTI_STATUS = 4,
  47. CMTI_SETTINGS = 5,
  48. CMTI_INFO = 6,
  49. CMTI_LOGMSG = 7,
  50. CMTI_RESETDONE = 8,
  51. CMTI_ERRINFO = 0xa,
  52. };
  53. enum cointerra_reset_level {
  54. CRL_WORK_UPDATE = 1,
  55. CRL_NEW_BLOCK = 2,
  56. CRL_INIT = 3,
  57. };
  58. struct cointerra_dev_state {
  59. libusb_device_handle *usbh;
  60. struct lowl_usb_endpoint *ep;
  61. unsigned pipes_per_asic;
  62. unsigned pipes_per_die;
  63. int works_requested;
  64. int next_work_id;
  65. };
  66. static const uint8_t cointerra_startseq[] = {COINTERRA_START_SEQ};
  67. static
  68. bool cointerra_open(const struct lowlevel_device_info * const info, const char * const repr, libusb_device_handle ** const usbh_p, struct lowl_usb_endpoint ** const ep_p)
  69. {
  70. if (libusb_open(info->lowl_data, usbh_p))
  71. applogr(false, LOG_DEBUG, "%s: USB open failed on %s",
  72. repr, info->devid);
  73. *ep_p = usb_open_ep_pair(*usbh_p, COINTERRA_EP_R, 64, COINTERRA_EP_W, 64);
  74. usb_ep_set_timeouts_ms(*ep_p, COINTERRA_USB_TIMEOUT, COINTERRA_USB_TIMEOUT);
  75. if (!*ep_p)
  76. {
  77. applog(LOG_DEBUG, "%s: Endpoint open failed on %s",
  78. repr, info->devid);
  79. libusb_close(*usbh_p);
  80. *usbh_p = NULL;
  81. return false;
  82. }
  83. return true;
  84. }
  85. static
  86. bool cointerra_write_msg(struct lowl_usb_endpoint * const ep, const char * const repr, const uint8_t msgtype, const void * const msgbody)
  87. {
  88. uint8_t buf[COINTERRA_PACKET_SIZE], *p;
  89. memcpy(buf, cointerra_startseq, sizeof(cointerra_startseq));
  90. p = &buf[sizeof(cointerra_startseq)];
  91. pk_u8(p, 0, msgtype);
  92. memcpy(&p[1], msgbody, COINTERRA_MSGBODY_SIZE);
  93. if (usb_write(ep, buf, sizeof(buf)) != sizeof(buf))
  94. return false;
  95. return true;
  96. }
  97. static
  98. bool cointerra_read_msg(uint8_t * const out_msgtype, uint8_t * const out, struct lowl_usb_endpoint * const ep, const char * const repr)
  99. {
  100. uint8_t ss[] = {COINTERRA_START_SEQ};
  101. uint8_t buf[COINTERRA_PACKET_SIZE];
  102. usb_search(ep, ss, sizeof(ss), NULL);
  103. const int xfer = usb_read(ep, buf, sizeof(buf));
  104. if (!xfer)
  105. return false;
  106. if (xfer != sizeof(buf))
  107. applogr(false, LOG_ERR, "%s: Packet size mismatch (actual=%d expected=%d)",
  108. repr, xfer, (int)sizeof(buf));
  109. uint8_t * const bufp = &buf[sizeof(ss)];
  110. *out_msgtype = upk_u8(bufp, 0);
  111. memcpy(out, &bufp[1], COINTERRA_MSGBODY_SIZE);
  112. return true;
  113. }
  114. static
  115. bool cointerra_request(struct lowl_usb_endpoint * const ep, const uint8_t msgtype, uint16_t interval_cs)
  116. {
  117. uint8_t buf[COINTERRA_MSGBODY_SIZE] = {0};
  118. pk_u16le(buf, 0, msgtype);
  119. pk_u16le(buf, 2, interval_cs);
  120. return cointerra_write_msg(ep, cointerra_drv.dname, CMTO_REQUEST, buf);
  121. }
  122. static
  123. bool cointerra_reset(struct lowl_usb_endpoint * const ep, const enum cointerra_reset_level crl)
  124. {
  125. uint8_t buf[COINTERRA_MSGBODY_SIZE] = { crl };
  126. return cointerra_write_msg(ep, cointerra_drv.dname, CMTO_RESET, buf);
  127. }
  128. static
  129. void cointerra_set_queue_full(struct cgpu_info * const dev, const bool nv)
  130. {
  131. if (dev->thr[0]->queue_full == nv)
  132. return;
  133. set_on_all_procs(dev, thr[0]->queue_full, nv);
  134. }
  135. static
  136. bool cointerra_lowl_match(const struct lowlevel_device_info * const info)
  137. {
  138. return lowlevel_match_lowlproduct(info, &lowl_usb, "GoldStrike");
  139. }
  140. static
  141. bool cointerra_lowl_probe(const struct lowlevel_device_info * const info)
  142. {
  143. bool rv = false;
  144. if (info->lowl != &lowl_usb)
  145. applogr(false, LOG_DEBUG, "%s: Matched \"%s\" %s, but lowlevel driver is not usb_generic!",
  146. __func__, info->product, info->devid);
  147. libusb_device_handle *usbh;
  148. struct lowl_usb_endpoint *ep;
  149. if (!cointerra_open(info, cointerra_drv.dname, &usbh, &ep))
  150. return false;
  151. unsigned pipes;
  152. {
  153. {
  154. uint8_t buf[COINTERRA_MSGBODY_SIZE] = {0};
  155. if (!cointerra_write_msg(ep, cointerra_drv.dname, CMTO_GET_INFO, buf))
  156. goto err;
  157. }
  158. uint8_t msgtype;
  159. uint8_t buf[COINTERRA_MSG_SIZE];
  160. while (true)
  161. {
  162. if (!cointerra_read_msg(&msgtype, buf, ep, cointerra_drv.dname))
  163. goto err;
  164. if (msgtype == CMTI_INFO)
  165. break;
  166. // FIXME: Timeout if we keep getting packets we don't care about
  167. }
  168. pipes = upk_u16le(buf, 8);
  169. }
  170. applog(LOG_DEBUG, "%s: Found %u pipes on %s",
  171. __func__, pipes, info->devid);
  172. struct cgpu_info * const cgpu = malloc(sizeof(*cgpu));
  173. *cgpu = (struct cgpu_info){
  174. .drv = &cointerra_drv,
  175. .procs = pipes,
  176. .device_data = lowlevel_ref(info),
  177. .threads = 1,
  178. .device_path = strdup(info->devid),
  179. .dev_manufacturer = maybe_strdup(info->manufacturer),
  180. .dev_product = maybe_strdup(info->product),
  181. .dev_serial = maybe_strdup(info->serial),
  182. .deven = DEV_ENABLED,
  183. };
  184. rv = add_cgpu(cgpu);
  185. err:
  186. usb_close_ep(ep);
  187. libusb_close(usbh);
  188. return rv;
  189. }
  190. static
  191. bool cointerra_init(struct thr_info * const master_thr)
  192. {
  193. struct cgpu_info * const dev = master_thr->cgpu;
  194. struct lowlevel_device_info * const info = dev->device_data;
  195. struct cointerra_dev_state * const devstate = malloc(sizeof(*devstate));
  196. dev->device_data = devstate;
  197. *devstate = (struct cointerra_dev_state){
  198. .pipes_per_die = 120,
  199. .pipes_per_asic = 240,
  200. };
  201. if (!cointerra_open(info, dev->dev_repr, &devstate->usbh, &devstate->ep))
  202. return false;
  203. struct lowl_usb_endpoint * const ep = devstate->ep;
  204. // Request regular status updates
  205. cointerra_request(ep, CMTI_STATUS, 0x83d);
  206. cointerra_reset(ep, CRL_INIT);
  207. // Queue is full until device asks for work
  208. cointerra_set_queue_full(dev, true);
  209. timer_set_delay_from_now(&master_thr->tv_poll, 100000);
  210. return true;
  211. }
  212. static
  213. bool cointerra_queue_append(struct thr_info * const thr, struct work * const work)
  214. {
  215. struct cgpu_info * const dev = thr->cgpu->device;
  216. struct thr_info * const master_thr = dev->thr[0];
  217. struct cointerra_dev_state * const devstate = dev->device_data;
  218. struct timeval tv_now, tv_latest;
  219. uint8_t buf[COINTERRA_MSGBODY_SIZE] = {0};
  220. uint16_t ntimeroll, zerobits;
  221. if (unlikely(!devstate->works_requested))
  222. {
  223. applog(LOG_DEBUG, "%s: Attempt to queue work while none requested; rejecting", dev->dev_repr);
  224. cointerra_set_queue_full(dev, true);
  225. return false;
  226. }
  227. timer_set_now(&tv_now);
  228. timer_set_delay(&tv_latest, &tv_now, COINTERRA_LATEST_RESULT_USECS);
  229. ntimeroll = max(0, work_ntime_range(work, &tv_now, &tv_latest, COINTERRA_DESIRED_ROLL));
  230. work->device_id = devstate->next_work_id;
  231. pk_u16be(buf, 0, work->device_id);
  232. swap32yes(&buf[ 6], work->midstate , 0x20 / 4);
  233. swap32yes(&buf[0x26], &work->data[0x40], 0xc / 4);
  234. pk_u16le(buf, 50, ntimeroll);
  235. // Use the real share difficulty up to COINTERRA_MAX_NONCE_DIFF
  236. if (work->work_difficulty >= COINTERRA_MAX_NONCE_DIFF)
  237. work->nonce_diff = COINTERRA_MAX_NONCE_DIFF;
  238. else
  239. work->nonce_diff = work->work_difficulty;
  240. zerobits = log2(floor(work->nonce_diff));
  241. work->nonce_diff = pow(2, zerobits);
  242. zerobits += 0x20;
  243. pk_u16le(buf, 52, zerobits);
  244. if (!cointerra_write_msg(devstate->ep, cointerra_drv.dname, CMTO_WORK, buf))
  245. return false;
  246. HASH_ADD_INT(master_thr->work, device_id, work);
  247. ++devstate->next_work_id;
  248. if (!--devstate->works_requested)
  249. {
  250. applog(LOG_DEBUG, "%s: Sent all requested works, queue full", dev->dev_repr);
  251. cointerra_set_queue_full(dev, true);
  252. }
  253. return true;
  254. }
  255. static
  256. void cointerra_queue_flush(struct thr_info * const thr)
  257. {
  258. }
  259. static
  260. bool cointerra_poll_msg(struct thr_info * const master_thr)
  261. {
  262. struct cgpu_info * const dev = master_thr->cgpu, *proc;
  263. struct thr_info *mythr;
  264. struct cointerra_dev_state * const devstate = dev->device_data;
  265. uint8_t msgtype;
  266. uint8_t buf[COINTERRA_MSGBODY_SIZE];
  267. if (!cointerra_read_msg(&msgtype, buf, devstate->ep, dev->dev_repr))
  268. return false;
  269. switch (msgtype)
  270. {
  271. case CMTI_WORKREQ:
  272. {
  273. devstate->works_requested = upk_u16le(buf, 0);
  274. const bool qf = !devstate->works_requested;
  275. applog(LOG_DEBUG, "%s: %u works requested",
  276. dev->dev_repr, devstate->works_requested);
  277. cointerra_set_queue_full(dev, qf);
  278. break;
  279. }
  280. case CMTI_MATCH:
  281. {
  282. struct work *work;
  283. const int workid = upk_u16be(buf, 0);
  284. const int die = buf[2], asic = buf[3], pipeno = buf[5];
  285. const unsigned procno = (asic * devstate->pipes_per_asic) + (die * devstate->pipes_per_die) + pipeno;
  286. const uint32_t timeoff = upk_u32le(buf, 42);
  287. const uint16_t zerobits = upk_u16le(buf, 52);
  288. const uint32_t nonce = upk_u32le(buf, 57);
  289. proc = get_proc_by_id(dev, procno) ?: dev;
  290. mythr = proc->thr[0];
  291. HASH_FIND_INT(master_thr->work, &workid, work);
  292. if (unlikely(!work))
  293. {
  294. applog(LOG_WARNING, "%"PRIpreprv": Got %s message about unknown work 0x%x",
  295. proc->proc_repr, "nonce found", workid);
  296. inc_hw_errors3(mythr, NULL, &nonce, 1.);
  297. break;
  298. }
  299. work->nonce_diff = pow(2, zerobits - 0x20);
  300. submit_noffset_nonce(mythr, work, nonce, timeoff);
  301. // hashes_done must be counted by matches because cointerra devices do not provide a way to know which pipe completed matchless work
  302. hashes_done2(mythr, 0x100000000, NULL);
  303. break;
  304. }
  305. case CMTI_WORKDONE:
  306. {
  307. const int workid = upk_u16be(buf, 0);
  308. struct work *work;
  309. HASH_FIND_INT(master_thr->work, &workid, work);
  310. if (unlikely(!work))
  311. {
  312. applog(LOG_WARNING, "%s: Got %s message about unknown work 0x%x",
  313. dev->dev_repr, "work done", workid);
  314. inc_hw_errors_only(master_thr);
  315. break;
  316. }
  317. HASH_DEL(master_thr->work, work);
  318. free_work(work);
  319. break;
  320. }
  321. case CMTI_STATUS:
  322. {
  323. proc = dev;
  324. for (int i = 0; i < 0x10; i += 2)
  325. {
  326. const float celcius = upk_u16le(buf, i) * 0.01;
  327. for (int j = 0; j < devstate->pipes_per_die; ++j)
  328. {
  329. proc->temp = celcius;
  330. proc = proc->next_proc;
  331. if (unlikely(!proc))
  332. goto die_temps_done;
  333. }
  334. }
  335. die_temps_done:
  336. // TODO: ambient temps, fan, voltage, etc
  337. ;
  338. }
  339. case CMTI_SETTINGS:
  340. case CMTI_INFO:
  341. break;
  342. case CMTI_LOGMSG:
  343. applog(LOG_NOTICE, "%s: Devlog: %.*s", dev->dev_repr, (int)COINTERRA_MSGBODY_SIZE, buf);
  344. break;
  345. case CMTI_RESETDONE:
  346. case CMTI_ERRINFO:
  347. break;
  348. }
  349. return true;
  350. }
  351. static
  352. void cointerra_poll(struct thr_info * const master_thr)
  353. {
  354. struct cgpu_info * const dev = master_thr->cgpu;
  355. struct timeval tv_timeout;
  356. timer_set_delay_from_now(&tv_timeout, 10000);
  357. while (true)
  358. {
  359. if (!cointerra_poll_msg(master_thr))
  360. {
  361. applog(LOG_DEBUG, "%s poll: No more messages", dev->dev_repr);
  362. break;
  363. }
  364. if (timer_passed(&tv_timeout, NULL))
  365. {
  366. applog(LOG_DEBUG, "%s poll: 10ms timeout met", dev->dev_repr);
  367. break;
  368. }
  369. }
  370. timer_set_delay_from_now(&master_thr->tv_poll, 100000);
  371. }
  372. struct device_drv cointerra_drv = {
  373. .dname = "cointerra",
  374. .name = "CTA",
  375. .lowl_match = cointerra_lowl_match,
  376. .lowl_probe = cointerra_lowl_probe,
  377. .minerloop = minerloop_queue,
  378. .thread_init = cointerra_init,
  379. .queue_append = cointerra_queue_append,
  380. .queue_flush = cointerra_queue_flush,
  381. .poll = cointerra_poll,
  382. };