Commit 0b014f78d6dc505378ea505c4f4c6c8071628a43

Con Kolivas 2014-03-05T22:27:13

Provide support for 2 chips in libbitfury sendhashdata and enable the 2nd chip on BXM devices.

diff --git a/driver-bitfury.c b/driver-bitfury.c
index fcca8dd..5cdf0e6 100644
--- a/driver-bitfury.c
+++ b/driver-bitfury.c
@@ -658,12 +658,21 @@ static bool bxm_reset_bitfury(struct cgpu_info *bitfury)
 
 static bool bxm_reinit(struct cgpu_info *bitfury, struct bitfury_info *info)
 {
-	spi_clear_buf(info);
-	spi_add_break(info);
-	spi_set_freq(info);
-	spi_send_conf(info);
-	spi_send_init(info);
-	return info->spi_txrx(bitfury, info);
+	bool ret;
+	int i;
+
+	for (i = 0; i < 2; i++) {
+		spi_clear_buf(info);
+		spi_add_break(info);
+		spi_add_fasync(info, i);
+		spi_set_freq(info);
+		spi_send_conf(info);
+		spi_send_init(info);
+		ret = info->spi_txrx(bitfury, info);
+		if (!ret)
+			break;
+	}
+	return ret;
 }
 
 static bool bxm_detect_one(struct cgpu_info *bitfury, struct bitfury_info *info)
@@ -1180,30 +1189,41 @@ static int64_t bxf_scan(struct cgpu_info *bitfury, struct bitfury_info *info)
 	return ret;
 }
 
-static int64_t nf1_scan(struct thr_info *thr, struct cgpu_info *bitfury,
-			struct bitfury_info *info)
+static void bitfury_check_work(struct thr_info *thr, struct cgpu_info *bitfury,
+			       struct bitfury_info *info, int chip_n)
 {
-	int64_t ret = 0;
-
-	if (!info->work) {
-		info->work = get_work(thr, thr->id);
+	if (!info->work[chip_n]) {
+		info->work[chip_n] = get_work(thr, thr->id);
 		if (unlikely(thr->work_restart)) {
-			free_work(info->work);
-			info->work = NULL;
-			return 0;
+			free_work(info->work[chip_n]);
+			info->work[chip_n] = NULL;
+			return;
 		}
-		bitfury_work_to_payload(&info->payload, info->work);
+		bitfury_work_to_payload(&info->payload[chip_n], info->work[chip_n]);
 	}
-	if (!libbitfury_sendHashData(thr, bitfury, info))
-		return -1;
-
-	if (info->job_switched) {
-		if (likely(info->owork))
-			free_work(info->owork);
-		info->owork = info->work;
-		info->work = NULL;
+
+	if (unlikely(bitfury->usbinfo.nodev))
+		return;
+
+	if (!libbitfury_sendHashData(thr, bitfury, info, chip_n))
+		usb_nodev(bitfury);
+
+	if (info->job_switched[chip_n]) {
+		if (likely(info->owork[chip_n]))
+			free_work(info->owork[chip_n]);
+		info->owork[chip_n] = info->work[chip_n];
+		info->work[chip_n] = NULL;
 	}
 
+}
+
+static int64_t nf1_scan(struct thr_info *thr, struct cgpu_info *bitfury,
+			struct bitfury_info *info)
+{
+	int64_t ret = 0;
+
+	bitfury_check_work(thr, bitfury, info, 0);
+
 	ret = bitfury_rate(info);
 
 	if (unlikely(bitfury->usbinfo.nodev)) {
@@ -1219,25 +1239,10 @@ static int64_t bxm_scan(struct thr_info *thr, struct cgpu_info *bitfury,
 			struct bitfury_info *info)
 {
 	int64_t ret = 0;
+	int chip_n;
 
-	if (!info->work) {
-		info->work = get_work(thr, thr->id);
-		if (unlikely(thr->work_restart)) {
-			free_work(info->work);
-			info->work = NULL;
-			return 0;
-		}
-		bitfury_work_to_payload(&info->payload, info->work);
-	}
-	if (!libbitfury_sendHashData(thr, bitfury, info))
-		return -1;
-
-	if (info->job_switched) {
-		if (likely(info->owork))
-			free_work(info->owork);
-		info->owork = info->work;
-		info->work = NULL;
-	}
+	for (chip_n = 0; chip_n < 2; chip_n++)
+		bitfury_check_work(thr, bitfury, info, chip_n);
 
 	ret = bitfury_rate(info);
 
diff --git a/driver-bitfury.h b/driver-bitfury.h
index b8d8ff8..fa8bef9 100644
--- a/driver-bitfury.h
+++ b/driver-bitfury.h
@@ -96,12 +96,12 @@ struct bitfury_info {
 	char spibuf[SPIBUF_SIZE];
 	unsigned int spibufsz;
 	int osc6_bits;
-	struct bitfury_payload payload;
-	unsigned oldbuf[17];
-	bool job_switched;
-	bool second_run;
-	struct work *work;
-	struct work *owork;
+	struct bitfury_payload payload[2];
+	unsigned oldbuf[17 * 2];
+	bool job_switched[2];
+	bool second_run[2];
+	struct work *work[2];
+	struct work *owork[2];
 
 	bool (*spi_txrx)(struct cgpu_info *, struct bitfury_info *info);
 };
diff --git a/libbitfury.c b/libbitfury.c
index c6c62d9..1857464 100644
--- a/libbitfury.c
+++ b/libbitfury.c
@@ -337,12 +337,13 @@ bool bitfury_checkresults(struct thr_info *thr, struct work *work, uint32_t nonc
 	return false;
 }
 
+/* Currently really only supports 2 chips, so chip_n can only be 0 or 1 */
 bool libbitfury_sendHashData(struct thr_info *thr, struct cgpu_info *bitfury,
-			     struct bitfury_info *info)
+			     struct bitfury_info *info, int chip_n)
 {
 	unsigned newbuf[17];
-	unsigned *oldbuf = info->oldbuf;
-	struct bitfury_payload *p = &(info->payload);
+	unsigned *oldbuf = &info->oldbuf[17 * chip_n];
+	struct bitfury_payload *p = &info->payload[chip_n];
 	unsigned int localvec[20];
 
 	/* Programming next value */
@@ -351,32 +352,32 @@ bool libbitfury_sendHashData(struct thr_info *thr, struct cgpu_info *bitfury,
 
 	spi_clear_buf(info);
 	spi_add_break(info);
+	spi_add_fasync(info, chip_n);
 	spi_add_data(info, 0x3000, (void*)localvec, 19 * 4);
 	if (!info->spi_txrx(bitfury, info))
 		return false;
 
-	memcpy(newbuf, info->spibuf + 4, 17 * 4);
+	memcpy(newbuf, info->spibuf + 4 + chip_n, 17 * 4);
 
-	info->job_switched = newbuf[16] != oldbuf[16];
+	info->job_switched[chip_n] = newbuf[16] != oldbuf[16];
 
-	if (likely(info->second_run)) {
-		if (info->job_switched) {
+	if (likely(info->second_run[chip_n])) {
+		if (info->job_switched[chip_n]) {
 			int i;
 
 			for (i = 0; i < 16; i++) {
-				if (oldbuf[i] != newbuf[i] && info->owork) {
+				if (oldbuf[i] != newbuf[i] && info->owork[chip_n]) {
 					uint32_t nonce; //possible nonce
 
 					nonce = decnonce(newbuf[i]);
-					if (bitfury_checkresults(thr, info->owork, nonce))
+					if (bitfury_checkresults(thr, info->owork[chip_n], nonce))
 						info->nonces++;
 				}
 			}
-
 			memcpy(oldbuf, newbuf, 17 * 4);
 		}
 	} else
-		info->second_run = true;
+		info->second_run[chip_n] = true;
 
 	cgsleep_ms(BITFURY_REFRESH_DELAY);
 
diff --git a/libbitfury.h b/libbitfury.h
index 884ee57..7d59573 100644
--- a/libbitfury.h
+++ b/libbitfury.h
@@ -28,6 +28,6 @@ bool mcp_spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info);
 bool ftdi_spi_txrx(struct cgpu_info *bitfury, struct bitfury_info *info);
 bool bitfury_checkresults(struct thr_info *thr, struct work *work, uint32_t nonce);
 bool libbitfury_sendHashData(struct thr_info *thr, struct cgpu_info *bitfury,
-			     struct bitfury_info *info);
+			     struct bitfury_info *info, int chip_n);
 
 #endif /* LIBBITFURY_H */