driver-klondike.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556
  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 bool 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. cglock_init(&klninfo->klist_lock);
  690. return true;
  691. }
  692. }
  693. usb_uninit(klncgpu);
  694. }
  695. free(klninfo->free);
  696. free(klninfo);
  697. free(klncgpu);
  698. return false;
  699. }
  700. static void klondike_detect(bool __maybe_unused hotplug)
  701. {
  702. usb_detect(&klondike_drv, klondike_detect_one);
  703. }
  704. static void klondike_identify(__maybe_unused struct cgpu_info *klncgpu)
  705. {
  706. /*
  707. KLINE kline;
  708. zero_kline(&kline);
  709. kline.hd.cmd = KLN_CMD_IDENT;
  710. kline.hd.dev = 0;
  711. SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  712. */
  713. }
  714. static void klondike_check_nonce(struct cgpu_info *klncgpu, KLIST *kitem)
  715. {
  716. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  717. struct work *work, *look, *tmp;
  718. KLINE *kline = &(kitem->kline);
  719. struct timeval tv_now;
  720. double us_diff;
  721. uint32_t nonce = K_NONCE(kline->wr.nonce) - 0xC0;
  722. applog(LOG_DEBUG, "%s%i:%d FOUND NONCE (%02x:%08x)",
  723. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  724. kline->wr.workid, (unsigned int)nonce);
  725. work = NULL;
  726. cgtime(&tv_now);
  727. rd_lock(&(klncgpu->qlock));
  728. HASH_ITER(hh, klncgpu->queued_work, look, tmp) {
  729. if (ms_tdiff(&tv_now, &(look->tv_stamp)) < OLD_WORK_MS &&
  730. (look->subid == (kline->wr.dev*256 + kline->wr.workid))) {
  731. work = look;
  732. break;
  733. }
  734. }
  735. rd_unlock(&(klncgpu->qlock));
  736. if (work) {
  737. wr_lock(&(klninfo->stat_lock));
  738. klninfo->devinfo[kline->wr.dev].noncecount++;
  739. klninfo->noncecount++;
  740. wr_unlock(&(klninfo->stat_lock));
  741. applog(LOG_DEBUG, "%s%i:%d SUBMIT NONCE (%02x:%08x)",
  742. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  743. kline->wr.workid, (unsigned int)nonce);
  744. cgtime(&tv_now);
  745. bool ok = submit_nonce(klncgpu->thr[0], work, nonce);
  746. applog(LOG_DEBUG, "%s%i:%d chip stats %d, %08x, %d, %d",
  747. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  748. kline->wr.dev, (unsigned int)nonce,
  749. klninfo->devinfo[kline->wr.dev].rangesize,
  750. klninfo->status[kline->wr.dev].kline.ws.chipcount);
  751. klninfo->devinfo[kline->wr.dev].chipstats[(nonce / klninfo->devinfo[kline->wr.dev].rangesize) + (ok ? 0 : klninfo->status[kline->wr.dev].kline.ws.chipcount)]++;
  752. us_diff = us_tdiff(&tv_now, &(kitem->tv_when));
  753. if (klninfo->delay_count == 0) {
  754. klninfo->delay_min = us_diff;
  755. klninfo->delay_max = us_diff;
  756. } else {
  757. if (klninfo->delay_min > us_diff)
  758. klninfo->delay_min = us_diff;
  759. if (klninfo->delay_max < us_diff)
  760. klninfo->delay_max = us_diff;
  761. }
  762. klninfo->delay_count++;
  763. klninfo->delay_total += us_diff;
  764. if (klninfo->nonce_count > 0) {
  765. us_diff = us_tdiff(&(kitem->tv_when), &(klninfo->tv_last_nonce_received));
  766. if (klninfo->nonce_count == 1) {
  767. klninfo->nonce_min = us_diff;
  768. klninfo->nonce_max = us_diff;
  769. } else {
  770. if (klninfo->nonce_min > us_diff)
  771. klninfo->nonce_min = us_diff;
  772. if (klninfo->nonce_max < us_diff)
  773. klninfo->nonce_max = us_diff;
  774. }
  775. klninfo->nonce_total += us_diff;
  776. }
  777. klninfo->nonce_count++;
  778. memcpy(&(klninfo->tv_last_nonce_received), &(kitem->tv_when),
  779. sizeof(klninfo->tv_last_nonce_received));
  780. return;
  781. }
  782. applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored",
  783. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  784. kline->wr.workid, (unsigned int)nonce);
  785. //inc_hw_errors(klncgpu->thr[0]);
  786. }
  787. // thread to keep looking for replies
  788. static void *klondike_get_replies(void *userdata)
  789. {
  790. struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
  791. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  792. KLIST *kitem = NULL;
  793. char *hexdata;
  794. int err, recd, slaves, dev, isc;
  795. bool overheat, sent;
  796. applog(LOG_DEBUG, "%s%i: listening for replies",
  797. klncgpu->drv->name, klncgpu->device_id);
  798. while (klncgpu->shutdown == false) {
  799. if (klncgpu->usbinfo.nodev)
  800. return NULL;
  801. if (kitem == NULL)
  802. kitem = allocate_kitem(klncgpu);
  803. else
  804. memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
  805. err = usb_read(klncgpu, (char *)&(kitem->kline), REPLY_SIZE, &recd, C_GETRESULTS);
  806. if (err || recd != REPLY_SIZE) {
  807. if (err != -7)
  808. applog(LOG_ERR, "%s%i: reply err=%d amt=%d",
  809. klncgpu->drv->name, klncgpu->device_id,
  810. err, recd);
  811. }
  812. if (!err && recd == REPLY_SIZE) {
  813. cgtime(&(kitem->tv_when));
  814. rd_lock(&(klninfo->stat_lock));
  815. kitem->block_seq = klninfo->block_seq;
  816. rd_unlock(&(klninfo->stat_lock));
  817. if (opt_log_level <= READ_DEBUG) {
  818. hexdata = bin2hex((unsigned char *)&(kitem->kline.hd.dev), recd-1);
  819. applog(READ_DEBUG, "%s%i:%d reply [%c:%s]",
  820. klncgpu->drv->name, klncgpu->device_id,
  821. (int)(kitem->kline.hd.dev),
  822. kitem->kline.hd.cmd, hexdata);
  823. free(hexdata);
  824. }
  825. // We can't check this until it's initialised
  826. if (klninfo->initialised) {
  827. rd_lock(&(klninfo->stat_lock));
  828. slaves = klninfo->status[0].kline.ws.slavecount;
  829. rd_unlock(&(klninfo->stat_lock));
  830. if (kitem->kline.hd.dev > slaves) {
  831. applog(LOG_ERR, "%s%i: reply [%c] has invalid dev=%d (max=%d) using 0",
  832. klncgpu->drv->name, klncgpu->device_id,
  833. (char)(kitem->kline.hd.cmd),
  834. (int)(kitem->kline.hd.dev),
  835. slaves);
  836. /* TODO: this is rather problematic if there are slaves
  837. * however without slaves - it should always be zero */
  838. kitem->kline.hd.dev = 0;
  839. } else {
  840. wr_lock(&(klninfo->stat_lock));
  841. klninfo->jobque[kitem->kline.hd.dev].late_update_sequential = 0;
  842. wr_unlock(&(klninfo->stat_lock));
  843. }
  844. }
  845. switch (kitem->kline.hd.cmd) {
  846. case KLN_CMD_NONCE:
  847. klondike_check_nonce(klncgpu, kitem);
  848. display_kline(klncgpu, &kitem->kline, msg_reply);
  849. break;
  850. case KLN_CMD_WORK:
  851. // We can't do/check this until it's initialised
  852. if (klninfo->initialised) {
  853. dev = kitem->kline.ws.dev;
  854. if (kitem->kline.ws.workqc == 0) {
  855. bool idle = false;
  856. rd_lock(&(klninfo->stat_lock));
  857. if (klninfo->jobque[dev].flushed == false)
  858. idle = true;
  859. slaves = klninfo->status[0].kline.ws.slavecount;
  860. rd_unlock(&(klninfo->stat_lock));
  861. if (idle)
  862. applog(LOG_WARNING, "%s%i:%d went idle before work was sent",
  863. klncgpu->drv->name,
  864. klncgpu->device_id,
  865. dev);
  866. }
  867. wr_lock(&(klninfo->stat_lock));
  868. klninfo->jobque[dev].flushed = false;
  869. wr_unlock(&(klninfo->stat_lock));
  870. }
  871. case KLN_CMD_STATUS:
  872. case KLN_CMD_ABORT:
  873. // We can't do/check this until it's initialised
  874. if (klninfo->initialised) {
  875. isc = 0;
  876. dev = kitem->kline.ws.dev;
  877. wr_lock(&(klninfo->stat_lock));
  878. klninfo->jobque[dev].workqc = (int)(kitem->kline.ws.workqc);
  879. cgtime(&(klninfo->jobque[dev].last_update));
  880. slaves = klninfo->status[0].kline.ws.slavecount;
  881. overheat = klninfo->jobque[dev].overheat;
  882. if (dev == 0) {
  883. if (kitem->kline.ws.slavecount != slaves)
  884. isc = ++klninfo->incorrect_slave_sequential;
  885. else
  886. isc = klninfo->incorrect_slave_sequential = 0;
  887. }
  888. wr_unlock(&(klninfo->stat_lock));
  889. if (isc) {
  890. applog(LOG_ERR, "%s%i:%d reply [%c] has a diff"
  891. " # of slaves=%d (curr=%d)%s",
  892. klncgpu->drv->name,
  893. klncgpu->device_id,
  894. dev,
  895. (char)(kitem->kline.ws.cmd),
  896. (int)(kitem->kline.ws.slavecount),
  897. slaves,
  898. isc <= KLN_ISS_IGNORE ? "" :
  899. " disabling device");
  900. if (isc > KLN_ISS_IGNORE)
  901. usb_nodev(klncgpu);
  902. break;
  903. }
  904. if (!overheat) {
  905. double temp = cvtKlnToC(kitem->kline.ws.temp);
  906. if (temp >= KLN_KILLWORK_TEMP) {
  907. KLINE kline;
  908. wr_lock(&(klninfo->stat_lock));
  909. klninfo->jobque[dev].overheat = true;
  910. wr_unlock(&(klninfo->stat_lock));
  911. applog(LOG_WARNING, "%s%i:%d Critical overheat (%.0fC)",
  912. klncgpu->drv->name,
  913. klncgpu->device_id,
  914. dev, temp);
  915. zero_kline(&kline);
  916. kline.hd.cmd = KLN_CMD_ABORT;
  917. kline.hd.dev = dev;
  918. sent = SendCmd(klncgpu, &kline, KSENDHD(0));
  919. kln_disable(klncgpu, dev, false);
  920. if (!sent) {
  921. applog(LOG_ERR, "%s%i:%d overheat failed to"
  922. " abort work - disabling device",
  923. klncgpu->drv->name,
  924. klncgpu->device_id,
  925. dev);
  926. usb_nodev(klncgpu);
  927. }
  928. }
  929. }
  930. }
  931. case KLN_CMD_ENABLE:
  932. wr_lock(&(klninfo->stat_lock));
  933. klninfo->errorcount += kitem->kline.ws.errorcount;
  934. klninfo->noisecount += kitem->kline.ws.noise;
  935. wr_unlock(&(klninfo->stat_lock));
  936. display_kline(klncgpu, &kitem->kline, msg_reply);
  937. kitem->ready = true;
  938. kitem = NULL;
  939. break;
  940. case KLN_CMD_CONFIG:
  941. display_kline(klncgpu, &kitem->kline, msg_reply);
  942. kitem->ready = true;
  943. kitem = NULL;
  944. break;
  945. case KLN_CMD_IDENT:
  946. display_kline(klncgpu, &kitem->kline, msg_reply);
  947. kitem->ready = true;
  948. kitem = NULL;
  949. break;
  950. default:
  951. display_kline(klncgpu, &kitem->kline, msg_reply);
  952. break;
  953. }
  954. }
  955. }
  956. return NULL;
  957. }
  958. static void klondike_flush_work(struct cgpu_info *klncgpu)
  959. {
  960. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  961. KLIST *kitem;
  962. KLINE kline;
  963. int slaves, dev;
  964. wr_lock(&(klninfo->stat_lock));
  965. klninfo->block_seq++;
  966. slaves = klninfo->status[0].kline.ws.slavecount;
  967. wr_unlock(&(klninfo->stat_lock));
  968. applog(LOG_DEBUG, "%s%i: flushing work",
  969. klncgpu->drv->name, klncgpu->device_id);
  970. zero_kline(&kline);
  971. kline.hd.cmd = KLN_CMD_ABORT;
  972. for (dev = 0; dev <= slaves; dev++) {
  973. kline.hd.dev = dev;
  974. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  975. if (kitem != NULL) {
  976. wr_lock(&(klninfo->stat_lock));
  977. memcpy((void *)&(klninfo->status[dev]),
  978. kitem,
  979. sizeof(klninfo->status[dev]));
  980. klninfo->jobque[dev].flushed = true;
  981. wr_unlock(&(klninfo->stat_lock));
  982. kitem = release_kitem(klncgpu, kitem);
  983. }
  984. }
  985. }
  986. static bool klondike_thread_prepare(struct thr_info *thr)
  987. {
  988. struct cgpu_info *klncgpu = thr->cgpu;
  989. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  990. if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) {
  991. applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id);
  992. return false;
  993. }
  994. pthread_detach(klninfo->replies_thr.pth);
  995. // let the listening get started
  996. cgsleep_ms(100);
  997. return klondike_init(klncgpu);
  998. }
  999. static bool klondike_thread_init(struct thr_info *thr)
  1000. {
  1001. struct cgpu_info *klncgpu = thr->cgpu;
  1002. if (klncgpu->usbinfo.nodev)
  1003. return false;
  1004. klondike_flush_work(klncgpu);
  1005. return true;
  1006. }
  1007. static void klondike_shutdown(struct thr_info *thr)
  1008. {
  1009. struct cgpu_info *klncgpu = thr->cgpu;
  1010. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1011. applog(LOG_DEBUG, "%s%i: shutting down work",
  1012. klncgpu->drv->name, klncgpu->device_id);
  1013. kln_disable(klncgpu, klninfo->status[0].kline.ws.slavecount, true);
  1014. klncgpu->shutdown = true;
  1015. }
  1016. static void klondike_thread_enable(struct thr_info *thr)
  1017. {
  1018. struct cgpu_info *klncgpu = thr->cgpu;
  1019. if (klncgpu->usbinfo.nodev)
  1020. return;
  1021. /*
  1022. KLINE kline;
  1023. zero_kline(&kline);
  1024. kline.hd.cmd = KLN_CMD_ENABLE;
  1025. kline.hd.dev = dev;
  1026. kline.hd.buf[0] = KLN_CMD_ENABLE_OFF;
  1027. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
  1028. */
  1029. }
  1030. static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
  1031. {
  1032. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1033. struct work *look, *tmp;
  1034. KLINE kline;
  1035. struct timeval tv_old;
  1036. int wque_size, wque_cleared;
  1037. if (klncgpu->usbinfo.nodev)
  1038. return false;
  1039. zero_kline(&kline);
  1040. kline.wt.cmd = KLN_CMD_WORK;
  1041. kline.wt.dev = dev;
  1042. memcpy(kline.wt.midstate, work->midstate, MIDSTATE_BYTES);
  1043. memcpy(kline.wt.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  1044. kline.wt.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
  1045. work->subid = dev*256 + kline.wt.workid;
  1046. cgtime(&work->tv_stamp);
  1047. if (opt_log_level <= LOG_DEBUG) {
  1048. char *hexdata = bin2hex((void *)&kline.wt, sizeof(kline.wt));
  1049. applog(LOG_DEBUG, "WORKDATA: %s", hexdata);
  1050. free(hexdata);
  1051. }
  1052. applog(LOG_DEBUG, "%s%i:%d sending work (%d:%02x)",
  1053. klncgpu->drv->name, klncgpu->device_id, dev,
  1054. dev, kline.wt.workid);
  1055. KLIST *kitem = SendCmdGetReply(klncgpu, &kline, sizeof(kline.wt));
  1056. if (kitem != NULL) {
  1057. wr_lock(&(klninfo->stat_lock));
  1058. memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(klninfo->status[dev]));
  1059. wr_unlock(&(klninfo->stat_lock));
  1060. kitem = release_kitem(klncgpu, kitem);
  1061. // remove old work
  1062. wque_size = 0;
  1063. wque_cleared = 0;
  1064. cgtime(&tv_old);
  1065. wr_lock(&klncgpu->qlock);
  1066. HASH_ITER(hh, klncgpu->queued_work, look, tmp) {
  1067. if (ms_tdiff(&tv_old, &(look->tv_stamp)) > OLD_WORK_MS) {
  1068. __work_completed(klncgpu, look);
  1069. free_work(look);
  1070. wque_cleared++;
  1071. } else
  1072. wque_size++;
  1073. }
  1074. wr_unlock(&klncgpu->qlock);
  1075. wr_lock(&(klninfo->stat_lock));
  1076. klninfo->wque_size = wque_size;
  1077. klninfo->wque_cleared = wque_cleared;
  1078. wr_unlock(&(klninfo->stat_lock));
  1079. return true;
  1080. }
  1081. return false;
  1082. }
  1083. static bool klondike_queue_full(struct cgpu_info *klncgpu)
  1084. {
  1085. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1086. struct work *work = NULL;
  1087. int dev, queued, slaves, seq, howlong;
  1088. struct timeval now;
  1089. bool nowork;
  1090. if (klncgpu->shutdown == true)
  1091. return true;
  1092. cgtime(&now);
  1093. rd_lock(&(klninfo->stat_lock));
  1094. slaves = klninfo->status[0].kline.ws.slavecount;
  1095. for (dev = 0; dev <= slaves; dev++)
  1096. if (ms_tdiff(&now, &(klninfo->jobque[dev].last_update)) > LATE_UPDATE_MS) {
  1097. klninfo->jobque[dev].late_update_count++;
  1098. seq = ++klninfo->jobque[dev].late_update_sequential;
  1099. rd_unlock(&(klninfo->stat_lock));
  1100. if (seq < LATE_UPDATE_LIMIT) {
  1101. applog(LOG_DEBUG, "%s%i:%d late update",
  1102. klncgpu->drv->name, klncgpu->device_id, dev);
  1103. klondike_get_stats(klncgpu);
  1104. goto que;
  1105. } else {
  1106. applog(LOG_WARNING, "%s%i:%d late update (%d) reached - attempting reset",
  1107. klncgpu->drv->name, klncgpu->device_id,
  1108. dev, LATE_UPDATE_LIMIT);
  1109. control_init(klncgpu);
  1110. kln_enable(klncgpu);
  1111. klondike_get_stats(klncgpu);
  1112. rd_lock(&(klninfo->stat_lock));
  1113. howlong = ms_tdiff(&now, &(klninfo->jobque[dev].last_update));
  1114. if (howlong > LATE_UPDATE_MS) {
  1115. rd_unlock(&(klninfo->stat_lock));
  1116. if (howlong > LATE_UPDATE_NODEV_MS) {
  1117. applog(LOG_ERR, "%s%i:%d reset failed - dropping device",
  1118. klncgpu->drv->name, klncgpu->device_id, dev);
  1119. usb_nodev(klncgpu);
  1120. } else
  1121. cgsleep_ms(LATE_UPDATE_SLEEP_MS);
  1122. return true;
  1123. }
  1124. break;
  1125. }
  1126. }
  1127. rd_unlock(&(klninfo->stat_lock));
  1128. que:
  1129. nowork = true;
  1130. for (queued = 0; queued < MAX_WORK_COUNT-1; queued++)
  1131. for (dev = 0; dev <= slaves; dev++) {
  1132. tryagain:
  1133. rd_lock(&(klninfo->stat_lock));
  1134. if (klninfo->jobque[dev].overheat) {
  1135. double temp = cvtKlnToC(klninfo->status[0].kline.ws.temp);
  1136. if ((queued == MAX_WORK_COUNT-2) &&
  1137. ms_tdiff(&now, &(klninfo->jobque[dev].last_update)) > (LATE_UPDATE_MS/2)) {
  1138. rd_unlock(&(klninfo->stat_lock));
  1139. klondike_get_stats(klncgpu);
  1140. goto tryagain;
  1141. }
  1142. if (temp <= KLN_COOLED_DOWN) {
  1143. klninfo->jobque[dev].overheat = false;
  1144. rd_unlock(&(klninfo->stat_lock));
  1145. applog(LOG_WARNING, "%s%i:%d Overheat recovered (%.0fC)",
  1146. klncgpu->drv->name, klncgpu->device_id,
  1147. dev, temp);
  1148. kln_enable(klncgpu);
  1149. goto tryagain;
  1150. } else {
  1151. rd_unlock(&(klninfo->stat_lock));
  1152. continue;
  1153. }
  1154. }
  1155. if (klninfo->jobque[dev].workqc <= queued) {
  1156. rd_unlock(&(klninfo->stat_lock));
  1157. if (!work)
  1158. work = get_queued(klncgpu);
  1159. if (unlikely(!work))
  1160. return false;
  1161. nowork = false;
  1162. if (klondike_send_work(klncgpu, dev, work))
  1163. return false;
  1164. } else
  1165. rd_unlock(&(klninfo->stat_lock));
  1166. }
  1167. if (nowork)
  1168. cgsleep_ms(10); // avoid a hard loop in case we have nothing to do
  1169. return true;
  1170. }
  1171. static int64_t klondike_scanwork(struct thr_info *thr)
  1172. {
  1173. struct cgpu_info *klncgpu = thr->cgpu;
  1174. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1175. int64_t newhashcount = 0;
  1176. int dev, slaves;
  1177. if (klncgpu->usbinfo.nodev)
  1178. return -1;
  1179. restart_wait(thr, 200);
  1180. if (klninfo->status != NULL) {
  1181. rd_lock(&(klninfo->stat_lock));
  1182. slaves = klninfo->status[0].kline.ws.slavecount;
  1183. for (dev = 0; dev <= slaves; dev++) {
  1184. uint64_t newhashdev = 0, hashcount;
  1185. int maxcount;
  1186. hashcount = K_HASHCOUNT(klninfo->status[dev].kline.ws.hashcount);
  1187. maxcount = K_MAXCOUNT(klninfo->status[dev].kline.ws.maxcount);
  1188. // todo: chg this to check workid for wrapped instead
  1189. if (klninfo->devinfo[dev].lasthashcount > hashcount)
  1190. newhashdev += maxcount; // hash counter wrapped
  1191. newhashdev += hashcount - klninfo->devinfo[dev].lasthashcount;
  1192. klninfo->devinfo[dev].lasthashcount = hashcount;
  1193. if (maxcount != 0)
  1194. klninfo->hashcount += (newhashdev << 32) / maxcount;
  1195. }
  1196. newhashcount += 0xffffffffull * (uint64_t)klninfo->noncecount;
  1197. klninfo->noncecount = 0;
  1198. rd_unlock(&(klninfo->stat_lock));
  1199. }
  1200. return newhashcount;
  1201. }
  1202. static void get_klondike_statline_before(char *buf, size_t siz, struct cgpu_info *klncgpu)
  1203. {
  1204. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1205. uint8_t temp = 0xFF;
  1206. uint16_t fan = 0;
  1207. uint16_t clock = 0;
  1208. int dev, slaves;
  1209. char tmp[16];
  1210. if (klninfo->status == NULL) {
  1211. blank_get_statline_before(buf, siz, klncgpu);
  1212. return;
  1213. }
  1214. rd_lock(&(klninfo->stat_lock));
  1215. slaves = klninfo->status[0].kline.ws.slavecount;
  1216. for (dev = 0; dev <= slaves; dev++) {
  1217. if (klninfo->status[dev].kline.ws.temp < temp)
  1218. temp = klninfo->status[dev].kline.ws.temp;
  1219. fan += klninfo->cfg[dev].kline.cfg.fantarget;
  1220. clock += (uint16_t)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  1221. }
  1222. rd_unlock(&(klninfo->stat_lock));
  1223. fan /= slaves + 1;
  1224. fan *= 100/255;
  1225. if (fan > 99) // short on screen space
  1226. fan = 99;
  1227. clock /= slaves + 1;
  1228. if (clock > 999) // error - so truncate it
  1229. clock = 999;
  1230. snprintf(tmp, sizeof(tmp), "%2.0fC", cvtKlnToC(temp));
  1231. if (strlen(tmp) < 4)
  1232. strcat(tmp, " ");
  1233. tailsprintf(buf, siz, "%3dMHz %2d%% %s| ", (int)clock, fan, tmp);
  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. };