driver-klondike.c 23 KB

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