driver-klondike.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 REPLY_BUFSIZE 16 // reply + 1 byte to mark used
  35. #define MAX_REPLY_COUNT 32 // more unhandled replies than this will result in data loss
  36. #define REPLY_WAIT_TIME 100 // poll interval for a cmd waiting it's reply
  37. #define CMD_REPLY_RETRIES 8 // how many retries for cmds
  38. #define MAX_WORK_COUNT 4 // for now, must be binary multiple and match firmware
  39. #define TACH_FACTOR 87890 // fan rpm divisor
  40. struct device_drv klondike_drv;
  41. typedef struct klondike_id {
  42. uint8_t version;
  43. uint8_t product[7];
  44. uint32_t serial;
  45. } IDENTITY;
  46. typedef struct klondike_status {
  47. uint8_t state;
  48. uint8_t chipcount;
  49. uint8_t slavecount;
  50. uint8_t workqc;
  51. uint8_t workid;
  52. uint8_t temp;
  53. uint8_t fanspeed;
  54. uint8_t errorcount;
  55. uint16_t hashcount;
  56. uint16_t maxcount;
  57. uint8_t noise;
  58. } WORKSTATUS;
  59. typedef struct _worktask {
  60. uint16_t pad1;
  61. uint8_t pad2;
  62. uint8_t workid;
  63. uint32_t midstate[8];
  64. uint32_t merkle[3];
  65. } WORKTASK;
  66. typedef struct _workresult {
  67. uint16_t pad;
  68. uint8_t device;
  69. uint8_t workid;
  70. uint32_t nonce;
  71. } WORKRESULT;
  72. typedef struct kondike_cfg {
  73. uint16_t hashclock;
  74. uint8_t temptarget;
  75. uint8_t tempcritical;
  76. uint8_t fantarget;
  77. uint8_t pad;
  78. } WORKCFG;
  79. typedef struct device_info {
  80. uint32_t noncecount;
  81. uint32_t nextworkid;
  82. uint16_t lasthashcount;
  83. uint64_t totalhashcount;
  84. uint32_t rangesize;
  85. uint32_t *chipstats;
  86. } DEVINFO;
  87. struct klondike_info {
  88. bool shutdown;
  89. pthread_rwlock_t stat_lock;
  90. struct thr_info replies_thr;
  91. WORKSTATUS *status;
  92. DEVINFO *devinfo;
  93. WORKCFG *cfg;
  94. char *replies;
  95. int nextreply;
  96. int noncecount;
  97. uint64_t hashcount;
  98. uint64_t errorcount;
  99. uint64_t noisecount;
  100. };
  101. IDENTITY KlondikeID;
  102. static double cvtKlnToC(uint8_t temp)
  103. {
  104. return (double)1/((double)1/(25+273.15) + log((double)temp*1000/(256-temp)/2200)/3987) - 273.15;
  105. }
  106. static int cvtCToKln(double deg)
  107. {
  108. double R = exp((1/(deg+273.15)-1/(273.15+25))*3987)*2200;
  109. return 256*R/(R+1000);
  110. }
  111. static char *SendCmdGetReply(struct cgpu_info *klncgpu, char Cmd, int device, int datalen, void *data)
  112. {
  113. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  114. char outbuf[64];
  115. int retries = CMD_REPLY_RETRIES;
  116. int chkreply = klninfo->nextreply;
  117. int sent, err;
  118. if (klncgpu->usbinfo.nodev)
  119. return NULL;
  120. outbuf[0] = Cmd;
  121. outbuf[1] = device;
  122. memcpy(outbuf+2, data, datalen);
  123. err = usb_write(klncgpu, outbuf, 2+datalen, &sent, C_REQUESTRESULTS);
  124. if (err < 0 || sent != 2+datalen) {
  125. applog(LOG_ERR, "%s (%s) Cmd:%c Dev:%d, write failed (%d:%d)", klncgpu->drv->dname, klncgpu->device_path, Cmd, device, sent, err);
  126. }
  127. while (retries-- > 0 && klninfo->shutdown == false) {
  128. cgsleep_ms(REPLY_WAIT_TIME);
  129. while (*(klninfo->replies + chkreply*REPLY_BUFSIZE) != Cmd || *(klninfo->replies + chkreply*REPLY_BUFSIZE + 2) != device) {
  130. if (++chkreply == MAX_REPLY_COUNT)
  131. chkreply = 0;
  132. if (chkreply == klninfo->nextreply)
  133. break;
  134. }
  135. if (chkreply == klninfo->nextreply)
  136. continue;
  137. *(klninfo->replies + chkreply*REPLY_BUFSIZE) = '!'; // mark to prevent re-use
  138. return klninfo->replies + chkreply*REPLY_BUFSIZE + 1;
  139. }
  140. return NULL;
  141. }
  142. static bool klondike_get_stats(struct cgpu_info *klncgpu)
  143. {
  144. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  145. int slaves, dev;
  146. if (klncgpu->usbinfo.nodev || klninfo->status == NULL)
  147. return false;
  148. applog(LOG_DEBUG, "Klondike getting status");
  149. slaves = klninfo->status[0].slavecount;
  150. // loop thru devices and get status for each
  151. wr_lock(&(klninfo->stat_lock));
  152. for (dev = 0; dev <= slaves; dev++) {
  153. char *reply = SendCmdGetReply(klncgpu, 'S', dev, 0, NULL);
  154. if (reply != NULL)
  155. memcpy((void *)(&(klninfo->status[dev])), reply+2, sizeof(klninfo->status[dev]));
  156. }
  157. wr_unlock(&(klninfo->stat_lock));
  158. // todo: detect slavecount change and realloc space
  159. return true;
  160. }
  161. static bool klondike_init(struct cgpu_info *klncgpu)
  162. {
  163. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  164. int slaves, dev;
  165. char *reply = SendCmdGetReply(klncgpu, 'S', 0, 0, NULL);
  166. if (reply == NULL)
  167. return false;
  168. slaves = ((WORKSTATUS *)(reply+2))->slavecount;
  169. if (klninfo->status == NULL) {
  170. applog(LOG_DEBUG, "Klondike initializing data");
  171. // alloc space for status, devinfo and cfg for master and slaves
  172. klninfo->status = calloc(slaves+1, sizeof(WORKSTATUS));
  173. if (unlikely(!klninfo->status))
  174. quit(1, "Failed to calloc status array in klondke_get_stats");
  175. klninfo->devinfo = calloc(slaves+1, sizeof(DEVINFO));
  176. if (unlikely(!klninfo->devinfo))
  177. quit(1, "Failed to calloc devinfo array in klondke_get_stats");
  178. klninfo->cfg = calloc(slaves+1, sizeof(WORKCFG));
  179. if (unlikely(!klninfo->cfg))
  180. quit(1, "Failed to calloc cfg array in klondke_get_stats");
  181. }
  182. WORKCFG cfgset = { 0,0,0,0,0 }; // zero init triggers read back only
  183. double temp1, temp2;
  184. int size = 2;
  185. if (opt_klondike_options != NULL) { // boundaries are checked by device, with valid values returned
  186. sscanf(opt_klondike_options, "%hu:%lf:%lf:%hhu", &cfgset.hashclock, &temp1, &temp2, &cfgset.fantarget);
  187. cfgset.temptarget = cvtCToKln(temp1);
  188. cfgset.tempcritical = cvtCToKln(temp2);
  189. cfgset.fantarget = (int)255*cfgset.fantarget/100;
  190. size = sizeof(cfgset);
  191. }
  192. for (dev = 0; dev <= slaves; dev++) {
  193. char *reply = SendCmdGetReply(klncgpu, 'C', dev, size, &cfgset);
  194. if (reply != NULL) {
  195. klninfo->cfg[dev] = *(WORKCFG *)(reply+2);
  196. applog(LOG_NOTICE, "Klondike config (%d: Clk: %d, T:%.0lf, C:%.0lf, F:%d)",
  197. dev, klninfo->cfg[dev].hashclock,
  198. cvtKlnToC(klninfo->cfg[dev].temptarget),
  199. cvtKlnToC(klninfo->cfg[dev].tempcritical),
  200. (int)100*klninfo->cfg[dev].fantarget/256);
  201. }
  202. }
  203. klondike_get_stats(klncgpu);
  204. for (dev = 0; dev <= slaves; dev++) {
  205. klninfo->devinfo[dev].rangesize = ((uint64_t)1<<32) / klninfo->status[dev].chipcount;
  206. klninfo->devinfo[dev].chipstats = calloc(klninfo->status[dev].chipcount*2 , sizeof(uint32_t));
  207. }
  208. SendCmdGetReply(klncgpu, 'E', 0, 1, "1");
  209. return true;
  210. }
  211. static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  212. {
  213. struct cgpu_info *klncgpu = usb_alloc_cgpu(&klondike_drv, 1);
  214. struct klondike_info *klninfo = NULL;
  215. if (unlikely(!klncgpu))
  216. quit(1, "Failed to calloc klncgpu in klondike_detect_one");
  217. klninfo = calloc(1, sizeof(*klninfo));
  218. if (unlikely(!klninfo))
  219. quit(1, "Failed to calloc klninfo in klondke_detect_one");
  220. klncgpu->device_data = (FILE *)klninfo;
  221. klninfo->replies = calloc(MAX_REPLY_COUNT, REPLY_BUFSIZE);
  222. if (unlikely(!klninfo->replies))
  223. quit(1, "Failed to calloc replies buffer in klondke_detect_one");
  224. klninfo->nextreply = 0;
  225. if (usb_init(klncgpu, dev, found)) {
  226. int attempts = 0;
  227. while (attempts++ < 3) {
  228. char devpath[20], reply[REPLY_SIZE];
  229. int sent, recd, err;
  230. sprintf(devpath, "%d:%d", (int)(klncgpu->usbinfo.bus_number), (int)(klncgpu->usbinfo.device_address));
  231. err = usb_write(klncgpu, "I", 2, &sent, C_REQUESTRESULTS);
  232. if (err < 0 || sent != 2) {
  233. applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)", klncgpu->drv->dname, devpath, sent, err);
  234. }
  235. cgsleep_ms(REPLY_WAIT_TIME*10);
  236. err = usb_read(klncgpu, reply, REPLY_SIZE, &recd, C_GETRESULTS);
  237. if (err < 0) {
  238. applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)", klncgpu->drv->dname, devpath, recd, err);
  239. } else if (recd < 1) {
  240. applog(LOG_ERR, "%s (%s) detect empty reply (%d)", klncgpu->drv->dname, devpath, recd);
  241. } else if (reply[0] == 'I' && reply[1] == 0) {
  242. applog(LOG_DEBUG, "%s (%s) detect successful", klncgpu->drv->dname, devpath);
  243. KlondikeID = *(IDENTITY *)(&reply[2]);
  244. klncgpu->device_path = strdup(devpath);
  245. update_usb_stats(klncgpu);
  246. if (!add_cgpu(klncgpu))
  247. break;
  248. applog(LOG_DEBUG, "Klondike cgpu added");
  249. return true;
  250. }
  251. }
  252. usb_uninit(klncgpu);
  253. }
  254. free(klninfo->replies);
  255. free(klncgpu);
  256. return false;
  257. }
  258. static void klondike_detect(bool __maybe_unused hotplug)
  259. {
  260. usb_detect(&klondike_drv, klondike_detect_one);
  261. }
  262. static void klondike_identify(__maybe_unused struct cgpu_info *klncgpu)
  263. {
  264. //SendCmdGetReply(klncgpu, 'I', 0, 0, NULL);
  265. }
  266. static void klondike_check_nonce(struct cgpu_info *klncgpu, WORKRESULT *result)
  267. {
  268. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  269. struct work *work, *tmp;
  270. applog(LOG_DEBUG, "Klondike FOUND NONCE (%02x:%08x)", result->workid, result->nonce);
  271. HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
  272. if (work->queued && (work->subid == (result->device*256 + result->workid))) {
  273. wr_lock(&(klninfo->stat_lock));
  274. klninfo->devinfo[result->device].noncecount++;
  275. klninfo->noncecount++;
  276. wr_unlock(&(klninfo->stat_lock));
  277. result->nonce = le32toh(result->nonce - 0xC0);
  278. applog(LOG_DEBUG, "Klondike SUBMIT NONCE (%02x:%08x)", result->workid, result->nonce);
  279. bool ok = submit_nonce(klncgpu->thr[0], work, result->nonce);
  280. applog(LOG_DEBUG, "Klondike chip stats %d, %08x, %d, %d", result->device, result->nonce, klninfo->devinfo[result->device].rangesize, klninfo->status[result->device].chipcount);
  281. klninfo->devinfo[result->device].chipstats[(result->nonce / klninfo->devinfo[result->device].rangesize) + (ok ? 0 : klninfo->status[result->device].chipcount)]++;
  282. return;
  283. }
  284. }
  285. applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored",
  286. klncgpu->drv->name, klncgpu->device_id, result->device, result->workid, result->nonce);
  287. //inc_hw_errors(klncgpu->thr[0]);
  288. }
  289. // thread to keep looking for replies
  290. static void *klondike_get_replies(void *userdata)
  291. {
  292. struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
  293. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  294. struct klondike_status *ks;
  295. char *replybuf;
  296. int err, recd;
  297. applog(LOG_DEBUG, "Klondike listening for replies");
  298. while (klninfo->shutdown == false) {
  299. if (klncgpu->usbinfo.nodev)
  300. return NULL;
  301. replybuf = klninfo->replies + klninfo->nextreply * REPLY_BUFSIZE;
  302. replybuf[0] = 0;
  303. err = usb_read(klncgpu, replybuf+1, REPLY_SIZE, &recd, C_GETRESULTS);
  304. if (!err && recd == REPLY_SIZE) {
  305. if (opt_log_level <= LOG_DEBUG) {
  306. char *hexdata = bin2hex((unsigned char *)(replybuf+1), recd);
  307. applog(LOG_DEBUG, "%s (%s) reply [%s:%s]", klncgpu->drv->dname, klncgpu->device_path, replybuf+1, hexdata);
  308. free(hexdata);
  309. }
  310. if (++klninfo->nextreply == MAX_REPLY_COUNT)
  311. klninfo->nextreply = 0;
  312. replybuf[0] = replybuf[1];
  313. switch (replybuf[0]) {
  314. case '=':
  315. klondike_check_nonce(klncgpu, (WORKRESULT *)replybuf);
  316. break;
  317. case 'S':
  318. case 'W':
  319. case 'A':
  320. case 'E':
  321. ks = (struct klondike_status *)(replybuf+1);
  322. wr_lock(&(klninfo->stat_lock));
  323. klninfo->errorcount += ks->errorcount;
  324. klninfo->noisecount += ks->noise;
  325. wr_unlock(&(klninfo->stat_lock));
  326. break;
  327. default:
  328. break;
  329. }
  330. }
  331. }
  332. return NULL;
  333. }
  334. static void klondike_flush_work(struct cgpu_info *klncgpu)
  335. {
  336. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  337. int dev;
  338. applog(LOG_DEBUG, "Klondike flushing work");
  339. for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
  340. char *reply = SendCmdGetReply(klncgpu, 'A', dev, 0, NULL);
  341. if (reply != NULL) {
  342. wr_lock(&(klninfo->stat_lock));
  343. klninfo->status[dev] = *(WORKSTATUS *)(reply+2);
  344. wr_unlock(&(klninfo->stat_lock));
  345. }
  346. }
  347. }
  348. static bool klondike_thread_prepare(struct thr_info *thr)
  349. {
  350. struct cgpu_info *klncgpu = thr->cgpu;
  351. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  352. if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) {
  353. applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id);
  354. return false;
  355. }
  356. pthread_detach(klninfo->replies_thr.pth);
  357. // let the listening get started
  358. cgsleep_ms(100);
  359. return klondike_init(klncgpu);
  360. }
  361. static bool klondike_thread_init(struct thr_info *thr)
  362. {
  363. struct cgpu_info *klncgpu = thr->cgpu;
  364. if (klncgpu->usbinfo.nodev)
  365. return false;
  366. klondike_flush_work(klncgpu);
  367. return true;
  368. }
  369. static void klondike_shutdown(struct thr_info *thr)
  370. {
  371. struct cgpu_info *klncgpu = thr->cgpu;
  372. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  373. int dev;
  374. applog(LOG_DEBUG, "Klondike shutting down work");
  375. for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
  376. SendCmdGetReply(klncgpu, 'E', dev, 1, "0");
  377. }
  378. klncgpu->shutdown = klninfo->shutdown = true;
  379. }
  380. static void klondike_thread_enable(struct thr_info *thr)
  381. {
  382. struct cgpu_info *klncgpu = thr->cgpu;
  383. if (klncgpu->usbinfo.nodev)
  384. return;
  385. //SendCmdGetReply(klncgpu, 'E', 0, 1, "0");
  386. }
  387. static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
  388. {
  389. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  390. struct work *tmp;
  391. WORKTASK data;
  392. if (klncgpu->usbinfo.nodev)
  393. return false;
  394. memcpy(data.midstate, work->midstate, MIDSTATE_BYTES);
  395. memcpy(data.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  396. data.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
  397. work->subid = dev*256 + data.workid;
  398. if (opt_log_level <= LOG_DEBUG) {
  399. char *hexdata = bin2hex(&data.workid, sizeof(data)-3);
  400. applog(LOG_DEBUG, "WORKDATA: %s", hexdata);
  401. free(hexdata);
  402. }
  403. applog(LOG_DEBUG, "Klondike sending work (%d:%02x)", dev, data.workid);
  404. char *reply = SendCmdGetReply(klncgpu, 'W', dev, sizeof(data)-3, &data.workid);
  405. if (reply != NULL) {
  406. wr_lock(&(klninfo->stat_lock));
  407. klninfo->status[dev] = *(WORKSTATUS *)(reply+2);
  408. wr_unlock(&(klninfo->stat_lock));
  409. // remove old work
  410. HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
  411. if (work->queued && (work->subid == (int)(dev*256 + ((klninfo->devinfo[dev].nextworkid-2*MAX_WORK_COUNT) & 0xFF))))
  412. work_completed(klncgpu, work);
  413. }
  414. return true;
  415. }
  416. return false;
  417. }
  418. static bool klondike_queue_full(struct cgpu_info *klncgpu)
  419. {
  420. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  421. struct work *work = NULL;
  422. int dev, queued;
  423. for (queued = 0; queued < MAX_WORK_COUNT-1; queued++)
  424. for (dev = 0; dev <= klninfo->status->slavecount; dev++)
  425. if (klninfo->status[dev].workqc <= queued) {
  426. if (!work)
  427. work = get_queued(klncgpu);
  428. if (unlikely(!work))
  429. return false;
  430. if (klondike_send_work(klncgpu, dev, work)) {
  431. work = NULL;
  432. break;
  433. }
  434. }
  435. return true;
  436. }
  437. static int64_t klondike_scanwork(struct thr_info *thr)
  438. {
  439. struct cgpu_info *klncgpu = thr->cgpu;
  440. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  441. int64_t newhashcount = 0;
  442. int dev;
  443. if (klncgpu->usbinfo.nodev)
  444. return -1;
  445. restart_wait(thr, 200);
  446. if (klninfo->status != NULL) {
  447. rd_lock(&(klninfo->stat_lock));
  448. for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
  449. uint64_t newhashdev = 0;
  450. if (klninfo->devinfo[dev].lasthashcount > klninfo->status[dev].hashcount) // todo: chg this to check workid for wrapped instead
  451. newhashdev += klninfo->status[dev].maxcount; // hash counter wrapped
  452. newhashdev += klninfo->status[dev].hashcount - klninfo->devinfo[dev].lasthashcount;
  453. klninfo->devinfo[dev].lasthashcount = klninfo->status[dev].hashcount;
  454. klninfo->hashcount += (newhashdev << 32) / klninfo->status[dev].maxcount;
  455. // todo: check stats for critical conditions
  456. }
  457. newhashcount += 0xffffffffull * (uint64_t)klninfo->noncecount;
  458. klninfo->noncecount = 0;
  459. rd_unlock(&(klninfo->stat_lock));
  460. }
  461. return newhashcount;
  462. }
  463. static void get_klondike_statline_before(char *buf, size_t siz, struct cgpu_info *klncgpu)
  464. {
  465. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  466. uint8_t temp = 0xFF;
  467. uint16_t fan = 0;
  468. int dev;
  469. if (klninfo->status == NULL)
  470. return;
  471. rd_lock(&(klninfo->stat_lock));
  472. for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
  473. if (klninfo->status[dev].temp < temp)
  474. temp = klninfo->status[dev].temp;
  475. fan += klninfo->cfg[dev].fantarget;
  476. }
  477. fan /= klninfo->status->slavecount+1;
  478. rd_unlock(&(klninfo->stat_lock));
  479. tailsprintf(buf, siz, " %3.0fC %3d%% | ", cvtKlnToC(temp), fan*100/255);
  480. }
  481. static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
  482. {
  483. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  484. struct api_data *root = NULL;
  485. char buf[32];
  486. int dev;
  487. if (klninfo->status == NULL)
  488. return NULL;
  489. rd_lock(&(klninfo->stat_lock));
  490. for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
  491. float fTemp = cvtKlnToC(klninfo->status[dev].temp);
  492. sprintf(buf, "Temp %d", dev);
  493. root = api_add_temp(root, buf, &fTemp, true);
  494. double dClk = (double)klninfo->cfg[dev].hashclock;
  495. sprintf(buf, "Clock %d", dev);
  496. root = api_add_freq(root, buf, &dClk, true);
  497. unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].fantarget / 255;
  498. sprintf(buf, "Fan Percent %d", dev);
  499. root = api_add_int(root, buf, (int *)(&iFan), true);
  500. iFan = 0;
  501. if (klninfo->status[dev].fanspeed > 0)
  502. iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].fanspeed;
  503. sprintf(buf, "Fan RPM %d", dev);
  504. root = api_add_int(root, buf, (int *)(&iFan), true);
  505. if (klninfo->devinfo[dev].chipstats != NULL) {
  506. char data[2048];
  507. char one[32];
  508. int n;
  509. sprintf(buf, "Nonces / Chip %d", dev);
  510. data[0] = '\0';
  511. for (n = 0; n < klninfo->status[dev].chipcount; n++) {
  512. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n]);
  513. strcat(data, one);
  514. }
  515. root = api_add_string(root, buf, data, true);
  516. sprintf(buf, "Errors / Chip %d", dev);
  517. data[0] = '\0';
  518. for (n = 0; n < klninfo->status[dev].chipcount; n++) {
  519. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].chipcount]);
  520. strcat(data, one);
  521. }
  522. root = api_add_string(root, buf, data, true);
  523. }
  524. }
  525. root = api_add_uint64(root, "Hash Count", &(klninfo->hashcount), true);
  526. root = api_add_uint64(root, "Error Count", &(klninfo->errorcount), true);
  527. root = api_add_uint64(root, "Noise Count", &(klninfo->noisecount), true);
  528. rd_unlock(&(klninfo->stat_lock));
  529. return root;
  530. }
  531. struct device_drv klondike_drv = {
  532. .drv_id = DRIVER_klondike,
  533. .dname = "Klondike",
  534. .name = "KLN",
  535. .drv_detect = klondike_detect,
  536. .get_api_stats = klondike_api_stats,
  537. .get_statline_before = get_klondike_statline_before,
  538. .get_stats = klondike_get_stats,
  539. .identify_device = klondike_identify,
  540. .thread_prepare = klondike_thread_prepare,
  541. .thread_init = klondike_thread_init,
  542. .hash_work = hash_queued_work,
  543. .scanwork = klondike_scanwork,
  544. .queue_full = klondike_queue_full,
  545. .flush_work = klondike_flush_work,
  546. .thread_shutdown = klondike_shutdown,
  547. .thread_enable = klondike_thread_enable
  548. };