driver-icarus.c 41 KB

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