driver-klondike.c 33 KB

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