driver-klondike.c 45 KB

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