driver-klondike.c 43 KB

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