driver-klondike.c 45 KB

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