driver-klondike.c 21 KB

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