Browse Source

ztex: Remove libztex slave device interface, simply passing fpgaNum to selectFpga

Luke Dashjr 12 years ago
parent
commit
e3240ff93b
3 changed files with 57 additions and 68 deletions
  1. 38 47
      driver-ztex.c
  2. 16 17
      libztex.c
  3. 3 4
      libztex.h

+ 38 - 47
driver-ztex.c

@@ -43,12 +43,12 @@ struct device_drv ztex_drv;
 static void ztex_disable(struct thr_info* thr);
 static bool ztex_prepare(struct thr_info *thr);
 
-static void ztex_selectFpga(struct libztex_device* ztex)
+static void ztex_selectFpga(struct libztex_device* ztex, int16_t fpgaNum)
 {
 	if (ztex->root->numberOfFpgas > 1) {
-		if (ztex->root->selectedFpga != ztex->fpgaNum)
+		if (ztex->root->selectedFpga != fpgaNum)
 			mutex_lock(&ztex->root->mutex);
-		libztex_selectFpga(ztex);
+		libztex_selectFpga(ztex, fpgaNum);
 	}
 }
 
@@ -60,7 +60,7 @@ static void ztex_releaseFpga(struct libztex_device* ztex)
 	}
 }
 
-static struct cgpu_info *ztex_setup(struct libztex_device *dev, int j, int fpgacount)
+static struct cgpu_info *ztex_setup(struct libztex_device *dev, int fpgacount)
 {
 	struct cgpu_info *ztex;
 	char *fpganame = (char*)dev->snString;
@@ -70,9 +70,8 @@ static struct cgpu_info *ztex_setup(struct libztex_device *dev, int j, int fpgac
 	ztex->device_ztex = dev;
 	ztex->procs = fpgacount;
 	ztex->threads = fpgacount;
-	dev->fpgaNum = j;
 	add_cgpu(ztex);
-	strcpy(ztex->device_ztex->repr, ztex->proc_repr);
+	strcpy(ztex->device_ztex->repr, ztex->dev_repr);
 	ztex->name = fpganame;
 	applog(LOG_INFO, "%"PRIpreprv": Found Ztex (ZTEX %s)", ztex->dev_repr, fpganame);
 
@@ -97,7 +96,7 @@ static int ztex_autodetect(void)
 		ztex_master = ztex_devices[i]->dev;
 		ztex_master->root = ztex_master;
 		fpgacount = libztex_numberOfFpgas(ztex_master);
-		ztex = ztex_setup(ztex_master, 0, fpgacount);
+		ztex = ztex_setup(ztex_master, fpgacount);
 
 		totaldevs += fpgacount;
 
@@ -119,10 +118,11 @@ static void ztex_detect()
 
 static bool ztex_change_clock_func(struct thr_info *thr, int bestM)
 {
+	struct cgpu_info *cgpu = thr->cgpu;
 	struct libztex_device *ztex = thr->cgpu->device_ztex;
 
-	ztex_selectFpga(ztex);
-	libztex_setFreq(ztex, bestM);
+	ztex_selectFpga(ztex, cgpu->proc_id);
+	libztex_setFreq(ztex, bestM, cgpu->proc_repr);
 	ztex_releaseFpga(ztex);
 
 	return true;
@@ -130,17 +130,18 @@ static bool ztex_change_clock_func(struct thr_info *thr, int bestM)
 
 static bool ztex_updateFreq(struct thr_info *thr)
 {
+	struct cgpu_info *cgpu = thr->cgpu;
 	struct libztex_device *ztex = thr->cgpu->device_ztex;
 	bool rv = dclk_updateFreq(&ztex->dclk, ztex_change_clock_func, thr);
 	if (unlikely(!rv)) {
-		ztex_selectFpga(ztex);
+		ztex_selectFpga(ztex, cgpu->proc_id);
 		libztex_resetFpga(ztex);
 		ztex_releaseFpga(ztex);
 	}
 	return rv;
 }
 
-static bool ztex_checkNonce(struct libztex_device *ztex,
+static bool ztex_checkNonce(struct cgpu_info *cgpu,
                             struct work *work,
                             struct libztex_hash_data *hdata)
 {
@@ -159,7 +160,7 @@ static bool ztex_checkNonce(struct libztex_device *ztex,
 	sha2(hash1, 32, hash2);
 
 	if (be32toh(hash2_32[7]) != ((hdata->hash7 + 0x5be0cd19) & 0xFFFFFFFF)) {
-		applog(LOG_DEBUG, "%s: checkNonce failed for %08x", ztex->repr, hdata->nonce);
+		applog(LOG_DEBUG, "%"PRIpreprv": checkNonce failed for %08x", cgpu->proc_repr, hdata->nonce);
 		return false;
 	}
 	return true;
@@ -168,6 +169,7 @@ static bool ztex_checkNonce(struct libztex_device *ztex,
 static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
                               __maybe_unused int64_t max_nonce)
 {
+	struct cgpu_info *cgpu = thr->cgpu;
 	struct libztex_device *ztex;
 	unsigned char sendbuf[44];
 	int i, j, k;
@@ -186,28 +188,28 @@ static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
 	memcpy(sendbuf, work->data + 64, 12);
 	memcpy(sendbuf + 12, work->midstate, 32);
 
-	ztex_selectFpga(ztex);
+	ztex_selectFpga(ztex, cgpu->proc_id);
 	i = libztex_sendHashData(ztex, sendbuf);
 	if (i < 0) {
 		// Something wrong happened in send
-		applog(LOG_ERR, "%s: Failed to send hash data with err %d, retrying", ztex->repr, i);
+		applog(LOG_ERR, "%"PRIpreprv": Failed to send hash data with err %d, retrying", cgpu->proc_repr, i);
 		nmsleep(500);
 		i = libztex_sendHashData(ztex, sendbuf);
 		if (i < 0) {
 			// And there's nothing we can do about it
 			ztex_disable(thr);
-			applog(LOG_ERR, "%s: Failed to send hash data with err %d, giving up", ztex->repr, i);
+			applog(LOG_ERR, "%"PRIpreprv": Failed to send hash data with err %d, giving up", cgpu->proc_repr, i);
 			ztex_releaseFpga(ztex);
 			return -1;
 		}
 	}
 	ztex_releaseFpga(ztex);
 
-	applog(LOG_DEBUG, "%s: sent hashdata", ztex->repr);
+	applog(LOG_DEBUG, "%"PRIpreprv": sent hashdata", cgpu->proc_repr);
 
 	lastnonce = calloc(1, sizeof(uint32_t)*ztex->numNonces);
 	if (lastnonce == NULL) {
-		applog(LOG_ERR, "%s: failed to allocate lastnonce[%d]", ztex->repr, ztex->numNonces);
+		applog(LOG_ERR, "%"PRIpreprv": failed to allocate lastnonce[%d]", cgpu->proc_repr, ztex->numNonces);
 		return -1;
 	}
 
@@ -215,7 +217,7 @@ static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
 	backlog_max = ztex->numNonces * (2 + ztex->extraSolutions);
 	backlog = calloc(1, sizeof(uint32_t) * backlog_max);
 	if (backlog == NULL) {
-		applog(LOG_ERR, "%s: failed to allocate backlog[%d]", ztex->repr, backlog_max);
+		applog(LOG_ERR, "%"PRIpreprv": failed to allocate backlog[%d]", cgpu->proc_repr, backlog_max);
 		free(lastnonce);
 		return -1;
 	}
@@ -223,25 +225,25 @@ static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
 	overflow = false;
 	int count = 0;
 
-	applog(LOG_DEBUG, "%s: entering poll loop", ztex->repr);
+	applog(LOG_DEBUG, "%"PRIpreprv": entering poll loop", cgpu->proc_repr);
 	while (!(overflow || thr->work_restart)) {
 		count++;
 		if (!restart_wait(thr, 250))
 		{
-			applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
+			applog(LOG_DEBUG, "%"PRIpreprv": New work detected", cgpu->proc_repr);
 			break;
 		}
-		ztex_selectFpga(ztex);
+		ztex_selectFpga(ztex, cgpu->proc_id);
 		i = libztex_readHashData(ztex, &hdata[0]);
 		if (i < 0) {
 			// Something wrong happened in read
-			applog(LOG_ERR, "%s: Failed to read hash data with err %d, retrying", ztex->repr, i);
+			applog(LOG_ERR, "%"PRIpreprv": Failed to read hash data with err %d, retrying", cgpu->proc_repr, i);
 			nmsleep(500);
 			i = libztex_readHashData(ztex, &hdata[0]);
 			if (i < 0) {
 				// And there's nothing we can do about it
 				ztex_disable(thr);
-				applog(LOG_ERR, "%s: Failed to read hash data with err %d, giving up", ztex->repr, i);
+				applog(LOG_ERR, "%"PRIpreprv": Failed to read hash data with err %d, giving up", cgpu->proc_repr, i);
 				free(lastnonce);
 				free(backlog);
 				ztex_releaseFpga(ztex);
@@ -251,7 +253,7 @@ static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
 		ztex_releaseFpga(ztex);
 
 		if (thr->work_restart) {
-			applog(LOG_DEBUG, "%s: New work detected", ztex->repr);
+			applog(LOG_DEBUG, "%"PRIpreprv": New work detected", cgpu->proc_repr);
 			break;
 		}
 
@@ -262,12 +264,12 @@ static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
 			if (nonce > noncecnt)
 				noncecnt = nonce;
 			if (((0xffffffff - nonce) < (nonce - lastnonce[i])) || nonce < lastnonce[i]) {
-				applog(LOG_DEBUG, "%s: overflow nonce=%08x lastnonce=%08x", ztex->repr, nonce, lastnonce[i]);
+				applog(LOG_DEBUG, "%"PRIpreprv": overflow nonce=%08x lastnonce=%08x", cgpu->proc_repr, nonce, lastnonce[i]);
 				overflow = true;
 			} else
 				lastnonce[i] = nonce;
 
-			if (!ztex_checkNonce(ztex, work, &hdata[i])) {
+			if (!ztex_checkNonce(cgpu, work, &hdata[i])) {
 				// do not count errors in the first 500ms after sendHashData (2x250 wait time)
 				if (count > 2)
 					dclk_errorCount(&ztex->dclk, 1.0 / ztex->numNonces);
@@ -299,7 +301,7 @@ static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
 					work->blk.nonce = 0xffffffff;
 					if (!j || test_nonce(work, nonce, false))
 						submit_nonce(thr, work, nonce);
-					applog(LOG_DEBUG, "%s: submitted %08x (from N%dE%d)", ztex->repr, nonce, i, j);
+					applog(LOG_DEBUG, "%"PRIpreprv": submitted %08x (from N%dE%d)", cgpu->proc_repr, nonce, i, j);
 				}
 			}
 		}
@@ -315,7 +317,7 @@ static int64_t ztex_scanhash(struct thr_info *thr, struct work *work,
 		return -1;
 	}
 
-	applog(LOG_DEBUG, "%s: exit %1.8X", ztex->repr, noncecnt);
+	applog(LOG_DEBUG, "%"PRIpreprv": exit %1.8X", cgpu->proc_repr, noncecnt);
 
 	work->blk.nonce = 0xffffffff;
 
@@ -360,17 +362,6 @@ static bool ztex_prepare(struct thr_info *thr)
 
 	cgtime(&now);
 	get_datestamp(cgpu->init, &now);
-
-	if (cgpu->proc_id)
-	{
-		struct libztex_device *ztex_master = cgpu->device->device_ztex;
-		ztex = malloc(sizeof(struct libztex_device));
-		memcpy(ztex, ztex_master, sizeof(*ztex));
-		cgpu->device_ztex = ztex;
-		ztex->root = ztex_master;
-		ztex->fpgaNum = cgpu->proc_id;
-		strcpy(ztex->repr, cgpu->proc_repr);
-	}
 	
 	{
 		char fpganame[LIBZTEX_SNSTRING_LEN+3+1];
@@ -378,20 +369,20 @@ static bool ztex_prepare(struct thr_info *thr)
 		cgpu->name = fpganame;
 	}
 
-	ztex_selectFpga(ztex);
-	if (libztex_configureFpga(ztex) != 0) {
+	ztex_selectFpga(ztex, cgpu->proc_id);
+	if (libztex_configureFpga(ztex, cgpu->proc_repr) != 0) {
 		libztex_resetFpga(ztex);
 		ztex_releaseFpga(ztex);
-		applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device_ztex->repr);
+		applog(LOG_ERR, "%"PRIpreprv": Disabling!", cgpu->proc_repr);
 		thr->cgpu->deven = DEV_DISABLED;
 		return true;
 	}
 	ztex->dclk.freqM = ztex->dclk.freqMaxM+1;;
 	//ztex_updateFreq(thr);
-	libztex_setFreq(ztex, ztex->dclk.freqMDefault);
+	libztex_setFreq(ztex, ztex->dclk.freqMDefault, cgpu->proc_repr);
 	ztex_releaseFpga(ztex);
 	notifier_init(thr->work_restart_notifier);
-	applog(LOG_DEBUG, "%s: prepare", ztex->repr);
+	applog(LOG_DEBUG, "%"PRIpreprv": prepare", cgpu->proc_repr);
 	return true;
 }
 
@@ -404,15 +395,15 @@ static void ztex_shutdown(struct thr_info *thr)
 		return;
 	
 	cgpu->device_ztex = NULL;
-	if (ztex->root->numberOfFpgas > 1 && ztex->fpgaNum == 0)
+	if (ztex->root->numberOfFpgas > 1 /*&& ztex->fpgaNum == 0*/)
 		pthread_mutex_destroy(&ztex->mutex);
-	applog(LOG_DEBUG, "%s: shutdown", ztex->repr);
+	applog(LOG_DEBUG, "%"PRIpreprv": shutdown", cgpu->proc_repr);
 	libztex_destroy_device(ztex);
 }
 
 static void ztex_disable(struct thr_info *thr)
 {
-	applog(LOG_ERR, "%s: Disabling!", thr->cgpu->device_ztex->repr);
+	applog(LOG_ERR, "%"PRIpreprv": Disabling!", thr->cgpu->proc_repr);
 	thr->cgpu->deven = DEV_DISABLED;
 	ztex_shutdown(thr);
 }

+ 16 - 17
libztex.c

@@ -356,7 +356,7 @@ static int libztex_getFpgaState(struct libztex_device *ztex, struct libztex_fpga
 	return 0;
 }
 
-static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firmware, bool force, char bs)
+static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firmware, bool force, char bs, const char *repr)
 {
 	struct libztex_fpgastate state;
 	const int transactionBytes = 65536;
@@ -386,7 +386,7 @@ static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firm
 	for (tries = 3; tries > 0; tries--) {
 		fp = open_bitstream("ztex", firmware);
 		if (!fp) {
-			applog(LOG_ERR, "%s: failed to read bitstream '%s'", ztex->repr, firmware);
+			applog(LOG_ERR, "%"PRIpreprv": failed to read bitstream '%s'", repr, firmware);
 			libusb_release_interface(ztex->hndl, settings[1]);
 			return -2;
 		}
@@ -425,7 +425,7 @@ static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firm
 
 		libztex_getFpgaState(ztex, &state);
 		if (!state.fpgaConfigured) {
-			applog(LOG_ERR, "%s: HS FPGA configuration failed: DONE pin does not go high", ztex->repr);
+			applog(LOG_ERR, "%"PRIpreprv": HS FPGA configuration failed: DONE pin does not go high", repr);
 			libusb_release_interface(ztex->hndl, settings[1]);
 			return -3;
 		}
@@ -434,11 +434,11 @@ static int libztex_configureFpgaHS(struct libztex_device *ztex, const char* firm
 	libusb_release_interface(ztex->hndl, settings[1]);
 
 	nmsleep(200);
-	applog(LOG_INFO, "%s: HS FPGA configuration done", ztex->repr);
+	applog(LOG_INFO, "%"PRIpreprv": HS FPGA configuration done", repr);
 	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, const char *repr)
 {
 	struct libztex_fpgastate state;
 	const int transactionBytes = 2048;
@@ -458,7 +458,7 @@ static int libztex_configureFpgaLS(struct libztex_device *ztex, const char* firm
 	for (tries = 10; tries > 0; tries--) {
 		fp = open_bitstream("ztex", firmware);
 		if (!fp) {
-			applog(LOG_ERR, "%s: failed to read bitstream '%s'", ztex->repr, firmware);
+			applog(LOG_ERR, "%"PRIpreprv": failed to read bitstream '%s'", repr, firmware);
 			return -2;
 		}
 
@@ -494,25 +494,25 @@ static int libztex_configureFpgaLS(struct libztex_device *ztex, const char* firm
 
 	libztex_getFpgaState(ztex, &state);
 	if (!state.fpgaConfigured) {
-		applog(LOG_ERR, "%s: LS FPGA configuration failed: DONE pin does not go high", ztex->repr);
+		applog(LOG_ERR, "%"PRIpreprv": LS FPGA configuration failed: DONE pin does not go high", repr);
 		return -3;
 	}
 
 	nmsleep(200);
-	applog(LOG_INFO, "%s: FPGA configuration done", ztex->repr);
+	applog(LOG_INFO, "%"PRIpreprv": FPGA configuration done", repr);
 	return 0;
 }
 
-int libztex_configureFpga(struct libztex_device *ztex)
+int libztex_configureFpga(struct libztex_device *ztex, const char *repr)
 {
 	char buf[256];
 	int rv;
 
 	strcpy(buf, ztex->bitFileName);
 	strcat(buf, ".bit");
-	rv = libztex_configureFpgaHS(ztex, buf, true, 2);
+	rv = libztex_configureFpgaHS(ztex, buf, true, 2, repr);
 	if (rv != 0)
-		rv = libztex_configureFpgaLS(ztex, buf, true, 2);
+		rv = libztex_configureFpgaLS(ztex, buf, true, 2, repr);
 	return rv;
 }
 
@@ -540,10 +540,9 @@ int libztex_numberOfFpgas(struct libztex_device *ztex)
 	return ztex->numberOfFpgas;
 }
 
-int libztex_selectFpga(struct libztex_device *ztex)
+int libztex_selectFpga(struct libztex_device *ztex, int16_t number)
 {
 	int cnt, fpgacnt = libztex_numberOfFpgas(ztex->root);
-	int16_t number = ztex->fpgaNum;
 
 	if (number < 0 || number >= fpgacnt) {
 		applog(LOG_WARNING, "%s: Trying to select wrong fpga (%d in %d)", ztex->repr, number, fpgacnt);
@@ -561,7 +560,7 @@ int libztex_selectFpga(struct libztex_device *ztex)
 	return 0;
 }
 
-int libztex_setFreq(struct libztex_device *ztex, uint16_t freq)
+int libztex_setFreq(struct libztex_device *ztex, uint16_t freq, const char *repr)
 {
 	int cnt;
 	uint16_t oldfreq = ztex->dclk.freqM;
@@ -576,14 +575,14 @@ int libztex_setFreq(struct libztex_device *ztex, uint16_t freq)
 	}
 	ztex->dclk.freqM = freq;
 	if (oldfreq > ztex->dclk.freqMaxM)
-		applog(LOG_WARNING, "%s: Frequency set to %u MHz (range: %u-%u)",
-		       ztex->repr,
+		applog(LOG_WARNING, "%"PRIpreprv": Frequency set to %u MHz (range: %u-%u)",
+		       repr,
 		       (unsigned)(ztex->freqM1 * (ztex->dclk.freqM + 1)),
 		       (unsigned)ztex->freqM1,
 		       (unsigned)(ztex->freqM1 * (ztex->dclk.freqMaxM + 1))
 		);
 	else
-		dclk_msg_freqchange(ztex->repr,
+		dclk_msg_freqchange(repr,
 		                    ztex->freqM1 * (oldfreq + 1),
 		                    ztex->freqM1 * (ztex->dclk.freqM + 1),
 		                    NULL);

+ 3 - 4
libztex.h

@@ -45,7 +45,6 @@ struct libztex_fpgastate {
 struct libztex_device {
 	pthread_mutex_t	mutex;
 	struct libztex_device *root;
-	int16_t fpgaNum;
 	struct libusb_device_descriptor descriptor;
 	libusb_device_handle *hndl; 
 	unsigned char usbbus;
@@ -88,12 +87,12 @@ extern int libztex_scanDevices (struct libztex_dev_list ***devs);
 extern void libztex_freeDevList (struct libztex_dev_list **devs);
 extern int libztex_prepare_device (struct libusb_device *dev, struct libztex_device** ztex);
 extern void libztex_destroy_device (struct libztex_device* ztex);
-extern int libztex_configureFpga (struct libztex_device *dev);
-extern int libztex_setFreq (struct libztex_device *ztex, uint16_t freq);
+extern int libztex_configureFpga (struct libztex_device *dev, const char *repr);
+extern int libztex_setFreq (struct libztex_device *ztex, uint16_t freq, const char *repr);
 extern int libztex_sendHashData (struct libztex_device *ztex, unsigned char *sendbuf);
 extern int libztex_readHashData (struct libztex_device *ztex, struct libztex_hash_data nonces[]);
 extern int libztex_resetFpga (struct libztex_device *ztex);
-extern int libztex_selectFpga(struct libztex_device *ztex);
+extern int libztex_selectFpga(struct libztex_device *ztex, int16_t fpgaNum);
 extern int libztex_numberOfFpgas(struct libztex_device *ztex);
 
 #endif /* __LIBZTEX_H__ */