driver-cointerra.c 41 KB

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