driver-klondike.c 41 KB

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