driver-cointerra.c 11 KB

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