Blame SOURCES/0001-net-nfp-use-generic-PCI-config-access-functions.patch

702481
From b419d9b1803882a15cf1448448720daaf568f21b Mon Sep 17 00:00:00 2001
702481
From: Alejandro Lucero <alejandro.lucero@netronome.com>
702481
Date: Mon, 18 Jun 2018 21:06:12 +0100
702481
Subject: [PATCH] net/nfp: use generic PCI config access functions
702481
702481
This patch avoids direct access to device config sysfs file using
702481
rte_pci_read_config instead.
702481
702481
Apart from replicating code, it turns out this direct access does
702481
not always work if non-root users execute DPDK apps. In those cases
702481
it is mandatory to go through VFIO specific function for reading pci
702481
config space.
702481
702481
Signed-off-by: Alejandro Lucero <alejandro.lucero@netronome.com>
702481
(cherry picked from commit caab11ea33f02d9a0869890b48e371f928090279)
702481
---
702481
 drivers/net/nfp/nfp_net.c                  |   4 +-
702481
 drivers/net/nfp/nfpcore/nfp_cpp.h          |   6 +-
702481
 drivers/net/nfp/nfpcore/nfp_cpp_pcie_ops.c | 111 +++++++--------------
702481
 drivers/net/nfp/nfpcore/nfp_cppcore.c      |   9 +-
702481
 4 files changed, 47 insertions(+), 83 deletions(-)
702481
702481
diff --git a/drivers/net/nfp/nfp_net.c b/drivers/net/nfp/nfp_net.c
702481
index 71249572d..62db54d8b 100644
702481
--- a/drivers/net/nfp/nfp_net.c
702481
+++ b/drivers/net/nfp/nfp_net.c
702481
@@ -3154,9 +3154,9 @@ static int nfp_pf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
702481
 	 * use a lock file if UIO is being used.
702481
 	 */
702481
 	if (dev->kdrv == RTE_KDRV_VFIO)
702481
-		cpp = nfp_cpp_from_device_name(dev->device.name, 0);
702481
+		cpp = nfp_cpp_from_device_name(dev, 0);
702481
 	else
702481
-		cpp = nfp_cpp_from_device_name(dev->device.name, 1);
702481
+		cpp = nfp_cpp_from_device_name(dev, 1);
702481
 
702481
 	if (!cpp) {
702481
 		RTE_LOG(ERR, PMD, "A CPP handle can not be obtained");
702481
diff --git a/drivers/net/nfp/nfpcore/nfp_cpp.h b/drivers/net/nfp/nfpcore/nfp_cpp.h
702481
index de2ff84e9..1427954c1 100644
702481
--- a/drivers/net/nfp/nfpcore/nfp_cpp.h
702481
+++ b/drivers/net/nfp/nfpcore/nfp_cpp.h
702481
@@ -6,6 +6,8 @@
702481
 #ifndef __NFP_CPP_H__
702481
 #define __NFP_CPP_H__
702481
 
702481
+#include <rte_ethdev_pci.h>
702481
+
702481
 #include "nfp-common/nfp_platform.h"
702481
 #include "nfp-common/nfp_resid.h"
702481
 
702481
@@ -54,7 +56,7 @@ struct nfp_cpp_operations {
702481
 	size_t area_priv_size;
702481
 
702481
 	/* Instance an NFP CPP */
702481
-	int (*init)(struct nfp_cpp *cpp, const char *devname);
702481
+	int (*init)(struct nfp_cpp *cpp, struct rte_pci_device *dev);
702481
 
702481
 	/*
702481
 	 * Free the bus.
702481
@@ -181,7 +183,7 @@ uint32_t __nfp_cpp_model_autodetect(struct nfp_cpp *cpp);
702481
  *
702481
  * @return NFP CPP handle, or NULL on failure (and set errno accordingly).
702481
  */
702481
-struct nfp_cpp *nfp_cpp_from_device_name(const char *devname,
702481
+struct nfp_cpp *nfp_cpp_from_device_name(struct rte_pci_device *dev,
702481
 					 int driver_lock_needed);
702481
 
702481
 /*
702481
diff --git a/drivers/net/nfp/nfpcore/nfp_cpp_pcie_ops.c b/drivers/net/nfp/nfpcore/nfp_cpp_pcie_ops.c
702481
index e46dbc7d7..7d132baa9 100644
702481
--- a/drivers/net/nfp/nfpcore/nfp_cpp_pcie_ops.c
702481
+++ b/drivers/net/nfp/nfpcore/nfp_cpp_pcie_ops.c
702481
@@ -32,6 +32,7 @@
702481
 #include <sys/file.h>
702481
 #include <sys/stat.h>
702481
 
702481
+#include <rte_ethdev_pci.h>
702481
 #include "nfp_cpp.h"
702481
 #include "nfp_target.h"
702481
 #include "nfp6000/nfp6000.h"
702481
@@ -638,61 +639,32 @@ nfp_acquire_process_lock(struct nfp_pcie_user *desc)
702481
 }
702481
 
702481
 static int
702481
-nfp6000_set_model(struct nfp_pcie_user *desc, struct nfp_cpp *cpp)
702481
+nfp6000_set_model(struct rte_pci_device *dev, struct nfp_cpp *cpp)
702481
 {
702481
-	char tmp_str[80];
702481
-	uint32_t tmp;
702481
-	int fp;
702481
-
702481
-	snprintf(tmp_str, sizeof(tmp_str), "%s/%s/config", PCI_DEVICES,
702481
-		 desc->busdev);
702481
-
702481
-	fp = open(tmp_str, O_RDONLY);
702481
-	if (!fp)
702481
-		return -1;
702481
-
702481
-	lseek(fp, 0x2e, SEEK_SET);
702481
+	uint32_t model;
702481
 
702481
-	if (read(fp, &tmp, sizeof(tmp)) != sizeof(tmp)) {
702481
-		printf("Error reading config file for model\n");
702481
+	if (rte_pci_read_config(dev, &model, 4, 0x2e) < 0) {
702481
+		printf("nfp set model failed\n");
702481
 		return -1;
702481
 	}
702481
 
702481
-	tmp = tmp << 16;
702481
-
702481
-	if (close(fp) == -1)
702481
-		return -1;
702481
-
702481
-	nfp_cpp_model_set(cpp, tmp);
702481
+	model  = model << 16;
702481
+	nfp_cpp_model_set(cpp, model);
702481
 
702481
 	return 0;
702481
 }
702481
 
702481
 static int
702481
-nfp6000_set_interface(struct nfp_pcie_user *desc, struct nfp_cpp *cpp)
702481
+nfp6000_set_interface(struct rte_pci_device *dev, struct nfp_cpp *cpp)
702481
 {
702481
-	char tmp_str[80];
702481
-	uint16_t tmp;
702481
-	int fp;
702481
-
702481
-	snprintf(tmp_str, sizeof(tmp_str), "%s/%s/config", PCI_DEVICES,
702481
-		 desc->busdev);
702481
+	uint16_t interface;
702481
 
702481
-	fp = open(tmp_str, O_RDONLY);
702481
-	if (!fp)
702481
-		return -1;
702481
-
702481
-	lseek(fp, 0x154, SEEK_SET);
702481
-
702481
-	if (read(fp, &tmp, sizeof(tmp)) != sizeof(tmp)) {
702481
-		printf("error reading config file for interface\n");
702481
+	if (rte_pci_read_config(dev, &interface, 2, 0x154) < 0) {
702481
+		printf("nfp set interface failed\n");
702481
 		return -1;
702481
 	}
702481
 
702481
-	if (close(fp) == -1)
702481
-		return -1;
702481
-
702481
-	nfp_cpp_interface_set(cpp, tmp);
702481
+	nfp_cpp_interface_set(cpp, interface);
702481
 
702481
 	return 0;
702481
 }
702481
@@ -703,7 +675,7 @@ nfp6000_set_interface(struct nfp_pcie_user *desc, struct nfp_cpp *cpp)
702481
 #define PCI_EXT_CAP_NEXT(header)	((header >> 20) & 0xffc)
702481
 #define PCI_EXT_CAP_ID_DSN	0x03
702481
 static int
702481
-nfp_pci_find_next_ext_capability(int fp, int cap)
702481
+nfp_pci_find_next_ext_capability(struct rte_pci_device *dev, int cap)
702481
 {
702481
 	uint32_t header;
702481
 	int ttl;
702481
@@ -712,9 +684,8 @@ nfp_pci_find_next_ext_capability(int fp, int cap)
702481
 	/* minimum 8 bytes per capability */
702481
 	ttl = (PCI_CFG_SPACE_EXP_SIZE - PCI_CFG_SPACE_SIZE) / 8;
702481
 
702481
-	lseek(fp, pos, SEEK_SET);
702481
-	if (read(fp, &header, sizeof(header)) != sizeof(header)) {
702481
-		printf("error reading config file for serial\n");
702481
+	if (rte_pci_read_config(dev, &header, 4, pos) < 0) {
702481
+		printf("nfp error reading extended capabilities\n");
702481
 		return -1;
702481
 	}
702481
 
702481
@@ -733,9 +704,8 @@ nfp_pci_find_next_ext_capability(int fp, int cap)
702481
 		if (pos < PCI_CFG_SPACE_SIZE)
702481
 			break;
702481
 
702481
-		lseek(fp, pos, SEEK_SET);
702481
-		if (read(fp, &header, sizeof(header)) != sizeof(header)) {
702481
-			printf("error reading config file for serial\n");
702481
+		if (rte_pci_read_config(dev, &header, 4, pos) < 0) {
702481
+			printf("nfp error reading extended capabilities\n");
702481
 			return -1;
702481
 		}
702481
 	}
702481
@@ -744,56 +714,47 @@ nfp_pci_find_next_ext_capability(int fp, int cap)
702481
 }
702481
 
702481
 static int
702481
-nfp6000_set_serial(struct nfp_pcie_user *desc, struct nfp_cpp *cpp)
702481
+nfp6000_set_serial(struct rte_pci_device *dev, struct nfp_cpp *cpp)
702481
 {
702481
-	char tmp_str[80];
702481
 	uint16_t tmp;
702481
 	uint8_t serial[6];
702481
 	int serial_len = 6;
702481
-	int fp, pos;
702481
+	int pos;
702481
 
702481
-	snprintf(tmp_str, sizeof(tmp_str), "%s/%s/config", PCI_DEVICES,
702481
-		 desc->busdev);
702481
-
702481
-	fp = open(tmp_str, O_RDONLY);
702481
-	if (!fp)
702481
-		return -1;
702481
-
702481
-	pos = nfp_pci_find_next_ext_capability(fp, PCI_EXT_CAP_ID_DSN);
702481
+	pos = nfp_pci_find_next_ext_capability(dev, PCI_EXT_CAP_ID_DSN);
702481
 	if (pos <= 0) {
702481
-		printf("PCI_EXT_CAP_ID_DSN not found. Using default offset\n");
702481
-		lseek(fp, 0x156, SEEK_SET);
702481
+		printf("PCI_EXT_CAP_ID_DSN not found. nfp set serial failed\n");
702481
+		return -1;
702481
 	} else {
702481
-		lseek(fp, pos + 6, SEEK_SET);
702481
+		pos += 6;
702481
 	}
702481
 
702481
-	if (read(fp, &tmp, sizeof(tmp)) != sizeof(tmp)) {
702481
-		printf("error reading config file for serial\n");
702481
+	if (rte_pci_read_config(dev, &tmp, 2, pos) < 0) {
702481
+		printf("nfp set serial failed\n");
702481
 		return -1;
702481
 	}
702481
 
702481
 	serial[4] = (uint8_t)((tmp >> 8) & 0xff);
702481
 	serial[5] = (uint8_t)(tmp & 0xff);
702481
 
702481
-	if (read(fp, &tmp, sizeof(tmp)) != sizeof(tmp)) {
702481
-		printf("error reading config file for serial\n");
702481
+	pos += 2;
702481
+	if (rte_pci_read_config(dev, &tmp, 2, pos) < 0) {
702481
+		printf("nfp set serial failed\n");
702481
 		return -1;
702481
 	}
702481
 
702481
 	serial[2] = (uint8_t)((tmp >> 8) & 0xff);
702481
 	serial[3] = (uint8_t)(tmp & 0xff);
702481
 
702481
-	if (read(fp, &tmp, sizeof(tmp)) != sizeof(tmp)) {
702481
-		printf("error reading config file for serial\n");
702481
+	pos += 2;
702481
+	if (rte_pci_read_config(dev, &tmp, 2, pos) < 0) {
702481
+		printf("nfp set serial failed\n");
702481
 		return -1;
702481
 	}
702481
 
702481
 	serial[0] = (uint8_t)((tmp >> 8) & 0xff);
702481
 	serial[1] = (uint8_t)(tmp & 0xff);
702481
 
702481
-	if (close(fp) == -1)
702481
-		return -1;
702481
-
702481
 	nfp_cpp_serial_set(cpp, serial, serial_len);
702481
 
702481
 	return 0;
702481
@@ -831,7 +792,7 @@ nfp6000_set_barsz(struct nfp_pcie_user *desc)
702481
 }
702481
 
702481
 static int
702481
-nfp6000_init(struct nfp_cpp *cpp, const char *devname)
702481
+nfp6000_init(struct nfp_cpp *cpp, struct rte_pci_device *dev)
702481
 {
702481
 	char link[120];
702481
 	char tmp_str[80];
702481
@@ -846,7 +807,7 @@ nfp6000_init(struct nfp_cpp *cpp, const char *devname)
702481
 
702481
 
702481
 	memset(desc->busdev, 0, BUSDEV_SZ);
702481
-	strncpy(desc->busdev, devname, strlen(devname));
702481
+	strncpy(desc->busdev, dev->device.name, sizeof(desc->busdev));
702481
 
702481
 	if (cpp->driver_lock_needed) {
702481
 		ret = nfp_acquire_process_lock(desc);
702481
@@ -872,11 +833,11 @@ nfp6000_init(struct nfp_cpp *cpp, const char *devname)
702481
 	if (desc->device == -1)
702481
 		return -1;
702481
 
702481
-	if (nfp6000_set_model(desc, cpp) < 0)
702481
+	if (nfp6000_set_model(dev, cpp) < 0)
702481
 		return -1;
702481
-	if (nfp6000_set_interface(desc, cpp) < 0)
702481
+	if (nfp6000_set_interface(dev, cpp) < 0)
702481
 		return -1;
702481
-	if (nfp6000_set_serial(desc, cpp) < 0)
702481
+	if (nfp6000_set_serial(dev, cpp) < 0)
702481
 		return -1;
702481
 	if (nfp6000_set_barsz(desc) < 0)
702481
 		return -1;
702481
diff --git a/drivers/net/nfp/nfpcore/nfp_cppcore.c b/drivers/net/nfp/nfpcore/nfp_cppcore.c
702481
index f61143f7e..75d3c9748 100644
702481
--- a/drivers/net/nfp/nfpcore/nfp_cppcore.c
702481
+++ b/drivers/net/nfp/nfpcore/nfp_cppcore.c
702481
@@ -12,6 +12,7 @@
702481
 #include <sys/types.h>
702481
 
702481
 #include <rte_byteorder.h>
702481
+#include <rte_ethdev_pci.h>
702481
 
702481
 #include "nfp_cpp.h"
702481
 #include "nfp_target.h"
702481
@@ -542,7 +543,7 @@ nfp_xpb_readl(struct nfp_cpp *cpp, uint32_t xpb_addr, uint32_t *value)
702481
 }
702481
 
702481
 static struct nfp_cpp *
702481
-nfp_cpp_alloc(const char *devname, int driver_lock_needed)
702481
+nfp_cpp_alloc(struct rte_pci_device *dev, int driver_lock_needed)
702481
 {
702481
 	const struct nfp_cpp_operations *ops;
702481
 	struct nfp_cpp *cpp;
702481
@@ -561,7 +562,7 @@ nfp_cpp_alloc(const char *devname, int driver_lock_needed)
702481
 	cpp->driver_lock_needed = driver_lock_needed;
702481
 
702481
 	if (cpp->op->init) {
702481
-		err = cpp->op->init(cpp, devname);
702481
+		err = cpp->op->init(cpp, dev);
702481
 		if (err < 0) {
702481
 			free(cpp);
702481
 			return NULL;
702481
@@ -604,9 +605,9 @@ nfp_cpp_free(struct nfp_cpp *cpp)
702481
 }
702481
 
702481
 struct nfp_cpp *
702481
-nfp_cpp_from_device_name(const char *devname, int driver_lock_needed)
702481
+nfp_cpp_from_device_name(struct rte_pci_device *dev, int driver_lock_needed)
702481
 {
702481
-	return nfp_cpp_alloc(devname, driver_lock_needed);
702481
+	return nfp_cpp_alloc(dev, driver_lock_needed);
702481
 }
702481
 
702481
 /*
702481
-- 
702481
2.17.1
702481