driver-icarus.c 28 KB

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