driver-klondike.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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. us_diff = us_tdiff(&(kitem->tv_when), &(klninfo->tv_last_nonce_received));
  584. if (klninfo->nonce_count == 0) {
  585. klninfo->nonce_min = us_diff;
  586. klninfo->nonce_max = us_diff;
  587. } else {
  588. if (klninfo->nonce_min > us_diff)
  589. klninfo->nonce_min = us_diff;
  590. if (klninfo->nonce_max < us_diff)
  591. klninfo->nonce_max = us_diff;
  592. }
  593. klninfo->nonce_count++;
  594. klninfo->nonce_total += us_diff;
  595. memcpy(&(klninfo->tv_last_nonce_received), &(kitem->tv_when),
  596. sizeof(klninfo->tv_last_nonce_received));
  597. return;
  598. }
  599. }
  600. applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored",
  601. klncgpu->drv->name, klncgpu->device_id,
  602. kline->wr.dev, kline->wr.workid, (unsigned int)nonce);
  603. //inc_hw_errors(klncgpu->thr[0]);
  604. }
  605. // thread to keep looking for replies
  606. static void *klondike_get_replies(void *userdata)
  607. {
  608. struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
  609. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  610. KLIST *kitem = NULL;
  611. char *hexdata;
  612. int err, recd;
  613. applog(LOG_ERR, "Klondike listening for replies");
  614. while (klninfo->shutdown == false) {
  615. if (klncgpu->usbinfo.nodev)
  616. return NULL;
  617. if (kitem == NULL)
  618. kitem = allocate_kitem(klncgpu);
  619. else
  620. memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
  621. err = usb_read(klncgpu, (char *)&(kitem->kline), REPLY_SIZE, &recd, C_GETRESULTS);
  622. if (!err && recd == REPLY_SIZE) {
  623. cgtime(&(kitem->tv_when));
  624. kitem->block_seq = klninfo->block_seq;
  625. if (opt_log_level <= READ_DEBUG) {
  626. hexdata = bin2hex((unsigned char *)&(kitem->kline.hd.dev), recd-1);
  627. applog(READ_DEBUG, "%s (%s) reply [%c:%s]",
  628. klncgpu->drv->dname, klncgpu->device_path,
  629. kitem->kline.hd.cmd, hexdata);
  630. free(hexdata);
  631. }
  632. switch (kitem->kline.hd.cmd) {
  633. case '=':
  634. klondike_check_nonce(klncgpu, kitem);
  635. display_kline(klncgpu, &kitem->kline);
  636. break;
  637. case 'S':
  638. case 'W':
  639. case 'A':
  640. case 'E':
  641. wr_lock(&(klninfo->stat_lock));
  642. klninfo->errorcount += kitem->kline.ws.errorcount;
  643. klninfo->noisecount += kitem->kline.ws.noise;
  644. wr_unlock(&(klninfo->stat_lock));
  645. display_kline(klncgpu, &kitem->kline);
  646. kitem->ready = true;
  647. kitem = NULL;
  648. break;
  649. case 'C':
  650. display_kline(klncgpu, &kitem->kline);
  651. kitem->ready = true;
  652. kitem = NULL;
  653. break;
  654. case 'I':
  655. display_kline(klncgpu, &kitem->kline);
  656. kitem->ready = true;
  657. kitem = NULL;
  658. break;
  659. default:
  660. display_kline(klncgpu, &kitem->kline);
  661. break;
  662. }
  663. }
  664. }
  665. return NULL;
  666. }
  667. static void klondike_flush_work(struct cgpu_info *klncgpu)
  668. {
  669. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  670. KLIST *kitem;
  671. KLINE kline;
  672. int slaves, dev;
  673. klninfo->block_seq++;
  674. applog(LOG_DEBUG, "Klondike flushing work");
  675. slaves = klninfo->status[0].kline.ws.slavecount;
  676. kline.hd.cmd = 'A';
  677. for (dev = 0; dev <= slaves; dev++) {
  678. kline.hd.dev = dev;
  679. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  680. if (kitem != NULL) {
  681. wr_lock(&(klninfo->stat_lock));
  682. memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(*kitem));
  683. wr_unlock(&(klninfo->stat_lock));
  684. release_kitem(klncgpu, kitem);
  685. kitem = NULL;
  686. }
  687. }
  688. }
  689. static bool klondike_thread_prepare(struct thr_info *thr)
  690. {
  691. struct cgpu_info *klncgpu = thr->cgpu;
  692. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  693. if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) {
  694. applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id);
  695. return false;
  696. }
  697. pthread_detach(klninfo->replies_thr.pth);
  698. // let the listening get started
  699. cgsleep_ms(100);
  700. return klondike_init(klncgpu);
  701. }
  702. static bool klondike_thread_init(struct thr_info *thr)
  703. {
  704. struct cgpu_info *klncgpu = thr->cgpu;
  705. if (klncgpu->usbinfo.nodev)
  706. return false;
  707. klondike_flush_work(klncgpu);
  708. return true;
  709. }
  710. static void klondike_shutdown(struct thr_info *thr)
  711. {
  712. struct cgpu_info *klncgpu = thr->cgpu;
  713. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  714. KLIST *kitem;
  715. KLINE kline;
  716. int dev;
  717. applog(LOG_DEBUG, "Klondike shutting down work");
  718. kline.hd.cmd = 'E';
  719. for (dev = 0; dev <= klninfo->status[0].kline.ws.slavecount; dev++) {
  720. kline.hd.dev = dev;
  721. kline.hd.buf[0] = '0';
  722. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
  723. if (kitem)
  724. release_kitem(klncgpu, kitem);
  725. }
  726. klncgpu->shutdown = klninfo->shutdown = true;
  727. }
  728. static void klondike_thread_enable(struct thr_info *thr)
  729. {
  730. struct cgpu_info *klncgpu = thr->cgpu;
  731. if (klncgpu->usbinfo.nodev)
  732. return;
  733. /*
  734. KLINE kline;
  735. kline.hd.cmd = 'E';
  736. kline.hd.dev = dev;
  737. kline.hd.buf[0] = '0';
  738. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
  739. */
  740. }
  741. static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
  742. {
  743. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  744. struct work *tmp;
  745. KLINE kline;
  746. if (klncgpu->usbinfo.nodev)
  747. return false;
  748. kline.wt.cmd = 'W';
  749. kline.wt.dev = dev;
  750. memcpy(kline.wt.midstate, work->midstate, MIDSTATE_BYTES);
  751. memcpy(kline.wt.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  752. kline.wt.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
  753. work->subid = dev*256 + kline.wt.workid;
  754. if (opt_log_level <= LOG_DEBUG) {
  755. char *hexdata = bin2hex((void *)&kline.wt, sizeof(kline.wt));
  756. applog(LOG_DEBUG, "WORKDATA: %s", hexdata);
  757. free(hexdata);
  758. }
  759. applog(LOG_DEBUG, "Klondike sending work (%d:%02x)", dev, kline.wt.workid);
  760. KLIST *kitem = SendCmdGetReply(klncgpu, &kline, sizeof(kline.wt));
  761. if (kitem != NULL) {
  762. wr_lock(&(klninfo->stat_lock));
  763. memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(*kitem));
  764. wr_unlock(&(klninfo->stat_lock));
  765. release_kitem(klncgpu, kitem);
  766. kitem = NULL;
  767. // remove old work
  768. HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
  769. if (work->queued && (work->subid == (int)(dev*256 + ((klninfo->devinfo[dev].nextworkid-2*MAX_WORK_COUNT) & 0xFF))))
  770. work_completed(klncgpu, work);
  771. }
  772. return true;
  773. }
  774. return false;
  775. }
  776. static bool klondike_queue_full(struct cgpu_info *klncgpu)
  777. {
  778. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  779. struct work *work = NULL;
  780. int dev, queued, slaves;
  781. slaves = klninfo->status[0].kline.ws.slavecount;
  782. for (queued = 0; queued < MAX_WORK_COUNT-1; queued++)
  783. for (dev = 0; dev <= slaves; dev++)
  784. if (klninfo->status[dev].kline.ws.workqc <= queued) {
  785. if (!work)
  786. work = get_queued(klncgpu);
  787. if (unlikely(!work))
  788. return false;
  789. if (klondike_send_work(klncgpu, dev, work)) {
  790. work = NULL;
  791. break;
  792. }
  793. }
  794. return true;
  795. }
  796. static int64_t klondike_scanwork(struct thr_info *thr)
  797. {
  798. struct cgpu_info *klncgpu = thr->cgpu;
  799. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  800. int64_t newhashcount = 0;
  801. int dev, slaves;
  802. if (klncgpu->usbinfo.nodev)
  803. return -1;
  804. restart_wait(thr, 200);
  805. if (klninfo->status != NULL) {
  806. rd_lock(&(klninfo->stat_lock));
  807. slaves = klninfo->status[0].kline.ws.slavecount;
  808. for (dev = 0; dev <= slaves; dev++) {
  809. uint64_t newhashdev = 0, hashcount;
  810. int maxcount;
  811. hashcount = K_HASHCOUNT(klninfo->status[dev].kline.ws.hashcount);
  812. maxcount = K_MAXCOUNT(klninfo->status[dev].kline.ws.maxcount);
  813. if (klninfo->devinfo[dev].lasthashcount > hashcount) // todo: chg this to check workid for wrapped instead
  814. newhashdev += maxcount; // hash counter wrapped
  815. newhashdev += hashcount - klninfo->devinfo[dev].lasthashcount;
  816. klninfo->devinfo[dev].lasthashcount = hashcount;
  817. if (maxcount != 0)
  818. klninfo->hashcount += (newhashdev << 32) / maxcount;
  819. // todo: check stats for critical conditions
  820. }
  821. newhashcount += 0xffffffffull * (uint64_t)klninfo->noncecount;
  822. klninfo->noncecount = 0;
  823. rd_unlock(&(klninfo->stat_lock));
  824. }
  825. return newhashcount;
  826. }
  827. static void get_klondike_statline_before(char *buf, size_t siz, struct cgpu_info *klncgpu)
  828. {
  829. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  830. uint8_t temp = 0xFF;
  831. uint16_t fan = 0;
  832. uint16_t clock = 0;
  833. int dev, slaves;
  834. char tmp[16];
  835. if (klninfo->status == NULL) {
  836. blank_get_statline_before(buf, siz, klncgpu);
  837. return;
  838. }
  839. rd_lock(&(klninfo->stat_lock));
  840. slaves = klninfo->status[0].kline.ws.slavecount;
  841. for (dev = 0; dev <= slaves; dev++) {
  842. if (klninfo->status[dev].kline.ws.temp < temp)
  843. temp = klninfo->status[dev].kline.ws.temp;
  844. fan += klninfo->cfg[dev].kline.cfg.fantarget;
  845. clock += (uint16_t)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  846. }
  847. fan /= slaves + 1;
  848. clock /= slaves + 1;
  849. rd_unlock(&(klninfo->stat_lock));
  850. snprintf(tmp, sizeof(tmp), "%2.0fC", cvtKlnToC(temp));
  851. if (strlen(tmp) < 4)
  852. strcat(tmp, " ");
  853. tailsprintf(buf, siz, "%3dMHz %3d%% %s| ", (int)clock, fan*100/255, tmp);
  854. }
  855. static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
  856. {
  857. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  858. struct api_data *root = NULL;
  859. char buf[32];
  860. int dev, slaves;
  861. if (klninfo->status == NULL)
  862. return NULL;
  863. rd_lock(&(klninfo->stat_lock));
  864. slaves = klninfo->status[0].kline.ws.slavecount;
  865. for (dev = 0; dev <= slaves; dev++) {
  866. float fTemp = cvtKlnToC(klninfo->status[dev].kline.ws.temp);
  867. sprintf(buf, "Temp %d", dev);
  868. root = api_add_temp(root, buf, &fTemp, true);
  869. double dClk = (double)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  870. sprintf(buf, "Clock %d", dev);
  871. root = api_add_freq(root, buf, &dClk, true);
  872. unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].kline.cfg.fantarget / 255;
  873. sprintf(buf, "Fan Percent %d", dev);
  874. root = api_add_int(root, buf, (int *)(&iFan), true);
  875. iFan = 0;
  876. if (klninfo->status[dev].kline.ws.fanspeed > 0)
  877. iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].kline.ws.fanspeed;
  878. sprintf(buf, "Fan RPM %d", dev);
  879. root = api_add_int(root, buf, (int *)(&iFan), true);
  880. if (klninfo->devinfo[dev].chipstats != NULL) {
  881. char data[2048];
  882. char one[32];
  883. int n;
  884. sprintf(buf, "Nonces / Chip %d", dev);
  885. data[0] = '\0';
  886. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  887. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n]);
  888. strcat(data, one);
  889. }
  890. root = api_add_string(root, buf, data, true);
  891. sprintf(buf, "Errors / Chip %d", dev);
  892. data[0] = '\0';
  893. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  894. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].kline.ws.chipcount]);
  895. strcat(data, one);
  896. }
  897. root = api_add_string(root, buf, data, true);
  898. }
  899. }
  900. root = api_add_uint64(root, "Hash Count", &(klninfo->hashcount), true);
  901. root = api_add_uint64(root, "Error Count", &(klninfo->errorcount), true);
  902. root = api_add_uint64(root, "Noise Count", &(klninfo->noisecount), true);
  903. root = api_add_int(root, "KLine Limit", &(klninfo->kline_count), true);
  904. root = api_add_int(root, "KLine Used", &(klninfo->used_count), true);
  905. root = api_add_elapsed(root, "KQue Delay Count", &(klninfo->delay_count), true);
  906. root = api_add_elapsed(root, "KQue Delay Total", &(klninfo->delay_total), true);
  907. root = api_add_elapsed(root, "KQue Delay Min", &(klninfo->delay_min), true);
  908. root = api_add_elapsed(root, "KQue Delay Max", &(klninfo->delay_max), true);
  909. double avg;
  910. if (klninfo->delay_count == 0)
  911. avg = 0;
  912. else
  913. avg = klninfo->delay_total / klninfo->delay_count;
  914. root = api_add_diff(root, "KQue Delay Avg", &avg, true);
  915. root = api_add_elapsed(root, "KQue Nonce Count", &(klninfo->nonce_count), true);
  916. root = api_add_elapsed(root, "KQue Nonce Total", &(klninfo->nonce_total), true);
  917. root = api_add_elapsed(root, "KQue Nonce Min", &(klninfo->nonce_min), true);
  918. root = api_add_elapsed(root, "KQue Nonce Max", &(klninfo->nonce_max), true);
  919. if (klninfo->nonce_count == 0)
  920. avg = 0;
  921. else
  922. avg = klninfo->nonce_total / klninfo->nonce_count;
  923. root = api_add_diff(root, "KQue Nonce Avg", &avg, true);
  924. rd_unlock(&(klninfo->stat_lock));
  925. return root;
  926. }
  927. struct device_drv klondike_drv = {
  928. .drv_id = DRIVER_klondike,
  929. .dname = "Klondike",
  930. .name = "KLN",
  931. .drv_detect = klondike_detect,
  932. .get_api_stats = klondike_api_stats,
  933. .get_statline_before = get_klondike_statline_before,
  934. .get_stats = klondike_get_stats,
  935. .identify_device = klondike_identify,
  936. .thread_prepare = klondike_thread_prepare,
  937. .thread_init = klondike_thread_init,
  938. .hash_work = hash_queued_work,
  939. .scanwork = klondike_scanwork,
  940. .queue_full = klondike_queue_full,
  941. .flush_work = klondike_flush_work,
  942. .thread_shutdown = klondike_shutdown,
  943. .thread_enable = klondike_thread_enable
  944. };