driver-klondike.c 41 KB

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