driver-klondike.c 36 KB

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