Some cleanup
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
diff --git a/libztex.c b/libztex.c
index 8bd4be0..28716d6 100644
--- a/libztex.c
+++ b/libztex.c
@@ -54,6 +54,7 @@ static bool libztex_checkDevice (struct libusb_device *dev) {
return false;
}
if (!(desc.idVendor == LIBZTEX_IDVENDOR && desc.idProduct == LIBZTEX_IDPRODUCT)) {
+ applog(LOG_DEBUG, "Not a ZTEX device %0.4x:%0.4x", desc.idVendor, desc.idProduct);
return false;
}
return true;
@@ -61,9 +62,8 @@ static bool libztex_checkDevice (struct libusb_device *dev) {
static bool libztex_checkCapability (struct libztex_device *ztex, int i, int j) {
if (!((i>=0) && (i<=5) && (j>=0) && (j<8) &&
- (((ztex->interfaceCapabilities[i] & 255) & (1 << j)) != 0))) {
+ (((ztex->interfaceCapabilities[i] & 255) & (1 << j)) != 0)))
applog(LOG_ERR, "%s: capability missing: %d %d", ztex->repr, i, i);
- }
return true;
}
@@ -99,20 +99,19 @@ static void libztex_swapBits (unsigned char *buf, int size) {
static int libztex_getFpgaState (struct libztex_device *ztex, struct libztex_fpgastate *state) {
int cnt;
unsigned char buf[9];
- if (!libztex_checkCapability(ztex, CAPABILITY_FPGA)) {
+ if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
return -1;
- }
cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x30, 0, 0, buf, 9, 1000);
if (unlikely(cnt < 0)) {
applog(LOG_ERR, "%s: Failed getFpgaState with err %d", ztex->repr, cnt);
return cnt;
}
- state->fpgaConfigured = buf[0] == 0;
+ state->fpgaConfigured = (buf[0] == 0);
state->fpgaChecksum = buf[1] & 0xff;
state->fpgaBytes = ((buf[5] & 0xff)<<24) | ((buf[4] & 0xff)<<16) | ((buf[3] & 0xff)<<8) | (buf[2] & 0xff);
state->fpgaInitB = buf[6] & 0xff;
state->fpgaFlashResult = buf[7];
- state->fpgaFlashBitSwap = buf[8] != 0;
+ state->fpgaFlashBitSwap = (buf[8] != 0);
return 0;
}
@@ -124,15 +123,13 @@ static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* fir
int tries, cnt, buf_p, i;
FILE *fp;
- if (!libztex_checkCapability(ztex, CAPABILITY_FPGA)) {
+ if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
return -1;
- }
libztex_getFpgaState(ztex, &state);
- if (!force) {
- if (state.fpgaConfigured) {
- return 1;
- }
+ if (!force && state.fpgaConfigured) {
+ applog(LOG_DEBUG, "Bitstream already configured");
+ return 1;
}
for (tries=10; tries>0; tries--) {
@@ -151,7 +148,7 @@ static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* fir
if (feof(fp))
pos--;
- if ( bs<0 || bs>1 )
+ if ( bs != 0 && bs != 1 )
bs = libztex_detectBitstreamBitOrder(buf, transactionBytes<pos ? transactionBytes : pos);
//* Reset fpga
@@ -201,7 +198,7 @@ static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* fir
return 3;
}
usleep(200000);
- applog(LOG_ERR, "%s: FPGA configuration done", ztex->repr);
+ applog(LOG_INFO, "%s: FPGA configuration done", ztex->repr);
return 0;
}
@@ -218,9 +215,8 @@ int libztex_configureFpga (struct libztex_device *ztex) {
int libztex_setFreq (struct libztex_device *ztex, uint16_t freq) {
int cnt;
- if (freq > ztex->freqMaxM) {
+ if (freq > ztex->freqMaxM)
freq = ztex->freqMaxM;
- }
cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x83, freq, 0, NULL, 0, 500);
if (unlikely(cnt < 0)) {
@@ -268,7 +264,7 @@ int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** z
}
cnt = libusb_get_string_descriptor_ascii (newdev->hndl, newdev->descriptor.iSerialNumber, newdev->snString,
- LIBZTEX_SNSTRING_LEN+1);
+ LIBZTEX_SNSTRING_LEN+1);
if (unlikely(cnt < 0)) {
applog(LOG_ERR, "Ztex check device: Failed to read device snString with err %d", cnt);
return cnt;
@@ -317,8 +313,8 @@ int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** z
return cnt;
}
- if (unlikely(!(buf[0]) == 4)) {
- if (unlikely(buf[0]) != 2) {
+ if (unlikely(buf[0] != 4)) {
+ if (unlikely(buf[0] != 2)) {
applog(LOG_ERR, "Invalid BTCMiner descriptor version. Firmware must be updated (%d).", buf[0]);
return 3;
}
@@ -326,7 +322,7 @@ int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** z
}
newdev->numNonces = buf[1] + 1;
- newdev->offsNonces = ((buf[2] & 255) | ((buf[3] & 255) << 8)) - 10000;
+ newdev->offsNonces = ((buf[2] & 255) | ((buf[3] & 255) << 8)) - 10000;
newdev->freqM1 = ( (buf[4] & 255) | ((buf[5] & 255) << 8) ) * 0.01;
newdev->freqMaxM = (buf[7] & 255);
newdev->freqM = (buf[6] & 255);
@@ -393,9 +389,8 @@ int libztex_scanDevices (struct libztex_dev_list*** devs_p) {
for (i = 0; i < found; i++) {
err = libztex_prepare_device(list[usbdevices[i]], &ztex);
- if (unlikely(err != 0)) {
+ if (unlikely(err != 0))
applog(LOG_ERR, "prepare device: %d", err);
- }
// check if valid
if (!ztex->valid) {
libztex_destroy_device(ztex);
@@ -404,9 +399,8 @@ int libztex_scanDevices (struct libztex_dev_list*** devs_p) {
devs[pos] = malloc(sizeof(struct libztex_dev_list));
devs[pos]->dev = ztex;
devs[pos]->next = NULL;
- if (pos > 0) {
+ if (pos > 0)
devs[pos-1]->next = devs[pos];
- }
pos++;
}
@@ -417,13 +411,11 @@ int libztex_scanDevices (struct libztex_dev_list*** devs_p) {
int libztex_sendHashData (struct libztex_device *ztex, unsigned char *sendbuf) {
int cnt;
- if (ztex == NULL || ztex->hndl == NULL) {
+ if (ztex == NULL || ztex->hndl == NULL)
return 0;
- }
cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x80, 0, 0, sendbuf, 44, 1000);
- if (unlikely(cnt < 0)) {
+ if (unlikely(cnt < 0))
applog(LOG_ERR, "%s: Failed sendHashData with err %d", ztex->repr, cnt);
- }
return cnt;
}
@@ -433,9 +425,8 @@ int libztex_readHashData (struct libztex_device *ztex, struct libztex_hash_data
unsigned char rbuf[12*8];
int cnt, i;
- if (ztex->hndl == NULL) {
+ if (ztex->hndl == NULL)
return 0;
- }
cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x81, 0, 0, rbuf, 12*ztex->numNonces, 1000);
if (unlikely(cnt < 0)) {
@@ -458,9 +449,8 @@ void libztex_freeDevList (struct libztex_dev_list **devs) {
ssize_t cnt = 0;
bool done = false;
while (!done) {
- if (devs[cnt]->next == NULL) {
+ if (devs[cnt]->next == NULL)
done = true;
- }
free(devs[cnt++]);
}
free(devs);
diff --git a/ztex.c b/ztex.c
index fea01dc..aed4660 100644
--- a/ztex.c
+++ b/ztex.c
@@ -59,10 +59,8 @@ static void ztex_detect()
applog(LOG_WARNING,"%s: Found Ztex, mark as %d", ztex->device->repr, ztex->device_id);
}
- if (cnt > 0) {
+ if (cnt > 0)
libztex_freeDevList(ztex_devices);
- }
-
}
static bool ztex_updateFreq (struct libztex_device* ztex)
@@ -70,10 +68,9 @@ static bool ztex_updateFreq (struct libztex_device* ztex)
int i, maxM, bestM;
double bestR, r;
- for (i=0; i<ztex->freqMaxM; i++) {
+ for (i=0; i<ztex->freqMaxM; i++)
if (ztex->maxErrorRate[i+1]*i < ztex->maxErrorRate[i]*(i+20))
ztex->maxErrorRate[i+1] = ztex->maxErrorRate[i]*(1.0+20.0/i);
- }
maxM = 0;
while (maxM<ztex->freqMDefault && ztex->maxErrorRate[maxM+1]<LIBZTEX_MAXMAXERRORRATE)
@@ -91,9 +88,8 @@ static bool ztex_updateFreq (struct libztex_device* ztex)
}
}
- if (bestM != ztex->freqM) {
+ if (bestM != ztex->freqM)
libztex_setFreq(ztex, bestM);
- }
maxM = ztex->freqMDefault;
while (maxM<ztex->freqMaxM && ztex->errorWeight[maxM+1] > 100)
@@ -166,42 +162,46 @@ static uint64_t ztex_scanhash(struct thr_info *thr, struct work *work,
i = libztex_sendHashData(ztex, sendbuf);
if (i < 0) {
// Something wrong happened in send
- applog(LOG_ERR, "%s: Failed to send hash data with err %d", ztex->repr, i);
+ applog(LOG_ERR, "%s: Failed to send hash data with err %d, retrying", ztex->repr, i);
usleep(500000);
i = libztex_sendHashData(ztex, sendbuf);
if (i < 0) {
// And there's nothing we can do about it
ztex_disable(thr);
+ applog(LOG_ERR, "%s: Failed to send hash data with err %d, giving up", ztex->repr, i);
return 0;
}
}
applog(LOG_DEBUG, "sent hashdata");
- for (i=0; i<ztex->numNonces; i++) {
+ for (i=0; i<ztex->numNonces; i++)
lastnonce[i] = 0;
- }
+
overflow = false;
while (!(overflow || work_restart[thr->id].restart)) {
usleep(250000);
if (work_restart[thr->id].restart) {
+ applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
break;
}
i = libztex_readHashData(ztex, &hdata[0]);
if (i < 0) {
// Something wrong happened in read
- applog(LOG_ERR, "%s: Failed to read hash data with err %d", ztex->repr, i);
+ applog(LOG_ERR, "%s: Failed to read hash data with err %d, retrying", ztex->repr, i);
usleep(500000);
i = libztex_readHashData(ztex, &hdata[0]);
if (i < 0) {
// And there's nothing we can do about it
ztex_disable(thr);
+ applog(LOG_ERR, "%s: Failed to read hash data with err %d, giving up", ztex->repr, i);
return 0;
}
}
if (work_restart[thr->id].restart) {
+ applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
break;
}
@@ -231,17 +231,16 @@ static uint64_t ztex_scanhash(struct thr_info *thr, struct work *work,
}
}
if (!found) {
- // new nonce found!
+ applog(LOG_DEBUG, "%s: Share found", ztex->repr);
backlog[backlog_p++] = nonce;
- if (backlog_p >= GOLDEN_BACKLOG) {
+ if (backlog_p >= GOLDEN_BACKLOG)
backlog_p = 0;
- }
#if defined(__BIGENDIAN__) || defined(MIPSEB)
nonce = swab32(nonce);
#endif
- work->blk.nonce = 0xffffffff;
- rv = submit_nonce(thr, work, nonce);
- applog(LOG_DEBUG, "submitted %0.8X %d", nonce, rv);
+ work->blk.nonce = 0xffffffff;
+ rv = submit_nonce(thr, work, nonce);
+ applog(LOG_DEBUG, "%s: submitted %0.8x %d", ztex->repr, nonce, rv);
}
}
@@ -250,15 +249,14 @@ static uint64_t ztex_scanhash(struct thr_info *thr, struct work *work,
}
ztex->errorRate[ztex->freqM] = ztex->errorCount[ztex->freqM] / ztex->errorWeight[ztex->freqM] * (ztex->errorWeight[ztex->freqM]<100 ? ztex->errorWeight[ztex->freqM]*0.01 : 1.0);
- if (ztex->errorRate[ztex->freqM] > ztex->maxErrorRate[ztex->freqM]) {
+ if (ztex->errorRate[ztex->freqM] > ztex->maxErrorRate[ztex->freqM])
ztex->maxErrorRate[ztex->freqM] = ztex->errorRate[ztex->freqM];
- }
- if (!ztex_updateFreq(ztex)) {
+ if (!ztex_updateFreq(ztex))
// Something really serious happened, so mark this thread as dead!
return 0;
- }
- applog(LOG_DEBUG, "exit %1.8X", noncecnt);
+
+ applog(LOG_DEBUG, "%s: exit %1.8X", ztex->repr, noncecnt);
work->blk.nonce = 0xffffffff;
@@ -281,18 +279,20 @@ static bool ztex_prepare(struct thr_info *thr)
gettimeofday(&now, NULL);
get_datestamp(ztex->init, &now);
- if (libztex_configureFpga(ztex->device) != 0) {
+ if (libztex_configureFpga(ztex->device) != 0)
return false;
- }
+
ztex->device->freqM = -1;
ztex_updateFreq(ztex->device);
+ applog(LOG_DEBUG, "%s: prepare", ztex->device->repr);
return true;
}
static void ztex_shutdown(struct thr_info *thr)
{
if (thr->cgpu->device != NULL) {
+ applog(LOG_DEBUG, "%s: shutdown", thr->cgpu->device->repr);
libztex_destroy_device(thr->cgpu->device);
thr->cgpu->device = NULL;
}