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

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