driver-klondike.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  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 "config.h"
  12. #include <float.h>
  13. #include <limits.h>
  14. #include <pthread.h>
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <strings.h>
  19. #include <sys/time.h>
  20. #include <unistd.h>
  21. #include <math.h>
  22. #ifdef WIN32
  23. #include <windows.h>
  24. #endif
  25. #include "compat.h"
  26. #include "deviceapi.h"
  27. #include "lowlevel.h"
  28. #include "lowl-usb.h"
  29. #include "miner.h"
  30. #define K1 "K1"
  31. #define K16 "K16"
  32. #define K64 "K64"
  33. static const char *msg_detect_send = "DSend";
  34. static const char *msg_detect_reply = "DReply";
  35. static const char *msg_send = "Send";
  36. static const char *msg_reply = "Reply";
  37. #define KLN_CMD_ABORT 'A'
  38. #define KLN_CMD_CONFIG 'C'
  39. #define KLN_CMD_ENABLE 'E'
  40. #define KLN_CMD_IDENT 'I'
  41. #define KLN_CMD_NONCE '='
  42. #define KLN_CMD_STATUS 'S'
  43. #define KLN_CMD_WORK 'W'
  44. #define KLN_CMD_ENABLE_OFF '0'
  45. #define KLN_CMD_ENABLE_ON '1'
  46. #define MIDSTATE_BYTES 32
  47. #define MERKLE_OFFSET 64
  48. #define MERKLE_BYTES 12
  49. #define REPLY_SIZE 15 // adequate for all types of replies
  50. #define MAX_KLINES 1024 // unhandled reply limit
  51. #define REPLY_WAIT_TIME 100 // poll interval for a cmd waiting it's reply
  52. #define CMD_REPLY_RETRIES 8 // how many retries for cmds
  53. #define MAX_WORK_COUNT 4 // for now, must be binary multiple and match firmware
  54. #define TACH_FACTOR 87890 // fan rpm divisor
  55. #define KLN_KILLWORK_TEMP 53.5
  56. #define KLN_COOLED_DOWN 45.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. * How many incorrect slave counts to ignore in a row
  66. * 2 means it allows random grabage returned twice
  67. * Until slaves are implemented, this should never occur
  68. * so allowing 2 in a row should ignore random errros
  69. */
  70. #define KLN_ISS_IGNORE 2
  71. /*
  72. * If the queue status hasn't been updated for this long then do it now
  73. * 5GH/s = 859ms per full nonce range
  74. */
  75. #define LATE_UPDATE_MS ((int)(2.5 * 1000))
  76. // If 5 late updates in a row, try to reset the device
  77. #define LATE_UPDATE_LIMIT 5
  78. // If the reset fails sleep for 1s
  79. #define LATE_UPDATE_SLEEP_MS 1000
  80. // However give up after 8s
  81. #define LATE_UPDATE_NODEV_MS ((int)(8.0 * 1000))
  82. BFG_REGISTER_DRIVER(klondike_drv)
  83. typedef struct klondike_header {
  84. uint8_t cmd;
  85. uint8_t dev;
  86. uint8_t buf[REPLY_SIZE-2];
  87. } HEADER;
  88. #define K_2(_bytes) ((int)(_bytes[0]) + \
  89. ((int)(_bytes[1]) << 8))
  90. #define K_4(_bytes) ((uint64_t)(_bytes[0]) + \
  91. ((uint64_t)(_bytes[1]) << 8) + \
  92. ((uint64_t)(_bytes[2]) << 16) + \
  93. ((uint64_t)(_bytes[3]) << 24))
  94. #define K_SERIAL(_serial) K_4(_serial)
  95. #define K_HASHCOUNT(_hashcount) K_2(_hashcount)
  96. #define K_MAXCOUNT(_maxcount) K_2(_maxcount)
  97. #define K_NONCE(_nonce) K_4(_nonce)
  98. #define K_HASHCLOCK(_hashclock) K_2(_hashclock)
  99. #define SET_HASHCLOCK(_hashclock, _value) do { \
  100. (_hashclock)[0] = (uint8_t)((_value) & 0xff); \
  101. (_hashclock)[1] = (uint8_t)(((_value) >> 8) & 0xff); \
  102. } while(0)
  103. #define KSENDHD(_add) (sizeof(uint8_t) + sizeof(uint8_t) + _add)
  104. typedef struct klondike_id {
  105. uint8_t cmd;
  106. uint8_t dev;
  107. uint8_t version;
  108. uint8_t product[7];
  109. uint8_t serial[4];
  110. } IDENTITY;
  111. typedef struct klondike_status {
  112. uint8_t cmd;
  113. uint8_t dev;
  114. uint8_t state;
  115. uint8_t chipcount;
  116. uint8_t slavecount;
  117. uint8_t workqc;
  118. uint8_t workid;
  119. uint8_t temp;
  120. uint8_t fanspeed;
  121. uint8_t errorcount;
  122. uint8_t hashcount[2];
  123. uint8_t maxcount[2];
  124. uint8_t noise;
  125. } WORKSTATUS;
  126. typedef struct _worktask {
  127. uint8_t cmd;
  128. uint8_t dev;
  129. uint8_t workid;
  130. uint8_t midstate[32];
  131. uint8_t merkle[12];
  132. } WORKTASK;
  133. typedef struct _workresult {
  134. uint8_t cmd;
  135. uint8_t dev;
  136. uint8_t workid;
  137. uint8_t nonce[4];
  138. } WORKRESULT;
  139. typedef struct klondike_cfg {
  140. uint8_t cmd;
  141. uint8_t dev;
  142. uint8_t hashclock[2];
  143. uint8_t temptarget;
  144. uint8_t tempcritical;
  145. uint8_t fantarget;
  146. uint8_t pad2;
  147. } WORKCFG;
  148. typedef struct kline {
  149. union {
  150. HEADER hd;
  151. IDENTITY id;
  152. WORKSTATUS ws;
  153. WORKTASK wt;
  154. WORKRESULT wr;
  155. WORKCFG cfg;
  156. };
  157. } KLINE;
  158. #define zero_kline(_kline) memset((void *)(_kline), 0, sizeof(KLINE));
  159. typedef struct device_info {
  160. uint32_t noncecount;
  161. uint32_t nextworkid;
  162. uint16_t lasthashcount;
  163. uint64_t totalhashcount;
  164. uint32_t rangesize;
  165. uint32_t *chipstats;
  166. } DEVINFO;
  167. typedef struct klist {
  168. struct klist *prev;
  169. struct klist *next;
  170. KLINE kline;
  171. struct timeval tv_when;
  172. int block_seq;
  173. bool ready;
  174. bool working;
  175. } KLIST;
  176. typedef struct jobque {
  177. int workqc;
  178. struct timeval last_update;
  179. bool overheat;
  180. bool flushed;
  181. int late_update_count;
  182. int late_update_sequential;
  183. } JOBQUE;
  184. struct klondike_info {
  185. pthread_rwlock_t stat_lock;
  186. struct thr_info replies_thr;
  187. cglock_t klist_lock;
  188. KLIST *used;
  189. KLIST *free;
  190. int kline_count;
  191. int used_count;
  192. int block_seq;
  193. KLIST *status;
  194. DEVINFO *devinfo;
  195. KLIST *cfg;
  196. JOBQUE *jobque;
  197. int noncecount;
  198. uint64_t hashcount;
  199. uint64_t errorcount;
  200. uint64_t noisecount;
  201. int incorrect_slave_sequential;
  202. int16_t nonce_offset;
  203. // us Delay from USB reply to being processed
  204. double delay_count;
  205. double delay_total;
  206. double delay_min;
  207. double delay_max;
  208. struct timeval tv_last_nonce_received;
  209. // Time from recieving one nonce to the next
  210. double nonce_count;
  211. double nonce_total;
  212. double nonce_min;
  213. double nonce_max;
  214. int wque_size;
  215. int wque_cleared;
  216. int clock;
  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. cgpu_set_defaults(klncgpu);
  634. zero_kline(&kline);
  635. kline.hd.cmd = KLN_CMD_STATUS;
  636. kline.hd.dev = 0;
  637. kitem = SendCmdGetReply(klncgpu, &kline, 0);
  638. if (kitem == NULL)
  639. return false;
  640. slaves = kitem->kline.ws.slavecount;
  641. if (klninfo->status == NULL) {
  642. applog(LOG_DEBUG, "%s%i: initializing data",
  643. klncgpu->drv->name, klncgpu->device_id);
  644. // alloc space for status, devinfo, cfg and jobque for master and slaves
  645. klninfo->status = calloc(slaves+1, sizeof(*(klninfo->status)));
  646. if (unlikely(!klninfo->status))
  647. quit(1, "Failed to calloc status array in klondke_get_stats");
  648. klninfo->devinfo = calloc(slaves+1, sizeof(*(klninfo->devinfo)));
  649. if (unlikely(!klninfo->devinfo))
  650. quit(1, "Failed to calloc devinfo array in klondke_get_stats");
  651. klninfo->cfg = calloc(slaves+1, sizeof(*(klninfo->cfg)));
  652. if (unlikely(!klninfo->cfg))
  653. quit(1, "Failed to calloc cfg array in klondke_get_stats");
  654. klninfo->jobque = calloc(slaves+1, sizeof(*(klninfo->jobque)));
  655. if (unlikely(!klninfo->jobque))
  656. quit(1, "Failed to calloc jobque array in klondke_get_stats");
  657. }
  658. memcpy((void *)(&(klninfo->status[0])), (void *)kitem, sizeof(klninfo->status[0]));
  659. kitem = release_kitem(klncgpu, kitem);
  660. // zero init triggers read back only
  661. zero_kline(&kline);
  662. kline.cfg.cmd = KLN_CMD_CONFIG;
  663. int size = 2;
  664. // boundaries are checked by device, with valid values returned
  665. {
  666. SET_HASHCLOCK(kline.cfg.hashclock, klninfo->clock);
  667. kline.cfg.temptarget = cvtCToKln(klncgpu->targettemp);
  668. kline.cfg.tempcritical = 0; // hard code for old firmware
  669. kline.cfg.fantarget = 0xff; // hard code for old firmware
  670. size = sizeof(kline.cfg) - 2;
  671. }
  672. for (dev = 0; dev <= slaves; dev++) {
  673. kline.cfg.dev = dev;
  674. kitem = SendCmdGetReply(klncgpu, &kline, size);
  675. if (kitem != NULL) {
  676. memcpy((void *)&(klninfo->cfg[dev]), kitem, sizeof(klninfo->cfg[dev]));
  677. applog(LOG_WARNING, "%s%i:%d config (%d: Clk: %d, T:%.0lf, C:%.0lf, F:%d)",
  678. klncgpu->drv->name, klncgpu->device_id, dev,
  679. dev, K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock),
  680. cvtKlnToC(klninfo->cfg[dev].kline.cfg.temptarget),
  681. cvtKlnToC(klninfo->cfg[dev].kline.cfg.tempcritical),
  682. (int)100*klninfo->cfg[dev].kline.cfg.fantarget/256);
  683. kitem = release_kitem(klncgpu, kitem);
  684. }
  685. }
  686. klondike_get_stats(klncgpu);
  687. klninfo->initialised = true;
  688. for (dev = 0; dev <= slaves; dev++) {
  689. klninfo->devinfo[dev].rangesize = ((uint64_t)1<<32) / klninfo->status[dev].kline.ws.chipcount;
  690. klninfo->devinfo[dev].chipstats = calloc(klninfo->status[dev].kline.ws.chipcount*2 , sizeof(uint32_t));
  691. }
  692. bool ok = kln_enable(klncgpu);
  693. if (!ok)
  694. applog(LOG_ERR, "%s%i: failed to enable", klncgpu->drv->name, klncgpu->device_id);
  695. return ok;
  696. }
  697. static void control_init(struct cgpu_info *klncgpu)
  698. {
  699. struct klondike_info * const klninfo = klncgpu->device_data;
  700. int err, interface;
  701. if (klninfo->usbinfo_nodev)
  702. return;
  703. interface = 0;
  704. err = libusb_control_transfer(klninfo->usbdev_handle, 0, 9, 1, interface, NULL, 0, 999);
  705. applog(LOG_DEBUG, "%s%i: reset got err %d",
  706. klncgpu->drv->name, klncgpu->device_id, err);
  707. }
  708. static
  709. const char *klondike_set_clock(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
  710. {
  711. struct klondike_info * const klninfo = proc->device_data;
  712. if (klninfo->initialised)
  713. return "Cannot change clock after initialisation";
  714. klninfo->clock = atoi(newvalue);
  715. return NULL;
  716. }
  717. static const struct bfg_set_device_definition klondike_set_device_funcs[] = {
  718. {"clock", klondike_set_clock, "clock frequency (can only be set at startup, with --set-device)"},
  719. {NULL}
  720. };
  721. static
  722. bool klondike_lowl_match(const struct lowlevel_device_info * const info)
  723. {
  724. if (!lowlevel_match_id(info, &lowl_usb, 0x04d8, 0xf60a))
  725. return false;
  726. return (info->manufacturer && strstr(info->manufacturer, "Klondike"));
  727. }
  728. static
  729. bool klondike_lowl_probe(const struct lowlevel_device_info * const info)
  730. {
  731. if (unlikely(info->lowl != &lowl_usb))
  732. {
  733. applog(LOG_DEBUG, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not usb!",
  734. __func__, info->product, info->serial);
  735. return false;
  736. }
  737. struct libusb_device * const dev = info->lowl_data;
  738. if (bfg_claim_libusb(&klondike_drv, true, dev))
  739. return false;
  740. // static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  741. struct cgpu_info * const klncgpu = malloc(sizeof(*klncgpu));
  742. struct klondike_info *klninfo = NULL;
  743. KLINE kline;
  744. if (unlikely(!klncgpu))
  745. quit(1, "Failed to calloc klncgpu in klondike_detect_one");
  746. *klncgpu = (struct cgpu_info){
  747. .drv = &klondike_drv,
  748. .deven = DEV_ENABLED,
  749. .threads = 1,
  750. .targettemp = 50,
  751. .cutofftemp = (int)KLN_KILLWORK_TEMP,
  752. .set_device_funcs = klondike_set_device_funcs,
  753. };
  754. klninfo = calloc(1, sizeof(*klninfo));
  755. if (unlikely(!klninfo))
  756. quit(1, "Failed to calloc klninfo in klondke_detect_one");
  757. klninfo->clock = 282;
  758. klncgpu->device_data = (void *)klninfo;
  759. klninfo->free = new_klist_set(klncgpu);
  760. if (usb_init(klncgpu, dev)) {
  761. int sent, recd, err;
  762. KLIST kitem;
  763. int attempts = 0;
  764. klncgpu->device_path = strdup(info->devid);
  765. control_init(klncgpu);
  766. while (attempts++ < 3) {
  767. kline.hd.cmd = KLN_CMD_IDENT;
  768. kline.hd.dev = 0;
  769. display_send_kline(klncgpu, &kline, msg_detect_send);
  770. err = usb_write(klncgpu, (char *)&(kline.hd), 2, &sent);
  771. if (err < 0 || sent != 2) {
  772. applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)",
  773. klncgpu->drv->dname,
  774. klncgpu->device_path,
  775. sent, err);
  776. }
  777. cgsleep_ms(REPLY_WAIT_TIME*10);
  778. err = usb_read(klncgpu, &kitem.kline, REPLY_SIZE, &recd);
  779. if (err < 0) {
  780. applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)",
  781. klncgpu->drv->dname,
  782. klncgpu->device_path,
  783. recd, err);
  784. } else if (recd < 1) {
  785. applog(LOG_ERR, "%s (%s) detect empty reply (%d)",
  786. klncgpu->drv->dname,
  787. klncgpu->device_path,
  788. recd);
  789. } else if (kitem.kline.hd.cmd == KLN_CMD_IDENT && kitem.kline.hd.dev == 0) {
  790. display_kline(klncgpu, &kitem.kline, msg_detect_reply);
  791. applog(LOG_DEBUG, "%s (%s) detect successful (%d attempt%s)",
  792. klncgpu->drv->dname,
  793. klncgpu->device_path,
  794. attempts, attempts == 1 ? "" : "s");
  795. if (!add_cgpu(klncgpu))
  796. break;
  797. applog(LOG_DEBUG, "Klondike cgpu added");
  798. rwlock_init(&klninfo->stat_lock);
  799. cglock_init(&klninfo->klist_lock);
  800. return true;
  801. }
  802. }
  803. usb_uninit(klncgpu);
  804. }
  805. free(klninfo->free);
  806. free(klninfo);
  807. free(klncgpu);
  808. return false;
  809. }
  810. static void klondike_check_nonce(struct cgpu_info *klncgpu, KLIST *kitem)
  811. {
  812. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  813. struct work *work, *look, *tmp;
  814. KLINE *kline = &(kitem->kline);
  815. struct timeval tv_now;
  816. double us_diff;
  817. uint32_t nonce = K_NONCE(kline->wr.nonce) + klninfo->nonce_offset;
  818. applog(LOG_DEBUG, "%s%i:%d FOUND NONCE (%02x:%08x)",
  819. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  820. kline->wr.workid, (unsigned int)nonce);
  821. work = NULL;
  822. cgtime(&tv_now);
  823. rd_lock(&(klncgpu->qlock));
  824. HASH_ITER(hh, klncgpu->queued_work, look, tmp) {
  825. if (ms_tdiff(&tv_now, &(look->tv_stamp)) < OLD_WORK_MS &&
  826. (look->subid == (kline->wr.dev*256 + kline->wr.workid))) {
  827. work = look;
  828. break;
  829. }
  830. }
  831. rd_unlock(&(klncgpu->qlock));
  832. if (work) {
  833. if (unlikely(!klninfo->nonce_offset))
  834. {
  835. bool test_c0 = test_nonce(work, nonce - 0xc0, false);
  836. bool test_180 = test_nonce(work, nonce - 0x180, false);
  837. if (test_c0)
  838. {
  839. if (unlikely(test_180))
  840. {
  841. applog(LOG_DEBUG, "%s: Matched both c0 and 180 offsets (%02x:%08lx)",
  842. klncgpu->dev_repr, kline->wr.workid, (unsigned long)nonce);
  843. submit_nonce(klncgpu->thr[0], work, nonce - 0x180);
  844. nonce -= 0xc0;
  845. }
  846. else
  847. {
  848. applog(LOG_DEBUG, "%s: Matched c0 offset (%02x:%08lx)",
  849. klncgpu->dev_repr, kline->wr.workid, (unsigned long)nonce);
  850. nonce += (klninfo->nonce_offset = -0xc0);
  851. }
  852. }
  853. else
  854. if (test_180)
  855. {
  856. applog(LOG_DEBUG, "%s: Matched 180 offset (%02x:%08lx)",
  857. klncgpu->dev_repr, kline->wr.workid, (unsigned long)nonce);
  858. nonce += (klninfo->nonce_offset = -0x180);
  859. }
  860. else
  861. applog(LOG_DEBUG, "%s: Matched neither c0 nor 180 offset (%02x:%08lx)",
  862. klncgpu->dev_repr, kline->wr.workid, (unsigned long)nonce);
  863. }
  864. wr_lock(&(klninfo->stat_lock));
  865. klninfo->devinfo[kline->wr.dev].noncecount++;
  866. klninfo->noncecount++;
  867. wr_unlock(&(klninfo->stat_lock));
  868. applog(LOG_DEBUG, "%s%i:%d SUBMIT NONCE (%02x:%08x)",
  869. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  870. kline->wr.workid, (unsigned int)nonce);
  871. cgtime(&tv_now);
  872. bool ok = submit_nonce(klncgpu->thr[0], work, nonce);
  873. applog(LOG_DEBUG, "%s%i:%d chip stats %d, %08x, %d, %d",
  874. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  875. kline->wr.dev, (unsigned int)nonce,
  876. klninfo->devinfo[kline->wr.dev].rangesize,
  877. klninfo->status[kline->wr.dev].kline.ws.chipcount);
  878. klninfo->devinfo[kline->wr.dev].chipstats[(nonce / klninfo->devinfo[kline->wr.dev].rangesize) + (ok ? 0 : klninfo->status[kline->wr.dev].kline.ws.chipcount)]++;
  879. us_diff = us_tdiff(&tv_now, &(kitem->tv_when));
  880. if (klninfo->delay_count == 0) {
  881. klninfo->delay_min = us_diff;
  882. klninfo->delay_max = us_diff;
  883. } else {
  884. if (klninfo->delay_min > us_diff)
  885. klninfo->delay_min = us_diff;
  886. if (klninfo->delay_max < us_diff)
  887. klninfo->delay_max = us_diff;
  888. }
  889. klninfo->delay_count++;
  890. klninfo->delay_total += us_diff;
  891. if (klninfo->nonce_count > 0) {
  892. us_diff = us_tdiff(&(kitem->tv_when), &(klninfo->tv_last_nonce_received));
  893. if (klninfo->nonce_count == 1) {
  894. klninfo->nonce_min = us_diff;
  895. klninfo->nonce_max = us_diff;
  896. } else {
  897. if (klninfo->nonce_min > us_diff)
  898. klninfo->nonce_min = us_diff;
  899. if (klninfo->nonce_max < us_diff)
  900. klninfo->nonce_max = us_diff;
  901. }
  902. klninfo->nonce_total += us_diff;
  903. }
  904. klninfo->nonce_count++;
  905. memcpy(&(klninfo->tv_last_nonce_received), &(kitem->tv_when),
  906. sizeof(klninfo->tv_last_nonce_received));
  907. return;
  908. }
  909. applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored",
  910. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  911. kline->wr.workid, (unsigned int)nonce);
  912. //inc_hw_errors(klncgpu->thr[0]);
  913. }
  914. // thread to keep looking for replies
  915. static void *klondike_get_replies(void *userdata)
  916. {
  917. struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
  918. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  919. KLIST *kitem = NULL;
  920. int err, recd, slaves, dev, isc;
  921. bool overheat, sent;
  922. applog(LOG_DEBUG, "%s%i: listening for replies",
  923. klncgpu->drv->name, klncgpu->device_id);
  924. while (klncgpu->shutdown == false) {
  925. if (klninfo->usbinfo_nodev)
  926. return NULL;
  927. if (kitem == NULL)
  928. kitem = allocate_kitem(klncgpu);
  929. else
  930. memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
  931. err = usb_read(klncgpu, &kitem->kline, REPLY_SIZE, &recd);
  932. if (err || recd != REPLY_SIZE) {
  933. if (err != -7)
  934. applog(LOG_ERR, "%s%i: reply err=%d amt=%d",
  935. klncgpu->drv->name, klncgpu->device_id,
  936. err, recd);
  937. }
  938. if (!err && recd == REPLY_SIZE) {
  939. cgtime(&(kitem->tv_when));
  940. rd_lock(&(klninfo->stat_lock));
  941. kitem->block_seq = klninfo->block_seq;
  942. rd_unlock(&(klninfo->stat_lock));
  943. if (opt_log_level <= READ_DEBUG) {
  944. char hexdata[recd * 2];
  945. bin2hex(hexdata, &kitem->kline.hd.dev, recd-1);
  946. applog(READ_DEBUG, "%s%i:%d reply [%c:%s]",
  947. klncgpu->drv->name, klncgpu->device_id,
  948. (int)(kitem->kline.hd.dev),
  949. kitem->kline.hd.cmd, hexdata);
  950. }
  951. // We can't check this until it's initialised
  952. if (klninfo->initialised) {
  953. rd_lock(&(klninfo->stat_lock));
  954. slaves = klninfo->status[0].kline.ws.slavecount;
  955. rd_unlock(&(klninfo->stat_lock));
  956. if (kitem->kline.hd.dev > slaves) {
  957. applog(LOG_ERR, "%s%i: reply [%c] has invalid dev=%d (max=%d) using 0",
  958. klncgpu->drv->name, klncgpu->device_id,
  959. (char)(kitem->kline.hd.cmd),
  960. (int)(kitem->kline.hd.dev),
  961. slaves);
  962. /* TODO: this is rather problematic if there are slaves
  963. * however without slaves - it should always be zero */
  964. kitem->kline.hd.dev = 0;
  965. } else {
  966. wr_lock(&(klninfo->stat_lock));
  967. klninfo->jobque[kitem->kline.hd.dev].late_update_sequential = 0;
  968. wr_unlock(&(klninfo->stat_lock));
  969. }
  970. }
  971. switch (kitem->kline.hd.cmd) {
  972. case KLN_CMD_NONCE:
  973. klondike_check_nonce(klncgpu, kitem);
  974. display_kline(klncgpu, &kitem->kline, msg_reply);
  975. break;
  976. case KLN_CMD_WORK:
  977. // We can't do/check this until it's initialised
  978. if (klninfo->initialised) {
  979. dev = kitem->kline.ws.dev;
  980. if (kitem->kline.ws.workqc == 0) {
  981. bool idle = false;
  982. rd_lock(&(klninfo->stat_lock));
  983. if (klninfo->jobque[dev].flushed == false)
  984. idle = true;
  985. slaves = klninfo->status[0].kline.ws.slavecount;
  986. rd_unlock(&(klninfo->stat_lock));
  987. if (idle)
  988. applog(LOG_WARNING, "%s%i:%d went idle before work was sent",
  989. klncgpu->drv->name,
  990. klncgpu->device_id,
  991. dev);
  992. }
  993. wr_lock(&(klninfo->stat_lock));
  994. klninfo->jobque[dev].flushed = false;
  995. wr_unlock(&(klninfo->stat_lock));
  996. }
  997. case KLN_CMD_STATUS:
  998. case KLN_CMD_ABORT:
  999. // We can't do/check this until it's initialised
  1000. if (klninfo->initialised) {
  1001. isc = 0;
  1002. dev = kitem->kline.ws.dev;
  1003. wr_lock(&(klninfo->stat_lock));
  1004. klninfo->jobque[dev].workqc = (int)(kitem->kline.ws.workqc);
  1005. cgtime(&(klninfo->jobque[dev].last_update));
  1006. slaves = klninfo->status[0].kline.ws.slavecount;
  1007. overheat = klninfo->jobque[dev].overheat;
  1008. if (dev == 0) {
  1009. if (kitem->kline.ws.slavecount != slaves)
  1010. isc = ++klninfo->incorrect_slave_sequential;
  1011. else
  1012. isc = klninfo->incorrect_slave_sequential = 0;
  1013. }
  1014. wr_unlock(&(klninfo->stat_lock));
  1015. if (isc) {
  1016. applog(LOG_ERR, "%s%i:%d reply [%c] has a diff"
  1017. " # of slaves=%d (curr=%d)%s",
  1018. klncgpu->drv->name,
  1019. klncgpu->device_id,
  1020. dev,
  1021. (char)(kitem->kline.ws.cmd),
  1022. (int)(kitem->kline.ws.slavecount),
  1023. slaves,
  1024. isc <= KLN_ISS_IGNORE ? "" :
  1025. " disabling device");
  1026. if (isc > KLN_ISS_IGNORE)
  1027. usb_nodev(klncgpu);
  1028. break;
  1029. }
  1030. if (!overheat) {
  1031. double temp = cvtKlnToC(kitem->kline.ws.temp);
  1032. if (temp >= KLN_KILLWORK_TEMP) {
  1033. KLINE kline;
  1034. wr_lock(&(klninfo->stat_lock));
  1035. klninfo->jobque[dev].overheat = true;
  1036. wr_unlock(&(klninfo->stat_lock));
  1037. applog(LOG_WARNING, "%s%i:%d Critical overheat (%.0fC)",
  1038. klncgpu->drv->name,
  1039. klncgpu->device_id,
  1040. dev, temp);
  1041. zero_kline(&kline);
  1042. kline.hd.cmd = KLN_CMD_ABORT;
  1043. kline.hd.dev = dev;
  1044. sent = SendCmd(klncgpu, &kline, KSENDHD(0));
  1045. kln_disable(klncgpu, dev, false);
  1046. if (!sent) {
  1047. applog(LOG_ERR, "%s%i:%d overheat failed to"
  1048. " abort work - disabling device",
  1049. klncgpu->drv->name,
  1050. klncgpu->device_id,
  1051. dev);
  1052. usb_nodev(klncgpu);
  1053. }
  1054. }
  1055. }
  1056. }
  1057. case KLN_CMD_ENABLE:
  1058. wr_lock(&(klninfo->stat_lock));
  1059. klninfo->errorcount += kitem->kline.ws.errorcount;
  1060. klninfo->noisecount += kitem->kline.ws.noise;
  1061. wr_unlock(&(klninfo->stat_lock));
  1062. display_kline(klncgpu, &kitem->kline, msg_reply);
  1063. kitem->ready = true;
  1064. kitem = NULL;
  1065. break;
  1066. case KLN_CMD_CONFIG:
  1067. display_kline(klncgpu, &kitem->kline, msg_reply);
  1068. kitem->ready = true;
  1069. kitem = NULL;
  1070. break;
  1071. case KLN_CMD_IDENT:
  1072. display_kline(klncgpu, &kitem->kline, msg_reply);
  1073. kitem->ready = true;
  1074. kitem = NULL;
  1075. break;
  1076. default:
  1077. display_kline(klncgpu, &kitem->kline, msg_reply);
  1078. break;
  1079. }
  1080. }
  1081. }
  1082. return NULL;
  1083. }
  1084. static void klondike_flush_work(struct cgpu_info *klncgpu)
  1085. {
  1086. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1087. KLIST *kitem;
  1088. KLINE kline;
  1089. int slaves, dev;
  1090. if (klninfo->initialised) {
  1091. wr_lock(&(klninfo->stat_lock));
  1092. klninfo->block_seq++;
  1093. slaves = klninfo->status[0].kline.ws.slavecount;
  1094. wr_unlock(&(klninfo->stat_lock));
  1095. applog(LOG_DEBUG, "%s%i: flushing work",
  1096. klncgpu->drv->name, klncgpu->device_id);
  1097. zero_kline(&kline);
  1098. kline.hd.cmd = KLN_CMD_ABORT;
  1099. for (dev = 0; dev <= slaves; dev++) {
  1100. kline.hd.dev = dev;
  1101. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  1102. if (kitem != NULL) {
  1103. wr_lock(&(klninfo->stat_lock));
  1104. memcpy((void *)&(klninfo->status[dev]),
  1105. kitem,
  1106. sizeof(klninfo->status[dev]));
  1107. klninfo->jobque[dev].flushed = true;
  1108. wr_unlock(&(klninfo->stat_lock));
  1109. kitem = release_kitem(klncgpu, kitem);
  1110. }
  1111. }
  1112. }
  1113. }
  1114. static bool klondike_thread_prepare(struct thr_info *thr)
  1115. {
  1116. struct cgpu_info *klncgpu = thr->cgpu;
  1117. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1118. if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) {
  1119. applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id);
  1120. return false;
  1121. }
  1122. pthread_detach(klninfo->replies_thr.pth);
  1123. // let the listening get started
  1124. cgsleep_ms(100);
  1125. return klondike_init(klncgpu);
  1126. }
  1127. static bool klondike_thread_init(struct thr_info *thr)
  1128. {
  1129. struct cgpu_info *klncgpu = thr->cgpu;
  1130. struct klondike_info * const klninfo = klncgpu->device_data;
  1131. notifier_init(thr->work_restart_notifier);
  1132. if (klninfo->usbinfo_nodev)
  1133. return false;
  1134. klondike_flush_work(klncgpu);
  1135. return true;
  1136. }
  1137. static void klondike_shutdown(struct thr_info *thr)
  1138. {
  1139. struct cgpu_info *klncgpu = thr->cgpu;
  1140. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1141. applog(LOG_DEBUG, "%s%i: shutting down work",
  1142. klncgpu->drv->name, klncgpu->device_id);
  1143. kln_disable(klncgpu, klninfo->status[0].kline.ws.slavecount, true);
  1144. klncgpu->shutdown = true;
  1145. }
  1146. static void klondike_thread_enable(struct thr_info *thr)
  1147. {
  1148. struct cgpu_info *klncgpu = thr->cgpu;
  1149. struct klondike_info * const klninfo = klncgpu->device_data;
  1150. if (klninfo->usbinfo_nodev)
  1151. return;
  1152. /*
  1153. KLINE kline;
  1154. zero_kline(&kline);
  1155. kline.hd.cmd = KLN_CMD_ENABLE;
  1156. kline.hd.dev = dev;
  1157. kline.hd.buf[0] = KLN_CMD_ENABLE_OFF;
  1158. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
  1159. */
  1160. }
  1161. static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
  1162. {
  1163. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1164. struct work *look, *tmp;
  1165. KLINE kline;
  1166. struct timeval tv_old;
  1167. int wque_size, wque_cleared;
  1168. if (klninfo->usbinfo_nodev)
  1169. return false;
  1170. zero_kline(&kline);
  1171. kline.wt.cmd = KLN_CMD_WORK;
  1172. kline.wt.dev = dev;
  1173. memcpy(kline.wt.midstate, work->midstate, MIDSTATE_BYTES);
  1174. memcpy(kline.wt.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  1175. kline.wt.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
  1176. work->subid = dev*256 + kline.wt.workid;
  1177. cgtime(&work->tv_stamp);
  1178. if (opt_log_level <= LOG_DEBUG) {
  1179. char hexdata[(sizeof(kline.wt) * 2) + 1];
  1180. bin2hex(hexdata, &kline.wt, sizeof(kline.wt));
  1181. applog(LOG_DEBUG, "WORKDATA: %s", hexdata);
  1182. }
  1183. applog(LOG_DEBUG, "%s%i:%d sending work (%d:%02x)",
  1184. klncgpu->drv->name, klncgpu->device_id, dev,
  1185. dev, kline.wt.workid);
  1186. KLIST *kitem = SendCmdGetReply(klncgpu, &kline, sizeof(kline.wt));
  1187. if (kitem != NULL) {
  1188. wr_lock(&(klninfo->stat_lock));
  1189. memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(klninfo->status[dev]));
  1190. wr_unlock(&(klninfo->stat_lock));
  1191. kitem = release_kitem(klncgpu, kitem);
  1192. // remove old work
  1193. wque_size = 0;
  1194. wque_cleared = 0;
  1195. cgtime(&tv_old);
  1196. wr_lock(&klncgpu->qlock);
  1197. HASH_ITER(hh, klncgpu->queued_work, look, tmp) {
  1198. if (ms_tdiff(&tv_old, &(look->tv_stamp)) > OLD_WORK_MS) {
  1199. __work_completed(klncgpu, look);
  1200. free_work(look);
  1201. wque_cleared++;
  1202. } else
  1203. wque_size++;
  1204. }
  1205. wr_unlock(&klncgpu->qlock);
  1206. wr_lock(&(klninfo->stat_lock));
  1207. klninfo->wque_size = wque_size;
  1208. klninfo->wque_cleared = wque_cleared;
  1209. wr_unlock(&(klninfo->stat_lock));
  1210. return true;
  1211. }
  1212. return false;
  1213. }
  1214. static bool klondike_queue_full(struct cgpu_info *klncgpu)
  1215. {
  1216. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1217. struct work *work = NULL;
  1218. int dev, queued, slaves, seq, howlong;
  1219. struct timeval now;
  1220. bool nowork;
  1221. if (klncgpu->shutdown == true)
  1222. return true;
  1223. cgtime(&now);
  1224. rd_lock(&(klninfo->stat_lock));
  1225. slaves = klninfo->status[0].kline.ws.slavecount;
  1226. for (dev = 0; dev <= slaves; dev++)
  1227. if (ms_tdiff(&now, &(klninfo->jobque[dev].last_update)) > LATE_UPDATE_MS) {
  1228. klninfo->jobque[dev].late_update_count++;
  1229. seq = ++klninfo->jobque[dev].late_update_sequential;
  1230. rd_unlock(&(klninfo->stat_lock));
  1231. if (seq < LATE_UPDATE_LIMIT) {
  1232. applog(LOG_DEBUG, "%s%i:%d late update",
  1233. klncgpu->drv->name, klncgpu->device_id, dev);
  1234. klondike_get_stats(klncgpu);
  1235. goto que;
  1236. } else {
  1237. applog(LOG_WARNING, "%s%i:%d late update (%d) reached - attempting reset",
  1238. klncgpu->drv->name, klncgpu->device_id,
  1239. dev, LATE_UPDATE_LIMIT);
  1240. control_init(klncgpu);
  1241. kln_enable(klncgpu);
  1242. klondike_get_stats(klncgpu);
  1243. rd_lock(&(klninfo->stat_lock));
  1244. howlong = ms_tdiff(&now, &(klninfo->jobque[dev].last_update));
  1245. if (howlong > LATE_UPDATE_MS) {
  1246. rd_unlock(&(klninfo->stat_lock));
  1247. if (howlong > LATE_UPDATE_NODEV_MS) {
  1248. applog(LOG_ERR, "%s%i:%d reset failed - dropping device",
  1249. klncgpu->drv->name, klncgpu->device_id, dev);
  1250. usb_nodev(klncgpu);
  1251. } else
  1252. cgsleep_ms(LATE_UPDATE_SLEEP_MS);
  1253. return true;
  1254. }
  1255. break;
  1256. }
  1257. }
  1258. rd_unlock(&(klninfo->stat_lock));
  1259. que:
  1260. nowork = true;
  1261. for (queued = 0; queued < MAX_WORK_COUNT-1; queued++)
  1262. for (dev = 0; dev <= slaves; dev++) {
  1263. tryagain:
  1264. rd_lock(&(klninfo->stat_lock));
  1265. if (klninfo->jobque[dev].overheat) {
  1266. double temp = cvtKlnToC(klninfo->status[0].kline.ws.temp);
  1267. if ((queued == MAX_WORK_COUNT-2) &&
  1268. ms_tdiff(&now, &(klninfo->jobque[dev].last_update)) > (LATE_UPDATE_MS/2)) {
  1269. rd_unlock(&(klninfo->stat_lock));
  1270. klondike_get_stats(klncgpu);
  1271. goto tryagain;
  1272. }
  1273. if (temp <= KLN_COOLED_DOWN) {
  1274. klninfo->jobque[dev].overheat = false;
  1275. rd_unlock(&(klninfo->stat_lock));
  1276. applog(LOG_WARNING, "%s%i:%d Overheat recovered (%.0fC)",
  1277. klncgpu->drv->name, klncgpu->device_id,
  1278. dev, temp);
  1279. kln_enable(klncgpu);
  1280. goto tryagain;
  1281. } else {
  1282. rd_unlock(&(klninfo->stat_lock));
  1283. continue;
  1284. }
  1285. }
  1286. if (klninfo->jobque[dev].workqc <= queued) {
  1287. rd_unlock(&(klninfo->stat_lock));
  1288. if (!work)
  1289. work = get_queued(klncgpu);
  1290. if (unlikely(!work))
  1291. return false;
  1292. nowork = false;
  1293. if (klondike_send_work(klncgpu, dev, work))
  1294. return false;
  1295. } else
  1296. rd_unlock(&(klninfo->stat_lock));
  1297. }
  1298. if (nowork)
  1299. cgsleep_ms(10); // avoid a hard loop in case we have nothing to do
  1300. return true;
  1301. }
  1302. static int64_t klondike_scanwork(struct thr_info *thr)
  1303. {
  1304. struct cgpu_info *klncgpu = thr->cgpu;
  1305. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1306. int64_t newhashcount = 0;
  1307. int dev, slaves;
  1308. if (klninfo->usbinfo_nodev)
  1309. return -1;
  1310. restart_wait(thr, 200);
  1311. if (klninfo->status != NULL) {
  1312. rd_lock(&(klninfo->stat_lock));
  1313. slaves = klninfo->status[0].kline.ws.slavecount;
  1314. for (dev = 0; dev <= slaves; dev++) {
  1315. uint64_t newhashdev = 0, hashcount;
  1316. int maxcount;
  1317. hashcount = K_HASHCOUNT(klninfo->status[dev].kline.ws.hashcount);
  1318. maxcount = K_MAXCOUNT(klninfo->status[dev].kline.ws.maxcount);
  1319. // todo: chg this to check workid for wrapped instead
  1320. if (klninfo->devinfo[dev].lasthashcount > hashcount)
  1321. newhashdev += maxcount; // hash counter wrapped
  1322. newhashdev += hashcount - klninfo->devinfo[dev].lasthashcount;
  1323. klninfo->devinfo[dev].lasthashcount = hashcount;
  1324. if (maxcount != 0)
  1325. klninfo->hashcount += (newhashdev << 32) / maxcount;
  1326. }
  1327. newhashcount += 0xffffffffull * (uint64_t)klninfo->noncecount;
  1328. klninfo->noncecount = 0;
  1329. rd_unlock(&(klninfo->stat_lock));
  1330. }
  1331. return newhashcount;
  1332. }
  1333. #ifdef HAVE_CURSES
  1334. static
  1335. void klondike_wlogprint_status(struct cgpu_info *klncgpu)
  1336. {
  1337. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1338. uint16_t fan = 0;
  1339. uint16_t clock = 0;
  1340. int dev, slaves;
  1341. if (klninfo->status == NULL) {
  1342. return;
  1343. }
  1344. rd_lock(&(klninfo->stat_lock));
  1345. slaves = klninfo->status[0].kline.ws.slavecount;
  1346. for (dev = 0; dev <= slaves; dev++) {
  1347. fan += klninfo->cfg[dev].kline.cfg.fantarget;
  1348. clock += (uint16_t)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  1349. }
  1350. rd_unlock(&(klninfo->stat_lock));
  1351. fan /= slaves + 1;
  1352. fan = 100 * fan / 255;
  1353. clock /= slaves + 1;
  1354. wlogprint("Frequency: %d MHz\n", (int)clock);
  1355. if (fan && fan <= 100)
  1356. wlogprint("Fan speed: %d%%\n", fan);
  1357. }
  1358. #endif
  1359. static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
  1360. {
  1361. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1362. struct api_data *root = NULL;
  1363. char buf[32];
  1364. int dev, slaves;
  1365. if (klninfo->status == NULL)
  1366. return NULL;
  1367. rd_lock(&(klninfo->stat_lock));
  1368. slaves = klninfo->status[0].kline.ws.slavecount;
  1369. for (dev = 0; dev <= slaves; dev++) {
  1370. float fTemp = cvtKlnToC(klninfo->status[dev].kline.ws.temp);
  1371. sprintf(buf, "Temp %d", dev);
  1372. root = api_add_temp(root, buf, &fTemp, true);
  1373. double dClk = (double)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  1374. sprintf(buf, "Clock %d", dev);
  1375. root = api_add_freq(root, buf, &dClk, true);
  1376. unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].kline.cfg.fantarget / 255;
  1377. sprintf(buf, "Fan Percent %d", dev);
  1378. root = api_add_int(root, buf, (int *)(&iFan), true);
  1379. iFan = 0;
  1380. if (klninfo->status[dev].kline.ws.fanspeed > 0)
  1381. iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].kline.ws.fanspeed;
  1382. sprintf(buf, "Fan RPM %d", dev);
  1383. root = api_add_int(root, buf, (int *)(&iFan), true);
  1384. if (klninfo->devinfo[dev].chipstats != NULL) {
  1385. char data[2048];
  1386. char one[32];
  1387. int n;
  1388. sprintf(buf, "Nonces / Chip %d", dev);
  1389. data[0] = '\0';
  1390. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  1391. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n]);
  1392. strcat(data, one);
  1393. }
  1394. root = api_add_string(root, buf, data, true);
  1395. sprintf(buf, "Errors / Chip %d", dev);
  1396. data[0] = '\0';
  1397. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  1398. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].kline.ws.chipcount]);
  1399. strcat(data, one);
  1400. }
  1401. root = api_add_string(root, buf, data, true);
  1402. }
  1403. }
  1404. root = api_add_uint64(root, "Hash Count", &(klninfo->hashcount), true);
  1405. root = api_add_uint64(root, "Error Count", &(klninfo->errorcount), true);
  1406. root = api_add_uint64(root, "Noise Count", &(klninfo->noisecount), true);
  1407. root = api_add_int(root, "KLine Limit", &(klninfo->kline_count), true);
  1408. root = api_add_int(root, "KLine Used", &(klninfo->used_count), true);
  1409. root = api_add_elapsed(root, "KQue Delay Count", &(klninfo->delay_count), true);
  1410. root = api_add_elapsed(root, "KQue Delay Total", &(klninfo->delay_total), true);
  1411. root = api_add_elapsed(root, "KQue Delay Min", &(klninfo->delay_min), true);
  1412. root = api_add_elapsed(root, "KQue Delay Max", &(klninfo->delay_max), true);
  1413. double avg;
  1414. if (klninfo->delay_count == 0)
  1415. avg = 0;
  1416. else
  1417. avg = klninfo->delay_total / klninfo->delay_count;
  1418. root = api_add_diff(root, "KQue Delay Avg", &avg, true);
  1419. root = api_add_elapsed(root, "KQue Nonce Count", &(klninfo->nonce_count), true);
  1420. root = api_add_elapsed(root, "KQue Nonce Total", &(klninfo->nonce_total), true);
  1421. root = api_add_elapsed(root, "KQue Nonce Min", &(klninfo->nonce_min), true);
  1422. root = api_add_elapsed(root, "KQue Nonce Max", &(klninfo->nonce_max), true);
  1423. if (klninfo->nonce_count == 0)
  1424. avg = 0;
  1425. else
  1426. avg = klninfo->nonce_total / klninfo->nonce_count;
  1427. root = api_add_diff(root, "KQue Nonce Avg", &avg, true);
  1428. root = api_add_int(root, "WQue Size", &(klninfo->wque_size), true);
  1429. root = api_add_int(root, "WQue Cleared", &(klninfo->wque_cleared), true);
  1430. rd_unlock(&(klninfo->stat_lock));
  1431. return root;
  1432. }
  1433. struct device_drv klondike_drv = {
  1434. .dname = "Klondike",
  1435. .name = "KLN",
  1436. .lowl_match = klondike_lowl_match,
  1437. .lowl_probe = klondike_lowl_probe,
  1438. .get_api_stats = klondike_api_stats,
  1439. .get_stats = klondike_get_stats,
  1440. .thread_prepare = klondike_thread_prepare,
  1441. .thread_init = klondike_thread_init,
  1442. .minerloop = hash_queued_work,
  1443. .scanwork = klondike_scanwork,
  1444. .queue_full = klondike_queue_full,
  1445. .flush_work = klondike_flush_work,
  1446. .thread_shutdown = klondike_shutdown,
  1447. .thread_enable = klondike_thread_enable,
  1448. #ifdef HAVE_CURSES
  1449. .proc_wlogprint_status = klondike_wlogprint_status,
  1450. #endif
  1451. };