driver-icarus.c 31 KB

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