A virtio driver needs to route its freeze and restore callbacks through the virtio framework rather than generic pm, or the restore callbacks from its parent (that runs before the driver's) will mark the driver ready before it can re-create its virtqueues, leading to a dead communication and violating the virtio specification. At driver side, this also leads to a hang at first SPI transaction after resume. Move the freeze/restore operations to the corresponding virtio_driver callbacks, so that the virtio core takes care of them. This aligns the SPI driver to what other virtio drivers are doing. In the restore callback, call virtio_device_ready() before resuming the SPI controller; in this way, the device is correctly notified of the readiness before any request can arrive from the driver. Behavior reproduced and fix tested on QEMU. Fixes: f98cabe3f6cf ("SPI: Add virtio SPI driver") Cc: stable@vger.kernel.org Signed-off-by: Francesco Valla --- drivers/spi/spi-virtio.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/drivers/spi/spi-virtio.c b/drivers/spi/spi-virtio.c index 2eeb79ff5aa2..c163cfb74910 100644 --- a/drivers/spi/spi-virtio.c +++ b/drivers/spi/spi-virtio.c @@ -365,37 +365,38 @@ static void virtio_spi_remove(struct virtio_device *vdev) virtio_spi_del_vq(vdev); } -static int virtio_spi_freeze(struct device *dev) +static int virtio_spi_freeze(struct virtio_device *vdev) { - struct spi_controller *ctrl = dev_get_drvdata(dev); - struct virtio_device *vdev = dev_to_virtio(dev); + struct spi_controller *ctrl = dev_get_drvdata(&vdev->dev); int ret; ret = spi_controller_suspend(ctrl); if (ret) { - dev_warn(dev, "cannot suspend controller (%d)\n", ret); + dev_warn(&vdev->dev, "cannot suspend controller (%d)\n", ret); return ret; } virtio_spi_del_vq(vdev); + return 0; } -static int virtio_spi_restore(struct device *dev) +static int virtio_spi_restore(struct virtio_device *vdev) { - struct spi_controller *ctrl = dev_get_drvdata(dev); - struct virtio_device *vdev = dev_to_virtio(dev); + struct spi_controller *ctrl = dev_get_drvdata(&vdev->dev); int ret; ret = virtio_spi_find_vqs(vdev->priv); if (ret) { - dev_err(dev, "problem starting vqueue (%d)\n", ret); + dev_err(&vdev->dev, "problem starting vqueue (%d)\n", ret); return ret; } + virtio_device_ready(vdev); + ret = spi_controller_resume(ctrl); if (ret) - dev_err(dev, "problem resuming controller (%d)\n", ret); + dev_err(&vdev->dev, "problem resuming controller (%d)\n", ret); return ret; } @@ -406,19 +407,15 @@ static struct virtio_device_id virtio_spi_id_table[] = { }; MODULE_DEVICE_TABLE(virtio, virtio_spi_id_table); -static const struct dev_pm_ops virtio_spi_pm_ops = { - .freeze = pm_sleep_ptr(virtio_spi_freeze), - .restore = pm_sleep_ptr(virtio_spi_restore), -}; - static struct virtio_driver virtio_spi_driver = { .driver = { .name = KBUILD_MODNAME, - .pm = &virtio_spi_pm_ops, }, .id_table = virtio_spi_id_table, .probe = virtio_spi_probe, .remove = virtio_spi_remove, + .freeze = pm_sleep_ptr(virtio_spi_freeze), + .restore = pm_sleep_ptr(virtio_spi_restore), }; module_virtio_driver(virtio_spi_driver); --- base-commit: ca20d5a011d35fc25e328fe31ac47f7aae557c5a change-id: 20261129-spi-virtio-restore-fix-5d457779bb30 Best regards, -- Francesco Valla