driver-klondike.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  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. static const char *msg_detect_send = "DSend";
  32. static const char *msg_detect_reply = "DReply";
  33. static const char *msg_send = "Send";
  34. static const char *msg_reply = "Reply";
  35. #define KLN_CMD_ABORT 'A'
  36. #define KLN_CMD_CONFIG 'C'
  37. #define KLN_CMD_ENABLE 'E'
  38. #define KLN_CMD_IDENT 'I'
  39. #define KLN_CMD_NONCE '='
  40. #define KLN_CMD_STATUS 'S'
  41. #define KLN_CMD_WORK 'W'
  42. #define KLN_CMD_ENABLE_OFF '0'
  43. #define KLN_CMD_ENABLE_ON '1'
  44. #define MIDSTATE_BYTES 32
  45. #define MERKLE_OFFSET 64
  46. #define MERKLE_BYTES 12
  47. #define REPLY_SIZE 15 // adequate for all types of replies
  48. #define MAX_KLINES 1024 // unhandled reply limit
  49. #define REPLY_WAIT_TIME 100 // poll interval for a cmd waiting it's reply
  50. #define CMD_REPLY_RETRIES 8 // how many retries for cmds
  51. #define MAX_WORK_COUNT 4 // for now, must be binary multiple and match firmware
  52. #define TACH_FACTOR 87890 // fan rpm divisor
  53. #define KLN_KILLWORK_TEMP 53.5
  54. #define KLN_COOLED_DOWN 45.5
  55. // If 5 late updates in a row, try to reset the device
  56. #define KLN_LATE_UPDATE_LIMIT 5
  57. /*
  58. * Work older than 5s will already be completed
  59. * FYI it must not be possible to complete 256 work
  60. * items this quickly on a single device -
  61. * thus limited to 219.9GH/s per device
  62. */
  63. #define OLD_WORK_MS ((int)(5 * 1000))
  64. /*
  65. * If the queue status hasn't been updated for this long then do it now
  66. * 5GH/s = 859ms per full nonce range
  67. */
  68. #define LATE_UPDATE_MS ((int)(2.5 * 1000))
  69. BFG_REGISTER_DRIVER(klondike_drv)
  70. typedef struct klondike_header {
  71. uint8_t cmd;
  72. uint8_t dev;
  73. uint8_t buf[REPLY_SIZE-2];
  74. } HEADER;
  75. #define K_2(_bytes) ((int)(_bytes[0]) + \
  76. ((int)(_bytes[1]) << 8))
  77. #define K_4(_bytes) ((uint64_t)(_bytes[0]) + \
  78. ((uint64_t)(_bytes[1]) << 8) + \
  79. ((uint64_t)(_bytes[2]) << 16) + \
  80. ((uint64_t)(_bytes[3]) << 24))
  81. #define K_SERIAL(_serial) K_4(_serial)
  82. #define K_HASHCOUNT(_hashcount) K_2(_hashcount)
  83. #define K_MAXCOUNT(_maxcount) K_2(_maxcount)
  84. #define K_NONCE(_nonce) K_4(_nonce)
  85. #define K_HASHCLOCK(_hashclock) K_2(_hashclock)
  86. #define SET_HASHCLOCK(_hashclock, _value) do { \
  87. (_hashclock)[0] = (uint8_t)((_value) & 0xff); \
  88. (_hashclock)[1] = (uint8_t)(((_value) >> 8) & 0xff); \
  89. } while(0)
  90. #define KSENDHD(_add) (sizeof(uint8_t) + sizeof(uint8_t) + _add)
  91. typedef struct klondike_id {
  92. uint8_t cmd;
  93. uint8_t dev;
  94. uint8_t version;
  95. uint8_t product[7];
  96. uint8_t serial[4];
  97. } IDENTITY;
  98. typedef struct klondike_status {
  99. uint8_t cmd;
  100. uint8_t dev;
  101. uint8_t state;
  102. uint8_t chipcount;
  103. uint8_t slavecount;
  104. uint8_t workqc;
  105. uint8_t workid;
  106. uint8_t temp;
  107. uint8_t fanspeed;
  108. uint8_t errorcount;
  109. uint8_t hashcount[2];
  110. uint8_t maxcount[2];
  111. uint8_t noise;
  112. } WORKSTATUS;
  113. typedef struct _worktask {
  114. uint8_t cmd;
  115. uint8_t dev;
  116. uint8_t workid;
  117. uint8_t midstate[32];
  118. uint8_t merkle[12];
  119. } WORKTASK;
  120. typedef struct _workresult {
  121. uint8_t cmd;
  122. uint8_t dev;
  123. uint8_t workid;
  124. uint8_t nonce[4];
  125. } WORKRESULT;
  126. typedef struct klondike_cfg {
  127. uint8_t cmd;
  128. uint8_t dev;
  129. uint8_t hashclock[2];
  130. uint8_t temptarget;
  131. uint8_t tempcritical;
  132. uint8_t fantarget;
  133. uint8_t pad2;
  134. } WORKCFG;
  135. typedef struct kline {
  136. union {
  137. HEADER hd;
  138. IDENTITY id;
  139. WORKSTATUS ws;
  140. WORKTASK wt;
  141. WORKRESULT wr;
  142. WORKCFG cfg;
  143. };
  144. } KLINE;
  145. #define zero_kline(_kline) memset((void *)(_kline), 0, sizeof(KLINE));
  146. typedef struct device_info {
  147. uint32_t noncecount;
  148. uint32_t nextworkid;
  149. uint16_t lasthashcount;
  150. uint64_t totalhashcount;
  151. uint32_t rangesize;
  152. uint32_t *chipstats;
  153. } DEVINFO;
  154. typedef struct klist {
  155. struct klist *prev;
  156. struct klist *next;
  157. KLINE kline;
  158. struct timeval tv_when;
  159. int block_seq;
  160. bool ready;
  161. bool working;
  162. } KLIST;
  163. typedef struct jobque {
  164. int workqc;
  165. struct timeval last_update;
  166. bool overheat;
  167. int late_update_count;
  168. int late_update_sequential;
  169. } JOBQUE;
  170. struct klondike_info {
  171. bool shutdown;
  172. pthread_rwlock_t stat_lock;
  173. struct thr_info replies_thr;
  174. cglock_t klist_lock;
  175. KLIST *used;
  176. KLIST *free;
  177. int kline_count;
  178. int used_count;
  179. int block_seq;
  180. KLIST *status;
  181. DEVINFO *devinfo;
  182. KLIST *cfg;
  183. JOBQUE *jobque;
  184. int noncecount;
  185. uint64_t hashcount;
  186. uint64_t errorcount;
  187. uint64_t noisecount;
  188. // us Delay from USB reply to being processed
  189. double delay_count;
  190. double delay_total;
  191. double delay_min;
  192. double delay_max;
  193. struct timeval tv_last_nonce_received;
  194. // Time from recieving one nonce to the next
  195. double nonce_count;
  196. double nonce_total;
  197. double nonce_min;
  198. double nonce_max;
  199. int wque_size;
  200. int wque_cleared;
  201. bool initialised;
  202. struct libusb_device_handle *usbdev_handle;
  203. // TODO:
  204. bool usbinfo_nodev;
  205. };
  206. static KLIST *new_klist_set(struct cgpu_info *klncgpu)
  207. {
  208. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  209. KLIST *klist = NULL;
  210. int i;
  211. klist = calloc(MAX_KLINES, sizeof(*klist));
  212. if (!klist)
  213. quit(1, "Failed to calloc klist - when old count=%d", klninfo->kline_count);
  214. klninfo->kline_count += MAX_KLINES;
  215. klist[0].prev = NULL;
  216. klist[0].next = &(klist[1]);
  217. for (i = 1; i < MAX_KLINES-1; i++) {
  218. klist[i].prev = &klist[i-1];
  219. klist[i].next = &klist[i+1];
  220. }
  221. klist[MAX_KLINES-1].prev = &(klist[MAX_KLINES-2]);
  222. klist[MAX_KLINES-1].next = NULL;
  223. return klist;
  224. }
  225. static KLIST *allocate_kitem(struct cgpu_info *klncgpu)
  226. {
  227. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  228. KLIST *kitem = NULL;
  229. int ran_out = 0;
  230. char errbuf[1024];
  231. cg_wlock(&klninfo->klist_lock);
  232. if (klninfo->free == NULL) {
  233. ran_out = klninfo->kline_count;
  234. klninfo->free = new_klist_set(klncgpu);
  235. snprintf(errbuf, sizeof(errbuf),
  236. "%s%i: KLINE count exceeded %d, now %d",
  237. klncgpu->drv->name, klncgpu->device_id,
  238. ran_out, klninfo->kline_count);
  239. }
  240. kitem = klninfo->free;
  241. klninfo->free = klninfo->free->next;
  242. if (klninfo->free)
  243. klninfo->free->prev = NULL;
  244. kitem->next = klninfo->used;
  245. kitem->prev = NULL;
  246. if (kitem->next)
  247. kitem->next->prev = kitem;
  248. klninfo->used = kitem;
  249. kitem->ready = false;
  250. kitem->working = false;
  251. memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
  252. klninfo->used_count++;
  253. cg_wunlock(&klninfo->klist_lock);
  254. if (ran_out > 0)
  255. applog(LOG_ERR, "%s", errbuf);
  256. return kitem;
  257. }
  258. static KLIST *release_kitem(struct cgpu_info *klncgpu, KLIST *kitem)
  259. {
  260. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  261. cg_wlock(&klninfo->klist_lock);
  262. if (kitem == klninfo->used)
  263. klninfo->used = kitem->next;
  264. if (kitem->next)
  265. kitem->next->prev = kitem->prev;
  266. if (kitem->prev)
  267. kitem->prev->next = kitem->next;
  268. kitem->next = klninfo->free;
  269. if (klninfo->free)
  270. klninfo->free->prev = kitem;
  271. kitem->prev = NULL;
  272. klninfo->free = kitem;
  273. klninfo->used_count--;
  274. cg_wunlock(&klninfo->klist_lock);
  275. return NULL;
  276. }
  277. static
  278. int usb_init(struct cgpu_info * const klncgpu, struct libusb_device * const dev)
  279. {
  280. struct klondike_info * const klninfo = klncgpu->device_data;
  281. int e;
  282. if (libusb_open(dev, &klninfo->usbdev_handle) != LIBUSB_SUCCESS)
  283. return 0;
  284. if (LIBUSB_SUCCESS != (e = libusb_set_configuration(klninfo->usbdev_handle, 1)))
  285. {
  286. applog(LOG_DEBUG, "%s: Failed to set configuration 1: %s",
  287. klondike_drv.dname, bfg_strerror(e, BST_LIBUSB));
  288. fail:
  289. libusb_close(klninfo->usbdev_handle);
  290. return 0;
  291. }
  292. if (LIBUSB_SUCCESS != (e = libusb_claim_interface(klninfo->usbdev_handle, 0)))
  293. {
  294. applog(LOG_DEBUG, "%s: Failed to claim interface 0: %s",
  295. klondike_drv.dname, bfg_strerror(e, BST_LIBUSB));
  296. goto fail;
  297. }
  298. return 1;
  299. }
  300. static
  301. int _usb_rw(struct cgpu_info * const klncgpu, void * const buf, const size_t bufsiz, int * const processed, int ep)
  302. {
  303. struct klondike_info * const klninfo = klncgpu->device_data;
  304. const unsigned int timeout = 999;
  305. unsigned char *cbuf = buf;
  306. int err, sent;
  307. *processed = 0;
  308. while (*processed < bufsiz)
  309. {
  310. err = libusb_bulk_transfer(klninfo->usbdev_handle, ep, cbuf, bufsiz, &sent, timeout);
  311. if (unlikely(err))
  312. return err;
  313. *processed += sent;
  314. }
  315. return LIBUSB_SUCCESS;
  316. }
  317. #define usb_read( klncgpu, buf, bufsiz, processed) _usb_rw(klncgpu, buf, bufsiz, processed, 1 | LIBUSB_ENDPOINT_IN)
  318. #define usb_write(klncgpu, buf, bufsiz, processed) _usb_rw(klncgpu, buf, bufsiz, processed, 1 | LIBUSB_ENDPOINT_OUT)
  319. static
  320. void usb_uninit(struct cgpu_info * const klncgpu)
  321. {
  322. struct klondike_info * const klninfo = klncgpu->device_data;
  323. libusb_release_interface(klninfo->usbdev_handle, 0);
  324. libusb_close(klninfo->usbdev_handle);
  325. }
  326. static double cvtKlnToC(uint8_t temp)
  327. {
  328. double Rt, stein, celsius;
  329. if (temp == 0)
  330. return 0.0;
  331. Rt = 1000.0 * 255.0 / (double)temp - 1000.0;
  332. stein = log(Rt / 2200.0) / 3987.0;
  333. stein += 1.0 / (double)(25.0 + 273.15);
  334. celsius = (1.0 / stein) - 273.15;
  335. // For display of bad data
  336. if (celsius < 0.0)
  337. celsius = 0.0;
  338. if (celsius > 200.0)
  339. celsius = 200.0;
  340. return celsius;
  341. }
  342. static int cvtCToKln(double deg)
  343. {
  344. double Rt, stein, temp;
  345. if (deg < 0.0)
  346. deg = 0.0;
  347. stein = 1.0 / (deg + 273.15);
  348. stein -= 1.0 / (double)(25.0 + 273.15);
  349. Rt = exp(stein * 3987.0) * 2200.0;
  350. if (Rt == -1000.0)
  351. Rt++;
  352. temp = 1000.0 * 256.0 / (Rt + 1000.0);
  353. if (temp > 255)
  354. temp = 255;
  355. if (temp < 0)
  356. temp = 0;
  357. return (int)temp;
  358. }
  359. // Change this to LOG_WARNING if you wish to always see the replies
  360. #define READ_DEBUG LOG_DEBUG
  361. static void display_kline(struct cgpu_info *klncgpu, KLINE *kline, const char *msg)
  362. {
  363. switch (kline->hd.cmd) {
  364. case KLN_CMD_NONCE:
  365. applog(READ_DEBUG,
  366. "%s%i:%d %s work [%c] dev=%d workid=%d"
  367. " nonce=0x%08x",
  368. klncgpu->drv->name, klncgpu->device_id,
  369. (int)(kline->wr.dev), msg, kline->wr.cmd,
  370. (int)(kline->wr.dev),
  371. (int)(kline->wr.workid),
  372. (unsigned int)K_NONCE(kline->wr.nonce) - 0xC0);
  373. break;
  374. case KLN_CMD_STATUS:
  375. case KLN_CMD_WORK:
  376. case KLN_CMD_ENABLE:
  377. case KLN_CMD_ABORT:
  378. applog(READ_DEBUG,
  379. "%s%i:%d %s status [%c] dev=%d chips=%d"
  380. " slaves=%d workcq=%d workid=%d temp=%d fan=%d"
  381. " errors=%d hashes=%d max=%d noise=%d",
  382. klncgpu->drv->name, klncgpu->device_id,
  383. (int)(kline->ws.dev), msg, kline->ws.cmd,
  384. (int)(kline->ws.dev),
  385. (int)(kline->ws.chipcount),
  386. (int)(kline->ws.slavecount),
  387. (int)(kline->ws.workqc),
  388. (int)(kline->ws.workid),
  389. (int)(kline->ws.temp),
  390. (int)(kline->ws.fanspeed),
  391. (int)(kline->ws.errorcount),
  392. K_HASHCOUNT(kline->ws.hashcount),
  393. K_MAXCOUNT(kline->ws.maxcount),
  394. (int)(kline->ws.noise));
  395. break;
  396. case KLN_CMD_CONFIG:
  397. applog(READ_DEBUG,
  398. "%s%i:%d %s config [%c] dev=%d clock=%d"
  399. " temptarget=%d tempcrit=%d fan=%d",
  400. klncgpu->drv->name, klncgpu->device_id,
  401. (int)(kline->cfg.dev), msg, kline->cfg.cmd,
  402. (int)(kline->cfg.dev),
  403. K_HASHCLOCK(kline->cfg.hashclock),
  404. (int)(kline->cfg.temptarget),
  405. (int)(kline->cfg.tempcritical),
  406. (int)(kline->cfg.fantarget));
  407. break;
  408. case KLN_CMD_IDENT:
  409. applog(READ_DEBUG,
  410. "%s%i:%d %s info [%c] version=0x%02x prod=%.7s"
  411. " serial=0x%08x",
  412. klncgpu->drv->name, klncgpu->device_id,
  413. (int)(kline->hd.dev), msg, kline->hd.cmd,
  414. (int)(kline->id.version),
  415. kline->id.product,
  416. (unsigned int)K_SERIAL(kline->id.serial));
  417. break;
  418. default:
  419. {
  420. char hexdata[REPLY_SIZE * 2];
  421. bin2hex(hexdata, &kline->hd.dev, REPLY_SIZE - 1);
  422. applog(LOG_ERR,
  423. "%s%i:%d %s [%c:%s] unknown and ignored",
  424. klncgpu->drv->name, klncgpu->device_id,
  425. (int)(kline->hd.dev), msg, kline->hd.cmd,
  426. hexdata);
  427. free(hexdata);
  428. break;
  429. }
  430. }
  431. }
  432. static void display_send_kline(struct cgpu_info *klncgpu, KLINE *kline, const char *msg)
  433. {
  434. switch (kline->hd.cmd) {
  435. case KLN_CMD_WORK:
  436. applog(READ_DEBUG,
  437. "%s%i:%d %s work [%c] dev=%d workid=0x%02x ...",
  438. klncgpu->drv->name, klncgpu->device_id,
  439. (int)(kline->wt.dev), msg, kline->ws.cmd,
  440. (int)(kline->wt.dev),
  441. (int)(kline->wt.workid));
  442. break;
  443. case KLN_CMD_CONFIG:
  444. applog(READ_DEBUG,
  445. "%s%i:%d %s config [%c] dev=%d clock=%d"
  446. " temptarget=%d tempcrit=%d fan=%d",
  447. klncgpu->drv->name, klncgpu->device_id,
  448. (int)(kline->cfg.dev), msg, kline->cfg.cmd,
  449. (int)(kline->cfg.dev),
  450. K_HASHCLOCK(kline->cfg.hashclock),
  451. (int)(kline->cfg.temptarget),
  452. (int)(kline->cfg.tempcritical),
  453. (int)(kline->cfg.fantarget));
  454. break;
  455. case KLN_CMD_IDENT:
  456. case KLN_CMD_STATUS:
  457. case KLN_CMD_ABORT:
  458. applog(READ_DEBUG,
  459. "%s%i:%d %s cmd [%c]",
  460. klncgpu->drv->name, klncgpu->device_id,
  461. (int)(kline->hd.dev), msg, kline->hd.cmd);
  462. break;
  463. case KLN_CMD_ENABLE:
  464. applog(READ_DEBUG,
  465. "%s%i:%d %s enable [%c] enable=%c",
  466. klncgpu->drv->name, klncgpu->device_id,
  467. (int)(kline->hd.dev), msg, kline->hd.cmd,
  468. (char)(kline->hd.buf[0]));
  469. break;
  470. case KLN_CMD_NONCE:
  471. default:
  472. {
  473. char hexdata[REPLY_SIZE * 2];
  474. bin2hex(hexdata, (unsigned char *)&(kline->hd.dev), REPLY_SIZE - 1);
  475. applog(LOG_ERR,
  476. "%s%i:%d %s [%c:%s] unknown/unexpected and ignored",
  477. klncgpu->drv->name, klncgpu->device_id,
  478. (int)(kline->hd.dev), msg, kline->hd.cmd,
  479. hexdata);
  480. break;
  481. }
  482. }
  483. }
  484. static bool SendCmd(struct cgpu_info *klncgpu, KLINE *kline, int datalen)
  485. {
  486. struct klondike_info * const klninfo = klncgpu->device_data;
  487. int err, amt, writ;
  488. if (klninfo->usbinfo_nodev)
  489. return false;
  490. display_send_kline(klncgpu, kline, msg_send);
  491. writ = KSENDHD(datalen);
  492. err = usb_write(klncgpu, kline, writ, &amt);
  493. if (err < 0 || amt != writ) {
  494. applog(LOG_ERR, "%s%i:%d Cmd:%c Dev:%d, write failed (%d:%d:%d)",
  495. klncgpu->drv->name, klncgpu->device_id,
  496. (int)(kline->hd.dev),
  497. kline->hd.cmd, (int)(kline->hd.dev),
  498. writ, amt, err);
  499. return false;
  500. }
  501. return true;
  502. }
  503. static KLIST *GetReply(struct cgpu_info *klncgpu, uint8_t cmd, uint8_t dev)
  504. {
  505. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  506. KLIST *kitem;
  507. int retries = CMD_REPLY_RETRIES;
  508. while (retries-- > 0 && klninfo->shutdown == false) {
  509. cgsleep_ms(REPLY_WAIT_TIME);
  510. cg_rlock(&klninfo->klist_lock);
  511. kitem = klninfo->used;
  512. while (kitem) {
  513. if (kitem->kline.hd.cmd == cmd &&
  514. kitem->kline.hd.dev == dev &&
  515. kitem->ready == true && kitem->working == false) {
  516. kitem->working = true;
  517. cg_runlock(&klninfo->klist_lock);
  518. return kitem;
  519. }
  520. kitem = kitem->next;
  521. }
  522. cg_runlock(&klninfo->klist_lock);
  523. }
  524. return NULL;
  525. }
  526. static KLIST *SendCmdGetReply(struct cgpu_info *klncgpu, KLINE *kline, int datalen)
  527. {
  528. if (!SendCmd(klncgpu, kline, datalen))
  529. return NULL;
  530. return GetReply(klncgpu, kline->hd.cmd, kline->hd.dev);
  531. }
  532. static bool klondike_get_stats(struct cgpu_info *klncgpu)
  533. {
  534. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  535. KLIST *kitem;
  536. KLINE kline;
  537. int slaves, dev;
  538. if (klninfo->usbinfo_nodev || klninfo->status == NULL)
  539. return false;
  540. applog(LOG_DEBUG, "%s%i: getting status",
  541. klncgpu->drv->name, klncgpu->device_id);
  542. rd_lock(&(klninfo->stat_lock));
  543. slaves = klninfo->status[0].kline.ws.slavecount;
  544. rd_unlock(&(klninfo->stat_lock));
  545. // loop thru devices and get status for each
  546. for (dev = 0; dev <= slaves; dev++) {
  547. zero_kline(&kline);
  548. kline.hd.cmd = KLN_CMD_STATUS;
  549. kline.hd.dev = dev;
  550. kitem = SendCmdGetReply(klncgpu, &kline, 0);
  551. if (kitem != NULL) {
  552. wr_lock(&(klninfo->stat_lock));
  553. memcpy((void *)(&(klninfo->status[dev])),
  554. (void *)kitem,
  555. sizeof(klninfo->status[dev]));
  556. wr_unlock(&(klninfo->stat_lock));
  557. kitem = release_kitem(klncgpu, kitem);
  558. } else {
  559. applog(LOG_ERR, "%s%i:%d failed to update stats",
  560. klncgpu->drv->name, klncgpu->device_id, dev);
  561. }
  562. }
  563. return true;
  564. }
  565. // TODO: this only enables the master (no slaves)
  566. static bool kln_enable(struct cgpu_info *klncgpu)
  567. {
  568. KLIST *kitem;
  569. KLINE kline;
  570. int tries = 2;
  571. bool ok = false;
  572. zero_kline(&kline);
  573. kline.hd.cmd = KLN_CMD_ENABLE;
  574. kline.hd.dev = 0;
  575. kline.hd.buf[0] = KLN_CMD_ENABLE_ON;
  576. while (tries-- > 0) {
  577. kitem = SendCmdGetReply(klncgpu, &kline, 1);
  578. if (kitem) {
  579. kitem = release_kitem(klncgpu, kitem);
  580. ok = true;
  581. break;
  582. }
  583. cgsleep_ms(50);
  584. }
  585. if (ok)
  586. cgsleep_ms(50);
  587. return ok;
  588. }
  589. static void kln_disable(struct cgpu_info *klncgpu, int dev, bool all)
  590. {
  591. KLINE kline;
  592. int i;
  593. zero_kline(&kline);
  594. kline.hd.cmd = KLN_CMD_ENABLE;
  595. kline.hd.buf[0] = KLN_CMD_ENABLE_OFF;
  596. for (i = (all ? 0 : dev); i <= dev; i++) {
  597. kline.hd.dev = i;
  598. SendCmd(klncgpu, &kline, KSENDHD(1));
  599. }
  600. }
  601. static bool klondike_init(struct cgpu_info *klncgpu)
  602. {
  603. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  604. KLIST *kitem;
  605. KLINE kline;
  606. int slaves, dev;
  607. klninfo->initialised = false;
  608. zero_kline(&kline);
  609. kline.hd.cmd = KLN_CMD_STATUS;
  610. kline.hd.dev = 0;
  611. kitem = SendCmdGetReply(klncgpu, &kline, 0);
  612. if (kitem == NULL)
  613. return false;
  614. slaves = kitem->kline.ws.slavecount;
  615. if (klninfo->status == NULL) {
  616. applog(LOG_DEBUG, "%s%i: initializing data",
  617. klncgpu->drv->name, klncgpu->device_id);
  618. // alloc space for status, devinfo, cfg and jobque for master and slaves
  619. klninfo->status = calloc(slaves+1, sizeof(*(klninfo->status)));
  620. if (unlikely(!klninfo->status))
  621. quit(1, "Failed to calloc status array in klondke_get_stats");
  622. klninfo->devinfo = calloc(slaves+1, sizeof(*(klninfo->devinfo)));
  623. if (unlikely(!klninfo->devinfo))
  624. quit(1, "Failed to calloc devinfo array in klondke_get_stats");
  625. klninfo->cfg = calloc(slaves+1, sizeof(*(klninfo->cfg)));
  626. if (unlikely(!klninfo->cfg))
  627. quit(1, "Failed to calloc cfg array in klondke_get_stats");
  628. klninfo->jobque = calloc(slaves+1, sizeof(*(klninfo->jobque)));
  629. if (unlikely(!klninfo->jobque))
  630. quit(1, "Failed to calloc jobque array in klondke_get_stats");
  631. }
  632. memcpy((void *)(&(klninfo->status[0])), (void *)kitem, sizeof(klninfo->status[0]));
  633. kitem = release_kitem(klncgpu, kitem);
  634. // zero init triggers read back only
  635. zero_kline(&kline);
  636. kline.cfg.cmd = KLN_CMD_CONFIG;
  637. int size = 2;
  638. // boundaries are checked by device, with valid values returned
  639. if (opt_klondike_options != NULL) {
  640. int hashclock;
  641. double temp1, temp2;
  642. sscanf(opt_klondike_options, "%d:%lf:%lf:%"SCNu8,
  643. &hashclock,
  644. &temp1, &temp2,
  645. &kline.cfg.fantarget);
  646. SET_HASHCLOCK(kline.cfg.hashclock, hashclock);
  647. kline.cfg.temptarget = cvtCToKln(temp1);
  648. kline.cfg.tempcritical = cvtCToKln(temp2);
  649. kline.cfg.fantarget = (int)255*kline.cfg.fantarget/100;
  650. size = sizeof(kline.cfg) - 2;
  651. }
  652. for (dev = 0; dev <= slaves; dev++) {
  653. kline.cfg.dev = dev;
  654. kitem = SendCmdGetReply(klncgpu, &kline, size);
  655. if (kitem != NULL) {
  656. memcpy((void *)&(klninfo->cfg[dev]), kitem, sizeof(klninfo->cfg[dev]));
  657. applog(LOG_WARNING, "%s%i:%d config (%d: Clk: %d, T:%.0lf, C:%.0lf, F:%d)",
  658. klncgpu->drv->name, klncgpu->device_id, dev,
  659. dev, K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock),
  660. cvtKlnToC(klninfo->cfg[dev].kline.cfg.temptarget),
  661. cvtKlnToC(klninfo->cfg[dev].kline.cfg.tempcritical),
  662. (int)100*klninfo->cfg[dev].kline.cfg.fantarget/256);
  663. kitem = release_kitem(klncgpu, kitem);
  664. }
  665. }
  666. klondike_get_stats(klncgpu);
  667. klninfo->initialised = true;
  668. for (dev = 0; dev <= slaves; dev++) {
  669. klninfo->devinfo[dev].rangesize = ((uint64_t)1<<32) / klninfo->status[dev].kline.ws.chipcount;
  670. klninfo->devinfo[dev].chipstats = calloc(klninfo->status[dev].kline.ws.chipcount*2 , sizeof(uint32_t));
  671. }
  672. bool ok = kln_enable(klncgpu);
  673. if (!ok)
  674. applog(LOG_ERR, "%s%i: failed to enable", klncgpu->drv->name, klncgpu->device_id);
  675. return ok;
  676. }
  677. static void control_init(struct cgpu_info *klncgpu)
  678. {
  679. struct klondike_info * const klninfo = klncgpu->device_data;
  680. int err, interface;
  681. if (klninfo->usbinfo_nodev)
  682. return;
  683. interface = 0;
  684. err = libusb_control_transfer(klninfo->usbdev_handle, 0, 9, 1, interface, NULL, 0, 999);
  685. applog(LOG_DEBUG, "%s%i: reset got err %d",
  686. klncgpu->drv->name, klncgpu->device_id, err);
  687. }
  688. static
  689. bool klondike_foundlowl(struct lowlevel_device_info * const info, __maybe_unused void * const userp)
  690. {
  691. if (unlikely(info->lowl != &lowl_usb))
  692. {
  693. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not usb!",
  694. __func__, info->product, info->serial);
  695. return false;
  696. }
  697. struct libusb_device * const dev = info->lowl_data;
  698. // static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  699. struct cgpu_info * const klncgpu = malloc(sizeof(*klncgpu));
  700. struct klondike_info *klninfo = NULL;
  701. KLINE kline;
  702. if (unlikely(!klncgpu))
  703. quit(1, "Failed to calloc klncgpu in klondike_detect_one");
  704. *klncgpu = (struct cgpu_info){
  705. .drv = &klondike_drv,
  706. .deven = DEV_ENABLED,
  707. .threads = 1,
  708. };
  709. klninfo = calloc(1, sizeof(*klninfo));
  710. if (unlikely(!klninfo))
  711. quit(1, "Failed to calloc klninfo in klondke_detect_one");
  712. klncgpu->device_data = (void *)klninfo;
  713. klninfo->free = new_klist_set(klncgpu);
  714. if (usb_init(klncgpu, dev)) {
  715. int sent, recd, err;
  716. KLIST kitem;
  717. int attempts = 0;
  718. klncgpu->device_path = strdup(info->devid);
  719. control_init(klncgpu);
  720. while (attempts++ < 3) {
  721. kline.hd.cmd = KLN_CMD_IDENT;
  722. kline.hd.dev = 0;
  723. display_send_kline(klncgpu, &kline, msg_detect_send);
  724. err = usb_write(klncgpu, (char *)&(kline.hd), 2, &sent);
  725. if (err < 0 || sent != 2) {
  726. applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)",
  727. klncgpu->drv->dname,
  728. klncgpu->device_path,
  729. sent, err);
  730. }
  731. cgsleep_ms(REPLY_WAIT_TIME*10);
  732. err = usb_read(klncgpu, &kitem.kline, REPLY_SIZE, &recd);
  733. if (err < 0) {
  734. applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)",
  735. klncgpu->drv->dname,
  736. klncgpu->device_path,
  737. recd, err);
  738. } else if (recd < 1) {
  739. applog(LOG_ERR, "%s (%s) detect empty reply (%d)",
  740. klncgpu->drv->dname,
  741. klncgpu->device_path,
  742. recd);
  743. } else if (kitem.kline.hd.cmd == KLN_CMD_IDENT && kitem.kline.hd.dev == 0) {
  744. display_kline(klncgpu, &kitem.kline, msg_detect_reply);
  745. applog(LOG_DEBUG, "%s (%s) detect successful (%d attempt%s)",
  746. klncgpu->drv->dname,
  747. klncgpu->device_path,
  748. attempts, attempts == 1 ? "" : "s");
  749. if (!add_cgpu(klncgpu))
  750. break;
  751. applog(LOG_DEBUG, "Klondike cgpu added");
  752. cglock_init(&klninfo->klist_lock);
  753. return true;
  754. }
  755. }
  756. usb_uninit(klncgpu);
  757. }
  758. free(klninfo->free);
  759. free(klninfo);
  760. free(klncgpu);
  761. return false;
  762. }
  763. static
  764. bool klondike_detect_one(const char *serial)
  765. {
  766. return lowlevel_detect_serial(klondike_foundlowl, serial);
  767. }
  768. static
  769. int klondike_autodetect()
  770. {
  771. return lowlevel_detect(klondike_foundlowl, "K16");
  772. }
  773. static
  774. void klondike_detect()
  775. {
  776. generic_detect(&klondike_drv, klondike_detect_one, klondike_autodetect, 0);
  777. }
  778. static
  779. bool klondike_identify(__maybe_unused struct cgpu_info * const klncgpu)
  780. {
  781. /*
  782. KLINE kline;
  783. zero_kline(&kline);
  784. kline.hd.cmd = KLN_CMD_IDENT;
  785. kline.hd.dev = 0;
  786. SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  787. */
  788. return false;
  789. }
  790. static void klondike_check_nonce(struct cgpu_info *klncgpu, KLIST *kitem)
  791. {
  792. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  793. struct work *work, *look, *tmp;
  794. KLINE *kline = &(kitem->kline);
  795. struct timeval tv_now;
  796. double us_diff;
  797. uint32_t nonce = K_NONCE(kline->wr.nonce) - 0xC0;
  798. applog(LOG_DEBUG, "%s%i:%d FOUND NONCE (%02x:%08x)",
  799. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  800. kline->wr.workid, (unsigned int)nonce);
  801. work = NULL;
  802. cgtime(&tv_now);
  803. rd_lock(&(klncgpu->qlock));
  804. HASH_ITER(hh, klncgpu->queued_work, look, tmp) {
  805. if (ms_tdiff(&tv_now, &(look->tv_stamp)) < OLD_WORK_MS &&
  806. (look->subid == (kline->wr.dev*256 + kline->wr.workid))) {
  807. work = look;
  808. break;
  809. }
  810. }
  811. rd_unlock(&(klncgpu->qlock));
  812. if (work) {
  813. wr_lock(&(klninfo->stat_lock));
  814. klninfo->devinfo[kline->wr.dev].noncecount++;
  815. klninfo->noncecount++;
  816. wr_unlock(&(klninfo->stat_lock));
  817. applog(LOG_DEBUG, "%s%i:%d SUBMIT NONCE (%02x:%08x)",
  818. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  819. kline->wr.workid, (unsigned int)nonce);
  820. cgtime(&tv_now);
  821. bool ok = submit_nonce(klncgpu->thr[0], work, nonce);
  822. applog(LOG_DEBUG, "%s%i:%d chip stats %d, %08x, %d, %d",
  823. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  824. kline->wr.dev, (unsigned int)nonce,
  825. klninfo->devinfo[kline->wr.dev].rangesize,
  826. klninfo->status[kline->wr.dev].kline.ws.chipcount);
  827. klninfo->devinfo[kline->wr.dev].chipstats[(nonce / klninfo->devinfo[kline->wr.dev].rangesize) + (ok ? 0 : klninfo->status[kline->wr.dev].kline.ws.chipcount)]++;
  828. us_diff = us_tdiff(&tv_now, &(kitem->tv_when));
  829. if (klninfo->delay_count == 0) {
  830. klninfo->delay_min = us_diff;
  831. klninfo->delay_max = us_diff;
  832. } else {
  833. if (klninfo->delay_min > us_diff)
  834. klninfo->delay_min = us_diff;
  835. if (klninfo->delay_max < us_diff)
  836. klninfo->delay_max = us_diff;
  837. }
  838. klninfo->delay_count++;
  839. klninfo->delay_total += us_diff;
  840. if (klninfo->nonce_count > 0) {
  841. us_diff = us_tdiff(&(kitem->tv_when), &(klninfo->tv_last_nonce_received));
  842. if (klninfo->nonce_count == 1) {
  843. klninfo->nonce_min = us_diff;
  844. klninfo->nonce_max = us_diff;
  845. } else {
  846. if (klninfo->nonce_min > us_diff)
  847. klninfo->nonce_min = us_diff;
  848. if (klninfo->nonce_max < us_diff)
  849. klninfo->nonce_max = us_diff;
  850. }
  851. klninfo->nonce_total += us_diff;
  852. }
  853. klninfo->nonce_count++;
  854. memcpy(&(klninfo->tv_last_nonce_received), &(kitem->tv_when),
  855. sizeof(klninfo->tv_last_nonce_received));
  856. return;
  857. }
  858. applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored",
  859. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  860. kline->wr.workid, (unsigned int)nonce);
  861. //inc_hw_errors(klncgpu->thr[0]);
  862. }
  863. // thread to keep looking for replies
  864. static void *klondike_get_replies(void *userdata)
  865. {
  866. struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
  867. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  868. KLIST *kitem = NULL;
  869. int err, recd, slaves, dev;
  870. bool overheat;
  871. applog(LOG_DEBUG, "%s%i: listening for replies",
  872. klncgpu->drv->name, klncgpu->device_id);
  873. while (klninfo->shutdown == false) {
  874. if (klninfo->usbinfo_nodev)
  875. return NULL;
  876. if (kitem == NULL)
  877. kitem = allocate_kitem(klncgpu);
  878. else
  879. memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
  880. err = usb_read(klncgpu, &kitem->kline, REPLY_SIZE, &recd);
  881. if (err || recd != REPLY_SIZE) {
  882. if (err != -7)
  883. applog(LOG_ERR, "%s%i: reply err=%d amt=%d",
  884. klncgpu->drv->name, klncgpu->device_id,
  885. err, recd);
  886. }
  887. if (!err && recd == REPLY_SIZE) {
  888. cgtime(&(kitem->tv_when));
  889. kitem->block_seq = klninfo->block_seq;
  890. if (opt_log_level <= READ_DEBUG) {
  891. char hexdata[recd * 2];
  892. bin2hex(hexdata, &kitem->kline.hd.dev, recd-1);
  893. applog(READ_DEBUG, "%s%i:%d reply [%c:%s]",
  894. klncgpu->drv->name, klncgpu->device_id,
  895. (int)(kitem->kline.hd.dev),
  896. kitem->kline.hd.cmd, hexdata);
  897. }
  898. // We can't check this until it's initialised
  899. if (klninfo->initialised) {
  900. wr_lock(&(klninfo->stat_lock));
  901. slaves = klninfo->status[0].kline.ws.slavecount;
  902. klninfo->jobque[dev].late_update_sequential = 0;
  903. wr_unlock(&(klninfo->stat_lock));
  904. if (kitem->kline.hd.dev > slaves) {
  905. applog(LOG_ERR, "%s%i: reply [%c] has invalid dev=%d (max=%d) using 0",
  906. klncgpu->drv->name, klncgpu->device_id,
  907. (char)(kitem->kline.hd.cmd),
  908. (int)(kitem->kline.hd.dev),
  909. slaves);
  910. kitem->kline.hd.dev = 0;
  911. }
  912. }
  913. switch (kitem->kline.hd.cmd) {
  914. case KLN_CMD_NONCE:
  915. klondike_check_nonce(klncgpu, kitem);
  916. display_kline(klncgpu, &kitem->kline, msg_reply);
  917. break;
  918. case KLN_CMD_STATUS:
  919. case KLN_CMD_WORK:
  920. case KLN_CMD_ABORT:
  921. // We can't do/check this until it's initialised
  922. if (klninfo->initialised) {
  923. dev = kitem->kline.ws.dev;
  924. wr_lock(&(klninfo->stat_lock));
  925. klninfo->jobque[dev].workqc = (int)(kitem->kline.ws.workqc);
  926. cgtime(&(klninfo->jobque[dev].last_update));
  927. slaves = klninfo->status[0].kline.ws.slavecount;
  928. overheat = klninfo->jobque[dev].overheat;
  929. wr_unlock(&(klninfo->stat_lock));
  930. if (kitem->kline.ws.slavecount != slaves) {
  931. applog(LOG_ERR, "%s%i:%d reply [%c] has a diff # of slaves=%d"
  932. " (curr=%d) dropping device to hotplug",
  933. klncgpu->drv->name, klncgpu->device_id,
  934. dev, (char)(kitem->kline.ws.cmd),
  935. (int)(kitem->kline.ws.slavecount),
  936. slaves);
  937. klninfo->shutdown = true;
  938. break;
  939. }
  940. if (!overheat) {
  941. double temp = cvtKlnToC(kitem->kline.ws.temp);
  942. if (temp >= KLN_KILLWORK_TEMP) {
  943. KLINE kline;
  944. wr_lock(&(klninfo->stat_lock));
  945. klninfo->jobque[dev].overheat = true;
  946. wr_unlock(&(klninfo->stat_lock));
  947. applog(LOG_ERR, "%s%i:%d Critical overheat (%.0fC)",
  948. klncgpu->drv->name, klncgpu->device_id,
  949. dev, temp);
  950. zero_kline(&kline);
  951. kline.hd.cmd = KLN_CMD_ABORT;
  952. kline.hd.dev = dev;
  953. if (!SendCmd(klncgpu, &kline, KSENDHD(0))) {
  954. applog(LOG_ERR, "%s%i:%d failed to abort work"
  955. " - dropping device to hotplug",
  956. klncgpu->drv->name,
  957. klncgpu->device_id,
  958. dev);
  959. klninfo->shutdown = true;
  960. }
  961. kln_disable(klncgpu, dev, false);
  962. }
  963. }
  964. }
  965. case KLN_CMD_ENABLE:
  966. wr_lock(&(klninfo->stat_lock));
  967. klninfo->errorcount += kitem->kline.ws.errorcount;
  968. klninfo->noisecount += kitem->kline.ws.noise;
  969. wr_unlock(&(klninfo->stat_lock));
  970. display_kline(klncgpu, &kitem->kline, msg_reply);
  971. kitem->ready = true;
  972. kitem = NULL;
  973. break;
  974. case KLN_CMD_CONFIG:
  975. display_kline(klncgpu, &kitem->kline, msg_reply);
  976. kitem->ready = true;
  977. kitem = NULL;
  978. break;
  979. case KLN_CMD_IDENT:
  980. display_kline(klncgpu, &kitem->kline, msg_reply);
  981. kitem->ready = true;
  982. kitem = NULL;
  983. break;
  984. default:
  985. display_kline(klncgpu, &kitem->kline, msg_reply);
  986. break;
  987. }
  988. }
  989. }
  990. return NULL;
  991. }
  992. static void klondike_flush_work(struct cgpu_info *klncgpu)
  993. {
  994. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  995. KLIST *kitem;
  996. KLINE kline;
  997. int slaves, dev;
  998. klninfo->block_seq++;
  999. applog(LOG_DEBUG, "%s%i: flushing work",
  1000. klncgpu->drv->name, klncgpu->device_id);
  1001. rd_lock(&(klninfo->stat_lock));
  1002. slaves = klninfo->status[0].kline.ws.slavecount;
  1003. rd_unlock(&(klninfo->stat_lock));
  1004. zero_kline(&kline);
  1005. kline.hd.cmd = KLN_CMD_ABORT;
  1006. for (dev = 0; dev <= slaves; dev++) {
  1007. kline.hd.dev = dev;
  1008. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  1009. if (kitem != NULL) {
  1010. wr_lock(&(klninfo->stat_lock));
  1011. memcpy((void *)&(klninfo->status[dev]),
  1012. kitem,
  1013. sizeof(klninfo->status[dev]));
  1014. wr_unlock(&(klninfo->stat_lock));
  1015. kitem = release_kitem(klncgpu, kitem);
  1016. }
  1017. }
  1018. }
  1019. static bool klondike_thread_prepare(struct thr_info *thr)
  1020. {
  1021. struct cgpu_info *klncgpu = thr->cgpu;
  1022. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1023. if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) {
  1024. applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id);
  1025. return false;
  1026. }
  1027. pthread_detach(klninfo->replies_thr.pth);
  1028. // let the listening get started
  1029. cgsleep_ms(100);
  1030. return klondike_init(klncgpu);
  1031. }
  1032. static bool klondike_thread_init(struct thr_info *thr)
  1033. {
  1034. struct cgpu_info *klncgpu = thr->cgpu;
  1035. struct klondike_info * const klninfo = klncgpu->device_data;
  1036. notifier_init(thr->work_restart_notifier);
  1037. if (klninfo->usbinfo_nodev)
  1038. return false;
  1039. klondike_flush_work(klncgpu);
  1040. return true;
  1041. }
  1042. static void klondike_shutdown(struct thr_info *thr)
  1043. {
  1044. struct cgpu_info *klncgpu = thr->cgpu;
  1045. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1046. applog(LOG_DEBUG, "%s%i: shutting down work",
  1047. klncgpu->drv->name, klncgpu->device_id);
  1048. kln_disable(klncgpu, klninfo->status[0].kline.ws.slavecount, true);
  1049. klncgpu->shutdown = klninfo->shutdown = true;
  1050. }
  1051. static void klondike_thread_enable(struct thr_info *thr)
  1052. {
  1053. struct cgpu_info *klncgpu = thr->cgpu;
  1054. struct klondike_info * const klninfo = klncgpu->device_data;
  1055. if (klninfo->usbinfo_nodev)
  1056. return;
  1057. /*
  1058. KLINE kline;
  1059. zero_kline(&kline);
  1060. kline.hd.cmd = KLN_CMD_ENABLE;
  1061. kline.hd.dev = dev;
  1062. kline.hd.buf[0] = KLN_CMD_ENABLE_OFF;
  1063. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
  1064. */
  1065. }
  1066. static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
  1067. {
  1068. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1069. struct work *look, *tmp;
  1070. KLINE kline;
  1071. struct timeval tv_old;
  1072. int wque_size, wque_cleared;
  1073. if (klninfo->usbinfo_nodev)
  1074. return false;
  1075. zero_kline(&kline);
  1076. kline.wt.cmd = KLN_CMD_WORK;
  1077. kline.wt.dev = dev;
  1078. memcpy(kline.wt.midstate, work->midstate, MIDSTATE_BYTES);
  1079. memcpy(kline.wt.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  1080. kline.wt.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
  1081. work->subid = dev*256 + kline.wt.workid;
  1082. cgtime(&work->tv_stamp);
  1083. if (opt_log_level <= LOG_DEBUG) {
  1084. char hexdata[(sizeof(kline.wt) * 2) + 1];
  1085. bin2hex(hexdata, &kline.wt, sizeof(kline.wt));
  1086. applog(LOG_DEBUG, "WORKDATA: %s", hexdata);
  1087. }
  1088. applog(LOG_DEBUG, "%s%i:%d sending work (%d:%02x)",
  1089. klncgpu->drv->name, klncgpu->device_id, dev,
  1090. dev, kline.wt.workid);
  1091. KLIST *kitem = SendCmdGetReply(klncgpu, &kline, sizeof(kline.wt));
  1092. if (kitem != NULL) {
  1093. wr_lock(&(klninfo->stat_lock));
  1094. memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(klninfo->status[dev]));
  1095. wr_unlock(&(klninfo->stat_lock));
  1096. kitem = release_kitem(klncgpu, kitem);
  1097. // remove old work
  1098. wque_size = 0;
  1099. wque_cleared = 0;
  1100. cgtime(&tv_old);
  1101. wr_lock(&klncgpu->qlock);
  1102. HASH_ITER(hh, klncgpu->queued_work, look, tmp) {
  1103. if (ms_tdiff(&tv_old, &(look->tv_stamp)) > OLD_WORK_MS) {
  1104. __work_completed(klncgpu, look);
  1105. free_work(look);
  1106. wque_cleared++;
  1107. } else
  1108. wque_size++;
  1109. }
  1110. wr_unlock(&klncgpu->qlock);
  1111. wr_lock(&(klninfo->stat_lock));
  1112. klninfo->wque_size = wque_size;
  1113. klninfo->wque_cleared = wque_cleared;
  1114. wr_unlock(&(klninfo->stat_lock));
  1115. return true;
  1116. }
  1117. return false;
  1118. }
  1119. static bool klondike_queue_full(struct cgpu_info *klncgpu)
  1120. {
  1121. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1122. struct work *work = NULL;
  1123. int dev, queued, slaves, seq;
  1124. struct timeval now;
  1125. bool nowork;
  1126. cgtime(&now);
  1127. rd_lock(&(klninfo->stat_lock));
  1128. slaves = klninfo->status[0].kline.ws.slavecount;
  1129. for (dev = 0; dev <= slaves; dev++)
  1130. if (ms_tdiff(&now, &(klninfo->jobque[dev].last_update)) > LATE_UPDATE_MS) {
  1131. klninfo->jobque[dev].late_update_count++;
  1132. seq = ++klninfo->jobque[dev].late_update_sequential;
  1133. rd_unlock(&(klninfo->stat_lock));
  1134. if (seq < KLN_LATE_UPDATE_LIMIT) {
  1135. applog(LOG_ERR, "%s%i:%d late update",
  1136. klncgpu->drv->name, klncgpu->device_id, dev);
  1137. klondike_get_stats(klncgpu);
  1138. goto que;
  1139. } else {
  1140. applog(LOG_ERR, "%s%i:%d late update (%d) reached - attempting reset",
  1141. klncgpu->drv->name, klncgpu->device_id,
  1142. dev, KLN_LATE_UPDATE_LIMIT);
  1143. control_init(klncgpu);
  1144. kln_enable(klncgpu);
  1145. klondike_get_stats(klncgpu);
  1146. rd_lock(&(klninfo->stat_lock));
  1147. if (ms_tdiff(&now, &(klninfo->jobque[dev].last_update)) > LATE_UPDATE_MS) {
  1148. rd_unlock(&(klninfo->stat_lock));
  1149. applog(LOG_ERR, "%s%i:%d reset failed - dropping device",
  1150. klncgpu->drv->name, klncgpu->device_id, dev);
  1151. klninfo->shutdown = true;
  1152. return false;
  1153. }
  1154. break;
  1155. }
  1156. }
  1157. rd_unlock(&(klninfo->stat_lock));
  1158. que:
  1159. nowork = true;
  1160. for (queued = 0; queued < MAX_WORK_COUNT-1; queued++)
  1161. for (dev = 0; dev <= slaves; dev++) {
  1162. tryagain:
  1163. rd_lock(&(klninfo->stat_lock));
  1164. if (klninfo->jobque[dev].overheat) {
  1165. double temp = cvtKlnToC(klninfo->status[0].kline.ws.temp);
  1166. if ((queued == MAX_WORK_COUNT-2) &&
  1167. ms_tdiff(&now, &(klninfo->jobque[dev].last_update)) > (LATE_UPDATE_MS/2)) {
  1168. rd_unlock(&(klninfo->stat_lock));
  1169. klondike_get_stats(klncgpu);
  1170. goto tryagain;
  1171. }
  1172. if (temp <= KLN_COOLED_DOWN) {
  1173. klninfo->jobque[dev].overheat = false;
  1174. rd_unlock(&(klninfo->stat_lock));
  1175. applog(LOG_ERR, "%s%i:%d Overheat recovered (%.0fC)",
  1176. klncgpu->drv->name, klncgpu->device_id,
  1177. dev, temp);
  1178. kln_enable(klncgpu);
  1179. goto tryagain;
  1180. } else {
  1181. rd_unlock(&(klninfo->stat_lock));
  1182. continue;
  1183. }
  1184. }
  1185. if (klninfo->jobque[dev].workqc <= queued) {
  1186. rd_unlock(&(klninfo->stat_lock));
  1187. if (!work)
  1188. work = get_queued(klncgpu);
  1189. if (unlikely(!work))
  1190. return false;
  1191. nowork = false;
  1192. if (klondike_send_work(klncgpu, dev, work))
  1193. return false;
  1194. } else
  1195. rd_unlock(&(klninfo->stat_lock));
  1196. }
  1197. if (nowork)
  1198. cgsleep_ms(10); // avoid a hard loop in case we have nothing to do
  1199. return true;
  1200. }
  1201. static int64_t klondike_scanwork(struct thr_info *thr)
  1202. {
  1203. struct cgpu_info *klncgpu = thr->cgpu;
  1204. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1205. int64_t newhashcount = 0;
  1206. int dev, slaves;
  1207. if (klninfo->usbinfo_nodev)
  1208. return -1;
  1209. restart_wait(thr, 200);
  1210. if (klninfo->status != NULL) {
  1211. rd_lock(&(klninfo->stat_lock));
  1212. slaves = klninfo->status[0].kline.ws.slavecount;
  1213. for (dev = 0; dev <= slaves; dev++) {
  1214. uint64_t newhashdev = 0, hashcount;
  1215. int maxcount;
  1216. hashcount = K_HASHCOUNT(klninfo->status[dev].kline.ws.hashcount);
  1217. maxcount = K_MAXCOUNT(klninfo->status[dev].kline.ws.maxcount);
  1218. // todo: chg this to check workid for wrapped instead
  1219. if (klninfo->devinfo[dev].lasthashcount > hashcount)
  1220. newhashdev += maxcount; // hash counter wrapped
  1221. newhashdev += hashcount - klninfo->devinfo[dev].lasthashcount;
  1222. klninfo->devinfo[dev].lasthashcount = hashcount;
  1223. if (maxcount != 0)
  1224. klninfo->hashcount += (newhashdev << 32) / maxcount;
  1225. }
  1226. newhashcount += 0xffffffffull * (uint64_t)klninfo->noncecount;
  1227. klninfo->noncecount = 0;
  1228. rd_unlock(&(klninfo->stat_lock));
  1229. }
  1230. return newhashcount;
  1231. }
  1232. static void get_klondike_statline_before(char *buf, size_t siz, struct cgpu_info *klncgpu)
  1233. {
  1234. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1235. uint8_t temp = 0xFF;
  1236. uint16_t fan = 0;
  1237. uint16_t clock = 0;
  1238. int dev, slaves;
  1239. char tmp[16];
  1240. if (klninfo->status == NULL) {
  1241. return;
  1242. }
  1243. rd_lock(&(klninfo->stat_lock));
  1244. slaves = klninfo->status[0].kline.ws.slavecount;
  1245. for (dev = 0; dev <= slaves; dev++) {
  1246. if (klninfo->status[dev].kline.ws.temp < temp)
  1247. temp = klninfo->status[dev].kline.ws.temp;
  1248. fan += klninfo->cfg[dev].kline.cfg.fantarget;
  1249. clock += (uint16_t)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  1250. }
  1251. rd_unlock(&(klninfo->stat_lock));
  1252. fan /= slaves + 1;
  1253. fan *= 100/255;
  1254. if (fan > 99) // short on screen space
  1255. fan = 99;
  1256. clock /= slaves + 1;
  1257. if (clock > 999) // error - so truncate it
  1258. clock = 999;
  1259. snprintf(tmp, sizeof(tmp), "%2.0fC", cvtKlnToC(temp));
  1260. if (strlen(tmp) < 4)
  1261. strcat(tmp, " ");
  1262. tailsprintf(buf, siz, "%3dMHz %2d%% %s| ", (int)clock, fan, tmp);
  1263. }
  1264. static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
  1265. {
  1266. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1267. struct api_data *root = NULL;
  1268. char buf[32];
  1269. int dev, slaves;
  1270. if (klninfo->status == NULL)
  1271. return NULL;
  1272. rd_lock(&(klninfo->stat_lock));
  1273. slaves = klninfo->status[0].kline.ws.slavecount;
  1274. for (dev = 0; dev <= slaves; dev++) {
  1275. float fTemp = cvtKlnToC(klninfo->status[dev].kline.ws.temp);
  1276. sprintf(buf, "Temp %d", dev);
  1277. root = api_add_temp(root, buf, &fTemp, true);
  1278. double dClk = (double)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  1279. sprintf(buf, "Clock %d", dev);
  1280. root = api_add_freq(root, buf, &dClk, true);
  1281. unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].kline.cfg.fantarget / 255;
  1282. sprintf(buf, "Fan Percent %d", dev);
  1283. root = api_add_int(root, buf, (int *)(&iFan), true);
  1284. iFan = 0;
  1285. if (klninfo->status[dev].kline.ws.fanspeed > 0)
  1286. iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].kline.ws.fanspeed;
  1287. sprintf(buf, "Fan RPM %d", dev);
  1288. root = api_add_int(root, buf, (int *)(&iFan), true);
  1289. if (klninfo->devinfo[dev].chipstats != NULL) {
  1290. char data[2048];
  1291. char one[32];
  1292. int n;
  1293. sprintf(buf, "Nonces / Chip %d", dev);
  1294. data[0] = '\0';
  1295. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  1296. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n]);
  1297. strcat(data, one);
  1298. }
  1299. root = api_add_string(root, buf, data, true);
  1300. sprintf(buf, "Errors / Chip %d", dev);
  1301. data[0] = '\0';
  1302. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  1303. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].kline.ws.chipcount]);
  1304. strcat(data, one);
  1305. }
  1306. root = api_add_string(root, buf, data, true);
  1307. }
  1308. }
  1309. root = api_add_uint64(root, "Hash Count", &(klninfo->hashcount), true);
  1310. root = api_add_uint64(root, "Error Count", &(klninfo->errorcount), true);
  1311. root = api_add_uint64(root, "Noise Count", &(klninfo->noisecount), true);
  1312. root = api_add_int(root, "KLine Limit", &(klninfo->kline_count), true);
  1313. root = api_add_int(root, "KLine Used", &(klninfo->used_count), true);
  1314. root = api_add_elapsed(root, "KQue Delay Count", &(klninfo->delay_count), true);
  1315. root = api_add_elapsed(root, "KQue Delay Total", &(klninfo->delay_total), true);
  1316. root = api_add_elapsed(root, "KQue Delay Min", &(klninfo->delay_min), true);
  1317. root = api_add_elapsed(root, "KQue Delay Max", &(klninfo->delay_max), true);
  1318. double avg;
  1319. if (klninfo->delay_count == 0)
  1320. avg = 0;
  1321. else
  1322. avg = klninfo->delay_total / klninfo->delay_count;
  1323. root = api_add_diff(root, "KQue Delay Avg", &avg, true);
  1324. root = api_add_elapsed(root, "KQue Nonce Count", &(klninfo->nonce_count), true);
  1325. root = api_add_elapsed(root, "KQue Nonce Total", &(klninfo->nonce_total), true);
  1326. root = api_add_elapsed(root, "KQue Nonce Min", &(klninfo->nonce_min), true);
  1327. root = api_add_elapsed(root, "KQue Nonce Max", &(klninfo->nonce_max), true);
  1328. if (klninfo->nonce_count == 0)
  1329. avg = 0;
  1330. else
  1331. avg = klninfo->nonce_total / klninfo->nonce_count;
  1332. root = api_add_diff(root, "KQue Nonce Avg", &avg, true);
  1333. root = api_add_int(root, "WQue Size", &(klninfo->wque_size), true);
  1334. root = api_add_int(root, "WQue Cleared", &(klninfo->wque_cleared), true);
  1335. rd_unlock(&(klninfo->stat_lock));
  1336. return root;
  1337. }
  1338. struct device_drv klondike_drv = {
  1339. .dname = "Klondike",
  1340. .name = "KLN",
  1341. .drv_detect = klondike_detect,
  1342. .get_api_stats = klondike_api_stats,
  1343. // .get_statline_before = get_klondike_statline_before,
  1344. .get_stats = klondike_get_stats,
  1345. .identify_device = klondike_identify,
  1346. .thread_prepare = klondike_thread_prepare,
  1347. .thread_init = klondike_thread_init,
  1348. .minerloop = hash_queued_work,
  1349. .scanwork = klondike_scanwork,
  1350. .queue_full = klondike_queue_full,
  1351. .flush_work = klondike_flush_work,
  1352. .thread_shutdown = klondike_shutdown,
  1353. .thread_enable = klondike_thread_enable
  1354. };