driver-klondike.c 24 KB

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