[dpdk-dev,RFC] eal: use busname and devargs to attach devices

Message ID 1487167444-642-1-git-send-email-jblunck@infradead.org (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers

Checks

Context Check Description
ci/checkpatch warning coding style issues
ci/Intel-compilation fail Compilation issues

Commit Message

Jan Blunck Feb. 15, 2017, 2:04 p.m. UTC
  Using just the device name to attach a device on the EAL level is not enough
for a unique identification of the bus it should get attached to. Therefore
lets change rte_eal_dev_attach() introduced in 16.11 to take the busname and
the raw devargs string.

Signed-off-by: Jan Blunck <jblunck@infradead.org>
---
 lib/librte_eal/common/eal_common_dev.c | 61 ++++++++++++++++++++++++++--------
 lib/librte_ether/rte_ethdev.c          | 17 +---------
 2 files changed, 49 insertions(+), 29 deletions(-)
  

Patch

diff --git a/lib/librte_eal/common/eal_common_dev.c b/lib/librte_eal/common/eal_common_dev.c
index 4bde430..bf41a94 100644
--- a/lib/librte_eal/common/eal_common_dev.c
+++ b/lib/librte_eal/common/eal_common_dev.c
@@ -37,6 +37,7 @@ 
 #include <inttypes.h>
 #include <sys/queue.h>
 
+#include <rte_bus.h>
 #include <rte_dev.h>
 #include <rte_devargs.h>
 #include <rte_debug.h>
@@ -76,29 +77,63 @@  void rte_eal_device_remove(struct rte_device *dev)
 	TAILQ_REMOVE(&dev_device_list, dev, next);
 }
 
-int rte_eal_dev_attach(const char *name, const char *devargs)
+int rte_eal_dev_attach(const char *busname, const char *devargs)
 {
-	struct rte_pci_addr addr;
+	struct rte_bus *bus;
+	rte_bus_attach_t attach;
+	int ret;
 
-	if (name == NULL || devargs == NULL) {
+	if (busname == NULL || devargs == NULL) {
 		RTE_LOG(ERR, EAL, "Invalid device or arguments provided\n");
 		return -EINVAL;
 	}
 
-	if (eal_parse_pci_DomBDF(name, &addr) == 0) {
-		if (rte_eal_pci_probe_one(&addr) < 0)
-			goto err;
+	if (!busname) {
+		char *name, *args;
+		struct rte_pci_addr addr;
 
-	} else {
-		if (rte_eal_vdev_init(name, devargs))
-			goto err;
+		/*
+		 * We default to virtual device but lets check for PCI device format for
+		 * backwards compatibility reasons.
+		 */
+		busname = "virtual";
+
+		if (rte_eal_parse_devargs_str(devargs, &name, &args))
+			return -EINVAL;
+
+		if (eal_parse_pci_DomBDF(name, &addr) == 0) {
+			RTE_LOG(ERR, EAL, "Deprectated\n");
+			busname = "pci";
+		}
+
+		free(name);
+		free(args);
 	}
 
-	return 0;
+	bus = rte_bus_find_by_name(busname);
+	if (!bus) {
+		RTE_LOG(ERR, EAL, "Can't find bus (%s)\n", busname);
+		return -EINVAL;
+	}
 
-err:
-	RTE_LOG(ERR, EAL, "Driver cannot attach the device (%s)\n", name);
-	return -EINVAL;
+	attach = bus->attach;
+	if (!attach) {
+		RTE_LOG(ERR, EAL, "Function not supported\n");
+		return -ENOTSUP;
+	}
+
+	ret = attach(devargs);
+	if (ret) {
+		RTE_LOG(ERR, EAL, "Attaching device to bus failed \n");
+		return ret;
+	}
+
+	/*
+	 * Probing might fail because some devices are not claimed by any driver
+	 * but we don't care: the device is properly attached at this point.
+	 */
+	(void)bus->probe();
+	return 0;
 }
 
 int rte_eal_dev_detach(const char *name)
diff --git a/lib/librte_ether/rte_ethdev.c b/lib/librte_ether/rte_ethdev.c
index eb0a94a..bbebc8f 100644
--- a/lib/librte_ether/rte_ethdev.c
+++ b/lib/librte_ether/rte_ethdev.c
@@ -474,29 +474,16 @@  rte_eth_dev_attach(const char *devargs, uint8_t *port_id)
 {
 	int ret = -1;
 	int current = rte_eth_dev_count();
-	char *name = NULL;
-	char *args = NULL;
 
 	if ((devargs == NULL) || (port_id == NULL)) {
 		ret = -EINVAL;
 		goto err;
 	}
 
-	/* parse devargs, then retrieve device name and args */
-	if (rte_eal_parse_devargs_str(devargs, &name, &args))
-		goto err;
-
-	ret = rte_eal_dev_attach(name, args);
+	ret = rte_eal_dev_attach(NULL, devargs);
 	if (ret < 0)
 		goto err;
 
-	/* no point looking at the port count if no port exists */
-	if (!rte_eth_dev_count()) {
-		RTE_LOG(ERR, EAL, "No port found for device (%s)\n", name);
-		ret = -1;
-		goto err;
-	}
-
 	/* if nothing happened, there is a bug here, since some driver told us
 	 * it did attach a device, but did not create a port.
 	 */
@@ -509,8 +496,6 @@  rte_eth_dev_attach(const char *devargs, uint8_t *port_id)
 	ret = 0;
 
 err:
-	free(name);
-	free(args);
 	return ret;
 }