DPDK patches and discussions
 help / color / mirror / Atom feed
From: Stephen Hemminger <stephen@networkplumber.org>
To: dev@dpdk.org
Cc: Stephen Hemminger <sthemmin@microsoft.com>
Subject: [dpdk-dev] [PATCH 10/13] virtio: localize mapping from rte_eth to pci
Date: Mon, 19 Dec 2016 13:59:41 -0800	[thread overview]
Message-ID: <20161219215944.17226-11-sthemmin@microsoft.com> (raw)
In-Reply-To: <20161219215944.17226-1-sthemmin@microsoft.com>

Use one function to get mapping from rte_eth_dev to pci_device.
Safely handle some possible misconfiguration of virtio_user related
to link state interrupt.

Signed-off-by: Stephen Hemminger <sthemmin@microsoft.com>
---
 drivers/net/virtio/virtio_ethdev.c | 90 ++++++++++++++++++++++++++------------
 1 file changed, 63 insertions(+), 27 deletions(-)

diff --git a/drivers/net/virtio/virtio_ethdev.c b/drivers/net/virtio/virtio_ethdev.c
index 079fd6c8..4db4568a 100644
--- a/drivers/net/virtio/virtio_ethdev.c
+++ b/drivers/net/virtio/virtio_ethdev.c
@@ -152,6 +152,20 @@ static const struct rte_virtio_xstats_name_off rte_virtio_txq_stat_strings[] = {
 #define VIRTIO_NB_TXQ_XSTATS (sizeof(rte_virtio_txq_stat_strings) / \
 			    sizeof(rte_virtio_txq_stat_strings[0]))
 
+static inline struct rte_pci_device *
+virtio_dev_to_pci(struct rte_eth_dev *eth_dev)
+{
+	return eth_dev->pci_dev;
+}
+
+static inline struct rte_intr_handle *
+virtio_dev_to_intr_handle(struct rte_eth_dev *eth_dev)
+{
+	struct rte_pci_device *pci_dev	= virtio_dev_to_pci(eth_dev);
+
+	return pci_dev ? &pci_dev->intr_handle : NULL;
+}
+
 static int
 virtio_send_command(struct virtnet_ctl *cvq, struct virtio_pmd_ctrl *ctrl,
 		int *dlen, int pkt_num)
@@ -1151,7 +1165,7 @@ virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features)
  * if link state changed.
  */
 static void
-virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
+virtio_interrupt_handler(struct rte_intr_handle *intr_handle,
 			 void *param)
 {
 	struct rte_eth_dev *dev = param;
@@ -1162,7 +1176,7 @@ virtio_interrupt_handler(__rte_unused struct rte_intr_handle *handle,
 	isr = vtpci_isr(hw);
 	PMD_DRV_LOG(INFO, "interrupt status = %#x", isr);
 
-	if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0)
+	if (rte_intr_enable(intr_handle) < 0)
 		PMD_DRV_LOG(ERR, "interrupt enable failed");
 
 	if (isr & VIRTIO_PCI_ISR_CONFIG) {
@@ -1190,7 +1204,7 @@ virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
 	struct virtio_hw *hw = eth_dev->data->dev_private;
 	struct virtio_net_config *config;
 	struct virtio_net_config local_config;
-	struct rte_pci_device *pci_dev = eth_dev->pci_dev;
+	struct rte_pci_device *pci_dev;
 	int ret;
 
 	/* Reset the device although not necessary at startup */
@@ -1210,7 +1224,9 @@ virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
 	else
 		eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
 
-	rte_eth_copy_pci_info(eth_dev, pci_dev);
+	pci_dev = virtio_dev_to_pci(eth_dev);
+	if (pci_dev)
+		rte_eth_copy_pci_info(eth_dev, pci_dev);
 
 	rx_func_get(eth_dev);
 
@@ -1278,10 +1294,9 @@ virtio_init_device(struct rte_eth_dev *eth_dev, uint64_t req_features)
 		return ret;
 	vtpci_reinit_complete(hw);
 
-	if (pci_dev)
-		PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
-			eth_dev->data->port_id, pci_dev->id.vendor_id,
-			pci_dev->id.device_id);
+	PMD_INIT_LOG(DEBUG, "port %d vendorID=0x%x deviceID=0x%x",
+		     eth_dev->data->port_id, pci_dev->id.vendor_id,
+		     pci_dev->id.device_id);
 
 	return 0;
 }
@@ -1294,7 +1309,7 @@ int
 eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
 {
 	struct virtio_hw *hw = eth_dev->data->dev_private;
-	struct rte_pci_device *pci_dev;
+	struct rte_pci_device *pci_dev = virtio_dev_to_pci(eth_dev);
 	uint32_t dev_flags = RTE_ETH_DEV_DETACHABLE;
 	int ret;
 
@@ -1317,8 +1332,6 @@ eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
 		return -ENOMEM;
 	}
 
-	pci_dev = eth_dev->pci_dev;
-
 	if (pci_dev) {
 		ret = vtpci_init(pci_dev, hw, &dev_flags);
 		if (ret)
@@ -1333,9 +1346,19 @@ eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
 		return ret;
 
 	/* Setup interrupt callback  */
-	if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
-		rte_intr_callback_register(&pci_dev->intr_handle,
-			virtio_interrupt_handler, eth_dev);
+	if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
+		struct rte_intr_handle *intr_handle
+			= virtio_dev_to_intr_handle(eth_dev);
+
+		if (!intr_handle) {
+			PMD_INIT_LOG(ERR,
+				     "Link state option not valid without pci");
+			return -EINVAL;
+		}
+
+		rte_intr_callback_register(intr_handle,
+					   virtio_interrupt_handler, eth_dev);
+	}
 
 	return 0;
 }
@@ -1343,7 +1366,7 @@ eth_virtio_dev_init(struct rte_eth_dev *eth_dev)
 static int
 eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
 {
-	struct rte_pci_device *pci_dev;
+	struct rte_pci_device *pci_dev = virtio_dev_to_pci(eth_dev);
 
 	PMD_INIT_FUNC_TRACE();
 
@@ -1352,7 +1375,6 @@ eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
 
 	virtio_dev_stop(eth_dev);
 	virtio_dev_close(eth_dev);
-	pci_dev = eth_dev->pci_dev;
 
 	eth_dev->dev_ops = NULL;
 	eth_dev->tx_pkt_burst = NULL;
@@ -1362,11 +1384,18 @@ eth_virtio_dev_uninit(struct rte_eth_dev *eth_dev)
 	eth_dev->data->mac_addrs = NULL;
 
 	/* reset interrupt callback  */
-	if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
-		rte_intr_callback_unregister(&pci_dev->intr_handle,
-						virtio_interrupt_handler,
-						eth_dev);
-	rte_eal_pci_unmap_device(pci_dev);
+	if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) {
+		struct rte_intr_handle *intr_handle
+			= virtio_dev_to_intr_handle(eth_dev);
+
+		if (intr_handle)
+			rte_intr_callback_unregister(intr_handle,
+						     virtio_interrupt_handler,
+						     eth_dev);
+	}
+
+	if (pci_dev)
+		rte_eal_pci_unmap_device(pci_dev);
 
 	PMD_INIT_LOG(DEBUG, "dev_uninit completed");
 
@@ -1476,12 +1505,20 @@ virtio_dev_start(struct rte_eth_dev *dev)
 
 	/* check if lsc interrupt feature is enabled */
 	if (dev->data->dev_conf.intr_conf.lsc) {
+		struct rte_intr_handle *intr_handle
+			= virtio_dev_to_intr_handle(dev);
+
+		if (!intr_handle) {
+			PMD_DRV_LOG(ERR, "link status not supported by bus");
+			return -ENOTSUP;
+		}
+
 		if (!(dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)) {
 			PMD_DRV_LOG(ERR, "link status not supported by host");
 			return -ENOTSUP;
 		}
 
-		if (rte_intr_enable(&dev->pci_dev->intr_handle) < 0) {
+		if (rte_intr_enable(intr_handle) < 0) {
 			PMD_DRV_LOG(ERR, "interrupt enable failed");
 			return -EIO;
 		}
@@ -1573,12 +1610,13 @@ static void virtio_dev_free_mbufs(struct rte_eth_dev *dev)
 static void
 virtio_dev_stop(struct rte_eth_dev *dev)
 {
+	struct rte_intr_handle *intr_handle = virtio_dev_to_intr_handle(dev);
 	struct rte_eth_link link;
 
 	PMD_INIT_LOG(DEBUG, "stop");
 
-	if (dev->data->dev_conf.intr_conf.lsc)
-		rte_intr_disable(&dev->pci_dev->intr_handle);
+	if (dev->data->dev_conf.intr_conf.lsc && intr_handle)
+		rte_intr_disable(intr_handle);
 
 	memset(&link, 0, sizeof(link));
 	virtio_dev_atomic_write_link_status(dev, &link);
@@ -1624,9 +1662,7 @@ virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
 	uint64_t tso_mask;
 	struct virtio_hw *hw = dev->data->dev_private;
 
-	if (dev->pci_dev)
-		dev_info->driver_name = dev->driver->pci_drv.driver.name;
-	else
+	if (!dev->pci_dev)
 		dev_info->driver_name = "virtio_user PMD";
 	dev_info->max_rx_queues =
 		RTE_MIN(hw->max_queue_pairs, VIRTIO_MAX_RX_QUEUES);
-- 
2.11.0

  parent reply	other threads:[~2016-12-19 22:00 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-19 21:59 [dpdk-dev] [RFC v2 00/13] Generalize rte_eth_dev model Stephen Hemminger
2016-12-19 21:59 ` [dpdk-dev] [PATCH 01/13] ethdev: increase length ethernet device internal name Stephen Hemminger
2016-12-20  6:53   ` Shreyansh Jain
2016-12-20 17:14     ` Stephen Hemminger
2016-12-19 21:59 ` [dpdk-dev] [PATCH 02/13] rte_device: make driver pointer const Stephen Hemminger
2016-12-20 11:14   ` Jan Blunck
2016-12-19 21:59 ` [dpdk-dev] [PATCH 03/13] eal: define container_of macro Stephen Hemminger
2016-12-19 21:59 ` [dpdk-dev] [PATCH 04/13] eal: introduce driver type Stephen Hemminger
2016-12-20  7:16   ` Shreyansh Jain
2016-12-20 17:16     ` Stephen Hemminger
2016-12-20 13:00   ` Jan Blunck
2016-12-20 17:09     ` Stephen Hemminger
2016-12-20 13:04   ` Ferruh Yigit
2016-12-19 21:59 ` [dpdk-dev] [PATCH 05/13] pmd: remove useless reset of dev_info->dev_pci Stephen Hemminger
2016-12-20 11:16   ` Jan Blunck
2016-12-19 21:59 ` [dpdk-dev] [PATCH 06/13] ethdev: make dev_info generic (not just PCI) Stephen Hemminger
2016-12-20 11:20   ` Jan Blunck
2016-12-19 21:59 ` [dpdk-dev] [PATCH 07/13] e1000: localize mapping from eth_dev to pci Stephen Hemminger
2016-12-19 21:59 ` [dpdk-dev] [PATCH 08/13] ixgbe: localize mapping from eth_dev to pci_device Stephen Hemminger
2016-12-19 21:59 ` [dpdk-dev] [PATCH 09/13] i40e: localize mapping of eth_dev to pci Stephen Hemminger
2016-12-19 21:59 ` Stephen Hemminger [this message]
2016-12-19 21:59 ` [dpdk-dev] [PATCH 11/13] broadcom: localize mapping from " Stephen Hemminger
2016-12-19 21:59 ` [dpdk-dev] [PATCH 12/13] ethdev: change pci_dev to generic device Stephen Hemminger
2016-12-19 21:59 ` [dpdk-dev] [PATCH 13/13] hyperv: VMBUS support infrastucture Stephen Hemminger
2016-12-20 12:48 ` [dpdk-dev] [RFC v2 00/13] Generalize rte_eth_dev model Jan Blunck

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20161219215944.17226-11-sthemmin@microsoft.com \
    --to=stephen@networkplumber.org \
    --cc=dev@dpdk.org \
    --cc=sthemmin@microsoft.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).