driver-cointerra.c 10 KB

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