Browse Source

Some cleanup

nfactor-troky
nelisky 13 years ago
parent
commit
9ba2e21793
  1. 54
      libztex.c
  2. 48
      ztex.c

54
libztex.c

@ -54,6 +54,7 @@ static bool libztex_checkDevice (struct libusb_device *dev) {
return false; return false;
} }
if (!(desc.idVendor == LIBZTEX_IDVENDOR && desc.idProduct == LIBZTEX_IDPRODUCT)) { 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 false;
} }
return true; 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) { static bool libztex_checkCapability (struct libztex_device *ztex, int i, int j) {
if (!((i>=0) && (i<=5) && (j>=0) && (j<8) && 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); applog(LOG_ERR, "%s: capability missing: %d %d", ztex->repr, i, i);
}
return true; 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) { static int libztex_getFpgaState (struct libztex_device *ztex, struct libztex_fpgastate *state) {
int cnt; int cnt;
unsigned char buf[9]; unsigned char buf[9];
if (!libztex_checkCapability(ztex, CAPABILITY_FPGA)) { if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
return -1; return -1;
}
cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x30, 0, 0, buf, 9, 1000); cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x30, 0, 0, buf, 9, 1000);
if (unlikely(cnt < 0)) { if (unlikely(cnt < 0)) {
applog(LOG_ERR, "%s: Failed getFpgaState with err %d", ztex->repr, cnt); applog(LOG_ERR, "%s: Failed getFpgaState with err %d", ztex->repr, cnt);
return cnt; return cnt;
} }
state->fpgaConfigured = buf[0] == 0; state->fpgaConfigured = (buf[0] == 0);
state->fpgaChecksum = buf[1] & 0xff; state->fpgaChecksum = buf[1] & 0xff;
state->fpgaBytes = ((buf[5] & 0xff)<<24) | ((buf[4] & 0xff)<<16) | ((buf[3] & 0xff)<<8) | (buf[2] & 0xff); state->fpgaBytes = ((buf[5] & 0xff)<<24) | ((buf[4] & 0xff)<<16) | ((buf[3] & 0xff)<<8) | (buf[2] & 0xff);
state->fpgaInitB = buf[6] & 0xff; state->fpgaInitB = buf[6] & 0xff;
state->fpgaFlashResult = buf[7]; state->fpgaFlashResult = buf[7];
state->fpgaFlashBitSwap = buf[8] != 0; state->fpgaFlashBitSwap = (buf[8] != 0);
return 0; return 0;
} }
@ -124,15 +123,13 @@ static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* fir
int tries, cnt, buf_p, i; int tries, cnt, buf_p, i;
FILE *fp; FILE *fp;
if (!libztex_checkCapability(ztex, CAPABILITY_FPGA)) { if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
return -1; return -1;
}
libztex_getFpgaState(ztex, &state); libztex_getFpgaState(ztex, &state);
if (!force) { if (!force && state.fpgaConfigured) {
if (state.fpgaConfigured) { applog(LOG_DEBUG, "Bitstream already configured");
return 1; return 1;
}
} }
for (tries=10; tries>0; tries--) { for (tries=10; tries>0; tries--) {
@ -151,7 +148,7 @@ static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* fir
if (feof(fp)) if (feof(fp))
pos--; pos--;
if ( bs<0 || bs>1 ) if ( bs != 0 && bs != 1 )
bs = libztex_detectBitstreamBitOrder(buf, transactionBytes<pos ? transactionBytes : pos); bs = libztex_detectBitstreamBitOrder(buf, transactionBytes<pos ? transactionBytes : pos);
//* Reset fpga //* Reset fpga
@ -201,7 +198,7 @@ static int libztex_configureFpgaLS (struct libztex_device *ztex, const char* fir
return 3; return 3;
} }
usleep(200000); usleep(200000);
applog(LOG_ERR, "%s: FPGA configuration done", ztex->repr); applog(LOG_INFO, "%s: FPGA configuration done", ztex->repr);
return 0; return 0;
} }
@ -218,9 +215,8 @@ int libztex_configureFpga (struct libztex_device *ztex) {
int libztex_setFreq (struct libztex_device *ztex, uint16_t freq) { int libztex_setFreq (struct libztex_device *ztex, uint16_t freq) {
int cnt; int cnt;
if (freq > ztex->freqMaxM) { if (freq > ztex->freqMaxM)
freq = ztex->freqMaxM; freq = ztex->freqMaxM;
}
cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x83, freq, 0, NULL, 0, 500); cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x83, freq, 0, NULL, 0, 500);
if (unlikely(cnt < 0)) { 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, 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)) { if (unlikely(cnt < 0)) {
applog(LOG_ERR, "Ztex check device: Failed to read device snString with err %d", cnt); applog(LOG_ERR, "Ztex check device: Failed to read device snString with err %d", cnt);
return cnt; return cnt;
@ -317,8 +313,8 @@ int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** z
return cnt; return cnt;
} }
if (unlikely(!(buf[0]) == 4)) { if (unlikely(buf[0] != 4)) {
if (unlikely(buf[0]) != 2) { if (unlikely(buf[0] != 2)) {
applog(LOG_ERR, "Invalid BTCMiner descriptor version. Firmware must be updated (%d).", buf[0]); applog(LOG_ERR, "Invalid BTCMiner descriptor version. Firmware must be updated (%d).", buf[0]);
return 3; return 3;
} }
@ -326,7 +322,7 @@ int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** z
} }
newdev->numNonces = buf[1] + 1; 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->freqM1 = ( (buf[4] & 255) | ((buf[5] & 255) << 8) ) * 0.01;
newdev->freqMaxM = (buf[7] & 255); newdev->freqMaxM = (buf[7] & 255);
newdev->freqM = (buf[6] & 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++) { for (i = 0; i < found; i++) {
err = libztex_prepare_device(list[usbdevices[i]], &ztex); err = libztex_prepare_device(list[usbdevices[i]], &ztex);
if (unlikely(err != 0)) { if (unlikely(err != 0))
applog(LOG_ERR, "prepare device: %d", err); applog(LOG_ERR, "prepare device: %d", err);
}
// check if valid // check if valid
if (!ztex->valid) { if (!ztex->valid) {
libztex_destroy_device(ztex); 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] = malloc(sizeof(struct libztex_dev_list));
devs[pos]->dev = ztex; devs[pos]->dev = ztex;
devs[pos]->next = NULL; devs[pos]->next = NULL;
if (pos > 0) { if (pos > 0)
devs[pos-1]->next = devs[pos]; devs[pos-1]->next = devs[pos];
}
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 libztex_sendHashData (struct libztex_device *ztex, unsigned char *sendbuf) {
int cnt; int cnt;
if (ztex == NULL || ztex->hndl == NULL) { if (ztex == NULL || ztex->hndl == NULL)
return 0; return 0;
}
cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x80, 0, 0, sendbuf, 44, 1000); 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); applog(LOG_ERR, "%s: Failed sendHashData with err %d", ztex->repr, cnt);
}
return cnt; return cnt;
} }
@ -433,9 +425,8 @@ int libztex_readHashData (struct libztex_device *ztex, struct libztex_hash_data
unsigned char rbuf[12*8]; unsigned char rbuf[12*8];
int cnt, i; int cnt, i;
if (ztex->hndl == NULL) { if (ztex->hndl == NULL)
return 0; return 0;
}
cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x81, 0, 0, rbuf, 12*ztex->numNonces, 1000); cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x81, 0, 0, rbuf, 12*ztex->numNonces, 1000);
if (unlikely(cnt < 0)) { if (unlikely(cnt < 0)) {
@ -458,9 +449,8 @@ void libztex_freeDevList (struct libztex_dev_list **devs) {
ssize_t cnt = 0; ssize_t cnt = 0;
bool done = false; bool done = false;
while (!done) { while (!done) {
if (devs[cnt]->next == NULL) { if (devs[cnt]->next == NULL)
done = true; done = true;
}
free(devs[cnt++]); free(devs[cnt++]);
} }
free(devs); free(devs);

48
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); 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); libztex_freeDevList(ztex_devices);
}
} }
static bool ztex_updateFreq (struct libztex_device* ztex) static bool ztex_updateFreq (struct libztex_device* ztex)
@ -70,10 +68,9 @@ static bool ztex_updateFreq (struct libztex_device* ztex)
int i, maxM, bestM; int i, maxM, bestM;
double bestR, r; 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)) if (ztex->maxErrorRate[i+1]*i < ztex->maxErrorRate[i]*(i+20))
ztex->maxErrorRate[i+1] = ztex->maxErrorRate[i]*(1.0+20.0/i); ztex->maxErrorRate[i+1] = ztex->maxErrorRate[i]*(1.0+20.0/i);
}
maxM = 0; maxM = 0;
while (maxM<ztex->freqMDefault && ztex->maxErrorRate[maxM+1]<LIBZTEX_MAXMAXERRORRATE) 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); libztex_setFreq(ztex, bestM);
}
maxM = ztex->freqMDefault; maxM = ztex->freqMDefault;
while (maxM<ztex->freqMaxM && ztex->errorWeight[maxM+1] > 100) 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); i = libztex_sendHashData(ztex, sendbuf);
if (i < 0) { if (i < 0) {
// Something wrong happened in send // 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); usleep(500000);
i = libztex_sendHashData(ztex, sendbuf); i = libztex_sendHashData(ztex, sendbuf);
if (i < 0) { if (i < 0) {
// And there's nothing we can do about it // And there's nothing we can do about it
ztex_disable(thr); ztex_disable(thr);
applog(LOG_ERR, "%s: Failed to send hash data with err %d, giving up", ztex->repr, i);
return 0; return 0;
} }
} }
applog(LOG_DEBUG, "sent hashdata"); applog(LOG_DEBUG, "sent hashdata");
for (i=0; i<ztex->numNonces; i++) { for (i=0; i<ztex->numNonces; i++)
lastnonce[i] = 0; lastnonce[i] = 0;
}
overflow = false; overflow = false;
while (!(overflow || work_restart[thr->id].restart)) { while (!(overflow || work_restart[thr->id].restart)) {
usleep(250000); usleep(250000);
if (work_restart[thr->id].restart) { if (work_restart[thr->id].restart) {
applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
break; break;
} }
i = libztex_readHashData(ztex, &hdata[0]); i = libztex_readHashData(ztex, &hdata[0]);
if (i < 0) { if (i < 0) {
// Something wrong happened in read // 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); usleep(500000);
i = libztex_readHashData(ztex, &hdata[0]); i = libztex_readHashData(ztex, &hdata[0]);
if (i < 0) { if (i < 0) {
// And there's nothing we can do about it // And there's nothing we can do about it
ztex_disable(thr); ztex_disable(thr);
applog(LOG_ERR, "%s: Failed to read hash data with err %d, giving up", ztex->repr, i);
return 0; return 0;
} }
} }
if (work_restart[thr->id].restart) { if (work_restart[thr->id].restart) {
applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
break; break;
} }
@ -231,17 +231,16 @@ static uint64_t ztex_scanhash(struct thr_info *thr, struct work *work,
} }
} }
if (!found) { if (!found) {
// new nonce found! applog(LOG_DEBUG, "%s: Share found", ztex->repr);
backlog[backlog_p++] = nonce; backlog[backlog_p++] = nonce;
if (backlog_p >= GOLDEN_BACKLOG) { if (backlog_p >= GOLDEN_BACKLOG)
backlog_p = 0; backlog_p = 0;
}
#if defined(__BIGENDIAN__) || defined(MIPSEB) #if defined(__BIGENDIAN__) || defined(MIPSEB)
nonce = swab32(nonce); nonce = swab32(nonce);
#endif #endif
work->blk.nonce = 0xffffffff; work->blk.nonce = 0xffffffff;
rv = submit_nonce(thr, work, nonce); rv = submit_nonce(thr, work, nonce);
applog(LOG_DEBUG, "submitted %0.8X %d", nonce, rv); 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); 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]; 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! // Something really serious happened, so mark this thread as dead!
return 0; return 0;
}
applog(LOG_DEBUG, "exit %1.8X", noncecnt); applog(LOG_DEBUG, "%s: exit %1.8X", ztex->repr, noncecnt);
work->blk.nonce = 0xffffffff; work->blk.nonce = 0xffffffff;
@ -281,18 +279,20 @@ static bool ztex_prepare(struct thr_info *thr)
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
get_datestamp(ztex->init, &now); get_datestamp(ztex->init, &now);
if (libztex_configureFpga(ztex->device) != 0) { if (libztex_configureFpga(ztex->device) != 0)
return false; return false;
}
ztex->device->freqM = -1; ztex->device->freqM = -1;
ztex_updateFreq(ztex->device); ztex_updateFreq(ztex->device);
applog(LOG_DEBUG, "%s: prepare", ztex->device->repr);
return true; return true;
} }
static void ztex_shutdown(struct thr_info *thr) static void ztex_shutdown(struct thr_info *thr)
{ {
if (thr->cgpu->device != NULL) { if (thr->cgpu->device != NULL) {
applog(LOG_DEBUG, "%s: shutdown", thr->cgpu->device->repr);
libztex_destroy_device(thr->cgpu->device); libztex_destroy_device(thr->cgpu->device);
thr->cgpu->device = NULL; thr->cgpu->device = NULL;
} }

Loading…
Cancel
Save