Browse Source

Merge branch 'cg_merges_20121207' into bfgminer

Luke Dashjr 13 years ago
parent
commit
0d5cc12498
13 changed files with 482 additions and 427 deletions
  1. 9 0
      README
  2. BIN
      bitstreams/ztex_ufm1_15d4.bin
  3. BIN
      bitstreams/ztex_ufm1_15y1.bin
  4. 4 6
      driver-cpu.c
  5. 0 3
      driver-opencl.c
  6. 7 9
      driver-ztex.c
  7. 354 182
      libztex.c
  8. 80 202
      miner.c
  9. 22 19
      miner.h
  10. 2 2
      sha256_4way.c
  11. 2 2
      sha256_via.c
  12. 1 0
      util.c
  13. 1 2
      windows-build.txt

+ 9 - 0
README

@@ -983,6 +983,15 @@ one. If you input the stratum port directly into your configuration, or use the
 special prefix "stratum+tcp://" instead of "http://", BFGMiner will ONLY try to
 special prefix "stratum+tcp://" instead of "http://", BFGMiner will ONLY try to
 use stratum protocol mining.
 use stratum protocol mining.
 
 
+Q: Why don't the statistics add up: Accepted, Rejected, Stale, Hardware Errors,
+Diff1 Work, etc. when mining greater than 1 difficulty shares?
+A: As an example, if you look at 'Difficulty Accepted' in the RPC API, the number
+of difficulty shares accepted does not usually exactly equal the amount of work
+done to find them. If you are mining at 8 difficulty, then you would expect on
+average to find one 8 difficulty share, per 8 single difficulty shares found.
+However, the number is actually random and converges over time, it is an average,
+not an exact value, thus you may find more or less than the expected average.
+
 ---
 ---
 
 
 This code is provided entirely free of charge by the programmer in his spare
 This code is provided entirely free of charge by the programmer in his spare

BIN
bitstreams/ztex_ufm1_15d4.bin


BIN
bitstreams/ztex_ufm1_15y1.bin


+ 4 - 6
driver-cpu.c

@@ -67,7 +67,7 @@ static inline void drop_policy(void)
 {
 {
 }
 }
 
 
-static inline void affine_to_cpu(__maybe_unused int id, __maybe_unused int cpu)
+static inline void affine_to_cpu(int __maybe_unused id, int __maybe_unused cpu)
 {
 {
 }
 }
 #endif
 #endif
@@ -75,7 +75,7 @@ static inline void affine_to_cpu(__maybe_unused int id, __maybe_unused int cpu)
 
 
 
 
 /* TODO: resolve externals */
 /* TODO: resolve externals */
-extern bool submit_work_sync(struct thr_info *thr, const struct work *work_in, struct timeval *tv);
+extern void submit_work_async(const struct work *work_in, struct timeval *tv);
 extern char *set_int_range(const char *arg, int *i, int min, int max);
 extern char *set_int_range(const char *arg, int *i, int min, int max);
 extern int dev_from_id(int thr_id);
 extern int dev_from_id(int thr_id);
 
 
@@ -785,7 +785,7 @@ static bool cpu_thread_prepare(struct thr_info *thr)
 	return true;
 	return true;
 }
 }
 
 
-static uint64_t cpu_can_limit_work(__maybe_unused struct thr_info *thr)
+static uint64_t cpu_can_limit_work(struct thr_info __maybe_unused *thr)
 {
 {
 	return 0xffff;
 	return 0xffff;
 }
 }
@@ -838,9 +838,7 @@ CPUSearch:
 	/* if nonce found, submit work */
 	/* if nonce found, submit work */
 	if (unlikely(rc)) {
 	if (unlikely(rc)) {
 		applog(LOG_DEBUG, "CPU %d found something?", dev_from_id(thr_id));
 		applog(LOG_DEBUG, "CPU %d found something?", dev_from_id(thr_id));
-		if (unlikely(!submit_work_sync(thr, work, NULL))) {
-			applog(LOG_ERR, "Failed to submit_work_sync in miner_thread %d", thr_id);
-		}
+		submit_work_async(work, NULL);
 		work->blk.nonce = last_nonce + 1;
 		work->blk.nonce = last_nonce + 1;
 		goto CPUSearch;
 		goto CPUSearch;
 	}
 	}

+ 0 - 3
driver-opencl.c

@@ -1805,6 +1805,3 @@ struct device_api opencl_api = {
 	.thread_shutdown = opencl_thread_shutdown,
 	.thread_shutdown = opencl_thread_shutdown,
 };
 };
 #endif
 #endif
-
-
-

+ 7 - 9
driver-ztex.c

@@ -186,7 +186,7 @@ static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
 	int backlog_p = 0, backlog_max;
 	int backlog_p = 0, backlog_max;
 	uint32_t *lastnonce;
 	uint32_t *lastnonce;
 	uint32_t nonce, noncecnt = 0;
 	uint32_t nonce, noncecnt = 0;
-	bool overflow, found, rv;
+	bool overflow, found;
 	struct libztex_hash_data hdata[GOLDEN_BACKLOG];
 	struct libztex_hash_data hdata[GOLDEN_BACKLOG];
 
 
 	ztex = thr->cgpu->device_ztex;
 	ztex = thr->cgpu->device_ztex;
@@ -213,22 +213,20 @@ static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
 	
 	
 	applog(LOG_DEBUG, "%s: sent hashdata", ztex->repr);
 	applog(LOG_DEBUG, "%s: sent hashdata", ztex->repr);
 
 
-	lastnonce = malloc(sizeof(uint32_t)*ztex->numNonces);
+	lastnonce = calloc(1, sizeof(uint32_t)*ztex->numNonces);
 	if (lastnonce == NULL) {
 	if (lastnonce == NULL) {
 		applog(LOG_ERR, "%s: failed to allocate lastnonce[%d]", ztex->repr, ztex->numNonces);
 		applog(LOG_ERR, "%s: failed to allocate lastnonce[%d]", ztex->repr, ztex->numNonces);
 		return -1;
 		return -1;
 	}
 	}
-	memset(lastnonce, 0, sizeof(uint32_t)*ztex->numNonces);
 
 
 	/* Add an extra slot for detecting dupes that lie around */
 	/* Add an extra slot for detecting dupes that lie around */
 	backlog_max = ztex->numNonces * (2 + ztex->extraSolutions);
 	backlog_max = ztex->numNonces * (2 + ztex->extraSolutions);
-	backlog = malloc(sizeof(uint32_t) * backlog_max);
+	backlog = calloc(1, sizeof(uint32_t) * backlog_max);
 	if (backlog == NULL) {
 	if (backlog == NULL) {
 		applog(LOG_ERR, "%s: failed to allocate backlog[%d]", ztex->repr, backlog_max);
 		applog(LOG_ERR, "%s: failed to allocate backlog[%d]", ztex->repr, backlog_max);
 		free(lastnonce);
 		free(lastnonce);
 		return -1;
 		return -1;
 	}
 	}
-	memset(backlog, 0, sizeof(uint32_t) * backlog_max);
 	
 	
 	overflow = false;
 	overflow = false;
 
 
@@ -297,9 +295,9 @@ static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
 							backlog_p = 0;
 							backlog_p = 0;
 						nonce = le32toh(nonce);
 						nonce = le32toh(nonce);
 						work->blk.nonce = 0xffffffff;
 						work->blk.nonce = 0xffffffff;
-						if ( (rv = test_nonce(work, nonce, false)) )
-						rv = submit_nonce(thr, work, nonce);
-						applog(LOG_DEBUG, "%s: submitted %0.8x (from N%dE%d) %d", ztex->repr, nonce, i, j, rv);
+						if (test_nonce(work, nonce, false))
+							submit_nonce(thr, work, nonce);
+						applog(LOG_DEBUG, "%s: submitted %0.8x (from N%dE%d)", ztex->repr, nonce, i, j);
 					}
 					}
 				}
 				}
 			}
 			}
@@ -370,10 +368,10 @@ static bool ztex_prepare(struct thr_info *thr)
 		ztex_releaseFpga(ztex);
 		ztex_releaseFpga(ztex);
 		return false;
 		return false;
 	}
 	}
-	ztex_releaseFpga(ztex);
 	ztex->dclk.freqM = ztex->dclk.freqMaxM+1;;
 	ztex->dclk.freqM = ztex->dclk.freqMaxM+1;;
 	//ztex_updateFreq(thr);
 	//ztex_updateFreq(thr);
 	libztex_setFreq(ztex, ztex->dclk.freqMDefault);
 	libztex_setFreq(ztex, ztex->dclk.freqMDefault);
+	ztex_releaseFpga(ztex);
 	applog(LOG_DEBUG, "%s: prepare", ztex->repr);
 	applog(LOG_DEBUG, "%s: prepare", ztex->repr);
 	return true;
 	return true;
 }
 }

+ 354 - 182
libztex.c

@@ -1,7 +1,9 @@
 /**
 /**
- *   libztex.c - Ztex 1.15x fpga board support library
+ *   libztex.c - Ztex 1.15x/1.15y fpga board support library
  *
  *
  *   Copyright (c) 2012 nelisky.btc@gmail.com
  *   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
  *   This work is based upon the Java SDK provided by ztex which is
  *   Copyright (C) 2009-2011 ZTEX GmbH.
  *   Copyright (C) 2009-2011 ZTEX GmbH.
@@ -20,16 +22,18 @@
  *   along with this program; if not, see http://www.gnu.org/licenses/.
  *   along with this program; if not, see http://www.gnu.org/licenses/.
 **/
 **/
 
 
+#define _GNU_SOURCE
+
 #include <stdio.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <unistd.h>
+#include <string.h>
+#include <config.h>
 
 
 #include "dynclock.h"
 #include "dynclock.h"
 #include "miner.h"
 #include "miner.h"
 #include "fpgautils.h"
 #include "fpgautils.h"
 #include "libztex.h"
 #include "libztex.h"
 
 
-#define BUFSIZE 256
-
 //* Capability index for EEPROM support.
 //* Capability index for EEPROM support.
 #define CAPABILITY_EEPROM 0,0
 #define CAPABILITY_EEPROM 0,0
 //* Capability index for FPGA configuration support. 
 //* Capability index for FPGA configuration support. 
@@ -47,22 +51,238 @@
 //* Capability index for multi FPGA support.
 //* Capability index for multi FPGA support.
 #define CAPABILITY_MULTI_FPGA 0,7
 #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: %s", __func__, libusb_error_name(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: %s", __func__, libusb_error_name(cnt));
+		return cnt;
+	}
 
 
-static bool libztex_checkDevice(struct libusb_device *dev)
+	/* 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 enum check_result libztex_checkDevice(struct libusb_device *dev)
 {
 {
+	FILE *fp = NULL;
+	libusb_device_handle *hndl = NULL;
 	struct libusb_device_descriptor desc;
 	struct libusb_device_descriptor desc;
-	int err;
+	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);
 	err = libusb_get_device_descriptor(dev, &desc);
 	if (unlikely(err != 0)) {
 	if (unlikely(err != 0)) {
 		applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
 		applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
-		return false;
+		return CHECK_ERROR;
 	}
 	}
-	if (!(desc.idVendor == LIBZTEX_IDVENDOR && desc.idProduct == LIBZTEX_IDPRODUCT)) {
+
+	if (desc.idVendor != LIBZTEX_IDVENDOR || desc.idProduct != LIBZTEX_IDPRODUCT) {
 		applog(LOG_DEBUG, "Not a ZTEX device %0.4x:%0.4x", desc.idVendor, desc.idProduct);
 		applog(LOG_DEBUG, "Not a ZTEX device %0.4x:%0.4x", desc.idVendor, desc.idProduct);
-		return false;
+		return CHECK_IS_NOT_ZTEX;
 	}
 	}
-	return true;
+
+	err = libusb_open(dev, &hndl);
+	if (err != LIBUSB_SUCCESS) {
+		applog(LOG_ERR, "%s: Can not open ZTEX device: %s", __func__, libusb_error_name(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__, got_bytes, 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;
+	}
+
+	// reset 1
+	buf[0] = 1;
+	cnt = libusb_control_transfer(hndl, 0x40, 0xA0, 0xE600, 0, buf, 1,1000);
+	if (cnt < 0)
+	{
+		applog(LOG_ERR, "Ztex reset 1 failed: %s", libusb_error_name(cnt));
+		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: %s", i, libusb_error_name(k));
+			goto done;
+		}
+	}
+
+	// reset 0
+	buf[0] = 0;
+	err = libusb_control_transfer(hndl, 0x40, 0xA0, 0xE600, 0, buf, 1,1000);
+	if (err < 0)
+	{
+		applog(LOG_ERR, "Ztex reset 0 failed: %s", libusb_error_name(err));
+		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)
 static bool libztex_checkCapability(struct libztex_device *ztex, int i, int j)
@@ -75,7 +295,7 @@ static bool libztex_checkCapability(struct libztex_device *ztex, int i, int j)
 	return true;
 	return true;
 }
 }
 
 
-static int libztex_detectBitstreamBitOrder(const unsigned char *buf, int size)
+static char libztex_detectBitstreamBitOrder(const unsigned char *buf, int size)
 {
 {
 	int i;
 	int i;
 
 
@@ -133,8 +353,7 @@ static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firm
 	struct libztex_fpgastate state;
 	struct libztex_fpgastate state;
 	const int transactionBytes = 65536;
 	const int transactionBytes = 65536;
 	unsigned char buf[transactionBytes], settings[2];
 	unsigned char buf[transactionBytes], settings[2];
-	int tries, cnt, buf_p, i;
-	ssize_t pos = 0;
+	int tries, cnt, err;
 	FILE *fp;
 	FILE *fp;
 
 
 	if (!libztex_checkCapability(ztex, CAPABILITY_HS_FPGA))
 	if (!libztex_checkCapability(ztex, CAPABILITY_HS_FPGA))
@@ -142,7 +361,7 @@ static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firm
 	libztex_getFpgaState(ztex, &state);
 	libztex_getFpgaState(ztex, &state);
 	if (!force && state.fpgaConfigured) {
 	if (!force && state.fpgaConfigured) {
 		applog(LOG_INFO, "Bitstream already configured");
 		applog(LOG_INFO, "Bitstream already configured");
-		return 1;
+		return 0;
 	}
 	}
 	cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x33, 0, 0, settings, 2, 1000);
 	cnt = libusb_control_transfer(ztex->hndl, 0xc0, 0x33, 0, 0, settings, 2, 1000);
 	if (unlikely(cnt < 0)) {
 	if (unlikely(cnt < 0)) {
@@ -155,56 +374,29 @@ static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firm
 	for (tries = 3; tries > 0; tries--) {
 	for (tries = 3; tries > 0; tries--) {
 		fp = open_bitstream("ztex", firmware);
 		fp = open_bitstream("ztex", firmware);
 		if (!fp) {
 		if (!fp) {
-			applog(LOG_ERR, "%s: failed to read firmware '%s'", ztex->repr, firmware);
+			applog(LOG_ERR, "%s: failed to read bitstream '%s'", ztex->repr, firmware);
 			return -2;
 			return -2;
 		}
 		}
 
 
-		while (pos < transactionBytes && !feof(fp)) {
-			buf[pos++] = getc(fp);
-		}
-
-		if (feof(fp))
-			pos--;
-
-		if (bs != 0 && bs != 1)
-			bs = libztex_detectBitstreamBitOrder(buf, transactionBytes < pos? transactionBytes: pos);
-
-
-		if (bs == 1)
-			libztex_swapBits(buf, pos);
-	 
 		libusb_control_transfer(ztex->hndl, 0x40, 0x34, 0, 0, NULL, 0, 1000);
 		libusb_control_transfer(ztex->hndl, 0x40, 0x34, 0, 0, NULL, 0, 1000);
 		// 0x34 - initHSFPGAConfiguration
 		// 0x34 - initHSFPGAConfiguration
 
 
-		buf_p = pos;
-		while (1) {
-			i = 0;
-			while (i < buf_p) {
-				if (libusb_bulk_transfer(ztex->hndl,
-				                                  settings[0],
-				                                  &buf[i],
-				                                  buf_p - i,
-				                                  &cnt, 1000) != 0) {
-					applog(LOG_ERR, "%s: Failed send hs fpga data", ztex->repr);
-					break;
-				}
-				usleep(500);
-				i += cnt;
-			}
-			if (i < buf_p || buf_p < transactionBytes)
-				break;
-			buf_p = 0;
-			while (buf_p < transactionBytes && !feof(fp)) {
-				buf[buf_p++] = getc(fp);
-			}
-			if (feof(fp))
-				buf_p--;
-			pos += buf_p;
-			if (buf_p == 0)
-				break;
+		do
+		{
+			int length = fread(buf,1,transactionBytes,fp);
+
+			if (bs != 0 && bs != 1)
+				bs = libztex_detectBitstreamBitOrder(buf, length);
 			if (bs == 1)
 			if (bs == 1)
-				libztex_swapBits(buf, buf_p);
+				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);
 		libusb_control_transfer(ztex->hndl, 0x40, 0x35, 0, 0, NULL, 0, 1000);
 		// 0x35 - finishHSFPGAConfiguration
 		// 0x35 - finishHSFPGAConfiguration
@@ -218,7 +410,6 @@ static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firm
 			applog(LOG_ERR, "%s: HS FPGA configuration failed: DONE pin does not go high", ztex->repr);
 			applog(LOG_ERR, "%s: HS FPGA configuration failed: DONE pin does not go high", ztex->repr);
 			return -3;
 			return -3;
 		}
 		}
-
 	}
 	}
 
 
 	libusb_release_interface(ztex->hndl, settings[1]);
 	libusb_release_interface(ztex->hndl, settings[1]);
@@ -226,16 +417,14 @@ static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firm
 	nmsleep(200);
 	nmsleep(200);
 	applog(LOG_INFO, "%s: HS FPGA configuration done", ztex->repr);
 	applog(LOG_INFO, "%s: HS FPGA configuration done", ztex->repr);
 	return 0;
 	return 0;
-
 }
 }
 
 
 static int libztex_configureFpgaLS(struct libztex_device *ztex, const char* firmware, bool force, char bs)
 static int libztex_configureFpgaLS(struct libztex_device *ztex, const char* firmware, bool force, char bs)
 {
 {
 	struct libztex_fpgastate state;
 	struct libztex_fpgastate state;
 	const int transactionBytes = 2048;
 	const int transactionBytes = 2048;
-	unsigned char buf[transactionBytes], cs;
-	int tries, cnt, buf_p, i;
-	ssize_t pos = 0;
+	unsigned char buf[transactionBytes];
+	int tries, cnt;
 	FILE *fp;
 	FILE *fp;
 
 
 	if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
 	if (!libztex_checkCapability(ztex, CAPABILITY_FPGA))
@@ -244,28 +433,16 @@ static int libztex_configureFpgaLS(struct libztex_device *ztex, const char* firm
 	libztex_getFpgaState(ztex, &state);
 	libztex_getFpgaState(ztex, &state);
 	if (!force && state.fpgaConfigured) {
 	if (!force && state.fpgaConfigured) {
 		applog(LOG_DEBUG, "Bitstream already configured");
 		applog(LOG_DEBUG, "Bitstream already configured");
-		return 1;
+		return 0;
 	}
 	}
 
 
 	for (tries = 10; tries > 0; tries--) {
 	for (tries = 10; tries > 0; tries--) {
 		fp = open_bitstream("ztex", firmware);
 		fp = open_bitstream("ztex", firmware);
 		if (!fp) {
 		if (!fp) {
-			applog(LOG_ERR, "%s: failed to read firmware '%s'", ztex->repr, firmware);
+			applog(LOG_ERR, "%s: failed to read bitstream '%s'", ztex->repr, firmware);
 			return -2;
 			return -2;
 		}
 		}
 
 
-		cs = 0;
-		while (pos < transactionBytes && !feof(fp)) {
-			buf[pos] = getc(fp);
-			cs += buf[pos++];
-		}
-
-		if (feof(fp))
-			pos--;
-
-		if (bs != 0 && bs != 1)
-			bs = libztex_detectBitstreamBitOrder(buf, transactionBytes < pos? transactionBytes: pos);
-
 		//* Reset fpga
 		//* Reset fpga
 		cnt = libztex_resetFpga(ztex);
 		cnt = libztex_resetFpga(ztex);
 		if (unlikely(cnt < 0)) {
 		if (unlikely(cnt < 0)) {
@@ -273,36 +450,24 @@ static int libztex_configureFpgaLS(struct libztex_device *ztex, const char* firm
 			continue;
 			continue;
 		}
 		}
 
 
-		if (bs == 1)
-			libztex_swapBits(buf, pos);
-	 
-		buf_p = pos;
-		while (1) {
-			i = 0;
-			while (i < buf_p) {
-				cnt = libusb_control_transfer(ztex->hndl, 0x40, 0x32, 0, 0, &buf[i], buf_p - i, 5000);
-				if (unlikely(cnt < 0)) {
-					applog(LOG_ERR, "%s: Failed send fpga data with err %d", ztex->repr, cnt);
-					break;
-				}
-				i += cnt;
-			}
-			if (i < buf_p || buf_p < transactionBytes)
+		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 hs fpga data", ztex->repr);
 				break;
 				break;
-			buf_p = 0;
-			while (buf_p < transactionBytes && !feof(fp)) {
-				buf[buf_p] = getc(fp);
-				cs += buf[buf_p++];
 			}
 			}
-			if (feof(fp))
-				buf_p--;
-			pos += buf_p;
-			if (buf_p == 0)
-				break;
-			if (bs == 1)
-				libztex_swapBits(buf, buf_p);
 		}
 		}
-		if (cnt >= 0)
+		while (!feof(fp));
+
+		if (cnt > 0)
 			tries = 0;
 			tries = 0;
 
 
 		fclose(fp);
 		fclose(fp);
@@ -326,9 +491,9 @@ int libztex_configureFpga(struct libztex_device *ztex)
 
 
 	strcpy(buf, ztex->bitFileName);
 	strcpy(buf, ztex->bitFileName);
 	strcat(buf, ".bit");
 	strcat(buf, ".bit");
-	rv = libztex_configureFpgaHS(ztex, buf, true, 2); 
+	rv = libztex_configureFpgaHS(ztex, buf, true, 2);
 	if (rv != 0)
 	if (rv != 0)
-		rv = libztex_configureFpgaLS(ztex, buf, true, 2); 
+		rv = libztex_configureFpgaLS(ztex, buf, true, 2);
 	return rv;
 	return rv;
 }
 }
 
 
@@ -415,72 +580,29 @@ int libztex_suspend(struct libztex_device *ztex) {
 }
 }
 
 
 int libztex_prepare_device(struct libusb_device *dev, struct libztex_device** ztex) {
 int libztex_prepare_device(struct libusb_device *dev, struct libztex_device** ztex) {
-	struct libztex_device *newdev;
+	struct libztex_device *newdev = *ztex;
 	int i, cnt, err;
 	int i, cnt, err;
 	unsigned char buf[64];
 	unsigned char buf[64];
-	uint16_t langid;
 
 
-	newdev = malloc(sizeof(struct libztex_device));
 	dclk_prepare(&newdev->dclk);
 	dclk_prepare(&newdev->dclk);
-	newdev->bitFileName = NULL;
-	newdev->numberOfFpgas = -1;
-	newdev->valid = false;
-	newdev->hndl = NULL;
-	*ztex = newdev;
+	err = libusb_open(dev, &newdev->hndl);
+	if (err != LIBUSB_SUCCESS) {
+		applog(LOG_ERR, "%s: Can not open ZTEX device: %s", __func__, libusb_error_name(err));
+		return CHECK_ERROR;
+	}
 
 
 	err = libusb_get_device_descriptor(dev, &newdev->descriptor);
 	err = libusb_get_device_descriptor(dev, &newdev->descriptor);
 	if (unlikely(err != 0)) {
 	if (unlikely(err != 0)) {
 		applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
 		applog(LOG_ERR, "Ztex check device: Failed to open read descriptor with error %d", err);
-		return err;
-	}
-
-	// Check vendorId and productId
-	if (!(newdev->descriptor.idVendor == LIBZTEX_IDVENDOR &&
-	    newdev->descriptor.idProduct == LIBZTEX_IDPRODUCT)) {
-		applog(LOG_ERR, "Not a ztex device? %0.4X, %0.4X", newdev->descriptor.idVendor, newdev->descriptor.idProduct);
-		return 1;
+		return CHECK_ERROR;
 	}
 	}
 
 
-	err = libusb_open(dev, &newdev->hndl);
-	if (unlikely(err != 0)) {
-		applog(LOG_ERR, "Ztex check device: Failed to open handle with error %d", err);
-		return err;
-	}
-
-	/* 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(newdev->hndl, LIBUSB_ENDPOINT_IN,
-	    LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_STRING << 8) | 0,
-	    0x0000, buf, sizeof(buf), 1000);
-	if (unlikely(cnt < 0)) {
-		applog(LOG_ERR, "Ztex check device: Failed to read device LANGIDs with err %d", cnt);
-		return cnt;
-	}
-
-	langid = libusb_le16_to_cpu(((uint16_t *)buf)[1]);
-
-	cnt = libusb_control_transfer(newdev->hndl, LIBUSB_ENDPOINT_IN,
-	    LIBUSB_REQUEST_GET_DESCRIPTOR,
-	    (LIBUSB_DT_STRING << 8) | newdev->descriptor.iSerialNumber,
-	    langid, buf, sizeof(buf), 1000);
+	cnt = libztex_get_string_descriptor_ascii(newdev->hndl, newdev->descriptor.iSerialNumber, newdev->snString, sizeof(newdev->snString));
 	if (unlikely(cnt < 0)) {
 	if (unlikely(cnt < 0)) {
 		applog(LOG_ERR, "Ztex check device: Failed to read device snString with err %d", cnt);
 		applog(LOG_ERR, "Ztex check device: Failed to read device snString with err %d", cnt);
 		return cnt;
 		return cnt;
 	}
 	}
 
 
-	/* num chars = (all bytes except bLength and bDescriptorType) / 2 */
-	for (i = 0; i <= (cnt - 2) / 2 && i < (int)sizeof(newdev->snString)-1; i++)
-		newdev->snString[i] = buf[2 + i*2];
-
-	newdev->snString[i] = 0;
-
 	cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
 	cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x22, 0, 0, buf, 40, 500);
 	if (unlikely(cnt < 0)) {
 	if (unlikely(cnt < 0)) {
 		applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
 		applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
@@ -517,7 +639,6 @@ int libztex_prepare_device(struct libusb_device *dev, struct libztex_device** zt
 	newdev->moduleReserved[10] = buf[28];
 	newdev->moduleReserved[10] = buf[28];
 	newdev->moduleReserved[11] = buf[29];
 	newdev->moduleReserved[11] = buf[29];
 
 
-
 	cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x82, 0, 0, buf, 64, 500);
 	cnt = libusb_control_transfer(newdev->hndl, 0xc0, 0x82, 0, 0, buf, 64, 500);
 	if (unlikely(cnt < 0)) {
 	if (unlikely(cnt < 0)) {
 		applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
 		applog(LOG_ERR, "Ztex check device: Failed to read ztex descriptor with err %d", cnt);
@@ -554,7 +675,7 @@ int libztex_prepare_device(struct libusb_device *dev, struct libztex_device** zt
 	newdev->suspendSupported = (buf[0] == 5);
 	newdev->suspendSupported = (buf[0] == 5);
 	newdev->hashesPerClock = buf[0] > 2? (((buf[8] & 255) | ((buf[9] & 255) << 8)) + 1) / 128.0: 1.0;
 	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;
 	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",
 	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->dclk.freqMaxM, newdev->dclk.freqM, newdev->suspendSupported ? "T": "F",
 	                 buf[0], newdev->numNonces, newdev->offsNonces, newdev->freqM1, newdev->dclk.freqMaxM, newdev->dclk.freqM, newdev->suspendSupported ? "T": "F",
 	                 newdev->hashesPerClock, newdev->extraSolutions);
 	                 newdev->hashesPerClock, newdev->extraSolutions);
@@ -588,57 +709,108 @@ void libztex_destroy_device(struct libztex_device* ztex)
 int libztex_scanDevices(struct libztex_dev_list*** devs_p)
 int libztex_scanDevices(struct libztex_dev_list*** devs_p)
 {
 {
 	int usbdevices[LIBZTEX_MAX_DESCRIPTORS];
 	int usbdevices[LIBZTEX_MAX_DESCRIPTORS];
-	struct libztex_dev_list **devs;
-	struct libztex_device *ztex;
-	int found = 0, pos = 0, err;
-	libusb_device **list;
-	ssize_t cnt, i = 0;
-
-	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", cnt);
-		return 0;
-	}
+	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", cnt);
+			goto done;
+		}
 
 
-	for (i = 0; i < cnt; i++) {
-		if (libztex_checkDevice(list[i])) {
-			// Got one!
-			usbdevices[found] = i;
-			found++;
+		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: %s", libusb_error_name(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);
 	devs = malloc(sizeof(struct libztex_dev_list *) * found);
 	if (devs == NULL) {
 	if (devs == NULL) {
 		applog(LOG_ERR, "Ztex scan devices: Failed to allocate memory");
 		applog(LOG_ERR, "Ztex scan devices: Failed to allocate memory");
-		return 0;
+		goto done;
 	}
 	}
 
 
 	for (i = 0; i < found; i++) {
 	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;
+		ztex->valid = false;
+
 		err = libztex_prepare_device(list[usbdevices[i]], &ztex);
 		err = libztex_prepare_device(list[usbdevices[i]], &ztex);
-		if (unlikely(err != 0))
+		if (unlikely(err != 0)) {
 			applog(LOG_ERR, "prepare device: %d", err);
 			applog(LOG_ERR, "prepare device: %d", err);
-		// check if valid
-		if (!ztex->valid) {
 			libztex_destroy_device(ztex);
 			libztex_destroy_device(ztex);
+			ztex = NULL;
 			continue;
 			continue;
 		}
 		}
+
 		devs[pos] = malloc(sizeof(struct libztex_dev_list));
 		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;
 		devs[pos]->dev = ztex;
+		ztex = NULL;
 		devs[pos]->next = NULL;
 		devs[pos]->next = NULL;
 		if (pos > 0)
 		if (pos > 0)
 			devs[pos - 1]->next = devs[pos];
 			devs[pos - 1]->next = devs[pos];
 		pos++;
 		pos++;
 	}
 	}
 
 
-	libusb_free_device_list(list, 1);
-	*devs_p = devs;
-	return 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 libztex_sendHashData(struct libztex_device *ztex, unsigned char *sendbuf)
 {
 {
-	int cnt, ret, len;
+	int cnt = 0, ret, len;
 
 
 	if (ztex == NULL || ztex->hndl == NULL)
 	if (ztex == NULL || ztex->hndl == NULL)
 		return 0;
 		return 0;
@@ -664,7 +836,7 @@ int libztex_readHashData(struct libztex_device *ztex, struct libztex_hash_data n
 
 
 	if (ztex->hndl == NULL)
 	if (ztex->hndl == NULL)
 		return 0;
 		return 0;
-	
+
 	rbuf = malloc(sizeof(unsigned char) * (ztex->numNonces * bufsize));
 	rbuf = malloc(sizeof(unsigned char) * (ztex->numNonces * bufsize));
 	if (rbuf == NULL) {
 	if (rbuf == NULL) {
 		applog(LOG_ERR, "%s: Failed to allocate memory for reading nonces", ztex->repr);
 		applog(LOG_ERR, "%s: Failed to allocate memory for reading nonces", ztex->repr);

+ 80 - 202
miner.c

@@ -79,20 +79,6 @@
 #	define USE_FPGA_SERIAL
 #	define USE_FPGA_SERIAL
 #endif
 #endif
 
 
-enum workio_commands {
-	WC_GET_WORK,
-	WC_SUBMIT_WORK,
-};
-
-struct workio_cmd {
-	enum workio_commands	cmd;
-	struct thr_info		*thr;
-	struct work		*work;
-	struct pool		*pool;
-
-	struct list_head list;
-};
-
 struct strategies strategies[] = {
 struct strategies strategies[] = {
 	{ "Failover" },
 	{ "Failover" },
 	{ "Round Robin" },
 	{ "Round Robin" },
@@ -233,11 +219,15 @@ static pthread_cond_t lp_cond;
 pthread_mutex_t restart_lock;
 pthread_mutex_t restart_lock;
 pthread_cond_t restart_cond;
 pthread_cond_t restart_cond;
 
 
+pthread_mutex_t kill_lock;
+pthread_cond_t kill_cond;
+
 double total_mhashes_done;
 double total_mhashes_done;
 static struct timeval total_tv_start, total_tv_end;
 static struct timeval total_tv_start, total_tv_end;
 static struct timeval miner_started;
 static struct timeval miner_started;
 
 
 pthread_mutex_t control_lock;
 pthread_mutex_t control_lock;
+pthread_mutex_t stats_lock;
 
 
 static pthread_mutex_t submitting_lock;
 static pthread_mutex_t submitting_lock;
 static int submitting, total_submitting;
 static int submitting, total_submitting;
@@ -1698,6 +1688,8 @@ void free_work(struct work *work)
 	free(work);
 	free(work);
 }
 }
 
 
+static char *workpadding = "000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000";
+
 static bool work_decode(struct pool *pool, struct work *work, json_t *val)
 static bool work_decode(struct pool *pool, struct work *work, json_t *val)
 {
 {
 	json_t *res_val = json_object_get(val, "result");
 	json_t *res_val = json_object_get(val, "result");
@@ -2466,12 +2458,15 @@ share_result(json_t *val, json_t *res, json_t *err, const struct work *work,
 	struct cgpu_info *cgpu = thr_info[work->thr_id].cgpu;
 	struct cgpu_info *cgpu = thr_info[work->thr_id].cgpu;
 
 
 	if ((json_is_null(err) || !err) && (json_is_null(res) || json_is_true(res))) {
 	if ((json_is_null(err) || !err) && (json_is_null(res) || json_is_true(res))) {
+		mutex_lock(&stats_lock);
 		cgpu->accepted++;
 		cgpu->accepted++;
 		total_accepted++;
 		total_accepted++;
 		pool->accepted++;
 		pool->accepted++;
 		cgpu->diff_accepted += work->work_difficulty;
 		cgpu->diff_accepted += work->work_difficulty;
 		total_diff_accepted += work->work_difficulty;
 		total_diff_accepted += work->work_difficulty;
 		pool->diff_accepted += work->work_difficulty;
 		pool->diff_accepted += work->work_difficulty;
+		mutex_unlock(&stats_lock);
+
 		pool->seq_rejects = 0;
 		pool->seq_rejects = 0;
 		cgpu->last_share_pool = pool->pool_no;
 		cgpu->last_share_pool = pool->pool_no;
 		cgpu->last_share_pool_time = time(NULL);
 		cgpu->last_share_pool_time = time(NULL);
@@ -2520,6 +2515,7 @@ share_result(json_t *val, json_t *res, json_t *err, const struct work *work,
 			test_work_current(&fakework);
 			test_work_current(&fakework);
 		}
 		}
 	} else {
 	} else {
+		mutex_lock(&stats_lock);
 		cgpu->rejected++;
 		cgpu->rejected++;
 		total_rejected++;
 		total_rejected++;
 		pool->rejected++;
 		pool->rejected++;
@@ -2527,6 +2523,8 @@ share_result(json_t *val, json_t *res, json_t *err, const struct work *work,
 		total_diff_rejected += work->work_difficulty;
 		total_diff_rejected += work->work_difficulty;
 		pool->diff_rejected += work->work_difficulty;
 		pool->diff_rejected += work->work_difficulty;
 		pool->seq_rejects++;
 		pool->seq_rejects++;
+		mutex_unlock(&stats_lock);
+
 		applog(LOG_DEBUG, "PROOF OF WORK RESULT: false (booooo)");
 		applog(LOG_DEBUG, "PROOF OF WORK RESULT: false (booooo)");
 		if (!QUIET) {
 		if (!QUIET) {
 			char where[20];
 			char where[20];
@@ -3029,24 +3027,6 @@ tryagain:
 	return rc;
 	return rc;
 }
 }
 
 
-static void workio_cmd_free(struct workio_cmd *wc)
-{
-	if (!wc)
-		return;
-
-	switch (wc->cmd) {
-	case WC_SUBMIT_WORK:
-		if (wc->work)
-		free_work(wc->work);
-		break;
-	default: /* do nothing */
-		break;
-	}
-
-	memset(wc, 0, sizeof(*wc));	/* poison */
-	free(wc);
-}
-
 #ifdef HAVE_CURSES
 #ifdef HAVE_CURSES
 static void disable_curses(void)
 static void disable_curses(void)
 {
 {
@@ -3125,6 +3105,11 @@ static void __kill_work(void)
 	applog(LOG_DEBUG, "Killing off API thread");
 	applog(LOG_DEBUG, "Killing off API thread");
 	thr = &thr_info[api_thr_id];
 	thr = &thr_info[api_thr_id];
 	thr_info_cancel(thr);
 	thr_info_cancel(thr);
+
+	applog(LOG_DEBUG, "Sending kill broadcast");
+	mutex_lock(&kill_lock);
+	pthread_cond_signal(&kill_cond);
+	mutex_unlock(&kill_lock);
 }
 }
 
 
 /* This should be the common exit path */
 /* This should be the common exit path */
@@ -3441,10 +3426,10 @@ static void gen_stratum_work(struct pool *pool, struct work *work);
 
 
 static void *get_work_thread(void *userdata)
 static void *get_work_thread(void *userdata)
 {
 {
-	struct workio_cmd *wc = (struct workio_cmd *)userdata;
+	struct pool *reqpool = (struct pool *)userdata;
+	struct pool *pool;
 	struct work *ret_work = NULL;
 	struct work *ret_work = NULL;
 	struct curl_ent *ce = NULL;
 	struct curl_ent *ce = NULL;
-	struct pool *pool;
 
 
 	pthread_detach(pthread_self());
 	pthread_detach(pthread_self());
 
 
@@ -3453,7 +3438,7 @@ static void *get_work_thread(void *userdata)
 	applog(LOG_DEBUG, "Creating extra get work thread");
 	applog(LOG_DEBUG, "Creating extra get work thread");
 
 
 retry:
 retry:
-	pool = wc->pool;
+	pool = reqpool;
 
 
 	if (pool->has_stratum) {
 	if (pool->has_stratum) {
 		while (!pool->stratum_active) {
 		while (!pool->stratum_active) {
@@ -3461,7 +3446,7 @@ retry:
 
 
 			sleep(5);
 			sleep(5);
 			if (altpool != pool) {
 			if (altpool != pool) {
-				wc->pool = altpool;
+				reqpool = altpool;
 				inc_queued(altpool);
 				inc_queued(altpool);
 				dec_queued(pool);
 				dec_queued(pool);
 				goto retry;
 				goto retry;
@@ -3494,7 +3479,7 @@ retry:
 		get_benchmark_work(ret_work);
 		get_benchmark_work(ret_work);
 		ret_work->queued = true;
 		ret_work->queued = true;
 	} else {
 	} else {
-		ret_work->pool = wc->pool;
+		ret_work->pool = reqpool;
 
 
 		if (!ce)
 		if (!ce)
 			ce = pop_curl_entry(pool);
 			ce = pop_curl_entry(pool);
@@ -3528,26 +3513,11 @@ retry:
 	}
 	}
 
 
 out:
 out:
-	workio_cmd_free(wc);
 	if (ce)
 	if (ce)
 		push_curl_entry(ce, pool);
 		push_curl_entry(ce, pool);
 	return NULL;
 	return NULL;
 }
 }
 
 
-/* As per the submit work system, we try to reuse the existing curl handles,
- * but start recruiting extra connections if we start accumulating queued
- * requests */
-static bool workio_get_work(struct workio_cmd *wc)
-{
-	pthread_t get_thread;
-
-	if (unlikely(pthread_create(&get_thread, NULL, get_work_thread, (void *)wc))) {
-		applog(LOG_ERR, "Failed to create get_work_thread");
-		return false;
-	}
-	return true;
-}
-
 static bool stale_work(struct work *work, bool share)
 static bool stale_work(struct work *work, bool share)
 {
 {
 	struct timeval now;
 	struct timeval now;
@@ -3650,10 +3620,13 @@ static void check_solve(struct work *work)
 static void submit_discard_share(struct work *work)
 static void submit_discard_share(struct work *work)
 {
 {
 	sharelog("discard", work);
 	sharelog("discard", work);
+
+	mutex_lock(&stats_lock);
 	++total_stale;
 	++total_stale;
 	++(work->pool->stale_shares);
 	++(work->pool->stale_shares);
 	total_diff_stale += work->work_difficulty;
 	total_diff_stale += work->work_difficulty;
 	work->pool->diff_stale += work->work_difficulty;
 	work->pool->diff_stale += work->work_difficulty;
+	mutex_unlock(&stats_lock);
 }
 }
 
 
 struct submit_work_state {
 struct submit_work_state {
@@ -3682,13 +3655,11 @@ static void sws_has_ce(struct submit_work_state *sws)
 	json_rpc_call_async(sws->ce->curl, pool->rpc_url, pool->rpc_userpass, sws->s, false, pool, true, sws);
 	json_rpc_call_async(sws->ce->curl, pool->rpc_url, pool->rpc_userpass, sws->s, false, pool, true, sws);
 }
 }
 
 
-static struct submit_work_state *begin_submission(struct workio_cmd *wc)
+static struct submit_work_state *begin_submission(struct work *work)
 {
 {
-	struct work *work;
 	struct pool *pool;
 	struct pool *pool;
 	struct submit_work_state *sws = NULL;
 	struct submit_work_state *sws = NULL;
 
 
-	work = wc->work;
 	pool = work->pool;
 	pool = work->pool;
 	sws = malloc(sizeof(*sws));
 	sws = malloc(sizeof(*sws));
 	*sws = (struct submit_work_state){
 	*sws = (struct submit_work_state){
@@ -3747,13 +3718,10 @@ static struct submit_work_state *begin_submission(struct workio_cmd *wc)
 		}
 		}
 	}
 	}
 
 
-	wc->work = NULL;
-	workio_cmd_free(wc);
 	return sws;
 	return sws;
 
 
 out:
 out:
 	free(sws);
 	free(sws);
-	workio_cmd_free(wc);
 	return NULL;
 	return NULL;
 }
 }
 
 
@@ -3805,9 +3773,8 @@ static void free_sws(struct submit_work_state *sws)
 	free(sws);
 	free(sws);
 }
 }
 
 
-static void *submit_work_thread(void *userdata)
+static void *submit_work_thread(__maybe_unused void *userdata)
 {
 {
-	struct workio_cmd *wc = (struct workio_cmd *)userdata;
 	int wip = 0;
 	int wip = 0;
 	CURLM *curlm;
 	CURLM *curlm;
 	long curlm_timeout_ms = -1;
 	long curlm_timeout_ms = -1;
@@ -3824,16 +3791,6 @@ static void *submit_work_thread(void *userdata)
 	curl_multi_setopt(curlm, CURLMOPT_TIMERFUNCTION, my_curl_timer_set);
 	curl_multi_setopt(curlm, CURLMOPT_TIMERFUNCTION, my_curl_timer_set);
 	curl_multi_setopt(curlm, CURLMOPT_TIMERDATA, &curlm_timeout_ms);
 	curl_multi_setopt(curlm, CURLMOPT_TIMERDATA, &curlm_timeout_ms);
 
 
-	if ( (sws = begin_submission(wc)) ) {
-		if (sws->ce)
-			curl_multi_add_handle(curlm, sws->ce->curl);
-		else
-		if (sws->s)
-			write_sws = sws;
-		++wip;
-		++total_submitting;
-	}
-
 	fd_set rfds, wfds, efds;
 	fd_set rfds, wfds, efds;
 	int maxfd;
 	int maxfd;
 	struct timeval timeout, *timeoutp;
 	struct timeval timeout, *timeoutp;
@@ -3847,9 +3804,9 @@ static void *submit_work_thread(void *userdata)
 			(void)read(submit_waiting_notifier[0], buf, sizeof(buf));
 			(void)read(submit_waiting_notifier[0], buf, sizeof(buf));
 		}
 		}
 		while (!list_empty(&submit_waiting)) {
 		while (!list_empty(&submit_waiting)) {
-			wc = list_entry(submit_waiting.next, struct workio_cmd, list);
-			list_del(&wc->list);
-			if ( (sws = begin_submission(wc)) ) {
+			struct work *work = list_entry(submit_waiting.next, struct work, list);
+			list_del(&work->list);
+			if ( (sws = begin_submission(work)) ) {
 				if (sws->ce)
 				if (sws->ce)
 					curl_multi_add_handle(curlm, sws->ce->curl);
 					curl_multi_add_handle(curlm, sws->ce->curl);
 				else if (sws->s) {
 				else if (sws->s) {
@@ -3952,21 +3909,6 @@ static void *submit_work_thread(void *userdata)
 	return NULL;
 	return NULL;
 }
 }
 
 
-/* We try to reuse curl handles as much as possible, but if there is already
- * work queued to be submitted, we start generating extra handles to submit
- * the shares to avoid ever increasing backlogs. This allows us to scale to
- * any size hardware */
-static bool workio_submit_work(struct workio_cmd *wc)
-{
-	pthread_t submit_thread;
-
-	if (unlikely(pthread_create(&submit_thread, NULL, submit_work_thread, (void *)wc))) {
-		applog(LOG_ERR, "Failed to create submit_work_thread");
-		return false;
-	}
-	return true;
-}
-
 /* Find the pool that currently has the highest priority */
 /* Find the pool that currently has the highest priority */
 static struct pool *priority_pool(int choice)
 static struct pool *priority_pool(int choice)
 {
 {
@@ -5312,58 +5254,6 @@ static void *input_thread(void __maybe_unused *userdata)
 }
 }
 #endif
 #endif
 
 
-/* This thread should not be shut down unless a problem occurs */
-static void *workio_thread(void *userdata)
-{
-	struct thr_info *mythr = userdata;
-	bool ok = true;
-
-	RenameThread("work_io");
-
-	while (ok) {
-		struct workio_cmd *wc;
-
-		applog(LOG_DEBUG, "Popping work to work thread");
-
-		/* wait for workio_cmd sent to us, on our queue */
-		wc = tq_pop(mythr->q, NULL);
-		if (unlikely(!wc)) {
-			applog(LOG_ERR, "Failed to tq_pop in workio_thread");
-			ok = false;
-			break;
-		}
-
-		/* process workio_cmd */
-		switch (wc->cmd) {
-		case WC_GET_WORK:
-			ok = workio_get_work(wc);
-			break;
-		case WC_SUBMIT_WORK:
-		{
-			mutex_lock(&submitting_lock);
-			if (submitting) {
-				list_add_tail(&wc->list, &submit_waiting);
-				(void)write(submit_waiting_notifier[1], "\0", 1);
-				mutex_unlock(&submitting_lock);
-				break;
-			}
-			++submitting;
-			mutex_unlock(&submitting_lock);
-
-			ok = workio_submit_work(wc);
-			break;
-		}
-		default:
-			ok = false;
-			break;
-		}
-	}
-
-	tq_freeze(mythr->q);
-
-	return NULL;
-}
-
 static void *api_thread(void *userdata)
 static void *api_thread(void *userdata)
 {
 {
 	struct thr_info *mythr = userdata;
 	struct thr_info *mythr = userdata;
@@ -6051,7 +5941,7 @@ static bool queue_request(void)
 {
 {
 	int ts, tq, maxq = opt_queue + mining_threads;
 	int ts, tq, maxq = opt_queue + mining_threads;
 	struct pool *pool, *cp;
 	struct pool *pool, *cp;
-	struct workio_cmd *wc;
+	pthread_t get_thread;
 	bool lagging;
 	bool lagging;
 
 
 	ts = total_staged();
 	ts = total_staged();
@@ -6068,24 +5958,11 @@ static bool queue_request(void)
 	if (pool->staged + pool->queued >= maxq)
 	if (pool->staged + pool->queued >= maxq)
 		return true;
 		return true;
 
 
-	/* fill out work request message */
-	wc = calloc(1, sizeof(*wc));
-	if (unlikely(!wc)) {
-		applog(LOG_ERR, "Failed to calloc wc in queue_request");
-		return false;
-	}
-
-	wc->cmd = WC_GET_WORK;
-	wc->pool = pool;
-
 	applog(LOG_DEBUG, "Queueing getwork request to work thread");
 	applog(LOG_DEBUG, "Queueing getwork request to work thread");
 
 
-	/* send work request to workio thread */
-	if (unlikely(!tq_push(thr_info[work_thr_id].q, wc))) {
-		applog(LOG_ERR, "Failed to tq_push in queue_request");
-		workio_cmd_free(wc);
-		return false;
-	}
+	/* send work request to get_work_thread */
+	if (unlikely(pthread_create(&get_thread, NULL, get_work_thread, (void *)pool)))
+		quit(1, "Failed to create get_work_thread in queue_request");
 
 
 	inc_queued(pool);
 	inc_queued(pool);
 
 
@@ -6275,7 +6152,7 @@ static void gen_stratum_work(struct pool *pool, struct work *work)
 	header = realloc_strcat(header, pool->swork.ntime);
 	header = realloc_strcat(header, pool->swork.ntime);
 	header = realloc_strcat(header, pool->swork.nbit);
 	header = realloc_strcat(header, pool->swork.nbit);
 	header = realloc_strcat(header, "00000000"); /* nonce */
 	header = realloc_strcat(header, "00000000"); /* nonce */
-	header = realloc_strcat(header, "000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000");
+	header = realloc_strcat(header, workpadding);
 
 
 	/* Store the stratum work diff to check it still matches the pool's
 	/* Store the stratum work diff to check it still matches the pool's
 	 * stratum diff when submitting shares */
 	 * stratum diff when submitting shares */
@@ -6410,35 +6287,30 @@ out:
 	work->mined = true;
 	work->mined = true;
 }
 }
 
 
-bool submit_work_sync(struct thr_info *thr, struct work *work_in, struct timeval *tv_work_found)
+void submit_work_async(struct work *work_in, struct timeval *tv_work_found)
 {
 {
-	struct workio_cmd *wc;
-
-	/* fill out work request message */
-	wc = calloc(1, sizeof(*wc));
-	if (unlikely(!wc)) {
-		applog(LOG_ERR, "Failed to calloc wc in submit_work_sync");
-		return false;
-	}
+	struct work *work = copy_work(work_in);
+	pthread_t submit_thread;
+	bool was_submitting;
 
 
-	wc->work = copy_work(work_in);
-	wc->cmd = WC_SUBMIT_WORK;
-	wc->thr = thr;
 	if (tv_work_found)
 	if (tv_work_found)
-		memcpy(&(wc->work->tv_work_found), tv_work_found, sizeof(struct timeval));
-
+		memcpy(&(work->tv_work_found), tv_work_found, sizeof(struct timeval));
 	applog(LOG_DEBUG, "Pushing submit work to work thread");
 	applog(LOG_DEBUG, "Pushing submit work to work thread");
 
 
-	/* send solution to workio thread */
-	if (unlikely(!tq_push(thr_info[work_thr_id].q, wc))) {
-		applog(LOG_ERR, "Failed to tq_push work in submit_work_sync");
-		goto err_out;
+	mutex_lock(&submitting_lock);
+	list_add_tail(&work->list, &submit_waiting);
+	was_submitting = submitting;
+	if (!submitting)
+		++submitting;
+	mutex_unlock(&submitting_lock);
+
+	if (was_submitting) {
+		(void)write(submit_waiting_notifier[1], "\0", 1);
+		return;
 	}
 	}
 
 
-	return true;
-err_out:
-	workio_cmd_free(wc);
-	return false;
+	if (unlikely(pthread_create(&submit_thread, NULL, submit_work_thread, NULL)))
+		quit(1, "Failed to create submit_work_thread");
 }
 }
 
 
 enum test_nonce2_result hashtest2(struct work *work, bool checktarget)
 enum test_nonce2_result hashtest2(struct work *work, bool checktarget)
@@ -6487,14 +6359,16 @@ enum test_nonce2_result _test_nonce2(struct work *work, uint32_t nonce, bool che
 	return hashtest2(work, checktarget);
 	return hashtest2(work, checktarget);
 }
 }
 
 
-bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
+void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
 {
 {
 	struct timeval tv_work_found;
 	struct timeval tv_work_found;
 	gettimeofday(&tv_work_found, NULL);
 	gettimeofday(&tv_work_found, NULL);
 
 
+	mutex_lock(&stats_lock);
 	total_diff1++;
 	total_diff1++;
 	thr->cgpu->diff1++;
 	thr->cgpu->diff1++;
 	work->pool->diff1++;
 	work->pool->diff1++;
+	mutex_unlock(&stats_lock);
 
 
 	/* Do one last check before attempting to submit the work */
 	/* Do one last check before attempting to submit the work */
 	/* Side effect: sets work->data for us */
 	/* Side effect: sets work->data for us */
@@ -6504,13 +6378,14 @@ bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
 			struct cgpu_info *cgpu = thr->cgpu;
 			struct cgpu_info *cgpu = thr->cgpu;
 			applog(LOG_WARNING, "%s %u: invalid nonce - HW error",
 			applog(LOG_WARNING, "%s %u: invalid nonce - HW error",
 			       cgpu->api->name, cgpu->device_id);
 			       cgpu->api->name, cgpu->device_id);
+			mutex_lock(&stats_lock);
 			++hw_errors;
 			++hw_errors;
 			++thr->cgpu->hw_errors;
 			++thr->cgpu->hw_errors;
+			mutex_unlock(&stats_lock);
 
 
 			if (thr->cgpu->api->hw_error)
 			if (thr->cgpu->api->hw_error)
 				thr->cgpu->api->hw_error(thr);
 				thr->cgpu->api->hw_error(thr);
-
-			return false;
+			return;
 		}
 		}
 		case TNR_HIGH:
 		case TNR_HIGH:
 			// Share above target, normal
 			// Share above target, normal
@@ -6520,11 +6395,11 @@ bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce)
 				scrypt_diff(work);
 				scrypt_diff(work);
 			else
 			else
 				share_diff(work);
 				share_diff(work);
-			return true;
+			return;
 		case TNR_GOOD:
 		case TNR_GOOD:
 			break;
 			break;
 	}
 	}
-	return submit_work_sync(thr, work, &tv_work_found);
+	submit_work_async(work, &tv_work_found);
 }
 }
 
 
 static inline bool abandon_work(struct work *work, struct timeval *wdiff, uint64_t hashes)
 static inline bool abandon_work(struct work *work, struct timeval *wdiff, uint64_t hashes)
@@ -7050,7 +6925,16 @@ static void *watchpool_thread(void __maybe_unused *userdata)
 			if (!opt_benchmark)
 			if (!opt_benchmark)
 				reap_curl(pool);
 				reap_curl(pool);
 
 
-			if (pool->enabled == POOL_DISABLED || pool->has_stratum)
+			/* Get a rolling utility per pool over 10 mins */
+			if (intervals > 19) {
+				int shares = pool->diff1 - pool->last_shares;
+
+				pool->last_shares = pool->diff1;
+				pool->utility = (pool->utility + (double)shares * 0.63) / 1.63;
+				pool->shares = pool->utility;
+			}
+
+			if ((pool->enabled == POOL_DISABLED || pool->has_stratum) && pool->probed)
 				continue;
 				continue;
 
 
 			/* Test pool is idle once every minute */
 			/* Test pool is idle once every minute */
@@ -7060,14 +6944,6 @@ static void *watchpool_thread(void __maybe_unused *userdata)
 					pool_resus(pool);
 					pool_resus(pool);
 			}
 			}
 
 
-			/* Get a rolling utility per pool over 10 mins */
-			if (intervals > 19) {
-				int shares = pool->diff1 - pool->last_shares;
-
-				pool->last_shares = pool->diff1;
-				pool->utility = (pool->utility + (double)shares * 0.63) / 1.63;
-				pool->shares = pool->utility;
-			}
 		}
 		}
 
 
 		if (pool_strategy == POOL_ROTATE && now.tv_sec - rotate_tv.tv_sec > 60 * opt_rotate_period) {
 		if (pool_strategy == POOL_ROTATE && now.tv_sec - rotate_tv.tv_sec > 60 * opt_rotate_period) {
@@ -7765,6 +7641,7 @@ int main(int argc, char *argv[])
 	mutex_init(&qd_lock);
 	mutex_init(&qd_lock);
 	mutex_init(&console_lock);
 	mutex_init(&console_lock);
 	mutex_init(&control_lock);
 	mutex_init(&control_lock);
+	mutex_init(&stats_lock);
 	mutex_init(&sharelog_lock);
 	mutex_init(&sharelog_lock);
 	mutex_init(&ch_lock);
 	mutex_init(&ch_lock);
 	mutex_init(&sshare_lock);
 	mutex_init(&sshare_lock);
@@ -7779,6 +7656,10 @@ int main(int argc, char *argv[])
 	if (unlikely(pthread_cond_init(&restart_cond, NULL)))
 	if (unlikely(pthread_cond_init(&restart_cond, NULL)))
 		quit(1, "Failed to pthread_cond_init restart_cond");
 		quit(1, "Failed to pthread_cond_init restart_cond");
 
 
+	mutex_init(&kill_lock);
+	if (unlikely(pthread_cond_init(&kill_cond, NULL)))
+		quit(1, "Failed to pthread_cond_init kill_cond");
+
 	notifier_init(submit_waiting_notifier);
 	notifier_init(submit_waiting_notifier);
 
 
 	sprintf(packagename, "%s %s", PACKAGE, VERSION);
 	sprintf(packagename, "%s %s", PACKAGE, VERSION);
@@ -8101,10 +7982,6 @@ int main(int argc, char *argv[])
 	if (!thr->q)
 	if (!thr->q)
 		quit(1, "Failed to tq_new");
 		quit(1, "Failed to tq_new");
 
 
-	/* start work I/O thread */
-	if (thr_info_create(thr, NULL, workio_thread, thr))
-		quit(1, "workio thread create failed");
-
 	stage_thr_id = mining_threads + 1;
 	stage_thr_id = mining_threads + 1;
 	thr = &thr_info[stage_thr_id];
 	thr = &thr_info[stage_thr_id];
 	thr->q = tq_new();
 	thr->q = tq_new();
@@ -8325,10 +8202,11 @@ begin_bench:
 	for (i = 0; i < mining_threads + opt_queue; i++)
 	for (i = 0; i < mining_threads + opt_queue; i++)
 		queue_request();
 		queue_request();
 
 
-	/* main loop - simply wait for workio thread to exit. This is not the
-	 * normal exit path and only occurs should the workio_thread die
-	 * unexpectedly */
-	pthread_join(thr_info[work_thr_id].pth, NULL);
+	/* Wait till we receive the conditional telling us to die */
+	mutex_lock(&kill_lock);
+	pthread_cond_wait(&kill_cond, &kill_lock);
+	mutex_unlock(&kill_lock);
+
 	applog(LOG_INFO, "workio thread dead, exiting.");
 	applog(LOG_INFO, "workio thread dead, exiting.");
 
 
 	clean_up();
 	clean_up();

+ 22 - 19
miner.h

@@ -292,31 +292,31 @@ struct thr_info;
 struct work;
 struct work;
 
 
 struct device_api {
 struct device_api {
-	const char*dname;
-	const char*name;
+	const char *dname;
+	const char *name;
 
 
 	// API-global functions
 	// API-global functions
 	void (*api_detect)();
 	void (*api_detect)();
 
 
 	// Device-specific functions
 	// Device-specific functions
-	void (*reinit_device)(struct cgpu_info*);
-	void (*get_statline_before)(char*, struct cgpu_info*);
-	void (*get_statline)(char*, struct cgpu_info*);
-	struct api_data* (*get_api_extra_device_detail)(struct cgpu_info*);
-	struct api_data* (*get_api_extra_device_status)(struct cgpu_info*);
-	struct api_data *(*get_api_stats)(struct cgpu_info*);
-	bool (*get_stats)(struct cgpu_info*);
-	bool (*identify_device)(struct cgpu_info*);  // e.g. to flash a led
+	void (*reinit_device)(struct cgpu_info *);
+	void (*get_statline_before)(char *, struct cgpu_info *);
+	void (*get_statline)(char *, struct cgpu_info *);
+	struct api_data* (*get_api_extra_device_detail)(struct cgpu_info *);
+	struct api_data* (*get_api_extra_device_status)(struct cgpu_info *);
+	struct api_data *(*get_api_stats)(struct cgpu_info *);
+	bool (*get_stats)(struct cgpu_info *);
+	bool (*identify_device)(struct cgpu_info *);  // e.g. to flash a led
 
 
 	// Thread-specific functions
 	// Thread-specific functions
-	bool (*thread_prepare)(struct thr_info*);
-	uint64_t (*can_limit_work)(struct thr_info*);
-	bool (*thread_init)(struct thr_info*);
-	bool (*prepare_work)(struct thr_info*, struct work*);
-	int64_t (*scanhash)(struct thr_info*, struct work*, int64_t);
-	void (*hw_error)(struct thr_info*);
-	void (*thread_shutdown)(struct thr_info*);
-	void (*thread_enable)(struct thr_info*);
+	bool (*thread_prepare)(struct thr_info *);
+	uint64_t (*can_limit_work)(struct thr_info *);
+	bool (*thread_init)(struct thr_info *);
+	bool (*prepare_work)(struct thr_info *, struct work *);
+	int64_t (*scanhash)(struct thr_info *, struct work *, int64_t);
+	void (*hw_error)(struct thr_info *);
+	void (*thread_shutdown)(struct thr_info *);
+	void (*thread_enable)(struct thr_info *);
 };
 };
 
 
 enum dev_enable {
 enum dev_enable {
@@ -1063,6 +1063,9 @@ struct work {
 	struct timeval	tv_work_start;
 	struct timeval	tv_work_start;
 	struct timeval	tv_work_found;
 	struct timeval	tv_work_found;
 	char		getwork_mode;
 	char		getwork_mode;
+
+	/* Used to queue shares in submit_waiting */
+	struct list_head list;
 };
 };
 
 
 extern void get_datestamp(char *, struct timeval *);
 extern void get_datestamp(char *, struct timeval *);
@@ -1074,7 +1077,7 @@ enum test_nonce2_result {
 extern enum test_nonce2_result _test_nonce2(struct work *, uint32_t nonce, bool checktarget);
 extern enum test_nonce2_result _test_nonce2(struct work *, uint32_t nonce, bool checktarget);
 #define test_nonce(work, nonce, checktarget)  (_test_nonce2(work, nonce, checktarget) == TNR_GOOD)
 #define test_nonce(work, nonce, checktarget)  (_test_nonce2(work, nonce, checktarget) == TNR_GOOD)
 #define test_nonce2(work, nonce)  (_test_nonce2(work, nonce, true))
 #define test_nonce2(work, nonce)  (_test_nonce2(work, nonce, true))
-bool submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
+extern void submit_nonce(struct thr_info *thr, struct work *work, uint32_t nonce);
 extern void tailsprintf(char *f, const char *fmt, ...);
 extern void tailsprintf(char *f, const char *fmt, ...);
 extern void wlogprint(const char *f, ...);
 extern void wlogprint(const char *f, ...);
 extern int curses_int(const char *query);
 extern int curses_int(const char *query);

+ 2 - 2
sha256_4way.c

@@ -47,11 +47,11 @@ static inline __m128i Maj(const __m128i b, const __m128i c, const __m128i d) {
     return _mm_xor_si128(_mm_xor_si128(_mm_and_si128(b,c),_mm_and_si128(b,d)),_mm_and_si128(c,d));
     return _mm_xor_si128(_mm_xor_si128(_mm_and_si128(b,c),_mm_and_si128(b,d)),_mm_and_si128(c,d));
 }
 }
 
 
-static __attribute__((always_inline)) __m128i  ROTR(__m128i x, const int n) {
+static inline __m128i  ROTR(__m128i x, const int n) {
     return _mm_or_si128(_mm_srli_epi32(x, n),_mm_slli_epi32(x, 32 - n));
     return _mm_or_si128(_mm_srli_epi32(x, n),_mm_slli_epi32(x, 32 - n));
 }
 }
 
 
-static  __attribute__((always_inline))  __m128i SHR(__m128i x, const int n) {
+static inline __m128i SHR(__m128i x, const int n) {
     return _mm_srli_epi32(x, n);
     return _mm_srli_epi32(x, n);
 }
 }
 
 

+ 2 - 2
sha256_via.c

@@ -19,9 +19,9 @@ static void via_sha256(void *hash, void *buf, unsigned len)
 		     :"memory");
 		     :"memory");
 }
 }
 
 
-bool scanhash_via(struct thr_info*thr, __maybe_unused const unsigned char *pmidstate,
+bool scanhash_via(struct thr_info*thr, const unsigned char __maybe_unused *pmidstate,
 	unsigned char *data_inout,
 	unsigned char *data_inout,
-	__maybe_unused unsigned char *phash1, __maybe_unused unsigned char *phash,
+	unsigned char __maybe_unused *phash1, unsigned char __maybe_unused *phash,
 	const unsigned char *target,
 	const unsigned char *target,
 		  uint32_t max_nonce, uint32_t *last_nonce,
 		  uint32_t max_nonce, uint32_t *last_nonce,
 		  uint32_t n)
 		  uint32_t n)

+ 1 - 0
util.c

@@ -1324,6 +1324,7 @@ bool auth_stratum(struct pool *pool)
 	}
 	}
 	ret = true;
 	ret = true;
 	applog(LOG_INFO, "Stratum authorisation success for pool %d", pool->pool_no);
 	applog(LOG_INFO, "Stratum authorisation success for pool %d", pool->pool_no);
+	pool->probed = true;
 out:
 out:
 	if (val)
 	if (val)
 		json_decref(val);
 		json_decref(val);

+ 1 - 2
windows-build.txt

@@ -193,8 +193,7 @@ Another way is to type "cd libusb" and then press the tab key; It will auto fill
 Type the lines below one at a time. Look for problems after each one before going on
 Type the lines below one at a time. Look for problems after each one before going on
 to the next.
 to the next.
 
 
-./autogen.sh --disable-debug-log
-./configure --prefix=/MinGW
+./autogen.sh --disable-debug-log --prefix=/MinGW
 make
 make
 make install
 make install