driver-klondike.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  1. /*
  2. * Copyright 2014 Luke Dashjr
  3. * Copyright 2013 Andrew Smith
  4. * Copyright 2013 Con Kolivas
  5. * Copyright 2013 Chris Savery
  6. * Copyright 2013-2014 Luke Dashjr
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 3 of the License, or (at your option)
  11. * any later version. See COPYING for more details.
  12. */
  13. #include "config.h"
  14. #include <float.h>
  15. #include <limits.h>
  16. #include <pthread.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <strings.h>
  21. #include <sys/time.h>
  22. #include <unistd.h>
  23. #include <math.h>
  24. #ifdef WIN32
  25. #include <windows.h>
  26. #endif
  27. #include "compat.h"
  28. #include "deviceapi.h"
  29. #include "driver-klondike.h"
  30. #include "lowlevel.h"
  31. #include "lowl-usb.h"
  32. #include "miner.h"
  33. #define K1 "K1"
  34. #define K16 "K16"
  35. #define K64 "K64"
  36. static const char *msg_detect_send = "DSend";
  37. static const char *msg_detect_reply = "DReply";
  38. static const char *msg_send = "Send";
  39. static const char *msg_reply = "Reply";
  40. #define KLN_CMD_ABORT 'A'
  41. #define KLN_CMD_CONFIG 'C'
  42. #define KLN_CMD_ENABLE 'E'
  43. #define KLN_CMD_IDENT 'I'
  44. #define KLN_CMD_NONCE '='
  45. #define KLN_CMD_STATUS 'S'
  46. #define KLN_CMD_WORK 'W'
  47. #define KLN_CMD_ENABLE_OFF '0'
  48. #define KLN_CMD_ENABLE_ON '1'
  49. #define MIDSTATE_BYTES 32
  50. #define MERKLE_OFFSET 64
  51. #define MERKLE_BYTES 12
  52. #define REPLY_SIZE 15 // adequate for all types of replies
  53. #define MAX_KLINES 1024 // unhandled reply limit
  54. #define CMD_REPLY_RETRIES 8 // how many retries for cmds
  55. #define TACH_FACTOR 87890 // fan rpm divisor
  56. #define KLN_KILLWORK_TEMP 53.5
  57. #define KLN_COOLED_DOWN 45.5
  58. /*
  59. * How many incorrect slave counts to ignore in a row
  60. * 2 means it allows random grabage returned twice
  61. * Until slaves are implemented, this should never occur
  62. * so allowing 2 in a row should ignore random errros
  63. */
  64. #define KLN_ISS_IGNORE 2
  65. /*
  66. * If the queue status hasn't been updated for this long then do it now
  67. * 5GH/s = 859ms per full nonce range
  68. */
  69. #define LATE_UPDATE_MS ((int)(2.5 * 1000))
  70. // If 5 late updates in a row, try to reset the device
  71. #define LATE_UPDATE_LIMIT 5
  72. // If the reset fails sleep for 1s
  73. #define LATE_UPDATE_SLEEP_MS 1000
  74. // However give up after 8s
  75. #define LATE_UPDATE_NODEV_MS ((int)(8.0 * 1000))
  76. BFG_REGISTER_DRIVER(klondike_drv)
  77. typedef struct klondike_header {
  78. uint8_t cmd;
  79. uint8_t dev;
  80. uint8_t buf[REPLY_SIZE-2];
  81. } HEADER;
  82. #define K_2(_bytes) ((int)(_bytes[0]) + \
  83. ((int)(_bytes[1]) << 8))
  84. #define K_4(_bytes) ((uint64_t)(_bytes[0]) + \
  85. ((uint64_t)(_bytes[1]) << 8) + \
  86. ((uint64_t)(_bytes[2]) << 16) + \
  87. ((uint64_t)(_bytes[3]) << 24))
  88. #define K_SERIAL(_serial) K_4(_serial)
  89. #define K_HASHCOUNT(_hashcount) K_2(_hashcount)
  90. #define K_MAXCOUNT(_maxcount) K_2(_maxcount)
  91. #define K_NONCE(_nonce) K_4(_nonce)
  92. #define K_HASHCLOCK(_hashclock) K_2(_hashclock)
  93. #define SET_HASHCLOCK(_hashclock, _value) do { \
  94. (_hashclock)[0] = (uint8_t)((_value) & 0xff); \
  95. (_hashclock)[1] = (uint8_t)(((_value) >> 8) & 0xff); \
  96. } while(0)
  97. #define KSENDHD(_add) (sizeof(uint8_t) + sizeof(uint8_t) + _add)
  98. typedef struct klondike_id {
  99. uint8_t cmd;
  100. uint8_t dev;
  101. uint8_t version;
  102. uint8_t product[7];
  103. uint8_t serial[4];
  104. } IDENTITY;
  105. typedef struct klondike_status {
  106. uint8_t cmd;
  107. uint8_t dev;
  108. uint8_t state;
  109. uint8_t chipcount;
  110. uint8_t slavecount;
  111. uint8_t workqc;
  112. uint8_t workid;
  113. uint8_t temp;
  114. uint8_t fanspeed;
  115. uint8_t errorcount;
  116. uint8_t hashcount[2];
  117. uint8_t maxcount[2];
  118. uint8_t noise;
  119. } WORKSTATUS;
  120. typedef struct _worktask {
  121. uint8_t cmd;
  122. uint8_t dev;
  123. uint8_t workid;
  124. uint8_t midstate[32];
  125. uint8_t merkle[12];
  126. } WORKTASK;
  127. typedef struct _workresult {
  128. uint8_t cmd;
  129. uint8_t dev;
  130. uint8_t workid;
  131. uint8_t nonce[4];
  132. } WORKRESULT;
  133. typedef struct klondike_cfg {
  134. uint8_t cmd;
  135. uint8_t dev;
  136. uint8_t hashclock[2];
  137. uint8_t temptarget;
  138. uint8_t tempcritical;
  139. uint8_t fantarget;
  140. uint8_t pad2;
  141. } WORKCFG;
  142. typedef struct kline {
  143. union {
  144. HEADER hd;
  145. IDENTITY id;
  146. WORKSTATUS ws;
  147. WORKTASK wt;
  148. WORKRESULT wr;
  149. WORKCFG cfg;
  150. };
  151. } KLINE;
  152. #define zero_kline(_kline) memset((void *)(_kline), 0, sizeof(KLINE));
  153. typedef struct device_info {
  154. uint32_t noncecount;
  155. uint32_t nextworkid;
  156. uint16_t lasthashcount;
  157. uint64_t totalhashcount;
  158. uint32_t rangesize;
  159. uint32_t *chipstats;
  160. } DEVINFO;
  161. typedef struct klist {
  162. struct klist *prev;
  163. struct klist *next;
  164. KLINE kline;
  165. struct timeval tv_when;
  166. int block_seq;
  167. bool ready;
  168. bool working;
  169. } KLIST;
  170. typedef struct jobque {
  171. int workqc;
  172. struct timeval last_update;
  173. bool overheat;
  174. bool flushed;
  175. int late_update_count;
  176. int late_update_sequential;
  177. } JOBQUE;
  178. static
  179. struct cgpu_info *klondike_get_proc(struct cgpu_info *cgpu, int procid)
  180. {
  181. while (procid--)
  182. if (cgpu->next_proc)
  183. cgpu = cgpu->next_proc;
  184. return cgpu;
  185. }
  186. static KLIST *new_klist_set(struct cgpu_info *klncgpu)
  187. {
  188. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  189. KLIST *klist = NULL;
  190. int i;
  191. klist = calloc(MAX_KLINES, sizeof(*klist));
  192. if (!klist)
  193. quit(1, "Failed to calloc klist - when old count=%d", klninfo->kline_count);
  194. klninfo->kline_count += MAX_KLINES;
  195. klist[0].prev = NULL;
  196. klist[0].next = &(klist[1]);
  197. for (i = 1; i < MAX_KLINES-1; i++) {
  198. klist[i].prev = &klist[i-1];
  199. klist[i].next = &klist[i+1];
  200. }
  201. klist[MAX_KLINES-1].prev = &(klist[MAX_KLINES-2]);
  202. klist[MAX_KLINES-1].next = NULL;
  203. return klist;
  204. }
  205. static KLIST *allocate_kitem(struct cgpu_info *klncgpu)
  206. {
  207. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  208. KLIST *kitem = NULL;
  209. int ran_out = 0;
  210. char errbuf[1024];
  211. cg_wlock(&klninfo->klist_lock);
  212. if (klninfo->free == NULL) {
  213. ran_out = klninfo->kline_count;
  214. klninfo->free = new_klist_set(klncgpu);
  215. snprintf(errbuf, sizeof(errbuf),
  216. "%s%i: KLINE count exceeded %d, now %d",
  217. klncgpu->drv->name, klncgpu->device_id,
  218. ran_out, klninfo->kline_count);
  219. }
  220. kitem = klninfo->free;
  221. klninfo->free = klninfo->free->next;
  222. if (klninfo->free)
  223. klninfo->free->prev = NULL;
  224. kitem->next = klninfo->used;
  225. kitem->prev = NULL;
  226. if (kitem->next)
  227. kitem->next->prev = kitem;
  228. klninfo->used = kitem;
  229. kitem->ready = false;
  230. kitem->working = false;
  231. memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
  232. klninfo->used_count++;
  233. cg_wunlock(&klninfo->klist_lock);
  234. if (ran_out > 0)
  235. applog(LOG_WARNING, "%s", errbuf);
  236. return kitem;
  237. }
  238. static KLIST *release_kitem(struct cgpu_info *klncgpu, KLIST *kitem)
  239. {
  240. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  241. cg_wlock(&klninfo->klist_lock);
  242. if (kitem == klninfo->used)
  243. klninfo->used = kitem->next;
  244. if (kitem->next)
  245. kitem->next->prev = kitem->prev;
  246. if (kitem->prev)
  247. kitem->prev->next = kitem->next;
  248. kitem->next = klninfo->free;
  249. if (klninfo->free)
  250. klninfo->free->prev = kitem;
  251. kitem->prev = NULL;
  252. klninfo->free = kitem;
  253. klninfo->used_count--;
  254. cg_wunlock(&klninfo->klist_lock);
  255. return NULL;
  256. }
  257. static
  258. int usb_init(struct cgpu_info * const klncgpu, struct libusb_device * const dev)
  259. {
  260. struct klondike_info * const klninfo = klncgpu->device_data;
  261. int e;
  262. if (libusb_open(dev, &klninfo->usbdev_handle) != LIBUSB_SUCCESS)
  263. return 0;
  264. if (LIBUSB_SUCCESS != (e = libusb_set_configuration(klninfo->usbdev_handle, 1)))
  265. {
  266. applog(LOG_DEBUG, "%s: Failed to set configuration 1: %s",
  267. klondike_drv.dname, bfg_strerror(e, BST_LIBUSB));
  268. fail:
  269. libusb_close(klninfo->usbdev_handle);
  270. return 0;
  271. }
  272. if (LIBUSB_SUCCESS != (e = libusb_claim_interface(klninfo->usbdev_handle, 0)))
  273. {
  274. applog(LOG_DEBUG, "%s: Failed to claim interface 0: %s",
  275. klondike_drv.dname, bfg_strerror(e, BST_LIBUSB));
  276. goto fail;
  277. }
  278. return 1;
  279. }
  280. static
  281. int _usb_rw(struct cgpu_info * const klncgpu, void * const buf, const size_t bufsiz, int * const processed, int ep)
  282. {
  283. struct klondike_info * const klninfo = klncgpu->device_data;
  284. const unsigned int timeout = 999;
  285. unsigned char *cbuf = buf;
  286. int err, sent;
  287. *processed = 0;
  288. while (*processed < bufsiz)
  289. {
  290. err = libusb_bulk_transfer(klninfo->usbdev_handle, ep, cbuf, bufsiz, &sent, timeout);
  291. if (unlikely(err))
  292. return err;
  293. *processed += sent;
  294. }
  295. return LIBUSB_SUCCESS;
  296. }
  297. #define usb_read( klncgpu, buf, bufsiz, processed) _usb_rw(klncgpu, buf, bufsiz, processed, 1 | LIBUSB_ENDPOINT_IN)
  298. #define usb_write(klncgpu, buf, bufsiz, processed) _usb_rw(klncgpu, buf, bufsiz, processed, 1 | LIBUSB_ENDPOINT_OUT)
  299. static
  300. void usb_nodev(__maybe_unused struct cgpu_info * const klncgpu)
  301. {
  302. // TODO
  303. }
  304. static
  305. void usb_uninit(struct cgpu_info * const klncgpu)
  306. {
  307. struct klondike_info * const klninfo = klncgpu->device_data;
  308. libusb_release_interface(klninfo->usbdev_handle, 0);
  309. libusb_close(klninfo->usbdev_handle);
  310. }
  311. static double cvtKlnToC(uint8_t temp)
  312. {
  313. double Rt, stein, celsius;
  314. if (temp == 0)
  315. return 0.0;
  316. Rt = 1000.0 * 255.0 / (double)temp - 1000.0;
  317. stein = log(Rt / 2200.0) / 3987.0;
  318. stein += 1.0 / (double)(25.0 + 273.15);
  319. celsius = (1.0 / stein) - 273.15;
  320. // For display of bad data
  321. if (celsius < 0.0)
  322. celsius = 0.0;
  323. if (celsius > 200.0)
  324. celsius = 200.0;
  325. return celsius;
  326. }
  327. static int cvtCToKln(double deg)
  328. {
  329. double Rt, stein, temp;
  330. if (deg < 0.0)
  331. deg = 0.0;
  332. stein = 1.0 / (deg + 273.15);
  333. stein -= 1.0 / (double)(25.0 + 273.15);
  334. Rt = exp(stein * 3987.0) * 2200.0;
  335. if (Rt == -1000.0)
  336. Rt++;
  337. temp = 1000.0 * 256.0 / (Rt + 1000.0);
  338. if (temp > 255)
  339. temp = 255;
  340. if (temp < 0)
  341. temp = 0;
  342. return (int)temp;
  343. }
  344. // Change this to LOG_WARNING if you wish to always see the replies
  345. #define READ_DEBUG LOG_DEBUG
  346. static void display_kline(struct cgpu_info *klncgpu, KLINE *kline, const char *msg)
  347. {
  348. const struct klondike_info * const klninfo = klncgpu->device_data;
  349. switch (kline->hd.cmd) {
  350. case KLN_CMD_NONCE:
  351. applog(READ_DEBUG,
  352. "%s%i:%d %s work [%c] dev=%d workid=%d"
  353. " nonce=0x%08x",
  354. klncgpu->drv->name, klncgpu->device_id,
  355. (int)(kline->wr.dev), msg, kline->wr.cmd,
  356. (int)(kline->wr.dev),
  357. (int)(kline->wr.workid),
  358. (unsigned int)K_NONCE(kline->wr.nonce) + klninfo->nonce_offset);
  359. break;
  360. case KLN_CMD_STATUS:
  361. case KLN_CMD_WORK:
  362. case KLN_CMD_ENABLE:
  363. case KLN_CMD_ABORT:
  364. applog(READ_DEBUG,
  365. "%s%i:%d %s status [%c] dev=%d chips=%d"
  366. " slaves=%d workcq=%d workid=%d temp=%d fan=%d"
  367. " errors=%d hashes=%d max=%d noise=%d",
  368. klncgpu->drv->name, klncgpu->device_id,
  369. (int)(kline->ws.dev), msg, kline->ws.cmd,
  370. (int)(kline->ws.dev),
  371. (int)(kline->ws.chipcount),
  372. (int)(kline->ws.slavecount),
  373. (int)(kline->ws.workqc),
  374. (int)(kline->ws.workid),
  375. (int)(kline->ws.temp),
  376. (int)(kline->ws.fanspeed),
  377. (int)(kline->ws.errorcount),
  378. K_HASHCOUNT(kline->ws.hashcount),
  379. K_MAXCOUNT(kline->ws.maxcount),
  380. (int)(kline->ws.noise));
  381. break;
  382. case KLN_CMD_CONFIG:
  383. applog(READ_DEBUG,
  384. "%s%i:%d %s config [%c] dev=%d clock=%d"
  385. " temptarget=%d tempcrit=%d fan=%d",
  386. klncgpu->drv->name, klncgpu->device_id,
  387. (int)(kline->cfg.dev), msg, kline->cfg.cmd,
  388. (int)(kline->cfg.dev),
  389. K_HASHCLOCK(kline->cfg.hashclock),
  390. (int)(kline->cfg.temptarget),
  391. (int)(kline->cfg.tempcritical),
  392. (int)(kline->cfg.fantarget));
  393. break;
  394. case KLN_CMD_IDENT:
  395. applog(READ_DEBUG,
  396. "%s%i:%d %s info [%c] version=0x%02x prod=%.7s"
  397. " serial=0x%08x",
  398. klncgpu->drv->name, klncgpu->device_id,
  399. (int)(kline->hd.dev), msg, kline->hd.cmd,
  400. (int)(kline->id.version),
  401. kline->id.product,
  402. (unsigned int)K_SERIAL(kline->id.serial));
  403. break;
  404. default:
  405. {
  406. char hexdata[REPLY_SIZE * 2];
  407. bin2hex(hexdata, &kline->hd.dev, REPLY_SIZE - 1);
  408. applog(LOG_ERR,
  409. "%s%i:%d %s [%c:%s] unknown and ignored",
  410. klncgpu->drv->name, klncgpu->device_id,
  411. (int)(kline->hd.dev), msg, kline->hd.cmd,
  412. hexdata);
  413. break;
  414. }
  415. }
  416. }
  417. static void display_send_kline(struct cgpu_info *klncgpu, KLINE *kline, const char *msg)
  418. {
  419. switch (kline->hd.cmd) {
  420. case KLN_CMD_WORK:
  421. applog(READ_DEBUG,
  422. "%s%i:%d %s work [%c] dev=%d workid=0x%02x ...",
  423. klncgpu->drv->name, klncgpu->device_id,
  424. (int)(kline->wt.dev), msg, kline->ws.cmd,
  425. (int)(kline->wt.dev),
  426. (int)(kline->wt.workid));
  427. break;
  428. case KLN_CMD_CONFIG:
  429. applog(READ_DEBUG,
  430. "%s%i:%d %s config [%c] dev=%d clock=%d"
  431. " temptarget=%d tempcrit=%d fan=%d",
  432. klncgpu->drv->name, klncgpu->device_id,
  433. (int)(kline->cfg.dev), msg, kline->cfg.cmd,
  434. (int)(kline->cfg.dev),
  435. K_HASHCLOCK(kline->cfg.hashclock),
  436. (int)(kline->cfg.temptarget),
  437. (int)(kline->cfg.tempcritical),
  438. (int)(kline->cfg.fantarget));
  439. break;
  440. case KLN_CMD_IDENT:
  441. case KLN_CMD_STATUS:
  442. case KLN_CMD_ABORT:
  443. applog(READ_DEBUG,
  444. "%s%i:%d %s cmd [%c]",
  445. klncgpu->drv->name, klncgpu->device_id,
  446. (int)(kline->hd.dev), msg, kline->hd.cmd);
  447. break;
  448. case KLN_CMD_ENABLE:
  449. applog(READ_DEBUG,
  450. "%s%i:%d %s enable [%c] enable=%c",
  451. klncgpu->drv->name, klncgpu->device_id,
  452. (int)(kline->hd.dev), msg, kline->hd.cmd,
  453. (char)(kline->hd.buf[0]));
  454. break;
  455. case KLN_CMD_NONCE:
  456. default:
  457. {
  458. char hexdata[REPLY_SIZE * 2];
  459. bin2hex(hexdata, (unsigned char *)&(kline->hd.dev), REPLY_SIZE - 1);
  460. applog(LOG_ERR,
  461. "%s%i:%d %s [%c:%s] unknown/unexpected and ignored",
  462. klncgpu->drv->name, klncgpu->device_id,
  463. (int)(kline->hd.dev), msg, kline->hd.cmd,
  464. hexdata);
  465. break;
  466. }
  467. }
  468. }
  469. static bool SendCmd(struct cgpu_info *klncgpu, KLINE *kline, int datalen)
  470. {
  471. struct klondike_info * const klninfo = klncgpu->device_data;
  472. int err, amt, writ;
  473. if (klninfo->usbinfo_nodev)
  474. return false;
  475. display_send_kline(klncgpu, kline, msg_send);
  476. writ = KSENDHD(datalen);
  477. err = usb_write(klncgpu, kline, writ, &amt);
  478. if (err < 0 || amt != writ) {
  479. applog(LOG_ERR, "%s%i:%d Cmd:%c Dev:%d, write failed (%d:%d:%d)",
  480. klncgpu->drv->name, klncgpu->device_id,
  481. (int)(kline->hd.dev),
  482. kline->hd.cmd, (int)(kline->hd.dev),
  483. writ, amt, err);
  484. return false;
  485. }
  486. return true;
  487. }
  488. static KLIST *GetReply(struct cgpu_info *klncgpu, uint8_t cmd, uint8_t dev)
  489. {
  490. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  491. KLIST *kitem;
  492. int retries = CMD_REPLY_RETRIES;
  493. while (retries-- > 0 && klncgpu->shutdown == false) {
  494. cgsleep_ms(klninfo->reply_wait_time);
  495. cg_rlock(&klninfo->klist_lock);
  496. kitem = klninfo->used;
  497. while (kitem) {
  498. if (kitem->kline.hd.cmd == cmd &&
  499. kitem->kline.hd.dev == dev &&
  500. kitem->ready == true && kitem->working == false) {
  501. kitem->working = true;
  502. cg_runlock(&klninfo->klist_lock);
  503. return kitem;
  504. }
  505. kitem = kitem->next;
  506. }
  507. cg_runlock(&klninfo->klist_lock);
  508. }
  509. return NULL;
  510. }
  511. static KLIST *SendCmdGetReply(struct cgpu_info *klncgpu, KLINE *kline, int datalen)
  512. {
  513. if (!SendCmd(klncgpu, kline, datalen))
  514. return NULL;
  515. return GetReply(klncgpu, kline->hd.cmd, kline->hd.dev);
  516. }
  517. static bool klondike_get_stats(struct cgpu_info *klncgpu)
  518. {
  519. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  520. KLIST *kitem;
  521. KLINE kline;
  522. int slaves, dev;
  523. uint8_t temp = 0xFF;
  524. if (klninfo->usbinfo_nodev || klninfo->status == NULL)
  525. return false;
  526. applog(LOG_DEBUG, "%s%i: getting status",
  527. klncgpu->drv->name, klncgpu->device_id);
  528. rd_lock(&(klninfo->stat_lock));
  529. slaves = klninfo->status[0].kline.ws.slavecount;
  530. rd_unlock(&(klninfo->stat_lock));
  531. // loop thru devices and get status for each
  532. for (dev = 0; dev <= slaves; dev++) {
  533. zero_kline(&kline);
  534. kline.hd.cmd = KLN_CMD_STATUS;
  535. kline.hd.dev = dev;
  536. kitem = SendCmdGetReply(klncgpu, &kline, 0);
  537. if (kitem != NULL) {
  538. wr_lock(&(klninfo->stat_lock));
  539. memcpy((void *)(&(klninfo->status[dev])),
  540. (void *)kitem,
  541. sizeof(klninfo->status[dev]));
  542. wr_unlock(&(klninfo->stat_lock));
  543. kitem = release_kitem(klncgpu, kitem);
  544. } else {
  545. applog(LOG_ERR, "%s%i:%d failed to update stats",
  546. klncgpu->drv->name, klncgpu->device_id, dev);
  547. }
  548. if (klninfo->status[dev].kline.ws.temp < temp)
  549. temp = klninfo->status[dev].kline.ws.temp;
  550. }
  551. klncgpu->temp = cvtKlnToC(temp);
  552. return true;
  553. }
  554. static bool kln_enable(struct cgpu_info *klncgpu)
  555. {
  556. struct klondike_info * const klninfo = klncgpu->device_data;
  557. KLIST *kitem;
  558. KLINE kline;
  559. const int slaves = klninfo->status[0].kline.ws.slavecount;
  560. zero_kline(&kline);
  561. kline.hd.cmd = KLN_CMD_ENABLE;
  562. kline.hd.buf[0] = KLN_CMD_ENABLE_ON;
  563. for (int dev = 0; dev <= slaves; ++dev)
  564. {
  565. kline.hd.dev = dev;
  566. for (int tries = 3; ; --tries)
  567. {
  568. kitem = SendCmdGetReply(klncgpu, &kline, 1);
  569. cgsleep_ms(50);
  570. if (kitem)
  571. {
  572. kitem = release_kitem(klncgpu, kitem);
  573. break;
  574. }
  575. if (tries == 1)
  576. return false;
  577. }
  578. }
  579. return true;
  580. }
  581. static void kln_disable(struct cgpu_info *klncgpu, int dev, bool all)
  582. {
  583. KLINE kline;
  584. int i;
  585. zero_kline(&kline);
  586. kline.hd.cmd = KLN_CMD_ENABLE;
  587. kline.hd.buf[0] = KLN_CMD_ENABLE_OFF;
  588. for (i = (all ? 0 : dev); i <= dev; i++) {
  589. kline.hd.dev = i;
  590. SendCmd(klncgpu, &kline, KSENDHD(1));
  591. }
  592. }
  593. static
  594. void klondike_zero_stats(struct cgpu_info * const proc)
  595. {
  596. struct klondike_info * const klninfo = proc->device_data;
  597. for (int devn = klninfo->status[0].kline.ws.slavecount; devn >= 0; --devn)
  598. for (int i = klninfo->status[devn].kline.ws.chipcount * 2; --i >= 0; )
  599. klninfo->devinfo[devn].chipstats[i] = 0;
  600. klninfo->hashcount = klninfo->errorcount = klninfo->noisecount = 0;
  601. klninfo->delay_count = klninfo->delay_total = klninfo->delay_min = klninfo->delay_max = 0;
  602. klninfo->nonce_count = klninfo->nonce_total = klninfo->nonce_min = klninfo->nonce_max = 0;
  603. }
  604. static bool klondike_init(struct cgpu_info *klncgpu)
  605. {
  606. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  607. KLIST *kitem;
  608. KLINE kline;
  609. int slaves, dev;
  610. klninfo->initialised = false;
  611. cgpu_set_defaults(klncgpu);
  612. zero_kline(&kline);
  613. kline.hd.cmd = KLN_CMD_STATUS;
  614. kline.hd.dev = 0;
  615. kitem = SendCmdGetReply(klncgpu, &kline, 0);
  616. if (kitem == NULL)
  617. return false;
  618. slaves = kitem->kline.ws.slavecount;
  619. if (klninfo->status == NULL) {
  620. applog(LOG_DEBUG, "%s%i: initializing data",
  621. klncgpu->drv->name, klncgpu->device_id);
  622. // alloc space for status, devinfo, cfg and jobque for master and slaves
  623. klninfo->status = calloc(slaves+1, sizeof(*(klninfo->status)));
  624. if (unlikely(!klninfo->status))
  625. quit(1, "Failed to calloc status array in klondke_get_stats");
  626. klninfo->devinfo = calloc(slaves+1, sizeof(*(klninfo->devinfo)));
  627. if (unlikely(!klninfo->devinfo))
  628. quit(1, "Failed to calloc devinfo array in klondke_get_stats");
  629. klninfo->cfg = calloc(slaves+1, sizeof(*(klninfo->cfg)));
  630. if (unlikely(!klninfo->cfg))
  631. quit(1, "Failed to calloc cfg array in klondke_get_stats");
  632. klninfo->jobque = calloc(slaves+1, sizeof(*(klninfo->jobque)));
  633. if (unlikely(!klninfo->jobque))
  634. quit(1, "Failed to calloc jobque array in klondke_get_stats");
  635. }
  636. memcpy((void *)(&(klninfo->status[0])), (void *)kitem, sizeof(klninfo->status[0]));
  637. kitem = release_kitem(klncgpu, kitem);
  638. // zero init triggers read back only
  639. zero_kline(&kline);
  640. kline.cfg.cmd = KLN_CMD_CONFIG;
  641. int size = 2;
  642. // boundaries are checked by device, with valid values returned
  643. {
  644. SET_HASHCLOCK(kline.cfg.hashclock, klninfo->clock);
  645. kline.cfg.temptarget = cvtCToKln(klncgpu->targettemp);
  646. kline.cfg.tempcritical = 0; // hard code for old firmware
  647. kline.cfg.fantarget = 0xff; // hard code for old firmware
  648. size = sizeof(kline.cfg) - 2;
  649. }
  650. for (dev = 0; dev <= slaves; dev++) {
  651. kline.cfg.dev = dev;
  652. kitem = SendCmdGetReply(klncgpu, &kline, size);
  653. if (kitem != NULL) {
  654. memcpy((void *)&(klninfo->cfg[dev]), kitem, sizeof(klninfo->cfg[dev]));
  655. applog(LOG_WARNING, "%s%i:%d config (%d: Clk: %d, T:%.0lf, C:%.0lf, F:%d)",
  656. klncgpu->drv->name, klncgpu->device_id, dev,
  657. dev, K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock),
  658. cvtKlnToC(klninfo->cfg[dev].kline.cfg.temptarget),
  659. cvtKlnToC(klninfo->cfg[dev].kline.cfg.tempcritical),
  660. (int)100*klninfo->cfg[dev].kline.cfg.fantarget/256);
  661. kitem = release_kitem(klncgpu, kitem);
  662. }
  663. }
  664. klondike_get_stats(klncgpu);
  665. klninfo->initialised = true;
  666. for (dev = 0; dev <= slaves; dev++) {
  667. klninfo->devinfo[dev].rangesize = ((uint64_t)1<<32) / klninfo->status[dev].kline.ws.chipcount;
  668. klninfo->devinfo[dev].chipstats = calloc(klninfo->status[dev].kline.ws.chipcount*2 , sizeof(uint32_t));
  669. }
  670. bool ok = kln_enable(klncgpu);
  671. if (!ok)
  672. applog(LOG_ERR, "%s%i: failed to enable", klncgpu->drv->name, klncgpu->device_id);
  673. return ok;
  674. }
  675. static void control_init(struct cgpu_info *klncgpu)
  676. {
  677. struct klondike_info * const klninfo = klncgpu->device_data;
  678. int err, interface;
  679. if (klninfo->usbinfo_nodev)
  680. return;
  681. interface = 0;
  682. err = libusb_control_transfer(klninfo->usbdev_handle, 0, 9, 1, interface, NULL, 0, 999);
  683. applog(LOG_DEBUG, "%s%i: reset got err %d",
  684. klncgpu->drv->name, klncgpu->device_id, err);
  685. }
  686. static
  687. const char *klondike_set_clock(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  688. {
  689. struct klondike_info * const klninfo = proc->device_data;
  690. if (klninfo->initialised)
  691. return "Cannot change clock after initialisation";
  692. klninfo->clock = atoi(newvalue);
  693. return NULL;
  694. }
  695. static
  696. const char *klondike_set_max_work_count(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  697. {
  698. struct klondike_info * const klninfo = proc->device_data;
  699. klninfo->max_work_count = atoi(newvalue);
  700. return NULL;
  701. }
  702. static
  703. const char *klondike_set_old_work_time(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  704. {
  705. struct klondike_info * const klninfo = proc->device_data;
  706. klninfo->old_work_ms = atof(newvalue) * 1000.0;
  707. return NULL;
  708. }
  709. static
  710. const char *klondike_set_reply_wait_time(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  711. {
  712. struct klondike_info * const klninfo = proc->device_data;
  713. klninfo->reply_wait_time = atoi(newvalue);
  714. return NULL;
  715. }
  716. static const struct bfg_set_device_definition klondike_set_device_funcs[] = {
  717. {"clock", klondike_set_clock, "clock frequency (can only be set at startup, with --set-device)"},
  718. {"max_work_count", klondike_set_max_work_count, "number of work items to queue on each bus"},
  719. {"old_work_time", klondike_set_old_work_time, "number of seconds to retain work"},
  720. {"reply_wait_time", klondike_set_reply_wait_time, "number of seconds poll interval"},
  721. {NULL}
  722. };
  723. static
  724. bool klondike_lowl_match(const struct lowlevel_device_info * const info)
  725. {
  726. if (!lowlevel_match_id(info, &lowl_usb, 0x04d8, 0xf60a))
  727. return false;
  728. return (info->manufacturer && strstr(info->manufacturer, "Klondike"));
  729. }
  730. bool klondike_lowl_probe_custom(const struct lowlevel_device_info * const info, struct device_drv * const drv, struct klondike_info * const klninfo)
  731. {
  732. if (unlikely(info->lowl != &lowl_usb))
  733. {
  734. bfg_probe_result_flags = BPR_WRONG_DEVTYPE;
  735. applog(LOG_DEBUG, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not usb!",
  736. __func__, info->product, info->serial);
  737. goto err;
  738. }
  739. struct libusb_device * const dev = info->lowl_data;
  740. if (bfg_claim_libusb(drv, true, dev))
  741. goto err;
  742. // static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  743. struct cgpu_info * const klncgpu = malloc(sizeof(*klncgpu));
  744. KLINE kline;
  745. if (unlikely(!klncgpu))
  746. quit(1, "Failed to calloc klncgpu in klondike_detect_one");
  747. *klncgpu = (struct cgpu_info){
  748. .drv = drv,
  749. .deven = DEV_ENABLED,
  750. .threads = 1,
  751. .targettemp = 50,
  752. .cutofftemp = (int)KLN_KILLWORK_TEMP,
  753. .set_device_funcs = klondike_set_device_funcs,
  754. };
  755. klncgpu->device_data = (void *)klninfo;
  756. klninfo->free = new_klist_set(klncgpu);
  757. if (usb_init(klncgpu, dev)) {
  758. int sent, recd, err;
  759. KLIST kitem;
  760. int attempts = 0;
  761. klncgpu->device_path = strdup(info->devid);
  762. control_init(klncgpu);
  763. while (attempts++ < 3) {
  764. kline.hd.cmd = KLN_CMD_IDENT;
  765. kline.hd.dev = 0;
  766. display_send_kline(klncgpu, &kline, msg_detect_send);
  767. err = usb_write(klncgpu, (char *)&(kline.hd), 2, &sent);
  768. if (err < 0 || sent != 2) {
  769. applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)",
  770. klncgpu->drv->dname,
  771. klncgpu->device_path,
  772. sent, err);
  773. }
  774. cgsleep_ms(klninfo->reply_wait_time * 10);
  775. err = usb_read(klncgpu, &kitem.kline, REPLY_SIZE, &recd);
  776. if (err < 0) {
  777. applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)",
  778. klncgpu->drv->dname,
  779. klncgpu->device_path,
  780. recd, err);
  781. } else if (recd < 1) {
  782. applog(LOG_ERR, "%s (%s) detect empty reply (%d)",
  783. klncgpu->drv->dname,
  784. klncgpu->device_path,
  785. recd);
  786. } else if (kitem.kline.hd.cmd == KLN_CMD_IDENT && kitem.kline.hd.dev == 0) {
  787. display_kline(klncgpu, &kitem.kline, msg_detect_reply);
  788. applog(LOG_DEBUG, "%s (%s) detect successful (%d attempt%s)",
  789. klncgpu->drv->dname,
  790. klncgpu->device_path,
  791. attempts, attempts == 1 ? "" : "s");
  792. kline.hd.cmd = KLN_CMD_STATUS;
  793. if (!SendCmd(klncgpu, &kline, 0))
  794. {
  795. applog(LOG_DEBUG, "%s (%s) status request failed",
  796. klncgpu->drv->dname, klncgpu->device_path);
  797. continue;
  798. }
  799. cgsleep_ms(klninfo->reply_wait_time * 10);
  800. err = usb_read(klncgpu, &kitem.kline, REPLY_SIZE, &recd);
  801. if (err < 0 || recd < REPLY_SIZE)
  802. {
  803. applog(LOG_DEBUG, "%s (%s) status request failed (2)",
  804. klncgpu->drv->dname, klncgpu->device_path);
  805. continue;
  806. }
  807. klncgpu->procs = 1 + kitem.kline.ws.slavecount;
  808. if (!add_cgpu(klncgpu))
  809. break;
  810. applog(LOG_DEBUG, "Klondike cgpu added");
  811. rwlock_init(&klninfo->stat_lock);
  812. cglock_init(&klninfo->klist_lock);
  813. return true;
  814. }
  815. }
  816. usb_uninit(klncgpu);
  817. }
  818. free(klninfo->free);
  819. free(klncgpu);
  820. err:
  821. free(klninfo);
  822. return false;
  823. }
  824. static
  825. bool klondike_lowl_probe(const struct lowlevel_device_info * const info)
  826. {
  827. struct klondike_info * const klninfo = malloc(sizeof(*klninfo));
  828. if (unlikely(!klninfo))
  829. applogr(false, LOG_ERR, "%s: Failed to malloc klninfo", __func__);
  830. *klninfo = (struct klondike_info){
  831. .clock = 282,
  832. .max_work_count = 4,
  833. .old_work_ms = 5000,
  834. .reply_wait_time = 100,
  835. };
  836. return klondike_lowl_probe_custom(info, &klondike_drv, klninfo);
  837. }
  838. static void klondike_check_nonce(struct cgpu_info *klncgpu, KLIST *kitem)
  839. {
  840. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  841. struct work *work, *look, *tmp;
  842. KLINE *kline = &(kitem->kline);
  843. struct cgpu_info * const proc = klondike_get_proc(klncgpu, kline->wr.dev);
  844. struct thr_info * const thr = proc->thr[0];
  845. struct timeval tv_now;
  846. double us_diff;
  847. uint32_t nonce = K_NONCE(kline->wr.nonce) + klninfo->nonce_offset;
  848. applog(LOG_DEBUG, "%"PRIpreprv": FOUND NONCE (%02x:%08x)",
  849. proc->proc_repr,
  850. kline->wr.workid, (unsigned int)nonce);
  851. work = NULL;
  852. cgtime(&tv_now);
  853. rd_lock(&(klncgpu->qlock));
  854. HASH_ITER(hh, klncgpu->queued_work, look, tmp) {
  855. if (ms_tdiff(&tv_now, &(look->tv_stamp)) < klninfo->old_work_ms &&
  856. (look->subid == (kline->wr.dev*256 + kline->wr.workid))) {
  857. work = look;
  858. break;
  859. }
  860. }
  861. rd_unlock(&(klncgpu->qlock));
  862. if (work) {
  863. if (unlikely(!klninfo->nonce_offset))
  864. {
  865. bool test_c0 = test_nonce(work, nonce - 0xc0, false);
  866. bool test_180 = test_nonce(work, nonce - 0x180, false);
  867. if (test_c0)
  868. {
  869. if (unlikely(test_180))
  870. {
  871. applog(LOG_DEBUG, "%s: Matched both c0 and 180 offsets (%02x:%08lx)",
  872. klncgpu->dev_repr, kline->wr.workid, (unsigned long)nonce);
  873. submit_nonce(thr, work, nonce - 0x180);
  874. nonce -= 0xc0;
  875. }
  876. else
  877. {
  878. applog(LOG_DEBUG, "%s: Matched c0 offset (%02x:%08lx)",
  879. klncgpu->dev_repr, kline->wr.workid, (unsigned long)nonce);
  880. nonce += (klninfo->nonce_offset = -0xc0);
  881. }
  882. }
  883. else
  884. if (test_180)
  885. {
  886. applog(LOG_DEBUG, "%s: Matched 180 offset (%02x:%08lx)",
  887. klncgpu->dev_repr, kline->wr.workid, (unsigned long)nonce);
  888. nonce += (klninfo->nonce_offset = -0x180);
  889. }
  890. else
  891. applog(LOG_DEBUG, "%s: Matched neither c0 nor 180 offset (%02x:%08lx)",
  892. klncgpu->dev_repr, kline->wr.workid, (unsigned long)nonce);
  893. }
  894. wr_lock(&(klninfo->stat_lock));
  895. klninfo->devinfo[kline->wr.dev].noncecount++;
  896. klninfo->noncecount++;
  897. wr_unlock(&(klninfo->stat_lock));
  898. applog(LOG_DEBUG, "%"PRIpreprv": SUBMIT NONCE (%02x:%08x)",
  899. proc->proc_repr,
  900. kline->wr.workid, (unsigned int)nonce);
  901. cgtime(&tv_now);
  902. bool ok = submit_nonce(thr, work, nonce);
  903. applog(LOG_DEBUG, "%"PRIpreprv": chip stats %d, %08x, %d, %d",
  904. proc->proc_repr,
  905. kline->wr.dev, (unsigned int)nonce,
  906. klninfo->devinfo[kline->wr.dev].rangesize,
  907. klninfo->status[kline->wr.dev].kline.ws.chipcount);
  908. klninfo->devinfo[kline->wr.dev].chipstats[(nonce / klninfo->devinfo[kline->wr.dev].rangesize) + (ok ? 0 : klninfo->status[kline->wr.dev].kline.ws.chipcount)]++;
  909. us_diff = us_tdiff(&tv_now, &(kitem->tv_when));
  910. if (klninfo->delay_count == 0) {
  911. klninfo->delay_min = us_diff;
  912. klninfo->delay_max = us_diff;
  913. } else {
  914. if (klninfo->delay_min > us_diff)
  915. klninfo->delay_min = us_diff;
  916. if (klninfo->delay_max < us_diff)
  917. klninfo->delay_max = us_diff;
  918. }
  919. klninfo->delay_count++;
  920. klninfo->delay_total += us_diff;
  921. if (klninfo->nonce_count > 0) {
  922. us_diff = us_tdiff(&(kitem->tv_when), &(klninfo->tv_last_nonce_received));
  923. if (klninfo->nonce_count == 1) {
  924. klninfo->nonce_min = us_diff;
  925. klninfo->nonce_max = us_diff;
  926. } else {
  927. if (klninfo->nonce_min > us_diff)
  928. klninfo->nonce_min = us_diff;
  929. if (klninfo->nonce_max < us_diff)
  930. klninfo->nonce_max = us_diff;
  931. }
  932. klninfo->nonce_total += us_diff;
  933. }
  934. klninfo->nonce_count++;
  935. hashes_done2(thr, 0x100000000, NULL);
  936. memcpy(&(klninfo->tv_last_nonce_received), &(kitem->tv_when),
  937. sizeof(klninfo->tv_last_nonce_received));
  938. return;
  939. }
  940. applog(LOG_ERR, "%"PRIpreprv": unknown work (%02x:%08x) - ignored",
  941. proc->proc_repr,
  942. kline->wr.workid, (unsigned int)nonce);
  943. inc_hw_errors2(thr, NULL, &nonce);
  944. }
  945. // thread to keep looking for replies
  946. static void *klondike_get_replies(void *userdata)
  947. {
  948. struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
  949. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  950. KLIST *kitem = NULL;
  951. int err, recd, slaves, dev, isc;
  952. bool overheat, sent;
  953. applog(LOG_DEBUG, "%s%i: listening for replies",
  954. klncgpu->drv->name, klncgpu->device_id);
  955. while (klncgpu->shutdown == false) {
  956. if (klninfo->usbinfo_nodev)
  957. return NULL;
  958. if (kitem == NULL)
  959. kitem = allocate_kitem(klncgpu);
  960. else
  961. memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
  962. err = usb_read(klncgpu, &kitem->kline, REPLY_SIZE, &recd);
  963. if (err || recd != REPLY_SIZE) {
  964. if (err != -7)
  965. applog(LOG_ERR, "%s%i: reply err=%d amt=%d",
  966. klncgpu->drv->name, klncgpu->device_id,
  967. err, recd);
  968. }
  969. if (!err && recd == REPLY_SIZE) {
  970. cgtime(&(kitem->tv_when));
  971. rd_lock(&(klninfo->stat_lock));
  972. kitem->block_seq = klninfo->block_seq;
  973. rd_unlock(&(klninfo->stat_lock));
  974. if (opt_log_level <= READ_DEBUG) {
  975. char hexdata[recd * 2];
  976. bin2hex(hexdata, &kitem->kline.hd.dev, recd-1);
  977. applog(READ_DEBUG, "%s%i:%d reply [%c:%s]",
  978. klncgpu->drv->name, klncgpu->device_id,
  979. (int)(kitem->kline.hd.dev),
  980. kitem->kline.hd.cmd, hexdata);
  981. }
  982. // We can't check this until it's initialised
  983. if (klninfo->initialised) {
  984. rd_lock(&(klninfo->stat_lock));
  985. slaves = klninfo->status[0].kline.ws.slavecount;
  986. rd_unlock(&(klninfo->stat_lock));
  987. if (kitem->kline.hd.dev > slaves) {
  988. applog(LOG_ERR, "%s%i: reply [%c] has invalid dev=%d (max=%d) using 0",
  989. klncgpu->drv->name, klncgpu->device_id,
  990. (char)(kitem->kline.hd.cmd),
  991. (int)(kitem->kline.hd.dev),
  992. slaves);
  993. /* TODO: this is rather problematic if there are slaves
  994. * however without slaves - it should always be zero */
  995. kitem->kline.hd.dev = 0;
  996. } else {
  997. wr_lock(&(klninfo->stat_lock));
  998. klninfo->jobque[kitem->kline.hd.dev].late_update_sequential = 0;
  999. wr_unlock(&(klninfo->stat_lock));
  1000. }
  1001. }
  1002. switch (kitem->kline.hd.cmd) {
  1003. case KLN_CMD_NONCE:
  1004. klondike_check_nonce(klncgpu, kitem);
  1005. display_kline(klncgpu, &kitem->kline, msg_reply);
  1006. break;
  1007. case KLN_CMD_WORK:
  1008. // We can't do/check this until it's initialised
  1009. if (klninfo->initialised) {
  1010. dev = kitem->kline.ws.dev;
  1011. if (kitem->kline.ws.workqc == 0) {
  1012. bool idle = false;
  1013. rd_lock(&(klninfo->stat_lock));
  1014. if (klninfo->jobque[dev].flushed == false)
  1015. idle = true;
  1016. slaves = klninfo->status[0].kline.ws.slavecount;
  1017. rd_unlock(&(klninfo->stat_lock));
  1018. if (idle)
  1019. applog(LOG_WARNING, "%s%i:%d went idle before work was sent",
  1020. klncgpu->drv->name,
  1021. klncgpu->device_id,
  1022. dev);
  1023. }
  1024. wr_lock(&(klninfo->stat_lock));
  1025. klninfo->jobque[dev].flushed = false;
  1026. wr_unlock(&(klninfo->stat_lock));
  1027. }
  1028. case KLN_CMD_STATUS:
  1029. case KLN_CMD_ABORT:
  1030. // We can't do/check this until it's initialised
  1031. if (klninfo->initialised) {
  1032. isc = 0;
  1033. dev = kitem->kline.ws.dev;
  1034. wr_lock(&(klninfo->stat_lock));
  1035. klninfo->jobque[dev].workqc = (int)(kitem->kline.ws.workqc);
  1036. cgtime(&(klninfo->jobque[dev].last_update));
  1037. slaves = klninfo->status[0].kline.ws.slavecount;
  1038. overheat = klninfo->jobque[dev].overheat;
  1039. if (dev == 0) {
  1040. if (kitem->kline.ws.slavecount != slaves)
  1041. isc = ++klninfo->incorrect_slave_sequential;
  1042. else
  1043. isc = klninfo->incorrect_slave_sequential = 0;
  1044. }
  1045. wr_unlock(&(klninfo->stat_lock));
  1046. if (isc) {
  1047. applog(LOG_ERR, "%s%i:%d reply [%c] has a diff"
  1048. " # of slaves=%d (curr=%d)%s",
  1049. klncgpu->drv->name,
  1050. klncgpu->device_id,
  1051. dev,
  1052. (char)(kitem->kline.ws.cmd),
  1053. (int)(kitem->kline.ws.slavecount),
  1054. slaves,
  1055. isc <= KLN_ISS_IGNORE ? "" :
  1056. " disabling device");
  1057. if (isc > KLN_ISS_IGNORE)
  1058. usb_nodev(klncgpu);
  1059. break;
  1060. }
  1061. if (!overheat) {
  1062. double temp = cvtKlnToC(kitem->kline.ws.temp);
  1063. if (temp >= KLN_KILLWORK_TEMP) {
  1064. KLINE kline;
  1065. wr_lock(&(klninfo->stat_lock));
  1066. klninfo->jobque[dev].overheat = true;
  1067. wr_unlock(&(klninfo->stat_lock));
  1068. applog(LOG_WARNING, "%s%i:%d Critical overheat (%.0fC)",
  1069. klncgpu->drv->name,
  1070. klncgpu->device_id,
  1071. dev, temp);
  1072. zero_kline(&kline);
  1073. kline.hd.cmd = KLN_CMD_ABORT;
  1074. kline.hd.dev = dev;
  1075. sent = SendCmd(klncgpu, &kline, KSENDHD(0));
  1076. kln_disable(klncgpu, dev, false);
  1077. if (!sent) {
  1078. applog(LOG_ERR, "%s%i:%d overheat failed to"
  1079. " abort work - disabling device",
  1080. klncgpu->drv->name,
  1081. klncgpu->device_id,
  1082. dev);
  1083. usb_nodev(klncgpu);
  1084. }
  1085. }
  1086. }
  1087. }
  1088. case KLN_CMD_ENABLE:
  1089. wr_lock(&(klninfo->stat_lock));
  1090. klninfo->errorcount += kitem->kline.ws.errorcount;
  1091. klninfo->noisecount += kitem->kline.ws.noise;
  1092. wr_unlock(&(klninfo->stat_lock));
  1093. display_kline(klncgpu, &kitem->kline, msg_reply);
  1094. kitem->ready = true;
  1095. kitem = NULL;
  1096. break;
  1097. case KLN_CMD_CONFIG:
  1098. display_kline(klncgpu, &kitem->kline, msg_reply);
  1099. kitem->ready = true;
  1100. kitem = NULL;
  1101. break;
  1102. case KLN_CMD_IDENT:
  1103. display_kline(klncgpu, &kitem->kline, msg_reply);
  1104. kitem->ready = true;
  1105. kitem = NULL;
  1106. break;
  1107. default:
  1108. display_kline(klncgpu, &kitem->kline, msg_reply);
  1109. break;
  1110. }
  1111. }
  1112. }
  1113. return NULL;
  1114. }
  1115. static void klondike_flush_work(struct cgpu_info *klncgpu)
  1116. {
  1117. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1118. KLIST *kitem;
  1119. KLINE kline;
  1120. int slaves, dev;
  1121. if (klninfo->initialised) {
  1122. wr_lock(&(klninfo->stat_lock));
  1123. klninfo->block_seq++;
  1124. slaves = klninfo->status[0].kline.ws.slavecount;
  1125. wr_unlock(&(klninfo->stat_lock));
  1126. applog(LOG_DEBUG, "%s%i: flushing work",
  1127. klncgpu->drv->name, klncgpu->device_id);
  1128. zero_kline(&kline);
  1129. kline.hd.cmd = KLN_CMD_ABORT;
  1130. for (dev = 0; dev <= slaves; dev++) {
  1131. kline.hd.dev = dev;
  1132. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  1133. if (kitem != NULL) {
  1134. wr_lock(&(klninfo->stat_lock));
  1135. memcpy((void *)&(klninfo->status[dev]),
  1136. kitem,
  1137. sizeof(klninfo->status[dev]));
  1138. klninfo->jobque[dev].flushed = true;
  1139. wr_unlock(&(klninfo->stat_lock));
  1140. kitem = release_kitem(klncgpu, kitem);
  1141. }
  1142. }
  1143. }
  1144. }
  1145. static bool klondike_thread_prepare(struct thr_info *thr)
  1146. {
  1147. struct cgpu_info *klncgpu = thr->cgpu;
  1148. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1149. if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) {
  1150. applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id);
  1151. return false;
  1152. }
  1153. pthread_detach(klninfo->replies_thr.pth);
  1154. // let the listening get started
  1155. cgsleep_ms(100);
  1156. return klondike_init(klncgpu);
  1157. }
  1158. static bool klondike_thread_init(struct thr_info *thr)
  1159. {
  1160. struct cgpu_info *klncgpu = thr->cgpu;
  1161. struct klondike_info * const klninfo = klncgpu->device_data;
  1162. notifier_init(thr->work_restart_notifier);
  1163. if (klninfo->usbinfo_nodev)
  1164. return false;
  1165. klondike_flush_work(klncgpu);
  1166. return true;
  1167. }
  1168. static void klondike_shutdown(struct thr_info *thr)
  1169. {
  1170. struct cgpu_info *klncgpu = thr->cgpu;
  1171. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1172. applog(LOG_DEBUG, "%s%i: shutting down work",
  1173. klncgpu->drv->name, klncgpu->device_id);
  1174. kln_disable(klncgpu, klninfo->status[0].kline.ws.slavecount, true);
  1175. klncgpu->shutdown = true;
  1176. }
  1177. static void klondike_thread_enable(struct thr_info *thr)
  1178. {
  1179. struct cgpu_info *klncgpu = thr->cgpu;
  1180. struct klondike_info * const klninfo = klncgpu->device_data;
  1181. if (klninfo->usbinfo_nodev)
  1182. return;
  1183. /*
  1184. KLINE kline;
  1185. zero_kline(&kline);
  1186. kline.hd.cmd = KLN_CMD_ENABLE;
  1187. kline.hd.dev = dev;
  1188. kline.hd.buf[0] = KLN_CMD_ENABLE_OFF;
  1189. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
  1190. */
  1191. }
  1192. static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
  1193. {
  1194. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1195. struct work *look, *tmp;
  1196. KLINE kline;
  1197. struct timeval tv_old;
  1198. int wque_size, wque_cleared;
  1199. if (klninfo->usbinfo_nodev)
  1200. return false;
  1201. zero_kline(&kline);
  1202. kline.wt.cmd = KLN_CMD_WORK;
  1203. kline.wt.dev = dev;
  1204. memcpy(kline.wt.midstate, work->midstate, MIDSTATE_BYTES);
  1205. memcpy(kline.wt.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  1206. kline.wt.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
  1207. work->subid = dev*256 + kline.wt.workid;
  1208. cgtime(&work->tv_stamp);
  1209. if (opt_log_level <= LOG_DEBUG) {
  1210. char hexdata[(sizeof(kline.wt) * 2) + 1];
  1211. bin2hex(hexdata, &kline.wt, sizeof(kline.wt));
  1212. applog(LOG_DEBUG, "WORKDATA: %s", hexdata);
  1213. }
  1214. applog(LOG_DEBUG, "%s%i:%d sending work (%d:%02x)",
  1215. klncgpu->drv->name, klncgpu->device_id, dev,
  1216. dev, kline.wt.workid);
  1217. KLIST *kitem = SendCmdGetReply(klncgpu, &kline, sizeof(kline.wt));
  1218. if (kitem != NULL) {
  1219. wr_lock(&(klninfo->stat_lock));
  1220. memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(klninfo->status[dev]));
  1221. wr_unlock(&(klninfo->stat_lock));
  1222. kitem = release_kitem(klncgpu, kitem);
  1223. // remove old work
  1224. wque_size = 0;
  1225. wque_cleared = 0;
  1226. cgtime(&tv_old);
  1227. wr_lock(&klncgpu->qlock);
  1228. HASH_ITER(hh, klncgpu->queued_work, look, tmp) {
  1229. if (ms_tdiff(&tv_old, &(look->tv_stamp)) > klninfo->old_work_ms) {
  1230. __work_completed(klncgpu, look);
  1231. free_work(look);
  1232. wque_cleared++;
  1233. } else
  1234. wque_size++;
  1235. }
  1236. wr_unlock(&klncgpu->qlock);
  1237. wr_lock(&(klninfo->stat_lock));
  1238. klninfo->wque_size = wque_size;
  1239. klninfo->wque_cleared = wque_cleared;
  1240. wr_unlock(&(klninfo->stat_lock));
  1241. return true;
  1242. }
  1243. return false;
  1244. }
  1245. static bool klondike_queue_full(struct cgpu_info *klncgpu)
  1246. {
  1247. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1248. struct work *work = NULL;
  1249. int dev, queued, slaves, seq, howlong;
  1250. struct timeval now;
  1251. bool nowork;
  1252. if (klncgpu->shutdown == true)
  1253. return true;
  1254. cgtime(&now);
  1255. rd_lock(&(klninfo->stat_lock));
  1256. slaves = klninfo->status[0].kline.ws.slavecount;
  1257. for (dev = 0; dev <= slaves; dev++)
  1258. if (ms_tdiff(&now, &(klninfo->jobque[dev].last_update)) > LATE_UPDATE_MS) {
  1259. klninfo->jobque[dev].late_update_count++;
  1260. seq = ++klninfo->jobque[dev].late_update_sequential;
  1261. rd_unlock(&(klninfo->stat_lock));
  1262. if (seq < LATE_UPDATE_LIMIT) {
  1263. applog(LOG_DEBUG, "%s%i:%d late update",
  1264. klncgpu->drv->name, klncgpu->device_id, dev);
  1265. klondike_get_stats(klncgpu);
  1266. goto que;
  1267. } else {
  1268. applog(LOG_WARNING, "%s%i:%d late update (%d) reached - attempting reset",
  1269. klncgpu->drv->name, klncgpu->device_id,
  1270. dev, LATE_UPDATE_LIMIT);
  1271. control_init(klncgpu);
  1272. kln_enable(klncgpu);
  1273. klondike_get_stats(klncgpu);
  1274. rd_lock(&(klninfo->stat_lock));
  1275. howlong = ms_tdiff(&now, &(klninfo->jobque[dev].last_update));
  1276. if (howlong > LATE_UPDATE_MS) {
  1277. rd_unlock(&(klninfo->stat_lock));
  1278. if (howlong > LATE_UPDATE_NODEV_MS) {
  1279. applog(LOG_ERR, "%s%i:%d reset failed - dropping device",
  1280. klncgpu->drv->name, klncgpu->device_id, dev);
  1281. usb_nodev(klncgpu);
  1282. } else
  1283. cgsleep_ms(LATE_UPDATE_SLEEP_MS);
  1284. return true;
  1285. }
  1286. break;
  1287. }
  1288. }
  1289. rd_unlock(&(klninfo->stat_lock));
  1290. que:
  1291. nowork = true;
  1292. for (queued = 0; queued < klninfo->max_work_count - 1; ++queued)
  1293. for (dev = 0; dev <= slaves; dev++) {
  1294. tryagain:
  1295. rd_lock(&(klninfo->stat_lock));
  1296. if (klninfo->jobque[dev].overheat) {
  1297. double temp = cvtKlnToC(klninfo->status[0].kline.ws.temp);
  1298. if ((queued == klninfo->max_work_count - 2) &&
  1299. ms_tdiff(&now, &(klninfo->jobque[dev].last_update)) > (LATE_UPDATE_MS/2)) {
  1300. rd_unlock(&(klninfo->stat_lock));
  1301. klondike_get_stats(klncgpu);
  1302. goto tryagain;
  1303. }
  1304. if (temp <= KLN_COOLED_DOWN) {
  1305. klninfo->jobque[dev].overheat = false;
  1306. rd_unlock(&(klninfo->stat_lock));
  1307. applog(LOG_WARNING, "%s%i:%d Overheat recovered (%.0fC)",
  1308. klncgpu->drv->name, klncgpu->device_id,
  1309. dev, temp);
  1310. kln_enable(klncgpu);
  1311. goto tryagain;
  1312. } else {
  1313. rd_unlock(&(klninfo->stat_lock));
  1314. continue;
  1315. }
  1316. }
  1317. if (klninfo->jobque[dev].workqc <= queued) {
  1318. rd_unlock(&(klninfo->stat_lock));
  1319. if (!work)
  1320. work = get_queued(klncgpu);
  1321. if (unlikely(!work))
  1322. return false;
  1323. nowork = false;
  1324. if (klondike_send_work(klncgpu, dev, work))
  1325. return false;
  1326. } else
  1327. rd_unlock(&(klninfo->stat_lock));
  1328. }
  1329. if (nowork)
  1330. cgsleep_ms(10); // avoid a hard loop in case we have nothing to do
  1331. return true;
  1332. }
  1333. static int64_t klondike_scanwork(struct thr_info *thr)
  1334. {
  1335. struct cgpu_info *klncgpu = thr->cgpu;
  1336. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1337. int dev, slaves;
  1338. if (klninfo->usbinfo_nodev)
  1339. return -1;
  1340. restart_wait(thr, 200);
  1341. if (klninfo->status != NULL) {
  1342. rd_lock(&(klninfo->stat_lock));
  1343. slaves = klninfo->status[0].kline.ws.slavecount;
  1344. for (dev = 0; dev <= slaves; dev++) {
  1345. uint64_t newhashdev = 0, hashcount;
  1346. int maxcount;
  1347. hashcount = K_HASHCOUNT(klninfo->status[dev].kline.ws.hashcount);
  1348. maxcount = K_MAXCOUNT(klninfo->status[dev].kline.ws.maxcount);
  1349. // todo: chg this to check workid for wrapped instead
  1350. if (klninfo->devinfo[dev].lasthashcount > hashcount)
  1351. newhashdev += maxcount; // hash counter wrapped
  1352. newhashdev += hashcount - klninfo->devinfo[dev].lasthashcount;
  1353. klninfo->devinfo[dev].lasthashcount = hashcount;
  1354. if (maxcount != 0)
  1355. klninfo->hashcount += (newhashdev << 32) / maxcount;
  1356. }
  1357. klninfo->noncecount = 0;
  1358. rd_unlock(&(klninfo->stat_lock));
  1359. }
  1360. return 0;
  1361. }
  1362. #ifdef HAVE_CURSES
  1363. static
  1364. void klondike_wlogprint_status(struct cgpu_info *klncgpu)
  1365. {
  1366. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1367. uint16_t fan = 0;
  1368. uint16_t clock = 0;
  1369. int dev, slaves;
  1370. if (klninfo->status == NULL) {
  1371. return;
  1372. }
  1373. rd_lock(&(klninfo->stat_lock));
  1374. slaves = klninfo->status[0].kline.ws.slavecount;
  1375. for (dev = 0; dev <= slaves; dev++) {
  1376. fan += klninfo->cfg[dev].kline.cfg.fantarget;
  1377. clock += (uint16_t)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  1378. }
  1379. rd_unlock(&(klninfo->stat_lock));
  1380. fan /= slaves + 1;
  1381. fan = 100 * fan / 255;
  1382. clock /= slaves + 1;
  1383. wlogprint("Frequency: %d MHz\n", (int)clock);
  1384. if (fan && fan <= 100)
  1385. wlogprint("Fan speed: %d%%\n", fan);
  1386. }
  1387. #endif
  1388. static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
  1389. {
  1390. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1391. struct api_data *root = NULL;
  1392. char buf[32];
  1393. int dev, slaves;
  1394. if (klninfo->status == NULL)
  1395. return NULL;
  1396. rd_lock(&(klninfo->stat_lock));
  1397. slaves = klninfo->status[0].kline.ws.slavecount;
  1398. for (dev = 0; dev <= slaves; dev++) {
  1399. float fTemp = cvtKlnToC(klninfo->status[dev].kline.ws.temp);
  1400. sprintf(buf, "Temp %d", dev);
  1401. root = api_add_temp(root, buf, &fTemp, true);
  1402. double dClk = (double)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  1403. sprintf(buf, "Clock %d", dev);
  1404. root = api_add_freq(root, buf, &dClk, true);
  1405. unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].kline.cfg.fantarget / 255;
  1406. sprintf(buf, "Fan Percent %d", dev);
  1407. root = api_add_int(root, buf, (int *)(&iFan), true);
  1408. iFan = 0;
  1409. if (klninfo->status[dev].kline.ws.fanspeed > 0)
  1410. iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].kline.ws.fanspeed;
  1411. sprintf(buf, "Fan RPM %d", dev);
  1412. root = api_add_int(root, buf, (int *)(&iFan), true);
  1413. if (klninfo->devinfo[dev].chipstats != NULL) {
  1414. char data[2048];
  1415. char one[32];
  1416. int n;
  1417. sprintf(buf, "Nonces / Chip %d", dev);
  1418. data[0] = '\0';
  1419. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  1420. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n]);
  1421. strcat(data, one);
  1422. }
  1423. root = api_add_string(root, buf, data, true);
  1424. sprintf(buf, "Errors / Chip %d", dev);
  1425. data[0] = '\0';
  1426. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  1427. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].kline.ws.chipcount]);
  1428. strcat(data, one);
  1429. }
  1430. root = api_add_string(root, buf, data, true);
  1431. }
  1432. }
  1433. root = api_add_uint64(root, "Hash Count", &(klninfo->hashcount), true);
  1434. root = api_add_uint64(root, "Error Count", &(klninfo->errorcount), true);
  1435. root = api_add_uint64(root, "Noise Count", &(klninfo->noisecount), true);
  1436. root = api_add_int(root, "KLine Limit", &(klninfo->kline_count), true);
  1437. root = api_add_int(root, "KLine Used", &(klninfo->used_count), true);
  1438. root = api_add_elapsed(root, "KQue Delay Count", &(klninfo->delay_count), true);
  1439. root = api_add_elapsed(root, "KQue Delay Total", &(klninfo->delay_total), true);
  1440. root = api_add_elapsed(root, "KQue Delay Min", &(klninfo->delay_min), true);
  1441. root = api_add_elapsed(root, "KQue Delay Max", &(klninfo->delay_max), true);
  1442. double avg;
  1443. if (klninfo->delay_count == 0)
  1444. avg = 0;
  1445. else
  1446. avg = klninfo->delay_total / klninfo->delay_count;
  1447. root = api_add_diff(root, "KQue Delay Avg", &avg, true);
  1448. root = api_add_elapsed(root, "KQue Nonce Count", &(klninfo->nonce_count), true);
  1449. root = api_add_elapsed(root, "KQue Nonce Total", &(klninfo->nonce_total), true);
  1450. root = api_add_elapsed(root, "KQue Nonce Min", &(klninfo->nonce_min), true);
  1451. root = api_add_elapsed(root, "KQue Nonce Max", &(klninfo->nonce_max), true);
  1452. if (klninfo->nonce_count == 0)
  1453. avg = 0;
  1454. else
  1455. avg = klninfo->nonce_total / klninfo->nonce_count;
  1456. root = api_add_diff(root, "KQue Nonce Avg", &avg, true);
  1457. root = api_add_int(root, "WQue Size", &(klninfo->wque_size), true);
  1458. root = api_add_int(root, "WQue Cleared", &(klninfo->wque_cleared), true);
  1459. rd_unlock(&(klninfo->stat_lock));
  1460. return root;
  1461. }
  1462. struct device_drv klondike_drv = {
  1463. .dname = "klondike",
  1464. .name = "KLN",
  1465. .lowl_match = klondike_lowl_match,
  1466. .lowl_probe = klondike_lowl_probe,
  1467. .get_api_stats = klondike_api_stats,
  1468. .get_stats = klondike_get_stats,
  1469. .thread_prepare = klondike_thread_prepare,
  1470. .thread_init = klondike_thread_init,
  1471. .minerloop = hash_queued_work,
  1472. .scanwork = klondike_scanwork,
  1473. .queue_full = klondike_queue_full,
  1474. .flush_work = klondike_flush_work,
  1475. .thread_shutdown = klondike_shutdown,
  1476. .thread_enable = klondike_thread_enable,
  1477. .zero_stats = klondike_zero_stats,
  1478. #ifdef HAVE_CURSES
  1479. .proc_wlogprint_status = klondike_wlogprint_status,
  1480. #endif
  1481. };