Skip to contentSkip to conversation

pci cleanup and blacklist rework

PATCH Superseded David Marchand 3 participants 26 messages Jan 22, 2016 – Mar 22, 2016

lore
Share & tools
lore
#0 lore raw

cover pci cleanup and blacklist rework

Before 2.2.0 release, while preparing for more changes in eal (and fixing a problem reported by Roger M. [1]), I came up with this patchset that tries to make the pci code more compact and easier to read.

I did limited testing, but this has been in my tree for quite some time now, so sending this to get feedback for 2.3 / 16.04.

The 4th patch introduces a change in linux eal.
Before, if a pci device was bound to no kernel driver, eal would set kdrv to "unknown". With this change, kdrv is set to "none".
This might make it possible to avoid the old issue of virtio devices being used by dpdk while still bound to kernel driver reported by Franck B. [2]. I'll let virtio guys look at this.
At the very least, it makes more sense to me.

The 5th and 6th patches could be squashed.
I just find it easier to review this way.

In the 8th patch, I noticed that a driver lookup is done when detaching a pci device while this makes no sense and looks buggy to me.

The last patch moves pci blacklist evaluation to rte_eal_pci_probe(). With this, it is now possible to attach a pci device blacklisted initially.
I am not entirely happy with this last patch, I will try to send a different solution next week (hooks in pci layer).

[1] http://dpdk.org/ml/archives/dev/2015-November/028140.html
[2] http://dpdk.org/ml/archives/dev/2015-September/023389.html

Signature · 21 lines
David Marchand

David Marchand (9):
  pci: no need for dynamic tailq init
  pci: add internal device list helpers
  pci: minor cleanup
  pci: rework sysfs parsing for driver
  pci: factorize probe/detach code
  pci: cosmetic change
  pci: factorize driver search
  pci: remove driver lookup from detach
  pci: blacklist only in global probe function

 lib/librte_eal/bsdapp/eal/eal_pci.c     |  37 +---
 lib/librte_eal/common/eal_common_pci.c  | 320 +++++++++++++++-----------------
 lib/librte_eal/common/include/rte_pci.h |  24 +++
 lib/librte_eal/linuxapp/eal/eal_pci.c   |  94 ++++------
 4 files changed, 218 insertions(+), 257 deletions(-)

-- 
1.9.1
#1 lore raw

PATCH 1/9 pci: no need for dynamic tailq init

These lists can be initialized once and for all at build time. With this, those lists are only manipulated in a common place (and we could even make them private).

---
 lib/librte_eal/bsdapp/eal/eal_pci.c    | 3 ---
 lib/librte_eal/common/eal_common_pci.c | 6 ++++--
 lib/librte_eal/linuxapp/eal/eal_pci.c  | 3 ---
 3 files changed, 4 insertions(+), 8 deletions(-)
lib/librte_eal/bsdapp/eal/eal_pci.c +0 -3
index 6c21fbd..4584522 100644
@@ -483,9 +483,6 @@ int rte_eal_pci_write_config(const struct rte_pci_device *dev,
 int
 rte_eal_pci_init(void)
 {
-	TAILQ_INIT(&pci_driver_list);
-	TAILQ_INIT(&pci_device_list);
-
 	/* for debug purposes, PCI can be disabled */
 	if (internal_config.no_pci)
 		return 0;
lib/librte_eal/common/eal_common_pci.c +4 -2
index dcfe947..1e12776 100644
@@ -82,8 +82,10 @@
 
 #include "eal_private.h"
 
-struct pci_driver_list pci_driver_list;
-struct pci_device_list pci_device_list;
+struct pci_driver_list pci_driver_list =
+	TAILQ_HEAD_INITIALIZER(pci_driver_list);
+struct pci_device_list pci_device_list =
+	TAILQ_HEAD_INITIALIZER(pci_device_list);
 
 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
 {
lib/librte_eal/linuxapp/eal/eal_pci.c +0 -3
index bc5b5be..a354f76 100644
@@ -625,9 +625,6 @@ int rte_eal_pci_write_config(const struct rte_pci_device *device,
 int
 rte_eal_pci_init(void)
 {
-	TAILQ_INIT(&pci_driver_list);
-	TAILQ_INIT(&pci_device_list);
-
 	/* for debug purposes, PCI can be disabled */
 	if (internal_config.no_pci)
 		return 0;
Signature · 1 line
1.9.1
#2 lore raw

PATCH 2/9 pci: add internal device list helpers

In reply to David Marchand

Remove duplicate code by moving this to helpers in common.

---
 lib/librte_eal/bsdapp/eal/eal_pci.c     | 34 +++++-----------
 lib/librte_eal/common/eal_common_pci.c  | 70 ++++++++++++++++++++++++---------
 lib/librte_eal/common/include/rte_pci.h | 24 +++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci.c   | 34 +++++-----------
 4 files changed, 94 insertions(+), 68 deletions(-)
lib/librte_eal/bsdapp/eal/eal_pci.c +9 -25
index 4584522..401cda3 100644
@@ -247,6 +247,7 @@ static int
 pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
 {
 	struct rte_pci_device *dev;
+	struct rte_pci_device *dev2;
 	struct pci_bar_io bar;
 	unsigned i, max;
 
@@ -311,32 +312,15 @@ pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
 		dev->mem_resource[i].phys_addr = bar.pbi_base & ~((uint64_t)0xf);
 	}
 
-	/* device is valid, add in list (sorted) */
-	if (TAILQ_EMPTY(&pci_device_list)) {
-		TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
-	}
+	dev2 = pci_find_device(&dev->addr);
+	if (!dev2)
+		pci_add_device(dev);
 	else {
-		struct rte_pci_device *dev2 = NULL;
-		int ret;
-
-		TAILQ_FOREACH(dev2, &pci_device_list, next) {
-			ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
-			if (ret > 0)
-				continue;
-			else if (ret < 0) {
-				TAILQ_INSERT_BEFORE(dev2, dev, next);
-				return 0;
-			} else { /* already registered */
-				dev2->kdrv = dev->kdrv;
-				dev2->max_vfs = dev->max_vfs;
-				memmove(dev2->mem_resource,
-					dev->mem_resource,
-					sizeof(dev->mem_resource));
-				free(dev);
-				return 0;
-			}
-		}
-		TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
+		dev2->kdrv = dev->kdrv;
+		dev2->max_vfs = dev->max_vfs;
+		memmove(dev2->mem_resource, dev->mem_resource,
+			sizeof(dev->mem_resource));
+		free(dev);
 	}
 
 	return 0;
lib/librte_eal/common/eal_common_pci.c +51 -19

91 lines not on this page · raw message

lib/librte_eal/common/include/rte_pci.h +24 -0
index 334c12e..4ba7192 100644
@@ -399,6 +399,30 @@ void *pci_map_resource(void *requested_addr, int fd, off_t offset,
 void pci_unmap_resource(void *requested_addr, size_t size);
 
 /**
+ * @internal
+ * Add a pci device to sorted pci device list
+ *
+ * @param dev
+ *	The PCI device object.
+ * @return
+ *   - 0 on success.
+ *   - negative on error.
+ */
+int pci_add_device(struct rte_pci_device *dev);
+
+/**
+ * @internal
+ * Find a pci device object based on a PCI Bus-Device-Function address.
+ *
+ * @param addr
+ *	The PCI Bus-Device-Function address to look for
+ * @return
+ *   - pointer to the object,
+ *   - NULL when not found.
+ */
+struct rte_pci_device *pci_find_device(const struct rte_pci_addr *addr);
+
+/**
  * Probe the single PCI device.
  *
  * Scan the content of the PCI bus, and find the pci device specified by pci
lib/librte_eal/linuxapp/eal/eal_pci.c +10 -24
index a354f76..83eec5d 100644
@@ -259,6 +259,7 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
 	char filename[PATH_MAX];
 	unsigned long tmp;
 	struct rte_pci_device *dev;
+	struct rte_pci_device *dev2;
 	char driver[PATH_MAX];
 	int ret;
 
@@ -364,30 +365,15 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
 	} else
 		dev->kdrv = RTE_KDRV_UNKNOWN;
 
-	/* device is valid, add in list (sorted) */
-	if (TAILQ_EMPTY(&pci_device_list)) {
-		TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
-	} else {
-		struct rte_pci_device *dev2;
-		int ret;
-
-		TAILQ_FOREACH(dev2, &pci_device_list, next) {
-			ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
-			if (ret > 0)
-				continue;
-
-			if (ret < 0) {
-				TAILQ_INSERT_BEFORE(dev2, dev, next);
-			} else { /* already registered */
-				dev2->kdrv = dev->kdrv;
-				dev2->max_vfs = dev->max_vfs;
-				memmove(dev2->mem_resource, dev->mem_resource,
-					sizeof(dev->mem_resource));
-				free(dev);
-			}
-			return 0;
-		}
-		TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
+	dev2 = pci_find_device(&dev->addr);
+	if (!dev2)
+		pci_add_device(dev);
+	else {
+		dev2->kdrv = dev->kdrv;
+		dev2->max_vfs = dev->max_vfs;
+		memmove(dev2->mem_resource, dev->mem_resource,
+			sizeof(dev->mem_resource));
+		free(dev);
 	}
 
 	return 0;
Signature · 1 line
1.9.1
#3 lore raw

PATCH 3/9 pci: minor cleanup

In reply to David Marchand

Those are the only fields that are explicitly set to 0 while the global dev pointer is set to 0 a few lines before.

---
 lib/librte_eal/linuxapp/eal/eal_pci.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
lib/librte_eal/linuxapp/eal/eal_pci.c +2 -7
index 83eec5d..7bec30f 100644
@@ -308,7 +308,6 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
 	dev->id.subsystem_device_id = (uint16_t)tmp;
 
 	/* get max_vfs */
-	dev->max_vfs = 0;
 	snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
 	if (!access(filename, F_OK) &&
 	    eal_parse_sysfs_value(filename, &tmp) == 0)
@@ -323,12 +322,8 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
 	}
 
 	/* get numa node */
-	snprintf(filename, sizeof(filename), "%s/numa_node",
-		 dirname);
-	if (access(filename, R_OK) != 0) {
-		/* if no NUMA support, set default to 0 */
-		dev->numa_node = 0;
-	} else {
+	snprintf(filename, sizeof(filename), "%s/numa_node", dirname);
+	if (!access(filename, R_OK)) {
 		if (eal_parse_sysfs_value(filename, &tmp) < 0) {
 			free(dev);
 			return -1;
Signature · 1 line
1.9.1
#4 lore raw

PATCH 4/9 pci: rework sysfs parsing for driver

In reply to David Marchand

There is no use for pci_get_kernel_driver_by_path() apart recognising kernel driver and fill kdrv field.
If driver parsing fails, report "none" driver rather than "unknown".

---
 lib/librte_eal/linuxapp/eal/eal_pci.c | 48 ++++++++++++++---------------------
 1 file changed, 19 insertions(+), 29 deletions(-)
lib/librte_eal/linuxapp/eal/eal_pci.c +19 -29
index 7bec30f..00fdee7 100644
@@ -94,32 +94,37 @@ error:
 }
 
 static int
-pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
+pci_parse_sysfs_driver(const char *filename, struct rte_pci_device *dev)
 {
 	int count;
 	char path[PATH_MAX];
 	char *name;
 
-	if (!filename || !dri_name)
-		return -1;
-
 	count = readlink(filename, path, PATH_MAX);
 	if (count >= PATH_MAX)
 		return -1;
 
-	/* For device does not have a driver */
-	if (count < 0)
-		return 1;
+	dev->kdrv = RTE_KDRV_NONE;
 
-	path[count] = '\0';
+	if (count > 0) {
+		dev->kdrv = RTE_KDRV_UNKNOWN;
 
-	name = strrchr(path, '/');
-	if (name) {
-		strncpy(dri_name, name + 1, strlen(name + 1) + 1);
-		return 0;
+		path[count] = '\0';
+		name = strrchr(path, '/');
+		if (name) {
+			name[0] = '\0';
+			name++;
+		}
+
+		if (!strcmp(name, "vfio-pci"))
+			dev->kdrv = RTE_KDRV_VFIO;
+		else if (!strcmp(name, "igb_uio"))
+			dev->kdrv = RTE_KDRV_IGB_UIO;
+		else if (!strcmp(name, "uio_pci_generic"))
+			dev->kdrv = RTE_KDRV_UIO_GENERIC;
 	}
 
-	return -1;
+	return 0;
 }
 
 /* Map pci device */
@@ -260,8 +265,6 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
 	unsigned long tmp;
 	struct rte_pci_device *dev;
 	struct rte_pci_device *dev2;
-	char driver[PATH_MAX];
-	int ret;
 
 	dev = malloc(sizeof(*dev));
 	if (dev == NULL)
@@ -341,25 +344,12 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
 
 	/* parse driver */
 	snprintf(filename, sizeof(filename), "%s/driver", dirname);
-	ret = pci_get_kernel_driver_by_path(filename, driver);
-	if (ret < 0) {
+	if (pci_parse_sysfs_driver(filename, dev) < 0) {
 		RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
 		free(dev);
 		return -1;
 	}
 
-	if (!ret) {
-		if (!strcmp(driver, "vfio-pci"))
-			dev->kdrv = RTE_KDRV_VFIO;
-		else if (!strcmp(driver, "igb_uio"))
-			dev->kdrv = RTE_KDRV_IGB_UIO;
-		else if (!strcmp(driver, "uio_pci_generic"))
-			dev->kdrv = RTE_KDRV_UIO_GENERIC;
-		else
-			dev->kdrv = RTE_KDRV_UNKNOWN;
-	} else
-		dev->kdrv = RTE_KDRV_UNKNOWN;
-
 	dev2 = pci_find_device(&dev->addr);
 	if (!dev2)
 		pci_add_device(dev);
Signature · 1 line
1.9.1
#5 lore raw

PATCH 5/9 pci: factorize probe/detach code

In reply to David Marchand

Move pci id matching to a helper and reuse it in probe and detach functions.

---
 lib/librte_eal/common/eal_common_pci.c | 67 ++++++++++++----------------------
 1 file changed, 23 insertions(+), 44 deletions(-)
lib/librte_eal/common/eal_common_pci.c +23 -44
index 2528775..44549f7 100644
@@ -174,14 +174,10 @@ pci_add_device(struct rte_pci_device *dev)
 	return 0;
 }
 
-/*
- * If vendor/device ID match, call the devinit() function of the
- * driver.
- */
 static int
-rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
+pci_driver_supports_device(const struct rte_pci_driver *dr,
+			   const struct rte_pci_device *dev)
 {
-	int ret;
 	const struct rte_pci_id *id_table;
 
 	for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
@@ -200,6 +196,20 @@ rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *d
 				id_table->subsystem_device_id != PCI_ANY_ID)
 			continue;
 
+		return 1;
+	}
+	return 0;
+}
+
+/*
+ * If vendor/device ID match, call the devinit() function of the
+ * driver.
+ */
+static int
+rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr,
+			     struct rte_pci_device *dev)
+{
+	int ret;
 		struct rte_pci_addr *loc = &dev->addr;
 
 		RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
@@ -240,9 +250,6 @@ rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *d
 
 		/* call the driver devinit() function */
 		return dr->devinit(dr, dev);
-	}
-	/* return positive value if driver is not found */
-	return 1;
 }
 
 /*
@@ -253,27 +260,6 @@ static int
 rte_eal_pci_detach_dev(struct rte_pci_driver *dr,
 		struct rte_pci_device *dev)
 {
-	const struct rte_pci_id *id_table;
-
-	if ((dr == NULL) || (dev == NULL))
-		return -EINVAL;
-
-	for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
-
-		/* check if device's identifiers match the driver's ones */
-		if (id_table->vendor_id != dev->id.vendor_id &&
-				id_table->vendor_id != PCI_ANY_ID)
-			continue;
-		if (id_table->device_id != dev->id.device_id &&
-				id_table->device_id != PCI_ANY_ID)
-			continue;
-		if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
-				id_table->subsystem_vendor_id != PCI_ANY_ID)
-			continue;
-		if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
-				id_table->subsystem_device_id != PCI_ANY_ID)
-			continue;
-
 		struct rte_pci_addr *loc = &dev->addr;
 
 		RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
@@ -294,10 +280,6 @@ rte_eal_pci_detach_dev(struct rte_pci_driver *dr,
 			pci_unmap_device(dev);
 
 		return 0;
-	}
-
-	/* return positive value if driver is not found */
-	return 1;
 }
 
 /*
@@ -311,16 +293,16 @@ pci_probe_all_drivers(struct rte_pci_device *dev)
 	struct rte_pci_driver *dr = NULL;
 	int rc = 0;
 
-	if (dev == NULL)
-		return -1;
-
 	TAILQ_FOREACH(dr, &pci_driver_list, next) {
+		if (!pci_driver_supports_device(dr, dev))
+			continue;
+
 		rc = rte_eal_pci_probe_one_driver(dr, dev);
 		if (rc < 0)
 			/* negative value is an error */
 			return -1;
 		if (rc > 0)
-			/* positive value means driver not found */
+			/* positive value means device is blacklisted */
 			continue;
 		return 0;
 	}
@@ -338,17 +320,14 @@ pci_detach_all_drivers(struct rte_pci_device *dev)
 	struct rte_pci_driver *dr = NULL;
 	int rc = 0;
 
-	if (dev == NULL)
-		return -1;
-
 	TAILQ_FOREACH(dr, &pci_driver_list, next) {
+		if (!pci_driver_supports_device(dr, dev))
+			continue;
+
 		rc = rte_eal_pci_detach_dev(dr, dev);
 		if (rc < 0)
 			/* negative value is an error */
 			return -1;
-		if (rc > 0)
-			/* positive value means driver not found */
-			continue;
 		return 0;
 	}
 	return 1;
Signature · 1 line
1.9.1
#6 lore raw

PATCH 6/9 pci: cosmetic change

In reply to David Marchand

Indent previous commit, rename functions (internal and local functions do not need rte_eal_ prefix).

---
 lib/librte_eal/common/eal_common_pci.c | 106 ++++++++++++++++-----------------
 1 file changed, 52 insertions(+), 54 deletions(-)
lib/librte_eal/common/eal_common_pci.c +52 -54
index 44549f7..769c42e 100644
@@ -206,50 +206,49 @@ pci_driver_supports_device(const struct rte_pci_driver *dr,
  * driver.
  */
 static int
-rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr,
-			     struct rte_pci_device *dev)
+pci_probe_device(struct rte_pci_driver *dr, struct rte_pci_device *dev)
 {
 	int ret;
-		struct rte_pci_addr *loc = &dev->addr;
+	struct rte_pci_addr *loc = &dev->addr;
 
-		RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
-				loc->domain, loc->bus, loc->devid, loc->function,
-				dev->numa_node);
+	RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
+		loc->domain, loc->bus, loc->devid, loc->function,
+		dev->numa_node);
 
-		RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
-				dev->id.device_id, dr->name);
+	RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
+		dev->id.device_id, dr->name);
 
-		/* no initialization when blacklisted, return without error */
-		if (dev->devargs != NULL &&
-			dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
-			RTE_LOG(DEBUG, EAL, "  Device is blacklisted, not initializing\n");
-			return 1;
-		}
+	/* no initialization when blacklisted, return without error */
+	if (dev->devargs != NULL &&
+		dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
+		RTE_LOG(DEBUG, EAL, "  Device is blacklisted, not initializing\n");
+		return 1;
+	}
 
-		if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
+	if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
 #ifdef RTE_PCI_CONFIG
-			/*
-			 * Set PCIe config space for high performance.
-			 * Return value can be ignored.
-			 */
-			pci_config_space_set(dev);
+		/*
+		 * Set PCIe config space for high performance.
+		 * Return value can be ignored.
+		 */
+		pci_config_space_set(dev);
 #endif
-			/* map resources for devices that use igb_uio */
-			ret = pci_map_device(dev);
-			if (ret != 0)
-				return ret;
-		} else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
-				rte_eal_process_type() == RTE_PROC_PRIMARY) {
-			/* unbind current driver */
-			if (pci_unbind_kernel_driver(dev) < 0)
-				return -1;
-		}
-
-		/* reference driver structure */
-		dev->driver = dr;
-
-		/* call the driver devinit() function */
-		return dr->devinit(dr, dev);
+		/* map resources for devices that use igb_uio */
+		ret = pci_map_device(dev);
+		if (ret != 0)
+			return ret;
+	} else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
+		   rte_eal_process_type() == RTE_PROC_PRIMARY) {
+		/* unbind current driver */
+		if (pci_unbind_kernel_driver(dev) < 0)
+			return -1;
+	}
+
+	/* reference driver structure */
+	dev->driver = dr;
+
+	/* call the driver devinit() function */
+	return dr->devinit(dr, dev);
 }
 
 /*
@@ -257,29 +256,28 @@ rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr,
  * driver.
  */
 static int
-rte_eal_pci_detach_dev(struct rte_pci_driver *dr,
-		struct rte_pci_device *dev)
+pci_detach_device(struct rte_pci_driver *dr, struct rte_pci_device *dev)
 {
-		struct rte_pci_addr *loc = &dev->addr;
+	struct rte_pci_addr *loc = &dev->addr;
 
-		RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
-				loc->domain, loc->bus, loc->devid,
-				loc->function, dev->numa_node);
+	RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
+		loc->domain, loc->bus, loc->devid,
+		loc->function, dev->numa_node);
 
-		RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
-				dev->id.device_id, dr->name);
+	RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
+		dev->id.device_id, dr->name);
 
-		if (dr->devuninit && (dr->devuninit(dev) < 0))
-			return -1;	/* negative value is an error */
+	if (dr->devuninit && (dr->devuninit(dev) < 0))
+		return -1;	/* negative value is an error */
 
-		/* clear driver structure */
-		dev->driver = NULL;
+	/* clear driver structure */
+	dev->driver = NULL;
 
-		if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
-			/* unmap resources for devices that use igb_uio */
-			pci_unmap_device(dev);
+	if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
+		/* unmap resources for devices that use igb_uio */
+		pci_unmap_device(dev);
 
-		return 0;
+	return 0;
 }
 
 /*
@@ -297,7 +295,7 @@ pci_probe_all_drivers(struct rte_pci_device *dev)
 		if (!pci_driver_supports_device(dr, dev))
 			continue;
 
-		rc = rte_eal_pci_probe_one_driver(dr, dev);
+		rc = pci_probe_device(dr, dev);
 		if (rc < 0)
 			/* negative value is an error */
 			return -1;
@@ -324,7 +322,7 @@ pci_detach_all_drivers(struct rte_pci_device *dev)
 		if (!pci_driver_supports_device(dr, dev))
 			continue;
 
-		rc = rte_eal_pci_detach_dev(dr, dev);
+		rc = pci_detach_device(dr, dev);
 		if (rc < 0)
 			/* negative value is an error */
 			return -1;
Signature · 1 line
1.9.1
#7 lore raw

PATCH 7/9 pci: factorize driver search

In reply to David Marchand

Same idea as a few commits before, no need to implement the same logic in probe and detach functions.
Here, the driver matching is done by a helper, then probe / detach functions are called respectively.

---
 lib/librte_eal/common/eal_common_pci.c | 96 ++++++++++++----------------------
 1 file changed, 34 insertions(+), 62 deletions(-)
lib/librte_eal/common/eal_common_pci.c +34 -62
index 769c42e..a1efd57 100644
@@ -280,55 +280,16 @@ pci_detach_device(struct rte_pci_driver *dr, struct rte_pci_device *dev)
 	return 0;
 }
 
-/*
- * If vendor/device ID match, call the devinit() function of all
- * registered driver for the given device. Return -1 if initialization
- * failed, return 1 if no driver is found for this device.
- */
-static int
-pci_probe_all_drivers(struct rte_pci_device *dev)
-{
-	struct rte_pci_driver *dr = NULL;
-	int rc = 0;
-
-	TAILQ_FOREACH(dr, &pci_driver_list, next) {
-		if (!pci_driver_supports_device(dr, dev))
-			continue;
-
-		rc = pci_probe_device(dr, dev);
-		if (rc < 0)
-			/* negative value is an error */
-			return -1;
-		if (rc > 0)
-			/* positive value means device is blacklisted */
-			continue;
-		return 0;
-	}
-	return 1;
-}
-
-/*
- * If vendor/device ID match, call the devuninit() function of all
- * registered driver for the given device. Return -1 if initialization
- * failed, return 1 if no driver is found for this device.
- */
-static int
-pci_detach_all_drivers(struct rte_pci_device *dev)
+static struct rte_pci_driver *
+pci_find_driver(struct rte_pci_device *dev)
 {
-	struct rte_pci_driver *dr = NULL;
-	int rc = 0;
+	struct rte_pci_driver *dr;
 
 	TAILQ_FOREACH(dr, &pci_driver_list, next) {
-		if (!pci_driver_supports_device(dr, dev))
-			continue;
-
-		rc = pci_detach_device(dr, dev);
-		if (rc < 0)
-			/* negative value is an error */
-			return -1;
-		return 0;
+		if (pci_driver_supports_device(dr, dev))
+			break;
 	}
-	return 1;
+	return dr;
 }
 
 /*
@@ -338,8 +299,8 @@ pci_detach_all_drivers(struct rte_pci_device *dev)
 int
 rte_eal_pci_probe_one(const struct rte_pci_addr *addr)
 {
-	struct rte_pci_device *dev = NULL;
-	int ret = 0;
+	struct rte_pci_device *dev;
+	struct rte_pci_driver *dr;
 
 	if (addr == NULL)
 		return -1;
@@ -348,8 +309,11 @@ rte_eal_pci_probe_one(const struct rte_pci_addr *addr)
 	if (!dev)
 		goto err_return;
 
-	ret = pci_probe_all_drivers(dev);
-	if (ret < 0)
+	dr = pci_find_driver(dev);
+	if (!dr)
+		goto err_return;
+
+	if (pci_probe_device(dr, dev) < 0)
 		goto err_return;
 
 	return 0;
@@ -367,8 +331,8 @@ err_return:
 int
 rte_eal_pci_detach(const struct rte_pci_addr *addr)
 {
-	struct rte_pci_device *dev = NULL;
-	int ret = 0;
+	struct rte_pci_device *dev;
+	struct rte_pci_driver *dr;
 
 	if (addr == NULL)
 		return -1;
@@ -377,8 +341,11 @@ rte_eal_pci_detach(const struct rte_pci_addr *addr)
 	if (!dev)
 		goto err_return;
 
-	ret = pci_detach_all_drivers(dev);
-	if (ret < 0)
+	dr = pci_find_driver(dev);
+	if (!dr)
+		goto err_return;
+
+	if (pci_detach_device(dr, dev) < 0)
 		goto err_return;
 
 	TAILQ_REMOVE(&pci_device_list, dev, next);
@@ -399,28 +366,33 @@ err_return:
 int
 rte_eal_pci_probe(void)
 {
-	struct rte_pci_device *dev = NULL;
+	struct rte_pci_device *dev;
+	struct rte_pci_driver *dr;
 	struct rte_devargs *devargs;
 	int probe_all = 0;
-	int ret = 0;
 
 	if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) == 0)
 		probe_all = 1;
 
 	TAILQ_FOREACH(dev, &pci_device_list, next) {
 
+		/* no driver available */
+		dr = pci_find_driver(dev);
+		if (!dr)
+			continue;
+
 		/* set devargs in PCI structure */
 		devargs = pci_devargs_lookup(dev);
 		if (devargs != NULL)
 			dev->devargs = devargs;
 
-		/* probe all or only whitelisted devices */
-		if (probe_all)
-			ret = pci_probe_all_drivers(dev);
-		else if (devargs != NULL &&
-			devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
-			ret = pci_probe_all_drivers(dev);
-		if (ret < 0)
+		/* skip if not probing all and device is not whitelisted */
+		if (!probe_all &&
+		    (devargs == NULL ||
+		     devargs->type != RTE_DEVTYPE_WHITELISTED_PCI))
+			continue;
+
+		if (pci_probe_device(dr, dev) < 0)
 			rte_exit(EXIT_FAILURE, "Requested device " PCI_PRI_FMT
 				 " cannot be used\n", dev->addr.domain, dev->addr.bus,
 				 dev->addr.devid, dev->addr.function);
Signature · 1 line
1.9.1
#8 lore raw

PATCH 8/9 pci: remove driver lookup from detach

In reply to David Marchand

A device is linked to a driver at probe time.
When detaching, we should call this same driver detach() function and no need for a driver lookup.

---
 lib/librte_eal/common/eal_common_pci.c | 25 ++++++++++---------------
 1 file changed, 10 insertions(+), 15 deletions(-)
lib/librte_eal/common/eal_common_pci.c +10 -15
index a1efd57..71222a5 100644
@@ -256,16 +256,9 @@ pci_probe_device(struct rte_pci_driver *dr, struct rte_pci_device *dev)
  * driver.
  */
 static int
-pci_detach_device(struct rte_pci_driver *dr, struct rte_pci_device *dev)
+pci_detach_device(struct rte_pci_device *dev)
 {
-	struct rte_pci_addr *loc = &dev->addr;
-
-	RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
-		loc->domain, loc->bus, loc->devid,
-		loc->function, dev->numa_node);
-
-	RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
-		dev->id.device_id, dr->name);
+	struct rte_pci_driver *dr = dev->driver;
 
 	if (dr->devuninit && (dr->devuninit(dev) < 0))
 		return -1;	/* negative value is an error */
@@ -332,20 +325,22 @@ int
 rte_eal_pci_detach(const struct rte_pci_addr *addr)
 {
 	struct rte_pci_device *dev;
-	struct rte_pci_driver *dr;
 
 	if (addr == NULL)
 		return -1;
 
 	dev = pci_find_device(addr);
-	if (!dev)
+	if (!dev || !dev->driver)
 		goto err_return;
 
-	dr = pci_find_driver(dev);
-	if (!dr)
-		goto err_return;
+	RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
+		dev->addr.domain, dev->addr.bus, dev->addr.devid,
+		dev->addr.function, dev->numa_node);
+
+	RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
+		dev->id.device_id, dev->driver->name);
 
-	if (pci_detach_device(dr, dev) < 0)
+	if (pci_detach_device(dev) < 0)
 		goto err_return;
 
 	TAILQ_REMOVE(&pci_device_list, dev, next);
Signature · 1 line
1.9.1
#9 lore raw

PATCH 9/9 pci: blacklist only in global probe function

In reply to David Marchand

Move blacklist evaluation to rte_eal_pci_probe().
This way, rte_eal_pci_probe_one() (used by hotplug when attaching) now accepts to probe devices that were blacklisted initially.

A new debug trace is added when skipping a device not part of a given whitelist.

---
 lib/librte_eal/common/eal_common_pci.c | 56 ++++++++++++++++++++--------------
 1 file changed, 33 insertions(+), 23 deletions(-)
lib/librte_eal/common/eal_common_pci.c +33 -23
index 71222a5..52d4594 100644
@@ -209,21 +209,6 @@ static int
 pci_probe_device(struct rte_pci_driver *dr, struct rte_pci_device *dev)
 {
 	int ret;
-	struct rte_pci_addr *loc = &dev->addr;
-
-	RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
-		loc->domain, loc->bus, loc->devid, loc->function,
-		dev->numa_node);
-
-	RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
-		dev->id.device_id, dr->name);
-
-	/* no initialization when blacklisted, return without error */
-	if (dev->devargs != NULL &&
-		dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
-		RTE_LOG(DEBUG, EAL, "  Device is blacklisted, not initializing\n");
-		return 1;
-	}
 
 	if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
 #ifdef RTE_PCI_CONFIG
@@ -306,6 +291,14 @@ rte_eal_pci_probe_one(const struct rte_pci_addr *addr)
 	if (!dr)
 		goto err_return;
 
+	RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
+		dev->addr.domain, dev->addr.bus,
+		dev->addr.devid, dev->addr.function,
+		dev->numa_node);
+
+	RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
+		dev->id.device_id, dr->name);
+
 	if (pci_probe_device(dr, dev) < 0)
 		goto err_return;
 
@@ -364,10 +357,8 @@ rte_eal_pci_probe(void)
 	struct rte_pci_device *dev;
 	struct rte_pci_driver *dr;
 	struct rte_devargs *devargs;
-	int probe_all = 0;
-
-	if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) == 0)
-		probe_all = 1;
+	int whitelist = rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI);
+	int blacklist = rte_eal_devargs_type_count(RTE_DEVTYPE_BLACKLISTED_PCI);
 
 	TAILQ_FOREACH(dev, &pci_device_list, next) {
 
@@ -381,11 +372,30 @@ rte_eal_pci_probe(void)
 		if (devargs != NULL)
 			dev->devargs = devargs;
 
-		/* skip if not probing all and device is not whitelisted */
-		if (!probe_all &&
-		    (devargs == NULL ||
-		     devargs->type != RTE_DEVTYPE_WHITELISTED_PCI))
+		RTE_LOG(DEBUG, EAL,
+			"PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
+			dev->addr.domain, dev->addr.bus,
+			dev->addr.devid, dev->addr.function,
+			dev->numa_node);
+
+		RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n",
+			dev->id.vendor_id, dev->id.device_id, dr->name);
+
+		/*
+		 * We want to probe this device when:
+		 * - there is no whitelist/blacklist,
+		 * - there is a whitelist, and device is part of it,
+		 * - there is a blacklist, and device is not part of it.
+		 */
+		if (whitelist && !devargs) {
+			RTE_LOG(DEBUG, EAL, "  Device is not whitelisted, not initializing\n");
+			continue;
+		}
+
+		if (blacklist && devargs) {
+			RTE_LOG(DEBUG, EAL, "  Device is blacklisted, not initializing\n");
 			continue;
+		}
 
 		if (pci_probe_device(dr, dev) < 0)
 			rte_exit(EXIT_FAILURE, "Requested device " PCI_PRI_FMT
Signature · 1 line
1.9.1
#10 lore raw
In reply to David Marchand
Quoted 7 lines · David Marchand

The 4th patch introduces a change in linux eal.
Before, if a pci device was bound to no kernel driver, eal would set kdrv to "unknown". With this change, kdrv is set to "none".
This might make it possible to avoid the old issue of virtio devices being used by dpdk while still bound to kernel driver reported by Franck B.. I'll let virtio guys look at this.
At the very least, it makes more sense to me.

Ok, actually, I had forgotten that Huawei had already sent a similar change [1]. So I suppose this patch commitlog is wrong, but the patch itself is still worth for the cleanup.

Thomas, I suppose you will integrate Huawei patches first. Then I will rebase and fix the commitlog.

[1] http://dpdk.org/dev/patchwork/patch/9718/

Signature · 1 line
David Marchand
#11 lore raw

v2 cover pci cleanup and blacklist rework

In reply to David Marchand

Before 2.2.0 release, while preparing for more changes in eal (and fixing a problem reported by Roger M. [1]), I came up with this (part of) patchset that tries to make the pci code more compact and easier to read.

I ended up introducing some hooks in the pci layer to customize pci blacklist / whitelist handling and make it possible to automatically bind / unbind pci devices to igb_uio (or equivalent) when attaching a device.

I am still not really happy:
- the pci blacklist / whitelist makes me think we should let the application tell eal which resources to use and get rid of the unconditional pci scan code, which means removing rte_eal_pci_probe() from rte_eal_init(), and remove rte_eal_dev_init() for vdevs,
- the more I look at this, the more I think automatic bind / unbind for pci devices should be called from the pmd context. The drivers know best what they require and what they want to do with the resources passed by the eal (see the drv_flags / RTE_KDRV_NONE / rte_eal_pci_map_device stuff for virtio pmd).
This behaviour would still be optional, on a per-device basis.

So, I think that these hooks are not that good of an idea and I kept them private for now, but anyway, sending this for comments.

Changes since v1:
- split the initial patchset. This current patchset now depends on
[2] sent separately which should be applied first,
- introduced hooks in pci common code,
- implemented automatic bind / unbind for "uio" pci devices

[1] http://dpdk.org/ml/archives/dev/2015-November/028140.html
[2] http://dpdk.org/ml/archives/dev/2016-January/032387.html

Signature · 24 lines
David Marchand

David Marchand (9):
  pci: add internal device list helpers
  pci/linux: minor cleanup
  pci/linux: rework sysfs parsing for driver
  pci: factorize probe/detach code
  pci: cosmetic change
  pci: factorize driver search
  pci: remove driver lookup from detach
  pci: implement blacklist using a hook
  pci: implement automatic bind/unbind

 lib/librte_eal/bsdapp/eal/eal_pci.c        |  59 ++--
 lib/librte_eal/common/eal_common_options.c |   8 +
 lib/librte_eal/common/eal_common_pci.c     | 438 ++++++++++++++++++-----------
 lib/librte_eal/common/eal_options.h        |   2 +
 lib/librte_eal/common/eal_private.h        |  64 +++++
 lib/librte_eal/common/include/rte_pci.h    |   7 +-
 lib/librte_eal/linuxapp/eal/eal_pci.c      | 301 ++++++++++++++++----
 7 files changed, 627 insertions(+), 252 deletions(-)

-- 
1.9.1
#12 lore raw

PATCH v2 1/9 pci: add internal device list helpers

Remove duplicate code by moving this to helpers in common.

---
Changes since v1:
- moved helpers to eal_private.h

---
 lib/librte_eal/bsdapp/eal/eal_pci.c    | 34 ++++-----------
 lib/librte_eal/common/eal_common_pci.c | 76 ++++++++++++++++++++++++----------
 lib/librte_eal/common/eal_private.h    | 26 ++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci.c  | 34 +++++----------
 4 files changed, 99 insertions(+), 71 deletions(-)
lib/librte_eal/bsdapp/eal/eal_pci.c +9 -25
index 5dd89e3..e95249b 100644
@@ -247,6 +247,7 @@ static int
 pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
 {
 	struct rte_pci_device *dev;
+	struct rte_pci_device *dev2;
 	struct pci_bar_io bar;
 	unsigned i, max;
 
@@ -311,32 +312,15 @@ pci_scan_one(int dev_pci_fd, struct pci_conf *conf)
 		dev->mem_resource[i].phys_addr = bar.pbi_base & ~((uint64_t)0xf);
 	}
 
-	/* device is valid, add in list (sorted) */
-	if (TAILQ_EMPTY(&pci_device_list)) {
-		TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
-	}
+	dev2 = pci_find_device(&dev->addr);
+	if (!dev2)
+		pci_add_device(dev);
 	else {
-		struct rte_pci_device *dev2 = NULL;
-		int ret;
-
-		TAILQ_FOREACH(dev2, &pci_device_list, next) {
-			ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
-			if (ret > 0)
-				continue;
-			else if (ret < 0) {
-				TAILQ_INSERT_BEFORE(dev2, dev, next);
-				return 0;
-			} else { /* already registered */
-				dev2->kdrv = dev->kdrv;
-				dev2->max_vfs = dev->max_vfs;
-				memmove(dev2->mem_resource,
-					dev->mem_resource,
-					sizeof(dev->mem_resource));
-				free(dev);
-				return 0;
-			}
-		}
-		TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
+		dev2->kdrv = dev->kdrv;
+		dev2->max_vfs = dev->max_vfs;
+		memmove(dev2->mem_resource, dev->mem_resource,
+			sizeof(dev->mem_resource));
+		free(dev);
 	}
 
 	return 0;
lib/librte_eal/common/eal_common_pci.c +54 -22

99 lines not on this page · raw message

lib/librte_eal/common/eal_private.h +26 -0
index ed1903f..e44a448 100644
@@ -155,6 +155,32 @@ struct rte_pci_driver;
 struct rte_pci_device;
 
 /**
+ * Add a pci device to sorted pci device list
+ *
+ * This function is private to EAL.
+ *
+ * @param dev
+ *	The PCI device object.
+ * @return
+ *   - 0 on success.
+ *   - negative on error.
+ */
+int pci_add_device(struct rte_pci_device *dev);
+
+/**
+ * Find a pci device object based on a PCI Bus-Device-Function address.
+ *
+ * This function is private to EAL.
+ *
+ * @param addr
+ *	The PCI Bus-Device-Function address to look for
+ * @return
+ *   - pointer to the object,
+ *   - NULL when not found.
+ */
+struct rte_pci_device *pci_find_device(const struct rte_pci_addr *addr);
+
+/**
  * Refresh a pci device object by asking the kernel for the latest information.
  *
  * This function is private to EAL.
lib/librte_eal/linuxapp/eal/eal_pci.c +10 -24
index 4fe8b60..d9d6de7 100644
@@ -259,6 +259,7 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
 	char filename[PATH_MAX];
 	unsigned long tmp;
 	struct rte_pci_device *dev;
+	struct rte_pci_device *dev2;
 	char driver[PATH_MAX];
 	int ret;
 
@@ -364,30 +365,15 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
 	} else
 		dev->kdrv = RTE_KDRV_UNKNOWN;
 
-	/* device is valid, add in list (sorted) */
-	if (TAILQ_EMPTY(&pci_device_list)) {
-		TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
-	} else {
-		struct rte_pci_device *dev2;
-		int ret;
-
-		TAILQ_FOREACH(dev2, &pci_device_list, next) {
-			ret = rte_eal_compare_pci_addr(&dev->addr, &dev2->addr);
-			if (ret > 0)
-				continue;
-
-			if (ret < 0) {
-				TAILQ_INSERT_BEFORE(dev2, dev, next);
-			} else { /* already registered */
-				dev2->kdrv = dev->kdrv;
-				dev2->max_vfs = dev->max_vfs;
-				memmove(dev2->mem_resource, dev->mem_resource,
-					sizeof(dev->mem_resource));
-				free(dev);
-			}
-			return 0;
-		}
-		TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
+	dev2 = pci_find_device(&dev->addr);
+	if (!dev2)
+		pci_add_device(dev);
+	else {
+		dev2->kdrv = dev->kdrv;
+		dev2->max_vfs = dev->max_vfs;
+		memmove(dev2->mem_resource, dev->mem_resource,
+			sizeof(dev->mem_resource));
+		free(dev);
 	}
 
 	return 0;
Signature · 1 line
1.9.1
#13 lore raw

PATCH v2 2/9 pci/linux: minor cleanup

In reply to David Marchand

Those are the only fields that are explicitly set to 0 while the global dev pointer is set to 0 a few lines before.

---
 lib/librte_eal/linuxapp/eal/eal_pci.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
lib/librte_eal/linuxapp/eal/eal_pci.c +2 -7
index d9d6de7..6751b48 100644
@@ -308,7 +308,6 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
 	dev->id.subsystem_device_id = (uint16_t)tmp;
 
 	/* get max_vfs */
-	dev->max_vfs = 0;
 	snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
 	if (!access(filename, F_OK) &&
 	    eal_parse_sysfs_value(filename, &tmp) == 0)
@@ -323,12 +322,8 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
 	}
 
 	/* get numa node */
-	snprintf(filename, sizeof(filename), "%s/numa_node",
-		 dirname);
-	if (access(filename, R_OK) != 0) {
-		/* if no NUMA support, set default to 0 */
-		dev->numa_node = 0;
-	} else {
+	snprintf(filename, sizeof(filename), "%s/numa_node", dirname);
+	if (!access(filename, R_OK)) {
 		if (eal_parse_sysfs_value(filename, &tmp) < 0) {
 			free(dev);
 			return -1;
Signature · 1 line
1.9.1
#14 lore raw

PATCH v2 3/9 pci/linux: rework sysfs parsing for driver

In reply to David Marchand

There is no use for pci_get_kernel_driver_by_path() apart recognising kernel driver and fill kdrv field.

---
Changes since v1:
- updated the commitlog, Huawei already did the "unknown" -> "none" change, so this patch ends up just refactoring code

---
 lib/librte_eal/linuxapp/eal/eal_pci.c | 48 ++++++++++++++---------------------
 1 file changed, 19 insertions(+), 29 deletions(-)
lib/librte_eal/linuxapp/eal/eal_pci.c +19 -29
index 6751b48..c3118fc 100644
@@ -94,32 +94,37 @@ error:
 }
 
 static int
-pci_get_kernel_driver_by_path(const char *filename, char *dri_name)
+pci_parse_sysfs_driver(const char *filename, struct rte_pci_device *dev)
 {
 	int count;
 	char path[PATH_MAX];
 	char *name;
 
-	if (!filename || !dri_name)
-		return -1;
-
 	count = readlink(filename, path, PATH_MAX);
 	if (count >= PATH_MAX)
 		return -1;
 
-	/* For device does not have a driver */
-	if (count < 0)
-		return 1;
+	dev->kdrv = RTE_KDRV_NONE;
 
-	path[count] = '\0';
+	if (count > 0) {
+		dev->kdrv = RTE_KDRV_UNKNOWN;
 
-	name = strrchr(path, '/');
-	if (name) {
-		strncpy(dri_name, name + 1, strlen(name + 1) + 1);
-		return 0;
+		path[count] = '\0';
+		name = strrchr(path, '/');
+		if (name) {
+			name[0] = '\0';
+			name++;
+		}
+
+		if (!strcmp(name, "vfio-pci"))
+			dev->kdrv = RTE_KDRV_VFIO;
+		else if (!strcmp(name, "igb_uio"))
+			dev->kdrv = RTE_KDRV_IGB_UIO;
+		else if (!strcmp(name, "uio_pci_generic"))
+			dev->kdrv = RTE_KDRV_UIO_GENERIC;
 	}
 
-	return -1;
+	return 0;
 }
 
 /* Map pci device */
@@ -260,8 +265,6 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
 	unsigned long tmp;
 	struct rte_pci_device *dev;
 	struct rte_pci_device *dev2;
-	char driver[PATH_MAX];
-	int ret;
 
 	dev = malloc(sizeof(*dev));
 	if (dev == NULL)
@@ -341,25 +344,12 @@ pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
 
 	/* parse driver */
 	snprintf(filename, sizeof(filename), "%s/driver", dirname);
-	ret = pci_get_kernel_driver_by_path(filename, driver);
-	if (ret < 0) {
+	if (pci_parse_sysfs_driver(filename, dev) < 0) {
 		RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
 		free(dev);
 		return -1;
 	}
 
-	if (!ret) {
-		if (!strcmp(driver, "vfio-pci"))
-			dev->kdrv = RTE_KDRV_VFIO;
-		else if (!strcmp(driver, "igb_uio"))
-			dev->kdrv = RTE_KDRV_IGB_UIO;
-		else if (!strcmp(driver, "uio_pci_generic"))
-			dev->kdrv = RTE_KDRV_UIO_GENERIC;
-		else
-			dev->kdrv = RTE_KDRV_UNKNOWN;
-	} else
-		dev->kdrv = RTE_KDRV_UNKNOWN;
-
 	dev2 = pci_find_device(&dev->addr);
 	if (!dev2)
 		pci_add_device(dev);
Signature · 1 line
1.9.1
#16 lore raw

PATCH v2 5/9 pci: cosmetic change

In reply to David Marchand

Indent previous commit, rename functions (internal and local functions do not need rte_eal_ prefix).

---
 lib/librte_eal/common/eal_common_pci.c | 106 ++++++++++++++++-----------------
 1 file changed, 52 insertions(+), 54 deletions(-)
lib/librte_eal/common/eal_common_pci.c +52 -54
index a6791c1..fd88305 100644
@@ -206,50 +206,49 @@ pci_driver_supports_device(const struct rte_pci_driver *dr,
  * driver.
  */
 static int
-rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr,
-			     struct rte_pci_device *dev)
+pci_probe_device(struct rte_pci_driver *dr, struct rte_pci_device *dev)
 {
 	int ret;
-		struct rte_pci_addr *loc = &dev->addr;
+	struct rte_pci_addr *loc = &dev->addr;
 
-		RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
-				loc->domain, loc->bus, loc->devid, loc->function,
-				dev->numa_node);
+	RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
+		loc->domain, loc->bus, loc->devid, loc->function,
+		dev->numa_node);
 
-		RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
-				dev->id.device_id, dr->name);
+	RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
+		dev->id.device_id, dr->name);
 
-		/* no initialization when blacklisted, return without error */
-		if (dev->devargs != NULL &&
-			dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
-			RTE_LOG(DEBUG, EAL, "  Device is blacklisted, not initializing\n");
-			return 1;
-		}
+	/* no initialization when blacklisted, return without error */
+	if (dev->devargs != NULL &&
+		dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
+		RTE_LOG(DEBUG, EAL, "  Device is blacklisted, not initializing\n");
+		return 1;
+	}
 
-		if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
+	if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
 #ifdef RTE_PCI_CONFIG
-			/*
-			 * Set PCIe config space for high performance.
-			 * Return value can be ignored.
-			 */
-			pci_config_space_set(dev);
+		/*
+		 * Set PCIe config space for high performance.
+		 * Return value can be ignored.
+		 */
+		pci_config_space_set(dev);
 #endif
-			/* map resources for devices that use igb_uio */
-			ret = pci_map_device(dev);
-			if (ret != 0)
-				return ret;
-		} else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
-				rte_eal_process_type() == RTE_PROC_PRIMARY) {
-			/* unbind current driver */
-			if (pci_unbind_kernel_driver(dev) < 0)
-				return -1;
-		}
-
-		/* reference driver structure */
-		dev->driver = dr;
-
-		/* call the driver devinit() function */
-		return dr->devinit(dr, dev);
+		/* map resources for devices that use igb_uio */
+		ret = pci_map_device(dev);
+		if (ret != 0)
+			return ret;
+	} else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
+		   rte_eal_process_type() == RTE_PROC_PRIMARY) {
+		/* unbind current driver */
+		if (pci_unbind_kernel_driver(dev) < 0)
+			return -1;
+	}
+
+	/* reference driver structure */
+	dev->driver = dr;
+
+	/* call the driver devinit() function */
+	return dr->devinit(dr, dev);
 }
 
 /*
@@ -257,29 +256,28 @@ rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr,
  * driver.
  */
 static int
-rte_eal_pci_detach_dev(struct rte_pci_driver *dr,
-		struct rte_pci_device *dev)
+pci_detach_device(struct rte_pci_driver *dr, struct rte_pci_device *dev)
 {
-		struct rte_pci_addr *loc = &dev->addr;
+	struct rte_pci_addr *loc = &dev->addr;
 
-		RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
-				loc->domain, loc->bus, loc->devid,
-				loc->function, dev->numa_node);
+	RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
+		loc->domain, loc->bus, loc->devid,
+		loc->function, dev->numa_node);
 
-		RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
-				dev->id.device_id, dr->name);
+	RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
+		dev->id.device_id, dr->name);
 
-		if (dr->devuninit && (dr->devuninit(dev) < 0))
-			return -1;	/* negative value is an error */
+	if (dr->devuninit && (dr->devuninit(dev) < 0))
+		return -1;	/* negative value is an error */
 
-		/* clear driver structure */
-		dev->driver = NULL;
+	/* clear driver structure */
+	dev->driver = NULL;
 
-		if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
-			/* unmap resources for devices that use igb_uio */
-			pci_unmap_device(dev);
+	if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
+		/* unmap resources for devices that use igb_uio */
+		pci_unmap_device(dev);
 
-		return 0;
+	return 0;
 }
 
 /*
@@ -297,7 +295,7 @@ pci_probe_all_drivers(struct rte_pci_device *dev)
 		if (!pci_driver_supports_device(dr, dev))
 			continue;
 
-		rc = rte_eal_pci_probe_one_driver(dr, dev);
+		rc = pci_probe_device(dr, dev);
 		if (rc < 0)
 			/* negative value is an error */
 			return -1;
@@ -324,7 +322,7 @@ pci_detach_all_drivers(struct rte_pci_device *dev)
 		if (!pci_driver_supports_device(dr, dev))
 			continue;
 
-		rc = rte_eal_pci_detach_dev(dr, dev);
+		rc = pci_detach_device(dr, dev);
 		if (rc < 0)
 			/* negative value is an error */
 			return -1;
Signature · 1 line
1.9.1
#17 lore raw

PATCH v2 6/9 pci: factorize driver search

In reply to David Marchand

Same idea as a few commits before, no need to implement the same logic in probe and detach functions.
Here, the driver matching is done by a helper, then probe / detach functions are called respectively.

---
 lib/librte_eal/common/eal_common_pci.c | 96 ++++++++++++----------------------
 1 file changed, 34 insertions(+), 62 deletions(-)
lib/librte_eal/common/eal_common_pci.c +34 -62
index fd88305..768421a 100644
@@ -280,55 +280,16 @@ pci_detach_device(struct rte_pci_driver *dr, struct rte_pci_device *dev)
 	return 0;
 }
 
-/*
- * If vendor/device ID match, call the devinit() function of all
- * registered driver for the given device. Return -1 if initialization
- * failed, return 1 if no driver is found for this device.
- */
-static int
-pci_probe_all_drivers(struct rte_pci_device *dev)
-{
-	struct rte_pci_driver *dr = NULL;
-	int rc = 0;
-
-	TAILQ_FOREACH(dr, &pci_driver_list, next) {
-		if (!pci_driver_supports_device(dr, dev))
-			continue;
-
-		rc = pci_probe_device(dr, dev);
-		if (rc < 0)
-			/* negative value is an error */
-			return -1;
-		if (rc > 0)
-			/* positive value means device is blacklisted */
-			continue;
-		return 0;
-	}
-	return 1;
-}
-
-/*
- * If vendor/device ID match, call the devuninit() function of all
- * registered driver for the given device. Return -1 if initialization
- * failed, return 1 if no driver is found for this device.
- */
-static int
-pci_detach_all_drivers(struct rte_pci_device *dev)
+static struct rte_pci_driver *
+pci_find_driver(struct rte_pci_device *dev)
 {
-	struct rte_pci_driver *dr = NULL;
-	int rc = 0;
+	struct rte_pci_driver *dr;
 
 	TAILQ_FOREACH(dr, &pci_driver_list, next) {
-		if (!pci_driver_supports_device(dr, dev))
-			continue;
-
-		rc = pci_detach_device(dr, dev);
-		if (rc < 0)
-			/* negative value is an error */
-			return -1;
-		return 0;
+		if (pci_driver_supports_device(dr, dev))
+			break;
 	}
-	return 1;
+	return dr;
 }
 
 /*
@@ -338,8 +299,8 @@ pci_detach_all_drivers(struct rte_pci_device *dev)
 int
 rte_eal_pci_probe_one(const struct rte_pci_addr *addr)
 {
-	struct rte_pci_device *dev = NULL;
-	int ret = 0;
+	struct rte_pci_device *dev;
+	struct rte_pci_driver *dr;
 
 	if (addr == NULL)
 		return -1;
@@ -353,8 +314,11 @@ rte_eal_pci_probe_one(const struct rte_pci_addr *addr)
 	if (!dev)
 		goto err_return;
 
-	ret = pci_probe_all_drivers(dev);
-	if (ret < 0)
+	dr = pci_find_driver(dev);
+	if (!dr)
+		goto err_return;
+
+	if (pci_probe_device(dr, dev) < 0)
 		goto err_return;
 
 	return 0;
@@ -372,8 +336,8 @@ err_return:
 int
 rte_eal_pci_detach(const struct rte_pci_addr *addr)
 {
-	struct rte_pci_device *dev = NULL;
-	int ret = 0;
+	struct rte_pci_device *dev;
+	struct rte_pci_driver *dr;
 
 	if (addr == NULL)
 		return -1;
@@ -382,8 +346,11 @@ rte_eal_pci_detach(const struct rte_pci_addr *addr)
 	if (!dev)
 		goto err_return;
 
-	ret = pci_detach_all_drivers(dev);
-	if (ret < 0)
+	dr = pci_find_driver(dev);
+	if (!dr)
+		goto err_return;
+
+	if (pci_detach_device(dr, dev) < 0)
 		goto err_return;
 
 	TAILQ_REMOVE(&pci_device_list, dev, next);
@@ -404,28 +371,33 @@ err_return:
 int
 rte_eal_pci_probe(void)
 {
-	struct rte_pci_device *dev = NULL;
+	struct rte_pci_device *dev;
+	struct rte_pci_driver *dr;
 	struct rte_devargs *devargs;
 	int probe_all = 0;
-	int ret = 0;
 
 	if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) == 0)
 		probe_all = 1;
 
 	TAILQ_FOREACH(dev, &pci_device_list, next) {
 
+		/* no driver available */
+		dr = pci_find_driver(dev);
+		if (!dr)
+			continue;
+
 		/* set devargs in PCI structure */
 		devargs = pci_devargs_lookup(dev);
 		if (devargs != NULL)
 			dev->devargs = devargs;
 
-		/* probe all or only whitelisted devices */
-		if (probe_all)
-			ret = pci_probe_all_drivers(dev);
-		else if (devargs != NULL &&
-			devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
-			ret = pci_probe_all_drivers(dev);
-		if (ret < 0)
+		/* skip if not probing all and device is not whitelisted */
+		if (!probe_all &&
+		    (devargs == NULL ||
+		     devargs->type != RTE_DEVTYPE_WHITELISTED_PCI))
+			continue;
+
+		if (pci_probe_device(dr, dev) < 0)
 			rte_exit(EXIT_FAILURE, "Requested device " PCI_PRI_FMT
 				 " cannot be used\n", dev->addr.domain, dev->addr.bus,
 				 dev->addr.devid, dev->addr.function);
Signature · 1 line
1.9.1
#15 lore raw

PATCH v2 4/9 pci: factorize probe/detach code

In reply to David Marchand

Move pci id matching to a helper and reuse it in probe and detach functions.

---
 lib/librte_eal/common/eal_common_pci.c | 67 ++++++++++++----------------------
 1 file changed, 23 insertions(+), 44 deletions(-)
lib/librte_eal/common/eal_common_pci.c +23 -44
index c3a33c6..a6791c1 100644
@@ -174,14 +174,10 @@ pci_add_device(struct rte_pci_device *dev)
 	return 0;
 }
 
-/*
- * If vendor/device ID match, call the devinit() function of the
- * driver.
- */
 static int
-rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
+pci_driver_supports_device(const struct rte_pci_driver *dr,
+			   const struct rte_pci_device *dev)
 {
-	int ret;
 	const struct rte_pci_id *id_table;
 
 	for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
@@ -200,6 +196,20 @@ rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *d
 				id_table->subsystem_device_id != PCI_ANY_ID)
 			continue;
 
+		return 1;
+	}
+	return 0;
+}
+
+/*
+ * If vendor/device ID match, call the devinit() function of the
+ * driver.
+ */
+static int
+rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr,
+			     struct rte_pci_device *dev)
+{
+	int ret;
 		struct rte_pci_addr *loc = &dev->addr;
 
 		RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
@@ -240,9 +250,6 @@ rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *d
 
 		/* call the driver devinit() function */
 		return dr->devinit(dr, dev);
-	}
-	/* return positive value if driver is not found */
-	return 1;
 }
 
 /*
@@ -253,27 +260,6 @@ static int
 rte_eal_pci_detach_dev(struct rte_pci_driver *dr,
 		struct rte_pci_device *dev)
 {
-	const struct rte_pci_id *id_table;
-
-	if ((dr == NULL) || (dev == NULL))
-		return -EINVAL;
-
-	for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
-
-		/* check if device's identifiers match the driver's ones */
-		if (id_table->vendor_id != dev->id.vendor_id &&
-				id_table->vendor_id != PCI_ANY_ID)
-			continue;
-		if (id_table->device_id != dev->id.device_id &&
-				id_table->device_id != PCI_ANY_ID)
-			continue;
-		if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
-				id_table->subsystem_vendor_id != PCI_ANY_ID)
-			continue;
-		if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
-				id_table->subsystem_device_id != PCI_ANY_ID)
-			continue;
-
 		struct rte_pci_addr *loc = &dev->addr;
 
 		RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
@@ -294,10 +280,6 @@ rte_eal_pci_detach_dev(struct rte_pci_driver *dr,
 			pci_unmap_device(dev);
 
 		return 0;
-	}
-
-	/* return positive value if driver is not found */
-	return 1;
 }
 
 /*
@@ -311,16 +293,16 @@ pci_probe_all_drivers(struct rte_pci_device *dev)
 	struct rte_pci_driver *dr = NULL;
 	int rc = 0;
 
-	if (dev == NULL)
-		return -1;
-
 	TAILQ_FOREACH(dr, &pci_driver_list, next) {
+		if (!pci_driver_supports_device(dr, dev))
+			continue;
+
 		rc = rte_eal_pci_probe_one_driver(dr, dev);
 		if (rc < 0)
 			/* negative value is an error */
 			return -1;
 		if (rc > 0)
-			/* positive value means driver not found */
+			/* positive value means device is blacklisted */
 			continue;
 		return 0;
 	}
@@ -338,17 +320,14 @@ pci_detach_all_drivers(struct rte_pci_device *dev)
 	struct rte_pci_driver *dr = NULL;
 	int rc = 0;
 
-	if (dev == NULL)
-		return -1;
-
 	TAILQ_FOREACH(dr, &pci_driver_list, next) {
+		if (!pci_driver_supports_device(dr, dev))
+			continue;
+
 		rc = rte_eal_pci_detach_dev(dr, dev);
 		if (rc < 0)
 			/* negative value is an error */
 			return -1;
-		if (rc > 0)
-			/* positive value means driver not found */
-			continue;
 		return 0;
 	}
 	return 1;
Signature · 1 line
1.9.1
#18 lore raw

PATCH v2 7/9 pci: remove driver lookup from detach

In reply to David Marchand

A device is linked to a driver at probe time.
When detaching, we should call this same driver detach() function and no need for a driver lookup.

---
Changes since v1:
- moved some logs for consistency

---
 lib/librte_eal/common/eal_common_pci.c | 19 +++++++------------
 1 file changed, 7 insertions(+), 12 deletions(-)
lib/librte_eal/common/eal_common_pci.c +7 -12
index 768421a..082eab8 100644
@@ -256,13 +256,9 @@ pci_probe_device(struct rte_pci_driver *dr, struct rte_pci_device *dev)
  * driver.
  */
 static int
-pci_detach_device(struct rte_pci_driver *dr, struct rte_pci_device *dev)
+pci_detach_device(struct rte_pci_device *dev)
 {
-	struct rte_pci_addr *loc = &dev->addr;
-
-	RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
-		loc->domain, loc->bus, loc->devid,
-		loc->function, dev->numa_node);
+	struct rte_pci_driver *dr = dev->driver;
 
 	RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
 		dev->id.device_id, dr->name);
@@ -337,20 +333,19 @@ int
 rte_eal_pci_detach(const struct rte_pci_addr *addr)
 {
 	struct rte_pci_device *dev;
-	struct rte_pci_driver *dr;
 
 	if (addr == NULL)
 		return -1;
 
 	dev = pci_find_device(addr);
-	if (!dev)
+	if (!dev || !dev->driver)
 		goto err_return;
 
-	dr = pci_find_driver(dev);
-	if (!dr)
-		goto err_return;
+	RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
+		dev->addr.domain, dev->addr.bus, dev->addr.devid,
+		dev->addr.function, dev->numa_node);
 
-	if (pci_detach_device(dr, dev) < 0)
+	if (pci_detach_device(dev) < 0)
 		goto err_return;
 
 	TAILQ_REMOVE(&pci_device_list, dev, next);
Signature · 1 line
1.9.1
#19 lore raw

PATCH v2 8/9 pci: implement blacklist using a hook

In reply to David Marchand

The idea is to prepare a generic hook system for all bus, but I am still unsure if this approach will be the right one, hence, keeping this as private for now.

Here, blacklisting policy is removed from pci scan code, making it possible to override it later by the application (once the api is judged generic and good enough).

With this, blacklist evaluation moves to rte_eal_pci_probe() only. This way, rte_eal_pci_probe_one() (used by hotplug when attaching) now accepts to probe devices that were blacklisted initially.

A new debug trace is added when skipping a device not part of a given whitelist.

---
 lib/librte_eal/common/eal_common_pci.c | 101 +++++++++++++++++++++++++--------
 1 file changed, 77 insertions(+), 24 deletions(-)
lib/librte_eal/common/eal_common_pci.c +77 -24
index 082eab8..4a0ec73 100644
@@ -87,6 +87,62 @@ struct pci_driver_list pci_driver_list =
 struct pci_device_list pci_device_list =
 	TAILQ_HEAD_INITIALIZER(pci_device_list);
 
+enum rte_eal_pci_hook {
+	RTE_EAL_PCI_SCAN,
+};
+
+enum rte_eal_pci_hook_return {
+	RTE_EAL_PCI_HOOK_OK,
+	RTE_EAL_PCI_HOOK_ERROR,
+	RTE_EAL_PCI_HOOK_SKIP,
+};
+
+typedef int (rte_eal_pci_hook_t)(enum rte_eal_pci_hook,
+				 struct rte_pci_driver *,
+				 struct rte_pci_device *);
+
+static int
+blacklist_pci_hook(enum rte_eal_pci_hook h,
+		   struct rte_pci_driver *dr __rte_unused,
+		   struct rte_pci_device *dev)
+{
+	int ret;
+
+	switch (h) {
+	case RTE_EAL_PCI_SCAN:
+	{
+		int whitelist =
+			rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI);
+		int blacklist =
+			rte_eal_devargs_type_count(RTE_DEVTYPE_BLACKLISTED_PCI);
+
+		/*
+		 * We want to probe this device when:
+		 * - there is no whitelist/blacklist,
+		 * - there is a whitelist, and device is part of it,
+		 * - there is a blacklist, and device is not part of it.
+		 */
+		if (whitelist && !dev->devargs) {
+			RTE_LOG(DEBUG, EAL, "  Device is not whitelisted, not initializing\n");
+			ret = RTE_EAL_PCI_HOOK_SKIP;
+		} else if (blacklist && dev->devargs) {
+			RTE_LOG(DEBUG, EAL, "  Device is blacklisted, not initializing\n");
+			ret = RTE_EAL_PCI_HOOK_SKIP;
+		} else
+			ret = RTE_EAL_PCI_HOOK_OK;
+		break;
+	}
+	default:
+		/* nothing to do here, just say ok */
+		ret = RTE_EAL_PCI_HOOK_OK;
+		break;
+	}
+
+	return ret;
+}
+
+static rte_eal_pci_hook_t *pci_hook = &blacklist_pci_hook;
+
 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
 {
 	struct rte_devargs *devargs;
@@ -209,22 +265,10 @@ static int
 pci_probe_device(struct rte_pci_driver *dr, struct rte_pci_device *dev)
 {
 	int ret;
-	struct rte_pci_addr *loc = &dev->addr;
-
-	RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
-		loc->domain, loc->bus, loc->devid, loc->function,
-		dev->numa_node);
 
 	RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
 		dev->id.device_id, dr->name);
 
-	/* no initialization when blacklisted, return without error */
-	if (dev->devargs != NULL &&
-		dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
-		RTE_LOG(DEBUG, EAL, "  Device is blacklisted, not initializing\n");
-		return 1;
-	}
-
 	if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
 #ifdef RTE_PCI_CONFIG
 		/*
@@ -310,6 +354,11 @@ rte_eal_pci_probe_one(const struct rte_pci_addr *addr)
 	if (!dev)
 		goto err_return;
 
+	RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
+		dev->addr.domain, dev->addr.bus,
+		dev->addr.devid, dev->addr.function,
+		dev->numa_node);
+
 	dr = pci_find_driver(dev);
 	if (!dr)
 		goto err_return;
@@ -369,27 +418,31 @@ rte_eal_pci_probe(void)
 	struct rte_pci_device *dev;
 	struct rte_pci_driver *dr;
 	struct rte_devargs *devargs;
-	int probe_all = 0;
-
-	if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) == 0)
-		probe_all = 1;
+	int ret;
 
 	TAILQ_FOREACH(dev, &pci_device_list, next) {
 
-		/* no driver available */
-		dr = pci_find_driver(dev);
-		if (!dr)
-			continue;
+		RTE_LOG(DEBUG, EAL,
+			"PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
+			dev->addr.domain, dev->addr.bus,
+			dev->addr.devid, dev->addr.function,
+			dev->numa_node);
 
 		/* set devargs in PCI structure */
 		devargs = pci_devargs_lookup(dev);
 		if (devargs != NULL)
 			dev->devargs = devargs;
 
-		/* skip if not probing all and device is not whitelisted */
-		if (!probe_all &&
-		    (devargs == NULL ||
-		     devargs->type != RTE_DEVTYPE_WHITELISTED_PCI))
+		ret = pci_hook(RTE_EAL_PCI_SCAN, NULL, dev);
+		if (ret != RTE_EAL_PCI_HOOK_OK) {
+			RTE_LOG(DEBUG, EAL, "  scan hook refused dev, err=%d\n",
+				ret);
+			continue;
+		}
+
+		/* no driver available */
+		dr = pci_find_driver(dev);
+		if (!dr)
 			continue;
 
 		if (pci_probe_device(dr, dev) < 0)
Signature · 1 line
1.9.1
#20 lore raw

PATCH v2 9/9 pci: implement automatic bind/unbind

In reply to David Marchand

Reuse pci hook to implement automatic bind / unbind.
The more I look at this, the more I think this should go to the PMDs themselves (with options per devices to control this), with EAL offering helpers to achieve this.

---
 lib/librte_eal/bsdapp/eal/eal_pci.c        |  25 ++++
 lib/librte_eal/common/eal_common_options.c |   8 ++
 lib/librte_eal/common/eal_common_pci.c     |  79 +++++++++++
 lib/librte_eal/common/eal_options.h        |   2 +
 lib/librte_eal/common/eal_private.h        |  38 ++++++
 lib/librte_eal/common/include/rte_pci.h    |   7 +-
 lib/librte_eal/linuxapp/eal/eal_pci.c      | 210 +++++++++++++++++++++++++++++
 7 files changed, 367 insertions(+), 2 deletions(-)
lib/librte_eal/bsdapp/eal/eal_pci.c +25 -0
index e95249b..130f7e9 100644
@@ -91,6 +91,31 @@ pci_unbind_kernel_driver(struct rte_pci_device *dev __rte_unused)
 	return -ENOTSUP;
 }
 
+int
+pci_rebind_device(const struct rte_pci_device *dev __rte_unused,
+		  const char *driver __rte_unused)
+{
+	RTE_LOG(ERR, EAL, "Rebinding device to pci kernel drivers is not implemented for BSD\n");
+	return -ENOTSUP;
+}
+
+int
+pci_mapping_driver_bound(const struct rte_pci_device *dev)
+{
+	int ret;
+
+	switch (dev->kdrv) {
+	case RTE_KDRV_NIC_UIO:
+		ret = 1;
+		break;
+	default:
+		ret = 0;
+		break;
+	}
+
+	return ret;
+}
+
 /* Map pci device */
 int
 pci_map_device(struct rte_pci_device *dev)
lib/librte_eal/common/eal_common_options.c +8 -0
index 29942ea..b646abd 100644
@@ -54,6 +54,7 @@
 #include "eal_internal_cfg.h"
 #include "eal_options.h"
 #include "eal_filesystem.h"
+#include "eal_private.h"
 
 #define BITS_PER_HEX 4
 
@@ -95,6 +96,7 @@ eal_long_options[] = {
 	{OPT_VFIO_INTR,         1, NULL, OPT_VFIO_INTR_NUM        },
 	{OPT_VMWARE_TSC_MAP,    0, NULL, OPT_VMWARE_TSC_MAP_NUM   },
 	{OPT_XEN_DOM0,          0, NULL, OPT_XEN_DOM0_NUM         },
+	{OPT_PCI_UIO_AUTOBIND,  1, NULL, OPT_PCI_UIO_AUTOBIND_NUM },
 	{0,                     0, NULL, 0                        }
 };
 
@@ -897,6 +899,10 @@ eal_parse_common_option(int opt, const char *optarg,
 		}
 		break;
 
+	case OPT_PCI_UIO_AUTOBIND_NUM:
+		pci_init_autobind(optarg);
+		break;
+
 	/* don't know what to do, leave this to caller */
 	default:
 		return 1;
@@ -1019,5 +1025,7 @@ eal_common_usage(void)
 	       "  --"OPT_NO_PCI"            Disable PCI\n"
 	       "  --"OPT_NO_HPET"           Disable HPET\n"
 	       "  --"OPT_NO_SHCONF"         No shared config (mmap'd files)\n"
+	       "  --"OPT_PCI_UIO_AUTOBIND"  Set default kernel driver to bind pci devices,\n"
+	       "                            when their associated pmd requires uio\n"
 	       "\n", RTE_MAX_LCORE);
 }
lib/librte_eal/common/eal_common_pci.c +79 -0

124 lines not on this page · raw message

lib/librte_eal/common/eal_options.h +2 -0
index a881c62..182bc3d 100644
@@ -83,6 +83,8 @@ enum {
 	OPT_VMWARE_TSC_MAP_NUM,
 #define OPT_XEN_DOM0          "xen-dom0"
 	OPT_XEN_DOM0_NUM,
+#define OPT_PCI_UIO_AUTOBIND  "pci-uio-autobind"
+	OPT_PCI_UIO_AUTOBIND_NUM,
 	OPT_LONG_MAX_NUM
 };
 
lib/librte_eal/common/eal_private.h +38 -0
index e44a448..9019144 100644
@@ -215,6 +215,44 @@ int pci_unbind_kernel_driver(struct rte_pci_device *dev);
 int pci_map_device(struct rte_pci_device *dev);
 
 /**
+ * Tell to the autobind system which driver to bind to.
+ *
+ * This function is private to EAL.
+ *
+ * @param name
+ *    The kernel driver to bind to.
+ */
+void pci_init_autobind(const char *name);
+
+/**
+ * Helper used by autobind system to check if device is already bound
+ * to an adequate driver.
+ *
+ * This function is private to EAL.
+ *
+ * @param dev
+ *	The PCI device object.
+ * @return
+ *   0 on success, positive if device is bound.
+ */
+int pci_mapping_driver_bound(const struct rte_pci_device *dev);
+
+/**
+ * Rebind a pci device to a kernel driver.
+ *
+ * This function is private to EAL.
+ *
+ * @param dev
+ *	The PCI device object.
+ * @param name
+ *    The kernel driver to bind to, passing an empty name tries to rebind
+ *    the device to original kernel driver.
+ * @return
+ *   0 on success, negative on error
+ */
+int pci_rebind_device(const struct rte_pci_device *dev, const char *name);
+
+/**
  * Unmap this device
  *
  * This function is private to EAL.
lib/librte_eal/common/include/rte_pci.h +5 -2
index 9edd5f5..75cab50 100644
@@ -90,8 +90,11 @@ TAILQ_HEAD(pci_driver_list, rte_pci_driver); /**< PCI drivers in D-linked Q. */
 extern struct pci_driver_list pci_driver_list; /**< Global list of PCI drivers. */
 extern struct pci_device_list pci_device_list; /**< Global list of PCI devices. */
 
-/** Pathname of PCI devices directory. */
-#define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
+/* FIXME: this is linux stuff, should not be in common/ */
+/** Pathname of PCI directories. */
+#define SYSFS_PCI "/sys/bus/pci"
+#define SYSFS_PCI_DEVICES SYSFS_PCI"/devices"
+#define SYSFS_PCI_DRIVERS SYSFS_PCI"/drivers"
 
 /** Formatting string for PCI device identifier: Ex: 0000:00:01.0 */
 #define PCI_PRI_FMT "%.4" PRIx16 ":%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
lib/librte_eal/linuxapp/eal/eal_pci.c +210 -0

222 lines not on this page · raw message

Signature · 1 line
1.9.1
#21 lore raw

Re: [PATCH v2 9/9] pci: implement automatic bind/unbind

Quoted 4 lines · David Marchand

Reuse pci hook to implement automatic bind / unbind.
The more I look at this, the more I think this should go to the PMDs themselves (with options per devices to control this), with EAL offering helpers to achieve this.

This sounds good to me.
As a basic software design rule, the decisions of using a given resource and/or performing a given operation (the intelligent knowledge) should systematically be taken at the maximum possible software level. Conversely, such high-level decisions should be offered the most appropriate low-level services to facilitate their implementation.
In the case we are considering here - the need to bind a PCI device - the Poll Mode Driver of that device is the only component which actually knows.
Conversely, the EAL layer is the best place where to implement the
OS-dependent and/or architecture-dependent device binding services to be
invoked by PMDs at PCI device probing time.

My 2 cents,
Ivan

Signature · 2 lines
Ivan Boule
6WIND Development Engineer
#22 lore raw
In reply to David Marchand

Hello David,

I am confused a bit. I started to review the "[PATCH 0/9] prepare for rte_device / rte_driver" series and then I've noticed there are 2 patch series having "pci:
no need for dynamic tailq init" patch there. But then, there is this v2 that does not have this patch. What is the right one? What should I look at. Is related?

Regards
Jan

Quoted 35 lines · On Fri, 29 Jan 2016 15:49:04 +0100 David Marchand <[email protected]>…

Before 2.2.0 release, while preparing for more changes in eal (and fixing a problem reported by Roger M. [1]), I came up with this (part of) patchset that tries to make the pci code more compact and easier to read.

I ended up introducing some hooks in the pci layer to customize pci blacklist / whitelist handling and make it possible to automatically bind / unbind pci devices to igb_uio (or equivalent) when attaching a device.

I am still not really happy:
- the pci blacklist / whitelist makes me think we should let the application tell eal which resources to use and get rid of the unconditional pci scan code, which means removing rte_eal_pci_probe() from rte_eal_init(), and remove rte_eal_dev_init() for vdevs,
- the more I look at this, the more I think automatic bind / unbind for pci devices should be called from the pmd context. The drivers know best what they require and what they want to do with the resources passed by the eal (see the drv_flags / RTE_KDRV_NONE / rte_eal_pci_map_device stuff for virtio pmd).
This behaviour would still be optional, on a per-device basis.

So, I think that these hooks are not that good of an idea and I kept them private for now, but anyway, sending this for comments.

Changes since v1:
- split the initial patchset. This current patchset now depends on
[2] sent separately which should be applied first,
- introduced hooks in pci common code,
- implemented automatic bind / unbind for "uio" pci devices

[1] http://dpdk.org/ml/archives/dev/2015-November/028140.html
[2] http://dpdk.org/ml/archives/dev/2016-January/032387.html

Signature · 4 lines
   Jan Viktorin                  E-mail: [email protected]
   System Architect              Web:    www.RehiveTech.com
   RehiveTech
   Brno, Czech Republic
#23 lore raw

Hello Jan,

Quoted 4 lines · Jan Viktorin

I am confused a bit. I started to review the "[PATCH 0/9] prepare for rte_device / rte_driver" series and then I've noticed there are 2 patch series having "pci:
no need for dynamic tailq init" patch there. But then, there is this v2 that does not have this patch. What is the right one? What should I look at. Is related?

I splitted this initial series, because I want to dissociate the work trying to get to rte_driver/rte_device from other work that modifies pci code (blacklist rework and "autobind").

I think you want to look at [1], but, if you want the global picture, first apply [1], then [2] ([2] is the v2 patchset which supersedes the initial patchset that had this "pci:
no need for dynamic tailq init" patch in it).

[1] http://dpdk.org/ml/archives/dev/2016-January/032387.html
[2] http://dpdk.org/ml/archives/dev/2016-January/032398.html

Regards,

Signature · 1 line
David Marchand
#24 lore raw
In reply to David Marchand
Quoted 3 lines · On Fri, 29 Jan 2016 15:49:04 +0100 David Marchand <[email protected]>…

Before 2.2.0 release, while preparing for more changes in eal (and fixing a problem reported by Roger M. [1]), I came up with this (part of) patchset that tries to make the pci code more compact and easier to read.

Hello David,

what is the state of this series at the moment? Do you expect some more reviews? I remember that I've sent some reviews but I don't think they are integrated in the v2. By the way, there two patch series joined into one or something like that, am I right?

Regards
Jan

Quoted 32 lines

I ended up introducing some hooks in the pci layer to customize pci blacklist / whitelist handling and make it possible to automatically bind / unbind pci devices to igb_uio (or equivalent) when attaching a device.

I am still not really happy:
- the pci blacklist / whitelist makes me think we should let the application tell eal which resources to use and get rid of the unconditional pci scan code, which means removing rte_eal_pci_probe() from rte_eal_init(), and remove rte_eal_dev_init() for vdevs,
- the more I look at this, the more I think automatic bind / unbind for pci devices should be called from the pmd context. The drivers know best what they require and what they want to do with the resources passed by the eal (see the drv_flags / RTE_KDRV_NONE / rte_eal_pci_map_device stuff for virtio pmd).
This behaviour would still be optional, on a per-device basis.

So, I think that these hooks are not that good of an idea and I kept them private for now, but anyway, sending this for comments.

Changes since v1:
- split the initial patchset. This current patchset now depends on
[2] sent separately which should be applied first,
- introduced hooks in pci common code,
- implemented automatic bind / unbind for "uio" pci devices

[1] http://dpdk.org/ml/archives/dev/2015-November/028140.html
[2] http://dpdk.org/ml/archives/dev/2016-January/032387.html

Signature · 4 lines
   Jan Viktorin                  E-mail: [email protected]
   System Architect              Web:    www.RehiveTech.com
   RehiveTech
   Brno, Czech Republic
#25 lore raw

Hello Jan,

Quoted 11 lines · Jan Viktorin
Quoted 3 lines · On Fri, 29 Jan 2016 15:49:04 +0100 David Marchand <[email protected]>…

Before 2.2.0 release, while preparing for more changes in eal (and fixing a problem reported by Roger M. [1]), I came up with this (part of) patchset that tries to make the pci code more compact and easier to read.

what is the state of this series at the moment? Do you expect some more reviews? I remember that I've sent some reviews but I don't think they are integrated in the v2. By the way, there two patch series joined into one or something like that, am I right?

Yes, I do remember your comments.
I have them in my tree, but this series still needs more work. Trying to come up with something this week.

Regards,

Signature · 1 line
David Marchand