From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gaetan.rivet@6wind.com>
Received: from mail-wr0-f170.google.com (mail-wr0-f170.google.com
 [209.85.128.170]) by dpdk.org (Postfix) with ESMTP id C9D2B4A63
 for <dev@dpdk.org>; Fri,  7 Apr 2017 17:28:28 +0200 (CEST)
Received: by mail-wr0-f170.google.com with SMTP id g19so67819545wrb.0
 for <dev@dpdk.org>; Fri, 07 Apr 2017 08:28:28 -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=++kNMder+7r736b1RI22Tz3e33xnqjlKzzj2RKicyOk=;
 b=ML6wGHcZtIZgpntI8f6Ln18AAtqkAKvTcptSX2l8HVQUXZ2JJ7d2NJIm6bOb0/GL+K
 g/wredAL/AVA6NM/Uf9H/Xh+MvpmjP03zp8zm1QQtSb2tRiljgCJQsJR9KkDQ+cNoy+R
 B05iL+Cz6hzGrRhkVJlD0oyUWiMct37IyhT4j9FULmf/ua1fh2aPwvSuytUDrfWOnmsW
 fTCHUsZQqfTUow96t19jlEODQ/Im1ZKqkvvv8aaRNw9DNN0gU141YH0mmSg3VYnPHIYh
 ndRttGb6tXJwjGeAEorfVCDV2r+sGMH7/5fyx6UI2rk9EdXDnr/cazJc3/z6Q1X3rdst
 Cwvg==
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=++kNMder+7r736b1RI22Tz3e33xnqjlKzzj2RKicyOk=;
 b=HZLYppQO9s3P+E+goFf180mW3ftnWewJoTB0RFjfe6EFEh1Hd5tITPuFzx3SupzNKK
 MXYRgCMJetNMuHTJ0miQZZAg4yxghL09CE492vZErvFAKzf/94dQgqoElybaRBv3LHwT
 oraoXf7F+5Jg/edq4n7Owyj1QJLtU5yn1GE852FQCd5cGNcCykVXrAq+psyIqPHwjqLv
 dlCiVFJ0oevl+TVpDYwKweUWa7wVwEKXEpmVTLVIHWWcexNnJNxrE4WU2aaq7MYaASO6
 2eAhSHj/aWxd6QBns8zQyS2PT9pPkhn493qmvPOyeh3XFWhVdZeRRrwO4uWe2yRZFHFL
 LL2g==
X-Gm-Message-State: AFeK/H1AyvEFj+bJ3PSgL/OWs9wZ9hVR3sk4fr1Ytz1h7FnaFlK/4WxUEIe749eluSZooam0
X-Received: by 10.223.132.35 with SMTP id 32mr13034447wrf.58.1491578908511;
 Fri, 07 Apr 2017 08:28:28 -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.27
 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128);
 Fri, 07 Apr 2017 08:28:27 -0700 (PDT)
From: Gaetan Rivet <gaetan.rivet@6wind.com>
To: dev@dpdk.org
Cc: Shreyansh Jain <shreyansh.jain@nxp.com>,
 Thomas Monjalon <thomas.monjalon@6wind.com>
Date: Fri,  7 Apr 2017 17:28:08 +0200
Message-Id: <e977dd20f31d2bcb3cca3f5d572fe309835797d5.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 3/5] pci: add bus driver
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:29 -0000

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

Based on EAL Bus APIs, PCI bus callbacks and support functions are
introduced in this patch.

EAL continues to have direct PCI init/scan calls as well. These would be
removed in subsequent patches to enable bus only PCI devices.

Signed-off-by: Shreyansh Jain <shreyansh.jain@nxp.com>
Reviewed-by: Ferruh Yigit <ferruh.yigit@intel.com>
Signed-off-by: Thomas Monjalon <thomas.monjalon@6wind.com>
Reviewed-by: Gaetan Rivet <gaetan.rivet@6wind.com>
---
 lib/librte_eal/bsdapp/eal/eal_pci.c     | 11 ++++++
 lib/librte_eal/common/eal_common_pci.c  | 25 +++++++++++++
 lib/librte_eal/common/include/rte_pci.h | 65 +++++++++++++++++++++++++++++++++
 lib/librte_eal/linuxapp/eal/eal_pci.c   | 12 ++++++
 4 files changed, 113 insertions(+)

diff --git a/lib/librte_eal/bsdapp/eal/eal_pci.c b/lib/librte_eal/bsdapp/eal/eal_pci.c
index 16a1743..06b5a3a 100644
--- a/lib/librte_eal/bsdapp/eal/eal_pci.c
+++ b/lib/librte_eal/bsdapp/eal/eal_pci.c
@@ -675,3 +675,14 @@ rte_eal_pci_init(void)
 	}
 	return 0;
 }
+
+struct rte_pci_bus rte_pci_bus = {
+	.bus = {
+		.scan = rte_eal_pci_scan,
+		.probe = rte_eal_pci_probe,
+	},
+	.device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
+	.driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
+};
+
+RTE_REGISTER_BUS(PCI_BUS_NAME, rte_pci_bus.bus);
diff --git a/lib/librte_eal/common/eal_common_pci.c b/lib/librte_eal/common/eal_common_pci.c
index 498a3bb..853a12c 100644
--- a/lib/librte_eal/common/eal_common_pci.c
+++ b/lib/librte_eal/common/eal_common_pci.c
@@ -72,6 +72,7 @@
 #include <rte_errno.h>
 #include <rte_interrupts.h>
 #include <rte_log.h>
+#include <rte_bus.h>
 #include <rte_pci.h>
 #include <rte_per_lcore.h>
 #include <rte_memory.h>
@@ -88,6 +89,8 @@ struct pci_driver_list pci_driver_list =
 struct pci_device_list pci_device_list =
 	TAILQ_HEAD_INITIALIZER(pci_device_list);
 
+extern struct rte_pci_bus rte_pci_bus;
+
 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
 
 const char *pci_get_sysfs_path(void)
@@ -489,3 +492,25 @@ rte_eal_pci_unregister(struct rte_pci_driver *driver)
 	rte_eal_driver_unregister(&driver->driver);
 	TAILQ_REMOVE(&pci_driver_list, driver, next);
 }
+
+/* Add a device to PCI bus */
+void
+rte_eal_pci_add_device(struct rte_pci_device *pci_dev)
+{
+	TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
+}
+
+/* Insert a device into a predefined position in PCI bus */
+void
+rte_eal_pci_insert_device(struct rte_pci_device *exist_pci_dev,
+			  struct rte_pci_device *new_pci_dev)
+{
+	TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
+}
+
+/* Remove a device from PCI bus */
+void
+rte_eal_pci_remove_device(struct rte_pci_device *pci_dev)
+{
+	TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
+}
diff --git a/lib/librte_eal/common/include/rte_pci.h b/lib/librte_eal/common/include/rte_pci.h
index 8557e47..6984fd5 100644
--- a/lib/librte_eal/common/include/rte_pci.h
+++ b/lib/librte_eal/common/include/rte_pci.h
@@ -85,6 +85,7 @@ extern "C" {
 #include <rte_debug.h>
 #include <rte_interrupts.h>
 #include <rte_dev.h>
+#include <rte_bus.h>
 
 TAILQ_HEAD(pci_device_list, rte_pci_device); /**< PCI devices in D-linked Q. */
 TAILQ_HEAD(pci_driver_list, rte_pci_driver); /**< PCI drivers in D-linked Q. */
@@ -111,6 +112,25 @@ const char *pci_get_sysfs_path(void);
 /** Maximum number of PCI resources. */
 #define PCI_MAX_RESOURCE 6
 
+/** Name of PCI Bus */
+#define PCI_BUS_NAME "PCI"
+
+/* Forward declarations */
+struct rte_pci_device;
+struct rte_pci_driver;
+
+/** List of PCI devices */
+TAILQ_HEAD(rte_pci_device_list, rte_pci_device);
+/** List of PCI drivers */
+TAILQ_HEAD(rte_pci_driver_list, rte_pci_driver);
+
+/* PCI Bus iterators */
+#define FOREACH_DEVICE_ON_PCIBUS(p)	\
+		TAILQ_FOREACH(p, &(rte_pci_bus.device_list), next)
+
+#define FOREACH_DRIVER_ON_PCIBUS(p)	\
+		TAILQ_FOREACH(p, &(rte_pci_bus.driver_list), next)
+
 /**
  * A structure describing an ID for a PCI driver. Each driver provides a
  * table of these IDs for each device that it supports.
@@ -206,12 +226,22 @@ typedef int (pci_remove_t)(struct rte_pci_device *);
 struct rte_pci_driver {
 	TAILQ_ENTRY(rte_pci_driver) next;       /**< Next in list. */
 	struct rte_driver driver;               /**< Inherit core driver. */
+	struct rte_pci_bus *bus;                /**< PCI bus reference. */
 	pci_probe_t *probe;                     /**< Device Probe function. */
 	pci_remove_t *remove;                   /**< Device Remove function. */
 	const struct rte_pci_id *id_table;	/**< ID table, NULL terminated. */
 	uint32_t drv_flags;                     /**< Flags contolling handling of device. */
 };
 
+/**
+ * Structure describing the PCI bus
+ */
+struct rte_pci_bus {
+	struct rte_bus bus;               /**< Inherit the generic class */
+	struct rte_pci_device_list device_list;  /**< List of PCI devices */
+	struct rte_pci_driver_list driver_list;  /**< List of PCI drivers */
+};
+
 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
 /** Device driver supports link state interrupt */
@@ -508,6 +538,41 @@ RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
 void rte_eal_pci_unregister(struct rte_pci_driver *driver);
 
 /**
+ * Add a PCI device to the PCI Bus (append to PCI Device list). This function
+ * also updates the bus references of the PCI Device (and the generic device
+ * object embedded within.
+ *
+ * @param pci_dev
+ *	PCI device to add
+ * @return void
+ */
+void rte_eal_pci_add_device(struct rte_pci_device *pci_dev);
+
+/**
+ * Insert a PCI device in the PCI Bus at a particular location in the device
+ * list. It also updates the PCI Bus reference of the new devices to be
+ * inserted.
+ *
+ * @param exist_pci_dev
+ *	Existing PCI device in PCI Bus
+ * @param new_pci_dev
+ *	PCI device to be added before exist_pci_dev
+ * @return void
+ */
+void rte_eal_pci_insert_device(struct rte_pci_device *exist_pci_dev,
+			       struct rte_pci_device *new_pci_dev);
+
+/**
+ * Remove a PCI device from the PCI Bus. This sets to NULL the bus references
+ * in the PCI device object as well as the generic device object.
+ *
+ * @param pci_device
+ *	PCI device to be removed from PCI Bus
+ * @return void
+ */
+void rte_eal_pci_remove_device(struct rte_pci_device *pci_device);
+
+/**
  * Read PCI config space.
  *
  * @param device
diff --git a/lib/librte_eal/linuxapp/eal/eal_pci.c b/lib/librte_eal/linuxapp/eal/eal_pci.c
index 7d6f50a..979b6f3 100644
--- a/lib/librte_eal/linuxapp/eal/eal_pci.c
+++ b/lib/librte_eal/linuxapp/eal/eal_pci.c
@@ -35,6 +35,7 @@
 #include <dirent.h>
 
 #include <rte_log.h>
+#include <rte_bus.h>
 #include <rte_pci.h>
 #include <rte_eal_memconfig.h>
 #include <rte_malloc.h>
@@ -728,3 +729,14 @@ rte_eal_pci_init(void)
 
 	return 0;
 }
+
+struct rte_pci_bus rte_pci_bus = {
+	.bus = {
+		.scan = rte_eal_pci_scan,
+		.probe = rte_eal_pci_probe,
+	},
+	.device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
+	.driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
+};
+
+RTE_REGISTER_BUS(PCI_BUS_NAME, rte_pci_bus.bus);
-- 
2.1.4