driver-klondike.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220
  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. us_diff = us_tdiff(&(kitem->tv_when), &(klninfo->tv_last_nonce_received));
  666. if (klninfo->nonce_count == 0) {
  667. klninfo->nonce_min = us_diff;
  668. klninfo->nonce_max = us_diff;
  669. } else {
  670. if (klninfo->nonce_min > us_diff)
  671. klninfo->nonce_min = us_diff;
  672. if (klninfo->nonce_max < us_diff)
  673. klninfo->nonce_max = us_diff;
  674. }
  675. klninfo->nonce_count++;
  676. klninfo->nonce_total += us_diff;
  677. memcpy(&(klninfo->tv_last_nonce_received), &(kitem->tv_when),
  678. sizeof(klninfo->tv_last_nonce_received));
  679. return;
  680. }
  681. }
  682. applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored",
  683. klncgpu->drv->name, klncgpu->device_id,
  684. kline->wr.dev, kline->wr.workid, (unsigned int)nonce);
  685. //inc_hw_errors(klncgpu->thr[0]);
  686. }
  687. // thread to keep looking for replies
  688. static void *klondike_get_replies(void *userdata)
  689. {
  690. struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
  691. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  692. KLIST *kitem = NULL;
  693. int err, recd;
  694. applog(LOG_ERR, "Klondike listening for replies");
  695. while (klninfo->shutdown == false) {
  696. if (klninfo->usbinfo_nodev)
  697. return NULL;
  698. if (kitem == NULL)
  699. kitem = allocate_kitem(klncgpu);
  700. else
  701. memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
  702. err = usb_read(klncgpu, &kitem->kline, REPLY_SIZE, &recd);
  703. if (!err && recd == REPLY_SIZE) {
  704. cgtime(&(kitem->tv_when));
  705. kitem->block_seq = klninfo->block_seq;
  706. if (opt_log_level <= READ_DEBUG) {
  707. char hexdata[recd * 2];
  708. bin2hex(hexdata, &kitem->kline.hd.dev, recd-1);
  709. applog(READ_DEBUG, "%s (%s) reply [%c:%s]",
  710. klncgpu->drv->dname, klncgpu->device_path,
  711. kitem->kline.hd.cmd, hexdata);
  712. }
  713. switch (kitem->kline.hd.cmd) {
  714. case '=':
  715. klondike_check_nonce(klncgpu, kitem);
  716. display_kline(klncgpu, &kitem->kline);
  717. break;
  718. case 'S':
  719. case 'W':
  720. case 'A':
  721. case 'E':
  722. wr_lock(&(klninfo->stat_lock));
  723. klninfo->errorcount += kitem->kline.ws.errorcount;
  724. klninfo->noisecount += kitem->kline.ws.noise;
  725. wr_unlock(&(klninfo->stat_lock));
  726. display_kline(klncgpu, &kitem->kline);
  727. kitem->ready = true;
  728. kitem = NULL;
  729. break;
  730. case 'C':
  731. display_kline(klncgpu, &kitem->kline);
  732. kitem->ready = true;
  733. kitem = NULL;
  734. break;
  735. case 'I':
  736. display_kline(klncgpu, &kitem->kline);
  737. kitem->ready = true;
  738. kitem = NULL;
  739. break;
  740. default:
  741. display_kline(klncgpu, &kitem->kline);
  742. break;
  743. }
  744. }
  745. }
  746. return NULL;
  747. }
  748. static void klondike_flush_work(struct cgpu_info *klncgpu)
  749. {
  750. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  751. KLIST *kitem;
  752. KLINE kline;
  753. int slaves, dev;
  754. klninfo->block_seq++;
  755. applog(LOG_DEBUG, "Klondike flushing work");
  756. slaves = klninfo->status[0].kline.ws.slavecount;
  757. kline.hd.cmd = 'A';
  758. for (dev = 0; dev <= slaves; dev++) {
  759. kline.hd.dev = dev;
  760. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  761. if (kitem != NULL) {
  762. wr_lock(&(klninfo->stat_lock));
  763. memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(*kitem));
  764. wr_unlock(&(klninfo->stat_lock));
  765. release_kitem(klncgpu, kitem);
  766. kitem = NULL;
  767. }
  768. }
  769. }
  770. static bool klondike_thread_prepare(struct thr_info *thr)
  771. {
  772. struct cgpu_info *klncgpu = thr->cgpu;
  773. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  774. if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) {
  775. applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id);
  776. return false;
  777. }
  778. pthread_detach(klninfo->replies_thr.pth);
  779. // let the listening get started
  780. cgsleep_ms(100);
  781. return klondike_init(klncgpu);
  782. }
  783. static bool klondike_thread_init(struct thr_info *thr)
  784. {
  785. struct cgpu_info *klncgpu = thr->cgpu;
  786. struct klondike_info * const klninfo = klncgpu->device_data;
  787. notifier_init(thr->work_restart_notifier);
  788. if (klninfo->usbinfo_nodev)
  789. return false;
  790. klondike_flush_work(klncgpu);
  791. return true;
  792. }
  793. static void klondike_shutdown(struct thr_info *thr)
  794. {
  795. struct cgpu_info *klncgpu = thr->cgpu;
  796. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  797. KLIST *kitem;
  798. KLINE kline;
  799. int dev;
  800. applog(LOG_DEBUG, "Klondike shutting down work");
  801. kline.hd.cmd = 'E';
  802. for (dev = 0; dev <= klninfo->status[0].kline.ws.slavecount; dev++) {
  803. kline.hd.dev = dev;
  804. kline.hd.buf[0] = '0';
  805. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
  806. if (kitem)
  807. release_kitem(klncgpu, kitem);
  808. }
  809. klncgpu->shutdown = klninfo->shutdown = true;
  810. }
  811. static void klondike_thread_enable(struct thr_info *thr)
  812. {
  813. struct cgpu_info *klncgpu = thr->cgpu;
  814. struct klondike_info * const klninfo = klncgpu->device_data;
  815. if (klninfo->usbinfo_nodev)
  816. return;
  817. /*
  818. KLINE kline;
  819. kline.hd.cmd = 'E';
  820. kline.hd.dev = dev;
  821. kline.hd.buf[0] = '0';
  822. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
  823. */
  824. }
  825. static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
  826. {
  827. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  828. struct work *tmp;
  829. KLINE kline;
  830. if (klninfo->usbinfo_nodev)
  831. return false;
  832. kline.wt.cmd = 'W';
  833. kline.wt.dev = dev;
  834. memcpy(kline.wt.midstate, work->midstate, MIDSTATE_BYTES);
  835. memcpy(kline.wt.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  836. kline.wt.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
  837. work->subid = dev*256 + kline.wt.workid;
  838. if (opt_log_level <= LOG_DEBUG) {
  839. char hexdata[(sizeof(kline.wt) * 2) + 1];
  840. bin2hex(hexdata, &kline.wt, sizeof(kline.wt));
  841. applog(LOG_DEBUG, "WORKDATA: %s", hexdata);
  842. }
  843. applog(LOG_DEBUG, "Klondike sending work (%d:%02x)", dev, kline.wt.workid);
  844. KLIST *kitem = SendCmdGetReply(klncgpu, &kline, sizeof(kline.wt));
  845. if (kitem != NULL) {
  846. wr_lock(&(klninfo->stat_lock));
  847. memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(*kitem));
  848. wr_unlock(&(klninfo->stat_lock));
  849. release_kitem(klncgpu, kitem);
  850. kitem = NULL;
  851. // remove old work
  852. HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
  853. if (work->queued && (work->subid == (int)(dev*256 + ((klninfo->devinfo[dev].nextworkid-2*MAX_WORK_COUNT) & 0xFF))))
  854. work_completed(klncgpu, work);
  855. }
  856. return true;
  857. }
  858. return false;
  859. }
  860. static bool klondike_queue_full(struct cgpu_info *klncgpu)
  861. {
  862. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  863. struct work *work = NULL;
  864. int dev, queued, slaves;
  865. slaves = klninfo->status[0].kline.ws.slavecount;
  866. for (queued = 0; queued < MAX_WORK_COUNT-1; queued++)
  867. for (dev = 0; dev <= slaves; dev++)
  868. if (klninfo->status[dev].kline.ws.workqc <= queued) {
  869. if (!work)
  870. work = get_queued(klncgpu);
  871. if (unlikely(!work))
  872. return false;
  873. if (klondike_send_work(klncgpu, dev, work)) {
  874. work = NULL;
  875. break;
  876. }
  877. }
  878. return true;
  879. }
  880. static int64_t klondike_scanwork(struct thr_info *thr)
  881. {
  882. struct cgpu_info *klncgpu = thr->cgpu;
  883. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  884. int64_t newhashcount = 0;
  885. int dev, slaves;
  886. if (klninfo->usbinfo_nodev)
  887. return -1;
  888. restart_wait(thr, 200);
  889. if (klninfo->status != NULL) {
  890. rd_lock(&(klninfo->stat_lock));
  891. slaves = klninfo->status[0].kline.ws.slavecount;
  892. for (dev = 0; dev <= slaves; dev++) {
  893. uint64_t newhashdev = 0, hashcount;
  894. int maxcount;
  895. hashcount = K_HASHCOUNT(klninfo->status[dev].kline.ws.hashcount);
  896. maxcount = K_MAXCOUNT(klninfo->status[dev].kline.ws.maxcount);
  897. if (klninfo->devinfo[dev].lasthashcount > hashcount) // todo: chg this to check workid for wrapped instead
  898. newhashdev += maxcount; // hash counter wrapped
  899. newhashdev += hashcount - klninfo->devinfo[dev].lasthashcount;
  900. klninfo->devinfo[dev].lasthashcount = hashcount;
  901. if (maxcount != 0)
  902. klninfo->hashcount += (newhashdev << 32) / maxcount;
  903. // todo: check stats for critical conditions
  904. }
  905. newhashcount += 0xffffffffull * (uint64_t)klninfo->noncecount;
  906. klninfo->noncecount = 0;
  907. rd_unlock(&(klninfo->stat_lock));
  908. }
  909. return newhashcount;
  910. }
  911. static void get_klondike_statline_before(char *buf, size_t siz, struct cgpu_info *klncgpu)
  912. {
  913. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  914. uint8_t temp = 0xFF;
  915. uint16_t fan = 0;
  916. uint16_t clock = 0;
  917. int dev, slaves;
  918. char tmp[16];
  919. if (klninfo->status == NULL) {
  920. return;
  921. }
  922. rd_lock(&(klninfo->stat_lock));
  923. slaves = klninfo->status[0].kline.ws.slavecount;
  924. for (dev = 0; dev <= slaves; dev++) {
  925. if (klninfo->status[dev].kline.ws.temp < temp)
  926. temp = klninfo->status[dev].kline.ws.temp;
  927. fan += klninfo->cfg[dev].kline.cfg.fantarget;
  928. clock += (uint16_t)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  929. }
  930. fan /= slaves + 1;
  931. clock /= slaves + 1;
  932. rd_unlock(&(klninfo->stat_lock));
  933. snprintf(tmp, sizeof(tmp), "%2.0fC", cvtKlnToC(temp));
  934. if (strlen(tmp) < 4)
  935. strcat(tmp, " ");
  936. tailsprintf(buf, siz, "%3dMHz %3d%% %s| ", (int)clock, fan*100/255, tmp);
  937. }
  938. static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
  939. {
  940. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  941. struct api_data *root = NULL;
  942. char buf[32];
  943. int dev, slaves;
  944. if (klninfo->status == NULL)
  945. return NULL;
  946. rd_lock(&(klninfo->stat_lock));
  947. slaves = klninfo->status[0].kline.ws.slavecount;
  948. for (dev = 0; dev <= slaves; dev++) {
  949. float fTemp = cvtKlnToC(klninfo->status[dev].kline.ws.temp);
  950. sprintf(buf, "Temp %d", dev);
  951. root = api_add_temp(root, buf, &fTemp, true);
  952. double dClk = (double)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  953. sprintf(buf, "Clock %d", dev);
  954. root = api_add_freq(root, buf, &dClk, true);
  955. unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].kline.cfg.fantarget / 255;
  956. sprintf(buf, "Fan Percent %d", dev);
  957. root = api_add_int(root, buf, (int *)(&iFan), true);
  958. iFan = 0;
  959. if (klninfo->status[dev].kline.ws.fanspeed > 0)
  960. iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].kline.ws.fanspeed;
  961. sprintf(buf, "Fan RPM %d", dev);
  962. root = api_add_int(root, buf, (int *)(&iFan), true);
  963. if (klninfo->devinfo[dev].chipstats != NULL) {
  964. char data[2048];
  965. char one[32];
  966. int n;
  967. sprintf(buf, "Nonces / Chip %d", dev);
  968. data[0] = '\0';
  969. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  970. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n]);
  971. strcat(data, one);
  972. }
  973. root = api_add_string(root, buf, data, true);
  974. sprintf(buf, "Errors / Chip %d", dev);
  975. data[0] = '\0';
  976. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  977. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].kline.ws.chipcount]);
  978. strcat(data, one);
  979. }
  980. root = api_add_string(root, buf, data, true);
  981. }
  982. }
  983. root = api_add_uint64(root, "Hash Count", &(klninfo->hashcount), true);
  984. root = api_add_uint64(root, "Error Count", &(klninfo->errorcount), true);
  985. root = api_add_uint64(root, "Noise Count", &(klninfo->noisecount), true);
  986. root = api_add_int(root, "KLine Limit", &(klninfo->kline_count), true);
  987. root = api_add_int(root, "KLine Used", &(klninfo->used_count), true);
  988. root = api_add_elapsed(root, "KQue Delay Count", &(klninfo->delay_count), true);
  989. root = api_add_elapsed(root, "KQue Delay Total", &(klninfo->delay_total), true);
  990. root = api_add_elapsed(root, "KQue Delay Min", &(klninfo->delay_min), true);
  991. root = api_add_elapsed(root, "KQue Delay Max", &(klninfo->delay_max), true);
  992. double avg;
  993. if (klninfo->delay_count == 0)
  994. avg = 0;
  995. else
  996. avg = klninfo->delay_total / klninfo->delay_count;
  997. root = api_add_diff(root, "KQue Delay Avg", &avg, true);
  998. root = api_add_elapsed(root, "KQue Nonce Count", &(klninfo->nonce_count), true);
  999. root = api_add_elapsed(root, "KQue Nonce Total", &(klninfo->nonce_total), true);
  1000. root = api_add_elapsed(root, "KQue Nonce Min", &(klninfo->nonce_min), true);
  1001. root = api_add_elapsed(root, "KQue Nonce Max", &(klninfo->nonce_max), true);
  1002. if (klninfo->nonce_count == 0)
  1003. avg = 0;
  1004. else
  1005. avg = klninfo->nonce_total / klninfo->nonce_count;
  1006. root = api_add_diff(root, "KQue Nonce Avg", &avg, true);
  1007. rd_unlock(&(klninfo->stat_lock));
  1008. return root;
  1009. }
  1010. struct device_drv klondike_drv = {
  1011. .dname = "Klondike",
  1012. .name = "KLN",
  1013. .drv_detect = klondike_detect,
  1014. .get_api_stats = klondike_api_stats,
  1015. // .get_statline_before = get_klondike_statline_before,
  1016. .get_stats = klondike_get_stats,
  1017. .identify_device = klondike_identify,
  1018. .thread_prepare = klondike_thread_prepare,
  1019. .thread_init = klondike_thread_init,
  1020. .minerloop = hash_queued_work,
  1021. .scanwork = klondike_scanwork,
  1022. .queue_full = klondike_queue_full,
  1023. .flush_work = klondike_flush_work,
  1024. .thread_shutdown = klondike_shutdown,
  1025. .thread_enable = klondike_thread_enable
  1026. };