driver-cointerra.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. /*
  2. * Copyright 2013-2014 Con Kolivas
  3. * Copyright 2014 Luke Dashjr
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. #include "config.h"
  11. #include <string.h>
  12. #include "miner.h"
  13. #include "deviceapi.h"
  14. #include "driver-cointerra.h"
  15. #include "lowlevel.h"
  16. #include "lowl-usb.h"
  17. #include <math.h>
  18. static const unsigned cointerra_desired_roll = 60;
  19. static const unsigned long cointerra_latest_result_usecs = (10 * 1000000);
  20. static const unsigned cointerra_max_nonce_diff = 0x20;
  21. #define COINTERRA_USB_TIMEOUT 500
  22. #define COINTERRA_PACKET_SIZE 0x40
  23. #define COINTERRA_START_SEQ 0x5a,0x5a
  24. #define COINTERRA_MSG_SIZE (COINTERRA_PACKET_SIZE - sizeof(cointerra_startseq))
  25. #define COINTERRA_MSGBODY_SIZE (COINTERRA_MSG_SIZE - 1)
  26. BFG_REGISTER_DRIVER(cointerra_drv)
  27. enum cointerra_msg_type_out {
  28. CMTO_RESET = 1,
  29. CMTO_WORK = 2,
  30. CMTO_REQUEST = 4,
  31. CMTO_HWERR = 5,
  32. CMTO_LEDCTL = 6,
  33. CMTO_HASHRATE = 7,
  34. CMTO_GET_INFO = 0x21,
  35. };
  36. static const uint8_t cointerra_startseq[] = {COINTERRA_START_SEQ};
  37. static const char *cointerra_hdr = "ZZ";
  38. static void cta_gen_message(char *msg, char type)
  39. {
  40. memset(msg, 0, CTA_MSG_SIZE);
  41. memcpy(msg, cointerra_hdr, 2);
  42. msg[CTA_MSG_TYPE] = type;
  43. }
  44. /* Find the number of leading zero bits in diff */
  45. static uint8_t diff_to_bits(double diff)
  46. {
  47. uint64_t diff64;
  48. uint8_t i;
  49. diff *= (double)2147483648.0;
  50. if (diff > 0x8000000000000000ULL)
  51. diff = 0x8000000000000000ULL;
  52. /* Convert it to an integer */
  53. diff64 = diff;
  54. for (i = 0; diff64; i++, diff64 >>= 1);
  55. return i;
  56. }
  57. static double bits_to_diff(uint8_t bits)
  58. {
  59. double ret = 1.0;
  60. if (likely(bits > 32))
  61. ret *= 1ull << (bits - 32);
  62. else if (unlikely(bits < 32))
  63. ret /= 1ull << (32 - bits);
  64. return ret;
  65. }
  66. static bool cta_reset_init(char *buf)
  67. {
  68. return ((buf[CTA_MSG_TYPE] == CTA_RECV_RDONE) && ((buf[CTA_RESET_TYPE]&0x3) == CTA_RESET_INIT));
  69. }
  70. static char *mystrstr(char *haystack, int size, const char *needle)
  71. {
  72. int loop = 0;
  73. while (loop < (size-1)) {
  74. if ((haystack[loop] == needle[0])&&
  75. (haystack[loop+1] == needle[1]))
  76. return &haystack[loop];
  77. loop++;
  78. }
  79. return NULL;
  80. }
  81. static
  82. bool cta_open(struct lowl_usb_endpoint * const ep, const char * const repr, struct cointerra_info * const devstate)
  83. {
  84. int amount, offset = 0;
  85. char buf[CTA_MSG_SIZE];
  86. cgtimer_t ts_start;
  87. bool ret = false;
  88. applog(LOG_INFO, "CTA_OPEN");
  89. cta_gen_message(buf, CTA_SEND_RESET);
  90. // set the initial difficulty
  91. buf[CTA_RESET_TYPE] = CTA_RESET_INIT | CTA_RESET_DIFF;
  92. buf[CTA_RESET_DIFF] = diff_to_bits(CTA_INIT_DIFF);
  93. buf[CTA_RESET_LOAD] = devstate->set_load ?: 255;
  94. buf[CTA_RESET_PSLOAD] = 0;
  95. amount = usb_write(ep, buf, CTA_MSG_SIZE);
  96. if (amount != CTA_MSG_SIZE) {
  97. applog(LOG_INFO, "Write error %s, wrote %d of %d",
  98. bfg_strerror(errno, BST_ERRNO),
  99. amount, CTA_MSG_SIZE);
  100. return ret;
  101. }
  102. cgtimer_time(&ts_start);
  103. /* Read from the device for up to 2 seconds discarding any data that
  104. * doesn't match a reset complete acknowledgement. */
  105. while (42) {
  106. cgtimer_t ts_now, ts_diff;
  107. char *msg;
  108. cgtimer_time(&ts_now);
  109. cgtimer_sub(&ts_now, &ts_start, &ts_diff);
  110. if (cgtimer_to_ms(&ts_diff) > 2000) {
  111. applog(LOG_DEBUG, "%s: Timed out waiting for response to reset init", repr);
  112. break;
  113. }
  114. amount = usb_read(ep, buf + offset, CTA_MSG_SIZE - offset);
  115. if (amount != (CTA_MSG_SIZE - offset) && amount != 0) {
  116. applog(LOG_INFO, "%s: Read error %s, read %d",
  117. repr, bfg_strerror(errno, BST_ERRNO), amount);
  118. break;
  119. }
  120. if (!amount)
  121. continue;
  122. msg = mystrstr(buf, amount, cointerra_hdr);
  123. if (!msg) {
  124. /* Keep the last byte in case it's the first byte of
  125. * the 2 byte header. */
  126. offset = 1;
  127. memmove(buf, buf + amount - 1, offset);
  128. continue;
  129. }
  130. if (msg > buf) {
  131. /* length of message = offset for next usb_read after moving */
  132. offset = CTA_MSG_SIZE - (msg - buf);
  133. memmove(buf, msg, offset);
  134. continue;
  135. }
  136. /* We have a full sized message starting with the header now */
  137. if (cta_reset_init(buf)) {
  138. /* We can't store any other data returned with this
  139. * reset since we have not allocated any memory for
  140. * a cointerra_info structure yet. */
  141. applog(LOG_INFO, "%s: Successful reset init received", repr);
  142. ret = true;
  143. break;
  144. }
  145. }
  146. return ret;
  147. }
  148. static
  149. bool cointerra_open(const struct lowlevel_device_info * const info, const char * const repr, struct libusb_device_handle ** const usbh_p, struct lowl_usb_endpoint ** const ep_p, struct cointerra_info * const devstate)
  150. {
  151. if (libusb_open(info->lowl_data, usbh_p))
  152. applogr(false, LOG_DEBUG, "%s: USB open failed on %s",
  153. cointerra_drv.dname, info->devid);
  154. *ep_p = usb_open_ep_pair(*usbh_p, LIBUSB_ENDPOINT_IN | 1, 64, LIBUSB_ENDPOINT_OUT | 1, 64);
  155. usb_ep_set_timeouts_ms(*ep_p, COINTERRA_USB_TIMEOUT, COINTERRA_USB_TIMEOUT);
  156. if (!*ep_p)
  157. {
  158. applog(LOG_DEBUG, "%s: Endpoint open failed on %s",
  159. cointerra_drv.dname, info->devid);
  160. fail:
  161. libusb_close(*usbh_p);
  162. *usbh_p = NULL;
  163. return false;
  164. }
  165. if (!cta_open(*ep_p, repr, devstate))
  166. {
  167. usb_close_ep(*ep_p);
  168. *ep_p = NULL;
  169. goto fail;
  170. }
  171. return true;
  172. }
  173. static void cta_clear_work(struct cgpu_info *cgpu)
  174. {
  175. struct work *work, *tmp;
  176. wr_lock(&cgpu->qlock);
  177. HASH_ITER(hh, cgpu->queued_work, work, tmp) {
  178. __work_completed(cgpu, work);
  179. free_work(work);
  180. }
  181. wr_unlock(&cgpu->qlock);
  182. }
  183. static void cta_close(struct cgpu_info *cointerra)
  184. {
  185. struct cointerra_info *info = cointerra->device_data;
  186. /* Open does the same reset init followed by response as is required to
  187. * close the device. */
  188. if (!cta_open(info->ep, cointerra->dev_repr, info)) {
  189. applog(LOG_INFO, "%s %d: Reset on close failed", cointerra->drv->name,
  190. cointerra->device_id);
  191. }
  192. mutex_destroy(&info->lock);
  193. mutex_destroy(&info->sendlock);
  194. /* Don't free info here to avoid trying to access dereferenced members
  195. * once a device is unplugged. */
  196. cta_clear_work(cointerra);
  197. }
  198. static void cta_parse_info(struct cgpu_info *, struct cointerra_info *, char *);
  199. static void msg_from_hu16(char *, int, uint16_t);
  200. static
  201. bool cointerra_wait_for_info(struct cointerra_info * const ctainfo, struct lowl_usb_endpoint * const ep)
  202. {
  203. char buf[CTA_MSG_SIZE];
  204. int amount;
  205. cta_gen_message(buf, CTA_SEND_REQUEST);
  206. msg_from_hu16(buf, CTA_REQ_MSGTYPE, CTA_RECV_INFO);
  207. msg_from_hu16(buf, CTA_REQ_INTERVAL, 0);
  208. amount = usb_write(ep, buf, CTA_MSG_SIZE);
  209. if (amount != CTA_MSG_SIZE)
  210. return false;
  211. do {
  212. amount = usb_read(ep, buf, CTA_MSG_SIZE);
  213. if (amount != CTA_MSG_SIZE)
  214. applogr(false, LOG_ERR, "%s: Read error %s, read %d",
  215. __func__, bfg_strerror(errno, BST_ERRNO), amount);
  216. if (memcmp(buf, cointerra_hdr, 2))
  217. applogr(false, LOG_ERR, "%s: Packet header mismatch", __func__);
  218. } while (buf[CTA_MSG_TYPE] != CTA_RECV_INFO);
  219. cta_parse_info(NULL, ctainfo, buf);
  220. return true;
  221. }
  222. static
  223. bool cointerra_lowl_probe(const struct lowlevel_device_info * const info)
  224. {
  225. struct cointerra_info ctainfo = { .set_load = 0, };
  226. struct libusb_device_handle *usbh;
  227. struct lowl_usb_endpoint *ep;
  228. bool b;
  229. if (!cointerra_open(info, cointerra_drv.dname, &usbh, &ep, &ctainfo))
  230. return false;
  231. mutex_init(&ctainfo.lock);
  232. b = cointerra_wait_for_info(&ctainfo, ep);
  233. mutex_destroy(&ctainfo.lock);
  234. usb_close_ep(ep);
  235. libusb_close(usbh);
  236. if (!b)
  237. return false;
  238. applog(LOG_DEBUG, "%s: Found %lu cores on %s",
  239. __func__, (unsigned long)ctainfo.cores, info->devid);
  240. libusb_device * const usbdev = info->lowl_data;
  241. if (bfg_claim_libusb(&cointerra_drv, true, usbdev))
  242. return false;
  243. struct cgpu_info * const dev = malloc(sizeof(*dev));
  244. *dev = (struct cgpu_info){
  245. .drv = &cointerra_drv,
  246. .procs = ctainfo.cores,
  247. .device_data = lowlevel_ref(info),
  248. .threads = 1,
  249. .device_path = strdup(info->devid),
  250. .dev_manufacturer = maybe_strdup(info->manufacturer),
  251. .dev_product = maybe_strdup(info->product),
  252. .dev_serial = maybe_strdup(info->serial),
  253. .deven = DEV_ENABLED,
  254. .min_nonce_diff = CTA_INIT_DIFF,
  255. };
  256. const bool rv = add_cgpu(dev);
  257. applog(LOG_INFO, "%s: Successfully set up %s",
  258. cointerra_drv.dname, dev->dev_repr);
  259. return rv;
  260. }
  261. static
  262. void cointerra_set_queue_full(struct cgpu_info * const dev, const bool nv)
  263. {
  264. if (dev->thr[0]->queue_full == nv)
  265. return;
  266. for_each_managed_proc(proc, dev)
  267. proc->thr[0]->queue_full = nv;
  268. }
  269. static
  270. bool cointerra_lowl_match(const struct lowlevel_device_info * const info)
  271. {
  272. return lowlevel_match_lowlproduct(info, &lowl_usb, "GoldStrike");
  273. }
  274. /* This function will remove a work item from the hashtable if it matches the
  275. * id in work->subid and return a pointer to the work but it will not free the
  276. * work. It may return NULL if it cannot find matching work. */
  277. static struct work *take_work_by_id(struct cgpu_info *cgpu, uint16_t id)
  278. {
  279. struct work *work, *tmp, *ret = NULL;
  280. wr_lock(&cgpu->qlock);
  281. HASH_ITER(hh, cgpu->queued_work, work, tmp) {
  282. if (work->subid == id) {
  283. ret = work;
  284. break;
  285. }
  286. }
  287. if (ret)
  288. __work_completed(cgpu, ret);
  289. wr_unlock(&cgpu->qlock);
  290. return ret;
  291. }
  292. /* This function will look up a work item in the hashtable if it matches the
  293. * id in work->subid and return a cloned work item if it matches. It may return
  294. * NULL if it cannot find matching work. */
  295. static struct work *clone_work_by_id(struct cgpu_info *cgpu, uint16_t id)
  296. {
  297. struct work *work, *tmp, *ret = NULL;
  298. rd_lock(&cgpu->qlock);
  299. HASH_ITER(hh, cgpu->queued_work, work, tmp) {
  300. if (work->subid == id) {
  301. ret = work;
  302. break;
  303. }
  304. }
  305. if (ret)
  306. ret = copy_work(ret);
  307. rd_unlock(&cgpu->qlock);
  308. return ret;
  309. }
  310. static bool cta_send_msg(struct cgpu_info *cointerra, char *buf);
  311. static uint16_t hu16_from_msg(char *buf, int msg)
  312. {
  313. return le16toh(*(uint16_t *)&buf[msg]);
  314. }
  315. static uint32_t hu32_from_msg(char *buf, int msg)
  316. {
  317. return le32toh(*(uint32_t *)&buf[msg]);
  318. }
  319. static uint64_t hu64_from_msg(char *buf, int msg)
  320. {
  321. return le64toh(*(uint64_t *)&buf[msg]);
  322. }
  323. static uint8_t u8_from_msg(char *buf, int msg)
  324. {
  325. return *(uint8_t *)&buf[msg];
  326. }
  327. static void msg_from_hu16(char *buf, int msg, uint16_t val)
  328. {
  329. *(uint16_t *)&buf[msg] = htole16(val);
  330. }
  331. static void cta_parse_reqwork(struct cgpu_info *cointerra, struct cointerra_info *info,
  332. char *buf)
  333. {
  334. uint16_t retwork;
  335. retwork = hu16_from_msg(buf, CTA_REQWORK_REQUESTS);
  336. applog(LOG_DEBUG, "%s %d: Request work message for %u items received",
  337. cointerra->drv->name, cointerra->device_id, retwork);
  338. mutex_lock(&info->lock);
  339. info->requested = retwork;
  340. cointerra_set_queue_full(cointerra, !retwork);
  341. mutex_unlock(&info->lock);
  342. }
  343. static void cta_parse_recvmatch(struct thr_info *thr, struct cgpu_info *cointerra,
  344. struct cointerra_info *info, char *buf)
  345. {
  346. struct cgpu_info *corecgpu;
  347. struct thr_info *corethr;
  348. uint32_t timestamp_offset, mcu_tag;
  349. uint16_t retwork;
  350. struct work *work;
  351. uint8_t asic, core, pipe, coreno;
  352. int pipeno, bitchar, bitbit;
  353. /* No endian switch needs doing here since it's sent and returned as
  354. * the same 4 bytes */
  355. retwork = *(uint16_t *)(&buf[CTA_DRIVER_TAG]);
  356. mcu_tag = hu32_from_msg(buf, CTA_MCU_TAG);
  357. const uint8_t wdiffbits = u8_from_msg(buf, CTA_WORK_DIFFBITS);
  358. const uint32_t nonce = hu32_from_msg(buf, CTA_MATCH_NONCE);
  359. asic = u8_from_msg(buf, CTA_MCU_ASIC);
  360. core = u8_from_msg(buf, CTA_MCU_CORE);
  361. pipe = u8_from_msg(buf, CTA_MCU_PIPE);
  362. pipeno = asic * 512 + core * 128 + pipe;
  363. // For some reason, pipe numbers skip 0x?f
  364. const int bfg_pipeno = ((pipe >> 4) * 0xf) + (pipe & 0xf);
  365. const unsigned procno = (asic * 480) + (core * 120) + bfg_pipeno;
  366. corecgpu = device_proc_by_id(cointerra, procno) ?: cointerra;
  367. corethr = corecgpu->thr[0];
  368. applog(LOG_DEBUG, "%s %d: Match message for id 0x%04x MCU id 0x%08x received",
  369. cointerra->drv->name, cointerra->device_id, retwork, mcu_tag);
  370. work = clone_work_by_id(cointerra, retwork);
  371. if (likely(work)) {
  372. unsigned char rhash[32];
  373. char outhash[16];
  374. double wdiff;
  375. uint64_t hashes;
  376. bool ret;
  377. timestamp_offset = hu32_from_msg(buf, CTA_MATCH_NOFFSET);
  378. if (timestamp_offset) {
  379. struct work *base_work = work;
  380. work = copy_work_noffset(base_work, timestamp_offset);
  381. free_work(base_work);
  382. }
  383. /* Test against the difficulty we asked for along with the work */
  384. wdiff = bits_to_diff(wdiffbits);
  385. hashes = (uint64_t)wdiff * 0x100000000ull;
  386. ret = true; // TODO: test_nonce_diff(work, nonce, wdiff);
  387. if (opt_debug) {
  388. /* Debugging, remove me */
  389. swab256(rhash, work->hash);
  390. bin2hex(outhash, rhash, 8);
  391. applog(LOG_DEBUG, "submit work %s 0x%04x 0x%08x %d 0x%08x",
  392. outhash, retwork, mcu_tag, timestamp_offset, nonce);
  393. }
  394. hashes_done2(corethr, hashes, NULL);
  395. if (likely(ret)) {
  396. coreno = asic * 4 + core;
  397. if (unlikely(asic > 1 || core > 3 || pipe > 127 || pipeno > 1023)) {
  398. applog(LOG_WARNING, "%s %d: MCU invalid pipe asic %d core %d pipe %d",
  399. cointerra->drv->name, cointerra->device_id, asic, core, pipe);
  400. coreno = 0;
  401. } else {
  402. info->last_pipe_nonce[pipeno] = time(NULL);
  403. bitchar = pipeno / 8;
  404. bitbit = pipeno % 8;
  405. info->pipe_bitmap[bitchar] |= 0x80 >> bitbit;
  406. }
  407. applog(LOG_DEBUG, "%"PRIpreprv": Submitting tested work job_id %s work_id %u",
  408. corecgpu->proc_repr, work->job_id, work->subid);
  409. ret = submit_nonce(corethr, work, nonce);
  410. mutex_lock(&info->lock);
  411. info->share_hashes += hashes;
  412. info->tot_core_hashes[coreno] += hashes;
  413. info->hashes += nonce;
  414. mutex_unlock(&info->lock);
  415. } else {
  416. char sendbuf[CTA_MSG_SIZE];
  417. applog(LOG_DEBUG, "%s %d: Notify bad match work",
  418. cointerra->drv->name, cointerra->device_id);
  419. if (opt_debug) {
  420. unsigned char midstate[32], wdata[12];
  421. char hexmidstate[68], hexwdata[28];
  422. uint16_t wid;
  423. memcpy(&wid, &info->work_id, 2);
  424. flip32(midstate, work->midstate);
  425. bin2hex(hexmidstate, midstate, 32);
  426. flip12(wdata, &work->data[64]);
  427. bin2hex(hexwdata, wdata, 12);
  428. applog(LOG_DEBUG, "False match sent: work id %u midstate %s blkhdr %s",
  429. wid, hexmidstate, hexwdata);
  430. applog(LOG_DEBUG, "False match reports: work id 0x%04x MCU id 0x%08x work diff %.1f",
  431. retwork, mcu_tag, wdiff);
  432. applog(LOG_DEBUG, "False match tested: nonce 0x%08x noffset %d %s",
  433. nonce, timestamp_offset, outhash);
  434. }
  435. /* Tell the device we got a false match */
  436. cta_gen_message(sendbuf, CTA_SEND_FMATCH);
  437. memcpy(sendbuf + 3, buf, CTA_MSG_SIZE - 3);
  438. cta_send_msg(cointerra, sendbuf);
  439. }
  440. free_work(work);
  441. } else {
  442. applog(LOG_INFO, "%s %d: Matching work id 0x%X %d not found", cointerra->drv->name,
  443. cointerra->device_id, retwork, __LINE__);
  444. inc_hw_errors3(corethr, NULL, &nonce, bits_to_diff(wdiffbits));
  445. mutex_lock(&info->lock);
  446. info->no_matching_work++;
  447. mutex_unlock(&info->lock);
  448. }
  449. }
  450. static void cta_parse_wdone(struct thr_info *thr, struct cgpu_info *cointerra,
  451. struct cointerra_info *info, char *buf)
  452. {
  453. uint16_t retwork = *(uint16_t *)(&buf[CTA_DRIVER_TAG]);
  454. struct work *work = take_work_by_id(cointerra, retwork);
  455. uint64_t hashes;
  456. if (likely(work))
  457. free_work(work);
  458. else {
  459. applog(LOG_INFO, "%s %d: Done work not found id 0x%X %d",
  460. cointerra->drv->name, cointerra->device_id, retwork, __LINE__);
  461. inc_hw_errors_only(thr);
  462. }
  463. /* Removing hashes from work done message */
  464. hashes = hu64_from_msg(buf, CTA_WDONE_NONCES);
  465. if (unlikely(hashes > (61 * 0x100000000ull))) {
  466. applog(LOG_INFO, "%s Invalid hash returned %"PRIu64"x %"PRIu64"x %"PRIu64"X",
  467. __func__, info->hashes, hashes, hashes);
  468. hashes = 0;
  469. }
  470. mutex_lock(&info->lock);
  471. info->hashes += hashes;
  472. mutex_unlock(&info->lock);
  473. }
  474. static void u16array_from_msg(uint16_t *u16, int entries, int var, char *buf)
  475. {
  476. int i, j;
  477. for (i = 0, j = 0; i < entries; i++, j += sizeof(uint16_t))
  478. u16[i] = hu16_from_msg(buf, var + j);
  479. }
  480. static void cta_parse_statread(struct cgpu_info *cointerra, struct cointerra_info *info,
  481. char *buf)
  482. {
  483. int i;
  484. mutex_lock(&info->lock);
  485. u16array_from_msg(info->coretemp, CTA_CORES, CTA_STAT_CORETEMPS, buf);
  486. info->ambtemp_low = hu16_from_msg(buf, CTA_STAT_AMBTEMP_LOW);
  487. info->ambtemp_avg = hu16_from_msg(buf, CTA_STAT_AMBTEMP_AVG);
  488. info->ambtemp_high = hu16_from_msg(buf, CTA_STAT_AMBTEMP_HIGH);
  489. u16array_from_msg(info->pump_tachs, CTA_PUMPS, CTA_STAT_PUMP_TACHS, buf);
  490. u16array_from_msg(info->fan_tachs, CTA_FANS, CTA_STAT_FAN_TACHS, buf);
  491. u16array_from_msg(info->corevolts, CTA_CORES, CTA_STAT_CORE_VOLTS, buf);
  492. info->volts33 = hu16_from_msg(buf, CTA_STAT_VOLTS33);
  493. info->volts12 = hu16_from_msg(buf, CTA_STAT_VOLTS12);
  494. info->inactive = hu16_from_msg(buf, CTA_STAT_INACTIVE);
  495. info->active = hu16_from_msg(buf, CTA_STAT_ACTIVE);
  496. mutex_unlock(&info->lock);
  497. struct cgpu_info *proc = cointerra;
  498. for (i = 0; i < CTA_CORES; i++) {
  499. float temp = info->coretemp[i] / 100.;
  500. for (int j = 0; j < 120; (++j), (proc = proc->next_proc))
  501. proc->temp = temp;
  502. }
  503. }
  504. static void u8array_from_msg(uint8_t *u8, int entries, int var, char *buf)
  505. {
  506. int i;
  507. for (i = 0; i < entries; i++)
  508. u8[i] = u8_from_msg(buf, var + i);
  509. }
  510. static void cta_parse_statset(struct cointerra_info *info, char *buf)
  511. {
  512. mutex_lock(&info->lock);
  513. u8array_from_msg(info->coreperf, CTA_CORES, CTA_STAT_PERFMODE, buf);
  514. u8array_from_msg(info->fanspeed, CTA_FANS, CTA_STAT_FANSPEEDS, buf);
  515. info->dies_active = u8_from_msg(buf, CTA_STAT_DIES_ACTIVE);
  516. u8array_from_msg(info->pipes_enabled, CTA_CORES, CTA_STAT_PIPES_ENABLED, buf);
  517. u16array_from_msg(info->corefreqs, CTA_CORES, CTA_STAT_CORE_FREQS, buf);
  518. info->uptime = hu32_from_msg(buf,CTA_STAT_UPTIME);
  519. mutex_unlock(&info->lock);
  520. }
  521. static void cta_parse_info(struct cgpu_info *cointerra, struct cointerra_info *info,
  522. char *buf)
  523. {
  524. mutex_lock(&info->lock);
  525. info->hwrev = hu64_from_msg(buf, CTA_INFO_HWREV);
  526. info->serial = hu32_from_msg(buf, CTA_INFO_SERNO);
  527. info->asics = u8_from_msg(buf, CTA_INFO_NUMASICS);
  528. info->dies = u8_from_msg(buf, CTA_INFO_NUMDIES);
  529. info->cores = hu16_from_msg(buf, CTA_INFO_NUMCORES);
  530. info->board_number = u8_from_msg(buf, CTA_INFO_BOARDNUMBER);
  531. info->fwrev[0] = u8_from_msg(buf, CTA_INFO_FWREV_MAJ);
  532. info->fwrev[1] = u8_from_msg(buf, CTA_INFO_FWREV_MIN);
  533. info->fwrev[2] = u8_from_msg(buf, CTA_INFO_FWREV_MIC);
  534. info->fw_year = hu16_from_msg(buf, CTA_INFO_FWDATE_YEAR);
  535. info->fw_month = u8_from_msg(buf, CTA_INFO_FWDATE_MONTH);
  536. info->fw_day = u8_from_msg(buf, CTA_INFO_FWDATE_DAY);
  537. info->init_diffbits = u8_from_msg(buf, CTA_INFO_INITDIFFBITS);
  538. info->min_diffbits = u8_from_msg(buf, CTA_INFO_MINDIFFBITS);
  539. info->max_diffbits = u8_from_msg(buf, CTA_INFO_MAXDIFFBITS);
  540. mutex_unlock(&info->lock);
  541. #if 0
  542. if (!cointerra->unique_id) {
  543. uint32_t b32 = htobe32(info->serial);
  544. cointerra->unique_id = malloc((4 * 2) + 1);
  545. bin2hex(cointerra->unique_id, &b32, 4);
  546. }
  547. #endif
  548. }
  549. static void cta_parse_rdone(struct cgpu_info *cointerra, struct cointerra_info *info,
  550. char *buf)
  551. {
  552. uint8_t reset_type, diffbits;
  553. uint64_t wdone;
  554. reset_type = buf[CTA_RESET_TYPE];
  555. diffbits = buf[CTA_RESET_DIFF];
  556. wdone = hu64_from_msg(buf, CTA_WDONE_NONCES);
  557. applog(LOG_INFO, "%s %d: Reset done type %u message %u diffbits %"PRIu64" done received",
  558. cointerra->drv->name, cointerra->device_id, reset_type, diffbits, wdone);
  559. if (wdone) {
  560. applog(LOG_INFO, "%s %d: Reset done type %u message %u diffbits %"PRIu64" done received",
  561. cointerra->drv->name, cointerra->device_id, reset_type, diffbits, wdone);
  562. mutex_lock(&info->lock);
  563. info->hashes += wdone;
  564. mutex_unlock(&info->lock);
  565. }
  566. /* Note that the cgsem that is posted here must not be waited on while
  567. * holding the info->lock to not get into a livelock since this
  568. * function also grabs the lock first and it's always best to not sleep
  569. * while holding a lock. */
  570. if (reset_type == CTA_RESET_NEW) {
  571. cta_clear_work(cointerra);
  572. /* Tell reset sender that the reset is complete
  573. * and it may resume. */
  574. notifier_wake(info->reset_notifier);
  575. }
  576. }
  577. static void cta_zero_stats(struct cgpu_info *cointerra);
  578. static void cta_parse_debug(struct cointerra_info *info, char *buf)
  579. {
  580. mutex_lock(&info->lock);
  581. info->tot_underruns = hu16_from_msg(buf, CTA_STAT_UNDERRUNS);
  582. u16array_from_msg(info->tot_hw_errors, CTA_CORES, CTA_STAT_HW_ERRORS, buf);
  583. info->tot_hashes = hu64_from_msg(buf, CTA_STAT_HASHES);
  584. info->tot_flushed_hashes = hu64_from_msg(buf, CTA_STAT_FLUSHED_HASHES);
  585. info->autovoltage = u8_from_msg(buf, CTA_STAT_AUTOVOLTAGE);
  586. info->current_ps_percent = u8_from_msg(buf, CTA_STAT_POWER_PERCENT);
  587. info->power_used = hu16_from_msg(buf,CTA_STAT_POWER_USED);
  588. info->power_voltage = hu16_from_msg(buf,CTA_STAT_VOLTAGE);
  589. info->ipower_used = hu16_from_msg(buf,CTA_STAT_IPOWER_USED);
  590. info->ipower_voltage = hu16_from_msg(buf,CTA_STAT_IVOLTAGE);
  591. info->power_temps[0] = hu16_from_msg(buf,CTA_STAT_PS_TEMP1);
  592. info->power_temps[1] = hu16_from_msg(buf,CTA_STAT_PS_TEMP2);
  593. mutex_unlock(&info->lock);
  594. #if 0
  595. /* Autovoltage is positive only once at startup and eventually drops
  596. * to zero. After that time we reset the stats since they're unreliable
  597. * till then. */
  598. if (unlikely(!info->autovoltage_complete && !info->autovoltage)) {
  599. struct cgpu_info *cointerra = info->thr->cgpu;
  600. info->autovoltage_complete = true;
  601. cgtime(&cointerra->dev_start_tv);
  602. cta_zero_stats(cointerra);
  603. cointerra->total_mhashes = 0;
  604. cointerra->accepted = 0;
  605. cointerra->rejected = 0;
  606. cointerra->hw_errors = 0;
  607. cointerra->utility = 0.0;
  608. cointerra->last_share_pool_time = 0;
  609. cointerra->diff1 = 0;
  610. cointerra->diff_accepted = 0;
  611. cointerra->diff_rejected = 0;
  612. cointerra->last_share_diff = 0;
  613. }
  614. #endif
  615. }
  616. static void cta_parse_msg(struct thr_info *thr, struct cgpu_info *cointerra,
  617. struct cointerra_info *info, char *buf)
  618. {
  619. switch (buf[CTA_MSG_TYPE]) {
  620. default:
  621. case CTA_RECV_UNUSED:
  622. applog(LOG_INFO, "%s %d: Unidentified message type %u",
  623. cointerra->drv->name, cointerra->device_id, buf[CTA_MSG_TYPE]);
  624. break;
  625. case CTA_RECV_REQWORK:
  626. cta_parse_reqwork(cointerra, info, buf);
  627. break;
  628. case CTA_RECV_MATCH:
  629. cta_parse_recvmatch(thr, cointerra, info, buf);
  630. break;
  631. case CTA_RECV_WDONE:
  632. applog(LOG_DEBUG, "%s %d: Work done message received",
  633. cointerra->drv->name, cointerra->device_id);
  634. cta_parse_wdone(thr, cointerra, info, buf);
  635. break;
  636. case CTA_RECV_STATREAD:
  637. applog(LOG_DEBUG, "%s %d: Status readings message received",
  638. cointerra->drv->name, cointerra->device_id);
  639. cta_parse_statread(cointerra, info, buf);
  640. break;
  641. case CTA_RECV_STATSET:
  642. applog(LOG_DEBUG, "%s %d: Status settings message received",
  643. cointerra->drv->name, cointerra->device_id);
  644. cta_parse_statset(info, buf);
  645. break;
  646. case CTA_RECV_INFO:
  647. applog(LOG_DEBUG, "%s %d: Info message received",
  648. cointerra->drv->name, cointerra->device_id);
  649. cta_parse_info(cointerra, info, buf);
  650. break;
  651. case CTA_RECV_MSG:
  652. applog(LOG_NOTICE, "%s %d: MSG: %s",
  653. cointerra->drv->name, cointerra->device_id, &buf[CTA_MSG_RECVD]);
  654. break;
  655. case CTA_RECV_RDONE:
  656. cta_parse_rdone(cointerra, info, buf);
  657. break;
  658. case CTA_RECV_STATDEBUG:
  659. cta_parse_debug(info, buf);
  660. break;
  661. }
  662. }
  663. static
  664. void cta_recv_thread(void *arg)
  665. {
  666. struct thr_info *thr = (struct thr_info *)arg;
  667. struct cgpu_info *cointerra = thr->cgpu;
  668. struct cointerra_info *info = cointerra->device_data;
  669. int offset = 0;
  670. while (likely(!cointerra->shutdown)) {
  671. char buf[CTA_READBUF_SIZE];
  672. int amount;
  673. if (unlikely(0))
  674. {
  675. applog(LOG_DEBUG, "%s %d: Device disappeared, disabling recv thread",
  676. cointerra->drv->name, cointerra->device_id);
  677. break;
  678. }
  679. amount = usb_read(info->ep, buf + offset, CTA_MSG_SIZE);
  680. if (amount != CTA_MSG_SIZE && amount != 0) {
  681. applog(LOG_ERR, "%s: Read error %s, read %d",
  682. cointerra->dev_repr, bfg_strerror(errno, BST_ERRNO), amount);
  683. break;
  684. }
  685. offset += amount;
  686. while (offset >= CTA_MSG_SIZE) {
  687. char *msg = mystrstr(buf, offset, cointerra_hdr);
  688. int begin;
  689. if (unlikely(!msg)) {
  690. applog(LOG_WARNING, "%s %d: No message header found, discarding buffer",
  691. cointerra->drv->name, cointerra->device_id);
  692. inc_hw_errors_only(thr);
  693. /* Save the last byte in case it's the fist
  694. * byte of a header. */
  695. begin = CTA_MSG_SIZE - 1;
  696. offset -= begin;
  697. memmove(buf, buf + begin, offset);
  698. continue;
  699. }
  700. if (unlikely(msg != buf)) {
  701. begin = msg - buf;
  702. applog(LOG_WARNING, "%s %d: Reads out of sync, discarding %d bytes",
  703. cointerra->drv->name, cointerra->device_id, begin);
  704. inc_hw_errors_only(thr);
  705. offset -= begin;
  706. memmove(buf, msg, offset);
  707. if (offset < CTA_MSG_SIZE)
  708. break;
  709. }
  710. /* We have enough buffer for a full message, parse now */
  711. cta_parse_msg(thr, cointerra, info, msg);
  712. offset -= CTA_MSG_SIZE;
  713. if (offset > 0)
  714. memmove(buf, buf + CTA_MSG_SIZE, offset);
  715. }
  716. break;
  717. }
  718. }
  719. static
  720. bool cointerra_write_msg(struct lowl_usb_endpoint * const ep, const char * const repr, const uint8_t msgtype, const void * const msgbody)
  721. {
  722. uint8_t buf[COINTERRA_PACKET_SIZE], *p;
  723. memcpy(buf, cointerra_startseq, sizeof(cointerra_startseq));
  724. p = &buf[sizeof(cointerra_startseq)];
  725. pk_u8(p, 0, msgtype);
  726. memcpy(&p[1], msgbody, COINTERRA_MSGBODY_SIZE);
  727. if (usb_write(ep, buf, sizeof(buf)) != sizeof(buf))
  728. return false;
  729. return true;
  730. }
  731. static bool cta_send_msg(struct cgpu_info *cointerra, char *buf)
  732. {
  733. struct cointerra_info *info = cointerra->device_data;
  734. int amount;
  735. /* Serialise usb writes to prevent overlap in case multiple threads
  736. * send messages */
  737. mutex_lock(&info->sendlock);
  738. amount = usb_write(info->ep, buf, CTA_MSG_SIZE);
  739. mutex_unlock(&info->sendlock);
  740. if (unlikely(amount != CTA_MSG_SIZE)) {
  741. applog(LOG_ERR, "%s: Write error %s, wrote %d of %d",
  742. cointerra->dev_repr, bfg_strerror(errno, BST_ERRNO), amount, CTA_MSG_SIZE);
  743. return false;
  744. }
  745. return true;
  746. }
  747. static bool cta_prepare(struct thr_info *thr)
  748. {
  749. struct cgpu_info *cointerra = thr->cgpu;
  750. struct lowlevel_device_info * const llinfo = cointerra->device_data;
  751. struct cointerra_info *info = calloc(sizeof(struct cointerra_info), 1);
  752. char buf[CTA_MSG_SIZE];
  753. sleep(1);
  754. if (unlikely(!info))
  755. quit(1, "Failed to calloc info in cta_detect_one");
  756. for_each_managed_proc(proc, cointerra)
  757. proc->device_data = info;
  758. /* Nominally set a requested value when starting, preempting the need
  759. * for a req-work message. */
  760. info->requested = CTA_MAX_QUEUE;
  761. cointerra_set_queue_full(cointerra, false);
  762. bool open_rv = cointerra_open(llinfo, cointerra->dev_repr, &info->usbh, &info->ep, info);
  763. lowlevel_devinfo_free(llinfo);
  764. if (!open_rv)
  765. return false;
  766. info->thr = thr;
  767. mutex_init(&info->lock);
  768. mutex_init(&info->sendlock);
  769. notifier_init(info->reset_notifier);
  770. /* Request a single status setting message */
  771. cta_gen_message(buf, CTA_SEND_REQUEST);
  772. msg_from_hu16(buf, CTA_REQ_MSGTYPE, CTA_RECV_STATSET);
  773. msg_from_hu16(buf, CTA_REQ_INTERVAL, 0);
  774. if (!cta_send_msg(cointerra, buf))
  775. return false;
  776. /* Request status debug messages every 60 seconds */
  777. cta_gen_message(buf, CTA_SEND_REQUEST);
  778. msg_from_hu16(buf, CTA_REQ_MSGTYPE, CTA_RECV_STATDEBUG);
  779. msg_from_hu16(buf, CTA_REQ_INTERVAL, 6000);
  780. if (!cta_send_msg(cointerra, buf))
  781. return false;
  782. cgtime(&info->core_hash_start);
  783. timer_set_now(&thr->tv_poll);
  784. return true;
  785. }
  786. static void cta_send_reset(struct cgpu_info *cointerra, struct cointerra_info *info,
  787. uint8_t reset_type, uint8_t diffbits);
  788. static void cta_flush_work(struct cgpu_info *cointerra);
  789. static
  790. bool cointerra_queue_append(struct thr_info * const thr, struct work * const work)
  791. {
  792. struct cgpu_info * const dev = thr->cgpu->device;
  793. // struct thr_info * const master_thr = dev->thr[0];
  794. struct cointerra_info * const devstate = dev->device_data;
  795. struct timeval tv_now, tv_latest;
  796. uint8_t buf[COINTERRA_MSGBODY_SIZE] = {0};
  797. uint16_t ntimeroll, zerobits;
  798. if (unlikely(!devstate->requested))
  799. {
  800. applog(LOG_DEBUG, "%s: Attempt to queue work while none requested; rejecting", dev->dev_repr);
  801. cointerra_set_queue_full(dev, true);
  802. return false;
  803. }
  804. timer_set_now(&tv_now);
  805. timer_set_delay(&tv_latest, &tv_now, cointerra_latest_result_usecs);
  806. ntimeroll = max(0, work_ntime_range(work, &tv_now, &tv_latest, cointerra_desired_roll));
  807. if (unlikely(!devstate->work_id))
  808. ++devstate->work_id;
  809. work->device_id = devstate->work_id;
  810. pk_u16be(buf, 0, work->device_id);
  811. work->subid = htobe16(work->device_id);
  812. swap32yes(&buf[ 6], work->midstate , 0x20 / 4);
  813. swap32yes(&buf[0x26], &work->data[0x40], 0xc / 4);
  814. pk_u16le(buf, 50, ntimeroll);
  815. // Use the real share difficulty up to cointerra_max_nonce_diff
  816. if (work->work_difficulty >= cointerra_max_nonce_diff)
  817. work->nonce_diff = cointerra_max_nonce_diff;
  818. else
  819. work->nonce_diff = work->work_difficulty;
  820. zerobits = log2(floor(work->nonce_diff));
  821. work->nonce_diff = pow(2, zerobits);
  822. zerobits += 0x20;
  823. pk_u16le(buf, 52, zerobits);
  824. if (!cointerra_write_msg(devstate->ep, cointerra_drv.dname, CMTO_WORK, buf))
  825. return false;
  826. // HASH_ADD_INT(master_thr->work, device_id, work);
  827. {
  828. ++dev->queued_count;
  829. timer_set_now(&work->tv_work_start);
  830. HASH_ADD_INT(dev->queued_work, id, work);
  831. }
  832. ++devstate->work_id;
  833. if (!--devstate->requested)
  834. {
  835. applog(LOG_DEBUG, "%s: Sent all requested works, queue full", dev->dev_repr);
  836. cointerra_set_queue_full(dev, true);
  837. }
  838. return true;
  839. }
  840. static
  841. void cointerra_queue_flush(struct thr_info * const thr)
  842. {
  843. }
  844. static void cta_send_reset(struct cgpu_info *cointerra, struct cointerra_info *info,
  845. uint8_t reset_type, uint8_t diffbits)
  846. {
  847. char buf[CTA_MSG_SIZE];
  848. int ret, retries = 0;
  849. /* Clear any accumulated messages in case we've gotten out of sync. */
  850. notifier_reset(info->reset_notifier);
  851. resend:
  852. cta_gen_message(buf, CTA_SEND_RESET);
  853. buf[CTA_RESET_TYPE] = reset_type;
  854. buf[CTA_RESET_LOAD] = info->set_load ?: 255;
  855. buf[CTA_RESET_PSLOAD] = 0;
  856. applog(LOG_INFO, "%s %d: Sending Reset type %u with diffbits %u", cointerra->drv->name,
  857. cointerra->device_id, reset_type, diffbits);
  858. cta_send_msg(cointerra, buf);
  859. /* Wait for read thread to parse a reset message and signal us we may
  860. * return to submitting other messages. Use a timeout in case we have
  861. * a problem and the reset done message never returns. */
  862. if (reset_type == CTA_RESET_NEW) {
  863. ret = notifier_wait_us(info->reset_notifier, CTA_RESET_TIMEOUT * 1000);
  864. if (ret) {
  865. if (++retries < 3) {
  866. applog(LOG_INFO, "%s %d: Timed out waiting for reset done msg, retrying",
  867. cointerra->drv->name, cointerra->device_id);
  868. goto resend;
  869. }
  870. applog(LOG_WARNING, "%s %d: Timed out waiting for reset done msg",
  871. cointerra->drv->name, cointerra->device_id);
  872. }
  873. /* Good place to flush any work we have */
  874. flush_queue(cointerra);
  875. }
  876. }
  877. static void cta_update_work(struct cgpu_info *);
  878. static void cta_flush_work(struct cgpu_info *cointerra)
  879. {
  880. struct cointerra_info *info = cointerra->device_data;
  881. if (1)
  882. cta_update_work(cointerra);
  883. else
  884. {
  885. applog(LOG_INFO, "%s %d: cta_flush_work %d", cointerra->drv->name, cointerra->device_id,
  886. __LINE__);
  887. cta_send_reset(cointerra, info, CTA_RESET_NEW, 0);
  888. }
  889. info->thr->work_restart = false;
  890. }
  891. static void cta_update_work(struct cgpu_info *cointerra)
  892. {
  893. struct cointerra_info *info = cointerra->device_data;
  894. applog(LOG_INFO, "%s %d: Update work", cointerra->drv->name, cointerra->device_id);
  895. cta_send_reset(cointerra, info, CTA_RESET_UPDATE, 0);
  896. }
  897. static void cta_zero_corehashes(struct cointerra_info *info)
  898. {
  899. int i;
  900. for (i = 0; i < CTA_CORES; i++)
  901. info->tot_core_hashes[i] = 0;
  902. cgtime(&info->core_hash_start);
  903. }
  904. /* Send per core hashrate calculations at regular intervals ~every 5 minutes */
  905. static void cta_send_corehashes(struct cgpu_info *cointerra, struct cointerra_info *info,
  906. double corehash_time)
  907. {
  908. uint16_t core_ghs[CTA_CORES];
  909. double k[CTA_CORES];
  910. char buf[CTA_MSG_SIZE];
  911. int i, offset;
  912. for (i = 0; i < CTA_CORES; i++) {
  913. k[i] = (double)info->tot_core_hashes[i] / ((double)32 * (double)0x100000000ull);
  914. k[i] = sqrt(k[i]) + 1;
  915. k[i] *= k[i];
  916. k[i] = k[i] * 32 * ((double)0x100000000ull / (double)1000000000) / corehash_time;
  917. core_ghs[i] = k[i];
  918. }
  919. cta_gen_message(buf, CTA_SEND_COREHASHRATE);
  920. offset = CTA_CORE_HASHRATES;
  921. for (i = 0; i < CTA_CORES; i++) {
  922. msg_from_hu16(buf, offset, core_ghs[i]);
  923. offset += 2; // uint16_t
  924. }
  925. cta_send_msg(cointerra, buf);
  926. }
  927. static void cta_scanwork(struct thr_info *thr)
  928. {
  929. struct cgpu_info *cointerra = thr->cgpu;
  930. struct cointerra_info *info = cointerra->device_data;
  931. double corehash_time;
  932. struct timeval now;
  933. cgtime(&now);
  934. if (unlikely(thr->work_restart)) {
  935. applog(LOG_INFO, "%s %d: Flush work line %d",
  936. cointerra->drv->name, cointerra->device_id,__LINE__);
  937. cta_flush_work(cointerra);
  938. } else {
  939. time_t now_t;
  940. int i;
  941. /* Discard work that was started more than 5 minutes ago as
  942. * a safety precaution backup in case the hardware failed to
  943. * return a work done message for some work items. */
  944. age_queued_work(cointerra, 300.0);
  945. /* Each core should be 1.7MH so at max diff of 32 should
  946. * average a share every ~80 seconds.Use this opportunity to
  947. * unset the bits in any pipes that have not returned a valid
  948. * nonce for over 30 full nonce ranges or 2400s. */
  949. now_t = time(NULL);
  950. for (i = 0; i < 1024; i++) {
  951. if (unlikely(now_t > info->last_pipe_nonce[i] + 2400)) {
  952. int bitchar = i / 8, bitbit = i % 8;
  953. info->pipe_bitmap[bitchar] &= ~(0x80 >> bitbit);
  954. }
  955. }
  956. /* Sleep for up to 0.5 seconds, waking if we need work or
  957. * have received a restart message. */
  958. cta_recv_thread(thr);
  959. if (thr->work_restart) {
  960. applog(LOG_INFO, "%s %d: Flush work line %d",
  961. cointerra->drv->name, cointerra->device_id,__LINE__);
  962. cta_flush_work(cointerra);
  963. }
  964. }
  965. corehash_time = tdiff(&now, &info->core_hash_start);
  966. if (corehash_time > 300) {
  967. cta_send_corehashes(cointerra, info, corehash_time);
  968. cta_zero_corehashes(info);
  969. }
  970. mutex_lock(&info->lock);
  971. info->tot_share_hashes += info->share_hashes;
  972. info->tot_calc_hashes += info->hashes;
  973. info->hashes = info->share_hashes = 0;
  974. mutex_unlock(&info->lock);
  975. }
  976. static void cta_shutdown(struct thr_info *thr)
  977. {
  978. struct cgpu_info *cointerra = thr->cgpu;
  979. cta_close(cointerra);
  980. }
  981. static void cta_zero_stats(struct cgpu_info *cointerra)
  982. {
  983. struct cointerra_info *info = cointerra->device_data;
  984. info->tot_calc_hashes = 0;
  985. info->tot_reset_hashes = info->tot_hashes;
  986. info->tot_share_hashes = 0;
  987. cta_zero_corehashes(info);
  988. }
  989. static int bits_set(char v)
  990. {
  991. int c;
  992. for (c = 0; v; c++)
  993. v &= v - 1;
  994. return c;
  995. }
  996. static struct api_data *cta_api_stats(struct cgpu_info *cgpu)
  997. {
  998. struct api_data *root = NULL;
  999. struct cointerra_info *info = cgpu->device_data;
  1000. double dev_runtime = cgpu_runtime(cgpu);
  1001. int i, asic, core, coreno = 0;
  1002. struct timeval now;
  1003. char bitmaphex[36];
  1004. uint64_t ghs, val;
  1005. char buf[64];
  1006. asic = cgpu->proc_id / 480;
  1007. int coreabs = cgpu->proc_id / 120;
  1008. core = coreabs % 4;
  1009. /* Info data */
  1010. root = api_add_uint16(root, "HW Revision", &info->hwrev, false);
  1011. root = api_add_uint32(root, "Serial", &info->serial, false);
  1012. root = api_add_uint8(root, "Asics", &info->asics, false);
  1013. root = api_add_uint8(root, "Dies", &info->dies, false);
  1014. root = api_add_uint16(root, "Cores", &info->cores, false);
  1015. root = api_add_uint8(root, "Board number", &info->board_number, false);
  1016. sprintf(buf, "%u.%u.%u", info->fwrev[0], info->fwrev[1], info->fwrev[2]);
  1017. root = api_add_string(root, "FW Revision", buf, true);
  1018. sprintf(buf, "%04u-%02u-%02u", info->fw_year, info->fw_month, info->fw_day);
  1019. root = api_add_string(root, "FW Date", buf, true);
  1020. root = api_add_uint8(root, "Init diffbits", &info->init_diffbits, false);
  1021. root = api_add_uint8(root, "Min diffbits", &info->min_diffbits, false);
  1022. root = api_add_uint8(root, "Max diffbits", &info->max_diffbits, false);
  1023. /* Status readings */
  1024. {
  1025. i = coreabs;
  1026. sprintf(buf, "CoreTemp%d", i);
  1027. root = api_add_int16(root, buf, &info->coretemp[i], false);
  1028. }
  1029. root = api_add_int16(root, "Ambient Low", &info->ambtemp_low, false);
  1030. root = api_add_int16(root, "Ambient Avg", &info->ambtemp_avg, false);
  1031. root = api_add_int16(root, "Ambient High", &info->ambtemp_high, false);
  1032. for (i = 0; i < CTA_PUMPS; i++) {
  1033. sprintf(buf, "PumpRPM%d", i);
  1034. root = api_add_uint16(root, buf, &info->pump_tachs[i], false);
  1035. }
  1036. for (i = 0; i < CTA_FANS; i++) {
  1037. sprintf(buf, "FanRPM%d", i);
  1038. root = api_add_uint16(root, buf, &info->fan_tachs[i], false);
  1039. }
  1040. {
  1041. i = coreabs;
  1042. sprintf(buf, "CoreFreqs%d", i);
  1043. root = api_add_uint16(root, buf, &info->corefreqs[i], false);
  1044. }
  1045. {
  1046. i = coreabs;
  1047. sprintf(buf, "CoreVolts%d", i);
  1048. root = api_add_uint16(root, buf, &info->corevolts[i], false);
  1049. }
  1050. root = api_add_uint16(root, "Volts3.3", &info->volts33, false);
  1051. root = api_add_uint16(root, "Volts12", &info->volts12, false);
  1052. root = api_add_uint16(root, "Inactive", &info->inactive, false);
  1053. root = api_add_uint16(root, "Active", &info->active, false);
  1054. /* Status settings */
  1055. {
  1056. i = coreabs;
  1057. sprintf(buf, "CorePerfMode%d", i);
  1058. root = api_add_uint8(root, buf, &info->coreperf[i], false);
  1059. }
  1060. for (i = 0; i < CTA_FANS; i++) {
  1061. sprintf(buf, "FanSpeed%d", i);
  1062. root = api_add_uint8(root, buf, &info->fanspeed[i], false);
  1063. }
  1064. root = api_add_uint8(root, "DiesActive", &info->dies_active, false);
  1065. {
  1066. i = coreabs;
  1067. sprintf(buf, "PipesEnabled%d", i);
  1068. root = api_add_uint8(root, buf, &info->pipes_enabled[i], false);
  1069. }
  1070. /* Status debug */
  1071. root = api_add_int(root, "Underruns", &info->tot_underruns, false);
  1072. {
  1073. i = coreabs;
  1074. sprintf(buf, "HWErrors%d", i);
  1075. root = api_add_uint16(root, buf, &info->tot_hw_errors[i], false);
  1076. }
  1077. ghs = info->tot_calc_hashes / dev_runtime;
  1078. root = api_add_uint64(root, "Calc hashrate", &ghs, true);
  1079. ghs = (info->tot_hashes - info->tot_reset_hashes) / dev_runtime;
  1080. root = api_add_uint64(root, "Hashrate", &ghs, true);
  1081. ghs = info->tot_share_hashes / dev_runtime;
  1082. root = api_add_uint64(root, "Share hashrate", &ghs, true);
  1083. root = api_add_uint64(root, "Total calc hashes", &info->tot_calc_hashes, false);
  1084. ghs = info->tot_hashes - info->tot_reset_hashes;
  1085. root = api_add_uint64(root, "Total hashes", &ghs, true);
  1086. root = api_add_uint64(root, "Total raw hashes", &info->tot_hashes, false);
  1087. root = api_add_uint64(root, "Total share hashes", &info->tot_share_hashes, false);
  1088. root = api_add_uint64(root, "Total flushed hashes", &info->tot_flushed_hashes, false);
  1089. val = cgpu->diff_accepted * 0x100000000ull;
  1090. root = api_add_uint64(root, "Accepted hashes", &val, true);
  1091. ghs = val / dev_runtime;
  1092. root = api_add_uint64(root, "Accepted hashrate", &ghs, true);
  1093. val = cgpu->diff_rejected * 0x100000000ull;
  1094. root = api_add_uint64(root, "Rejected hashes", &val, true);
  1095. ghs = val / dev_runtime;
  1096. root = api_add_uint64(root, "Rejected hashrate", &ghs, true);
  1097. cgtime(&now);
  1098. dev_runtime = tdiff(&now, &info->core_hash_start);
  1099. if (dev_runtime < 1)
  1100. dev_runtime = 1;
  1101. {
  1102. i = coreabs;
  1103. sprintf(buf, "Core%d hashrate", i);
  1104. ghs = info->tot_core_hashes[i] / dev_runtime;
  1105. root = api_add_uint64(root, buf, &ghs, true);
  1106. }
  1107. root = api_add_uint32(root, "Uptime",&info->uptime,false);
  1108. {
  1109. char bitmapcount[40], asiccore[12];
  1110. int count = 0;
  1111. sprintf(asiccore, "Asic%dCore%d", asic, core);
  1112. bin2hex(bitmaphex, &info->pipe_bitmap[coreno], 16);
  1113. for (i = coreno; i < coreno + 16; i++)
  1114. count += bits_set(info->pipe_bitmap[i]);
  1115. snprintf(bitmapcount, 40, "%d:%s", count, bitmaphex);
  1116. root = api_add_string(root, asiccore, bitmapcount, true);
  1117. coreno += 16;
  1118. }
  1119. root = api_add_uint8(root, "AV", &info->autovoltage, false);
  1120. root = api_add_uint8(root, "Power Supply Percent", &info->current_ps_percent, false);
  1121. root = api_add_uint16(root, "Power Used", &info->power_used, false);
  1122. root = api_add_uint16(root, "IOUT", &info->power_used, false);
  1123. root = api_add_uint16(root, "VOUT", &info->power_voltage, false);
  1124. root = api_add_uint16(root, "IIN", &info->ipower_used, false);
  1125. root = api_add_uint16(root, "VIN", &info->ipower_voltage, false);
  1126. root = api_add_uint16(root, "PSTemp1", &info->power_temps[0], false);
  1127. root = api_add_uint16(root, "PSTemp2", &info->power_temps[1], false);
  1128. return root;
  1129. }
  1130. struct device_drv cointerra_drv = {
  1131. .dname = "cointerra",
  1132. .name = "CTA",
  1133. .lowl_match = cointerra_lowl_match,
  1134. .lowl_probe = cointerra_lowl_probe,
  1135. .thread_init = cta_prepare,
  1136. .minerloop = minerloop_queue,
  1137. .queue_append = cointerra_queue_append,
  1138. .queue_flush = cointerra_queue_flush,
  1139. // TODO .update_work = cta_update_work,
  1140. .poll = cta_scanwork,
  1141. .get_api_stats = cta_api_stats,
  1142. .thread_shutdown = cta_shutdown,
  1143. .zero_stats = cta_zero_stats,
  1144. };