driver-klondike.c 47 KB

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