driver-lketc.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. /*
  2. * Copyright 2013-2014 Con Kolivas <kernel@kolivas.org>
  3. * Copyright 2014 LKETC Integrated Systems Limited
  4. * Copyright 2014 Dominik Lehner
  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 <pthread.h>
  13. #include <stdint.h>
  14. #include <stdio.h>
  15. #include <sys/time.h>
  16. #include <sys/types.h>
  17. #include <unistd.h>
  18. #ifndef WIN32
  19. #include <sys/select.h>
  20. #include <termios.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #ifndef O_CLOEXEC
  24. #define O_CLOEXEC 0
  25. #endif
  26. #else
  27. #include "compat.h"
  28. #include <windows.h>
  29. #include <io.h>
  30. #endif
  31. #include "miner.h"
  32. #include "usbutils.h"
  33. #include "fpgautils.h"
  34. #include "elist.h"
  35. #include "util.h"
  36. #include "driver-lketc.h"
  37. #define using_libusb(info) ((info)->using_libusb > 0)
  38. #define using_serial(info) ((info)->using_libusb == 0)
  39. // Configuration options
  40. extern bool opt_lketc_debug;
  41. extern int opt_lketc_chips_count; // number of Zeus chips chained together
  42. extern int opt_lketc_chip_clk; // frequency to run chips with
  43. extern bool opt_lketc_nocheck_golden; // bypass hashrate check
  44. //static int opt_lketc_chips_count_max = 1; // smallest power of 2 >= opt_lketc_chips_count
  45. // is currently auto-calculated
  46. // Index for device-specific options
  47. //static int option_offset = -1;
  48. // Unset upon first hotplug check
  49. static bool initial_startup_phase = true;
  50. static struct name_chip_map {
  51. char *model_name;
  52. int chips_count;
  53. } lketc_models[] = {
  54. { "Blizzard", 6 },
  55. //{ "Cyclone", 96 }, // model renamed??
  56. { "Hurricane X2", 48 },
  57. { "Hurricane X3", 64 },
  58. { "Thunder X2", 96 },
  59. { "Thunder X3", 128 },
  60. { NULL, 0 }
  61. };
  62. /************************************************************
  63. * Utility Functions
  64. ************************************************************/
  65. static void flush_uart(int fd)
  66. {
  67. #ifdef WIN32
  68. const HANDLE fh = (HANDLE)_get_osfhandle(fd);
  69. PurgeComm(fh, PURGE_RXCLEAR);
  70. #else
  71. tcflush(fd, TCIFLUSH);
  72. #endif
  73. }
  74. static int __maybe_unused flush_fd(int fd)
  75. {
  76. static char discard[10];
  77. return read(fd, discard, sizeof(discard));
  78. }
  79. static void rev(unsigned char *s, size_t l)
  80. {
  81. size_t i, j;
  82. unsigned char t;
  83. for (i = 0, j = l - 1; i < j; i++, j--) {
  84. t = s[i];
  85. s[i] = s[j];
  86. s[j] = t;
  87. }
  88. }
  89. static int log_2(int value)
  90. {
  91. int x = 0;
  92. while (value > 1) {
  93. value >>= 1;
  94. x++;
  95. }
  96. return x;
  97. }
  98. static uint32_t __maybe_unused chip_index(uint32_t value, int bit_num)
  99. {
  100. uint32_t newvalue = 0;
  101. int i;
  102. // isolate bits 19-28, then shift right to get the
  103. // highest bits that distinguish multiple chips
  104. value = (value & 0x1ff80000) >> (29 - bit_num);
  105. for (i = 0; i < bit_num; i++) {
  106. newvalue = newvalue << 1;
  107. newvalue += value & 0x01;
  108. value = value >> 1;
  109. }
  110. return newvalue;
  111. }
  112. static int lowest_pow2(int min)
  113. {
  114. int i;
  115. for (i = 1; i < 1024; i = i * 2) {
  116. if (min <= i){
  117. return i;
  118. }
  119. }
  120. return 1024;
  121. }
  122. static void notify_send_work_thread(struct cgpu_info *lketc)
  123. {
  124. struct LKETC_INFO *info = lketc->device_data;
  125. cgsem_post(&info->wusem);
  126. }
  127. /************************************************************
  128. * I/O helper functions
  129. ************************************************************/
  130. #define lketc_serial_open_detect(devpath, baud, purge) serial_open_ex(devpath, baud, LKETC_READ_FAULT_DECISECONDS, 0, purge, true)
  131. #define lketc_serial_open(devpath, baud, purge) serial_open_ex(devpath, baud, LKETC_READ_FAULT_DECISECONDS, 0, purge, true)
  132. #define lketc_serial_close(fd) close(fd)
  133. static bool lketc_reopen(struct cgpu_info *lketc)
  134. {
  135. struct LKETC_INFO *info = lketc->device_data;
  136. int try, fd = -1;
  137. if (!using_serial(info)) // sanity check
  138. return false;
  139. if (info->device_fd != -1) {
  140. applog(LOG_DEBUG, "Closing %s%d on %s (fd=%d)",
  141. lketc->drv->name, lketc->device_id, lketc->device_path, info->device_fd);
  142. lketc_serial_close(info->device_fd);
  143. info->device_fd = -1;
  144. cgsleep_ms(2000);
  145. }
  146. applog(LOG_DEBUG, "Attempting to open %s%d on %s",
  147. lketc->drv->name, lketc->device_id, lketc->device_path);
  148. for (try = 0; try < 3; ++try) {
  149. fd = lketc_serial_open(lketc->device_path, info->baud, true);
  150. if (likely(fd > -1))
  151. break;
  152. cgsleep_ms(3000);
  153. }
  154. if (unlikely(fd < 0)) {
  155. applog(LOG_ERR, "Failed to open %s%d on %s (%d attempts)",
  156. lketc->drv->name, lketc->device_id, lketc->device_path, try);
  157. return false;
  158. }
  159. info->device_fd = fd;
  160. applog(LOG_DEBUG, "Successfully opened %s%d on %s (%d attempts, fd=%d)",
  161. lketc->drv->name, lketc->device_id, lketc->device_path, try, info->device_fd);
  162. return true;
  163. }
  164. static int lketc_serial_write(int fd, const void *buf, size_t len)
  165. {
  166. ssize_t ret;
  167. size_t total = 0;
  168. #if LKETC_PROTOCOL_DEBUG
  169. if (opt_lketc_debug) {
  170. char *hexstr;
  171. hexstr = bin2hex(buf, len);
  172. applog(LOG_DEBUG, "> %s", hexstr);
  173. free(hexstr);
  174. }
  175. #endif
  176. while (total < len) {
  177. #ifndef WIN32
  178. ret = write(fd, buf, len);
  179. #else
  180. ret = win32write(fd, buf, len);
  181. #endif
  182. if (ret < 0) {
  183. applog(LOG_ERR, "lketc_serial_write (%d): error on write: %s", fd, strerror(errno));
  184. return -1;
  185. }
  186. total += (size_t)ret;
  187. }
  188. return total;
  189. }
  190. static int lketc_serial_read(int fd, void *buf, size_t len, int read_count, struct timeval *tv_firstbyte)
  191. {
  192. ssize_t ret;
  193. size_t total = 0;
  194. int rc = 0;
  195. while (total < len) {
  196. #ifndef WIN32
  197. ret = read(fd, buf + total, len - total);
  198. #else
  199. ret = win32read(fd, buf + total, len - total);
  200. #endif
  201. if (ret < 0) {
  202. applog(LOG_ERR, "lketc_serial_read (%d): error on read: %s", fd, strerror(errno));
  203. return -1;
  204. }
  205. if (tv_firstbyte != NULL && total == 0)
  206. cgtime(tv_firstbyte);
  207. applog(LOG_DEBUG, "lketc_serial_read: read returned %d", (int)ret);
  208. if (ret == 0 && ++rc >= read_count)
  209. break;
  210. total += (size_t)ret;
  211. }
  212. #if LKETC_PROTOCOL_DEBUG
  213. if (opt_lketc_debug) {
  214. char *hexstr;
  215. if (total > 0) {
  216. hexstr = bin2hex(buf, total);
  217. applog(LOG_DEBUG, "< %s", hexstr);
  218. free(hexstr);
  219. } else {
  220. applog(LOG_DEBUG, "< (no data)");
  221. }
  222. }
  223. #endif
  224. return total;
  225. }
  226. /************************************************************
  227. * Detection and setup
  228. ************************************************************/
  229. static unsigned char lketc_clk_to_freqcode(int clkfreq)
  230. {
  231. if (clkfreq > LKETC_CLK_MAX) {
  232. applog(LOG_WARNING, "Clock frequency %d too high, resetting to %d",
  233. clkfreq, LKETC_CLK_MAX);
  234. clkfreq = LKETC_CLK_MAX;
  235. }
  236. if (clkfreq < LKETC_CLK_MIN) {
  237. applog(LOG_WARNING, "Clock frequency %d too low, resetting to %d",
  238. clkfreq, LKETC_CLK_MIN);
  239. clkfreq = LKETC_CLK_MIN;
  240. }
  241. return (unsigned char)((double)clkfreq * 2. / 3.);
  242. }
  243. static void lketc_get_device_options(const char *devid, int *chips_count, int *chip_clk, const char *options)
  244. {
  245. char *p, *all, *found = NULL;
  246. long lval;
  247. int index = 0;
  248. char *lastslsh = MAX(strrchr(devid, '/'), strrchr(devid, '\\'));
  249. if (lastslsh != NULL)
  250. ++lastslsh;
  251. // set global default options
  252. *chips_count = (opt_lketc_chips_count) ? opt_lketc_chips_count : LKETC_MIN_CHIPS;
  253. *chip_clk = (opt_lketc_chip_clk) ? opt_lketc_chip_clk : LKETC_CLK_MIN;
  254. if (options == NULL)
  255. return;
  256. all = strdup(options);
  257. for (p = strtok(all, ";"); p != NULL; p = strtok(NULL, ";")) {
  258. if (strncmp(p, devid, strlen(devid)) == 0) {
  259. found = p;
  260. break;
  261. }
  262. if (lastslsh != NULL && strncmp(p, lastslsh, strlen(lastslsh)) == 0) {
  263. found = p;
  264. break;
  265. }
  266. }
  267. if (found == NULL) {
  268. free(all);
  269. return;
  270. }
  271. for (p = strtok(found, ","); p != NULL; p = strtok(NULL, ",")) {
  272. lval = strtol(p, NULL, 10);
  273. switch (index++) {
  274. case 1: // chip count
  275. if (lval < LKETC_MIN_CHIPS || lval > LKETC_MAX_CHIPS) {
  276. applog(LOG_ERR, "Invalid chip count %ld for Zeus device %s",
  277. lval, devid);
  278. break;
  279. }
  280. *chips_count = (int)lval;
  281. break;
  282. case 2: // clock
  283. if (lval < LKETC_CLK_MIN || lval > LKETC_CLK_MAX) {
  284. applog(LOG_ERR, "Invalid clock speed %ld for Zeus device %s",
  285. lval, devid);
  286. break;
  287. }
  288. *chip_clk = (int)lval;
  289. break;
  290. default:
  291. break;
  292. }
  293. }
  294. free(all);
  295. return;
  296. }
  297. static char *lketc_device_name(int chips_count)
  298. {
  299. struct name_chip_map *p;
  300. for (p = lketc_models; p->model_name != NULL; ++p) {
  301. if (p->chips_count == chips_count)
  302. return p->model_name;
  303. }
  304. return NULL;
  305. }
  306. static int lketc_usb_control_transfer_data(struct cgpu_info *lketc, uint8_t request_type, uint8_t bRequest,
  307. uint16_t wValue, uint16_t wIndex, uint32_t *data, int siz, enum usb_cmds cmd)
  308. {
  309. int err = usb_transfer_data(lketc, request_type, bRequest, wValue, wIndex, data, siz, cmd);
  310. if (err)
  311. applog(LOG_DEBUG, "%s%d: error %d on USB control transfer %s",
  312. lketc->drv->name, lketc->cgminer_id, err, usb_cmdname(cmd));
  313. return err;
  314. }
  315. static inline int lketc_usb_control_transfer(struct cgpu_info *lketc, uint8_t request_type, uint8_t bRequest,
  316. uint16_t wValue, uint16_t wIndex, enum usb_cmds cmd)
  317. {
  318. return lketc_usb_control_transfer_data(lketc, request_type, bRequest, wValue, wIndex, NULL, 0, cmd);
  319. }
  320. static bool lketc_initialize_cp2102(struct cgpu_info *lketc)
  321. {
  322. int interface = usb_interface(lketc);
  323. //uint32_t baudrate = CP210X_DATA_BAUD;
  324. // Enable the UART
  325. if (lketc_usb_control_transfer(lketc, CP210X_TYPE_OUT, CP210X_REQUEST_IFC_ENABLE,
  326. CP210X_VALUE_UART_ENABLE, interface, C_ENABLE_UART))
  327. return false;
  328. // Set data control
  329. if (lketc_usb_control_transfer(lketc, CP210X_TYPE_OUT, CP210X_REQUEST_DATA,
  330. CP210X_VALUE_DATA, interface, C_SETDATA))
  331. return false;
  332. // Zeusminers have baud hardcoded to 115200, and reject baud commands, even to same value
  333. // Set the baud
  334. //if (lketc_usb_control_transfer_data(lketc, CP210X_TYPE_OUT, CP210X_REQUEST_BAUD,
  335. // 0, interface, &baudrate, sizeof(baudrate), C_SETBAUD))
  336. // return false;
  337. return true;
  338. }
  339. static bool lketc_initialize_ftdi(struct cgpu_info *lketc)
  340. {
  341. int interface = usb_interface(lketc);
  342. return true;
  343. }
  344. static bool lketc_initialize_usb(struct cgpu_info *lketc)
  345. {
  346. enum sub_ident ident;
  347. if (lketc->usbinfo.nodev)
  348. return false;
  349. ident = usb_ident(lketc);
  350. switch (ident) {
  351. case IDENT_LKE:
  352. return lketc_initialize_cp2102(lketc);
  353. default:
  354. applog(LOG_ERR, "lketc_initialize_usb called on wrong device, ident=%d", ident);
  355. return false;
  356. }
  357. }
  358. static struct cgpu_info *lketc_detect_one_usb(struct libusb_device *dev, struct usb_find_devices *found)
  359. {
  360. struct cgpu_info *lketc;
  361. struct LKETC_INFO *info;
  362. lketc = usb_alloc_cgpu(&lketc_drv, 1);
  363. if (!usb_init(lketc, dev, found))
  364. goto usbdealloc;
  365. info = calloc(1, sizeof(struct LKETC_INFO));
  366. if (unlikely(!info))
  367. goto usbdealloc;
  368. lketc->device_data = info;
  369. lketc->deven = DEV_ENABLED;
  370. lketc->threads = 1;
  371. info->device_fd = -1;
  372. info->using_libusb = 1;
  373. if (lketc->usbdev->serial_string && strlen(lketc->usbdev->serial_string) > 4)
  374. lketc->unique_id = lketc->usbdev->serial_string;
  375. else
  376. lketc->unique_id = lketc->device_path;
  377. strncpy(info->device_name, lketc->unique_id, sizeof(info->device_name) - 1);
  378. info->device_name[sizeof(info->device_name) - 1] = '\0';
  379. lketc_get_device_options(lketc->unique_id, &info->chips_count, &info->chip_clk, opt_lketc_options);
  380. lketc->name = lketc_device_name(info->chips_count);
  381. info->freqcode = lketc_clk_to_freqcode(info->chip_clk);
  382. info->baud = LKETC_IO_SPEED;
  383. info->cores_per_chip = LKETC_CHIP_CORES;
  384. info->chips_count_max = lowest_pow2(info->chips_count);
  385. info->chips_bit_num = log_2(info->chips_count_max);
  386. info->next_chip_clk = -1;
  387. libusb_reset_device(lketc->usbdev->handle);
  388. update_usb_stats(lketc);
  389. lketc->usbdev->usb_type = USB_TYPE_STD;
  390. if (!lketc_initialize_usb(lketc)) {
  391. applog(LOG_ERR, "Failed to initialize Zeus USB-UART interface");
  392. goto alldealloc;
  393. }
  394. info->golden_speed_per_core = (((info->chip_clk * 2.) / 3.) * 1024.) / 8.;
  395. info->work_timeout.tv_sec = 4294967296LL / (info->golden_speed_per_core * info->cores_per_chip * info->chips_count_max) * 0.9;
  396. info->work_timeout.tv_usec = 0;
  397. info->read_count = (uint32_t)((4294967296LL*10)/(info->cores_per_chip*info->chips_count_max*info->golden_speed_per_core*2));
  398. info->read_count = info->read_count*3/4;
  399. if (!add_cgpu(lketc))
  400. goto alldealloc;
  401. return lketc;
  402. alldealloc:
  403. usb_uninit(lketc);
  404. free(lketc->device_data);
  405. lketc->device_data = NULL;
  406. usbdealloc:
  407. lketc = usb_free_cgpu(lketc);
  408. return NULL;
  409. }
  410. static bool lketc_detect_one_serial(const char *devpath)
  411. {
  412. struct timeval tv_start, tv_finish;
  413. int i, fd, baud, cores_per_chip, chips_count_max, chips_count, chip_clk;
  414. //int this_option_offset = ++option_offset;
  415. unsigned char freqcode_init, freqcode;
  416. uint32_t nonce;
  417. uint64_t golden_speed_per_core;
  418. /* this check here is needed as a failsafe because the serial_detect
  419. * functions do not keep track of devices already opened */
  420. for (i = 0; i < total_devices; ++i) {
  421. if (devices[i]->device_path && !strcasecmp(devices[i]->device_path, devpath))
  422. return false;
  423. }
  424. uint32_t golden_nonce_val = be32toh(0x268d0300); // 0xd26 = 3366
  425. unsigned char ob_bin[LKETC_COMMAND_PKT_LEN], nonce_bin[LKETC_EVENT_PKT_LEN];
  426. static const char golden_ob[] =
  427. "55aa0001"
  428. "00038000063b0b1b028f32535e900609c15dc49a42b1d8492a6dd4f8f15295c989a1decf584a6aa93be26066d3185f55ef635b5865a7a79b7fa74121a6bb819da416328a9bd2f8cef72794bf02000000";
  429. static const char golden_ob2[] =
  430. "55aa00ff"
  431. "c00278894532091be6f16a5381ad33619dacb9e6a4a6e79956aac97b51112bfb93dc450b8fc765181a344b6244d42d78625f5c39463bbfdc10405ff711dc1222dd065b015ac9c2c66e28da7202000000";
  432. lketc_get_device_options(devpath, &chips_count, &chip_clk, opt_lketc_options);
  433. baud = LKETC_IO_SPEED; // baud rate is fixed
  434. cores_per_chip = LKETC_CHIP_CORES; // cores/chip also fixed
  435. chips_count_max = lowest_pow2(chips_count);
  436. //if (chips_count > opt_lketc_chips_count_max)
  437. // opt_lketc_chips_count_max = lowest_pow2(chips_count);
  438. //chips_count_max = opt_lketc_chips_count_max;
  439. if (initial_startup_phase)
  440. applog(LOG_INFO, "Zeus Detect: Attempting to open %s", devpath);
  441. fd = lketc_serial_open_detect(devpath, baud, true);
  442. if (unlikely(fd == -1)) {
  443. if (initial_startup_phase)
  444. applog(LOG_ERR, "Zeus Detect: Failed to open %s", devpath);
  445. return false;
  446. }
  447. freqcode = lketc_clk_to_freqcode(chip_clk);
  448. // from 150M step to the high or low speed. we need to add delay and resend to init chip
  449. if (chip_clk > 150)
  450. freqcode_init = lketc_clk_to_freqcode(165);
  451. else
  452. freqcode_init = lketc_clk_to_freqcode(139);
  453. flush_uart(fd);
  454. hex2bin(ob_bin, golden_ob2, sizeof(ob_bin));
  455. ob_bin[0] = freqcode_init;
  456. ob_bin[1] = ~freqcode_init;
  457. ob_bin[2] = 0x00;
  458. ob_bin[3] = 0x01;
  459. for (i = 0; i < 2; ++i) {
  460. lketc_serial_write(fd, ob_bin, sizeof(ob_bin));
  461. cgsleep_ms(500); // what is the minimum the miners need/will accept?
  462. flush_uart(fd);
  463. }
  464. hex2bin(ob_bin, golden_ob2, sizeof(ob_bin));
  465. ob_bin[0] = freqcode;
  466. ob_bin[1] = ~freqcode;
  467. ob_bin[2] = 0x00;
  468. ob_bin[3] = 0x01;
  469. for (i = 0; i < 2; ++i) {
  470. lketc_serial_write(fd, ob_bin, sizeof(ob_bin));
  471. cgsleep_ms(500);
  472. flush_uart(fd);
  473. }
  474. if (!opt_lketc_nocheck_golden) {
  475. memset(nonce_bin, 0, sizeof(nonce_bin));
  476. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  477. ob_bin[0] = freqcode;
  478. ob_bin[1] = ~freqcode;
  479. ob_bin[2] = 0x00;
  480. ob_bin[3] = 0x01;
  481. for (i = 0; i < 2; ++i) {
  482. lketc_serial_write(fd, ob_bin, sizeof(ob_bin));
  483. cgtime(&tv_start);
  484. if (lketc_serial_read(fd, nonce_bin, sizeof(nonce_bin), 25, &tv_finish) == sizeof(nonce_bin))
  485. break;
  486. }
  487. lketc_serial_close(fd);
  488. memcpy(&nonce, nonce_bin, sizeof(nonce_bin));
  489. nonce = be32toh(nonce);
  490. if (nonce != golden_nonce_val) {
  491. applog(LOG_ERR, "Zeus Detect: "
  492. "Test failed at %s: got %08x, should be: %08x",
  493. devpath, nonce, golden_nonce_val);
  494. return false;
  495. }
  496. golden_speed_per_core = (uint64_t)((double)0xd26 / tdiff(&tv_finish, &tv_start));
  497. if (opt_lketc_debug)
  498. applog(LOG_INFO, "Test succeeded at %s: got %08x",
  499. devpath, nonce);
  500. } else {
  501. lketc_serial_close(fd);
  502. golden_speed_per_core = (((chip_clk * 2.) / 3.) * 1024.) / 8.;
  503. }
  504. /* We have a real Zeus miner! */
  505. struct cgpu_info *lketc;
  506. struct LKETC_INFO *info;
  507. lketc = calloc(1, sizeof(struct cgpu_info));
  508. if (unlikely(!lketc))
  509. quit(1, "Failed to malloc struct cgpu_info");
  510. info = calloc(1, sizeof(struct LKETC_INFO));
  511. if (unlikely(!info))
  512. quit(1, "Failed to malloc struct LKETC_INFO");
  513. lketc->drv = &lketc_drv;
  514. lketc->name = lketc_device_name(chips_count);
  515. lketc->device_path = strdup(devpath);
  516. lketc->device_data = info;
  517. lketc->deven = DEV_ENABLED;
  518. lketc->threads = 1;
  519. applog(LOG_NOTICE, "Found Zeus at %s, mark as %d",
  520. devpath, lketc->device_id);
  521. applog(LOG_INFO, "Zeus: Init: %d baud=%d cores_per_chip=%d chips_count=%d",
  522. lketc->device_id, baud, cores_per_chip, chips_count);
  523. info->device_fd = -1;
  524. info->using_libusb = 0;
  525. lketc->unique_id = MAX( strrchr(lketc->device_path, '/'),
  526. strrchr(lketc->device_path, '\\'));
  527. if (lketc->unique_id == NULL)
  528. lketc->unique_id = lketc->device_path;
  529. else
  530. ++lketc->unique_id;
  531. strncpy(info->device_name, lketc->unique_id, sizeof(info->device_name) - 1);
  532. info->device_name[sizeof(info->device_name) - 1] = '\0';
  533. info->work_timeout.tv_sec = 4294967296LL / (golden_speed_per_core * cores_per_chip * chips_count_max) * 0.9;
  534. info->work_timeout.tv_usec = 0;
  535. info->golden_speed_per_core = golden_speed_per_core;
  536. info->read_count = (uint32_t)((4294967296LL*10)/(cores_per_chip*chips_count_max*golden_speed_per_core*2));
  537. info->read_count = info->read_count*3/4;
  538. info->next_chip_clk = -1;
  539. info->freqcode = freqcode;
  540. info->baud = baud;
  541. info->cores_per_chip = cores_per_chip;
  542. info->chips_count = chips_count;
  543. info->chips_count_max = chips_count_max;
  544. if ((chips_count_max & (chips_count_max - 1)) != 0)
  545. quit(1, "chips_count_max must be a power of 2");
  546. info->chip_clk = chip_clk;
  547. info->chips_bit_num = log_2(chips_count_max);
  548. if (!add_cgpu(lketc))
  549. quit(1, "Failed to add_cgpu");
  550. return true;
  551. }
  552. /************************************************************
  553. * Host <-> ASIC protocol implementation
  554. ************************************************************/
  555. static void lketc_purge_work(struct cgpu_info *lketc)
  556. {
  557. struct LKETC_INFO *info = lketc->device_data;
  558. mutex_lock(&info->lock);
  559. if (info->current_work != NULL) {
  560. free_work(info->current_work);
  561. info->current_work = NULL;
  562. }
  563. notify_send_work_thread(lketc);
  564. mutex_unlock(&info->lock);
  565. }
  566. #define nonce_range_start(cperc, cmax, core, chip) \
  567. (((0xffffffff / cperc + 1) * core) + ((0x1fffffff / cmax + 1) * chip))
  568. static bool lketc_read_response(struct cgpu_info *lketc)
  569. {
  570. struct LKETC_INFO *info = lketc->device_data;
  571. unsigned char evtpkt[LKETC_EVENT_PKT_LEN];
  572. uint32_t nonce, chip, core;
  573. int ret, err;
  574. double duration_s;
  575. bool valid;
  576. if (using_libusb(info)) {
  577. err = usb_read_timeout(lketc, (char *)evtpkt, sizeof(evtpkt), &ret, 250, C_GETRESULTS);
  578. if (err != LIBUSB_SUCCESS && err != LIBUSB_ERROR_TIMEOUT) {
  579. applog(LOG_ERR, "%s%d: USB read error: %s",
  580. lketc->drv->name, lketc->device_id, libusb_error_name(err));
  581. return false;
  582. } else if (err == LIBUSB_ERROR_TIMEOUT) {
  583. return true;
  584. }
  585. } else {
  586. ret = lketc_serial_read(info->device_fd, evtpkt, sizeof(evtpkt), 1, NULL);
  587. if (ret < 0) { // error
  588. info->serial_reopen = true;
  589. notify_send_work_thread(lketc);
  590. return false;
  591. } else if (ret == 0) { // timeout
  592. return true;
  593. }
  594. flush_uart(info->device_fd);
  595. }
  596. cgtime(&info->workend);
  597. memcpy(&nonce, evtpkt, sizeof(evtpkt));
  598. nonce = be32toh(nonce);
  599. mutex_lock(&info->lock);
  600. if (info->current_work == NULL) { // work was flushed before we read response
  601. applog(LOG_DEBUG, "%s%d: Received nonce for flushed work",
  602. lketc->drv->name, lketc->device_id);
  603. mutex_unlock(&info->lock);
  604. return true;
  605. }
  606. valid = submit_nonce(info->thr, info->current_work, nonce);
  607. ++info->workdone;
  608. core = (nonce & 0xe0000000) >> 29; // core indicated by 3 highest bits
  609. chip = (nonce & 0x1ff80000) >> (29 - info->chips_bit_num);
  610. duration_s = tdiff(&info->workend, &info->workstart);
  611. if (chip < LKETC_MAX_CHIPS && core < LKETC_CHIP_CORES) {
  612. ++info->nonce_count[chip][core];
  613. if (!valid)
  614. ++info->error_count[chip][core];
  615. if (valid && duration_s > 0) {
  616. info->hashes_per_s = (nonce - nonce_range_start(info->cores_per_chip, info->chips_count_max, core, chip)) / duration_s * info->cores_per_chip * info->chips_count;
  617. info->last_nonce = nonce;
  618. }
  619. } else {
  620. applog(LOG_INFO, "%s%d: Corrupt nonce message received, cannot determine chip and core",
  621. lketc->drv->name, lketc->device_id);
  622. }
  623. mutex_unlock(&info->lock);
  624. return true;
  625. }
  626. static bool lketc_check_need_work(struct cgpu_info *lketc)
  627. {
  628. struct LKETC_INFO *info = lketc->device_data;
  629. struct thr_info *thr = info->thr;
  630. struct work *work;
  631. bool need_work;
  632. need_work = (info->current_work == NULL);
  633. if (need_work) {
  634. work = get_work(thr, thr->id); // get_work can block, so done outside mutex_lock
  635. mutex_lock(&info->lock);
  636. if (info->current_work == NULL) { // verify still NULL
  637. work->devflag = false;
  638. info->current_work = work;
  639. } else {
  640. need_work = false;
  641. }
  642. mutex_unlock(&info->lock);
  643. if (!need_work)
  644. discard_work(work);
  645. }
  646. return need_work;
  647. }
  648. static bool lketc_send_work(struct cgpu_info *lketc, struct work *work)
  649. {
  650. struct LKETC_INFO *info = lketc->device_data;
  651. unsigned char cmdpkt[LKETC_COMMAND_PKT_LEN];
  652. uint32_t diff_code, diff;
  653. int ret;
  654. diff = work->work_difficulty;
  655. if (diff < 1)
  656. diff = 1;
  657. diff_code = 0xffff / diff;
  658. applog(LOG_DEBUG, "lketc_send_work: diff=%d diff_code=%04x", diff, diff_code);
  659. cmdpkt[0] = info->freqcode;
  660. cmdpkt[1] = ~(info->freqcode);
  661. cmdpkt[2] = (diff_code & 0xff00) >> 8;
  662. cmdpkt[3] = (diff_code & 0x00ff);
  663. memcpy(cmdpkt + 4, work->data, 80);
  664. rev(cmdpkt + 4, 80);
  665. if (using_libusb(info)) { // in libusb mode we send via usb ;)
  666. if (usb_write(lketc, (char *)cmdpkt, sizeof(cmdpkt), &ret, C_SENDWORK) != LIBUSB_SUCCESS ||
  667. ret != sizeof(cmdpkt))
  668. return false;
  669. } else { // otherwise direct via serial port
  670. ret = lketc_serial_write(info->device_fd, cmdpkt, sizeof(cmdpkt));
  671. if (ret < 0) {
  672. info->serial_reopen = true;
  673. notify_send_work_thread(lketc);
  674. return false;
  675. }
  676. }
  677. return true;
  678. }
  679. static void *lketc_send_work_thread(void *data)
  680. {
  681. struct cgpu_info *lketc = (struct cgpu_info *)data;
  682. struct LKETC_INFO *info = lketc->device_data;
  683. char threadname[24];
  684. struct timeval tv_now, tv_spent, tv_rem;
  685. int retval;
  686. snprintf(threadname, sizeof(threadname), "Zeus/%d", lketc->device_id);
  687. RenameThread(threadname);
  688. applog(LOG_INFO, "%s%d: serial I/O thread running, %s",
  689. lketc->drv->name, lketc->device_id, threadname);
  690. while (likely(!lketc->shutdown)) {
  691. if (unlikely(info->thr->pause || lketc->deven != DEV_ENABLED)) {
  692. cgsem_wait(&info->wusem);
  693. lketc_purge_work(lketc);
  694. continue;
  695. }
  696. if (unlikely(using_libusb(info) && lketc->usbinfo.nodev))
  697. break;
  698. if (unlikely(info->serial_reopen)) {
  699. if (using_serial(info) && !lketc_reopen(lketc)) {
  700. applog(LOG_ERR, "Failed to reopen %s%d on %s, shutting down",
  701. lketc->drv->name, lketc->device_id, lketc->device_path);
  702. break;
  703. }
  704. info->serial_reopen = false;
  705. lketc_purge_work(lketc);
  706. }
  707. lketc_check_need_work(lketc);
  708. mutex_lock(&info->lock);
  709. if (info->current_work && !info->current_work->devflag) {
  710. /* send task to device */
  711. if (opt_lketc_debug)
  712. applog(LOG_INFO, "Sending work");
  713. if (lketc_send_work(lketc, info->current_work)) {
  714. info->current_work->devflag = true;
  715. cgtime(&info->workstart);
  716. if (info->next_chip_clk != -1) {
  717. info->chip_clk = info->next_chip_clk;
  718. info->next_chip_clk = -1;
  719. }
  720. } else {
  721. applog(LOG_NOTICE, "%s%d: I/O error while sending work, will retry",
  722. lketc->drv->name, lketc->device_id);
  723. mutex_unlock(&info->lock);
  724. continue;
  725. }
  726. }
  727. mutex_unlock(&info->lock);
  728. cgtime(&tv_now);
  729. timersub(&tv_now, &info->workstart, &tv_spent);
  730. timersub(&info->work_timeout, &tv_spent, &tv_rem);
  731. if (opt_lketc_debug) {
  732. applog(LOG_DEBUG, "Workstart: %d.%06d", (int)info->workstart.tv_sec, (int)info->workstart.tv_usec);
  733. applog(LOG_DEBUG, "Spent: %d.%06d", (int)tv_spent.tv_sec, (int)tv_spent.tv_usec);
  734. applog(LOG_DEBUG, "Remaining: %d.%06d", (int)tv_rem.tv_sec, (int)tv_rem.tv_usec);
  735. }
  736. retval = cgsem_mswait(&info->wusem, (tv_rem.tv_sec < 1) ? 5000 : tv_rem.tv_sec * 1000);
  737. if (retval == ETIMEDOUT)
  738. lketc_purge_work(lketc); // abandon current work
  739. }
  740. lketc->shutdown = true;
  741. return NULL;
  742. }
  743. /************************************************************
  744. * CGMiner Interface functions
  745. ************************************************************/
  746. static int lketc_autoscan()
  747. {
  748. int found = 0;
  749. applog(LOG_DEBUG, "lketc_autoscan() called");
  750. found += serial_autodetect_udev(lketc_detect_one_serial, LKETC_USB_ID_MODEL_STR1);
  751. found += serial_autodetect_udev(lketc_detect_one_serial, LKETC_USB_ID_MODEL_STR2);
  752. return found;
  753. }
  754. static void lketc_detect(bool __maybe_unused hotplug)
  755. {
  756. static int serial_usb = 0;
  757. if (initial_startup_phase && hotplug)
  758. initial_startup_phase = false;
  759. if (serial_usb == 0)
  760. serial_usb = (list_empty(&scan_devices)) ? -1 : 1;
  761. if (serial_usb < 0)
  762. usb_detect(&lketc_drv, lketc_detect_one_usb);
  763. else
  764. serial_detect_iauto(&lketc_drv, lketc_detect_one_serial, lketc_autoscan);
  765. }
  766. static bool lketc_prepare(struct thr_info *thr)
  767. {
  768. struct cgpu_info *lketc = thr->cgpu;
  769. struct LKETC_INFO *info = lketc->device_data;
  770. applog(LOG_NOTICE, "%s%d opened on %s",
  771. lketc->drv->name, lketc->device_id, lketc->device_path);
  772. info->serial_reopen = (using_serial(info)) ? true : false;
  773. info->thr = thr;
  774. mutex_init(&info->lock);
  775. cgsem_init(&info->wusem);
  776. // Use qualitative value until first result is returned
  777. info->hashes_per_s = info->golden_speed_per_core * info->cores_per_chip * info->chips_count;
  778. return true;
  779. }
  780. static bool lketc_thread_init(struct thr_info *thr)
  781. {
  782. struct cgpu_info *lketc = thr->cgpu;
  783. struct LKETC_INFO *info = lketc->device_data;
  784. if (pthread_create(&info->sworkpth, NULL, lketc_send_work_thread, lketc)) {
  785. applog(LOG_ERR, "%s%d: Failed to create I/O thread",
  786. lketc->drv->name, lketc->device_id);
  787. return false;
  788. }
  789. return true;
  790. }
  791. static int64_t lketc_scanwork(struct thr_info *thr)
  792. {
  793. struct cgpu_info *lketc = thr->cgpu;
  794. struct LKETC_INFO *info = lketc->device_data;
  795. struct timeval old_scanwork_time;
  796. double elapsed_s;
  797. int64_t estimate_hashes;
  798. if (unlikely(using_libusb(info) && lketc->usbinfo.nodev))
  799. return -1;
  800. if (unlikely(using_serial(info) && info->serial_reopen)) {
  801. cgsleep_ms(500);
  802. return 0;
  803. }
  804. if (unlikely(lketc_read_response(lketc) < 0)) // reads either from serial or libusb or times out
  805. return 0;
  806. if (thr->work_restart || thr->work_update) {
  807. lketc_purge_work(lketc);
  808. thr->work_restart = false;
  809. thr->work_update = false;
  810. }
  811. mutex_lock(&info->lock);
  812. old_scanwork_time = info->scanwork_time;
  813. cgtime(&info->scanwork_time);
  814. elapsed_s = tdiff(&info->scanwork_time, &old_scanwork_time);
  815. estimate_hashes = elapsed_s * info->hashes_per_s;
  816. mutex_unlock(&info->lock);
  817. if (unlikely(estimate_hashes > 0xffffffff))
  818. estimate_hashes = 0xffffffff;
  819. return estimate_hashes;
  820. }
  821. #define lketc_update_work lketc_flush_work
  822. static void lketc_flush_work(struct cgpu_info *lketc)
  823. {
  824. lketc_purge_work(lketc);
  825. if (opt_lketc_debug)
  826. applog(LOG_INFO, "lketc_flush_work: Tickling I/O thread");
  827. }
  828. static struct api_data *lketc_api_stats(struct cgpu_info *lketc)
  829. {
  830. struct LKETC_INFO *info = lketc->device_data;
  831. struct api_data *root = NULL;
  832. static struct timeval tv_now, tv_diff, tv_diff2;
  833. static double khs_core, khs_chip, khs_board;
  834. cgtime(&tv_now);
  835. timersub(&tv_now, &(info->workstart), &tv_diff);
  836. timersub(&(info->workend), &(info->workstart), &tv_diff2);
  837. root = api_add_string(root, "Device Name", lketc->unique_id, false);
  838. khs_core = (double)info->golden_speed_per_core / 1000.;
  839. khs_chip = (double)info->golden_speed_per_core * (double)info->cores_per_chip / 1000.;
  840. khs_board = (double)info->golden_speed_per_core * (double)info->cores_per_chip * (double)info->chips_count / 1000.;
  841. root = api_add_khs(root, "KHS/Core", &khs_core, false);
  842. root = api_add_khs(root, "KHS/Chip", &khs_chip, false);
  843. root = api_add_khs(root, "KHS/Board", &khs_board, false);
  844. root = api_add_int(root, "Frequency", &(info->chip_clk), false);
  845. root = api_add_int(root, "Cores/Chip", &(info->cores_per_chip), false);
  846. root = api_add_int(root, "Chips Count", &(info->chips_count), false);
  847. root = api_add_timeval(root, "Time Spent Current Work", &tv_diff, false);
  848. root = api_add_timeval(root, "Work Timeout", &(info->work_timeout), false);
  849. /* It would be nice to report per chip/core nonce and error counts,
  850. * but with more powerful miners with > 100 chips each with 8 cores
  851. * there is too much information and we'd overflow the api buffer.
  852. * Perhaps another api command to query individual chips? */
  853. /* these values are more for diagnostic and debugging */
  854. if (opt_lketc_debug) {
  855. root = api_add_int(root, "chips_count_max", &(info->chips_count_max), false);
  856. root = api_add_int(root, "chips_bit_num", &(info->chips_bit_num), false);
  857. root = api_add_uint32(root, "read_count", &(info->read_count), false);
  858. root = api_add_double(root, "hashes_per_s", &(info->hashes_per_s), false);
  859. root = api_add_uint32(root, "last_nonce", &(info->last_nonce), false);
  860. root = api_add_timeval(root, "last_nonce_time", &tv_diff2, false);
  861. }
  862. return root;
  863. }
  864. static void lketc_get_statline_before(char *buf, size_t bufsiz, struct cgpu_info *lketc)
  865. {
  866. struct LKETC_INFO *info = lketc->device_data;
  867. if (lketc->name)
  868. tailsprintf(buf, bufsiz, "%-12s %4d MHz ", lketc->name, info->chip_clk);
  869. else
  870. tailsprintf(buf, bufsiz, "%4d chips %4d MHz ", info->chips_count, info->chip_clk);
  871. }
  872. static char *lketc_set_device(struct cgpu_info *lketc, char *option, char *setting, char *replybuf)
  873. {
  874. struct LKETC_INFO *info = lketc->device_data;
  875. int val;
  876. if (strcasecmp(option, "help") == 0) {
  877. sprintf(replybuf, "freq: range %d-%d, abortwork: true/false",
  878. LKETC_CLK_MIN, LKETC_CLK_MAX);
  879. return replybuf;
  880. }
  881. if (strcasecmp(option, "freq") == 0) {
  882. if (!setting || !*setting) {
  883. sprintf(replybuf, "missing freq setting");
  884. return replybuf;
  885. }
  886. val = atoi(setting);
  887. if (val < LKETC_CLK_MIN || val > LKETC_CLK_MAX) {
  888. sprintf(replybuf, "invalid freq: '%s' valid range %d-%d",
  889. setting, LKETC_CLK_MIN, LKETC_CLK_MAX);
  890. return replybuf;
  891. }
  892. mutex_lock(&info->lock);
  893. info->next_chip_clk = val;
  894. info->freqcode = lketc_clk_to_freqcode(val);
  895. mutex_unlock(&info->lock);
  896. return NULL;
  897. }
  898. if (strcasecmp(option, "abortwork") == 0) {
  899. if (!setting || !*setting) {
  900. sprintf(replybuf, "missing true/false");
  901. return replybuf;
  902. }
  903. if (strcasecmp(setting, "true") != 0) {
  904. sprintf(replybuf, "not aborting current work");
  905. return replybuf;
  906. }
  907. lketc_purge_work(lketc);
  908. return NULL;
  909. }
  910. sprintf(replybuf, "Unknown option: %s", option);
  911. return replybuf;
  912. }
  913. static void lketc_thread_enable(struct thr_info *thr)
  914. {
  915. struct cgpu_info *lketc = thr->cgpu;
  916. notify_send_work_thread(lketc);
  917. }
  918. static void lketc_shutdown(struct thr_info *thr)
  919. {
  920. struct cgpu_info *lketc = thr->cgpu;
  921. struct LKETC_INFO *info = lketc->device_data;
  922. applog(LOG_NOTICE, "%s%d: Shutting down", lketc->drv->name, lketc->device_id);
  923. pthread_join(info->sworkpth, NULL);
  924. mutex_destroy(&info->lock);
  925. cgsem_destroy(&info->wusem);
  926. if (info->device_fd != -1) {
  927. lketc_serial_close(info->device_fd);
  928. info->device_fd = -1;
  929. }
  930. }
  931. struct device_drv lketc_drv = {
  932. .drv_id = DRIVER_lketc,
  933. .dname = "Zeus",
  934. .name = "LKE",
  935. .max_diff = 32768,
  936. .drv_detect = lketc_detect,
  937. .thread_prepare = lketc_prepare,
  938. .thread_init = lketc_thread_init,
  939. .hash_work = hash_driver_work,
  940. .scanwork = lketc_scanwork,
  941. .flush_work = lketc_flush_work,
  942. .update_work = lketc_update_work,
  943. .get_api_stats = lketc_api_stats,
  944. .get_statline_before = lketc_get_statline_before,
  945. .set_device = lketc_set_device,
  946. .thread_enable = lketc_thread_enable,
  947. .thread_shutdown = lketc_shutdown,
  948. };