driver-icarus.c 41 KB

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