> -----Original Message-----
> From: Maxime Coquelin <maxime.coque...@redhat.com>
> Sent: Monday, December 21, 2020 5:14 AM
> To: dev@dpdk.org; Xia, Chenbo <chenbo....@intel.com>; olivier.m...@6wind.com;
> amore...@redhat.com; david.march...@redhat.com
> Cc: Maxime Coquelin <maxime.coque...@redhat.com>
> Subject: [PATCH 16/40] 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 <maxime.coque...@redhat.com>
> ---
>  drivers/net/virtio/virtio.h             | 75 ++++++++++++++++++++++
>  drivers/net/virtio/virtio_ethdev.c      | 20 +++---
>  drivers/net/virtio/virtio_pci.c         | 26 ++++----
>  drivers/net/virtio/virtio_pci.h         | 82 ++-----------------------
>  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, 110 insertions(+), 108 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..eb078bc227
> --- /dev/null
> +++ b/drivers/net/virtio/virtio.h
> @@ -0,0 +1,75 @@
> +/* SPDX-License-Identifier: BSD-3-Clause
> + * Copyright(c) 2010-2014 Intel Corporation
> + * Copyright(c) 2020 Red Hat, Inc.
> + */
> +
> +#ifndef _VIRTIO_H_
> +#define _VIRTIO_H_
> +
> +#include <rte_ether.h>
> +
> +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);
> +     int (*dev_close)(struct virtio_hw *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_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 86d8930e78..80b3fdba9e 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
> @@ -1290,7 +1290,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);
> 
> @@ -1315,7 +1315,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) {
>               PMD_INIT_LOG(ERR, "Features not OK at bus level\n");
>               return -1;
>       }
> @@ -1551,7 +1551,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;
> @@ -1569,7 +1569,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);
>  }
> @@ -2081,7 +2081,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;
> @@ -2411,7 +2411,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 230a438bf7..df69fcdd45 100644
> --- a/drivers/net/virtio/virtio_pci.c
> +++ b/drivers/net/virtio/virtio_pci.c
> @@ -267,7 +267,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,
> @@ -515,7 +515,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,
> @@ -538,14 +538,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
> @@ -558,7 +558,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;
>  }
> @@ -566,9 +566,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
> @@ -581,21 +581,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 *
> @@ -772,7 +772,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;
>       }
> @@ -791,7 +791,7 @@ 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:
> diff --git a/drivers/net/virtio/virtio_pci.h b/drivers/net/virtio/virtio_pci.h
> index c3db36d2fc..8b07c4a369 100644
> --- a/drivers/net/virtio/virtio_pci.h
> +++ b/drivers/net/virtio/virtio_pci.h
> @@ -12,6 +12,8 @@
>  #include <rte_bus_pci.h>
>  #include <rte_ethdev_driver.h>
> 
> +#include "virtio.h"
> +
>  struct virtqueue;
>  struct virtnet_ctl;
> 
> @@ -214,68 +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);
> -     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;
> @@ -290,19 +230,6 @@ struct virtio_pci_dev {
> 
>  #define virtio_pci_get_dev(hw) container_of(hw, 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
> @@ -380,8 +307,7 @@ enum virtio_msix_status vtpci_msix_detect(struct
> rte_pci_device *dev);
>  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 17342ae7d8..347dc291e8 100644
> --- a/drivers/net/virtio/virtio_pci_ethdev.c
> +++ b/drivers/net/virtio/virtio_pci_ethdev.c
> @@ -19,6 +19,7 @@
>  #include <rte_dev.h>
>  #include <rte_kvargs.h>
> 
> +#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 1420db32be..14468ddf52 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.
> @@ -701,7 +701,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 e4a1393816..9d2089766b 100644
> --- a/drivers/net/virtio/virtqueue.h
> +++ b/drivers/net/virtio/virtqueue.h
> @@ -564,7 +564,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.29.2

Reviewed-by: Chenbo Xia <chenbo....@intel.com>

Reply via email to