driver-klondike.c 48 KB

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