driver-klondike.c 16 KB

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