Browse Source

Merge branch 'knc' into bfgminer

Conflicts:
	miner.c
Luke Dashjr 12 years ago
parent
commit
9ba89af0aa
11 changed files with 761 additions and 43 deletions
  1. 4 0
      Makefile.am
  2. 25 0
      configure.ac
  3. 53 33
      deviceapi.c
  4. 7 0
      deviceapi.h
  5. 3 1
      driver-bitforce.c
  6. 2 2
      driver-bitfury.c
  7. 634 0
      driver-knc.c
  8. 22 2
      miner.c
  9. 2 1
      miner.h
  10. 2 3
      spidevc.c
  11. 7 1
      spidevc.h

+ 4 - 0
Makefile.am

@@ -213,6 +213,10 @@ if HAS_AVALON
 bfgminer_SOURCES += driver-avalon.c driver-avalon.h hexdump.c
 bfgminer_SOURCES += driver-avalon.c driver-avalon.h hexdump.c
 endif
 endif
 
 
+if USE_KNC
+bfgminer_SOURCES += driver-knc.c
+endif
+
 if HAS_MODMINER
 if HAS_MODMINER
 bfgminer_SOURCES += driver-modminer.c
 bfgminer_SOURCES += driver-modminer.c
 endif
 endif

+ 25 - 0
configure.ac

@@ -382,6 +382,31 @@ if test "x$avalon" = xyes; then
 fi
 fi
 AM_CONDITIONAL([HAS_AVALON], [test x$avalon = xyes])
 AM_CONDITIONAL([HAS_AVALON], [test x$avalon = xyes])
 
 
+driverlist="$driverlist knc"
+AC_ARG_ENABLE([knc],
+	[AC_HELP_STRING([--enable-knc],[Compile support for KnC (default disabled)])],
+	[knc=$enableval],
+	[knc=no]
+	)
+if test "x$knc" = xyes; then
+	AC_CHECK_HEADERS([linux/i2c-dev-user.h])
+	AC_CHECK_DECL([i2c_smbus_read_word_data],[true],[
+		AC_MSG_ERROR([linux/i2c-dev.h header from i2c-tools (NOT linux headers) is required for knc driver])
+	],[
+		#include <stddef.h>
+		#ifdef HAVE_LINUX_I2C_DEV_USER_H
+		#include <linux/i2c-dev-user.h>
+		#else
+		#ifdef NEED_LINUX_I2C_H
+		#include <linux/i2c.h>
+		#endif
+		#include <linux/i2c-dev.h>
+		#endif
+	])
+	AC_DEFINE([USE_KNC], [1], [Defined to 1 if KnC support is wanted])
+fi
+AM_CONDITIONAL([USE_KNC], [test x$knc = xyes])
+
 httpsrv=auto
 httpsrv=auto
 AC_ARG_WITH([libmicrohttpd],
 AC_ARG_WITH([libmicrohttpd],
 	[AC_HELP_STRING([--without-libmicrohttpd],[Compile support for libmicrohttpd getwork server (default enabled)])],
 	[AC_HELP_STRING([--without-libmicrohttpd],[Compile support for libmicrohttpd getwork server (default enabled)])],

+ 53 - 33
deviceapi.c

@@ -168,6 +168,9 @@ void minerloop_scanhash(struct thr_info *mythr)
 	pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
 	pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
 #endif
 #endif
 	
 	
+	if (cgpu->deven != DEV_ENABLED)
+		mt_disable(mythr);
+	
 	while (likely(!cgpu->shutdown)) {
 	while (likely(!cgpu->shutdown)) {
 		mythr->work_restart = false;
 		mythr->work_restart = false;
 		request_work(mythr);
 		request_work(mythr);
@@ -284,13 +287,16 @@ void job_results_fetched(struct thr_info *mythr)
 	if (mythr->_proceed_with_new_job)
 	if (mythr->_proceed_with_new_job)
 		do_job_start(mythr);
 		do_job_start(mythr);
 	else
 	else
-	if (likely(mythr->prev_work))
 	{
 	{
-		struct timeval tv_now;
-		
-		timer_set_now(&tv_now);
-		
-		do_process_results(mythr, &tv_now, mythr->prev_work, true);
+		if (likely(mythr->prev_work))
+		{
+			struct timeval tv_now;
+			
+			timer_set_now(&tv_now);
+			
+			do_process_results(mythr, &tv_now, mythr->prev_work, true);
+		}
+		mt_disable_start(mythr);
 	}
 	}
 }
 }
 
 
@@ -406,24 +412,33 @@ void do_notifier_select(struct thr_info *thr, struct timeval *tvp_timeout)
 		notifier_read(thr->work_restart_notifier);
 		notifier_read(thr->work_restart_notifier);
 }
 }
 
 
-void minerloop_async(struct thr_info *mythr)
+static
+void _minerloop_setup(struct thr_info *mythr)
 {
 {
-	struct thr_info *thr = mythr;
-	struct cgpu_info *cgpu = mythr->cgpu;
-	struct device_drv *api = cgpu->drv;
-	struct timeval tv_now;
-	struct timeval tv_timeout;
-	struct cgpu_info *proc;
-	bool is_running, should_be_running;
+	struct cgpu_info * const cgpu = mythr->cgpu, *proc;
 	
 	
 	if (mythr->work_restart_notifier[1] == -1)
 	if (mythr->work_restart_notifier[1] == -1)
 		notifier_init(mythr->work_restart_notifier);
 		notifier_init(mythr->work_restart_notifier);
+	
 	for (proc = cgpu; proc; proc = proc->next_proc)
 	for (proc = cgpu; proc; proc = proc->next_proc)
 	{
 	{
 		mythr = proc->thr[0];
 		mythr = proc->thr[0];
 		timer_set_now(&mythr->tv_watchdog);
 		timer_set_now(&mythr->tv_watchdog);
 		proc->disable_watchdog = true;
 		proc->disable_watchdog = true;
 	}
 	}
+}
+
+void minerloop_async(struct thr_info *mythr)
+{
+	struct thr_info *thr = mythr;
+	struct cgpu_info *cgpu = mythr->cgpu;
+	struct device_drv *api = cgpu->drv;
+	struct timeval tv_now;
+	struct timeval tv_timeout;
+	struct cgpu_info *proc;
+	bool is_running, should_be_running;
+	
+	_minerloop_setup(mythr);
 	
 	
 	while (likely(!cgpu->shutdown)) {
 	while (likely(!cgpu->shutdown)) {
 		tv_timeout.tv_sec = -1;
 		tv_timeout.tv_sec = -1;
@@ -451,15 +466,20 @@ void minerloop_async(struct thr_info *mythr)
 			}
 			}
 			else  // ! should_be_running
 			else  // ! should_be_running
 			{
 			{
-				if (unlikely(is_running && !mythr->_job_transition_in_progress))
+				if (unlikely((is_running || !thr->_mt_disable_called) && !mythr->_job_transition_in_progress))
 				{
 				{
 disabled: ;
 disabled: ;
-					mythr->tv_morework.tv_sec = -1;
-					if (mythr->busy_state != TBS_GETTING_RESULTS)
-						do_get_results(mythr, false);
-					else
-						// Avoid starting job when pending result fetch completes
-						mythr->_proceed_with_new_job = false;
+					timer_unset(&mythr->tv_morework);
+					if (is_running)
+					{
+						if (mythr->busy_state != TBS_GETTING_RESULTS)
+							do_get_results(mythr, false);
+						else
+							// Avoid starting job when pending result fetch completes
+							mythr->_proceed_with_new_job = false;
+					}
+					else  // !thr->_mt_disable_called
+						mt_disable_start(mythr);
 				}
 				}
 			}
 			}
 			
 			
@@ -514,8 +534,7 @@ void minerloop_queue(struct thr_info *thr)
 	bool should_be_running;
 	bool should_be_running;
 	struct work *work;
 	struct work *work;
 	
 	
-	if (thr->work_restart_notifier[1] == -1)
-		notifier_init(thr->work_restart_notifier);
+	_minerloop_setup(thr);
 	
 	
 	while (likely(!cgpu->shutdown)) {
 	while (likely(!cgpu->shutdown)) {
 		tv_timeout.tv_sec = -1;
 		tv_timeout.tv_sec = -1;
@@ -528,11 +547,8 @@ void minerloop_queue(struct thr_info *thr)
 redo:
 redo:
 			if (should_be_running)
 			if (should_be_running)
 			{
 			{
-				if (unlikely(!mythr->_last_sbr_state))
-				{
+				if (unlikely(mythr->_mt_disable_called))
 					mt_disable_finish(mythr);
 					mt_disable_finish(mythr);
-					mythr->_last_sbr_state = should_be_running;
-				}
 				
 				
 				if (unlikely(mythr->work_restart))
 				if (unlikely(mythr->work_restart))
 				{
 				{
@@ -560,20 +576,27 @@ redo:
 				}
 				}
 			}
 			}
 			else
 			else
-			if (unlikely(mythr->_last_sbr_state))
+			if (unlikely(!mythr->_mt_disable_called))
 			{
 			{
-				mythr->_last_sbr_state = should_be_running;
 				do_queue_flush(mythr);
 				do_queue_flush(mythr);
+				mt_disable_start(mythr);
 			}
 			}
 			
 			
 			if (timer_passed(&mythr->tv_poll, &tv_now))
 			if (timer_passed(&mythr->tv_poll, &tv_now))
 				api->poll(mythr);
 				api->poll(mythr);
 			
 			
+			if (timer_passed(&mythr->tv_watchdog, &tv_now))
+			{
+				timer_set_delay(&mythr->tv_watchdog, &tv_now, WATCHDOG_INTERVAL * 1000000);
+				bfg_watchdog(proc, &tv_now);
+			}
+			
 			should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
 			should_be_running = (proc->deven == DEV_ENABLED && !mythr->pause);
 			if (should_be_running && !mythr->queue_full)
 			if (should_be_running && !mythr->queue_full)
 				goto redo;
 				goto redo;
 			
 			
 			reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
 			reduce_timeout_to(&tv_timeout, &mythr->tv_poll);
+			reduce_timeout_to(&tv_timeout, &mythr->tv_watchdog);
 		}
 		}
 		
 		
 		do_notifier_select(thr, &tv_timeout);
 		do_notifier_select(thr, &tv_timeout);
@@ -600,13 +623,10 @@ void *miner_thread(void *userdata)
 		goto out;
 		goto out;
 	}
 	}
 
 
-	if (cgpu->deven != DEV_ENABLED)
-		mt_disable_start(mythr);
-	
 	thread_reportout(mythr);
 	thread_reportout(mythr);
 	applog(LOG_DEBUG, "Popping ping in miner thread");
 	applog(LOG_DEBUG, "Popping ping in miner thread");
 	notifier_read(mythr->notifier);  // Wait for a notification to start
 	notifier_read(mythr->notifier);  // Wait for a notification to start
-
+	
 	cgtime(&cgpu->cgminer_stats.start_tv);
 	cgtime(&cgpu->cgminer_stats.start_tv);
 	if (drv->minerloop)
 	if (drv->minerloop)
 		drv->minerloop(mythr);
 		drv->minerloop(mythr);

+ 7 - 0
deviceapi.h

@@ -39,6 +39,12 @@ extern bool add_cgpu_slave(struct cgpu_info *, struct cgpu_info *master);
 typedef bool(*detectone_func_t)(const char*);
 typedef bool(*detectone_func_t)(const char*);
 typedef int(*autoscan_func_t)();
 typedef int(*autoscan_func_t)();
 
 
+enum generic_detect_flags {
+	GDF_FORCE_AUTO = 1,
+	GDF_REQUIRE_DNAME = 2,
+	GDF_DEFAULT_NOAUTO = 4,
+};
+
 extern int _serial_detect(struct device_drv *api, detectone_func_t, autoscan_func_t, int flags);
 extern int _serial_detect(struct device_drv *api, detectone_func_t, autoscan_func_t, int flags);
 #define serial_detect_fauto(api, detectone, autoscan)  \
 #define serial_detect_fauto(api, detectone, autoscan)  \
 	_serial_detect(api, detectone, autoscan, 1)
 	_serial_detect(api, detectone, autoscan, 1)
@@ -54,6 +60,7 @@ extern int _serial_detect(struct device_drv *api, detectone_func_t, autoscan_fun
 	_serial_detect(api, NULL     , autoscan, 0)
 	_serial_detect(api, NULL     , autoscan, 0)
 #define noserial_detect_manual(api, autoscan)  \
 #define noserial_detect_manual(api, autoscan)  \
 	_serial_detect(api, NULL     , autoscan, 4)
 	_serial_detect(api, NULL     , autoscan, 4)
+#define generic_detect(drv, detectone, autoscan, flags)  _serial_detect(drv, detectone, autoscan, flags)
 
 
 extern FILE *open_bitstream(const char *dname, const char *filename);
 extern FILE *open_bitstream(const char *dname, const char *filename);
 
 

+ 3 - 1
driver-bitforce.c

@@ -2055,7 +2055,9 @@ static void bitforce_queue_thread_deven(struct thr_info *thr)
 	struct bitforce_data *data = bitforce->device_data;
 	struct bitforce_data *data = bitforce->device_data;
 	struct thr_info *thisthr;
 	struct thr_info *thisthr;
 	
 	
-	for (thisbf = bitforce; thisbf && thisbf->device_data == data; thisbf = thisbf->next_proc)
+	for (thisbf = bitforce->device; thisbf && thisbf->device_data != data; thisbf = thisbf->next_proc)
+	{}
+	for ( ; thisbf && thisbf->device_data == data; thisbf = thisbf->next_proc)
 	{
 	{
 		thisthr = bitforce->thr[0];
 		thisthr = bitforce->thr[0];
 		
 		

+ 2 - 2
driver-bitfury.c

@@ -684,7 +684,7 @@ struct api_data *bitfury_api_device_status(struct cgpu_info * const cgpu)
 }
 }
 
 
 static
 static
-bool _bitfury_set_device_parse_setting(int * const rv, char * const setting, char * const replybuf, const int maxval)
+bool _bitfury_set_device_parse_setting(uint32_t * const rv, char * const setting, char * const replybuf, const int maxval)
 {
 {
 	char *p;
 	char *p;
 	long int nv;
 	long int nv;
@@ -707,7 +707,7 @@ bool _bitfury_set_device_parse_setting(int * const rv, char * const setting, cha
 char *bitfury_set_device(struct cgpu_info * const proc, char * const option, char * const setting, char * const replybuf)
 char *bitfury_set_device(struct cgpu_info * const proc, char * const option, char * const setting, char * const replybuf)
 {
 {
 	struct bitfury_device * const bitfury = proc->device_data;
 	struct bitfury_device * const bitfury = proc->device_data;
-	int newval;
+	uint32_t newval;
 	
 	
 	if (!strcasecmp(option, "help"))
 	if (!strcasecmp(option, "help"))
 	{
 	{

+ 634 - 0
driver-knc.c

@@ -0,0 +1,634 @@
+/*
+ * Copyright 2013 Luke Dashjr
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your option)
+ * any later version.  See COPYING for more details.
+ */
+
+#include "config.h"
+
+#include <stdbool.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <sys/ioctl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#ifdef HAVE_LINUX_I2C_DEV_USER_H
+#include <linux/i2c-dev-user.h>
+#else
+#include <linux/i2c-dev.h>
+#endif
+#include <linux/spi/spidev.h>
+
+#include <uthash.h>
+
+#include "deviceapi.h"
+#include "logging.h"
+#include "miner.h"
+#include "spidevc.h"
+
+#define KNC_POLL_INTERVAL_US 10000
+#define KNC_SPI_SPEED 3000000
+#define KNC_SPI_DELAY 0
+#define KNC_SPI_MODE  (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH)
+#define KNC_SPI_BITS  8
+
+static const char * const i2cpath = "/dev/i2c-2";
+
+#define KNC_I2C_TEMPLATE "/dev/i2c-%d"
+
+enum knc_request_cmd {
+	KNC_REQ_SUBMIT_WORK = 2,
+	KNC_REQ_FLUSH_QUEUE = 3,
+};
+
+enum knc_reply_type {
+	KNC_REPLY_NONCE_FOUND = 1,
+	KNC_REPLY_WORK_DONE   = 2,
+};
+
+struct device_drv knc_drv;
+
+struct knc_device {
+	int i2c;
+	struct spi_port *spi;
+	struct cgpu_info *cgpu;
+	
+	bool need_flush;
+	struct work *workqueue;
+	int workqueue_size;
+	int workqueue_max;
+	int next_id;
+	
+	struct work *devicework;
+};
+
+struct knc_core {
+	int asicno;
+	int coreno;
+};
+
+static
+bool knc_detect_one(const char *devpath)
+{
+	static struct cgpu_info *prev_cgpu = NULL;
+	struct cgpu_info *cgpu;
+	int i;
+	const int fd = open(i2cpath, O_RDWR);
+	char *leftover = NULL;
+	const int i2cslave = strtol(devpath, &leftover, 0);
+	uint8_t buf[0x20];
+	
+	if (leftover && leftover[0])
+		return false;
+	
+	if (unlikely(fd == -1))
+	{
+		applog(LOG_DEBUG, "%s: Failed to open %s", __func__, i2cpath);
+		return false;
+	}
+	
+	if (ioctl(fd, I2C_SLAVE, i2cslave))
+	{
+		close(fd);
+		applog(LOG_DEBUG, "%s: Failed to select i2c slave 0x%x",
+		       __func__, i2cslave);
+		return false;
+	}
+	
+	i = i2c_smbus_read_i2c_block_data(fd, 0, 0x20, buf);
+	close(fd);
+	if (-1 == i)
+	{
+		applog(LOG_DEBUG, "%s: 0x%x: Failed to read i2c block data",
+		       __func__, i2cslave);
+		return false;
+	}
+	for (i = 0; ; ++i)
+	{
+		if (buf[i] == 3)
+			break;
+		if (i == 0x1f)
+			return false;
+	}
+	
+	cgpu = malloc(sizeof(*cgpu));
+	*cgpu = (struct cgpu_info){
+		.drv = &knc_drv,
+		.device_path = strdup(devpath),
+		.deven = DEV_ENABLED,
+		.procs = 192,
+		.threads = prev_cgpu ? 0 : 1,
+	};
+	const bool rv = add_cgpu_slave(cgpu, prev_cgpu);
+	prev_cgpu = cgpu;
+	return rv;
+}
+
+static int knc_detect_auto(void)
+{
+	const int first = 0x20, last = 0x26;
+	char devpath[4];
+	int found = 0, i;
+	
+	for (i = first; i <= last; ++i)
+	{
+		sprintf(devpath, "%d", i);
+		if (knc_detect_one(devpath))
+			++found;
+	}
+	
+	return found;
+}
+
+static void knc_detect(void)
+{
+	generic_detect(&knc_drv, knc_detect_one, knc_detect_auto, GDF_REQUIRE_DNAME | GDF_DEFAULT_NOAUTO);
+}
+
+
+static
+bool knc_spi_open(const char *repr, struct spi_port * const spi)
+{
+	const char * const spipath = "/dev/spidev1.0";
+	const int fd = open(spipath, O_RDWR);
+	const uint8_t lsbfirst = 0;
+	if (fd == -1)
+		return false;
+	if (ioctl(fd, SPI_IOC_WR_MODE         , &spi->mode )) goto fail;
+	if (ioctl(fd, SPI_IOC_WR_LSB_FIRST    , &lsbfirst  )) goto fail;
+	if (ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &spi->bits )) goto fail;
+	if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ , &spi->speed)) goto fail;
+	spi->fd = fd;
+	return true;
+
+fail:
+	close(fd);
+	spi->fd = -1;
+	applog(LOG_WARNING, "%s: Failed to open %s", repr, spipath);
+	return false;
+}
+
+static
+bool knc_spi_txrx(struct spi_port * const spi)
+{
+	const void * const wrbuf = spi_gettxbuf(spi);
+	void * const rdbuf = spi_getrxbuf(spi);
+	const size_t bufsz = spi_getbufsz(spi);
+	const int fd = spi->fd;
+	struct spi_ioc_transfer xf = {
+		.tx_buf = (uintptr_t) wrbuf,
+		.rx_buf = (uintptr_t) rdbuf,
+		.len = bufsz,
+		.delay_usecs = spi->delay,
+		.speed_hz = spi->speed,
+		.bits_per_word = spi->bits,
+	};
+	return (ioctl(fd, SPI_IOC_MESSAGE(1), &xf) > 0);
+}
+
+static
+void knc_clean_flush(struct spi_port * const spi)
+{
+	const uint8_t flushcmd = KNC_REQ_FLUSH_QUEUE << 4;
+	const size_t spi_req_sz = 0x1000;
+	
+	spi_clear_buf(spi);
+	spi_emit_buf(spi, &flushcmd, 1);
+	spi_emit_nop(spi, spi_req_sz - spi_getbufsz(spi));
+	applog(LOG_DEBUG, "%s: Issuing flush command to clear out device queues", knc_drv.dname);
+	spi_txrx(spi);
+}
+
+static
+bool knc_init(struct thr_info * const thr)
+{
+	const int max_cores = 192;
+	struct thr_info *mythr;
+	struct cgpu_info * const cgpu = thr->cgpu, *proc;
+	struct knc_device *knc;
+	struct knc_core *knccore;
+	struct spi_port *spi;
+	const int i2c = open(i2cpath, O_RDWR);
+	int i2cslave, i, j;
+	uint8_t buf[0x20];
+	
+	if (unlikely(i2c == -1))
+	{
+		applog(LOG_DEBUG, "%s: Failed to open %s", __func__, i2cpath);
+		return false;
+	}
+	
+	knc = malloc(sizeof(*knc));
+	
+	for (proc = cgpu; proc; )
+	{
+		if (proc->device != proc)
+		{
+			applog(LOG_WARNING, "%"PRIpreprv": Extra processor?", proc->proc_repr);
+			continue;
+		}
+		
+		i2cslave = atoi(proc->device_path);
+		
+		if (ioctl(i2c, I2C_SLAVE, i2cslave))
+		{
+			applog(LOG_DEBUG, "%s: Failed to select i2c slave 0x%x",
+			       __func__, i2cslave);
+			return false;
+		}
+		
+		for (i = 0; i < max_cores; i += 0x20)
+		{
+			i2c_smbus_read_i2c_block_data(i2c, i, 0x20, buf);
+			for (j = 0; j < 0x20; ++j)
+			{
+				mythr = proc->thr[0];
+				mythr->cgpu_data = knccore = malloc(sizeof(*knccore));
+				*knccore = (struct knc_core){
+					.asicno = i2cslave - 0x20,
+					.coreno = i + j,
+				};
+				if (proc != cgpu)
+					mythr->queue_full = true;
+				proc->device_data = knc;
+				if (buf[j] != 3)
+					proc->deven = DEV_DISABLED;
+				
+				proc = proc->next_proc;
+				if ((!proc) || proc->device == proc)
+					goto nomorecores;
+			}
+		}
+nomorecores: ;
+	}
+	
+	spi = malloc(sizeof(*spi));
+	*knc = (struct knc_device){
+		.i2c = i2c,
+		.spi = spi,
+		.cgpu = cgpu,
+		.workqueue_max = 1,
+	};
+	*spi = (struct spi_port){
+		.txrx = knc_spi_txrx,
+		.cgpu = cgpu,
+		.repr = knc_drv.dname,
+		.logprio = LOG_ERR,
+		.speed = KNC_SPI_SPEED,
+		.delay = KNC_SPI_DELAY,
+		.mode  = KNC_SPI_MODE,
+		.bits  = KNC_SPI_BITS,
+	};
+	
+	if (!knc_spi_open(cgpu->dev_repr, spi))
+		return false;
+	
+	knc_clean_flush(spi);
+	
+	timer_set_now(&thr->tv_poll);
+	
+	return true;
+}
+
+static
+void knc_remove_local_queue(struct knc_device * const knc, struct work * const work)
+{
+	DL_DELETE(knc->workqueue, work);
+	free_work(work);
+	--knc->workqueue_size;
+}
+
+static
+void knc_prune_local_queue(struct thr_info *thr)
+{
+	struct cgpu_info * const cgpu = thr->cgpu;
+	struct knc_device * const knc = cgpu->device_data;
+	struct work *work, *tmp;
+	
+	DL_FOREACH_SAFE(knc->workqueue, work, tmp)
+	{
+		if (stale_work(work, false))
+			knc_remove_local_queue(knc, work);
+	}
+	thr->queue_full = (knc->workqueue_size >= knc->workqueue_max);
+}
+
+static
+bool knc_queue_append(struct thr_info * const thr, struct work * const work)
+{
+	struct cgpu_info * const cgpu = thr->cgpu;
+	struct knc_device * const knc = cgpu->device_data;
+	
+	if (knc->workqueue_size >= knc->workqueue_max)
+	{
+		knc_prune_local_queue(thr);
+		if (thr->queue_full)
+			return false;
+	}
+	
+	DL_APPEND(knc->workqueue, work);
+	++knc->workqueue_size;
+	
+	thr->queue_full = (knc->workqueue_size >= knc->workqueue_max);
+	if (thr->queue_full)
+		knc_prune_local_queue(thr);
+	
+	return true;
+}
+
+#define HASH_LAST_ADDED(head, out)  \
+	(out = (head) ? (ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail)) : NULL)
+
+static
+void knc_queue_flush(struct thr_info * const thr)
+{
+	struct cgpu_info * const cgpu = thr->cgpu;
+	struct knc_device * const knc = cgpu->device_data;
+	struct work *work, *tmp;
+	
+	if (knc->cgpu != cgpu)
+		return;
+	
+	DL_FOREACH_SAFE(knc->workqueue, work, tmp)
+	{
+		knc_remove_local_queue(knc, work);
+	}
+	thr->queue_full = false;
+	
+	HASH_LAST_ADDED(knc->devicework, work);
+	if (work && stale_work(work, true))
+	{
+		knc->need_flush = true;
+		timer_set_now(&thr->tv_poll);
+	}
+}
+
+static inline
+uint16_t get_u16be(const void * const p)
+{
+	const uint8_t * const b = p;
+	return (((uint16_t)b[0]) << 8) | b[1];
+}
+
+static inline
+uint32_t get_u32be(const void * const p)
+{
+	const uint8_t * const b = p;
+	return (((uint32_t)b[0]) << 0x18)
+	     | (((uint32_t)b[1]) << 0x10)
+	     | (((uint32_t)b[2]) << 8)
+	     |             b[3];
+}
+
+static
+void knc_poll(struct thr_info * const thr)
+{
+	struct thr_info *mythr;
+	struct cgpu_info * const cgpu = thr->cgpu, *proc;
+	struct knc_device * const knc = cgpu->device_data;
+	struct spi_port * const spi = knc->spi;
+	struct knc_core *knccore;
+	struct work *work, *tmp;
+	uint8_t buf[0x30], *rxbuf;
+	int works_sent = 0, asicno, i;
+	uint16_t workaccept;
+	int workid = knc->next_id;
+	uint32_t nonce, coreno;
+	size_t spi_req_sz = 0x1000;
+	unsigned long delay_usecs = KNC_POLL_INTERVAL_US;
+	
+	knc_prune_local_queue(thr);
+	
+	spi_clear_buf(spi);
+	if (knc->need_flush)
+	{
+		applog(LOG_NOTICE, "%s: Abandoning stale searches to restart", knc_drv.dname);
+		buf[0] = KNC_REQ_FLUSH_QUEUE << 4;
+		spi_emit_buf(spi, buf, sizeof(buf));
+	}
+	DL_FOREACH(knc->workqueue, work)
+	{
+		buf[0] = KNC_REQ_SUBMIT_WORK << 4;
+		buf[1] = 0;
+		buf[2] = (workid >> 8) & 0x7f;
+		buf[3] = workid & 0xff;
+		
+		for (i = 0; i < 0x20; ++i)
+			buf[4 + i] = work->midstate[0x1f - i];
+		for (i = 0; i < 0xc; ++i)
+			buf[0x24 + i] = work->data[0x4b - i];
+		
+		spi_emit_buf(spi, buf, sizeof(buf));
+		
+		++works_sent;
+		++workid;
+	}
+	spi_emit_nop(spi, spi_req_sz - spi_getbufsz(spi));
+	spi_txrx(spi);
+	
+	rxbuf = spi_getrxbuf(spi);
+	if (rxbuf[3] & 1)
+		applog(LOG_DEBUG, "%s: Receive buffer overflow reported", knc_drv.dname);
+	workaccept = get_u16be(&rxbuf[6]);
+	applog(LOG_DEBUG, "%s: %lu/%d jobs accepted to queue (max=%d)",
+	       knc_drv.dname, (unsigned long)workaccept, works_sent, knc->workqueue_max);
+	
+	while (true)
+	{
+		rxbuf += 0xc;
+		spi_req_sz -= 0xc;
+		if (spi_req_sz < 0xc)
+			break;
+		
+		const int rtype = rxbuf[0] >> 6;
+		if (rtype && opt_debug)
+		{
+			char x[(0xc * 2) + 1];
+			bin2hex(x, rxbuf, 0xc);
+			applog(LOG_DEBUG, "%s: RECV: %s", knc_drv.dname, x);
+		}
+		if (rtype != KNC_REPLY_NONCE_FOUND && rtype != KNC_REPLY_WORK_DONE)
+			continue;
+		
+		asicno = (rxbuf[0] & 0x38) >> 3;
+		coreno = get_u32be(&rxbuf[8]);
+		proc = cgpu;
+		while (true)
+		{
+			knccore = proc->thr[0]->cgpu_data;
+			if (knccore->asicno == asicno)
+				break;
+			do {
+				proc = proc->next_proc;
+			} while(proc != proc->device);
+		}
+		for (i = 0; i < coreno; ++i)
+			proc = proc->next_proc;
+		mythr = proc->thr[0];
+		
+		i = get_u16be(&rxbuf[2]);
+		HASH_FIND_INT(knc->devicework, &i, work);
+		if (!work)
+		{
+			const char * const msgtype = (rtype == KNC_REPLY_NONCE_FOUND) ? "nonce found" : "work done";
+			applog(LOG_WARNING, "%"PRIpreprv": Got %s message about unknown work 0x%04x",
+			       proc->proc_repr, msgtype, i);
+			if (KNC_REPLY_NONCE_FOUND == rtype)
+			{
+				nonce = get_u32be(&rxbuf[4]);
+				nonce = le32toh(nonce);
+				inc_hw_errors2(mythr, NULL, &nonce);
+			}
+			else
+				inc_hw_errors2(mythr, NULL, NULL);
+			continue;
+		}
+		
+		switch (rtype)
+		{
+			case KNC_REPLY_NONCE_FOUND:
+				nonce = get_u32be(&rxbuf[4]);
+				nonce = le32toh(nonce);
+				submit_nonce(mythr, work, nonce);
+				break;
+			case KNC_REPLY_WORK_DONE:
+				HASH_DEL(knc->devicework, work);
+				free_work(work);
+				hashes_done2(mythr, 0x100000000, NULL);
+				break;
+		}
+	}
+	
+	if (knc->need_flush)
+	{
+		knc->need_flush = false;
+		HASH_ITER(hh, knc->devicework, work, tmp)
+		{
+			HASH_DEL(knc->devicework, work);
+			free_work(work);
+		}
+		delay_usecs = 0;
+	}
+	
+	if (workaccept)
+	{
+		if (workaccept >= knc->workqueue_max)
+		{
+			knc->workqueue_max = workaccept;
+			delay_usecs = 0;
+		}
+		DL_FOREACH_SAFE(knc->workqueue, work, tmp)
+		{
+			--knc->workqueue_size;
+			DL_DELETE(knc->workqueue, work);
+			work->device_id = knc->next_id++ & 0x7fff;
+			HASH_ADD_INT(knc->devicework, device_id, work);
+			if (!--workaccept)
+				break;
+		}
+		thr->queue_full = (knc->workqueue_size >= knc->workqueue_max);
+	}
+	
+	timer_set_delay_from_now(&thr->tv_poll, delay_usecs);
+}
+
+static
+bool _knc_core_setstatus(struct thr_info * const thr, uint8_t val)
+{
+	struct cgpu_info * const proc = thr->cgpu;
+	struct knc_device * const knc = proc->device_data;
+	struct knc_core * const knccore = thr->cgpu_data;
+	const int i2c = knc->i2c;
+	const int i2cslave = 0x20 + knccore->asicno;
+	
+	if (ioctl(i2c, I2C_SLAVE, i2cslave))
+	{
+		applog(LOG_DEBUG, "%"PRIpreprv": %s: Failed to select i2c slave 0x%x",
+		       proc->proc_repr, __func__, i2cslave);
+		return false;
+	}
+	
+	return (-1 != i2c_smbus_write_byte_data(i2c, knccore->coreno, val));
+}
+
+static
+void knc_core_disable(struct thr_info * const thr)
+{
+	_knc_core_setstatus(thr, 0);
+}
+
+static
+void knc_core_enable(struct thr_info * const thr)
+{
+	_knc_core_setstatus(thr, 1);
+}
+
+static
+bool knc_get_stats(struct cgpu_info * const cgpu)
+{
+	if (cgpu->device != cgpu)
+		return true;
+	
+	struct knc_core * const knccore = cgpu->thr[0]->cgpu_data;
+	struct cgpu_info *proc;
+	const int i2cdev = knccore->asicno + 3;
+	const int i2cslave = 0x48;
+	int i2c;
+	int32_t rawtemp;
+	float temp;
+	bool rv = false;
+	
+	char i2cpath[sizeof(KNC_I2C_TEMPLATE)];
+	sprintf(i2cpath, KNC_I2C_TEMPLATE, i2cdev);
+	i2c = open(i2cpath, O_RDWR);
+	if (i2c == -1)
+	{
+		applog(LOG_DEBUG, "%s: %s: Failed to open %s",
+		       cgpu->dev_repr, __func__, i2cpath);
+		return false;
+	}
+	
+	if (ioctl(i2c, I2C_SLAVE, i2cslave))
+	{
+		applog(LOG_DEBUG, "%s: %s: Failed to select i2c slave 0x%x",
+		       cgpu->dev_repr, __func__, i2cslave);
+		goto out;
+	}
+	
+	rawtemp = i2c_smbus_read_word_data(i2c, 0);
+	if (rawtemp == -1)
+		goto out;
+	temp = ((float)(rawtemp & 0xff));
+	if (rawtemp & 0x100)
+		temp += 0.5;
+	
+	for (proc = cgpu; proc && proc->device == cgpu; proc = proc->next_proc)
+		proc->temp = temp;
+	
+	rv = true;
+out:
+	close(i2c);
+	return rv;
+}
+
+struct device_drv knc_drv = {
+	.dname = "knc",
+	.name = "KNC",
+	.drv_detect = knc_detect,
+	
+	.thread_init = knc_init,
+	.thread_disable = knc_core_disable,
+	.thread_enable  = knc_core_enable,
+	
+	.minerloop = minerloop_queue,
+	.queue_append = knc_queue_append,
+	.queue_flush = knc_queue_flush,
+	.poll = knc_poll,
+	
+	.get_stats = knc_get_stats,
+};

+ 22 - 2
miner.c

@@ -8580,6 +8580,7 @@ void __thr_being_msg(int prio, struct thr_info *thr, const char *being)
 		applog(prio, "%"PRIpreprv" %s", proc->proc_repr, being);
 		applog(prio, "%"PRIpreprv" %s", proc->proc_repr, being);
 }
 }
 
 
+// Called by asynchronous minerloops, when they find their processor should be disabled
 void mt_disable_start(struct thr_info *mythr)
 void mt_disable_start(struct thr_info *mythr)
 {
 {
 	struct cgpu_info *cgpu = mythr->cgpu;
 	struct cgpu_info *cgpu = mythr->cgpu;
@@ -8597,6 +8598,7 @@ void mt_disable_start(struct thr_info *mythr)
 	__thr_being_msg(LOG_WARNING, mythr, "being disabled");
 	__thr_being_msg(LOG_WARNING, mythr, "being disabled");
 	mythr->rolling = mythr->cgpu->rolling = 0;
 	mythr->rolling = mythr->cgpu->rolling = 0;
 	thread_reportout(mythr);
 	thread_reportout(mythr);
+	mythr->_mt_disable_called = true;
 }
 }
 
 
 /* Create a hashtable of work items for devices with a queue. The device
 /* Create a hashtable of work items for devices with a queue. The device
@@ -8766,6 +8768,9 @@ void hash_queued_work(struct thr_info *mythr)
 	const int thr_id = mythr->id;
 	const int thr_id = mythr->id;
 	int64_t hashes_done = 0;
 	int64_t hashes_done = 0;
 
 
+	if (unlikely(cgpu->deven != DEV_ENABLED))
+		mt_disable(mythr);
+	
 	while (likely(!cgpu->shutdown)) {
 	while (likely(!cgpu->shutdown)) {
 		struct timeval diff;
 		struct timeval diff;
 		int64_t hashes;
 		int64_t hashes;
@@ -8804,6 +8809,7 @@ void hash_queued_work(struct thr_info *mythr)
 	// cgpu->deven = DEV_DISABLED; set in miner_thread
 	// cgpu->deven = DEV_DISABLED; set in miner_thread
 }
 }
 
 
+// Called by minerloop, when it is re-enabling a processor
 void mt_disable_finish(struct thr_info *mythr)
 void mt_disable_finish(struct thr_info *mythr)
 {
 {
 	struct device_drv *drv = mythr->cgpu->drv;
 	struct device_drv *drv = mythr->cgpu->drv;
@@ -8812,15 +8818,19 @@ void mt_disable_finish(struct thr_info *mythr)
 	__thr_being_msg(LOG_WARNING, mythr, "being re-enabled");
 	__thr_being_msg(LOG_WARNING, mythr, "being re-enabled");
 	if (drv->thread_enable)
 	if (drv->thread_enable)
 		drv->thread_enable(mythr);
 		drv->thread_enable(mythr);
+	mythr->_mt_disable_called = false;
 }
 }
 
 
+// Called by synchronous minerloops, when they find their processor should be disabled
+// Calls mt_disable_start, waits until it's re-enabled, then calls mt_disable_finish
 void mt_disable(struct thr_info *mythr)
 void mt_disable(struct thr_info *mythr)
 {
 {
+	const struct cgpu_info * const cgpu = mythr->cgpu;
 	mt_disable_start(mythr);
 	mt_disable_start(mythr);
 	applog(LOG_DEBUG, "Waiting for wakeup notification in miner thread");
 	applog(LOG_DEBUG, "Waiting for wakeup notification in miner thread");
 	do {
 	do {
 		notifier_read(mythr->notifier);
 		notifier_read(mythr->notifier);
-	} while (mythr->pause);
+	} while (mythr->pause || cgpu->deven != DEV_ENABLED);
 	mt_disable_finish(mythr);
 	mt_disable_finish(mythr);
 }
 }
 
 
@@ -9883,6 +9893,10 @@ extern struct device_drv icarus_drv;
 extern struct device_drv avalon_drv;
 extern struct device_drv avalon_drv;
 #endif
 #endif
 
 
+#ifdef USE_KNC
+extern struct device_drv knc_drv;
+#endif
+
 #ifdef USE_LITTLEFURY
 #ifdef USE_LITTLEFURY
 extern struct device_drv littlefury_drv;
 extern struct device_drv littlefury_drv;
 #endif
 #endif
@@ -9998,6 +10012,11 @@ void drv_detect_all()
 		bigpic_drv.drv_detect();
 		bigpic_drv.drv_detect();
 #endif
 #endif
 
 
+#ifdef USE_KNC
+	if (!opt_scrypt)
+		knc_drv.drv_detect();
+#endif
+
 #ifdef USE_MODMINER
 #ifdef USE_MODMINER
 	if (!opt_scrypt)
 	if (!opt_scrypt)
 		modminer_drv.drv_detect();
 		modminer_drv.drv_detect();
@@ -10085,7 +10104,6 @@ void allocate_cgpu(struct cgpu_info *cgpu, unsigned int *kp)
 		thr->mutex_request[1] = INVSOCK;
 		thr->mutex_request[1] = INVSOCK;
 		thr->_job_transition_in_progress = true;
 		thr->_job_transition_in_progress = true;
 		timerclear(&thr->tv_morework);
 		timerclear(&thr->tv_morework);
-		thr->_last_sbr_state = true;
 
 
 		thr->scanhash_working = true;
 		thr->scanhash_working = true;
 		thr->hashes_done = 0;
 		thr->hashes_done = 0;
@@ -10128,6 +10146,8 @@ void start_cgpu(struct cgpu_info *cgpu)
 
 
 		if (unlikely(thr_info_create(thr, NULL, miner_thread, thr)))
 		if (unlikely(thr_info_create(thr, NULL, miner_thread, thr)))
 			quit(1, "thread %d create failed", thr->id);
 			quit(1, "thread %d create failed", thr->id);
+		
+		notifier_wake(thr->notifier);
 	}
 	}
 	if (cgpu->deven == DEV_ENABLED)
 	if (cgpu->deven == DEV_ENABLED)
 		proc_enable(cgpu);
 		proc_enable(cgpu);

+ 2 - 1
miner.h

@@ -627,6 +627,7 @@ struct thr_info {
 	struct work *work;
 	struct work *work;
 	struct work *next_work;
 	struct work *next_work;
 	enum thr_busy_state busy_state;
 	enum thr_busy_state busy_state;
+	bool _mt_disable_called;
 	struct timeval tv_morework;
 	struct timeval tv_morework;
 	struct work *results_work;
 	struct work *results_work;
 	bool _job_transition_in_progress;
 	bool _job_transition_in_progress;
@@ -643,7 +644,6 @@ struct thr_info {
 	// Used by minerloop_queue
 	// Used by minerloop_queue
 	struct work *work_list;
 	struct work *work_list;
 	bool queue_full;
 	bool queue_full;
-	bool _last_sbr_state;
 
 
 	bool	work_restart;
 	bool	work_restart;
 	notifier_t work_restart_notifier;
 	notifier_t work_restart_notifier;
@@ -1330,6 +1330,7 @@ struct work {
 
 
 	unsigned char	work_restart_id;
 	unsigned char	work_restart_id;
 	int		id;
 	int		id;
+	int		device_id;
 	UT_hash_handle hh;
 	UT_hash_handle hh;
 	
 	
 	double		work_difficulty;
 	double		work_difficulty;

+ 2 - 3
spidevc.c

@@ -218,8 +218,7 @@ void *spi_emit_buf_reverse(struct spi_port *port, const void *p, size_t sz)
 	return rv;
 	return rv;
 }
 }
 
 
-static
-void spi_emit_buf(struct spi_port *port, void *str, size_t sz)
+void spi_emit_buf(struct spi_port * const port, const void * const str, const size_t sz)
 {
 {
 	if (port->spibufsz + sz >= SPIMAXSZ)
 	if (port->spibufsz + sz >= SPIMAXSZ)
 		return;
 		return;
@@ -248,7 +247,7 @@ void spi_emit_fasync(struct spi_port *port, int n)
 
 
 void spi_emit_nop(struct spi_port *port, int n) {
 void spi_emit_nop(struct spi_port *port, int n) {
 	int i;
 	int i;
-	for (i = 0; i < n; n++) {
+	for (i = 0; i < n; ++i) {
 		spi_emit_buf(port, "\x0", 1);
 		spi_emit_buf(port, "\x0", 1);
 	}
 	}
 }
 }

+ 7 - 1
spidevc.h

@@ -20,7 +20,12 @@ struct spi_port {
 	struct cgpu_info *cgpu;
 	struct cgpu_info *cgpu;
 	const char *repr;
 	const char *repr;
 	int logprio;
 	int logprio;
-	int speed;
+	
+	int fd;
+	uint32_t speed;
+	uint16_t delay;
+	uint8_t mode;
+	uint8_t bits;
 };
 };
 
 
 extern struct spi_port *sys_spi;
 extern struct spi_port *sys_spi;
@@ -51,6 +56,7 @@ size_t spi_getbufsz(struct spi_port *port)
 	return port->spibufsz;
 	return port->spibufsz;
 }
 }
 
 
+extern void spi_emit_buf(struct spi_port *, const void *, size_t);
 
 
 extern void spi_emit_break(struct spi_port *port); /* BREAK CONNECTIONS AFTER RESET */
 extern void spi_emit_break(struct spi_port *port); /* BREAK CONNECTIONS AFTER RESET */
 extern void spi_emit_fsync(struct spi_port *port); /* FEED-THROUGH TO NEXT CHIP SYNCHRONOUSLY (WITH FLIP-FLOP) */
 extern void spi_emit_fsync(struct spi_port *port); /* FEED-THROUGH TO NEXT CHIP SYNCHRONOUSLY (WITH FLIP-FLOP) */