driver-cointerra.c 40 KB

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