Commit 0b64d438efede0bd45eb2f7fd2cf64f411901609

Con Kolivas 2013-11-10T18:13:26

Fine tune the reading of results in bitfury driver to not lose any across work restarts or corrupt due to store results not parsed during restart.

diff --git a/driver-bitfury.c b/driver-bitfury.c
index b177626..3331d82 100644
--- a/driver-bitfury.c
+++ b/driver-bitfury.c
@@ -224,13 +224,12 @@ static int64_t bitfury_scanwork(struct thr_info *thr)
 {
 	struct cgpu_info *bitfury = thr->cgpu;
 	struct bitfury_info *info = bitfury->device_data;
+	int amount, i, aged = 0, total = 0, ms_diff;
 	struct work *work, *tmp;
-	int amount, i, aged = 0;
 	struct timeval tv_now;
 	double nonce_rate;
 	int64_t ret = 0;
 	char buf[45];
-	int ms_diff;
 
 	work = get_queue_work(thr, bitfury, thr->id);
 	if (unlikely(thr->work_restart)) {
@@ -248,27 +247,28 @@ static int64_t bitfury_scanwork(struct thr_info *thr)
 	cgtime(&tv_now);
 	ms_diff = 600 - ms_tdiff(&tv_now, &info->tv_start);
 	if (ms_diff > 0) {
-		usb_read_timeout_cancellable(bitfury, info->buf, 512, &amount, ms_diff, C_BF1_GETRES);
-		info->tot += amount;
+		usb_read_timeout_cancellable(bitfury, info->buf, 512, &amount, ms_diff,
+					     C_BF1_GETRES);
+		total += amount;
 	}
 
-	if (unlikely(thr->work_restart))
-		goto out;
-
 	/* Now look for the bulk of the previous work results, they will come
 	 * in a batch following the first data. */
 	cgtime(&tv_now);
 	ms_diff = BF1WAIT - ms_tdiff(&tv_now, &info->tv_start);
-	if (unlikely(ms_diff < 10))
+	/* If a work restart was sent, just empty the buffer. */
+	if (unlikely(ms_diff < 10 || thr->work_restart))
 		ms_diff = 10;
-	usb_read_once_timeout_cancellable(bitfury, info->buf + info->tot, BF1MSGSIZE,
+	usb_read_once_timeout_cancellable(bitfury, info->buf + total, BF1MSGSIZE,
 					  &amount, ms_diff, C_BF1_GETRES);
-	info->tot += amount;
+	total += amount;
 	while (amount) {
-		usb_read_once_timeout(bitfury, info->buf + info->tot, 512, &amount, 10, C_BF1_GETRES);
-		info->tot += amount;
+		usb_read_once_timeout(bitfury, info->buf + total, 512, &amount, 10,
+				      C_BF1_GETRES);
+		total += amount;
 	};
 
+	/* Don't send whatever work we've stored if we got a restart */
 	if (unlikely(thr->work_restart))
 		goto out;
 
@@ -280,9 +280,10 @@ static int64_t bitfury_scanwork(struct thr_info *thr)
 	/* Get response acknowledging work */
 	usb_read(bitfury, buf, BF1MSGSIZE, &amount, C_BF1_GETWORK);
 
+out:
 	/* Search for what work the nonce matches in order of likelihood. Last
 	 * entry is end of result marker. */
-	for (i = 0; i < info->tot - BF1MSGSIZE; i += BF1MSGSIZE) {
+	for (i = 0; i < total - BF1MSGSIZE; i += BF1MSGSIZE) {
 		bool found = false;
 		uint32_t nonce;
 
@@ -304,8 +305,6 @@ static int64_t bitfury_scanwork(struct thr_info *thr)
 			inc_hw_errors(thr);
 	}
 
-	info->tot = 0;
-out:
 	cgtime(&tv_now);
 
 	/* This iterates over the hashlist finding work started more than 6
diff --git a/driver-bitfury.h b/driver-bitfury.h
index 2f09de1..79921b8 100644
--- a/driver-bitfury.h
+++ b/driver-bitfury.h
@@ -19,7 +19,6 @@ struct bitfury_info {
 	char product[8];
 	uint32_t serial;
 	char buf[512];
-	int tot;
 	int nonces;
 	int total_nonces;
 	double saved_nonces;