Professional Documents
Culture Documents
#include "config.h"
#include "compat.h"
#include "miner.h"
#include "sha2.h"
#include "klist.h"
#include <ctype.h>
/*
* Tested on RPi running both Raspbian and Arch
* with BlackArrow BitFury V1 & V2 GPIO Controller
* with 16 chip BlackArrow BitFury boards
*/
#ifndef LINUX
static void bab_detect(__maybe_unused bool hotplug)
{
}
#else
#include <unistd.h>
#include <linux/spi/spidev.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#define BAB_SPI_BUS 0
#define BAB_SPI_CHIP 0
#define BAB_DELAY_USECS 0
#define BAB_TRF_DELAY 0
#define BAB_INP_GPIO(_n) BAB_ADDR((_n) / 10) &= (~(7 << (((_n) % 10) * 3)))
#define BAB_OUT_GPIO(_n) BAB_ADDR((_n) / 10) |= (1 << (((_n) % 10) * 3))
#define BAB_OUT_GPIO_V(_n, _v) BAB_ADDR((_n) / 10) |= (((_v) <= 3 ? (_v) + 4 : \
((_v) == 4 ? 3 : 2)) << (((_n) % 10) * 3))
#define BAB_NOOP 0
#define BAB_BREAK ((uint8_t *)"\04")
#define BAB_ASYNC ((uint8_t *)"\05")
#define BAB_SYNC ((uint8_t *)"\06")
#define BAB_ADD_MIN 4
#define BAB_ADD_MAX 128
#define BAB_BASEA 4
#define BAB_BASEB 61
#define BAB_COUNTERS 16
static const uint8_t bab_counters[BAB_COUNTERS] = {
64, 64,
BAB_BASEA, BAB_BASEA+4,
BAB_BASEA+2, BAB_BASEA+2+16,
BAB_BASEA, BAB_BASEA+1,
(BAB_BASEB)%65, (BAB_BASEB+1)%65,
(BAB_BASEB+3)%65, (BAB_BASEB+3+16)%65,
(BAB_BASEB+4)%65, (BAB_BASEB+4+4)%65,
(BAB_BASEB+3+3)%65, (BAB_BASEB+3+1+3)%65
};
#define BAB_W1 16
static const uint32_t bab_w1[BAB_W1] = {
0, 0, 0, 0xffffffff,
0x80000000, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0x00000280
};
#define BAB_W2 8
static const uint32_t bab_w2[BAB_W2] = {
0x80000000, 0, 0, 0,
0, 0, 0, 0x00000100
};
#define BAB_TEST_DATA 19
static const uint32_t bab_test_data[BAB_TEST_DATA] = {
0xb0e72d8e, 0x1dc5b862, 0xe9e7c4a6, 0x3050f1f5,
0x8a1a6b7e, 0x7ec384e8, 0x42c1c3fc, 0x8ed158a1,
0x8a1a6b7e, 0x6f484872, 0x4ff0bb9b, 0x12c97f07,
0xb0e72d8e, 0x55d979bc, 0x39403296, 0x40f09e84,
0x8a0bb7b7, 0x33af304f, 0x0b290c1a //, 0xf0c4e61f
};
/*
* maximum chip speed available for auto tuner
* speed/nrate/hrate/watt
* 53/ 97/ 100/ 84
* 54/ 98/ 107/ 88
* 55/ 99/ 115/ 93
* 56/ 101/ 125/ 99
*/
#define BAB_MAXSPEED 57
#define BAB_DEFMAXSPEED 55
#define BAB_DEFSPEED 53
#define BAB_MINSPEED 52
#define BAB_ABSMINSPEED 32
/*
* % of errors to tune the speed up or down
* 1.0 to 10.0 should average around 5.5% errors
*/
#define BAB_TUNEUP 1.0
#define BAB_TUNEDOWN 10.0
#define MIDSTATE_BYTES 32
#define MERKLE_OFFSET 64
#define MERKLE_BYTES 12
#define BLOCK_HEADER_BYTES 80
// Auto adjust
#define BAB_AUTO_REG 0
#define BAB_AUTO_VAL 0x01
// iclk
#define BAB_ICLK_REG 1
#define BAB_ICLK_VAL 0x02
// No fast clock
#define BAB_FAST_REG 2
#define BAB_FAST_VAL 0x04
// Divide by 2
#define BAB_DIV2_REG 3
#define BAB_DIV2_VAL 0x08
// Slow Clock
#define BAB_SLOW_REG 4
#define BAB_SLOW_VAL 0x10
// No oclk
#define BAB_OCLK_REG 6
#define BAB_OCLK_VAL 0x20
// Has configured
#define BAB_CFGD_VAL 0x40
#define BAB_REG_CLR_FROM 7
#define BAB_REG_CLR_TO 11
/*
* valid: 0x01 0x03 0x07 0x0F 0x1F 0x3F 0x7F 0xFF
* max { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0x00 }
* max { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x00 }
* avg { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x00, 0x00 }
* slo { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x00 }
* min { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
* good: 0x1F (97) 0x3F (104) 0x7F (109) 0xFF (104)
*/
#define BAB_OSC 8
static const uint8_t bab_osc_bits[BAB_OSC] =
{ 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF };
#define BAB_NONCE_OFFSETS 3
#define BAB_OFF_0x1C_STA 2
#define BAB_OFF_0x1C_FIN 2
#define BAB_OFF_OTHER_STA 0
#define BAB_OFF_OTHER_FIN 1
struct bab_work_send {
uint32_t midstate[MIDSTATE_UINTS];
uint32_t ms3steps[MIDSTATE_UINTS];
uint32_t merkle7;
uint32_t ntime;
uint32_t bits;
};
#define BAB_REPLY_NONCES 16
struct bab_work_reply {
uint32_t nonce[BAB_REPLY_NONCES];
uint32_t jobsel;
uint32_t spichk;
};
// Work
typedef struct witem {
struct work *work;
struct bab_work_send chip_input;
bool ci_setup;
bool rolled;
int nonces;
struct timeval work_start;
} WITEM;
#define ALLOC_SITEMS 8
#define LIMIT_SITEMS 0
// SPI I/O
typedef struct sitem {
uint32_t siz;
uint8_t wbuf[BAB_MAXBUF];
uint8_t rbuf[BAB_MAXBUF];
uint32_t chip_off[BAB_MAXCHIPS+1];
uint32_t bank_off[BAB_MAXBANKS+2];
// WITEMs used to build the work
K_ITEM *witems[BAB_MAXCHIPS];
struct timeval work_start;
} SITEM;
// Results
typedef struct ritem {
int chip;
int nonces;
uint32_t nonce[BAB_REPLY_NONCES];
bool not_first_reply;
struct timeval when;
} RITEM;
// Nonce History
typedef struct nitem {
struct timeval found;
} NITEM;
#define BAB_CHIP_SPEEDS 6
// less than or equal GH/s
static double chip_speed_ranges[BAB_CHIP_SPEEDS - 1] =
{ 0.0, 0.8, 1.6, 2.2, 2.8 };
// Greater than the last one above means it's the last speed
static char *chip_speed_names[BAB_CHIP_SPEEDS] =
{ "Bad", "V.Slow", "Slow", "OK", "Good", "Fast" };
/*
* This is required to do chip tuning
* If disabled, it will simply run the chips at default speed
* unless they never return valid results
*/
#define UPDATE_HISTORY 1
struct bab_info {
struct thr_info spi_thr;
struct thr_info res_thr;
pthread_mutex_t did_lock;
pthread_mutex_t nonce_lock;
int version;
int spifd;
int chips;
int chips_per_bank[BAB_MAXBANKS+1];
int missing_chips_per_bank[BAB_MAXBANKS+1];
int bank_first_chip[BAB_MAXBANKS+1];
int bank_last_chip[BAB_MAXBANKS+1];
int boards;
int banks;
uint32_t chip_spis[BAB_MAXCHIPS+1];
int reply_wait;
uint64_t reply_waits;
cgsem_t scan_work;
cgsem_t spi_work;
cgsem_t spi_reply;
cgsem_t process_reply;
bool disabled[BAB_MAXCHIPS];
int total_disabled;
uint8_t chip_fast[BAB_MAXCHIPS];
uint8_t chip_conf[BAB_MAXCHIPS];
uint8_t old_fast[BAB_MAXCHIPS];
uint8_t old_conf[BAB_MAXCHIPS];
uint8_t chip_bank[BAB_MAXCHIPS+1];
uint8_t osc[BAB_OSC];
/*
* Ignore errors in the first work reply since
* they may be from a previous run or random junk
* There can be >100 with just one 16 chip board
*/
uint32_t initial_ignored;
bool not_first_reply[BAB_MAXCHIPS];
// Stats
uint64_t core_good[BAB_MAXCHIPS][BAB_CORES];
uint64_t core_bad[BAB_MAXCHIPS][BAB_CORES];
uint64_t chip_spie[BAB_MAXCHIPS]; // spi errors
uint64_t chip_miso[BAB_MAXCHIPS]; // msio errors
uint64_t chip_nonces[BAB_MAXCHIPS];
uint64_t chip_good[BAB_MAXCHIPS];
uint64_t chip_bad[BAB_MAXCHIPS];
uint64_t chip_ncore[BAB_MAXCHIPS][BAB_X_COORD][BAB_Y_COORD];
uint64_t chip_cont_bad[BAB_MAXCHIPS];
uint64_t chip_max_bad[BAB_MAXCHIPS];
uint64_t discarded_e0s;
uint64_t untested_nonces;
uint64_t tested_nonces;
uint64_t new_nonces;
uint64_t ok_nonces;
uint64_t nonce_offset_count[BAB_NONCE_OFFSETS];
uint64_t total_tests;
uint64_t max_tests_per_nonce;
uint64_t total_links;
uint64_t total_proc_links;
uint64_t max_links;
uint64_t max_proc_links;
uint64_t total_work_links;
uint64_t fail;
uint64_t fail_total_tests;
uint64_t fail_total_links;
uint64_t fail_total_work_links;
uint64_t ign_total_tests;
uint64_t ign_total_links;
uint64_t ign_total_work_links;
uint64_t send_count;
double send_total;
double send_min;
double send_max;
// Work
K_LIST *wfree_list;
K_STORE *available_work;
K_STORE *chip_work[BAB_MAXCHIPS];
// SPI I/O
K_LIST *sfree_list;
// Waiting to send
K_STORE *spi_list;
// Sent
K_STORE *spi_sent;
// Results
K_LIST *rfree_list;
K_STORE *res_list;
// Nonce History
K_LIST *nfree_list;
K_STORE *good_nonces[BAB_MAXCHIPS];
K_STORE *bad_nonces[BAB_MAXCHIPS];
bool initialised;
};
/*
* Amount of time for history
* Older items in nonce_history are discarded
* 300s / 5 minutes
*/
#define HISTORY_TIME_S 300
/*
* If the SPI I/O thread waits longer than this long for work
* it will report an error saying how long it's waiting
* and again every BAB_STD_WAIT_mS after that
*/
#define BAB_LONG_uS 1200000
/*
* If work wasn't available early enough,
* report every BAB_LONG_WAIT_mS until it is
*/
#define BAB_LONG_WAIT_mS 888
/*
* Some amount of time to wait for work
* before checking how long we've waited
*/
#define BAB_STD_WAIT_mS 888
/*
* How long to wait for the ioctl() to complete (per BANK)
* This is a failsafe in case the ioctl() fails
* since bab_txrx() will already post a wakeup when it completes
* V1 is set to this x 2
* V2 is set to this x active banks
*/
#define BAB_REPLY_WAIT_mS 160
/*
* Work items older than this should not expect results
* It has to allow for the result buffer returned with the next result
* 0.75GH/s takes 5.727s to do a full nonce range
* If HW is too high, consider increasing this to see if work is being
* expired too early (due to slow chips)
*/
#define BAB_WORK_EXPIRE_mS 7800
/*
* If a chip only has bad results after this time limit in seconds,
* then switch it down to min_speed
*/
#define BAB_BAD_TO_MIN (HISTORY_TIME_S + 10)
/*
* Also, just to be sure it's actually mining, it must have got this
* many bad results before considering disabling it
*/
#define BAB_BAD_COUNT 100
/*
* If a chip only has bad results after this time limit in seconds,
* then disable it
* A chip only returning bad results will use a lot more CPU than
* an ok chip since all results will be tested against all unexpired
* work that's been sent to the chip
*/
#define BAB_BAD_DEAD (BAB_BAD_TO_MIN * 2)
/*
* Maximum bab_queue_full() will roll work if it is allowed to
* Since work can somtimes (rarely) queue up with many chips,
* limit it to avoid it getting too much range in the pending work
*/
#define BAB_MAX_ROLLTIME 42
a = p[0];
b = p[1];
c = p[2];
d = p[3];
e = p[4];
f = p[5];
g = p[6];
h = p[7];
for (i = 0; i < 3; i++) {
new_e = p[i+16] + sha256_k[i] + h + CH(e,f,g) + SHA256_F2(e) + d;
new_a = p[i+16] + sha256_k[i] + h + CH(e,f,g) + SHA256_F2(e) +
SHA256_F1(a) + MAJ(a,b,c);
d = c;
c = b;
b = a;
a = new_a;
h = g;
g = f;
f = e;
e = new_e;
}
p[15] = a;
p[14] = b;
p[13] = c;
p[12] = d;
p[11] = e;
p[10] = f;
p[9] = g;
p[8] = h;
}
/* Byte reversal */
in = (((in & 0xaaaaaaaa) >> 1) | ((in & 0x55555555) << 1));
in = (((in & 0xcccccccc) >> 2) | ((in & 0x33333333) << 2));
in = (((in & 0xf0f0f0f0) >> 4) | ((in & 0x0f0f0f0f) << 4));
/* Extraction */
if (in & 1)
out |= (1 << 23);
if (in & 2)
out |= (1 << 22);
out -= 0x800004;
return out;
}
cgtime(&now);
K_WLOCK(babinfo->chip_work[chip]);
tail = babinfo->chip_work[chip]->tail;
expired_item = false;
// Discard expired work
while (tail) {
if (ms_tdiff(&now, &(DATAW(tail)->work_start)) < BAB_WORK_EXPIRE_mS)
break;
if (tail == witem)
expired_item = true;
k_unlink_item(babinfo->chip_work[chip], tail);
K_WUNLOCK(babinfo->chip_work[chip]);
if (DATAW(tail)->rolled)
free_work(DATAW(tail)->work);
else
work_completed(babcgpu, DATAW(tail)->work);
K_WLOCK(babinfo->chip_work[chip]);
k_add_head(babinfo->wfree_list, tail);
tail = babinfo->chip_work[chip]->tail;
}
// If we didn't expire witem, then remove all older than it
if (!expired_item && witem && witem->next) {
tail = babinfo->chip_work[chip]->tail;
while (tail && tail != witem) {
k_unlink_item(babinfo->chip_work[chip], tail);
K_WUNLOCK(babinfo->chip_work[chip]);
if (DATAW(tail)->rolled)
free_work(DATAW(tail)->work);
else
work_completed(babcgpu, DATAW(tail)->work);
K_WLOCK(babinfo->chip_work[chip]);
k_add_head(babinfo->wfree_list, tail);
tail = babinfo->chip_work[chip]->tail;
}
}
K_WUNLOCK(babinfo->chip_work[chip]);
}
BAB_INP_GPIO(10);
BAB_OUT_GPIO(10);
BAB_INP_GPIO(11);
BAB_OUT_GPIO(11);
if (bank) {
for (i = 0; i < BAB_MAXBANKS; i++) {
BAB_INP_GPIO(banks[i]);
BAB_OUT_GPIO(banks[i]);
if (bank == i+1)
BAB_GPIO_SET = 1 << banks[i];
else
BAB_GPIO_CLR = 1 << banks[i];
}
cgsleep_us(4096);
} else {
for (i = 0; i < BAB_MAXBANKS; i++)
BAB_INP_GPIO(banks[i]);
}
wbuf = (uintptr_t)(DATAS(item)->wbuf);
rbuf = (uintptr_t)(DATAS(item)->rbuf);
siz = (uint32_t)(DATAS(item)->siz);
memset(&tran, 0, sizeof(tran));
tran.speed_hz = babinfo->speed_hz;
tran.delay_usecs = babinfo->delay_usecs;
i = 0;
pos = 0;
for (bank = 0; bank <= BAB_MAXBANKS; bank++) {
if (DATAS(item)->bank_off[bank]) {
bab_reset(bank, 64);
break;
}
}
count = 0;
while (siz > 0) {
tran.tx_buf = wbuf;
tran.rx_buf = rbuf;
tran.speed_hz = BAB_SPI_SPEED;
if (pos == DATAS(item)->bank_off[bank]) {
for (; ++bank <= BAB_MAXBANKS; ) {
if (DATAS(item)->bank_off[bank] > pos) {
bab_reset(bank, 64);
break;
}
}
}
if (siz < BAB_SPI_BUFSIZ)
tran.len = siz;
else
tran.len = BAB_SPI_BUFSIZ;
if (pos < DATAS(item)->bank_off[bank] &&
DATAS(item)->bank_off[bank] < (pos + tran.len))
tran.len = DATAS(item)->bank_off[bank] - pos;
if (unlikely(babinfo->chip_spis[i] == BAB_SPI_SPEED)) {
applog(LOG_DEBUG, "%s%d: %s() chip[%d] speed %d shouldn't be %d"
BAB_FFL,
babcgpu->drv->name, babcgpu->device_id,
__func__, i, (int)babinfo->chip_spis[i],
BAB_SPI_SPEED, BAB_FFL_PASS);
}
if (unlikely(tran.speed_hz == BAB_SPI_SPEED)) {
applog(LOG_DEBUG, "%s%d: %s() transfer speed %d shouldn't be %d"
BAB_FFL,
babcgpu->drv->name, babcgpu->device_id,
__func__, (int)tran.speed_hz,
BAB_SPI_SPEED, BAB_FFL_PASS);
}
count++;
if (ioctl(babinfo->spifd, SPI_IOC_MESSAGE(1), (void *)&tran) < 0) {
if (!detect_ignore || errno != 110) {
for (bank = BAB_MAXBANKS; bank >= 0; bank--) {
if (DATAS(item)->bank_off[bank] &&
pos >= DATAS(item)->bank_off[bank]) {
break;
}
}
for (chip1 = babinfo->chips-1; chip1 >= 0; chip1--) {
if (DATAS(item)->chip_off[chip1] &&
pos >= DATAS(item)->chip_off[chip1]) {
break;
}
}
for (chip2 = babinfo->chips-1; chip2 >= 0; chip2--) {
if (DATAS(item)->chip_off[chip2] &&
(pos + tran.len) >= DATAS(item)->chip_off[chip2])
{
break;
}
}
applog(LOG_ERR, "%s%d: ioctl (%d) siz=%d bank=%d chip=%d-
%d"
" failed err=%d" BAB_FFL,
babcgpu->drv->name,
babcgpu->device_id,
count, (int)(tran.len),
bank, chip1, chip2,
errno, BAB_FFL_PASS);
}
return false;
}
siz -= tran.len;
wbuf += tran.len;
rbuf += tran.len;
pos += tran.len;
static void _bab_add_buf_rev(K_ITEM *item, const uint8_t *data, uint32_t siz, const
char *file, const char *func, const int line)
{
uint32_t now_used, i;
uint8_t tmp;
now_used = DATAS(item)->siz;
if (now_used + siz >= BAB_MAXBUF) {
quitfrom(1, file, func, line,
"%s() buffer limit of %d exceeded=%d siz=%d",
__func__, BAB_MAXBUF, (int)(now_used + siz), (int)siz);
}
DATAS(item)->siz += siz;
}
static void _bab_add_buf(K_ITEM *item, const uint8_t *data, size_t siz, const char
*file, const char *func, const int line)
{
uint32_t now_used;
now_used = DATAS(item)->siz;
if (now_used + siz >= BAB_MAXBUF) {
quitfrom(1, file, func, line,
"%s() DATAS buffer limit of %d exceeded=%d siz=%d",
__func__, BAB_MAXBUF, (int)(now_used + siz), (int)siz);
}
static void _bab_add_noops(K_ITEM *item, size_t siz, const char *file, const char
*func, const int line)
{
uint32_t now_used;
now_used = DATAS(item)->siz;
if (now_used + siz >= BAB_MAXBUF) {
quitfrom(1, file, func, line,
"%s() DATAS buffer limit of %d exceeded=%d siz=%d",
__func__, BAB_MAXBUF, (int)(now_used + siz), (int)siz);
}
static void _bab_add_data(K_ITEM *item, uint32_t addr, const uint8_t *data, size_t
siz, const char *file, const char *func, const int line)
{
uint8_t tmp[3];
int trf_siz;
static void _bab_config_reg(K_ITEM *item, uint32_t reg, bool enable, const char
*file, const char *func, const int line)
{
if (enable) {
_bab_add_data(item, BAB_REG_ADDR + reg*32,
bab_reg_ena, sizeof(bab_reg_ena), BAB_FFL_PASS);
} else {
_bab_add_data(item, BAB_REG_ADDR + reg*32,
bab_reg_dis, sizeof(bab_reg_dis), BAB_FFL_PASS);
}
for (i = 0; i < BAB_OSC && fast > BAB_OSC; i++, fast -= BAB_OSC) {
babinfo->osc[i] = 0xff;
}
if (i < BAB_OSC && fast > 0 && fast <= BAB_OSC)
babinfo->osc[i++] = bab_osc_bits[fast - 1];
for (; i < BAB_OSC; i++)
babinfo->osc[i] = 0x00;
BAB_ADD_BREAK(sitem);
for (i = 0; i < babinfo->chips; i++) {
if (babinfo->chip_bank[i] != bank) {
DATAS(sitem)->bank_off[bank] = DATAS(sitem)->siz;
bank = babinfo->chip_bank[i];
BAB_ADD_BREAK(sitem);
}
if (!(babinfo->disabled[i])) {
if (BAB_CFGD_SET(babinfo->chip_conf[i]) || !babinfo-
>chip_conf[i]) {
bab_set_osc(babinfo, i);
bab_add_data(sitem, BAB_OSC_ADDR, babinfo->osc,
sizeof(babinfo->osc));
bab_config_reg(sitem, BAB_ICLK_REG, BAB_ICLK_BIT(babinfo-
>chip_conf[i]));
bab_config_reg(sitem, BAB_FAST_REG, BAB_FAST_BIT(babinfo-
>chip_conf[i]));
bab_config_reg(sitem, BAB_DIV2_REG, BAB_DIV2_BIT(babinfo-
>chip_conf[i]));
bab_config_reg(sitem, BAB_SLOW_REG, BAB_SLOW_BIT(babinfo-
>chip_conf[i]));
bab_config_reg(sitem, BAB_OCLK_REG, BAB_OCLK_BIT(babinfo-
>chip_conf[i]));
for (reg = BAB_REG_CLR_FROM; reg <= BAB_REG_CLR_TO; reg++)
bab_config_reg(sitem, reg, false);
if (babinfo->chip_conf[i]) {
bab_add_data(sitem, BAB_COUNT_ADDR, bab_counters,
sizeof(bab_counters));
bab_add_data(sitem, BAB_W1A_ADDR, bab_w1,
sizeof(bab_w1));
bab_add_data(sitem, BAB_W1B_ADDR, bab_w1,
sizeof(bab_w1)/2);
bab_add_data(sitem, BAB_W2_ADDR, bab_w2,
sizeof(bab_w2));
babinfo->chip_conf[i] ^= BAB_CFGD_VAL;
}
babinfo->old_fast[i] = babinfo->chip_fast[i];
babinfo->old_conf[i] = babinfo->chip_conf[i];
} else {
if (babinfo->old_fast[i] != babinfo->chip_fast[i]) {
bab_set_osc(babinfo, i);
bab_add_data(sitem, BAB_OSC_ADDR, babinfo->osc,
sizeof(babinfo->osc));
babinfo->old_fast[i] = babinfo->chip_fast[i];
}
if (babinfo->old_conf[i] != babinfo->chip_conf[i]) {
if (BAB_ICLK_SET(babinfo->old_conf[i]) !=
BAB_ICLK_SET(babinfo->chip_conf[i]))
bab_config_reg(sitem, BAB_ICLK_REG,
BAB_ICLK_BIT(babinfo-
>chip_conf[i]));
if (BAB_FAST_SET(babinfo->old_conf[i]) !=
BAB_FAST_SET(babinfo->chip_conf[i]))
bab_config_reg(sitem, BAB_FAST_REG,
BAB_FAST_BIT(babinfo-
>chip_conf[i]));
if (BAB_DIV2_SET(babinfo->old_conf[i]) !=
BAB_DIV2_SET(babinfo->chip_conf[i]))
bab_config_reg(sitem, BAB_DIV2_REG,
BAB_DIV2_BIT(babinfo-
>chip_conf[i]));
if (BAB_SLOW_SET(babinfo->old_conf[i]) !=
BAB_SLOW_SET(babinfo->chip_conf[i]))
bab_config_reg(sitem, BAB_SLOW_REG,
BAB_SLOW_BIT(babinfo-
>chip_conf[i]));
if (BAB_OCLK_SET(babinfo->old_conf[i]) !=
BAB_OCLK_SET(babinfo->chip_conf[i]))
bab_config_reg(sitem, BAB_OCLK_REG,
BAB_OCLK_BIT(babinfo-
>chip_conf[i]));
babinfo->old_conf[i] = babinfo->chip_conf[i];
}
}
DATAS(sitem)->chip_off[i] = DATAS(sitem)->siz + 3;
chip_input = &(DATAW(DATAS(sitem)->witems[i])->chip_input);
if (babinfo->chip_conf[i])
bab_add_data(sitem, BAB_INP_ADDR, (uint8_t *)chip_input,
sizeof(*chip_input));
K_WLOCK(babinfo->spi_list);
k_add_head(babinfo->spi_list, sitem);
K_WUNLOCK(babinfo->spi_list);
cgsem_post(&(babinfo->spi_work));
}
static bool bab_get(__maybe_unused struct cgpu_info *babcgpu, struct bab_info
*babinfo, struct timeval *when)
{
K_ITEM *item;
bool delayed;
int i;
item = NULL;
delayed = false;
while (item == NULL) {
cgsem_mswait(&(babinfo->spi_reply), babinfo->reply_wait);
K_WLOCK(babinfo->spi_sent);
item = k_unlink_tail(babinfo->spi_sent);
K_WUNLOCK(babinfo->spi_sent);
if (!item) {
if (!delayed) {
applog(LOG_WARNING, "%s%d: Delay getting work reply ...",
babcgpu->drv->name,
babcgpu->device_id);
delayed = true;
babinfo->reply_waits++;
}
}
}
K_WLOCK(babinfo->sfree_list);
k_add_head(babinfo->sfree_list, item);
K_WUNLOCK(babinfo->sfree_list);
return true;
}
memset(item->data, 0, babinfo->sfree_list->siz);
BAB_ADD_BREAK(item);
for (i = first; i < last && i < BAB_MAXCHIPS; i++) {
DATAS(item)->chip_off[i] = DATAS(item)->siz + 3;
bab_add_data(item, BAB_INP_ADDR, bab_test_data, sizeof(bab_test_data));
BAB_ADD_ASYNC(item);
}
DATAS(item)->chip_off[i] = DATAS(item)->siz;
DATAS(item)->bank_off[bank] = DATAS(item)->siz;
babinfo->chips = i;
bab_txrx(item, true);
DATAS(item)->siz = 0;
babinfo->chips = first;
for (i = first; i < last && i < BAB_MAXCHIPS; i++) {
uint32_t tmp[DATA_UINTS-1];
memcpy(tmp, DATAS(item)->rbuf + DATAS(item)->chip_off[i], sizeof(tmp));
DATAS(item)->chip_off[i] = 0;
for (j = 0; j < BAB_REPLY_NONCES; j++) {
if (tmp[j] != 0xffffffff && tmp[j] != 0x00000000) {
babinfo->chip_bank[i] = bank;
babinfo->chips = i + 1;
break;
}
}
}
for (i = first ; i < babinfo->chips; i++)
babinfo->chip_bank[i] = bank;
K_WLOCK(babinfo->sfree_list);
k_add_head(babinfo->sfree_list, item);
K_WUNLOCK(babinfo->sfree_list);
}
bab_ioc[4].value = (int)(babinfo->speed_hz);
bab_ioc[5].value = (int)(babinfo->speed_hz);
close(memfd);
babcgpu->device_path = strdup(buf);
return true;
close_out:
close(babinfo->spifd);
babinfo->spifd = 0;
free(babcgpu->device_path);
babcgpu->device_path = NULL;
map_out:
munmap((void *)(babinfo->gpio), BAB_SPI_BUFSIZ);
babinfo->gpio = NULL;
bad_out:
return false;
}
chips = 0;
babinfo->version = 2;
babinfo->banks = 0;
for (bank = 1; bank <= BAB_MAXBANKS; bank++) {
for (chipoff = 0; chipoff < BAB_BANKCHIPS; chipoff++) {
chip = babinfo->chips + chipoff;
babinfo->chip_spis[chip] = 625000;
}
bab_reset(bank, 64);
bab_detect_chips(babcgpu, babinfo, bank, babinfo->chips, babinfo-
>chips + BAB_BANKCHIPS);
new_chips = babinfo->chips - chips;
babinfo->chips_per_bank[bank] = new_chips;
if (new_chips > 0) {
babinfo->bank_first_chip[bank] = babinfo->chips -
new_chips;
babinfo->bank_last_chip[bank] = babinfo->chips - 1;
}
chips = babinfo->chips;
if (new_chips == 0)
boards = 0;
else {
boards = (int)((float)(new_chips - 1) / BAB_BOARDCHIPS) +
1;
babinfo->banks++;
}
applog(LOG_WARNING, "%s V2 bank %d: %d chips %d board%s",
babcgpu->drv->dname, bank, new_chips,
boards, (boards == 1) ? "" : "s");
babinfo->boards += boards;
if (opt_bab_options == NULL)
return;
which = 0;
ptr = opt_bab_options;
while (ptr && *ptr) {
colon = strchr(ptr, ':');
if (colon)
*(colon++) = '\0';
switch (which) {
case 0:
if (*ptr && tolower(*ptr) != 'd') {
val = atoi(ptr);
if (!isdigit(*ptr) || val < BAB_ABSMINSPEED || val >
BAB_MAXSPEED) {
quit(1, "%s"INVOP"%s '%s' must be %d <= %s <=
%d",
babcgpu->drv->dname,
bab_options[which],
ptr, BAB_ABSMINSPEED,
bab_options[which],
BAB_MAXSPEED);
}
babinfo->max_speed = (uint8_t)val;
// Adjust def,min down if they are above max
specified
if (babinfo->def_speed > babinfo->max_speed)
babinfo->def_speed = babinfo->max_speed;
if (babinfo->min_speed > babinfo->max_speed)
babinfo->min_speed = babinfo->max_speed;
}
break;
case 1:
if (*ptr && tolower(*ptr) != 'd') {
val = atoi(ptr);
if (!isdigit(*ptr) || val < BAB_ABSMINSPEED || val >
babinfo->max_speed) {
quit(1, "%s"INVOP"%s '%s' must be %d <= %s <=
%d",
babcgpu->drv->dname,
bab_options[which],
ptr, BAB_ABSMINSPEED,
bab_options[which],
babinfo->max_speed);
}
babinfo->def_speed = (uint8_t)val;
// Adjust min down if is is above def specified
if (babinfo->min_speed > babinfo->def_speed)
babinfo->min_speed = babinfo->def_speed;
}
break;
case 2:
if (*ptr && tolower(*ptr) != 'd') {
val = atoi(ptr);
if (!isdigit(*ptr) || val < BAB_ABSMINSPEED || val >
babinfo->def_speed) {
quit(1, "%s"INVOP"%s '%s' must be %d <= %s <=
%d",
babcgpu->drv->dname,
bab_options[which],
ptr, BAB_ABSMINSPEED,
bab_options[which],
babinfo->def_speed);
}
babinfo->min_speed = (uint8_t)val;
}
break;
case 3:
if (*ptr && tolower(*ptr) != 'd') {
fval = atof(ptr);
if (!isdigit(*ptr) || fval < 0.0 || fval > 100.0) {
quit(1, "%s"INVOP"%s '%s' must be 0.0 <= %s <=
100.0",
babcgpu->drv->dname,
bab_options[which], ptr,
bab_options[which]);
}
babinfo->tune_up = fval;
}
break;
case 4:
if (*ptr && tolower(*ptr) != 'd') {
fval = atof(ptr);
if (!isdigit(*ptr) || fval < 0.0 || fval > 100.0) {
quit(1, "%s"INVOP"%s '%s' must be %f <= %s <=
100.0",
babcgpu->drv->dname,
bab_options[which],
ptr, babinfo->tune_up,
bab_options[which]);
}
babinfo->tune_down = fval;
}
break;
case 5:
if (*ptr && tolower(*ptr) != 'd') {
val = atoi(ptr);
if (!isdigit(*ptr) || val < 10000 || val > 10000000)
{
quit(1, "%s"INVOP"%s '%s' must be 10,000 <= %s
<= 10,000,000",
babcgpu->drv->dname,
bab_options[which], ptr,
bab_options[which]);
}
babinfo->speed_hz = (uint32_t)val;
}
break;
case 6:
if (*ptr && tolower(*ptr) != 'd') {
val = atoi(ptr);
if (!isdigit(*ptr) || val < 0 || val > 65535) {
quit(1, "%s"INVOP"%s '%s' must be 0 <= %s <=
65535",
babcgpu->drv->dname,
bab_options[which], ptr,
bab_options[which]);
}
babinfo->delay_usecs = (uint16_t)val;
}
break;
case 7:
if (*ptr && tolower(*ptr) != 'd') {
lval = atol(ptr);
if (!isdigit(*ptr) || lval < 0) {
quit(1, "%s"INVOP"%s '%s' must be %s >= 0",
babcgpu->drv->dname,
bab_options[which], ptr,
bab_options[which]);
}
babinfo->trf_delay = (uint64_t)lval;
}
break;
default:
break;
}
ptr = colon;
which++;
}
}
if (hotplug)
return;
babcgpu->drv = &bab_drv;
babcgpu->deven = DEV_ENABLED;
babcgpu->threads = 1;
babinfo->max_speed = BAB_DEFMAXSPEED;
babinfo->def_speed = BAB_DEFSPEED;
babinfo->min_speed = BAB_ABSMINSPEED;
babinfo->tune_up = BAB_TUNEUP;
babinfo->tune_down = BAB_TUNEDOWN;
babinfo->speed_hz = BAB_SPI_SPEED;
babinfo->delay_usecs = BAB_DELAY_USECS;
babinfo->trf_delay = BAB_TRF_DELAY;
bab_get_options(babcgpu, babinfo);
bab_init_chips(babcgpu, babinfo);
if (babinfo->boards) {
applog(LOG_WARNING, "%s found %d chips %d board%s",
babcgpu->drv->dname, babinfo->chips,
babinfo->boards,
(babinfo->boards == 1) ? "" : "s");
} else {
applog(LOG_WARNING, "%s found %d chips",
babcgpu->drv->dname, babinfo->chips);
}
if (babinfo->chips == 0)
goto cleanup;
if (!add_cgpu(babcgpu))
goto cleanup;
cgsem_init(&(babinfo->scan_work));
cgsem_init(&(babinfo->spi_work));
cgsem_init(&(babinfo->spi_reply));
cgsem_init(&(babinfo->process_reply));
mutex_init(&babinfo->did_lock);
mutex_init(&babinfo->nonce_lock);
// Exclude detection
cgtime(&(babcgpu->dev_start_tv));
// Ignore detection tests
babinfo->last_did.tv_sec = 0;
babinfo->initialised = true;
return;
cleanup:
close(babinfo->spifd);
munmap((void *)(babinfo->gpio), BAB_SPI_BUFSIZ);
unalloc:
free(babinfo);
free(babcgpu);
}
cgtime(&start);
while (babcgpu->shutdown == false) {
K_WLOCK(babinfo->spi_list);
sitem = k_unlink_tail(babinfo->spi_list);
K_WUNLOCK(babinfo->spi_list);
if (!sitem) {
cgtime(&stop);
wait = us_tdiff(&stop, &start);
if (wait > BAB_LONG_uS) {
applog(LOG_WARNING, "%s%i: SPI waiting %fs ...",
babcgpu->drv->name,
babcgpu->device_id,
(float)wait / 1000000.0);
cgsem_mswait(&(babinfo->spi_work), BAB_LONG_WAIT_mS);
} else
cgsem_mswait(&(babinfo->spi_work), (int)((BAB_LONG_uS -
wait) / 1000));
continue;
}
/*
* TODO: handle if an LP happened after bab_do_work() started
* i.e. we don't want to send the work
* Have an LP counter that at this point would show the work
* is stale - so don't send it
*/
cgtime(&send);
bab_txrx(sitem, false);
cgtime(&start);
// The work isn't added to the chip until it has been sent
K_WLOCK(babinfo->wfree_list);
for (chip = 0; chip < babinfo->chips; chip++) {
witem = DATAS(sitem)->witems[chip];
if (witem) {
memcpy(&(DATAW(witem)->work_start), &(DATAS(sitem)-
>work_start),
sizeof(DATAW(witem)->work_start));
k_add_head(babinfo->chip_work[chip], witem);
#if UPDATE_HISTORY
babinfo->work_count[chip]++;
#endif
if (babinfo->first_work[chip].tv_sec == 0)
memcpy(&(babinfo->first_work[chip]), &send,
sizeof(send));
}
}
K_WUNLOCK(babinfo->wfree_list);
K_WLOCK(babinfo->spi_sent);
k_add_head(babinfo->spi_sent, sitem);
K_WUNLOCK(babinfo->spi_sent);
cgsem_post(&(babinfo->spi_reply));
// Store stats
if (babinfo->last_sent_work.tv_sec) {
delay = tdiff(&send, &(babinfo->last_sent_work));
babinfo->delay_count++;
if (babinfo->delay_min == 0 || babinfo->delay_min > delay)
babinfo->delay_min = delay;
if (babinfo->delay_max < delay)
babinfo->delay_max = delay;
if (delay < BAB_DELAY_BASE)
band = 0;
else if (delay >=
(BAB_DELAY_BASE+BAB_DELAY_STEP*(BAB_DELAY_BANDS+1)))
band = BAB_DELAY_BANDS+1;
else
band = (int)(((double)delay - BAB_DELAY_BASE) /
BAB_DELAY_STEP) + 1;
babinfo->delay_bands[band]++;
}
memcpy(&(babinfo->last_sent_work), &send, sizeof(start));
cgsem_mswait(&(babinfo->spi_work), BAB_STD_WAIT_mS);
}
return NULL;
}
mutex_lock(&(babinfo->did_lock));
babinfo->last_did.tv_sec = 0;
mutex_unlock(&(babinfo->did_lock));
cgsem_post(&(babinfo->scan_work));
}
#define DATA_MERKLE7 16
#define DATA_NTIME 17
#define DATA_BITS 18
#define DATA_NONCE 19
#if UPDATE_HISTORY
static void process_history(struct cgpu_info *babcgpu, int chip, struct timeval
*when, bool good, struct timeval *now)
{
struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
uint64_t good_nonces, bad_nonces;
uint8_t chip_fast;
double tune;
K_ITEM *item;
int i;
K_WLOCK(babinfo->nfree_list);
item = k_unlink_head(babinfo->nfree_list);
memcpy(&(DATAN(item)->found), when, sizeof(*when));
if (good)
k_add_head(babinfo->good_nonces[chip], item);
else
k_add_head(babinfo->bad_nonces[chip], item);
k_unlink_item(babinfo->good_nonces[i], item);
k_add_head(babinfo->nfree_list, item);
item = babinfo->good_nonces[i]->tail;
}
item = babinfo->bad_nonces[i]->tail;
while (item) {
if (tdiff(now, &(DATAN(item)->found)) < HISTORY_TIME_S)
break;
k_unlink_item(babinfo->bad_nonces[i], item);
k_add_head(babinfo->nfree_list, item);
item = babinfo->bad_nonces[i]->tail;
}
}
good_nonces = babinfo->good_nonces[chip]->count;
bad_nonces = babinfo->bad_nonces[chip]->count;
K_WUNLOCK(babinfo->nfree_list);
// Tuning ...
if (tdiff(now, &(babinfo->first_work[chip])) >= HISTORY_TIME_S &&
tdiff(now, &(babinfo->last_tune[chip])) >= HISTORY_TIME_S &&
(good_nonces + bad_nonces) > 0) {
chip_fast = babinfo->chip_fast[chip];
/*
* If bad then step it down and remember the speed
* TODO: does a speed change reset the chip? Or is there a reset?
*/
if (good_nonces == 0) {
if (chip_fast > babinfo->min_speed) {
if (babinfo->bad_fast[chip] > chip_fast)
babinfo->bad_fast[chip] = chip_fast;
babinfo->chip_fast[chip]--;
babinfo->bad_msg[chip] = true;
}
}
goto tune_over;
}
/*
* It 'was' permanently BAD but a good nonce came back!
*/
if (babinfo->bad_msg[chip]) {
applog(LOG_WARNING, "%s%d: Chip %d REVIVED - at speed %d",
babcgpu->drv->name, babcgpu->device_id,
chip, (int)chip_fast);
babinfo->bad_msg[chip] = false;
}
/*
* Since we have found 'some' nonces -
* make sure bad_fast is higher than current chip_fast
*/
if (babinfo->bad_fast[chip] <= chip_fast)
babinfo->bad_fast[chip] = chip_fast + 1;
/*
* TODO: it appears some chips just get a % bad at low speeds
* so we should handle them by weighting the speed reduction vs
* the HW% gained from the reduction (i.e. GH/s)
* Maybe handle that when they hit min_speed, then do a gradual speed
* up verifying if it is really making GH/s worse or better
*/
// Tune it down if error rate is too high (and it's above min)
if (tune >= babinfo->tune_down && chip_fast > babinfo->min_speed) {
babinfo->chip_fast[chip]--;
goto tune_over;
}
/*
* TODO: if we are at bad_fast-1 and tune_up
* and bad_fast was set more than some time limit ago
* then consider increasing bad_fast by 1?
*/
goto tune_over;
}
tune_over:
cgtime(&(babinfo->last_tune[chip]));
}
}
#endif
/*
* Find the matching work item by checking each nonce against
* work items for the nonces chip
*/
static K_ITEM *process_nonce(struct thr_info *thr, struct cgpu_info *babcgpu,
K_ITEM *ritem, uint32_t raw_nonce, K_ITEM *newest_witem)
{
struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
unsigned int links, proc_links, work_links, tests;
int try_sta, try_fin, offset;
K_ITEM *witem, *wtail;
struct timeval now;
bool not_first_reply;
uint32_t nonce;
int chip;
chip = DATAR(ritem)->chip;
not_first_reply = DATAR(ritem)->not_first_reply;
babinfo->chip_nonces[chip]++;
/*
* We can grab the head of the chip work queue and then release
* the lock and follow it to the end and back, since the other
* thread will only add items above the head - it wont touch
* any of the prev/next pointers from the head to the end -
* except the head->prev pointer may get changed
*/
K_RLOCK(babinfo->chip_work[chip]);
witem = babinfo->chip_work[chip]->head;
K_RUNLOCK(babinfo->chip_work[chip]);
if (!witem) {
applog(LOG_ERR, "%s%i: chip %d has no work, 1 nonce discarded!",
babcgpu->drv->name, babcgpu->device_id, chip);
babinfo->untested_nonces++;
return newest_witem;
}
babinfo->tested_nonces++;
nonce = bab_decnonce(raw_nonce);
cgtime(&now);
mutex_lock(&(babinfo->nonce_lock));
babinfo->new_nonces++;
mutex_unlock(&(babinfo->nonce_lock));
babinfo->ok_nonces++;
babinfo->total_tests += tests;
if (babinfo->max_tests_per_nonce < tests)
babinfo->max_tests_per_nonce = tests;
babinfo->total_links += links;
babinfo->total_proc_links += proc_links;
if (babinfo->max_links < links)
babinfo->max_links = links;
if (babinfo->max_proc_links < proc_links)
babinfo->max_proc_links = proc_links;
babinfo->total_work_links += work_links;
babinfo->chip_cont_bad[chip] = 0;
#if UPDATE_HISTORY
process_history(babcgpu, chip,
&(DATAR(ritem)->when),
true, &now);
#endif
if (newest_witem == NULL ||
ms_tdiff(&(DATAW(wtail)->work_start),
&(DATAW(newest_witem)->work_start)) < 0)
return wtail;
return newest_witem;
}
}
}
}
if (wtail == witem)
break;
wtail = wtail->prev;
links++;
proc_links++;
}
if (not_first_reply) {
babinfo->chip_bad[chip]++;
inc_hw_errors(thr);
babinfo->fail++;
babinfo->fail_total_tests += tests;
babinfo->fail_total_links += links;
babinfo->fail_total_work_links += work_links;
babinfo->chip_cont_bad[chip]++;
if (babinfo->chip_max_bad[chip] < babinfo->chip_cont_bad[chip])
babinfo->chip_max_bad[chip] = babinfo->chip_cont_bad[chip];
return newest_witem;
}
/*
* On completion discard any work items older than BAB_WORK_EXPIRE_mS
* and any work items of the chip older than the work of the newest nonce work item
*/
static void oknonces(struct thr_info *thr, struct cgpu_info *babcgpu, K_ITEM
*ritem)
{
uint32_t raw_nonce;
K_ITEM *witem;
int nonces;
witem = NULL;
ritem = NULL;
while (babcgpu->shutdown == false) {
K_WLOCK(babinfo->res_list);
if (ritem) {
// Release the old one
k_add_head(babinfo->rfree_list, ritem);
ritem = NULL;
}
// Check for a new one
ritem = k_unlink_tail(babinfo->res_list);
K_WUNLOCK(babinfo->res_list);
if (!ritem) {
cgsem_mswait(&(babinfo->process_reply), BAB_RESULT_DELAY_mS);
continue;
}
return NULL;
}
/*
* 1.0s per nonce = 4.2GH/s
* 0.9s per nonce = 4.8GH/s
* On a slow machine, reducing this may resolve:
* BaB0: SPI waiting 1.2...s
*/
#define BAB_STD_WORK_DELAY_uS 900000
cgtime(&now);
mutex_lock(&(babinfo->did_lock));
delay = us_tdiff(&now, &(babinfo->last_did));
mutex_unlock(&(babinfo->did_lock));
if (delay < BAB_STD_WORK_DELAY_uS)
return false;
K_WLOCK(babinfo->sfree_list);
sitem = k_unlink_head_zero(babinfo->sfree_list);
K_WUNLOCK(babinfo->sfree_list);
K_WLOCK(babinfo->sfree_list);
k_add_head(babinfo->sfree_list, sitem);
K_WUNLOCK(babinfo->sfree_list);
return false;
}
/*
* TODO: do this when we get work except on LP?
* (not LP so we only do ms3steps for work required)
* Though that may more likely trigger the applog(short work
list) above?
*/
if (DATAW(witem)->ci_setup == false) {
memcpy((void *)&(DATAW(witem)->chip_input.midstate[0]),
DATAW(witem)->work->midstate,
sizeof(DATAW(witem)->work->midstate));
memcpy((void *)&(DATAW(witem)->chip_input.merkle7),
(void *)&(DATAW(witem)->work->data[WORK_MERKLE7]),
MERKLE_BYTES);
bab_ms3steps((void *)&(DATAW(witem)->chip_input));
DATAW(witem)->ci_setup = true;
}
DATAS(sitem)->witems[chip] = witem;
work_items++;
}
}
// Send
bab_put(babinfo, sitem);
// Receive
res = bab_get(babcgpu, babinfo, &when);
if (!res) {
applog(LOG_DEBUG, "%s%i: didn't get work reply ...",
babcgpu->drv->name, babcgpu->device_id);
return false;
}
spichk = babinfo->chip_results[chip].spichk;
if (spichk != 0 && spichk != 0xffffffff) {
babinfo->chip_spie[chip]++;
spie++;
// Test the results anyway
}
nonces = 0;
for (rep = 0; rep < BAB_REPLY_NONCES; rep++) {
nonce = babinfo->chip_results[chip].nonce[rep];
if (nonce != babinfo->chip_prev[chip].nonce[rep]) {
if ((nonce & BAB_EVIL_MASK) == BAB_EVIL_NONCE)
babinfo->discarded_e0s++;
else
DATAR(ritem)->nonce[nonces++] = nonce;
}
}
if (nonces == BAB_REPLY_NONCES) {
babinfo->chip_miso[chip]++;
miso++;
// Test the results anyway
}
/*
* Send even with zero nonces
* so cleanup_older() is called for the chip
*/
DATAR(ritem)->nonces = nonces;
K_WLOCK(babinfo->res_list);
k_add_head(babinfo->res_list, ritem);
K_WUNLOCK(babinfo->res_list);
cgsem_post(&(babinfo->process_reply));
babinfo->not_first_reply[chip] = true;
memcpy((void *)(&(babinfo->chip_prev[chip])),
(void *)(&(babinfo->chip_results[chip])),
sizeof(struct bab_work_reply));
}
return true;
}
/*
* We require a seperate results checking thread since there is a lot
* of work done checking the results multiple times - thus we don't
* want that delay affecting sending/receiving work to/from the device
*/
if (thr_info_create(&(babinfo->res_thr), NULL, bab_res, (void *)babcgpu)) {
applog(LOG_ERR, "%s%i: Results thread create failed",
babcgpu->drv->name, babcgpu->device_id);
return false;
}
pthread_detach(babinfo->res_thr.pth);
return true;
}
babcgpu->shutdown = true;
}
K_RLOCK(babinfo->available_work);
count = babinfo->available_work->count;
K_RUNLOCK(babinfo->available_work);
K_WLOCK(babinfo->wfree_list);
item = k_unlink_head_zero(babinfo->wfree_list);
DATAW(item)->work = usework;
DATAW(item)->rolled = rolled;
k_add_head(babinfo->available_work, item);
K_WUNLOCK(babinfo->wfree_list);
} while (--need > 0 && ++roll <= roll_limit);
} else {
// Avoid a hard loop when we can't get work fast enough
cgsleep_us(42);
}
if (need > 0)
ret = false;
else
ret = true;
}
return ret;
}
/*
* TODO: allow this to run through more than once - the second+
* time not sending any new work unless a flush occurs since:
* at the moment we have BAB_STD_WORK_mS latency added to earliest replies
*/
static int64_t bab_scanwork(__maybe_unused struct thr_info *thr)
{
struct cgpu_info *babcgpu = thr->cgpu;
struct bab_info *babinfo = (struct bab_info *)(babcgpu->device_data);
int64_t hashcount = 0;
int count;
bab_do_work(babcgpu);
K_RLOCK(babinfo->available_work);
count = babinfo->available_work->count;
K_RUNLOCK(babinfo->available_work);
mutex_lock(&(babinfo->nonce_lock));
if (babinfo->new_nonces) {
hashcount += 0xffffffffull * babinfo->new_nonces;
babinfo->new_nonces = 0;
}
mutex_unlock(&(babinfo->nonce_lock));
return hashcount;
}
#define CHIPS_PER_STAT 16
#define FMT_RANGE "%d-%d"
if (babinfo->initialised == false)
return NULL;
memset(&speeds, 0, sizeof(speeds));
data[0] = '\0';
for (i = 0; i <= BAB_MAXBANKS; i++) {
snprintf(buf, sizeof(buf), "%s%d",
(i == 0) ? "" : " ",
babinfo->chips_per_bank[i]);
strcat(data, buf);
}
root = api_add_string(root, "Chips Per Bank", data, true);
data[0] = '\0';
for (i = 0; i <= BAB_MAXBANKS; i++) {
snprintf(buf, sizeof(buf), "%s%d",
(i == 0) ? "" : " ",
babinfo->missing_chips_per_bank[i]);
strcat(data, buf);
}
root = api_add_string(root, "Missing Chips Per Bank", data, true);
cgtime(&now);
elapsed = tdiff(&now, &(babcgpu->dev_start_tv));
K_RLOCK(babinfo->nfree_list);
for (i = 0; i < babinfo->chips; i++) {
item = babinfo->good_nonces[i]->tail;
elapsed_is_good[i] = true;
if (!item)
history_elapsed[i] = 0;
else
history_elapsed[i] = tdiff(&now, &(DATAN(item)->found));
item = babinfo->bad_nonces[i]->tail;
if (item) {
diff = tdiff(&now, &(DATAN(item)->found));
if (history_elapsed[i] < diff) {
history_elapsed[i] = diff;
elapsed_is_good[i] = false;
}
}
history_good[i] = babinfo->good_nonces[i]->count;
history_bad[i] = babinfo->bad_nonces[i]->count;
}
K_RUNLOCK(babinfo->nfree_list);
his_ghs_tot = 0;
for (i = 0; i < babinfo->chips; i += CHIPS_PER_STAT) {
to = i + CHIPS_PER_STAT - 1;
if (to >= babinfo->chips)
to = babinfo->chips - 1;
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%"PRIu64,
j == i ? "" : " ",
babinfo->chip_nonces[j]);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Nonces "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%"PRIu64,
j == i ? "" : " ",
babinfo->chip_good[j]);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Good "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%"PRIu64,
j == i ? "" : " ",
babinfo->chip_bad[j]);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Bad "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s0x%02x",
j == i ? "" : " ",
(int)(babinfo->chip_conf[j]));
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Conf "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%d",
j == i ? "" : " ",
(int)(babinfo->chip_fast[j]));
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Fast "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%d",
j == i ? "" : " ",
(int)(babinfo->chip_spie[j]));
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Spie "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%d",
j == i ? "" : " ",
(int)(babinfo->chip_miso[j]));
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Miso "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
tot = (float)(babinfo->chip_good[j] + babinfo->chip_bad[j]);
if (tot != 0)
hw = 100.0 * (float)(babinfo->chip_bad[j]) / tot;
else
hw = 0;
snprintf(buf, sizeof(buf),
"%s%.3f",
j == i ? "" : " ", hw);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "HW%% "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
ghs_sum = 0;
data[0] = '\0';
for (j = i; j <= to; j++) {
if (elapsed > 0) {
ghs = (double)(babinfo->chip_good[j]) * 0xffffffffull /
elapsed / 1000000000.0;
} else
ghs = 0;
snprintf(buf, sizeof(buf),
"%s%.3f",
j == i ? "" : " ", ghs);
strcat(data, buf);
ghs_sum += (float)ghs;
}
snprintf(buf, sizeof(buf), "GHs "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%"PRIu64,
j == i ? "" : " ",
babinfo->chip_cont_bad[j]);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Cont-Bad "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%"PRIu64,
j == i ? "" : " ",
babinfo->chip_max_bad[j]);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "Max-Bad "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%"PRIu64,
j == i ? "" : " ",
history_good[j]);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "History Good "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
snprintf(buf, sizeof(buf),
"%s%"PRIu64,
j == i ? "" : " ",
history_bad[j]);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "History Bad "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
data[0] = '\0';
for (j = i; j <= to; j++) {
tot = (float)(history_good[j] + history_bad[j]);
if (tot != 0)
hw = 100.0 * (float)(history_bad[j]) / tot;
else
hw = 0;
snprintf(buf, sizeof(buf),
"%s%.3f",
j == i ? "" : " ", hw);
strcat(data, buf);
}
snprintf(buf, sizeof(buf), "History HW%% "FMT_RANGE, i, to);
root = api_add_string(root, buf, data, true);
ghs_sum = 0;
data[0] = '\0';
for (j = i; j <= to; j++) {
if (history_elapsed[j] > 0) {
double num = history_good[j];
// exclude the first nonce?
if (elapsed_is_good[j])
num--;
ghs = num * 0xffffffffull /
history_elapsed[j] / 1000000000.0;
} else
ghs = 0;
snprintf(buf, sizeof(buf),
"%s%.3f",
j == i ? "" : " ", ghs);
strcat(data, buf);
ghs_sum += (float)ghs;
his_ghs_tot += ghs_sum;
}
his_good_tot = his_bad_tot = 0;
for (i = 0; i < babinfo->chips; i++) {
his_good_tot += history_good[i];
his_bad_tot += history_bad[i];
}
if (his_good_tot + his_bad_tot)
tot = 100.0 * (float)his_bad_tot / (float)(his_good_tot + his_bad_tot);
else
tot = 0.0;
root = api_add_avg(root, "Total History HW%", &tot, true);
switch (babinfo->version) {
case 1:
i = j = BAB_V1_BANK;
break;
case 2:
i = 1;
j = BAB_MAXBANKS;
break;
}
data[0] = '\0';
for (bank = i; bank <= j; bank++) {
if (babinfo->bank_first_chip[bank] >= 0) {
chip = babinfo->bank_first_chip[bank];
to = babinfo->bank_last_chip[bank];
for (; chip <= to; chip += BAB_BOARDCHIPS) {
bad = true;
for (k = chip; (k <= to) && (k < (chip+BAB_BOARDCHIPS)); k+
+) {
if (history_elapsed[k] > 0) {
double num = history_good[k];
// exclude the first nonce?
if (elapsed_is_good[k])
num--;
ghs = num * 0xffffffffull /
history_elapsed[k] / 1000000000.0;
} else
ghs = 0;
data[0] = '\0';
for (bank = i; bank <= j; bank++) {
if (babinfo->bank_first_chip[bank] >= 0) {
to = babinfo->bank_first_chip[bank];
chip = babinfo->bank_last_chip[bank];
for (; chip >= to; chip--) {
bad = true;
if (history_elapsed[chip] > 0) {
double num = history_good[chip];
// exclude the first nonce?
if (elapsed_is_good[chip])
num--;
ghs = num * 0xffffffffull /
history_elapsed[chip] / 1000000000.0;
} else
ghs = 0;
chip_work = 0;
for (i = 0; i < babinfo->chips; i++)
chip_work += babinfo->chip_work[i]->count;
spi_work = babinfo->spi_list->count * babinfo->chips;
data[0] = '\0';
for (i = 0; i <= BAB_DELAY_BANDS; i++) {
snprintf(buf, sizeof(buf),
"%s<%.1f=%"PRIu64,
i == 0 ? "" : " ",
BAB_DELAY_BASE+(BAB_DELAY_STEP*i),
babinfo->delay_bands[i]);
strcat(data, buf);
}
snprintf(buf, sizeof(buf),
" >=%.1f=%"PRIu64,
BAB_DELAY_BASE+BAB_DELAY_STEP*(BAB_DELAY_BANDS+1),
babinfo->delay_bands[BAB_DELAY_BANDS+1]);
strcat(data, buf);
root = api_add_string(root, "Delay Bands", data, true);
i = (int)(babinfo->max_speed);
root = api_add_int(root, bab_options[0], &i, true);
i = (int)(babinfo->def_speed);
root = api_add_int(root, bab_options[1], &i, true);
i = (int)(babinfo->min_speed);
root = api_add_int(root, bab_options[2], &i, true);
root = api_add_double(root, bab_options[3], &(babinfo->tune_up), true);
root = api_add_double(root, bab_options[4], &(babinfo->tune_down), true);
i = (int)(babinfo->speed_hz);
root = api_add_int(root, bab_options[5], &i, true);
i = (int)(babinfo->delay_usecs);
root = api_add_int(root, bab_options[6], &i, true);
root = api_add_uint64(root, bab_options[7], &(babinfo->trf_delay), true);
return root;
}
cgtime(&now);
elapsed = tdiff(&now, &(babcgpu->dev_start_tv));
// At least get 15s of nonces before saying anything is bad
if (elapsed > 15.0) {
K_RLOCK(babinfo->nfree_list);
for (i = 0; i < babinfo->chips; i++) {
if (babinfo->good_nonces[i]->count == 0 &&
babinfo->bad_nonces[i]->count > 1)
bad++;
}
K_RUNLOCK(babinfo->nfree_list);
}