driver-klondike.c 21 KB

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