driver-lketc.c 32 KB

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