Professional Documents
Culture Documents
Driver Icarus
Driver Icarus
/*
* Those code should be works fine with V2 and V3 bitstream of Icarus.
* Operation:
* No detection implement.
* Input: 64B = 32B midstate + 20B fill bytes + last 12 bytes of block head.
* Return: send back 32bits immediately when Icarus found a valid nonce.
* no query protocol implemented here, if no data send back in ~11.3
* seconds (full cover time on 32bit nonce range by 380MH/s speed)
* just send another work.
* Notice:
* 1. Icarus will start calculate when you push a work to them, even they
* are busy.
* 2. The 2 FPGAs on Icarus will distribute the job, one will calculate the
* 0 ~ 7FFFFFFF, another one will cover the 80000000 ~ FFFFFFFF.
* 3. It's possible for 2 FPGAs both find valid nonce in the meantime, the 2
* valid nonce will all be send back.
* 4. Icarus will stop work when: a valid nonce has been found or 32 bits
* nonce range is completely calculated.
*/
#include <float.h>
#include <limits.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <strings.h>
#include <sys/time.h>
#include <unistd.h>
#include <math.h>
#include "config.h"
#include "compat.h"
#include "miner.h"
#include "usbutils.h"
#define ICARUS_BUF_SIZE 8
// The size of a successful nonce read
#define ANT_READ_SIZE 5
#define ICARUS_READ_SIZE 4
#define ROCK_READ_SIZE 8
// TODO: USB? Different calculation? - see usbstats to work it out e.g. 1/2 of
normal send time
// or even use that number? 1/2
// #define ICARUS_READ_TIME(baud) ((double)ICARUS_READ_SIZE * (double)8.0 /
(double)(baud))
// maybe 1ms?
#define ICARUS_READ_TIME(baud) (0.001)
#define ICARUS_CMR2_TIMEOUT 1
#define CAIRNSMORE2_INTS 4
struct ICARUS_HISTORY {
struct timeval finish;
double sumXiTi;
double sumXi;
double sumTi;
double sumXi2;
uint32_t values;
uint32_t hash_count_min;
uint32_t hash_count_max;
};
#if (NONCE_CORRECTION_TIMES == 5)
static int32_t rbox_corr_values[] = {0, 1, -1, -2, -4};
#endif
#if (NONCE_CORRECTION_TIMES == 9)
static int32_t rbox_corr_values[] = {0, 1, -1, 2, -2, 3, -3, 4, -4};
#endif
#if (NONCE_CORRECTION_TIMES == 3)
static int32_t rbox_corr_values[] = {0, 1, -1};
#endif
#define ANT_QUEUE_NUM 36
typedef enum {
NONCE_DATA1_OFFSET = 0,
NONCE_DATA2_OFFSET,
NONCE_DATA3_OFFSET,
NONCE_DATA4_OFFSET,
NONCE_TASK_CMD_OFFSET,
NONCE_CHIP_NO_OFFSET,
NONCE_TASK_NO_OFFSET,
NONCE_COMMAND_OFFSET,
NONCE_MAX_OFFSET
} NONCE_OFFSET;
typedef enum {
NONCE_DATA_CMD = 0,
NONCE_TASK_COMPLETE_CMD,
NONCE_GET_TASK_CMD,
} NONCE_COMMAND;
typedef enum {
ROCKMINER_RBOX = 0,
ROCKMINER_T1,
ROCKMINER_T2,
ROCKMINER_MAX
} ROCKMINER_PRODUCT_T;
struct ICARUS_INFO {
enum sub_ident ident;
int intinfo;
int timeout;
double fullnonce;
int count;
double W;
uint32_t values;
uint64_t hash_count_range;
// icarus-options
int baud;
int work_division;
int fpga_count;
uint32_t nonce_mask;
uint8_t cmr2_speed;
bool speed_next_work;
bool flash_next_work;
int nonce_size;
bool failing;
pthread_mutex_t lock;
ROCKMINER_DEVICE_INFO rmdev;
struct work *base_work; // For when we roll work
struct work *g_work[MAX_CHIP_NUM][MAX_WORK_BUFFER_SIZE];
uint32_t last_nonce[MAX_CHIP_NUM][MAX_WORK_BUFFER_SIZE];
char rock_init[64];
uint64_t nonces_checked;
uint64_t nonces_correction_times;
uint64_t nonces_correction_tests;
uint64_t nonces_fail;
uint64_t nonces_correction[NONCE_CORRECTION_TIMES];
#define ICARUS_MIDSTATE_SIZE 32
#define ICARUS_UNUSED_SIZE 16
#define ICARUS_WORK_SIZE 12
#define ICARUS_WORK_DATA_OFFSET 64
#define ANT_UNUSED_SIZE 15
struct ICARUS_WORK {
uint8_t midstate[ICARUS_MIDSTATE_SIZE];
// These 4 bytes are for CMR2 bitstreams that handle MHz adjustment
uint8_t check;
uint8_t data;
uint8_t cmd;
uint8_t prefix;
uint8_t unused[ANT_UNUSED_SIZE];
uint8_t id; // Used only by ANT, otherwise unused by other icarus
uint8_t work[ICARUS_WORK_SIZE];
};
struct {
float freq;
uint16_t hex;
} u3freqtable[] = {
{ 100, 0x0783 },
{ 125, 0x0983 },
{ 150, 0x0b83 },
{ 175, 0x0d83 },
{ 193.75, 0x0f03 },
{ 196.88, 0x1f07 },
{ 200, 0x0782 },
{ 206.25, 0x1006 },
{ 212.5, 0x1086 },
{ 218.75, 0x1106 },
{ 225, 0x0882 },
{ 237.5, 0x1286 },
{ 243.75, 0x1306 },
{ 250, 0x0982 },
};
struct {
float freq;
uint16_t hex;
} compacfreqtable[] = {
{ 100, 0x0783 },
{ 106.25, 0x0803 },
{ 112.5, 0x0883 },
{ 118.75, 0x0903 },
{ 125, 0x0983 },
{ 131.25, 0x0a03 },
{ 137.5, 0x0a83 },
{ 143.75, 0x1687 },
{ 150, 0x0b83 },
{ 156.25, 0x0c03 },
{ 162.5, 0x0c83 },
{ 168.75, 0x1a87 },
{ 175, 0x0d83 },
{ 181.25, 0x0e83 },
{ 193.75, 0x0f03 },
{ 196.88, 0x1f07 },
{ 200, 0x0782 },
{ 206.25, 0x1006 },
{ 212.5, 0x1086 },
{ 218.75, 0x1106 },
{ 225, 0x0882 },
{ 231.25, 0x1206 },
{ 237.5, 0x1286 },
{ 243.75, 0x1306 },
{ 250, 0x0982 },
{ 256.25, 0x1406 },
{ 262.5, 0x0a02 },
{ 268.75, 0x1506 },
{ 275, 0x0a82 },
{ 281.25, 0x1606 },
{ 287.5, 0x0b02 },
{ 293.75, 0x1706 },
{ 300, 0x0b82 },
{ 306.25, 0x1806 },
{ 312.5, 0x0c02 },
{ 318.75, 0x1906 },
{ 325, 0x0c82 },
{ 331.25, 0x1a06 },
{ 337.5, 0x0d02 },
{ 343.75, 0x1b06 },
{ 350, 0x0d82 },
{ 356.25, 0x1c06 },
{ 362.5, 0x0e02 },
{ 368.75, 0x1d06 },
{ 375, 0x0e82 },
{ 381.25, 0x1e06 },
{ 387.5, 0x0f02 },
{ 393.75, 0x1f06 },
{ 400, 0x0f82 },
{ 412.5, 0x1006 },
{ 425, 0x0801 },
{ 437.5, 0x1105 },
{ 450, 0x0881 },
{ 462.5, 0x1205 },
{ 475, 0x0901 },
{ 487.5, 0x1305 },
{ 500, 0x0981 },
};
/*
#define ICA_BUFSIZ (0x200)
if (icarus->usbinfo.nodev)
return;
switch (ident) {
case IDENT_BLT:
case IDENT_LLT:
case IDENT_CMR1:
case IDENT_CMR2:
// Reset
transfer(icarus, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
FTDI_VALUE_RESET,
interface, C_RESET);
if (icarus->usbinfo.nodev)
return;
// Latency
_usb_ftdi_set_latency(icarus, info->intinfo);
if (icarus->usbinfo.nodev)
return;
if (icarus->usbinfo.nodev)
return;
if (icarus->usbinfo.nodev)
return;
if (icarus->usbinfo.nodev)
return;
if (icarus->usbinfo.nodev)
return;
if (icarus->usbinfo.nodev)
return;
if (icarus->usbinfo.nodev)
return;
if (icarus->usbinfo.nodev)
return;
// Vendor
transfer(icarus, PL2303_VENDOR_OUT, PL2303_REQUEST_VENDOR,
PL2303_VALUE_VENDOR,
interface, C_VENDOR);
break;
case IDENT_AMU:
case IDENT_ANU:
case IDENT_AU3:
case IDENT_BSC:
case IDENT_GSC:
case IDENT_LIN:
// Enable the UART
transfer(icarus, CP210X_TYPE_OUT, CP210X_REQUEST_IFC_ENABLE,
CP210X_VALUE_UART_ENABLE,
interface, C_ENABLE_UART);
if (icarus->usbinfo.nodev)
return;
if (icarus->usbinfo.nodev)
return;
#define ICA_NONCE_ERROR -1
#define ICA_NONCE_OK 0
#define ICA_NONCE_RESTART 1
#define ICA_NONCE_TIMEOUT 2
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)
{
struct ICARUS_INFO *info = (struct ICARUS_INFO *)(icarus->device_data);
int err, amt, rc;
if (icarus->usbinfo.nodev)
return ICA_NONCE_ERROR;
cgtime(tv_start);
err = usb_read_ii_timeout_cancellable(icarus, info->intinfo, (char *)buf,
info->nonce_size, &amt, read_time,
C_GETRESULTS);
cgtime(tv_finish);
rc = SECTOMS(tdiff(tv_finish, tv_start));
if (thr && thr->work_restart) {
applog(LOG_DEBUG, "Icarus Read: Work restart at %d ms", rc);
return ICA_NONCE_RESTART;
}
if (amt > 0)
applog(LOG_DEBUG, "Icarus Read: Timeout reading for %d ms", rc);
else
applog(LOG_DEBUG, "Icarus Read: No data for %d ms", rc);
return ICA_NONCE_TIMEOUT;
}
if (opt_icarus_timing == NULL)
buf[0] = '\0';
else {
ptr = opt_icarus_timing;
for (i = 0; i < this_option_offset; i++) {
comma = strchr(ptr, ',');
if (comma == NULL)
break;
ptr = comma + 1;
}
ident = usb_ident(icarus);
switch (ident) {
case IDENT_ICA:
case IDENT_AVA:
info->Hs = ICARUS_REV3_HASH_TIME;
read_count_timing = ICARUS_READ_COUNT_TIMING;
break;
case IDENT_BLT:
case IDENT_LLT:
info->Hs = LANCELOT_HASH_TIME;
read_count_timing = ICARUS_READ_COUNT_TIMING;
break;
case IDENT_AMU:
info->Hs = ASICMINERUSB_HASH_TIME;
read_count_timing = ICARUS_READ_COUNT_TIMING;
break;
case IDENT_CMR1:
info->Hs = CAIRNSMORE1_HASH_TIME;
read_count_timing = ICARUS_READ_COUNT_TIMING;
break;
case IDENT_CMR2:
info->Hs = CAIRNSMORE2_HASH_TIME;
read_count_timing = ICARUS_READ_COUNT_TIMING;
break;
case IDENT_ANU:
info->Hs = ANTMINERUSB_HASH_TIME;
read_count_timing = ANTUSB_READ_COUNT_TIMING;
break;
case IDENT_AU3:
info->Hs = ANTU3_HASH_TIME;
read_count_timing = ANTU3_READ_COUNT_TIMING;
break;
case IDENT_BSC:
case IDENT_GSC:
info->Hs = COMPAC_HASH_TIME;
read_count_timing = COMPAC_READ_COUNT_TIMING;
break;
default:
quit(1, "Icarus get_options() called with invalid %s ident=%d",
icarus->drv->name, ident);
}
info->read_time = 0;
info->read_time_limit = 0; // 0 = no limit
if (strcasecmp(buf, MODE_SHORT_STR) == 0) {
// short
info->read_time = read_count_timing;
info->timing_mode = MODE_SHORT;
info->do_icarus_timing = true;
} else if (strncasecmp(buf, MODE_SHORT_STREQ, strlen(MODE_SHORT_STREQ)) == 0)
{
// short=limit
info->read_time = read_count_timing;
info->timing_mode = MODE_SHORT;
info->do_icarus_timing = true;
info->read_time_limit = atoi(&buf[strlen(MODE_SHORT_STREQ)]);
if (info->read_time_limit < 0)
info->read_time_limit = 0;
if (info->read_time_limit > ICARUS_READ_TIME_LIMIT_MAX)
info->read_time_limit = ICARUS_READ_TIME_LIMIT_MAX;
} else if (strcasecmp(buf, MODE_LONG_STR) == 0) {
// long
info->read_time = read_count_timing;
info->timing_mode = MODE_LONG;
info->do_icarus_timing = true;
} else if (strncasecmp(buf, MODE_LONG_STREQ, strlen(MODE_LONG_STREQ)) == 0) {
// long=limit
info->read_time = read_count_timing;
info->timing_mode = MODE_LONG;
info->do_icarus_timing = true;
info->read_time_limit = atoi(&buf[strlen(MODE_LONG_STREQ)]);
if (info->read_time_limit < 0)
info->read_time_limit = 0;
if (info->read_time_limit > ICARUS_READ_TIME_LIMIT_MAX)
info->read_time_limit = ICARUS_READ_TIME_LIMIT_MAX;
} else if ((Hs = atof(buf)) != 0) {
// ns[=read_time]
info->Hs = Hs / NANOSEC;
info->fullnonce = info->Hs * (((double)0xffffffff) + 1);
info->timing_mode = MODE_VALUE;
info->do_icarus_timing = false;
} else {
// Anything else in buf just uses DEFAULT mode
info->timing_mode = MODE_DEFAULT;
info->do_icarus_timing = false;
}
info->min_data_count = MIN_DATA_COUNT;
// yes we can calculate these, but this way it's easy to see what they are
switch (work_division) {
case 1:
nonce_mask = 0xffffffff;
break;
case 2:
nonce_mask = 0x7fffffff;
break;
case 4:
nonce_mask = 0x3fffffff;
break;
case 8:
nonce_mask = 0x1fffffff;
break;
default:
quit(1, "Invalid2 icarus-options for work_division (%d) must be 1, 2, 4
or 8", work_division);
}
return nonce_mask;
}
if (opt_icarus_options == NULL)
buf[0] = '\0';
else {
ptr = opt_icarus_options;
for (i = 0; i < this_option_offset; i++) {
comma = strchr(ptr, ',');
if (comma == NULL)
break;
ptr = comma + 1;
}
ident = usb_ident(icarus);
switch (ident) {
case IDENT_ICA:
case IDENT_BLT:
case IDENT_LLT:
case IDENT_AVA:
*baud = ICARUS_IO_SPEED;
*work_division = 2;
*fpga_count = 2;
break;
case IDENT_AMU:
case IDENT_ANU:
case IDENT_AU3:
case IDENT_BSC:
case IDENT_GSC:
*baud = ICARUS_IO_SPEED;
*work_division = 1;
*fpga_count = 1;
break;
case IDENT_CMR1:
*baud = ICARUS_IO_SPEED;
*work_division = 2;
*fpga_count = 2;
break;
case IDENT_CMR2:
*baud = ICARUS_IO_SPEED;
*work_division = 1;
*fpga_count = 1;
break;
default:
quit(1, "Icarus get_options() called with invalid %s ident=%d",
icarus->drv->name, ident);
}
if (*buf) {
colon = strchr(buf, ':');
if (colon)
*(colon++) = '\0';
if (*buf) {
tmp = atoi(buf);
switch (tmp) {
case 115200:
*baud = 115200;
break;
case 57600:
*baud = 57600;
break;
default:
quit(1, "Invalid icarus-options for baud (%s) must be
115200 or 57600", buf);
}
}
if (*colon) {
tmp = atoi(colon);
if (tmp == 1 || tmp == 2 || tmp == 4 || tmp == 8) {
*work_division = tmp;
*fpga_count = tmp; // default to the same
} else {
quit(1, "Invalid icarus-options for work_division
(%s) must be 1, 2, 4 or 8", colon);
}
}
j = 0x80;
k = 0;
for (i = 0; i < len; i++) {
if (*ptr & j)
din = 1;
else
din = 0;
crcout[0] = crcin[4] ^ din;
crcout[1] = crcin[0];
crcout[2] = crcin[1] ^ crcin[4] ^ din;
crcout[3] = crcin[2];
crcout[4] = crcin[3];
j = j >> 1;
k++;
if (k == 8) {
j = 0x80;
k = 0;
ptr++;
}
memcpy(crcin, crcout, 5);
}
crc = 0;
if (crcin[4])
crc |= 0x10;
if (crcin[3])
crc |= 0x08;
if (crcin[2])
crc |= 0x04;
if (crcin[1])
crc |= 0x02;
if (crcin[0])
crc |= 0x01;
return crc;
}
if (!best_fout)
best_fout = ANT_U1_DEFFREQ;
if (!freq)
freq = ANT_U3_DEFFREQ;
do {
u3freq = u3freqtable[i].freq;
if (u3freq <= freq)
anu_freq_hex = u3freqtable[i].hex;
i++;
} while (u3freq < ANT_U3_MAXFREQ);
return anu_freq_hex;
}
if (!anu_freq_hex)
anu_freq_hex = anu_find_freqhex();
memset(cmd_buf, 0, 4);
memset(rdreg_buf, 0, 4);
cmd_buf[0] = 2 | 0x80;
cmd_buf[1] = (anu_freq_hex & 0xff00u) >> 8;
cmd_buf[2] = (anu_freq_hex & 0x00ffu);
cmd_buf[3] = crc5(cmd_buf, 27);
rdreg_buf[0] = 4 | 0x80;
rdreg_buf[1] = 0; //16-23
rdreg_buf[2] = 0x04; //8-15
rdreg_buf[3] = crc5(rdreg_buf, 27);
return true;
}
static void set_anu_volt(struct cgpu_info *icarus)
{
unsigned char voltage_data[2], cmd_buf[4];
char volt_buf[8];
int err, amount;
info->rmdev.dev_detect_time = time(NULL);
}
do {
usb_read_ii_timeout(icarus, info->intinfo, buf, 512, &amt, 100,
C_GETRESULTS);
} while (amt > 0);
}
info->ident = usb_ident(icarus);
switch (info->ident) {
case IDENT_ICA:
case IDENT_AVA:
case IDENT_BLT:
case IDENT_LLT:
case IDENT_AMU:
case IDENT_CMR1:
info->timeout = ICARUS_WAIT_TIMEOUT;
break;
case IDENT_ANU:
info->timeout = ANT_WAIT_TIMEOUT;
break;
case IDENT_AU3:
info->timeout = AU3_WAIT_TIMEOUT;
break;
case IDENT_CMR2:
if (found->intinfo_count != CAIRNSMORE2_INTS) {
quithere(1, "CMR2 Interface count (%d) isn't expected: %d",
found->intinfo_count,
CAIRNSMORE2_INTS);
}
info->timeout = ICARUS_CMR2_TIMEOUT;
cmr2_count = 0;
for (i = 0; i < CAIRNSMORE2_INTS; i++)
cmr2_ok[i] = false;
break;
default:
quit(1, "%s icarus_detect_one() invalid %s ident=%d",
icarus->drv->dname, icarus->drv->dname, info->ident);
}
info->nonce_size = ICARUS_READ_SIZE;
// For CMR2 test each USB Interface
retry:
tries = 2;
ok = false;
while (!ok && tries-- > 0) {
icarus_clear(icarus, info);
icarus_initialise(icarus, baud);
if (info->u3) {
uint16_t anu_freq_hex = anu3_find_freqhex();
set_anu_volt(icarus);
if (!set_anu_freq(icarus, info, anu_freq_hex)) {
applog(LOG_WARNING, "%s %i: Failed to set frequency, too
much overclock?",
icarus->drv->name, icarus->device_id);
continue;
}
icarus->usbdev->ident = info->ident = IDENT_AU3;
info->Hs = ANTU3_HASH_TIME;
icarus->drv->name = "AU3";
applog(LOG_DEBUG, "%s %i: Detected Antminer U3", icarus->drv-
>name,
icarus->device_id);
} else if (info->ident == IDENT_ANU && !info->u3) {
if (!set_anu_freq(icarus, info, 0)) {
applog(LOG_WARNING, "%s %i: Failed to set frequency, too
much overclock?",
icarus->drv->name, icarus->device_id);
continue;
}
}
memset(nonce_bin, 0, sizeof(nonce_bin));
ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, NULL,
300);
if (ret != ICA_NONCE_OK)
continue;
}
if (info->nonce_size == ICARUS_READ_SIZE && usb_buffer_size(icarus) ==
1) {
info->ant = true;
usb_buffer_clear(icarus);
icarus->usbdev->ident = info->ident = IDENT_ANU;
info->nonce_size = ANT_READ_SIZE;
info->Hs = ANTMINERUSB_HASH_TIME;
icarus->drv->name = "ANU";
applog(LOG_DEBUG, "%s %i: Detected Antminer U1/2/3, changing
nonce size to %d",
icarus->drv->name, icarus->device_id, ANT_READ_SIZE);
}
if (!ok) {
if (info->ident != IDENT_CMR2) {
if (info->u3)
goto unshin;
info->u3 = true;
goto retry;
}
cmr2_ok[info->intinfo] = true;
cmr2_count++;
if (info->intinfo < CAIRNSMORE2_INTS-1) {
info->intinfo++;
goto retry;
}
}
}
if (info->ident == IDENT_CMR2) {
if (cmr2_count == 0) {
applog(LOG_ERR,
"Icarus Detect: Test failed at %s: for all %d CMR2
Interfaces",
icarus->device_path, CAIRNSMORE2_INTS);
goto unshin;
}
update_usb_stats(icarus);
if (info->ident == IDENT_CMR2) {
applog(LOG_INFO, "%s %d: with %d Interface%s",
icarus->drv->name, icarus->device_id,
cmr2_count, cmr2_count > 1 ? "s" : "");
info->baud = baud;
info->work_division = work_division;
info->fpga_count = fpga_count;
info->nonce_mask = mask(work_division);
set_timing_mode(this_option_offset, icarus);
if (info->ident == IDENT_CMR2) {
int i;
for (i = info->intinfo + 1; i < icarus->usbdev->found->intinfo_count;
i++) {
struct cgpu_info *cgtmp;
struct ICARUS_INFO *intmp;
if (!cmr2_ok[i])
continue;
cgtmp = usb_copy_cgpu(icarus);
if (!cgtmp) {
applog(LOG_ERR, "%s %d: Init failed initinfo %d",
icarus->drv->name, icarus->device_id, i);
continue;
}
cgtmp->usbinfo.usbstat = USB_NOSTAT;
intmp->intinfo = i;
icarus_initialise(cgtmp, baud);
if (!add_cgpu(cgtmp)) {
usb_uninit(cgtmp);
free(intmp);
continue;
}
update_usb_stats(cgtmp);
}
}
return icarus;
unshin:
usb_uninit(icarus);
free(info);
icarus->device_data = NULL;
shin:
icarus = usb_free_cgpu(icarus);
return NULL;
}
info->ident = usb_ident(compac);
info->compac_ramp_idx = 0;
info->compac_ramp_freq = compacfreqtable[info->compac_ramp_idx].freq;
info->compac_target_freq = opt_compac_freq;
compac_freq_hex = compacfreqtable[info->compac_ramp_idx].hex;
if (add_cgpu(compac)) {
char *tmp_str = cgmalloc(50 * sizeof(char));
strncpy(tmp_str,compac->unique_id,11);
strncpy(compac->unique_id,"\0\0\0\0\0\0\0\0\0\0\0",11);
strncpy(compac->unique_id,(tmp_str)+3*sizeof(char),8);
update_usb_stats(compac);
set_timing_mode(this_option_offset, compac);
return compac;
}
}
usb_uninit(compac);
free(info);
compac->device_data = NULL;
compac = usb_free_cgpu(compac);
return NULL;
}
mutex_lock(&info->lock);
work = info->base_work;
info->base_work = NULL;
mutex_unlock(&info->lock);
if (work)
free_work(work);
}
static struct cgpu_info *rock_detect_one(struct libusb_device *dev, struct
usb_find_devices *found)
{
struct ICARUS_INFO *info;
struct timeval tv_start, tv_finish;
char *ob_hex = NULL;
tries = MAX_TRIES;
ok = false;
while (!ok && tries-- > 0) {
icarus_initialise(icarus, info->baud);
usb_buffer_clear(icarus);
err = usb_write_ii(icarus, info->intinfo,
(char *)(&workdata), sizeof(workdata), &amount,
C_SENDWORK);
if (err != LIBUSB_SUCCESS || amount != sizeof(workdata))
continue;
memset(nonce_bin, 0, sizeof(nonce_bin));
ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, NULL,
100);
newname = NULL;
switch (nonce_bin[NONCE_CHIP_NO_OFFSET] & RM_PRODUCT_MASK) {
case RM_PRODUCT_T1:
newname = "LIR"; // Rocketbox
info->rmdev.product_id = ROCKMINER_T1;
info->rmdev.chip_max = 12;
info->rmdev.min_frq = 200;
info->rmdev.def_frq = 330;
info->rmdev.max_frq = 400;
break;
#if 0
case RM_PRODUCT_T2: // what's this?
newname = "LIX";
info->rmdev.product_id = ROCKMINER_T2;
info->rmdev.chip_max = 16;
info->rmdev.min_frq = 200;
info->rmdev.def_frq = 300;
info->rmdev.max_frq = 400;
break;
#endif
case RM_PRODUCT_RBOX:
newname = "LIN"; // R-Box
info->rmdev.product_id = ROCKMINER_RBOX;
info->rmdev.chip_max = 4;
info->rmdev.min_frq = 200;
info->rmdev.def_frq = 270;
info->rmdev.max_frq = 400;
break;
default:
continue;
}
ok = true;
break;
} else {
applog(LOG_DEBUG, "detect_one gold_nonce compare error
times = %d",
correction_times);
if (tries < 0 && info->ident != IDENT_CMR2) {
applog(LOG_WARNING,
"Icarus Detect: "
"Test failed at %s: get %s, should: %s",
icarus->device_path, nonce_hex, golden_nonce);
}
if (nonce == 0)
break;
}
free(nonce_hex);
correction_times++;
}
}
if (!ok)
goto unshin;
if (newname) {
if (!icarus->drv->copy)
icarus->drv = copy_drv(icarus->drv);
icarus->drv->name = newname;
}
icarus->drv->scanwork = rock_scanwork;
icarus->drv->dname = "Rockminer";
icarus->drv->get_statline_before = &rock_statline_before;
icarus->drv->flush_work = &rock_flush;
mutex_init(&info->lock);
return icarus;
unshin:
usb_uninit(icarus);
free(info);
icarus->device_data = NULL;
shin:
icarus = usb_free_cgpu(icarus);
return NULL;
}
if (info->ant)
info->antworks = cgcalloc(sizeof(struct work *), ANT_QUEUE_NUM);
return true;
}
workdata.prefix = ICARUS_CMR2_PREFIX;
workdata.cmd = cmd;
workdata.data = data;
workdata.check = workdata.data ^ workdata.cmd ^ workdata.prefix ^
ICARUS_CMR2_CHECK;
if (info->speed_next_work) {
info->speed_next_work = false;
cmr2_command(icarus, ICARUS_CMR2_CMD_SPEED, info->cmr2_speed);
return;
}
if (info->flash_next_work) {
info->flash_next_work = false;
cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_ON);
cgsleep_ms(250);
cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH,
ICARUS_CMR2_DATA_FLASH_OFF);
cgsleep_ms(250);
cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH, ICARUS_CMR2_DATA_FLASH_ON);
cgsleep_ms(250);
cmr2_command(icarus, ICARUS_CMR2_CMD_FLASH,
ICARUS_CMR2_DATA_FLASH_OFF);
return;
}
}
info->g_work[chip_no][current_task_id] = work;
} else {
work = info->g_work[chip_no][current_task_id];
applog(LOG_DEBUG, "::resend work");
}
workdata.unused[ICARUS_UNUSED_SIZE - 3] = info->rmdev.chip[chip_no].freq;
//icarus->freq/10 - 1; ;
workdata.unused[ICARUS_UNUSED_SIZE - 2] = chip_no ;
workdata.id = 0x55;
if (opt_debug) {
ob_hex = bin2hex((void *)(work->data), 128);
applog(LOG_WARNING, "%s %d: work->data %s",
icarus->drv->name, icarus->device_id, ob_hex);
free(ob_hex);
}
if (info->g_work[chip_no][current_task_id])
{
free_work(info->g_work[chip_no][current_task_id]);
info->g_work[chip_no][current_task_id] = NULL;
}
return;
}
return;
}
cgtime(&tv_history_start);
history0 = &(info->history[0]);
if (history0->values == 0)
timeradd(tv_start, &history_sec, &(history0->finish));
Ti = (double)(elapsed->tv_sec)
+ ((double)(elapsed->tv_usec))/((double)1000000)
- ((double)ICARUS_READ_TIME(info->baud));
Xi = (double)hash_count;
history0->sumXiTi += Xi * Ti;
history0->sumXi += Xi;
history0->sumTi += Ti;
history0->sumXi2 += Xi * Xi;
history0->values++;
history0->sumXiTi += history->sumXiTi;
history0->sumXi += history->sumXi;
history0->sumTi += history->sumTi;
history0->sumXi2 += history->sumXi2;
history0->values += history->values;
info->Hs = Hs;
info->read_time = read_time;
info->fullnonce = fullnonce;
info->count = count;
info->W = W;
info->values = values;
info->hash_count_range = hash_count_range;
// Device is gone
if (icarus->usbinfo.nodev)
return -1;
elapsed.tv_sec = elapsed.tv_usec = 0;
if (info->speed_next_work || info->flash_next_work)
cmr2_commands(icarus);
if (opt_debug) {
ob_hex = bin2hex((void *)(&workdata), sizeof(workdata));
applog(LOG_DEBUG, "%s %d: sent %s",
icarus->drv->name, icarus->device_id, ob_hex);
free(ob_hex);
}
more_nonces:
/* Icarus will return nonces or nothing. If we know we have enough data
* for a response in the buffer already, there will be no usb read
* performed. */
memset(nonce_bin, 0, sizeof(nonce_bin));
ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, thr, info-
>read_time);
if (ret == ICA_NONCE_ERROR)
goto out;
hash_count = estimate_hashes;
goto out;
}
if (info->ant) {
workid = nonce_bin[4] & 0x1F;
if (info->antworks[workid])
work = info->antworks[workid];
else
goto out;
}
/* U3s return shares fast enough to use just that for hashrate
* calculation, otherwise the result is inaccurate instead. */
if (info->ant) {
info->nonces++;
if (usb_buffer_size(icarus) >= ANT_READ_SIZE)
goto more_nonces;
} else {
hash_count = (nonce & info->nonce_mask);
hash_count++;
hash_count *= info->fpga_count;
}
#if 0
// This appears to only return zero nonce values
if (usb_buffer_size(icarus) > 3) {
memcpy((char *)&nonce, icarus->usbdev->buffer, sizeof(nonce_bin));
nonce = htobe32(nonce);
applog(LOG_WARNING, "%s %d: attempting to submit 2nd nonce = 0x%08lX",
icarus->drv->name, icarus->device_id,
(long unsigned int)nonce);
curr_hw_errors = icarus->hw_errors;
submit_nonce(thr, work, nonce);
was_hw_error = (curr_hw_errors > icarus->hw_errors);
}
#endif
if (opt_debug || info->do_icarus_timing)
timersub(&tv_finish, &tv_start, &elapsed);
return hash_count;
}
int chip_no = 0;
time_t recv_time = 0;
// Device is gone
if (icarus->usbinfo.nodev)
return -1;
elapsed.tv_sec = elapsed.tv_usec = 0;
memset(nonce_bin, 0, sizeof(nonce_bin));
ret = icarus_get_nonce(icarus, nonce_bin, &tv_start, &tv_finish, thr,
3000);//info->read_time);
temp = (double)nonce_bin[NONCE_COMMAND_OFFSET];
if (temp != 128)
icarus->temp = temp;
if (nonce_data.cmd_value == NONCE_TASK_COMPLETE_CMD) {
info->rmdev.chip[nonce_data.chip_no].last_received_task_complete_time =
time(NULL);
if (info->g_work[nonce_data.chip_no][nonce_data.task_no]) {
free_work(info->g_work[nonce_data.chip_no][nonce_data.task_no]);
info->g_work[nonce_data.chip_no][nonce_data.task_no] = NULL;
}
goto out;
}
if (nonce_data.cmd_value == NONCE_GET_TASK_CMD) {
rock_send_task(nonce_data.chip_no, nonce_data.task_no, thr);
goto out;
}
if (ret == ICA_NONCE_TIMEOUT)
rock_send_task(nonce_data.chip_no, nonce_data.task_no, thr);
work = info->g_work[nonce_data.chip_no][nonce_data.task_no];
if (work == NULL)
goto out;
if (ret == ICA_NONCE_ERROR)
goto out;
goto out;
}
if (correction_times > 0) {
info->nonces_correction_tests++;
if (correction_times == 1)
info->nonces_correction_times++;
}
new_nonce = nonce + rbox_corr_values[correction_times];
/* Basic dupe testing */
if (new_nonce == info->last_nonce[nonce_data.chip_no]
[nonce_data.task_no])
break;
if (test_nonce(work, new_nonce)) {
nonce = new_nonce;
submit_tested_work(thr, work);
info->last_nonce[nonce_data.chip_no][nonce_data.task_no] = nonce;
info->nonces_correction[correction_times]++;
hash_count++;
info->failing = false;
applog(LOG_DEBUG, "Rockminer nonce :::OK:::");
break;
} else {
applog(LOG_DEBUG, "Rockminer nonce error times = %d",
correction_times);
if (new_nonce == 0)
break;
}
correction_times++;
}
if (correction_times >= NONCE_CORRECTION_TIMES) {
inc_hw_errors(thr);
info->nonces_fail++;
}
if (opt_debug || info->do_icarus_timing)
timersub(&tv_finish, &tv_start, &elapsed);
out:
return hash_count;
}
if (info->ident == IDENT_LIN) {
root = api_add_string(root, "rock_init", info->rock_init, false);
root = api_add_uint8(root, "rock_chips", &(info->rmdev.detect_chip_no),
false);
root = api_add_uint8(root, "rock_chip_max", &(info->rmdev.chip_max),
false);
root = api_add_uint8(root, "rock_prod_id", &(info->rmdev.product_id),
false);
root = api_add_avg(root, "rock_min_freq", &(info->rmdev.min_frq),
false);
root = api_add_avg(root, "rock_max_freq", &(info->rmdev.max_frq),
false);
root = api_add_uint64(root, "rock_check", &(info->nonces_checked),
false);
root = api_add_uint64(root, "rock_corr", &(info-
>nonces_correction_times), false);
root = api_add_uint64(root, "rock_corr_tests", &(info-
>nonces_correction_tests), false);
root = api_add_uint64(root, "rock_corr_fail", &(info->nonces_fail),
false);
if (info->nonces_checked <= 0)
avg = 0;
else
avg = (float)(info->nonces_correction_tests) / (float)(info-
>nonces_checked);
root = api_add_avg(root, "rock_corr_avg", &avg, true);
data[0] = '\0';
off = 0;
for (i = 0; i < NONCE_CORRECTION_TIMES; i++) {
len = snprintf(data+off, sizeof(data)-off,
"%s%"PRIu64,
i > 0 ? "/" : "",
info->nonces_correction[i]);
if (len >= (sizeof(data)-off))
off = sizeof(data)-1;
else {
if (len > 0)
off += len;
}
}
root = api_add_string(root, "rock_corr_finds", data, true);
}
return root;
}
if (info->ant) {
if (info->compac)
tailsprintf(buf, bufsiz, "%3.0fMHz", info->compac_ramp_freq);
else if (info->u3)
tailsprintf(buf, bufsiz, "%3.0fMHz %3dmV", opt_au3_freq,
opt_au3_volt);
else
tailsprintf(buf, bufsiz, "%3.0fMHz", opt_anu_freq);
} else if (info->ident == IDENT_CMR2 && info->cmr2_speed > 0)
tailsprintf(buf, bufsiz, "%5.1fMhz", (float)(info->cmr2_speed) *
ICARUS_CMR2_SPEED_FACTOR);
}
if (info->ident == IDENT_CMR2)
info->flash_next_work = true;
}
static char *icarus_set(struct cgpu_info *cgpu, char *option, char *setting, char
*replybuf)
{
struct ICARUS_INFO *info = (struct ICARUS_INFO *)(cgpu->device_data);
int val;
if (info->ident != IDENT_CMR2) {
strcpy(replybuf, "no set options available");
return replybuf;
}
if (strcasecmp(option, "help") == 0) {
sprintf(replybuf, "clock: range %d-%d",
ICARUS_CMR2_SPEED_MIN_INT, ICARUS_CMR2_SPEED_MAX_INT);
return replybuf;
}
if (strcasecmp(option, "clock") == 0) {
if (!setting || !*setting) {
sprintf(replybuf, "missing clock setting");
return replybuf;
}
val = atoi(setting);
if (val < ICARUS_CMR2_SPEED_MIN_INT || val > ICARUS_CMR2_SPEED_MAX_INT)
{
sprintf(replybuf, "invalid clock: '%s' valid range %d-%d",
setting,
ICARUS_CMR2_SPEED_MIN_INT,
ICARUS_CMR2_SPEED_MAX_INT);
}
info->cmr2_speed = CMR2_INT_TO_SPEED(val);
info->speed_next_work = true;
return NULL;
}