driver-klondike.c 15 KB

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