driver-icarus.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. /*
  2. * Copyright 2012-2013 Andrew Smith
  3. * Copyright 2012 Xiangfu <xiangfu@openmobilefree.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the Free
  7. * Software Foundation; either version 3 of the License, or (at your option)
  8. * any later version. See COPYING for more details.
  9. */
  10. /*
  11. * Those code should be works fine with V2 and V3 bitstream of Icarus.
  12. * Operation:
  13. * No detection implement.
  14. * Input: 64B = 32B midstate + 20B fill bytes + last 12 bytes of block head.
  15. * Return: send back 32bits immediately when Icarus found a valid nonce.
  16. * no query protocol implemented here, if no data send back in ~11.3
  17. * seconds (full cover time on 32bit nonce range by 380MH/s speed)
  18. * just send another work.
  19. * Notice:
  20. * 1. Icarus will start calculate when you push a work to them, even they
  21. * are busy.
  22. * 2. The 2 FPGAs on Icarus will distribute the job, one will calculate the
  23. * 0 ~ 7FFFFFFF, another one will cover the 80000000 ~ FFFFFFFF.
  24. * 3. It's possible for 2 FPGAs both find valid nonce in the meantime, the 2
  25. * valid nonce will all be send back.
  26. * 4. Icarus will stop work when: a valid nonce has been found or 32 bits
  27. * nonce range is completely calculated.
  28. */
  29. #include <float.h>
  30. #include <limits.h>
  31. #include <pthread.h>
  32. #include <stdint.h>
  33. #include <stdio.h>
  34. #include <strings.h>
  35. #include <sys/time.h>
  36. #include <unistd.h>
  37. #include "config.h"
  38. #ifdef WIN32
  39. #include <windows.h>
  40. #endif
  41. #include "compat.h"
  42. #include "miner.h"
  43. #include "usbutils.h"
  44. // The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h
  45. #define ICARUS_IO_SPEED 115200
  46. // The size of a successful nonce read
  47. #define ICARUS_READ_SIZE 4
  48. // Ensure the sizes are correct for the Serial read
  49. #if (ICARUS_READ_SIZE != 4)
  50. #error ICARUS_READ_SIZE must be 4
  51. #endif
  52. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  53. ASSERT1(sizeof(uint32_t) == 4);
  54. // TODO: USB? Different calculation? - see usbstats to work it out e.g. 1/2 of normal send time
  55. // or even use that number? 1/2
  56. // #define ICARUS_READ_TIME(baud) ((double)ICARUS_READ_SIZE * (double)8.0 / (double)(baud))
  57. // maybe 1ms?
  58. #define ICARUS_READ_TIME(baud) (0.001)
  59. // USB ms timeout to wait
  60. #define ICARUS_WAIT_TIMEOUT 100
  61. // In timing mode: Default starting value until an estimate can be obtained
  62. // 5000 ms allows for up to a ~840MH/s device
  63. #define ICARUS_READ_COUNT_TIMING 5000
  64. #define ICARUS_READ_COUNT_MIN ICARUS_WAIT_TIMEOUT
  65. #define SECTOMS(s) ((int)((s) * 1000))
  66. // How many ms below the expected completion time to abort work
  67. // extra in case the last read is delayed
  68. #define ICARUS_READ_REDUCE ((int)(ICARUS_WAIT_TIMEOUT * 1.5))
  69. // For a standard Icarus REV3 (to 5 places)
  70. // Since this rounds up a the last digit - it is a slight overestimate
  71. // Thus the hash rate will be a VERY slight underestimate
  72. // (by a lot less than the displayed accuracy)
  73. // Minor inaccuracy of these numbers doesn't affect the work done,
  74. // only the displayed MH/s
  75. #define ICARUS_REV3_HASH_TIME 0.0000000026316
  76. #define LANCELOT_HASH_TIME 0.0000000025000
  77. #define ASICMINERUSB_HASH_TIME 0.0000000029761
  78. #define NANOSEC 1000000000.0
  79. // Icarus Rev3 doesn't send a completion message when it finishes
  80. // the full nonce range, so to avoid being idle we must abort the
  81. // work (by starting a new work item) shortly before it finishes
  82. //
  83. // Thus we need to estimate 2 things:
  84. // 1) How many hashes were done if the work was aborted
  85. // 2) How high can the timeout be before the Icarus is idle,
  86. // to minimise the number of work items started
  87. // We set 2) to 'the calculated estimate' - ICARUS_READ_REDUCE
  88. // to ensure the estimate ends before idle
  89. //
  90. // The simple calculation used is:
  91. // Tn = Total time in seconds to calculate n hashes
  92. // Hs = seconds per hash
  93. // Xn = number of hashes
  94. // W = code/usb overhead per work
  95. //
  96. // Rough but reasonable estimate:
  97. // Tn = Hs * Xn + W (of the form y = mx + b)
  98. //
  99. // Thus:
  100. // Line of best fit (using least squares)
  101. //
  102. // Hs = (n*Sum(XiTi)-Sum(Xi)*Sum(Ti))/(n*Sum(Xi^2)-Sum(Xi)^2)
  103. // W = Sum(Ti)/n - (Hs*Sum(Xi))/n
  104. //
  105. // N.B. W is less when aborting work since we aren't waiting for the reply
  106. // to be transferred back (ICARUS_READ_TIME)
  107. // Calculating the hashes aborted at n seconds is thus just n/Hs
  108. // (though this is still a slight overestimate due to code delays)
  109. //
  110. // Both below must be exceeded to complete a set of data
  111. // Minimum how long after the first, the last data point must be
  112. #define HISTORY_SEC 60
  113. // Minimum how many points a single ICARUS_HISTORY should have
  114. #define MIN_DATA_COUNT 5
  115. // The value MIN_DATA_COUNT used is doubled each history until it exceeds:
  116. #define MAX_MIN_DATA_COUNT 100
  117. static struct timeval history_sec = { HISTORY_SEC, 0 };
  118. // Store the last INFO_HISTORY data sets
  119. // [0] = current data, not yet ready to be included as an estimate
  120. // Each new data set throws the last old set off the end thus
  121. // keeping a ongoing average of recent data
  122. #define INFO_HISTORY 10
  123. struct ICARUS_HISTORY {
  124. struct timeval finish;
  125. double sumXiTi;
  126. double sumXi;
  127. double sumTi;
  128. double sumXi2;
  129. uint32_t values;
  130. uint32_t hash_count_min;
  131. uint32_t hash_count_max;
  132. };
  133. enum timing_mode { MODE_DEFAULT, MODE_SHORT, MODE_LONG, MODE_VALUE };
  134. static const char *MODE_DEFAULT_STR = "default";
  135. static const char *MODE_SHORT_STR = "short";
  136. static const char *MODE_LONG_STR = "long";
  137. static const char *MODE_VALUE_STR = "value";
  138. static const char *MODE_UNKNOWN_STR = "unknown";
  139. struct ICARUS_INFO {
  140. // time to calculate the golden_ob
  141. uint64_t golden_hashes;
  142. struct timeval golden_tv;
  143. struct ICARUS_HISTORY history[INFO_HISTORY+1];
  144. uint32_t min_data_count;
  145. // seconds per Hash
  146. double Hs;
  147. // ms til we abort
  148. int read_time;
  149. enum timing_mode timing_mode;
  150. bool do_icarus_timing;
  151. double fullnonce;
  152. int count;
  153. double W;
  154. uint32_t values;
  155. uint64_t hash_count_range;
  156. // Determine the cost of history processing
  157. // (which will only affect W)
  158. uint64_t history_count;
  159. struct timeval history_time;
  160. // icarus-options
  161. int baud;
  162. int work_division;
  163. int fpga_count;
  164. uint32_t nonce_mask;
  165. };
  166. #define END_CONDITION 0x0000ffff
  167. // One for each possible device
  168. static struct ICARUS_INFO **icarus_info;
  169. // Looking for options in --icarus-timing and --icarus-options:
  170. //
  171. // Code increments this each time we start to look at a device
  172. // However, this means that if other devices are checked by
  173. // the Icarus code (e.g. Avalon only as at 20130517)
  174. // they will count in the option offset
  175. //
  176. // This, however, is deterministic so that's OK
  177. //
  178. // If we were to increment after successfully finding an Icarus
  179. // that would be random since an Icarus may fail and thus we'd
  180. // not be able to predict the option order
  181. //
  182. // Devices are checked in the order libusb finds them which is ?
  183. //
  184. static int option_offset = -1;
  185. struct device_drv icarus_drv;
  186. static void transfer(struct cgpu_info *icarus, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, enum usb_cmds cmd)
  187. {
  188. int err;
  189. err = usb_transfer(icarus, request_type, bRequest, wValue, wIndex, cmd);
  190. applog(LOG_DEBUG, "%s%i: %s got err %d",
  191. icarus->drv->name, icarus->device_id,
  192. usb_cmdname(cmd), err);
  193. }
  194. // TODO: handle baud
  195. static void icarus_initialise(struct cgpu_info *icarus, __maybe_unused int baud)
  196. {
  197. if (icarus->usbinfo.nodev)
  198. return;
  199. switch (icarus->usbdev->ident) {
  200. case IDENT_BLT:
  201. case IDENT_LLT:
  202. // Latency
  203. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_LATENCY, FTDI_VALUE_LATENCY,
  204. icarus->usbdev->found->interface, C_LATENCY);
  205. if (icarus->usbinfo.nodev)
  206. return;
  207. // Reset
  208. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_RESET,
  209. icarus->usbdev->found->interface, C_RESET);
  210. if (icarus->usbinfo.nodev)
  211. return;
  212. // Set data control
  213. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_DATA, FTDI_VALUE_DATA_BLT,
  214. icarus->usbdev->found->interface, C_SETDATA);
  215. if (icarus->usbinfo.nodev)
  216. return;
  217. // Set the baud
  218. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, FTDI_VALUE_BAUD_BLT,
  219. (FTDI_INDEX_BAUD_BLT & 0xff00) | icarus->usbdev->found->interface,
  220. C_SETBAUD);
  221. if (icarus->usbinfo.nodev)
  222. return;
  223. // Set Modem Control
  224. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM, FTDI_VALUE_MODEM,
  225. icarus->usbdev->found->interface, C_SETMODEM);
  226. if (icarus->usbinfo.nodev)
  227. return;
  228. // Set Flow Control
  229. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW, FTDI_VALUE_FLOW,
  230. icarus->usbdev->found->interface, C_SETFLOW);
  231. if (icarus->usbinfo.nodev)
  232. return;
  233. // Clear any sent data
  234. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_TX,
  235. icarus->usbdev->found->interface, C_PURGETX);
  236. if (icarus->usbinfo.nodev)
  237. return;
  238. // Clear any received data
  239. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_RX,
  240. icarus->usbdev->found->interface, C_PURGERX);
  241. break;
  242. case IDENT_ICA:
  243. break;
  244. case IDENT_AMU:
  245. break;
  246. case IDENT_CMR:
  247. break;
  248. default:
  249. quit(1, "icarus_intialise() called with invalid %s%i ident=%d",
  250. icarus->drv->name, icarus->device_id,
  251. icarus->usbdev->ident);
  252. }
  253. }
  254. static void rev(unsigned char *s, size_t l)
  255. {
  256. size_t i, j;
  257. unsigned char t;
  258. for (i = 0, j = l - 1; i < j; i++, j--) {
  259. t = s[i];
  260. s[i] = s[j];
  261. s[j] = t;
  262. }
  263. }
  264. #define ICA_NONCE_ERROR -1
  265. #define ICA_NONCE_OK 0
  266. #define ICA_NONCE_RESTART 1
  267. #define ICA_NONCE_TIMEOUT 2
  268. static int icarus_get_nonce(struct cgpu_info *icarus, unsigned char *buf, struct timeval *tv_start, struct timeval *tv_finish, struct thr_info *thr, int read_time)
  269. {
  270. struct timeval read_start, read_finish;
  271. int err, amt;
  272. int rc = 0;
  273. int read_amount = ICARUS_READ_SIZE;
  274. bool first = true;
  275. cgtime(tv_start);
  276. while (true) {
  277. if (icarus->usbinfo.nodev)
  278. return ICA_NONCE_ERROR;
  279. cgtime(&read_start);
  280. err = usb_read_timeout(icarus, (char *)buf, read_amount, &amt, ICARUS_WAIT_TIMEOUT, C_GETRESULTS);
  281. cgtime(&read_finish);
  282. if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) {
  283. applog(LOG_ERR, "%s%i: Comms error", icarus->drv->name, icarus->device_id);
  284. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  285. return ICA_NONCE_ERROR;
  286. }
  287. if (first)
  288. copy_time(tv_finish, &read_finish);
  289. // TODO: test if there is more data? to read a 2nd nonce?
  290. if (amt >= read_amount)
  291. return ICA_NONCE_OK;
  292. rc += SECTOMS(tdiff(&read_finish, &read_start));
  293. if (rc >= read_time) {
  294. if (amt > 0)
  295. applog(LOG_DEBUG, "Icarus Read: Timeout reading for %d ms", rc);
  296. else
  297. applog(LOG_DEBUG, "Icarus Read: No data for %d ms", rc);
  298. return ICA_NONCE_TIMEOUT;
  299. }
  300. if (thr && thr->work_restart) {
  301. if (opt_debug) {
  302. applog(LOG_DEBUG,
  303. "Icarus Read: Work restart at %d ms", rc);
  304. }
  305. return ICA_NONCE_RESTART;
  306. }
  307. if (amt > 0) {
  308. buf += amt;
  309. read_amount -= amt;
  310. first = false;
  311. }
  312. }
  313. }
  314. static const char *timing_mode_str(enum timing_mode timing_mode)
  315. {
  316. switch(timing_mode) {
  317. case MODE_DEFAULT:
  318. return MODE_DEFAULT_STR;
  319. case MODE_SHORT:
  320. return MODE_SHORT_STR;
  321. case MODE_LONG:
  322. return MODE_LONG_STR;
  323. case MODE_VALUE:
  324. return MODE_VALUE_STR;
  325. default:
  326. return MODE_UNKNOWN_STR;
  327. }
  328. }
  329. static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
  330. {
  331. struct ICARUS_INFO *info = icarus_info[icarus->device_id];
  332. double Hs;
  333. char buf[BUFSIZ+1];
  334. char *ptr, *comma, *eq;
  335. size_t max;
  336. int i;
  337. if (opt_icarus_timing == NULL)
  338. buf[0] = '\0';
  339. else {
  340. ptr = opt_icarus_timing;
  341. for (i = 0; i < this_option_offset; i++) {
  342. comma = strchr(ptr, ',');
  343. if (comma == NULL)
  344. break;
  345. ptr = comma + 1;
  346. }
  347. comma = strchr(ptr, ',');
  348. if (comma == NULL)
  349. max = strlen(ptr);
  350. else
  351. max = comma - ptr;
  352. if (max > BUFSIZ)
  353. max = BUFSIZ;
  354. strncpy(buf, ptr, max);
  355. buf[max] = '\0';
  356. }
  357. info->Hs = 0;
  358. info->read_time = 0;
  359. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  360. info->Hs = ICARUS_REV3_HASH_TIME;
  361. info->read_time = ICARUS_READ_COUNT_TIMING;
  362. info->timing_mode = MODE_SHORT;
  363. info->do_icarus_timing = true;
  364. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  365. info->Hs = ICARUS_REV3_HASH_TIME;
  366. info->read_time = ICARUS_READ_COUNT_TIMING;
  367. info->timing_mode = MODE_LONG;
  368. info->do_icarus_timing = true;
  369. } else if ((Hs = atof(buf)) != 0) {
  370. info->Hs = Hs / NANOSEC;
  371. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  372. if ((eq = strchr(buf, '=')) != NULL)
  373. info->read_time = atoi(eq+1) * ICARUS_WAIT_TIMEOUT;
  374. if (info->read_time < ICARUS_READ_COUNT_MIN)
  375. info->read_time = SECTOMS(info->fullnonce) - ICARUS_READ_REDUCE;
  376. if (unlikely(info->read_time < ICARUS_READ_COUNT_MIN))
  377. info->read_time = ICARUS_READ_COUNT_MIN;
  378. info->timing_mode = MODE_VALUE;
  379. info->do_icarus_timing = false;
  380. } else {
  381. // Anything else in buf just uses DEFAULT mode
  382. info->Hs = ICARUS_REV3_HASH_TIME;
  383. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  384. if ((eq = strchr(buf, '=')) != NULL)
  385. info->read_time = atoi(eq+1) * ICARUS_WAIT_TIMEOUT;
  386. if (info->read_time < ICARUS_READ_COUNT_MIN)
  387. info->read_time = SECTOMS(info->fullnonce) - ICARUS_READ_REDUCE;
  388. if (unlikely(info->read_time < ICARUS_READ_COUNT_MIN))
  389. info->read_time = ICARUS_READ_COUNT_MIN;
  390. info->timing_mode = MODE_DEFAULT;
  391. info->do_icarus_timing = false;
  392. }
  393. info->min_data_count = MIN_DATA_COUNT;
  394. applog(LOG_DEBUG, "Icarus: Init: %d mode=%s read_time=%dms Hs=%e",
  395. icarus->device_id, timing_mode_str(info->timing_mode), info->read_time, info->Hs);
  396. }
  397. static uint32_t mask(int work_division)
  398. {
  399. char err_buf[BUFSIZ+1];
  400. uint32_t nonce_mask = 0x7fffffff;
  401. // yes we can calculate these, but this way it's easy to see what they are
  402. switch (work_division) {
  403. case 1:
  404. nonce_mask = 0xffffffff;
  405. break;
  406. case 2:
  407. nonce_mask = 0x7fffffff;
  408. break;
  409. case 4:
  410. nonce_mask = 0x3fffffff;
  411. break;
  412. case 8:
  413. nonce_mask = 0x1fffffff;
  414. break;
  415. default:
  416. sprintf(err_buf, "Invalid2 icarus-options for work_division (%d) must be 1, 2, 4 or 8", work_division);
  417. quit(1, err_buf);
  418. }
  419. return nonce_mask;
  420. }
  421. static void get_options(int this_option_offset, int *baud, int *work_division, int *fpga_count)
  422. {
  423. char err_buf[BUFSIZ+1];
  424. char buf[BUFSIZ+1];
  425. char *ptr, *comma, *colon, *colon2;
  426. size_t max;
  427. int i, tmp;
  428. if (opt_icarus_options == NULL)
  429. buf[0] = '\0';
  430. else {
  431. ptr = opt_icarus_options;
  432. for (i = 0; i < this_option_offset; i++) {
  433. comma = strchr(ptr, ',');
  434. if (comma == NULL)
  435. break;
  436. ptr = comma + 1;
  437. }
  438. comma = strchr(ptr, ',');
  439. if (comma == NULL)
  440. max = strlen(ptr);
  441. else
  442. max = comma - ptr;
  443. if (max > BUFSIZ)
  444. max = BUFSIZ;
  445. strncpy(buf, ptr, max);
  446. buf[max] = '\0';
  447. }
  448. *baud = ICARUS_IO_SPEED;
  449. *work_division = 2;
  450. *fpga_count = 2;
  451. if (*buf) {
  452. colon = strchr(buf, ':');
  453. if (colon)
  454. *(colon++) = '\0';
  455. if (*buf) {
  456. tmp = atoi(buf);
  457. switch (tmp) {
  458. case 115200:
  459. *baud = 115200;
  460. break;
  461. case 57600:
  462. *baud = 57600;
  463. break;
  464. default:
  465. sprintf(err_buf, "Invalid icarus-options for baud (%s) must be 115200 or 57600", buf);
  466. quit(1, err_buf);
  467. }
  468. }
  469. if (colon && *colon) {
  470. colon2 = strchr(colon, ':');
  471. if (colon2)
  472. *(colon2++) = '\0';
  473. if (*colon) {
  474. tmp = atoi(colon);
  475. if (tmp == 1 || tmp == 2 || tmp == 4 || tmp == 8) {
  476. *work_division = tmp;
  477. *fpga_count = tmp; // default to the same
  478. } else {
  479. sprintf(err_buf, "Invalid icarus-options for work_division (%s) must be 1, 2, 4 or 8", colon);
  480. quit(1, err_buf);
  481. }
  482. }
  483. if (colon2 && *colon2) {
  484. tmp = atoi(colon2);
  485. if (tmp > 0 && tmp <= *work_division)
  486. *fpga_count = tmp;
  487. else {
  488. sprintf(err_buf, "Invalid icarus-options for fpga_count (%s) must be >0 and <=work_division (%d)", colon2, *work_division);
  489. quit(1, err_buf);
  490. }
  491. }
  492. }
  493. }
  494. }
  495. static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  496. {
  497. int this_option_offset = ++option_offset;
  498. char devpath[20];
  499. struct ICARUS_INFO *info;
  500. struct timeval tv_start, tv_finish;
  501. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  502. // N.B. golden_ob MUST take less time to calculate
  503. // than the timeout set in icarus_open()
  504. // This one takes ~0.53ms on Rev3 Icarus
  505. const char golden_ob[] =
  506. "4679ba4ec99876bf4bfe086082b40025"
  507. "4df6c356451471139a3afa71e48f544a"
  508. "00000000000000000000000000000000"
  509. "0000000087320b1a1426674f2fa722ce";
  510. const char golden_nonce[] = "000187a2";
  511. const uint32_t golden_nonce_val = 0x000187a2;
  512. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  513. char *nonce_hex;
  514. int baud, work_division, fpga_count;
  515. struct cgpu_info *icarus;
  516. int ret, err, amount, tries;
  517. get_options(this_option_offset, &baud, &work_division, &fpga_count);
  518. icarus = calloc(1, sizeof(struct cgpu_info));
  519. if (unlikely(!icarus))
  520. quit(1, "Failed to calloc icarus in icarus_detect_one");
  521. icarus->drv = &icarus_drv;
  522. icarus->deven = DEV_ENABLED;
  523. icarus->threads = 1;
  524. if (!usb_init(icarus, dev, found))
  525. goto shin;
  526. // TODO: set options based on ident if options not supplied
  527. // add a flag to say options were set by parameters
  528. sprintf(devpath, "%d:%d",
  529. (int)(icarus->usbinfo.bus_number),
  530. (int)(icarus->usbinfo.device_address));
  531. icarus->device_path = strdup(devpath);
  532. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  533. tries = 0;
  534. retry:
  535. while (++tries) {
  536. icarus_initialise(icarus, baud);
  537. err = usb_write(icarus, (char *)ob_bin, sizeof(ob_bin), &amount, C_SENDTESTWORK);
  538. if (err == LIBUSB_SUCCESS && amount == sizeof(ob_bin))
  539. break;
  540. if (tries > 2)
  541. goto unshin;
  542. }
  543. memset(nonce_bin, 0, sizeof(nonce_bin));
  544. ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, NULL, 100);
  545. if (ret != ICA_NONCE_OK) {
  546. if (tries < 3)
  547. goto retry;
  548. goto unshin;
  549. }
  550. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  551. if (strncmp(nonce_hex, golden_nonce, 8)) {
  552. applog(LOG_ERR,
  553. "Icarus Detect: "
  554. "Test failed at %s: get %s, should: %s",
  555. devpath, nonce_hex, golden_nonce);
  556. free(nonce_hex);
  557. goto unshin;
  558. }
  559. applog(LOG_DEBUG,
  560. "Icarus Detect: "
  561. "Test succeeded at %s: got %s",
  562. devpath, nonce_hex);
  563. free(nonce_hex);
  564. /* We have a real Icarus! */
  565. if (!add_cgpu(icarus))
  566. goto unshin;
  567. update_usb_stats(icarus);
  568. icarus_info = realloc(icarus_info, sizeof(struct ICARUS_INFO *) * (total_devices + 1));
  569. if (unlikely(!icarus_info))
  570. quit(1, "Failed to realloc ICARUS_INFO");
  571. applog(LOG_INFO, "Found Icarus at %s, mark as %d",
  572. devpath, icarus->device_id);
  573. applog(LOG_DEBUG, "Icarus: Init: %d baud=%d work_division=%d fpga_count=%d",
  574. icarus->device_id, baud, work_division, fpga_count);
  575. // Since we are adding a new device on the end it needs to always be allocated
  576. icarus_info[icarus->device_id] = (struct ICARUS_INFO *)malloc(sizeof(struct ICARUS_INFO));
  577. if (unlikely(!(icarus_info[icarus->device_id])))
  578. quit(1, "Failed to malloc ICARUS_INFO");
  579. info = icarus_info[icarus->device_id];
  580. // Initialise everything to zero for a new device
  581. memset(info, 0, sizeof(struct ICARUS_INFO));
  582. info->baud = baud;
  583. info->work_division = work_division;
  584. info->fpga_count = fpga_count;
  585. info->nonce_mask = mask(work_division);
  586. info->golden_hashes = (golden_nonce_val & info->nonce_mask) * fpga_count;
  587. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  588. set_timing_mode(this_option_offset, icarus);
  589. return true;
  590. unshin:
  591. usb_uninit(icarus);
  592. free(icarus->device_path);
  593. shin:
  594. free(icarus);
  595. return false;
  596. }
  597. static void icarus_detect()
  598. {
  599. usb_detect(&icarus_drv, icarus_detect_one);
  600. }
  601. static bool icarus_prepare(struct thr_info *thr)
  602. {
  603. struct cgpu_info *icarus = thr->cgpu;
  604. struct timeval now;
  605. cgtime(&now);
  606. get_datestamp(icarus->init, &now);
  607. return true;
  608. }
  609. static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  610. __maybe_unused int64_t max_nonce)
  611. {
  612. struct cgpu_info *icarus = thr->cgpu;
  613. int ret, err, amount;
  614. struct ICARUS_INFO *info;
  615. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  616. char *ob_hex;
  617. uint32_t nonce;
  618. int64_t hash_count;
  619. struct timeval tv_start, tv_finish, elapsed;
  620. struct timeval tv_history_start, tv_history_finish;
  621. double Ti, Xi;
  622. int curr_hw_errors, i;
  623. bool was_hw_error;
  624. struct ICARUS_HISTORY *history0, *history;
  625. int count;
  626. double Hs, W, fullnonce;
  627. int read_time;
  628. int64_t estimate_hashes;
  629. uint32_t values;
  630. int64_t hash_count_range;
  631. // Device is gone
  632. if (icarus->usbinfo.nodev)
  633. return -1;
  634. info = icarus_info[icarus->device_id];
  635. elapsed.tv_sec = elapsed.tv_usec = 0;
  636. memset(ob_bin, 0, sizeof(ob_bin));
  637. memcpy(ob_bin, work->midstate, 32);
  638. memcpy(ob_bin + 52, work->data + 64, 12);
  639. rev(ob_bin, 32);
  640. rev(ob_bin + 52, 12);
  641. err = usb_write(icarus, (char *)ob_bin, sizeof(ob_bin), &amount, C_SENDWORK);
  642. if (err < 0 || amount != sizeof(ob_bin)) {
  643. applog(LOG_ERR, "%s%i: Comms error", icarus->drv->name, icarus->device_id);
  644. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  645. icarus_initialise(icarus, info->baud);
  646. return 0;
  647. }
  648. if (opt_debug) {
  649. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  650. applog(LOG_DEBUG, "Icarus %d sent: %s",
  651. icarus->device_id, ob_hex);
  652. free(ob_hex);
  653. }
  654. /* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
  655. memset(nonce_bin, 0, sizeof(nonce_bin));
  656. ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, thr, info->read_time);
  657. if (ret == ICA_NONCE_ERROR)
  658. return 0;
  659. work->blk.nonce = 0xffffffff;
  660. // aborted before becoming idle, get new work
  661. if (ret == ICA_NONCE_TIMEOUT || ret == ICA_NONCE_RESTART) {
  662. timersub(&tv_finish, &tv_start, &elapsed);
  663. // ONLY up to just when it aborted
  664. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  665. estimate_hashes = ((double)(elapsed.tv_sec)
  666. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  667. // If some Serial-USB delay allowed the full nonce range to
  668. // complete it can't have done more than a full nonce
  669. if (unlikely(estimate_hashes > 0xffffffff))
  670. estimate_hashes = 0xffffffff;
  671. if (opt_debug) {
  672. applog(LOG_DEBUG, "Icarus %d no nonce = 0x%08lX hashes (%ld.%06lds)",
  673. icarus->device_id, (long unsigned int)estimate_hashes,
  674. elapsed.tv_sec, elapsed.tv_usec);
  675. }
  676. return estimate_hashes;
  677. }
  678. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  679. nonce = htobe32(nonce);
  680. curr_hw_errors = icarus->hw_errors;
  681. submit_nonce(thr, work, nonce);
  682. was_hw_error = (curr_hw_errors > icarus->hw_errors);
  683. hash_count = (nonce & info->nonce_mask);
  684. hash_count++;
  685. hash_count *= info->fpga_count;
  686. if (opt_debug || info->do_icarus_timing)
  687. timersub(&tv_finish, &tv_start, &elapsed);
  688. if (opt_debug) {
  689. applog(LOG_DEBUG, "Icarus %d nonce = 0x%08x = 0x%08lX hashes (%ld.%06lds)",
  690. icarus->device_id, nonce, (long unsigned int)hash_count,
  691. elapsed.tv_sec, elapsed.tv_usec);
  692. }
  693. // ignore possible end condition values ... and hw errors
  694. if (info->do_icarus_timing
  695. && !was_hw_error
  696. && ((nonce & info->nonce_mask) > END_CONDITION)
  697. && ((nonce & info->nonce_mask) < (info->nonce_mask & ~END_CONDITION))) {
  698. cgtime(&tv_history_start);
  699. history0 = &(info->history[0]);
  700. if (history0->values == 0)
  701. timeradd(&tv_start, &history_sec, &(history0->finish));
  702. Ti = (double)(elapsed.tv_sec)
  703. + ((double)(elapsed.tv_usec))/((double)1000000)
  704. - ((double)ICARUS_READ_TIME(info->baud));
  705. Xi = (double)hash_count;
  706. history0->sumXiTi += Xi * Ti;
  707. history0->sumXi += Xi;
  708. history0->sumTi += Ti;
  709. history0->sumXi2 += Xi * Xi;
  710. history0->values++;
  711. if (history0->hash_count_max < hash_count)
  712. history0->hash_count_max = hash_count;
  713. if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
  714. history0->hash_count_min = hash_count;
  715. if (history0->values >= info->min_data_count
  716. && timercmp(&tv_start, &(history0->finish), >)) {
  717. for (i = INFO_HISTORY; i > 0; i--)
  718. memcpy(&(info->history[i]),
  719. &(info->history[i-1]),
  720. sizeof(struct ICARUS_HISTORY));
  721. // Initialise history0 to zero for summary calculation
  722. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  723. // We just completed a history data set
  724. // So now recalc read_time based on the whole history thus we will
  725. // initially get more accurate until it completes INFO_HISTORY
  726. // total data sets
  727. count = 0;
  728. for (i = 1 ; i <= INFO_HISTORY; i++) {
  729. history = &(info->history[i]);
  730. if (history->values >= MIN_DATA_COUNT) {
  731. count++;
  732. history0->sumXiTi += history->sumXiTi;
  733. history0->sumXi += history->sumXi;
  734. history0->sumTi += history->sumTi;
  735. history0->sumXi2 += history->sumXi2;
  736. history0->values += history->values;
  737. if (history0->hash_count_max < history->hash_count_max)
  738. history0->hash_count_max = history->hash_count_max;
  739. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  740. history0->hash_count_min = history->hash_count_min;
  741. }
  742. }
  743. // All history data
  744. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  745. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  746. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  747. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  748. values = history0->values;
  749. // Initialise history0 to zero for next data set
  750. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  751. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  752. read_time = SECTOMS(fullnonce) - ICARUS_READ_REDUCE;
  753. info->Hs = Hs;
  754. info->read_time = read_time;
  755. info->fullnonce = fullnonce;
  756. info->count = count;
  757. info->W = W;
  758. info->values = values;
  759. info->hash_count_range = hash_count_range;
  760. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  761. info->min_data_count *= 2;
  762. else if (info->timing_mode == MODE_SHORT)
  763. info->do_icarus_timing = false;
  764. // applog(LOG_WARNING, "Icarus %d Re-estimate: read_time=%d fullnonce=%fs history count=%d Hs=%e W=%e values=%d hash range=0x%08lx min data count=%u", icarus->device_id, read_time, fullnonce, count, Hs, W, values, hash_count_range, info->min_data_count);
  765. applog(LOG_WARNING, "Icarus %d Re-estimate: Hs=%e W=%e read_time=%dms fullnonce=%.3fs",
  766. icarus->device_id, Hs, W, read_time, fullnonce);
  767. }
  768. info->history_count++;
  769. cgtime(&tv_history_finish);
  770. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  771. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  772. }
  773. return hash_count;
  774. }
  775. static struct api_data *icarus_api_stats(struct cgpu_info *cgpu)
  776. {
  777. struct api_data *root = NULL;
  778. struct ICARUS_INFO *info = icarus_info[cgpu->device_id];
  779. // Warning, access to these is not locked - but we don't really
  780. // care since hashing performance is way more important than
  781. // locking access to displaying API debug 'stats'
  782. // If locking becomes an issue for any of them, use copy_data=true also
  783. root = api_add_int(root, "read_time", &(info->read_time), false);
  784. root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
  785. root = api_add_int(root, "count", &(info->count), false);
  786. root = api_add_hs(root, "Hs", &(info->Hs), false);
  787. root = api_add_double(root, "W", &(info->W), false);
  788. root = api_add_uint(root, "total_values", &(info->values), false);
  789. root = api_add_uint64(root, "range", &(info->hash_count_range), false);
  790. root = api_add_uint64(root, "history_count", &(info->history_count), false);
  791. root = api_add_timeval(root, "history_time", &(info->history_time), false);
  792. root = api_add_uint(root, "min_data_count", &(info->min_data_count), false);
  793. root = api_add_uint(root, "timing_values", &(info->history[0].values), false);
  794. root = api_add_const(root, "timing_mode", timing_mode_str(info->timing_mode), false);
  795. root = api_add_bool(root, "is_timing", &(info->do_icarus_timing), false);
  796. root = api_add_int(root, "baud", &(info->baud), false);
  797. root = api_add_int(root, "work_division", &(info->work_division), false);
  798. root = api_add_int(root, "fpga_count", &(info->fpga_count), false);
  799. return root;
  800. }
  801. static void icarus_shutdown(__maybe_unused struct thr_info *thr)
  802. {
  803. // TODO: ?
  804. }
  805. struct device_drv icarus_drv = {
  806. .drv_id = DRIVER_ICARUS,
  807. .dname = "Icarus",
  808. .name = "ICA",
  809. .drv_detect = icarus_detect,
  810. .get_api_stats = icarus_api_stats,
  811. .thread_prepare = icarus_prepare,
  812. .scanhash = icarus_scanhash,
  813. .thread_shutdown = icarus_shutdown,
  814. };