driver-klondike.c 45 KB

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