driver-klondike.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  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 (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 (ms_tdiff(&tv_old, &(look->tv_stamp)) > OLD_WORK_MS) {
  948. __work_completed(klncgpu, look);
  949. free_work(look);
  950. } else
  951. wque_size++;
  952. }
  953. wr_unlock(&klncgpu->qlock);
  954. wr_lock(&(klninfo->stat_lock));
  955. klninfo->wque_size = wque_size;
  956. klninfo->wque_cleared = wque_cleared;
  957. wr_unlock(&(klninfo->stat_lock));
  958. return true;
  959. }
  960. return false;
  961. }
  962. static bool klondike_queue_full(struct cgpu_info *klncgpu)
  963. {
  964. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  965. struct work *work = NULL;
  966. int dev, queued, slaves;
  967. struct timeval now;
  968. cgtime(&now);
  969. rd_lock(&(klninfo->stat_lock));
  970. slaves = klninfo->status[0].kline.ws.slavecount;
  971. for (dev = 0; dev <= slaves; dev++)
  972. if (ms_tdiff(&now, &(klninfo->jobque[dev].last_update)) > LATE_UPDATE_MS) {
  973. rd_unlock(&(klninfo->stat_lock));
  974. applog(LOG_ERR, "%s%i: late update",
  975. klncgpu->drv->name, klncgpu->device_id);
  976. klondike_get_stats(klncgpu);
  977. goto que;
  978. }
  979. rd_unlock(&(klninfo->stat_lock));
  980. que:
  981. for (queued = 0; queued < MAX_WORK_COUNT-1; queued++)
  982. for (dev = 0; dev <= slaves; dev++) {
  983. rd_lock(&(klninfo->stat_lock));
  984. if (klninfo->jobque[dev].workqc <= queued) {
  985. rd_unlock(&(klninfo->stat_lock));
  986. if (!work)
  987. work = get_queued(klncgpu);
  988. if (unlikely(!work))
  989. return false;
  990. if (klondike_send_work(klncgpu, dev, work))
  991. return false;
  992. } else
  993. rd_unlock(&(klninfo->stat_lock));
  994. }
  995. return true;
  996. }
  997. static int64_t klondike_scanwork(struct thr_info *thr)
  998. {
  999. struct cgpu_info *klncgpu = thr->cgpu;
  1000. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1001. int64_t newhashcount = 0;
  1002. int dev, slaves;
  1003. if (klninfo->usbinfo_nodev)
  1004. return -1;
  1005. restart_wait(thr, 200);
  1006. if (klninfo->status != NULL) {
  1007. rd_lock(&(klninfo->stat_lock));
  1008. slaves = klninfo->status[0].kline.ws.slavecount;
  1009. for (dev = 0; dev <= slaves; dev++) {
  1010. uint64_t newhashdev = 0, hashcount;
  1011. int maxcount;
  1012. hashcount = K_HASHCOUNT(klninfo->status[dev].kline.ws.hashcount);
  1013. maxcount = K_MAXCOUNT(klninfo->status[dev].kline.ws.maxcount);
  1014. if (klninfo->devinfo[dev].lasthashcount > hashcount) // todo: chg this to check workid for wrapped instead
  1015. newhashdev += maxcount; // hash counter wrapped
  1016. newhashdev += hashcount - klninfo->devinfo[dev].lasthashcount;
  1017. klninfo->devinfo[dev].lasthashcount = hashcount;
  1018. if (maxcount != 0)
  1019. klninfo->hashcount += (newhashdev << 32) / maxcount;
  1020. // todo: check stats for critical conditions
  1021. }
  1022. newhashcount += 0xffffffffull * (uint64_t)klninfo->noncecount;
  1023. klninfo->noncecount = 0;
  1024. rd_unlock(&(klninfo->stat_lock));
  1025. }
  1026. return newhashcount;
  1027. }
  1028. static void get_klondike_statline_before(char *buf, size_t siz, struct cgpu_info *klncgpu)
  1029. {
  1030. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1031. uint8_t temp = 0xFF;
  1032. uint16_t fan = 0;
  1033. uint16_t clock = 0;
  1034. int dev, slaves;
  1035. char tmp[16];
  1036. if (klninfo->status == NULL) {
  1037. return;
  1038. }
  1039. rd_lock(&(klninfo->stat_lock));
  1040. slaves = klninfo->status[0].kline.ws.slavecount;
  1041. for (dev = 0; dev <= slaves; dev++) {
  1042. if (klninfo->status[dev].kline.ws.temp < temp)
  1043. temp = klninfo->status[dev].kline.ws.temp;
  1044. fan += klninfo->cfg[dev].kline.cfg.fantarget;
  1045. clock += (uint16_t)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  1046. }
  1047. fan /= slaves + 1;
  1048. clock /= slaves + 1;
  1049. rd_unlock(&(klninfo->stat_lock));
  1050. snprintf(tmp, sizeof(tmp), "%2.0fC", cvtKlnToC(temp));
  1051. if (strlen(tmp) < 4)
  1052. strcat(tmp, " ");
  1053. tailsprintf(buf, siz, "%3dMHz %3d%% %s| ", (int)clock, fan*100/255, tmp);
  1054. }
  1055. static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
  1056. {
  1057. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1058. struct api_data *root = NULL;
  1059. char buf[32];
  1060. int dev, slaves;
  1061. if (klninfo->status == NULL)
  1062. return NULL;
  1063. rd_lock(&(klninfo->stat_lock));
  1064. slaves = klninfo->status[0].kline.ws.slavecount;
  1065. for (dev = 0; dev <= slaves; dev++) {
  1066. float fTemp = cvtKlnToC(klninfo->status[dev].kline.ws.temp);
  1067. sprintf(buf, "Temp %d", dev);
  1068. root = api_add_temp(root, buf, &fTemp, true);
  1069. double dClk = (double)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  1070. sprintf(buf, "Clock %d", dev);
  1071. root = api_add_freq(root, buf, &dClk, true);
  1072. unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].kline.cfg.fantarget / 255;
  1073. sprintf(buf, "Fan Percent %d", dev);
  1074. root = api_add_int(root, buf, (int *)(&iFan), true);
  1075. iFan = 0;
  1076. if (klninfo->status[dev].kline.ws.fanspeed > 0)
  1077. iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].kline.ws.fanspeed;
  1078. sprintf(buf, "Fan RPM %d", dev);
  1079. root = api_add_int(root, buf, (int *)(&iFan), true);
  1080. if (klninfo->devinfo[dev].chipstats != NULL) {
  1081. char data[2048];
  1082. char one[32];
  1083. int n;
  1084. sprintf(buf, "Nonces / Chip %d", dev);
  1085. data[0] = '\0';
  1086. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  1087. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n]);
  1088. strcat(data, one);
  1089. }
  1090. root = api_add_string(root, buf, data, true);
  1091. sprintf(buf, "Errors / Chip %d", dev);
  1092. data[0] = '\0';
  1093. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  1094. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].kline.ws.chipcount]);
  1095. strcat(data, one);
  1096. }
  1097. root = api_add_string(root, buf, data, true);
  1098. }
  1099. }
  1100. root = api_add_uint64(root, "Hash Count", &(klninfo->hashcount), true);
  1101. root = api_add_uint64(root, "Error Count", &(klninfo->errorcount), true);
  1102. root = api_add_uint64(root, "Noise Count", &(klninfo->noisecount), true);
  1103. root = api_add_int(root, "KLine Limit", &(klninfo->kline_count), true);
  1104. root = api_add_int(root, "KLine Used", &(klninfo->used_count), true);
  1105. root = api_add_elapsed(root, "KQue Delay Count", &(klninfo->delay_count), true);
  1106. root = api_add_elapsed(root, "KQue Delay Total", &(klninfo->delay_total), true);
  1107. root = api_add_elapsed(root, "KQue Delay Min", &(klninfo->delay_min), true);
  1108. root = api_add_elapsed(root, "KQue Delay Max", &(klninfo->delay_max), true);
  1109. double avg;
  1110. if (klninfo->delay_count == 0)
  1111. avg = 0;
  1112. else
  1113. avg = klninfo->delay_total / klninfo->delay_count;
  1114. root = api_add_diff(root, "KQue Delay Avg", &avg, true);
  1115. root = api_add_elapsed(root, "KQue Nonce Count", &(klninfo->nonce_count), true);
  1116. root = api_add_elapsed(root, "KQue Nonce Total", &(klninfo->nonce_total), true);
  1117. root = api_add_elapsed(root, "KQue Nonce Min", &(klninfo->nonce_min), true);
  1118. root = api_add_elapsed(root, "KQue Nonce Max", &(klninfo->nonce_max), true);
  1119. if (klninfo->nonce_count == 0)
  1120. avg = 0;
  1121. else
  1122. avg = klninfo->nonce_total / klninfo->nonce_count;
  1123. root = api_add_diff(root, "KQue Nonce Avg", &avg, true);
  1124. root = api_add_int(root, "WQue Size", &(klninfo->wque_size), true);
  1125. root = api_add_int(root, "WQue Cleared", &(klninfo->wque_cleared), true);
  1126. rd_unlock(&(klninfo->stat_lock));
  1127. return root;
  1128. }
  1129. struct device_drv klondike_drv = {
  1130. .dname = "Klondike",
  1131. .name = "KLN",
  1132. .drv_detect = klondike_detect,
  1133. .get_api_stats = klondike_api_stats,
  1134. // .get_statline_before = get_klondike_statline_before,
  1135. .get_stats = klondike_get_stats,
  1136. .identify_device = klondike_identify,
  1137. .thread_prepare = klondike_thread_prepare,
  1138. .thread_init = klondike_thread_init,
  1139. .minerloop = hash_queued_work,
  1140. .scanwork = klondike_scanwork,
  1141. .queue_full = klondike_queue_full,
  1142. .flush_work = klondike_flush_work,
  1143. .thread_shutdown = klondike_shutdown,
  1144. .thread_enable = klondike_thread_enable
  1145. };