Professional Documents
Culture Documents
Driver BFLSC
Driver BFLSC
#include "config.h"
#include <float.h>
#include <limits.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>
#include <unistd.h>
#include "compat.h"
#include "miner.h"
#include "usbutils.h"
#include "driver-bflsc.h"
if (strstr(ver, "1.0.0"))
return BFLSC_DRV1;
if (strstr(ver, "1.2."))
return BFLSC_DRV2;
static void xlinkstr(char *xlink, size_t siz, int dev, struct bflsc_info *sc_info)
{
if (dev > 0)
snprintf(xlink, siz, " x-%d", dev);
else {
if (sc_info->sc_count > 1)
strcpy(xlink, " master");
else
*xlink = '\0';
}
}
static void bflsc_applog(struct cgpu_info *bflsc, int dev, enum usb_cmds cmd, int
amount, int err)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
char xlink[17];
p_lines = 0;
p_items = NULL;
if (!buf || !(*buf)) {
applog(LOG_DEBUG, "USB: %s%i: (%d) empty %s",
bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
return ok;
}
ptr = strdup(buf);
while (ptr && *ptr) {
p_items = cgrealloc(p_items, ++p_lines * sizeof(*p_items));
p_items[p_lines-1] = ptr;
ptr = strchr(ptr, '\n');
if (ptr)
*(ptr++) = '\0';
else {
applog(LOG_DEBUG, "USB: %s%i: (%d) missing lf(s) in %s",
bflsc->drv->name, bflsc->device_id, dev, usb_cmdname(cmd));
return ok;
}
}
ok = true;
return ok;
}
enum breakmode {
NOCOLON,
ONECOLON,
ALLCOLON // Temperature uses this
};
p_count = 0;
p_firstname = NULL;
p_fields = NULL;
p_lf = NULL;
if (!buf || !(*buf))
return ok;
ok = true;
return ok;
}
if (dev == 0) {
usb_buffer_clear(bflsc);
*stage = 1;
*sent = false;
err = usb_write(bflsc, send1, send1_len, amount, send1_cmd);
if (err < 0 || *amount < send1_len)
return err;
*sent = true;
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv1_cmd);
if (!isokerr(err, recv, *amount))
return err;
usb_buffer_clear(bflsc);
*stage = 2;
*sent = false;
err = usb_write(bflsc, send2, send2_len, amount, send2_cmd);
if (err < 0 || *amount < send2_len)
return err;
*sent = true;
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv2_cmd);
return err;
}
data.header = BFLSC_XLINKHDR;
data.deviceAddress = (uint8_t)dev;
tried = 0;
while (tried++ < 3) {
data.payloadSize = send1_len;
memcpy(data.payloadData, send1, send1_len);
len = DATAFORWARDSIZE(data);
usb_buffer_clear(bflsc);
*stage = 1;
*sent = false;
err = usb_write(bflsc, (char *)&data, len, amount, send1_cmd);
if (err < 0 || *amount < send1_len)
return err;
*sent = true;
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv1_cmd);
if (err != LIBUSB_SUCCESS)
return err;
data.payloadSize = send2_len;
memcpy(data.payloadData, send2, send2_len);
len = DATAFORWARDSIZE(data);
usb_buffer_clear(bflsc);
*stage = 2;
*sent = false;
err = usb_write(bflsc, (char *)&data, len, amount, send2_cmd);
if (err < 0 || *amount < send2_len)
return err;
*sent = true;
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv2_cmd);
if (err != LIBUSB_SUCCESS)
return err;
// x-link timeout? - try again?
if (strstr(recv, BFLSC_XTIMEOUT))
continue;
// SUCCESS - return it
break;
}
return err;
}
if (dev == 0) {
usb_buffer_clear(bflsc);
*sent = false;
err = usb_write(bflsc, send, send_len, amount, send_cmd);
if (err < 0 || *amount < send_len) {
// N.B. thus !(*sent) directly implies err < 0 or *amount <
send_len
return err;
}
*sent = true;
if (read_ok == READ_OK)
err = usb_read_ok(bflsc, recv, recv_siz, amount, recv_cmd);
else
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv_cmd);
return err;
}
data.header = BFLSC_XLINKHDR;
data.deviceAddress = (uint8_t)dev;
data.payloadSize = send_len;
memcpy(data.payloadData, send, send_len);
len = DATAFORWARDSIZE(data);
tried = 0;
while (tried++ < 3) {
usb_buffer_clear(bflsc);
*sent = false;
err = usb_write(bflsc, (char *)&data, len, amount, recv_cmd);
if (err < 0 || *amount < send_len)
return err;
*sent = true;
if (read_ok == READ_OK)
err = usb_read_ok(bflsc, recv, recv_siz, amount, recv_cmd);
else
err = usb_read_nl(bflsc, recv, recv_siz, amount, recv_cmd);
static int write_to_dev(struct cgpu_info *bflsc, int dev, char *buf, int buflen,
int *amount, enum usb_cmds cmd)
{
struct DataForwardToChain data;
int len;
/*
* The protocol is syncronous so any previous excess can be
* discarded and assumed corrupt data or failed USB transfers
*/
usb_buffer_clear(bflsc);
if (dev == 0)
return usb_write(bflsc, buf, buflen, amount, cmd);
data.header = BFLSC_XLINKHDR;
data.deviceAddress = (uint8_t)dev;
data.payloadSize = buflen;
memcpy(data.payloadData, buf, buflen);
len = DATAFORWARDSIZE(data);
// Device is gone
if (bflsc->usbinfo.nodev)
return;
mutex_lock(&bflsc->device_mutex);
err = send_recv_ss(bflsc, dev, &sent, &amount,
BFLSC_QFLUSH, BFLSC_QFLUSH_LEN, C_QUEFLUSH,
buf, sizeof(buf)-1, C_QUEFLUSHREPLY, READ_NL);
mutex_unlock(&bflsc->device_mutex);
if (!sent)
bflsc_applog(bflsc, dev, C_QUEFLUSH, amount, err);
else {
// TODO: do we care if we don't get 'OK'? (always will in normal
processing)
}
}
mutex_lock(&(bflsc->device_mutex));
*err = send_recv_ss(bflsc, dev, &readok, amount,
BFLSC_QRES, BFLSC_QRES_LEN, C_REQUESTRESULTS,
buf, bufsiz-1, C_GETRESULTS, READ_OK);
mutex_unlock(&(bflsc->device_mutex));
if (!readok) {
if (!ignore)
bflsc_applog(bflsc, dev, C_REQUESTRESULTS, *amount, *err);
return readok;
}
// TODO: does x-link bypass the other device FTDI? (I think it does)
// So no initialisation required except for the master device?
if (bflsc->usbinfo.nodev)
return;
interface = usb_interface(bflsc);
// Reset
err = usb_transfer(bflsc, FTDI_TYPE_OUT, FTDI_REQUEST_RESET,
FTDI_VALUE_RESET, interface, C_RESET);
applog(LOG_DEBUG, "%s%i: reset got err %d",
bflsc->drv->name, bflsc->device_id, err);
if (bflsc->usbinfo.nodev)
return;
usb_ftdi_set_latency(bflsc);
if (bflsc->usbinfo.nodev)
return;
if (bflsc->usbinfo.nodev)
return;
if (bflsc->usbinfo.nodev)
return;
if (bflsc->usbinfo.nodev)
return;
if (bflsc->usbinfo.nodev)
return;
if (bflsc->usbinfo.nodev)
return;
if (!bflsc->cutofftemp)
bflsc->cutofftemp = opt_bflsc_overheat;
}
mutex_lock(&(bflsc->device_mutex));
__bflsc_initialise(bflsc);
mutex_unlock(&(bflsc->device_mutex));
/*
* Kano's first dev Jalapeno output:
* DEVICE: BitFORCE SC<LF>
* FIRMWARE: 1.0.0<LF>
* ENGINES: 30<LF>
* FREQUENCY: [UNKNOWN]<LF>
* XLINK MODE: MASTER<LF>
* XLINK PRESENT: YES<LF>
* --DEVICES IN CHAIN: 0<LF>
* --CHAIN PRESENCE MASK: 00000000<LF>
* OK<LF>
*/
/*
* Don't use send_recv_ss() since we have a different receive timeout
* Also getinfo() is called multiple times if it fails anyway
*/
err = write_to_dev(bflsc, dev, BFLSC_DETAILS, BFLSC_DETAILS_LEN, &amount,
C_REQUESTDETAILS);
if (err < 0 || amount != BFLSC_DETAILS_LEN) {
applog(LOG_ERR, "%s detect (%s) send details request failed (%d:%d)",
bflsc->drv->dname, bflsc->device_path, amount, err);
return ok;
}
tmp = str_text(buf);
strncpy(sc_dev.getinfo, tmp, sizeof(sc_dev.getinfo));
sc_dev.getinfo[sizeof(sc_dev.getinfo)-1] = '\0';
free(tmp);
if (sc_info->driver_version == BFLSC_DRVUNDEF) {
applog(LOG_WARNING, "%s detect (%s) missing %s",
bflsc->drv->dname, bflsc->device_path, BFLSC_DI_FIRMWARE);
goto ne;
}
ok = true;
goto ne;
mata:
freebreakdown(&count, &firstname, &fields);
ok = false;
ne:
freetolines(&lines, &items);
return ok;
}
if (!sent) {
applog(LOG_ERR, "%s detect (%s) send identify request failed (%d:%d)",
bflsc->drv->dname, bflsc->device_path, amount, err);
goto unshin;
}
if (init_count > 0)
applog(LOG_WARNING, "%s detect (%s) init failed %d times %.2fs",
bflsc->drv->dname, bflsc->device_path, init_count,
tdiff(&init_now, &init_start));
if (err < 0) {
applog(LOG_ERR, "%s detect (%s) error identify reply (%d:%d)",
bflsc->drv->dname, bflsc->device_path, amount, err);
} else {
applog(LOG_ERR, "%s detect (%s) empty identify reply (%d)",
bflsc->drv->dname, bflsc->device_path, amount);
}
goto unshin;
}
buf[amount] = '\0';
if (unlikely(strstr(buf, BFLSC_IDENTITY))) {
if (ident_first) {
applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' trying
again ...",
bflsc->drv->dname, bflsc->device_path, buf);
ident_first = false;
goto retry;
}
applog(LOG_DEBUG, "%s detect (%s) didn't recognise '%s' on 2nd
attempt",
bflsc->drv->dname, bflsc->device_path, buf);
goto unshin;
}
int tries = 0;
while (7734) {
if (getinfo(bflsc, 0))
break;
cgsleep_ms(40);
}
switch (sc_info->driver_version) {
case BFLSC_DRV1:
sc_info->que_size = BFLSC_QUE_SIZE_V1;
sc_info->que_full_enough = BFLSC_QUE_FULL_ENOUGH_V1;
sc_info->que_watermark = BFLSC_QUE_WATERMARK_V1;
sc_info->que_low = BFLSC_QUE_LOW_V1;
sc_info->que_noncecount = QUE_NONCECOUNT_V1;
sc_info->que_fld_min = QUE_FLD_MIN_V1;
sc_info->que_fld_max = QUE_FLD_MAX_V1;
// Only Jalapeno uses 1.0.0
sc_info->flush_size = 1;
break;
case BFLSC_DRV2:
case BFLSC_DRVUNDEF:
default:
sc_info->driver_version = BFLSC_DRV2;
sc_info->que_size = BFLSC_QUE_SIZE_V2;
sc_info->que_full_enough = BFLSC_QUE_FULL_ENOUGH_V2;
sc_info->que_watermark = BFLSC_QUE_WATERMARK_V2;
sc_info->que_low = BFLSC_QUE_LOW_V2;
sc_info->que_noncecount = QUE_NONCECOUNT_V2;
sc_info->que_fld_min = QUE_FLD_MIN_V2;
sc_info->que_fld_max = QUE_FLD_MAX_V2;
// TODO: this can be reduced to total chip count
sc_info->flush_size = 16 * sc_info->sc_count;
break;
}
sc_info->scan_sleep_time = BAS_SCAN_TIME;
sc_info->results_sleep_time = BFLSC_RES_TIME;
sc_info->default_ms_work = (unsigned int)BAS_WORK_TIME;
latency = BAS_LATENCY;
sc_info->ident = usb_ident(bflsc);
if (sc_info->ident == IDENT_BMA) {
bflsc->drv->queue_full = &bflsc28_queue_full;
sc_info->scan_sleep_time = BMA_SCAN_TIME;
sc_info->default_ms_work = (unsigned int)BMA_WORK_TIME;
sc_info->results_sleep_time = BMA_RES_TIME;
}
if (latency != bflsc->usbdev->found->latency) {
bflsc->usbdev->found->latency = latency;
usb_ftdi_set_latency(bflsc);
}
if (newname) {
if (!bflsc->drv->copy)
bflsc->drv = copy_drv(bflsc->drv);
bflsc->drv->name = newname;
}
if (!add_cgpu(bflsc))
goto unshin;
update_usb_stats(bflsc);
mutex_init(&bflsc->device_mutex);
rwlock_init(&sc_info->stat_lock);
return bflsc;
unshin:
usb_uninit(bflsc);
shin:
free(bflsc->device_data);
bflsc->device_data = NULL;
if (bflsc->name != blank) {
free(bflsc->name);
bflsc->name = NULL;
}
bflsc = usb_free_cgpu(bflsc);
return NULL;
}
rd_lock(&(sc_info->stat_lock));
for (i = 0; i < sc_info->sc_count; i++) {
if (sc_info->sc_devs[i].temp1 > temp)
temp = sc_info->sc_devs[i].temp1;
if (sc_info->sc_devs[i].temp2 > temp)
temp = sc_info->sc_devs[i].temp2;
if (sc_info->sc_devs[i].vcc2 > vcc2)
vcc2 = sc_info->sc_devs[i].vcc2;
}
rd_unlock(&(sc_info->stat_lock));
bflsc_send_flush_work(bflsc, dev);
rd_lock(&bflsc->qlock);
rd_unlock(&bflsc->qlock);
if (did) {
wr_lock(&(sc_info->stat_lock));
sc_info->sc_devs[dev].flushed = true;
sc_info->sc_devs[dev].flush_id = sc_info->sc_devs[dev].result_id;
sc_info->sc_devs[dev].work_queued = 0;
wr_unlock(&(sc_info->stat_lock));
}
}
// Device is gone
if (bflsc->usbinfo.nodev)
return;
mutex_lock(&bflsc->device_mutex);
if (!sent)
bflsc_applog(bflsc, dev, C_SETVOLT, amount, err);
else {
// Don't care
}
sc_info->volt_next_stat = false;
return;
}
// Device is gone
if (bflsc->usbinfo.nodev)
return;
mutex_lock(&bflsc->device_mutex);
if (!sent)
bflsc_applog(bflsc, dev, C_SETCLOCK, amount, err);
else {
// Don't care
}
sc_info->clock_next_stat = false;
return;
}
// Device is gone
if (bflsc->usbinfo.nodev)
return;
if (!sent)
bflsc_applog(bflsc, dev, C_REQUESTFLASH, amount, err);
else {
// Don't care
}
return;
}
/* Flush and stop all work if the device reaches the thermal cutoff temp, or
* temporarily stop queueing work if it's in the throttling range. */
static void bflsc_manage_temp(struct cgpu_info *bflsc, struct bflsc_dev *sc_dev,
int dev, float temp)
{
bflsc->temp = temp;
if (bflsc->cutofftemp > 0) {
int cutoff = bflsc->cutofftemp;
int throttle = cutoff - BFLSC_TEMP_THROTTLE;
int recover = cutoff - BFLSC_TEMP_RECOVER;
if (sc_dev->overheat) {
if (temp < recover)
sc_dev->overheat = false;
} else if (temp > throttle) {
sc_dev->overheat = true;
if (temp > cutoff) {
applog(LOG_WARNING, "%s%i: temp (%.1f) hit thermal cutoff
limit %d, stopping work!",
bflsc->drv->name, bflsc->device_id, temp, cutoff);
dev_error(bflsc, REASON_DEV_THERMAL_CUTOFF);
flush_one_dev(bflsc, dev);
} else {
applog(LOG_NOTICE, "%s%i: temp (%.1f) hit thermal throttle
limit %d, throttling",
bflsc->drv->name, bflsc->device_id, temp, throttle);
}
}
}
}
// Device is gone
if (bflsc->usbinfo.nodev)
return false;
if (sc_info->volt_next_stat || sc_info->clock_next_stat) {
if (sc_info->volt_next_stat)
bflsc_set_volt(bflsc, dev);
if (sc_info->clock_next_stat)
bflsc_set_clock(bflsc, dev);
return true;
}
if (!sent) {
applog(LOG_ERR, "%s%i: Error: Request%s temp invalid/timed out (%d:
%d)",
bflsc->drv->name, bflsc->device_id, xlink, amount, err);
return false;
} else {
if (err < 0 || amount < 1) {
if (err < 0) {
applog(LOG_ERR, "%s%i: Error: Get%s temp return
invalid/timed out (%d:%d)",
bflsc->drv->name, bflsc->device_id, xlink,
amount, err);
} else {
applog(LOG_ERR, "%s%i: Error: Get%s temp returned nothing
(%d:%d)",
bflsc->drv->name, bflsc->device_id, xlink,
amount, err);
}
return false;
}
}
if (!sent) {
applog(LOG_ERR, "%s%i: Error: Request%s volts invalid/timed out (%d:
%d)",
bflsc->drv->name, bflsc->device_id, xlink, amount, err);
return false;
} else {
if (err < 0 || amount < 1) {
if (err < 0) {
applog(LOG_ERR, "%s%i: Error: Get%s volt return
invalid/timed out (%d:%d)",
bflsc->drv->name, bflsc->device_id, xlink,
amount, err);
} else {
applog(LOG_ERR, "%s%i: Error: Get%s volt returned nothing
(%d:%d)",
bflsc->drv->name, bflsc->device_id, xlink,
amount, err);
}
return false;
}
}
sc_dev = &sc_info->sc_devs[dev];
vcc1 = (float)atoi(fields[0]) / 1000.0;
vcc2 = (float)atoi(fields[1]) / 1000.0;
vmain = (float)atoi(fields[2]) / 1000.0;
if (unlikely(sc_dev->temp1_5min_av == 0))
sc_dev->temp1_5min_av = temp1;
else {
sc_dev->temp1_5min_av += temp1 * .0042;
sc_dev->temp1_5min_av /= 1.0042;
}
if (unlikely(sc_dev->temp2_5min_av == 0))
sc_dev->temp2_5min_av = temp2;
else {
sc_dev->temp2_5min_av += temp2 * .0042;
sc_dev->temp2_5min_av /= 1.0042;
}
wr_unlock(&(sc_info->stat_lock));
return true;
}
wr_lock(&bflsc->qlock);
HASH_FIND_INT(sc_info->bworks, &id, bwork);
if (likely(bwork)) {
HASH_DEL(sc_info->bworks, bwork);
work = bwork->work;
free(bwork);
}
wr_unlock(&bflsc->qlock);
return work;
}
static void process_nonces(struct cgpu_info *bflsc, int dev, char *xlink, char
*data, int count, char **fields, int *nonces)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
struct thr_info *thr = bflsc->thr[0];
struct work *work = NULL;
int8_t core = -1;
uint32_t nonce;
int i, num, x;
char *tmp;
bool res;
if (sc_info->ident == IDENT_BMA) {
unsigned int ucore;
num = atoi(fields[sc_info->que_noncecount]);
if (num != count - sc_info->que_fld_min) {
tmp = str_text(data);
applogsiz(LOG_INFO, BFLSC_APPLOGSIZ,
"%s%i:%s incorrect data count (%d) will use %d instead from
(%s)",
bflsc->drv->name, bflsc->device_id, xlink, num,
count - sc_info->que_fld_max, tmp);
free(tmp);
inc_bflsc_errors(thr, sc_info, core);
}
if (sc_info->ident == IDENT_BMA) {
int uid;
res = false;
x = 0;
for (i = sc_info->que_fld_min; i < count; i++) {
if (strlen(fields[i]) != 8) {
tmp = str_text(data);
applogsiz(LOG_INFO, BFLSC_APPLOGSIZ,
"%s%i:%s invalid nonce (%s) will try to process
anyway",
bflsc->drv->name, bflsc->device_id, xlink, tmp);
free(tmp);
}
(*nonces)++;
x++;
inc_bflsc_nonces(sc_info, core);
} else
inc_core_errors(sc_info, core);
}
wr_lock(&(sc_info->stat_lock));
if (res)
sc_info->sc_devs[dev].result_id++;
if (x > QUE_MAX_RESULTS)
x = QUE_MAX_RESULTS + 1;
(sc_info->result_size[x])++;
sc_info->sc_devs[dev].work_complete++;
sc_info->sc_devs[dev].hashes_unsent += FULLNONCE;
// If not flushed (stale)
if (!(work->devflag))
sc_info->sc_devs[dev].work_queued -= 1;
wr_unlock(&(sc_info->stat_lock));
free_work(work);
}
static int process_results(struct cgpu_info *bflsc, int dev, char *pbuf, int
*nonces, int *in_process)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
char **items, *firstname, **fields, *lf;
int que = 0, i, lines, count;
char *tmp, *tmp2, *buf;
char xlink[17];
bool res;
*nonces = 0;
*in_process = 0;
buf = strdup(pbuf);
if (!strncmp(buf, "INPROCESS", 9))
sscanf(buf, "INPROCESS:%d\n%s", in_process, pbuf);
res = tolines(bflsc, dev, buf, &lines, &items, C_GETRESULTS);
if (!res || lines < 1) {
tmp = str_text(pbuf);
applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
"%s%i:%s empty result (%s) ignored",
bflsc->drv->name, bflsc->device_id, xlink, tmp);
free(tmp);
goto arigatou;
}
que = atoi(fields[0]);
if (que != (lines - QUE_RES_LINES_MIN)) {
i = que;
// 1+ In case the last line isn't 'OK' - try to process it
que = 1 + lines - QUE_RES_LINES_MIN;
tmp = str_text(pbuf);
tmp2 = str_text(items[0]);
applogsiz(LOG_ERR, BFLSC_APPLOGSIZ,
"%s%i:%s incorrect result count %d (%s) will try %d (%s)",
bflsc->drv->name, bflsc->device_id, xlink, i, tmp2, que,
tmp);
free(tmp2);
free(tmp);
arigatou:
freetolines(&lines, &items);
free(buf);
return que;
}
cgtime(&now);
for (i = 0; i < sc_info->sc_count; i++) {
copy_time(&(sc_info->sc_devs[i].last_check_result), &now);
copy_time(&(sc_info->sc_devs[i].last_dev_result), &now);
copy_time(&(sc_info->sc_devs[i].last_nonce_result), &now);
}
if (bflsc->usbinfo.nodev)
return NULL;
dev = -1;
oldest = FLT_MAX;
cgtime(&now);
if (bflsc->usbinfo.nodev)
return NULL;
cgsleep_prepare_r(&ts_start);
if (dev == -1)
goto utsura;
cgtime(&(sc_info->sc_devs[dev].last_check_result));
utsura:
cgsleep_ms_r(&ts_start, sc_info->results_sleep_time);
}
return NULL;
}
return true;
}
bflsc_flush_work(bflsc);
sc_info->shutdown = true;
}
if (bflsc->usbinfo.nodev)
return;
bflsc_initialise(bflsc);
}
// Device is gone
if (bflsc->usbinfo.nodev)
return false;
work = get_queued(bflsc);
if (unlikely(!work)) {
mutex_unlock(&bflsc->device_mutex);
return ret;
}
memcpy(data.midState, work->midstate, MIDSTATE_BYTES);
memcpy(data.blockData, work->data + MERKLE_OFFSET, MERKLE_BYTES);
try = 0;
re_send:
err = send_recv_ds(bflsc, dev, &stage, &sent, &amount,
BFLSC_QJOB, BFLSC_QJOB_LEN, C_REQUESTQUEJOB,
C_REQUESTQUEJOBSTATUS,
(char *)&data, len, C_QUEJOB, C_QUEJOBSTATUS,
buf, sizeof(buf)-1);
mutex_unlock(&(bflsc->device_mutex));
switch (stage) {
case 1:
if (!sent) {
bflsc_applog(bflsc, dev, C_REQUESTQUEJOB, amount, err);
goto out;
} else {
// TODO: handle other errors ...
// Try twice
if (try++ < 1 && amount > 1 &&
strstr(buf, BFLSC_TIMEOUT))
goto re_send;
// Try twice
if (try++ < 1 && amount > 1 &&
strstr(buf, BFLSC_TIMEOUT))
goto re_send;
wr_lock(&(sc_info->stat_lock));
sc_info->sc_devs[dev].work_queued++;
wr_unlock(&(sc_info->stat_lock));
work->subid = dev;
ret = true;
out:
if (unlikely(!ret))
work_completed(bflsc, work);
return ret;
}
#define JP_COMMAND 0
#define JP_STREAMLENGTH 2
#define JP_SIGNATURE 4
#define JP_JOBSINARRY 5
#define JP_JOBSARRY 6
#define JP_ARRAYSIZE 45
if (bflsc->usbinfo.nodev)
return true;
wr_lock(&bflsc->qlock);
base_work = __get_queued(bflsc);
if (likely(base_work))
__work_completed(bflsc, base_work);
wr_unlock(&bflsc->qlock);
if (unlikely(!base_work))
return ret;
created = 1;
create = 9;
if (base_work->drv_rolllimit < create)
create = base_work->drv_rolllimit;
works[0] = base_work;
for (i = 1; i <= create ; i++) {
created++;
work = make_clone(base_work);
roll_work(base_work);
works[i] = work;
}
/* Create the maximum number of work items we can queue by nrolling one */
for (i = 0; i < created; i++) {
work = works[i];
memcpy(job_pack + offset, work->midstate, MIDSTATE_BYTES);
offset += MIDSTATE_BYTES;
memcpy(job_pack + offset, work->data + MERKLE_OFFSET, MERKLE_BYTES);
offset += MERKLE_BYTES;
job_pack[offset] = 0xaa; // EndOfBlock signature
offset++;
}
job_pack[offset++] = 0xfe; // EndOfWrapper
ptr = alloca(strlen(buf));
if (sscanf(buf, "OK:QUEUED %d:%s", &queued, ptr) != 2) {
applog(LOG_WARNING, "%s%d: Failed to parse queue response %s",
bflsc->drv->name, bflsc->device_id, buf);
goto out;
}
if (queued < 1 || queued > 10) {
applog(LOG_WARNING, "%s%d: Invalid queued count %d",
bflsc->drv->name, bflsc->device_id, queued);
queued = 0;
goto out;
}
for (i = 0; i < queued; i++) {
struct bflsc_work *bwork, *oldbwork;
unsigned int uid;
work = works[i];
field = Strsep(&ptr, ",");
if (!field) {
applog(LOG_WARNING, "%s%d: Ran out of queued IDs after %d of %d",
bflsc->drv->name, bflsc->device_id, i, queued);
queued = i;
goto out;
}
sscanf(field, "%04x", &uid);
bwork = cgcalloc(sizeof(struct bflsc_work), 1);
bwork->id = uid;
bwork->work = work;
wr_lock(&bflsc->qlock);
HASH_REPLACE_INT(sc_info->bworks, id, bwork, oldbwork);
if (oldbwork) {
free_work(oldbwork->work);
free(oldbwork);
}
wr_unlock(&bflsc->qlock);
sc_info->sc_devs[0].work_queued++;
}
if (queued < created)
ret = true;
out:
for (i = queued; i < created; i++) {
work = works[i];
discard_work(work);
}
return ret;
}
tried = -1;
// if something is wrong with a device try the next one available
// TODO: try them all? Add an unavailable flag to sc_devs[i] init to 0 here
first
while (++tries < 3) {
bool mandatory = false;
if (dev == -1) {
que = sc_info->que_size * 10; // 10x is certainly above the MAX
it could be
// The first device with the smallest amount queued
for (i = 0; i < sc_info->sc_count; i++) {
if (i != tried && sc_info->sc_devs[i].work_queued < que &&
!sc_info->sc_devs[i].overheat) {
dev = i;
que = sc_info->sc_devs[i].work_queued;
}
}
if (que > sc_info->que_full_enough)
dev = -1;
else if (que < sc_info->que_low)
mandatory = true;
}
rd_unlock(&(sc_info->stat_lock));
return ret;
}
// Device is gone
if (bflsc->usbinfo.nodev)
return -1;
flushed = false;
// Single lock check if any are flagged as flushed
rd_lock(&(sc_info->stat_lock));
for (dev = 0; dev < sc_info->sc_count; dev++)
flushed |= sc_info->sc_devs[dev].flushed;
rd_unlock(&(sc_info->stat_lock));
wr_lock(&(sc_info->stat_lock));
sc_info->sc_devs[dev].flushed = false;
wr_unlock(&(sc_info->stat_lock));
}
}
}
rd_lock(&sc_info->stat_lock);
old_sleep_time = sc_info->scan_sleep_time;
for (i = 0; i < sc_info->sc_count; i++) {
if (sc_info->sc_devs[i].work_queued < min_queued)
min_queued = sc_info->sc_devs[i].work_queued;
}
rd_unlock(&sc_info->stat_lock);
new_sleep_time = old_sleep_time;
/* Increase slowly but decrease quickly */
if (min_queued > sc_info->que_full_enough && old_sleep_time <
BFLSC_MAX_SLEEP)
new_sleep_time = old_sleep_time * 21 / 20;
else if (min_queued < sc_info->que_low)
new_sleep_time = old_sleep_time * 2 / 3;
wr_lock(&sc_info->stat_lock);
sc_info->scan_sleep_time = new_sleep_time;
wr_unlock(&sc_info->stat_lock);
}
}
return ret;
}
#define BFLSC_OVER_TEMP 75
/* Set the fanspeed to auto for any valid value <= BFLSC_OVER_TEMP,
* or max for any value > BFLSC_OVER_TEMP or if we don't know the temperature. */
static void bflsc_set_fanspeed(struct cgpu_info *bflsc)
{
struct bflsc_info *sc_info = (struct bflsc_info *)bflsc->device_data;
char buf[BFLSC_BUFSIZ+1];
char data[16+1];
int amount;
bool sent;
mutex_lock(&bflsc->device_mutex);
send_recv_ss(bflsc, 0, &sent, &amount,
data, strlen(data), C_SETFAN,
buf, sizeof(buf)-1, C_FANREPLY, READ_NL);
mutex_unlock(&bflsc->device_mutex);
}
// Device is gone
if (bflsc->usbinfo.nodev)
return false;
// Device is gone
if (bflsc->usbinfo.nodev)
return false;
bflsc_set_fanspeed(bflsc);
return allok;
}
static char *bflsc_set(struct cgpu_info *bflsc, char *option, char *setting, char
*replybuf)
{
struct bflsc_info *sc_info = (struct bflsc_info *)(bflsc->device_data);
int val;
if (sc_info->ident != IDENT_BMA) {
strcpy(replybuf, "no set options available");
return replybuf;
}
if (strcasecmp(option, "help") == 0) {
sprintf(replybuf, "volt: range 0-9 clock: range 0-15");
return replybuf;
}
if (strcasecmp(option, "volt") == 0) {
if (!setting || !*setting) {
sprintf(replybuf, "missing volt setting");
return replybuf;
}
val = atoi(setting);
if (val < 0 || val > 9) {
sprintf(replybuf, "invalid volt: '%s' valid range 0-9",
setting);
}
sc_info->volt_next = val;
sc_info->volt_next_stat = true;
return NULL;
}
if (strcasecmp(option, "clock") == 0) {
if (!setting || !*setting) {
sprintf(replybuf, "missing clock setting");
return replybuf;
}
val = atoi(setting);
if (val < 0 || val > 15) {
sprintf(replybuf, "invalid clock: '%s' valid range 0-15",
setting);
}
sc_info->clock_next = val;
sc_info->clock_next_stat = true;
return NULL;
}
if (bflsc->usbinfo.nodev)
return false;
bflsc_initialise(bflsc);
return true;
}
// there should be a new API function to return device info that isn't the standard
stuff
// instead of bflsc_api_stats - since the stats should really just be internal code
info
// and the new one should be UNusual device stats/extra details - like the stuff
below
buf[0] = '\0';
for (i = 0; i <= QUE_MAX_RESULTS + 1; i++)
tailsprintf(buf, sizeof(buf), "%s%"PRIu64, (i > 0) ? "/" : "", sc_info-
>result_size[i]);
root = api_add_string(root, "Result Size", buf, true);
rd_unlock(&(sc_info->stat_lock));
i = (int)(sc_info->driver_version);
root = api_add_int(root, "Driver", &i, true);
root = api_add_string(root, "Firmware", sc_info->sc_devs[0].firmware, false);
root = api_add_string(root, "Chips", sc_info->sc_devs[0].chips, false);
root = api_add_int(root, "Que Size", &(sc_info->que_size), false);
root = api_add_int(root, "Que Full", &(sc_info->que_full_enough), false);
root = api_add_int(root, "Que Watermark", &(sc_info->que_watermark), false);
root = api_add_int(root, "Que Low", &(sc_info->que_low), false);
root = api_add_escape(root, "GetInfo", sc_info->sc_devs[0].getinfo, false);
/*
else a whole lot of something like these ... etc
root = api_add_temp(root, "X-%d-Temp1", &(sc_info->temp1), false);
root = api_add_temp(root, "X-%d-Temp2", &(sc_info->temp2), false);
root = api_add_volts(root, "X-%d-Vcc1", &(sc_info->vcc1), false);
root = api_add_volts(root, "X-%d-Vcc2", &(sc_info->vcc2), false);
root = api_add_volts(root, "X-%d-Vmain", &(sc_info->vmain), false);
*/
if (sc_info->ident == IDENT_BMA) {
for (i = 0; i < 128; i += 16) {
data[0] = '\0';
off = 0;
for (j = 0; j < 16; j++) {
len = snprintf(data+off, sizeof(data)-off,
"%s%"PRIu64,
j > 0 ? " " : "",
sc_info->cortex_nonces[i+j]);
if (len >= (sizeof(data)-off))
off = sizeof(data)-1;
else {
if (len > 0)
off += len;
}
}
sprintf(buf, "Cortex %02x-%02x Nonces", i, i+15);
root = api_add_string(root, buf, data, true);
}
for (i = 0; i < 128; i += 16) {
data[0] = '\0';
off = 0;
for (j = 0; j < 16; j++) {
len = snprintf(data+off, sizeof(data)-off,
"%s%"PRIu64,
j > 0 ? " " : "",
sc_info->cortex_hw[i+j]);
if (len >= (sizeof(data)-off))
off = sizeof(data)-1;
else {
if (len > 0)
off += len;
}
}
sprintf(buf, "Cortex %02x-%02x HW Errors", i, i+15);
root = api_add_string(root, buf, data, true);
}
} else if (sc_info->que_noncecount != QUE_NONCECOUNT_V1) {
for (i = 0; i < 16; i++) {
sprintf(buf, "Core%d Nonces", i);
root = api_add_uint64(root, buf, &sc_info->core_nonces[i],
false);
}
for (i = 0; i < 16; i++) {
sprintf(buf, "Core%d HW Errors", i);
root = api_add_uint64(root, buf, &sc_info->core_hw[i], false);
}
}
return root;
}