driver-klondike.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  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_foundlowl(struct lowlevel_device_info * const info, __maybe_unused void * const userp)
  708. {
  709. if (unlikely(info->lowl != &lowl_usb))
  710. {
  711. applog(LOG_WARNING, "%s: Matched \"%s\" serial \"%s\", but lowlevel driver is not usb!",
  712. __func__, info->product, info->serial);
  713. return false;
  714. }
  715. struct libusb_device * const dev = info->lowl_data;
  716. // static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  717. struct cgpu_info * const klncgpu = malloc(sizeof(*klncgpu));
  718. struct klondike_info *klninfo = NULL;
  719. KLINE kline;
  720. if (unlikely(!klncgpu))
  721. quit(1, "Failed to calloc klncgpu in klondike_detect_one");
  722. *klncgpu = (struct cgpu_info){
  723. .drv = &klondike_drv,
  724. .deven = DEV_ENABLED,
  725. .threads = 1,
  726. .cutofftemp = (int)KLN_KILLWORK_TEMP,
  727. };
  728. klninfo = calloc(1, sizeof(*klninfo));
  729. if (unlikely(!klninfo))
  730. quit(1, "Failed to calloc klninfo in klondke_detect_one");
  731. klncgpu->device_data = (void *)klninfo;
  732. klninfo->free = new_klist_set(klncgpu);
  733. if (usb_init(klncgpu, dev)) {
  734. int sent, recd, err;
  735. KLIST kitem;
  736. int attempts = 0;
  737. klncgpu->device_path = strdup(info->devid);
  738. control_init(klncgpu);
  739. while (attempts++ < 3) {
  740. kline.hd.cmd = KLN_CMD_IDENT;
  741. kline.hd.dev = 0;
  742. display_send_kline(klncgpu, &kline, msg_detect_send);
  743. err = usb_write(klncgpu, (char *)&(kline.hd), 2, &sent);
  744. if (err < 0 || sent != 2) {
  745. applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)",
  746. klncgpu->drv->dname,
  747. klncgpu->device_path,
  748. sent, err);
  749. }
  750. cgsleep_ms(REPLY_WAIT_TIME*10);
  751. err = usb_read(klncgpu, &kitem.kline, REPLY_SIZE, &recd);
  752. if (err < 0) {
  753. applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)",
  754. klncgpu->drv->dname,
  755. klncgpu->device_path,
  756. recd, err);
  757. } else if (recd < 1) {
  758. applog(LOG_ERR, "%s (%s) detect empty reply (%d)",
  759. klncgpu->drv->dname,
  760. klncgpu->device_path,
  761. recd);
  762. } else if (kitem.kline.hd.cmd == KLN_CMD_IDENT && kitem.kline.hd.dev == 0) {
  763. display_kline(klncgpu, &kitem.kline, msg_detect_reply);
  764. applog(LOG_DEBUG, "%s (%s) detect successful (%d attempt%s)",
  765. klncgpu->drv->dname,
  766. klncgpu->device_path,
  767. attempts, attempts == 1 ? "" : "s");
  768. if (!add_cgpu(klncgpu))
  769. break;
  770. applog(LOG_DEBUG, "Klondike cgpu added");
  771. rwlock_init(&klninfo->stat_lock);
  772. cglock_init(&klninfo->klist_lock);
  773. return true;
  774. }
  775. }
  776. usb_uninit(klncgpu);
  777. }
  778. free(klninfo->free);
  779. free(klninfo);
  780. free(klncgpu);
  781. return false;
  782. }
  783. static
  784. bool klondike_detect_one(const char *serial)
  785. {
  786. return lowlevel_detect_serial(klondike_foundlowl, serial);
  787. }
  788. static
  789. bool klondike_foundlowl_checkmanuf(struct lowlevel_device_info * const info, void * const userp)
  790. {
  791. if (info->manufacturer && strstr(info->manufacturer, "Klondike"))
  792. return klondike_foundlowl(info, userp);
  793. return false;
  794. }
  795. static
  796. int klondike_autodetect()
  797. {
  798. return lowlevel_detect_id(klondike_foundlowl_checkmanuf, NULL, &lowl_usb, 0x04d8, 0xf60a);
  799. }
  800. static
  801. void klondike_detect()
  802. {
  803. generic_detect(&klondike_drv, klondike_detect_one, klondike_autodetect, 0);
  804. }
  805. static void klondike_check_nonce(struct cgpu_info *klncgpu, KLIST *kitem)
  806. {
  807. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  808. struct work *work, *look, *tmp;
  809. KLINE *kline = &(kitem->kline);
  810. struct timeval tv_now;
  811. double us_diff;
  812. uint32_t nonce = K_NONCE(kline->wr.nonce) - 0xC0;
  813. applog(LOG_DEBUG, "%s%i:%d FOUND NONCE (%02x:%08x)",
  814. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  815. kline->wr.workid, (unsigned int)nonce);
  816. work = NULL;
  817. cgtime(&tv_now);
  818. rd_lock(&(klncgpu->qlock));
  819. HASH_ITER(hh, klncgpu->queued_work, look, tmp) {
  820. if (ms_tdiff(&tv_now, &(look->tv_stamp)) < OLD_WORK_MS &&
  821. (look->subid == (kline->wr.dev*256 + kline->wr.workid))) {
  822. work = look;
  823. break;
  824. }
  825. }
  826. rd_unlock(&(klncgpu->qlock));
  827. if (work) {
  828. wr_lock(&(klninfo->stat_lock));
  829. klninfo->devinfo[kline->wr.dev].noncecount++;
  830. klninfo->noncecount++;
  831. wr_unlock(&(klninfo->stat_lock));
  832. applog(LOG_DEBUG, "%s%i:%d SUBMIT NONCE (%02x:%08x)",
  833. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  834. kline->wr.workid, (unsigned int)nonce);
  835. cgtime(&tv_now);
  836. bool ok = submit_nonce(klncgpu->thr[0], work, nonce);
  837. applog(LOG_DEBUG, "%s%i:%d chip stats %d, %08x, %d, %d",
  838. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  839. kline->wr.dev, (unsigned int)nonce,
  840. klninfo->devinfo[kline->wr.dev].rangesize,
  841. klninfo->status[kline->wr.dev].kline.ws.chipcount);
  842. klninfo->devinfo[kline->wr.dev].chipstats[(nonce / klninfo->devinfo[kline->wr.dev].rangesize) + (ok ? 0 : klninfo->status[kline->wr.dev].kline.ws.chipcount)]++;
  843. us_diff = us_tdiff(&tv_now, &(kitem->tv_when));
  844. if (klninfo->delay_count == 0) {
  845. klninfo->delay_min = us_diff;
  846. klninfo->delay_max = us_diff;
  847. } else {
  848. if (klninfo->delay_min > us_diff)
  849. klninfo->delay_min = us_diff;
  850. if (klninfo->delay_max < us_diff)
  851. klninfo->delay_max = us_diff;
  852. }
  853. klninfo->delay_count++;
  854. klninfo->delay_total += us_diff;
  855. if (klninfo->nonce_count > 0) {
  856. us_diff = us_tdiff(&(kitem->tv_when), &(klninfo->tv_last_nonce_received));
  857. if (klninfo->nonce_count == 1) {
  858. klninfo->nonce_min = us_diff;
  859. klninfo->nonce_max = us_diff;
  860. } else {
  861. if (klninfo->nonce_min > us_diff)
  862. klninfo->nonce_min = us_diff;
  863. if (klninfo->nonce_max < us_diff)
  864. klninfo->nonce_max = us_diff;
  865. }
  866. klninfo->nonce_total += us_diff;
  867. }
  868. klninfo->nonce_count++;
  869. memcpy(&(klninfo->tv_last_nonce_received), &(kitem->tv_when),
  870. sizeof(klninfo->tv_last_nonce_received));
  871. return;
  872. }
  873. applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored",
  874. klncgpu->drv->name, klncgpu->device_id, (int)(kline->wr.dev),
  875. kline->wr.workid, (unsigned int)nonce);
  876. inc_hw_errors2(klncgpu->thr[0], NULL, &nonce);
  877. }
  878. // thread to keep looking for replies
  879. static void *klondike_get_replies(void *userdata)
  880. {
  881. struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
  882. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  883. KLIST *kitem = NULL;
  884. int err, recd, slaves, dev, isc;
  885. bool overheat, sent;
  886. applog(LOG_DEBUG, "%s%i: listening for replies",
  887. klncgpu->drv->name, klncgpu->device_id);
  888. while (klncgpu->shutdown == false) {
  889. if (klninfo->usbinfo_nodev)
  890. return NULL;
  891. if (kitem == NULL)
  892. kitem = allocate_kitem(klncgpu);
  893. else
  894. memset((void *)&(kitem->kline), 0, sizeof(kitem->kline));
  895. err = usb_read(klncgpu, &kitem->kline, REPLY_SIZE, &recd);
  896. if (err || recd != REPLY_SIZE) {
  897. if (err != -7)
  898. applog(LOG_ERR, "%s%i: reply err=%d amt=%d",
  899. klncgpu->drv->name, klncgpu->device_id,
  900. err, recd);
  901. }
  902. if (!err && recd == REPLY_SIZE) {
  903. cgtime(&(kitem->tv_when));
  904. rd_lock(&(klninfo->stat_lock));
  905. kitem->block_seq = klninfo->block_seq;
  906. rd_unlock(&(klninfo->stat_lock));
  907. if (opt_log_level <= READ_DEBUG) {
  908. char hexdata[recd * 2];
  909. bin2hex(hexdata, &kitem->kline.hd.dev, recd-1);
  910. applog(READ_DEBUG, "%s%i:%d reply [%c:%s]",
  911. klncgpu->drv->name, klncgpu->device_id,
  912. (int)(kitem->kline.hd.dev),
  913. kitem->kline.hd.cmd, hexdata);
  914. }
  915. // We can't check this until it's initialised
  916. if (klninfo->initialised) {
  917. rd_lock(&(klninfo->stat_lock));
  918. slaves = klninfo->status[0].kline.ws.slavecount;
  919. rd_unlock(&(klninfo->stat_lock));
  920. if (kitem->kline.hd.dev > slaves) {
  921. applog(LOG_ERR, "%s%i: reply [%c] has invalid dev=%d (max=%d) using 0",
  922. klncgpu->drv->name, klncgpu->device_id,
  923. (char)(kitem->kline.hd.cmd),
  924. (int)(kitem->kline.hd.dev),
  925. slaves);
  926. /* TODO: this is rather problematic if there are slaves
  927. * however without slaves - it should always be zero */
  928. kitem->kline.hd.dev = 0;
  929. } else {
  930. wr_lock(&(klninfo->stat_lock));
  931. klninfo->jobque[kitem->kline.hd.dev].late_update_sequential = 0;
  932. wr_unlock(&(klninfo->stat_lock));
  933. }
  934. }
  935. switch (kitem->kline.hd.cmd) {
  936. case KLN_CMD_NONCE:
  937. klondike_check_nonce(klncgpu, kitem);
  938. display_kline(klncgpu, &kitem->kline, msg_reply);
  939. break;
  940. case KLN_CMD_WORK:
  941. // We can't do/check this until it's initialised
  942. if (klninfo->initialised) {
  943. dev = kitem->kline.ws.dev;
  944. if (kitem->kline.ws.workqc == 0) {
  945. bool idle = false;
  946. rd_lock(&(klninfo->stat_lock));
  947. if (klninfo->jobque[dev].flushed == false)
  948. idle = true;
  949. slaves = klninfo->status[0].kline.ws.slavecount;
  950. rd_unlock(&(klninfo->stat_lock));
  951. if (idle)
  952. applog(LOG_WARNING, "%s%i:%d went idle before work was sent",
  953. klncgpu->drv->name,
  954. klncgpu->device_id,
  955. dev);
  956. }
  957. wr_lock(&(klninfo->stat_lock));
  958. klninfo->jobque[dev].flushed = false;
  959. wr_unlock(&(klninfo->stat_lock));
  960. }
  961. case KLN_CMD_STATUS:
  962. case KLN_CMD_ABORT:
  963. // We can't do/check this until it's initialised
  964. if (klninfo->initialised) {
  965. isc = 0;
  966. dev = kitem->kline.ws.dev;
  967. wr_lock(&(klninfo->stat_lock));
  968. klninfo->jobque[dev].workqc = (int)(kitem->kline.ws.workqc);
  969. cgtime(&(klninfo->jobque[dev].last_update));
  970. slaves = klninfo->status[0].kline.ws.slavecount;
  971. overheat = klninfo->jobque[dev].overheat;
  972. if (dev == 0) {
  973. if (kitem->kline.ws.slavecount != slaves)
  974. isc = ++klninfo->incorrect_slave_sequential;
  975. else
  976. isc = klninfo->incorrect_slave_sequential = 0;
  977. }
  978. wr_unlock(&(klninfo->stat_lock));
  979. if (isc) {
  980. applog(LOG_ERR, "%s%i:%d reply [%c] has a diff"
  981. " # of slaves=%d (curr=%d)%s",
  982. klncgpu->drv->name,
  983. klncgpu->device_id,
  984. dev,
  985. (char)(kitem->kline.ws.cmd),
  986. (int)(kitem->kline.ws.slavecount),
  987. slaves,
  988. isc <= KLN_ISS_IGNORE ? "" :
  989. " disabling device");
  990. if (isc > KLN_ISS_IGNORE)
  991. usb_nodev(klncgpu);
  992. break;
  993. }
  994. if (!overheat) {
  995. double temp = cvtKlnToC(kitem->kline.ws.temp);
  996. if (temp >= KLN_KILLWORK_TEMP) {
  997. KLINE kline;
  998. wr_lock(&(klninfo->stat_lock));
  999. klninfo->jobque[dev].overheat = true;
  1000. wr_unlock(&(klninfo->stat_lock));
  1001. applog(LOG_WARNING, "%s%i:%d Critical overheat (%.0fC)",
  1002. klncgpu->drv->name,
  1003. klncgpu->device_id,
  1004. dev, temp);
  1005. zero_kline(&kline);
  1006. kline.hd.cmd = KLN_CMD_ABORT;
  1007. kline.hd.dev = dev;
  1008. sent = SendCmd(klncgpu, &kline, KSENDHD(0));
  1009. kln_disable(klncgpu, dev, false);
  1010. if (!sent) {
  1011. applog(LOG_ERR, "%s%i:%d overheat failed to"
  1012. " abort work - disabling device",
  1013. klncgpu->drv->name,
  1014. klncgpu->device_id,
  1015. dev);
  1016. usb_nodev(klncgpu);
  1017. }
  1018. }
  1019. }
  1020. }
  1021. case KLN_CMD_ENABLE:
  1022. wr_lock(&(klninfo->stat_lock));
  1023. klninfo->errorcount += kitem->kline.ws.errorcount;
  1024. klninfo->noisecount += kitem->kline.ws.noise;
  1025. wr_unlock(&(klninfo->stat_lock));
  1026. display_kline(klncgpu, &kitem->kline, msg_reply);
  1027. kitem->ready = true;
  1028. kitem = NULL;
  1029. break;
  1030. case KLN_CMD_CONFIG:
  1031. display_kline(klncgpu, &kitem->kline, msg_reply);
  1032. kitem->ready = true;
  1033. kitem = NULL;
  1034. break;
  1035. case KLN_CMD_IDENT:
  1036. display_kline(klncgpu, &kitem->kline, msg_reply);
  1037. kitem->ready = true;
  1038. kitem = NULL;
  1039. break;
  1040. default:
  1041. display_kline(klncgpu, &kitem->kline, msg_reply);
  1042. break;
  1043. }
  1044. }
  1045. }
  1046. return NULL;
  1047. }
  1048. static void klondike_flush_work(struct cgpu_info *klncgpu)
  1049. {
  1050. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1051. KLIST *kitem;
  1052. KLINE kline;
  1053. int slaves, dev;
  1054. wr_lock(&(klninfo->stat_lock));
  1055. klninfo->block_seq++;
  1056. slaves = klninfo->status[0].kline.ws.slavecount;
  1057. wr_unlock(&(klninfo->stat_lock));
  1058. applog(LOG_DEBUG, "%s%i: flushing work",
  1059. klncgpu->drv->name, klncgpu->device_id);
  1060. zero_kline(&kline);
  1061. kline.hd.cmd = KLN_CMD_ABORT;
  1062. for (dev = 0; dev <= slaves; dev++) {
  1063. kline.hd.dev = dev;
  1064. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(0));
  1065. if (kitem != NULL) {
  1066. wr_lock(&(klninfo->stat_lock));
  1067. memcpy((void *)&(klninfo->status[dev]),
  1068. kitem,
  1069. sizeof(klninfo->status[dev]));
  1070. klninfo->jobque[dev].flushed = true;
  1071. wr_unlock(&(klninfo->stat_lock));
  1072. kitem = release_kitem(klncgpu, kitem);
  1073. }
  1074. }
  1075. }
  1076. static bool klondike_thread_prepare(struct thr_info *thr)
  1077. {
  1078. struct cgpu_info *klncgpu = thr->cgpu;
  1079. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1080. if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) {
  1081. applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id);
  1082. return false;
  1083. }
  1084. pthread_detach(klninfo->replies_thr.pth);
  1085. // let the listening get started
  1086. cgsleep_ms(100);
  1087. return klondike_init(klncgpu);
  1088. }
  1089. static bool klondike_thread_init(struct thr_info *thr)
  1090. {
  1091. struct cgpu_info *klncgpu = thr->cgpu;
  1092. struct klondike_info * const klninfo = klncgpu->device_data;
  1093. notifier_init(thr->work_restart_notifier);
  1094. if (klninfo->usbinfo_nodev)
  1095. return false;
  1096. klondike_flush_work(klncgpu);
  1097. return true;
  1098. }
  1099. static void klondike_shutdown(struct thr_info *thr)
  1100. {
  1101. struct cgpu_info *klncgpu = thr->cgpu;
  1102. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1103. applog(LOG_DEBUG, "%s%i: shutting down work",
  1104. klncgpu->drv->name, klncgpu->device_id);
  1105. kln_disable(klncgpu, klninfo->status[0].kline.ws.slavecount, true);
  1106. klncgpu->shutdown = true;
  1107. }
  1108. static void klondike_thread_enable(struct thr_info *thr)
  1109. {
  1110. struct cgpu_info *klncgpu = thr->cgpu;
  1111. struct klondike_info * const klninfo = klncgpu->device_data;
  1112. if (klninfo->usbinfo_nodev)
  1113. return;
  1114. /*
  1115. KLINE kline;
  1116. zero_kline(&kline);
  1117. kline.hd.cmd = KLN_CMD_ENABLE;
  1118. kline.hd.dev = dev;
  1119. kline.hd.buf[0] = KLN_CMD_ENABLE_OFF;
  1120. kitem = SendCmdGetReply(klncgpu, &kline, KSENDHD(1));
  1121. */
  1122. }
  1123. static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
  1124. {
  1125. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1126. struct work *look, *tmp;
  1127. KLINE kline;
  1128. struct timeval tv_old;
  1129. int wque_size, wque_cleared;
  1130. if (klninfo->usbinfo_nodev)
  1131. return false;
  1132. zero_kline(&kline);
  1133. kline.wt.cmd = KLN_CMD_WORK;
  1134. kline.wt.dev = dev;
  1135. memcpy(kline.wt.midstate, work->midstate, MIDSTATE_BYTES);
  1136. memcpy(kline.wt.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  1137. kline.wt.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
  1138. work->subid = dev*256 + kline.wt.workid;
  1139. cgtime(&work->tv_stamp);
  1140. if (opt_log_level <= LOG_DEBUG) {
  1141. char hexdata[(sizeof(kline.wt) * 2) + 1];
  1142. bin2hex(hexdata, &kline.wt, sizeof(kline.wt));
  1143. applog(LOG_DEBUG, "WORKDATA: %s", hexdata);
  1144. }
  1145. applog(LOG_DEBUG, "%s%i:%d sending work (%d:%02x)",
  1146. klncgpu->drv->name, klncgpu->device_id, dev,
  1147. dev, kline.wt.workid);
  1148. KLIST *kitem = SendCmdGetReply(klncgpu, &kline, sizeof(kline.wt));
  1149. if (kitem != NULL) {
  1150. wr_lock(&(klninfo->stat_lock));
  1151. memcpy((void *)&(klninfo->status[dev]), kitem, sizeof(klninfo->status[dev]));
  1152. wr_unlock(&(klninfo->stat_lock));
  1153. kitem = release_kitem(klncgpu, kitem);
  1154. // remove old work
  1155. wque_size = 0;
  1156. wque_cleared = 0;
  1157. cgtime(&tv_old);
  1158. wr_lock(&klncgpu->qlock);
  1159. HASH_ITER(hh, klncgpu->queued_work, look, tmp) {
  1160. if (ms_tdiff(&tv_old, &(look->tv_stamp)) > OLD_WORK_MS) {
  1161. __work_completed(klncgpu, look);
  1162. free_work(look);
  1163. wque_cleared++;
  1164. } else
  1165. wque_size++;
  1166. }
  1167. wr_unlock(&klncgpu->qlock);
  1168. wr_lock(&(klninfo->stat_lock));
  1169. klninfo->wque_size = wque_size;
  1170. klninfo->wque_cleared = wque_cleared;
  1171. wr_unlock(&(klninfo->stat_lock));
  1172. return true;
  1173. }
  1174. return false;
  1175. }
  1176. static bool klondike_queue_full(struct cgpu_info *klncgpu)
  1177. {
  1178. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1179. struct work *work = NULL;
  1180. int dev, queued, slaves, seq, howlong;
  1181. struct timeval now;
  1182. bool nowork;
  1183. if (klncgpu->shutdown == true)
  1184. return true;
  1185. cgtime(&now);
  1186. rd_lock(&(klninfo->stat_lock));
  1187. slaves = klninfo->status[0].kline.ws.slavecount;
  1188. for (dev = 0; dev <= slaves; dev++)
  1189. if (ms_tdiff(&now, &(klninfo->jobque[dev].last_update)) > LATE_UPDATE_MS) {
  1190. klninfo->jobque[dev].late_update_count++;
  1191. seq = ++klninfo->jobque[dev].late_update_sequential;
  1192. rd_unlock(&(klninfo->stat_lock));
  1193. if (seq < LATE_UPDATE_LIMIT) {
  1194. applog(LOG_DEBUG, "%s%i:%d late update",
  1195. klncgpu->drv->name, klncgpu->device_id, dev);
  1196. klondike_get_stats(klncgpu);
  1197. goto que;
  1198. } else {
  1199. applog(LOG_WARNING, "%s%i:%d late update (%d) reached - attempting reset",
  1200. klncgpu->drv->name, klncgpu->device_id,
  1201. dev, LATE_UPDATE_LIMIT);
  1202. control_init(klncgpu);
  1203. kln_enable(klncgpu);
  1204. klondike_get_stats(klncgpu);
  1205. rd_lock(&(klninfo->stat_lock));
  1206. howlong = ms_tdiff(&now, &(klninfo->jobque[dev].last_update));
  1207. if (howlong > LATE_UPDATE_MS) {
  1208. rd_unlock(&(klninfo->stat_lock));
  1209. if (howlong > LATE_UPDATE_NODEV_MS) {
  1210. applog(LOG_ERR, "%s%i:%d reset failed - dropping device",
  1211. klncgpu->drv->name, klncgpu->device_id, dev);
  1212. usb_nodev(klncgpu);
  1213. } else
  1214. cgsleep_ms(LATE_UPDATE_SLEEP_MS);
  1215. return true;
  1216. }
  1217. break;
  1218. }
  1219. }
  1220. rd_unlock(&(klninfo->stat_lock));
  1221. que:
  1222. nowork = true;
  1223. for (queued = 0; queued < MAX_WORK_COUNT-1; queued++)
  1224. for (dev = 0; dev <= slaves; dev++) {
  1225. tryagain:
  1226. rd_lock(&(klninfo->stat_lock));
  1227. if (klninfo->jobque[dev].overheat) {
  1228. double temp = cvtKlnToC(klninfo->status[0].kline.ws.temp);
  1229. if ((queued == MAX_WORK_COUNT-2) &&
  1230. ms_tdiff(&now, &(klninfo->jobque[dev].last_update)) > (LATE_UPDATE_MS/2)) {
  1231. rd_unlock(&(klninfo->stat_lock));
  1232. klondike_get_stats(klncgpu);
  1233. goto tryagain;
  1234. }
  1235. if (temp <= KLN_COOLED_DOWN) {
  1236. klninfo->jobque[dev].overheat = false;
  1237. rd_unlock(&(klninfo->stat_lock));
  1238. applog(LOG_WARNING, "%s%i:%d Overheat recovered (%.0fC)",
  1239. klncgpu->drv->name, klncgpu->device_id,
  1240. dev, temp);
  1241. kln_enable(klncgpu);
  1242. goto tryagain;
  1243. } else {
  1244. rd_unlock(&(klninfo->stat_lock));
  1245. continue;
  1246. }
  1247. }
  1248. if (klninfo->jobque[dev].workqc <= queued) {
  1249. rd_unlock(&(klninfo->stat_lock));
  1250. if (!work)
  1251. work = get_queued(klncgpu);
  1252. if (unlikely(!work))
  1253. return false;
  1254. nowork = false;
  1255. if (klondike_send_work(klncgpu, dev, work))
  1256. return false;
  1257. } else
  1258. rd_unlock(&(klninfo->stat_lock));
  1259. }
  1260. if (nowork)
  1261. cgsleep_ms(10); // avoid a hard loop in case we have nothing to do
  1262. return true;
  1263. }
  1264. static int64_t klondike_scanwork(struct thr_info *thr)
  1265. {
  1266. struct cgpu_info *klncgpu = thr->cgpu;
  1267. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1268. int64_t newhashcount = 0;
  1269. int dev, slaves;
  1270. if (klninfo->usbinfo_nodev)
  1271. return -1;
  1272. restart_wait(thr, 200);
  1273. if (klninfo->status != NULL) {
  1274. rd_lock(&(klninfo->stat_lock));
  1275. slaves = klninfo->status[0].kline.ws.slavecount;
  1276. for (dev = 0; dev <= slaves; dev++) {
  1277. uint64_t newhashdev = 0, hashcount;
  1278. int maxcount;
  1279. hashcount = K_HASHCOUNT(klninfo->status[dev].kline.ws.hashcount);
  1280. maxcount = K_MAXCOUNT(klninfo->status[dev].kline.ws.maxcount);
  1281. // todo: chg this to check workid for wrapped instead
  1282. if (klninfo->devinfo[dev].lasthashcount > hashcount)
  1283. newhashdev += maxcount; // hash counter wrapped
  1284. newhashdev += hashcount - klninfo->devinfo[dev].lasthashcount;
  1285. klninfo->devinfo[dev].lasthashcount = hashcount;
  1286. if (maxcount != 0)
  1287. klninfo->hashcount += (newhashdev << 32) / maxcount;
  1288. }
  1289. newhashcount += 0xffffffffull * (uint64_t)klninfo->noncecount;
  1290. klninfo->noncecount = 0;
  1291. rd_unlock(&(klninfo->stat_lock));
  1292. }
  1293. return newhashcount;
  1294. }
  1295. #ifdef HAVE_CURSES
  1296. static
  1297. void klondike_wlogprint_status(struct cgpu_info *klncgpu)
  1298. {
  1299. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1300. uint16_t fan = 0;
  1301. uint16_t clock = 0;
  1302. int dev, slaves;
  1303. if (klninfo->status == NULL) {
  1304. return;
  1305. }
  1306. rd_lock(&(klninfo->stat_lock));
  1307. slaves = klninfo->status[0].kline.ws.slavecount;
  1308. for (dev = 0; dev <= slaves; dev++) {
  1309. fan += klninfo->cfg[dev].kline.cfg.fantarget;
  1310. clock += (uint16_t)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  1311. }
  1312. rd_unlock(&(klninfo->stat_lock));
  1313. fan /= slaves + 1;
  1314. fan = 100 * fan / 255;
  1315. clock /= slaves + 1;
  1316. if (clock && clock <= 999)
  1317. wlogprint("Frequency: %d MHz\n", (int)clock);
  1318. if (fan && fan <= 100)
  1319. wlogprint("Fan speed: %d%%\n", fan);
  1320. }
  1321. #endif
  1322. static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
  1323. {
  1324. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  1325. struct api_data *root = NULL;
  1326. char buf[32];
  1327. int dev, slaves;
  1328. if (klninfo->status == NULL)
  1329. return NULL;
  1330. rd_lock(&(klninfo->stat_lock));
  1331. slaves = klninfo->status[0].kline.ws.slavecount;
  1332. for (dev = 0; dev <= slaves; dev++) {
  1333. float fTemp = cvtKlnToC(klninfo->status[dev].kline.ws.temp);
  1334. sprintf(buf, "Temp %d", dev);
  1335. root = api_add_temp(root, buf, &fTemp, true);
  1336. double dClk = (double)K_HASHCLOCK(klninfo->cfg[dev].kline.cfg.hashclock);
  1337. sprintf(buf, "Clock %d", dev);
  1338. root = api_add_freq(root, buf, &dClk, true);
  1339. unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].kline.cfg.fantarget / 255;
  1340. sprintf(buf, "Fan Percent %d", dev);
  1341. root = api_add_int(root, buf, (int *)(&iFan), true);
  1342. iFan = 0;
  1343. if (klninfo->status[dev].kline.ws.fanspeed > 0)
  1344. iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].kline.ws.fanspeed;
  1345. sprintf(buf, "Fan RPM %d", dev);
  1346. root = api_add_int(root, buf, (int *)(&iFan), true);
  1347. if (klninfo->devinfo[dev].chipstats != NULL) {
  1348. char data[2048];
  1349. char one[32];
  1350. int n;
  1351. sprintf(buf, "Nonces / Chip %d", dev);
  1352. data[0] = '\0';
  1353. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  1354. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n]);
  1355. strcat(data, one);
  1356. }
  1357. root = api_add_string(root, buf, data, true);
  1358. sprintf(buf, "Errors / Chip %d", dev);
  1359. data[0] = '\0';
  1360. for (n = 0; n < klninfo->status[dev].kline.ws.chipcount; n++) {
  1361. snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].kline.ws.chipcount]);
  1362. strcat(data, one);
  1363. }
  1364. root = api_add_string(root, buf, data, true);
  1365. }
  1366. }
  1367. root = api_add_uint64(root, "Hash Count", &(klninfo->hashcount), true);
  1368. root = api_add_uint64(root, "Error Count", &(klninfo->errorcount), true);
  1369. root = api_add_uint64(root, "Noise Count", &(klninfo->noisecount), true);
  1370. root = api_add_int(root, "KLine Limit", &(klninfo->kline_count), true);
  1371. root = api_add_int(root, "KLine Used", &(klninfo->used_count), true);
  1372. root = api_add_elapsed(root, "KQue Delay Count", &(klninfo->delay_count), true);
  1373. root = api_add_elapsed(root, "KQue Delay Total", &(klninfo->delay_total), true);
  1374. root = api_add_elapsed(root, "KQue Delay Min", &(klninfo->delay_min), true);
  1375. root = api_add_elapsed(root, "KQue Delay Max", &(klninfo->delay_max), true);
  1376. double avg;
  1377. if (klninfo->delay_count == 0)
  1378. avg = 0;
  1379. else
  1380. avg = klninfo->delay_total / klninfo->delay_count;
  1381. root = api_add_diff(root, "KQue Delay Avg", &avg, true);
  1382. root = api_add_elapsed(root, "KQue Nonce Count", &(klninfo->nonce_count), true);
  1383. root = api_add_elapsed(root, "KQue Nonce Total", &(klninfo->nonce_total), true);
  1384. root = api_add_elapsed(root, "KQue Nonce Min", &(klninfo->nonce_min), true);
  1385. root = api_add_elapsed(root, "KQue Nonce Max", &(klninfo->nonce_max), true);
  1386. if (klninfo->nonce_count == 0)
  1387. avg = 0;
  1388. else
  1389. avg = klninfo->nonce_total / klninfo->nonce_count;
  1390. root = api_add_diff(root, "KQue Nonce Avg", &avg, true);
  1391. root = api_add_int(root, "WQue Size", &(klninfo->wque_size), true);
  1392. root = api_add_int(root, "WQue Cleared", &(klninfo->wque_cleared), true);
  1393. rd_unlock(&(klninfo->stat_lock));
  1394. return root;
  1395. }
  1396. struct device_drv klondike_drv = {
  1397. .dname = "Klondike",
  1398. .name = "KLN",
  1399. .drv_detect = klondike_detect,
  1400. .get_api_stats = klondike_api_stats,
  1401. .get_stats = klondike_get_stats,
  1402. .thread_prepare = klondike_thread_prepare,
  1403. .thread_init = klondike_thread_init,
  1404. .minerloop = hash_queued_work,
  1405. .scanwork = klondike_scanwork,
  1406. .queue_full = klondike_queue_full,
  1407. .flush_work = klondike_flush_work,
  1408. .thread_shutdown = klondike_shutdown,
  1409. .thread_enable = klondike_thread_enable,
  1410. #ifdef HAVE_CURSES
  1411. .proc_wlogprint_status = klondike_wlogprint_status,
  1412. #endif
  1413. };