driver-klondike.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  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 bool klondike_init(struct cgpu_info *klncgpu)
  593. {
  594. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  595. KLIST *kitem;
  596. KLINE kline;
  597. int slaves, dev;
  598. klninfo->initialised = false;
  599. cgpu_set_defaults(klncgpu);
  600. zero_kline(&kline);
  601. kline.hd.cmd = KLN_CMD_STATUS;
  602. kline.hd.dev = 0;
  603. kitem = SendCmdGetReply(klncgpu, &kline, 0);
  604. if (kitem == NULL)
  605. return false;
  606. slaves = kitem->kline.ws.slavecount;
  607. if (klninfo->status == NULL) {
  608. applog(LOG_DEBUG, "%s%i: initializing data",
  609. klncgpu->drv->name, klncgpu->device_id);
  610. // alloc space for status, devinfo, cfg and jobque for master and slaves
  611. klninfo->status = calloc(slaves+1, sizeof(*(klninfo->status)));
  612. if (unlikely(!klninfo->status))
  613. quit(1, "Failed to calloc status array in klondke_get_stats");
  614. klninfo->devinfo = calloc(slaves+1, sizeof(*(klninfo->devinfo)));
  615. if (unlikely(!klninfo->devinfo))
  616. quit(1, "Failed to calloc devinfo array in klondke_get_stats");
  617. klninfo->cfg = calloc(slaves+1, sizeof(*(klninfo->cfg)));
  618. if (unlikely(!klninfo->cfg))
  619. quit(1, "Failed to calloc cfg array in klondke_get_stats");
  620. klninfo->jobque = calloc(slaves+1, sizeof(*(klninfo->jobque)));
  621. if (unlikely(!klninfo->jobque))
  622. quit(1, "Failed to calloc jobque array in klondke_get_stats");
  623. }
  624. memcpy((void *)(&(klninfo->status[0])), (void *)kitem, sizeof(klninfo->status[0]));
  625. kitem = release_kitem(klncgpu, kitem);
  626. // zero init triggers read back only
  627. zero_kline(&kline);
  628. kline.cfg.cmd = KLN_CMD_CONFIG;
  629. int size = 2;
  630. // boundaries are checked by device, with valid values returned
  631. {
  632. SET_HASHCLOCK(kline.cfg.hashclock, klninfo->clock);
  633. kline.cfg.temptarget = cvtCToKln(klncgpu->targettemp);
  634. kline.cfg.tempcritical = 0; // hard code for old firmware
  635. kline.cfg.fantarget = 0xff; // hard code for old firmware
  636. size = sizeof(kline.cfg) - 2;
  637. }
  638. for (dev = 0; dev <= slaves; dev++) {
  639. kline.cfg.dev = dev;
  640. kitem = SendCmdGetReply(klncgpu, &kline, size);
  641. if (kitem != NULL) {
  642. memcpy((void *)&(klninfo->cfg[dev]), kitem, sizeof(klninfo->cfg[dev]));
  643. applog(LOG_WARNING, "%s%i:%d config (%d: Clk: %d, T:%.0lf, C:%.0lf, F:%d)",
  644. klncgpu->drv->name, klncgpu->device_id, dev,
  645. dev, K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock),
  646. cvtKlnToC(klninfo->cfg[dev].kline.cfg.temptarget),
  647. cvtKlnToC(klninfo->cfg[dev].kline.cfg.tempcritical),
  648. (int)100*klninfo->cfg[dev].kline.cfg.fantarget/256);
  649. kitem = release_kitem(klncgpu, kitem);
  650. }
  651. }
  652. klondike_get_stats(klncgpu);
  653. klninfo->initialised = true;
  654. for (dev = 0; dev <= slaves; dev++) {
  655. klninfo->devinfo[dev].rangesize = ((uint64_t)1<<32) / klninfo->status[dev].kline.ws.chipcount;
  656. klninfo->devinfo[dev].chipstats = calloc(klninfo->status[dev].kline.ws.chipcount*2 , sizeof(uint32_t));
  657. }
  658. bool ok = kln_enable(klncgpu);
  659. if (!ok)
  660. applog(LOG_ERR, "%s%i: failed to enable", klncgpu->drv->name, klncgpu->device_id);
  661. return ok;
  662. }
  663. static void control_init(struct cgpu_info *klncgpu)
  664. {
  665. struct klondike_info * const klninfo = klncgpu->device_data;
  666. int err, interface;
  667. if (klninfo->usbinfo_nodev)
  668. return;
  669. interface = 0;
  670. err = libusb_control_transfer(klninfo->usbdev_handle, 0, 9, 1, interface, NULL, 0, 999);
  671. applog(LOG_DEBUG, "%s%i: reset got err %d",
  672. klncgpu->drv->name, klncgpu->device_id, err);
  673. }
  674. static
  675. 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)
  676. {
  677. struct klondike_info * const klninfo = proc->device_data;
  678. if (klninfo->initialised)
  679. return "Cannot change clock after initialisation";
  680. klninfo->clock = atoi(newvalue);
  681. return NULL;
  682. }
  683. static
  684. 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)
  685. {
  686. struct klondike_info * const klninfo = proc->device_data;
  687. klninfo->max_work_count = atoi(newvalue);
  688. return NULL;
  689. }
  690. static
  691. 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)
  692. {
  693. struct klondike_info * const klninfo = proc->device_data;
  694. klninfo->old_work_ms = atof(newvalue) * 1000.0;
  695. return NULL;
  696. }
  697. static
  698. 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)
  699. {
  700. struct klondike_info * const klninfo = proc->device_data;
  701. klninfo->reply_wait_time = atoi(newvalue);
  702. return NULL;
  703. }
  704. static const struct bfg_set_device_definition klondike_set_device_funcs[] = {
  705. {"clock", klondike_set_clock, "clock frequency (can only be set at startup, with --set-device)"},
  706. {"max_work_count", klondike_set_max_work_count, "number of work items to queue on each bus"},
  707. {"old_work_time", klondike_set_old_work_time, "number of seconds to retain work"},
  708. {"reply_wait_time", klondike_set_reply_wait_time, "number of seconds poll interval"},
  709. {NULL}
  710. };
  711. static
  712. bool klondike_lowl_match(const struct lowlevel_device_info * const info)
  713. {
  714. if (!lowlevel_match_id(info, &lowl_usb, 0x04d8, 0xf60a))
  715. return false;
  716. return (info->manufacturer && strstr(info->manufacturer, "Klondike"));
  717. }
  718. bool klondike_lowl_probe_custom(const struct lowlevel_device_info * const info, struct device_drv * const drv, struct klondike_info * const klninfo)
  719. {
  720. if (unlikely(info->lowl != &lowl_usb))
  721. {
  722. bfg_probe_result_flags = BPR_WRONG_DEVTYPE;
  723. applog(LOG_DEBUG, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not usb!",
  724. __func__, info->product, info->serial);
  725. goto err;
  726. }
  727. struct libusb_device * const dev = info->lowl_data;
  728. if (bfg_claim_libusb(drv, true, dev))
  729. goto err;
  730. // static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  731. struct cgpu_info * const klncgpu = malloc(sizeof(*klncgpu));
  732. KLINE kline;
  733. if (unlikely(!klncgpu))
  734. quit(1, "Failed to calloc klncgpu in klondike_detect_one");
  735. *klncgpu = (struct cgpu_info){
  736. .drv = drv,
  737. .deven = DEV_ENABLED,
  738. .threads = 1,
  739. .targettemp = 50,
  740. .cutofftemp = (int)KLN_KILLWORK_TEMP,
  741. .set_device_funcs = klondike_set_device_funcs,
  742. };
  743. klncgpu->device_data = (void *)klninfo;
  744. klninfo->free = new_klist_set(klncgpu);
  745. if (usb_init(klncgpu, dev)) {
  746. int sent, recd, err;
  747. KLIST kitem;
  748. int attempts = 0;
  749. klncgpu->device_path = strdup(info->devid);
  750. control_init(klncgpu);
  751. while (attempts++ < 3) {
  752. kline.hd.cmd = KLN_CMD_IDENT;
  753. kline.hd.dev = 0;
  754. display_send_kline(klncgpu, &kline, msg_detect_send);
  755. err = usb_write(klncgpu, (char *)&(kline.hd), 2, &sent);
  756. if (err < 0 || sent != 2) {
  757. applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)",
  758. klncgpu->drv->dname,
  759. klncgpu->device_path,
  760. sent, err);
  761. }
  762. cgsleep_ms(klninfo->reply_wait_time * 10);
  763. err = usb_read(klncgpu, &kitem.kline, REPLY_SIZE, &recd);
  764. if (err < 0) {
  765. applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)",
  766. klncgpu->drv->dname,
  767. klncgpu->device_path,
  768. recd, err);
  769. } else if (recd < 1) {
  770. applog(LOG_ERR, "%s (%s) detect empty reply (%d)",
  771. klncgpu->drv->dname,
  772. klncgpu->device_path,
  773. recd);
  774. } else if (kitem.kline.hd.cmd == KLN_CMD_IDENT && kitem.kline.hd.dev == 0) {
  775. display_kline(klncgpu, &kitem.kline, msg_detect_reply);
  776. applog(LOG_DEBUG, "%s (%s) detect successful (%d attempt%s)",
  777. klncgpu->drv->dname,
  778. klncgpu->device_path,
  779. attempts, attempts == 1 ? "" : "s");
  780. kline.hd.cmd = KLN_CMD_STATUS;
  781. if (!SendCmd(klncgpu, &kline, 0))
  782. {
  783. applog(LOG_DEBUG, "%s (%s) status request failed",
  784. klncgpu->drv->dname, klncgpu->device_path);
  785. continue;
  786. }
  787. cgsleep_ms(klninfo->reply_wait_time * 10);
  788. err = usb_read(klncgpu, &kitem.kline, REPLY_SIZE, &recd);
  789. if (err < 0 || recd < REPLY_SIZE)
  790. {
  791. applog(LOG_DEBUG, "%s (%s) status request failed (2)",
  792. klncgpu->drv->dname, klncgpu->device_path);
  793. continue;
  794. }
  795. klncgpu->procs = 1 + kitem.kline.ws.slavecount;
  796. if (!add_cgpu(klncgpu))
  797. break;
  798. applog(LOG_DEBUG, "Klondike cgpu added");
  799. rwlock_init(&klninfo->stat_lock);
  800. cglock_init(&klninfo->klist_lock);
  801. return true;
  802. }
  803. }
  804. usb_uninit(klncgpu);
  805. }
  806. free(klninfo->free);
  807. free(klncgpu);
  808. err:
  809. free(klninfo);
  810. return false;
  811. }
  812. static
  813. bool klondike_lowl_probe(const struct lowlevel_device_info * const info)
  814. {
  815. struct klondike_info * const klninfo = malloc(sizeof(*klninfo));
  816. if (unlikely(!klninfo))
  817. applogr(false, LOG_ERR, "%s: Failed to malloc klninfo", __func__);
  818. *klninfo = (struct klondike_info){
  819. .clock = 282,
  820. .max_work_count = 4,
  821. .old_work_ms = 5000,
  822. .reply_wait_time = 100,
  823. };
  824. return klondike_lowl_probe_custom(info, &klondike_drv, klninfo);
  825. }
  826. static void klondike_check_nonce(struct cgpu_info *klncgpu, KLIST *kitem)
  827. {
  828. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  829. struct work *work, *look, *tmp;
  830. KLINE *kline = &(kitem->kline);
  831. struct cgpu_info * const proc = klondike_get_proc(klncgpu, kline->wr.dev);
  832. struct thr_info * const thr = proc->thr[0];
  833. struct timeval tv_now;
  834. double us_diff;
  835. uint32_t nonce = K_NONCE(kline->wr.nonce) + klninfo->nonce_offset;
  836. applog(LOG_DEBUG, "%"PRIpreprv": FOUND NONCE (%02x:%08x)",
  837. proc->proc_repr,
  838. kline->wr.workid, (unsigned int)nonce);
  839. work = NULL;
  840. cgtime(&tv_now);
  841. rd_lock(&(klncgpu->qlock));
  842. HASH_ITER(hh, klncgpu->queued_work, look, tmp) {
  843. if (ms_tdiff(&tv_now, &(look->tv_stamp)) < klninfo->old_work_ms &&
  844. (look->subid == (kline->wr.dev*256 + kline->wr.workid))) {
  845. work = look;
  846. break;
  847. }
  848. }
  849. rd_unlock(&(klncgpu->qlock));
  850. if (work) {
  851. if (unlikely(!klninfo->nonce_offset))
  852. {
  853. bool test_c0 = test_nonce(work, nonce - 0xc0, false);
  854. bool test_180 = test_nonce(work, nonce - 0x180, false);
  855. if (test_c0)
  856. {
  857. if (unlikely(test_180))
  858. {
  859. applog(LOG_DEBUG, "%s: Matched both c0 and 180 offsets (%02x:%08lx)",
  860. klncgpu->dev_repr, kline->wr.workid, (unsigned long)nonce);
  861. submit_nonce(thr, work, nonce - 0x180);
  862. nonce -= 0xc0;
  863. }
  864. else
  865. {
  866. applog(LOG_DEBUG, "%s: Matched c0 offset (%02x:%08lx)",
  867. klncgpu->dev_repr, kline->wr.workid, (unsigned long)nonce);
  868. nonce += (klninfo->nonce_offset = -0xc0);
  869. }
  870. }
  871. else
  872. if (test_180)
  873. {
  874. applog(LOG_DEBUG, "%s: Matched 180 offset (%02x:%08lx)",
  875. klncgpu->dev_repr, kline->wr.workid, (unsigned long)nonce);
  876. nonce += (klninfo->nonce_offset = -0x180);
  877. }
  878. else
  879. applog(LOG_DEBUG, "%s: Matched neither c0 nor 180 offset (%02x:%08lx)",
  880. klncgpu->dev_repr, kline->wr.workid, (unsigned long)nonce);
  881. }
  882. wr_lock(&(klninfo->stat_lock));
  883. klninfo->devinfo[kline->wr.dev].noncecount++;
  884. klninfo->noncecount++;
  885. wr_unlock(&(klninfo->stat_lock));
  886. applog(LOG_DEBUG, "%"PRIpreprv": SUBMIT NONCE (%02x:%08x)",
  887. proc->proc_repr,
  888. kline->wr.workid, (unsigned int)nonce);
  889. cgtime(&tv_now);
  890. bool ok = submit_nonce(thr, work, nonce);
  891. applog(LOG_DEBUG, "%"PRIpreprv": chip stats %d, %08x, %d, %d",
  892. proc->proc_repr,
  893. kline->wr.dev, (unsigned int)nonce,
  894. klninfo->devinfo[kline->wr.dev].rangesize,
  895. klninfo->status[kline->wr.dev].kline.ws.chipcount);
  896. klninfo->devinfo[kline->wr.dev].chipstats[(nonce / klninfo->devinfo[kline->wr.dev].rangesize) + (ok ? 0 : klninfo->status[kline->wr.dev].kline.ws.chipcount)]++;
  897. us_diff = us_tdiff(&tv_now, &(kitem->tv_when));
  898. if (klninfo->delay_count == 0) {
  899. klninfo->delay_min = us_diff;
  900. klninfo->delay_max = us_diff;
  901. } else {
  902. if (klninfo->delay_min > us_diff)
  903. klninfo->delay_min = us_diff;
  904. if (klninfo->delay_max < us_diff)
  905. klninfo->delay_max = us_diff;
  906. }
  907. klninfo->delay_count++;
  908. klninfo->delay_total += us_diff;
  909. if (klninfo->nonce_count > 0) {
  910. us_diff = us_tdiff(&(kitem->tv_when), &(klninfo->tv_last_nonce_received));
  911. if (klninfo->nonce_count == 1) {
  912. klninfo->nonce_min = us_diff;
  913. klninfo->nonce_max = us_diff;
  914. } else {
  915. if (klninfo->nonce_min > us_diff)
  916. klninfo->nonce_min = us_diff;
  917. if (klninfo->nonce_max < us_diff)
  918. klninfo->nonce_max = us_diff;
  919. }
  920. klninfo->nonce_total += us_diff;
  921. }
  922. klninfo->nonce_count++;
  923. hashes_done2(thr, 0x100000000, NULL);
  924. memcpy(&(klninfo->tv_last_nonce_received), &(kitem->tv_when),
  925. sizeof(klninfo->tv_last_nonce_received));
  926. return;
  927. }
  928. applog(LOG_ERR, "%"PRIpreprv": unknown work (%02x:%08x) - ignored",
  929. proc->proc_repr,
  930. kline->wr.workid, (unsigned int)nonce);
  931. inc_hw_errors2(thr, NULL, &nonce);
  932. }
  933. // thread to keep looking for replies
  934. static void *klondike_get_replies(void *userdata)
  935. {
  936. struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
  937. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  938. KLIST *kitem = NULL;
  939. int err, recd, slaves, dev, isc;
  940. bool overheat, sent;
  941. applog(LOG_DEBUG, "%s%i: listening for replies",
  942. klncgpu->drv->name, klncgpu->device_id);
  943. while (klncgpu->shutdown == false) {
  944. if (klninfo->usbinfo_nodev)
  945. return NULL;
  946. if (kitem == NULL)
  947. kitem = allocate_kitem(klncgpu);
  948. else
  949. memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
  950. err = usb_read(klncgpu, &kitem->kline, REPLY_SIZE, &recd);
  951. if (err || recd != REPLY_SIZE) {
  952. if (err != -7)
  953. applog(LOG_ERR, "%s%i: reply err=%d amt=%d",
  954. klncgpu->drv->name, klncgpu->device_id,
  955. err, recd);
  956. }
  957. if (!err && recd == REPLY_SIZE) {
  958. cgtime(&(kitem->tv_when));
  959. rd_lock(&(klninfo->stat_lock));
  960. kitem->block_seq = klninfo->block_seq;
  961. rd_unlock(&(klninfo->stat_lock));
  962. if (opt_log_level <= READ_DEBUG) {
  963. char hexdata[recd * 2];
  964. bin2hex(hexdata, &kitem->kline.hd.dev, recd-1);
  965. applog(READ_DEBUG, "%s%i:%d reply [%c:%s]",
  966. klncgpu->drv->name, klncgpu->device_id,
  967. (int)(kitem->kline.hd.dev),
  968. kitem->kline.hd.cmd, hexdata);
  969. }
  970. // We can't check this until it's initialised
  971. if (klninfo->initialised) {
  972. rd_lock(&(klninfo->stat_lock));
  973. slaves = klninfo->status[0].kline.ws.slavecount;
  974. rd_unlock(&(klninfo->stat_lock));
  975. if (kitem->kline.hd.dev > slaves) {
  976. applog(LOG_ERR, "%s%i: reply [%c] has invalid dev=%d (max=%d) using 0",
  977. klncgpu->drv->name, klncgpu->device_id,
  978. (char)(kitem->kline.hd.cmd),
  979. (int)(kitem->kline.hd.dev),
  980. slaves);
  981. /* TODO: this is rather problematic if there are slaves
  982. * however without slaves - it should always be zero */
  983. kitem->kline.hd.dev = 0;
  984. } else {
  985. wr_lock(&(klninfo->stat_lock));
  986. klninfo->jobque[kitem->kline.hd.dev].late_update_sequential = 0;
  987. wr_unlock(&(klninfo->stat_lock));
  988. }
  989. }
  990. switch (kitem->kline.hd.cmd) {
  991. case KLN_CMD_NONCE:
  992. klondike_check_nonce(klncgpu, kitem);
  993. display_kline(klncgpu, &kitem->kline, msg_reply);
  994. break;
  995. case KLN_CMD_WORK:
  996. // We can't do/check this until it's initialised
  997. if (klninfo->initialised) {
  998. dev = kitem->kline.ws.dev;
  999. if (kitem->kline.ws.workqc == 0) {
  1000. bool idle = false;
  1001. rd_lock(&(klninfo->stat_lock));
  1002. if (klninfo->jobque[dev].flushed == false)
  1003. idle = true;
  1004. slaves = klninfo->status[0].kline.ws.slavecount;
  1005. rd_unlock(&(klninfo->stat_lock));
  1006. if (idle)
  1007. applog(LOG_WARNING, "%s%i:%d went idle before work was sent",
  1008. klncgpu->drv->name,
  1009. klncgpu->device_id,
  1010. dev);
  1011. }
  1012. wr_lock(&(klninfo->stat_lock));
  1013. klninfo->jobque[dev].flushed = false;
  1014. wr_unlock(&(klninfo->stat_lock));
  1015. }
  1016. case KLN_CMD_STATUS:
  1017. case KLN_CMD_ABORT:
  1018. // We can't do/check this until it's initialised
  1019. if (klninfo->initialised) {
  1020. isc = 0;
  1021. dev = kitem->kline.ws.dev;
  1022. wr_lock(&(klninfo->stat_lock));
  1023. klninfo->jobque[dev].workqc = (int)(kitem->kline.ws.workqc);
  1024. cgtime(&(klninfo->jobque[dev].last_update));
  1025. slaves = klninfo->status[0].kline.ws.slavecount;
  1026. overheat = klninfo->jobque[dev].overheat;
  1027. if (dev == 0) {
  1028. if (kitem->kline.ws.slavecount != slaves)
  1029. isc = ++klninfo->incorrect_slave_sequential;
  1030. else
  1031. isc = klninfo->incorrect_slave_sequential = 0;
  1032. }
  1033. wr_unlock(&(klninfo->stat_lock));
  1034. if (isc) {
  1035. applog(LOG_ERR, "%s%i:%d reply [%c] has a diff"
  1036. " # of slaves=%d (curr=%d)%s",
  1037. klncgpu->drv->name,
  1038. klncgpu->device_id,
  1039. dev,
  1040. (char)(kitem->kline.ws.cmd),
  1041. (int)(kitem->kline.ws.slavecount),
  1042. slaves,
  1043. isc <= KLN_ISS_IGNORE ? "" :
  1044. " disabling device");
  1045. if (isc > KLN_ISS_IGNORE)
  1046. usb_nodev(klncgpu);
  1047. break;
  1048. }
  1049. if (!overheat) {
  1050. double temp = cvtKlnToC(kitem->kline.ws.temp);
  1051. if (temp >= KLN_KILLWORK_TEMP) {
  1052. KLINE kline;
  1053. wr_lock(&(klninfo->stat_lock));
  1054. klninfo->jobque[dev].overheat = true;
  1055. wr_unlock(&(klninfo->stat_lock));
  1056. applog(LOG_WARNING, "%s%i:%d Critical overheat (%.0fC)",
  1057. klncgpu->drv->name,
  1058. klncgpu->device_id,
  1059. dev, temp);
  1060. zero_kline(&kline);
  1061. kline.hd.cmd = KLN_CMD_ABORT;
  1062. kline.hd.dev = dev;
  1063. sent = SendCmd(klncgpu, &kline, KSENDHD(0));
  1064. kln_disable(klncgpu, dev, false);
  1065. if (!sent) {
  1066. applog(LOG_ERR, "%s%i:%d overheat failed to"
  1067. " abort work - disabling device",
  1068. klncgpu->drv->name,
  1069. klncgpu->device_id,
  1070. dev);
  1071. usb_nodev(klncgpu);
  1072. }
  1073. }
  1074. }
  1075. }
  1076. case KLN_CMD_ENABLE:
  1077. wr_lock(&(klninfo->stat_lock));
  1078. klninfo->errorcount += kitem->kline.ws.errorcount;
  1079. klninfo->noisecount += kitem->kline.ws.noise;
  1080. wr_unlock(&(klninfo->stat_lock));
  1081. display_kline(klncgpu, &kitem->kline, msg_reply);
  1082. kitem->ready = true;
  1083. kitem = NULL;
  1084. break;
  1085. case KLN_CMD_CONFIG:
  1086. display_kline(klncgpu, &kitem->kline, msg_reply);
  1087. kitem->ready = true;
  1088. kitem = NULL;
  1089. break;
  1090. case KLN_CMD_IDENT:
  1091. display_kline(klncgpu, &kitem->kline, msg_reply);
  1092. kitem->ready = true;
  1093. kitem = NULL;
  1094. break;
  1095. default:
  1096. display_kline(klncgpu, &kitem->kline, msg_reply);
  1097. break;
  1098. }
  1099. }
  1100. }
  1101. return NULL;
  1102. }
  1103. static void klondike_flush_work(struct cgpu_info *klncgpu)
  1104. {
  1105. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1106. KLIST *kitem;
  1107. KLINE kline;
  1108. int slaves, dev;
  1109. if (klninfo->initialised) {
  1110. wr_lock(&(klninfo->stat_lock));
  1111. klninfo->block_seq++;
  1112. slaves = klninfo->status[0].kline.ws.slavecount;
  1113. wr_unlock(&(klninfo->stat_lock));
  1114. applog(LOG_DEBUG, "%s%i: flushing work",
  1115. klncgpu->drv->name, klncgpu->device_id);
  1116. zero_kline(&kline);
  1117. kline.hd.cmd = KLN_CMD_ABORT;
  1118. for (dev = 0; dev <= slaves; dev++) {
  1119. kline.hd.dev = dev;
  1120. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  1121. if (kitem != NULL) {
  1122. wr_lock(&(klninfo->stat_lock));
  1123. memcpy((void *)&(klninfo->status[dev]),
  1124. kitem,
  1125. sizeof(klninfo->status[dev]));
  1126. klninfo->jobque[dev].flushed = true;
  1127. wr_unlock(&(klninfo->stat_lock));
  1128. kitem = release_kitem(klncgpu, kitem);
  1129. }
  1130. }
  1131. }
  1132. }
  1133. static bool klondike_thread_prepare(struct thr_info *thr)
  1134. {
  1135. struct cgpu_info *klncgpu = thr->cgpu;
  1136. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1137. if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) {
  1138. applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id);
  1139. return false;
  1140. }
  1141. pthread_detach(klninfo->replies_thr.pth);
  1142. // let the listening get started
  1143. cgsleep_ms(100);
  1144. return klondike_init(klncgpu);
  1145. }
  1146. static bool klondike_thread_init(struct thr_info *thr)
  1147. {
  1148. struct cgpu_info *klncgpu = thr->cgpu;
  1149. struct klondike_info * const klninfo = klncgpu->device_data;
  1150. notifier_init(thr->work_restart_notifier);
  1151. if (klninfo->usbinfo_nodev)
  1152. return false;
  1153. klondike_flush_work(klncgpu);
  1154. return true;
  1155. }
  1156. static void klondike_shutdown(struct thr_info *thr)
  1157. {
  1158. struct cgpu_info *klncgpu = thr->cgpu;
  1159. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1160. applog(LOG_DEBUG, "%s%i: shutting down work",
  1161. klncgpu->drv->name, klncgpu->device_id);
  1162. kln_disable(klncgpu, klninfo->status[0].kline.ws.slavecount, true);
  1163. klncgpu->shutdown = true;
  1164. }
  1165. static void klondike_thread_enable(struct thr_info *thr)
  1166. {
  1167. struct cgpu_info *klncgpu = thr->cgpu;
  1168. struct klondike_info * const klninfo = klncgpu->device_data;
  1169. if (klninfo->usbinfo_nodev)
  1170. return;
  1171. /*
  1172. KLINE kline;
  1173. zero_kline(&kline);
  1174. kline.hd.cmd = KLN_CMD_ENABLE;
  1175. kline.hd.dev = dev;
  1176. kline.hd.buf[0] = KLN_CMD_ENABLE_OFF;
  1177. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
  1178. */
  1179. }
  1180. static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
  1181. {
  1182. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1183. struct work *look, *tmp;
  1184. KLINE kline;
  1185. struct timeval tv_old;
  1186. int wque_size, wque_cleared;
  1187. if (klninfo->usbinfo_nodev)
  1188. return false;
  1189. zero_kline(&kline);
  1190. kline.wt.cmd = KLN_CMD_WORK;
  1191. kline.wt.dev = dev;
  1192. memcpy(kline.wt.midstate, work->midstate, MIDSTATE_BYTES);
  1193. memcpy(kline.wt.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  1194. kline.wt.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
  1195. work->subid = dev*256 + kline.wt.workid;
  1196. cgtime(&work->tv_stamp);
  1197. if (opt_log_level <= LOG_DEBUG) {
  1198. char hexdata[(sizeof(kline.wt) * 2) + 1];
  1199. bin2hex(hexdata, &kline.wt, sizeof(kline.wt));
  1200. applog(LOG_DEBUG, "WORKDATA: %s", hexdata);
  1201. }
  1202. applog(LOG_DEBUG, "%s%i:%d sending work (%d:%02x)",
  1203. klncgpu->drv->name, klncgpu->device_id, dev,
  1204. dev, kline.wt.workid);
  1205. KLIST *kitem = SendCmdGetReply(klncgpu, &kline, sizeof(kline.wt));
  1206. if (kitem != NULL) {
  1207. wr_lock(&(klninfo->stat_lock));
  1208. memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(klninfo->status[dev]));
  1209. wr_unlock(&(klninfo->stat_lock));
  1210. kitem = release_kitem(klncgpu, kitem);
  1211. // remove old work
  1212. wque_size = 0;
  1213. wque_cleared = 0;
  1214. cgtime(&tv_old);
  1215. wr_lock(&klncgpu->qlock);
  1216. HASH_ITER(hh, klncgpu->queued_work, look, tmp) {
  1217. if (ms_tdiff(&tv_old, &(look->tv_stamp)) > klninfo->old_work_ms) {
  1218. __work_completed(klncgpu, look);
  1219. free_work(look);
  1220. wque_cleared++;
  1221. } else
  1222. wque_size++;
  1223. }
  1224. wr_unlock(&klncgpu->qlock);
  1225. wr_lock(&(klninfo->stat_lock));
  1226. klninfo->wque_size = wque_size;
  1227. klninfo->wque_cleared = wque_cleared;
  1228. wr_unlock(&(klninfo->stat_lock));
  1229. return true;
  1230. }
  1231. return false;
  1232. }
  1233. static bool klondike_queue_full(struct cgpu_info *klncgpu)
  1234. {
  1235. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1236. struct work *work = NULL;
  1237. int dev, queued, slaves, seq, howlong;
  1238. struct timeval now;
  1239. bool nowork;
  1240. if (klncgpu->shutdown == true)
  1241. return true;
  1242. cgtime(&now);
  1243. rd_lock(&(klninfo->stat_lock));
  1244. slaves = klninfo->status[0].kline.ws.slavecount;
  1245. for (dev = 0; dev <= slaves; dev++)
  1246. if (ms_tdiff(&now, &(klninfo->jobque[dev].last_update)) > LATE_UPDATE_MS) {
  1247. klninfo->jobque[dev].late_update_count++;
  1248. seq = ++klninfo->jobque[dev].late_update_sequential;
  1249. rd_unlock(&(klninfo->stat_lock));
  1250. if (seq < LATE_UPDATE_LIMIT) {
  1251. applog(LOG_DEBUG, "%s%i:%d late update",
  1252. klncgpu->drv->name, klncgpu->device_id, dev);
  1253. klondike_get_stats(klncgpu);
  1254. goto que;
  1255. } else {
  1256. applog(LOG_WARNING, "%s%i:%d late update (%d) reached - attempting reset",
  1257. klncgpu->drv->name, klncgpu->device_id,
  1258. dev, LATE_UPDATE_LIMIT);
  1259. control_init(klncgpu);
  1260. kln_enable(klncgpu);
  1261. klondike_get_stats(klncgpu);
  1262. rd_lock(&(klninfo->stat_lock));
  1263. howlong = ms_tdiff(&now, &(klninfo->jobque[dev].last_update));
  1264. if (howlong > LATE_UPDATE_MS) {
  1265. rd_unlock(&(klninfo->stat_lock));
  1266. if (howlong > LATE_UPDATE_NODEV_MS) {
  1267. applog(LOG_ERR, "%s%i:%d reset failed - dropping device",
  1268. klncgpu->drv->name, klncgpu->device_id, dev);
  1269. usb_nodev(klncgpu);
  1270. } else
  1271. cgsleep_ms(LATE_UPDATE_SLEEP_MS);
  1272. return true;
  1273. }
  1274. break;
  1275. }
  1276. }
  1277. rd_unlock(&(klninfo->stat_lock));
  1278. que:
  1279. nowork = true;
  1280. for (queued = 0; queued < klninfo->max_work_count - 1; ++queued)
  1281. for (dev = 0; dev <= slaves; dev++) {
  1282. tryagain:
  1283. rd_lock(&(klninfo->stat_lock));
  1284. if (klninfo->jobque[dev].overheat) {
  1285. double temp = cvtKlnToC(klninfo->status[0].kline.ws.temp);
  1286. if ((queued == klninfo->max_work_count - 2) &&
  1287. ms_tdiff(&now, &(klninfo->jobque[dev].last_update)) > (LATE_UPDATE_MS/2)) {
  1288. rd_unlock(&(klninfo->stat_lock));
  1289. klondike_get_stats(klncgpu);
  1290. goto tryagain;
  1291. }
  1292. if (temp <= KLN_COOLED_DOWN) {
  1293. klninfo->jobque[dev].overheat = false;
  1294. rd_unlock(&(klninfo->stat_lock));
  1295. applog(LOG_WARNING, "%s%i:%d Overheat recovered (%.0fC)",
  1296. klncgpu->drv->name, klncgpu->device_id,
  1297. dev, temp);
  1298. kln_enable(klncgpu);
  1299. goto tryagain;
  1300. } else {
  1301. rd_unlock(&(klninfo->stat_lock));
  1302. continue;
  1303. }
  1304. }
  1305. if (klninfo->jobque[dev].workqc <= queued) {
  1306. rd_unlock(&(klninfo->stat_lock));
  1307. if (!work)
  1308. work = get_queued(klncgpu);
  1309. if (unlikely(!work))
  1310. return false;
  1311. nowork = false;
  1312. if (klondike_send_work(klncgpu, dev, work))
  1313. return false;
  1314. } else
  1315. rd_unlock(&(klninfo->stat_lock));
  1316. }
  1317. if (nowork)
  1318. cgsleep_ms(10); // avoid a hard loop in case we have nothing to do
  1319. return true;
  1320. }
  1321. static int64_t klondike_scanwork(struct thr_info *thr)
  1322. {
  1323. struct cgpu_info *klncgpu = thr->cgpu;
  1324. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1325. int dev, slaves;
  1326. if (klninfo->usbinfo_nodev)
  1327. return -1;
  1328. restart_wait(thr, 200);
  1329. if (klninfo->status != NULL) {
  1330. rd_lock(&(klninfo->stat_lock));
  1331. slaves = klninfo->status[0].kline.ws.slavecount;
  1332. for (dev = 0; dev <= slaves; dev++) {
  1333. uint64_t newhashdev = 0, hashcount;
  1334. int maxcount;
  1335. hashcount = K_HASHCOUNT(klninfo->status[dev].kline.ws.hashcount);
  1336. maxcount = K_MAXCOUNT(klninfo->status[dev].kline.ws.maxcount);
  1337. // todo: chg this to check workid for wrapped instead
  1338. if (klninfo->devinfo[dev].lasthashcount > hashcount)
  1339. newhashdev += maxcount; // hash counter wrapped
  1340. newhashdev += hashcount - klninfo->devinfo[dev].lasthashcount;
  1341. klninfo->devinfo[dev].lasthashcount = hashcount;
  1342. if (maxcount != 0)
  1343. klninfo->hashcount += (newhashdev << 32) / maxcount;
  1344. }
  1345. klninfo->noncecount = 0;
  1346. rd_unlock(&(klninfo->stat_lock));
  1347. }
  1348. return 0;
  1349. }
  1350. #ifdef HAVE_CURSES
  1351. static
  1352. void klondike_wlogprint_status(struct cgpu_info *klncgpu)
  1353. {
  1354. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1355. uint16_t fan = 0;
  1356. uint16_t clock = 0;
  1357. int dev, slaves;
  1358. if (klninfo->status == NULL) {
  1359. return;
  1360. }
  1361. rd_lock(&(klninfo->stat_lock));
  1362. slaves = klninfo->status[0].kline.ws.slavecount;
  1363. for (dev = 0; dev <= slaves; dev++) {
  1364. fan += klninfo->cfg[dev].kline.cfg.fantarget;
  1365. clock += (uint16_t)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  1366. }
  1367. rd_unlock(&(klninfo->stat_lock));
  1368. fan /= slaves + 1;
  1369. fan = 100 * fan / 255;
  1370. clock /= slaves + 1;
  1371. wlogprint("Frequency: %d MHz\n", (int)clock);
  1372. if (fan && fan <= 100)
  1373. wlogprint("Fan speed: %d%%\n", fan);
  1374. }
  1375. #endif
  1376. static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
  1377. {
  1378. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1379. struct api_data *root = NULL;
  1380. char buf[32];
  1381. int dev, slaves;
  1382. if (klninfo->status == NULL)
  1383. return NULL;
  1384. rd_lock(&(klninfo->stat_lock));
  1385. slaves = klninfo->status[0].kline.ws.slavecount;
  1386. for (dev = 0; dev <= slaves; dev++) {
  1387. float fTemp = cvtKlnToC(klninfo->status[dev].kline.ws.temp);
  1388. sprintf(buf, "Temp %d", dev);
  1389. root = api_add_temp(root, buf, &fTemp, true);
  1390. double dClk = (double)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  1391. sprintf(buf, "Clock %d", dev);
  1392. root = api_add_freq(root, buf, &dClk, true);
  1393. unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].kline.cfg.fantarget / 255;
  1394. sprintf(buf, "Fan Percent %d", dev);
  1395. root = api_add_int(root, buf, (int *)(&iFan), true);
  1396. iFan = 0;
  1397. if (klninfo->status[dev].kline.ws.fanspeed > 0)
  1398. iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].kline.ws.fanspeed;
  1399. sprintf(buf, "Fan RPM %d", dev);
  1400. root = api_add_int(root, buf, (int *)(&iFan), true);
  1401. if (klninfo->devinfo[dev].chipstats != NULL) {
  1402. char data[2048];
  1403. char one[32];
  1404. int n;
  1405. sprintf(buf, "Nonces / Chip %d", dev);
  1406. data[0] = '\0';
  1407. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  1408. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n]);
  1409. strcat(data, one);
  1410. }
  1411. root = api_add_string(root, buf, data, true);
  1412. sprintf(buf, "Errors / Chip %d", dev);
  1413. data[0] = '\0';
  1414. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  1415. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].kline.ws.chipcount]);
  1416. strcat(data, one);
  1417. }
  1418. root = api_add_string(root, buf, data, true);
  1419. }
  1420. }
  1421. root = api_add_uint64(root, "Hash Count", &(klninfo->hashcount), true);
  1422. root = api_add_uint64(root, "Error Count", &(klninfo->errorcount), true);
  1423. root = api_add_uint64(root, "Noise Count", &(klninfo->noisecount), true);
  1424. root = api_add_int(root, "KLine Limit", &(klninfo->kline_count), true);
  1425. root = api_add_int(root, "KLine Used", &(klninfo->used_count), true);
  1426. root = api_add_elapsed(root, "KQue Delay Count", &(klninfo->delay_count), true);
  1427. root = api_add_elapsed(root, "KQue Delay Total", &(klninfo->delay_total), true);
  1428. root = api_add_elapsed(root, "KQue Delay Min", &(klninfo->delay_min), true);
  1429. root = api_add_elapsed(root, "KQue Delay Max", &(klninfo->delay_max), true);
  1430. double avg;
  1431. if (klninfo->delay_count == 0)
  1432. avg = 0;
  1433. else
  1434. avg = klninfo->delay_total / klninfo->delay_count;
  1435. root = api_add_diff(root, "KQue Delay Avg", &avg, true);
  1436. root = api_add_elapsed(root, "KQue Nonce Count", &(klninfo->nonce_count), true);
  1437. root = api_add_elapsed(root, "KQue Nonce Total", &(klninfo->nonce_total), true);
  1438. root = api_add_elapsed(root, "KQue Nonce Min", &(klninfo->nonce_min), true);
  1439. root = api_add_elapsed(root, "KQue Nonce Max", &(klninfo->nonce_max), true);
  1440. if (klninfo->nonce_count == 0)
  1441. avg = 0;
  1442. else
  1443. avg = klninfo->nonce_total / klninfo->nonce_count;
  1444. root = api_add_diff(root, "KQue Nonce Avg", &avg, true);
  1445. root = api_add_int(root, "WQue Size", &(klninfo->wque_size), true);
  1446. root = api_add_int(root, "WQue Cleared", &(klninfo->wque_cleared), true);
  1447. rd_unlock(&(klninfo->stat_lock));
  1448. return root;
  1449. }
  1450. struct device_drv klondike_drv = {
  1451. .dname = "klondike",
  1452. .name = "KLN",
  1453. .lowl_match = klondike_lowl_match,
  1454. .lowl_probe = klondike_lowl_probe,
  1455. .get_api_stats = klondike_api_stats,
  1456. .get_stats = klondike_get_stats,
  1457. .thread_prepare = klondike_thread_prepare,
  1458. .thread_init = klondike_thread_init,
  1459. .minerloop = hash_queued_work,
  1460. .scanwork = klondike_scanwork,
  1461. .queue_full = klondike_queue_full,
  1462. .flush_work = klondike_flush_work,
  1463. .thread_shutdown = klondike_shutdown,
  1464. .thread_enable = klondike_thread_enable,
  1465. #ifdef HAVE_CURSES
  1466. .proc_wlogprint_status = klondike_wlogprint_status,
  1467. #endif
  1468. };