driver-icarus.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  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,
  391. struct timeval *tv_finish, struct thr_info *thr, int read_time)
  392. {
  393. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  394. int err, amt, rc;
  395. if (icarus->usbinfo.nodev)
  396. return ICA_NONCE_ERROR;
  397. cgtime(tv_start);
  398. err = usb_read_ii_timeout_cancellable(icarus, info->intinfo, (char *)buf,
  399. ICARUS_READ_SIZE, &amt, read_time,
  400. C_GETRESULTS);
  401. cgtime(tv_finish);
  402. if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) {
  403. applog(LOG_ERR, "%s%i: Comms error (rerr=%d amt=%d)", icarus->drv->name,
  404. icarus->device_id, err, amt);
  405. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  406. return ICA_NONCE_ERROR;
  407. }
  408. if (amt >= ICARUS_READ_SIZE)
  409. return ICA_NONCE_OK;
  410. rc = SECTOMS(tdiff(tv_finish, tv_start));
  411. if (thr && thr->work_restart) {
  412. applog(LOG_DEBUG, "Icarus Read: Work restart at %d ms", rc);
  413. return ICA_NONCE_RESTART;
  414. }
  415. if (amt > 0)
  416. applog(LOG_DEBUG, "Icarus Read: Timeout reading for %d ms", rc);
  417. else
  418. applog(LOG_DEBUG, "Icarus Read: No data for %d ms", rc);
  419. return ICA_NONCE_TIMEOUT;
  420. }
  421. static const char *timing_mode_str(enum timing_mode timing_mode)
  422. {
  423. switch(timing_mode) {
  424. case MODE_DEFAULT:
  425. return MODE_DEFAULT_STR;
  426. case MODE_SHORT:
  427. return MODE_SHORT_STR;
  428. case MODE_LONG:
  429. return MODE_LONG_STR;
  430. case MODE_VALUE:
  431. return MODE_VALUE_STR;
  432. default:
  433. return MODE_UNKNOWN_STR;
  434. }
  435. }
  436. static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus)
  437. {
  438. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  439. enum sub_ident ident;
  440. double Hs;
  441. char buf[BUFSIZ+1];
  442. char *ptr, *comma, *eq;
  443. size_t max;
  444. int i;
  445. if (opt_icarus_timing == NULL)
  446. buf[0] = '\0';
  447. else {
  448. ptr = opt_icarus_timing;
  449. for (i = 0; i < this_option_offset; i++) {
  450. comma = strchr(ptr, ',');
  451. if (comma == NULL)
  452. break;
  453. ptr = comma + 1;
  454. }
  455. comma = strchr(ptr, ',');
  456. if (comma == NULL)
  457. max = strlen(ptr);
  458. else
  459. max = comma - ptr;
  460. if (max > BUFSIZ)
  461. max = BUFSIZ;
  462. strncpy(buf, ptr, max);
  463. buf[max] = '\0';
  464. }
  465. ident = usb_ident(icarus);
  466. switch (ident) {
  467. case IDENT_ICA:
  468. info->Hs = ICARUS_REV3_HASH_TIME;
  469. break;
  470. case IDENT_BLT:
  471. case IDENT_LLT:
  472. info->Hs = LANCELOT_HASH_TIME;
  473. break;
  474. case IDENT_AMU:
  475. info->Hs = ASICMINERUSB_HASH_TIME;
  476. break;
  477. case IDENT_CMR1:
  478. info->Hs = CAIRNSMORE1_HASH_TIME;
  479. break;
  480. case IDENT_CMR2:
  481. info->Hs = CAIRNSMORE2_HASH_TIME;
  482. break;
  483. default:
  484. quit(1, "Icarus get_options() called with invalid %s ident=%d",
  485. icarus->drv->name, ident);
  486. }
  487. info->read_time = 0;
  488. info->read_time_limit = 0; // 0 = no limit
  489. if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
  490. // short
  491. info->read_time = ICARUS_READ_COUNT_TIMING;
  492. info->timing_mode = MODE_SHORT;
  493. info->do_icarus_timing = true;
  494. } else if (strncasecmp(buf, MODE_SHORT_STREQ, strlen(MODE_SHORT_STREQ)) == 0) {
  495. // short=limit
  496. info->read_time = ICARUS_READ_COUNT_TIMING;
  497. info->timing_mode = MODE_SHORT;
  498. info->do_icarus_timing = true;
  499. info->read_time_limit = atoi(&buf[strlen(MODE_SHORT_STREQ)]);
  500. if (info->read_time_limit < 0)
  501. info->read_time_limit = 0;
  502. if (info->read_time_limit > ICARUS_READ_TIME_LIMIT_MAX)
  503. info->read_time_limit = ICARUS_READ_TIME_LIMIT_MAX;
  504. } else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
  505. // long
  506. info->read_time = ICARUS_READ_COUNT_TIMING;
  507. info->timing_mode = MODE_LONG;
  508. info->do_icarus_timing = true;
  509. } else if (strncasecmp(buf, MODE_LONG_STREQ, strlen(MODE_LONG_STREQ)) == 0) {
  510. // long=limit
  511. info->read_time = ICARUS_READ_COUNT_TIMING;
  512. info->timing_mode = MODE_LONG;
  513. info->do_icarus_timing = true;
  514. info->read_time_limit = atoi(&buf[strlen(MODE_LONG_STREQ)]);
  515. if (info->read_time_limit < 0)
  516. info->read_time_limit = 0;
  517. if (info->read_time_limit > ICARUS_READ_TIME_LIMIT_MAX)
  518. info->read_time_limit = ICARUS_READ_TIME_LIMIT_MAX;
  519. } else if ((Hs = atof(buf)) != 0) {
  520. // ns[=read_time]
  521. info->Hs = Hs / NANOSEC;
  522. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  523. if ((eq = strchr(buf, '=')) != NULL)
  524. info->read_time = atoi(eq+1) * ICARUS_WAIT_TIMEOUT;
  525. if (info->read_time < ICARUS_READ_COUNT_MIN)
  526. info->read_time = SECTOMS(info->fullnonce) - ICARUS_READ_REDUCE;
  527. if (unlikely(info->read_time < ICARUS_READ_COUNT_MIN))
  528. info->read_time = ICARUS_READ_COUNT_MIN;
  529. info->timing_mode = MODE_VALUE;
  530. info->do_icarus_timing = false;
  531. } else {
  532. // Anything else in buf just uses DEFAULT mode
  533. info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
  534. if ((eq = strchr(buf, '=')) != NULL)
  535. info->read_time = atoi(eq+1) * ICARUS_WAIT_TIMEOUT;
  536. if (info->read_time < ICARUS_READ_COUNT_MIN)
  537. info->read_time = SECTOMS(info->fullnonce) - ICARUS_READ_REDUCE;
  538. if (unlikely(info->read_time < ICARUS_READ_COUNT_MIN))
  539. info->read_time = ICARUS_READ_COUNT_MIN;
  540. info->timing_mode = MODE_DEFAULT;
  541. info->do_icarus_timing = false;
  542. }
  543. info->min_data_count = MIN_DATA_COUNT;
  544. // All values are in multiples of ICARUS_WAIT_TIMEOUT
  545. info->read_time_limit *= ICARUS_WAIT_TIMEOUT;
  546. applog(LOG_DEBUG, "%s: cgid %d Init: mode=%s read_time=%dms limit=%dms Hs=%e",
  547. icarus->drv->name, icarus->cgminer_id,
  548. timing_mode_str(info->timing_mode),
  549. info->read_time, info->read_time_limit, info->Hs);
  550. }
  551. static uint32_t mask(int work_division)
  552. {
  553. uint32_t nonce_mask = 0x7fffffff;
  554. // yes we can calculate these, but this way it's easy to see what they are
  555. switch (work_division) {
  556. case 1:
  557. nonce_mask = 0xffffffff;
  558. break;
  559. case 2:
  560. nonce_mask = 0x7fffffff;
  561. break;
  562. case 4:
  563. nonce_mask = 0x3fffffff;
  564. break;
  565. case 8:
  566. nonce_mask = 0x1fffffff;
  567. break;
  568. default:
  569. quit(1, "Invalid2 icarus-options for work_division (%d) must be 1, 2, 4 or 8", work_division);
  570. }
  571. return nonce_mask;
  572. }
  573. static void get_options(int this_option_offset, struct cgpu_info *icarus, int *baud, int *work_division, int *fpga_count)
  574. {
  575. char buf[BUFSIZ+1];
  576. char *ptr, *comma, *colon, *colon2;
  577. enum sub_ident ident;
  578. size_t max;
  579. int i, tmp;
  580. if (opt_icarus_options == NULL)
  581. buf[0] = '\0';
  582. else {
  583. ptr = opt_icarus_options;
  584. for (i = 0; i < this_option_offset; i++) {
  585. comma = strchr(ptr, ',');
  586. if (comma == NULL)
  587. break;
  588. ptr = comma + 1;
  589. }
  590. comma = strchr(ptr, ',');
  591. if (comma == NULL)
  592. max = strlen(ptr);
  593. else
  594. max = comma - ptr;
  595. if (max > BUFSIZ)
  596. max = BUFSIZ;
  597. strncpy(buf, ptr, max);
  598. buf[max] = '\0';
  599. }
  600. ident = usb_ident(icarus);
  601. switch (ident) {
  602. case IDENT_ICA:
  603. case IDENT_BLT:
  604. case IDENT_LLT:
  605. *baud = ICARUS_IO_SPEED;
  606. *work_division = 2;
  607. *fpga_count = 2;
  608. break;
  609. case IDENT_AMU:
  610. *baud = ICARUS_IO_SPEED;
  611. *work_division = 1;
  612. *fpga_count = 1;
  613. break;
  614. case IDENT_CMR1:
  615. *baud = ICARUS_IO_SPEED;
  616. *work_division = 2;
  617. *fpga_count = 2;
  618. break;
  619. case IDENT_CMR2:
  620. *baud = ICARUS_IO_SPEED;
  621. *work_division = 1;
  622. *fpga_count = 1;
  623. break;
  624. default:
  625. quit(1, "Icarus get_options() called with invalid %s ident=%d",
  626. icarus->drv->name, ident);
  627. }
  628. if (*buf) {
  629. colon = strchr(buf, ':');
  630. if (colon)
  631. *(colon++) = '\0';
  632. if (*buf) {
  633. tmp = atoi(buf);
  634. switch (tmp) {
  635. case 115200:
  636. *baud = 115200;
  637. break;
  638. case 57600:
  639. *baud = 57600;
  640. break;
  641. default:
  642. quit(1, "Invalid icarus-options for baud (%s) must be 115200 or 57600", buf);
  643. }
  644. }
  645. if (colon && *colon) {
  646. colon2 = strchr(colon, ':');
  647. if (colon2)
  648. *(colon2++) = '\0';
  649. if (*colon) {
  650. tmp = atoi(colon);
  651. if (tmp == 1 || tmp == 2 || tmp == 4 || tmp == 8) {
  652. *work_division = tmp;
  653. *fpga_count = tmp; // default to the same
  654. } else {
  655. quit(1, "Invalid icarus-options for work_division (%s) must be 1, 2, 4 or 8", colon);
  656. }
  657. }
  658. if (colon2 && *colon2) {
  659. tmp = atoi(colon2);
  660. if (tmp > 0 && tmp <= *work_division)
  661. *fpga_count = tmp;
  662. else {
  663. quit(1, "Invalid icarus-options for fpga_count (%s) must be >0 and <=work_division (%d)", colon2, *work_division);
  664. }
  665. }
  666. }
  667. }
  668. }
  669. static bool icarus_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
  670. {
  671. int this_option_offset = ++option_offset;
  672. struct ICARUS_INFO *info;
  673. struct timeval tv_start, tv_finish;
  674. // Block 171874 nonce = (0xa2870100) = 0x000187a2
  675. // N.B. golden_ob MUST take less time to calculate
  676. // than the timeout set in icarus_open()
  677. // This one takes ~0.53ms on Rev3 Icarus
  678. const char golden_ob[] =
  679. "4679ba4ec99876bf4bfe086082b40025"
  680. "4df6c356451471139a3afa71e48f544a"
  681. "00000000000000000000000000000000"
  682. "0000000087320b1a1426674f2fa722ce";
  683. const char golden_nonce[] = "000187a2";
  684. const uint32_t golden_nonce_val = 0x000187a2;
  685. unsigned char nonce_bin[ICARUS_READ_SIZE];
  686. struct ICARUS_WORK workdata;
  687. char *nonce_hex;
  688. int baud, uninitialised_var(work_division), uninitialised_var(fpga_count);
  689. struct cgpu_info *icarus;
  690. int ret, err, amount, tries, i;
  691. bool ok;
  692. bool cmr2_ok[CAIRNSMORE2_INTS];
  693. int cmr2_count;
  694. if ((sizeof(workdata) << 1) != (sizeof(golden_ob) - 1))
  695. quithere(1, "Data and golden_ob sizes don't match");
  696. icarus = usb_alloc_cgpu(&icarus_drv, 1);
  697. if (!usb_init(icarus, dev, found))
  698. goto shin;
  699. usb_buffer_enable(icarus);
  700. get_options(this_option_offset, icarus, &baud, &work_division, &fpga_count);
  701. hex2bin((void *)(&workdata), golden_ob, sizeof(workdata));
  702. info = (struct ICARUS_INFO *)calloc(1, sizeof(struct ICARUS_INFO));
  703. if (unlikely(!info))
  704. quit(1, "Failed to malloc ICARUS_INFO");
  705. icarus->device_data = (void *)info;
  706. info->ident = usb_ident(icarus);
  707. switch (info->ident) {
  708. case IDENT_ICA:
  709. case IDENT_BLT:
  710. case IDENT_LLT:
  711. case IDENT_AMU:
  712. case IDENT_CMR1:
  713. info->timeout = ICARUS_WAIT_TIMEOUT;
  714. break;
  715. case IDENT_CMR2:
  716. if (found->intinfo_count != CAIRNSMORE2_INTS) {
  717. quithere(1, "CMR2 Interface count (%d) isn't expected: %d",
  718. found->intinfo_count,
  719. CAIRNSMORE2_INTS);
  720. }
  721. info->timeout = ICARUS_CMR2_TIMEOUT;
  722. cmr2_count = 0;
  723. for (i = 0; i < CAIRNSMORE2_INTS; i++)
  724. cmr2_ok[i] = false;
  725. break;
  726. default:
  727. quit(1, "%s icarus_detect_one() invalid %s ident=%d",
  728. icarus->drv->dname, icarus->drv->dname, info->ident);
  729. }
  730. // For CMR2 test each USB Interface
  731. cmr2_retry:
  732. tries = 2;
  733. ok = false;
  734. while (!ok && tries-- > 0) {
  735. icarus_initialise(icarus, baud);
  736. err = usb_write_ii(icarus, info->intinfo,
  737. (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
  738. if (err != LIBUSB_SUCCESS || amount != sizeof(workdata))
  739. continue;
  740. memset(nonce_bin, 0, sizeof(nonce_bin));
  741. ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, NULL, 100);
  742. if (ret != ICA_NONCE_OK)
  743. continue;
  744. nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin));
  745. if (strncmp(nonce_hex, golden_nonce, 8) == 0)
  746. ok = true;
  747. else {
  748. if (tries < 0 && info->ident != IDENT_CMR2) {
  749. applog(LOG_ERR,
  750. "Icarus Detect: "
  751. "Test failed at %s: get %s, should: %s",
  752. icarus->device_path, nonce_hex, golden_nonce);
  753. }
  754. }
  755. free(nonce_hex);
  756. }
  757. if (!ok) {
  758. if (info->ident != IDENT_CMR2)
  759. goto unshin;
  760. if (info->intinfo < CAIRNSMORE2_INTS-1) {
  761. info->intinfo++;
  762. goto cmr2_retry;
  763. }
  764. } else {
  765. if (info->ident == IDENT_CMR2) {
  766. applog(LOG_DEBUG,
  767. "Icarus Detect: "
  768. "Test succeeded at %s i%d: got %s",
  769. icarus->device_path, info->intinfo, golden_nonce);
  770. cmr2_ok[info->intinfo] = true;
  771. cmr2_count++;
  772. if (info->intinfo < CAIRNSMORE2_INTS-1) {
  773. info->intinfo++;
  774. goto cmr2_retry;
  775. }
  776. }
  777. }
  778. if (info->ident == IDENT_CMR2) {
  779. if (cmr2_count == 0) {
  780. applog(LOG_ERR,
  781. "Icarus Detect: Test failed at %s: for all %d CMR2 Interfaces",
  782. icarus->device_path, CAIRNSMORE2_INTS);
  783. goto unshin;
  784. }
  785. // set the interface to the first one that succeeded
  786. for (i = 0; i < CAIRNSMORE2_INTS; i++)
  787. if (cmr2_ok[i]) {
  788. info->intinfo = i;
  789. break;
  790. }
  791. } else {
  792. applog(LOG_DEBUG,
  793. "Icarus Detect: "
  794. "Test succeeded at %s: got %s",
  795. icarus->device_path, golden_nonce);
  796. }
  797. /* We have a real Icarus! */
  798. if (!add_cgpu(icarus))
  799. goto unshin;
  800. update_usb_stats(icarus);
  801. applog(LOG_INFO, "%s%d: Found at %s",
  802. icarus->drv->name, icarus->device_id, icarus->device_path);
  803. if (info->ident == IDENT_CMR2) {
  804. applog(LOG_INFO, "%s%d: with %d Interface%s",
  805. icarus->drv->name, icarus->device_id,
  806. cmr2_count, cmr2_count > 1 ? "s" : "");
  807. // Assume 1 or 2 are running FPGA pairs
  808. if (cmr2_count < 3) {
  809. work_division = fpga_count = 2;
  810. info->Hs /= 2;
  811. }
  812. }
  813. applog(LOG_DEBUG, "%s%d: Init baud=%d work_division=%d fpga_count=%d",
  814. icarus->drv->name, icarus->device_id, baud, work_division, fpga_count);
  815. info->baud = baud;
  816. info->work_division = work_division;
  817. info->fpga_count = fpga_count;
  818. info->nonce_mask = mask(work_division);
  819. info->golden_hashes = (golden_nonce_val & info->nonce_mask) * fpga_count;
  820. timersub(&tv_finish, &tv_start, &(info->golden_tv));
  821. set_timing_mode(this_option_offset, icarus);
  822. if (info->ident == IDENT_CMR2) {
  823. int i;
  824. for (i = info->intinfo + 1; i < icarus->usbdev->found->intinfo_count; i++) {
  825. struct cgpu_info *cgtmp;
  826. struct ICARUS_INFO *intmp;
  827. if (!cmr2_ok[i])
  828. continue;
  829. cgtmp = usb_copy_cgpu(icarus);
  830. if (!cgtmp) {
  831. applog(LOG_ERR, "%s%d: Init failed initinfo %d",
  832. icarus->drv->name, icarus->device_id, i);
  833. continue;
  834. }
  835. cgtmp->usbinfo.usbstat = USB_NOSTAT;
  836. intmp = (struct ICARUS_INFO *)malloc(sizeof(struct ICARUS_INFO));
  837. if (unlikely(!intmp))
  838. quit(1, "Failed2 to malloc ICARUS_INFO");
  839. cgtmp->device_data = (void *)intmp;
  840. // Initialise everything to match
  841. memcpy(intmp, info, sizeof(struct ICARUS_INFO));
  842. intmp->intinfo = i;
  843. icarus_initialise(cgtmp, baud);
  844. if (!add_cgpu(cgtmp)) {
  845. usb_uninit(cgtmp);
  846. free(intmp);
  847. continue;
  848. }
  849. update_usb_stats(cgtmp);
  850. }
  851. }
  852. return true;
  853. unshin:
  854. usb_uninit(icarus);
  855. free(info);
  856. icarus->device_data = NULL;
  857. shin:
  858. icarus = usb_free_cgpu(icarus);
  859. return false;
  860. }
  861. static void icarus_detect(bool __maybe_unused hotplug)
  862. {
  863. usb_detect(&icarus_drv, icarus_detect_one);
  864. }
  865. static bool icarus_prepare(__maybe_unused struct thr_info *thr)
  866. {
  867. // struct cgpu_info *icarus = thr->cgpu;
  868. return true;
  869. }
  870. static void cmr2_command(struct cgpu_info *icarus, uint8_t cmd, uint8_t data)
  871. {
  872. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  873. struct ICARUS_WORK workdata;
  874. int amount;
  875. memset((void *)(&workdata), 0, sizeof(workdata));
  876. workdata.prefix = ICARUS_CMR2_PREFIX;
  877. workdata.cmd = cmd;
  878. workdata.data = data;
  879. workdata.check = workdata.data ^ workdata.cmd ^ workdata.prefix ^ ICARUS_CMR2_CHECK;
  880. usb_write_ii(icarus, info->intinfo, (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
  881. }
  882. static void cmr2_commands(struct cgpu_info *icarus)
  883. {
  884. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  885. if (info->speed_next_work) {
  886. info->speed_next_work = false;
  887. cmr2_command(icarus, ICARUS_CMR2_CMD_SPEED, info->cmr2_speed);
  888. return;
  889. }
  890. if (info->flash_next_work) {
  891. info->flash_next_work = false;
  892. cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_ON);
  893. cgsleep_ms(250);
  894. cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_OFF);
  895. cgsleep_ms(250);
  896. cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_ON);
  897. cgsleep_ms(250);
  898. cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_OFF);
  899. return;
  900. }
  901. }
  902. static int64_t icarus_scanwork(struct thr_info *thr)
  903. {
  904. struct cgpu_info *icarus = thr->cgpu;
  905. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
  906. int ret, err, amount;
  907. unsigned char nonce_bin[ICARUS_READ_SIZE];
  908. struct ICARUS_WORK workdata;
  909. char *ob_hex;
  910. uint32_t nonce;
  911. int64_t hash_count = 0;
  912. struct timeval tv_start, tv_finish, elapsed;
  913. struct timeval tv_history_start, tv_history_finish;
  914. double Ti, Xi;
  915. int curr_hw_errors, i;
  916. bool was_hw_error;
  917. struct work *work;
  918. struct ICARUS_HISTORY *history0, *history;
  919. int count;
  920. double Hs, W, fullnonce;
  921. int read_time;
  922. bool limited;
  923. int64_t estimate_hashes;
  924. uint32_t values;
  925. int64_t hash_count_range;
  926. // Device is gone
  927. if (icarus->usbinfo.nodev)
  928. return -1;
  929. elapsed.tv_sec = elapsed.tv_usec = 0;
  930. work = get_work(thr, thr->id);
  931. memset((void *)(&workdata), 0, sizeof(workdata));
  932. memcpy(&(workdata.midstate), work->midstate, ICARUS_MIDSTATE_SIZE);
  933. memcpy(&(workdata.work), work->data + ICARUS_WORK_DATA_OFFSET, ICARUS_WORK_SIZE);
  934. rev((void *)(&(workdata.midstate)), ICARUS_MIDSTATE_SIZE);
  935. rev((void *)(&(workdata.work)), ICARUS_WORK_SIZE);
  936. if (info->speed_next_work || info->flash_next_work)
  937. cmr2_commands(icarus);
  938. // We only want results for the work we are about to send
  939. usb_buffer_clear(icarus);
  940. err = usb_write_ii(icarus, info->intinfo, (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK);
  941. if (err < 0 || amount != sizeof(workdata)) {
  942. applog(LOG_ERR, "%s%i: Comms error (werr=%d amt=%d)",
  943. icarus->drv->name, icarus->device_id, err, amount);
  944. dev_error(icarus, REASON_DEV_COMMS_ERROR);
  945. icarus_initialise(icarus, info->baud);
  946. goto out;
  947. }
  948. if (opt_debug) {
  949. ob_hex = bin2hex((void *)(&workdata), sizeof(workdata));
  950. applog(LOG_DEBUG, "%s%d: sent %s",
  951. icarus->drv->name, icarus->device_id, ob_hex);
  952. free(ob_hex);
  953. }
  954. /* Icarus will return 4 bytes (ICARUS_READ_SIZE) nonces or nothing */
  955. memset(nonce_bin, 0, sizeof(nonce_bin));
  956. ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, thr, info->read_time);
  957. if (ret == ICA_NONCE_ERROR)
  958. goto out;
  959. work->blk.nonce = 0xffffffff;
  960. // aborted before becoming idle, get new work
  961. if (ret == ICA_NONCE_TIMEOUT || ret == ICA_NONCE_RESTART) {
  962. timersub(&tv_finish, &tv_start, &elapsed);
  963. // ONLY up to just when it aborted
  964. // We didn't read a reply so we don't subtract ICARUS_READ_TIME
  965. estimate_hashes = ((double)(elapsed.tv_sec)
  966. + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs;
  967. // If some Serial-USB delay allowed the full nonce range to
  968. // complete it can't have done more than a full nonce
  969. if (unlikely(estimate_hashes > 0xffffffff))
  970. estimate_hashes = 0xffffffff;
  971. applog(LOG_DEBUG, "%s%d: no nonce = 0x%08lX hashes (%ld.%06lds)",
  972. icarus->drv->name, icarus->device_id,
  973. (long unsigned int)estimate_hashes,
  974. elapsed.tv_sec, elapsed.tv_usec);
  975. hash_count = estimate_hashes;
  976. goto out;
  977. }
  978. memcpy((char *)&nonce, nonce_bin, sizeof(nonce_bin));
  979. nonce = htobe32(nonce);
  980. curr_hw_errors = icarus->hw_errors;
  981. submit_nonce(thr, work, nonce);
  982. was_hw_error = (curr_hw_errors > icarus->hw_errors);
  983. hash_count = (nonce & info->nonce_mask);
  984. hash_count++;
  985. hash_count *= info->fpga_count;
  986. #if 0
  987. // This appears to only return zero nonce values
  988. if (usb_buffer_size(icarus) > 3) {
  989. memcpy((char *)&nonce, icarus->usbdev->buffer, sizeof(nonce_bin));
  990. nonce = htobe32(nonce);
  991. applog(LOG_WARNING, "%s%d: attempting to submit 2nd nonce = 0x%08lX",
  992. icarus->drv->name, icarus->device_id,
  993. (long unsigned int)nonce);
  994. curr_hw_errors = icarus->hw_errors;
  995. submit_nonce(thr, work, nonce);
  996. was_hw_error = (curr_hw_errors > icarus->hw_errors);
  997. }
  998. #endif
  999. if (opt_debug || info->do_icarus_timing)
  1000. timersub(&tv_finish, &tv_start, &elapsed);
  1001. applog(LOG_DEBUG, "%s%d: nonce = 0x%08x = 0x%08lX hashes (%ld.%06lds)",
  1002. icarus->drv->name, icarus->device_id,
  1003. nonce, (long unsigned int)hash_count,
  1004. elapsed.tv_sec, elapsed.tv_usec);
  1005. // Ignore possible end condition values ... and hw errors
  1006. // TODO: set limitations on calculated values depending on the device
  1007. // to avoid crap values caused by CPU/Task Switching/Swapping/etc
  1008. if (info->do_icarus_timing
  1009. && !was_hw_error
  1010. && ((nonce & info->nonce_mask) > END_CONDITION)
  1011. && ((nonce & info->nonce_mask) < (info->nonce_mask & ~END_CONDITION))) {
  1012. cgtime(&tv_history_start);
  1013. history0 = &(info->history[0]);
  1014. if (history0->values == 0)
  1015. timeradd(&tv_start, &history_sec, &(history0->finish));
  1016. Ti = (double)(elapsed.tv_sec)
  1017. + ((double)(elapsed.tv_usec))/((double)1000000)
  1018. - ((double)ICARUS_READ_TIME(info->baud));
  1019. Xi = (double)hash_count;
  1020. history0->sumXiTi += Xi * Ti;
  1021. history0->sumXi += Xi;
  1022. history0->sumTi += Ti;
  1023. history0->sumXi2 += Xi * Xi;
  1024. history0->values++;
  1025. if (history0->hash_count_max < hash_count)
  1026. history0->hash_count_max = hash_count;
  1027. if (history0->hash_count_min > hash_count || history0->hash_count_min == 0)
  1028. history0->hash_count_min = hash_count;
  1029. if (history0->values >= info->min_data_count
  1030. && timercmp(&tv_start, &(history0->finish), >)) {
  1031. for (i = INFO_HISTORY; i > 0; i--)
  1032. memcpy(&(info->history[i]),
  1033. &(info->history[i-1]),
  1034. sizeof(struct ICARUS_HISTORY));
  1035. // Initialise history0 to zero for summary calculation
  1036. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  1037. // We just completed a history data set
  1038. // So now recalc read_time based on the whole history thus we will
  1039. // initially get more accurate until it completes INFO_HISTORY
  1040. // total data sets
  1041. count = 0;
  1042. for (i = 1 ; i <= INFO_HISTORY; i++) {
  1043. history = &(info->history[i]);
  1044. if (history->values >= MIN_DATA_COUNT) {
  1045. count++;
  1046. history0->sumXiTi += history->sumXiTi;
  1047. history0->sumXi += history->sumXi;
  1048. history0->sumTi += history->sumTi;
  1049. history0->sumXi2 += history->sumXi2;
  1050. history0->values += history->values;
  1051. if (history0->hash_count_max < history->hash_count_max)
  1052. history0->hash_count_max = history->hash_count_max;
  1053. if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0)
  1054. history0->hash_count_min = history->hash_count_min;
  1055. }
  1056. }
  1057. // All history data
  1058. Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi)
  1059. / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi);
  1060. W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values;
  1061. hash_count_range = history0->hash_count_max - history0->hash_count_min;
  1062. values = history0->values;
  1063. // Initialise history0 to zero for next data set
  1064. memset(history0, 0, sizeof(struct ICARUS_HISTORY));
  1065. fullnonce = W + Hs * (((double)0xffffffff) + 1);
  1066. read_time = SECTOMS(fullnonce) - ICARUS_READ_REDUCE;
  1067. if (info->read_time_limit > 0 && read_time > info->read_time_limit) {
  1068. read_time = info->read_time_limit;
  1069. limited = true;
  1070. } else
  1071. limited = false;
  1072. info->Hs = Hs;
  1073. info->read_time = read_time;
  1074. info->fullnonce = fullnonce;
  1075. info->count = count;
  1076. info->W = W;
  1077. info->values = values;
  1078. info->hash_count_range = hash_count_range;
  1079. if (info->min_data_count < MAX_MIN_DATA_COUNT)
  1080. info->min_data_count *= 2;
  1081. else if (info->timing_mode == MODE_SHORT)
  1082. info->do_icarus_timing = false;
  1083. applog(LOG_WARNING, "%s%d Re-estimate: Hs=%e W=%e read_time=%dms%s fullnonce=%.3fs",
  1084. icarus->drv->name, icarus->device_id, Hs, W, read_time,
  1085. limited ? " (limited)" : "", fullnonce);
  1086. }
  1087. info->history_count++;
  1088. cgtime(&tv_history_finish);
  1089. timersub(&tv_history_finish, &tv_history_start, &tv_history_finish);
  1090. timeradd(&tv_history_finish, &(info->history_time), &(info->history_time));
  1091. }
  1092. out:
  1093. free_work(work);
  1094. return hash_count;
  1095. }
  1096. static struct api_data *icarus_api_stats(struct cgpu_info *cgpu)
  1097. {
  1098. struct api_data *root = NULL;
  1099. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
  1100. // Warning, access to these is not locked - but we don't really
  1101. // care since hashing performance is way more important than
  1102. // locking access to displaying API debug 'stats'
  1103. // If locking becomes an issue for any of them, use copy_data=true also
  1104. root = api_add_int(root, "read_time", &(info->read_time), false);
  1105. root = api_add_int(root, "read_time_limit", &(info->read_time_limit), false);
  1106. root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
  1107. root = api_add_int(root, "count", &(info->count), false);
  1108. root = api_add_hs(root, "Hs", &(info->Hs), false);
  1109. root = api_add_double(root, "W", &(info->W), false);
  1110. root = api_add_uint(root, "total_values", &(info->values), false);
  1111. root = api_add_uint64(root, "range", &(info->hash_count_range), false);
  1112. root = api_add_uint64(root, "history_count", &(info->history_count), false);
  1113. root = api_add_timeval(root, "history_time", &(info->history_time), false);
  1114. root = api_add_uint(root, "min_data_count", &(info->min_data_count), false);
  1115. root = api_add_uint(root, "timing_values", &(info->history[0].values), false);
  1116. root = api_add_const(root, "timing_mode", timing_mode_str(info->timing_mode), false);
  1117. root = api_add_bool(root, "is_timing", &(info->do_icarus_timing), false);
  1118. root = api_add_int(root, "baud", &(info->baud), false);
  1119. root = api_add_int(root, "work_division", &(info->work_division), false);
  1120. root = api_add_int(root, "fpga_count", &(info->fpga_count), false);
  1121. return root;
  1122. }
  1123. static void icarus_statline_before(char *buf, size_t bufsiz, struct cgpu_info *cgpu)
  1124. {
  1125. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
  1126. if (info->ident == IDENT_CMR2 && info->cmr2_speed > 0)
  1127. tailsprintf(buf, bufsiz, "%5.1fMhz", (float)(info->cmr2_speed) * ICARUS_CMR2_SPEED_FACTOR);
  1128. else
  1129. tailsprintf(buf, bufsiz, " ");
  1130. tailsprintf(buf, bufsiz, " | ");
  1131. }
  1132. static void icarus_shutdown(__maybe_unused struct thr_info *thr)
  1133. {
  1134. // TODO: ?
  1135. }
  1136. static void icarus_identify(struct cgpu_info *cgpu)
  1137. {
  1138. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
  1139. if (info->ident == IDENT_CMR2)
  1140. info->flash_next_work = true;
  1141. }
  1142. static char *icarus_set(struct cgpu_info *cgpu, char *option, char *setting, char *replybuf)
  1143. {
  1144. struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
  1145. int val;
  1146. if (info->ident != IDENT_CMR2) {
  1147. strcpy(replybuf, "no set options available");
  1148. return replybuf;
  1149. }
  1150. if (strcasecmp(option, "help") == 0) {
  1151. sprintf(replybuf, "clock: range %d-%d",
  1152. ICARUS_CMR2_SPEED_MIN_INT, ICARUS_CMR2_SPEED_MAX_INT);
  1153. return replybuf;
  1154. }
  1155. if (strcasecmp(option, "clock") == 0) {
  1156. if (!setting || !*setting) {
  1157. sprintf(replybuf, "missing clock setting");
  1158. return replybuf;
  1159. }
  1160. val = atoi(setting);
  1161. if (val < ICARUS_CMR2_SPEED_MIN_INT || val > ICARUS_CMR2_SPEED_MAX_INT) {
  1162. sprintf(replybuf, "invalid clock: '%s' valid range %d-%d",
  1163. setting,
  1164. ICARUS_CMR2_SPEED_MIN_INT,
  1165. ICARUS_CMR2_SPEED_MAX_INT);
  1166. }
  1167. info->cmr2_speed = CMR2_INT_TO_SPEED(val);
  1168. info->speed_next_work = true;
  1169. return NULL;
  1170. }
  1171. sprintf(replybuf, "Unknown option: %s", option);
  1172. return replybuf;
  1173. }
  1174. struct device_drv icarus_drv = {
  1175. .drv_id = DRIVER_icarus,
  1176. .dname = "Icarus",
  1177. .name = "ICA",
  1178. .drv_detect = icarus_detect,
  1179. .hash_work = &hash_driver_work,
  1180. .get_api_stats = icarus_api_stats,
  1181. .get_statline_before = icarus_statline_before,
  1182. .set_device = icarus_set,
  1183. .identify_device = icarus_identify,
  1184. .thread_prepare = icarus_prepare,
  1185. .scanwork = icarus_scanwork,
  1186. .thread_shutdown = icarus_shutdown,
  1187. };