driver-icarus.c 31 KB

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