driver-klondike.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * Copyright 2013 Andrew Smith
  3. * Copyright 2013 Con Kolivas
  4. * Copyright 2013 Chris Savery
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 3 of the License, or (at your option)
  9. * any later version. See COPYING for more details.
  10. */
  11. #include <float.h>
  12. #include <limits.h>
  13. #include <pthread.h>
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <strings.h>
  17. #include <sys/time.h>
  18. #include <unistd.h>
  19. #include <math.h>
  20. #include "config.h"
  21. #ifdef WIN32
  22. #include <windows.h>
  23. #endif
  24. #include "compat.h"
  25. #include "miner.h"
  26. #include "usbutils.h"
  27. #define KLN "KLN"
  28. #define K1 "K1"
  29. #define K16 "K16"
  30. #define K64 "K64"
  31. #define MIDSTATE_BYTES 32
  32. #define MERKLE_OFFSET 64
  33. #define MERKLE_BYTES 12
  34. #define REPLY_BUFSIZE 32 // adequate for all types of replies
  35. #define MAX_REPLY_COUNT 32 // more unhandled replies than this will result in data loss
  36. #define REPLY_WAIT_TIME 100 // time to wait for a cmd polling it's reply
  37. #define MAX_WORK_COUNT 4 // for now, must be binary multiple and match firmware
  38. struct device_drv klondike_drv;
  39. typedef struct klondike_id {
  40. uint8_t version;
  41. uint8_t product[7];
  42. uint32_t serial;
  43. } IDENTITY;
  44. typedef struct klondike_status {
  45. uint8_t state;
  46. uint8_t chipcount;
  47. uint8_t slavecount;
  48. uint8_t workqc;
  49. uint8_t workid;
  50. uint8_t temp;
  51. uint8_t fanspeed;
  52. uint8_t errorcount;
  53. uint16_t hashcount;
  54. uint16_t maxcount;
  55. } WORKSTATUS;
  56. typedef struct _worktask {
  57. uint16_t pad1;
  58. uint8_t pad2;
  59. uint8_t workid;
  60. uint32_t midstate[8];
  61. uint32_t merkle[3];
  62. } WORKTASK;
  63. typedef struct _workresult {
  64. uint16_t pad;
  65. uint8_t device;
  66. uint8_t workid;
  67. uint32_t nonce;
  68. } WORKRESULT;
  69. typedef struct kondike_cfg {
  70. uint16_t hashclock;
  71. uint8_t temptarget;
  72. uint8_t tempcritical;
  73. uint8_t fantarget;
  74. uint8_t pad;
  75. } WORKCFG;
  76. typedef struct device_info {
  77. uint32_t noncecount;
  78. uint32_t nextworkid;
  79. uint16_t lasthashcount;
  80. uint64_t totalhashcount;
  81. } DEVINFO;
  82. struct klondike_info {
  83. bool shutdown;
  84. pthread_rwlock_t stat_lock;
  85. struct thr_info replies_thr;
  86. WORKSTATUS *status;
  87. DEVINFO *devinfo;
  88. WORKCFG *cfg;
  89. char *replies;
  90. int nextreply;
  91. };
  92. IDENTITY KlondikeID;
  93. static char *SendCmdGetReply(struct cgpu_info *klncgpu, char Cmd, int device, int datalen, void *data)
  94. {
  95. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  96. char outbuf[64];
  97. int retries = 10;
  98. int chkreply = klninfo->nextreply;
  99. int sent, err;
  100. if (klncgpu->usbinfo.nodev)
  101. return NULL;
  102. outbuf[0] = Cmd;
  103. outbuf[1] = device;
  104. memcpy(outbuf+2, data, datalen);
  105. err = usb_write(klncgpu, outbuf, 2+datalen, &sent, C_REQUESTRESULTS);
  106. if (err < 0 || sent != 2+datalen) {
  107. applog(LOG_ERR, "%s (%s) Cmd:%c Dev:%d, write failed (%d:%d)", klncgpu->drv->dname, klncgpu->device_path, Cmd, device, sent, err);
  108. }
  109. while(retries-- > 0) {
  110. nmsleep(REPLY_WAIT_TIME);
  111. while(*(klninfo->replies + chkreply*REPLY_BUFSIZE) != Cmd) {
  112. if(++chkreply == MAX_REPLY_COUNT)
  113. chkreply = 0;
  114. if(chkreply == klninfo->nextreply)
  115. break;
  116. }
  117. if(chkreply == klninfo->nextreply)
  118. continue;
  119. *(klninfo->replies + chkreply*REPLY_BUFSIZE) = '!'; // mark to prevent re-use
  120. return klninfo->replies + chkreply*REPLY_BUFSIZE + 1;
  121. }
  122. return NULL;
  123. }
  124. static bool klondike_get_stats(struct cgpu_info *klncgpu)
  125. {
  126. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  127. char replybuf[REPLY_BUFSIZE];
  128. char devpath[20];
  129. bool allok = false;
  130. int sent, recd, err;
  131. int slaves, dev;
  132. if (klncgpu->usbinfo.nodev)
  133. return false;
  134. char *reply = SendCmdGetReply(klncgpu, 'S', 0, 0, NULL);
  135. if(reply != NULL && reply[0] == 'S' && reply[1] == 0) {
  136. // todo: detect slavecount change and realloc space
  137. slaves = ((WORKSTATUS *)(reply+2))->slavecount;
  138. if(klninfo->status == NULL) {
  139. applog(LOG_DEBUG, "Klondike initializing status data");
  140. // alloc space for status, devinfo and cfg for master and slaves
  141. klninfo->status = calloc(slaves+1, sizeof(WORKSTATUS));
  142. if (unlikely(!klninfo->status))
  143. quit(1, "Failed to calloc status array in klondke_get_stats");
  144. klninfo->devinfo = calloc(slaves+1, sizeof(DEVINFO));
  145. if (unlikely(!klninfo->devinfo))
  146. quit(1, "Failed to calloc devinfo array in klondke_get_stats");
  147. klninfo->cfg = calloc(slaves+1, sizeof(WORKCFG));
  148. if (unlikely(!klninfo->cfg))
  149. quit(1, "Failed to calloc cfg array in klondke_get_stats");
  150. // where does saved user cfg info come from?
  151. // todo: set user cfg to devices
  152. }
  153. applog(LOG_DEBUG, "Klondike updating device status");
  154. // device 0 is master and must exist
  155. wr_lock(&(klninfo->stat_lock));
  156. klninfo->status[0] = *(WORKSTATUS *)(reply+2);
  157. // loop thru slaves and get status for each
  158. for(dev = 1; dev < slaves; dev++) {
  159. }
  160. wr_unlock(&(klninfo->stat_lock));
  161. allok = true;
  162. }
  163. return allok;
  164. }
  165. static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  166. {
  167. struct cgpu_info *klncgpu = calloc(1, sizeof(*klncgpu));
  168. struct klondike_info *klninfo = NULL;
  169. int attempts = 0;
  170. if (unlikely(!klncgpu))
  171. quit(1, "Failed to calloc klncgpu in klondike_detect_one");
  172. klncgpu->drv = &klondike_drv;
  173. klncgpu->deven = DEV_ENABLED;
  174. klncgpu->threads = 1;
  175. klninfo = calloc(1, sizeof(*klninfo));
  176. if (unlikely(!klninfo))
  177. quit(1, "Failed to calloc klninfo in klondke_detect_one");
  178. // TODO: fix ... everywhere ...
  179. klncgpu->device_data = (FILE *)klninfo;
  180. klninfo->replies = calloc(MAX_REPLY_COUNT, REPLY_BUFSIZE);
  181. if (unlikely(!klninfo->replies))
  182. quit(1, "Failed to calloc replies buffer in klondke_detect_one");
  183. klninfo->nextreply = 0;
  184. if (usb_init(klncgpu, dev, found)) {
  185. while(attempts++ < 3) {
  186. char devpath[20], reply[32];
  187. int sent, recd, err;
  188. sprintf(devpath, "%d:%d", (int)(klncgpu->usbinfo.bus_number), (int)(klncgpu->usbinfo.device_address));
  189. err = usb_write(klncgpu, "I", 2, &sent, C_REQUESTRESULTS);
  190. if (err < 0 || sent != 2) {
  191. applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)", klncgpu->drv->dname, devpath, sent, err);
  192. }
  193. nmsleep(REPLY_WAIT_TIME*10);
  194. err = usb_read(klncgpu, reply, sizeof(IDENTITY)+2, &recd, C_GETRESULTS);
  195. if (err < 0) {
  196. applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)", klncgpu->drv->dname, devpath, recd, err);
  197. } else if (recd < 1) {
  198. applog(LOG_ERR, "%s (%s) detect empty reply (%d)", klncgpu->drv->dname, devpath, recd);
  199. } else if(reply[0] == 'I' && reply[1] == 0) {
  200. applog(LOG_DEBUG, "%s (%s) detect successful", klncgpu->drv->dname, devpath);
  201. KlondikeID = *(IDENTITY *)(&reply[2]);
  202. klncgpu->device_path = strdup(devpath);
  203. update_usb_stats(klncgpu);
  204. if(!add_cgpu(klncgpu))
  205. break;
  206. applog(LOG_DEBUG, "Klondike cgpu added");
  207. return true;
  208. }
  209. }
  210. usb_uninit(klncgpu);
  211. }
  212. free(klncgpu);
  213. return false;
  214. }
  215. static void klondike_detect(void)
  216. {
  217. usb_detect(&klondike_drv, klondike_detect_one);
  218. }
  219. static void klondike_identify(struct cgpu_info *klncgpu)
  220. {
  221. SendCmdGetReply(klncgpu, 'I', 0, 0, NULL);
  222. }
  223. static void klondike_check_nonce(struct cgpu_info *klncgpu, WORKRESULT *result)
  224. {
  225. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  226. struct work *work, *tmp;
  227. applog(LOG_DEBUG, "Klondike FOUND NONCE (x%08x)", result->nonce);
  228. HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
  229. if (work->queued && (work->subid == (result->device*256 + result->workid))) {
  230. // devflag is used to flag stale work
  231. work->devflag = true;
  232. wr_lock(&(klninfo->stat_lock));
  233. klninfo->devinfo[result->device].noncecount++;
  234. wr_unlock(&(klninfo->stat_lock));
  235. result->nonce = le32toh(result->nonce - 0xC0);
  236. applog(LOG_DEBUG, "Klondike SUBMIT NONCE (x%08x)", result->nonce);
  237. submit_nonce(klncgpu->thr[0], work, result->nonce);
  238. }
  239. return;
  240. }
  241. applog(LOG_ERR, "%s%i:%d failed to find nonce work (x%08x) - can't be processed - ignored",
  242. klncgpu->drv->name, klncgpu->device_id, result->device, result->nonce);
  243. //inc_hw_errors(klncgpu->thr[0]);
  244. }
  245. // thread to keep looking for replies
  246. static void *klondike_get_replies(void *userdata)
  247. {
  248. struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
  249. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  250. char *replybuf;
  251. int err, recd;
  252. applog(LOG_DEBUG, "Klondike listening for replies");
  253. while (klninfo->shutdown == false) {
  254. if (klncgpu->usbinfo.nodev)
  255. return NULL;
  256. replybuf = klninfo->replies + klninfo->nextreply * REPLY_BUFSIZE;
  257. replybuf[0] = 0;
  258. err = usb_read(klncgpu, replybuf+1, REPLY_BUFSIZE-1, &recd, C_GETRESULTS);
  259. if (err < 0 && err != -7)
  260. applog(LOG_DEBUG, "%s (%s) error reply (%d:%d)", klncgpu->drv->dname, klncgpu->device_path, recd, err);
  261. if (recd < 1)
  262. continue;
  263. else {
  264. if(opt_log_level <= LOG_DEBUG) {
  265. char *hexdata = bin2hex(replybuf+1, recd);
  266. applog(LOG_DEBUG, "%s (%s) reply [%s:%s]", klncgpu->drv->dname, klncgpu->device_path, replybuf+1, hexdata);
  267. free(hexdata);
  268. }
  269. if(++klninfo->nextreply == MAX_REPLY_COUNT)
  270. klninfo->nextreply = 0;
  271. replybuf[0] = replybuf[1];
  272. if(replybuf[0] == '=')
  273. klondike_check_nonce(klncgpu, (WORKRESULT *)replybuf);
  274. }
  275. }
  276. return NULL;
  277. }
  278. static bool klondike_thread_prepare(struct thr_info *thr)
  279. {
  280. struct cgpu_info *klncgpu = thr->cgpu;
  281. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  282. struct timeval now;
  283. if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) {
  284. applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id);
  285. return false;
  286. }
  287. pthread_detach(klninfo->replies_thr.pth);
  288. // init status data structures
  289. nmsleep(500);
  290. klondike_get_stats(klncgpu);
  291. return true;
  292. }
  293. static void klondike_flush_work(struct cgpu_info *klncgpu)
  294. {
  295. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  296. int dev;
  297. applog(LOG_DEBUG, "Klondike flushing work work");
  298. for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
  299. char *reply = SendCmdGetReply(klncgpu, 'A', dev, 0, NULL);
  300. if(reply != NULL && reply[0] == 'A' && reply[1] == dev) {
  301. wr_lock(&(klninfo->stat_lock));
  302. klninfo->status[dev] = *(WORKSTATUS *)(reply+2);
  303. wr_unlock(&(klninfo->stat_lock));
  304. }
  305. }
  306. }
  307. static bool klondike_thread_init(struct thr_info *thr)
  308. {
  309. struct cgpu_info *klncgpu = thr->cgpu;
  310. if (klncgpu->usbinfo.nodev)
  311. return false;
  312. klondike_flush_work(klncgpu);
  313. return true;
  314. }
  315. static void klondike_shutdown(struct thr_info *thr)
  316. {
  317. struct cgpu_info *klncgpu = thr->cgpu;
  318. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  319. klondike_flush_work(klncgpu);
  320. klninfo->shutdown = true;
  321. }
  322. static void klondike_thread_enable(struct thr_info *thr)
  323. {
  324. struct cgpu_info *klncgpu = thr->cgpu;
  325. if (klncgpu->usbinfo.nodev)
  326. return;
  327. klondike_flush_work(klncgpu);
  328. }
  329. static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
  330. {
  331. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  332. WORKTASK data;
  333. if (klncgpu->usbinfo.nodev)
  334. return false;
  335. memcpy(data.midstate, work->midstate, MIDSTATE_BYTES);
  336. memcpy(data.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
  337. data.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++);
  338. work->subid = dev*256 + data.workid;
  339. applog(LOG_DEBUG, "Klondike sending work (%d:x%02x)", dev, data.workid);
  340. char *reply = SendCmdGetReply(klncgpu, 'W', dev, sizeof(data)-3, &data.workid);
  341. if(reply != NULL && reply[0] == 'W' && reply[1] == dev) {
  342. wr_lock(&(klninfo->stat_lock));
  343. klninfo->status[dev] = *(WORKSTATUS *)(reply+2);
  344. wr_unlock(&(klninfo->stat_lock));
  345. return true;
  346. }
  347. return false;
  348. }
  349. static bool klondike_queue_full(struct cgpu_info *klncgpu)
  350. {
  351. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  352. struct work *work = NULL;
  353. int dev, queued;
  354. for(queued = 0; queued < MAX_WORK_COUNT-1; queued++)
  355. for(dev = 0; dev <= klninfo->status->slavecount; dev++)
  356. if (klninfo->status[dev].workqc <= queued) {
  357. if (!work)
  358. work = get_queued(klncgpu);
  359. if (unlikely(!work))
  360. return false;
  361. if (klondike_send_work(klncgpu, dev, work)) {
  362. work = NULL;
  363. break;
  364. }
  365. }
  366. return true;
  367. }
  368. static int64_t klondike_scanwork(struct thr_info *thr)
  369. {
  370. struct cgpu_info *klncgpu = thr->cgpu;
  371. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  372. int64_t newhashcount = 0;
  373. int dev;
  374. if (klncgpu->usbinfo.nodev)
  375. return -1;
  376. restart_wait(500);
  377. if (klninfo->status != NULL) {
  378. rd_lock(&(klninfo->stat_lock));
  379. for(dev = 0; dev <= klninfo->status->slavecount; dev++) {
  380. uint64_t newhashdev = 0;
  381. if(klninfo->devinfo[dev].lasthashcount > klninfo->status[dev].hashcount)
  382. newhashdev += klninfo->status[dev].maxcount; // hash counter wrapped
  383. newhashdev += klninfo->status[dev].hashcount - klninfo->devinfo[dev].lasthashcount;
  384. klninfo->devinfo[dev].lasthashcount = klninfo->status[dev].hashcount;
  385. newhashcount += (newhashdev << 32) / klninfo->status[dev].maxcount;
  386. }
  387. rd_unlock(&(klninfo->stat_lock));
  388. }
  389. return newhashcount;
  390. }
  391. static double convertKlnTemp(uint8_t temp)
  392. {
  393. return (double)1/((double)1/(25+273.15) + log((double)temp*1000/(256-temp)/2200)/3987) - 273.15;
  394. }
  395. static void get_klondike_statline_before(char *buf, struct cgpu_info *klncgpu)
  396. {
  397. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  398. uint8_t temp = 0xFF;
  399. int dev;
  400. rd_lock(&(klninfo->stat_lock));
  401. for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
  402. if (klninfo->status[dev].temp < temp)
  403. temp = klninfo->status[dev].temp;
  404. }
  405. rd_unlock(&(klninfo->stat_lock));
  406. tailsprintf(buf, " %3.0fC 1.2V | ", convertKlnTemp(temp));
  407. }
  408. static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
  409. {
  410. struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
  411. struct api_data *root = NULL;
  412. rd_lock(&(klninfo->stat_lock));
  413. // todo: convert temp and make correct type float
  414. //root = api_add_temp(root, "Temp", &(klninfo->status->temp), true);
  415. // todo: convert hashclock value to MHz and correct type double
  416. //root = api_add_freq(root, "Clock", &(klninfo->cfg->hashclock), true);
  417. rd_unlock(&(klninfo->stat_lock));
  418. }
  419. struct device_drv klondike_drv = {
  420. .drv_id = DRIVER_KLONDIKE,
  421. .dname = "Klondike",
  422. .name = KLN,
  423. .drv_detect = klondike_detect,
  424. .get_api_stats = klondike_api_stats,
  425. .get_statline_before = get_klondike_statline_before,
  426. .get_stats = klondike_get_stats,
  427. .identify_device = klondike_identify,
  428. .thread_prepare = klondike_thread_prepare,
  429. .thread_init = klondike_thread_init,
  430. .hash_work = hash_queued_work,
  431. .scanwork = klondike_scanwork,
  432. .queue_full = klondike_queue_full,
  433. .flush_work = klondike_flush_work,
  434. .thread_shutdown = klondike_shutdown,
  435. .thread_enable = klondike_thread_enable
  436. };