1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141 |
- #include "config.h"
- #include <stdarg.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <math.h>
- #include "logging.h"
- #include "miner.h"
- #include "usbutils.h"
- #include "fpgautils.h"
- #include "util.h"
- #define BITSTREAM_FILENAME "fpgaminer_top_fixed7_197MHz.ncd"
- #define BISTREAM_USER_ID "\2\4$B"
- #define BITSTREAM_MAGIC_0 0
- #define BITSTREAM_MAGIC_1 9
- #define MODMINER_CUTOFF_TEMP 60.0
- #define MODMINER_OVERHEAT_TEMP 50.0
- #define MODMINER_RECOVER_TEMP 46.5
- #define MODMINER_TEMP_UP_LIMIT 47.0
- #define MODMINER_HW_ERROR_PERCENT 0.75
- #define ITS_DEAD_JIM 300
- #define MODMINER_MAX_CLOCK 230
- #define MODMINER_DEF_CLOCK 200
- #define MODMINER_MIN_CLOCK 160
- #define MODMINER_CLOCK_UP 2
- #define MODMINER_CLOCK_SET 0
- #define MODMINER_CLOCK_DOWN -2
- #define MODMINER_CLOCK_OVERHEAT 0
- #define MODMINER_CLOCK_DEAD -6
- #define MODMINER_CLOCK_CUTOFF -10
- #define MODMINER_PING "\x00"
- #define MODMINER_GET_VERSION "\x01"
- #define MODMINER_FPGA_COUNT "\x02"
- #define MODMINER_GET_IDCODE '\x03'
- #define MODMINER_GET_USERCODE '\x04'
- #define MODMINER_PROGRAM '\x05'
- #define MODMINER_SET_CLOCK '\x06'
- #define MODMINER_READ_CLOCK '\x07'
- #define MODMINER_SEND_WORK '\x08'
- #define MODMINER_CHECK_WORK '\x09'
- #define MODMINER_TEMP1 '\x0a'
- #define MODMINER_TEMP2 '\x0d'
- #define MODMINER_SET_REG '\x0b'
- #define MODMINER_GET_REG '\x0c'
- #define FPGAID_ALL 4
- #define MODMINER_TRY_UP 96
- #define MODMINER_EARLY_UP 6
- #define MODMINER_MIN_BACK 12
- static const char NOOP[] = MODMINER_PING "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff";
- static void do_ping(struct cgpu_info *modminer)
- {
- char buf[0x100+1];
- int err, amount;
-
- err = usb_write(modminer, (char *)NOOP, sizeof(NOOP)-1, &amount, C_PING);
- applog(LOG_DEBUG, "%s%u: flush noop got %d err %d",
- modminer->drv->name, modminer->fpgaid, amount, err);
-
- while ((err = usb_read_once(modminer, buf, sizeof(buf)-1, &amount, C_CLEAR)) == 0 && amount > 0)
- applog(LOG_DEBUG, "%s%u: clear got %d",
- modminer->drv->name, modminer->fpgaid, amount);
- applog(LOG_DEBUG, "%s%u: final clear got %d err %d",
- modminer->drv->name, modminer->fpgaid, amount, err);
- }
- static struct cgpu_info *modminer_detect_one(struct libusb_device *dev, struct usb_find_devices *found)
- {
- char buf[0x100+1];
- char *devname = NULL;
- char devpath[32];
- int err, i, amount;
- bool added = false;
- struct cgpu_info *modminer = usb_alloc_cgpu(&modminer_drv, 1);
- modminer->modminer_mutex = calloc(1, sizeof(*(modminer->modminer_mutex)));
- mutex_init(modminer->modminer_mutex);
- modminer->fpgaid = (char)0;
- if (!usb_init(modminer, dev, found))
- goto shin;
- do_ping(modminer);
- if ((err = usb_write(modminer, MODMINER_GET_VERSION, 1, &amount, C_REQUESTVERSION)) < 0 || amount != 1) {
- applog(LOG_ERR, "%s detect (%s) send version request failed (%d:%d)",
- modminer->drv->dname, modminer->device_path, amount, err);
- goto unshin;
- }
- if ((err = usb_read_once(modminer, buf, sizeof(buf)-1, &amount, C_GETVERSION)) < 0 || amount < 1) {
- if (err < 0)
- applog(LOG_ERR, "%s detect (%s) no version reply (%d)",
- modminer->drv->dname, modminer->device_path, err);
- else
- applog(LOG_ERR, "%s detect (%s) empty version reply (%d)",
- modminer->drv->dname, modminer->device_path, amount);
- applog(LOG_DEBUG, "%s detect (%s) check the firmware",
- modminer->drv->dname, modminer->device_path);
- goto unshin;
- }
- buf[amount] = '\0';
- devname = strdup(buf);
- applog(LOG_DEBUG, "%s (%s) identified as: %s", modminer->drv->dname, modminer->device_path, devname);
- if ((err = usb_write(modminer, MODMINER_FPGA_COUNT, 1, &amount, C_REQUESTFPGACOUNT) < 0 || amount != 1)) {
- applog(LOG_ERR, "%s detect (%s) FPGA count request failed (%d:%d)",
- modminer->drv->dname, modminer->device_path, amount, err);
- goto unshin;
- }
- if ((err = usb_read(modminer, buf, 1, &amount, C_GETFPGACOUNT)) < 0 || amount != 1) {
- applog(LOG_ERR, "%s detect (%s) no FPGA count reply (%d:%d)",
- modminer->drv->dname, modminer->device_path, amount, err);
- goto unshin;
- }
-
-
- if (buf[0] == 0) {
- applog(LOG_ERR, "%s detect (%s) zero FPGA count from %s",
- modminer->drv->dname, modminer->device_path, devname);
- goto unshin;
- }
- if (buf[0] < 1 || buf[0] > 4) {
- applog(LOG_ERR, "%s detect (%s) invalid FPGA count (%u) from %s",
- modminer->drv->dname, modminer->device_path, buf[0], devname);
- goto unshin;
- }
- applog(LOG_DEBUG, "%s (%s) %s has %u FPGAs",
- modminer->drv->dname, modminer->device_path, devname, buf[0]);
- modminer->name = devname;
-
-
- for (i = 0; i < buf[0]; i++) {
- struct cgpu_info *tmp = usb_copy_cgpu(modminer);
- sprintf(devpath, "%d:%d:%d",
- (int)(modminer->usbinfo.bus_number),
- (int)(modminer->usbinfo.device_address),
- i);
- tmp->device_path = strdup(devpath);
-
- if (added)
- tmp->usbinfo.usbstat = USB_NOSTAT;
- tmp->fpgaid = (char)i;
- tmp->modminer_mutex = modminer->modminer_mutex;
- if (!add_cgpu(tmp)) {
- tmp = usb_free_cgpu(tmp);
- goto unshin;
- }
- update_usb_stats(tmp);
- added = true;
- }
- modminer = usb_free_cgpu(modminer);
- return modminer;
- unshin:
- if (!added)
- usb_uninit(modminer);
- shin:
- if (!added) {
- free(modminer->modminer_mutex);
- modminer->modminer_mutex = NULL;
- }
- modminer = usb_free_cgpu(modminer);
- if (added)
- return modminer;
- else
- return NULL;
- }
- static void modminer_detect(bool __maybe_unused hotplug)
- {
- usb_detect(&modminer_drv, modminer_detect_one);
- }
- static bool get_expect(struct cgpu_info *modminer, FILE *f, char c)
- {
- char buf;
- if (fread(&buf, 1, 1, f) != 1) {
- applog(LOG_ERR, "%s%u: Error (%d) reading bitstream (%c)",
- modminer->drv->name, modminer->device_id, errno, c);
- return false;
- }
- if (buf != c) {
- applog(LOG_ERR, "%s%u: bitstream code mismatch (%c)",
- modminer->drv->name, modminer->device_id, c);
- return false;
- }
- return true;
- }
- static bool get_info(struct cgpu_info *modminer, FILE *f, char *buf, int bufsiz, const char *name)
- {
- unsigned char siz[2];
- int len;
- if (fread(siz, 2, 1, f) != 1) {
- applog(LOG_ERR, "%s%u: Error (%d) reading bitstream '%s' len",
- modminer->drv->name, modminer->device_id, errno, name);
- return false;
- }
- len = siz[0] * 256 + siz[1];
- if (len >= bufsiz) {
- applog(LOG_ERR, "%s%u: Bitstream '%s' len too large (%d)",
- modminer->drv->name, modminer->device_id, name, len);
- return false;
- }
- if (fread(buf, len, 1, f) != 1) {
- applog(LOG_ERR, "%s%u: Error (%d) reading bitstream '%s'",
- modminer->drv->name, modminer->device_id, errno, name);
- return false;
- }
- buf[len] = '\0';
- return true;
- }
- #define USE_DEFAULT_TIMEOUT 0
- static bool get_status_timeout(struct cgpu_info *modminer, char *msg, unsigned int timeout, enum usb_cmds cmd)
- {
- int err, amount;
- char buf[1];
- if (timeout == USE_DEFAULT_TIMEOUT)
- err = usb_read(modminer, buf, 1, &amount, cmd);
- else
- err = usb_read_timeout(modminer, buf, 1, &amount, timeout, cmd);
- if (err < 0 || amount != 1) {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_ERR, "%s%u: Error (%d:%d) getting %s reply",
- modminer->drv->name, modminer->device_id, amount, err, msg);
- return false;
- }
- if (buf[0] != 1) {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_ERR, "%s%u: Error, invalid %s reply (was %d should be 1)",
- modminer->drv->name, modminer->device_id, msg, buf[0]);
- return false;
- }
- return true;
- }
- static bool get_status(struct cgpu_info *modminer, char *msg, enum usb_cmds cmd)
- {
- return get_status_timeout(modminer, msg, USE_DEFAULT_TIMEOUT, cmd);
- }
- static bool modminer_fpga_upload_bitstream(struct cgpu_info *modminer)
- {
- const char *bsfile = BITSTREAM_FILENAME;
- char buf[0x100], *p;
- char devmsg[64];
- unsigned char *ubuf = (unsigned char *)buf;
- unsigned long totlen, len;
- size_t buflen, remaining;
- float nextmsg, upto;
- char fpgaid = FPGAID_ALL;
- int err, amount, tries;
- char *ptr;
- FILE *f = open_bitstream("modminer", bsfile);
- if (!f) {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_ERR, "%s%u: Error (%d) opening bitstream file %s",
- modminer->drv->name, modminer->device_id, errno, bsfile);
- return false;
- }
- if (fread(buf, 2, 1, f) != 1) {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_ERR, "%s%u: Error (%d) reading bitstream magic",
- modminer->drv->name, modminer->device_id, errno);
- goto dame;
- }
- if (buf[0] != BITSTREAM_MAGIC_0 || buf[1] != BITSTREAM_MAGIC_1) {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_ERR, "%s%u: bitstream has incorrect magic (%u,%u) instead of (%u,%u)",
- modminer->drv->name, modminer->device_id,
- buf[0], buf[1],
- BITSTREAM_MAGIC_0, BITSTREAM_MAGIC_1);
- goto dame;
- }
- if (fseek(f, 11L, SEEK_CUR)) {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_ERR, "%s%u: Error (%d) bitstream seek failed",
- modminer->drv->name, modminer->device_id, errno);
- goto dame;
- }
- if (!get_expect(modminer, f, 'a'))
- goto undame;
- if (!get_info(modminer, f, buf, sizeof(buf), "Design name"))
- goto undame;
- applog(LOG_DEBUG, "%s%u: bitstream file '%s' info:",
- modminer->drv->name, modminer->device_id, bsfile);
- applog(LOG_DEBUG, " Design name: '%s'", buf);
- p = strrchr(buf, ';') ? : buf;
- p = strrchr(buf, '=') ? : p;
- if (p[0] == '=')
- p++;
- unsigned long fwusercode = (unsigned long)strtoll(p, &p, 16);
- if (p[0] != '\0') {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_ERR, "%s%u: Bad usercode in bitstream file",
- modminer->drv->name, modminer->device_id);
- goto dame;
- }
- if (fwusercode == 0xffffffff) {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_ERR, "%s%u: bitstream doesn't support user code",
- modminer->drv->name, modminer->device_id);
- goto dame;
- }
- applog(LOG_DEBUG, " Version: %lu, build %lu", (fwusercode >> 8) & 0xff, fwusercode & 0xff);
- if (!get_expect(modminer, f, 'b'))
- goto undame;
- if (!get_info(modminer, f, buf, sizeof(buf), "Part number"))
- goto undame;
- applog(LOG_DEBUG, " Part number: '%s'", buf);
- if (!get_expect(modminer, f, 'c'))
- goto undame;
- if (!get_info(modminer, f, buf, sizeof(buf), "Build date"))
- goto undame;
- applog(LOG_DEBUG, " Build date: '%s'", buf);
- if (!get_expect(modminer, f, 'd'))
- goto undame;
- if (!get_info(modminer, f, buf, sizeof(buf), "Build time"))
- goto undame;
- applog(LOG_DEBUG, " Build time: '%s'", buf);
- if (!get_expect(modminer, f, 'e'))
- goto undame;
- if (fread(buf, 4, 1, f) != 1) {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_ERR, "%s%u: Error (%d) reading bitstream data len",
- modminer->drv->name, modminer->device_id, errno);
- goto dame;
- }
- len = ((unsigned long)ubuf[0] << 24) | ((unsigned long)ubuf[1] << 16) | (ubuf[2] << 8) | ubuf[3];
- applog(LOG_DEBUG, " Bitstream size: %lu", len);
- strcpy(devmsg, modminer->device_path);
- ptr = strrchr(devmsg, ':');
- if (ptr)
- *ptr = '\0';
- applog(LOG_WARNING, "%s%u: Programming all FPGA on %s ... Mining will not start until complete",
- modminer->drv->name, modminer->device_id, devmsg);
- buf[0] = MODMINER_PROGRAM;
- buf[1] = fpgaid;
- buf[2] = (len >> 0) & 0xff;
- buf[3] = (len >> 8) & 0xff;
- buf[4] = (len >> 16) & 0xff;
- buf[5] = (len >> 24) & 0xff;
- if ((err = usb_write(modminer, buf, 6, &amount, C_STARTPROGRAM)) < 0 || amount != 6) {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_ERR, "%s%u: Program init failed (%d:%d)",
- modminer->drv->name, modminer->device_id, amount, err);
- goto dame;
- }
- if (!get_status(modminer, "initialise", C_STARTPROGRAMSTATUS))
- goto undame;
- #define WRITE_SIZE 32
- totlen = len;
- nextmsg = 0.1;
- while (len > 0) {
- buflen = len < WRITE_SIZE ? len : WRITE_SIZE;
- if (fread(buf, buflen, 1, f) != 1) {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_ERR, "%s%u: bitstream file read error %d (%lu bytes left)",
- modminer->drv->name, modminer->device_id, errno, len);
- goto dame;
- }
- tries = 0;
- ptr = buf;
- remaining = buflen;
- while ((err = usb_write(modminer, ptr, remaining, &amount, C_PROGRAM)) < 0 || amount != (int)remaining) {
- if (err == LIBUSB_ERROR_TIMEOUT && amount > 0 && ++tries < 4) {
- remaining -= amount;
- ptr += amount;
- if (opt_debug)
- applog(LOG_DEBUG, "%s%u: Program timeout (%d:%d) sent %d tries %d",
- modminer->drv->name, modminer->device_id,
- amount, err, (int)remaining, tries);
- if (!get_status(modminer, "write status", C_PROGRAMSTATUS2))
- goto dame;
- } else {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_ERR, "%s%u: Program failed (%d:%d) sent %d",
- modminer->drv->name, modminer->device_id, amount, err, (int)remaining);
- goto dame;
- }
- }
- if (!get_status(modminer, "write status", C_PROGRAMSTATUS))
- goto dame;
- len -= buflen;
- upto = (float)(totlen - len) / (float)(totlen);
- if (upto >= nextmsg) {
- applog(LOG_WARNING,
- "%s%u: Programming %.1f%% (%lu out of %lu)",
- modminer->drv->name, modminer->device_id, upto*100, (totlen - len), totlen);
- nextmsg += 0.1;
- }
- }
- if (!get_status(modminer, "final status", C_FINALPROGRAMSTATUS))
- goto undame;
- applog(LOG_WARNING, "%s%u: Programming completed for all FPGA on %s",
- modminer->drv->name, modminer->device_id, devmsg);
-
- cgsleep_ms(666);
- usb_set_dev_start(modminer);
- return true;
- undame:
- ;
- mutex_unlock(modminer->modminer_mutex);
- ;
- dame:
- fclose(f);
- return false;
- }
- static bool modminer_fpga_prepare(struct thr_info *thr)
- {
- struct modminer_fpga_state *state;
- state = thr->cgpu_data = calloc(1, sizeof(struct modminer_fpga_state));
- state->shares_to_good = MODMINER_EARLY_UP;
- state->overheated = false;
- return true;
- }
- static const char *clocknodev = "clock failed - no device";
- static const char *clockoldwork = "clock already changed for this work";
- static const char *clocktoolow = "clock too low";
- static const char *clocktoohi = "clock too high";
- static const char *clocksetfail = "clock set command failed";
- static const char *clockreplyfail = "clock reply failed";
- static const char *modminer_delta_clock(struct thr_info *thr, int delta, bool temp, bool force)
- {
- struct cgpu_info *modminer = thr->cgpu;
- struct modminer_fpga_state *state = thr->cgpu_data;
- unsigned char cmd[6], buf[1];
- int err, amount;
-
- if (modminer->usbinfo.nodev)
- return clocknodev;
-
- if (!state->new_work && !force)
- return clockoldwork;
- state->new_work = false;
- state->shares = 0;
- state->shares_last_hw = 0;
- state->hw_errors = 0;
-
- if (delta < 0 && (modminer->clock + delta) < MODMINER_MIN_CLOCK)
- return clocktoolow;
- if (delta > 0 && (modminer->clock + delta) > MODMINER_MAX_CLOCK)
- return clocktoohi;
- if (delta < 0) {
- if (temp)
- state->shares_to_good = MODMINER_MIN_BACK;
- else {
- if ((state->shares_to_good * 2) < MODMINER_TRY_UP)
- state->shares_to_good *= 2;
- else
- state->shares_to_good = MODMINER_TRY_UP;
- }
- }
- modminer->clock += delta;
- cmd[0] = MODMINER_SET_CLOCK;
- cmd[1] = modminer->fpgaid;
- cmd[2] = modminer->clock;
- cmd[3] = cmd[4] = cmd[5] = '\0';
- mutex_lock(modminer->modminer_mutex);
- if ((err = usb_write(modminer, (char *)cmd, 6, &amount, C_SETCLOCK)) < 0 || amount != 6) {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_ERR, "%s%u: Error writing set clock speed (%d:%d)",
- modminer->drv->name, modminer->device_id, amount, err);
- return clocksetfail;
- }
- if ((err = usb_read(modminer, (char *)(&buf), 1, &amount, C_REPLYSETCLOCK)) < 0 || amount != 1) {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_ERR, "%s%u: Error reading set clock speed (%d:%d)",
- modminer->drv->name, modminer->device_id, amount, err);
- return clockreplyfail;
- }
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_WARNING, "%s%u: Set clock speed %sto %u",
- modminer->drv->name, modminer->device_id,
- (delta < 0) ? "down " : (delta > 0 ? "up " : ""),
- modminer->clock);
- return NULL;
- }
- static bool modminer_fpga_init(struct thr_info *thr)
- {
- struct cgpu_info *modminer = thr->cgpu;
- unsigned char cmd[2], buf[4];
- int err, amount;
- mutex_lock(modminer->modminer_mutex);
- cmd[0] = MODMINER_GET_USERCODE;
- cmd[1] = modminer->fpgaid;
- if ((err = usb_write(modminer, (char *)cmd, 2, &amount, C_REQUESTUSERCODE)) < 0 || amount != 2) {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_ERR, "%s%u: Error requesting USER code (%d:%d)",
- modminer->drv->name, modminer->device_id, amount, err);
- return false;
- }
- if ((err = usb_read(modminer, (char *)buf, 4, &amount, C_GETUSERCODE)) < 0 || amount != 4) {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_ERR, "%s%u: Error reading USER code (%d:%d)",
- modminer->drv->name, modminer->device_id, amount, err);
- return false;
- }
- if (memcmp(buf, BISTREAM_USER_ID, 4)) {
- applog(LOG_ERR, "%s%u: FPGA not programmed",
- modminer->drv->name, modminer->device_id);
- if (!modminer_fpga_upload_bitstream(modminer))
- return false;
- mutex_unlock(modminer->modminer_mutex);
- } else {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_DEBUG, "%s%u: FPGA is already programmed :)",
- modminer->drv->name, modminer->device_id);
- }
- modminer->clock = MODMINER_DEF_CLOCK;
- modminer_delta_clock(thr, MODMINER_CLOCK_SET, false, false);
- thr->primary_thread = true;
- return true;
- }
- static void get_modminer_statline_before(char *buf, size_t bufsiz, struct cgpu_info *modminer)
- {
- tailsprintf(buf, bufsiz, "%s%.1fC %3uMHz",
- (modminer->temp < 10) ? " " : "",
- modminer->temp,
- (unsigned int)(modminer->clock));
- }
- static bool modminer_start_work(struct thr_info *thr, struct work *work)
- {
- struct cgpu_info *modminer = thr->cgpu;
- struct modminer_fpga_state *state = thr->cgpu_data;
- int err, amount;
- char cmd[48];
- bool sta;
- cmd[0] = MODMINER_SEND_WORK;
- cmd[1] = modminer->fpgaid;
- memcpy(&cmd[2], work->midstate, 32);
- memcpy(&cmd[34], work->data + 64, 12);
- if (state->first_work.tv_sec == 0)
- cgtime(&state->first_work);
- if (state->last_nonce.tv_sec == 0)
- cgtime(&state->last_nonce);
- mutex_lock(modminer->modminer_mutex);
- if ((err = usb_write(modminer, cmd, 46, &amount, C_SENDWORK)) < 0 || amount != 46) {
- mutex_unlock(modminer->modminer_mutex);
- applog(LOG_ERR, "%s%u: Start work failed (%d:%d)",
- modminer->drv->name, modminer->device_id, amount, err);
- return false;
- }
- cgtime(&state->tv_workstart);
- sta = get_status(modminer, "start work", C_SENDWORKSTATUS);
- if (sta) {
- mutex_unlock(modminer->modminer_mutex);
- state->new_work = true;
- }
- return sta;
- }
- static void check_temperature(struct thr_info *thr)
- {
- struct cgpu_info *modminer = thr->cgpu;
- struct modminer_fpga_state *state = thr->cgpu_data;
- char cmd[2], temperature[2];
- int tbytes, tamount;
- int amount;
-
- if (modminer->usbinfo.nodev)
- return;
- if (state->one_byte_temp) {
- cmd[0] = MODMINER_TEMP1;
- tbytes = 1;
- } else {
- cmd[0] = MODMINER_TEMP2;
- tbytes = 2;
- }
- cmd[1] = modminer->fpgaid;
- mutex_lock(modminer->modminer_mutex);
- if (usb_write(modminer, (char *)cmd, 2, &amount, C_REQUESTTEMPERATURE) == 0 && amount == 2 &&
- usb_read(modminer, (char *)(&temperature), tbytes, &tamount, C_GETTEMPERATURE) == 0 && tamount == tbytes) {
- mutex_unlock(modminer->modminer_mutex);
- if (state->one_byte_temp)
- modminer->temp = temperature[0];
- else {
-
- modminer->temp = roundf((temperature[1] * 256.0 + temperature[0]) / 0.128) / 1000.0;
- state->tried_two_byte_temp = true;
- }
- if (state->overheated) {
-
- if (modminer->temp < MODMINER_RECOVER_TEMP) {
- state->overheated = false;
- applog(LOG_WARNING, "%s%u: Recovered, temp less than (%.1f) now %.3f",
- modminer->drv->name, modminer->device_id,
- MODMINER_RECOVER_TEMP, modminer->temp);
- }
- }
- else if (modminer->temp >= MODMINER_OVERHEAT_TEMP) {
- if (modminer->temp >= MODMINER_CUTOFF_TEMP) {
- applog(LOG_WARNING, "%s%u: Hit thermal cutoff limit! (%.1f) at %.3f",
- modminer->drv->name, modminer->device_id,
- MODMINER_CUTOFF_TEMP, modminer->temp);
- modminer_delta_clock(thr, MODMINER_CLOCK_CUTOFF, true, false);
- state->overheated = true;
- dev_error(modminer, REASON_DEV_THERMAL_CUTOFF);
- } else {
- applog(LOG_WARNING, "%s%u: Overheat limit (%.1f) reached %.3f",
- modminer->drv->name, modminer->device_id,
- MODMINER_OVERHEAT_TEMP, modminer->temp);
-
- if (MODMINER_CLOCK_OVERHEAT != 0)
- modminer_delta_clock(thr, MODMINER_CLOCK_OVERHEAT, true, false);
- state->overheated = true;
- dev_error(modminer, REASON_DEV_OVER_HEAT);
- }
- }
- } else {
- mutex_unlock(modminer->modminer_mutex);
- if (!state->tried_two_byte_temp) {
- state->tried_two_byte_temp = true;
- state->one_byte_temp = true;
- }
- }
- }
- #define work_restart(thr) thr->work_restart
- static const double processtime = 17.0;
- static const double overheattime = 26.9;
- static uint64_t modminer_process_results(struct thr_info *thr, struct work *work)
- {
- struct cgpu_info *modminer = thr->cgpu;
- struct modminer_fpga_state *state = thr->cgpu_data;
- struct timeval now;
- char cmd[2];
- uint32_t nonce;
- uint32_t curr_hw_errors;
- int err, amount, amount2;
- int timeoutloop;
- double timeout;
- int temploop;
-
- if (modminer->usbinfo.nodev)
- return -1;
-
-
-
- check_temperature(thr);
- cmd[0] = MODMINER_CHECK_WORK;
- cmd[1] = modminer->fpgaid;
- timeoutloop = 0;
- temploop = 0;
- while (0x80085) {
- mutex_lock(modminer->modminer_mutex);
- if ((err = usb_write(modminer, cmd, 2, &amount, C_REQUESTWORKSTATUS)) < 0 || amount != 2) {
- mutex_unlock(modminer->modminer_mutex);
-
-
- if (err == LIBUSB_ERROR_TIMEOUT && ++timeoutloop < 5) {
- state->timeout_fail++;
- goto tryagain;
- }
- applog(LOG_ERR, "%s%u: Error sending (get nonce) (%d:%d)",
- modminer->drv->name, modminer->device_id, amount, err);
- return -1;
- }
- err = usb_read(modminer, (char *)(&nonce), 4, &amount, C_GETWORKSTATUS);
- while (err == LIBUSB_SUCCESS && amount < 4) {
- size_t remain = 4 - amount;
- char *pos = ((char *)(&nonce)) + amount;
- state->success_more++;
- err = usb_read(modminer, pos, remain, &amount2, C_GETWORKSTATUS);
- amount += amount2;
- }
- mutex_unlock(modminer->modminer_mutex);
- if (err < 0 || amount < 4) {
-
-
- if (err == LIBUSB_ERROR_TIMEOUT && ++timeoutloop < 10) {
- state->timeout_fail++;
- goto tryagain;
- }
- applog(LOG_ERR, "%s%u: Error reading (get nonce) (%d:%d)",
- modminer->drv->name, modminer->device_id, amount+amount2, err);
- }
- if (memcmp(&nonce, "\xff\xff\xff\xff", 4)) {
-
- state->shares++;
- curr_hw_errors = state->hw_errors;
- submit_nonce(thr, work, nonce);
- if (state->hw_errors > curr_hw_errors) {
- cgtime(&now);
-
- if (tdiff(&now, &state->first_work) < 2.0) {
- state->shares = 0;
- state->shares_last_hw = 0;
- state->hw_errors = 0;
- } else {
- state->shares_last_hw = state->shares;
- if (modminer->clock > MODMINER_DEF_CLOCK || state->hw_errors > 1) {
- float pct = (state->hw_errors * 100.0 / (state->shares ? : 1.0));
- if (pct >= MODMINER_HW_ERROR_PERCENT)
- modminer_delta_clock(thr, MODMINER_CLOCK_DOWN, false, false);
- }
- }
- } else {
- cgtime(&state->last_nonce);
- state->death_stage_one = false;
-
- if (((state->shares - state->shares_last_hw) >= state->shares_to_good) &&
- modminer->temp < MODMINER_TEMP_UP_LIMIT)
- modminer_delta_clock(thr, MODMINER_CLOCK_UP, false, false);
- }
- } else {
-
- double death = ITS_DEAD_JIM * (state->death_stage_one ? 2.0 : 1.0);
- cgtime(&now);
- if (tdiff(&now, &state->last_nonce) >= death) {
- if (state->death_stage_one) {
- modminer_delta_clock(thr, MODMINER_CLOCK_DEAD, false, true);
- applog(LOG_ERR, "%s%u: DEATH clock down",
- modminer->drv->name, modminer->device_id);
-
- state->last_nonce.tv_sec = 0;
- state->last_nonce.tv_usec = 0;
- state->death_stage_one = false;
- return -1;
- } else {
- modminer_delta_clock(thr, MODMINER_CLOCK_DEAD, false, true);
- applog(LOG_ERR, "%s%u: death clock down",
- modminer->drv->name, modminer->device_id);
- state->death_stage_one = true;
- }
- }
- }
- tryagain:
- if (work_restart(thr))
- break;
- if (state->overheated == true) {
-
- if (++temploop > 4) {
- check_temperature(thr);
- temploop = 0;
- }
- }
- if (state->overheated == true)
- timeout = overheattime;
- else
- timeout = processtime;
- cgtime(&now);
- if (tdiff(&now, &state->tv_workstart) > timeout)
- break;
-
- cgsleep_ms(100);
- if (work_restart(thr))
- break;
- }
- struct timeval tv_workend, elapsed;
- cgtime(&tv_workend);
- timersub(&tv_workend, &state->tv_workstart, &elapsed);
-
- uint64_t hashes = (uint64_t)modminer->clock * (((uint64_t)elapsed.tv_sec * 1000000) + elapsed.tv_usec);
-
- if (hashes > 0xffffffff)
- hashes = 0xffffffff;
- work->nonce = 0xffffffff;
- return hashes;
- }
- static int64_t modminer_scanhash(struct thr_info *thr, struct work *work, int64_t __maybe_unused max_nonce)
- {
- struct modminer_fpga_state *state = thr->cgpu_data;
- struct timeval tv1, tv2;
- int64_t hashes;
-
- if (thr->cgpu->usbinfo.nodev)
- return -1;
-
- if (state->overheated == true) {
- cgtime(&tv1);
- while (state->overheated == true) {
- check_temperature(thr);
-
- if (thr->cgpu->usbinfo.nodev)
- return -1;
- if (state->overheated == true) {
- cgtime(&tv2);
-
- if (work_restart(thr) || tdiff(&tv2, &tv1) > 30)
- return 0;
-
- cgsleep_ms(1000);
- }
- }
- }
- if (!modminer_start_work(thr, work))
- return -1;
- hashes = modminer_process_results(thr, work);
- if (hashes == -1)
- return hashes;
- return hashes;
- }
- static void modminer_hw_error(struct thr_info *thr)
- {
- struct modminer_fpga_state *state = thr->cgpu_data;
- state->hw_errors++;
- }
- static void modminer_fpga_shutdown(struct thr_info *thr)
- {
- free(thr->cgpu_data);
- thr->cgpu_data = NULL;
- }
- static char *modminer_set_device(struct cgpu_info *modminer, char *option, char *setting, char *replybuf, size_t siz)
- {
- const char *ret;
- int val;
- if (strcasecmp(option, "help") == 0) {
- snprintf(replybuf, siz, "clock: range %d-%d and a multiple of 2",
- MODMINER_MIN_CLOCK, MODMINER_MAX_CLOCK);
- return replybuf;
- }
- if (strcasecmp(option, "clock") == 0) {
- if (!setting || !*setting) {
- snprintf(replybuf, siz, "missing clock setting");
- return replybuf;
- }
- val = atoi(setting);
- if (val < MODMINER_MIN_CLOCK || val > MODMINER_MAX_CLOCK || (val & 1) != 0) {
- snprintf(replybuf, siz, "invalid clock: '%s' valid range %d-%d and a multiple of 2",
- setting, MODMINER_MIN_CLOCK, MODMINER_MAX_CLOCK);
- return replybuf;
- }
- val -= (int)(modminer->clock);
- ret = modminer_delta_clock(modminer->thr[0], val, false, true);
- if (ret) {
- snprintf(replybuf, siz, "Set clock failed: %s", ret);
- return replybuf;
- } else
- return NULL;
- }
- snprintf(replybuf, siz, "Unknown option: %s", option);
- return replybuf;
- }
- struct device_drv modminer_drv = {
- .drv_id = DRIVER_modminer,
- .dname = "ModMiner",
- .name = "MMQ",
- .drv_detect = modminer_detect,
- .get_statline_before = get_modminer_statline_before,
- .set_device = modminer_set_device,
- .thread_prepare = modminer_fpga_prepare,
- .thread_init = modminer_fpga_init,
- .scanhash = modminer_scanhash,
- .hw_error = modminer_hw_error,
- .thread_shutdown = modminer_fpga_shutdown,
- };
|