mirror of https://github.com/GOSTSec/sgminer
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
622 lines
18 KiB
622 lines
18 KiB
12 years ago
|
/*
|
||
|
* 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>
|
||
12 years ago
|
#include <math.h>
|
||
12 years ago
|
|
||
|
#include "config.h"
|
||
|
|
||
|
#ifdef WIN32
|
||
|
#include <windows.h>
|
||
|
#endif
|
||
|
|
||
|
#include "compat.h"
|
||
|
#include "miner.h"
|
||
|
#include "usbutils.h"
|
||
|
|
||
12 years ago
|
#define KLN "KLN"
|
||
12 years ago
|
#define K1 "K1"
|
||
|
#define K16 "K16"
|
||
|
#define K64 "K64"
|
||
|
|
||
12 years ago
|
#define MIDSTATE_BYTES 32
|
||
|
#define MERKLE_OFFSET 64
|
||
|
#define MERKLE_BYTES 12
|
||
12 years ago
|
|
||
11 years ago
|
#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
|
||
12 years ago
|
#define REPLY_WAIT_TIME 100 // poll interval for a cmd waiting it's reply
|
||
11 years ago
|
#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
|
||
12 years ago
|
|
||
12 years ago
|
struct device_drv klondike_drv;
|
||
12 years ago
|
|
||
12 years ago
|
typedef struct klondike_id {
|
||
12 years ago
|
uint8_t version;
|
||
12 years ago
|
uint8_t product[7];
|
||
12 years ago
|
uint32_t serial;
|
||
12 years ago
|
} IDENTITY;
|
||
12 years ago
|
|
||
12 years ago
|
typedef struct klondike_status {
|
||
12 years ago
|
uint8_t state;
|
||
|
uint8_t chipcount;
|
||
|
uint8_t slavecount;
|
||
|
uint8_t workqc;
|
||
|
uint8_t workid;
|
||
|
uint8_t temp;
|
||
|
uint8_t fanspeed;
|
||
12 years ago
|
uint8_t errorcount;
|
||
12 years ago
|
uint16_t hashcount;
|
||
12 years ago
|
uint16_t maxcount;
|
||
|
} WORKSTATUS;
|
||
|
|
||
|
typedef struct _worktask {
|
||
|
uint16_t pad1;
|
||
|
uint8_t pad2;
|
||
11 years ago
|
uint8_t workid;
|
||
|
uint32_t midstate[8];
|
||
|
uint32_t merkle[3];
|
||
12 years ago
|
} WORKTASK;
|
||
|
|
||
|
typedef struct _workresult {
|
||
|
uint16_t pad;
|
||
|
uint8_t device;
|
||
11 years ago
|
uint8_t workid;
|
||
|
uint32_t nonce;
|
||
12 years ago
|
} WORKRESULT;
|
||
|
|
||
|
typedef struct kondike_cfg {
|
||
12 years ago
|
uint16_t hashclock;
|
||
|
uint8_t temptarget;
|
||
|
uint8_t tempcritical;
|
||
|
uint8_t fantarget;
|
||
12 years ago
|
uint8_t pad;
|
||
|
} WORKCFG;
|
||
|
|
||
|
typedef struct device_info {
|
||
|
uint32_t noncecount;
|
||
|
uint32_t nextworkid;
|
||
|
uint16_t lasthashcount;
|
||
12 years ago
|
uint64_t totalhashcount;
|
||
|
uint32_t rangesize;
|
||
|
uint32_t *chipstats;
|
||
12 years ago
|
} 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;
|
||
12 years ago
|
};
|
||
|
|
||
12 years ago
|
IDENTITY KlondikeID;
|
||
|
|
||
12 years ago
|
static double cvtKlnToC(uint8_t temp)
|
||
|
{
|
||
|
return (double)1/((double)1/(25+273.15) + log((double)temp*1000/(256-temp)/2200)/3987) - 273.15;
|
||
|
}
|
||
|
|
||
|
static int cvtCToKln(double deg)
|
||
|
{
|
||
|
double R = exp((1/(deg+273.15)-1/(273.15+25))*3987)*2200;
|
||
|
return 256*R/(R+1000);
|
||
|
}
|
||
|
|
||
12 years ago
|
static char *SendCmdGetReply(struct cgpu_info *klncgpu, char Cmd, int device, int datalen, void *data)
|
||
12 years ago
|
{
|
||
12 years ago
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
|
||
|
char outbuf[64];
|
||
12 years ago
|
int retries = CMD_REPLY_RETRIES;
|
||
12 years ago
|
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);
|
||
|
}
|
||
11 years ago
|
while (retries-- > 0 && klninfo->shutdown == false) {
|
||
11 years ago
|
cgsleep_ms(REPLY_WAIT_TIME);
|
||
11 years ago
|
while (*(klninfo->replies + chkreply*REPLY_BUFSIZE) != Cmd || *(klninfo->replies + chkreply*REPLY_BUFSIZE + 2) != device) {
|
||
11 years ago
|
if (++chkreply == MAX_REPLY_COUNT)
|
||
12 years ago
|
chkreply = 0;
|
||
11 years ago
|
if (chkreply == klninfo->nextreply)
|
||
12 years ago
|
break;
|
||
|
}
|
||
11 years ago
|
if (chkreply == klninfo->nextreply)
|
||
12 years ago
|
continue;
|
||
|
*(klninfo->replies + chkreply*REPLY_BUFSIZE) = '!'; // mark to prevent re-use
|
||
|
return klninfo->replies + chkreply*REPLY_BUFSIZE + 1;
|
||
|
}
|
||
|
return NULL;
|
||
|
}
|
||
12 years ago
|
|
||
|
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));
|
||
11 years ago
|
for (dev = 0; dev <= slaves; dev++) {
|
||
12 years ago
|
char *reply = SendCmdGetReply(klncgpu, 'S', dev, 0, NULL);
|
||
11 years ago
|
if (reply != NULL)
|
||
12 years ago
|
klninfo->status[dev] = *(WORKSTATUS *)(reply+2);
|
||
|
}
|
||
|
wr_unlock(&(klninfo->stat_lock));
|
||
|
|
||
|
// todo: detect slavecount change and realloc space
|
||
|
|
||
|
return true;
|
||
|
}
|
||
12 years ago
|
|
||
|
static bool klondike_init(struct cgpu_info *klncgpu)
|
||
12 years ago
|
{
|
||
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
|
||
12 years ago
|
int slaves, dev;
|
||
12 years ago
|
|
||
12 years ago
|
char *reply = SendCmdGetReply(klncgpu, 'S', 0, 0, NULL);
|
||
11 years ago
|
if (reply == NULL)
|
||
12 years ago
|
return false;
|
||
12 years ago
|
|
||
|
slaves = ((WORKSTATUS *)(reply+2))->slavecount;
|
||
11 years ago
|
if (klninfo->status == NULL) {
|
||
12 years ago
|
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");
|
||
|
}
|
||
12 years ago
|
|
||
|
WORKCFG cfgset = { 0,0,0,0,0 }; // zero init triggers read back only
|
||
|
double temp1, temp2;
|
||
|
int size = 2;
|
||
|
|
||
11 years ago
|
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);
|
||
12 years ago
|
cfgset.temptarget = cvtCToKln(temp1);
|
||
|
cfgset.tempcritical = cvtCToKln(temp2);
|
||
12 years ago
|
cfgset.fantarget = (int)255*cfgset.fantarget/100;
|
||
12 years ago
|
size = sizeof(cfgset);
|
||
|
}
|
||
|
|
||
11 years ago
|
for (dev = 0; dev <= slaves; dev++) {
|
||
12 years ago
|
char *reply = SendCmdGetReply(klncgpu, 'C', dev, size, &cfgset);
|
||
11 years ago
|
if (reply != NULL) {
|
||
12 years ago
|
klninfo->cfg[dev] = *(WORKCFG *)(reply+2);
|
||
12 years ago
|
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);
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|
||
12 years ago
|
klondike_get_stats(klncgpu);
|
||
11 years ago
|
for (dev = 0; dev <= slaves; dev++) {
|
||
12 years ago
|
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));
|
||
|
}
|
||
12 years ago
|
|
||
|
SendCmdGetReply(klncgpu, 'E', 0, 1, "1");
|
||
|
|
||
|
return true;
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
|
static bool klondike_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
|
||
|
{
|
||
12 years ago
|
struct cgpu_info *klncgpu = usb_alloc_cgpu(&klondike_drv, 1);
|
||
12 years ago
|
struct klondike_info *klninfo = NULL;
|
||
|
|
||
|
if (unlikely(!klncgpu))
|
||
|
quit(1, "Failed to calloc klncgpu in klondike_detect_one");
|
||
12 years ago
|
|
||
12 years ago
|
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)) {
|
||
12 years ago
|
int attempts = 0;
|
||
11 years ago
|
while (attempts++ < 3) {
|
||
12 years ago
|
char devpath[20], reply[REPLY_SIZE];
|
||
12 years ago
|
int sent, recd, err;
|
||
12 years ago
|
|
||
12 years ago
|
sprintf(devpath, "%d:%d", (int)(klncgpu->usbinfo.bus_number), (int)(klncgpu->usbinfo.device_address));
|
||
|
err = usb_write(klncgpu, "I", 2, &sent, C_REQUESTRESULTS);
|
||
12 years ago
|
if (err < 0 || sent != 2) {
|
||
12 years ago
|
applog(LOG_ERR, "%s (%s) detect write failed (%d:%d)", klncgpu->drv->dname, devpath, sent, err);
|
||
12 years ago
|
}
|
||
11 years ago
|
cgsleep_ms(REPLY_WAIT_TIME*10);
|
||
12 years ago
|
err = usb_read(klncgpu, reply, REPLY_SIZE, &recd, C_GETRESULTS);
|
||
12 years ago
|
if (err < 0) {
|
||
12 years ago
|
applog(LOG_ERR, "%s (%s) detect read failed (%d:%d)", klncgpu->drv->dname, devpath, recd, err);
|
||
12 years ago
|
} else if (recd < 1) {
|
||
12 years ago
|
applog(LOG_ERR, "%s (%s) detect empty reply (%d)", klncgpu->drv->dname, devpath, recd);
|
||
11 years ago
|
} else if (reply[0] == 'I' && reply[1] == 0) {
|
||
12 years ago
|
|
||
|
applog(LOG_DEBUG, "%s (%s) detect successful", klncgpu->drv->dname, devpath);
|
||
|
KlondikeID = *(IDENTITY *)(&reply[2]);
|
||
|
klncgpu->device_path = strdup(devpath);
|
||
|
update_usb_stats(klncgpu);
|
||
11 years ago
|
if (!add_cgpu(klncgpu))
|
||
12 years ago
|
break;
|
||
|
applog(LOG_DEBUG, "Klondike cgpu added");
|
||
12 years ago
|
return true;
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|
||
12 years ago
|
usb_uninit(klncgpu);
|
||
12 years ago
|
}
|
||
12 years ago
|
free(klninfo->replies);
|
||
12 years ago
|
free(klncgpu);
|
||
12 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
static void klondike_detect(void)
|
||
|
{
|
||
|
usb_detect(&klondike_drv, klondike_detect_one);
|
||
|
}
|
||
|
|
||
11 years ago
|
static void klondike_identify(__maybe_unused struct cgpu_info *klncgpu)
|
||
12 years ago
|
{
|
||
12 years ago
|
//SendCmdGetReply(klncgpu, 'I', 0, 0, NULL);
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
12 years ago
|
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;
|
||
|
|
||
12 years ago
|
applog(LOG_DEBUG, "Klondike FOUND NONCE (%02x:%08x)", result->workid, result->nonce);
|
||
12 years ago
|
|
||
12 years ago
|
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++;
|
||
|
wr_unlock(&(klninfo->stat_lock));
|
||
|
|
||
|
result->nonce = le32toh(result->nonce - 0xC0);
|
||
12 years ago
|
applog(LOG_DEBUG, "Klondike SUBMIT NONCE (%02x:%08x)", result->workid, result->nonce);
|
||
12 years ago
|
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)]++;
|
||
12 years ago
|
return;
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
applog(LOG_ERR, "%s%i:%d unknown work (%02x:%08x) - ignored",
|
||
12 years ago
|
klncgpu->drv->name, klncgpu->device_id, result->device, result->workid, result->nonce);
|
||
12 years ago
|
//inc_hw_errors(klncgpu->thr[0]);
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
// thread to keep looking for replies
|
||
|
static void *klondike_get_replies(void *userdata)
|
||
12 years ago
|
{
|
||
12 years ago
|
struct cgpu_info *klncgpu = (struct cgpu_info *)userdata;
|
||
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
|
||
|
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;
|
||
|
|
||
12 years ago
|
err = usb_read(klncgpu, replybuf+1, REPLY_SIZE, &recd, C_GETRESULTS);
|
||
11 years ago
|
if (!err && recd == REPLY_SIZE) {
|
||
|
if (opt_log_level <= LOG_DEBUG) {
|
||
|
char *hexdata = bin2hex((unsigned char *)(replybuf+1), recd);
|
||
12 years ago
|
applog(LOG_DEBUG, "%s (%s) reply [%s:%s]", klncgpu->drv->dname, klncgpu->device_path, replybuf+1, hexdata);
|
||
|
free(hexdata);
|
||
|
}
|
||
11 years ago
|
if (++klninfo->nextreply == MAX_REPLY_COUNT)
|
||
12 years ago
|
klninfo->nextreply = 0;
|
||
|
|
||
|
replybuf[0] = replybuf[1];
|
||
11 years ago
|
if (replybuf[0] == '=')
|
||
12 years ago
|
klondike_check_nonce(klncgpu, (WORKRESULT *)replybuf);
|
||
12 years ago
|
}
|
||
12 years ago
|
}
|
||
|
return NULL;
|
||
|
}
|
||
12 years ago
|
|
||
12 years ago
|
static void klondike_flush_work(struct cgpu_info *klncgpu)
|
||
|
{
|
||
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
|
||
|
int dev;
|
||
12 years ago
|
|
||
12 years ago
|
applog(LOG_DEBUG, "Klondike flushing work");
|
||
12 years ago
|
for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
|
||
|
char *reply = SendCmdGetReply(klncgpu, 'A', dev, 0, NULL);
|
||
11 years ago
|
if (reply != NULL) {
|
||
12 years ago
|
wr_lock(&(klninfo->stat_lock));
|
||
|
klninfo->status[dev] = *(WORKSTATUS *)(reply+2);
|
||
|
wr_unlock(&(klninfo->stat_lock));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
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
|
||
11 years ago
|
cgsleep_ms(100);
|
||
12 years ago
|
|
||
|
return klondike_init(klncgpu);
|
||
|
}
|
||
|
|
||
12 years ago
|
static bool klondike_thread_init(struct thr_info *thr)
|
||
|
{
|
||
12 years ago
|
struct cgpu_info *klncgpu = thr->cgpu;
|
||
12 years ago
|
|
||
12 years ago
|
if (klncgpu->usbinfo.nodev)
|
||
12 years ago
|
return false;
|
||
|
|
||
12 years ago
|
klondike_flush_work(klncgpu);
|
||
12 years ago
|
|
||
12 years ago
|
return true;
|
||
|
}
|
||
|
|
||
|
static void klondike_shutdown(struct thr_info *thr)
|
||
|
{
|
||
12 years ago
|
struct cgpu_info *klncgpu = thr->cgpu;
|
||
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
|
||
12 years ago
|
int dev;
|
||
12 years ago
|
|
||
12 years ago
|
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;
|
||
12 years ago
|
}
|
||
|
|
||
|
static void klondike_thread_enable(struct thr_info *thr)
|
||
|
{
|
||
12 years ago
|
struct cgpu_info *klncgpu = thr->cgpu;
|
||
12 years ago
|
|
||
12 years ago
|
if (klncgpu->usbinfo.nodev)
|
||
12 years ago
|
return;
|
||
12 years ago
|
|
||
12 years ago
|
//SendCmdGetReply(klncgpu, 'E', 0, 1, "0");
|
||
12 years ago
|
|
||
|
}
|
||
|
|
||
12 years ago
|
static bool klondike_send_work(struct cgpu_info *klncgpu, int dev, struct work *work)
|
||
12 years ago
|
{
|
||
12 years ago
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
|
||
12 years ago
|
struct work *tmp;
|
||
12 years ago
|
WORKTASK data;
|
||
12 years ago
|
|
||
12 years ago
|
if (klncgpu->usbinfo.nodev)
|
||
12 years ago
|
return false;
|
||
|
|
||
12 years ago
|
memcpy(data.midstate, work->midstate, MIDSTATE_BYTES);
|
||
|
memcpy(data.merkle, work->data + MERKLE_OFFSET, MERKLE_BYTES);
|
||
12 years ago
|
data.workid = (uint8_t)(klninfo->devinfo[dev].nextworkid++ & 0xFF);
|
||
12 years ago
|
work->subid = dev*256 + data.workid;
|
||
|
|
||
11 years ago
|
if (opt_log_level <= LOG_DEBUG) {
|
||
12 years ago
|
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);
|
||
12 years ago
|
char *reply = SendCmdGetReply(klncgpu, 'W', dev, sizeof(data)-3, &data.workid);
|
||
11 years ago
|
if (reply != NULL) {
|
||
12 years ago
|
wr_lock(&(klninfo->stat_lock));
|
||
|
klninfo->status[dev] = *(WORKSTATUS *)(reply+2);
|
||
|
wr_unlock(&(klninfo->stat_lock));
|
||
12 years ago
|
|
||
|
// remove old work
|
||
|
HASH_ITER(hh, klncgpu->queued_work, work, tmp) {
|
||
11 years ago
|
if (work->queued && (work->subid == (int)(dev*256 + ((klninfo->devinfo[dev].nextworkid-2*MAX_WORK_COUNT) & 0xFF))))
|
||
12 years ago
|
work_completed(klncgpu, work);
|
||
|
}
|
||
12 years ago
|
return true;
|
||
|
}
|
||
|
return false;
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
static bool klondike_queue_full(struct cgpu_info *klncgpu)
|
||
12 years ago
|
{
|
||
12 years ago
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
|
||
|
struct work *work = NULL;
|
||
|
int dev, queued;
|
||
12 years ago
|
|
||
11 years ago
|
for (queued = 0; queued < MAX_WORK_COUNT-1; queued++)
|
||
|
for (dev = 0; dev <= klninfo->status->slavecount; dev++)
|
||
12 years ago
|
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;
|
||
12 years ago
|
}
|
||
|
|
||
|
static int64_t klondike_scanwork(struct thr_info *thr)
|
||
|
{
|
||
12 years ago
|
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)
|
||
12 years ago
|
return -1;
|
||
12 years ago
|
|
||
11 years ago
|
restart_wait(thr, 200);
|
||
12 years ago
|
if (klninfo->status != NULL) {
|
||
|
rd_lock(&(klninfo->stat_lock));
|
||
11 years ago
|
for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
|
||
12 years ago
|
uint64_t newhashdev = 0;
|
||
11 years ago
|
if (klninfo->devinfo[dev].lasthashcount > klninfo->status[dev].hashcount) // todo: chg this to check workid for wrapped instead
|
||
12 years ago
|
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;
|
||
|
newhashcount += (newhashdev << 32) / klninfo->status[dev].maxcount;
|
||
12 years ago
|
|
||
12 years ago
|
// todo: check stats for critical conditions
|
||
12 years ago
|
|
||
12 years ago
|
}
|
||
|
rd_unlock(&(klninfo->stat_lock));
|
||
|
}
|
||
|
return newhashcount;
|
||
12 years ago
|
}
|
||
|
|
||
|
|
||
11 years ago
|
static void get_klondike_statline_before(char *buf, size_t siz, struct cgpu_info *klncgpu)
|
||
12 years ago
|
{
|
||
12 years ago
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
|
||
|
uint8_t temp = 0xFF;
|
||
12 years ago
|
uint16_t fan = 0;
|
||
12 years ago
|
int dev;
|
||
12 years ago
|
|
||
11 years ago
|
if (klninfo->status == NULL)
|
||
12 years ago
|
return;
|
||
12 years ago
|
|
||
|
rd_lock(&(klninfo->stat_lock));
|
||
|
for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
|
||
|
if (klninfo->status[dev].temp < temp)
|
||
|
temp = klninfo->status[dev].temp;
|
||
12 years ago
|
fan += klninfo->cfg[dev].fantarget;
|
||
12 years ago
|
}
|
||
12 years ago
|
fan /= klninfo->status->slavecount+1;
|
||
12 years ago
|
rd_unlock(&(klninfo->stat_lock));
|
||
|
|
||
11 years ago
|
tailsprintf(buf, siz, " %3.0fC %3d%% | ", cvtKlnToC(temp), fan*100/255);
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
static struct api_data *klondike_api_stats(struct cgpu_info *klncgpu)
|
||
12 years ago
|
{
|
||
12 years ago
|
struct klondike_info *klninfo = (struct klondike_info *)(klncgpu->device_data);
|
||
|
struct api_data *root = NULL;
|
||
12 years ago
|
char buf[32];
|
||
12 years ago
|
int dev;
|
||
12 years ago
|
|
||
11 years ago
|
if (klninfo->status == NULL)
|
||
12 years ago
|
return NULL;
|
||
|
|
||
12 years ago
|
rd_lock(&(klninfo->stat_lock));
|
||
12 years ago
|
for (dev = 0; dev <= klninfo->status->slavecount; dev++) {
|
||
|
|
||
12 years ago
|
float fTemp = cvtKlnToC(klninfo->status[dev].temp);
|
||
12 years ago
|
sprintf(buf, "Temp %d", dev);
|
||
|
root = api_add_temp(root, buf, &fTemp, true);
|
||
|
|
||
11 years ago
|
double dClk = (double)klninfo->cfg[dev].hashclock;
|
||
12 years ago
|
sprintf(buf, "Clock %d", dev);
|
||
|
root = api_add_freq(root, buf, &dClk, true);
|
||
12 years ago
|
|
||
12 years ago
|
unsigned int iFan = (unsigned int)100 * klninfo->cfg[dev].fantarget / 255;
|
||
12 years ago
|
sprintf(buf, "Fan Percent %d", dev);
|
||
11 years ago
|
root = api_add_int(root, buf, (int *)(&iFan), true);
|
||
12 years ago
|
|
||
12 years ago
|
iFan = 0;
|
||
11 years ago
|
if (klninfo->status[dev].fanspeed > 0)
|
||
12 years ago
|
iFan = (unsigned int)TACH_FACTOR / klninfo->status[dev].fanspeed;
|
||
12 years ago
|
sprintf(buf, "Fan RPM %d", dev);
|
||
11 years ago
|
root = api_add_int(root, buf, (int *)(&iFan), true);
|
||
12 years ago
|
|
||
11 years ago
|
if (klninfo->devinfo[dev].chipstats != NULL) {
|
||
11 years ago
|
char data[128];
|
||
12 years ago
|
int n;
|
||
|
sprintf(buf, "Nonces / Chip %d", dev);
|
||
11 years ago
|
for (n = 0; n < klninfo->status[dev].chipcount; n++)
|
||
11 years ago
|
sprintf(data+n*8, "%07d ", klninfo->devinfo[dev].chipstats[n]);
|
||
|
data[127] = 0;
|
||
12 years ago
|
root = api_add_string(root, buf, data, true);
|
||
|
|
||
|
sprintf(buf, "Errors / Chip %d", dev);
|
||
11 years ago
|
for (n = 0; n < klninfo->status[dev].chipcount; n++)
|
||
11 years ago
|
sprintf(data+n*8, "%07d ", klninfo->devinfo[dev].chipstats[n + klninfo->status[dev].chipcount]);
|
||
|
data[127] = 0;
|
||
12 years ago
|
root = api_add_string(root, buf, data, true);
|
||
|
}
|
||
12 years ago
|
}
|
||
12 years ago
|
rd_unlock(&(klninfo->stat_lock));
|
||
12 years ago
|
|
||
|
return root;
|
||
12 years ago
|
}
|
||
12 years ago
|
|
||
|
struct device_drv klondike_drv = {
|
||
|
.drv_id = DRIVER_KLONDIKE,
|
||
|
.dname = "Klondike",
|
||
12 years ago
|
.name = KLN,
|
||
12 years ago
|
.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
|
||
|
};
|