driver-klondike.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498
  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;
  584. double temp1, temp2;
  585. sscanf(opt_klondike_options, "%d:%lf:%lf:%"SCNu8,
  586. &hashclock,
  587. &temp1, &temp2,
  588. &kline.cfg.fantarget);
  589. SET_HASHCLOCK(kline.cfg.hashclock, hashclock);
  590. kline.cfg.temptarget = cvtCToKln(temp1);
  591. kline.cfg.tempcritical = cvtCToKln(temp2);
  592. kline.cfg.fantarget = (int)255*kline.cfg.fantarget/100;
  593. size = sizeof(kline.cfg) - 2;
  594. }
  595. for (dev = 0; dev <= slaves; dev++) {
  596. kline.cfg.dev = dev;
  597. kitem = SendCmdGetReply(klncgpu, &kline, size);
  598. if (kitem != NULL) {
  599. memcpy((void *)&(klninfo->cfg[dev]), kitem, sizeof(klninfo->cfg[dev]));
  600. applog(LOG_WARNING, "%s%i:%d config (%d: Clk: %d, T:%.0lf, C:%.0lf, F:%d)",
  601. klncgpu->drv->name, klncgpu->device_id, dev,
  602. dev, K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock),
  603. cvtKlnToC(klninfo->cfg[dev].kline.cfg.temptarget),
  604. cvtKlnToC(klninfo->cfg[dev].kline.cfg.tempcritical),
  605. (int)100*klninfo->cfg[dev].kline.cfg.fantarget/256);
  606. kitem = release_kitem(klncgpu, kitem);
  607. }
  608. }
  609. klondike_get_stats(klncgpu);
  610. klninfo->initialised = true;
  611. for (dev = 0; dev <= slaves; dev++) {
  612. klninfo->devinfo[dev].rangesize = ((uint64_t)1<<32) / klninfo->status[dev].kline.ws.chipcount;
  613. klninfo->devinfo[dev].chipstats = calloc(klninfo->status[dev].kline.ws.chipcount*2 , sizeof(uint32_t));
  614. }
  615. bool ok = kln_enable(klncgpu);
  616. if (!ok)
  617. applog(LOG_ERR, "%s%i: failed to enable", klncgpu->drv->name, klncgpu->device_id);
  618. return ok;
  619. }
  620. static void control_init(struct cgpu_info *klncgpu)
  621. {
  622. int err, interface;
  623. if (klncgpu->usbinfo.nodev)
  624. return;
  625. interface = usb_interface(klncgpu);
  626. err = usb_transfer(klncgpu, 0, 9, 1, interface, C_RESET);
  627. applog(LOG_DEBUG, "%s%i: reset got err %d",
  628. klncgpu->drv->name, klncgpu->device_id, err);
  629. }
  630. static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  631. {
  632. struct cgpu_info *klncgpu = usb_alloc_cgpu(&klondike_drv, 1);
  633. struct klondike_info *klninfo = NULL;
  634. KLINE kline;
  635. if (unlikely(!klncgpu))
  636. quit(1, "Failed to calloc klncgpu in klondike_detect_one");
  637. klninfo = calloc(1, sizeof(*klninfo));
  638. if (unlikely(!klninfo))
  639. quit(1, "Failed to calloc klninfo in klondke_detect_one");
  640. klncgpu->device_data = (void *)klninfo;
  641. klninfo->free = new_klist_set(klncgpu);
  642. if (usb_init(klncgpu, dev, found)) {
  643. int sent, recd, err;
  644. KLIST kitem;
  645. int attempts = 0;
  646. control_init(klncgpu);
  647. while (attempts++ < 3) {
  648. kline.hd.cmd = KLN_CMD_IDENT;
  649. kline.hd.dev = 0;
  650. display_send_kline(klncgpu, &kline, msg_detect_send);
  651. err = usb_write(klncgpu, (char *)&(kline.hd), 2, &sent, C_REQUESTRESULTS);
  652. if (err < 0 || sent != 2) {
  653. applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)",
  654. klncgpu->drv->dname,
  655. klncgpu->device_path,
  656. sent, err);
  657. }
  658. cgsleep_ms(REPLY_WAIT_TIME*10);
  659. err = usb_read(klncgpu, (char *)&(kitem.kline), REPLY_SIZE, &recd, C_GETRESULTS);
  660. if (err < 0) {
  661. applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)",
  662. klncgpu->drv->dname,
  663. klncgpu->device_path,
  664. recd, err);
  665. } else if (recd < 1) {
  666. applog(LOG_ERR, "%s (%s) detect empty reply (%d)",
  667. klncgpu->drv->dname,
  668. klncgpu->device_path,
  669. recd);
  670. } else if (kitem.kline.hd.cmd == KLN_CMD_IDENT && kitem.kline.hd.dev == 0) {
  671. display_kline(klncgpu, &kitem.kline, msg_detect_reply);
  672. applog(LOG_DEBUG, "%s (%s) detect successful (%d attempt%s)",
  673. klncgpu->drv->dname,
  674. klncgpu->device_path,
  675. attempts, attempts == 1 ? "" : "s");
  676. if (!add_cgpu(klncgpu))
  677. break;
  678. update_usb_stats(klncgpu);
  679. applog(LOG_DEBUG, "Klondike cgpu added");
  680. cglock_init(&klninfo->klist_lock);
  681. return true;
  682. }
  683. }
  684. usb_uninit(klncgpu);
  685. }
  686. free(klninfo->free);
  687. free(klninfo);
  688. free(klncgpu);
  689. return false;
  690. }
  691. static void klondike_detect(bool __maybe_unused hotplug)
  692. {
  693. usb_detect(&klondike_drv, klondike_detect_one);
  694. }
  695. static void klondike_identify(__maybe_unused struct cgpu_info *klncgpu)
  696. {
  697. /*
  698. KLINE kline;
  699. zero_kline(&kline);
  700. kline.hd.cmd = KLN_CMD_IDENT;
  701. kline.hd.dev = 0;
  702. SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  703. */
  704. }
  705. static void klondike_check_nonce(struct cgpu_info *klncgpu, KLIST *kitem)
  706. {
  707. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  708. struct work *work, *look, *tmp;
  709. KLINE *kline = &(kitem->kline);
  710. struct timeval tv_now;
  711. double us_diff;
  712. uint32_t nonce = K_NONCE(kline->wr.nonce) - 0xC0;
  713. applog(LOG_DEBUG, "%s%i:%d FOUND NONCE (%02x:%08x)",
  714. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  715. kline->wr.workid, (unsigned int)nonce);
  716. work = NULL;
  717. cgtime(&tv_now);
  718. rd_lock(&(klncgpu->qlock));
  719. HASH_ITER(hh, klncgpu->queued_work, look, tmp) {
  720. if (ms_tdiff(&tv_now, &(look->tv_stamp)) < OLD_WORK_MS &&
  721. (look->subid == (kline->wr.dev*256 + kline->wr.workid))) {
  722. work = look;
  723. break;
  724. }
  725. }
  726. rd_unlock(&(klncgpu->qlock));
  727. if (work) {
  728. wr_lock(&(klninfo->stat_lock));
  729. klninfo->devinfo[kline->wr.dev].noncecount++;
  730. klninfo->noncecount++;
  731. wr_unlock(&(klninfo->stat_lock));
  732. applog(LOG_DEBUG, "%s%i:%d SUBMIT NONCE (%02x:%08x)",
  733. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  734. kline->wr.workid, (unsigned int)nonce);
  735. cgtime(&tv_now);
  736. bool ok = submit_nonce(klncgpu->thr[0], work, nonce);
  737. applog(LOG_DEBUG, "%s%i:%d chip stats %d, %08x, %d, %d",
  738. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  739. kline->wr.dev, (unsigned int)nonce,
  740. klninfo->devinfo[kline->wr.dev].rangesize,
  741. klninfo->status[kline->wr.dev].kline.ws.chipcount);
  742. klninfo->devinfo[kline->wr.dev].chipstats[(nonce / klninfo->devinfo[kline->wr.dev].rangesize) + (ok ? 0 : klninfo->status[kline->wr.dev].kline.ws.chipcount)]++;
  743. us_diff = us_tdiff(&tv_now, &(kitem->tv_when));
  744. if (klninfo->delay_count == 0) {
  745. klninfo->delay_min = us_diff;
  746. klninfo->delay_max = us_diff;
  747. } else {
  748. if (klninfo->delay_min > us_diff)
  749. klninfo->delay_min = us_diff;
  750. if (klninfo->delay_max < us_diff)
  751. klninfo->delay_max = us_diff;
  752. }
  753. klninfo->delay_count++;
  754. klninfo->delay_total += us_diff;
  755. if (klninfo->nonce_count > 0) {
  756. us_diff = us_tdiff(&(kitem->tv_when), &(klninfo->tv_last_nonce_received));
  757. if (klninfo->nonce_count == 1) {
  758. klninfo->nonce_min = us_diff;
  759. klninfo->nonce_max = us_diff;
  760. } else {
  761. if (klninfo->nonce_min > us_diff)
  762. klninfo->nonce_min = us_diff;
  763. if (klninfo->nonce_max < us_diff)
  764. klninfo->nonce_max = us_diff;
  765. }
  766. klninfo->nonce_total += us_diff;
  767. }
  768. klninfo->nonce_count++;
  769. memcpy(&(klninfo->tv_last_nonce_received), &(kitem->tv_when),
  770. sizeof(klninfo->tv_last_nonce_received));
  771. return;
  772. }
  773. applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored",
  774. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  775. kline->wr.workid, (unsigned int)nonce);
  776. //inc_hw_errors(klncgpu->thr[0]);
  777. }
  778. // thread to keep looking for replies
  779. static void *klondike_get_replies(void *userdata)
  780. {
  781. struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
  782. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  783. KLIST *kitem = NULL;
  784. char *hexdata;
  785. int err, recd, slaves, dev;
  786. bool overheat;
  787. applog(LOG_DEBUG, "%s%i: listening for replies",
  788. klncgpu->drv->name, klncgpu->device_id);
  789. while (klninfo->shutdown == false) {
  790. if (klncgpu->usbinfo.nodev)
  791. return NULL;
  792. if (kitem == NULL)
  793. kitem = allocate_kitem(klncgpu);
  794. else
  795. memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
  796. err = usb_read(klncgpu, (char *)&(kitem->kline), REPLY_SIZE, &recd, C_GETRESULTS);
  797. if (err || recd != REPLY_SIZE) {
  798. if (err != -7)
  799. applog(LOG_ERR, "%s%i: reply err=%d amt=%d",
  800. klncgpu->drv->name, klncgpu->device_id,
  801. err, recd);
  802. }
  803. if (!err && recd == REPLY_SIZE) {
  804. cgtime(&(kitem->tv_when));
  805. kitem->block_seq = klninfo->block_seq;
  806. if (opt_log_level <= READ_DEBUG) {
  807. hexdata = bin2hex((unsigned char *)&(kitem->kline.hd.dev), recd-1);
  808. applog(READ_DEBUG, "%s%i:%d reply [%c:%s]",
  809. klncgpu->drv->name, klncgpu->device_id,
  810. (int)(kitem->kline.hd.dev),
  811. kitem->kline.hd.cmd, hexdata);
  812. free(hexdata);
  813. }
  814. // We can't check this until it's initialised
  815. if (klninfo->initialised) {
  816. rd_lock(&(klninfo->stat_lock));
  817. slaves = klninfo->status[0].kline.ws.slavecount;
  818. rd_unlock(&(klninfo->stat_lock));
  819. if (kitem->kline.hd.dev > slaves) {
  820. applog(LOG_ERR, "%s%i: reply [%c] has invalid dev=%d (max=%d) using 0",
  821. klncgpu->drv->name, klncgpu->device_id,
  822. (char)(kitem->kline.hd.cmd),
  823. (int)(kitem->kline.hd.dev),
  824. slaves);
  825. /* TODO: this is rather problematic if there are slaves
  826. * however without slaves - it should always be zero */
  827. kitem->kline.hd.dev = 0;
  828. } else {
  829. wr_lock(&(klninfo->stat_lock));
  830. klninfo->jobque[kitem->kline.hd.dev].late_update_sequential = 0;
  831. wr_unlock(&(klninfo->stat_lock));
  832. }
  833. }
  834. switch (kitem->kline.hd.cmd) {
  835. case KLN_CMD_NONCE:
  836. klondike_check_nonce(klncgpu, kitem);
  837. display_kline(klncgpu, &kitem->kline, msg_reply);
  838. break;
  839. case KLN_CMD_STATUS:
  840. case KLN_CMD_WORK:
  841. case KLN_CMD_ABORT:
  842. // We can't do/check this until it's initialised
  843. if (klninfo->initialised) {
  844. dev = kitem->kline.ws.dev;
  845. wr_lock(&(klninfo->stat_lock));
  846. klninfo->jobque[dev].workqc = (int)(kitem->kline.ws.workqc);
  847. cgtime(&(klninfo->jobque[dev].last_update));
  848. slaves = klninfo->status[0].kline.ws.slavecount;
  849. overheat = klninfo->jobque[dev].overheat;
  850. wr_unlock(&(klninfo->stat_lock));
  851. if (kitem->kline.ws.slavecount != slaves) {
  852. applog(LOG_ERR, "%s%i:%d reply [%c] has a diff # of slaves=%d"
  853. " (curr=%d) dropping device to hotplug",
  854. klncgpu->drv->name, klncgpu->device_id,
  855. dev, (char)(kitem->kline.ws.cmd),
  856. (int)(kitem->kline.ws.slavecount),
  857. slaves);
  858. klninfo->shutdown = true;
  859. break;
  860. }
  861. if (!overheat) {
  862. double temp = cvtKlnToC(kitem->kline.ws.temp);
  863. if (temp >= KLN_KILLWORK_TEMP) {
  864. KLINE kline;
  865. wr_lock(&(klninfo->stat_lock));
  866. klninfo->jobque[dev].overheat = true;
  867. wr_unlock(&(klninfo->stat_lock));
  868. applog(LOG_ERR, "%s%i:%d Critical overheat (%.0fC)",
  869. klncgpu->drv->name, klncgpu->device_id,
  870. dev, temp);
  871. zero_kline(&kline);
  872. kline.hd.cmd = KLN_CMD_ABORT;
  873. kline.hd.dev = dev;
  874. if (!SendCmd(klncgpu, &kline, KSENDHD(0))) {
  875. applog(LOG_ERR, "%s%i:%d failed to abort work"
  876. " - dropping device to hotplug",
  877. klncgpu->drv->name,
  878. klncgpu->device_id,
  879. dev);
  880. klninfo->shutdown = true;
  881. }
  882. kln_disable(klncgpu, dev, false);
  883. }
  884. }
  885. }
  886. case KLN_CMD_ENABLE:
  887. wr_lock(&(klninfo->stat_lock));
  888. klninfo->errorcount += kitem->kline.ws.errorcount;
  889. klninfo->noisecount += kitem->kline.ws.noise;
  890. wr_unlock(&(klninfo->stat_lock));
  891. display_kline(klncgpu, &kitem->kline, msg_reply);
  892. kitem->ready = true;
  893. kitem = NULL;
  894. break;
  895. case KLN_CMD_CONFIG:
  896. display_kline(klncgpu, &kitem->kline, msg_reply);
  897. kitem->ready = true;
  898. kitem = NULL;
  899. break;
  900. case KLN_CMD_IDENT:
  901. display_kline(klncgpu, &kitem->kline, msg_reply);
  902. kitem->ready = true;
  903. kitem = NULL;
  904. break;
  905. default:
  906. display_kline(klncgpu, &kitem->kline, msg_reply);
  907. break;
  908. }
  909. }
  910. }
  911. return NULL;
  912. }
  913. static void klondike_flush_work(struct cgpu_info *klncgpu)
  914. {
  915. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  916. KLIST *kitem;
  917. KLINE kline;
  918. int slaves, dev;
  919. klninfo->block_seq++;
  920. applog(LOG_DEBUG, "%s%i: flushing work",
  921. klncgpu->drv->name, klncgpu->device_id);
  922. rd_lock(&(klninfo->stat_lock));
  923. slaves = klninfo->status[0].kline.ws.slavecount;
  924. rd_unlock(&(klninfo->stat_lock));
  925. zero_kline(&kline);
  926. kline.hd.cmd = KLN_CMD_ABORT;
  927. for (dev = 0; dev <= slaves; dev++) {
  928. kline.hd.dev = dev;
  929. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  930. if (kitem != NULL) {
  931. wr_lock(&(klninfo->stat_lock));
  932. memcpy((void *)&(klninfo->status[dev]),
  933. kitem,
  934. sizeof(klninfo->status[dev]));
  935. wr_unlock(&(klninfo->stat_lock));
  936. kitem = release_kitem(klncgpu, kitem);
  937. }
  938. }
  939. }
  940. static bool klondike_thread_prepare(struct thr_info *thr)
  941. {
  942. struct cgpu_info *klncgpu = thr->cgpu;
  943. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  944. if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) {
  945. applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id);
  946. return false;
  947. }
  948. pthread_detach(klninfo->replies_thr.pth);
  949. // let the listening get started
  950. cgsleep_ms(100);
  951. return klondike_init(klncgpu);
  952. }
  953. static bool klondike_thread_init(struct thr_info *thr)
  954. {
  955. struct cgpu_info *klncgpu = thr->cgpu;
  956. if (klncgpu->usbinfo.nodev)
  957. return false;
  958. klondike_flush_work(klncgpu);
  959. return true;
  960. }
  961. static void klondike_shutdown(struct thr_info *thr)
  962. {
  963. struct cgpu_info *klncgpu = thr->cgpu;
  964. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  965. applog(LOG_DEBUG, "%s%i: shutting down work",
  966. klncgpu->drv->name, klncgpu->device_id);
  967. kln_disable(klncgpu, klninfo->status[0].kline.ws.slavecount, true);
  968. klncgpu->shutdown = klninfo->shutdown = true;
  969. }
  970. static void klondike_thread_enable(struct thr_info *thr)
  971. {
  972. struct cgpu_info *klncgpu = thr->cgpu;
  973. if (klncgpu->usbinfo.nodev)
  974. return;
  975. /*
  976. KLINE kline;
  977. zero_kline(&kline);
  978. kline.hd.cmd = KLN_CMD_ENABLE;
  979. kline.hd.dev = dev;
  980. kline.hd.buf[0] = KLN_CMD_ENABLE_OFF;
  981. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
  982. */
  983. }
  984. static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
  985. {
  986. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  987. struct work *look, *tmp;
  988. KLINE kline;
  989. struct timeval tv_old;
  990. int wque_size, wque_cleared;
  991. if (klncgpu->usbinfo.nodev)
  992. return false;
  993. zero_kline(&kline);
  994. kline.wt.cmd = KLN_CMD_WORK;
  995. kline.wt.dev = dev;
  996. memcpy(kline.wt.midstate, work->midstate, MIDSTATE_BYTES);
  997. memcpy(kline.wt.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  998. kline.wt.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
  999. work->subid = dev*256 + kline.wt.workid;
  1000. cgtime(&work->tv_stamp);
  1001. if (opt_log_level <= LOG_DEBUG) {
  1002. char *hexdata = bin2hex((void *)&kline.wt, sizeof(kline.wt));
  1003. applog(LOG_DEBUG, "WORKDATA: %s", hexdata);
  1004. free(hexdata);
  1005. }
  1006. applog(LOG_DEBUG, "%s%i:%d sending work (%d:%02x)",
  1007. klncgpu->drv->name, klncgpu->device_id, dev,
  1008. dev, kline.wt.workid);
  1009. KLIST *kitem = SendCmdGetReply(klncgpu, &kline, sizeof(kline.wt));
  1010. if (kitem != NULL) {
  1011. wr_lock(&(klninfo->stat_lock));
  1012. memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(klninfo->status[dev]));
  1013. wr_unlock(&(klninfo->stat_lock));
  1014. kitem = release_kitem(klncgpu, kitem);
  1015. // remove old work
  1016. wque_size = 0;
  1017. wque_cleared = 0;
  1018. cgtime(&tv_old);
  1019. wr_lock(&klncgpu->qlock);
  1020. HASH_ITER(hh, klncgpu->queued_work, look, tmp) {
  1021. if (ms_tdiff(&tv_old, &(look->tv_stamp)) > OLD_WORK_MS) {
  1022. __work_completed(klncgpu, look);
  1023. free_work(look);
  1024. wque_cleared++;
  1025. } else
  1026. wque_size++;
  1027. }
  1028. wr_unlock(&klncgpu->qlock);
  1029. wr_lock(&(klninfo->stat_lock));
  1030. klninfo->wque_size = wque_size;
  1031. klninfo->wque_cleared = wque_cleared;
  1032. wr_unlock(&(klninfo->stat_lock));
  1033. return true;
  1034. }
  1035. return false;
  1036. }
  1037. static bool klondike_queue_full(struct cgpu_info *klncgpu)
  1038. {
  1039. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1040. struct work *work = NULL;
  1041. int dev, queued, slaves, seq;
  1042. struct timeval now;
  1043. bool nowork;
  1044. cgtime(&now);
  1045. rd_lock(&(klninfo->stat_lock));
  1046. slaves = klninfo->status[0].kline.ws.slavecount;
  1047. for (dev = 0; dev <= slaves; dev++)
  1048. if (ms_tdiff(&now, &(klninfo->jobque[dev].last_update)) > LATE_UPDATE_MS) {
  1049. klninfo->jobque[dev].late_update_count++;
  1050. seq = ++klninfo->jobque[dev].late_update_sequential;
  1051. rd_unlock(&(klninfo->stat_lock));
  1052. if (seq < KLN_LATE_UPDATE_LIMIT) {
  1053. applog(LOG_ERR, "%s%i:%d late update",
  1054. klncgpu->drv->name, klncgpu->device_id, dev);
  1055. klondike_get_stats(klncgpu);
  1056. goto que;
  1057. } else {
  1058. applog(LOG_ERR, "%s%i:%d late update (%d) reached - attempting reset",
  1059. klncgpu->drv->name, klncgpu->device_id,
  1060. dev, KLN_LATE_UPDATE_LIMIT);
  1061. control_init(klncgpu);
  1062. kln_enable(klncgpu);
  1063. klondike_get_stats(klncgpu);
  1064. rd_lock(&(klninfo->stat_lock));
  1065. if (ms_tdiff(&now, &(klninfo->jobque[dev].last_update)) > LATE_UPDATE_MS) {
  1066. rd_unlock(&(klninfo->stat_lock));
  1067. applog(LOG_ERR, "%s%i:%d reset failed - dropping device",
  1068. klncgpu->drv->name, klncgpu->device_id, dev);
  1069. klninfo->shutdown = true;
  1070. return false;
  1071. }
  1072. break;
  1073. }
  1074. }
  1075. rd_unlock(&(klninfo->stat_lock));
  1076. que:
  1077. nowork = true;
  1078. for (queued = 0; queued < MAX_WORK_COUNT-1; queued++)
  1079. for (dev = 0; dev <= slaves; dev++) {
  1080. tryagain:
  1081. rd_lock(&(klninfo->stat_lock));
  1082. if (klninfo->jobque[dev].overheat) {
  1083. double temp = cvtKlnToC(klninfo->status[0].kline.ws.temp);
  1084. if ((queued == MAX_WORK_COUNT-2) &&
  1085. ms_tdiff(&now, &(klninfo->jobque[dev].last_update)) > (LATE_UPDATE_MS/2)) {
  1086. rd_unlock(&(klninfo->stat_lock));
  1087. klondike_get_stats(klncgpu);
  1088. goto tryagain;
  1089. }
  1090. if (temp <= KLN_COOLED_DOWN) {
  1091. klninfo->jobque[dev].overheat = false;
  1092. rd_unlock(&(klninfo->stat_lock));
  1093. applog(LOG_ERR, "%s%i:%d Overheat recovered (%.0fC)",
  1094. klncgpu->drv->name, klncgpu->device_id,
  1095. dev, temp);
  1096. kln_enable(klncgpu);
  1097. goto tryagain;
  1098. } else {
  1099. rd_unlock(&(klninfo->stat_lock));
  1100. continue;
  1101. }
  1102. }
  1103. if (klninfo->jobque[dev].workqc <= queued) {
  1104. rd_unlock(&(klninfo->stat_lock));
  1105. if (!work)
  1106. work = get_queued(klncgpu);
  1107. if (unlikely(!work))
  1108. return false;
  1109. nowork = false;
  1110. if (klondike_send_work(klncgpu, dev, work))
  1111. return false;
  1112. } else
  1113. rd_unlock(&(klninfo->stat_lock));
  1114. }
  1115. if (nowork)
  1116. cgsleep_ms(10); // avoid a hard loop in case we have nothing to do
  1117. return true;
  1118. }
  1119. static int64_t klondike_scanwork(struct thr_info *thr)
  1120. {
  1121. struct cgpu_info *klncgpu = thr->cgpu;
  1122. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1123. int64_t newhashcount = 0;
  1124. int dev, slaves;
  1125. if (klncgpu->usbinfo.nodev)
  1126. return -1;
  1127. restart_wait(thr, 200);
  1128. if (klninfo->status != NULL) {
  1129. rd_lock(&(klninfo->stat_lock));
  1130. slaves = klninfo->status[0].kline.ws.slavecount;
  1131. for (dev = 0; dev <= slaves; dev++) {
  1132. uint64_t newhashdev = 0, hashcount;
  1133. int maxcount;
  1134. hashcount = K_HASHCOUNT(klninfo->status[dev].kline.ws.hashcount);
  1135. maxcount = K_MAXCOUNT(klninfo->status[dev].kline.ws.maxcount);
  1136. // todo: chg this to check workid for wrapped instead
  1137. if (klninfo->devinfo[dev].lasthashcount > hashcount)
  1138. newhashdev += maxcount; // hash counter wrapped
  1139. newhashdev += hashcount - klninfo->devinfo[dev].lasthashcount;
  1140. klninfo->devinfo[dev].lasthashcount = hashcount;
  1141. if (maxcount != 0)
  1142. klninfo->hashcount += (newhashdev << 32) / maxcount;
  1143. }
  1144. newhashcount += 0xffffffffull * (uint64_t)klninfo->noncecount;
  1145. klninfo->noncecount = 0;
  1146. rd_unlock(&(klninfo->stat_lock));
  1147. }
  1148. return newhashcount;
  1149. }
  1150. static void get_klondike_statline_before(char *buf, size_t siz, struct cgpu_info *klncgpu)
  1151. {
  1152. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1153. uint8_t temp = 0xFF;
  1154. uint16_t fan = 0;
  1155. uint16_t clock = 0;
  1156. int dev, slaves;
  1157. char tmp[16];
  1158. if (klninfo->status == NULL) {
  1159. blank_get_statline_before(buf, siz, klncgpu);
  1160. return;
  1161. }
  1162. rd_lock(&(klninfo->stat_lock));
  1163. slaves = klninfo->status[0].kline.ws.slavecount;
  1164. for (dev = 0; dev <= slaves; dev++) {
  1165. if (klninfo->status[dev].kline.ws.temp < temp)
  1166. temp = klninfo->status[dev].kline.ws.temp;
  1167. fan += klninfo->cfg[dev].kline.cfg.fantarget;
  1168. clock += (uint16_t)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  1169. }
  1170. rd_unlock(&(klninfo->stat_lock));
  1171. fan /= slaves + 1;
  1172. fan *= 100/255;
  1173. if (fan > 99) // short on screen space
  1174. fan = 99;
  1175. clock /= slaves + 1;
  1176. if (clock > 999) // error - so truncate it
  1177. clock = 999;
  1178. snprintf(tmp, sizeof(tmp), "%2.0fC", cvtKlnToC(temp));
  1179. if (strlen(tmp) < 4)
  1180. strcat(tmp, " ");
  1181. tailsprintf(buf, siz, "%3dMHz %2d%% %s| ", (int)clock, fan, tmp);
  1182. }
  1183. static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
  1184. {
  1185. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1186. struct api_data *root = NULL;
  1187. char buf[32];
  1188. int dev, slaves;
  1189. if (klninfo->status == NULL)
  1190. return NULL;
  1191. rd_lock(&(klninfo->stat_lock));
  1192. slaves = klninfo->status[0].kline.ws.slavecount;
  1193. for (dev = 0; dev <= slaves; dev++) {
  1194. float fTemp = cvtKlnToC(klninfo->status[dev].kline.ws.temp);
  1195. sprintf(buf, "Temp %d", dev);
  1196. root = api_add_temp(root, buf, &fTemp, true);
  1197. double dClk = (double)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  1198. sprintf(buf, "Clock %d", dev);
  1199. root = api_add_freq(root, buf, &dClk, true);
  1200. unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].kline.cfg.fantarget / 255;
  1201. sprintf(buf, "Fan Percent %d", dev);
  1202. root = api_add_int(root, buf, (int *)(&iFan), true);
  1203. iFan = 0;
  1204. if (klninfo->status[dev].kline.ws.fanspeed > 0)
  1205. iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].kline.ws.fanspeed;
  1206. sprintf(buf, "Fan RPM %d", dev);
  1207. root = api_add_int(root, buf, (int *)(&iFan), true);
  1208. if (klninfo->devinfo[dev].chipstats != NULL) {
  1209. char data[2048];
  1210. char one[32];
  1211. int n;
  1212. sprintf(buf, "Nonces / Chip %d", dev);
  1213. data[0] = '\0';
  1214. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  1215. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n]);
  1216. strcat(data, one);
  1217. }
  1218. root = api_add_string(root, buf, data, true);
  1219. sprintf(buf, "Errors / Chip %d", dev);
  1220. data[0] = '\0';
  1221. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  1222. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].kline.ws.chipcount]);
  1223. strcat(data, one);
  1224. }
  1225. root = api_add_string(root, buf, data, true);
  1226. }
  1227. }
  1228. root = api_add_uint64(root, "Hash Count", &(klninfo->hashcount), true);
  1229. root = api_add_uint64(root, "Error Count", &(klninfo->errorcount), true);
  1230. root = api_add_uint64(root, "Noise Count", &(klninfo->noisecount), true);
  1231. root = api_add_int(root, "KLine Limit", &(klninfo->kline_count), true);
  1232. root = api_add_int(root, "KLine Used", &(klninfo->used_count), true);
  1233. root = api_add_elapsed(root, "KQue Delay Count", &(klninfo->delay_count), true);
  1234. root = api_add_elapsed(root, "KQue Delay Total", &(klninfo->delay_total), true);
  1235. root = api_add_elapsed(root, "KQue Delay Min", &(klninfo->delay_min), true);
  1236. root = api_add_elapsed(root, "KQue Delay Max", &(klninfo->delay_max), true);
  1237. double avg;
  1238. if (klninfo->delay_count == 0)
  1239. avg = 0;
  1240. else
  1241. avg = klninfo->delay_total / klninfo->delay_count;
  1242. root = api_add_diff(root, "KQue Delay Avg", &avg, true);
  1243. root = api_add_elapsed(root, "KQue Nonce Count", &(klninfo->nonce_count), true);
  1244. root = api_add_elapsed(root, "KQue Nonce Total", &(klninfo->nonce_total), true);
  1245. root = api_add_elapsed(root, "KQue Nonce Min", &(klninfo->nonce_min), true);
  1246. root = api_add_elapsed(root, "KQue Nonce Max", &(klninfo->nonce_max), true);
  1247. if (klninfo->nonce_count == 0)
  1248. avg = 0;
  1249. else
  1250. avg = klninfo->nonce_total / klninfo->nonce_count;
  1251. root = api_add_diff(root, "KQue Nonce Avg", &avg, true);
  1252. root = api_add_int(root, "WQue Size", &(klninfo->wque_size), true);
  1253. root = api_add_int(root, "WQue Cleared", &(klninfo->wque_cleared), true);
  1254. rd_unlock(&(klninfo->stat_lock));
  1255. return root;
  1256. }
  1257. struct device_drv klondike_drv = {
  1258. .drv_id = DRIVER_klondike,
  1259. .dname = "Klondike",
  1260. .name = "KLN",
  1261. .drv_detect = klondike_detect,
  1262. .get_api_stats = klondike_api_stats,
  1263. .get_statline_before = get_klondike_statline_before,
  1264. .get_stats = klondike_get_stats,
  1265. .identify_device = klondike_identify,
  1266. .thread_prepare = klondike_thread_prepare,
  1267. .thread_init = klondike_thread_init,
  1268. .hash_work = hash_queued_work,
  1269. .scanwork = klondike_scanwork,
  1270. .queue_full = klondike_queue_full,
  1271. .flush_work = klondike_flush_work,
  1272. .thread_shutdown = klondike_shutdown,
  1273. .thread_enable = klondike_thread_enable
  1274. };