driver-icarus.c 46 KB

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