driver-zeus.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. /*
  2. * Copyright 2013-2014 Con Kolivas <kernel@kolivas.org>
  3. * Copyright 2014 Zeus 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-zeus.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_zeus_debug;
  41. extern int opt_zeus_chips_count; // number of Zeus chips chained together
  42. extern int opt_zeus_chip_clk; // frequency to run chips with
  43. extern bool opt_zeus_nocheck_golden; // bypass hashrate check
  44. //static int opt_zeus_chips_count_max = 1; // smallest power of 2 >= opt_zeus_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. } zeus_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 *zeus)
  123. {
  124. struct ZEUS_INFO *info = zeus->device_data;
  125. cgsem_post(&info->wusem);
  126. }
  127. /************************************************************
  128. * I/O helper functions
  129. ************************************************************/
  130. #define zeus_serial_open_detect(devpath, baud, purge) serial_open_ex(devpath, baud, ZEUS_READ_FAULT_DECISECONDS, 0, purge, true)
  131. #define zeus_serial_open(devpath, baud, purge) serial_open_ex(devpath, baud, ZEUS_READ_FAULT_DECISECONDS, 0, purge, true)
  132. #define zeus_serial_close(fd) close(fd)
  133. static bool zeus_reopen(struct cgpu_info *zeus)
  134. {
  135. struct ZEUS_INFO *info = zeus->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. zeus->drv->name, zeus->device_id, zeus->device_path, info->device_fd);
  142. zeus_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. zeus->drv->name, zeus->device_id, zeus->device_path);
  148. for (try = 0; try < 3; ++try) {
  149. fd = zeus_serial_open(zeus->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. zeus->drv->name, zeus->device_id, zeus->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. zeus->drv->name, zeus->device_id, zeus->device_path, try, info->device_fd);
  162. return true;
  163. }
  164. static int zeus_serial_write(int fd, const void *buf, size_t len)
  165. {
  166. ssize_t ret;
  167. size_t total = 0;
  168. #if ZEUS_PROTOCOL_DEBUG
  169. if (opt_zeus_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, "zeus_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 zeus_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, "zeus_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, "zeus_serial_read: read returned %d", (int)ret);
  208. if (ret == 0 && ++rc >= read_count)
  209. break;
  210. total += (size_t)ret;
  211. }
  212. #if ZEUS_PROTOCOL_DEBUG
  213. if (opt_zeus_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 zeus_clk_to_freqcode(int clkfreq)
  230. {
  231. if (clkfreq > ZEUS_CLK_MAX) {
  232. applog(LOG_WARNING, "Clock frequency %d too high, resetting to %d",
  233. clkfreq, ZEUS_CLK_MAX);
  234. clkfreq = ZEUS_CLK_MAX;
  235. }
  236. if (clkfreq < ZEUS_CLK_MIN) {
  237. applog(LOG_WARNING, "Clock frequency %d too low, resetting to %d",
  238. clkfreq, ZEUS_CLK_MIN);
  239. clkfreq = ZEUS_CLK_MIN;
  240. }
  241. return (unsigned char)((double)clkfreq * 2. / 3.);
  242. }
  243. static void zeus_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_zeus_chips_count) ? opt_zeus_chips_count : ZEUS_MIN_CHIPS;
  253. *chip_clk = (opt_zeus_chip_clk) ? opt_zeus_chip_clk : ZEUS_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 < ZEUS_MIN_CHIPS || lval > ZEUS_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 < ZEUS_CLK_MIN || lval > ZEUS_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 *zeus_device_name(int chips_count)
  298. {
  299. struct name_chip_map *p;
  300. for (p = zeus_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 zeus_usb_control_transfer_data(struct cgpu_info *zeus, 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(zeus, 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. zeus->drv->name, zeus->cgminer_id, err, usb_cmdname(cmd));
  313. return err;
  314. }
  315. static inline int zeus_usb_control_transfer(struct cgpu_info *zeus, uint8_t request_type, uint8_t bRequest,
  316. uint16_t wValue, uint16_t wIndex, enum usb_cmds cmd)
  317. {
  318. return zeus_usb_control_transfer_data(zeus, request_type, bRequest, wValue, wIndex, NULL, 0, cmd);
  319. }
  320. static bool zeus_initialize_cp2102(struct cgpu_info *zeus)
  321. {
  322. int interface = usb_interface(zeus);
  323. //uint32_t baudrate = CP210X_DATA_BAUD;
  324. // Enable the UART
  325. if (zeus_usb_control_transfer(zeus, 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 (zeus_usb_control_transfer(zeus, 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 (zeus_usb_control_transfer_data(zeus, CP210X_TYPE_OUT, CP210X_REQUEST_BAUD,
  335. // 0, interface, &baudrate, sizeof(baudrate), C_SETBAUD))
  336. // return false;
  337. return true;
  338. }
  339. static bool zeus_initialize_ftdi(struct cgpu_info *zeus)
  340. {
  341. int interface = usb_interface(zeus);
  342. // Reset
  343. if (zeus_usb_control_transfer(zeus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  344. FTDI_VALUE_RESET, interface, C_RESET))
  345. return false;
  346. // Latency
  347. usb_ftdi_set_latency(zeus);
  348. // Data
  349. if (zeus_usb_control_transfer(zeus, FTDI_TYPE_OUT, FTDI_REQUEST_DATA,
  350. FTDI_VALUE_DATA_ZUS, interface, C_SETDATA))
  351. return false;
  352. // Baudrate
  353. if (zeus_usb_control_transfer(zeus, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD,
  354. FTDI_VALUE_BAUD_ZUS, (FTDI_INDEX_BAUD_ZUS & 0xff00) | interface, C_SETBAUD))
  355. return false;
  356. // Modem control
  357. if (zeus_usb_control_transfer(zeus, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM,
  358. FTDI_VALUE_MODEM, interface, C_SETMODEM))
  359. return false;
  360. // Flow control
  361. if (zeus_usb_control_transfer(zeus, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW,
  362. FTDI_VALUE_FLOW, interface, C_SETFLOW))
  363. return false;
  364. // Clear buffers
  365. if (zeus_usb_control_transfer(zeus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  366. FTDI_VALUE_PURGE_TX, interface, C_PURGETX))
  367. return false;
  368. if (zeus_usb_control_transfer(zeus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
  369. FTDI_VALUE_PURGE_RX, interface, C_PURGERX))
  370. return false;
  371. return true;
  372. }
  373. static bool zeus_initialize_usb(struct cgpu_info *zeus)
  374. {
  375. enum sub_ident ident;
  376. if (zeus->usbinfo.nodev)
  377. return false;
  378. ident = usb_ident(zeus);
  379. switch (ident) {
  380. case IDENT_ZUS1:
  381. return zeus_initialize_cp2102(zeus);
  382. case IDENT_ZUS2:
  383. return zeus_initialize_ftdi(zeus);
  384. default:
  385. applog(LOG_ERR, "zeus_initialize_usb called on wrong device, ident=%d", ident);
  386. return false;
  387. }
  388. }
  389. static struct cgpu_info *zeus_detect_one_usb(struct libusb_device *dev, struct usb_find_devices *found)
  390. {
  391. struct cgpu_info *zeus;
  392. struct ZEUS_INFO *info;
  393. zeus = usb_alloc_cgpu(&zeus_drv, 1);
  394. if (!usb_init(zeus, dev, found))
  395. goto usbdealloc;
  396. info = calloc(1, sizeof(struct ZEUS_INFO));
  397. if (unlikely(!info))
  398. goto usbdealloc;
  399. zeus->device_data = info;
  400. zeus->deven = DEV_ENABLED;
  401. zeus->threads = 1;
  402. info->device_fd = -1;
  403. info->using_libusb = 1;
  404. if (zeus->usbdev->serial_string && strlen(zeus->usbdev->serial_string) > 4)
  405. zeus->unique_id = zeus->usbdev->serial_string;
  406. else
  407. zeus->unique_id = zeus->device_path;
  408. strncpy(info->device_name, zeus->unique_id, sizeof(info->device_name) - 1);
  409. info->device_name[sizeof(info->device_name) - 1] = '\0';
  410. zeus_get_device_options(zeus->unique_id, &info->chips_count, &info->chip_clk, opt_zeus_options);
  411. zeus->name = zeus_device_name(info->chips_count);
  412. info->freqcode = zeus_clk_to_freqcode(info->chip_clk);
  413. info->baud = ZEUS_IO_SPEED;
  414. info->cores_per_chip = ZEUS_CHIP_CORES;
  415. info->chips_count_max = lowest_pow2(info->chips_count);
  416. info->chips_bit_num = log_2(info->chips_count_max);
  417. info->next_chip_clk = -1;
  418. libusb_reset_device(zeus->usbdev->handle);
  419. update_usb_stats(zeus);
  420. zeus->usbdev->usb_type = USB_TYPE_STD;
  421. if (!zeus_initialize_usb(zeus)) {
  422. applog(LOG_ERR, "Failed to initialize Zeus USB-UART interface");
  423. goto alldealloc;
  424. }
  425. info->golden_speed_per_core = (((info->chip_clk * 2.) / 3.) * 1024.) / 8.;
  426. info->work_timeout.tv_sec = 4294967296LL / (info->golden_speed_per_core * info->cores_per_chip * info->chips_count_max) * 0.9;
  427. info->work_timeout.tv_usec = 0;
  428. info->read_count = (uint32_t)((4294967296LL*10)/(info->cores_per_chip*info->chips_count_max*info->golden_speed_per_core*2));
  429. info->read_count = info->read_count*3/4;
  430. if (!add_cgpu(zeus))
  431. goto alldealloc;
  432. return zeus;
  433. alldealloc:
  434. usb_uninit(zeus);
  435. free(zeus->device_data);
  436. zeus->device_data = NULL;
  437. usbdealloc:
  438. zeus = usb_free_cgpu(zeus);
  439. return NULL;
  440. }
  441. static bool zeus_detect_one_serial(const char *devpath)
  442. {
  443. struct timeval tv_start, tv_finish;
  444. int i, fd, baud, cores_per_chip, chips_count_max, chips_count, chip_clk;
  445. //int this_option_offset = ++option_offset;
  446. unsigned char freqcode_init, freqcode;
  447. uint32_t nonce;
  448. uint64_t golden_speed_per_core;
  449. /* this check here is needed as a failsafe because the serial_detect
  450. * functions do not keep track of devices already opened */
  451. for (i = 0; i < total_devices; ++i) {
  452. if (devices[i]->device_path && !strcasecmp(devices[i]->device_path, devpath))
  453. return false;
  454. }
  455. uint32_t golden_nonce_val = be32toh(0x268d0300); // 0xd26 = 3366
  456. unsigned char ob_bin[ZEUS_COMMAND_PKT_LEN], nonce_bin[ZEUS_EVENT_PKT_LEN];
  457. static const char golden_ob[] =
  458. "55aa0001"
  459. "00038000063b0b1b028f32535e900609c15dc49a42b1d8492a6dd4f8f15295c989a1decf584a6aa93be26066d3185f55ef635b5865a7a79b7fa74121a6bb819da416328a9bd2f8cef72794bf02000000";
  460. static const char golden_ob2[] =
  461. "55aa00ff"
  462. "c00278894532091be6f16a5381ad33619dacb9e6a4a6e79956aac97b51112bfb93dc450b8fc765181a344b6244d42d78625f5c39463bbfdc10405ff711dc1222dd065b015ac9c2c66e28da7202000000";
  463. zeus_get_device_options(devpath, &chips_count, &chip_clk, opt_zeus_options);
  464. baud = ZEUS_IO_SPEED; // baud rate is fixed
  465. cores_per_chip = ZEUS_CHIP_CORES; // cores/chip also fixed
  466. chips_count_max = lowest_pow2(chips_count);
  467. //if (chips_count > opt_zeus_chips_count_max)
  468. // opt_zeus_chips_count_max = lowest_pow2(chips_count);
  469. //chips_count_max = opt_zeus_chips_count_max;
  470. if (initial_startup_phase)
  471. applog(LOG_INFO, "Zeus Detect: Attempting to open %s", devpath);
  472. fd = zeus_serial_open_detect(devpath, baud, true);
  473. if (unlikely(fd == -1)) {
  474. if (initial_startup_phase)
  475. applog(LOG_ERR, "Zeus Detect: Failed to open %s", devpath);
  476. return false;
  477. }
  478. freqcode = zeus_clk_to_freqcode(chip_clk);
  479. // from 150M step to the high or low speed. we need to add delay and resend to init chip
  480. if (chip_clk > 150)
  481. freqcode_init = zeus_clk_to_freqcode(165);
  482. else
  483. freqcode_init = zeus_clk_to_freqcode(139);
  484. flush_uart(fd);
  485. hex2bin(ob_bin, golden_ob2, sizeof(ob_bin));
  486. ob_bin[0] = freqcode_init;
  487. ob_bin[1] = ~freqcode_init;
  488. ob_bin[2] = 0x00;
  489. ob_bin[3] = 0x01;
  490. for (i = 0; i < 2; ++i) {
  491. zeus_serial_write(fd, ob_bin, sizeof(ob_bin));
  492. cgsleep_ms(500); // what is the minimum the miners need/will accept?
  493. flush_uart(fd);
  494. }
  495. hex2bin(ob_bin, golden_ob2, sizeof(ob_bin));
  496. ob_bin[0] = freqcode;
  497. ob_bin[1] = ~freqcode;
  498. ob_bin[2] = 0x00;
  499. ob_bin[3] = 0x01;
  500. for (i = 0; i < 2; ++i) {
  501. zeus_serial_write(fd, ob_bin, sizeof(ob_bin));
  502. cgsleep_ms(500);
  503. flush_uart(fd);
  504. }
  505. if (!opt_zeus_nocheck_golden) {
  506. memset(nonce_bin, 0, sizeof(nonce_bin));
  507. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  508. ob_bin[0] = freqcode;
  509. ob_bin[1] = ~freqcode;
  510. ob_bin[2] = 0x00;
  511. ob_bin[3] = 0x01;
  512. for (i = 0; i < 2; ++i) {
  513. zeus_serial_write(fd, ob_bin, sizeof(ob_bin));
  514. cgtime(&tv_start);
  515. if (zeus_serial_read(fd, nonce_bin, sizeof(nonce_bin), 25, &tv_finish) == sizeof(nonce_bin))
  516. break;
  517. }
  518. zeus_serial_close(fd);
  519. memcpy(&nonce, nonce_bin, sizeof(nonce_bin));
  520. nonce = be32toh(nonce);
  521. if (nonce != golden_nonce_val) {
  522. applog(LOG_ERR, "Zeus Detect: "
  523. "Test failed at %s: got %08x, should be: %08x",
  524. devpath, nonce, golden_nonce_val);
  525. return false;
  526. }
  527. golden_speed_per_core = (uint64_t)((double)0xd26 / tdiff(&tv_finish, &tv_start));
  528. if (opt_zeus_debug)
  529. applog(LOG_INFO, "Test succeeded at %s: got %08x",
  530. devpath, nonce);
  531. } else {
  532. zeus_serial_close(fd);
  533. golden_speed_per_core = (((chip_clk * 2.) / 3.) * 1024.) / 8.;
  534. }
  535. /* We have a real Zeus miner! */
  536. struct cgpu_info *zeus;
  537. struct ZEUS_INFO *info;
  538. zeus = calloc(1, sizeof(struct cgpu_info));
  539. if (unlikely(!zeus))
  540. quit(1, "Failed to malloc struct cgpu_info");
  541. info = calloc(1, sizeof(struct ZEUS_INFO));
  542. if (unlikely(!info))
  543. quit(1, "Failed to malloc struct ZEUS_INFO");
  544. zeus->drv = &zeus_drv;
  545. zeus->name = zeus_device_name(chips_count);
  546. zeus->device_path = strdup(devpath);
  547. zeus->device_data = info;
  548. zeus->deven = DEV_ENABLED;
  549. zeus->threads = 1;
  550. applog(LOG_NOTICE, "Found Zeus at %s, mark as %d",
  551. devpath, zeus->device_id);
  552. applog(LOG_INFO, "Zeus: Init: %d baud=%d cores_per_chip=%d chips_count=%d",
  553. zeus->device_id, baud, cores_per_chip, chips_count);
  554. info->device_fd = -1;
  555. info->using_libusb = 0;
  556. zeus->unique_id = MAX( strrchr(zeus->device_path, '/'),
  557. strrchr(zeus->device_path, '\\'));
  558. if (zeus->unique_id == NULL)
  559. zeus->unique_id = zeus->device_path;
  560. else
  561. ++zeus->unique_id;
  562. strncpy(info->device_name, zeus->unique_id, sizeof(info->device_name) - 1);
  563. info->device_name[sizeof(info->device_name) - 1] = '\0';
  564. info->work_timeout.tv_sec = 4294967296LL / (golden_speed_per_core * cores_per_chip * chips_count_max) * 0.9;
  565. info->work_timeout.tv_usec = 0;
  566. info->golden_speed_per_core = golden_speed_per_core;
  567. info->read_count = (uint32_t)((4294967296LL*10)/(cores_per_chip*chips_count_max*golden_speed_per_core*2));
  568. info->read_count = info->read_count*3/4;
  569. info->next_chip_clk = -1;
  570. info->freqcode = freqcode;
  571. info->baud = baud;
  572. info->cores_per_chip = cores_per_chip;
  573. info->chips_count = chips_count;
  574. info->chips_count_max = chips_count_max;
  575. if ((chips_count_max & (chips_count_max - 1)) != 0)
  576. quit(1, "chips_count_max must be a power of 2");
  577. info->chip_clk = chip_clk;
  578. info->chips_bit_num = log_2(chips_count_max);
  579. if (!add_cgpu(zeus))
  580. quit(1, "Failed to add_cgpu");
  581. return true;
  582. }
  583. /************************************************************
  584. * Host <-> ASIC protocol implementation
  585. ************************************************************/
  586. static void zeus_purge_work(struct cgpu_info *zeus)
  587. {
  588. struct ZEUS_INFO *info = zeus->device_data;
  589. mutex_lock(&info->lock);
  590. if (info->current_work != NULL) {
  591. free_work(info->current_work);
  592. info->current_work = NULL;
  593. }
  594. notify_send_work_thread(zeus);
  595. mutex_unlock(&info->lock);
  596. }
  597. #define nonce_range_start(cperc, cmax, core, chip) \
  598. (((0xffffffff / cperc + 1) * core) + ((0x1fffffff / cmax + 1) * chip))
  599. static bool zeus_read_response(struct cgpu_info *zeus)
  600. {
  601. struct ZEUS_INFO *info = zeus->device_data;
  602. unsigned char evtpkt[ZEUS_EVENT_PKT_LEN];
  603. uint32_t nonce, chip, core;
  604. int ret, err;
  605. double duration_s;
  606. bool valid;
  607. if (using_libusb(info)) {
  608. err = usb_read_timeout(zeus, (char *)evtpkt, sizeof(evtpkt), &ret, 250, C_GETRESULTS);
  609. if (err != LIBUSB_SUCCESS && err != LIBUSB_ERROR_TIMEOUT) {
  610. applog(LOG_ERR, "%s%d: USB read error: %s",
  611. zeus->drv->name, zeus->device_id, libusb_error_name(err));
  612. return false;
  613. } else if (err == LIBUSB_ERROR_TIMEOUT) {
  614. return true;
  615. }
  616. } else {
  617. ret = zeus_serial_read(info->device_fd, evtpkt, sizeof(evtpkt), 1, NULL);
  618. if (ret < 0) { // error
  619. info->serial_reopen = true;
  620. notify_send_work_thread(zeus);
  621. return false;
  622. } else if (ret == 0) { // timeout
  623. return true;
  624. }
  625. flush_uart(info->device_fd);
  626. }
  627. cgtime(&info->workend);
  628. memcpy(&nonce, evtpkt, sizeof(evtpkt));
  629. nonce = be32toh(nonce);
  630. mutex_lock(&info->lock);
  631. if (info->current_work == NULL) { // work was flushed before we read response
  632. applog(LOG_DEBUG, "%s%d: Received nonce for flushed work",
  633. zeus->drv->name, zeus->device_id);
  634. mutex_unlock(&info->lock);
  635. return true;
  636. }
  637. valid = submit_nonce(info->thr, info->current_work, nonce);
  638. ++info->workdone;
  639. core = (nonce & 0xe0000000) >> 29; // core indicated by 3 highest bits
  640. chip = (nonce & 0x1ff80000) >> (29 - info->chips_bit_num);
  641. duration_s = tdiff(&info->workend, &info->workstart);
  642. if (chip < ZEUS_MAX_CHIPS && core < ZEUS_CHIP_CORES) {
  643. ++info->nonce_count[chip][core];
  644. if (!valid)
  645. ++info->error_count[chip][core];
  646. if (valid && duration_s > 0) {
  647. 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;
  648. info->last_nonce = nonce;
  649. }
  650. } else {
  651. applog(LOG_INFO, "%s%d: Corrupt nonce message received, cannot determine chip and core",
  652. zeus->drv->name, zeus->device_id);
  653. }
  654. mutex_unlock(&info->lock);
  655. return true;
  656. }
  657. static bool zeus_check_need_work(struct cgpu_info *zeus)
  658. {
  659. struct ZEUS_INFO *info = zeus->device_data;
  660. struct thr_info *thr = info->thr;
  661. struct work *work;
  662. bool need_work;
  663. need_work = (info->current_work == NULL);
  664. if (need_work) {
  665. work = get_work(thr, thr->id); // get_work can block, so done outside mutex_lock
  666. mutex_lock(&info->lock);
  667. if (info->current_work == NULL) { // verify still NULL
  668. work->devflag = false;
  669. info->current_work = work;
  670. } else {
  671. need_work = false;
  672. }
  673. mutex_unlock(&info->lock);
  674. if (!need_work)
  675. discard_work(work);
  676. }
  677. return need_work;
  678. }
  679. static bool zeus_send_work(struct cgpu_info *zeus, struct work *work)
  680. {
  681. struct ZEUS_INFO *info = zeus->device_data;
  682. unsigned char cmdpkt[ZEUS_COMMAND_PKT_LEN];
  683. uint32_t diff_code, diff;
  684. int ret;
  685. diff = work->work_difficulty;
  686. if (diff < 1)
  687. diff = 1;
  688. diff_code = 0xffff / diff;
  689. applog(LOG_DEBUG, "zeus_send_work: diff=%d diff_code=%04x", diff, diff_code);
  690. cmdpkt[0] = info->freqcode;
  691. cmdpkt[1] = ~(info->freqcode);
  692. cmdpkt[2] = (diff_code & 0xff00) >> 8;
  693. cmdpkt[3] = (diff_code & 0x00ff);
  694. memcpy(cmdpkt + 4, work->data, 80);
  695. rev(cmdpkt + 4, 80);
  696. if (using_libusb(info)) { // in libusb mode we send via usb ;)
  697. if (usb_write(zeus, (char *)cmdpkt, sizeof(cmdpkt), &ret, C_SENDWORK) != LIBUSB_SUCCESS ||
  698. ret != sizeof(cmdpkt))
  699. return false;
  700. } else { // otherwise direct via serial port
  701. ret = zeus_serial_write(info->device_fd, cmdpkt, sizeof(cmdpkt));
  702. if (ret < 0) {
  703. info->serial_reopen = true;
  704. notify_send_work_thread(zeus);
  705. return false;
  706. }
  707. }
  708. return true;
  709. }
  710. static void *zeus_send_work_thread(void *data)
  711. {
  712. struct cgpu_info *zeus = (struct cgpu_info *)data;
  713. struct ZEUS_INFO *info = zeus->device_data;
  714. char threadname[24];
  715. struct timeval tv_now, tv_spent, tv_rem;
  716. int retval;
  717. snprintf(threadname, sizeof(threadname), "Zeus/%d", zeus->device_id);
  718. RenameThread(threadname);
  719. applog(LOG_INFO, "%s%d: serial I/O thread running, %s",
  720. zeus->drv->name, zeus->device_id, threadname);
  721. while (likely(!zeus->shutdown)) {
  722. if (unlikely(info->thr->pause || zeus->deven != DEV_ENABLED)) {
  723. cgsem_wait(&info->wusem);
  724. zeus_purge_work(zeus);
  725. continue;
  726. }
  727. if (unlikely(using_libusb(info) && zeus->usbinfo.nodev))
  728. break;
  729. if (unlikely(info->serial_reopen)) {
  730. if (using_serial(info) && !zeus_reopen(zeus)) {
  731. applog(LOG_ERR, "Failed to reopen %s%d on %s, shutting down",
  732. zeus->drv->name, zeus->device_id, zeus->device_path);
  733. break;
  734. }
  735. info->serial_reopen = false;
  736. zeus_purge_work(zeus);
  737. }
  738. zeus_check_need_work(zeus);
  739. mutex_lock(&info->lock);
  740. if (info->current_work && !info->current_work->devflag) {
  741. /* send task to device */
  742. if (opt_zeus_debug)
  743. applog(LOG_INFO, "Sending work");
  744. if (zeus_send_work(zeus, info->current_work)) {
  745. info->current_work->devflag = true;
  746. cgtime(&info->workstart);
  747. if (info->next_chip_clk != -1) {
  748. info->chip_clk = info->next_chip_clk;
  749. info->next_chip_clk = -1;
  750. }
  751. } else {
  752. applog(LOG_NOTICE, "%s%d: I/O error while sending work, will retry",
  753. zeus->drv->name, zeus->device_id);
  754. mutex_unlock(&info->lock);
  755. continue;
  756. }
  757. }
  758. mutex_unlock(&info->lock);
  759. cgtime(&tv_now);
  760. timersub(&tv_now, &info->workstart, &tv_spent);
  761. timersub(&info->work_timeout, &tv_spent, &tv_rem);
  762. if (opt_zeus_debug) {
  763. applog(LOG_DEBUG, "Workstart: %d.%06d", (int)info->workstart.tv_sec, (int)info->workstart.tv_usec);
  764. applog(LOG_DEBUG, "Spent: %d.%06d", (int)tv_spent.tv_sec, (int)tv_spent.tv_usec);
  765. applog(LOG_DEBUG, "Remaining: %d.%06d", (int)tv_rem.tv_sec, (int)tv_rem.tv_usec);
  766. }
  767. retval = cgsem_mswait(&info->wusem, (tv_rem.tv_sec < 1) ? 5000 : tv_rem.tv_sec * 1000);
  768. if (retval == ETIMEDOUT)
  769. zeus_purge_work(zeus); // abandon current work
  770. }
  771. zeus->shutdown = true;
  772. return NULL;
  773. }
  774. /************************************************************
  775. * CGMiner Interface functions
  776. ************************************************************/
  777. static int zeus_autoscan()
  778. {
  779. int found = 0;
  780. applog(LOG_DEBUG, "zeus_autoscan() called");
  781. found += serial_autodetect_udev(zeus_detect_one_serial, ZEUS_USB_ID_MODEL_STR1);
  782. found += serial_autodetect_udev(zeus_detect_one_serial, ZEUS_USB_ID_MODEL_STR2);
  783. return found;
  784. }
  785. static void zeus_detect(bool __maybe_unused hotplug)
  786. {
  787. static int serial_usb = 0;
  788. if (initial_startup_phase && hotplug)
  789. initial_startup_phase = false;
  790. if (serial_usb == 0)
  791. serial_usb = (list_empty(&scan_devices)) ? -1 : 1;
  792. if (serial_usb < 0)
  793. usb_detect(&zeus_drv, zeus_detect_one_usb);
  794. else
  795. serial_detect_iauto(&zeus_drv, zeus_detect_one_serial, zeus_autoscan);
  796. }
  797. static bool zeus_prepare(struct thr_info *thr)
  798. {
  799. struct cgpu_info *zeus = thr->cgpu;
  800. struct ZEUS_INFO *info = zeus->device_data;
  801. applog(LOG_NOTICE, "%s%d opened on %s",
  802. zeus->drv->name, zeus->device_id, zeus->device_path);
  803. info->serial_reopen = (using_serial(info)) ? true : false;
  804. info->thr = thr;
  805. mutex_init(&info->lock);
  806. cgsem_init(&info->wusem);
  807. // Use qualitative value until first result is returned
  808. info->hashes_per_s = info->golden_speed_per_core * info->cores_per_chip * info->chips_count;
  809. return true;
  810. }
  811. static bool zeus_thread_init(struct thr_info *thr)
  812. {
  813. struct cgpu_info *zeus = thr->cgpu;
  814. struct ZEUS_INFO *info = zeus->device_data;
  815. if (pthread_create(&info->sworkpth, NULL, zeus_send_work_thread, zeus)) {
  816. applog(LOG_ERR, "%s%d: Failed to create I/O thread",
  817. zeus->drv->name, zeus->device_id);
  818. return false;
  819. }
  820. return true;
  821. }
  822. static int64_t zeus_scanwork(struct thr_info *thr)
  823. {
  824. struct cgpu_info *zeus = thr->cgpu;
  825. struct ZEUS_INFO *info = zeus->device_data;
  826. struct timeval old_scanwork_time;
  827. double elapsed_s;
  828. int64_t estimate_hashes;
  829. if (unlikely(using_libusb(info) && zeus->usbinfo.nodev))
  830. return -1;
  831. if (unlikely(using_serial(info) && info->serial_reopen)) {
  832. cgsleep_ms(500);
  833. return 0;
  834. }
  835. if (unlikely(zeus_read_response(zeus) < 0)) // reads either from serial or libusb or times out
  836. return 0;
  837. if (thr->work_restart || thr->work_update) {
  838. zeus_purge_work(zeus);
  839. thr->work_restart = false;
  840. thr->work_update = false;
  841. }
  842. mutex_lock(&info->lock);
  843. old_scanwork_time = info->scanwork_time;
  844. cgtime(&info->scanwork_time);
  845. elapsed_s = tdiff(&info->scanwork_time, &old_scanwork_time);
  846. estimate_hashes = elapsed_s * info->hashes_per_s;
  847. mutex_unlock(&info->lock);
  848. if (unlikely(estimate_hashes > 0xffffffff))
  849. estimate_hashes = 0xffffffff;
  850. return estimate_hashes;
  851. }
  852. #define zeus_update_work zeus_flush_work
  853. static void zeus_flush_work(struct cgpu_info *zeus)
  854. {
  855. zeus_purge_work(zeus);
  856. if (opt_zeus_debug)
  857. applog(LOG_INFO, "zeus_flush_work: Tickling I/O thread");
  858. }
  859. static struct api_data *zeus_api_stats(struct cgpu_info *zeus)
  860. {
  861. struct ZEUS_INFO *info = zeus->device_data;
  862. struct api_data *root = NULL;
  863. static struct timeval tv_now, tv_diff, tv_diff2;
  864. static double khs_core, khs_chip, khs_board;
  865. cgtime(&tv_now);
  866. timersub(&tv_now, &(info->workstart), &tv_diff);
  867. timersub(&(info->workend), &(info->workstart), &tv_diff2);
  868. root = api_add_string(root, "Device Name", zeus->unique_id, false);
  869. khs_core = (double)info->golden_speed_per_core / 1000.;
  870. khs_chip = (double)info->golden_speed_per_core * (double)info->cores_per_chip / 1000.;
  871. khs_board = (double)info->golden_speed_per_core * (double)info->cores_per_chip * (double)info->chips_count / 1000.;
  872. root = api_add_khs(root, "KHS/Core", &khs_core, false);
  873. root = api_add_khs(root, "KHS/Chip", &khs_chip, false);
  874. root = api_add_khs(root, "KHS/Board", &khs_board, false);
  875. root = api_add_int(root, "Frequency", &(info->chip_clk), false);
  876. root = api_add_int(root, "Cores/Chip", &(info->cores_per_chip), false);
  877. root = api_add_int(root, "Chips Count", &(info->chips_count), false);
  878. root = api_add_timeval(root, "Time Spent Current Work", &tv_diff, false);
  879. root = api_add_timeval(root, "Work Timeout", &(info->work_timeout), false);
  880. /* It would be nice to report per chip/core nonce and error counts,
  881. * but with more powerful miners with > 100 chips each with 8 cores
  882. * there is too much information and we'd overflow the api buffer.
  883. * Perhaps another api command to query individual chips? */
  884. /* these values are more for diagnostic and debugging */
  885. if (opt_zeus_debug) {
  886. root = api_add_int(root, "chips_count_max", &(info->chips_count_max), false);
  887. root = api_add_int(root, "chips_bit_num", &(info->chips_bit_num), false);
  888. root = api_add_uint32(root, "read_count", &(info->read_count), false);
  889. root = api_add_double(root, "hashes_per_s", &(info->hashes_per_s), false);
  890. root = api_add_uint32(root, "last_nonce", &(info->last_nonce), false);
  891. root = api_add_timeval(root, "last_nonce_time", &tv_diff2, false);
  892. }
  893. return root;
  894. }
  895. static void zeus_get_statline_before(char *buf, size_t bufsiz, struct cgpu_info *zeus)
  896. {
  897. struct ZEUS_INFO *info = zeus->device_data;
  898. if (zeus->name)
  899. tailsprintf(buf, bufsiz, "%-12s %4d MHz ", zeus->name, info->chip_clk);
  900. else
  901. tailsprintf(buf, bufsiz, "%4d chips %4d MHz ", info->chips_count, info->chip_clk);
  902. }
  903. static char *zeus_set_device(struct cgpu_info *zeus, char *option, char *setting, char *replybuf)
  904. {
  905. struct ZEUS_INFO *info = zeus->device_data;
  906. int val;
  907. if (strcasecmp(option, "help") == 0) {
  908. sprintf(replybuf, "freq: range %d-%d, abortwork: true/false",
  909. ZEUS_CLK_MIN, ZEUS_CLK_MAX);
  910. return replybuf;
  911. }
  912. if (strcasecmp(option, "freq") == 0) {
  913. if (!setting || !*setting) {
  914. sprintf(replybuf, "missing freq setting");
  915. return replybuf;
  916. }
  917. val = atoi(setting);
  918. if (val < ZEUS_CLK_MIN || val > ZEUS_CLK_MAX) {
  919. sprintf(replybuf, "invalid freq: '%s' valid range %d-%d",
  920. setting, ZEUS_CLK_MIN, ZEUS_CLK_MAX);
  921. return replybuf;
  922. }
  923. mutex_lock(&info->lock);
  924. info->next_chip_clk = val;
  925. info->freqcode = zeus_clk_to_freqcode(val);
  926. mutex_unlock(&info->lock);
  927. return NULL;
  928. }
  929. if (strcasecmp(option, "abortwork") == 0) {
  930. if (!setting || !*setting) {
  931. sprintf(replybuf, "missing true/false");
  932. return replybuf;
  933. }
  934. if (strcasecmp(setting, "true") != 0) {
  935. sprintf(replybuf, "not aborting current work");
  936. return replybuf;
  937. }
  938. zeus_purge_work(zeus);
  939. return NULL;
  940. }
  941. sprintf(replybuf, "Unknown option: %s", option);
  942. return replybuf;
  943. }
  944. static void zeus_thread_enable(struct thr_info *thr)
  945. {
  946. struct cgpu_info *zeus = thr->cgpu;
  947. notify_send_work_thread(zeus);
  948. }
  949. static void zeus_shutdown(struct thr_info *thr)
  950. {
  951. struct cgpu_info *zeus = thr->cgpu;
  952. struct ZEUS_INFO *info = zeus->device_data;
  953. applog(LOG_NOTICE, "%s%d: Shutting down", zeus->drv->name, zeus->device_id);
  954. pthread_join(info->sworkpth, NULL);
  955. mutex_destroy(&info->lock);
  956. cgsem_destroy(&info->wusem);
  957. if (info->device_fd != -1) {
  958. zeus_serial_close(info->device_fd);
  959. info->device_fd = -1;
  960. }
  961. }
  962. struct device_drv zeus_drv = {
  963. .drv_id = DRIVER_zeus,
  964. .dname = "Zeus",
  965. .name = "ZUS",
  966. .max_diff = 32768,
  967. .drv_detect = zeus_detect,
  968. .thread_prepare = zeus_prepare,
  969. .thread_init = zeus_thread_init,
  970. .hash_work = hash_driver_work,
  971. .scanwork = zeus_scanwork,
  972. .flush_work = zeus_flush_work,
  973. .update_work = zeus_update_work,
  974. .get_api_stats = zeus_api_stats,
  975. .get_statline_before = zeus_get_statline_before,
  976. .set_device = zeus_set_device,
  977. .thread_enable = zeus_thread_enable,
  978. .thread_shutdown = zeus_shutdown,
  979. };