driver-klondike.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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 "deviceapi.h"
  26. #include "lowlevel.h"
  27. #include "miner.h"
  28. #define K1 "K1"
  29. #define K16 "K16"
  30. #define K64 "K64"
  31. #define MIDSTATE_BYTES 32
  32. #define MERKLE_OFFSET 64
  33. #define MERKLE_BYTES 12
  34. #define REPLY_SIZE 15 // adequate for all types of replies
  35. #define REPLY_BUFSIZE 16 // reply + 1 byte to mark used
  36. #define MAX_REPLY_COUNT 32 // more unhandled replies than this will result in data loss
  37. #define REPLY_WAIT_TIME 100 // poll interval for a cmd waiting it's reply
  38. #define CMD_REPLY_RETRIES 8 // how many retries for cmds
  39. #define MAX_WORK_COUNT 4 // for now, must be binary multiple and match firmware
  40. #define TACH_FACTOR 87890 // fan rpm divisor
  41. BFG_REGISTER_DRIVER(klondike_drv)
  42. typedef struct klondike_id {
  43. uint8_t version;
  44. uint8_t product[7];
  45. uint32_t serial;
  46. } IDENTITY;
  47. typedef struct klondike_status {
  48. uint8_t state;
  49. uint8_t chipcount;
  50. uint8_t slavecount;
  51. uint8_t workqc;
  52. uint8_t workid;
  53. uint8_t temp;
  54. uint8_t fanspeed;
  55. uint8_t errorcount;
  56. uint16_t hashcount;
  57. uint16_t maxcount;
  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. pthread_mutex_t devlock;
  99. struct libusb_device_handle *usbdev_handle;
  100. // TODO:
  101. bool usbinfo_nodev;
  102. };
  103. IDENTITY KlondikeID;
  104. static
  105. int usb_init(struct cgpu_info * const klncgpu, struct libusb_device * const dev)
  106. {
  107. struct klondike_info * const klninfo = klncgpu->device_data;
  108. int e;
  109. if (libusb_open(dev, &klninfo->usbdev_handle) != LIBUSB_SUCCESS)
  110. return 0;
  111. if (LIBUSB_SUCCESS != (e = libusb_set_configuration(klninfo->usbdev_handle, 1)))
  112. {
  113. applog(LOG_DEBUG, "%s: Failed to set configuration 1: %s",
  114. klondike_drv.dname, bfg_strerror(e, BST_LIBUSB));
  115. fail:
  116. libusb_close(klninfo->usbdev_handle);
  117. return 0;
  118. }
  119. if (LIBUSB_SUCCESS != (e = libusb_claim_interface(klninfo->usbdev_handle, 0)))
  120. {
  121. applog(LOG_DEBUG, "%s: Failed to claim interface 0: %s",
  122. klondike_drv.dname, bfg_strerror(e, BST_LIBUSB));
  123. goto fail;
  124. }
  125. return 1;
  126. }
  127. static
  128. int _usb_rw(struct cgpu_info * const klncgpu, void * const buf, const size_t bufsiz, int * const processed, int ep)
  129. {
  130. struct klondike_info * const klninfo = klncgpu->device_data;
  131. const unsigned int timeout = 999;
  132. unsigned char *cbuf = buf;
  133. int err, sent;
  134. *processed = 0;
  135. while (*processed < bufsiz)
  136. {
  137. mutex_lock(&klninfo->devlock);
  138. err = libusb_bulk_transfer(klninfo->usbdev_handle, ep, cbuf, bufsiz, &sent, timeout);
  139. mutex_unlock(&klninfo->devlock);
  140. if (unlikely(err))
  141. return err;
  142. *processed += sent;
  143. }
  144. return LIBUSB_SUCCESS;
  145. }
  146. #define usb_read( klncgpu, buf, bufsiz, processed) _usb_rw(klncgpu, buf, bufsiz, processed, 1 | LIBUSB_ENDPOINT_IN)
  147. #define usb_write(klncgpu, buf, bufsiz, processed) _usb_rw(klncgpu, buf, bufsiz, processed, 1 | LIBUSB_ENDPOINT_OUT)
  148. static
  149. void usb_uninit(struct cgpu_info * const klncgpu)
  150. {
  151. struct klondike_info * const klninfo = klncgpu->device_data;
  152. libusb_release_interface(klninfo->usbdev_handle, 0);
  153. libusb_close(klninfo->usbdev_handle);
  154. }
  155. static double cvtKlnToC(uint8_t temp)
  156. {
  157. return (double)1/((double)1/(25+273.15) + log((double)temp*1000/(256-temp)/2200)/3987) - 273.15;
  158. }
  159. static int cvtCToKln(double deg)
  160. {
  161. double R = exp((1/(deg+273.15)-1/(273.15+25))*3987)*2200;
  162. return 256*R/(R+1000);
  163. }
  164. static char *SendCmdGetReply(struct cgpu_info *klncgpu, char Cmd, int device, int datalen, void *data)
  165. {
  166. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  167. char outbuf[64];
  168. int retries = CMD_REPLY_RETRIES;
  169. int chkreply = klninfo->nextreply;
  170. int sent, err;
  171. if (klninfo->usbinfo_nodev)
  172. return NULL;
  173. outbuf[0] = Cmd;
  174. outbuf[1] = device;
  175. memcpy(outbuf+2, data, datalen);
  176. err = usb_write(klncgpu, outbuf, 2+datalen, &sent);
  177. if (err < 0 || sent != 2+datalen) {
  178. applog(LOG_ERR, "%s (%s) Cmd:%c Dev:%d, write failed (%d:%d)", klncgpu->drv->dname, klncgpu->device_path, Cmd, device, sent, err);
  179. }
  180. while (retries-- > 0 && klninfo->shutdown == false) {
  181. cgsleep_ms(REPLY_WAIT_TIME);
  182. while (*(klninfo->replies + chkreply*REPLY_BUFSIZE) != Cmd || *(klninfo->replies + chkreply*REPLY_BUFSIZE + 2) != device) {
  183. if (++chkreply == MAX_REPLY_COUNT)
  184. chkreply = 0;
  185. if (chkreply == klninfo->nextreply)
  186. break;
  187. }
  188. if (chkreply == klninfo->nextreply)
  189. continue;
  190. *(klninfo->replies + chkreply*REPLY_BUFSIZE) = '!'; // mark to prevent re-use
  191. return klninfo->replies + chkreply*REPLY_BUFSIZE + 1;
  192. }
  193. return NULL;
  194. }
  195. static bool klondike_get_stats(struct cgpu_info *klncgpu)
  196. {
  197. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  198. int slaves, dev;
  199. if (klninfo->usbinfo_nodev || klninfo->status == NULL)
  200. return false;
  201. applog(LOG_DEBUG, "Klondike getting status");
  202. slaves = klninfo->status[0].slavecount;
  203. // loop thru devices and get status for each
  204. wr_lock(&(klninfo->stat_lock));
  205. for (dev = 0; dev <= slaves; dev++) {
  206. char *reply = SendCmdGetReply(klncgpu, 'S', dev, 0, NULL);
  207. if (reply != NULL)
  208. memcpy((void *)(&(klninfo->status[dev])), reply+2, sizeof(klninfo->status[dev]));
  209. }
  210. wr_unlock(&(klninfo->stat_lock));
  211. // todo: detect slavecount change and realloc space
  212. return true;
  213. }
  214. static bool klondike_init(struct cgpu_info *klncgpu)
  215. {
  216. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  217. int slaves, dev;
  218. char *reply = SendCmdGetReply(klncgpu, 'S', 0, 0, NULL);
  219. if (reply == NULL)
  220. return false;
  221. slaves = ((WORKSTATUS *)(reply+2))->slavecount;
  222. if (klninfo->status == NULL) {
  223. applog(LOG_DEBUG, "Klondike initializing data");
  224. // alloc space for status, devinfo and cfg for master and slaves
  225. klninfo->status = calloc(slaves+1, sizeof(WORKSTATUS));
  226. if (unlikely(!klninfo->status))
  227. quit(1, "Failed to calloc status array in klondke_get_stats");
  228. klninfo->devinfo = calloc(slaves+1, sizeof(DEVINFO));
  229. if (unlikely(!klninfo->devinfo))
  230. quit(1, "Failed to calloc devinfo array in klondke_get_stats");
  231. klninfo->cfg = calloc(slaves+1, sizeof(WORKCFG));
  232. if (unlikely(!klninfo->cfg))
  233. quit(1, "Failed to calloc cfg array in klondke_get_stats");
  234. }
  235. WORKCFG cfgset = { 0,0,0,0,0 }; // zero init triggers read back only
  236. double temp1, temp2;
  237. int size = 2;
  238. if (opt_klondike_options != NULL) { // boundaries are checked by device, with valid values returned
  239. sscanf(opt_klondike_options, "%hu:%lf:%lf:%hhu", &cfgset.hashclock, &temp1, &temp2, &cfgset.fantarget);
  240. cfgset.temptarget = cvtCToKln(temp1);
  241. cfgset.tempcritical = cvtCToKln(temp2);
  242. cfgset.fantarget = (int)255*cfgset.fantarget/100;
  243. size = sizeof(cfgset);
  244. }
  245. for (dev = 0; dev <= slaves; dev++) {
  246. char *reply = SendCmdGetReply(klncgpu, 'C', dev, size, &cfgset);
  247. if (reply != NULL) {
  248. klninfo->cfg[dev] = *(WORKCFG *)(reply+2);
  249. applog(LOG_NOTICE, "Klondike config (%d: Clk: %d, T:%.0lf, C:%.0lf, F:%d)",
  250. dev, klninfo->cfg[dev].hashclock,
  251. cvtKlnToC(klninfo->cfg[dev].temptarget),
  252. cvtKlnToC(klninfo->cfg[dev].tempcritical),
  253. (int)100*klninfo->cfg[dev].fantarget/256);
  254. }
  255. }
  256. klondike_get_stats(klncgpu);
  257. for (dev = 0; dev <= slaves; dev++) {
  258. klninfo->devinfo[dev].rangesize = ((uint64_t)1<<32) / klninfo->status[dev].chipcount;
  259. klninfo->devinfo[dev].chipstats = calloc(klninfo->status[dev].chipcount*2 , sizeof(uint32_t));
  260. }
  261. SendCmdGetReply(klncgpu, 'E', 0, 1, "1");
  262. return true;
  263. }
  264. static
  265. bool klondike_foundlowl(struct lowlevel_device_info * const info, __maybe_unused void * const userp)
  266. {
  267. if (unlikely(info->lowl != &lowl_usb))
  268. {
  269. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not usb!",
  270. __func__, info->product, info->serial);
  271. return false;
  272. }
  273. struct libusb_device * const dev = info->lowl_data;
  274. // static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  275. struct cgpu_info * const klncgpu = malloc(sizeof(*klncgpu));
  276. struct klondike_info *klninfo = NULL;
  277. if (unlikely(!klncgpu))
  278. quit(1, "Failed to calloc klncgpu in klondike_detect_one");
  279. *klncgpu = (struct cgpu_info){
  280. .drv = &klondike_drv,
  281. .deven = DEV_ENABLED,
  282. .threads = 1,
  283. };
  284. klninfo = calloc(1, sizeof(*klninfo));
  285. if (unlikely(!klninfo))
  286. quit(1, "Failed to calloc klninfo in klondke_detect_one");
  287. klncgpu->device_data = (FILE *)klninfo;
  288. mutex_init(&klninfo->devlock);
  289. klninfo->replies = calloc(MAX_REPLY_COUNT, REPLY_BUFSIZE);
  290. if (unlikely(!klninfo->replies))
  291. quit(1, "Failed to calloc replies buffer in klondke_detect_one");
  292. klninfo->nextreply = 0;
  293. if (usb_init(klncgpu, dev)) {
  294. int attempts = 0;
  295. while (attempts++ < 3) {
  296. char reply[REPLY_SIZE];
  297. const char * const devpath = info->devid;
  298. int sent, recd, err;
  299. err = usb_write(klncgpu, "I", 2, &sent);
  300. if (err < 0 || sent != 2) {
  301. applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)", klncgpu->drv->dname, devpath, sent, err);
  302. }
  303. cgsleep_ms(REPLY_WAIT_TIME*10);
  304. err = usb_read(klncgpu, reply, REPLY_SIZE, &recd);
  305. if (err < 0) {
  306. applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)", klncgpu->drv->dname, devpath, recd, err);
  307. } else if (recd < 1) {
  308. applog(LOG_ERR, "%s (%s) detect empty reply (%d)", klncgpu->drv->dname, devpath, recd);
  309. } else if (reply[0] == 'I' && reply[1] == 0) {
  310. applog(LOG_DEBUG, "%s (%s) detect successful", klncgpu->drv->dname, devpath);
  311. KlondikeID = *(IDENTITY *)(&reply[2]);
  312. klncgpu->device_path = strdup(devpath);
  313. if (!add_cgpu(klncgpu))
  314. break;
  315. applog(LOG_DEBUG, "Klondike cgpu added");
  316. return true;
  317. }
  318. }
  319. usb_uninit(klncgpu);
  320. }
  321. free(klninfo->replies);
  322. free(klncgpu);
  323. return false;
  324. }
  325. static
  326. bool klondike_detect_one(const char *serial)
  327. {
  328. return lowlevel_detect_serial(klondike_foundlowl, serial);
  329. }
  330. static
  331. int klondike_autodetect()
  332. {
  333. return lowlevel_detect(klondike_foundlowl, "K16");
  334. }
  335. static
  336. void klondike_detect()
  337. {
  338. generic_detect(&klondike_drv, klondike_detect_one, klondike_autodetect, 0);
  339. }
  340. static
  341. bool klondike_identify(__maybe_unused struct cgpu_info * const klncgpu)
  342. {
  343. //SendCmdGetReply(klncgpu, 'I', 0, 0, NULL);
  344. return false;
  345. }
  346. static void klondike_check_nonce(struct cgpu_info *klncgpu, WORKRESULT *result)
  347. {
  348. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  349. struct work *work, *tmp;
  350. applog(LOG_DEBUG, "Klondike FOUND NONCE (%02x:%08x)", result->workid, result->nonce);
  351. HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
  352. if (work->queued && (work->subid == (result->device*256 + result->workid))) {
  353. wr_lock(&(klninfo->stat_lock));
  354. klninfo->devinfo[result->device].noncecount++;
  355. klninfo->noncecount++;
  356. wr_unlock(&(klninfo->stat_lock));
  357. result->nonce = le32toh(result->nonce - 0xC0);
  358. applog(LOG_DEBUG, "Klondike SUBMIT NONCE (%02x:%08x)", result->workid, result->nonce);
  359. bool ok = submit_nonce(klncgpu->thr[0], work, result->nonce);
  360. applog(LOG_DEBUG, "Klondike chip stats %d, %08x, %d, %d", result->device, result->nonce, klninfo->devinfo[result->device].rangesize, klninfo->status[result->device].chipcount);
  361. klninfo->devinfo[result->device].chipstats[(result->nonce / klninfo->devinfo[result->device].rangesize) + (ok ? 0 : klninfo->status[result->device].chipcount)]++;
  362. return;
  363. }
  364. }
  365. applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored",
  366. klncgpu->drv->name, klncgpu->device_id, result->device, result->workid, result->nonce);
  367. //inc_hw_errors(klncgpu->thr[0]);
  368. }
  369. // thread to keep looking for replies
  370. static void *klondike_get_replies(void *userdata)
  371. {
  372. struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
  373. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  374. char *replybuf;
  375. int err, recd;
  376. applog(LOG_DEBUG, "Klondike listening for replies");
  377. while (klninfo->shutdown == false) {
  378. if (klninfo->usbinfo_nodev)
  379. return NULL;
  380. replybuf = klninfo->replies + klninfo->nextreply * REPLY_BUFSIZE;
  381. replybuf[0] = 0;
  382. err = usb_read(klncgpu, replybuf+1, REPLY_SIZE, &recd);
  383. if (!err && recd == REPLY_SIZE) {
  384. if (opt_log_level <= LOG_DEBUG) {
  385. char hexdata[(recd * 2) + 1];
  386. bin2hex(hexdata, &replybuf[1], recd);
  387. applog(LOG_DEBUG, "%s (%s) reply [%s:%s]", klncgpu->drv->dname, klncgpu->device_path, replybuf+1, hexdata);
  388. }
  389. if (++klninfo->nextreply == MAX_REPLY_COUNT)
  390. klninfo->nextreply = 0;
  391. replybuf[0] = replybuf[1];
  392. if (replybuf[0] == '=')
  393. klondike_check_nonce(klncgpu, (WORKRESULT *)replybuf);
  394. }
  395. }
  396. return NULL;
  397. }
  398. static void klondike_flush_work(struct cgpu_info *klncgpu)
  399. {
  400. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  401. int dev;
  402. applog(LOG_DEBUG, "Klondike flushing work");
  403. for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
  404. char *reply = SendCmdGetReply(klncgpu, 'A', dev, 0, NULL);
  405. if (reply != NULL) {
  406. wr_lock(&(klninfo->stat_lock));
  407. klninfo->status[dev] = *(WORKSTATUS *)(reply+2);
  408. wr_unlock(&(klninfo->stat_lock));
  409. }
  410. }
  411. }
  412. static bool klondike_thread_prepare(struct thr_info *thr)
  413. {
  414. struct cgpu_info *klncgpu = thr->cgpu;
  415. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  416. if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) {
  417. applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id);
  418. return false;
  419. }
  420. pthread_detach(klninfo->replies_thr.pth);
  421. // let the listening get started
  422. cgsleep_ms(100);
  423. return klondike_init(klncgpu);
  424. }
  425. static bool klondike_thread_init(struct thr_info *thr)
  426. {
  427. struct cgpu_info *klncgpu = thr->cgpu;
  428. struct klondike_info * const klninfo = klncgpu->device_data;
  429. notifier_init(thr->work_restart_notifier);
  430. if (klninfo->usbinfo_nodev)
  431. return false;
  432. klondike_flush_work(klncgpu);
  433. return true;
  434. }
  435. static void klondike_shutdown(struct thr_info *thr)
  436. {
  437. struct cgpu_info *klncgpu = thr->cgpu;
  438. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  439. int dev;
  440. applog(LOG_DEBUG, "Klondike shutting down work");
  441. for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
  442. SendCmdGetReply(klncgpu, 'E', dev, 1, "0");
  443. }
  444. klncgpu->shutdown = klninfo->shutdown = true;
  445. }
  446. static void klondike_thread_enable(struct thr_info *thr)
  447. {
  448. struct cgpu_info *klncgpu = thr->cgpu;
  449. struct klondike_info * const klninfo = klncgpu->device_data;
  450. if (klninfo->usbinfo_nodev)
  451. return;
  452. //SendCmdGetReply(klncgpu, 'E', 0, 1, "0");
  453. }
  454. static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
  455. {
  456. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  457. struct work *tmp;
  458. WORKTASK data;
  459. if (klninfo->usbinfo_nodev)
  460. return false;
  461. memcpy(data.midstate, work->midstate, MIDSTATE_BYTES);
  462. memcpy(data.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  463. data.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
  464. work->subid = dev*256 + data.workid;
  465. if (opt_log_level <= LOG_DEBUG) {
  466. const size_t sz = sizeof(data) - 3;
  467. char hexdata[(sz * 2) + 1];
  468. bin2hex(hexdata, &data.workid, sz);
  469. applog(LOG_DEBUG, "WORKDATA: %s", hexdata);
  470. }
  471. applog(LOG_DEBUG, "Klondike sending work (%d:%02x)", dev, data.workid);
  472. char *reply = SendCmdGetReply(klncgpu, 'W', dev, sizeof(data)-3, &data.workid);
  473. if (reply != NULL) {
  474. wr_lock(&(klninfo->stat_lock));
  475. klninfo->status[dev] = *(WORKSTATUS *)(reply+2);
  476. wr_unlock(&(klninfo->stat_lock));
  477. // remove old work
  478. HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
  479. if (work->queued && (work->subid == (int)(dev*256 + ((klninfo->devinfo[dev].nextworkid-2*MAX_WORK_COUNT) & 0xFF))))
  480. work_completed(klncgpu, work);
  481. }
  482. return true;
  483. }
  484. return false;
  485. }
  486. static bool klondike_queue_full(struct cgpu_info *klncgpu)
  487. {
  488. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  489. struct work *work = NULL;
  490. int dev, queued;
  491. for (queued = 0; queued < MAX_WORK_COUNT-1; queued++)
  492. for (dev = 0; dev <= klninfo->status->slavecount; dev++)
  493. if (klninfo->status[dev].workqc <= queued) {
  494. if (!work)
  495. work = get_queued(klncgpu);
  496. if (unlikely(!work))
  497. return false;
  498. if (klondike_send_work(klncgpu, dev, work)) {
  499. work = NULL;
  500. break;
  501. }
  502. }
  503. return true;
  504. }
  505. static int64_t klondike_scanwork(struct thr_info *thr)
  506. {
  507. struct cgpu_info *klncgpu = thr->cgpu;
  508. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  509. int64_t newhashcount = 0;
  510. int dev;
  511. if (klninfo->usbinfo_nodev)
  512. return -1;
  513. restart_wait(thr, 200);
  514. if (klninfo->status != NULL) {
  515. rd_lock(&(klninfo->stat_lock));
  516. for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
  517. uint64_t newhashdev = 0;
  518. if (klninfo->devinfo[dev].lasthashcount > klninfo->status[dev].hashcount) // todo: chg this to check workid for wrapped instead
  519. newhashdev += klninfo->status[dev].maxcount; // hash counter wrapped
  520. newhashdev += klninfo->status[dev].hashcount - klninfo->devinfo[dev].lasthashcount;
  521. klninfo->devinfo[dev].lasthashcount = klninfo->status[dev].hashcount;
  522. klninfo->hashcount += (newhashdev << 32) / klninfo->status[dev].maxcount;
  523. newhashcount += 0xffffffffull * (uint64_t)klninfo->noncecount;
  524. // todo: check stats for critical conditions
  525. }
  526. rd_unlock(&(klninfo->stat_lock));
  527. }
  528. return newhashcount;
  529. }
  530. static void get_klondike_statline_before(char *buf, size_t siz, struct cgpu_info *klncgpu)
  531. {
  532. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  533. uint8_t temp = 0xFF;
  534. uint16_t fan = 0;
  535. int dev;
  536. if (klninfo->status == NULL)
  537. return;
  538. rd_lock(&(klninfo->stat_lock));
  539. for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
  540. if (klninfo->status[dev].temp < temp)
  541. temp = klninfo->status[dev].temp;
  542. fan += klninfo->cfg[dev].fantarget;
  543. }
  544. fan /= klninfo->status->slavecount+1;
  545. rd_unlock(&(klninfo->stat_lock));
  546. tailsprintf(buf, siz, " %3.0fC %3d%% | ", cvtKlnToC(temp), fan*100/255);
  547. }
  548. static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
  549. {
  550. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  551. struct api_data *root = NULL;
  552. char buf[32];
  553. int dev;
  554. if (klninfo->status == NULL)
  555. return NULL;
  556. rd_lock(&(klninfo->stat_lock));
  557. for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
  558. float fTemp = cvtKlnToC(klninfo->status[dev].temp);
  559. sprintf(buf, "Temp %d", dev);
  560. root = api_add_temp(root, buf, &fTemp, true);
  561. double dClk = (double)klninfo->cfg[dev].hashclock;
  562. sprintf(buf, "Clock %d", dev);
  563. root = api_add_freq(root, buf, &dClk, true);
  564. unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].fantarget / 255;
  565. sprintf(buf, "Fan Percent %d", dev);
  566. root = api_add_int(root, buf, (int *)(&iFan), true);
  567. iFan = 0;
  568. if (klninfo->status[dev].fanspeed > 0)
  569. iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].fanspeed;
  570. sprintf(buf, "Fan RPM %d", dev);
  571. root = api_add_int(root, buf, (int *)(&iFan), true);
  572. if (klninfo->devinfo[dev].chipstats != NULL) {
  573. char data[128];
  574. int n;
  575. sprintf(buf, "Nonces / Chip %d", dev);
  576. for (n = 0; n < klninfo->status[dev].chipcount; n++)
  577. sprintf(data+n*8, "%07d ", klninfo->devinfo[dev].chipstats[n]);
  578. data[127] = 0;
  579. root = api_add_string(root, buf, data, true);
  580. sprintf(buf, "Errors / Chip %d", dev);
  581. for (n = 0; n < klninfo->status[dev].chipcount; n++)
  582. sprintf(data+n*8, "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].chipcount]);
  583. data[127] = 0;
  584. root = api_add_string(root, buf, data, true);
  585. }
  586. }
  587. root = api_add_uint64(root, "Hash Count", &(klninfo->hashcount), true);
  588. rd_unlock(&(klninfo->stat_lock));
  589. return root;
  590. }
  591. struct device_drv klondike_drv = {
  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. .minerloop = 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. };