driver-cointerra.c 12 KB

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