driver-cointerra.c 41 KB

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