From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gaetan.rivet@6wind.com>
Received: from mail-wm0-f47.google.com (mail-wm0-f47.google.com [74.125.82.47])
 by dpdk.org (Postfix) with ESMTP id C0F595592
 for <dev@dpdk.org>; Fri,  7 Apr 2017 17:28:27 +0200 (CEST)
Received: by mail-wm0-f47.google.com with SMTP id o81so46831301wmb.1
 for <dev@dpdk.org>; Fri, 07 Apr 2017 08:28:27 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=6wind-com.20150623.gappssmtp.com; s=20150623;
 h=from:to:cc:subject:date:message-id:in-reply-to:references
 :in-reply-to:references;
 bh=CYypjBPgVPEzTtKzkbSr0kP1G+Fstohq6OdanpSEyDc=;
 b=nKKy4Iy9vIchFGf3juRpXzRcRh0QXdXV3LMhT2Z4YGTPzF0RyzP5WXmRL8QY8L9Itg
 jbsXXJ15oITF7gp4a4/eFe0AAx0a2bY1kpWyd8kYW2No/6se4VjlWE/Y8wbAd+QcZKWF
 cmlNH1A7zTcghIUdG0GAyPe4z6OPszqq7Ww4z3VRQMlez8cRKPmnWmkopkuKnutG8/EW
 HO7jq0/wdK6cJNFK+PYKhFRq1dU1T4A5o44EeusFMRoIx5KfGMuc8GfuG9RQwpQsnb5a
 JJy0polECDEL3jK6lXe7d/4yaSXIN1NtXfLm9uEEKF4R1ynTqr2KmNeqiULPzI56RXq4
 50fA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20161025;
 h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to
 :references:in-reply-to:references;
 bh=CYypjBPgVPEzTtKzkbSr0kP1G+Fstohq6OdanpSEyDc=;
 b=fyaT7YruMGlChOcOjrm3GJKwL+oZ1LMPj0xs3dfIsjesgSA5/9DymgqCwcLzPGin0P
 ZmIXUNED96xrWaftxQJbel6Pt67iEl2lrhUAJZDsGy5sr7YVm9olnJMqh9hdXPT4GeqJ
 QRQd0VEdsykH1NUe2PBBGxjsnF2pHgWd5C621LEtnMcxDazzqabY+VssE8dR8+AvPhZk
 A9dp1cWby3GGIfxEfdukzZJ5+dmUt9Gdmd06h90Ja945gkW3y9IJfGOroM4B7M7X4veU
 E03yEm7GF4B6ts4i+71Ho9t2Neqlf1HEo3g7g/P4/tUZhBxIY4nYEZsjwtrewCPF9ZbB
 pONg==
X-Gm-Message-State: AFeK/H3a6UesM9SHVfYvkYfJTPAo74SqLs+B4EjifclrghY9SglwKbbDehho2v3cnjFlVzpR
X-Received: by 10.28.66.211 with SMTP id k80mr28089374wmi.5.1491578906076;
 Fri, 07 Apr 2017 08:28:26 -0700 (PDT)
Received: from bidouze.dev.6wind.com (host.78.145.23.62.rev.coltfrance.com.
 [62.23.145.78])
 by smtp.gmail.com with ESMTPSA id b199sm6699363wmb.13.2017.04.07.08.28.24
 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128);
 Fri, 07 Apr 2017 08:28:24 -0700 (PDT)
From: Gaetan Rivet <gaetan.rivet@6wind.com>
To: dev@dpdk.org
Cc: Shreyansh Jain <shreyansh.jain@nxp.com>
Date: Fri,  7 Apr 2017 17:28:06 +0200
Message-Id: <3c6336ba85824c803f20cda957552c1de3845cee.1491577715.git.gaetan.rivet@6wind.com>
X-Mailer: git-send-email 2.1.4
In-Reply-To: <cover.1491577715.git.gaetan.rivet@6wind.com>
References: <1484801117-779-1-git-send-email-thomas.monjalon@6wind.com>
 <cover.1491577715.git.gaetan.rivet@6wind.com>
In-Reply-To: <cover.1491577715.git.gaetan.rivet@6wind.com>
References: <cover.1491577715.git.gaetan.rivet@6wind.com>
Subject: [dpdk-dev] [PATCH v12 1/5] pci: split match and probe
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <http://dpdk.org/ml/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://dpdk.org/ml/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <http://dpdk.org/ml/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
X-List-Received-Date: Fri, 07 Apr 2017 15:28:28 -0000

From: Shreyansh Jain <shreyansh.jain@nxp.com>

Matching of PCI device address and driver ID table is being done at two
discreet locations duplicating the code. (rte_eal_pci_probe_one_driver
and rte_eal_pci_detach_dev).

Refactor the match logic as a single function.

Signed-off-by: Shreyansh Jain <shreyansh.jain@nxp.com>
Reviewed-by: Ferruh Yigit <ferruh.yigit@intel.com>
Signed-off-by: Gaetan Rivet <gaetan.rivet@6wind.com>
---
 lib/librte_eal/common/eal_common_pci.c | 176 ++++++++++++++++++---------------
 1 file changed, 98 insertions(+), 78 deletions(-)

diff --git a/lib/librte_eal/common/eal_common_pci.c b/lib/librte_eal/common/eal_common_pci.c
index 1ab92c9..83a81c4 100644
--- a/lib/librte_eal/common/eal_common_pci.c
+++ b/lib/librte_eal/common/eal_common_pci.c
@@ -154,73 +154,108 @@ pci_unmap_resource(void *requested_addr, size_t size)
 }
 
 /*
- * If vendor/device ID match, call the probe() function of the
- * driver.
+ * Match the PCI Driver and Device using the ID Table
+ *
+ * @param pci_drv
+ *	PCI driver from which ID table would be extracted
+ * @param pci_dev
+ *	PCI device to match against the driver
+ * @return
+ *	1 for successful match
+ *	0 for unsuccessful match
  */
 static int
-rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
+rte_pci_match(const struct rte_pci_driver *pci_drv,
+	      const struct rte_pci_device *pci_dev)
 {
-	int ret;
 	const struct rte_pci_id *id_table;
 
-	for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
-
+	for (id_table = pci_drv->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 &&
+		if (id_table->vendor_id != pci_dev->id.vendor_id &&
 				id_table->vendor_id != PCI_ANY_ID)
 			continue;
-		if (id_table->device_id != dev->id.device_id &&
+		if (id_table->device_id != pci_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)
+		if (id_table->subsystem_vendor_id !=
+		    pci_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)
+		if (id_table->subsystem_device_id !=
+		    pci_dev->id.subsystem_device_id &&
+		    id_table->subsystem_device_id != PCI_ANY_ID)
 			continue;
-		if (id_table->class_id != dev->id.class_id &&
+		if (id_table->class_id != pci_dev->id.class_id &&
 				id_table->class_id != RTE_CLASS_ANY_ID)
 			continue;
 
-		struct rte_pci_addr *loc = &dev->addr;
+		return 1;
+	}
 
-		RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
-				loc->domain, loc->bus, loc->devid, loc->function,
-				dev->device.numa_node);
+	return 0;
+}
 
-		/* no initialization when blacklisted, return without error */
-		if (dev->device.devargs != NULL &&
-			dev->device.devargs->type ==
-				RTE_DEVTYPE_BLACKLISTED_PCI) {
-			RTE_LOG(INFO, EAL, "  Device is blacklisted, not initializing\n");
-			return 1;
-		}
+/*
+ * If vendor/device ID match, call the probe() 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;
 
-		RTE_LOG(INFO, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
-				dev->id.device_id, dr->driver.name);
+	if ((dr == NULL) || (dev == NULL))
+		return -EINVAL;
 
-		if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
-			/* map resources for devices that use igb_uio or VFIO */
-			ret = rte_eal_pci_map_device(dev);
-			if (ret != 0)
-				return ret;
-		}
+	loc = &dev->addr;
 
-		/* reference driver structure */
-		dev->driver = dr;
+	/* The device is not blacklisted; Check if driver supports it */
+	if (!rte_pci_match(dr, dev)) {
+		/* Match of device and driver failed */
+		RTE_LOG(DEBUG, EAL, "Driver (%s) doesn't match the device\n",
+			dr->driver.name);
+		return 1;
+	}
 
-		/* call the driver probe() function */
-		ret = dr->probe(dr, dev);
-		if (ret) {
-			dev->driver = NULL;
-			if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
-				rte_eal_pci_unmap_device(dev);
-		}
+	RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
+			loc->domain, loc->bus, loc->devid, loc->function,
+			dev->device.numa_node);
+
+	/* no initialization when blacklisted, return without error */
+	if (dev->device.devargs != NULL &&
+		dev->device.devargs->type ==
+			RTE_DEVTYPE_BLACKLISTED_PCI) {
+		RTE_LOG(INFO, EAL, "  Device is blacklisted, not"
+			" initializing\n");
+		return 1;
+	}
 
-		return ret;
+	RTE_LOG(INFO, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
+		dev->id.device_id, dr->driver.name);
+
+	if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
+		/* map resources for devices that use igb_uio */
+		ret = rte_eal_pci_map_device(dev);
+		if (ret != 0)
+			return ret;
 	}
-	/* return positive value if driver doesn't support this device */
-	return 1;
+
+	/* reference driver structure */
+	dev->driver = dr;
+
+	/* call the driver probe() function */
+	ret = dr->probe(dr, dev);
+	if (ret) {
+		dev->driver = NULL;
+		if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
+			rte_eal_pci_unmap_device(dev);
+	}
+
+	return ret;
 }
 
 /*
@@ -231,51 +266,36 @@ static int
 rte_eal_pci_detach_dev(struct rte_pci_driver *dr,
 		struct rte_pci_device *dev)
 {
-	const struct rte_pci_id *id_table;
+	struct rte_pci_addr *loc;
 
 	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;
+	if (!rte_pci_match(dr, dev)) {
+		/* Device and driver don't match */
+		return 1;
+	}
 
-		RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
-				loc->domain, loc->bus, loc->devid,
-				loc->function, dev->device.numa_node);
+	loc = &dev->addr;
 
-		RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
-				dev->id.device_id, dr->driver.name);
+	RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
+			loc->domain, loc->bus, loc->devid,
+			loc->function, dev->device.numa_node);
 
-		if (dr->remove && (dr->remove(dev) < 0))
-			return -1;	/* negative value is an error */
+	RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
+			dev->id.device_id, dr->driver.name);
 
-		/* clear driver structure */
-		dev->driver = NULL;
+	if (dr->remove && (dr->remove(dev) < 0))
+		return -1;	/* negative value is an error */
 
-		if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
-			/* unmap resources for devices that use igb_uio */
-			rte_eal_pci_unmap_device(dev);
+	/* clear driver structure */
+	dev->driver = NULL;
 
-		return 0;
-	}
+	if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
+		/* unmap resources for devices that use igb_uio */
+		rte_eal_pci_unmap_device(dev);
 
-	/* return positive value if driver doesn't support this device */
-	return 1;
+	return 0;
 }
 
 /*
-- 
2.1.4