driver-icarus.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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. #define AMU_PREF_PACKET 256
  50. #define BLT_PREF_PACKET 512
  51. #define ICA_PREF_PACKET 256
  52. // Ensure the sizes are correct for the Serial read
  53. #if (ICARUS_READ_SIZE != 4)
  54. #error ICARUS_READ_SIZE must be 4
  55. #endif
  56. #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1]
  57. ASSERT1(sizeof(uint32_t) == 4);
  58. // TODO: USB? Different calculation? - see usbstats to work it out e.g. 1/2 of normal send time
  59. // or even use that number? 1/2
  60. // #define ICARUS_READ_TIME(baud) ((double)ICARUS_READ_SIZE * (double)8.0 / (double)(baud))
  61. // maybe 1ms?
  62. #define ICARUS_READ_TIME(baud) (0.001)
  63. // USB ms timeout to wait - user specified timeouts are multiples of this
  64. #define ICARUS_WAIT_TIMEOUT 100
  65. #define ICARUS_CMR2_TIMEOUT 1
  66. // Defined in multiples of ICARUS_WAIT_TIMEOUT
  67. // Must of course be greater than ICARUS_READ_COUNT_TIMING/ICARUS_WAIT_TIMEOUT
  68. // There's no need to have this bigger, since the overhead/latency of extra work
  69. // is pretty small once you get beyond a 10s nonce range time and 10s also
  70. // means that nothing slower than 429MH/s can go idle so most icarus devices
  71. // will always mine without idling
  72. #define ICARUS_READ_TIME_LIMIT_MAX 100
  73. // In timing mode: Default starting value until an estimate can be obtained
  74. // 5000 ms allows for up to a ~840MH/s device
  75. #define ICARUS_READ_COUNT_TIMING 5000
  76. #define ICARUS_READ_COUNT_MIN ICARUS_WAIT_TIMEOUT
  77. #define SECTOMS(s) ((int)((s) * 1000))
  78. // How many ms below the expected completion time to abort work
  79. // extra in case the last read is delayed
  80. #define ICARUS_READ_REDUCE ((int)(ICARUS_WAIT_TIMEOUT * 1.5))
  81. // For a standard Icarus REV3 (to 5 places)
  82. // Since this rounds up a the last digit - it is a slight overestimate
  83. // Thus the hash rate will be a VERY slight underestimate
  84. // (by a lot less than the displayed accuracy)
  85. // Minor inaccuracy of these numbers doesn't affect the work done,
  86. // only the displayed MH/s
  87. #define ICARUS_REV3_HASH_TIME 0.0000000026316
  88. #define LANCELOT_HASH_TIME 0.0000000025000
  89. #define ASICMINERUSB_HASH_TIME 0.0000000029761
  90. // TODO: What is it?
  91. #define CAIRNSMORE1_HASH_TIME 0.0000000027000
  92. // Per FPGA
  93. #define CAIRNSMORE2_HASH_TIME 0.0000000066600
  94. #define NANOSEC 1000000000.0
  95. // Icarus Rev3 doesn't send a completion message when it finishes
  96. // the full nonce range, so to avoid being idle we must abort the
  97. // work (by starting a new work item) shortly before it finishes
  98. //
  99. // Thus we need to estimate 2 things:
  100. // 1) How many hashes were done if the work was aborted
  101. // 2) How high can the timeout be before the Icarus is idle,
  102. // to minimise the number of work items started
  103. // We set 2) to 'the calculated estimate' - ICARUS_READ_REDUCE
  104. // to ensure the estimate ends before idle
  105. //
  106. // The simple calculation used is:
  107. // Tn = Total time in seconds to calculate n hashes
  108. // Hs = seconds per hash
  109. // Xn = number of hashes
  110. // W = code/usb overhead per work
  111. //
  112. // Rough but reasonable estimate:
  113. // Tn = Hs * Xn + W (of the form y = mx + b)
  114. //
  115. // Thus:
  116. // Line of best fit (using least squares)
  117. //
  118. // Hs = (n*Sum(XiTi)-Sum(Xi)*Sum(Ti))/(n*Sum(Xi^2)-Sum(Xi)^2)
  119. // W = Sum(Ti)/n - (Hs*Sum(Xi))/n
  120. //
  121. // N.B. W is less when aborting work since we aren't waiting for the reply
  122. // to be transferred back (ICARUS_READ_TIME)
  123. // Calculating the hashes aborted at n seconds is thus just n/Hs
  124. // (though this is still a slight overestimate due to code delays)
  125. //
  126. // Both below must be exceeded to complete a set of data
  127. // Minimum how long after the first, the last data point must be
  128. #define HISTORY_SEC 60
  129. // Minimum how many points a single ICARUS_HISTORY should have
  130. #define MIN_DATA_COUNT 5
  131. // The value MIN_DATA_COUNT used is doubled each history until it exceeds:
  132. #define MAX_MIN_DATA_COUNT 100
  133. static struct timeval history_sec = { HISTORY_SEC, 0 };
  134. // Store the last INFO_HISTORY data sets
  135. // [0] = current data, not yet ready to be included as an estimate
  136. // Each new data set throws the last old set off the end thus
  137. // keeping a ongoing average of recent data
  138. #define INFO_HISTORY 10
  139. struct ICARUS_HISTORY {
  140. struct timeval finish;
  141. double sumXiTi;
  142. double sumXi;
  143. double sumTi;
  144. double sumXi2;
  145. uint32_t values;
  146. uint32_t hash_count_min;
  147. uint32_t hash_count_max;
  148. };
  149. enum timing_mode { MODE_DEFAULT, MODE_SHORT, MODE_LONG, MODE_VALUE };
  150. static const char *MODE_DEFAULT_STR = "default";
  151. static const char *MODE_SHORT_STR = "short";
  152. static const char *MODE_SHORT_STREQ = "short=";
  153. static const char *MODE_LONG_STR = "long";
  154. static const char *MODE_LONG_STREQ = "long=";
  155. static const char *MODE_VALUE_STR = "value";
  156. static const char *MODE_UNKNOWN_STR = "unknown";
  157. struct ICARUS_INFO {
  158. int intinfo;
  159. // time to calculate the golden_ob
  160. uint64_t golden_hashes;
  161. struct timeval golden_tv;
  162. struct ICARUS_HISTORY history[INFO_HISTORY+1];
  163. uint32_t min_data_count;
  164. int timeout;
  165. // seconds per Hash
  166. double Hs;
  167. // ms til we abort
  168. int read_time;
  169. // ms limit for (short=/long=) read_time
  170. int read_time_limit;
  171. enum timing_mode timing_mode;
  172. bool do_icarus_timing;
  173. double fullnonce;
  174. int count;
  175. double W;
  176. uint32_t values;
  177. uint64_t hash_count_range;
  178. // Determine the cost of history processing
  179. // (which will only affect W)
  180. uint64_t history_count;
  181. struct timeval history_time;
  182. // icarus-options
  183. int baud;
  184. int work_division;
  185. int fpga_count;
  186. uint32_t nonce_mask;
  187. bool initialised;
  188. };
  189. #define END_CONDITION 0x0000ffff
  190. // Looking for options in --icarus-timing and --icarus-options:
  191. //
  192. // Code increments this each time we start to look at a device
  193. // However, this means that if other devices are checked by
  194. // the Icarus code (e.g. Avalon only as at 20130517)
  195. // they will count in the option offset
  196. //
  197. // This, however, is deterministic so that's OK
  198. //
  199. // If we were to increment after successfully finding an Icarus
  200. // that would be random since an Icarus may fail and thus we'd
  201. // not be able to predict the option order
  202. //
  203. // Devices are checked in the order libusb finds them which is ?
  204. //
  205. static int option_offset = -1;
  206. /*
  207. #define ICA_BUFSIZ (0x200)
  208. static void transfer_read(struct cgpu_info *icarus, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, char *buf, int bufsiz, int *amount, enum usb_cmds cmd)
  209. {
  210. int err;
  211. err = usb_transfer_read(icarus, request_type, bRequest, wValue, wIndex, buf, bufsiz, amount, cmd);
  212. applog(LOG_DEBUG, "%s: cgid %d %s got err %d",
  213. icarus->drv->name, icarus->cgminer_id,
  214. usb_cmdname(cmd), err);
  215. }
  216. */
  217. static void _transfer(struct cgpu_info *icarus, uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, uint32_t *data, int siz, enum usb_cmds cmd)
  218. {
  219. int err;
  220. err = usb_transfer_data(icarus, request_type, bRequest, wValue, wIndex, data, siz, cmd);
  221. applog(LOG_DEBUG, "%s: cgid %d %s got err %d",
  222. icarus->drv->name, icarus->cgminer_id,
  223. usb_cmdname(cmd), err);
  224. }
  225. #define transfer(icarus, request_type, bRequest, wValue, wIndex, cmd) \
  226. _transfer(icarus, request_type, bRequest, wValue, wIndex, NULL, 0, cmd)
  227. static void icarus_initialise(struct cgpu_info *icarus, int baud)
  228. {
  229. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  230. uint16_t wValue, wIndex;
  231. enum sub_ident ident;
  232. int interface;
  233. if (icarus->usbinfo.nodev)
  234. return;
  235. usb_set_cps(icarus, baud / 10);
  236. usb_enable_cps(icarus);
  237. interface = _usb_interface(icarus, info->intinfo);
  238. ident = usb_ident(icarus);
  239. switch (ident) {
  240. case IDENT_BLT:
  241. case IDENT_LLT:
  242. case IDENT_CMR1:
  243. case IDENT_CMR2:
  244. usb_set_pps(icarus, BLT_PREF_PACKET);
  245. // Reset
  246. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_RESET,
  247. interface, C_RESET);
  248. if (icarus->usbinfo.nodev)
  249. return;
  250. // Latency
  251. _usb_ftdi_set_latency(icarus, info->intinfo);
  252. if (icarus->usbinfo.nodev)
  253. return;
  254. // Set data control
  255. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_DATA, FTDI_VALUE_DATA_BLT,
  256. interface, C_SETDATA);
  257. if (icarus->usbinfo.nodev)
  258. return;
  259. // default to BLT/LLT 115200
  260. wValue = FTDI_VALUE_BAUD_BLT;
  261. wIndex = FTDI_INDEX_BAUD_BLT;
  262. if (ident == IDENT_CMR1 || ident == IDENT_CMR2) {
  263. switch (baud) {
  264. case 115200:
  265. wValue = FTDI_VALUE_BAUD_CMR_115;
  266. wIndex = FTDI_INDEX_BAUD_CMR_115;
  267. break;
  268. case 57600:
  269. wValue = FTDI_VALUE_BAUD_CMR_57;
  270. wIndex = FTDI_INDEX_BAUD_CMR_57;
  271. break;
  272. default:
  273. quit(1, "icarus_intialise() invalid baud (%d) for Cairnsmore1", baud);
  274. break;
  275. }
  276. }
  277. // Set the baud
  278. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, wValue,
  279. (wIndex & 0xff00) | interface, C_SETBAUD);
  280. if (icarus->usbinfo.nodev)
  281. return;
  282. // Set Modem Control
  283. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM, FTDI_VALUE_MODEM,
  284. interface, C_SETMODEM);
  285. if (icarus->usbinfo.nodev)
  286. return;
  287. // Set Flow Control
  288. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW, FTDI_VALUE_FLOW,
  289. interface, C_SETFLOW);
  290. if (icarus->usbinfo.nodev)
  291. return;
  292. // Clear any sent data
  293. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_TX,
  294. interface, C_PURGETX);
  295. if (icarus->usbinfo.nodev)
  296. return;
  297. // Clear any received data
  298. transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_RX,
  299. interface, C_PURGERX);
  300. break;
  301. case IDENT_ICA:
  302. usb_set_pps(icarus, ICA_PREF_PACKET);
  303. // Set Data Control
  304. transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_CTRL, PL2303_VALUE_CTRL,
  305. interface, C_SETDATA);
  306. if (icarus->usbinfo.nodev)
  307. return;
  308. // Set Line Control
  309. uint32_t ica_data[2] = { PL2303_VALUE_LINE0, PL2303_VALUE_LINE1 };
  310. _transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_LINE, PL2303_VALUE_LINE,
  311. interface, &ica_data[0], PL2303_VALUE_LINE_SIZE, C_SETLINE);
  312. if (icarus->usbinfo.nodev)
  313. return;
  314. // Vendor
  315. transfer(icarus, PL2303_VENDOR_OUT, PL2303_REQUEST_VENDOR, PL2303_VALUE_VENDOR,
  316. interface, C_VENDOR);
  317. break;
  318. case IDENT_AMU:
  319. usb_set_pps(icarus, AMU_PREF_PACKET);
  320. // Enable the UART
  321. transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_IFC_ENABLE,
  322. CP210X_VALUE_UART_ENABLE,
  323. interface, C_ENABLE_UART);
  324. if (icarus->usbinfo.nodev)
  325. return;
  326. // Set data control
  327. transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_DATA, CP210X_VALUE_DATA,
  328. interface, C_SETDATA);
  329. if (icarus->usbinfo.nodev)
  330. return;
  331. // Set the baud
  332. uint32_t data = CP210X_DATA_BAUD;
  333. _transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_BAUD, 0,
  334. interface, &data, sizeof(data), C_SETBAUD);
  335. break;
  336. default:
  337. quit(1, "icarus_intialise() called with invalid %s cgid %i ident=%d",
  338. icarus->drv->name, icarus->cgminer_id, ident);
  339. }
  340. info->initialised = true;
  341. }
  342. static void rev(unsigned char *s, size_t l)
  343. {
  344. size_t i, j;
  345. unsigned char t;
  346. for (i = 0, j = l - 1; i < j; i++, j--) {
  347. t = s[i];
  348. s[i] = s[j];
  349. s[j] = t;
  350. }
  351. }
  352. #define ICA_NONCE_ERROR -1
  353. #define ICA_NONCE_OK 0
  354. #define ICA_NONCE_RESTART 1
  355. #define ICA_NONCE_TIMEOUT 2
  356. 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)
  357. {
  358. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  359. struct timeval read_start, read_finish;
  360. int err, amt;
  361. int rc = 0, delay;
  362. int read_amount = ICARUS_READ_SIZE;
  363. bool first = true;
  364. cgtime(tv_start);
  365. while (true) {
  366. if (icarus->usbinfo.nodev)
  367. return ICA_NONCE_ERROR;
  368. cgtime(&read_start);
  369. err = usb_read_ii_timeout(icarus, info->intinfo,
  370. (char *)buf, read_amount, &amt,
  371. info->timeout, C_GETRESULTS);
  372. cgtime(&read_finish);
  373. if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) {
  374. applog(LOG_ERR, "%s%i: Comms error (rerr=%d amt=%d)",
  375. icarus->drv->name, icarus->device_id, err, amt);
  376. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  377. return ICA_NONCE_ERROR;
  378. }
  379. if (first)
  380. copy_time(tv_finish, &read_finish);
  381. if (amt >= read_amount)
  382. return ICA_NONCE_OK;
  383. rc = SECTOMS(tdiff(&read_finish, tv_start));
  384. if (rc >= read_time) {
  385. if (amt > 0)
  386. applog(LOG_DEBUG, "Icarus Read: Timeout reading for %d ms", rc);
  387. else
  388. applog(LOG_DEBUG, "Icarus Read: No data for %d ms", rc);
  389. return ICA_NONCE_TIMEOUT;
  390. }
  391. if (thr && thr->work_restart) {
  392. applog(LOG_DEBUG, "Icarus Read: Work restart at %d ms", rc);
  393. return ICA_NONCE_RESTART;
  394. }
  395. if (amt > 0) {
  396. buf += amt;
  397. read_amount -= amt;
  398. first = false;
  399. }
  400. if (info->timeout < ICARUS_WAIT_TIMEOUT) {
  401. delay = ICARUS_WAIT_TIMEOUT - rc;
  402. if (delay > 0) {
  403. cgsleep_ms(delay);
  404. if (thr && thr->work_restart) {
  405. applog(LOG_DEBUG, "Icarus Read: Work restart at %d ms", rc);
  406. return ICA_NONCE_RESTART;
  407. }
  408. }
  409. }
  410. }
  411. }
  412. static const char *timing_mode_str(enum timing_mode timing_mode)
  413. {
  414. switch(timing_mode) {
  415. case MODE_DEFAULT:
  416. return MODE_DEFAULT_STR;
  417. case MODE_SHORT:
  418. return MODE_SHORT_STR;
  419. case MODE_LONG:
  420. return MODE_LONG_STR;
  421. case MODE_VALUE:
  422. return MODE_VALUE_STR;
  423. default:
  424. return MODE_UNKNOWN_STR;
  425. }
  426. }
  427. static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
  428. {
  429. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  430. enum sub_ident ident;
  431. double Hs;
  432. char buf[BUFSIZ+1];
  433. char *ptr, *comma, *eq;
  434. size_t max;
  435. int i;
  436. if (opt_icarus_timing == NULL)
  437. buf[0] = '\0';
  438. else {
  439. ptr = opt_icarus_timing;
  440. for (i = 0; i < this_option_offset; i++) {
  441. comma = strchr(ptr, ',');
  442. if (comma == NULL)
  443. break;
  444. ptr = comma + 1;
  445. }
  446. comma = strchr(ptr, ',');
  447. if (comma == NULL)
  448. max = strlen(ptr);
  449. else
  450. max = comma - ptr;
  451. if (max > BUFSIZ)
  452. max = BUFSIZ;
  453. strncpy(buf, ptr, max);
  454. buf[max] = '\0';
  455. }
  456. ident = usb_ident(icarus);
  457. switch (ident) {
  458. case IDENT_ICA:
  459. info->Hs = ICARUS_REV3_HASH_TIME;
  460. break;
  461. case IDENT_BLT:
  462. case IDENT_LLT:
  463. info->Hs = LANCELOT_HASH_TIME;
  464. break;
  465. case IDENT_AMU:
  466. info->Hs = ASICMINERUSB_HASH_TIME;
  467. break;
  468. case IDENT_CMR1:
  469. info->Hs = CAIRNSMORE1_HASH_TIME;
  470. break;
  471. case IDENT_CMR2:
  472. info->Hs = CAIRNSMORE2_HASH_TIME;
  473. break;
  474. default:
  475. quit(1, "Icarus get_options() called with invalid %s ident=%d",
  476. icarus->drv->name, ident);
  477. }
  478. info->read_time = 0;
  479. info->read_time_limit = 0; // 0 = no limit
  480. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  481. // short
  482. info->read_time = ICARUS_READ_COUNT_TIMING;
  483. info->timing_mode = MODE_SHORT;
  484. info->do_icarus_timing = true;
  485. } else if (strncasecmp(buf, MODE_SHORT_STREQ, strlen(MODE_SHORT_STREQ)) == 0) {
  486. // short=limit
  487. info->read_time = ICARUS_READ_COUNT_TIMING;
  488. info->timing_mode = MODE_SHORT;
  489. info->do_icarus_timing = true;
  490. info->read_time_limit = atoi(&buf[strlen(MODE_SHORT_STREQ)]);
  491. if (info->read_time_limit < 0)
  492. info->read_time_limit = 0;
  493. if (info->read_time_limit > ICARUS_READ_TIME_LIMIT_MAX)
  494. info->read_time_limit = ICARUS_READ_TIME_LIMIT_MAX;
  495. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  496. // long
  497. info->read_time = ICARUS_READ_COUNT_TIMING;
  498. info->timing_mode = MODE_LONG;
  499. info->do_icarus_timing = true;
  500. } else if (strncasecmp(buf, MODE_LONG_STREQ, strlen(MODE_LONG_STREQ)) == 0) {
  501. // long=limit
  502. info->read_time = ICARUS_READ_COUNT_TIMING;
  503. info->timing_mode = MODE_LONG;
  504. info->do_icarus_timing = true;
  505. info->read_time_limit = atoi(&buf[strlen(MODE_LONG_STREQ)]);
  506. if (info->read_time_limit < 0)
  507. info->read_time_limit = 0;
  508. if (info->read_time_limit > ICARUS_READ_TIME_LIMIT_MAX)
  509. info->read_time_limit = ICARUS_READ_TIME_LIMIT_MAX;
  510. } else if ((Hs = atof(buf)) != 0) {
  511. // ns[=read_time]
  512. info->Hs = Hs / NANOSEC;
  513. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  514. if ((eq = strchr(buf, '=')) != NULL)
  515. info->read_time = atoi(eq+1) * ICARUS_WAIT_TIMEOUT;
  516. if (info->read_time < ICARUS_READ_COUNT_MIN)
  517. info->read_time = SECTOMS(info->fullnonce) - ICARUS_READ_REDUCE;
  518. if (unlikely(info->read_time < ICARUS_READ_COUNT_MIN))
  519. info->read_time = ICARUS_READ_COUNT_MIN;
  520. info->timing_mode = MODE_VALUE;
  521. info->do_icarus_timing = false;
  522. } else {
  523. // Anything else in buf just uses DEFAULT mode
  524. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  525. if ((eq = strchr(buf, '=')) != NULL)
  526. info->read_time = atoi(eq+1) * ICARUS_WAIT_TIMEOUT;
  527. if (info->read_time < ICARUS_READ_COUNT_MIN)
  528. info->read_time = SECTOMS(info->fullnonce) - ICARUS_READ_REDUCE;
  529. if (unlikely(info->read_time < ICARUS_READ_COUNT_MIN))
  530. info->read_time = ICARUS_READ_COUNT_MIN;
  531. info->timing_mode = MODE_DEFAULT;
  532. info->do_icarus_timing = false;
  533. }
  534. info->min_data_count = MIN_DATA_COUNT;
  535. // All values are in multiples of ICARUS_WAIT_TIMEOUT
  536. info->read_time_limit *= ICARUS_WAIT_TIMEOUT;
  537. applog(LOG_DEBUG, "%s: cgid %d Init: mode=%s read_time=%dms limit=%dms Hs=%e",
  538. icarus->drv->name, icarus->cgminer_id,
  539. timing_mode_str(info->timing_mode),
  540. info->read_time, info->read_time_limit, info->Hs);
  541. }
  542. static uint32_t mask(int work_division)
  543. {
  544. uint32_t nonce_mask = 0x7fffffff;
  545. // yes we can calculate these, but this way it's easy to see what they are
  546. switch (work_division) {
  547. case 1:
  548. nonce_mask = 0xffffffff;
  549. break;
  550. case 2:
  551. nonce_mask = 0x7fffffff;
  552. break;
  553. case 4:
  554. nonce_mask = 0x3fffffff;
  555. break;
  556. case 8:
  557. nonce_mask = 0x1fffffff;
  558. break;
  559. default:
  560. quit(1, "Invalid2 icarus-options for work_division (%d) must be 1, 2, 4 or 8", work_division);
  561. }
  562. return nonce_mask;
  563. }
  564. static void get_options(int this_option_offset, struct cgpu_info *icarus, int *baud, int *work_division, int *fpga_count)
  565. {
  566. char buf[BUFSIZ+1];
  567. char *ptr, *comma, *colon, *colon2;
  568. enum sub_ident ident;
  569. size_t max;
  570. int i, tmp;
  571. if (opt_icarus_options == NULL)
  572. buf[0] = '\0';
  573. else {
  574. ptr = opt_icarus_options;
  575. for (i = 0; i < this_option_offset; i++) {
  576. comma = strchr(ptr, ',');
  577. if (comma == NULL)
  578. break;
  579. ptr = comma + 1;
  580. }
  581. comma = strchr(ptr, ',');
  582. if (comma == NULL)
  583. max = strlen(ptr);
  584. else
  585. max = comma - ptr;
  586. if (max > BUFSIZ)
  587. max = BUFSIZ;
  588. strncpy(buf, ptr, max);
  589. buf[max] = '\0';
  590. }
  591. ident = usb_ident(icarus);
  592. switch (ident) {
  593. case IDENT_ICA:
  594. case IDENT_BLT:
  595. case IDENT_LLT:
  596. *baud = ICARUS_IO_SPEED;
  597. *work_division = 2;
  598. *fpga_count = 2;
  599. break;
  600. case IDENT_AMU:
  601. *baud = ICARUS_IO_SPEED;
  602. *work_division = 1;
  603. *fpga_count = 1;
  604. break;
  605. case IDENT_CMR1:
  606. *baud = ICARUS_IO_SPEED;
  607. *work_division = 2;
  608. *fpga_count = 2;
  609. break;
  610. case IDENT_CMR2:
  611. *baud = ICARUS_IO_SPEED;
  612. *work_division = 1;
  613. *fpga_count = 1;
  614. break;
  615. default:
  616. quit(1, "Icarus get_options() called with invalid %s ident=%d",
  617. icarus->drv->name, ident);
  618. }
  619. if (*buf) {
  620. colon = strchr(buf, ':');
  621. if (colon)
  622. *(colon++) = '\0';
  623. if (*buf) {
  624. tmp = atoi(buf);
  625. switch (tmp) {
  626. case 115200:
  627. *baud = 115200;
  628. break;
  629. case 57600:
  630. *baud = 57600;
  631. break;
  632. default:
  633. quit(1, "Invalid icarus-options for baud (%s) must be 115200 or 57600", buf);
  634. }
  635. }
  636. if (colon && *colon) {
  637. colon2 = strchr(colon, ':');
  638. if (colon2)
  639. *(colon2++) = '\0';
  640. if (*colon) {
  641. tmp = atoi(colon);
  642. if (tmp == 1 || tmp == 2 || tmp == 4 || tmp == 8) {
  643. *work_division = tmp;
  644. *fpga_count = tmp; // default to the same
  645. } else {
  646. quit(1, "Invalid icarus-options for work_division (%s) must be 1, 2, 4 or 8", colon);
  647. }
  648. }
  649. if (colon2 && *colon2) {
  650. tmp = atoi(colon2);
  651. if (tmp > 0 && tmp <= *work_division)
  652. *fpga_count = tmp;
  653. else {
  654. quit(1, "Invalid icarus-options for fpga_count (%s) must be >0 and <=work_division (%d)", colon2, *work_division);
  655. }
  656. }
  657. }
  658. }
  659. }
  660. static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  661. {
  662. int this_option_offset = ++option_offset;
  663. struct ICARUS_INFO *info;
  664. struct timeval tv_start, tv_finish;
  665. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  666. // N.B. golden_ob MUST take less time to calculate
  667. // than the timeout set in icarus_open()
  668. // This one takes ~0.53ms on Rev3 Icarus
  669. const char golden_ob[] =
  670. "4679ba4ec99876bf4bfe086082b40025"
  671. "4df6c356451471139a3afa71e48f544a"
  672. "00000000000000000000000000000000"
  673. "0000000087320b1a1426674f2fa722ce";
  674. const char golden_nonce[] = "000187a2";
  675. const uint32_t golden_nonce_val = 0x000187a2;
  676. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  677. char *nonce_hex;
  678. int baud, uninitialised_var(work_division), uninitialised_var(fpga_count);
  679. struct cgpu_info *icarus;
  680. int ret, err, amount, tries;
  681. enum sub_ident ident;
  682. bool ok;
  683. icarus = usb_alloc_cgpu(&icarus_drv, 1);
  684. if (!usb_init(icarus, dev, found))
  685. goto shin;
  686. usb_buffer_enable(icarus);
  687. get_options(this_option_offset, icarus, &baud, &work_division, &fpga_count);
  688. hex2bin(ob_bin, golden_ob, sizeof(ob_bin));
  689. info = (struct ICARUS_INFO *)calloc(1, sizeof(struct ICARUS_INFO));
  690. if (unlikely(!info))
  691. quit(1, "Failed to malloc ICARUS_INFO");
  692. icarus->device_data = (void *)info;
  693. ident = usb_ident(icarus);
  694. switch (ident) {
  695. case IDENT_ICA:
  696. case IDENT_BLT:
  697. case IDENT_LLT:
  698. case IDENT_AMU:
  699. case IDENT_CMR1:
  700. info->timeout = ICARUS_WAIT_TIMEOUT;
  701. break;
  702. case IDENT_CMR2:
  703. info->timeout = ICARUS_CMR2_TIMEOUT;
  704. break;
  705. default:
  706. quit(1, "%s icarus_detect_one() invalid %s ident=%d",
  707. icarus->drv->dname, icarus->drv->dname, ident);
  708. }
  709. tries = 2;
  710. ok = false;
  711. while (!ok && tries-- > 0) {
  712. icarus_initialise(icarus, baud);
  713. err = usb_write(icarus, (char *)ob_bin, sizeof(ob_bin), &amount, C_SENDTESTWORK);
  714. if (err != LIBUSB_SUCCESS || amount != sizeof(ob_bin))
  715. continue;
  716. memset(nonce_bin, 0, sizeof(nonce_bin));
  717. ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, NULL, 100);
  718. if (ret != ICA_NONCE_OK)
  719. continue;
  720. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  721. if (strncmp(nonce_hex, golden_nonce, 8) == 0)
  722. ok = true;
  723. else {
  724. if (tries < 0) {
  725. applog(LOG_ERR,
  726. "Icarus Detect: "
  727. "Test failed at %s: get %s, should: %s",
  728. icarus->device_path, nonce_hex, golden_nonce);
  729. }
  730. }
  731. free(nonce_hex);
  732. }
  733. if (!ok)
  734. goto unshin;
  735. applog(LOG_DEBUG,
  736. "Icarus Detect: "
  737. "Test succeeded at %s: got %s",
  738. icarus->device_path, golden_nonce);
  739. /* We have a real Icarus! */
  740. if (!add_cgpu(icarus))
  741. goto unshin;
  742. update_usb_stats(icarus);
  743. applog(LOG_INFO, "%s%d: Found at %s",
  744. icarus->drv->name, icarus->device_id, icarus->device_path);
  745. applog(LOG_DEBUG, "%s%d: Init baud=%d work_division=%d fpga_count=%d",
  746. icarus->drv->name, icarus->device_id, baud, work_division, fpga_count);
  747. info->baud = baud;
  748. info->work_division = work_division;
  749. info->fpga_count = fpga_count;
  750. info->nonce_mask = mask(work_division);
  751. info->golden_hashes = (golden_nonce_val & info->nonce_mask) * fpga_count;
  752. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  753. set_timing_mode(this_option_offset, icarus);
  754. if (usb_ident(icarus) == IDENT_CMR2) {
  755. int i;
  756. for (i = 1; i < icarus->usbdev->found->intinfo_count; i++) {
  757. struct cgpu_info *cgtmp;
  758. struct ICARUS_INFO *intmp;
  759. cgtmp = usb_copy_cgpu(icarus);
  760. if (!cgtmp) {
  761. applog(LOG_ERR, "%s%d: Init failed initinfo %d",
  762. icarus->drv->name, icarus->device_id, i);
  763. continue;
  764. }
  765. cgtmp->usbinfo.usbstat = USB_NOSTAT;
  766. intmp = (struct ICARUS_INFO *)malloc(sizeof(struct ICARUS_INFO));
  767. if (unlikely(!intmp))
  768. quit(1, "Failed2 to malloc ICARUS_INFO");
  769. cgtmp->device_data = (void *)intmp;
  770. // Initialise everything to match
  771. memcpy(intmp, info, sizeof(struct ICARUS_INFO));
  772. intmp->intinfo = i;
  773. icarus_initialise(cgtmp, baud);
  774. if (!add_cgpu(cgtmp)) {
  775. usb_uninit(cgtmp);
  776. free(intmp);
  777. continue;
  778. }
  779. update_usb_stats(cgtmp);
  780. }
  781. }
  782. return true;
  783. unshin:
  784. usb_uninit(icarus);
  785. free(info);
  786. icarus->device_data = NULL;
  787. shin:
  788. icarus = usb_free_cgpu(icarus);
  789. return false;
  790. }
  791. static void icarus_detect(bool __maybe_unused hotplug)
  792. {
  793. usb_detect(&icarus_drv, icarus_detect_one);
  794. }
  795. static bool icarus_prepare(__maybe_unused struct thr_info *thr)
  796. {
  797. // struct cgpu_info *icarus = thr->cgpu;
  798. return true;
  799. }
  800. static int64_t icarus_scanhash(struct thr_info *thr, struct work *work,
  801. __maybe_unused int64_t max_nonce)
  802. {
  803. struct cgpu_info *icarus = thr->cgpu;
  804. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  805. int ret, err, amount;
  806. unsigned char ob_bin[64], nonce_bin[ICARUS_READ_SIZE];
  807. char *ob_hex;
  808. uint32_t nonce;
  809. int64_t hash_count;
  810. struct timeval tv_start, tv_finish, elapsed;
  811. struct timeval tv_history_start, tv_history_finish;
  812. double Ti, Xi;
  813. int curr_hw_errors, i;
  814. bool was_hw_error;
  815. struct ICARUS_HISTORY *history0, *history;
  816. int count;
  817. double Hs, W, fullnonce;
  818. int read_time;
  819. bool limited;
  820. int64_t estimate_hashes;
  821. uint32_t values;
  822. int64_t hash_count_range;
  823. // Device is gone
  824. if (icarus->usbinfo.nodev)
  825. return -1;
  826. if (!info->initialised)
  827. icarus_initialise(icarus, info->baud);
  828. elapsed.tv_sec = elapsed.tv_usec = 0;
  829. memset(ob_bin, 0, sizeof(ob_bin));
  830. memcpy(ob_bin, work->midstate, 32);
  831. memcpy(ob_bin + 52, work->data + 64, 12);
  832. rev(ob_bin, 32);
  833. rev(ob_bin + 52, 12);
  834. // We only want results for the work we are about to send
  835. usb_buffer_clear(icarus);
  836. err = usb_write_ii(icarus, info->intinfo, (char *)ob_bin, sizeof(ob_bin), &amount, C_SENDWORK);
  837. if (err < 0 || amount != sizeof(ob_bin)) {
  838. applog(LOG_ERR, "%s%i: Comms error (werr=%d amt=%d)",
  839. icarus->drv->name, icarus->device_id, err, amount);
  840. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  841. icarus_initialise(icarus, info->baud);
  842. return 0;
  843. }
  844. if (opt_debug) {
  845. ob_hex = bin2hex(ob_bin, sizeof(ob_bin));
  846. applog(LOG_DEBUG, "%s%d: sent %s",
  847. icarus->drv->name, icarus->device_id, ob_hex);
  848. free(ob_hex);
  849. }
  850. /* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
  851. memset(nonce_bin, 0, sizeof(nonce_bin));
  852. ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, thr, info->read_time);
  853. if (ret == ICA_NONCE_ERROR)
  854. return 0;
  855. work->blk.nonce = 0xffffffff;
  856. // aborted before becoming idle, get new work
  857. if (ret == ICA_NONCE_TIMEOUT || ret == ICA_NONCE_RESTART) {
  858. timersub(&tv_finish, &tv_start, &elapsed);
  859. // ONLY up to just when it aborted
  860. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  861. estimate_hashes = ((double)(elapsed.tv_sec)
  862. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  863. // If some Serial-USB delay allowed the full nonce range to
  864. // complete it can't have done more than a full nonce
  865. if (unlikely(estimate_hashes > 0xffffffff))
  866. estimate_hashes = 0xffffffff;
  867. applog(LOG_DEBUG, "%s%d: no nonce = 0x%08lX hashes (%ld.%06lds)",
  868. icarus->drv->name, icarus->device_id,
  869. (long unsigned int)estimate_hashes,
  870. elapsed.tv_sec, elapsed.tv_usec);
  871. return estimate_hashes;
  872. }
  873. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  874. nonce = htobe32(nonce);
  875. curr_hw_errors = icarus->hw_errors;
  876. submit_nonce(thr, work, nonce);
  877. was_hw_error = (curr_hw_errors > icarus->hw_errors);
  878. hash_count = (nonce & info->nonce_mask);
  879. hash_count++;
  880. hash_count *= info->fpga_count;
  881. #if 0
  882. // This appears to only return zero nonce values
  883. if (usb_buffer_size(icarus) > 3) {
  884. memcpy((char *)&nonce, icarus->usbdev->buffer, sizeof(nonce_bin));
  885. nonce = htobe32(nonce);
  886. applog(LOG_WARNING, "%s%d: attempting to submit 2nd nonce = 0x%08lX",
  887. icarus->drv->name, icarus->device_id,
  888. (long unsigned int)nonce);
  889. curr_hw_errors = icarus->hw_errors;
  890. submit_nonce(thr, work, nonce);
  891. was_hw_error = (curr_hw_errors > icarus->hw_errors);
  892. }
  893. #endif
  894. if (opt_debug || info->do_icarus_timing)
  895. timersub(&tv_finish, &tv_start, &elapsed);
  896. applog(LOG_DEBUG, "%s%d: nonce = 0x%08x = 0x%08lX hashes (%ld.%06lds)",
  897. icarus->drv->name, icarus->device_id,
  898. nonce, (long unsigned int)hash_count,
  899. elapsed.tv_sec, elapsed.tv_usec);
  900. // Ignore possible end condition values ... and hw errors
  901. // TODO: set limitations on calculated values depending on the device
  902. // to avoid crap values caused by CPU/Task Switching/Swapping/etc
  903. if (info->do_icarus_timing
  904. && !was_hw_error
  905. && ((nonce & info->nonce_mask) > END_CONDITION)
  906. && ((nonce & info->nonce_mask) < (info->nonce_mask & ~END_CONDITION))) {
  907. cgtime(&tv_history_start);
  908. history0 = &(info->history[0]);
  909. if (history0->values == 0)
  910. timeradd(&tv_start, &history_sec, &(history0->finish));
  911. Ti = (double)(elapsed.tv_sec)
  912. + ((double)(elapsed.tv_usec))/((double)1000000)
  913. - ((double)ICARUS_READ_TIME(info->baud));
  914. Xi = (double)hash_count;
  915. history0->sumXiTi += Xi * Ti;
  916. history0->sumXi += Xi;
  917. history0->sumTi += Ti;
  918. history0->sumXi2 += Xi * Xi;
  919. history0->values++;
  920. if (history0->hash_count_max < hash_count)
  921. history0->hash_count_max = hash_count;
  922. if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
  923. history0->hash_count_min = hash_count;
  924. if (history0->values >= info->min_data_count
  925. && timercmp(&tv_start, &(history0->finish), >)) {
  926. for (i = INFO_HISTORY; i > 0; i--)
  927. memcpy(&(info->history[i]),
  928. &(info->history[i-1]),
  929. sizeof(struct ICARUS_HISTORY));
  930. // Initialise history0 to zero for summary calculation
  931. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  932. // We just completed a history data set
  933. // So now recalc read_time based on the whole history thus we will
  934. // initially get more accurate until it completes INFO_HISTORY
  935. // total data sets
  936. count = 0;
  937. for (i = 1 ; i <= INFO_HISTORY; i++) {
  938. history = &(info->history[i]);
  939. if (history->values >= MIN_DATA_COUNT) {
  940. count++;
  941. history0->sumXiTi += history->sumXiTi;
  942. history0->sumXi += history->sumXi;
  943. history0->sumTi += history->sumTi;
  944. history0->sumXi2 += history->sumXi2;
  945. history0->values += history->values;
  946. if (history0->hash_count_max < history->hash_count_max)
  947. history0->hash_count_max = history->hash_count_max;
  948. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  949. history0->hash_count_min = history->hash_count_min;
  950. }
  951. }
  952. // All history data
  953. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  954. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  955. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  956. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  957. values = history0->values;
  958. // Initialise history0 to zero for next data set
  959. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  960. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  961. read_time = SECTOMS(fullnonce) - ICARUS_READ_REDUCE;
  962. if (info->read_time_limit > 0 && read_time > info->read_time_limit) {
  963. read_time = info->read_time_limit;
  964. limited = true;
  965. } else
  966. limited = false;
  967. info->Hs = Hs;
  968. info->read_time = read_time;
  969. info->fullnonce = fullnonce;
  970. info->count = count;
  971. info->W = W;
  972. info->values = values;
  973. info->hash_count_range = hash_count_range;
  974. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  975. info->min_data_count *= 2;
  976. else if (info->timing_mode == MODE_SHORT)
  977. info->do_icarus_timing = false;
  978. applog(LOG_WARNING, "%s%d Re-estimate: Hs=%e W=%e read_time=%dms%s fullnonce=%.3fs",
  979. icarus->drv->name, icarus->device_id, Hs, W, read_time,
  980. limited ? " (limited)" : "", fullnonce);
  981. }
  982. info->history_count++;
  983. cgtime(&tv_history_finish);
  984. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  985. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  986. }
  987. return hash_count;
  988. }
  989. static struct api_data *icarus_api_stats(struct cgpu_info *cgpu)
  990. {
  991. struct api_data *root = NULL;
  992. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
  993. // Warning, access to these is not locked - but we don't really
  994. // care since hashing performance is way more important than
  995. // locking access to displaying API debug 'stats'
  996. // If locking becomes an issue for any of them, use copy_data=true also
  997. root = api_add_int(root, "read_time", &(info->read_time), false);
  998. root = api_add_int(root, "read_time_limit", &(info->read_time_limit), false);
  999. root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
  1000. root = api_add_int(root, "count", &(info->count), false);
  1001. root = api_add_hs(root, "Hs", &(info->Hs), false);
  1002. root = api_add_double(root, "W", &(info->W), false);
  1003. root = api_add_uint(root, "total_values", &(info->values), false);
  1004. root = api_add_uint64(root, "range", &(info->hash_count_range), false);
  1005. root = api_add_uint64(root, "history_count", &(info->history_count), false);
  1006. root = api_add_timeval(root, "history_time", &(info->history_time), false);
  1007. root = api_add_uint(root, "min_data_count", &(info->min_data_count), false);
  1008. root = api_add_uint(root, "timing_values", &(info->history[0].values), false);
  1009. root = api_add_const(root, "timing_mode", timing_mode_str(info->timing_mode), false);
  1010. root = api_add_bool(root, "is_timing", &(info->do_icarus_timing), false);
  1011. root = api_add_int(root, "baud", &(info->baud), false);
  1012. root = api_add_int(root, "work_division", &(info->work_division), false);
  1013. root = api_add_int(root, "fpga_count", &(info->fpga_count), false);
  1014. return root;
  1015. }
  1016. static void icarus_shutdown(__maybe_unused struct thr_info *thr)
  1017. {
  1018. // TODO: ?
  1019. }
  1020. struct device_drv icarus_drv = {
  1021. .drv_id = DRIVER_icarus,
  1022. .dname = "Icarus",
  1023. .name = "ICA",
  1024. .drv_detect = icarus_detect,
  1025. .get_api_stats = icarus_api_stats,
  1026. .thread_prepare = icarus_prepare,
  1027. .scanhash = icarus_scanhash,
  1028. .thread_shutdown = icarus_shutdown,
  1029. };