driver-klondike.c 44 KB

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