driver-cointerra.c 11 KB

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