driver-klondike.c 46 KB

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