driver-icarus.c 31 KB

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