diff mbox series

[10/40] net/virtio: add callback for device closing

Message ID 20201220211405.313012-11-maxime.coquelin@redhat.com (mailing list archive)
State Superseded, archived
Delegated to: Maxime Coquelin
Headers show
Series net/virtio: Virtio PMD rework | expand

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Maxime Coquelin Dec. 20, 2020, 9:13 p.m. UTC
This patch introduces a new callback for device closing,
making virtio_dev_close() bus-agnostic.

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 drivers/net/virtio/meson.build          |  2 --
 drivers/net/virtio/virtio_ethdev.c      | 13 +------------
 drivers/net/virtio/virtio_pci.c         | 25 +++++++++++++++++++++++++
 drivers/net/virtio/virtio_pci.h         |  2 ++
 drivers/net/virtio/virtio_user_ethdev.c | 11 +++++++++++
 5 files changed, 39 insertions(+), 14 deletions(-)

Comments

Chenbo Xia Dec. 30, 2020, 3:07 a.m. UTC | #1
> -----Original Message-----
> From: Maxime Coquelin <maxime.coquelin@redhat.com>
> Sent: Monday, December 21, 2020 5:14 AM
> To: dev@dpdk.org; Xia, Chenbo <chenbo.xia@intel.com>; olivier.matz@6wind.com;
> amorenoz@redhat.com; david.marchand@redhat.com
> Cc: Maxime Coquelin <maxime.coquelin@redhat.com>
> Subject: [PATCH 10/40] net/virtio: add callback for device closing
> 
> This patch introduces a new callback for device closing,
> making virtio_dev_close() bus-agnostic.
> 
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
> ---
>  drivers/net/virtio/meson.build          |  2 --
>  drivers/net/virtio/virtio_ethdev.c      | 13 +------------
>  drivers/net/virtio/virtio_pci.c         | 25 +++++++++++++++++++++++++
>  drivers/net/virtio/virtio_pci.h         |  2 ++
>  drivers/net/virtio/virtio_user_ethdev.c | 11 +++++++++++
>  5 files changed, 39 insertions(+), 14 deletions(-)
> 
> diff --git a/drivers/net/virtio/meson.build b/drivers/net/virtio/meson.build
> index 8e0f1a9951..0b62418f33 100644
> --- a/drivers/net/virtio/meson.build
> +++ b/drivers/net/virtio/meson.build
> @@ -37,8 +37,6 @@ elif arch_subdir == 'arm' and
> host_machine.cpu_family().startswith('aarch64')
>  endif
> 
>  if is_linux
> -	dpdk_conf.set('RTE_VIRTIO_USER', 1)
> -
>  	sources += files('virtio_user_ethdev.c',
>  		'virtio_user/vhost_kernel.c',
>  		'virtio_user/vhost_kernel_tap.c',
> diff --git a/drivers/net/virtio/virtio_ethdev.c
> b/drivers/net/virtio/virtio_ethdev.c
> index 13e2ec998a..00aa38e4ef 100644
> --- a/drivers/net/virtio/virtio_ethdev.c
> +++ b/drivers/net/virtio/virtio_ethdev.c
> @@ -718,18 +718,7 @@ virtio_dev_close(struct rte_eth_dev *dev)
>  	virtio_dev_free_mbufs(dev);
>  	virtio_free_queues(hw);
> 
> -#ifdef RTE_VIRTIO_USER
> -	if (hw->bus_type == VIRTIO_BUS_USER)
> -		virtio_user_dev_uninit(dev->data->dev_private);
> -	else
> -#endif
> -	if (dev->device) {
> -		rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(dev));
> -		if (hw->bus_type == VIRTIO_BUS_PCI_LEGACY)
> -			rte_pci_ioport_unmap(VTPCI_IO(hw));
> -	}
> -
> -	return 0;
> +	return VTPCI_OPS(hw)->dev_close(hw);
>  }
> 
>  static int
> diff --git a/drivers/net/virtio/virtio_pci.c b/drivers/net/virtio/virtio_pci.c
> index 7f0c066968..599d8afa6b 100644
> --- a/drivers/net/virtio/virtio_pci.c
> +++ b/drivers/net/virtio/virtio_pci.c
> @@ -241,6 +241,17 @@ legacy_notify_queue(struct virtio_hw *hw, struct
> virtqueue *vq)
>  		VIRTIO_PCI_QUEUE_NOTIFY);
>  }
> 
> +static int
> +legacy_dev_close(struct virtio_hw *hw)
> +{
> +	struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
> +
> +	rte_pci_unmap_device(dev->pci_dev);
> +	rte_pci_ioport_unmap(VTPCI_IO(hw));
> +
> +	return 0;
> +}
> +
>  const struct virtio_pci_ops legacy_ops = {
>  	.read_dev_cfg	= legacy_read_dev_config,
>  	.write_dev_cfg	= legacy_write_dev_config,
> @@ -255,6 +266,7 @@ const struct virtio_pci_ops legacy_ops = {
>  	.setup_queue	= legacy_setup_queue,
>  	.del_queue	= legacy_del_queue,
>  	.notify_queue	= legacy_notify_queue,
> +	.dev_close	= legacy_dev_close,
>  };
> 
>  static inline void
> @@ -446,6 +458,16 @@ modern_notify_queue(struct virtio_hw *hw, struct
> virtqueue *vq)
>  	rte_write32(notify_data, vq->notify_addr);
>  }
> 
> +static int
> +modern_dev_close(struct virtio_hw *hw)
> +{
> +	struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
> +
> +	rte_pci_unmap_device(dev->pci_dev);
> +
> +	return 0;
> +}
> +
>  const struct virtio_pci_ops modern_ops = {
>  	.read_dev_cfg	= modern_read_dev_config,
>  	.write_dev_cfg	= modern_write_dev_config,
> @@ -460,6 +482,7 @@ const struct virtio_pci_ops modern_ops = {
>  	.setup_queue	= modern_setup_queue,
>  	.del_queue	= modern_del_queue,
>  	.notify_queue	= modern_notify_queue,
> +	.dev_close	= modern_dev_close,
>  };
> 
> 
> @@ -691,6 +714,8 @@ vtpci_init(struct rte_pci_device *pci_dev, struct
> virtio_pci_dev *dev)
>  {
>  	struct virtio_hw *hw = &dev->hw;
> 
> +	dev->pci_dev = pci_dev;
> +
>  	/*
>  	 * Try if we can succeed reading virtio pci caps, which exists
>  	 * only on modern pci device. If failed, we fallback to legacy
> diff --git a/drivers/net/virtio/virtio_pci.h b/drivers/net/virtio/virtio_pci.h
> index 3e245ed630..4f7d0e479e 100644
> --- a/drivers/net/virtio/virtio_pci.h
> +++ b/drivers/net/virtio/virtio_pci.h
> @@ -239,6 +239,7 @@ struct virtio_pci_ops {
>  	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;
> @@ -291,6 +292,7 @@ struct virtio_hw {
> 
>  struct virtio_pci_dev {
>  	struct virtio_hw hw;
> +	struct rte_pci_device *pci_dev;
>  	bool modern;
>  };
> 
> diff --git a/drivers/net/virtio/virtio_user_ethdev.c
> b/drivers/net/virtio/virtio_user_ethdev.c
> index f4775ff141..f9a2dbae71 100644
> --- a/drivers/net/virtio/virtio_user_ethdev.c
> +++ b/drivers/net/virtio/virtio_user_ethdev.c
> @@ -462,6 +462,16 @@ virtio_user_notify_queue(struct virtio_hw *hw, struct
> virtqueue *vq)
>  			    strerror(errno));
>  }
> 
> +static int
> +virtio_user_dev_close(struct virtio_hw *hw)
> +{
> +	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
> +
> +	virtio_user_dev_uninit(dev);
> +
> +	return 0;
> +}
> +
>  const struct virtio_pci_ops virtio_user_ops = {
>  	.read_dev_cfg	= virtio_user_read_dev_config,
>  	.write_dev_cfg	= virtio_user_write_dev_config,
> @@ -476,6 +486,7 @@ const struct virtio_pci_ops virtio_user_ops = {
>  	.setup_queue	= virtio_user_setup_queue,
>  	.del_queue	= virtio_user_del_queue,
>  	.notify_queue	= virtio_user_notify_queue,
> +	.dev_close	= virtio_user_dev_close,
>  };
> 
>  static const char *valid_args[] = {
> --
> 2.29.2

Reviewed-by: Chenbo Xia <chenbo.xia@intel.com>
David Marchand Jan. 6, 2021, 9:33 a.m. UTC | #2
On Sun, Dec 20, 2020 at 10:14 PM Maxime Coquelin
<maxime.coquelin@redhat.com> wrote:
>
> This patch introduces a new callback for device closing,
> making virtio_dev_close() bus-agnostic.
>
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>

Reviewed-by: David Marchand <david.marchand@redhat.com>
diff mbox series

Patch

diff --git a/drivers/net/virtio/meson.build b/drivers/net/virtio/meson.build
index 8e0f1a9951..0b62418f33 100644
--- a/drivers/net/virtio/meson.build
+++ b/drivers/net/virtio/meson.build
@@ -37,8 +37,6 @@  elif arch_subdir == 'arm' and host_machine.cpu_family().startswith('aarch64')
 endif
 
 if is_linux
-	dpdk_conf.set('RTE_VIRTIO_USER', 1)
-
 	sources += files('virtio_user_ethdev.c',
 		'virtio_user/vhost_kernel.c',
 		'virtio_user/vhost_kernel_tap.c',
diff --git a/drivers/net/virtio/virtio_ethdev.c b/drivers/net/virtio/virtio_ethdev.c
index 13e2ec998a..00aa38e4ef 100644
--- a/drivers/net/virtio/virtio_ethdev.c
+++ b/drivers/net/virtio/virtio_ethdev.c
@@ -718,18 +718,7 @@  virtio_dev_close(struct rte_eth_dev *dev)
 	virtio_dev_free_mbufs(dev);
 	virtio_free_queues(hw);
 
-#ifdef RTE_VIRTIO_USER
-	if (hw->bus_type == VIRTIO_BUS_USER)
-		virtio_user_dev_uninit(dev->data->dev_private);
-	else
-#endif
-	if (dev->device) {
-		rte_pci_unmap_device(RTE_ETH_DEV_TO_PCI(dev));
-		if (hw->bus_type == VIRTIO_BUS_PCI_LEGACY)
-			rte_pci_ioport_unmap(VTPCI_IO(hw));
-	}
-
-	return 0;
+	return VTPCI_OPS(hw)->dev_close(hw);
 }
 
 static int
diff --git a/drivers/net/virtio/virtio_pci.c b/drivers/net/virtio/virtio_pci.c
index 7f0c066968..599d8afa6b 100644
--- a/drivers/net/virtio/virtio_pci.c
+++ b/drivers/net/virtio/virtio_pci.c
@@ -241,6 +241,17 @@  legacy_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
 		VIRTIO_PCI_QUEUE_NOTIFY);
 }
 
+static int
+legacy_dev_close(struct virtio_hw *hw)
+{
+	struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
+
+	rte_pci_unmap_device(dev->pci_dev);
+	rte_pci_ioport_unmap(VTPCI_IO(hw));
+
+	return 0;
+}
+
 const struct virtio_pci_ops legacy_ops = {
 	.read_dev_cfg	= legacy_read_dev_config,
 	.write_dev_cfg	= legacy_write_dev_config,
@@ -255,6 +266,7 @@  const struct virtio_pci_ops legacy_ops = {
 	.setup_queue	= legacy_setup_queue,
 	.del_queue	= legacy_del_queue,
 	.notify_queue	= legacy_notify_queue,
+	.dev_close	= legacy_dev_close,
 };
 
 static inline void
@@ -446,6 +458,16 @@  modern_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
 	rte_write32(notify_data, vq->notify_addr);
 }
 
+static int
+modern_dev_close(struct virtio_hw *hw)
+{
+	struct virtio_pci_dev *dev = virtio_pci_get_dev(hw);
+
+	rte_pci_unmap_device(dev->pci_dev);
+
+	return 0;
+}
+
 const struct virtio_pci_ops modern_ops = {
 	.read_dev_cfg	= modern_read_dev_config,
 	.write_dev_cfg	= modern_write_dev_config,
@@ -460,6 +482,7 @@  const struct virtio_pci_ops modern_ops = {
 	.setup_queue	= modern_setup_queue,
 	.del_queue	= modern_del_queue,
 	.notify_queue	= modern_notify_queue,
+	.dev_close	= modern_dev_close,
 };
 
 
@@ -691,6 +714,8 @@  vtpci_init(struct rte_pci_device *pci_dev, struct virtio_pci_dev *dev)
 {
 	struct virtio_hw *hw = &dev->hw;
 
+	dev->pci_dev = pci_dev;
+
 	/*
 	 * Try if we can succeed reading virtio pci caps, which exists
 	 * only on modern pci device. If failed, we fallback to legacy
diff --git a/drivers/net/virtio/virtio_pci.h b/drivers/net/virtio/virtio_pci.h
index 3e245ed630..4f7d0e479e 100644
--- a/drivers/net/virtio/virtio_pci.h
+++ b/drivers/net/virtio/virtio_pci.h
@@ -239,6 +239,7 @@  struct virtio_pci_ops {
 	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;
@@ -291,6 +292,7 @@  struct virtio_hw {
 
 struct virtio_pci_dev {
 	struct virtio_hw hw;
+	struct rte_pci_device *pci_dev;
 	bool modern;
 };
 
diff --git a/drivers/net/virtio/virtio_user_ethdev.c b/drivers/net/virtio/virtio_user_ethdev.c
index f4775ff141..f9a2dbae71 100644
--- a/drivers/net/virtio/virtio_user_ethdev.c
+++ b/drivers/net/virtio/virtio_user_ethdev.c
@@ -462,6 +462,16 @@  virtio_user_notify_queue(struct virtio_hw *hw, struct virtqueue *vq)
 			    strerror(errno));
 }
 
+static int
+virtio_user_dev_close(struct virtio_hw *hw)
+{
+	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
+
+	virtio_user_dev_uninit(dev);
+
+	return 0;
+}
+
 const struct virtio_pci_ops virtio_user_ops = {
 	.read_dev_cfg	= virtio_user_read_dev_config,
 	.write_dev_cfg	= virtio_user_write_dev_config,
@@ -476,6 +486,7 @@  const struct virtio_pci_ops virtio_user_ops = {
 	.setup_queue	= virtio_user_setup_queue,
 	.del_queue	= virtio_user_del_queue,
 	.notify_queue	= virtio_user_notify_queue,
+	.dev_close	= virtio_user_dev_close,
 };
 
 static const char *valid_args[] = {