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 <sthem...@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