driver-klondike.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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 "miner.h"
  26. #include "usbutils.h"
  27. #define K1 "K1"
  28. #define K16 "K16"
  29. #define K64 "K64"
  30. #define MIDSTATE_BYTES 32
  31. #define MERKLE_OFFSET 64
  32. #define MERKLE_BYTES 12
  33. #define REPLY_SIZE 15 // adequate for all types of replies
  34. #define MAX_KLINES 1024 // unhandled reply limit
  35. #define REPLY_WAIT_TIME 100 // poll interval for a cmd waiting it's reply
  36. #define CMD_REPLY_RETRIES 8 // how many retries for cmds
  37. #define MAX_WORK_COUNT 4 // for now, must be binary multiple and match firmware
  38. #define TACH_FACTOR 87890 // fan rpm divisor
  39. struct device_drv klondike_drv;
  40. typedef struct klondike_header {
  41. uint8_t cmd;
  42. uint8_t dev;
  43. uint8_t buf[REPLY_SIZE-2];
  44. } HEADER;
  45. #define K_2(_bytes) ((int)(_bytes[0]) + \
  46. ((int)(_bytes[1]) << 8))
  47. #define K_4(_bytes) ((uint64_t)(_bytes[0]) + \
  48. ((uint64_t)(_bytes[1]) << 8) + \
  49. ((uint64_t)(_bytes[2]) << 16) + \
  50. ((uint64_t)(_bytes[3]) << 24))
  51. #define K_SERIAL(_serial) K_4(_serial)
  52. #define K_HASHCOUNT(_hashcount) K_2(_hashcount)
  53. #define K_MAXCOUNT(_maxcount) K_2(_maxcount)
  54. #define K_NONCE(_nonce) K_4(_nonce)
  55. #define K_HASHCLOCK(_hashclock) K_2(_hashclock)
  56. #define SET_HASHCLOCK(_hashclock, _value) do { \
  57. (_hashclock)[0] = (uint8_t)((_value) & 0xff); \
  58. (_hashclock)[1] = (uint8_t)(((_value) >> 8) & 0xff); \
  59. } while(0)
  60. #define KSENDHD(_add) (sizeof(char) + sizeof(uint8_t) + _add)
  61. typedef struct klondike_id {
  62. uint8_t cmd;
  63. uint8_t dev;
  64. uint8_t version;
  65. uint8_t product[7];
  66. uint8_t serial[4];
  67. } IDENTITY;
  68. typedef struct klondike_status {
  69. uint8_t cmd;
  70. uint8_t dev;
  71. uint8_t state;
  72. uint8_t chipcount;
  73. uint8_t slavecount;
  74. uint8_t workqc;
  75. uint8_t workid;
  76. uint8_t temp;
  77. uint8_t fanspeed;
  78. uint8_t errorcount;
  79. uint8_t hashcount[2];
  80. uint8_t maxcount[2];
  81. uint8_t noise;
  82. } WORKSTATUS;
  83. typedef struct _worktask {
  84. uint8_t cmd;
  85. uint8_t dev;
  86. uint8_t workid;
  87. uint8_t midstate[32];
  88. uint8_t merkle[12];
  89. } WORKTASK;
  90. typedef struct _workresult {
  91. uint8_t cmd;
  92. uint8_t dev;
  93. uint8_t workid;
  94. uint8_t nonce[4];
  95. } WORKRESULT;
  96. typedef struct klondike_cfg {
  97. uint8_t cmd;
  98. uint8_t dev;
  99. uint8_t hashclock[2];
  100. uint8_t temptarget;
  101. uint8_t tempcritical;
  102. uint8_t fantarget;
  103. uint8_t pad2;
  104. } WORKCFG;
  105. typedef struct kline {
  106. union {
  107. HEADER hd;
  108. IDENTITY id;
  109. WORKSTATUS ws;
  110. WORKTASK wt;
  111. WORKRESULT wr;
  112. WORKCFG cfg;
  113. };
  114. } KLINE;
  115. typedef struct device_info {
  116. uint32_t noncecount;
  117. uint32_t nextworkid;
  118. uint16_t lasthashcount;
  119. uint64_t totalhashcount;
  120. uint32_t rangesize;
  121. uint32_t *chipstats;
  122. } DEVINFO;
  123. typedef struct klist {
  124. struct klist *prev;
  125. struct klist *next;
  126. KLINE kline;
  127. struct timeval tv_when;
  128. int block_seq;
  129. bool ready;
  130. bool working;
  131. } KLIST;
  132. struct klondike_info {
  133. bool shutdown;
  134. pthread_rwlock_t stat_lock;
  135. struct thr_info replies_thr;
  136. cglock_t klist_lock;
  137. KLIST *used;
  138. KLIST *free;
  139. int kline_count;
  140. int used_count;
  141. int block_seq;
  142. KLIST *status;
  143. DEVINFO *devinfo;
  144. KLIST *cfg;
  145. int noncecount;
  146. uint64_t hashcount;
  147. uint64_t errorcount;
  148. uint64_t noisecount;
  149. // us Delay from USB reply to being processed
  150. double delay_count;
  151. double delay_total;
  152. double delay_min;
  153. double delay_max;
  154. struct timeval tv_last_nonce_received;
  155. // Time from recieving one nonce to the next
  156. double nonce_count;
  157. double nonce_total;
  158. double nonce_min;
  159. double nonce_max;
  160. };
  161. static KLIST *new_klist_set(struct cgpu_info *klncgpu)
  162. {
  163. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  164. KLIST *klist = NULL;
  165. int i;
  166. klist = calloc(MAX_KLINES, sizeof(*klist));
  167. if (!klist)
  168. quit(1, "Failed to calloc klist - when old count=%d", klninfo->kline_count);
  169. klninfo->kline_count += MAX_KLINES;
  170. klist[0].prev = NULL;
  171. klist[0].next = &(klist[1]);
  172. for (i = 1; i < MAX_KLINES-1; i++) {
  173. klist[i].prev = &klist[i-1];
  174. klist[i].next = &klist[i+1];
  175. }
  176. klist[MAX_KLINES-1].prev = &(klist[MAX_KLINES-2]);
  177. klist[MAX_KLINES-1].next = NULL;
  178. return klist;
  179. }
  180. static KLIST *allocate_kitem(struct cgpu_info *klncgpu)
  181. {
  182. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  183. KLIST *kitem = NULL;
  184. int ran_out = 0;
  185. char errbuf[1024];
  186. cg_wlock(&klninfo->klist_lock);
  187. if (klninfo->free == NULL) {
  188. ran_out = klninfo->kline_count;
  189. klninfo->free = new_klist_set(klncgpu);
  190. snprintf(errbuf, sizeof(errbuf),
  191. "%s%i: KLINE count exceeded %d, now %d",
  192. klncgpu->drv->name, klncgpu->device_id,
  193. ran_out, klninfo->kline_count);
  194. }
  195. kitem = klninfo->free;
  196. klninfo->free = klninfo->free->next;
  197. if (klninfo->free)
  198. klninfo->free->prev = NULL;
  199. kitem->next = klninfo->used;
  200. kitem->prev = NULL;
  201. if (kitem->next)
  202. kitem->next->prev = kitem;
  203. klninfo->used = kitem;
  204. kitem->ready = false;
  205. kitem->working = false;
  206. memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
  207. klninfo->used_count++;
  208. cg_wunlock(&klninfo->klist_lock);
  209. if (ran_out > 0)
  210. applog(LOG_ERR, "%s", errbuf);
  211. return kitem;
  212. }
  213. static void release_kitem(struct cgpu_info *klncgpu, KLIST *kitem)
  214. {
  215. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  216. cg_wlock(&klninfo->klist_lock);
  217. if (kitem == klninfo->used)
  218. klninfo->used = kitem->next;
  219. if (kitem->next)
  220. kitem->next->prev = kitem->prev;
  221. if (kitem->prev)
  222. kitem->prev->next = kitem->next;
  223. kitem->next = klninfo->free;
  224. if (klninfo->free)
  225. klninfo->free->prev = kitem;
  226. kitem->prev = NULL;
  227. klninfo->free = kitem;
  228. klninfo->used_count--;
  229. cg_wunlock(&klninfo->klist_lock);
  230. }
  231. static double cvtKlnToC(uint8_t temp)
  232. {
  233. double Rt, stein, celsius;
  234. if (temp == 0)
  235. return 0.0;
  236. Rt = 1000.0 * 255.0 / (double)temp - 1000.0;
  237. stein = log(Rt / 2200.0) / 3987.0;
  238. stein += 1.0 / (double)(25.0 + 273.15);
  239. celsius = (1.0 / stein) - 273.15;
  240. // For display of bad data
  241. if (celsius < 0.0)
  242. celsius = 0.0;
  243. if (celsius > 200.0)
  244. celsius = 200.0;
  245. return celsius;
  246. }
  247. static int cvtCToKln(double deg)
  248. {
  249. double Rt, stein, temp;
  250. if (deg < 0.0)
  251. deg = 0.0;
  252. stein = 1.0 / (deg + 273.15);
  253. stein -= 1.0 / (double)(25.0 + 273.15);
  254. Rt = exp(stein * 3987.0) * 2200.0;
  255. if (Rt == -1000.0)
  256. Rt++;
  257. temp = 1000.0 * 256.0 / (Rt + 1000.0);
  258. if (temp > 255)
  259. temp = 255;
  260. if (temp < 0)
  261. temp = 0;
  262. return (int)temp;
  263. }
  264. // Change this to LOG_WARNING if you wish to always see the replies
  265. #define READ_DEBUG LOG_DEBUG
  266. //#define READ_DEBUG LOG_ERR
  267. static void display_kline(struct cgpu_info *klncgpu, KLINE *kline)
  268. {
  269. char *hexdata;
  270. switch (kline->hd.cmd) {
  271. case '=':
  272. applog(READ_DEBUG,
  273. "%s (%s) work [%c] dev=%d workid=%d"
  274. " nonce=0x%08x",
  275. klncgpu->drv->dname, klncgpu->device_path,
  276. kline->wr.cmd,
  277. (int)(kline->wr.dev),
  278. (int)(kline->wr.workid),
  279. (unsigned int)K_NONCE(kline->wr.nonce));
  280. break;
  281. case 'S':
  282. case 'W':
  283. case 'A':
  284. case 'E':
  285. applog(READ_DEBUG,
  286. "%s (%s) status [%c] dev=%d chips=%d"
  287. " slaves=%d workcq=%d workid=%d temp=%d fan=%d"
  288. " errors=%d hashes=%d max=%d noise=%d",
  289. klncgpu->drv->dname, klncgpu->device_path,
  290. kline->ws.cmd,
  291. (int)(kline->ws.dev),
  292. (int)(kline->ws.chipcount),
  293. (int)(kline->ws.slavecount),
  294. (int)(kline->ws.workqc),
  295. (int)(kline->ws.workid),
  296. (int)(kline->ws.temp),
  297. (int)(kline->ws.fanspeed),
  298. (int)(kline->ws.errorcount),
  299. K_HASHCOUNT(kline->ws.hashcount),
  300. K_MAXCOUNT(kline->ws.maxcount),
  301. (int)(kline->ws.noise));
  302. break;
  303. case 'C':
  304. applog(READ_DEBUG,
  305. "%s (%s) config [%c] dev=%d clock=%d"
  306. " temptarget=%d tempcrit=%d fan=%d",
  307. klncgpu->drv->dname, klncgpu->device_path,
  308. kline->cfg.cmd,
  309. (int)(kline->cfg.dev),
  310. K_HASHCLOCK(kline->cfg.hashclock),
  311. (int)(kline->cfg.temptarget),
  312. (int)(kline->cfg.tempcritical),
  313. (int)(kline->cfg.fantarget));
  314. break;
  315. case 'I':
  316. applog(READ_DEBUG,
  317. "%s (%s) info [%c] version=0x%02x prod=%.7s"
  318. " serial=0x%08x",
  319. klncgpu->drv->dname, klncgpu->device_path,
  320. kline->hd.cmd,
  321. (int)(kline->id.version),
  322. kline->id.product,
  323. (unsigned int)K_SERIAL(kline->id.serial));
  324. break;
  325. default:
  326. hexdata = bin2hex((unsigned char *)&(kline->hd.dev), REPLY_SIZE - 1);
  327. applog(LOG_ERR,
  328. "%s (%s) [%c:%s] unknown and ignored",
  329. klncgpu->drv->dname, klncgpu->device_path,
  330. kline->hd.cmd, hexdata);
  331. free(hexdata);
  332. break;
  333. }
  334. }
  335. static KLIST *SendCmdGetReply(struct cgpu_info *klncgpu, KLINE *kline, int datalen)
  336. {
  337. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  338. KLIST *kitem;
  339. int retries = CMD_REPLY_RETRIES;
  340. int err, amt, writ;
  341. if (klncgpu->usbinfo.nodev)
  342. return NULL;
  343. writ = KSENDHD(datalen);
  344. err = usb_write(klncgpu, (char *)kline, writ, &amt, C_REQUESTRESULTS);
  345. if (err < 0 || amt != writ) {
  346. applog(LOG_ERR, "%s (%s) Cmd:%c Dev:%d, write failed (%d:%d:%d)",
  347. klncgpu->drv->dname, klncgpu->device_path,
  348. kline->hd.cmd, (int)kline->hd.dev,
  349. writ, amt, err);
  350. }
  351. while (retries-- > 0 && klninfo->shutdown == false) {
  352. cgsleep_ms(REPLY_WAIT_TIME);
  353. cg_rlock(&klninfo->klist_lock);
  354. kitem = klninfo->used;
  355. while (kitem) {
  356. if (kitem->kline.hd.cmd == kline->hd.cmd &&
  357. kitem->kline.hd.dev == kline->hd.dev &&
  358. kitem->ready == true && kitem->working == false) {
  359. kitem->working = true;
  360. cg_runlock(&klninfo->klist_lock);
  361. return kitem;
  362. }
  363. kitem = kitem->next;
  364. }
  365. cg_runlock(&klninfo->klist_lock);
  366. }
  367. return NULL;
  368. }
  369. static bool klondike_get_stats(struct cgpu_info *klncgpu)
  370. {
  371. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  372. KLIST *kitem;
  373. KLINE kline;
  374. int slaves, dev;
  375. if (klncgpu->usbinfo.nodev || klninfo->status == NULL)
  376. return false;
  377. applog(LOG_DEBUG, "Klondike getting status");
  378. slaves = klninfo->status[0].kline.ws.slavecount;
  379. // loop thru devices and get status for each
  380. for (dev = 0; dev <= slaves; dev++) {
  381. kline.hd.cmd = 'S';
  382. kline.hd.dev = dev;
  383. kitem = SendCmdGetReply(klncgpu, &kline, 0);
  384. if (kitem != NULL) {
  385. wr_lock(&(klninfo->stat_lock));
  386. memcpy((void *)(&(klninfo->status[dev])), (void *)kitem, sizeof(*kitem));
  387. wr_unlock(&(klninfo->stat_lock));
  388. release_kitem(klncgpu, kitem);
  389. kitem = NULL;
  390. }
  391. }
  392. // todo: detect slavecount change and realloc space
  393. return true;
  394. }
  395. static bool klondike_init(struct cgpu_info *klncgpu)
  396. {
  397. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  398. KLIST *kitem;
  399. KLINE kline;
  400. int slaves, dev;
  401. kline.hd.cmd = 'S';
  402. kline.hd.dev = 0;
  403. kitem = SendCmdGetReply(klncgpu, &kline, 0);
  404. if (kitem == NULL)
  405. return false;
  406. slaves = kitem->kline.ws.slavecount;
  407. release_kitem(klncgpu, kitem);
  408. kitem = NULL;
  409. if (klninfo->status == NULL) {
  410. applog(LOG_DEBUG, "Klondike initializing data");
  411. // alloc space for status, devinfo and cfg for master and slaves
  412. klninfo->status = calloc(slaves+1, sizeof(KLIST));
  413. if (unlikely(!klninfo->status))
  414. quit(1, "Failed to calloc status array in klondke_get_stats");
  415. klninfo->devinfo = calloc(slaves+1, sizeof(DEVINFO));
  416. if (unlikely(!klninfo->devinfo))
  417. quit(1, "Failed to calloc devinfo array in klondke_get_stats");
  418. klninfo->cfg = calloc(slaves+1, sizeof(KLIST));
  419. if (unlikely(!klninfo->cfg))
  420. quit(1, "Failed to calloc cfg array in klondke_get_stats");
  421. }
  422. // zero init triggers read back only
  423. memset(&(kline.cfg), 0, sizeof(kline.cfg));
  424. kline.cfg.cmd = 'C';
  425. int size = 2;
  426. // boundaries are checked by device, with valid values returned
  427. if (opt_klondike_options != NULL) {
  428. int hashclock;
  429. double temp1, temp2;
  430. sscanf(opt_klondike_options, "%d:%lf:%lf:%"SCNu8,
  431. &hashclock,
  432. &temp1, &temp2,
  433. &kline.cfg.fantarget);
  434. SET_HASHCLOCK(kline.cfg.hashclock, hashclock);
  435. kline.cfg.temptarget = cvtCToKln(temp1);
  436. kline.cfg.tempcritical = cvtCToKln(temp2);
  437. kline.cfg.fantarget = (int)255*kline.cfg.fantarget/100;
  438. size = sizeof(kline.cfg) - 2;
  439. }
  440. for (dev = 0; dev <= slaves; dev++) {
  441. kline.cfg.dev = dev;
  442. kitem = SendCmdGetReply(klncgpu, &kline, size);
  443. if (kitem != NULL) {
  444. memcpy((void *)&(klninfo->cfg[dev]), kitem, sizeof(*kitem));
  445. applog(LOG_WARNING, "Klondike config (%d: Clk: %d, T:%.0lf, C:%.0lf, F:%d)",
  446. dev, K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock),
  447. cvtKlnToC(klninfo->cfg[dev].kline.cfg.temptarget),
  448. cvtKlnToC(klninfo->cfg[dev].kline.cfg.tempcritical),
  449. (int)100*klninfo->cfg[dev].kline.cfg.fantarget/256);
  450. release_kitem(klncgpu, kitem);
  451. kitem = NULL;
  452. }
  453. }
  454. klondike_get_stats(klncgpu);
  455. for (dev = 0; dev <= slaves; dev++) {
  456. klninfo->devinfo[dev].rangesize = ((uint64_t)1<<32) / klninfo->status[dev].kline.ws.chipcount;
  457. klninfo->devinfo[dev].chipstats = calloc(klninfo->status[dev].kline.ws.chipcount*2 , sizeof(uint32_t));
  458. }
  459. int tries = 2;
  460. bool ok = false;
  461. kline.hd.cmd = 'E';
  462. kline.hd.dev = 0;
  463. kline.hd.buf[0] = '1';
  464. while (tries-- > 0) {
  465. kitem = SendCmdGetReply(klncgpu, &kline, 1);
  466. if (kitem) {
  467. release_kitem(klncgpu, kitem);
  468. kitem = NULL;
  469. ok = true;
  470. break;
  471. }
  472. }
  473. if (!ok)
  474. applog(LOG_ERR, "%s%i: failed to enable", klncgpu->drv->name, klncgpu->device_id);
  475. return ok;
  476. }
  477. static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  478. {
  479. struct cgpu_info *klncgpu = usb_alloc_cgpu(&klondike_drv, 1);
  480. struct klondike_info *klninfo = NULL;
  481. if (unlikely(!klncgpu))
  482. quit(1, "Failed to calloc klncgpu in klondike_detect_one");
  483. klninfo = calloc(1, sizeof(*klninfo));
  484. if (unlikely(!klninfo))
  485. quit(1, "Failed to calloc klninfo in klondke_detect_one");
  486. klncgpu->device_data = (void *)klninfo;
  487. klninfo->free = new_klist_set(klncgpu);
  488. if (usb_init(klncgpu, dev, found)) {
  489. int sent, recd, err;
  490. KLIST kitem;
  491. int attempts = 0;
  492. while (attempts++ < 3) {
  493. err = usb_write(klncgpu, "I", 2, &sent, C_REQUESTRESULTS);
  494. if (err < 0 || sent != 2) {
  495. applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)",
  496. klncgpu->drv->dname,
  497. klncgpu->device_path,
  498. sent, err);
  499. }
  500. cgsleep_ms(REPLY_WAIT_TIME*10);
  501. err = usb_read(klncgpu, (char *)&(kitem.kline), REPLY_SIZE, &recd, C_GETRESULTS);
  502. if (err < 0) {
  503. applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)",
  504. klncgpu->drv->dname,
  505. klncgpu->device_path,
  506. recd, err);
  507. } else if (recd < 1) {
  508. applog(LOG_ERR, "%s (%s) detect empty reply (%d)",
  509. klncgpu->drv->dname,
  510. klncgpu->device_path,
  511. recd);
  512. } else if (kitem.kline.hd.cmd == 'I' && kitem.kline.hd.dev == 0) {
  513. display_kline(klncgpu, &kitem.kline);
  514. applog(LOG_DEBUG, "%s (%s) detect successful",
  515. klncgpu->drv->dname,
  516. klncgpu->device_path);
  517. if (!add_cgpu(klncgpu))
  518. break;
  519. update_usb_stats(klncgpu);
  520. applog(LOG_DEBUG, "Klondike cgpu added");
  521. cglock_init(&klninfo->klist_lock);
  522. return true;
  523. }
  524. }
  525. usb_uninit(klncgpu);
  526. }
  527. free(klninfo->free);
  528. free(klninfo);
  529. free(klncgpu);
  530. return false;
  531. }
  532. static void klondike_detect(bool __maybe_unused hotplug)
  533. {
  534. usb_detect(&klondike_drv, klondike_detect_one);
  535. }
  536. static void klondike_identify(__maybe_unused struct cgpu_info *klncgpu)
  537. {
  538. /*
  539. KLINE kline;
  540. kline.hd.cmd = 'I';
  541. kline.hd.dev = 0;
  542. SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  543. */
  544. }
  545. static void klondike_check_nonce(struct cgpu_info *klncgpu, KLIST *kitem)
  546. {
  547. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  548. struct work *work, *tmp;
  549. KLINE *kline = &(kitem->kline);
  550. struct timeval tv_now;
  551. double us_diff;
  552. uint32_t nonce = K_NONCE(kline->wr.nonce) - 0xC0;
  553. applog(LOG_DEBUG, "Klondike FOUND NONCE (%02x:%08x)",
  554. kline->wr.workid, (unsigned int)nonce);
  555. HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
  556. if (work->queued && (work->subid == (kline->wr.dev*256 + kline->wr.workid))) {
  557. wr_lock(&(klninfo->stat_lock));
  558. klninfo->devinfo[kline->wr.dev].noncecount++;
  559. klninfo->noncecount++;
  560. wr_unlock(&(klninfo->stat_lock));
  561. // kline->wr.nonce = le32toh(kline->wr.nonce - 0xC0);
  562. applog(LOG_DEBUG, "Klondike SUBMIT NONCE (%02x:%08x)",
  563. kline->wr.workid, (unsigned int)nonce);
  564. cgtime(&tv_now);
  565. bool ok = submit_nonce(klncgpu->thr[0], work, nonce);
  566. applog(LOG_DEBUG, "Klondike chip stats %d, %08x, %d, %d",
  567. kline->wr.dev, (unsigned int)nonce,
  568. klninfo->devinfo[kline->wr.dev].rangesize,
  569. klninfo->status[kline->wr.dev].kline.ws.chipcount);
  570. klninfo->devinfo[kline->wr.dev].chipstats[(nonce / klninfo->devinfo[kline->wr.dev].rangesize) + (ok ? 0 : klninfo->status[kline->wr.dev].kline.ws.chipcount)]++;
  571. us_diff = us_tdiff(&tv_now, &(kitem->tv_when));
  572. if (klninfo->delay_count == 0) {
  573. klninfo->delay_min = us_diff;
  574. klninfo->delay_max = us_diff;
  575. } else {
  576. if (klninfo->delay_min > us_diff)
  577. klninfo->delay_min = us_diff;
  578. if (klninfo->delay_max < us_diff)
  579. klninfo->delay_max = us_diff;
  580. }
  581. klninfo->delay_count++;
  582. klninfo->delay_total += us_diff;
  583. if (klninfo->nonce_count > 0) {
  584. us_diff = us_tdiff(&(kitem->tv_when), &(klninfo->tv_last_nonce_received));
  585. if (klninfo->nonce_count == 1) {
  586. klninfo->nonce_min = us_diff;
  587. klninfo->nonce_max = us_diff;
  588. } else {
  589. if (klninfo->nonce_min > us_diff)
  590. klninfo->nonce_min = us_diff;
  591. if (klninfo->nonce_max < us_diff)
  592. klninfo->nonce_max = us_diff;
  593. }
  594. klninfo->nonce_total += us_diff;
  595. }
  596. klninfo->nonce_count++;
  597. memcpy(&(klninfo->tv_last_nonce_received), &(kitem->tv_when),
  598. sizeof(klninfo->tv_last_nonce_received));
  599. return;
  600. }
  601. }
  602. applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored",
  603. klncgpu->drv->name, klncgpu->device_id,
  604. kline->wr.dev, kline->wr.workid, (unsigned int)nonce);
  605. //inc_hw_errors(klncgpu->thr[0]);
  606. }
  607. // thread to keep looking for replies
  608. static void *klondike_get_replies(void *userdata)
  609. {
  610. struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
  611. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  612. KLIST *kitem = NULL;
  613. char *hexdata;
  614. int err, recd;
  615. applog(LOG_ERR, "Klondike listening for replies");
  616. while (klninfo->shutdown == false) {
  617. if (klncgpu->usbinfo.nodev)
  618. return NULL;
  619. if (kitem == NULL)
  620. kitem = allocate_kitem(klncgpu);
  621. else
  622. memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
  623. err = usb_read(klncgpu, (char *)&(kitem->kline), REPLY_SIZE, &recd, C_GETRESULTS);
  624. if (!err && recd == REPLY_SIZE) {
  625. cgtime(&(kitem->tv_when));
  626. kitem->block_seq = klninfo->block_seq;
  627. if (opt_log_level <= READ_DEBUG) {
  628. hexdata = bin2hex((unsigned char *)&(kitem->kline.hd.dev), recd-1);
  629. applog(READ_DEBUG, "%s (%s) reply [%c:%s]",
  630. klncgpu->drv->dname, klncgpu->device_path,
  631. kitem->kline.hd.cmd, hexdata);
  632. free(hexdata);
  633. }
  634. switch (kitem->kline.hd.cmd) {
  635. case '=':
  636. klondike_check_nonce(klncgpu, kitem);
  637. display_kline(klncgpu, &kitem->kline);
  638. break;
  639. case 'S':
  640. case 'W':
  641. case 'A':
  642. case 'E':
  643. wr_lock(&(klninfo->stat_lock));
  644. klninfo->errorcount += kitem->kline.ws.errorcount;
  645. klninfo->noisecount += kitem->kline.ws.noise;
  646. wr_unlock(&(klninfo->stat_lock));
  647. display_kline(klncgpu, &kitem->kline);
  648. kitem->ready = true;
  649. kitem = NULL;
  650. break;
  651. case 'C':
  652. display_kline(klncgpu, &kitem->kline);
  653. kitem->ready = true;
  654. kitem = NULL;
  655. break;
  656. case 'I':
  657. display_kline(klncgpu, &kitem->kline);
  658. kitem->ready = true;
  659. kitem = NULL;
  660. break;
  661. default:
  662. display_kline(klncgpu, &kitem->kline);
  663. break;
  664. }
  665. }
  666. }
  667. return NULL;
  668. }
  669. static void klondike_flush_work(struct cgpu_info *klncgpu)
  670. {
  671. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  672. KLIST *kitem;
  673. KLINE kline;
  674. int slaves, dev;
  675. klninfo->block_seq++;
  676. applog(LOG_DEBUG, "Klondike flushing work");
  677. slaves = klninfo->status[0].kline.ws.slavecount;
  678. kline.hd.cmd = 'A';
  679. for (dev = 0; dev <= slaves; dev++) {
  680. kline.hd.dev = dev;
  681. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  682. if (kitem != NULL) {
  683. wr_lock(&(klninfo->stat_lock));
  684. memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(*kitem));
  685. wr_unlock(&(klninfo->stat_lock));
  686. release_kitem(klncgpu, kitem);
  687. kitem = NULL;
  688. }
  689. }
  690. }
  691. static bool klondike_thread_prepare(struct thr_info *thr)
  692. {
  693. struct cgpu_info *klncgpu = thr->cgpu;
  694. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  695. if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) {
  696. applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id);
  697. return false;
  698. }
  699. pthread_detach(klninfo->replies_thr.pth);
  700. // let the listening get started
  701. cgsleep_ms(100);
  702. return klondike_init(klncgpu);
  703. }
  704. static bool klondike_thread_init(struct thr_info *thr)
  705. {
  706. struct cgpu_info *klncgpu = thr->cgpu;
  707. if (klncgpu->usbinfo.nodev)
  708. return false;
  709. klondike_flush_work(klncgpu);
  710. return true;
  711. }
  712. static void klondike_shutdown(struct thr_info *thr)
  713. {
  714. struct cgpu_info *klncgpu = thr->cgpu;
  715. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  716. KLIST *kitem;
  717. KLINE kline;
  718. int dev;
  719. applog(LOG_DEBUG, "Klondike shutting down work");
  720. kline.hd.cmd = 'E';
  721. for (dev = 0; dev <= klninfo->status[0].kline.ws.slavecount; dev++) {
  722. kline.hd.dev = dev;
  723. kline.hd.buf[0] = '0';
  724. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
  725. if (kitem)
  726. release_kitem(klncgpu, kitem);
  727. }
  728. klncgpu->shutdown = klninfo->shutdown = true;
  729. }
  730. static void klondike_thread_enable(struct thr_info *thr)
  731. {
  732. struct cgpu_info *klncgpu = thr->cgpu;
  733. if (klncgpu->usbinfo.nodev)
  734. return;
  735. /*
  736. KLINE kline;
  737. kline.hd.cmd = 'E';
  738. kline.hd.dev = dev;
  739. kline.hd.buf[0] = '0';
  740. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
  741. */
  742. }
  743. static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
  744. {
  745. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  746. struct work *tmp;
  747. KLINE kline;
  748. if (klncgpu->usbinfo.nodev)
  749. return false;
  750. kline.wt.cmd = 'W';
  751. kline.wt.dev = dev;
  752. memcpy(kline.wt.midstate, work->midstate, MIDSTATE_BYTES);
  753. memcpy(kline.wt.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  754. kline.wt.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
  755. work->subid = dev*256 + kline.wt.workid;
  756. if (opt_log_level <= LOG_DEBUG) {
  757. char *hexdata = bin2hex((void *)&kline.wt, sizeof(kline.wt));
  758. applog(LOG_DEBUG, "WORKDATA: %s", hexdata);
  759. free(hexdata);
  760. }
  761. applog(LOG_DEBUG, "Klondike sending work (%d:%02x)", dev, kline.wt.workid);
  762. KLIST *kitem = SendCmdGetReply(klncgpu, &kline, sizeof(kline.wt));
  763. if (kitem != NULL) {
  764. wr_lock(&(klninfo->stat_lock));
  765. memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(*kitem));
  766. wr_unlock(&(klninfo->stat_lock));
  767. release_kitem(klncgpu, kitem);
  768. kitem = NULL;
  769. // remove old work
  770. HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
  771. if (work->queued && (work->subid == (int)(dev*256 + ((klninfo->devinfo[dev].nextworkid-2*MAX_WORK_COUNT) & 0xFF))))
  772. work_completed(klncgpu, work);
  773. }
  774. return true;
  775. }
  776. return false;
  777. }
  778. static bool klondike_queue_full(struct cgpu_info *klncgpu)
  779. {
  780. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  781. struct work *work = NULL;
  782. int dev, queued, slaves;
  783. slaves = klninfo->status[0].kline.ws.slavecount;
  784. for (queued = 0; queued < MAX_WORK_COUNT-1; queued++)
  785. for (dev = 0; dev <= slaves; dev++)
  786. if (klninfo->status[dev].kline.ws.workqc <= queued) {
  787. if (!work)
  788. work = get_queued(klncgpu);
  789. if (unlikely(!work))
  790. return false;
  791. if (klondike_send_work(klncgpu, dev, work)) {
  792. work = NULL;
  793. break;
  794. }
  795. }
  796. return true;
  797. }
  798. static int64_t klondike_scanwork(struct thr_info *thr)
  799. {
  800. struct cgpu_info *klncgpu = thr->cgpu;
  801. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  802. int64_t newhashcount = 0;
  803. int dev, slaves;
  804. if (klncgpu->usbinfo.nodev)
  805. return -1;
  806. restart_wait(thr, 200);
  807. if (klninfo->status != NULL) {
  808. rd_lock(&(klninfo->stat_lock));
  809. slaves = klninfo->status[0].kline.ws.slavecount;
  810. for (dev = 0; dev <= slaves; dev++) {
  811. uint64_t newhashdev = 0, hashcount;
  812. int maxcount;
  813. hashcount = K_HASHCOUNT(klninfo->status[dev].kline.ws.hashcount);
  814. maxcount = K_MAXCOUNT(klninfo->status[dev].kline.ws.maxcount);
  815. if (klninfo->devinfo[dev].lasthashcount > hashcount) // todo: chg this to check workid for wrapped instead
  816. newhashdev += maxcount; // hash counter wrapped
  817. newhashdev += hashcount - klninfo->devinfo[dev].lasthashcount;
  818. klninfo->devinfo[dev].lasthashcount = hashcount;
  819. if (maxcount != 0)
  820. klninfo->hashcount += (newhashdev << 32) / maxcount;
  821. // todo: check stats for critical conditions
  822. }
  823. newhashcount += 0xffffffffull * (uint64_t)klninfo->noncecount;
  824. klninfo->noncecount = 0;
  825. rd_unlock(&(klninfo->stat_lock));
  826. }
  827. return newhashcount;
  828. }
  829. static void get_klondike_statline_before(char *buf, size_t siz, struct cgpu_info *klncgpu)
  830. {
  831. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  832. uint8_t temp = 0xFF;
  833. uint16_t fan = 0;
  834. uint16_t clock = 0;
  835. int dev, slaves;
  836. char tmp[16];
  837. if (klninfo->status == NULL) {
  838. blank_get_statline_before(buf, siz, klncgpu);
  839. return;
  840. }
  841. rd_lock(&(klninfo->stat_lock));
  842. slaves = klninfo->status[0].kline.ws.slavecount;
  843. for (dev = 0; dev <= slaves; dev++) {
  844. if (klninfo->status[dev].kline.ws.temp < temp)
  845. temp = klninfo->status[dev].kline.ws.temp;
  846. fan += klninfo->cfg[dev].kline.cfg.fantarget;
  847. clock += (uint16_t)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  848. }
  849. fan /= slaves + 1;
  850. clock /= slaves + 1;
  851. rd_unlock(&(klninfo->stat_lock));
  852. snprintf(tmp, sizeof(tmp), "%2.0fC", cvtKlnToC(temp));
  853. if (strlen(tmp) < 4)
  854. strcat(tmp, " ");
  855. tailsprintf(buf, siz, "%3dMHz %3d%% %s| ", (int)clock, fan*100/255, tmp);
  856. }
  857. static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
  858. {
  859. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  860. struct api_data *root = NULL;
  861. char buf[32];
  862. int dev, slaves;
  863. if (klninfo->status == NULL)
  864. return NULL;
  865. rd_lock(&(klninfo->stat_lock));
  866. slaves = klninfo->status[0].kline.ws.slavecount;
  867. for (dev = 0; dev <= slaves; dev++) {
  868. float fTemp = cvtKlnToC(klninfo->status[dev].kline.ws.temp);
  869. sprintf(buf, "Temp %d", dev);
  870. root = api_add_temp(root, buf, &fTemp, true);
  871. double dClk = (double)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  872. sprintf(buf, "Clock %d", dev);
  873. root = api_add_freq(root, buf, &dClk, true);
  874. unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].kline.cfg.fantarget / 255;
  875. sprintf(buf, "Fan Percent %d", dev);
  876. root = api_add_int(root, buf, (int *)(&iFan), true);
  877. iFan = 0;
  878. if (klninfo->status[dev].kline.ws.fanspeed > 0)
  879. iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].kline.ws.fanspeed;
  880. sprintf(buf, "Fan RPM %d", dev);
  881. root = api_add_int(root, buf, (int *)(&iFan), true);
  882. if (klninfo->devinfo[dev].chipstats != NULL) {
  883. char data[2048];
  884. char one[32];
  885. int n;
  886. sprintf(buf, "Nonces / Chip %d", dev);
  887. data[0] = '\0';
  888. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  889. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n]);
  890. strcat(data, one);
  891. }
  892. root = api_add_string(root, buf, data, true);
  893. sprintf(buf, "Errors / Chip %d", dev);
  894. data[0] = '\0';
  895. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  896. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].kline.ws.chipcount]);
  897. strcat(data, one);
  898. }
  899. root = api_add_string(root, buf, data, true);
  900. }
  901. }
  902. root = api_add_uint64(root, "Hash Count", &(klninfo->hashcount), true);
  903. root = api_add_uint64(root, "Error Count", &(klninfo->errorcount), true);
  904. root = api_add_uint64(root, "Noise Count", &(klninfo->noisecount), true);
  905. root = api_add_int(root, "KLine Limit", &(klninfo->kline_count), true);
  906. root = api_add_int(root, "KLine Used", &(klninfo->used_count), true);
  907. root = api_add_elapsed(root, "KQue Delay Count", &(klninfo->delay_count), true);
  908. root = api_add_elapsed(root, "KQue Delay Total", &(klninfo->delay_total), true);
  909. root = api_add_elapsed(root, "KQue Delay Min", &(klninfo->delay_min), true);
  910. root = api_add_elapsed(root, "KQue Delay Max", &(klninfo->delay_max), true);
  911. double avg;
  912. if (klninfo->delay_count == 0)
  913. avg = 0;
  914. else
  915. avg = klninfo->delay_total / klninfo->delay_count;
  916. root = api_add_diff(root, "KQue Delay Avg", &avg, true);
  917. root = api_add_elapsed(root, "KQue Nonce Count", &(klninfo->nonce_count), true);
  918. root = api_add_elapsed(root, "KQue Nonce Total", &(klninfo->nonce_total), true);
  919. root = api_add_elapsed(root, "KQue Nonce Min", &(klninfo->nonce_min), true);
  920. root = api_add_elapsed(root, "KQue Nonce Max", &(klninfo->nonce_max), true);
  921. if (klninfo->nonce_count == 0)
  922. avg = 0;
  923. else
  924. avg = klninfo->nonce_total / klninfo->nonce_count;
  925. root = api_add_diff(root, "KQue Nonce Avg", &avg, true);
  926. rd_unlock(&(klninfo->stat_lock));
  927. return root;
  928. }
  929. struct device_drv klondike_drv = {
  930. .drv_id = DRIVER_klondike,
  931. .dname = "Klondike",
  932. .name = "KLN",
  933. .drv_detect = klondike_detect,
  934. .get_api_stats = klondike_api_stats,
  935. .get_statline_before = get_klondike_statline_before,
  936. .get_stats = klondike_get_stats,
  937. .identify_device = klondike_identify,
  938. .thread_prepare = klondike_thread_prepare,
  939. .thread_init = klondike_thread_init,
  940. .hash_work = hash_queued_work,
  941. .scanwork = klondike_scanwork,
  942. .queue_full = klondike_queue_full,
  943. .flush_work = klondike_flush_work,
  944. .thread_shutdown = klondike_shutdown,
  945. .thread_enable = klondike_thread_enable
  946. };