driver-klondike.c 42 KB

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