mirror of https://github.com/GOSTSec/sgminer
Con Kolivas
11 years ago
29 changed files with 931 additions and 1593 deletions
@ -1,24 +0,0 @@ |
|||||||
All the bitstream files included in this directory that follow the name pattern ztex_*.bit are: |
|
||||||
|
|
||||||
---- |
|
||||||
|
|
||||||
Copyright (C) 2009-2011 ZTEX GmbH. |
|
||||||
http://www.ztex.de |
|
||||||
|
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or modify |
|
||||||
it under the terms of the GNU General Public License version 3 as |
|
||||||
published by the Free Software Foundation. |
|
||||||
|
|
||||||
This program is distributed in the hope that it will be useful, but |
|
||||||
WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
||||||
General Public License for more details. |
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License |
|
||||||
along with this program; if not, see http://www.gnu.org/licenses/. |
|
||||||
|
|
||||||
---- |
|
||||||
|
|
||||||
You can find the original sources at the BTCMiner project home page: http://www.ztex.de/btcminer/ |
|
||||||
|
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,719 @@ |
|||||||
|
/*
|
||||||
|
* Copyright 2013 Andrew Smith |
||||||
|
* Copyright 2013 Con Kolivas |
||||||
|
* Copyright 2013 Chris Savery |
||||||
|
* |
||||||
|
* This program is free software; you can redistribute it and/or modify it |
||||||
|
* under the terms of the GNU General Public License as published by the Free |
||||||
|
* Software Foundation; either version 3 of the License, or (at your option) |
||||||
|
* any later version. See COPYING for more details. |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <float.h> |
||||||
|
#include <limits.h> |
||||||
|
#include <pthread.h> |
||||||
|
#include <stdint.h> |
||||||
|
#include <stdio.h> |
||||||
|
#include <strings.h> |
||||||
|
#include <sys/time.h> |
||||||
|
#include <unistd.h> |
||||||
|
#include <math.h> |
||||||
|
|
||||||
|
#include "config.h" |
||||||
|
|
||||||
|
#ifdef WIN32 |
||||||
|
#include <windows.h> |
||||||
|
#endif |
||||||
|
|
||||||
|
#include "compat.h" |
||||||
|
#include "miner.h" |
||||||
|
#include "usbutils.h" |
||||||
|
|
||||||
|
#define K1 "K1" |
||||||
|
#define K16 "K16" |
||||||
|
#define K64 "K64" |
||||||
|
|
||||||
|
#define MIDSTATE_BYTES 32 |
||||||
|
#define MERKLE_OFFSET 64 |
||||||
|
#define MERKLE_BYTES 12 |
||||||
|
|
||||||
|
#define REPLY_SIZE 15 // adequate for all types of replies
|
||||||
|
#define REPLY_BUFSIZE 16 // reply + 1 byte to mark used
|
||||||
|
#define MAX_REPLY_COUNT 32 // more unhandled replies than this will result in data loss
|
||||||
|
#define REPLY_WAIT_TIME 100 // poll interval for a cmd waiting it's reply
|
||||||
|
#define CMD_REPLY_RETRIES 8 // how many retries for cmds
|
||||||
|
#define MAX_WORK_COUNT 4 // for now, must be binary multiple and match firmware
|
||||||
|
#define TACH_FACTOR 87890 // fan rpm divisor
|
||||||
|
|
||||||
|
struct device_drv klondike_drv; |
||||||
|
|
||||||
|
typedef struct klondike_id { |
||||||
|
uint8_t version; |
||||||
|
uint8_t product[7]; |
||||||
|
uint32_t serial; |
||||||
|
} IDENTITY; |
||||||
|
|
||||||
|
typedef struct klondike_status { |
||||||
|
uint8_t state; |
||||||
|
uint8_t chipcount; |
||||||
|
uint8_t slavecount; |
||||||
|
uint8_t workqc; |
||||||
|
uint8_t workid; |
||||||
|
uint8_t temp; |
||||||
|
uint8_t fanspeed; |
||||||
|
uint8_t errorcount; |
||||||
|
uint16_t hashcount; |
||||||
|
uint16_t maxcount; |
||||||
|
uint8_t noise; |
||||||
|
} WORKSTATUS; |
||||||
|
|
||||||
|
typedef struct _worktask { |
||||||
|
uint16_t pad1; |
||||||
|
uint8_t pad2; |
||||||
|
uint8_t workid; |
||||||
|
uint32_t midstate[8]; |
||||||
|
uint32_t merkle[3]; |
||||||
|
} WORKTASK; |
||||||
|
|
||||||
|
typedef struct _workresult { |
||||||
|
uint16_t pad; |
||||||
|
uint8_t device; |
||||||
|
uint8_t workid; |
||||||
|
uint32_t nonce; |
||||||
|
} WORKRESULT; |
||||||
|
|
||||||
|
typedef struct klondike_cfg { |
||||||
|
uint16_t hashclock; |
||||||
|
uint8_t temptarget; |
||||||
|
uint8_t tempcritical; |
||||||
|
uint8_t fantarget; |
||||||
|
uint8_t pad; |
||||||
|
} WORKCFG; |
||||||
|
|
||||||
|
typedef struct device_info { |
||||||
|
uint32_t noncecount; |
||||||
|
uint32_t nextworkid; |
||||||
|
uint16_t lasthashcount; |
||||||
|
uint64_t totalhashcount; |
||||||
|
uint32_t rangesize; |
||||||
|
uint32_t *chipstats; |
||||||
|
} DEVINFO; |
||||||
|
|
||||||
|
struct klondike_info { |
||||||
|
bool shutdown; |
||||||
|
pthread_rwlock_t stat_lock; |
||||||
|
struct thr_info replies_thr; |
||||||
|
WORKSTATUS *status; |
||||||
|
DEVINFO *devinfo; |
||||||
|
WORKCFG *cfg; |
||||||
|
char *replies; |
||||||
|
int nextreply; |
||||||
|
int noncecount; |
||||||
|
uint64_t hashcount; |
||||||
|
uint64_t errorcount; |
||||||
|
uint64_t noisecount; |
||||||
|
}; |
||||||
|
|
||||||
|
IDENTITY KlondikeID; |
||||||
|
|
||||||
|
static double cvtKlnToC(uint8_t temp) |
||||||
|
{ |
||||||
|
double Rt, stein, celsius; |
||||||
|
|
||||||
|
Rt = 1000.0 * 255.0 / (double)temp - 1000.0; |
||||||
|
|
||||||
|
stein = log(Rt / 2200.0) / 3987.0; |
||||||
|
|
||||||
|
stein += 1.0 / (double)(25.0 + 273.15); |
||||||
|
|
||||||
|
celsius = (1.0 / stein) - 273.15; |
||||||
|
|
||||||
|
return celsius; |
||||||
|
} |
||||||
|
|
||||||
|
static int cvtCToKln(double deg) |
||||||
|
{ |
||||||
|
double R = exp((1/(deg+273.15)-1/(273.15+25))*3987)*2200; |
||||||
|
return 256*R/(R+1000); |
||||||
|
} |
||||||
|
|
||||||
|
static char *SendCmdGetReply(struct cgpu_info *klncgpu, char Cmd, int device, int datalen, void *data) |
||||||
|
{ |
||||||
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data); |
||||||
|
char outbuf[64]; |
||||||
|
int retries = CMD_REPLY_RETRIES; |
||||||
|
int chkreply = klninfo->nextreply; |
||||||
|
int sent, err; |
||||||
|
|
||||||
|
if (klncgpu->usbinfo.nodev) |
||||||
|
return NULL; |
||||||
|
|
||||||
|
outbuf[0] = Cmd; |
||||||
|
outbuf[1] = device; |
||||||
|
memcpy(outbuf+2, data, datalen); |
||||||
|
err = usb_write(klncgpu, outbuf, 2+datalen, &sent, C_REQUESTRESULTS); |
||||||
|
if (err < 0 || sent != 2+datalen) { |
||||||
|
applog(LOG_ERR, "%s (%s) Cmd:%c Dev:%d, write failed (%d:%d)", klncgpu->drv->dname, klncgpu->device_path, Cmd, device, sent, err); |
||||||
|
} |
||||||
|
while (retries-- > 0 && klninfo->shutdown == false) { |
||||||
|
cgsleep_ms(REPLY_WAIT_TIME); |
||||||
|
while (*(klninfo->replies + chkreply*REPLY_BUFSIZE) != Cmd || *(klninfo->replies + chkreply*REPLY_BUFSIZE + 2) != device) { |
||||||
|
if (++chkreply == MAX_REPLY_COUNT) |
||||||
|
chkreply = 0; |
||||||
|
if (chkreply == klninfo->nextreply) |
||||||
|
break; |
||||||
|
} |
||||||
|
if (chkreply == klninfo->nextreply) |
||||||
|
continue; |
||||||
|
*(klninfo->replies + chkreply*REPLY_BUFSIZE) = '!'; // mark to prevent re-use
|
||||||
|
return klninfo->replies + chkreply*REPLY_BUFSIZE + 1; |
||||||
|
} |
||||||
|
return NULL; |
||||||
|
} |
||||||
|
|
||||||
|
static bool klondike_get_stats(struct cgpu_info *klncgpu) |
||||||
|
{ |
||||||
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data); |
||||||
|
int slaves, dev; |
||||||
|
|
||||||
|
if (klncgpu->usbinfo.nodev || klninfo->status == NULL) |
||||||
|
return false; |
||||||
|
|
||||||
|
applog(LOG_DEBUG, "Klondike getting status"); |
||||||
|
slaves = klninfo->status[0].slavecount; |
||||||
|
|
||||||
|
// loop thru devices and get status for each
|
||||||
|
wr_lock(&(klninfo->stat_lock)); |
||||||
|
for (dev = 0; dev <= slaves; dev++) { |
||||||
|
char *reply = SendCmdGetReply(klncgpu, 'S', dev, 0, NULL); |
||||||
|
if (reply != NULL) |
||||||
|
memcpy((void *)(&(klninfo->status[dev])), reply+2, sizeof(klninfo->status[dev])); |
||||||
|
} |
||||||
|
wr_unlock(&(klninfo->stat_lock)); |
||||||
|
|
||||||
|
// todo: detect slavecount change and realloc space
|
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
static bool klondike_init(struct cgpu_info *klncgpu) |
||||||
|
{ |
||||||
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data); |
||||||
|
int slaves, dev; |
||||||
|
|
||||||
|
char *reply = SendCmdGetReply(klncgpu, 'S', 0, 0, NULL); |
||||||
|
if (reply == NULL) |
||||||
|
return false; |
||||||
|
|
||||||
|
slaves = ((WORKSTATUS *)(reply+2))->slavecount; |
||||||
|
if (klninfo->status == NULL) { |
||||||
|
applog(LOG_DEBUG, "Klondike initializing data"); |
||||||
|
|
||||||
|
// alloc space for status, devinfo and cfg for master and slaves
|
||||||
|
klninfo->status = calloc(slaves+1, sizeof(WORKSTATUS)); |
||||||
|
if (unlikely(!klninfo->status)) |
||||||
|
quit(1, "Failed to calloc status array in klondke_get_stats"); |
||||||
|
klninfo->devinfo = calloc(slaves+1, sizeof(DEVINFO)); |
||||||
|
if (unlikely(!klninfo->devinfo)) |
||||||
|
quit(1, "Failed to calloc devinfo array in klondke_get_stats"); |
||||||
|
klninfo->cfg = calloc(slaves+1, sizeof(WORKCFG)); |
||||||
|
if (unlikely(!klninfo->cfg)) |
||||||
|
quit(1, "Failed to calloc cfg array in klondke_get_stats"); |
||||||
|
} |
||||||
|
|
||||||
|
WORKCFG cfgset = { 0,0,0,0,0 }; // zero init triggers read back only
|
||||||
|
double temp1, temp2; |
||||||
|
int size = 2; |
||||||
|
|
||||||
|
if (opt_klondike_options != NULL) { // boundaries are checked by device, with valid values returned
|
||||||
|
sscanf(opt_klondike_options, "%hu:%lf:%lf:%hhu", &cfgset.hashclock, &temp1, &temp2, &cfgset.fantarget); |
||||||
|
cfgset.temptarget = cvtCToKln(temp1); |
||||||
|
cfgset.tempcritical = cvtCToKln(temp2); |
||||||
|
cfgset.fantarget = (int)255*cfgset.fantarget/100; |
||||||
|
size = sizeof(cfgset); |
||||||
|
} |
||||||
|
|
||||||
|
for (dev = 0; dev <= slaves; dev++) { |
||||||
|
char *reply = SendCmdGetReply(klncgpu, 'C', dev, size, &cfgset); |
||||||
|
if (reply != NULL) { |
||||||
|
klninfo->cfg[dev] = *(WORKCFG *)(reply+2); |
||||||
|
applog(LOG_NOTICE, "Klondike config (%d: Clk: %d, T:%.0lf, C:%.0lf, F:%d)", |
||||||
|
dev, klninfo->cfg[dev].hashclock, |
||||||
|
cvtKlnToC(klninfo->cfg[dev].temptarget), |
||||||
|
cvtKlnToC(klninfo->cfg[dev].tempcritical), |
||||||
|
(int)100*klninfo->cfg[dev].fantarget/256); |
||||||
|
} |
||||||
|
} |
||||||
|
klondike_get_stats(klncgpu); |
||||||
|
for (dev = 0; dev <= slaves; dev++) { |
||||||
|
klninfo->devinfo[dev].rangesize = ((uint64_t)1<<32) / klninfo->status[dev].chipcount; |
||||||
|
klninfo->devinfo[dev].chipstats = calloc(klninfo->status[dev].chipcount*2 , sizeof(uint32_t)); |
||||||
|
} |
||||||
|
|
||||||
|
SendCmdGetReply(klncgpu, 'E', 0, 1, "1"); |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found) |
||||||
|
{ |
||||||
|
struct cgpu_info *klncgpu = usb_alloc_cgpu(&klondike_drv, 1); |
||||||
|
struct klondike_info *klninfo = NULL; |
||||||
|
|
||||||
|
if (unlikely(!klncgpu)) |
||||||
|
quit(1, "Failed to calloc klncgpu in klondike_detect_one"); |
||||||
|
|
||||||
|
klninfo = calloc(1, sizeof(*klninfo)); |
||||||
|
if (unlikely(!klninfo)) |
||||||
|
quit(1, "Failed to calloc klninfo in klondke_detect_one"); |
||||||
|
klncgpu->device_data = (FILE *)klninfo; |
||||||
|
|
||||||
|
klninfo->replies = calloc(MAX_REPLY_COUNT, REPLY_BUFSIZE); |
||||||
|
if (unlikely(!klninfo->replies)) |
||||||
|
quit(1, "Failed to calloc replies buffer in klondke_detect_one"); |
||||||
|
klninfo->nextreply = 0; |
||||||
|
|
||||||
|
if (usb_init(klncgpu, dev, found)) { |
||||||
|
int attempts = 0; |
||||||
|
while (attempts++ < 3) { |
||||||
|
char devpath[20], reply[REPLY_SIZE]; |
||||||
|
int sent, recd, err; |
||||||
|
|
||||||
|
sprintf(devpath, "%d:%d", (int)(klncgpu->usbinfo.bus_number), (int)(klncgpu->usbinfo.device_address)); |
||||||
|
err = usb_write(klncgpu, "I", 2, &sent, C_REQUESTRESULTS); |
||||||
|
if (err < 0 || sent != 2) { |
||||||
|
applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)", klncgpu->drv->dname, devpath, sent, err); |
||||||
|
} |
||||||
|
cgsleep_ms(REPLY_WAIT_TIME*10); |
||||||
|
err = usb_read(klncgpu, reply, REPLY_SIZE, &recd, C_GETRESULTS); |
||||||
|
if (err < 0) { |
||||||
|
applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)", klncgpu->drv->dname, devpath, recd, err); |
||||||
|
} else if (recd < 1) { |
||||||
|
applog(LOG_ERR, "%s (%s) detect empty reply (%d)", klncgpu->drv->dname, devpath, recd); |
||||||
|
} else if (reply[0] == 'I' && reply[1] == 0) { |
||||||
|
|
||||||
|
applog(LOG_DEBUG, "%s (%s) detect successful", klncgpu->drv->dname, devpath); |
||||||
|
KlondikeID = *(IDENTITY *)(&reply[2]); |
||||||
|
klncgpu->device_path = strdup(devpath); |
||||||
|
update_usb_stats(klncgpu); |
||||||
|
if (!add_cgpu(klncgpu)) |
||||||
|
break; |
||||||
|
applog(LOG_DEBUG, "Klondike cgpu added"); |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
||||||
|
usb_uninit(klncgpu); |
||||||
|
} |
||||||
|
free(klninfo->replies); |
||||||
|
free(klncgpu); |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
static void klondike_detect(bool __maybe_unused hotplug) |
||||||
|
{ |
||||||
|
usb_detect(&klondike_drv, klondike_detect_one); |
||||||
|
} |
||||||
|
|
||||||
|
static void klondike_identify(__maybe_unused struct cgpu_info *klncgpu) |
||||||
|
{ |
||||||
|
//SendCmdGetReply(klncgpu, 'I', 0, 0, NULL);
|
||||||
|
} |
||||||
|
|
||||||
|
static void klondike_check_nonce(struct cgpu_info *klncgpu, WORKRESULT *result) |
||||||
|
{ |
||||||
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data); |
||||||
|
struct work *work, *tmp; |
||||||
|
|
||||||
|
applog(LOG_DEBUG, "Klondike FOUND NONCE (%02x:%08x)", result->workid, result->nonce); |
||||||
|
|
||||||
|
HASH_ITER(hh, klncgpu->queued_work, work, tmp) { |
||||||
|
if (work->queued && (work->subid == (result->device*256 + result->workid))) { |
||||||
|
|
||||||
|
wr_lock(&(klninfo->stat_lock)); |
||||||
|
klninfo->devinfo[result->device].noncecount++; |
||||||
|
klninfo->noncecount++; |
||||||
|
wr_unlock(&(klninfo->stat_lock)); |
||||||
|
|
||||||
|
result->nonce = le32toh(result->nonce - 0xC0); |
||||||
|
applog(LOG_DEBUG, "Klondike SUBMIT NONCE (%02x:%08x)", result->workid, result->nonce); |
||||||
|
bool ok = submit_nonce(klncgpu->thr[0], work, result->nonce); |
||||||
|
|
||||||
|
applog(LOG_DEBUG, "Klondike chip stats %d, %08x, %d, %d", result->device, result->nonce, klninfo->devinfo[result->device].rangesize, klninfo->status[result->device].chipcount); |
||||||
|
klninfo->devinfo[result->device].chipstats[(result->nonce / klninfo->devinfo[result->device].rangesize) + (ok ? 0 : klninfo->status[result->device].chipcount)]++; |
||||||
|
return; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored", |
||||||
|
klncgpu->drv->name, klncgpu->device_id, result->device, result->workid, result->nonce); |
||||||
|
//inc_hw_errors(klncgpu->thr[0]);
|
||||||
|
} |
||||||
|
|
||||||
|
// Change this to LOG_WARNING if you wish to always see the replies
|
||||||
|
#define READ_DEBUG LOG_DEBUG |
||||||
|
|
||||||
|
// thread to keep looking for replies
|
||||||
|
static void *klondike_get_replies(void *userdata) |
||||||
|
{ |
||||||
|
struct cgpu_info *klncgpu = (struct cgpu_info *)userdata; |
||||||
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data); |
||||||
|
struct klondike_status *ks; |
||||||
|
struct _workresult *wr; |
||||||
|
struct klondike_cfg *kc; |
||||||
|
struct klondike_id *ki; |
||||||
|
char *replybuf; |
||||||
|
int err, recd; |
||||||
|
|
||||||
|
applog(LOG_DEBUG, "Klondike listening for replies"); |
||||||
|
|
||||||
|
while (klninfo->shutdown == false) { |
||||||
|
if (klncgpu->usbinfo.nodev) |
||||||
|
return NULL; |
||||||
|
|
||||||
|
replybuf = klninfo->replies + klninfo->nextreply * REPLY_BUFSIZE; |
||||||
|
replybuf[0] = 0; |
||||||
|
|
||||||
|
err = usb_read(klncgpu, replybuf+1, REPLY_SIZE, &recd, C_GETRESULTS); |
||||||
|
if (!err && recd == REPLY_SIZE) { |
||||||
|
if (opt_log_level <= READ_DEBUG) { |
||||||
|
char *hexdata = bin2hex((unsigned char *)(replybuf+1), recd); |
||||||
|
applog(READ_DEBUG, "%s (%s) reply [%s:%s]", klncgpu->drv->dname, klncgpu->device_path, replybuf+1, hexdata); |
||||||
|
free(hexdata); |
||||||
|
} |
||||||
|
if (++klninfo->nextreply == MAX_REPLY_COUNT) |
||||||
|
klninfo->nextreply = 0; |
||||||
|
|
||||||
|
replybuf[0] = replybuf[1]; |
||||||
|
switch (replybuf[0]) { |
||||||
|
case '=': |
||||||
|
wr = (struct _workresult *)(replybuf+1); |
||||||
|
klondike_check_nonce(klncgpu, (WORKRESULT *)replybuf); |
||||||
|
applog(READ_DEBUG, |
||||||
|
"%s (%s) reply: work [%c] device=%d workid=%d" |
||||||
|
" nonce=0x%08x", |
||||||
|
klncgpu->drv->dname, klncgpu->device_path, |
||||||
|
*(replybuf+1), |
||||||
|
(int)(wr->device), |
||||||
|
(int)(wr->workid), |
||||||
|
(unsigned int)(wr->nonce)); |
||||||
|
break; |
||||||
|
case 'S': |
||||||
|
case 'W': |
||||||
|
case 'A': |
||||||
|
case 'E': |
||||||
|
ks = (struct klondike_status *)(replybuf+1); |
||||||
|
wr_lock(&(klninfo->stat_lock)); |
||||||
|
klninfo->errorcount += ks->errorcount; |
||||||
|
klninfo->noisecount += ks->noise; |
||||||
|
wr_unlock(&(klninfo->stat_lock)); |
||||||
|
applog(READ_DEBUG, |
||||||
|
"%s (%s) reply: status [%c] chips=%d slaves=%d" |
||||||
|
" workcq=%d workid=%d temp=%d fan=%d errors=%d" |
||||||
|
" hashes=%d max=%d noise=%d", |
||||||
|
klncgpu->drv->dname, klncgpu->device_path, |
||||||
|
*(replybuf+1), |
||||||
|
(int)(ks->chipcount), |
||||||
|
(int)(ks->slavecount), |
||||||
|
(int)(ks->workqc), |
||||||
|
(int)(ks->workid), |
||||||
|
(int)(ks->temp), |
||||||
|
(int)(ks->fanspeed), |
||||||
|
(int)(ks->errorcount), |
||||||
|
(int)(ks->hashcount), |
||||||
|
(int)(ks->maxcount), |
||||||
|
(int)(ks->noise)); |
||||||
|
break; |
||||||
|
case 'C': |
||||||
|
kc = (struct klondike_cfg *)(replybuf+2); |
||||||
|
applog(READ_DEBUG, |
||||||
|
"%s (%s) reply: config [%c] clock=%d temptarget=%d" |
||||||
|
" tempcrit=%d fan=%d", |
||||||
|
klncgpu->drv->dname, klncgpu->device_path, |
||||||
|
*(replybuf+1), |
||||||
|
(int)(kc->hashclock), |
||||||
|
(int)(kc->temptarget), |
||||||
|
(int)(kc->tempcritical), |
||||||
|
(int)(kc->fantarget)); |
||||||
|
break; |
||||||
|
case 'I': |
||||||
|
ki = (struct klondike_id *)(replybuf+2); |
||||||
|
applog(READ_DEBUG, |
||||||
|
"%s (%s) reply: info [%c] version=0x%02x prod=%.7s" |
||||||
|
" serial=0x%08x", |
||||||
|
klncgpu->drv->dname, klncgpu->device_path, |
||||||
|
*(replybuf+1), |
||||||
|
(int)(ki->version), |
||||||
|
ki->product, |
||||||
|
(unsigned int)(ki->serial)); |
||||||
|
break; |
||||||
|
default: |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return NULL; |
||||||
|
} |
||||||
|
|
||||||
|
static void klondike_flush_work(struct cgpu_info *klncgpu) |
||||||
|
{ |
||||||
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data); |
||||||
|
int dev; |
||||||
|
|
||||||
|
applog(LOG_DEBUG, "Klondike flushing work"); |
||||||
|
for (dev = 0; dev <= klninfo->status->slavecount; dev++) { |
||||||
|
char *reply = SendCmdGetReply(klncgpu, 'A', dev, 0, NULL); |
||||||
|
if (reply != NULL) { |
||||||
|
wr_lock(&(klninfo->stat_lock)); |
||||||
|
klninfo->status[dev] = *(WORKSTATUS *)(reply+2); |
||||||
|
wr_unlock(&(klninfo->stat_lock)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static bool klondike_thread_prepare(struct thr_info *thr) |
||||||
|
{ |
||||||
|
struct cgpu_info *klncgpu = thr->cgpu; |
||||||
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data); |
||||||
|
|
||||||
|
if (thr_info_create(&(klninfo->replies_thr), NULL, klondike_get_replies, (void *)klncgpu)) { |
||||||
|
applog(LOG_ERR, "%s%i: thread create failed", klncgpu->drv->name, klncgpu->device_id); |
||||||
|
return false; |
||||||
|
} |
||||||
|
pthread_detach(klninfo->replies_thr.pth); |
||||||
|
|
||||||
|
// let the listening get started
|
||||||
|
cgsleep_ms(100); |
||||||
|
|
||||||
|
return klondike_init(klncgpu); |
||||||
|
} |
||||||
|
|
||||||
|
static bool klondike_thread_init(struct thr_info *thr) |
||||||
|
{ |
||||||
|
struct cgpu_info *klncgpu = thr->cgpu; |
||||||
|
|
||||||
|
if (klncgpu->usbinfo.nodev) |
||||||
|
return false; |
||||||
|
|
||||||
|
klondike_flush_work(klncgpu); |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
static void klondike_shutdown(struct thr_info *thr) |
||||||
|
{ |
||||||
|
struct cgpu_info *klncgpu = thr->cgpu; |
||||||
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data); |
||||||
|
int dev; |
||||||
|
|
||||||
|
applog(LOG_DEBUG, "Klondike shutting down work"); |
||||||
|
for (dev = 0; dev <= klninfo->status->slavecount; dev++) { |
||||||
|
SendCmdGetReply(klncgpu, 'E', dev, 1, "0"); |
||||||
|
} |
||||||
|
klncgpu->shutdown = klninfo->shutdown = true; |
||||||
|
} |
||||||
|
|
||||||
|
static void klondike_thread_enable(struct thr_info *thr) |
||||||
|
{ |
||||||
|
struct cgpu_info *klncgpu = thr->cgpu; |
||||||
|
|
||||||
|
if (klncgpu->usbinfo.nodev) |
||||||
|
return; |
||||||
|
|
||||||
|
//SendCmdGetReply(klncgpu, 'E', 0, 1, "0");
|
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work) |
||||||
|
{ |
||||||
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data); |
||||||
|
struct work *tmp; |
||||||
|
WORKTASK data; |
||||||
|
|
||||||
|
if (klncgpu->usbinfo.nodev) |
||||||
|
return false; |
||||||
|
|
||||||
|
memcpy(data.midstate, work->midstate, MIDSTATE_BYTES); |
||||||
|
memcpy(data.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES); |
||||||
|
data.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF); |
||||||
|
work->subid = dev*256 + data.workid; |
||||||
|
|
||||||
|
if (opt_log_level <= LOG_DEBUG) { |
||||||
|
char *hexdata = bin2hex(&data.workid, sizeof(data)-3); |
||||||
|
applog(LOG_DEBUG, "WORKDATA: %s", hexdata); |
||||||
|
free(hexdata); |
||||||
|
} |
||||||
|
|
||||||
|
applog(LOG_DEBUG, "Klondike sending work (%d:%02x)", dev, data.workid); |
||||||
|
char *reply = SendCmdGetReply(klncgpu, 'W', dev, sizeof(data)-3, &data.workid); |
||||||
|
if (reply != NULL) { |
||||||
|
wr_lock(&(klninfo->stat_lock)); |
||||||
|
klninfo->status[dev] = *(WORKSTATUS *)(reply+2); |
||||||
|
wr_unlock(&(klninfo->stat_lock)); |
||||||
|
|
||||||
|
// remove old work
|
||||||
|
HASH_ITER(hh, klncgpu->queued_work, work, tmp) { |
||||||
|
if (work->queued && (work->subid == (int)(dev*256 + ((klninfo->devinfo[dev].nextworkid-2*MAX_WORK_COUNT) & 0xFF)))) |
||||||
|
work_completed(klncgpu, work); |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
static bool klondike_queue_full(struct cgpu_info *klncgpu) |
||||||
|
{ |
||||||
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data); |
||||||
|
struct work *work = NULL; |
||||||
|
int dev, queued; |
||||||
|
|
||||||
|
for (queued = 0; queued < MAX_WORK_COUNT-1; queued++) |
||||||
|
for (dev = 0; dev <= klninfo->status->slavecount; dev++) |
||||||
|
if (klninfo->status[dev].workqc <= queued) { |
||||||
|
if (!work) |
||||||
|
work = get_queued(klncgpu); |
||||||
|
if (unlikely(!work)) |
||||||
|
return false; |
||||||
|
if (klondike_send_work(klncgpu, dev, work)) { |
||||||
|
work = NULL; |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
static int64_t klondike_scanwork(struct thr_info *thr) |
||||||
|
{ |
||||||
|
struct cgpu_info *klncgpu = thr->cgpu; |
||||||
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data); |
||||||
|
int64_t newhashcount = 0; |
||||||
|
int dev; |
||||||
|
|
||||||
|
if (klncgpu->usbinfo.nodev) |
||||||
|
return -1; |
||||||
|
|
||||||
|
restart_wait(thr, 200); |
||||||
|
if (klninfo->status != NULL) { |
||||||
|
rd_lock(&(klninfo->stat_lock)); |
||||||
|
for (dev = 0; dev <= klninfo->status->slavecount; dev++) { |
||||||
|
uint64_t newhashdev = 0; |
||||||
|
if (klninfo->devinfo[dev].lasthashcount > klninfo->status[dev].hashcount) // todo: chg this to check workid for wrapped instead
|
||||||
|
newhashdev += klninfo->status[dev].maxcount; // hash counter wrapped
|
||||||
|
newhashdev += klninfo->status[dev].hashcount - klninfo->devinfo[dev].lasthashcount; |
||||||
|
klninfo->devinfo[dev].lasthashcount = klninfo->status[dev].hashcount; |
||||||
|
if (klninfo->status[dev].maxcount != 0) |
||||||
|
klninfo->hashcount += (newhashdev << 32) / klninfo->status[dev].maxcount; |
||||||
|
|
||||||
|
// todo: check stats for critical conditions
|
||||||
|
} |
||||||
|
newhashcount += 0xffffffffull * (uint64_t)klninfo->noncecount; |
||||||
|
klninfo->noncecount = 0; |
||||||
|
rd_unlock(&(klninfo->stat_lock)); |
||||||
|
} |
||||||
|
return newhashcount; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
static void get_klondike_statline_before(char *buf, size_t siz, struct cgpu_info *klncgpu) |
||||||
|
{ |
||||||
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data); |
||||||
|
uint8_t temp = 0xFF; |
||||||
|
uint16_t fan = 0; |
||||||
|
int dev; |
||||||
|
|
||||||
|
if (klninfo->status == NULL) |
||||||
|
return; |
||||||
|
|
||||||
|
rd_lock(&(klninfo->stat_lock)); |
||||||
|
for (dev = 0; dev <= klninfo->status->slavecount; dev++) { |
||||||
|
if (klninfo->status[dev].temp < temp) |
||||||
|
temp = klninfo->status[dev].temp; |
||||||
|
fan += klninfo->cfg[dev].fantarget; |
||||||
|
} |
||||||
|
fan /= klninfo->status->slavecount+1; |
||||||
|
rd_unlock(&(klninfo->stat_lock)); |
||||||
|
|
||||||
|
tailsprintf(buf, siz, " %3.0fC %3d%% | ", cvtKlnToC(temp), fan*100/255); |
||||||
|
} |
||||||
|
|
||||||
|
static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu) |
||||||
|
{ |
||||||
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data); |
||||||
|
struct api_data *root = NULL; |
||||||
|
char buf[32]; |
||||||
|
int dev; |
||||||
|
|
||||||
|
if (klninfo->status == NULL) |
||||||
|
return NULL; |
||||||
|
|
||||||
|
rd_lock(&(klninfo->stat_lock)); |
||||||
|
for (dev = 0; dev <= klninfo->status->slavecount; dev++) { |
||||||
|
|
||||||
|
float fTemp = cvtKlnToC(klninfo->status[dev].temp); |
||||||
|
sprintf(buf, "Temp %d", dev); |
||||||
|
root = api_add_temp(root, buf, &fTemp, true); |
||||||
|
|
||||||
|
double dClk = (double)klninfo->cfg[dev].hashclock; |
||||||
|
sprintf(buf, "Clock %d", dev); |
||||||
|
root = api_add_freq(root, buf, &dClk, true); |
||||||
|
|
||||||
|
unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].fantarget / 255; |
||||||
|
sprintf(buf, "Fan Percent %d", dev); |
||||||
|
root = api_add_int(root, buf, (int *)(&iFan), true); |
||||||
|
|
||||||
|
iFan = 0; |
||||||
|
if (klninfo->status[dev].fanspeed > 0) |
||||||
|
iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].fanspeed; |
||||||
|
sprintf(buf, "Fan RPM %d", dev); |
||||||
|
root = api_add_int(root, buf, (int *)(&iFan), true); |
||||||
|
|
||||||
|
if (klninfo->devinfo[dev].chipstats != NULL) { |
||||||
|
char data[2048]; |
||||||
|
char one[32]; |
||||||
|
int n; |
||||||
|
|
||||||
|
sprintf(buf, "Nonces / Chip %d", dev); |
||||||
|
data[0] = '\0'; |
||||||
|
for (n = 0; n < klninfo->status[dev].chipcount; n++) { |
||||||
|
snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n]); |
||||||
|
strcat(data, one); |
||||||
|
} |
||||||
|
root = api_add_string(root, buf, data, true); |
||||||
|
|
||||||
|
sprintf(buf, "Errors / Chip %d", dev); |
||||||
|
data[0] = '\0'; |
||||||
|
for (n = 0; n < klninfo->status[dev].chipcount; n++) { |
||||||
|
snprintf(one, sizeof(one), "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].chipcount]); |
||||||
|
strcat(data, one); |
||||||
|
} |
||||||
|
root = api_add_string(root, buf, data, true); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
root = api_add_uint64(root, "Hash Count", &(klninfo->hashcount), true); |
||||||
|
root = api_add_uint64(root, "Error Count", &(klninfo->errorcount), true); |
||||||
|
root = api_add_uint64(root, "Noise Count", &(klninfo->noisecount), true); |
||||||
|
|
||||||
|
rd_unlock(&(klninfo->stat_lock)); |
||||||
|
|
||||||
|
return root; |
||||||
|
} |
||||||
|
|
||||||
|
struct device_drv klondike_drv = { |
||||||
|
.drv_id = DRIVER_klondike, |
||||||
|
.dname = "Klondike", |
||||||
|
.name = "KLN", |
||||||
|
.drv_detect = klondike_detect, |
||||||
|
.get_api_stats = klondike_api_stats, |
||||||
|
.get_statline_before = get_klondike_statline_before, |
||||||
|
.get_stats = klondike_get_stats, |
||||||
|
.identify_device = klondike_identify, |
||||||
|
.thread_prepare = klondike_thread_prepare, |
||||||
|
.thread_init = klondike_thread_init, |
||||||
|
.hash_work = hash_queued_work, |
||||||
|
.scanwork = klondike_scanwork, |
||||||
|
.queue_full = klondike_queue_full, |
||||||
|
.flush_work = klondike_flush_work, |
||||||
|
.thread_shutdown = klondike_shutdown, |
||||||
|
.thread_enable = klondike_thread_enable |
||||||
|
}; |
@ -1,423 +0,0 @@ |
|||||||
/**
|
|
||||||
* ztex.c - cgminer worker for Ztex 1.15x fpga board |
|
||||||
* |
|
||||||
* Copyright (c) 2012 nelisky.btc@gmail.com |
|
||||||
* |
|
||||||
* This work is based upon the Java SDK provided by ztex which is |
|
||||||
* Copyright (C) 2009-2011 ZTEX GmbH. |
|
||||||
* http://www.ztex.de
|
|
||||||
* |
|
||||||
* This work is based upon the icarus.c worker which was |
|
||||||
* Copyright 2012 Luke Dashjr |
|
||||||
* Copyright 2012 Xiangfu <xiangfu@openmobilefree.com> |
|
||||||
* |
|
||||||
* This program is free software; you can redistribute it and/or modify |
|
||||||
* it under the terms of the GNU General Public License version 2 as |
|
||||||
* published by the Free Software Foundation. |
|
||||||
* |
|
||||||
* This program is distributed in the hope that it will be useful, but |
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
||||||
* General Public License for more details. |
|
||||||
* |
|
||||||
* You should have received a copy of the GNU General Public License |
|
||||||
* along with this program; if not, see http://www.gnu.org/licenses/.
|
|
||||||
**/ |
|
||||||
#include "miner.h" |
|
||||||
#include <unistd.h> |
|
||||||
#include <sha2.h> |
|
||||||
#include "libztex.h" |
|
||||||
#include "util.h" |
|
||||||
|
|
||||||
#define GOLDEN_BACKLOG 5 |
|
||||||
|
|
||||||
// Forward declarations
|
|
||||||
static void ztex_disable(struct thr_info* thr); |
|
||||||
static bool ztex_prepare(struct thr_info *thr); |
|
||||||
|
|
||||||
static void ztex_selectFpga(struct libztex_device* ztex) |
|
||||||
{ |
|
||||||
if (ztex->root->numberOfFpgas > 1) { |
|
||||||
if (ztex->root->selectedFpga != ztex->fpgaNum) |
|
||||||
mutex_lock(&ztex->root->mutex); |
|
||||||
libztex_selectFpga(ztex); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
static void ztex_releaseFpga(struct libztex_device* ztex) |
|
||||||
{ |
|
||||||
if (ztex->root->numberOfFpgas > 1) { |
|
||||||
ztex->root->selectedFpga = -1; |
|
||||||
mutex_unlock(&ztex->root->mutex); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
static void ztex_detect(bool __maybe_unused hotplug) |
|
||||||
{ |
|
||||||
int cnt; |
|
||||||
int i,j; |
|
||||||
int fpgacount; |
|
||||||
struct libztex_dev_list **ztex_devices; |
|
||||||
struct libztex_device *ztex_slave; |
|
||||||
struct cgpu_info *ztex; |
|
||||||
|
|
||||||
cnt = libztex_scanDevices(&ztex_devices); |
|
||||||
if (cnt > 0) |
|
||||||
applog(LOG_WARNING, "Found %d ztex board%s", cnt, cnt > 1 ? "s" : ""); |
|
||||||
|
|
||||||
for (i = 0; i < cnt; i++) { |
|
||||||
ztex = calloc(1, sizeof(struct cgpu_info)); |
|
||||||
ztex->drv = &ztex_drv; |
|
||||||
ztex->device_ztex = ztex_devices[i]->dev; |
|
||||||
ztex->threads = 1; |
|
||||||
ztex->device_ztex->fpgaNum = 0; |
|
||||||
ztex->device_ztex->root = ztex->device_ztex; |
|
||||||
add_cgpu(ztex); |
|
||||||
|
|
||||||
fpgacount = libztex_numberOfFpgas(ztex->device_ztex); |
|
||||||
|
|
||||||
if (fpgacount > 1) |
|
||||||
pthread_mutex_init(&ztex->device_ztex->mutex, NULL); |
|
||||||
|
|
||||||
for (j = 1; j < fpgacount; j++) { |
|
||||||
ztex = calloc(1, sizeof(struct cgpu_info)); |
|
||||||
ztex->drv = &ztex_drv; |
|
||||||
ztex_slave = calloc(1, sizeof(struct libztex_device)); |
|
||||||
memcpy(ztex_slave, ztex_devices[i]->dev, sizeof(struct libztex_device)); |
|
||||||
ztex->device_ztex = ztex_slave; |
|
||||||
ztex->threads = 1; |
|
||||||
ztex_slave->fpgaNum = j; |
|
||||||
ztex_slave->root = ztex_devices[i]->dev; |
|
||||||
ztex_slave->repr[strlen(ztex_slave->repr) - 1] = ('1' + j); |
|
||||||
add_cgpu(ztex); |
|
||||||
} |
|
||||||
|
|
||||||
applog(LOG_WARNING,"%s: Found Ztex (fpga count = %d) , mark as %d", ztex->device_ztex->repr, fpgacount, ztex->device_id); |
|
||||||
} |
|
||||||
|
|
||||||
if (cnt > 0) |
|
||||||
libztex_freeDevList(ztex_devices); |
|
||||||
} |
|
||||||
|
|
||||||
static bool ztex_updateFreq(struct libztex_device* ztex) |
|
||||||
{ |
|
||||||
int i, maxM, bestM; |
|
||||||
double bestR, r; |
|
||||||
|
|
||||||
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) |
|
||||||
maxM++; |
|
||||||
while (maxM < ztex->freqMaxM && ztex->errorWeight[maxM] > 150 && ztex->maxErrorRate[maxM + 1] < LIBZTEX_MAXMAXERRORRATE) |
|
||||||
maxM++; |
|
||||||
|
|
||||||
bestM = 0; |
|
||||||
bestR = 0; |
|
||||||
for (i = 0; i <= maxM; i++) { |
|
||||||
r = (i + 1 + (i == ztex->freqM? LIBZTEX_ERRORHYSTERESIS: 0)) * (1 - ztex->maxErrorRate[i]); |
|
||||||
if (r > bestR) { |
|
||||||
bestM = i; |
|
||||||
bestR = r; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
if (bestM != ztex->freqM) { |
|
||||||
ztex_selectFpga(ztex); |
|
||||||
libztex_setFreq(ztex, bestM); |
|
||||||
ztex_releaseFpga(ztex); |
|
||||||
} |
|
||||||
|
|
||||||
maxM = ztex->freqMDefault; |
|
||||||
while (maxM < ztex->freqMaxM && ztex->errorWeight[maxM + 1] > 100) |
|
||||||
maxM++; |
|
||||||
if ((bestM < (1.0 - LIBZTEX_OVERHEATTHRESHOLD) * maxM) && bestM < maxM - 1) { |
|
||||||
ztex_selectFpga(ztex); |
|
||||||
libztex_resetFpga(ztex); |
|
||||||
ztex_releaseFpga(ztex); |
|
||||||
applog(LOG_ERR, "%s: frequency drop of %.1f%% detect. This may be caused by overheating. FPGA is shut down to prevent damage.", |
|
||||||
ztex->repr, (1.0 - 1.0 * bestM / maxM) * 100); |
|
||||||
return false; |
|
||||||
} |
|
||||||
return true; |
|
||||||
} |
|
||||||
|
|
||||||
|
|
||||||
static uint32_t ztex_checkNonce(struct work *work, uint32_t nonce) |
|
||||||
{ |
|
||||||
uint32_t *data32 = (uint32_t *)(work->data); |
|
||||||
unsigned char swap[80]; |
|
||||||
uint32_t *swap32 = (uint32_t *)swap; |
|
||||||
unsigned char hash1[32]; |
|
||||||
unsigned char hash2[32]; |
|
||||||
uint32_t *hash2_32 = (uint32_t *)hash2; |
|
||||||
int i; |
|
||||||
|
|
||||||
swap32[76/4] = htonl(nonce); |
|
||||||
|
|
||||||
for (i = 0; i < 76 / 4; i++) |
|
||||||
swap32[i] = swab32(data32[i]); |
|
||||||
|
|
||||||
sha256(swap, 80, hash1); |
|
||||||
sha256(hash1, 32, hash2); |
|
||||||
|
|
||||||
return htonl(hash2_32[7]); |
|
||||||
} |
|
||||||
|
|
||||||
static int64_t ztex_scanhash(struct thr_info *thr, struct work *work, |
|
||||||
__maybe_unused int64_t max_nonce) |
|
||||||
{ |
|
||||||
struct libztex_device *ztex; |
|
||||||
unsigned char sendbuf[44]; |
|
||||||
int i, j, k; |
|
||||||
uint32_t *backlog; |
|
||||||
int backlog_p = 0, backlog_max; |
|
||||||
uint32_t *lastnonce; |
|
||||||
uint32_t nonce, noncecnt = 0; |
|
||||||
bool overflow, found; |
|
||||||
struct libztex_hash_data hdata[GOLDEN_BACKLOG]; |
|
||||||
|
|
||||||
if (thr->cgpu->deven == DEV_DISABLED) |
|
||||||
return -1; |
|
||||||
|
|
||||||
ztex = thr->cgpu->device_ztex; |
|
||||||
|
|
||||||
memcpy(sendbuf, work->data + 64, 12); |
|
||||||
memcpy(sendbuf + 12, work->midstate, 32); |
|
||||||
|
|
||||||
ztex_selectFpga(ztex); |
|
||||||
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, retrying", ztex->repr, i); |
|
||||||
cgsleep_ms(500); |
|
||||||
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); |
|
||||||
ztex_releaseFpga(ztex); |
|
||||||
return -1; |
|
||||||
} |
|
||||||
} |
|
||||||
ztex_releaseFpga(ztex); |
|
||||||
|
|
||||||
applog(LOG_DEBUG, "%s: sent hashdata", ztex->repr); |
|
||||||
|
|
||||||
lastnonce = calloc(1, sizeof(uint32_t)*ztex->numNonces); |
|
||||||
if (lastnonce == NULL) { |
|
||||||
applog(LOG_ERR, "%s: failed to allocate lastnonce[%d]", ztex->repr, ztex->numNonces); |
|
||||||
return -1; |
|
||||||
} |
|
||||||
|
|
||||||
/* Add an extra slot for detecting dupes that lie around */ |
|
||||||
backlog_max = ztex->numNonces * (2 + ztex->extraSolutions); |
|
||||||
backlog = calloc(1, sizeof(uint32_t) * backlog_max); |
|
||||||
if (backlog == NULL) { |
|
||||||
applog(LOG_ERR, "%s: failed to allocate backlog[%d]", ztex->repr, backlog_max); |
|
||||||
return -1; |
|
||||||
} |
|
||||||
|
|
||||||
overflow = false; |
|
||||||
int count = 0; |
|
||||||
int validNonces = 0; |
|
||||||
double errorCount = 0; |
|
||||||
|
|
||||||
applog(LOG_DEBUG, "%s: entering poll loop", ztex->repr); |
|
||||||
while (!(overflow || thr->work_restart)) { |
|
||||||
count++; |
|
||||||
|
|
||||||
int sleepcount = 0; |
|
||||||
while (thr->work_restart == 0 && sleepcount < 25) { |
|
||||||
cgsleep_ms(10); |
|
||||||
sleepcount += 1; |
|
||||||
} |
|
||||||
|
|
||||||
if (thr->work_restart) { |
|
||||||
applog(LOG_DEBUG, "%s: New work detected", ztex->repr); |
|
||||||
break; |
|
||||||
} |
|
||||||
|
|
||||||
ztex_selectFpga(ztex); |
|
||||||
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, retrying", ztex->repr, i); |
|
||||||
cgsleep_ms(500); |
|
||||||
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); |
|
||||||
free(lastnonce); |
|
||||||
free(backlog); |
|
||||||
ztex_releaseFpga(ztex); |
|
||||||
return -1; |
|
||||||
} |
|
||||||
} |
|
||||||
ztex_releaseFpga(ztex); |
|
||||||
|
|
||||||
if (thr->work_restart) { |
|
||||||
applog(LOG_DEBUG, "%s: New work detected", ztex->repr); |
|
||||||
break; |
|
||||||
} |
|
||||||
|
|
||||||
ztex->errorCount[ztex->freqM] *= 0.995; |
|
||||||
ztex->errorWeight[ztex->freqM] = ztex->errorWeight[ztex->freqM] * 0.995 + 1.0; |
|
||||||
|
|
||||||
for (i = 0; i < ztex->numNonces; i++) { |
|
||||||
nonce = hdata[i].nonce; |
|
||||||
if (nonce > noncecnt) |
|
||||||
noncecnt = nonce; |
|
||||||
if (((0xffffffff - nonce) < (nonce - lastnonce[i])) || nonce < lastnonce[i]) { |
|
||||||
applog(LOG_DEBUG, "%s: overflow nonce=%08x lastnonce=%08x", ztex->repr, nonce, lastnonce[i]); |
|
||||||
overflow = true; |
|
||||||
} else |
|
||||||
lastnonce[i] = nonce; |
|
||||||
|
|
||||||
if (ztex_checkNonce(work, nonce) != (hdata->hash7 + 0x5be0cd19)) { |
|
||||||
applog(LOG_DEBUG, "%s: checkNonce failed for %08X", ztex->repr, nonce); |
|
||||||
|
|
||||||
// do not count errors in the first 500ms after sendHashData (2x250 wait time)
|
|
||||||
if (count > 2) { |
|
||||||
thr->cgpu->hw_errors++; |
|
||||||
errorCount += (1.0 / ztex->numNonces); |
|
||||||
} |
|
||||||
} |
|
||||||
else |
|
||||||
validNonces++; |
|
||||||
|
|
||||||
|
|
||||||
for (j=0; j<=ztex->extraSolutions; j++) { |
|
||||||
nonce = hdata[i].goldenNonce[j]; |
|
||||||
|
|
||||||
if (nonce == ztex->offsNonces) { |
|
||||||
continue; |
|
||||||
} |
|
||||||
|
|
||||||
// precheck the extraSolutions since they often fail
|
|
||||||
if (j > 0 && ztex_checkNonce(work, nonce) != 0) { |
|
||||||
continue; |
|
||||||
} |
|
||||||
|
|
||||||
found = false; |
|
||||||
for (k = 0; k < backlog_max; k++) { |
|
||||||
if (backlog[k] == nonce) { |
|
||||||
found = true; |
|
||||||
break; |
|
||||||
} |
|
||||||
} |
|
||||||
if (!found) { |
|
||||||
applog(LOG_DEBUG, "%s: Share found N%dE%d", ztex->repr, i, j); |
|
||||||
backlog[backlog_p++] = nonce; |
|
||||||
|
|
||||||
if (backlog_p >= backlog_max) |
|
||||||
backlog_p = 0; |
|
||||||
|
|
||||||
work->blk.nonce = 0xffffffff; |
|
||||||
submit_nonce(thr, work, nonce); |
|
||||||
applog(LOG_DEBUG, "%s: submitted %08x", ztex->repr, nonce); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
// only add the errorCount if we had at least some valid nonces or
|
|
||||||
// had no valid nonces in the last round
|
|
||||||
if (errorCount > 0.0) { |
|
||||||
if (ztex->nonceCheckValid > 0 && validNonces == 0) { |
|
||||||
applog(LOG_ERR, "%s: resetting %.1f errors", ztex->repr, errorCount); |
|
||||||
} |
|
||||||
else { |
|
||||||
ztex->errorCount[ztex->freqM] += errorCount; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
// remember the number of valid nonces for the check in the next round
|
|
||||||
ztex->nonceCheckValid = validNonces; |
|
||||||
|
|
||||||
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]) |
|
||||||
ztex->maxErrorRate[ztex->freqM] = ztex->errorRate[ztex->freqM]; |
|
||||||
|
|
||||||
if (!ztex_updateFreq(ztex)) { |
|
||||||
// Something really serious happened, so mark this thread as dead!
|
|
||||||
free(lastnonce); |
|
||||||
free(backlog); |
|
||||||
|
|
||||||
return -1; |
|
||||||
} |
|
||||||
|
|
||||||
applog(LOG_DEBUG, "%s: exit %1.8X", ztex->repr, noncecnt); |
|
||||||
|
|
||||||
work->blk.nonce = 0xffffffff; |
|
||||||
|
|
||||||
free(lastnonce); |
|
||||||
free(backlog); |
|
||||||
|
|
||||||
return noncecnt; |
|
||||||
} |
|
||||||
|
|
||||||
static void ztex_statline_before(char *buf, size_t bufsiz, struct cgpu_info *cgpu) |
|
||||||
{ |
|
||||||
if (cgpu->deven == DEV_ENABLED) { |
|
||||||
tailsprintf(buf, bufsiz, "%s-%d | ", cgpu->device_ztex->snString, cgpu->device_ztex->fpgaNum+1); |
|
||||||
tailsprintf(buf, bufsiz, "%0.1fMHz | ", cgpu->device_ztex->freqM1 * (cgpu->device_ztex->freqM + 1)); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
static bool ztex_prepare(struct thr_info *thr) |
|
||||||
{ |
|
||||||
struct cgpu_info *cgpu = thr->cgpu; |
|
||||||
struct libztex_device *ztex = cgpu->device_ztex; |
|
||||||
|
|
||||||
ztex_selectFpga(ztex); |
|
||||||
if (libztex_configureFpga(ztex) != 0) { |
|
||||||
libztex_resetFpga(ztex); |
|
||||||
ztex_releaseFpga(ztex); |
|
||||||
applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device_ztex->repr); |
|
||||||
thr->cgpu->deven = DEV_DISABLED; |
|
||||||
return true; |
|
||||||
} |
|
||||||
ztex->freqM = ztex->freqMaxM+1;; |
|
||||||
//ztex_updateFreq(ztex);
|
|
||||||
libztex_setFreq(ztex, ztex->freqMDefault); |
|
||||||
ztex_releaseFpga(ztex); |
|
||||||
applog(LOG_DEBUG, "%s: prepare", ztex->repr); |
|
||||||
return true; |
|
||||||
} |
|
||||||
|
|
||||||
static void ztex_shutdown(struct thr_info *thr) |
|
||||||
{ |
|
||||||
if (thr->cgpu->device_ztex != NULL) { |
|
||||||
if (thr->cgpu->device_ztex->fpgaNum == 0) |
|
||||||
pthread_mutex_destroy(&thr->cgpu->device_ztex->mutex); |
|
||||||
applog(LOG_DEBUG, "%s: shutdown", thr->cgpu->device_ztex->repr); |
|
||||||
libztex_destroy_device(thr->cgpu->device_ztex); |
|
||||||
thr->cgpu->device_ztex = NULL; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
static void ztex_disable(struct thr_info *thr) |
|
||||||
{ |
|
||||||
struct cgpu_info *cgpu; |
|
||||||
|
|
||||||
applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device_ztex->repr); |
|
||||||
cgpu = get_devices(thr->cgpu->device_id); |
|
||||||
cgpu->deven = DEV_DISABLED; |
|
||||||
ztex_shutdown(thr); |
|
||||||
} |
|
||||||
|
|
||||||
struct device_drv ztex_drv = { |
|
||||||
.drv_id = DRIVER_ztex, |
|
||||||
.dname = "ztex", |
|
||||||
.name = "ZTX", |
|
||||||
.drv_detect = ztex_detect, |
|
||||||
.get_statline_before = ztex_statline_before, |
|
||||||
.thread_prepare = ztex_prepare, |
|
||||||
.scanhash = ztex_scanhash, |
|
||||||
.thread_shutdown = ztex_shutdown, |
|
||||||
}; |
|
||||||
|
|
@ -1,915 +0,0 @@ |
|||||||
/**
|
|
||||||
* libztex.c - Ztex 1.15x/1.15y fpga board support library |
|
||||||
* |
|
||||||
* Copyright (c) 2012 nelisky.btc@gmail.com |
|
||||||
* Copyright (c) 2012 Denis Ahrens <denis@h3q.com> |
|
||||||
* Copyright (c) 2012 Peter Stuge <peter@stuge.se> |
|
||||||
* |
|
||||||
* This work is based upon the Java SDK provided by ztex which is |
|
||||||
* Copyright (C) 2009-2011 ZTEX GmbH. |
|
||||||
* http://www.ztex.de
|
|
||||||
* |
|
||||||
* This program is free software; you can redistribute it and/or modify |
|
||||||
* it under the terms of the GNU General Public License version 2 as |
|
||||||
* published by the Free Software Foundation. |
|
||||||
* |
|
||||||
* This program is distributed in the hope that it will be useful, but |
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
||||||
* General Public License for more details. |
|
||||||
* |
|
||||||
* You should have received a copy of the GNU General Public License |
|
||||||
* along with this program; if not, see http://www.gnu.org/licenses/.
|
|
||||||
**/ |
|
||||||
|
|
||||||
#include "config.h" |
|
||||||
|
|
||||||
#include <stdio.h> |
|
||||||
#include <unistd.h> |
|
||||||
#include <string.h> |
|
||||||
|
|
||||||
#include "miner.h" |
|
||||||
#include "fpgautils.h" |
|
||||||
#include "libztex.h" |
|
||||||
|
|
||||||
//* Capability index for EEPROM support.
|
|
||||||
#define CAPABILITY_EEPROM 0,0 |
|
||||||
//* Capability index for FPGA configuration support.
|
|
||||||
#define CAPABILITY_FPGA 0,1 |
|
||||||
//* Capability index for FLASH memory support.
|
|
||||||
#define CAPABILITY_FLASH 0,2 |
|
||||||
//* Capability index for DEBUG helper support.
|
|
||||||
#define CAPABILITY_DEBUG 0,3 |
|
||||||
//* Capability index for AVR XMEGA support.
|
|
||||||
#define CAPABILITY_XMEGA 0,4 |
|
||||||
//* Capability index for AVR XMEGA support.
|
|
||||||
#define CAPABILITY_HS_FPGA 0,5 |
|
||||||
//* Capability index for AVR XMEGA support.
|
|
||||||
#define CAPABILITY_MAC_EEPROM 0,6 |
|
||||||
//* Capability index for multi FPGA support.
|
|
||||||
#define CAPABILITY_MULTI_FPGA 0,7 |
|
||||||
|
|
||||||
static int libztex_get_string_descriptor_ascii(libusb_device_handle *dev, uint8_t desc_index, |
|
||||||
unsigned char *data, int length) |
|
||||||
{ |
|
||||||
int i, cnt; |
|
||||||
uint16_t langid; |
|
||||||
unsigned char buf[260]; |
|
||||||
|
|
||||||
/* We open code string descriptor retrieval and ASCII decoding here
|
|
||||||
* in order to work around that libusb_get_string_descriptor_ascii() |
|
||||||
* in the FreeBSD libusb implementation hits a bug in ZTEX firmware, |
|
||||||
* where the device returns more bytes than requested, causing babble, |
|
||||||
* which makes FreeBSD return an error to us. |
|
||||||
* |
|
||||||
* Avoid the mess by doing it manually the same way as libusb-1.0. |
|
||||||
*/ |
|
||||||
|
|
||||||
cnt = libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN, |
|
||||||
LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | 0, |
|
||||||
0x0000, buf, sizeof(buf), 1000); |
|
||||||
if (cnt < 0) { |
|
||||||
applog(LOG_ERR, "%s: Failed to read LANGIDs: %d", __func__, cnt); |
|
||||||
return cnt; |
|
||||||
} |
|
||||||
|
|
||||||
langid = libusb_le16_to_cpu(((uint16_t *)buf)[1]); |
|
||||||
|
|
||||||
cnt = libusb_control_transfer(dev, LIBUSB_ENDPOINT_IN, |
|
||||||
LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | desc_index, |
|
||||||
langid, buf, sizeof(buf), 1000); |
|
||||||
if (cnt < 0) { |
|
||||||
applog(LOG_ERR, "%s: Failed to read string descriptor: %d", __func__, cnt); |
|
||||||
return cnt; |
|
||||||
} |
|
||||||
|
|
||||||
/* num chars = (all bytes except bLength and bDescriptorType) / 2 */ |
|
||||||
for (i = 0; i <= (cnt - 2) / 2 && i < length-1; i++) |
|
||||||
data[i] = buf[2 + i*2]; |
|
||||||
|
|
||||||
data[i] = 0; |
|
||||||
|
|
||||||
return LIBUSB_SUCCESS; |
|
||||||
} |
|
||||||
|
|
||||||
enum check_result |
|
||||||
{ |
|
||||||
CHECK_ERROR, |
|
||||||
CHECK_IS_NOT_ZTEX, |
|
||||||
CHECK_OK, |
|
||||||
CHECK_RESCAN, |
|
||||||
}; |
|
||||||
|
|
||||||
static bool libztex_firmwareReset(struct libusb_device_handle *hndl, bool enable) |
|
||||||
{ |
|
||||||
uint8_t reset = enable; |
|
||||||
int cnt = libusb_control_transfer(hndl, 0x40, 0xA0, 0xE600, 0, &reset, 1, 1000); |
|
||||||
if (cnt < 0) |
|
||||||
{ |
|
||||||
applog(LOG_ERR, "Ztex reset %d failed: %d", enable, cnt); |
|
||||||
return 1; |
|
||||||
} |
|
||||||
|
|
||||||
return 0; |
|
||||||
} |
|
||||||
|
|
||||||
static enum check_result libztex_checkDevice(struct libusb_device *dev) |
|
||||||
{ |
|
||||||
FILE *fp = NULL; |
|
||||||
libusb_device_handle *hndl = NULL; |
|
||||||
struct libusb_device_descriptor desc; |
|
||||||
int ret = CHECK_ERROR, err, cnt; |
|
||||||
size_t got_bytes, length; |
|
||||||
unsigned char buf[64], *fw_buf; |
|
||||||
unsigned int i; |
|
||||||
|
|
||||||
err = libusb_get_device_descriptor(dev, &desc); |
|
||||||
if (unlikely(err != 0)) { |
|
||||||
applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err); |
|
||||||
return CHECK_ERROR; |
|
||||||
} |
|
||||||
|
|
||||||
if (desc.idVendor != LIBZTEX_IDVENDOR || desc.idProduct != LIBZTEX_IDPRODUCT) { |
|
||||||
applog(LOG_DEBUG, "Not a ZTEX device %04x:%04x", desc.idVendor, desc.idProduct); |
|
||||||
return CHECK_IS_NOT_ZTEX; |
|
||||||
} |
|
||||||
|
|
||||||
err = libusb_open(dev, &hndl); |
|
||||||
if (err != LIBUSB_SUCCESS) { |
|
||||||
applog(LOG_ERR, "%s: Can not open ZTEX device: %d", __func__, err); |
|
||||||
goto done; |
|
||||||
} |
|
||||||
|
|
||||||
cnt = libusb_control_transfer(hndl, 0xc0, 0x22, 0, 0, buf, 40, 500); |
|
||||||
if (unlikely(cnt < 0)) { |
|
||||||
applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt); |
|
||||||
goto done; |
|
||||||
} |
|
||||||
|
|
||||||
if (buf[0] != 40 || buf[1] != 1 || buf[2] != 'Z' || buf[3] != 'T' || buf[4] != 'E' || buf[5] != 'X') { |
|
||||||
applog(LOG_ERR, "Ztex check device: Error reading ztex descriptor"); |
|
||||||
goto done; |
|
||||||
} |
|
||||||
|
|
||||||
if (buf[6] != 10) |
|
||||||
{ |
|
||||||
ret = CHECK_IS_NOT_ZTEX; |
|
||||||
goto done; |
|
||||||
} |
|
||||||
|
|
||||||
// 15 = 1.15y 13 = 1.15d or 1.15x
|
|
||||||
switch(buf[7]) |
|
||||||
{ |
|
||||||
case 13: |
|
||||||
applog(LOG_ERR, "Found ztex board 1.15d or 1.15x"); |
|
||||||
break; |
|
||||||
case 15: |
|
||||||
applog(LOG_ERR, "Found ztex board 1.15y"); |
|
||||||
break; |
|
||||||
default: |
|
||||||
applog(LOG_ERR, "Found unknown ztex board"); |
|
||||||
ret = CHECK_IS_NOT_ZTEX; |
|
||||||
goto done; |
|
||||||
} |
|
||||||
|
|
||||||
// testing for dummy firmware
|
|
||||||
if (buf[8] != 0) { |
|
||||||
ret = CHECK_OK; |
|
||||||
goto done; |
|
||||||
} |
|
||||||
|
|
||||||
applog(LOG_ERR, "Found dummy firmware, trying to send mining firmware"); |
|
||||||
|
|
||||||
char productString[32]; |
|
||||||
|
|
||||||
cnt = libztex_get_string_descriptor_ascii(hndl, desc.iProduct, (unsigned char*)productString, sizeof(productString)); |
|
||||||
if (unlikely(cnt < 0)) { |
|
||||||
applog(LOG_ERR, "Ztex check device: Failed to read device productString with err %d", cnt); |
|
||||||
return cnt; |
|
||||||
} |
|
||||||
|
|
||||||
applog(LOG_ERR, "productString: %s", productString); |
|
||||||
|
|
||||||
unsigned char productID2 = buf[7]; |
|
||||||
char *firmware = NULL; |
|
||||||
|
|
||||||
if (strcmp("USB-FPGA Module 1.15d (default)", productString) == 0 && productID2 == 13) |
|
||||||
{ |
|
||||||
firmware = "ztex_ufm1_15d4.bin"; |
|
||||||
} |
|
||||||
else if (strcmp("USB-FPGA Module 1.15x (default)", productString) == 0 && productID2 == 13) |
|
||||||
{ |
|
||||||
firmware = "ztex_ufm1_15d4.bin"; |
|
||||||
} |
|
||||||
else if (strcmp("USB-FPGA Module 1.15y (default)", productString) == 0 && productID2 == 15) |
|
||||||
{ |
|
||||||
firmware = "ztex_ufm1_15y1.bin"; |
|
||||||
} |
|
||||||
|
|
||||||
if (firmware == NULL) |
|
||||||
{ |
|
||||||
applog(LOG_ERR, "could not figure out which firmware to use"); |
|
||||||
goto done; |
|
||||||
} |
|
||||||
|
|
||||||
applog(LOG_ERR, "Mining firmware filename: %s", firmware); |
|
||||||
|
|
||||||
fp = open_bitstream("ztex", firmware); |
|
||||||
if (!fp) { |
|
||||||
applog(LOG_ERR, "failed to open firmware file '%s'", firmware); |
|
||||||
goto done; |
|
||||||
} |
|
||||||
|
|
||||||
if (0 != fseek(fp, 0, SEEK_END)) { |
|
||||||
applog(LOG_ERR, "Ztex firmware fseek: %s", strerror(errno)); |
|
||||||
goto done; |
|
||||||
} |
|
||||||
|
|
||||||
length = ftell(fp); |
|
||||||
rewind(fp); |
|
||||||
fw_buf = malloc(length); |
|
||||||
if (!fw_buf) { |
|
||||||
applog(LOG_ERR, "%s: Can not allocate memory: %s", __func__, strerror(errno)); |
|
||||||
goto done; |
|
||||||
} |
|
||||||
|
|
||||||
got_bytes = fread(fw_buf, 1, length, fp); |
|
||||||
fclose(fp); |
|
||||||
fp = NULL; |
|
||||||
|
|
||||||
if (got_bytes < length) { |
|
||||||
applog(LOG_ERR, "%s: Incomplete firmware read: %d/%d", __func__, (int)got_bytes, (int)length); |
|
||||||
goto done; |
|
||||||
} |
|
||||||
|
|
||||||
// in buf[] is still the identifier of the dummy firmware
|
|
||||||
// use it to compare it with the new firmware
|
|
||||||
char *rv = memmem(fw_buf, got_bytes, buf, 8); |
|
||||||
if (rv == NULL) |
|
||||||
{ |
|
||||||
applog(LOG_ERR, "%s: found firmware is not ZTEX", __func__); |
|
||||||
goto done; |
|
||||||
} |
|
||||||
|
|
||||||
// check for dummy firmware
|
|
||||||
if (rv[8] == 0) |
|
||||||
{ |
|
||||||
applog(LOG_ERR, "%s: found a ZTEX dummy firmware", __func__); |
|
||||||
goto done; |
|
||||||
} |
|
||||||
|
|
||||||
if (libztex_firmwareReset(hndl, true)) |
|
||||||
goto done; |
|
||||||
|
|
||||||
for (i = 0; i < length; i+= 256) { |
|
||||||
// firmware wants data in small chunks like 256 bytes
|
|
||||||
int numbytes = (length - i) < 256 ? (length - i) : 256; |
|
||||||
int k = libusb_control_transfer(hndl, 0x40, 0xA0, i, 0, fw_buf + i, numbytes, 1000); |
|
||||||
if (k < numbytes) |
|
||||||
{ |
|
||||||
applog(LOG_ERR, "Ztex device: Failed to write firmware at %d with err: %d", i, k); |
|
||||||
goto done; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
if (libztex_firmwareReset(hndl, false)) |
|
||||||
goto done; |
|
||||||
|
|
||||||
applog(LOG_ERR, "Ztex device: succesfully wrote firmware"); |
|
||||||
ret = CHECK_RESCAN; |
|
||||||
|
|
||||||
done: |
|
||||||
if (fp) |
|
||||||
fclose(fp); |
|
||||||
if (hndl) |
|
||||||
libusb_close(hndl); |
|
||||||
return ret; |
|
||||||
} |
|
||||||
|
|
||||||
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))) { |
|
||||||
applog(LOG_ERR, "%s: capability missing: %d %d", ztex->repr, i, j); |
|
||||||
return false; |
|
||||||
} |
|
||||||
return true; |
|
||||||
} |
|
||||||
|
|
||||||
static char libztex_detectBitstreamBitOrder(const unsigned char *buf, int size) |
|
||||||
{ |
|
||||||
int i; |
|
||||||
|
|
||||||
for (i = 0; i < size - 4; i++) { |
|
||||||
if (((buf[i] & 255) == 0xaa) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0x55) && ((buf[i + 3] & 255) == 0x66)) |
|
||||||
return 1; |
|
||||||
if (((buf[i] & 255) == 0x55) && ((buf[i + 1] & 255) == 0x99) && ((buf[i + 2] & 255) == 0xaa) && ((buf[i + 3] & 255) == 0x66)) |
|
||||||
return 0; |
|
||||||
} |
|
||||||
applog(LOG_WARNING, "Unable to determine bitstream bit order: no signature found"); |
|
||||||
return 0; |
|
||||||
} |
|
||||||
|
|
||||||
static void libztex_swapBits(unsigned char *buf, int size) |
|
||||||
{ |
|
||||||
unsigned char c; |
|
||||||
int i; |
|
||||||
|
|
||||||
for (i = 0; i < size; i++) { |
|
||||||
c = buf[i]; |
|
||||||
buf[i] = ((c & 128) >> 7) | |
|
||||||
((c & 64) >> 5) | |
|
||||||
((c & 32) >> 3) | |
|
||||||
((c & 16) >> 1) | |
|
||||||
((c & 8) << 1) | |
|
||||||
((c & 4) << 3) | |
|
||||||
((c & 2) << 5) | |
|
||||||
((c & 1) << 7); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
static int libztex_getFpgaState(struct libztex_device *ztex, struct libztex_fpgastate *state) |
|
||||||
{ |
|
||||||
unsigned char buf[9]; |
|
||||||
int cnt; |
|
||||||
|
|
||||||
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->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); |
|
||||||
return 0; |
|
||||||
} |
|
||||||
|
|
||||||
static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firmware, bool force, char bs) |
|
||||||
{ |
|
||||||
struct libztex_fpgastate state; |
|
||||||
const int transactionBytes = 65536; |
|
||||||
unsigned char buf[transactionBytes], settings[2]; |
|
||||||
int tries, cnt, err; |
|
||||||
FILE *fp; |
|
||||||
|
|
||||||
if (!libztex_checkCapability(ztex, CAPABILITY_HS_FPGA)) |
|
||||||
return -1; |
|
||||||
libztex_getFpgaState(ztex, &state); |
|
||||||
if (!force && state.fpgaConfigured) { |
|
||||||
applog(LOG_INFO, "Bitstream already configured"); |
|
||||||
return 0; |
|
||||||
} |
|
||||||
cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x33, 0, 0, settings, 2, 1000); |
|
||||||
if (unlikely(cnt < 0)) { |
|
||||||
applog(LOG_ERR, "%s: Failed getHSFpgaSettings with err %d", ztex->repr, cnt); |
|
||||||
return cnt; |
|
||||||
} |
|
||||||
|
|
||||||
err = libusb_claim_interface(ztex->hndl, settings[1]); |
|
||||||
if (err != LIBUSB_SUCCESS) { |
|
||||||
applog(LOG_ERR, "%s: failed to claim interface for hs transfer", ztex->repr); |
|
||||||
return -4; |
|
||||||
} |
|
||||||
|
|
||||||
for (tries = 3; tries > 0; tries--) { |
|
||||||
fp = open_bitstream("ztex", firmware); |
|
||||||
if (!fp) { |
|
||||||
applog(LOG_ERR, "%s: failed to read bitstream '%s'", ztex->repr, firmware); |
|
||||||
libusb_release_interface(ztex->hndl, settings[1]); |
|
||||||
return -2; |
|
||||||
} |
|
||||||
|
|
||||||
libusb_control_transfer(ztex->hndl, 0x40, 0x34, 0, 0, NULL, 0, 1000); |
|
||||||
// 0x34 - initHSFPGAConfiguration
|
|
||||||
|
|
||||||
do |
|
||||||
{ |
|
||||||
int length = fread(buf,1,transactionBytes,fp); |
|
||||||
|
|
||||||
if (bs != 0 && bs != 1) |
|
||||||
bs = libztex_detectBitstreamBitOrder(buf, length); |
|
||||||
if (bs == 1) |
|
||||||
libztex_swapBits(buf, length); |
|
||||||
|
|
||||||
err = libusb_bulk_transfer(ztex->hndl, settings[0], buf, length, &cnt, 1000); |
|
||||||
if (cnt != length) |
|
||||||
applog(LOG_ERR, "%s: cnt != length", ztex->repr); |
|
||||||
if (err != 0) |
|
||||||
applog(LOG_ERR, "%s: Failed send hs fpga data", ztex->repr); |
|
||||||
} |
|
||||||
while (!feof(fp)); |
|
||||||
|
|
||||||
libusb_control_transfer(ztex->hndl, 0x40, 0x35, 0, 0, NULL, 0, 1000); |
|
||||||
// 0x35 - finishHSFPGAConfiguration
|
|
||||||
if (cnt >= 0) |
|
||||||
tries = 0; |
|
||||||
|
|
||||||
fclose(fp); |
|
||||||
|
|
||||||
libztex_getFpgaState(ztex, &state); |
|
||||||
if (!state.fpgaConfigured) { |
|
||||||
applog(LOG_ERR, "%s: HS FPGA configuration failed: DONE pin does not go high", ztex->repr); |
|
||||||
libusb_release_interface(ztex->hndl, settings[1]); |
|
||||||
return -3; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
libusb_release_interface(ztex->hndl, settings[1]); |
|
||||||
|
|
||||||
cgsleep_ms(200); |
|
||||||
applog(LOG_INFO, "%s: HS FPGA configuration done", ztex->repr); |
|
||||||
return 0; |
|
||||||
} |
|
||||||
|
|
||||||
static int libztex_configureFpgaLS(struct libztex_device *ztex, const char* firmware, bool force, char bs) |
|
||||||
{ |
|
||||||
struct libztex_fpgastate state; |
|
||||||
const int transactionBytes = 2048; |
|
||||||
unsigned char buf[transactionBytes]; |
|
||||||
int tries, cnt; |
|
||||||
FILE *fp; |
|
||||||
|
|
||||||
if (!libztex_checkCapability(ztex, CAPABILITY_FPGA)) |
|
||||||
return -1; |
|
||||||
|
|
||||||
libztex_getFpgaState(ztex, &state); |
|
||||||
if (!force && state.fpgaConfigured) { |
|
||||||
applog(LOG_DEBUG, "Bitstream already configured"); |
|
||||||
return 0; |
|
||||||
} |
|
||||||
|
|
||||||
for (tries = 10; tries > 0; tries--) { |
|
||||||
fp = open_bitstream("ztex", firmware); |
|
||||||
if (!fp) { |
|
||||||
applog(LOG_ERR, "%s: failed to read bitstream '%s'", ztex->repr, firmware); |
|
||||||
return -2; |
|
||||||
} |
|
||||||
|
|
||||||
//* Reset fpga
|
|
||||||
cnt = libztex_resetFpga(ztex); |
|
||||||
if (unlikely(cnt < 0)) { |
|
||||||
applog(LOG_ERR, "%s: Failed reset fpga with err %d", ztex->repr, cnt); |
|
||||||
continue; |
|
||||||
} |
|
||||||
|
|
||||||
do |
|
||||||
{ |
|
||||||
int length = fread(buf, 1, transactionBytes, fp); |
|
||||||
|
|
||||||
if (bs != 0 && bs != 1) |
|
||||||
bs = libztex_detectBitstreamBitOrder(buf, length); |
|
||||||
if (bs == 1) |
|
||||||
libztex_swapBits(buf, length); |
|
||||||
cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x32, 0, 0, buf, length, 5000); |
|
||||||
if (cnt != length) |
|
||||||
{ |
|
||||||
applog(LOG_ERR, "%s: Failed send ls fpga data", ztex->repr); |
|
||||||
break; |
|
||||||
} |
|
||||||
} |
|
||||||
while (!feof(fp)); |
|
||||||
|
|
||||||
if (cnt > 0) |
|
||||||
tries = 0; |
|
||||||
|
|
||||||
fclose(fp); |
|
||||||
} |
|
||||||
|
|
||||||
libztex_getFpgaState(ztex, &state); |
|
||||||
if (!state.fpgaConfigured) { |
|
||||||
applog(LOG_ERR, "%s: LS FPGA configuration failed: DONE pin does not go high", ztex->repr); |
|
||||||
return -3; |
|
||||||
} |
|
||||||
|
|
||||||
cgsleep_ms(200); |
|
||||||
applog(LOG_INFO, "%s: FPGA configuration done", ztex->repr); |
|
||||||
return 0; |
|
||||||
} |
|
||||||
|
|
||||||
int libztex_configureFpga(struct libztex_device *ztex) |
|
||||||
{ |
|
||||||
char buf[256]; |
|
||||||
int rv; |
|
||||||
|
|
||||||
strcpy(buf, ztex->bitFileName); |
|
||||||
strcat(buf, ".bit"); |
|
||||||
rv = libztex_configureFpgaHS(ztex, buf, true, 2); |
|
||||||
if (rv != 0) |
|
||||||
rv = libztex_configureFpgaLS(ztex, buf, true, 2); |
|
||||||
return rv; |
|
||||||
} |
|
||||||
|
|
||||||
int libztex_numberOfFpgas(struct libztex_device *ztex) |
|
||||||
{ |
|
||||||
int cnt; |
|
||||||
unsigned char buf[3]; |
|
||||||
|
|
||||||
if (ztex->numberOfFpgas < 0) { |
|
||||||
if (libztex_checkCapability(ztex, CAPABILITY_MULTI_FPGA)) { |
|
||||||
cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x50, 0, 0, buf, 3, 1000); |
|
||||||
if (unlikely(cnt < 0)) { |
|
||||||
applog(LOG_ERR, "%s: Failed getMultiFpgaInfo with err %d", ztex->repr, cnt); |
|
||||||
return cnt; |
|
||||||
} |
|
||||||
ztex->numberOfFpgas = buf[0] + 1; |
|
||||||
ztex->selectedFpga = -1;//buf[1];
|
|
||||||
ztex->parallelConfigSupport = (buf[2] == 1); |
|
||||||
} else { |
|
||||||
ztex->numberOfFpgas = 1; |
|
||||||
ztex->selectedFpga = -1;//0;
|
|
||||||
ztex->parallelConfigSupport = false; |
|
||||||
} |
|
||||||
} |
|
||||||
return ztex->numberOfFpgas; |
|
||||||
} |
|
||||||
|
|
||||||
int libztex_selectFpga(struct libztex_device *ztex) |
|
||||||
{ |
|
||||||
int cnt, fpgacnt = libztex_numberOfFpgas(ztex->root); |
|
||||||
int16_t number = ztex->fpgaNum; |
|
||||||
|
|
||||||
if (number < 0 || number >= fpgacnt) { |
|
||||||
applog(LOG_WARNING, "%s: Trying to select wrong fpga (%d in %d)", ztex->repr, number, fpgacnt); |
|
||||||
return 1; |
|
||||||
} |
|
||||||
if (ztex->root->selectedFpga != number && libztex_checkCapability(ztex->root, CAPABILITY_MULTI_FPGA)) { |
|
||||||
cnt = libusb_control_transfer(ztex->root->hndl, 0x40, 0x51, (uint16_t)number, 0, NULL, 0, 500); |
|
||||||
if (unlikely(cnt < 0)) { |
|
||||||
applog(LOG_ERR, "Ztex check device: Failed to set fpga with err %d", cnt); |
|
||||||
ztex->root->selectedFpga = -1; |
|
||||||
return cnt; |
|
||||||
} |
|
||||||
ztex->root->selectedFpga = number; |
|
||||||
} |
|
||||||
return 0; |
|
||||||
} |
|
||||||
|
|
||||||
int libztex_setFreq(struct libztex_device *ztex, uint16_t freq) |
|
||||||
{ |
|
||||||
int cnt; |
|
||||||
uint16_t oldfreq = ztex->freqM; |
|
||||||
|
|
||||||
if (freq > ztex->freqMaxM) |
|
||||||
freq = ztex->freqMaxM; |
|
||||||
|
|
||||||
cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x83, freq, 0, NULL, 0, 500); |
|
||||||
if (unlikely(cnt < 0)) { |
|
||||||
applog(LOG_ERR, "Ztex check device: Failed to set frequency with err %d", cnt); |
|
||||||
return cnt; |
|
||||||
} |
|
||||||
ztex->freqM = freq; |
|
||||||
if (oldfreq > ztex->freqMaxM) |
|
||||||
applog(LOG_WARNING, "%s: Frequency set to %0.1f MHz", |
|
||||||
ztex->repr, ztex->freqM1 * (ztex->freqM + 1)); |
|
||||||
else |
|
||||||
applog(LOG_WARNING, "%s: Frequency change from %0.1f to %0.1f MHz", |
|
||||||
ztex->repr, ztex->freqM1 * (oldfreq + 1), ztex->freqM1 * (ztex->freqM + 1)); |
|
||||||
|
|
||||||
return 0; |
|
||||||
} |
|
||||||
|
|
||||||
int libztex_resetFpga(struct libztex_device *ztex) |
|
||||||
{ |
|
||||||
return libusb_control_transfer(ztex->hndl, 0x40, 0x31, 0, 0, NULL, 0, 1000); |
|
||||||
} |
|
||||||
|
|
||||||
int libztex_suspend(struct libztex_device *ztex) |
|
||||||
{ |
|
||||||
if (ztex->suspendSupported) { |
|
||||||
return libusb_control_transfer(ztex->hndl, 0x40, 0x84, 0, 0, NULL, 0, 1000); |
|
||||||
} else { |
|
||||||
return 0; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
int libztex_prepare_device(struct libusb_device *dev, struct libztex_device** ztex) |
|
||||||
{ |
|
||||||
struct libztex_device *newdev = *ztex; |
|
||||||
int i, cnt, err; |
|
||||||
unsigned char buf[64]; |
|
||||||
|
|
||||||
err = libusb_open(dev, &newdev->hndl); |
|
||||||
if (err != LIBUSB_SUCCESS) { |
|
||||||
applog(LOG_ERR, "%s: Can not open ZTEX device: %d", __func__, err); |
|
||||||
return CHECK_ERROR; |
|
||||||
} |
|
||||||
|
|
||||||
err = libusb_get_device_descriptor(dev, &newdev->descriptor); |
|
||||||
if (unlikely(err != 0)) { |
|
||||||
applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err); |
|
||||||
return CHECK_ERROR; |
|
||||||
} |
|
||||||
|
|
||||||
cnt = libztex_get_string_descriptor_ascii(newdev->hndl, newdev->descriptor.iSerialNumber, newdev->snString, sizeof(newdev->snString)); |
|
||||||
if (unlikely(cnt < 0)) { |
|
||||||
applog(LOG_ERR, "Ztex check device: Failed to read device snString with err %d", cnt); |
|
||||||
return cnt; |
|
||||||
} |
|
||||||
|
|
||||||
cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x22, 0, 0, buf, 40, 500); |
|
||||||
if (unlikely(cnt < 0)) { |
|
||||||
applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt); |
|
||||||
return cnt; |
|
||||||
} |
|
||||||
|
|
||||||
if (buf[0] != 40 || buf[1] != 1 || buf[2] != 'Z' || buf[3] != 'T' || buf[4] != 'E' || buf[5] != 'X') { |
|
||||||
applog(LOG_ERR, "Ztex check device: Error reading ztex descriptor"); |
|
||||||
return 2; |
|
||||||
} |
|
||||||
|
|
||||||
newdev->productId[0] = buf[6]; |
|
||||||
newdev->productId[1] = buf[7]; |
|
||||||
newdev->productId[2] = buf[8]; |
|
||||||
newdev->productId[3] = buf[9]; |
|
||||||
newdev->fwVersion = buf[10]; |
|
||||||
newdev->interfaceVersion = buf[11]; |
|
||||||
newdev->interfaceCapabilities[0] = buf[12]; |
|
||||||
newdev->interfaceCapabilities[1] = buf[13]; |
|
||||||
newdev->interfaceCapabilities[2] = buf[14]; |
|
||||||
newdev->interfaceCapabilities[3] = buf[15]; |
|
||||||
newdev->interfaceCapabilities[4] = buf[16]; |
|
||||||
newdev->interfaceCapabilities[5] = buf[17]; |
|
||||||
newdev->moduleReserved[0] = buf[18]; |
|
||||||
newdev->moduleReserved[1] = buf[19]; |
|
||||||
newdev->moduleReserved[2] = buf[20]; |
|
||||||
newdev->moduleReserved[3] = buf[21]; |
|
||||||
newdev->moduleReserved[4] = buf[22]; |
|
||||||
newdev->moduleReserved[5] = buf[23]; |
|
||||||
newdev->moduleReserved[6] = buf[24]; |
|
||||||
newdev->moduleReserved[7] = buf[25]; |
|
||||||
newdev->moduleReserved[8] = buf[26]; |
|
||||||
newdev->moduleReserved[9] = buf[27]; |
|
||||||
newdev->moduleReserved[10] = buf[28]; |
|
||||||
newdev->moduleReserved[11] = buf[29]; |
|
||||||
|
|
||||||
cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x82, 0, 0, buf, 64, 500); |
|
||||||
if (unlikely(cnt < 0)) { |
|
||||||
applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt); |
|
||||||
return cnt; |
|
||||||
} |
|
||||||
|
|
||||||
if (unlikely(buf[0] != 5)) { |
|
||||||
if (unlikely(buf[0] != 2 && buf[0] != 4)) { |
|
||||||
applog(LOG_ERR, "Invalid BTCMiner descriptor version. Firmware must be updated (%d).", buf[0]); |
|
||||||
return 3; |
|
||||||
} |
|
||||||
applog(LOG_WARNING, "Firmware out of date (%d).", buf[0]); |
|
||||||
} |
|
||||||
|
|
||||||
i = buf[0] > 4? 11: (buf[0] > 2? 10: 8); |
|
||||||
|
|
||||||
while (cnt < 64 && buf[cnt] != 0) |
|
||||||
cnt++; |
|
||||||
if (cnt < i + 1) { |
|
||||||
applog(LOG_ERR, "Invalid bitstream file name ."); |
|
||||||
return 4; |
|
||||||
} |
|
||||||
|
|
||||||
newdev->bitFileName = malloc(sizeof(char) * (cnt + 1)); |
|
||||||
memcpy(newdev->bitFileName, &buf[i], cnt); |
|
||||||
newdev->bitFileName[cnt] = 0; |
|
||||||
|
|
||||||
newdev->numNonces = buf[1] + 1; |
|
||||||
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); |
|
||||||
newdev->freqMDefault = newdev->freqM; |
|
||||||
newdev->suspendSupported = (buf[0] == 5); |
|
||||||
newdev->hashesPerClock = buf[0] > 2? (((buf[8] & 255) | ((buf[9] & 255) << 8)) + 1) / 128.0: 1.0; |
|
||||||
newdev->extraSolutions = buf[0] > 4? buf[10]: 0; |
|
||||||
|
|
||||||
applog(LOG_DEBUG, "PID: %d numNonces: %d offsNonces: %d freqM1: %f freqMaxM: %d freqM: %d suspendSupported: %s hashesPerClock: %f extraSolutions: %d", |
|
||||||
buf[0], newdev->numNonces, newdev->offsNonces, newdev->freqM1, newdev->freqMaxM, newdev->freqM, newdev->suspendSupported ? "T": "F", |
|
||||||
newdev->hashesPerClock, newdev->extraSolutions); |
|
||||||
|
|
||||||
if (buf[0] < 4) { |
|
||||||
if (strncmp(newdev->bitFileName, "ztex_ufm1_15b", 13) != 0) |
|
||||||
newdev->hashesPerClock = 0.5; |
|
||||||
applog(LOG_WARNING, "HASHES_PER_CLOCK not defined, assuming %0.2f", newdev->hashesPerClock); |
|
||||||
} |
|
||||||
|
|
||||||
for (cnt=0; cnt < 255; cnt++) { |
|
||||||
newdev->errorCount[cnt] = 0; |
|
||||||
newdev->errorWeight[cnt] = 0; |
|
||||||
newdev->errorRate[cnt] = 0; |
|
||||||
newdev->maxErrorRate[cnt] = 0; |
|
||||||
} |
|
||||||
|
|
||||||
// fake that the last round found something valid
|
|
||||||
newdev->nonceCheckValid = 1; |
|
||||||
|
|
||||||
newdev->usbbus = libusb_get_bus_number(dev); |
|
||||||
newdev->usbaddress = libusb_get_device_address(dev); |
|
||||||
sprintf(newdev->repr, "ZTEX %s-1", newdev->snString); |
|
||||||
return 0; |
|
||||||
} |
|
||||||
|
|
||||||
void libztex_destroy_device(struct libztex_device* ztex) |
|
||||||
{ |
|
||||||
if (ztex->hndl != NULL) { |
|
||||||
libusb_close(ztex->hndl); |
|
||||||
ztex->hndl = NULL; |
|
||||||
} |
|
||||||
if (ztex->bitFileName != NULL) { |
|
||||||
free(ztex->bitFileName); |
|
||||||
ztex->bitFileName = NULL; |
|
||||||
} |
|
||||||
free(ztex); |
|
||||||
} |
|
||||||
|
|
||||||
int libztex_scanDevices(struct libztex_dev_list*** devs_p) |
|
||||||
{ |
|
||||||
int usbdevices[LIBZTEX_MAX_DESCRIPTORS]; |
|
||||||
struct libztex_dev_list **devs = NULL; |
|
||||||
struct libztex_device *ztex = NULL; |
|
||||||
int found, max_found = 0, pos = 0, err, rescan, ret = 0; |
|
||||||
libusb_device **list = NULL; |
|
||||||
ssize_t cnt, i; |
|
||||||
|
|
||||||
do { |
|
||||||
cnt = libusb_get_device_list(NULL, &list); |
|
||||||
if (unlikely(cnt < 0)) { |
|
||||||
applog(LOG_ERR, "Ztex scan devices: Failed to list usb devices with err %d", (int)cnt); |
|
||||||
goto done; |
|
||||||
} |
|
||||||
|
|
||||||
for (found = rescan = i = 0; i < cnt; i++) { |
|
||||||
err = libztex_checkDevice(list[i]); |
|
||||||
switch (err) { |
|
||||||
case CHECK_ERROR: |
|
||||||
applog(LOG_ERR, "Ztex: Can not check device: %d", err); |
|
||||||
continue; |
|
||||||
case CHECK_IS_NOT_ZTEX: |
|
||||||
continue; |
|
||||||
case CHECK_OK: |
|
||||||
// Got one!
|
|
||||||
usbdevices[found++] = i; |
|
||||||
break; |
|
||||||
case CHECK_RESCAN: |
|
||||||
rescan = 1; |
|
||||||
found++; |
|
||||||
break; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
if (found < max_found) |
|
||||||
rescan = 1; |
|
||||||
else if (found > max_found) |
|
||||||
max_found = found; |
|
||||||
|
|
||||||
if (rescan) |
|
||||||
libusb_free_device_list(list, 1); |
|
||||||
} while (rescan); |
|
||||||
|
|
||||||
if (0 == found) |
|
||||||
goto done; |
|
||||||
|
|
||||||
devs = malloc(sizeof(struct libztex_dev_list *) * found); |
|
||||||
if (devs == NULL) { |
|
||||||
applog(LOG_ERR, "Ztex scan devices: Failed to allocate memory"); |
|
||||||
goto done; |
|
||||||
} |
|
||||||
|
|
||||||
for (i = 0; i < found; i++) { |
|
||||||
if (!ztex) { |
|
||||||
ztex = malloc(sizeof(*ztex)); |
|
||||||
if (!ztex) { |
|
||||||
applog(LOG_ERR, "%s: Can not allocate memory for device struct: %s", __func__, strerror(errno)); |
|
||||||
goto done; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
ztex->bitFileName = NULL; |
|
||||||
ztex->numberOfFpgas = -1; |
|
||||||
|
|
||||||
err = libztex_prepare_device(list[usbdevices[i]], &ztex); |
|
||||||
if (unlikely(err != 0)) { |
|
||||||
applog(LOG_ERR, "prepare device: %d", err); |
|
||||||
libztex_destroy_device(ztex); |
|
||||||
ztex = NULL; |
|
||||||
continue; |
|
||||||
} |
|
||||||
|
|
||||||
devs[pos] = malloc(sizeof(struct libztex_dev_list)); |
|
||||||
if (NULL == devs[pos]) { |
|
||||||
applog(LOG_ERR, "%s: Can not allocate memory for device: %s", __func__, strerror(errno)); |
|
||||||
libztex_destroy_device(ztex); |
|
||||||
ztex = NULL; |
|
||||||
continue; |
|
||||||
} |
|
||||||
|
|
||||||
devs[pos]->dev = ztex; |
|
||||||
ztex = NULL; |
|
||||||
devs[pos]->next = NULL; |
|
||||||
if (pos > 0) |
|
||||||
devs[pos - 1]->next = devs[pos]; |
|
||||||
pos++; |
|
||||||
} |
|
||||||
|
|
||||||
ret = pos; |
|
||||||
|
|
||||||
done: |
|
||||||
if (ret > 0) |
|
||||||
*devs_p = devs; |
|
||||||
else if (devs) |
|
||||||
free(devs); |
|
||||||
if (list) |
|
||||||
libusb_free_device_list(list, 1); |
|
||||||
return ret; |
|
||||||
} |
|
||||||
|
|
||||||
int libztex_sendHashData(struct libztex_device *ztex, unsigned char *sendbuf) |
|
||||||
{ |
|
||||||
int cnt = 0, ret, len; |
|
||||||
|
|
||||||
if (ztex == NULL || ztex->hndl == NULL) |
|
||||||
return 0; |
|
||||||
ret = 44; len = 0; |
|
||||||
while (ret > 0) { |
|
||||||
cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x80, 0, 0, sendbuf + len, ret, 1000); |
|
||||||
if (cnt >= 0) { |
|
||||||
ret -= cnt; |
|
||||||
len += cnt; |
|
||||||
} else |
|
||||||
break; |
|
||||||
} |
|
||||||
if (unlikely(cnt < 0)) |
|
||||||
applog(LOG_ERR, "%s: Failed sendHashData with err %d", ztex->repr, cnt); |
|
||||||
|
|
||||||
return cnt; |
|
||||||
} |
|
||||||
|
|
||||||
int libztex_readHashData(struct libztex_device *ztex, struct libztex_hash_data nonces[]) |
|
||||||
{ |
|
||||||
int bufsize = 12 + ztex->extraSolutions * 4; |
|
||||||
int cnt = 0, i, j, ret, len; |
|
||||||
unsigned char *rbuf; |
|
||||||
|
|
||||||
if (ztex->hndl == NULL) |
|
||||||
return 0; |
|
||||||
|
|
||||||
rbuf = malloc(sizeof(unsigned char) * (ztex->numNonces * bufsize)); |
|
||||||
if (rbuf == NULL) { |
|
||||||
applog(LOG_ERR, "%s: Failed to allocate memory for reading nonces", ztex->repr); |
|
||||||
return 0; |
|
||||||
} |
|
||||||
ret = bufsize * ztex->numNonces; len = 0; |
|
||||||
while (ret > 0) { |
|
||||||
cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x81, 0, 0, rbuf + len, ret, 1000); |
|
||||||
if (cnt >= 0) { |
|
||||||
ret -= cnt; |
|
||||||
len += cnt; |
|
||||||
} else |
|
||||||
break; |
|
||||||
} |
|
||||||
|
|
||||||
if (unlikely(cnt < 0)) { |
|
||||||
applog(LOG_ERR, "%s: Failed readHashData with err %d", ztex->repr, cnt); |
|
||||||
free(rbuf); |
|
||||||
return cnt; |
|
||||||
} |
|
||||||
|
|
||||||
for (i=0; i<ztex->numNonces; i++) { |
|
||||||
memcpy((char*)&nonces[i].goldenNonce[0], &rbuf[i*bufsize], 4); |
|
||||||
nonces[i].goldenNonce[0] -= ztex->offsNonces; |
|
||||||
//applog(LOG_DEBUG, "W %d:0 %0.8x", i, nonces[i].goldenNonce[0]);
|
|
||||||
|
|
||||||
memcpy((char*)&nonces[i].nonce, &rbuf[(i*bufsize)+4], 4); |
|
||||||
memcpy((char*)&nonces[i].hash7, &rbuf[(i*bufsize)+8], 4); |
|
||||||
|
|
||||||
nonces[i].nonce = htole32(nonces[i].nonce); |
|
||||||
nonces[i].hash7 = htole32(nonces[i].hash7); |
|
||||||
|
|
||||||
nonces[i].nonce -= ztex->offsNonces; |
|
||||||
|
|
||||||
for (j=0; j<ztex->extraSolutions; j++) { |
|
||||||
memcpy((char*)&nonces[i].goldenNonce[j+1], &rbuf[(i*bufsize)+12+(j*4)], 4); |
|
||||||
nonces[i].goldenNonce[j+1] = htole32(nonces[i].goldenNonce[j+1]); |
|
||||||
nonces[i].goldenNonce[j+1] -= ztex->offsNonces; |
|
||||||
//applog(LOG_DEBUG, "W %d:%d %0.8x", i, j+1, nonces[i].goldenNonce[j+1]);
|
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
free(rbuf); |
|
||||||
return cnt; |
|
||||||
} |
|
||||||
|
|
||||||
void libztex_freeDevList(struct libztex_dev_list **devs) |
|
||||||
{ |
|
||||||
bool done = false; |
|
||||||
ssize_t cnt = 0; |
|
||||||
|
|
||||||
while (!done) { |
|
||||||
if (devs[cnt]->next == NULL) |
|
||||||
done = true; |
|
||||||
free(devs[cnt++]); |
|
||||||
} |
|
||||||
free(devs); |
|
||||||
} |
|
||||||
|
|
@ -1,108 +0,0 @@ |
|||||||
/**
|
|
||||||
* libztex.h - headers for Ztex 1.15x fpga board support library |
|
||||||
* |
|
||||||
* Copyright (c) 2012 nelisky.btc@gmail.com |
|
||||||
* |
|
||||||
* This work is based upon the Java SDK provided by ztex which is |
|
||||||
* Copyright (C) 2009-2011 ZTEX GmbH. |
|
||||||
* http://www.ztex.de
|
|
||||||
* |
|
||||||
* This program is free software; you can redistribute it and/or modify |
|
||||||
* it under the terms of the GNU General Public License version 2 as |
|
||||||
* published by the Free Software Foundation. |
|
||||||
* |
|
||||||
* This program is distributed in the hope that it will be useful, but |
|
||||||
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
||||||
* General Public License for more details. |
|
||||||
* |
|
||||||
* You should have received a copy of the GNU General Public License |
|
||||||
* along with this program; if not, see http://www.gnu.org/licenses/.
|
|
||||||
**/ |
|
||||||
#ifndef __LIBZTEX_H__ |
|
||||||
#define __LIBZTEX_H__ |
|
||||||
|
|
||||||
#include <libusb.h> |
|
||||||
|
|
||||||
#define LIBZTEX_MAX_DESCRIPTORS 512 |
|
||||||
#define LIBZTEX_SNSTRING_LEN 10 |
|
||||||
|
|
||||||
#define LIBZTEX_IDVENDOR 0x221A |
|
||||||
#define LIBZTEX_IDPRODUCT 0x0100 |
|
||||||
|
|
||||||
#define LIBZTEX_MAXMAXERRORRATE 0.05 |
|
||||||
#define LIBZTEX_ERRORHYSTERESIS 0.1 |
|
||||||
#define LIBZTEX_OVERHEATTHRESHOLD 0.4 |
|
||||||
|
|
||||||
struct libztex_fpgastate { |
|
||||||
bool fpgaConfigured; |
|
||||||
unsigned char fpgaChecksum; |
|
||||||
uint16_t fpgaBytes; |
|
||||||
unsigned char fpgaInitB; |
|
||||||
unsigned char fpgaFlashResult; |
|
||||||
bool fpgaFlashBitSwap; |
|
||||||
}; |
|
||||||
|
|
||||||
struct libztex_device { |
|
||||||
pthread_mutex_t mutex; |
|
||||||
struct libztex_device *root; |
|
||||||
int16_t fpgaNum; |
|
||||||
struct libusb_device_descriptor descriptor; |
|
||||||
libusb_device_handle *hndl; |
|
||||||
unsigned char usbbus; |
|
||||||
unsigned char usbaddress; |
|
||||||
unsigned char snString[LIBZTEX_SNSTRING_LEN+1]; |
|
||||||
unsigned char productId[4]; |
|
||||||
unsigned char fwVersion; |
|
||||||
unsigned char interfaceVersion; |
|
||||||
unsigned char interfaceCapabilities[6]; |
|
||||||
unsigned char moduleReserved[12]; |
|
||||||
uint8_t numNonces; |
|
||||||
uint16_t offsNonces; |
|
||||||
double freqM1; |
|
||||||
uint8_t freqM; |
|
||||||
uint8_t freqMaxM; |
|
||||||
uint8_t freqMDefault; |
|
||||||
char* bitFileName; |
|
||||||
bool suspendSupported; |
|
||||||
double hashesPerClock; |
|
||||||
uint8_t extraSolutions; |
|
||||||
|
|
||||||
double errorCount[256]; |
|
||||||
double errorWeight[256]; |
|
||||||
double errorRate[256]; |
|
||||||
double maxErrorRate[256]; |
|
||||||
|
|
||||||
int16_t nonceCheckValid; |
|
||||||
|
|
||||||
int16_t numberOfFpgas; |
|
||||||
int selectedFpga; |
|
||||||
bool parallelConfigSupport; |
|
||||||
|
|
||||||
char repr[20]; |
|
||||||
}; |
|
||||||
|
|
||||||
struct libztex_dev_list { |
|
||||||
struct libztex_device *dev; |
|
||||||
struct libztex_dev_list *next; |
|
||||||
}; |
|
||||||
|
|
||||||
struct libztex_hash_data { |
|
||||||
uint32_t goldenNonce[2]; |
|
||||||
uint32_t nonce; |
|
||||||
uint32_t hash7; |
|
||||||
}; |
|
||||||
|
|
||||||
extern int libztex_scanDevices (struct libztex_dev_list ***devs); |
|
||||||
extern void libztex_freeDevList (struct libztex_dev_list **devs); |
|
||||||
extern int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** ztex); |
|
||||||
extern void libztex_destroy_device (struct libztex_device* ztex); |
|
||||||
extern int libztex_configureFpga (struct libztex_device *dev); |
|
||||||
extern int libztex_setFreq (struct libztex_device *ztex, uint16_t freq); |
|
||||||
extern int libztex_sendHashData (struct libztex_device *ztex, unsigned char *sendbuf); |
|
||||||
extern int libztex_readHashData (struct libztex_device *ztex, struct libztex_hash_data nonces[]); |
|
||||||
extern int libztex_resetFpga (struct libztex_device *ztex); |
|
||||||
extern int libztex_selectFpga(struct libztex_device *ztex); |
|
||||||
extern int libztex_numberOfFpgas(struct libztex_device *ztex); |
|
||||||
|
|
||||||
#endif /* __LIBZTEX_H__ */ |
|
@ -1,5 +0,0 @@ |
|||||||
- verify setting cgpu.status=DEAD does in fact stop the thread |
|
||||||
- allow configuring bitstream directory |
|
||||||
- HS fpga config |
|
||||||
- allow configuring LIBZTEX_OVERHEATTHRESHOLD |
|
||||||
- hotplug support? |
|
Loading…
Reference in new issue