driver-klondike.c 40 KB

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