driver-klondike.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  1. /*
  2. * Copyright 2013 Andrew Smith
  3. * Copyright 2013 Con Kolivas
  4. * Copyright 2013 Chris Savery
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include <float.h>
  12. #include <limits.h>
  13. #include <pthread.h>
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <strings.h>
  17. #include <sys/time.h>
  18. #include <unistd.h>
  19. #include <math.h>
  20. #include "config.h"
  21. #ifdef WIN32
  22. #include <windows.h>
  23. #endif
  24. #include "compat.h"
  25. #include "deviceapi.h"
  26. #include "lowlevel.h"
  27. #include "miner.h"
  28. #define K1 "K1"
  29. #define K16 "K16"
  30. #define K64 "K64"
  31. #define MIDSTATE_BYTES 32
  32. #define MERKLE_OFFSET 64
  33. #define MERKLE_BYTES 12
  34. #define REPLY_SIZE 15 // adequate for all types of replies
  35. #define MAX_KLINES 1024 // unhandled reply limit
  36. #define REPLY_WAIT_TIME 100 // poll interval for a cmd waiting it's reply
  37. #define CMD_REPLY_RETRIES 8 // how many retries for cmds
  38. #define MAX_WORK_COUNT 4 // for now, must be binary multiple and match firmware
  39. #define TACH_FACTOR 87890 // fan rpm divisor
  40. BFG_REGISTER_DRIVER(klondike_drv)
  41. typedef struct klondike_header {
  42. uint8_t cmd;
  43. uint8_t dev;
  44. uint8_t buf[REPLY_SIZE-2];
  45. } HEADER;
  46. #define K_2(_bytes) ((int)(_bytes[0]) + \
  47. ((int)(_bytes[1]) << 8))
  48. #define K_4(_bytes) ((uint64_t)(_bytes[0]) + \
  49. ((uint64_t)(_bytes[1]) << 8) + \
  50. ((uint64_t)(_bytes[2]) << 16) + \
  51. ((uint64_t)(_bytes[3]) << 24))
  52. #define K_SERIAL(_serial) K_4(_serial)
  53. #define K_HASHCOUNT(_hashcount) K_2(_hashcount)
  54. #define K_MAXCOUNT(_maxcount) K_2(_maxcount)
  55. #define K_NONCE(_nonce) K_4(_nonce)
  56. #define K_HASHCLOCK(_hashclock) K_2(_hashclock)
  57. #define SET_HASHCLOCK(_hashclock, _value) do { \
  58. (_hashclock)[0] = (uint8_t)((_value) & 0xff); \
  59. (_hashclock)[1] = (uint8_t)(((_value) >> 8) & 0xff); \
  60. } while(0)
  61. #define KSENDHD(_add) (sizeof(char) + sizeof(uint8_t) + _add)
  62. typedef struct klondike_id {
  63. uint8_t cmd;
  64. uint8_t dev;
  65. uint8_t version;
  66. uint8_t product[7];
  67. uint8_t serial[4];
  68. } IDENTITY;
  69. typedef struct klondike_status {
  70. uint8_t cmd;
  71. uint8_t dev;
  72. uint8_t state;
  73. uint8_t chipcount;
  74. uint8_t slavecount;
  75. uint8_t workqc;
  76. uint8_t workid;
  77. uint8_t temp;
  78. uint8_t fanspeed;
  79. uint8_t errorcount;
  80. uint8_t hashcount[2];
  81. uint8_t maxcount[2];
  82. uint8_t noise;
  83. } WORKSTATUS;
  84. typedef struct _worktask {
  85. uint8_t cmd;
  86. uint8_t dev;
  87. uint8_t workid;
  88. uint8_t midstate[32];
  89. uint8_t merkle[12];
  90. } WORKTASK;
  91. typedef struct _workresult {
  92. uint8_t cmd;
  93. uint8_t dev;
  94. uint8_t workid;
  95. uint8_t nonce[4];
  96. } WORKRESULT;
  97. typedef struct klondike_cfg {
  98. uint8_t cmd;
  99. uint8_t dev;
  100. uint8_t hashclock[2];
  101. uint8_t temptarget;
  102. uint8_t tempcritical;
  103. uint8_t fantarget;
  104. uint8_t pad2;
  105. } WORKCFG;
  106. typedef struct kline {
  107. union {
  108. HEADER hd;
  109. IDENTITY id;
  110. WORKSTATUS ws;
  111. WORKTASK wt;
  112. WORKRESULT wr;
  113. WORKCFG cfg;
  114. };
  115. } KLINE;
  116. typedef struct device_info {
  117. uint32_t noncecount;
  118. uint32_t nextworkid;
  119. uint16_t lasthashcount;
  120. uint64_t totalhashcount;
  121. uint32_t rangesize;
  122. uint32_t *chipstats;
  123. } DEVINFO;
  124. typedef struct klist {
  125. struct klist *prev;
  126. struct klist *next;
  127. KLINE kline;
  128. struct timeval tv_when;
  129. int block_seq;
  130. bool ready;
  131. bool working;
  132. } KLIST;
  133. struct klondike_info {
  134. bool shutdown;
  135. pthread_rwlock_t stat_lock;
  136. struct thr_info replies_thr;
  137. cglock_t klist_lock;
  138. KLIST *used;
  139. KLIST *free;
  140. int kline_count;
  141. int used_count;
  142. int block_seq;
  143. KLIST *status;
  144. DEVINFO *devinfo;
  145. KLIST *cfg;
  146. int noncecount;
  147. uint64_t hashcount;
  148. uint64_t errorcount;
  149. uint64_t noisecount;
  150. // us Delay from USB reply to being processed
  151. double delay_count;
  152. double delay_total;
  153. double delay_min;
  154. double delay_max;
  155. struct timeval tv_last_nonce_received;
  156. // Time from recieving one nonce to the next
  157. double nonce_count;
  158. double nonce_total;
  159. double nonce_min;
  160. double nonce_max;
  161. struct libusb_device_handle *usbdev_handle;
  162. // TODO:
  163. bool usbinfo_nodev;
  164. };
  165. static KLIST *new_klist_set(struct cgpu_info *klncgpu)
  166. {
  167. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  168. KLIST *klist = NULL;
  169. int i;
  170. klist = calloc(MAX_KLINES, sizeof(*klist));
  171. if (!klist)
  172. quit(1, "Failed to calloc klist - when old count=%d", klninfo->kline_count);
  173. klninfo->kline_count += MAX_KLINES;
  174. klist[0].prev = NULL;
  175. klist[0].next = &(klist[1]);
  176. for (i = 1; i < MAX_KLINES-1; i++) {
  177. klist[i].prev = &klist[i-1];
  178. klist[i].next = &klist[i+1];
  179. }
  180. klist[MAX_KLINES-1].prev = &(klist[MAX_KLINES-2]);
  181. klist[MAX_KLINES-1].next = NULL;
  182. return klist;
  183. }
  184. static KLIST *allocate_kitem(struct cgpu_info *klncgpu)
  185. {
  186. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  187. KLIST *kitem = NULL;
  188. int ran_out = 0;
  189. char errbuf[1024];
  190. cg_wlock(&klninfo->klist_lock);
  191. if (klninfo->free == NULL) {
  192. ran_out = klninfo->kline_count;
  193. klninfo->free = new_klist_set(klncgpu);
  194. snprintf(errbuf, sizeof(errbuf),
  195. "%s%i: KLINE count exceeded %d, now %d",
  196. klncgpu->drv->name, klncgpu->device_id,
  197. ran_out, klninfo->kline_count);
  198. }
  199. kitem = klninfo->free;
  200. klninfo->free = klninfo->free->next;
  201. if (klninfo->free)
  202. klninfo->free->prev = NULL;
  203. kitem->next = klninfo->used;
  204. kitem->prev = NULL;
  205. if (kitem->next)
  206. kitem->next->prev = kitem;
  207. klninfo->used = kitem;
  208. kitem->ready = false;
  209. kitem->working = false;
  210. memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
  211. klninfo->used_count++;
  212. cg_wunlock(&klninfo->klist_lock);
  213. if (ran_out > 0)
  214. applog(LOG_ERR, "%s", errbuf);
  215. return kitem;
  216. }
  217. static void release_kitem(struct cgpu_info *klncgpu, KLIST *kitem)
  218. {
  219. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  220. cg_wlock(&klninfo->klist_lock);
  221. if (kitem == klninfo->used)
  222. klninfo->used = kitem->next;
  223. if (kitem->next)
  224. kitem->next->prev = kitem->prev;
  225. if (kitem->prev)
  226. kitem->prev->next = kitem->next;
  227. kitem->next = klninfo->free;
  228. if (klninfo->free)
  229. klninfo->free->prev = kitem;
  230. kitem->prev = NULL;
  231. klninfo->free = kitem;
  232. klninfo->used_count--;
  233. cg_wunlock(&klninfo->klist_lock);
  234. }
  235. static
  236. int usb_init(struct cgpu_info * const klncgpu, struct libusb_device * const dev)
  237. {
  238. struct klondike_info * const klninfo = klncgpu->device_data;
  239. int e;
  240. if (libusb_open(dev, &klninfo->usbdev_handle) != LIBUSB_SUCCESS)
  241. return 0;
  242. if (LIBUSB_SUCCESS != (e = libusb_set_configuration(klninfo->usbdev_handle, 1)))
  243. {
  244. applog(LOG_DEBUG, "%s: Failed to set configuration 1: %s",
  245. klondike_drv.dname, bfg_strerror(e, BST_LIBUSB));
  246. fail:
  247. libusb_close(klninfo->usbdev_handle);
  248. return 0;
  249. }
  250. if (LIBUSB_SUCCESS != (e = libusb_claim_interface(klninfo->usbdev_handle, 0)))
  251. {
  252. applog(LOG_DEBUG, "%s: Failed to claim interface 0: %s",
  253. klondike_drv.dname, bfg_strerror(e, BST_LIBUSB));
  254. goto fail;
  255. }
  256. return 1;
  257. }
  258. static
  259. int _usb_rw(struct cgpu_info * const klncgpu, void * const buf, const size_t bufsiz, int * const processed, int ep)
  260. {
  261. struct klondike_info * const klninfo = klncgpu->device_data;
  262. const unsigned int timeout = 999;
  263. unsigned char *cbuf = buf;
  264. int err, sent;
  265. *processed = 0;
  266. while (*processed < bufsiz)
  267. {
  268. err = libusb_bulk_transfer(klninfo->usbdev_handle, ep, cbuf, bufsiz, &sent, timeout);
  269. if (unlikely(err))
  270. return err;
  271. *processed += sent;
  272. }
  273. return LIBUSB_SUCCESS;
  274. }
  275. #define usb_read( klncgpu, buf, bufsiz, processed) _usb_rw(klncgpu, buf, bufsiz, processed, 1 | LIBUSB_ENDPOINT_IN)
  276. #define usb_write(klncgpu, buf, bufsiz, processed) _usb_rw(klncgpu, buf, bufsiz, processed, 1 | LIBUSB_ENDPOINT_OUT)
  277. static
  278. void usb_uninit(struct cgpu_info * const klncgpu)
  279. {
  280. struct klondike_info * const klninfo = klncgpu->device_data;
  281. libusb_release_interface(klninfo->usbdev_handle, 0);
  282. libusb_close(klninfo->usbdev_handle);
  283. }
  284. static double cvtKlnToC(uint8_t temp)
  285. {
  286. double Rt, stein, celsius;
  287. if (temp == 0)
  288. return 0.0;
  289. Rt = 1000.0 * 255.0 / (double)temp - 1000.0;
  290. stein = log(Rt / 2200.0) / 3987.0;
  291. stein += 1.0 / (double)(25.0 + 273.15);
  292. celsius = (1.0 / stein) - 273.15;
  293. // For display of bad data
  294. if (celsius < 0.0)
  295. celsius = 0.0;
  296. if (celsius > 200.0)
  297. celsius = 200.0;
  298. return celsius;
  299. }
  300. static int cvtCToKln(double deg)
  301. {
  302. double Rt, stein, temp;
  303. if (deg < 0.0)
  304. deg = 0.0;
  305. stein = 1.0 / (deg + 273.15);
  306. stein -= 1.0 / (double)(25.0 + 273.15);
  307. Rt = exp(stein * 3987.0) * 2200.0;
  308. if (Rt == -1000.0)
  309. Rt++;
  310. temp = 1000.0 * 256.0 / (Rt + 1000.0);
  311. if (temp > 255)
  312. temp = 255;
  313. if (temp < 0)
  314. temp = 0;
  315. return (int)temp;
  316. }
  317. // Change this to LOG_WARNING if you wish to always see the replies
  318. #define READ_DEBUG LOG_DEBUG
  319. //#define READ_DEBUG LOG_ERR
  320. static void display_kline(struct cgpu_info *klncgpu, KLINE *kline)
  321. {
  322. switch (kline->hd.cmd) {
  323. case '=':
  324. applog(READ_DEBUG,
  325. "%s (%s) work [%c] dev=%d workid=%d"
  326. " nonce=0x%08x",
  327. klncgpu->drv->dname, klncgpu->device_path,
  328. kline->wr.cmd,
  329. (int)(kline->wr.dev),
  330. (int)(kline->wr.workid),
  331. (unsigned int)K_NONCE(kline->wr.nonce));
  332. break;
  333. case 'S':
  334. case 'W':
  335. case 'A':
  336. case 'E':
  337. applog(READ_DEBUG,
  338. "%s (%s) status [%c] dev=%d chips=%d"
  339. " slaves=%d workcq=%d workid=%d temp=%d fan=%d"
  340. " errors=%d hashes=%d max=%d noise=%d",
  341. klncgpu->drv->dname, klncgpu->device_path,
  342. kline->ws.cmd,
  343. (int)(kline->ws.dev),
  344. (int)(kline->ws.chipcount),
  345. (int)(kline->ws.slavecount),
  346. (int)(kline->ws.workqc),
  347. (int)(kline->ws.workid),
  348. (int)(kline->ws.temp),
  349. (int)(kline->ws.fanspeed),
  350. (int)(kline->ws.errorcount),
  351. K_HASHCOUNT(kline->ws.hashcount),
  352. K_MAXCOUNT(kline->ws.maxcount),
  353. (int)(kline->ws.noise));
  354. break;
  355. case 'C':
  356. applog(READ_DEBUG,
  357. "%s (%s) config [%c] dev=%d clock=%d"
  358. " temptarget=%d tempcrit=%d fan=%d",
  359. klncgpu->drv->dname, klncgpu->device_path,
  360. kline->cfg.cmd,
  361. (int)(kline->cfg.dev),
  362. K_HASHCLOCK(kline->cfg.hashclock),
  363. (int)(kline->cfg.temptarget),
  364. (int)(kline->cfg.tempcritical),
  365. (int)(kline->cfg.fantarget));
  366. break;
  367. case 'I':
  368. applog(READ_DEBUG,
  369. "%s (%s) info [%c] version=0x%02x prod=%.7s"
  370. " serial=0x%08x",
  371. klncgpu->drv->dname, klncgpu->device_path,
  372. kline->hd.cmd,
  373. (int)(kline->id.version),
  374. kline->id.product,
  375. (unsigned int)K_SERIAL(kline->id.serial));
  376. break;
  377. default:
  378. {
  379. char hexdata[REPLY_SIZE * 2];
  380. bin2hex(hexdata, &kline->hd.dev, REPLY_SIZE - 1);
  381. applog(LOG_ERR,
  382. "%s (%s) [%c:%s] unknown and ignored",
  383. klncgpu->drv->dname, klncgpu->device_path,
  384. kline->hd.cmd, hexdata);
  385. free(hexdata);
  386. break;
  387. }
  388. }
  389. }
  390. static KLIST *SendCmdGetReply(struct cgpu_info *klncgpu, KLINE *kline, int datalen)
  391. {
  392. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  393. KLIST *kitem;
  394. int retries = CMD_REPLY_RETRIES;
  395. int err, amt, writ;
  396. if (klninfo->usbinfo_nodev)
  397. return NULL;
  398. writ = KSENDHD(datalen);
  399. err = usb_write(klncgpu, kline, writ, &amt);
  400. if (err < 0 || amt != writ) {
  401. applog(LOG_ERR, "%s (%s) Cmd:%c Dev:%d, write failed (%d:%d:%d)",
  402. klncgpu->drv->dname, klncgpu->device_path,
  403. kline->hd.cmd, (int)kline->hd.dev,
  404. writ, amt, err);
  405. }
  406. while (retries-- > 0 && klninfo->shutdown == false) {
  407. cgsleep_ms(REPLY_WAIT_TIME);
  408. cg_rlock(&klninfo->klist_lock);
  409. kitem = klninfo->used;
  410. while (kitem) {
  411. if (kitem->kline.hd.cmd == kline->hd.cmd &&
  412. kitem->kline.hd.dev == kline->hd.dev &&
  413. kitem->ready == true && kitem->working == false) {
  414. kitem->working = true;
  415. cg_runlock(&klninfo->klist_lock);
  416. return kitem;
  417. }
  418. kitem = kitem->next;
  419. }
  420. cg_runlock(&klninfo->klist_lock);
  421. }
  422. return NULL;
  423. }
  424. static bool klondike_get_stats(struct cgpu_info *klncgpu)
  425. {
  426. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  427. KLIST *kitem;
  428. KLINE kline;
  429. int slaves, dev;
  430. if (klninfo->usbinfo_nodev || klninfo->status == NULL)
  431. return false;
  432. applog(LOG_DEBUG, "Klondike getting status");
  433. slaves = klninfo->status[0].kline.ws.slavecount;
  434. // loop thru devices and get status for each
  435. for (dev = 0; dev <= slaves; dev++) {
  436. kline.hd.cmd = 'S';
  437. kline.hd.dev = dev;
  438. kitem = SendCmdGetReply(klncgpu, &kline, 0);
  439. if (kitem != NULL) {
  440. wr_lock(&(klninfo->stat_lock));
  441. memcpy((void *)(&(klninfo->status[dev])), (void *)kitem, sizeof(*kitem));
  442. wr_unlock(&(klninfo->stat_lock));
  443. release_kitem(klncgpu, kitem);
  444. kitem = NULL;
  445. }
  446. }
  447. // todo: detect slavecount change and realloc space
  448. return true;
  449. }
  450. static bool klondike_init(struct cgpu_info *klncgpu)
  451. {
  452. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  453. KLIST *kitem;
  454. KLINE kline;
  455. int slaves, dev;
  456. kline.hd.cmd = 'S';
  457. kline.hd.dev = 0;
  458. kitem = SendCmdGetReply(klncgpu, &kline, 0);
  459. if (kitem == NULL)
  460. return false;
  461. slaves = kitem->kline.ws.slavecount;
  462. release_kitem(klncgpu, kitem);
  463. kitem = NULL;
  464. if (klninfo->status == NULL) {
  465. applog(LOG_DEBUG, "Klondike initializing data");
  466. // alloc space for status, devinfo and cfg for master and slaves
  467. klninfo->status = calloc(slaves+1, sizeof(KLIST));
  468. if (unlikely(!klninfo->status))
  469. quit(1, "Failed to calloc status array in klondke_get_stats");
  470. klninfo->devinfo = calloc(slaves+1, sizeof(DEVINFO));
  471. if (unlikely(!klninfo->devinfo))
  472. quit(1, "Failed to calloc devinfo array in klondke_get_stats");
  473. klninfo->cfg = calloc(slaves+1, sizeof(KLIST));
  474. if (unlikely(!klninfo->cfg))
  475. quit(1, "Failed to calloc cfg array in klondke_get_stats");
  476. }
  477. // zero init triggers read back only
  478. memset(&(kline.cfg), 0, sizeof(kline.cfg));
  479. kline.cfg.cmd = 'C';
  480. int size = 2;
  481. // boundaries are checked by device, with valid values returned
  482. if (opt_klondike_options != NULL) {
  483. int hashclock;
  484. double temp1, temp2;
  485. sscanf(opt_klondike_options, "%d:%lf:%lf:%"SCNu8,
  486. &hashclock,
  487. &temp1, &temp2,
  488. &kline.cfg.fantarget);
  489. SET_HASHCLOCK(kline.cfg.hashclock, hashclock);
  490. kline.cfg.temptarget = cvtCToKln(temp1);
  491. kline.cfg.tempcritical = cvtCToKln(temp2);
  492. kline.cfg.fantarget = (int)255*kline.cfg.fantarget/100;
  493. size = sizeof(kline.cfg) - 2;
  494. }
  495. for (dev = 0; dev <= slaves; dev++) {
  496. kline.cfg.dev = dev;
  497. kitem = SendCmdGetReply(klncgpu, &kline, size);
  498. if (kitem != NULL) {
  499. memcpy((void *)&(klninfo->cfg[dev]), kitem, sizeof(*kitem));
  500. applog(LOG_WARNING, "Klondike config (%d: Clk: %d, T:%.0lf, C:%.0lf, F:%d)",
  501. dev, K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock),
  502. cvtKlnToC(klninfo->cfg[dev].kline.cfg.temptarget),
  503. cvtKlnToC(klninfo->cfg[dev].kline.cfg.tempcritical),
  504. (int)100*klninfo->cfg[dev].kline.cfg.fantarget/256);
  505. release_kitem(klncgpu, kitem);
  506. kitem = NULL;
  507. }
  508. }
  509. klondike_get_stats(klncgpu);
  510. for (dev = 0; dev <= slaves; dev++) {
  511. klninfo->devinfo[dev].rangesize = ((uint64_t)1<<32) / klninfo->status[dev].kline.ws.chipcount;
  512. klninfo->devinfo[dev].chipstats = calloc(klninfo->status[dev].kline.ws.chipcount*2 , sizeof(uint32_t));
  513. }
  514. int tries = 2;
  515. bool ok = false;
  516. kline.hd.cmd = 'E';
  517. kline.hd.dev = 0;
  518. kline.hd.buf[0] = '1';
  519. while (tries-- > 0) {
  520. kitem = SendCmdGetReply(klncgpu, &kline, 1);
  521. if (kitem) {
  522. release_kitem(klncgpu, kitem);
  523. kitem = NULL;
  524. ok = true;
  525. break;
  526. }
  527. }
  528. if (!ok)
  529. applog(LOG_ERR, "%s%i: failed to enable", klncgpu->drv->name, klncgpu->device_id);
  530. return ok;
  531. }
  532. static
  533. bool klondike_foundlowl(struct lowlevel_device_info * const info, __maybe_unused void * const userp)
  534. {
  535. if (unlikely(info->lowl != &lowl_usb))
  536. {
  537. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not usb!",
  538. __func__, info->product, info->serial);
  539. return false;
  540. }
  541. struct libusb_device * const dev = info->lowl_data;
  542. // static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  543. struct cgpu_info * const klncgpu = malloc(sizeof(*klncgpu));
  544. struct klondike_info *klninfo = NULL;
  545. if (unlikely(!klncgpu))
  546. quit(1, "Failed to calloc klncgpu in klondike_detect_one");
  547. *klncgpu = (struct cgpu_info){
  548. .drv = &klondike_drv,
  549. .deven = DEV_ENABLED,
  550. .threads = 1,
  551. };
  552. klninfo = calloc(1, sizeof(*klninfo));
  553. if (unlikely(!klninfo))
  554. quit(1, "Failed to calloc klninfo in klondke_detect_one");
  555. klncgpu->device_data = (void *)klninfo;
  556. klninfo->free = new_klist_set(klncgpu);
  557. if (usb_init(klncgpu, dev)) {
  558. int sent, recd, err;
  559. KLIST kitem;
  560. int attempts = 0;
  561. klncgpu->device_path = strdup(info->devid);
  562. while (attempts++ < 3) {
  563. err = usb_write(klncgpu, "I", 2, &sent);
  564. if (err < 0 || sent != 2) {
  565. applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)",
  566. klncgpu->drv->dname,
  567. klncgpu->device_path,
  568. sent, err);
  569. }
  570. cgsleep_ms(REPLY_WAIT_TIME*10);
  571. err = usb_read(klncgpu, &kitem.kline, REPLY_SIZE, &recd);
  572. if (err < 0) {
  573. applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)",
  574. klncgpu->drv->dname,
  575. klncgpu->device_path,
  576. recd, err);
  577. } else if (recd < 1) {
  578. applog(LOG_ERR, "%s (%s) detect empty reply (%d)",
  579. klncgpu->drv->dname,
  580. klncgpu->device_path,
  581. recd);
  582. } else if (kitem.kline.hd.cmd == 'I' && kitem.kline.hd.dev == 0) {
  583. display_kline(klncgpu, &kitem.kline);
  584. applog(LOG_DEBUG, "%s (%s) detect successful",
  585. klncgpu->drv->dname,
  586. klncgpu->device_path);
  587. if (!add_cgpu(klncgpu))
  588. break;
  589. applog(LOG_DEBUG, "Klondike cgpu added");
  590. cglock_init(&klninfo->klist_lock);
  591. return true;
  592. }
  593. }
  594. usb_uninit(klncgpu);
  595. }
  596. free(klninfo->free);
  597. free(klninfo);
  598. free(klncgpu);
  599. return false;
  600. }
  601. static
  602. bool klondike_detect_one(const char *serial)
  603. {
  604. return lowlevel_detect_serial(klondike_foundlowl, serial);
  605. }
  606. static
  607. int klondike_autodetect()
  608. {
  609. return lowlevel_detect(klondike_foundlowl, "K16");
  610. }
  611. static
  612. void klondike_detect()
  613. {
  614. generic_detect(&klondike_drv, klondike_detect_one, klondike_autodetect, 0);
  615. }
  616. static
  617. bool klondike_identify(__maybe_unused struct cgpu_info * const klncgpu)
  618. {
  619. /*
  620. KLINE kline;
  621. kline.hd.cmd = 'I';
  622. kline.hd.dev = 0;
  623. SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  624. */
  625. return false;
  626. }
  627. static void klondike_check_nonce(struct cgpu_info *klncgpu, KLIST *kitem)
  628. {
  629. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  630. struct work *work, *tmp;
  631. KLINE *kline = &(kitem->kline);
  632. struct timeval tv_now;
  633. double us_diff;
  634. uint32_t nonce = K_NONCE(kline->wr.nonce) - 0xC0;
  635. applog(LOG_DEBUG, "Klondike FOUND NONCE (%02x:%08x)",
  636. kline->wr.workid, (unsigned int)nonce);
  637. HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
  638. if (work->queued && (work->subid == (kline->wr.dev*256 + kline->wr.workid))) {
  639. wr_lock(&(klninfo->stat_lock));
  640. klninfo->devinfo[kline->wr.dev].noncecount++;
  641. klninfo->noncecount++;
  642. wr_unlock(&(klninfo->stat_lock));
  643. // kline->wr.nonce = le32toh(kline->wr.nonce - 0xC0);
  644. applog(LOG_DEBUG, "Klondike SUBMIT NONCE (%02x:%08x)",
  645. kline->wr.workid, (unsigned int)nonce);
  646. cgtime(&tv_now);
  647. bool ok = submit_nonce(klncgpu->thr[0], work, nonce);
  648. applog(LOG_DEBUG, "Klondike chip stats %d, %08x, %d, %d",
  649. kline->wr.dev, (unsigned int)nonce,
  650. klninfo->devinfo[kline->wr.dev].rangesize,
  651. klninfo->status[kline->wr.dev].kline.ws.chipcount);
  652. klninfo->devinfo[kline->wr.dev].chipstats[(nonce / klninfo->devinfo[kline->wr.dev].rangesize) + (ok ? 0 : klninfo->status[kline->wr.dev].kline.ws.chipcount)]++;
  653. us_diff = us_tdiff(&tv_now, &(kitem->tv_when));
  654. if (klninfo->delay_count == 0) {
  655. klninfo->delay_min = us_diff;
  656. klninfo->delay_max = us_diff;
  657. } else {
  658. if (klninfo->delay_min > us_diff)
  659. klninfo->delay_min = us_diff;
  660. if (klninfo->delay_max < us_diff)
  661. klninfo->delay_max = us_diff;
  662. }
  663. klninfo->delay_count++;
  664. klninfo->delay_total += us_diff;
  665. if (klninfo->nonce_count > 0) {
  666. us_diff = us_tdiff(&(kitem->tv_when), &(klninfo->tv_last_nonce_received));
  667. if (klninfo->nonce_count == 1) {
  668. klninfo->nonce_min = us_diff;
  669. klninfo->nonce_max = us_diff;
  670. } else {
  671. if (klninfo->nonce_min > us_diff)
  672. klninfo->nonce_min = us_diff;
  673. if (klninfo->nonce_max < us_diff)
  674. klninfo->nonce_max = us_diff;
  675. }
  676. klninfo->nonce_total += us_diff;
  677. }
  678. klninfo->nonce_count++;
  679. memcpy(&(klninfo->tv_last_nonce_received), &(kitem->tv_when),
  680. sizeof(klninfo->tv_last_nonce_received));
  681. return;
  682. }
  683. }
  684. applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored",
  685. klncgpu->drv->name, klncgpu->device_id,
  686. kline->wr.dev, kline->wr.workid, (unsigned int)nonce);
  687. //inc_hw_errors(klncgpu->thr[0]);
  688. }
  689. // thread to keep looking for replies
  690. static void *klondike_get_replies(void *userdata)
  691. {
  692. struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
  693. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  694. KLIST *kitem = NULL;
  695. int err, recd;
  696. applog(LOG_ERR, "Klondike listening for replies");
  697. while (klninfo->shutdown == false) {
  698. if (klninfo->usbinfo_nodev)
  699. return NULL;
  700. if (kitem == NULL)
  701. kitem = allocate_kitem(klncgpu);
  702. else
  703. memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
  704. err = usb_read(klncgpu, &kitem->kline, REPLY_SIZE, &recd);
  705. if (!err && recd == REPLY_SIZE) {
  706. cgtime(&(kitem->tv_when));
  707. kitem->block_seq = klninfo->block_seq;
  708. if (opt_log_level <= READ_DEBUG) {
  709. char hexdata[recd * 2];
  710. bin2hex(hexdata, &kitem->kline.hd.dev, recd-1);
  711. applog(READ_DEBUG, "%s (%s) reply [%c:%s]",
  712. klncgpu->drv->dname, klncgpu->device_path,
  713. kitem->kline.hd.cmd, hexdata);
  714. }
  715. switch (kitem->kline.hd.cmd) {
  716. case '=':
  717. klondike_check_nonce(klncgpu, kitem);
  718. display_kline(klncgpu, &kitem->kline);
  719. break;
  720. case 'S':
  721. case 'W':
  722. case 'A':
  723. case 'E':
  724. wr_lock(&(klninfo->stat_lock));
  725. klninfo->errorcount += kitem->kline.ws.errorcount;
  726. klninfo->noisecount += kitem->kline.ws.noise;
  727. wr_unlock(&(klninfo->stat_lock));
  728. display_kline(klncgpu, &kitem->kline);
  729. kitem->ready = true;
  730. kitem = NULL;
  731. break;
  732. case 'C':
  733. display_kline(klncgpu, &kitem->kline);
  734. kitem->ready = true;
  735. kitem = NULL;
  736. break;
  737. case 'I':
  738. display_kline(klncgpu, &kitem->kline);
  739. kitem->ready = true;
  740. kitem = NULL;
  741. break;
  742. default:
  743. display_kline(klncgpu, &kitem->kline);
  744. break;
  745. }
  746. }
  747. }
  748. return NULL;
  749. }
  750. static void klondike_flush_work(struct cgpu_info *klncgpu)
  751. {
  752. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  753. KLIST *kitem;
  754. KLINE kline;
  755. int slaves, dev;
  756. klninfo->block_seq++;
  757. applog(LOG_DEBUG, "Klondike flushing work");
  758. slaves = klninfo->status[0].kline.ws.slavecount;
  759. kline.hd.cmd = 'A';
  760. for (dev = 0; dev <= slaves; dev++) {
  761. kline.hd.dev = dev;
  762. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  763. if (kitem != NULL) {
  764. wr_lock(&(klninfo->stat_lock));
  765. memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(*kitem));
  766. wr_unlock(&(klninfo->stat_lock));
  767. release_kitem(klncgpu, kitem);
  768. kitem = NULL;
  769. }
  770. }
  771. }
  772. static bool klondike_thread_prepare(struct thr_info *thr)
  773. {
  774. struct cgpu_info *klncgpu = thr->cgpu;
  775. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  776. if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) {
  777. applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id);
  778. return false;
  779. }
  780. pthread_detach(klninfo->replies_thr.pth);
  781. // let the listening get started
  782. cgsleep_ms(100);
  783. return klondike_init(klncgpu);
  784. }
  785. static bool klondike_thread_init(struct thr_info *thr)
  786. {
  787. struct cgpu_info *klncgpu = thr->cgpu;
  788. struct klondike_info * const klninfo = klncgpu->device_data;
  789. notifier_init(thr->work_restart_notifier);
  790. if (klninfo->usbinfo_nodev)
  791. return false;
  792. klondike_flush_work(klncgpu);
  793. return true;
  794. }
  795. static void klondike_shutdown(struct thr_info *thr)
  796. {
  797. struct cgpu_info *klncgpu = thr->cgpu;
  798. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  799. KLIST *kitem;
  800. KLINE kline;
  801. int dev;
  802. applog(LOG_DEBUG, "Klondike shutting down work");
  803. kline.hd.cmd = 'E';
  804. for (dev = 0; dev <= klninfo->status[0].kline.ws.slavecount; dev++) {
  805. kline.hd.dev = dev;
  806. kline.hd.buf[0] = '0';
  807. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
  808. if (kitem)
  809. release_kitem(klncgpu, kitem);
  810. }
  811. klncgpu->shutdown = klninfo->shutdown = true;
  812. }
  813. static void klondike_thread_enable(struct thr_info *thr)
  814. {
  815. struct cgpu_info *klncgpu = thr->cgpu;
  816. struct klondike_info * const klninfo = klncgpu->device_data;
  817. if (klninfo->usbinfo_nodev)
  818. return;
  819. /*
  820. KLINE kline;
  821. kline.hd.cmd = 'E';
  822. kline.hd.dev = dev;
  823. kline.hd.buf[0] = '0';
  824. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
  825. */
  826. }
  827. static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
  828. {
  829. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  830. struct work *tmp;
  831. KLINE kline;
  832. if (klninfo->usbinfo_nodev)
  833. return false;
  834. kline.wt.cmd = 'W';
  835. kline.wt.dev = dev;
  836. memcpy(kline.wt.midstate, work->midstate, MIDSTATE_BYTES);
  837. memcpy(kline.wt.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  838. kline.wt.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
  839. work->subid = dev*256 + kline.wt.workid;
  840. if (opt_log_level <= LOG_DEBUG) {
  841. char hexdata[(sizeof(kline.wt) * 2) + 1];
  842. bin2hex(hexdata, &kline.wt, sizeof(kline.wt));
  843. applog(LOG_DEBUG, "WORKDATA: %s", hexdata);
  844. }
  845. applog(LOG_DEBUG, "Klondike sending work (%d:%02x)", dev, kline.wt.workid);
  846. KLIST *kitem = SendCmdGetReply(klncgpu, &kline, sizeof(kline.wt));
  847. if (kitem != NULL) {
  848. wr_lock(&(klninfo->stat_lock));
  849. memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(*kitem));
  850. wr_unlock(&(klninfo->stat_lock));
  851. release_kitem(klncgpu, kitem);
  852. kitem = NULL;
  853. // remove old work
  854. HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
  855. if (work->queued && (work->subid == (int)(dev*256 + ((klninfo->devinfo[dev].nextworkid-2*MAX_WORK_COUNT) & 0xFF))))
  856. work_completed(klncgpu, work);
  857. }
  858. return true;
  859. }
  860. return false;
  861. }
  862. static bool klondike_queue_full(struct cgpu_info *klncgpu)
  863. {
  864. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  865. struct work *work = NULL;
  866. int dev, queued, slaves;
  867. slaves = klninfo->status[0].kline.ws.slavecount;
  868. for (queued = 0; queued < MAX_WORK_COUNT-1; queued++)
  869. for (dev = 0; dev <= slaves; dev++)
  870. if (klninfo->status[dev].kline.ws.workqc <= queued) {
  871. if (!work)
  872. work = get_queued(klncgpu);
  873. if (unlikely(!work))
  874. return false;
  875. if (klondike_send_work(klncgpu, dev, work)) {
  876. work = NULL;
  877. break;
  878. }
  879. }
  880. return true;
  881. }
  882. static int64_t klondike_scanwork(struct thr_info *thr)
  883. {
  884. struct cgpu_info *klncgpu = thr->cgpu;
  885. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  886. int64_t newhashcount = 0;
  887. int dev, slaves;
  888. if (klninfo->usbinfo_nodev)
  889. return -1;
  890. restart_wait(thr, 200);
  891. if (klninfo->status != NULL) {
  892. rd_lock(&(klninfo->stat_lock));
  893. slaves = klninfo->status[0].kline.ws.slavecount;
  894. for (dev = 0; dev <= slaves; dev++) {
  895. uint64_t newhashdev = 0, hashcount;
  896. int maxcount;
  897. hashcount = K_HASHCOUNT(klninfo->status[dev].kline.ws.hashcount);
  898. maxcount = K_MAXCOUNT(klninfo->status[dev].kline.ws.maxcount);
  899. if (klninfo->devinfo[dev].lasthashcount > hashcount) // todo: chg this to check workid for wrapped instead
  900. newhashdev += maxcount; // hash counter wrapped
  901. newhashdev += hashcount - klninfo->devinfo[dev].lasthashcount;
  902. klninfo->devinfo[dev].lasthashcount = hashcount;
  903. if (maxcount != 0)
  904. klninfo->hashcount += (newhashdev << 32) / maxcount;
  905. // todo: check stats for critical conditions
  906. }
  907. newhashcount += 0xffffffffull * (uint64_t)klninfo->noncecount;
  908. klninfo->noncecount = 0;
  909. rd_unlock(&(klninfo->stat_lock));
  910. }
  911. return newhashcount;
  912. }
  913. static void get_klondike_statline_before(char *buf, size_t siz, struct cgpu_info *klncgpu)
  914. {
  915. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  916. uint8_t temp = 0xFF;
  917. uint16_t fan = 0;
  918. uint16_t clock = 0;
  919. int dev, slaves;
  920. char tmp[16];
  921. if (klninfo->status == NULL) {
  922. return;
  923. }
  924. rd_lock(&(klninfo->stat_lock));
  925. slaves = klninfo->status[0].kline.ws.slavecount;
  926. for (dev = 0; dev <= slaves; dev++) {
  927. if (klninfo->status[dev].kline.ws.temp < temp)
  928. temp = klninfo->status[dev].kline.ws.temp;
  929. fan += klninfo->cfg[dev].kline.cfg.fantarget;
  930. clock += (uint16_t)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  931. }
  932. fan /= slaves + 1;
  933. clock /= slaves + 1;
  934. rd_unlock(&(klninfo->stat_lock));
  935. snprintf(tmp, sizeof(tmp), "%2.0fC", cvtKlnToC(temp));
  936. if (strlen(tmp) < 4)
  937. strcat(tmp, " ");
  938. tailsprintf(buf, siz, "%3dMHz %3d%% %s| ", (int)clock, fan*100/255, tmp);
  939. }
  940. static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
  941. {
  942. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  943. struct api_data *root = NULL;
  944. char buf[32];
  945. int dev, slaves;
  946. if (klninfo->status == NULL)
  947. return NULL;
  948. rd_lock(&(klninfo->stat_lock));
  949. slaves = klninfo->status[0].kline.ws.slavecount;
  950. for (dev = 0; dev <= slaves; dev++) {
  951. float fTemp = cvtKlnToC(klninfo->status[dev].kline.ws.temp);
  952. sprintf(buf, "Temp %d", dev);
  953. root = api_add_temp(root, buf, &fTemp, true);
  954. double dClk = (double)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  955. sprintf(buf, "Clock %d", dev);
  956. root = api_add_freq(root, buf, &dClk, true);
  957. unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].kline.cfg.fantarget / 255;
  958. sprintf(buf, "Fan Percent %d", dev);
  959. root = api_add_int(root, buf, (int *)(&iFan), true);
  960. iFan = 0;
  961. if (klninfo->status[dev].kline.ws.fanspeed > 0)
  962. iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].kline.ws.fanspeed;
  963. sprintf(buf, "Fan RPM %d", dev);
  964. root = api_add_int(root, buf, (int *)(&iFan), true);
  965. if (klninfo->devinfo[dev].chipstats != NULL) {
  966. char data[2048];
  967. char one[32];
  968. int n;
  969. sprintf(buf, "Nonces / Chip %d", dev);
  970. data[0] = '\0';
  971. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  972. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n]);
  973. strcat(data, one);
  974. }
  975. root = api_add_string(root, buf, data, true);
  976. sprintf(buf, "Errors / Chip %d", dev);
  977. data[0] = '\0';
  978. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  979. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].kline.ws.chipcount]);
  980. strcat(data, one);
  981. }
  982. root = api_add_string(root, buf, data, true);
  983. }
  984. }
  985. root = api_add_uint64(root, "Hash Count", &(klninfo->hashcount), true);
  986. root = api_add_uint64(root, "Error Count", &(klninfo->errorcount), true);
  987. root = api_add_uint64(root, "Noise Count", &(klninfo->noisecount), true);
  988. root = api_add_int(root, "KLine Limit", &(klninfo->kline_count), true);
  989. root = api_add_int(root, "KLine Used", &(klninfo->used_count), true);
  990. root = api_add_elapsed(root, "KQue Delay Count", &(klninfo->delay_count), true);
  991. root = api_add_elapsed(root, "KQue Delay Total", &(klninfo->delay_total), true);
  992. root = api_add_elapsed(root, "KQue Delay Min", &(klninfo->delay_min), true);
  993. root = api_add_elapsed(root, "KQue Delay Max", &(klninfo->delay_max), true);
  994. double avg;
  995. if (klninfo->delay_count == 0)
  996. avg = 0;
  997. else
  998. avg = klninfo->delay_total / klninfo->delay_count;
  999. root = api_add_diff(root, "KQue Delay Avg", &avg, true);
  1000. root = api_add_elapsed(root, "KQue Nonce Count", &(klninfo->nonce_count), true);
  1001. root = api_add_elapsed(root, "KQue Nonce Total", &(klninfo->nonce_total), true);
  1002. root = api_add_elapsed(root, "KQue Nonce Min", &(klninfo->nonce_min), true);
  1003. root = api_add_elapsed(root, "KQue Nonce Max", &(klninfo->nonce_max), true);
  1004. if (klninfo->nonce_count == 0)
  1005. avg = 0;
  1006. else
  1007. avg = klninfo->nonce_total / klninfo->nonce_count;
  1008. root = api_add_diff(root, "KQue Nonce Avg", &avg, true);
  1009. rd_unlock(&(klninfo->stat_lock));
  1010. return root;
  1011. }
  1012. struct device_drv klondike_drv = {
  1013. .dname = "Klondike",
  1014. .name = "KLN",
  1015. .drv_detect = klondike_detect,
  1016. .get_api_stats = klondike_api_stats,
  1017. // .get_statline_before = get_klondike_statline_before,
  1018. .get_stats = klondike_get_stats,
  1019. .identify_device = klondike_identify,
  1020. .thread_prepare = klondike_thread_prepare,
  1021. .thread_init = klondike_thread_init,
  1022. .minerloop = hash_queued_work,
  1023. .scanwork = klondike_scanwork,
  1024. .queue_full = klondike_queue_full,
  1025. .flush_work = klondike_flush_work,
  1026. .thread_shutdown = klondike_shutdown,
  1027. .thread_enable = klondike_thread_enable
  1028. };