driver-klondike.c 45 KB

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