/ driver-icarus.c
driver-icarus.c
1 /* 2 * Copyright 2012-2013 Andrew Smith 3 * Copyright 2012 Xiangfu <xiangfu@openmobilefree.com> 4 * Copyright 2013-2015 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 /* 13 * Those code should be works fine with V2 and V3 bitstream of Icarus. 14 * Operation: 15 * No detection implement. 16 * Input: 64B = 32B midstate + 20B fill bytes + last 12 bytes of block head. 17 * Return: send back 32bits immediately when Icarus found a valid nonce. 18 * no query protocol implemented here, if no data send back in ~11.3 19 * seconds (full cover time on 32bit nonce range by 380MH/s speed) 20 * just send another work. 21 * Notice: 22 * 1. Icarus will start calculate when you push a work to them, even they 23 * are busy. 24 * 2. The 2 FPGAs on Icarus will distribute the job, one will calculate the 25 * 0 ~ 7FFFFFFF, another one will cover the 80000000 ~ FFFFFFFF. 26 * 3. It's possible for 2 FPGAs both find valid nonce in the meantime, the 2 27 * valid nonce will all be send back. 28 * 4. Icarus will stop work when: a valid nonce has been found or 32 bits 29 * nonce range is completely calculated. 30 */ 31 32 33 #include <float.h> 34 #include <limits.h> 35 #include <pthread.h> 36 #include <stdint.h> 37 #include <stdio.h> 38 #include <strings.h> 39 #include <sys/time.h> 40 #include <unistd.h> 41 #include <math.h> 42 43 #include "config.h" 44 45 #include "compat.h" 46 #include "miner.h" 47 #include "usbutils.h" 48 49 // The serial I/O speed - Linux uses a define 'B115200' in bits/termios.h 50 #define ICARUS_IO_SPEED 115200 51 52 #define ICARUS_BUF_SIZE 8 53 // The size of a successful nonce read 54 #define ANT_READ_SIZE 5 55 #define ICARUS_READ_SIZE 4 56 #define ROCK_READ_SIZE 8 57 58 // Ensure the sizes are correct for the Serial read 59 #if (ICARUS_READ_SIZE != 4) 60 #error ICARUS_READ_SIZE must be 4 61 #endif 62 #define ASSERT1(condition) __maybe_unused static char sizeof_uint32_t_must_be_4[(condition)?1:-1] 63 ASSERT1(sizeof(uint32_t) == 4); 64 65 // TODO: USB? Different calculation? - see usbstats to work it out e.g. 1/2 of normal send time 66 // or even use that number? 1/2 67 // #define ICARUS_READ_TIME(baud) ((double)ICARUS_READ_SIZE * (double)8.0 / (double)(baud)) 68 // maybe 1ms? 69 #define ICARUS_READ_TIME(baud) (0.001) 70 71 // USB ms timeout to wait - user specified timeouts are multiples of this 72 #define ICA_WAIT_TIMEOUT 100 73 #define ANT_WAIT_TIMEOUT 10 74 #define AU3_WAIT_TIMEOUT 1 75 #define GSC_WAIT_TIMEOUT 1 76 #define ICARUS_WAIT_TIMEOUT (info->compac ? GSC_WAIT_TIMEOUT : (info->u3 ? AU3_WAIT_TIMEOUT : (info->ant ? ANT_WAIT_TIMEOUT : ICA_WAIT_TIMEOUT))) 77 78 #define ICARUS_CMR2_TIMEOUT 1 79 80 // Defined in multiples of ICARUS_WAIT_TIMEOUT 81 // Must of course be greater than ICARUS_READ_COUNT_TIMING/ICARUS_WAIT_TIMEOUT 82 // There's no need to have this bigger, since the overhead/latency of extra work 83 // is pretty small once you get beyond a 10s nonce range time and 10s also 84 // means that nothing slower than 429MH/s can go idle so most icarus devices 85 // will always mine without idling 86 #define ICARUS_READ_TIME_LIMIT_MAX 100 87 88 // In timing mode: Default starting value until an estimate can be obtained 89 // 5000 ms allows for up to a ~840MH/s device 90 #define ICARUS_READ_COUNT_TIMING 5000 91 92 // Antminer USB is > 1GH/s so use a shorter limit 93 // 1000 ms allows for up to ~4GH/s device 94 #define ANTUSB_READ_COUNT_TIMING 1000 95 96 #define ANTU3_READ_COUNT_TIMING 100 97 98 #define COMPAC_READ_COUNT_TIMING 5000 99 100 #define ICARUS_READ_COUNT_MIN ICARUS_WAIT_TIMEOUT 101 #define SECTOMS(s) ((int)((s) * 1000)) 102 // How many ms below the expected completion time to abort work 103 // extra in case the last read is delayed 104 #define ICARUS_READ_REDUCE ((int)(ICARUS_WAIT_TIMEOUT * 1.5)) 105 106 // For a standard Icarus REV3 (to 5 places) 107 // Since this rounds up a the last digit - it is a slight overestimate 108 // Thus the hash rate will be a VERY slight underestimate 109 // (by a lot less than the displayed accuracy) 110 // Minor inaccuracy of these numbers doesn't affect the work done, 111 // only the displayed MH/s 112 #define ICARUS_REV3_HASH_TIME 0.0000000026316 113 #define LANCELOT_HASH_TIME 0.0000000025000 114 #define ASICMINERUSB_HASH_TIME 0.0000000029761 115 // TODO: What is it? 116 #define CAIRNSMORE1_HASH_TIME 0.0000000027000 117 // Per FPGA 118 #define CAIRNSMORE2_HASH_TIME 0.0000000066600 119 #define NANOSEC 1000000000.0 120 #define ANTMINERUSB_HASH_MHZ 0.000000125 121 #define ANTMINERUSB_HASH_TIME (ANTMINERUSB_HASH_MHZ / (double)(opt_anu_freq)) 122 #define ANTU3_HASH_MHZ 0.0000000032 123 #define ANTU3_HASH_TIME (ANTU3_HASH_MHZ / (double)(opt_au3_freq)) 124 #define COMPAC_HASH_MHZ 0.0000000128 125 #define COMPAC_HASH_TIME (COMPAC_HASH_MHZ / (double)(opt_compac_freq)) 126 127 #define CAIRNSMORE2_INTS 4 128 129 // Icarus Rev3 doesn't send a completion message when it finishes 130 // the full nonce range, so to avoid being idle we must abort the 131 // work (by starting a new work item) shortly before it finishes 132 // 133 // Thus we need to estimate 2 things: 134 // 1) How many hashes were done if the work was aborted 135 // 2) How high can the timeout be before the Icarus is idle, 136 // to minimise the number of work items started 137 // We set 2) to 'the calculated estimate' - ICARUS_READ_REDUCE 138 // to ensure the estimate ends before idle 139 // 140 // The simple calculation used is: 141 // Tn = Total time in seconds to calculate n hashes 142 // Hs = seconds per hash 143 // Xn = number of hashes 144 // W = code/usb overhead per work 145 // 146 // Rough but reasonable estimate: 147 // Tn = Hs * Xn + W (of the form y = mx + b) 148 // 149 // Thus: 150 // Line of best fit (using least squares) 151 // 152 // Hs = (n*Sum(XiTi)-Sum(Xi)*Sum(Ti))/(n*Sum(Xi^2)-Sum(Xi)^2) 153 // W = Sum(Ti)/n - (Hs*Sum(Xi))/n 154 // 155 // N.B. W is less when aborting work since we aren't waiting for the reply 156 // to be transferred back (ICARUS_READ_TIME) 157 // Calculating the hashes aborted at n seconds is thus just n/Hs 158 // (though this is still a slight overestimate due to code delays) 159 // 160 161 // Both below must be exceeded to complete a set of data 162 // Minimum how long after the first, the last data point must be 163 #define HISTORY_SEC 60 164 // Minimum how many points a single ICARUS_HISTORY should have 165 #define MIN_DATA_COUNT 5 166 // The value MIN_DATA_COUNT used is doubled each history until it exceeds: 167 #define MAX_MIN_DATA_COUNT 100 168 169 static struct timeval history_sec = { HISTORY_SEC, 0 }; 170 171 // Store the last INFO_HISTORY data sets 172 // [0] = current data, not yet ready to be included as an estimate 173 // Each new data set throws the last old set off the end thus 174 // keeping a ongoing average of recent data 175 #define INFO_HISTORY 10 176 177 struct ICARUS_HISTORY { 178 struct timeval finish; 179 double sumXiTi; 180 double sumXi; 181 double sumTi; 182 double sumXi2; 183 uint32_t values; 184 uint32_t hash_count_min; 185 uint32_t hash_count_max; 186 }; 187 188 enum timing_mode { MODE_DEFAULT, MODE_SHORT, MODE_LONG, MODE_VALUE }; 189 190 static const char *MODE_DEFAULT_STR = "default"; 191 static const char *MODE_SHORT_STR = "short"; 192 static const char *MODE_SHORT_STREQ = "short="; 193 static const char *MODE_LONG_STR = "long"; 194 static const char *MODE_LONG_STREQ = "long="; 195 static const char *MODE_VALUE_STR = "value"; 196 static const char *MODE_UNKNOWN_STR = "unknown"; 197 198 #define MAX_DEVICE_NUM 100 199 #define MAX_WORK_BUFFER_SIZE 2 200 #define MAX_CHIP_NUM 24 201 // Set it to 3, 5 or 9 202 #define NONCE_CORRECTION_TIMES 5 203 #define MAX_TRIES 4 204 #define RM_CMD_MASK 0x0F 205 #define RM_STATUS_MASK 0xF0 206 #define RM_CHIP_MASK 0x3F 207 #define RM_PRODUCT_MASK 0xC0 208 #define RM_PRODUCT_RBOX 0x00 209 #define RM_PRODUCT_T1 0x40 210 #define RM_PRODUCT_T2 0x80 211 #define RM_PRODUCT_TEST 0xC0 212 213 #if (NONCE_CORRECTION_TIMES == 5) 214 static int32_t rbox_corr_values[] = {0, 1, -1, -2, -4}; 215 #endif 216 #if (NONCE_CORRECTION_TIMES == 9) 217 static int32_t rbox_corr_values[] = {0, 1, -1, 2, -2, 3, -3, 4, -4}; 218 #endif 219 #if (NONCE_CORRECTION_TIMES == 3) 220 static int32_t rbox_corr_values[] = {0, 1, -1}; 221 #endif 222 223 #define ANT_QUEUE_NUM 36 224 225 typedef enum { 226 NONCE_DATA1_OFFSET = 0, 227 NONCE_DATA2_OFFSET, 228 NONCE_DATA3_OFFSET, 229 NONCE_DATA4_OFFSET, 230 NONCE_TASK_CMD_OFFSET, 231 NONCE_CHIP_NO_OFFSET, 232 NONCE_TASK_NO_OFFSET, 233 NONCE_COMMAND_OFFSET, 234 NONCE_MAX_OFFSET 235 } NONCE_OFFSET; 236 237 typedef enum { 238 NONCE_DATA_CMD = 0, 239 NONCE_TASK_COMPLETE_CMD, 240 NONCE_GET_TASK_CMD, 241 } NONCE_COMMAND; 242 243 typedef struct nonce_data { 244 int chip_no; 245 unsigned int task_no ; 246 unsigned char work_state; 247 int cmd_value; 248 } NONCE_DATA; 249 250 typedef enum { 251 ROCKMINER_RBOX = 0, 252 ROCKMINER_T1, 253 ROCKMINER_T2, 254 ROCKMINER_MAX 255 } ROCKMINER_PRODUCT_T; 256 257 typedef struct rockminer_chip_info { 258 unsigned char freq; 259 int error_cnt; 260 time_t last_received_task_complete_time; 261 } ROCKMINER_CHIP_INFO; 262 263 typedef struct rockminer_device_info { 264 unsigned char detect_chip_no; 265 unsigned char chip_max; 266 unsigned char product_id; 267 float min_frq; 268 float def_frq; 269 float max_frq; 270 ROCKMINER_CHIP_INFO chip[MAX_CHIP_NUM]; 271 time_t dev_detect_time; 272 } ROCKMINER_DEVICE_INFO; 273 274 struct ICARUS_INFO { 275 enum sub_ident ident; 276 int intinfo; 277 278 // time to calculate the golden_ob 279 uint64_t golden_hashes; 280 struct timeval golden_tv; 281 282 struct ICARUS_HISTORY history[INFO_HISTORY+1]; 283 uint32_t min_data_count; 284 285 int timeout; 286 287 // seconds per Hash 288 double Hs; 289 // ms til we abort 290 int read_time; 291 // ms limit for (short=/long=) read_time 292 int read_time_limit; 293 // How long without hashes is considered a failed device 294 int fail_time; 295 296 enum timing_mode timing_mode; 297 bool do_icarus_timing; 298 299 double fullnonce; 300 int count; 301 double W; 302 uint32_t values; 303 uint64_t hash_count_range; 304 305 // Determine the cost of history processing 306 // (which will only affect W) 307 uint64_t history_count; 308 struct timeval history_time; 309 310 // icarus-options 311 int baud; 312 int work_division; 313 int fpga_count; 314 uint32_t nonce_mask; 315 316 uint8_t cmr2_speed; 317 bool speed_next_work; 318 bool flash_next_work; 319 320 int nonce_size; 321 322 bool failing; 323 324 pthread_mutex_t lock; 325 326 ROCKMINER_DEVICE_INFO rmdev; 327 struct work *base_work; // For when we roll work 328 struct work *g_work[MAX_CHIP_NUM][MAX_WORK_BUFFER_SIZE]; 329 uint32_t last_nonce[MAX_CHIP_NUM][MAX_WORK_BUFFER_SIZE]; 330 char rock_init[64]; 331 uint64_t nonces_checked; 332 uint64_t nonces_correction_times; 333 uint64_t nonces_correction_tests; 334 uint64_t nonces_fail; 335 uint64_t nonces_correction[NONCE_CORRECTION_TIMES]; 336 337 struct work **antworks; 338 int nonces; 339 int workid; 340 bool ant; 341 bool u3; 342 bool compac; 343 float compac_ramp_freq; 344 float compac_target_freq; 345 uint16_t compac_ramp_idx; 346 }; 347 348 #define ICARUS_MIDSTATE_SIZE 32 349 #define ICARUS_UNUSED_SIZE 16 350 #define ICARUS_WORK_SIZE 12 351 352 #define ICARUS_WORK_DATA_OFFSET 64 353 354 #define ICARUS_CMR2_SPEED_FACTOR 2.5 355 #define ICARUS_CMR2_SPEED_MIN_INT 100 356 #define ICARUS_CMR2_SPEED_DEF_INT 180 357 #define ICARUS_CMR2_SPEED_MAX_INT 220 358 #define CMR2_INT_TO_SPEED(_speed) ((uint8_t)((float)_speed / ICARUS_CMR2_SPEED_FACTOR)) 359 #define ICARUS_CMR2_SPEED_MIN CMR2_INT_TO_SPEED(ICARUS_CMR2_SPEED_MIN_INT) 360 #define ICARUS_CMR2_SPEED_DEF CMR2_INT_TO_SPEED(ICARUS_CMR2_SPEED_DEF_INT) 361 #define ICARUS_CMR2_SPEED_MAX CMR2_INT_TO_SPEED(ICARUS_CMR2_SPEED_MAX_INT) 362 #define ICARUS_CMR2_SPEED_INC 1 363 #define ICARUS_CMR2_SPEED_DEC -1 364 #define ICARUS_CMR2_SPEED_FAIL -10 365 366 #define ICARUS_CMR2_PREFIX ((uint8_t)0xB7) 367 #define ICARUS_CMR2_CMD_SPEED ((uint8_t)0) 368 #define ICARUS_CMR2_CMD_FLASH ((uint8_t)1) 369 #define ICARUS_CMR2_DATA_FLASH_OFF ((uint8_t)0) 370 #define ICARUS_CMR2_DATA_FLASH_ON ((uint8_t)1) 371 #define ICARUS_CMR2_CHECK ((uint8_t)0x6D) 372 373 #define ANT_UNUSED_SIZE 15 374 375 struct ICARUS_WORK { 376 uint8_t midstate[ICARUS_MIDSTATE_SIZE]; 377 // These 4 bytes are for CMR2 bitstreams that handle MHz adjustment 378 uint8_t check; 379 uint8_t data; 380 uint8_t cmd; 381 uint8_t prefix; 382 uint8_t unused[ANT_UNUSED_SIZE]; 383 uint8_t id; // Used only by ANT, otherwise unused by other icarus 384 uint8_t work[ICARUS_WORK_SIZE]; 385 }; 386 387 #define ANT_U1_DEFFREQ 200 388 #define ANT_U3_DEFFREQ 225 389 #define ANT_U3_MAXFREQ 250 390 #define COMPAC_DEFFREQ 150 391 #define COMPAC_MAXFREQ 500 392 393 struct { 394 float freq; 395 uint16_t hex; 396 } u3freqtable[] = { 397 { 100, 0x0783 }, 398 { 125, 0x0983 }, 399 { 150, 0x0b83 }, 400 { 175, 0x0d83 }, 401 { 193.75, 0x0f03 }, 402 { 196.88, 0x1f07 }, 403 { 200, 0x0782 }, 404 { 206.25, 0x1006 }, 405 { 212.5, 0x1086 }, 406 { 218.75, 0x1106 }, 407 { 225, 0x0882 }, 408 { 237.5, 0x1286 }, 409 { 243.75, 0x1306 }, 410 { 250, 0x0982 }, 411 }; 412 struct { 413 float freq; 414 uint16_t hex; 415 } compacfreqtable[] = { 416 { 100, 0x0783 }, 417 { 106.25, 0x0803 }, 418 { 112.5, 0x0883 }, 419 { 118.75, 0x0903 }, 420 { 125, 0x0983 }, 421 { 131.25, 0x0a03 }, 422 { 137.5, 0x0a83 }, 423 { 143.75, 0x1687 }, 424 { 150, 0x0b83 }, 425 { 156.25, 0x0c03 }, 426 { 162.5, 0x0c83 }, 427 { 168.75, 0x1a87 }, 428 { 175, 0x0d83 }, 429 { 181.25, 0x0e83 }, 430 { 193.75, 0x0f03 }, 431 { 196.88, 0x1f07 }, 432 { 200, 0x0782 }, 433 { 206.25, 0x1006 }, 434 { 212.5, 0x1086 }, 435 { 218.75, 0x1106 }, 436 { 225, 0x0882 }, 437 { 231.25, 0x1206 }, 438 { 237.5, 0x1286 }, 439 { 243.75, 0x1306 }, 440 { 250, 0x0982 }, 441 { 256.25, 0x1406 }, 442 { 262.5, 0x0a02 }, 443 { 268.75, 0x1506 }, 444 { 275, 0x0a82 }, 445 { 281.25, 0x1606 }, 446 { 287.5, 0x0b02 }, 447 { 293.75, 0x1706 }, 448 { 300, 0x0b82 }, 449 { 306.25, 0x1806 }, 450 { 312.5, 0x0c02 }, 451 { 318.75, 0x1906 }, 452 { 325, 0x0c82 }, 453 { 331.25, 0x1a06 }, 454 { 337.5, 0x0d02 }, 455 { 343.75, 0x1b06 }, 456 { 350, 0x0d82 }, 457 { 356.25, 0x1c06 }, 458 { 362.5, 0x0e02 }, 459 { 368.75, 0x1d06 }, 460 { 375, 0x0e82 }, 461 { 381.25, 0x1e06 }, 462 { 387.5, 0x0f02 }, 463 { 393.75, 0x1f06 }, 464 { 400, 0x0f82 }, 465 { 412.5, 0x1006 }, 466 { 425, 0x0801 }, 467 { 437.5, 0x1105 }, 468 { 450, 0x0881 }, 469 { 462.5, 0x1205 }, 470 { 475, 0x0901 }, 471 { 487.5, 0x1305 }, 472 { 500, 0x0981 }, 473 }; 474 475 #define END_CONDITION 0x0000ffff 476 477 // Looking for options in --icarus-timing and --icarus-options: 478 // 479 // Code increments this each time we start to look at a device 480 // However, this means that if other devices are checked by 481 // the Icarus code (e.g. Avalon only as at 20130517) 482 // they will count in the option offset 483 // 484 // This, however, is deterministic so that's OK 485 // 486 // If we were to increment after successfully finding an Icarus 487 // that would be random since an Icarus may fail and thus we'd 488 // not be able to predict the option order 489 // 490 // Devices are checked in the order libusb finds them which is ? 491 // 492 static int option_offset = -1; 493 494 /* 495 #define ICA_BUFSIZ (0x200) 496 497 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) 498 { 499 int err; 500 501 err = usb_transfer_read(icarus, request_type, bRequest, wValue, wIndex, buf, bufsiz, amount, cmd); 502 503 applog(LOG_DEBUG, "%s: cgid %d %s got err %d", 504 icarus->drv->name, icarus->cgminer_id, 505 usb_cmdname(cmd), err); 506 } 507 */ 508 509 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) 510 { 511 int err; 512 513 err = usb_transfer_data(icarus, request_type, bRequest, wValue, wIndex, data, siz, cmd); 514 515 applog(LOG_DEBUG, "%s: cgid %d %s got err %d", 516 icarus->drv->name, icarus->cgminer_id, 517 usb_cmdname(cmd), err); 518 } 519 520 #define transfer(icarus, request_type, bRequest, wValue, wIndex, cmd) \ 521 _transfer(icarus, request_type, bRequest, wValue, wIndex, NULL, 0, cmd) 522 523 static void icarus_initialise(struct cgpu_info *icarus, int baud) 524 { 525 struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data); 526 uint16_t wValue, wIndex; 527 enum sub_ident ident; 528 int interface; 529 530 if (icarus->usbinfo.nodev) 531 return; 532 533 interface = _usb_interface(icarus, info->intinfo); 534 ident = usb_ident(icarus); 535 536 switch (ident) { 537 case IDENT_BLT: 538 case IDENT_LLT: 539 case IDENT_CMR1: 540 case IDENT_CMR2: 541 // Reset 542 transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_RESET, 543 interface, C_RESET); 544 545 if (icarus->usbinfo.nodev) 546 return; 547 548 // Latency 549 _usb_ftdi_set_latency(icarus, info->intinfo); 550 551 if (icarus->usbinfo.nodev) 552 return; 553 554 // Set data control 555 transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_DATA, FTDI_VALUE_DATA_BLT, 556 interface, C_SETDATA); 557 558 if (icarus->usbinfo.nodev) 559 return; 560 561 // default to BLT/LLT 115200 562 wValue = FTDI_VALUE_BAUD_BLT; 563 wIndex = FTDI_INDEX_BAUD_BLT; 564 565 if (ident == IDENT_CMR1 || ident == IDENT_CMR2) { 566 switch (baud) { 567 case 115200: 568 wValue = FTDI_VALUE_BAUD_CMR_115; 569 wIndex = FTDI_INDEX_BAUD_CMR_115; 570 break; 571 case 57600: 572 wValue = FTDI_VALUE_BAUD_CMR_57; 573 wIndex = FTDI_INDEX_BAUD_CMR_57; 574 break; 575 default: 576 quit(1, "icarus_intialise() invalid baud (%d) for Cairnsmore1", baud); 577 break; 578 } 579 } 580 581 // Set the baud 582 transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_BAUD, wValue, 583 (wIndex & 0xff00) | interface, C_SETBAUD); 584 585 if (icarus->usbinfo.nodev) 586 return; 587 588 // Set Modem Control 589 transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_MODEM, FTDI_VALUE_MODEM, 590 interface, C_SETMODEM); 591 592 if (icarus->usbinfo.nodev) 593 return; 594 595 // Set Flow Control 596 transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_FLOW, FTDI_VALUE_FLOW, 597 interface, C_SETFLOW); 598 599 if (icarus->usbinfo.nodev) 600 return; 601 602 // Clear any sent data 603 transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_TX, 604 interface, C_PURGETX); 605 606 if (icarus->usbinfo.nodev) 607 return; 608 609 // Clear any received data 610 transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET, FTDI_VALUE_PURGE_RX, 611 interface, C_PURGERX); 612 break; 613 case IDENT_ICA: 614 // Set Data Control 615 transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_CTRL, PL2303_VALUE_CTRL, 616 interface, C_SETDATA); 617 618 if (icarus->usbinfo.nodev) 619 return; 620 621 // Set Line Control 622 uint32_t ica_data[2] = { PL2303_VALUE_LINE0, PL2303_VALUE_LINE1 }; 623 _transfer(icarus, PL2303_CTRL_OUT, PL2303_REQUEST_LINE, PL2303_VALUE_LINE, 624 interface, &ica_data[0], PL2303_VALUE_LINE_SIZE, C_SETLINE); 625 626 if (icarus->usbinfo.nodev) 627 return; 628 629 // Vendor 630 transfer(icarus, PL2303_VENDOR_OUT, PL2303_REQUEST_VENDOR, PL2303_VALUE_VENDOR, 631 interface, C_VENDOR); 632 break; 633 case IDENT_AMU: 634 case IDENT_ANU: 635 case IDENT_AU3: 636 case IDENT_BSC: 637 case IDENT_GSC: 638 case IDENT_LIN: 639 // Enable the UART 640 transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_IFC_ENABLE, 641 CP210X_VALUE_UART_ENABLE, 642 interface, C_ENABLE_UART); 643 644 if (icarus->usbinfo.nodev) 645 return; 646 647 // Set data control 648 transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_DATA, CP210X_VALUE_DATA, 649 interface, C_SETDATA); 650 651 if (icarus->usbinfo.nodev) 652 return; 653 654 // Set the baud 655 uint32_t data = CP210X_DATA_BAUD; 656 _transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_BAUD, 0, 657 interface, &data, sizeof(data), C_SETBAUD); 658 break; 659 case IDENT_AVA: 660 break; 661 default: 662 quit(1, "icarus_intialise() called with invalid %s cgid %i ident=%d", 663 icarus->drv->name, icarus->cgminer_id, ident); 664 } 665 } 666 667 static void rev(unsigned char *s, size_t l) 668 { 669 size_t i, j; 670 unsigned char t; 671 672 for (i = 0, j = l - 1; i < j; i++, j--) { 673 t = s[i]; 674 s[i] = s[j]; 675 s[j] = t; 676 } 677 } 678 679 #define ICA_NONCE_ERROR -1 680 #define ICA_NONCE_OK 0 681 #define ICA_NONCE_RESTART 1 682 #define ICA_NONCE_TIMEOUT 2 683 684 static int icarus_get_nonce(struct cgpu_info *icarus, unsigned char *buf, struct timeval *tv_start, 685 struct timeval *tv_finish, struct thr_info *thr, int read_time) 686 { 687 struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data); 688 int err, amt, rc; 689 690 if (icarus->usbinfo.nodev) 691 return ICA_NONCE_ERROR; 692 693 cgtime(tv_start); 694 err = usb_read_ii_timeout_cancellable(icarus, info->intinfo, (char *)buf, 695 info->nonce_size, &amt, read_time, 696 C_GETRESULTS); 697 cgtime(tv_finish); 698 699 if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) { 700 applog(LOG_ERR, "%s %i: Comms error (rerr=%d amt=%d)", icarus->drv->name, 701 icarus->device_id, err, amt); 702 dev_error(icarus, REASON_DEV_COMMS_ERROR); 703 return ICA_NONCE_ERROR; 704 } 705 706 if (amt >= info->nonce_size) 707 return ICA_NONCE_OK; 708 709 rc = SECTOMS(tdiff(tv_finish, tv_start)); 710 if (thr && thr->work_restart) { 711 applog(LOG_DEBUG, "Icarus Read: Work restart at %d ms", rc); 712 return ICA_NONCE_RESTART; 713 } 714 715 if (amt > 0) 716 applog(LOG_DEBUG, "Icarus Read: Timeout reading for %d ms", rc); 717 else 718 applog(LOG_DEBUG, "Icarus Read: No data for %d ms", rc); 719 return ICA_NONCE_TIMEOUT; 720 } 721 722 723 static const char *timing_mode_str(enum timing_mode timing_mode) 724 { 725 switch(timing_mode) { 726 case MODE_DEFAULT: 727 return MODE_DEFAULT_STR; 728 case MODE_SHORT: 729 return MODE_SHORT_STR; 730 case MODE_LONG: 731 return MODE_LONG_STR; 732 case MODE_VALUE: 733 return MODE_VALUE_STR; 734 default: 735 return MODE_UNKNOWN_STR; 736 } 737 } 738 739 static void set_timing_mode(int this_option_offset, struct cgpu_info *icarus) 740 { 741 struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data); 742 int read_count_timing = 0; 743 enum sub_ident ident; 744 double Hs, fail_time; 745 char buf[BUFSIZ+1]; 746 char *ptr, *comma, *eq; 747 size_t max; 748 int i; 749 750 if (opt_icarus_timing == NULL) 751 buf[0] = '\0'; 752 else { 753 ptr = opt_icarus_timing; 754 for (i = 0; i < this_option_offset; i++) { 755 comma = strchr(ptr, ','); 756 if (comma == NULL) 757 break; 758 ptr = comma + 1; 759 } 760 761 comma = strchr(ptr, ','); 762 if (comma == NULL) 763 max = strlen(ptr); 764 else 765 max = comma - ptr; 766 767 if (max > BUFSIZ) 768 max = BUFSIZ; 769 strncpy(buf, ptr, max); 770 buf[max] = '\0'; 771 } 772 773 ident = usb_ident(icarus); 774 switch (ident) { 775 case IDENT_ICA: 776 case IDENT_AVA: 777 info->Hs = ICARUS_REV3_HASH_TIME; 778 read_count_timing = ICARUS_READ_COUNT_TIMING; 779 break; 780 case IDENT_BLT: 781 case IDENT_LLT: 782 info->Hs = LANCELOT_HASH_TIME; 783 read_count_timing = ICARUS_READ_COUNT_TIMING; 784 break; 785 case IDENT_AMU: 786 info->Hs = ASICMINERUSB_HASH_TIME; 787 read_count_timing = ICARUS_READ_COUNT_TIMING; 788 break; 789 case IDENT_CMR1: 790 info->Hs = CAIRNSMORE1_HASH_TIME; 791 read_count_timing = ICARUS_READ_COUNT_TIMING; 792 break; 793 case IDENT_CMR2: 794 info->Hs = CAIRNSMORE2_HASH_TIME; 795 read_count_timing = ICARUS_READ_COUNT_TIMING; 796 break; 797 case IDENT_ANU: 798 info->Hs = ANTMINERUSB_HASH_TIME; 799 read_count_timing = ANTUSB_READ_COUNT_TIMING; 800 break; 801 case IDENT_AU3: 802 info->Hs = ANTU3_HASH_TIME; 803 read_count_timing = ANTU3_READ_COUNT_TIMING; 804 break; 805 case IDENT_BSC: 806 case IDENT_GSC: 807 info->Hs = COMPAC_HASH_TIME; 808 read_count_timing = COMPAC_READ_COUNT_TIMING; 809 break; 810 default: 811 quit(1, "Icarus get_options() called with invalid %s ident=%d", 812 icarus->drv->name, ident); 813 } 814 815 info->read_time = 0; 816 info->read_time_limit = 0; // 0 = no limit 817 818 if (strcasecmp(buf, MODE_SHORT_STR) == 0) { 819 // short 820 info->read_time = read_count_timing; 821 822 info->timing_mode = MODE_SHORT; 823 info->do_icarus_timing = true; 824 } else if (strncasecmp(buf, MODE_SHORT_STREQ, strlen(MODE_SHORT_STREQ)) == 0) { 825 // short=limit 826 info->read_time = read_count_timing; 827 828 info->timing_mode = MODE_SHORT; 829 info->do_icarus_timing = true; 830 831 info->read_time_limit = atoi(&buf[strlen(MODE_SHORT_STREQ)]); 832 if (info->read_time_limit < 0) 833 info->read_time_limit = 0; 834 if (info->read_time_limit > ICARUS_READ_TIME_LIMIT_MAX) 835 info->read_time_limit = ICARUS_READ_TIME_LIMIT_MAX; 836 } else if (strcasecmp(buf, MODE_LONG_STR) == 0) { 837 // long 838 info->read_time = read_count_timing; 839 840 info->timing_mode = MODE_LONG; 841 info->do_icarus_timing = true; 842 } else if (strncasecmp(buf, MODE_LONG_STREQ, strlen(MODE_LONG_STREQ)) == 0) { 843 // long=limit 844 info->read_time = read_count_timing; 845 846 info->timing_mode = MODE_LONG; 847 info->do_icarus_timing = true; 848 849 info->read_time_limit = atoi(&buf[strlen(MODE_LONG_STREQ)]); 850 if (info->read_time_limit < 0) 851 info->read_time_limit = 0; 852 if (info->read_time_limit > ICARUS_READ_TIME_LIMIT_MAX) 853 info->read_time_limit = ICARUS_READ_TIME_LIMIT_MAX; 854 } else if ((Hs = atof(buf)) != 0) { 855 // ns[=read_time] 856 info->Hs = Hs / NANOSEC; 857 info->fullnonce = info->Hs * (((double)0xffffffff) + 1); 858 859 if ((eq = strchr(buf, '=')) != NULL) 860 info->read_time = atoi(eq+1) * ICARUS_WAIT_TIMEOUT; 861 862 if (info->read_time < ICARUS_READ_COUNT_MIN) 863 info->read_time = SECTOMS(info->fullnonce) - ICARUS_READ_REDUCE; 864 865 if (unlikely(info->read_time < ICARUS_READ_COUNT_MIN)) 866 info->read_time = ICARUS_READ_COUNT_MIN; 867 868 info->timing_mode = MODE_VALUE; 869 info->do_icarus_timing = false; 870 } else { 871 // Anything else in buf just uses DEFAULT mode 872 873 info->fullnonce = info->Hs * (((double)0xffffffff) + 1); 874 875 if ((eq = strchr(buf, '=')) != NULL) 876 info->read_time = atoi(eq+1) * ICARUS_WAIT_TIMEOUT; 877 878 if (info->read_time < ICARUS_READ_COUNT_MIN) 879 info->read_time = SECTOMS(info->fullnonce) - ICARUS_READ_REDUCE; 880 881 if (unlikely(info->read_time < ICARUS_READ_COUNT_MIN)) 882 info->read_time = ICARUS_READ_COUNT_MIN; 883 884 info->timing_mode = MODE_DEFAULT; 885 info->do_icarus_timing = false; 886 } 887 888 info->min_data_count = MIN_DATA_COUNT; 889 890 // All values are in multiples of ICARUS_WAIT_TIMEOUT 891 info->read_time_limit *= ICARUS_WAIT_TIMEOUT; 892 893 applog(LOG_DEBUG, "%s: cgid %d Init: mode=%s read_time=%dms limit=%dms Hs=%e", 894 icarus->drv->name, icarus->cgminer_id, 895 timing_mode_str(info->timing_mode), 896 info->read_time, info->read_time_limit, info->Hs); 897 898 /* Set the time to detect a dead device to 30 full nonce ranges. */ 899 fail_time = info->Hs * 0xffffffffull * 30.0; 900 /* Integer accuracy is definitely enough. */ 901 info->fail_time = fail_time + 1; 902 } 903 904 static uint32_t mask(int work_division) 905 { 906 uint32_t nonce_mask = 0x7fffffff; 907 908 // yes we can calculate these, but this way it's easy to see what they are 909 switch (work_division) { 910 case 1: 911 nonce_mask = 0xffffffff; 912 break; 913 case 2: 914 nonce_mask = 0x7fffffff; 915 break; 916 case 4: 917 nonce_mask = 0x3fffffff; 918 break; 919 case 8: 920 nonce_mask = 0x1fffffff; 921 break; 922 default: 923 quit(1, "Invalid2 icarus-options for work_division (%d) must be 1, 2, 4 or 8", work_division); 924 } 925 926 return nonce_mask; 927 } 928 929 static void get_options(int this_option_offset, struct cgpu_info *icarus, int *baud, int *work_division, int *fpga_count) 930 { 931 char buf[BUFSIZ+1]; 932 char *ptr, *comma, *colon, *colon2; 933 enum sub_ident ident; 934 size_t max; 935 int i, tmp; 936 937 if (opt_icarus_options == NULL) 938 buf[0] = '\0'; 939 else { 940 ptr = opt_icarus_options; 941 for (i = 0; i < this_option_offset; i++) { 942 comma = strchr(ptr, ','); 943 if (comma == NULL) 944 break; 945 ptr = comma + 1; 946 } 947 948 comma = strchr(ptr, ','); 949 if (comma == NULL) 950 max = strlen(ptr); 951 else 952 max = comma - ptr; 953 954 if (max > BUFSIZ) 955 max = BUFSIZ; 956 strncpy(buf, ptr, max); 957 buf[max] = '\0'; 958 } 959 960 ident = usb_ident(icarus); 961 switch (ident) { 962 case IDENT_ICA: 963 case IDENT_BLT: 964 case IDENT_LLT: 965 case IDENT_AVA: 966 *baud = ICARUS_IO_SPEED; 967 *work_division = 2; 968 *fpga_count = 2; 969 break; 970 case IDENT_AMU: 971 case IDENT_ANU: 972 case IDENT_AU3: 973 case IDENT_BSC: 974 case IDENT_GSC: 975 *baud = ICARUS_IO_SPEED; 976 *work_division = 1; 977 *fpga_count = 1; 978 break; 979 case IDENT_CMR1: 980 *baud = ICARUS_IO_SPEED; 981 *work_division = 2; 982 *fpga_count = 2; 983 break; 984 case IDENT_CMR2: 985 *baud = ICARUS_IO_SPEED; 986 *work_division = 1; 987 *fpga_count = 1; 988 break; 989 default: 990 quit(1, "Icarus get_options() called with invalid %s ident=%d", 991 icarus->drv->name, ident); 992 } 993 994 if (*buf) { 995 colon = strchr(buf, ':'); 996 if (colon) 997 *(colon++) = '\0'; 998 999 if (*buf) { 1000 tmp = atoi(buf); 1001 switch (tmp) { 1002 case 115200: 1003 *baud = 115200; 1004 break; 1005 case 57600: 1006 *baud = 57600; 1007 break; 1008 default: 1009 quit(1, "Invalid icarus-options for baud (%s) must be 115200 or 57600", buf); 1010 } 1011 } 1012 1013 if (colon && *colon) { 1014 colon2 = strchr(colon, ':'); 1015 if (colon2) 1016 *(colon2++) = '\0'; 1017 1018 if (*colon) { 1019 tmp = atoi(colon); 1020 if (tmp == 1 || tmp == 2 || tmp == 4 || tmp == 8) { 1021 *work_division = tmp; 1022 *fpga_count = tmp; // default to the same 1023 } else { 1024 quit(1, "Invalid icarus-options for work_division (%s) must be 1, 2, 4 or 8", colon); 1025 } 1026 } 1027 1028 if (colon2 && *colon2) { 1029 tmp = atoi(colon2); 1030 if (tmp > 0 && tmp <= *work_division) 1031 *fpga_count = tmp; 1032 else { 1033 quit(1, "Invalid icarus-options for fpga_count (%s) must be >0 and <=work_division (%d)", colon2, *work_division); 1034 } 1035 } 1036 } 1037 } 1038 } 1039 1040 unsigned char crc5(unsigned char *ptr, unsigned char len) 1041 { 1042 unsigned char i, j, k; 1043 unsigned char crc = 0x1f; 1044 1045 unsigned char crcin[5] = {1, 1, 1, 1, 1}; 1046 unsigned char crcout[5] = {1, 1, 1, 1, 1}; 1047 unsigned char din = 0; 1048 1049 j = 0x80; 1050 k = 0; 1051 for (i = 0; i < len; i++) { 1052 if (*ptr & j) 1053 din = 1; 1054 else 1055 din = 0; 1056 crcout[0] = crcin[4] ^ din; 1057 crcout[1] = crcin[0]; 1058 crcout[2] = crcin[1] ^ crcin[4] ^ din; 1059 crcout[3] = crcin[2]; 1060 crcout[4] = crcin[3]; 1061 1062 j = j >> 1; 1063 k++; 1064 if (k == 8) { 1065 j = 0x80; 1066 k = 0; 1067 ptr++; 1068 } 1069 memcpy(crcin, crcout, 5); 1070 } 1071 crc = 0; 1072 if (crcin[4]) 1073 crc |= 0x10; 1074 if (crcin[3]) 1075 crc |= 0x08; 1076 if (crcin[2]) 1077 crc |= 0x04; 1078 if (crcin[1]) 1079 crc |= 0x02; 1080 if (crcin[0]) 1081 crc |= 0x01; 1082 return crc; 1083 } 1084 1085 static uint16_t anu_find_freqhex(void) 1086 { 1087 float fout, best_fout = opt_anu_freq; 1088 int od, nf, nr, no, n, m, bs; 1089 uint16_t anu_freq_hex = 0; 1090 float best_diff = 1000; 1091 1092 if (!best_fout) 1093 best_fout = ANT_U1_DEFFREQ; 1094 1095 for (od = 0; od < 4; od++) { 1096 no = 1 << od; 1097 for (n = 0; n < 16; n++) { 1098 nr = n + 1; 1099 for (m = 0; m < 64; m++) { 1100 nf = m + 1; 1101 fout = 25 * (float)nf /((float)(nr) * (float)(no)); 1102 if (fabsf(fout - opt_anu_freq) > best_diff) 1103 continue; 1104 if (500 <= (fout * no) && (fout * no) <= 1000) 1105 bs = 1; 1106 else 1107 bs = 0; 1108 best_diff = fabsf(fout - opt_anu_freq); 1109 best_fout = fout; 1110 anu_freq_hex = (bs << 14) | (m << 7) | (n << 2) | od; 1111 if (fout == opt_anu_freq) { 1112 applog(LOG_DEBUG, "ANU found exact frequency %.1f with hex %04x", 1113 opt_anu_freq, anu_freq_hex); 1114 goto out; 1115 } 1116 } 1117 } 1118 } 1119 applog(LOG_NOTICE, "ANU found nearest frequency %.1f with hex %04x", best_fout, 1120 anu_freq_hex); 1121 out: 1122 return anu_freq_hex; 1123 } 1124 1125 static uint16_t anu3_find_freqhex(void) 1126 { 1127 int i = 0, freq = opt_au3_freq, u3freq; 1128 uint16_t anu_freq_hex = 0x0882; 1129 1130 if (!freq) 1131 freq = ANT_U3_DEFFREQ; 1132 1133 do { 1134 u3freq = u3freqtable[i].freq; 1135 if (u3freq <= freq) 1136 anu_freq_hex = u3freqtable[i].hex; 1137 i++; 1138 } while (u3freq < ANT_U3_MAXFREQ); 1139 1140 return anu_freq_hex; 1141 } 1142 1143 static bool set_anu_freq(struct cgpu_info *icarus, struct ICARUS_INFO *info, uint16_t anu_freq_hex) 1144 { 1145 unsigned char cmd_buf[4], rdreg_buf[4]; 1146 int amount, err; 1147 char buf[512]; 1148 1149 if (!anu_freq_hex) 1150 anu_freq_hex = anu_find_freqhex(); 1151 memset(cmd_buf, 0, 4); 1152 memset(rdreg_buf, 0, 4); 1153 cmd_buf[0] = 2 | 0x80; 1154 cmd_buf[1] = (anu_freq_hex & 0xff00u) >> 8; 1155 cmd_buf[2] = (anu_freq_hex & 0x00ffu); 1156 cmd_buf[3] = crc5(cmd_buf, 27); 1157 1158 rdreg_buf[0] = 4 | 0x80; 1159 rdreg_buf[1] = 0; //16-23 1160 rdreg_buf[2] = 0x04; //8-15 1161 rdreg_buf[3] = crc5(rdreg_buf, 27); 1162 1163 applog(LOG_DEBUG, "%s %i: Send frequency %02x%02x%02x%02x", icarus->drv->name, icarus->device_id, 1164 cmd_buf[0], cmd_buf[1], cmd_buf[2], cmd_buf[3]); 1165 err = usb_write_ii(icarus, info->intinfo, (char *)cmd_buf, 4, &amount, C_ANU_SEND_CMD); 1166 if (err != LIBUSB_SUCCESS || amount != 4) { 1167 applog(LOG_ERR, "%s %i: Write freq Comms error (werr=%d amount=%d)", 1168 icarus->drv->name, icarus->device_id, err, amount); 1169 return false; 1170 } 1171 err = usb_read_ii_timeout(icarus, info->intinfo, buf, 512, &amount, 100, C_GETRESULTS); 1172 if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) { 1173 applog(LOG_ERR, "%s %i: Read freq Comms error (rerr=%d amount=%d)", 1174 icarus->drv->name, icarus->device_id, err, amount); 1175 return false; 1176 } 1177 1178 applog(LOG_DEBUG, "%s %i: Send freq getstatus %02x%02x%02x%02x", icarus->drv->name, icarus->device_id, 1179 rdreg_buf[0], rdreg_buf[1], rdreg_buf[2], rdreg_buf[3]); 1180 err = usb_write_ii(icarus, info->intinfo, (char *)cmd_buf, 4, &amount, C_ANU_SEND_RDREG); 1181 if (err != LIBUSB_SUCCESS || amount != 4) { 1182 applog(LOG_ERR, "%s %i: Write freq Comms error (werr=%d amount=%d)", 1183 icarus->drv->name, icarus->device_id, err, amount); 1184 return false; 1185 } 1186 err = usb_read_ii_timeout(icarus, info->intinfo, buf, 512, &amount, 100, C_GETRESULTS); 1187 if (err < 0 && err != LIBUSB_ERROR_TIMEOUT) { 1188 applog(LOG_ERR, "%s %i: Read freq Comms error (rerr=%d amount=%d)", 1189 icarus->drv->name, icarus->device_id, err, amount); 1190 return false; 1191 } 1192 1193 return true; 1194 } 1195 1196 static void set_anu_volt(struct cgpu_info *icarus) 1197 { 1198 unsigned char voltage_data[2], cmd_buf[4]; 1199 char volt_buf[8]; 1200 int err, amount; 1201 1202 /* Allow a zero setting to imply not to try and set voltage */ 1203 if (!opt_au3_volt) 1204 return; 1205 if (opt_au3_volt < 725 || opt_au3_volt > 850) { 1206 applog(LOG_WARNING, "Invalid ANU voltage %d specified, must be 725-850", opt_au3_volt); 1207 return; 1208 } 1209 sprintf(volt_buf, "%04d", opt_au3_volt); 1210 hex2bin(voltage_data, volt_buf, 2); 1211 cmd_buf[0] = 0xaa; 1212 cmd_buf[1] = voltage_data[0]; 1213 cmd_buf[1] &=0x0f; 1214 cmd_buf[1] |=0xb0; 1215 cmd_buf[2] = voltage_data[1]; 1216 cmd_buf[3] = 0x00; //0-7 1217 cmd_buf[3] = crc5(cmd_buf, 4*8 - 5); 1218 cmd_buf[3] |= 0xc0; 1219 applog(LOG_INFO, "Send ANU voltage %02x%02x%02x%02x", cmd_buf[0], cmd_buf[1], cmd_buf[2], cmd_buf[3]); 1220 cgsleep_ms(500); 1221 err = usb_write(icarus, (char * )cmd_buf, 4, &amount, C_ANU_SEND_VOLT); 1222 if (err != LIBUSB_SUCCESS || amount != 4) 1223 applog(LOG_ERR, "Write voltage Comms error (werr=%d amount=%d)", err, amount); 1224 } 1225 1226 static void rock_init_last_received_task_complete_time(struct ICARUS_INFO *info) 1227 { 1228 int i; 1229 1230 if (opt_rock_freq < info->rmdev.min_frq || 1231 opt_rock_freq > info->rmdev.max_frq) 1232 opt_rock_freq = info->rmdev.def_frq; 1233 1234 for (i = 0; i < MAX_CHIP_NUM; ++i) { 1235 info->rmdev.chip[i].last_received_task_complete_time = time(NULL); 1236 info->rmdev.chip[i].freq = opt_rock_freq/10 - 1; 1237 info->rmdev.chip[i].error_cnt = 0; 1238 } 1239 1240 info->rmdev.dev_detect_time = time(NULL); 1241 } 1242 1243 1244 static void icarus_clear(struct cgpu_info *icarus, struct ICARUS_INFO *info) 1245 { 1246 char buf[512]; 1247 int amt; 1248 1249 do { 1250 usb_read_ii_timeout(icarus, info->intinfo, buf, 512, &amt, 100, C_GETRESULTS); 1251 } while (amt > 0); 1252 } 1253 1254 static struct cgpu_info *icarus_detect_one(struct libusb_device *dev, struct usb_find_devices *found) 1255 { 1256 int this_option_offset = ++option_offset; 1257 struct ICARUS_INFO *info; 1258 struct timeval tv_start, tv_finish; 1259 1260 // Block 171874 nonce = (0xa2870100) = 0x000187a2 1261 // N.B. golden_ob MUST take less time to calculate 1262 // than the timeout set in icarus_open() 1263 // This one takes ~0.53ms on Rev3 Icarus 1264 const char golden_ob[] = 1265 "4679ba4ec99876bf4bfe086082b40025" 1266 "4df6c356451471139a3afa71e48f544a" 1267 "00000000000000000000000000000000" 1268 "0000000087320b1a1426674f2fa722ce"; 1269 1270 const char golden_nonce[] = "000187a2"; 1271 const uint32_t golden_nonce_val = 0x000187a2; 1272 unsigned char nonce_bin[ICARUS_BUF_SIZE]; 1273 struct ICARUS_WORK workdata; 1274 char *nonce_hex; 1275 int baud, uninitialised_var(work_division), uninitialised_var(fpga_count); 1276 bool anu_freqset = false; 1277 struct cgpu_info *icarus; 1278 int ret, err, amount, tries, i; 1279 bool ok; 1280 bool cmr2_ok[CAIRNSMORE2_INTS]; 1281 int cmr2_count; 1282 1283 if ((sizeof(workdata) << 1) != (sizeof(golden_ob) - 1)) 1284 quithere(1, "Data and golden_ob sizes don't match"); 1285 1286 icarus = usb_alloc_cgpu(&icarus_drv, 1); 1287 1288 if (!usb_init(icarus, dev, found)) 1289 goto shin; 1290 1291 get_options(this_option_offset, icarus, &baud, &work_division, &fpga_count); 1292 1293 hex2bin((void *)(&workdata), golden_ob, sizeof(workdata)); 1294 1295 info = cgcalloc(1, sizeof(struct ICARUS_INFO)); 1296 icarus->device_data = (void *)info; 1297 1298 info->ident = usb_ident(icarus); 1299 switch (info->ident) { 1300 case IDENT_ICA: 1301 case IDENT_AVA: 1302 case IDENT_BLT: 1303 case IDENT_LLT: 1304 case IDENT_AMU: 1305 case IDENT_CMR1: 1306 info->timeout = ICARUS_WAIT_TIMEOUT; 1307 break; 1308 case IDENT_ANU: 1309 info->timeout = ANT_WAIT_TIMEOUT; 1310 break; 1311 case IDENT_AU3: 1312 info->timeout = AU3_WAIT_TIMEOUT; 1313 break; 1314 case IDENT_CMR2: 1315 if (found->intinfo_count != CAIRNSMORE2_INTS) { 1316 quithere(1, "CMR2 Interface count (%d) isn't expected: %d", 1317 found->intinfo_count, 1318 CAIRNSMORE2_INTS); 1319 } 1320 info->timeout = ICARUS_CMR2_TIMEOUT; 1321 cmr2_count = 0; 1322 for (i = 0; i < CAIRNSMORE2_INTS; i++) 1323 cmr2_ok[i] = false; 1324 break; 1325 default: 1326 quit(1, "%s icarus_detect_one() invalid %s ident=%d", 1327 icarus->drv->dname, icarus->drv->dname, info->ident); 1328 } 1329 1330 info->nonce_size = ICARUS_READ_SIZE; 1331 // For CMR2 test each USB Interface 1332 1333 retry: 1334 1335 tries = 2; 1336 ok = false; 1337 while (!ok && tries-- > 0) { 1338 icarus_clear(icarus, info); 1339 icarus_initialise(icarus, baud); 1340 1341 if (info->u3) { 1342 uint16_t anu_freq_hex = anu3_find_freqhex(); 1343 1344 set_anu_volt(icarus); 1345 if (!set_anu_freq(icarus, info, anu_freq_hex)) { 1346 applog(LOG_WARNING, "%s %i: Failed to set frequency, too much overclock?", 1347 icarus->drv->name, icarus->device_id); 1348 continue; 1349 } 1350 icarus->usbdev->ident = info->ident = IDENT_AU3; 1351 info->Hs = ANTU3_HASH_TIME; 1352 icarus->drv->name = "AU3"; 1353 applog(LOG_DEBUG, "%s %i: Detected Antminer U3", icarus->drv->name, 1354 icarus->device_id); 1355 } else if (info->ident == IDENT_ANU && !info->u3) { 1356 if (!set_anu_freq(icarus, info, 0)) { 1357 applog(LOG_WARNING, "%s %i: Failed to set frequency, too much overclock?", 1358 icarus->drv->name, icarus->device_id); 1359 continue; 1360 } 1361 } 1362 1363 err = usb_write_ii(icarus, info->intinfo, 1364 (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK); 1365 1366 if (err != LIBUSB_SUCCESS || amount != sizeof(workdata)) 1367 continue; 1368 1369 memset(nonce_bin, 0, sizeof(nonce_bin)); 1370 ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, NULL, 300); 1371 if (ret != ICA_NONCE_OK) 1372 continue; 1373 1374 if (info->nonce_size == ICARUS_READ_SIZE && usb_buffer_size(icarus) == 4) { 1375 applog(LOG_DEBUG, "%s %i: Detected Rockminer, deferring detection", 1376 icarus->drv->name, icarus->device_id); 1377 usb_buffer_clear(icarus); 1378 break; 1379 1380 } 1381 if (info->nonce_size == ICARUS_READ_SIZE && usb_buffer_size(icarus) == 1) { 1382 info->ant = true; 1383 usb_buffer_clear(icarus); 1384 icarus->usbdev->ident = info->ident = IDENT_ANU; 1385 info->nonce_size = ANT_READ_SIZE; 1386 info->Hs = ANTMINERUSB_HASH_TIME; 1387 icarus->drv->name = "ANU"; 1388 applog(LOG_DEBUG, "%s %i: Detected Antminer U1/2/3, changing nonce size to %d", 1389 icarus->drv->name, icarus->device_id, ANT_READ_SIZE); 1390 } 1391 1392 nonce_hex = bin2hex(nonce_bin, sizeof(nonce_bin)); 1393 if (strncmp(nonce_hex, golden_nonce, 8) == 0) { 1394 if (info->ant && !anu_freqset) 1395 anu_freqset = true; 1396 else 1397 ok = true; 1398 } else { 1399 if (tries < 0 && info->ident != IDENT_CMR2) { 1400 applog(LOG_ERR, 1401 "Icarus Detect: " 1402 "Test failed at %s: get %s, should: %s", 1403 icarus->device_path, nonce_hex, golden_nonce); 1404 } 1405 } 1406 free(nonce_hex); 1407 } 1408 1409 if (!ok) { 1410 if (info->ident != IDENT_CMR2) { 1411 if (info->u3) 1412 goto unshin; 1413 info->u3 = true; 1414 goto retry; 1415 } 1416 1417 if (info->intinfo < CAIRNSMORE2_INTS-1) { 1418 info->intinfo++; 1419 goto retry; 1420 } 1421 } else { 1422 if (info->ident == IDENT_CMR2) { 1423 applog(LOG_DEBUG, 1424 "Icarus Detect: " 1425 "Test succeeded at %s i%d: got %s", 1426 icarus->device_path, info->intinfo, golden_nonce); 1427 1428 cmr2_ok[info->intinfo] = true; 1429 cmr2_count++; 1430 if (info->intinfo < CAIRNSMORE2_INTS-1) { 1431 info->intinfo++; 1432 goto retry; 1433 } 1434 } 1435 } 1436 1437 if (info->ident == IDENT_CMR2) { 1438 if (cmr2_count == 0) { 1439 applog(LOG_ERR, 1440 "Icarus Detect: Test failed at %s: for all %d CMR2 Interfaces", 1441 icarus->device_path, CAIRNSMORE2_INTS); 1442 goto unshin; 1443 } 1444 1445 // set the interface to the first one that succeeded 1446 for (i = 0; i < CAIRNSMORE2_INTS; i++) 1447 if (cmr2_ok[i]) { 1448 info->intinfo = i; 1449 break; 1450 } 1451 } else { 1452 applog(LOG_DEBUG, 1453 "Icarus Detect: " 1454 "Test succeeded at %s: got %s", 1455 icarus->device_path, golden_nonce); 1456 } 1457 1458 /* We have a real Icarus! */ 1459 if (!add_cgpu(icarus)) 1460 goto unshin; 1461 1462 update_usb_stats(icarus); 1463 1464 applog(LOG_INFO, "%s %d: Found at %s", 1465 icarus->drv->name, icarus->device_id, icarus->device_path); 1466 1467 if (info->ident == IDENT_CMR2) { 1468 applog(LOG_INFO, "%s %d: with %d Interface%s", 1469 icarus->drv->name, icarus->device_id, 1470 cmr2_count, cmr2_count > 1 ? "s" : ""); 1471 1472 // Assume 1 or 2 are running FPGA pairs 1473 if (cmr2_count < 3) { 1474 work_division = fpga_count = 2; 1475 info->Hs /= 2; 1476 } 1477 } 1478 1479 applog(LOG_DEBUG, "%s %d: Init baud=%d work_division=%d fpga_count=%d", 1480 icarus->drv->name, icarus->device_id, baud, work_division, fpga_count); 1481 1482 info->baud = baud; 1483 info->work_division = work_division; 1484 info->fpga_count = fpga_count; 1485 info->nonce_mask = mask(work_division); 1486 1487 info->golden_hashes = (golden_nonce_val & info->nonce_mask) * fpga_count; 1488 timersub(&tv_finish, &tv_start, &(info->golden_tv)); 1489 1490 set_timing_mode(this_option_offset, icarus); 1491 1492 if (info->ident == IDENT_CMR2) { 1493 int i; 1494 for (i = info->intinfo + 1; i < icarus->usbdev->found->intinfo_count; i++) { 1495 struct cgpu_info *cgtmp; 1496 struct ICARUS_INFO *intmp; 1497 1498 if (!cmr2_ok[i]) 1499 continue; 1500 1501 cgtmp = usb_copy_cgpu(icarus); 1502 if (!cgtmp) { 1503 applog(LOG_ERR, "%s %d: Init failed initinfo %d", 1504 icarus->drv->name, icarus->device_id, i); 1505 continue; 1506 } 1507 1508 cgtmp->usbinfo.usbstat = USB_NOSTAT; 1509 1510 intmp = cgmalloc(sizeof(struct ICARUS_INFO)); 1511 1512 cgtmp->device_data = (void *)intmp; 1513 1514 // Initialise everything to match 1515 memcpy(intmp, info, sizeof(struct ICARUS_INFO)); 1516 1517 intmp->intinfo = i; 1518 1519 icarus_initialise(cgtmp, baud); 1520 1521 if (!add_cgpu(cgtmp)) { 1522 usb_uninit(cgtmp); 1523 free(intmp); 1524 continue; 1525 } 1526 1527 update_usb_stats(cgtmp); 1528 } 1529 } 1530 1531 return icarus; 1532 1533 unshin: 1534 1535 usb_uninit(icarus); 1536 free(info); 1537 icarus->device_data = NULL; 1538 1539 shin: 1540 1541 icarus = usb_free_cgpu(icarus); 1542 1543 return NULL; 1544 } 1545 1546 static struct cgpu_info *compac_detect_one(struct libusb_device *dev, struct usb_find_devices *found) 1547 { 1548 struct ICARUS_INFO *info; 1549 struct timeval tv_start, tv_finish; 1550 struct cgpu_info *compac = usb_alloc_cgpu(&icarus_drv, 1); 1551 1552 if (!usb_init(compac, dev, found)) { 1553 compac = usb_free_cgpu(compac); 1554 return NULL; 1555 } 1556 1557 info = cgcalloc(1, sizeof(struct ICARUS_INFO)); 1558 compac->device_data = info; 1559 1560 info->ident = usb_ident(compac); 1561 1562 if (info->ident == IDENT_BSC || info->ident == IDENT_GSC) { 1563 int this_option_offset = ++option_offset; 1564 const uint32_t golden_nonce_val = 0x000187a2; 1565 int baud, uninitialised_var(work_division), uninitialised_var(fpga_count); 1566 uint16_t compac_freq_hex; 1567 1568 applog(LOG_DEBUG, "%s %i: Detected GekkoScience Compac", compac->drv->name, 1569 compac->device_id); 1570 1571 get_options(this_option_offset, compac, &baud, &work_division, &fpga_count); 1572 1573 info->compac_ramp_idx = 0; 1574 info->compac_ramp_freq = compacfreqtable[info->compac_ramp_idx].freq; 1575 info->compac_target_freq = opt_compac_freq; 1576 1577 compac_freq_hex = compacfreqtable[info->compac_ramp_idx].hex; 1578 1579 if (!set_anu_freq(compac, info, compac_freq_hex)) { 1580 applog(LOG_WARNING, "%s %i: Failed to set frequency, too much overclock?", 1581 compac->drv->name, compac->device_id); 1582 } 1583 1584 applog(LOG_DEBUG, "%s %d: Init baud=%d work_division=%d fpga_count=%d", 1585 compac->drv->name, compac->device_id, baud, work_division, fpga_count); 1586 1587 info->ant = true; 1588 info->compac = true; 1589 info->baud = baud; 1590 info->work_division = work_division; 1591 info->fpga_count = fpga_count; 1592 info->nonce_mask = mask(work_division); 1593 info->nonce_size = ANT_READ_SIZE; 1594 1595 if (add_cgpu(compac)) { 1596 char *tmp_str = cgmalloc(50 * sizeof(char)); 1597 1598 strncpy(tmp_str,compac->unique_id,11); 1599 strncpy(compac->unique_id,"\0\0\0\0\0\0\0\0\0\0\0",11); 1600 strncpy(compac->unique_id,(tmp_str)+3*sizeof(char),8); 1601 1602 update_usb_stats(compac); 1603 1604 info->golden_hashes = (golden_nonce_val & info->nonce_mask) * fpga_count; 1605 timersub(&tv_finish, &tv_start, &(info->golden_tv)); 1606 1607 set_timing_mode(this_option_offset, compac); 1608 return compac; 1609 } 1610 } 1611 1612 usb_uninit(compac); 1613 free(info); 1614 compac->device_data = NULL; 1615 1616 compac = usb_free_cgpu(compac); 1617 return NULL; 1618 } 1619 1620 static int64_t rock_scanwork(struct thr_info *thr); 1621 1622 static void rock_statline_before(char *buf, size_t bufsiz, struct cgpu_info *cgpu) 1623 { 1624 if (cgpu->temp) 1625 tailsprintf(buf, bufsiz, "%3.0fMHz %3.0fC", opt_rock_freq, cgpu->temp); 1626 else 1627 tailsprintf(buf, bufsiz, "%.0fMHz", opt_rock_freq); 1628 } 1629 1630 /* The only thing to do on flush_work is to remove the base work to prevent us 1631 * rolling what is now stale work */ 1632 static void rock_flush(struct cgpu_info *icarus) 1633 { 1634 struct ICARUS_INFO *info = icarus->device_data; 1635 struct work *work; 1636 1637 mutex_lock(&info->lock); 1638 work = info->base_work; 1639 info->base_work = NULL; 1640 mutex_unlock(&info->lock); 1641 1642 if (work) 1643 free_work(work); 1644 } 1645 1646 static struct cgpu_info *rock_detect_one(struct libusb_device *dev, struct usb_find_devices *found) 1647 { 1648 struct ICARUS_INFO *info; 1649 struct timeval tv_start, tv_finish; 1650 char *ob_hex = NULL; 1651 1652 // Block 171874 nonce = (0xa2870100) = 0x000187a2 1653 // N.B. golden_ob MUST take less time to calculate 1654 // than the timeout set in icarus_open() 1655 // This one takes ~0.53ms on Rev3 Icarus 1656 const char golden_ob[] = 1657 "4679ba4ec99876bf4bfe086082b40025" 1658 "4df6c356451471139a3afa71e48f544a" 1659 "00000000000000000000000000000000" 1660 "aa1ff05587320b1a1426674f2fa722ce"; 1661 1662 const char golden_nonce[] = "000187a2"; 1663 const uint32_t golden_nonce_val = 0x000187a2; 1664 unsigned char nonce_bin[ICARUS_BUF_SIZE]; 1665 struct ICARUS_WORK workdata; 1666 char *nonce_hex; 1667 struct cgpu_info *icarus; 1668 int ret, err, amount, tries; 1669 bool ok; 1670 int correction_times = 0; 1671 NONCE_DATA nonce_data; 1672 uint32_t nonce; 1673 char *newname = NULL; 1674 1675 if ((sizeof(workdata) << 1) != (sizeof(golden_ob) - 1)) 1676 quithere(1, "Data and golden_ob sizes don't match"); 1677 1678 icarus = usb_alloc_cgpu(&icarus_drv, 1); 1679 1680 if (!usb_init(icarus, dev, found)) 1681 goto shin; 1682 1683 hex2bin((void *)(&workdata), golden_ob, sizeof(workdata)); 1684 rev((void *)(&(workdata.midstate)), ICARUS_MIDSTATE_SIZE); 1685 rev((void *)(&(workdata.work)), ICARUS_WORK_SIZE); 1686 if (opt_debug) { 1687 ob_hex = bin2hex((void *)(&workdata), sizeof(workdata)); 1688 applog(LOG_WARNING, "%s %d: send_gold_nonce %s", 1689 icarus->drv->name, icarus->device_id, ob_hex); 1690 free(ob_hex); 1691 } 1692 1693 info = cgcalloc(1, sizeof(struct ICARUS_INFO)); 1694 (void)memset(info, 0, sizeof(struct ICARUS_INFO)); 1695 icarus->device_data = (void *)info; 1696 icarus->usbdev->ident = info->ident = IDENT_LIN; 1697 info->nonce_size = ROCK_READ_SIZE; 1698 info->fail_time = 10; 1699 info->nonce_mask = 0xffffffff; 1700 update_usb_stats(icarus); 1701 1702 tries = MAX_TRIES; 1703 ok = false; 1704 while (!ok && tries-- > 0) { 1705 icarus_initialise(icarus, info->baud); 1706 1707 applog(LOG_DEBUG, "tries: %d", tries); 1708 workdata.unused[ICARUS_UNUSED_SIZE - 3] = opt_rock_freq/10 - 1; 1709 workdata.unused[ICARUS_UNUSED_SIZE - 2] = (MAX_TRIES-1-tries); 1710 info->rmdev.detect_chip_no++; 1711 if (info->rmdev.detect_chip_no >= MAX_TRIES) 1712 info->rmdev.detect_chip_no = 0; 1713 //g_detect_chip_no = (g_detect_chip_no + 1) & MAX_CHIP_NUM; 1714 1715 usb_buffer_clear(icarus); 1716 err = usb_write_ii(icarus, info->intinfo, 1717 (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK); 1718 if (err != LIBUSB_SUCCESS || amount != sizeof(workdata)) 1719 continue; 1720 1721 memset(nonce_bin, 0, sizeof(nonce_bin)); 1722 ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, NULL, 100); 1723 1724 applog(LOG_DEBUG, "Rockminer nonce_bin: %02x %02x %02x %02x %02x %02x %02x %02x", 1725 nonce_bin[0], nonce_bin[1], nonce_bin[2], nonce_bin[3], 1726 nonce_bin[4], nonce_bin[5], nonce_bin[6], nonce_bin[7]); 1727 if (ret != ICA_NONCE_OK) { 1728 applog(LOG_DEBUG, "detect_one get_gold_nonce error, tries = %d", tries); 1729 continue; 1730 } 1731 if (usb_buffer_size(icarus) == 1) { 1732 applog(LOG_INFO, "Rock detect found an ANU, skipping"); 1733 usb_buffer_clear(icarus); 1734 break; 1735 } 1736 1737 newname = NULL; 1738 switch (nonce_bin[NONCE_CHIP_NO_OFFSET] & RM_PRODUCT_MASK) { 1739 case RM_PRODUCT_T1: 1740 newname = "LIR"; // Rocketbox 1741 info->rmdev.product_id = ROCKMINER_T1; 1742 info->rmdev.chip_max = 12; 1743 info->rmdev.min_frq = 200; 1744 info->rmdev.def_frq = 330; 1745 info->rmdev.max_frq = 400; 1746 break; 1747 #if 0 1748 case RM_PRODUCT_T2: // what's this? 1749 newname = "LIX"; 1750 info->rmdev.product_id = ROCKMINER_T2; 1751 info->rmdev.chip_max = 16; 1752 info->rmdev.min_frq = 200; 1753 info->rmdev.def_frq = 300; 1754 info->rmdev.max_frq = 400; 1755 break; 1756 #endif 1757 case RM_PRODUCT_RBOX: 1758 newname = "LIN"; // R-Box 1759 info->rmdev.product_id = ROCKMINER_RBOX; 1760 info->rmdev.chip_max = 4; 1761 info->rmdev.min_frq = 200; 1762 info->rmdev.def_frq = 270; 1763 info->rmdev.max_frq = 400; 1764 break; 1765 default: 1766 continue; 1767 } 1768 1769 snprintf(info->rock_init, sizeof(info->rock_init), "%02x %02x %02x %02x", 1770 nonce_bin[4], nonce_bin[5], nonce_bin[6], nonce_bin[7]); 1771 1772 nonce_data.chip_no = nonce_bin[NONCE_CHIP_NO_OFFSET] & RM_CHIP_MASK; 1773 if (nonce_data.chip_no >= info->rmdev.chip_max) 1774 nonce_data.chip_no = 0; 1775 1776 nonce_data.cmd_value = nonce_bin[NONCE_TASK_CMD_OFFSET] & RM_CMD_MASK; 1777 if (nonce_data.cmd_value == NONCE_TASK_COMPLETE_CMD) { 1778 applog(LOG_DEBUG, "complete g_detect_chip_no: %d", info->rmdev.detect_chip_no); 1779 workdata.unused[ICARUS_UNUSED_SIZE - 3] = opt_rock_freq/10 - 1; 1780 workdata.unused[ICARUS_UNUSED_SIZE - 2] = info->rmdev.detect_chip_no; 1781 info->rmdev.detect_chip_no++; 1782 if (info->rmdev.detect_chip_no >= MAX_TRIES) 1783 info->rmdev.detect_chip_no = 0; 1784 1785 err = usb_write_ii(icarus, info->intinfo, 1786 (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK); 1787 if (err != LIBUSB_SUCCESS || amount != sizeof(workdata)) 1788 continue; 1789 applog(LOG_DEBUG, "send_gold_nonce usb_write_ii"); 1790 continue; 1791 } 1792 1793 memcpy((char *)&nonce, nonce_bin, ICARUS_READ_SIZE); 1794 nonce = htobe32(nonce); 1795 applog(LOG_DEBUG, "Rockminer nonce: %08X", nonce); 1796 correction_times = 0; 1797 while (correction_times < NONCE_CORRECTION_TIMES) { 1798 nonce_hex = bin2hex(nonce_bin, 4); 1799 if (golden_nonce_val == nonce + rbox_corr_values[correction_times]) { 1800 memset(&(info->g_work[0]), 0, sizeof(info->g_work)); 1801 rock_init_last_received_task_complete_time(info); 1802 1803 ok = true; 1804 break; 1805 } else { 1806 applog(LOG_DEBUG, "detect_one gold_nonce compare error times = %d", 1807 correction_times); 1808 if (tries < 0 && info->ident != IDENT_CMR2) { 1809 applog(LOG_WARNING, 1810 "Icarus Detect: " 1811 "Test failed at %s: get %s, should: %s", 1812 icarus->device_path, nonce_hex, golden_nonce); 1813 } 1814 1815 if (nonce == 0) 1816 break; 1817 } 1818 free(nonce_hex); 1819 correction_times++; 1820 } 1821 } 1822 1823 if (!ok) 1824 goto unshin; 1825 1826 if (newname) { 1827 if (!icarus->drv->copy) 1828 icarus->drv = copy_drv(icarus->drv); 1829 icarus->drv->name = newname; 1830 } 1831 1832 applog(LOG_DEBUG, "Icarus Detect: Test succeeded at %s: got %s", 1833 icarus->device_path, golden_nonce); 1834 1835 /* We have a real Rockminer! */ 1836 if (!add_cgpu(icarus)) 1837 goto unshin; 1838 1839 icarus->drv->scanwork = rock_scanwork; 1840 icarus->drv->dname = "Rockminer"; 1841 icarus->drv->get_statline_before = &rock_statline_before; 1842 icarus->drv->flush_work = &rock_flush; 1843 mutex_init(&info->lock); 1844 1845 applog(LOG_INFO, "%s %d: Found at %s", 1846 icarus->drv->name, icarus->device_id, 1847 icarus->device_path); 1848 1849 timersub(&tv_finish, &tv_start, &(info->golden_tv)); 1850 1851 return icarus; 1852 1853 unshin: 1854 1855 usb_uninit(icarus); 1856 free(info); 1857 icarus->device_data = NULL; 1858 1859 shin: 1860 1861 icarus = usb_free_cgpu(icarus); 1862 1863 return NULL; 1864 } 1865 1866 static void icarus_detect(bool __maybe_unused hotplug) 1867 { 1868 usb_detect(&icarus_drv, rock_detect_one); 1869 usb_detect(&icarus_drv, compac_detect_one); 1870 usb_detect(&icarus_drv, icarus_detect_one); 1871 } 1872 1873 static bool icarus_prepare(struct thr_info *thr) 1874 { 1875 struct cgpu_info *icarus = thr->cgpu; 1876 struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data); 1877 1878 if (info->ant) 1879 info->antworks = cgcalloc(sizeof(struct work *), ANT_QUEUE_NUM); 1880 return true; 1881 } 1882 1883 static void cmr2_command(struct cgpu_info *icarus, uint8_t cmd, uint8_t data) 1884 { 1885 struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data); 1886 struct ICARUS_WORK workdata; 1887 int amount; 1888 1889 memset((void *)(&workdata), 0, sizeof(workdata)); 1890 1891 workdata.prefix = ICARUS_CMR2_PREFIX; 1892 workdata.cmd = cmd; 1893 workdata.data = data; 1894 workdata.check = workdata.data ^ workdata.cmd ^ workdata.prefix ^ ICARUS_CMR2_CHECK; 1895 1896 usb_write_ii(icarus, info->intinfo, (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK); 1897 } 1898 1899 static void cmr2_commands(struct cgpu_info *icarus) 1900 { 1901 struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data); 1902 1903 if (info->speed_next_work) { 1904 info->speed_next_work = false; 1905 cmr2_command(icarus, ICARUS_CMR2_CMD_SPEED, info->cmr2_speed); 1906 return; 1907 } 1908 1909 if (info->flash_next_work) { 1910 info->flash_next_work = false; 1911 cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_ON); 1912 cgsleep_ms(250); 1913 cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_OFF); 1914 cgsleep_ms(250); 1915 cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_ON); 1916 cgsleep_ms(250); 1917 cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_OFF); 1918 return; 1919 } 1920 } 1921 1922 void rock_send_task(unsigned char chip_no, unsigned int current_task_id, struct thr_info *thr) 1923 { 1924 struct cgpu_info *icarus = thr->cgpu; 1925 struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data); 1926 int err, amount; 1927 struct ICARUS_WORK workdata; 1928 char *ob_hex; 1929 struct work *work = NULL; 1930 1931 /* Only base_work needs locking since it can be asynchronously deleted 1932 * by flush work */ 1933 if (info->g_work[chip_no][current_task_id] == NULL) { 1934 mutex_lock(&info->lock); 1935 if (!info->base_work) 1936 info->base_work = get_work(thr, thr->id); 1937 if (info->base_work->drv_rolllimit > 0) { 1938 info->base_work->drv_rolllimit--; 1939 roll_work(info->base_work); 1940 work = make_clone(info->base_work); 1941 } else { 1942 work = info->base_work; 1943 info->base_work = NULL; 1944 } 1945 mutex_unlock(&info->lock); 1946 1947 info->g_work[chip_no][current_task_id] = work; 1948 } else { 1949 work = info->g_work[chip_no][current_task_id]; 1950 applog(LOG_DEBUG, "::resend work"); 1951 } 1952 1953 memset((void *)(&workdata), 0, sizeof(workdata)); 1954 memcpy(&(workdata.midstate), work->midstate, ICARUS_MIDSTATE_SIZE); 1955 memcpy(&(workdata.work), work->data + ICARUS_WORK_DATA_OFFSET, ICARUS_WORK_SIZE); 1956 workdata.unused[ICARUS_UNUSED_SIZE - 4] = 0xaa; 1957 if (info->rmdev.chip[chip_no].freq > (info->rmdev.max_frq/10 - 1) || 1958 info->rmdev.chip[chip_no].freq < (info->rmdev.min_frq/10 - 1)) 1959 rock_init_last_received_task_complete_time(info); 1960 1961 workdata.unused[ICARUS_UNUSED_SIZE - 3] = info->rmdev.chip[chip_no].freq; //icarus->freq/10 - 1; ; 1962 workdata.unused[ICARUS_UNUSED_SIZE - 2] = chip_no ; 1963 workdata.id = 0x55; 1964 1965 if (opt_debug) { 1966 ob_hex = bin2hex((void *)(work->data), 128); 1967 applog(LOG_WARNING, "%s %d: work->data %s", 1968 icarus->drv->name, icarus->device_id, ob_hex); 1969 free(ob_hex); 1970 } 1971 1972 // We only want results for the work we are about to send 1973 usb_buffer_clear(icarus); 1974 1975 err = usb_write_ii(icarus, info->intinfo, (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK); 1976 1977 if (err < 0 || amount != sizeof(workdata)) { 1978 applog(LOG_ERR, "%s %i: Comms error (werr=%d amt=%d)", 1979 icarus->drv->name, icarus->device_id, err, amount); 1980 dev_error(icarus, REASON_DEV_COMMS_ERROR); 1981 icarus_initialise(icarus, info->baud); 1982 1983 if (info->g_work[chip_no][current_task_id]) 1984 { 1985 free_work(info->g_work[chip_no][current_task_id]); 1986 info->g_work[chip_no][current_task_id] = NULL; 1987 } 1988 1989 return; 1990 } 1991 1992 return; 1993 } 1994 1995 static void process_history(struct cgpu_info *icarus, struct ICARUS_INFO *info, uint32_t nonce, 1996 uint64_t hash_count, struct timeval *elapsed, struct timeval *tv_start) 1997 { 1998 struct ICARUS_HISTORY *history0, *history; 1999 struct timeval tv_history_start, tv_history_finish; 2000 int count; 2001 double Hs, W, fullnonce; 2002 int read_time, i; 2003 bool limited; 2004 uint32_t values; 2005 int64_t hash_count_range; 2006 double Ti, Xi; 2007 2008 // Ignore possible end condition values ... 2009 // TODO: set limitations on calculated values depending on the device 2010 // to avoid crap values caused by CPU/Task Switching/Swapping/etc 2011 if ((nonce & info->nonce_mask) <= END_CONDITION || 2012 (nonce & info->nonce_mask) >= (info->nonce_mask & ~END_CONDITION)) 2013 return; 2014 2015 cgtime(&tv_history_start); 2016 2017 history0 = &(info->history[0]); 2018 2019 if (history0->values == 0) 2020 timeradd(tv_start, &history_sec, &(history0->finish)); 2021 2022 Ti = (double)(elapsed->tv_sec) 2023 + ((double)(elapsed->tv_usec))/((double)1000000) 2024 - ((double)ICARUS_READ_TIME(info->baud)); 2025 Xi = (double)hash_count; 2026 history0->sumXiTi += Xi * Ti; 2027 history0->sumXi += Xi; 2028 history0->sumTi += Ti; 2029 history0->sumXi2 += Xi * Xi; 2030 2031 history0->values++; 2032 2033 if (history0->hash_count_max < hash_count) 2034 history0->hash_count_max = hash_count; 2035 if (history0->hash_count_min > hash_count || history0->hash_count_min == 0) 2036 history0->hash_count_min = hash_count; 2037 2038 if (history0->values >= info->min_data_count 2039 && timercmp(tv_start, &(history0->finish), >)) { 2040 for (i = INFO_HISTORY; i > 0; i--) 2041 memcpy(&(info->history[i]), 2042 &(info->history[i-1]), 2043 sizeof(struct ICARUS_HISTORY)); 2044 2045 // Initialise history0 to zero for summary calculation 2046 memset(history0, 0, sizeof(struct ICARUS_HISTORY)); 2047 2048 // We just completed a history data set 2049 // So now recalc read_time based on the whole history thus we will 2050 // initially get more accurate until it completes INFO_HISTORY 2051 // total data sets 2052 count = 0; 2053 for (i = 1 ; i <= INFO_HISTORY; i++) { 2054 history = &(info->history[i]); 2055 if (history->values >= MIN_DATA_COUNT) { 2056 count++; 2057 2058 history0->sumXiTi += history->sumXiTi; 2059 history0->sumXi += history->sumXi; 2060 history0->sumTi += history->sumTi; 2061 history0->sumXi2 += history->sumXi2; 2062 history0->values += history->values; 2063 2064 if (history0->hash_count_max < history->hash_count_max) 2065 history0->hash_count_max = history->hash_count_max; 2066 if (history0->hash_count_min > history->hash_count_min || history0->hash_count_min == 0) 2067 history0->hash_count_min = history->hash_count_min; 2068 } 2069 } 2070 2071 // All history data 2072 Hs = (history0->values*history0->sumXiTi - history0->sumXi*history0->sumTi) 2073 / (history0->values*history0->sumXi2 - history0->sumXi*history0->sumXi); 2074 W = history0->sumTi/history0->values - Hs*history0->sumXi/history0->values; 2075 hash_count_range = history0->hash_count_max - history0->hash_count_min; 2076 values = history0->values; 2077 2078 // Initialise history0 to zero for next data set 2079 memset(history0, 0, sizeof(struct ICARUS_HISTORY)); 2080 2081 fullnonce = W + Hs * (((double)0xffffffff) + 1); 2082 read_time = SECTOMS(fullnonce) - ICARUS_READ_REDUCE; 2083 if (info->read_time_limit > 0 && read_time > info->read_time_limit) { 2084 read_time = info->read_time_limit; 2085 limited = true; 2086 } else 2087 limited = false; 2088 2089 info->Hs = Hs; 2090 info->read_time = read_time; 2091 2092 info->fullnonce = fullnonce; 2093 info->count = count; 2094 info->W = W; 2095 info->values = values; 2096 info->hash_count_range = hash_count_range; 2097 2098 if (info->min_data_count < MAX_MIN_DATA_COUNT) 2099 info->min_data_count *= 2; 2100 else if (info->timing_mode == MODE_SHORT) 2101 info->do_icarus_timing = false; 2102 2103 applog(LOG_WARNING, "%s %d Re-estimate: Hs=%e W=%e read_time=%dms%s fullnonce=%.3fs", 2104 icarus->drv->name, icarus->device_id, Hs, W, read_time, 2105 limited ? " (limited)" : "", fullnonce); 2106 } 2107 info->history_count++; 2108 cgtime(&tv_history_finish); 2109 2110 timersub(&tv_history_finish, &tv_history_start, &tv_history_finish); 2111 timeradd(&tv_history_finish, &(info->history_time), &(info->history_time)); 2112 } 2113 2114 static int64_t icarus_scanwork(struct thr_info *thr) 2115 { 2116 struct cgpu_info *icarus = thr->cgpu; 2117 struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data); 2118 int ret, err, amount; 2119 unsigned char nonce_bin[ICARUS_BUF_SIZE]; 2120 struct ICARUS_WORK workdata; 2121 char *ob_hex; 2122 uint32_t nonce; 2123 int64_t hash_count = 0; 2124 struct timeval tv_start, tv_finish, elapsed; 2125 int curr_hw_errors; 2126 bool was_hw_error; 2127 struct work *work; 2128 int64_t estimate_hashes; 2129 uint8_t workid = 0; 2130 2131 if (info->compac && info->compac_ramp_freq < info->compac_target_freq) { 2132 uint16_t compac_freq_hex = compacfreqtable[++info->compac_ramp_idx].hex; 2133 2134 if (!set_anu_freq(icarus, info, compac_freq_hex)) { 2135 applog(LOG_WARNING, "%s %i: Failed to set frequency, too much overclock?", 2136 icarus->drv->name, icarus->device_id); 2137 info->compac_target_freq = info->compac_ramp_freq; 2138 } else 2139 info->compac_ramp_freq = compacfreqtable[info->compac_ramp_idx].freq; 2140 } 2141 2142 if (unlikely(share_work_tdiff(icarus) > info->fail_time)) { 2143 if (info->failing) { 2144 if (share_work_tdiff(icarus) > info->fail_time + 60) { 2145 applog(LOG_ERR, "%s %d: Device failed to respond to restart", 2146 icarus->drv->name, icarus->device_id); 2147 usb_nodev(icarus); 2148 return -1; 2149 } 2150 } else { 2151 applog(LOG_WARNING, "%s %d: No valid hashes for over %d secs, attempting to reset", 2152 icarus->drv->name, icarus->device_id, info->fail_time); 2153 usb_reset(icarus); 2154 info->failing = true; 2155 } 2156 } 2157 2158 // Device is gone 2159 if (icarus->usbinfo.nodev) 2160 return -1; 2161 2162 elapsed.tv_sec = elapsed.tv_usec = 0; 2163 2164 work = get_work(thr, thr->id); 2165 memset((void *)(&workdata), 0, sizeof(workdata)); 2166 memcpy(&(workdata.midstate), work->midstate, ICARUS_MIDSTATE_SIZE); 2167 memcpy(&(workdata.work), work->data + ICARUS_WORK_DATA_OFFSET, ICARUS_WORK_SIZE); 2168 rev((void *)(&(workdata.midstate)), ICARUS_MIDSTATE_SIZE); 2169 rev((void *)(&(workdata.work)), ICARUS_WORK_SIZE); 2170 if (info->ant) { 2171 workid = info->workid; 2172 if (++info->workid >= 0x1F) 2173 info->workid = 0; 2174 if (info->antworks[workid]) 2175 free_work(info->antworks[workid]); 2176 info->antworks[workid] = work; 2177 workdata.id = workid; 2178 } 2179 2180 if (info->speed_next_work || info->flash_next_work) 2181 cmr2_commands(icarus); 2182 2183 // We only want results for the work we are about to send 2184 usb_buffer_clear(icarus); 2185 2186 err = usb_write_ii(icarus, info->intinfo, (char *)(&workdata), sizeof(workdata), &amount, C_SENDWORK); 2187 if (err < 0 || amount != sizeof(workdata)) { 2188 applog(LOG_ERR, "%s %i: Comms error (werr=%d amt=%d)", 2189 icarus->drv->name, icarus->device_id, err, amount); 2190 dev_error(icarus, REASON_DEV_COMMS_ERROR); 2191 icarus_initialise(icarus, info->baud); 2192 goto out; 2193 } 2194 2195 if (opt_debug) { 2196 ob_hex = bin2hex((void *)(&workdata), sizeof(workdata)); 2197 applog(LOG_DEBUG, "%s %d: sent %s", 2198 icarus->drv->name, icarus->device_id, ob_hex); 2199 free(ob_hex); 2200 } 2201 more_nonces: 2202 /* Icarus will return nonces or nothing. If we know we have enough data 2203 * for a response in the buffer already, there will be no usb read 2204 * performed. */ 2205 memset(nonce_bin, 0, sizeof(nonce_bin)); 2206 ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, thr, info->read_time); 2207 if (ret == ICA_NONCE_ERROR) 2208 goto out; 2209 2210 // aborted before becoming idle, get new work 2211 if (ret == ICA_NONCE_TIMEOUT || ret == ICA_NONCE_RESTART) { 2212 if (info->ant) 2213 goto out; 2214 2215 timersub(&tv_finish, &tv_start, &elapsed); 2216 2217 // ONLY up to just when it aborted 2218 // We didn't read a reply so we don't subtract ICARUS_READ_TIME 2219 estimate_hashes = ((double)(elapsed.tv_sec) 2220 + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs; 2221 2222 // If some Serial-USB delay allowed the full nonce range to 2223 // complete it can't have done more than a full nonce 2224 if (unlikely(estimate_hashes > 0xffffffff)) 2225 estimate_hashes = 0xffffffff; 2226 2227 applog(LOG_DEBUG, "%s %d: no nonce = 0x%08lX hashes (%ld.%06lds)", 2228 icarus->drv->name, icarus->device_id, 2229 (long unsigned int)estimate_hashes, 2230 (long)elapsed.tv_sec, (long)elapsed.tv_usec); 2231 2232 hash_count = estimate_hashes; 2233 goto out; 2234 } 2235 2236 if (info->ant) { 2237 workid = nonce_bin[4] & 0x1F; 2238 if (info->antworks[workid]) 2239 work = info->antworks[workid]; 2240 else 2241 goto out; 2242 } 2243 2244 memcpy((char *)&nonce, nonce_bin, ICARUS_READ_SIZE); 2245 nonce = htobe32(nonce); 2246 curr_hw_errors = icarus->hw_errors; 2247 if (submit_nonce(thr, work, nonce)) 2248 info->failing = false; 2249 was_hw_error = (curr_hw_errors < icarus->hw_errors); 2250 2251 /* U3s return shares fast enough to use just that for hashrate 2252 * calculation, otherwise the result is inaccurate instead. */ 2253 if (info->ant) { 2254 info->nonces++; 2255 if (usb_buffer_size(icarus) >= ANT_READ_SIZE) 2256 goto more_nonces; 2257 } else { 2258 hash_count = (nonce & info->nonce_mask); 2259 hash_count++; 2260 hash_count *= info->fpga_count; 2261 } 2262 2263 #if 0 2264 // This appears to only return zero nonce values 2265 if (usb_buffer_size(icarus) > 3) { 2266 memcpy((char *)&nonce, icarus->usbdev->buffer, sizeof(nonce_bin)); 2267 nonce = htobe32(nonce); 2268 applog(LOG_WARNING, "%s %d: attempting to submit 2nd nonce = 0x%08lX", 2269 icarus->drv->name, icarus->device_id, 2270 (long unsigned int)nonce); 2271 curr_hw_errors = icarus->hw_errors; 2272 submit_nonce(thr, work, nonce); 2273 was_hw_error = (curr_hw_errors > icarus->hw_errors); 2274 } 2275 #endif 2276 2277 if (opt_debug || info->do_icarus_timing) 2278 timersub(&tv_finish, &tv_start, &elapsed); 2279 2280 applog(LOG_DEBUG, "%s %d: nonce = 0x%08x = 0x%08lX hashes (%ld.%06lds)", 2281 icarus->drv->name, icarus->device_id, 2282 nonce, (long unsigned int)hash_count, 2283 (long)elapsed.tv_sec, (long)elapsed.tv_usec); 2284 2285 if (info->do_icarus_timing && !was_hw_error) 2286 process_history(icarus, info, nonce, hash_count, &elapsed, &tv_start); 2287 out: 2288 if (!info->ant) 2289 free_work(work); 2290 else { 2291 /* Ant USBs free the work themselves. Return only one full 2292 * nonce worth on each pass to smooth out displayed hashrate */ 2293 if (info->nonces) { 2294 hash_count = 0xffffffff; 2295 info->nonces--; 2296 } 2297 } 2298 2299 return hash_count; 2300 } 2301 2302 static int64_t rock_scanwork(struct thr_info *thr) 2303 { 2304 struct cgpu_info *icarus = thr->cgpu; 2305 struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data); 2306 int ret; 2307 unsigned char nonce_bin[ICARUS_BUF_SIZE]; 2308 uint32_t nonce; 2309 int64_t hash_count = 0; 2310 struct timeval tv_start, tv_finish, elapsed; 2311 struct work *work = NULL; 2312 int64_t estimate_hashes; 2313 int correction_times = 0; 2314 NONCE_DATA nonce_data; 2315 double temp; 2316 2317 int chip_no = 0; 2318 time_t recv_time = 0; 2319 2320 if (unlikely(share_work_tdiff(icarus) > info->fail_time)) { 2321 if (info->failing) { 2322 if (share_work_tdiff(icarus) > info->fail_time + 60) { 2323 applog(LOG_ERR, "%s %d: Device failed to respond to restart", 2324 icarus->drv->name, icarus->device_id); 2325 usb_nodev(icarus); 2326 return -1; 2327 } 2328 } else { 2329 applog(LOG_WARNING, "%s %d: No valid hashes for over %d secs, attempting to reset", 2330 icarus->drv->name, icarus->device_id, info->fail_time); 2331 usb_reset(icarus); 2332 info->failing = true; 2333 } 2334 } 2335 2336 // Device is gone 2337 if (icarus->usbinfo.nodev) 2338 return -1; 2339 2340 elapsed.tv_sec = elapsed.tv_usec = 0; 2341 2342 for (chip_no = 0; chip_no < info->rmdev.chip_max; chip_no++) { 2343 recv_time = time(NULL); 2344 if (recv_time > info->rmdev.chip[chip_no].last_received_task_complete_time + 1) { 2345 info->rmdev.chip[chip_no].last_received_task_complete_time = recv_time; 2346 rock_send_task(chip_no, 0,thr); 2347 break; 2348 } 2349 } 2350 2351 memset(nonce_bin, 0, sizeof(nonce_bin)); 2352 ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, thr, 3000);//info->read_time); 2353 2354 nonce_data.chip_no = nonce_bin[NONCE_CHIP_NO_OFFSET] & RM_CHIP_MASK; 2355 if (nonce_data.chip_no >= info->rmdev.chip_max) 2356 nonce_data.chip_no = 0; 2357 nonce_data.task_no = nonce_bin[NONCE_TASK_NO_OFFSET] & 0x1; 2358 nonce_data.cmd_value = nonce_bin[NONCE_TASK_CMD_OFFSET] & RM_CMD_MASK; 2359 nonce_data.work_state = nonce_bin[NONCE_TASK_CMD_OFFSET] & RM_STATUS_MASK; 2360 2361 temp = (double)nonce_bin[NONCE_COMMAND_OFFSET]; 2362 if (temp != 128) 2363 icarus->temp = temp; 2364 2365 if (nonce_data.cmd_value == NONCE_TASK_COMPLETE_CMD) { 2366 info->rmdev.chip[nonce_data.chip_no].last_received_task_complete_time = time(NULL); 2367 if (info->g_work[nonce_data.chip_no][nonce_data.task_no]) { 2368 free_work(info->g_work[nonce_data.chip_no][nonce_data.task_no]); 2369 info->g_work[nonce_data.chip_no][nonce_data.task_no] = NULL; 2370 } 2371 goto out; 2372 } 2373 2374 if (nonce_data.cmd_value == NONCE_GET_TASK_CMD) { 2375 rock_send_task(nonce_data.chip_no, nonce_data.task_no, thr); 2376 goto out; 2377 } 2378 2379 if (ret == ICA_NONCE_TIMEOUT) 2380 rock_send_task(nonce_data.chip_no, nonce_data.task_no, thr); 2381 2382 work = info->g_work[nonce_data.chip_no][nonce_data.task_no]; 2383 if (work == NULL) 2384 goto out; 2385 2386 if (ret == ICA_NONCE_ERROR) 2387 goto out; 2388 2389 // aborted before becoming idle, get new work 2390 if (ret == ICA_NONCE_TIMEOUT || ret == ICA_NONCE_RESTART) { 2391 timersub(&tv_finish, &tv_start, &elapsed); 2392 2393 // ONLY up to just when it aborted 2394 // We didn't read a reply so we don't subtract ICARUS_READ_TIME 2395 estimate_hashes = ((double)(elapsed.tv_sec) 2396 + ((double)(elapsed.tv_usec))/((double)1000000)) / info->Hs; 2397 2398 // If some Serial-USB delay allowed the full nonce range to 2399 // complete it can't have done more than a full nonce 2400 if (unlikely(estimate_hashes > 0xffffffff)) 2401 estimate_hashes = 0xffffffff; 2402 2403 applog(LOG_DEBUG, "%s %d: no nonce = 0x%08lX hashes (%ld.%06lds)", 2404 icarus->drv->name, icarus->device_id, 2405 (long unsigned int)estimate_hashes, 2406 (long)elapsed.tv_sec, (long)elapsed.tv_usec); 2407 2408 goto out; 2409 } 2410 2411 memcpy((char *)&nonce, nonce_bin, ICARUS_READ_SIZE); 2412 nonce = htobe32(nonce); 2413 recv_time = time(NULL); 2414 if ((recv_time-info->rmdev.dev_detect_time) >= 60) { 2415 unsigned char i; 2416 info->rmdev.dev_detect_time = recv_time; 2417 for (i = 0; i < info->rmdev.chip_max; i ++) { 2418 if (info->rmdev.chip[i].error_cnt >= 12) { 2419 if (info->rmdev.chip[i].freq > info->rmdev.min_frq) 2420 info->rmdev.chip[i].freq--; 2421 } else if (info->rmdev.chip[i].error_cnt <= 1) { 2422 if (info->rmdev.chip[i].freq < (info->rmdev.def_frq / 10 - 1)) 2423 info->rmdev.chip[i].freq++; 2424 } 2425 info->rmdev.chip[i].error_cnt = 0; 2426 } 2427 } 2428 2429 correction_times = 0; 2430 info->nonces_checked++; 2431 while (correction_times < NONCE_CORRECTION_TIMES) { 2432 uint32_t new_nonce; 2433 2434 if (correction_times > 0) { 2435 info->nonces_correction_tests++; 2436 if (correction_times == 1) 2437 info->nonces_correction_times++; 2438 } 2439 new_nonce = nonce + rbox_corr_values[correction_times]; 2440 /* Basic dupe testing */ 2441 if (new_nonce == info->last_nonce[nonce_data.chip_no][nonce_data.task_no]) 2442 break; 2443 if (test_nonce(work, new_nonce)) { 2444 nonce = new_nonce; 2445 submit_tested_work(thr, work); 2446 info->last_nonce[nonce_data.chip_no][nonce_data.task_no] = nonce; 2447 info->nonces_correction[correction_times]++; 2448 hash_count++; 2449 info->failing = false; 2450 applog(LOG_DEBUG, "Rockminer nonce :::OK:::"); 2451 break; 2452 } else { 2453 applog(LOG_DEBUG, "Rockminer nonce error times = %d", correction_times); 2454 if (new_nonce == 0) 2455 break; 2456 } 2457 correction_times++; 2458 } 2459 if (correction_times >= NONCE_CORRECTION_TIMES) { 2460 inc_hw_errors(thr); 2461 info->nonces_fail++; 2462 } 2463 2464 hash_count = (hash_count * info->nonce_mask); 2465 2466 if (opt_debug || info->do_icarus_timing) 2467 timersub(&tv_finish, &tv_start, &elapsed); 2468 2469 applog(LOG_DEBUG, "%s %d: nonce = 0x%08x = 0x%08lX hashes (%ld.%06lds)", 2470 icarus->drv->name, icarus->device_id, 2471 nonce, (long unsigned int)hash_count, 2472 (long)elapsed.tv_sec, (long)elapsed.tv_usec); 2473 2474 out: 2475 2476 return hash_count; 2477 } 2478 2479 static struct api_data *icarus_api_stats(struct cgpu_info *cgpu) 2480 { 2481 struct api_data *root = NULL; 2482 struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data); 2483 char data[4096]; 2484 int i, off; 2485 size_t len; 2486 float avg; 2487 2488 // Warning, access to these is not locked - but we don't really 2489 // care since hashing performance is way more important than 2490 // locking access to displaying API debug 'stats' 2491 // If locking becomes an issue for any of them, use copy_data=true also 2492 root = api_add_int(root, "read_time", &(info->read_time), false); 2493 root = api_add_int(root, "read_time_limit", &(info->read_time_limit), false); 2494 root = api_add_double(root, "fullnonce", &(info->fullnonce), false); 2495 root = api_add_int(root, "count", &(info->count), false); 2496 root = api_add_hs(root, "Hs", &(info->Hs), false); 2497 root = api_add_double(root, "W", &(info->W), false); 2498 root = api_add_uint(root, "total_values", &(info->values), false); 2499 root = api_add_uint64(root, "range", &(info->hash_count_range), false); 2500 root = api_add_uint64(root, "history_count", &(info->history_count), false); 2501 root = api_add_timeval(root, "history_time", &(info->history_time), false); 2502 root = api_add_uint(root, "min_data_count", &(info->min_data_count), false); 2503 root = api_add_uint(root, "timing_values", &(info->history[0].values), false); 2504 root = api_add_const(root, "timing_mode", timing_mode_str(info->timing_mode), false); 2505 root = api_add_bool(root, "is_timing", &(info->do_icarus_timing), false); 2506 root = api_add_int(root, "baud", &(info->baud), false); 2507 root = api_add_int(root, "work_division", &(info->work_division), false); 2508 root = api_add_int(root, "fpga_count", &(info->fpga_count), false); 2509 2510 if (info->ident == IDENT_LIN) { 2511 root = api_add_string(root, "rock_init", info->rock_init, false); 2512 root = api_add_uint8(root, "rock_chips", &(info->rmdev.detect_chip_no), false); 2513 root = api_add_uint8(root, "rock_chip_max", &(info->rmdev.chip_max), false); 2514 root = api_add_uint8(root, "rock_prod_id", &(info->rmdev.product_id), false); 2515 root = api_add_avg(root, "rock_min_freq", &(info->rmdev.min_frq), false); 2516 root = api_add_avg(root, "rock_max_freq", &(info->rmdev.max_frq), false); 2517 root = api_add_uint64(root, "rock_check", &(info->nonces_checked), false); 2518 root = api_add_uint64(root, "rock_corr", &(info->nonces_correction_times), false); 2519 root = api_add_uint64(root, "rock_corr_tests", &(info->nonces_correction_tests), false); 2520 root = api_add_uint64(root, "rock_corr_fail", &(info->nonces_fail), false); 2521 if (info->nonces_checked <= 0) 2522 avg = 0; 2523 else 2524 avg = (float)(info->nonces_correction_tests) / (float)(info->nonces_checked); 2525 root = api_add_avg(root, "rock_corr_avg", &avg, true); 2526 data[0] = '\0'; 2527 off = 0; 2528 for (i = 0; i < NONCE_CORRECTION_TIMES; i++) { 2529 len = snprintf(data+off, sizeof(data)-off, 2530 "%s%"PRIu64, 2531 i > 0 ? "/" : "", 2532 info->nonces_correction[i]); 2533 if (len >= (sizeof(data)-off)) 2534 off = sizeof(data)-1; 2535 else { 2536 if (len > 0) 2537 off += len; 2538 } 2539 } 2540 root = api_add_string(root, "rock_corr_finds", data, true); 2541 } 2542 2543 return root; 2544 } 2545 2546 static void icarus_statline_before(char *buf, size_t bufsiz, struct cgpu_info *cgpu) 2547 { 2548 struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data); 2549 2550 if (info->ant) { 2551 if (info->compac) 2552 tailsprintf(buf, bufsiz, "%3.0fMHz", info->compac_ramp_freq); 2553 else if (info->u3) 2554 tailsprintf(buf, bufsiz, "%3.0fMHz %3dmV", opt_au3_freq, opt_au3_volt); 2555 else 2556 tailsprintf(buf, bufsiz, "%3.0fMHz", opt_anu_freq); 2557 } else if (info->ident == IDENT_CMR2 && info->cmr2_speed > 0) 2558 tailsprintf(buf, bufsiz, "%5.1fMhz", (float)(info->cmr2_speed) * ICARUS_CMR2_SPEED_FACTOR); 2559 } 2560 2561 static void icarus_shutdown(__maybe_unused struct thr_info *thr) 2562 { 2563 // TODO: ? 2564 } 2565 2566 static void icarus_identify(struct cgpu_info *cgpu) 2567 { 2568 struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data); 2569 2570 if (info->ident == IDENT_CMR2) 2571 info->flash_next_work = true; 2572 } 2573 2574 static char *icarus_set(struct cgpu_info *cgpu, char *option, char *setting, char *replybuf) 2575 { 2576 struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data); 2577 int val; 2578 2579 if (info->ident != IDENT_CMR2) { 2580 strcpy(replybuf, "no set options available"); 2581 return replybuf; 2582 } 2583 2584 if (strcasecmp(option, "help") == 0) { 2585 sprintf(replybuf, "clock: range %d-%d", 2586 ICARUS_CMR2_SPEED_MIN_INT, ICARUS_CMR2_SPEED_MAX_INT); 2587 return replybuf; 2588 } 2589 2590 if (strcasecmp(option, "clock") == 0) { 2591 if (!setting || !*setting) { 2592 sprintf(replybuf, "missing clock setting"); 2593 return replybuf; 2594 } 2595 2596 val = atoi(setting); 2597 if (val < ICARUS_CMR2_SPEED_MIN_INT || val > ICARUS_CMR2_SPEED_MAX_INT) { 2598 sprintf(replybuf, "invalid clock: '%s' valid range %d-%d", 2599 setting, 2600 ICARUS_CMR2_SPEED_MIN_INT, 2601 ICARUS_CMR2_SPEED_MAX_INT); 2602 } 2603 2604 info->cmr2_speed = CMR2_INT_TO_SPEED(val); 2605 info->speed_next_work = true; 2606 2607 return NULL; 2608 } 2609 2610 sprintf(replybuf, "Unknown option: %s", option); 2611 return replybuf; 2612 } 2613 2614 struct device_drv icarus_drv = { 2615 .drv_id = DRIVER_icarus, 2616 .dname = "Icarus", 2617 .name = "ICA", 2618 .drv_detect = icarus_detect, 2619 .hash_work = &hash_driver_work, 2620 .get_api_stats = icarus_api_stats, 2621 .get_statline_before = icarus_statline_before, 2622 .set_device = icarus_set, 2623 .identify_device = icarus_identify, 2624 .thread_prepare = icarus_prepare, 2625 .scanwork = icarus_scanwork, 2626 .thread_shutdown = icarus_shutdown, 2627 };