Commit db8c31c81948aa34542617ce6dd46d407917f853

Con Kolivas 2014-02-23T20:21:36

Make hfa hotplug inherit some parameters from a previous instance if the serial number exists and is matching, avoiding dropping the clock on all devices.

diff --git a/driver-hashfast.c b/driver-hashfast.c
index af655ef..33ad493 100644
--- a/driver-hashfast.c
+++ b/driver-hashfast.c
@@ -421,7 +421,6 @@ tryagain:
 	       db->inflight_target);
 	applog(LOG_INFO, "%s %d:      sequence_modulus: %d", hashfast->drv->name, hashfast->device_id,
 	       db->sequence_modulus);
-	info->num_sequence = db->sequence_modulus;
 
 	// Now a copy of the config data used
 	if (!hfa_get_data(hashfast, (char *)&info->config_data, U32SIZE(info->config_data))) {
@@ -453,6 +452,8 @@ tryagain:
 		       hashfast->drv->name, hashfast->device_id);
 		return false;
 	}
+	info->num_sequence = db->sequence_modulus;
+	info->serial_number = db->serial_number;
 	info->base_clock = db->hash_clockrate;
 
 	return true;
@@ -494,9 +495,29 @@ static bool hfa_send_shutdown(struct cgpu_info *hashfast)
 	return ret;
 }
 
+static void hfa_inherit_device(struct cgpu_info *hashfast, struct cgpu_info *cgpu)
+{
+	struct hashfast_info *info = hashfast->device_data, *cinfo = cgpu->device_data;
+	int newdevice_id;
+
+	applog(LOG_INFO, "Found matching zombie device for %s %d at device %d",
+	       hashfast->drv->name, hashfast->device_id, cgpu->device_id);
+	/* Make the new device instance inherit relevant data
+		* from the old instance. */
+	newdevice_id = hashfast->device_id;
+	hashfast->device_id = cgpu->device_id;
+	cgpu->device_id = newdevice_id;
+	info->resets = cinfo->resets;
+	if (info->hash_clock_rate != cinfo->hash_clock_rate) {
+		info->hash_clock_rate = cinfo->hash_clock_rate;
+		hfa_reset(hashfast, hashfast->device_data);
+	}
+}
+
 static bool hfa_detect_common(struct cgpu_info *hashfast)
 {
 	struct hashfast_info *info;
+	struct cgpu_info *cgpu;
 	bool ret;
 	int i;
 
@@ -537,6 +558,25 @@ static bool hfa_detect_common(struct cgpu_info *hashfast)
 	if (!info->works)
 		quit(1, "Failed to calloc info works in hfa_detect_common");
 
+	info->cgpu = hashfast;
+
+	/* If the device doesn't have a serial number, don't try to match it
+	 * with a zombie instance. */
+	if (!info->serial_number)
+		return true;
+
+	/* See if we can find a zombie instance of the same device */
+	list_for_each_cgpu(i, cgpu) {
+		struct hashfast_info *cinfo;
+
+		if (cgpu->drv->drv_id != DRIVER_hashfast)
+			continue;
+		if (!cgpu->usbinfo.nodev)
+			continue;
+		cinfo = cgpu->device_data;
+		if (info->serial_number == cinfo->serial_number)
+			hfa_inherit_device(hashfast, cgpu);
+	}
 	return true;
 }
 
@@ -1323,8 +1363,6 @@ static int64_t hfa_scanwork(struct thr_info *thr)
 			info->hash_clock_rate -= 10;
 			if (info->hash_clock_rate < HFA_CLOCK_DEFAULT)
 				info->hash_clock_rate = HFA_CLOCK_DEFAULT;
-			if (info->hash_clock_rate < opt_hfa_hash_clock)
-				opt_hfa_hash_clock = info->hash_clock_rate;
 			applog(LOG_WARNING, "%s %d: Decreasing clock speed to %d with reset",
 			       hashfast->drv->name, hashfast->device_id, info->hash_clock_rate);
 		}
diff --git a/driver-hashfast.h b/driver-hashfast.h
index cf8b28a..140e388 100644
--- a/driver-hashfast.h
+++ b/driver-hashfast.h
@@ -95,6 +95,7 @@ struct hf_die_data {
 };
 
 struct hashfast_info {
+	struct cgpu_info *cgpu;                     // Points back to parent structure
 	int asic_count;                             // # of chips in the chain
 	int core_count;                             // # of cores per chip
 	int device_type;                            // What sort of device this is
@@ -112,6 +113,7 @@ struct hashfast_info {
 	uint32_t *core_bitmap;                      // Core OK bitmap test results, run with PLL Bypassed
 	int group_ntime_roll;                       // Total ntime roll amount per group
 	int core_ntime_roll;                        // Total core ntime roll amount
+	uint32_t serial_number;                     // db->serial_number if it exists
 
 	pthread_mutex_t lock;
 	pthread_mutex_t rlock;
diff --git a/miner.h b/miner.h
index 0f70491..a4e843b 100644
--- a/miner.h
+++ b/miner.h
@@ -288,6 +288,10 @@ struct strategies {
 
 struct cgpu_info;
 
+#define list_for_each_cgpu(thri, cgpu0) \
+	for (thri = 0, cgpu0 = mining_thr[thri]->cgpu; thri < mining_threads; \
+		thri++, cgpu0 = mining_thr[thri]->cgpu)
+
 extern void blank_get_statline_before(char *buf, size_t bufsiz, struct cgpu_info __maybe_unused *cgpu);
 
 struct api_data;