driver-klondike.c 42 KB

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