driver-klondike.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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])),
  387. (void *)kitem,
  388. sizeof(klninfo->status[dev]));
  389. wr_unlock(&(klninfo->stat_lock));
  390. release_kitem(klncgpu, kitem);
  391. kitem = NULL;
  392. }
  393. }
  394. // todo: detect slavecount change and realloc space
  395. return true;
  396. }
  397. static bool klondike_init(struct cgpu_info *klncgpu)
  398. {
  399. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  400. KLIST *kitem;
  401. KLINE kline;
  402. int slaves, dev;
  403. kline.hd.cmd = 'S';
  404. kline.hd.dev = 0;
  405. kitem = SendCmdGetReply(klncgpu, &kline, 0);
  406. if (kitem == NULL)
  407. return false;
  408. slaves = kitem->kline.ws.slavecount;
  409. release_kitem(klncgpu, kitem);
  410. kitem = NULL;
  411. if (klninfo->status == NULL) {
  412. applog(LOG_DEBUG, "Klondike initializing data");
  413. // alloc space for status, devinfo and cfg for master and slaves
  414. klninfo->status = calloc(slaves+1, sizeof(KLIST));
  415. if (unlikely(!klninfo->status))
  416. quit(1, "Failed to calloc status array in klondke_get_stats");
  417. klninfo->devinfo = calloc(slaves+1, sizeof(DEVINFO));
  418. if (unlikely(!klninfo->devinfo))
  419. quit(1, "Failed to calloc devinfo array in klondke_get_stats");
  420. klninfo->cfg = calloc(slaves+1, sizeof(KLIST));
  421. if (unlikely(!klninfo->cfg))
  422. quit(1, "Failed to calloc cfg array in klondke_get_stats");
  423. }
  424. // zero init triggers read back only
  425. memset(&(kline.cfg), 0, sizeof(kline.cfg));
  426. kline.cfg.cmd = 'C';
  427. int size = 2;
  428. // boundaries are checked by device, with valid values returned
  429. if (opt_klondike_options != NULL) {
  430. int hashclock;
  431. double temp1, temp2;
  432. sscanf(opt_klondike_options, "%d:%lf:%lf:%"SCNu8,
  433. &hashclock,
  434. &temp1, &temp2,
  435. &kline.cfg.fantarget);
  436. SET_HASHCLOCK(kline.cfg.hashclock, hashclock);
  437. kline.cfg.temptarget = cvtCToKln(temp1);
  438. kline.cfg.tempcritical = cvtCToKln(temp2);
  439. kline.cfg.fantarget = (int)255*kline.cfg.fantarget/100;
  440. size = sizeof(kline.cfg) - 2;
  441. }
  442. for (dev = 0; dev <= slaves; dev++) {
  443. kline.cfg.dev = dev;
  444. kitem = SendCmdGetReply(klncgpu, &kline, size);
  445. if (kitem != NULL) {
  446. memcpy((void *)&(klninfo->cfg[dev]), kitem, sizeof(klninfo->cfg[dev]));
  447. applog(LOG_WARNING, "Klondike config (%d: Clk: %d, T:%.0lf, C:%.0lf, F:%d)",
  448. dev, K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock),
  449. cvtKlnToC(klninfo->cfg[dev].kline.cfg.temptarget),
  450. cvtKlnToC(klninfo->cfg[dev].kline.cfg.tempcritical),
  451. (int)100*klninfo->cfg[dev].kline.cfg.fantarget/256);
  452. release_kitem(klncgpu, kitem);
  453. kitem = NULL;
  454. }
  455. }
  456. klondike_get_stats(klncgpu);
  457. for (dev = 0; dev <= slaves; dev++) {
  458. klninfo->devinfo[dev].rangesize = ((uint64_t)1<<32) / klninfo->status[dev].kline.ws.chipcount;
  459. klninfo->devinfo[dev].chipstats = calloc(klninfo->status[dev].kline.ws.chipcount*2 , sizeof(uint32_t));
  460. }
  461. int tries = 2;
  462. bool ok = false;
  463. kline.hd.cmd = 'E';
  464. kline.hd.dev = 0;
  465. kline.hd.buf[0] = '1';
  466. while (tries-- > 0) {
  467. kitem = SendCmdGetReply(klncgpu, &kline, 1);
  468. if (kitem) {
  469. release_kitem(klncgpu, kitem);
  470. kitem = NULL;
  471. ok = true;
  472. break;
  473. }
  474. }
  475. if (!ok)
  476. applog(LOG_ERR, "%s%i: failed to enable", klncgpu->drv->name, klncgpu->device_id);
  477. return ok;
  478. }
  479. static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  480. {
  481. struct cgpu_info *klncgpu = usb_alloc_cgpu(&klondike_drv, 1);
  482. struct klondike_info *klninfo = NULL;
  483. if (unlikely(!klncgpu))
  484. quit(1, "Failed to calloc klncgpu in klondike_detect_one");
  485. klninfo = calloc(1, sizeof(*klninfo));
  486. if (unlikely(!klninfo))
  487. quit(1, "Failed to calloc klninfo in klondke_detect_one");
  488. klncgpu->device_data = (void *)klninfo;
  489. klninfo->free = new_klist_set(klncgpu);
  490. if (usb_init(klncgpu, dev, found)) {
  491. int sent, recd, err;
  492. KLIST kitem;
  493. int attempts = 0;
  494. while (attempts++ < 3) {
  495. err = usb_write(klncgpu, "I", 2, &sent, C_REQUESTRESULTS);
  496. if (err < 0 || sent != 2) {
  497. applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)",
  498. klncgpu->drv->dname,
  499. klncgpu->device_path,
  500. sent, err);
  501. }
  502. cgsleep_ms(REPLY_WAIT_TIME*10);
  503. err = usb_read(klncgpu, (char *)&(kitem.kline), REPLY_SIZE, &recd, C_GETRESULTS);
  504. if (err < 0) {
  505. applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)",
  506. klncgpu->drv->dname,
  507. klncgpu->device_path,
  508. recd, err);
  509. } else if (recd < 1) {
  510. applog(LOG_ERR, "%s (%s) detect empty reply (%d)",
  511. klncgpu->drv->dname,
  512. klncgpu->device_path,
  513. recd);
  514. } else if (kitem.kline.hd.cmd == 'I' && kitem.kline.hd.dev == 0) {
  515. display_kline(klncgpu, &kitem.kline);
  516. applog(LOG_DEBUG, "%s (%s) detect successful",
  517. klncgpu->drv->dname,
  518. klncgpu->device_path);
  519. if (!add_cgpu(klncgpu))
  520. break;
  521. update_usb_stats(klncgpu);
  522. applog(LOG_DEBUG, "Klondike cgpu added");
  523. cglock_init(&klninfo->klist_lock);
  524. return true;
  525. }
  526. }
  527. usb_uninit(klncgpu);
  528. }
  529. free(klninfo->free);
  530. free(klninfo);
  531. free(klncgpu);
  532. return false;
  533. }
  534. static void klondike_detect(bool __maybe_unused hotplug)
  535. {
  536. usb_detect(&klondike_drv, klondike_detect_one);
  537. }
  538. static void klondike_identify(__maybe_unused struct cgpu_info *klncgpu)
  539. {
  540. /*
  541. KLINE kline;
  542. kline.hd.cmd = 'I';
  543. kline.hd.dev = 0;
  544. SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  545. */
  546. }
  547. static void klondike_check_nonce(struct cgpu_info *klncgpu, KLIST *kitem)
  548. {
  549. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  550. struct work *work, *tmp;
  551. KLINE *kline = &(kitem->kline);
  552. struct timeval tv_now;
  553. double us_diff;
  554. uint32_t nonce = K_NONCE(kline->wr.nonce) - 0xC0;
  555. applog(LOG_DEBUG, "Klondike FOUND NONCE (%02x:%08x)",
  556. kline->wr.workid, (unsigned int)nonce);
  557. HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
  558. if (work->queued && (work->subid == (kline->wr.dev*256 + kline->wr.workid))) {
  559. wr_lock(&(klninfo->stat_lock));
  560. klninfo->devinfo[kline->wr.dev].noncecount++;
  561. klninfo->noncecount++;
  562. wr_unlock(&(klninfo->stat_lock));
  563. // kline->wr.nonce = le32toh(kline->wr.nonce - 0xC0);
  564. applog(LOG_DEBUG, "Klondike SUBMIT NONCE (%02x:%08x)",
  565. kline->wr.workid, (unsigned int)nonce);
  566. cgtime(&tv_now);
  567. bool ok = submit_nonce(klncgpu->thr[0], work, nonce);
  568. applog(LOG_DEBUG, "Klondike chip stats %d, %08x, %d, %d",
  569. kline->wr.dev, (unsigned int)nonce,
  570. klninfo->devinfo[kline->wr.dev].rangesize,
  571. klninfo->status[kline->wr.dev].kline.ws.chipcount);
  572. klninfo->devinfo[kline->wr.dev].chipstats[(nonce / klninfo->devinfo[kline->wr.dev].rangesize) + (ok ? 0 : klninfo->status[kline->wr.dev].kline.ws.chipcount)]++;
  573. us_diff = us_tdiff(&tv_now, &(kitem->tv_when));
  574. if (klninfo->delay_count == 0) {
  575. klninfo->delay_min = us_diff;
  576. klninfo->delay_max = us_diff;
  577. } else {
  578. if (klninfo->delay_min > us_diff)
  579. klninfo->delay_min = us_diff;
  580. if (klninfo->delay_max < us_diff)
  581. klninfo->delay_max = us_diff;
  582. }
  583. klninfo->delay_count++;
  584. klninfo->delay_total += us_diff;
  585. if (klninfo->nonce_count > 0) {
  586. us_diff = us_tdiff(&(kitem->tv_when), &(klninfo->tv_last_nonce_received));
  587. if (klninfo->nonce_count == 1) {
  588. klninfo->nonce_min = us_diff;
  589. klninfo->nonce_max = us_diff;
  590. } else {
  591. if (klninfo->nonce_min > us_diff)
  592. klninfo->nonce_min = us_diff;
  593. if (klninfo->nonce_max < us_diff)
  594. klninfo->nonce_max = us_diff;
  595. }
  596. klninfo->nonce_total += us_diff;
  597. }
  598. klninfo->nonce_count++;
  599. memcpy(&(klninfo->tv_last_nonce_received), &(kitem->tv_when),
  600. sizeof(klninfo->tv_last_nonce_received));
  601. return;
  602. }
  603. }
  604. applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored",
  605. klncgpu->drv->name, klncgpu->device_id,
  606. kline->wr.dev, kline->wr.workid, (unsigned int)nonce);
  607. //inc_hw_errors(klncgpu->thr[0]);
  608. }
  609. // thread to keep looking for replies
  610. static void *klondike_get_replies(void *userdata)
  611. {
  612. struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
  613. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  614. KLIST *kitem = NULL;
  615. char *hexdata;
  616. int err, recd;
  617. applog(LOG_ERR, "Klondike listening for replies");
  618. while (klninfo->shutdown == false) {
  619. if (klncgpu->usbinfo.nodev)
  620. return NULL;
  621. if (kitem == NULL)
  622. kitem = allocate_kitem(klncgpu);
  623. else
  624. memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
  625. err = usb_read(klncgpu, (char *)&(kitem->kline), REPLY_SIZE, &recd, C_GETRESULTS);
  626. if (!err && recd == REPLY_SIZE) {
  627. cgtime(&(kitem->tv_when));
  628. kitem->block_seq = klninfo->block_seq;
  629. if (opt_log_level <= READ_DEBUG) {
  630. hexdata = bin2hex((unsigned char *)&(kitem->kline.hd.dev), recd-1);
  631. applog(READ_DEBUG, "%s (%s) reply [%c:%s]",
  632. klncgpu->drv->dname, klncgpu->device_path,
  633. kitem->kline.hd.cmd, hexdata);
  634. free(hexdata);
  635. }
  636. switch (kitem->kline.hd.cmd) {
  637. case '=':
  638. klondike_check_nonce(klncgpu, kitem);
  639. display_kline(klncgpu, &kitem->kline);
  640. break;
  641. case 'S':
  642. case 'W':
  643. case 'A':
  644. case 'E':
  645. wr_lock(&(klninfo->stat_lock));
  646. klninfo->errorcount += kitem->kline.ws.errorcount;
  647. klninfo->noisecount += kitem->kline.ws.noise;
  648. wr_unlock(&(klninfo->stat_lock));
  649. display_kline(klncgpu, &kitem->kline);
  650. kitem->ready = true;
  651. kitem = NULL;
  652. break;
  653. case 'C':
  654. display_kline(klncgpu, &kitem->kline);
  655. kitem->ready = true;
  656. kitem = NULL;
  657. break;
  658. case 'I':
  659. display_kline(klncgpu, &kitem->kline);
  660. kitem->ready = true;
  661. kitem = NULL;
  662. break;
  663. default:
  664. display_kline(klncgpu, &kitem->kline);
  665. break;
  666. }
  667. }
  668. }
  669. return NULL;
  670. }
  671. static void klondike_flush_work(struct cgpu_info *klncgpu)
  672. {
  673. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  674. KLIST *kitem;
  675. KLINE kline;
  676. int slaves, dev;
  677. klninfo->block_seq++;
  678. applog(LOG_DEBUG, "Klondike flushing work");
  679. slaves = klninfo->status[0].kline.ws.slavecount;
  680. kline.hd.cmd = 'A';
  681. for (dev = 0; dev <= slaves; dev++) {
  682. kline.hd.dev = dev;
  683. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  684. if (kitem != NULL) {
  685. wr_lock(&(klninfo->stat_lock));
  686. memcpy((void *)&(klninfo->status[dev]),
  687. kitem,
  688. sizeof(klninfo->status[dev]));
  689. wr_unlock(&(klninfo->stat_lock));
  690. release_kitem(klncgpu, kitem);
  691. kitem = NULL;
  692. }
  693. }
  694. }
  695. static bool klondike_thread_prepare(struct thr_info *thr)
  696. {
  697. struct cgpu_info *klncgpu = thr->cgpu;
  698. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  699. if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) {
  700. applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id);
  701. return false;
  702. }
  703. pthread_detach(klninfo->replies_thr.pth);
  704. // let the listening get started
  705. cgsleep_ms(100);
  706. return klondike_init(klncgpu);
  707. }
  708. static bool klondike_thread_init(struct thr_info *thr)
  709. {
  710. struct cgpu_info *klncgpu = thr->cgpu;
  711. if (klncgpu->usbinfo.nodev)
  712. return false;
  713. klondike_flush_work(klncgpu);
  714. return true;
  715. }
  716. static void klondike_shutdown(struct thr_info *thr)
  717. {
  718. struct cgpu_info *klncgpu = thr->cgpu;
  719. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  720. KLIST *kitem;
  721. KLINE kline;
  722. int dev;
  723. applog(LOG_DEBUG, "Klondike shutting down work");
  724. kline.hd.cmd = 'E';
  725. for (dev = 0; dev <= klninfo->status[0].kline.ws.slavecount; dev++) {
  726. kline.hd.dev = dev;
  727. kline.hd.buf[0] = '0';
  728. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
  729. if (kitem)
  730. release_kitem(klncgpu, kitem);
  731. }
  732. klncgpu->shutdown = klninfo->shutdown = true;
  733. }
  734. static void klondike_thread_enable(struct thr_info *thr)
  735. {
  736. struct cgpu_info *klncgpu = thr->cgpu;
  737. if (klncgpu->usbinfo.nodev)
  738. return;
  739. /*
  740. KLINE kline;
  741. kline.hd.cmd = 'E';
  742. kline.hd.dev = dev;
  743. kline.hd.buf[0] = '0';
  744. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
  745. */
  746. }
  747. static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
  748. {
  749. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  750. struct work *tmp;
  751. KLINE kline;
  752. if (klncgpu->usbinfo.nodev)
  753. return false;
  754. kline.wt.cmd = 'W';
  755. kline.wt.dev = dev;
  756. memcpy(kline.wt.midstate, work->midstate, MIDSTATE_BYTES);
  757. memcpy(kline.wt.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  758. kline.wt.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
  759. work->subid = dev*256 + kline.wt.workid;
  760. if (opt_log_level <= LOG_DEBUG) {
  761. char *hexdata = bin2hex((void *)&kline.wt, sizeof(kline.wt));
  762. applog(LOG_DEBUG, "WORKDATA: %s", hexdata);
  763. free(hexdata);
  764. }
  765. applog(LOG_DEBUG, "Klondike sending work (%d:%02x)", dev, kline.wt.workid);
  766. KLIST *kitem = SendCmdGetReply(klncgpu, &kline, sizeof(kline.wt));
  767. if (kitem != NULL) {
  768. wr_lock(&(klninfo->stat_lock));
  769. memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(klninfo->status[dev]));
  770. wr_unlock(&(klninfo->stat_lock));
  771. release_kitem(klncgpu, kitem);
  772. kitem = NULL;
  773. // remove old work
  774. HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
  775. if (work->queued && (work->subid == (int)(dev*256 + ((klninfo->devinfo[dev].nextworkid-2*MAX_WORK_COUNT) & 0xFF))))
  776. work_completed(klncgpu, work);
  777. }
  778. return true;
  779. }
  780. return false;
  781. }
  782. static bool klondike_queue_full(struct cgpu_info *klncgpu)
  783. {
  784. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  785. struct work *work = NULL;
  786. int dev, queued, slaves;
  787. slaves = klninfo->status[0].kline.ws.slavecount;
  788. for (queued = 0; queued < MAX_WORK_COUNT-1; queued++)
  789. for (dev = 0; dev <= slaves; dev++)
  790. if (klninfo->status[dev].kline.ws.workqc <= queued) {
  791. if (!work)
  792. work = get_queued(klncgpu);
  793. if (unlikely(!work))
  794. return false;
  795. if (klondike_send_work(klncgpu, dev, work)) {
  796. work = NULL;
  797. break;
  798. }
  799. }
  800. return true;
  801. }
  802. static int64_t klondike_scanwork(struct thr_info *thr)
  803. {
  804. struct cgpu_info *klncgpu = thr->cgpu;
  805. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  806. int64_t newhashcount = 0;
  807. int dev, slaves;
  808. if (klncgpu->usbinfo.nodev)
  809. return -1;
  810. restart_wait(thr, 200);
  811. if (klninfo->status != NULL) {
  812. rd_lock(&(klninfo->stat_lock));
  813. slaves = klninfo->status[0].kline.ws.slavecount;
  814. for (dev = 0; dev <= slaves; dev++) {
  815. uint64_t newhashdev = 0, hashcount;
  816. int maxcount;
  817. hashcount = K_HASHCOUNT(klninfo->status[dev].kline.ws.hashcount);
  818. maxcount = K_MAXCOUNT(klninfo->status[dev].kline.ws.maxcount);
  819. if (klninfo->devinfo[dev].lasthashcount > hashcount) // todo: chg this to check workid for wrapped instead
  820. newhashdev += maxcount; // hash counter wrapped
  821. newhashdev += hashcount - klninfo->devinfo[dev].lasthashcount;
  822. klninfo->devinfo[dev].lasthashcount = hashcount;
  823. if (maxcount != 0)
  824. klninfo->hashcount += (newhashdev << 32) / maxcount;
  825. // todo: check stats for critical conditions
  826. }
  827. newhashcount += 0xffffffffull * (uint64_t)klninfo->noncecount;
  828. klninfo->noncecount = 0;
  829. rd_unlock(&(klninfo->stat_lock));
  830. }
  831. return newhashcount;
  832. }
  833. static void get_klondike_statline_before(char *buf, size_t siz, struct cgpu_info *klncgpu)
  834. {
  835. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  836. uint8_t temp = 0xFF;
  837. uint16_t fan = 0;
  838. uint16_t clock = 0;
  839. int dev, slaves;
  840. char tmp[16];
  841. if (klninfo->status == NULL) {
  842. blank_get_statline_before(buf, siz, klncgpu);
  843. return;
  844. }
  845. rd_lock(&(klninfo->stat_lock));
  846. slaves = klninfo->status[0].kline.ws.slavecount;
  847. for (dev = 0; dev <= slaves; dev++) {
  848. if (klninfo->status[dev].kline.ws.temp < temp)
  849. temp = klninfo->status[dev].kline.ws.temp;
  850. fan += klninfo->cfg[dev].kline.cfg.fantarget;
  851. clock += (uint16_t)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  852. }
  853. fan /= slaves + 1;
  854. clock /= slaves + 1;
  855. rd_unlock(&(klninfo->stat_lock));
  856. snprintf(tmp, sizeof(tmp), "%2.0fC", cvtKlnToC(temp));
  857. if (strlen(tmp) < 4)
  858. strcat(tmp, " ");
  859. tailsprintf(buf, siz, "%3dMHz %3d%% %s| ", (int)clock, fan*100/255, tmp);
  860. }
  861. static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
  862. {
  863. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  864. struct api_data *root = NULL;
  865. char buf[32];
  866. int dev, slaves;
  867. if (klninfo->status == NULL)
  868. return NULL;
  869. rd_lock(&(klninfo->stat_lock));
  870. slaves = klninfo->status[0].kline.ws.slavecount;
  871. for (dev = 0; dev <= slaves; dev++) {
  872. float fTemp = cvtKlnToC(klninfo->status[dev].kline.ws.temp);
  873. sprintf(buf, "Temp %d", dev);
  874. root = api_add_temp(root, buf, &fTemp, true);
  875. double dClk = (double)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  876. sprintf(buf, "Clock %d", dev);
  877. root = api_add_freq(root, buf, &dClk, true);
  878. unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].kline.cfg.fantarget / 255;
  879. sprintf(buf, "Fan Percent %d", dev);
  880. root = api_add_int(root, buf, (int *)(&iFan), true);
  881. iFan = 0;
  882. if (klninfo->status[dev].kline.ws.fanspeed > 0)
  883. iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].kline.ws.fanspeed;
  884. sprintf(buf, "Fan RPM %d", dev);
  885. root = api_add_int(root, buf, (int *)(&iFan), true);
  886. if (klninfo->devinfo[dev].chipstats != NULL) {
  887. char data[2048];
  888. char one[32];
  889. int n;
  890. sprintf(buf, "Nonces / Chip %d", dev);
  891. data[0] = '\0';
  892. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  893. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n]);
  894. strcat(data, one);
  895. }
  896. root = api_add_string(root, buf, data, true);
  897. sprintf(buf, "Errors / Chip %d", dev);
  898. data[0] = '\0';
  899. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  900. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].kline.ws.chipcount]);
  901. strcat(data, one);
  902. }
  903. root = api_add_string(root, buf, data, true);
  904. }
  905. }
  906. root = api_add_uint64(root, "Hash Count", &(klninfo->hashcount), true);
  907. root = api_add_uint64(root, "Error Count", &(klninfo->errorcount), true);
  908. root = api_add_uint64(root, "Noise Count", &(klninfo->noisecount), true);
  909. root = api_add_int(root, "KLine Limit", &(klninfo->kline_count), true);
  910. root = api_add_int(root, "KLine Used", &(klninfo->used_count), true);
  911. root = api_add_elapsed(root, "KQue Delay Count", &(klninfo->delay_count), true);
  912. root = api_add_elapsed(root, "KQue Delay Total", &(klninfo->delay_total), true);
  913. root = api_add_elapsed(root, "KQue Delay Min", &(klninfo->delay_min), true);
  914. root = api_add_elapsed(root, "KQue Delay Max", &(klninfo->delay_max), true);
  915. double avg;
  916. if (klninfo->delay_count == 0)
  917. avg = 0;
  918. else
  919. avg = klninfo->delay_total / klninfo->delay_count;
  920. root = api_add_diff(root, "KQue Delay Avg", &avg, true);
  921. root = api_add_elapsed(root, "KQue Nonce Count", &(klninfo->nonce_count), true);
  922. root = api_add_elapsed(root, "KQue Nonce Total", &(klninfo->nonce_total), true);
  923. root = api_add_elapsed(root, "KQue Nonce Min", &(klninfo->nonce_min), true);
  924. root = api_add_elapsed(root, "KQue Nonce Max", &(klninfo->nonce_max), true);
  925. if (klninfo->nonce_count == 0)
  926. avg = 0;
  927. else
  928. avg = klninfo->nonce_total / klninfo->nonce_count;
  929. root = api_add_diff(root, "KQue Nonce Avg", &avg, true);
  930. rd_unlock(&(klninfo->stat_lock));
  931. return root;
  932. }
  933. struct device_drv klondike_drv = {
  934. .drv_id = DRIVER_klondike,
  935. .dname = "Klondike",
  936. .name = "KLN",
  937. .drv_detect = klondike_detect,
  938. .get_api_stats = klondike_api_stats,
  939. .get_statline_before = get_klondike_statline_before,
  940. .get_stats = klondike_get_stats,
  941. .identify_device = klondike_identify,
  942. .thread_prepare = klondike_thread_prepare,
  943. .thread_init = klondike_thread_init,
  944. .hash_work = hash_queued_work,
  945. .scanwork = klondike_scanwork,
  946. .queue_full = klondike_queue_full,
  947. .flush_work = klondike_flush_work,
  948. .thread_shutdown = klondike_shutdown,
  949. .thread_enable = klondike_thread_enable
  950. };