From f8b60756360491439578266ff0ee5eb2bcf39dc3 Mon Sep 17 00:00:00 2001 From: Maxime Coquelin Date: Tue, 26 Jan 2021 11:16:13 +0100 Subject: [PATCH] net/virtio: introduce generic virtio header This patch moves virtio_hw and virtio callbacks into a generic virtio header, now that they have been curated from PCI references. Signed-off-by: Maxime Coquelin Reviewed-by: Chenbo Xia --- drivers/net/virtio/virtio.h | 74 ++++++++++++++++++++++ drivers/net/virtio/virtio_ethdev.c | 32 +++++----- drivers/net/virtio/virtio_pci.c | 28 ++++----- drivers/net/virtio/virtio_pci.h | 84 ++----------------------- drivers/net/virtio/virtio_pci_ethdev.c | 5 +- drivers/net/virtio/virtio_user_ethdev.c | 8 +-- drivers/net/virtio/virtqueue.h | 2 +- 7 files changed, 116 insertions(+), 117 deletions(-) create mode 100644 drivers/net/virtio/virtio.h diff --git a/drivers/net/virtio/virtio.h b/drivers/net/virtio/virtio.h new file mode 100644 index 0000000000..e55e3329f9 --- /dev/null +++ b/drivers/net/virtio/virtio.h @@ -0,0 +1,74 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2010-2014 Intel Corporation + * Copyright(c) 2021 Red Hat, Inc. + */ + +#ifndef _VIRTIO_H_ +#define _VIRTIO_H_ + +#include + +struct virtio_hw { + struct virtqueue **vqs; + uint64_t guest_features; + uint16_t vtnet_hdr_size; + uint8_t started; + uint8_t weak_barriers; + uint8_t vlan_strip; + uint8_t has_tx_offload; + uint8_t has_rx_offload; + uint8_t use_vec_rx; + uint8_t use_vec_tx; + uint8_t use_inorder_rx; + uint8_t use_inorder_tx; + uint8_t opened; + uint16_t port_id; + uint8_t mac_addr[RTE_ETHER_ADDR_LEN]; + uint32_t speed; /* link speed in MB */ + uint8_t duplex; + uint8_t use_msix; + uint16_t max_mtu; + /* + * App management thread and virtio interrupt handler thread + * both can change device state, this lock is meant to avoid + * such a contention. + */ + rte_spinlock_t state_lock; + struct rte_mbuf **inject_pkts; + uint16_t max_queue_pairs; + uint64_t req_guest_features; + struct virtnet_ctl *cvq; +}; + +struct virtio_ops { + void (*read_dev_cfg)(struct virtio_hw *hw, size_t offset, void *dst, int len); + void (*write_dev_cfg)(struct virtio_hw *hw, size_t offset, const void *src, int len); + uint8_t (*get_status)(struct virtio_hw *hw); + void (*set_status)(struct virtio_hw *hw, uint8_t status); + uint64_t (*get_features)(struct virtio_hw *hw); + void (*set_features)(struct virtio_hw *hw, uint64_t features); + int (*features_ok)(struct virtio_hw *hw); + uint8_t (*get_isr)(struct virtio_hw *hw); + uint16_t (*set_config_irq)(struct virtio_hw *hw, uint16_t vec); + uint16_t (*set_queue_irq)(struct virtio_hw *hw, struct virtqueue *vq, uint16_t vec); + uint16_t (*get_queue_num)(struct virtio_hw *hw, uint16_t queue_id); + int (*setup_queue)(struct virtio_hw *hw, struct virtqueue *vq); + void (*del_queue)(struct virtio_hw *hw, struct virtqueue *vq); + void (*notify_queue)(struct virtio_hw *hw, struct virtqueue *vq); + void (*intr_detect)(struct virtio_hw *hw); + int (*dev_close)(struct virtio_hw *hw); +}; + +/* + * This structure stores per-process data. Only virtio_ops for now. + */ +struct virtio_hw_internal { + const struct virtio_ops *virtio_ops; +}; + +#define VIRTIO_OPS(hw) (virtio_hw_internal[(hw)->port_id].virtio_ops) + +extern struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS]; + + +#endif /* _VIRTIO_H_ */ diff --git a/drivers/net/virtio/virtio_ethdev.c b/drivers/net/virtio/virtio_ethdev.c index 0e1c39e5a3..caaed1e713 100644 --- a/drivers/net/virtio/virtio_ethdev.c +++ b/drivers/net/virtio/virtio_ethdev.c @@ -446,7 +446,7 @@ virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx) * Read the virtqueue size from the Queue Size field * Always power of 2 and if 0 virtqueue does not exist */ - vq_size = VTPCI_OPS(hw)->get_queue_num(hw, vtpci_queue_idx); + vq_size = VIRTIO_OPS(hw)->get_queue_num(hw, vtpci_queue_idx); PMD_INIT_LOG(DEBUG, "vq_size: %u", vq_size); if (vq_size == 0) { PMD_INIT_LOG(ERR, "virtqueue does not exist"); @@ -608,7 +608,7 @@ virtio_init_queue(struct rte_eth_dev *dev, uint16_t vtpci_queue_idx) } } - if (VTPCI_OPS(hw)->setup_queue(hw, vq) < 0) { + if (VIRTIO_OPS(hw)->setup_queue(hw, vq) < 0) { PMD_INIT_LOG(ERR, "setup_queue failed"); return -EINVAL; } @@ -703,7 +703,7 @@ virtio_dev_close(struct rte_eth_dev *dev) /* reset the NIC */ if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) - VTPCI_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR); + VIRTIO_OPS(hw)->set_config_irq(hw, VIRTIO_MSI_NO_VECTOR); if (intr_conf->rxq) virtio_queues_unbind_intr(dev); @@ -718,7 +718,7 @@ virtio_dev_close(struct rte_eth_dev *dev) virtio_dev_free_mbufs(dev); virtio_free_queues(hw); - return VTPCI_OPS(hw)->dev_close(hw); + return VIRTIO_OPS(hw)->dev_close(hw); } static int @@ -1261,8 +1261,8 @@ virtio_intr_unmask(struct rte_eth_dev *dev) if (rte_intr_ack(dev->intr_handle) < 0) return -1; - if (VTPCI_OPS(hw)->intr_detect) - VTPCI_OPS(hw)->intr_detect(hw); + if (VIRTIO_OPS(hw)->intr_detect) + VIRTIO_OPS(hw)->intr_detect(hw); return 0; } @@ -1275,8 +1275,8 @@ virtio_intr_enable(struct rte_eth_dev *dev) if (rte_intr_enable(dev->intr_handle) < 0) return -1; - if (VTPCI_OPS(hw)->intr_detect) - VTPCI_OPS(hw)->intr_detect(hw); + if (VIRTIO_OPS(hw)->intr_detect) + VIRTIO_OPS(hw)->intr_detect(hw); return 0; } @@ -1289,8 +1289,8 @@ virtio_intr_disable(struct rte_eth_dev *dev) if (rte_intr_disable(dev->intr_handle) < 0) return -1; - if (VTPCI_OPS(hw)->intr_detect) - VTPCI_OPS(hw)->intr_detect(hw); + if (VIRTIO_OPS(hw)->intr_detect) + VIRTIO_OPS(hw)->intr_detect(hw); return 0; } @@ -1305,7 +1305,7 @@ virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features) req_features); /* Read device(host) feature bits */ - host_features = VTPCI_OPS(hw)->get_features(hw); + host_features = VIRTIO_OPS(hw)->get_features(hw); PMD_INIT_LOG(DEBUG, "host_features before negotiate = %" PRIx64, host_features); @@ -1330,7 +1330,7 @@ virtio_negotiate_features(struct virtio_hw *hw, uint64_t req_features) PMD_INIT_LOG(DEBUG, "features after negotiate = %" PRIx64, hw->guest_features); - if (VTPCI_OPS(hw)->features_ok(hw) < 0) + if (VIRTIO_OPS(hw)->features_ok(hw) < 0) return -1; if (vtpci_with_feature(hw, VIRTIO_F_VERSION_1)) { @@ -1564,7 +1564,7 @@ virtio_queues_bind_intr(struct rte_eth_dev *dev) PMD_INIT_LOG(INFO, "queue/interrupt binding"); for (i = 0; i < dev->data->nb_rx_queues; ++i) { dev->intr_handle->intr_vec[i] = i + 1; - if (VTPCI_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) == + if (VIRTIO_OPS(hw)->set_queue_irq(hw, hw->vqs[i * 2], i + 1) == VIRTIO_MSI_NO_VECTOR) { PMD_DRV_LOG(ERR, "failed to set queue vector"); return -EBUSY; @@ -1582,7 +1582,7 @@ virtio_queues_unbind_intr(struct rte_eth_dev *dev) PMD_INIT_LOG(INFO, "queue/interrupt unbinding"); for (i = 0; i < dev->data->nb_rx_queues; ++i) - VTPCI_OPS(hw)->set_queue_irq(hw, + VIRTIO_OPS(hw)->set_queue_irq(hw, hw->vqs[i * VTNET_CQ], VIRTIO_MSI_NO_VECTOR); } @@ -2092,7 +2092,7 @@ virtio_dev_configure(struct rte_eth_dev *dev) if (dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC) /* Enable vector (0) for Link State Intrerrupt */ - if (VTPCI_OPS(hw)->set_config_irq(hw, 0) == + if (VIRTIO_OPS(hw)->set_config_irq(hw, 0) == VIRTIO_MSI_NO_VECTOR) { PMD_DRV_LOG(ERR, "failed to set config vector"); return -EBUSY; @@ -2433,7 +2433,7 @@ virtio_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info) dev_info->max_rx_pktlen = VIRTIO_MAX_RX_PKTLEN; dev_info->max_mac_addrs = VIRTIO_MAX_MAC_ADDRS; - host_features = VTPCI_OPS(hw)->get_features(hw); + host_features = VIRTIO_OPS(hw)->get_features(hw); dev_info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP; dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_JUMBO_FRAME; if (host_features & (1ULL << VIRTIO_NET_F_GUEST_CSUM)) { diff --git a/drivers/net/virtio/virtio_pci.c b/drivers/net/virtio/virtio_pci.c index 3fe0631a30..e0d8a53ca6 100644 --- a/drivers/net/virtio/virtio_pci.c +++ b/drivers/net/virtio/virtio_pci.c @@ -325,7 +325,7 @@ legacy_dev_close(struct virtio_hw *hw) return 0; } -const struct virtio_pci_ops legacy_ops = { +const struct virtio_ops legacy_ops = { .read_dev_cfg = legacy_read_dev_config, .write_dev_cfg = legacy_write_dev_config, .get_status = legacy_get_status, @@ -584,7 +584,7 @@ modern_dev_close(struct virtio_hw *hw) return 0; } -const struct virtio_pci_ops modern_ops = { +const struct virtio_ops modern_ops = { .read_dev_cfg = modern_read_dev_config, .write_dev_cfg = modern_write_dev_config, .get_status = modern_get_status, @@ -608,14 +608,14 @@ void vtpci_read_dev_config(struct virtio_hw *hw, size_t offset, void *dst, int length) { - VTPCI_OPS(hw)->read_dev_cfg(hw, offset, dst, length); + VIRTIO_OPS(hw)->read_dev_cfg(hw, offset, dst, length); } void vtpci_write_dev_config(struct virtio_hw *hw, size_t offset, const void *src, int length) { - VTPCI_OPS(hw)->write_dev_cfg(hw, offset, src, length); + VIRTIO_OPS(hw)->write_dev_cfg(hw, offset, src, length); } uint64_t @@ -628,7 +628,7 @@ vtpci_negotiate_features(struct virtio_hw *hw, uint64_t host_features) * host all support. */ features = host_features & hw->guest_features; - VTPCI_OPS(hw)->set_features(hw, features); + VIRTIO_OPS(hw)->set_features(hw, features); return features; } @@ -636,9 +636,9 @@ vtpci_negotiate_features(struct virtio_hw *hw, uint64_t host_features) void vtpci_reset(struct virtio_hw *hw) { - VTPCI_OPS(hw)->set_status(hw, VIRTIO_CONFIG_STATUS_RESET); + VIRTIO_OPS(hw)->set_status(hw, VIRTIO_CONFIG_STATUS_RESET); /* flush status write */ - VTPCI_OPS(hw)->get_status(hw); + VIRTIO_OPS(hw)->get_status(hw); } void @@ -651,21 +651,21 @@ void vtpci_set_status(struct virtio_hw *hw, uint8_t status) { if (status != VIRTIO_CONFIG_STATUS_RESET) - status |= VTPCI_OPS(hw)->get_status(hw); + status |= VIRTIO_OPS(hw)->get_status(hw); - VTPCI_OPS(hw)->set_status(hw, status); + VIRTIO_OPS(hw)->set_status(hw, status); } uint8_t vtpci_get_status(struct virtio_hw *hw) { - return VTPCI_OPS(hw)->get_status(hw); + return VIRTIO_OPS(hw)->get_status(hw); } uint8_t vtpci_isr(struct virtio_hw *hw) { - return VTPCI_OPS(hw)->get_isr(hw); + return VIRTIO_OPS(hw)->get_isr(hw); } static void * @@ -842,7 +842,7 @@ vtpci_init(struct rte_pci_device *pci_dev, struct virtio_pci_dev *dev) */ if (virtio_read_caps(pci_dev, hw) == 0) { PMD_INIT_LOG(INFO, "modern virtio pci detected."); - virtio_hw_internal[hw->port_id].vtpci_ops = &modern_ops; + VIRTIO_OPS(hw) = &modern_ops; dev->modern = true; goto msix_detect; } @@ -861,11 +861,11 @@ vtpci_init(struct rte_pci_device *pci_dev, struct virtio_pci_dev *dev) return -1; } - virtio_hw_internal[hw->port_id].vtpci_ops = &legacy_ops; + VIRTIO_OPS(hw) = &legacy_ops; dev->modern = false; msix_detect: - VTPCI_OPS(hw)->intr_detect(hw); + VIRTIO_OPS(hw)->intr_detect(hw); return 0; } diff --git a/drivers/net/virtio/virtio_pci.h b/drivers/net/virtio/virtio_pci.h index e4becaae62..12fc0202f7 100644 --- a/drivers/net/virtio/virtio_pci.h +++ b/drivers/net/virtio/virtio_pci.h @@ -12,6 +12,8 @@ #include #include +#include "virtio.h" + struct virtqueue; struct virtnet_ctl; @@ -214,70 +216,6 @@ struct virtio_pci_common_cfg { uint32_t queue_used_hi; /* read-write */ }; -struct virtio_hw; - -struct virtio_pci_ops { - void (*read_dev_cfg)(struct virtio_hw *hw, size_t offset, - void *dst, int len); - void (*write_dev_cfg)(struct virtio_hw *hw, size_t offset, - const void *src, int len); - - uint8_t (*get_status)(struct virtio_hw *hw); - void (*set_status)(struct virtio_hw *hw, uint8_t status); - - uint64_t (*get_features)(struct virtio_hw *hw); - void (*set_features)(struct virtio_hw *hw, uint64_t features); - int (*features_ok)(struct virtio_hw *hw); - - uint8_t (*get_isr)(struct virtio_hw *hw); - - uint16_t (*set_config_irq)(struct virtio_hw *hw, uint16_t vec); - - uint16_t (*set_queue_irq)(struct virtio_hw *hw, struct virtqueue *vq, - uint16_t vec); - - uint16_t (*get_queue_num)(struct virtio_hw *hw, uint16_t queue_id); - int (*setup_queue)(struct virtio_hw *hw, struct virtqueue *vq); - void (*del_queue)(struct virtio_hw *hw, struct virtqueue *vq); - void (*notify_queue)(struct virtio_hw *hw, struct virtqueue *vq); - void (*intr_detect)(struct virtio_hw *hw); - int (*dev_close)(struct virtio_hw *hw); -}; - -struct virtio_net_config; - -struct virtio_hw { - struct virtqueue **vqs; - uint64_t guest_features; - uint16_t vtnet_hdr_size; - uint8_t started; - uint8_t weak_barriers; - uint8_t vlan_strip; - uint8_t has_tx_offload; - uint8_t has_rx_offload; - uint8_t use_vec_rx; - uint8_t use_vec_tx; - uint8_t use_inorder_rx; - uint8_t use_inorder_tx; - uint8_t opened; - uint16_t port_id; - uint8_t mac_addr[RTE_ETHER_ADDR_LEN]; - uint32_t speed; /* link speed in MB */ - uint8_t duplex; - uint8_t use_msix; - uint16_t max_mtu; - /* - * App management thread and virtio interrupt handler thread - * both can change device state, this lock is meant to avoid - * such a contention. - */ - rte_spinlock_t state_lock; - struct rte_mbuf **inject_pkts; - uint16_t max_queue_pairs; - uint64_t req_guest_features; - struct virtnet_ctl *cvq; -}; - struct virtio_pci_dev { struct virtio_hw hw; struct rte_pci_device *pci_dev; @@ -291,19 +229,6 @@ struct virtio_pci_dev { #define virtio_pci_get_dev(hwp) container_of(hwp, struct virtio_pci_dev, hw) -/* - * While virtio_hw is stored in shared memory, this structure stores - * some infos that may vary in the multiple process model locally. - * For example, the vtpci_ops pointer. - */ -struct virtio_hw_internal { - const struct virtio_pci_ops *vtpci_ops; -}; - -#define VTPCI_OPS(hw) (virtio_hw_internal[(hw)->port_id].vtpci_ops) - -extern struct virtio_hw_internal virtio_hw_internal[RTE_MAX_ETHPORTS]; - /* * This structure is just a reference to read * net device specific config space; it just a chodu structure @@ -379,8 +304,7 @@ uint8_t vtpci_isr(struct virtio_hw *); void vtpci_legacy_ioport_unmap(struct virtio_hw *hw); int vtpci_legacy_ioport_map(struct virtio_hw *hw); -extern const struct virtio_pci_ops legacy_ops; -extern const struct virtio_pci_ops modern_ops; -extern const struct virtio_pci_ops virtio_user_ops; +extern const struct virtio_ops legacy_ops; +extern const struct virtio_ops modern_ops; #endif /* _VIRTIO_PCI_H_ */ diff --git a/drivers/net/virtio/virtio_pci_ethdev.c b/drivers/net/virtio/virtio_pci_ethdev.c index e725c105af..1b818c4565 100644 --- a/drivers/net/virtio/virtio_pci_ethdev.c +++ b/drivers/net/virtio/virtio_pci_ethdev.c @@ -19,6 +19,7 @@ #include #include +#include "virtio.h" #include "virtio_ethdev.h" #include "virtio_pci.h" #include "virtio_logs.h" @@ -83,9 +84,9 @@ eth_virtio_pci_init(struct rte_eth_dev *eth_dev) } } else { if (dev->modern) - VTPCI_OPS(hw) = &modern_ops; + VIRTIO_OPS(hw) = &modern_ops; else - VTPCI_OPS(hw) = &legacy_ops; + VIRTIO_OPS(hw) = &legacy_ops; ret = virtio_remap_pci(RTE_ETH_DEV_TO_PCI(eth_dev), dev); if (ret < 0) { diff --git a/drivers/net/virtio/virtio_user_ethdev.c b/drivers/net/virtio/virtio_user_ethdev.c index fb8d45a6bb..659b4c6359 100644 --- a/drivers/net/virtio/virtio_user_ethdev.c +++ b/drivers/net/virtio/virtio_user_ethdev.c @@ -20,7 +20,7 @@ #include "virtio_ethdev.h" #include "virtio_logs.h" -#include "virtio_pci.h" +#include "virtio.h" #include "virtqueue.h" #include "virtio_rxtx.h" #include "virtio_user/virtio_user_dev.h" @@ -478,7 +478,7 @@ virtio_user_dev_close(struct virtio_hw *hw) return 0; } -const struct virtio_pci_ops virtio_user_ops = { +const struct virtio_ops virtio_user_ops = { .read_dev_cfg = virtio_user_read_dev_config, .write_dev_cfg = virtio_user_write_dev_config, .get_status = virtio_user_get_status, @@ -635,7 +635,7 @@ virtio_user_eth_dev_alloc(struct rte_vdev_device *vdev) hw->port_id = data->port_id; dev->port_id = data->port_id; - virtio_hw_internal[hw->port_id].vtpci_ops = &virtio_user_ops; + VIRTIO_OPS(hw) = &virtio_user_ops; /* * MSIX is required to enable LSC (see virtio_init_device). * Here just pretend that we support msix. @@ -692,7 +692,7 @@ virtio_user_pmd_probe(struct rte_vdev_device *vdev) dev = eth_dev->data->dev_private; hw = &dev->hw; - VTPCI_OPS(hw) = &virtio_user_ops; + VIRTIO_OPS(hw) = &virtio_user_ops; if (eth_virtio_dev_init(eth_dev) < 0) { PMD_INIT_LOG(ERR, "eth_virtio_dev_init fails"); diff --git a/drivers/net/virtio/virtqueue.h b/drivers/net/virtio/virtqueue.h index 3a9ce29069..6293cd2604 100644 --- a/drivers/net/virtio/virtqueue.h +++ b/drivers/net/virtio/virtqueue.h @@ -563,7 +563,7 @@ virtqueue_kick_prepare_packed(struct virtqueue *vq) static inline void virtqueue_notify(struct virtqueue *vq) { - VTPCI_OPS(vq->hw)->notify_queue(vq->hw, vq); + VIRTIO_OPS(vq->hw)->notify_queue(vq->hw, vq); } #ifdef RTE_LIBRTE_VIRTIO_DEBUG_DUMP -- 2.20.1