The reset device operation, usually is an operation that might fail from various reasons. For example, the controller might be in a bad state and can't answer to any request. Usually, the paravirt SW based virtio devices always succeed in reset operation but this is not the case for HW based virtio devices. This commit is also a preparation for adding a timeout mechanism for resetting virtio devices. Signed-off-by: Max Gurtovoy <mgurtovoy@xxxxxxxxxx> --- drivers/remoteproc/remoteproc_virtio.c | 3 ++- drivers/virtio/virtio.c | 22 +++++++++++++++------- drivers/virtio/virtio_mmio.c | 3 ++- drivers/virtio/virtio_pci_legacy.c | 3 ++- drivers/virtio/virtio_pci_modern.c | 3 ++- drivers/virtio/virtio_vdpa.c | 3 ++- include/linux/virtio_config.h | 5 +++-- 7 files changed, 28 insertions(+), 14 deletions(-) diff --git a/drivers/remoteproc/remoteproc_virtio.c b/drivers/remoteproc/remoteproc_virtio.c index 0cc617f76068..432f39288c15 100644 --- a/drivers/remoteproc/remoteproc_virtio.c +++ b/drivers/remoteproc/remoteproc_virtio.c @@ -191,7 +191,7 @@ static void rproc_virtio_set_status(struct virtio_device *vdev, u8 status) dev_dbg(&vdev->dev, "status: %d\n", status); } -static void rproc_virtio_reset(struct virtio_device *vdev) +static int rproc_virtio_reset(struct virtio_device *vdev) { struct rproc_vdev *rvdev = vdev_to_rvdev(vdev); struct fw_rsc_vdev *rsc; @@ -200,6 +200,7 @@ static void rproc_virtio_reset(struct virtio_device *vdev) rsc->status = 0; dev_dbg(&vdev->dev, "reset !\n"); + return 0; } /* provide the vdev features as retrieved from the firmware */ diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 4b15c00c0a0a..ddbfd5b5f3bd 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -338,7 +338,7 @@ int register_virtio_device(struct virtio_device *dev) /* Assign a unique device index and hence name. */ err = ida_simple_get(&virtio_index_ida, 0, 0, GFP_KERNEL); if (err < 0) - goto out; + goto out_err; dev->index = err; dev_set_name(&dev->dev, "virtio%u", dev->index); @@ -349,7 +349,9 @@ int register_virtio_device(struct virtio_device *dev) /* We always start by resetting the device, in case a previous * driver messed it up. This also tests that code path a little. */ - dev->config->reset(dev); + err = dev->config->reset(dev); + if (err) + goto out_ida; /* Acknowledge that we've seen the device. */ virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE); @@ -362,10 +364,14 @@ int register_virtio_device(struct virtio_device *dev) */ err = device_add(&dev->dev); if (err) - ida_simple_remove(&virtio_index_ida, dev->index); -out: - if (err) - virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED); + goto out_ida; + + return 0; + +out_ida: + ida_simple_remove(&virtio_index_ida, dev->index); +out_err: + virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED); return err; } EXPORT_SYMBOL_GPL(register_virtio_device); @@ -408,7 +414,9 @@ int virtio_device_restore(struct virtio_device *dev) /* We always start by resetting the device, in case a previous * driver messed it up. */ - dev->config->reset(dev); + ret = dev->config->reset(dev); + if (ret) + goto err; /* Acknowledge that we've seen the device. */ virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE); diff --git a/drivers/virtio/virtio_mmio.c b/drivers/virtio/virtio_mmio.c index 56128b9c46eb..12b8f048c48d 100644 --- a/drivers/virtio/virtio_mmio.c +++ b/drivers/virtio/virtio_mmio.c @@ -256,12 +256,13 @@ static void vm_set_status(struct virtio_device *vdev, u8 status) writel(status, vm_dev->base + VIRTIO_MMIO_STATUS); } -static void vm_reset(struct virtio_device *vdev) +static int vm_reset(struct virtio_device *vdev) { struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev); /* 0 status means a reset. */ writel(0, vm_dev->base + VIRTIO_MMIO_STATUS); + return 0; } diff --git a/drivers/virtio/virtio_pci_legacy.c b/drivers/virtio/virtio_pci_legacy.c index d62e9835aeec..ff4d9506aa65 100644 --- a/drivers/virtio/virtio_pci_legacy.c +++ b/drivers/virtio/virtio_pci_legacy.c @@ -89,7 +89,7 @@ static void vp_set_status(struct virtio_device *vdev, u8 status) iowrite8(status, vp_dev->ioaddr + VIRTIO_PCI_STATUS); } -static void vp_reset(struct virtio_device *vdev) +static int vp_reset(struct virtio_device *vdev) { struct virtio_pci_device *vp_dev = to_vp_device(vdev); /* 0 status means a reset. */ @@ -99,6 +99,7 @@ static void vp_reset(struct virtio_device *vdev) ioread8(vp_dev->ioaddr + VIRTIO_PCI_STATUS); /* Flush pending VQ/configuration callbacks. */ vp_synchronize_vectors(vdev); + return 0; } static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector) diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c index fbd4ebc00eb6..cc3412a96a17 100644 --- a/drivers/virtio/virtio_pci_modern.c +++ b/drivers/virtio/virtio_pci_modern.c @@ -158,7 +158,7 @@ static void vp_set_status(struct virtio_device *vdev, u8 status) vp_modern_set_status(&vp_dev->mdev, status); } -static void vp_reset(struct virtio_device *vdev) +static int vp_reset(struct virtio_device *vdev) { struct virtio_pci_device *vp_dev = to_vp_device(vdev); struct virtio_pci_modern_device *mdev = &vp_dev->mdev; @@ -174,6 +174,7 @@ static void vp_reset(struct virtio_device *vdev) msleep(1); /* Flush pending VQ/configuration callbacks. */ vp_synchronize_vectors(vdev); + return 0; } static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector) diff --git a/drivers/virtio/virtio_vdpa.c b/drivers/virtio/virtio_vdpa.c index e28acf482e0c..da76190f7b07 100644 --- a/drivers/virtio/virtio_vdpa.c +++ b/drivers/virtio/virtio_vdpa.c @@ -97,11 +97,12 @@ static void virtio_vdpa_set_status(struct virtio_device *vdev, u8 status) return ops->set_status(vdpa, status); } -static void virtio_vdpa_reset(struct virtio_device *vdev) +static int virtio_vdpa_reset(struct virtio_device *vdev) { struct vdpa_device *vdpa = vd_get_vdpa(vdev); vdpa_reset(vdpa); + return 0; } static bool virtio_vdpa_notify(struct virtqueue *vq) diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h index 8519b3ae5d52..d2b0f1699a75 100644 --- a/include/linux/virtio_config.h +++ b/include/linux/virtio_config.h @@ -44,9 +44,10 @@ struct virtio_shm_region { * status: the new status byte * @reset: reset the device * vdev: the virtio device - * After this, status and feature negotiation must be done again + * Upon success, status and feature negotiation must be done again * Device must not be reset from its vq/config callbacks, or in * parallel with being added/removed. + * Returns 0 on success or error status. * @find_vqs: find virtqueues and instantiate them. * vdev: the virtio_device * nvqs: the number of virtqueues to find @@ -82,7 +83,7 @@ struct virtio_config_ops { u32 (*generation)(struct virtio_device *vdev); u8 (*get_status)(struct virtio_device *vdev); void (*set_status)(struct virtio_device *vdev, u8 status); - void (*reset)(struct virtio_device *vdev); + int (*reset)(struct virtio_device *vdev); int (*find_vqs)(struct virtio_device *, unsigned nvqs, struct virtqueue *vqs[], vq_callback_t *callbacks[], const char * const names[], const bool *ctx, -- 2.25.4