ed5979
From 1ed1f8fc20a4883bc0bc1f58d299b0278abc5442 Mon Sep 17 00:00:00 2001
ed5979
From: Matthew Rosato <mjrosato@linux.ibm.com>
ed5979
Date: Fri, 9 Dec 2022 14:57:00 -0500
ed5979
Subject: [PATCH 8/9] s390x/pci: reset ISM passthrough devices on shutdown and
ed5979
 system reset
ed5979
MIME-Version: 1.0
ed5979
Content-Type: text/plain; charset=UTF-8
ed5979
Content-Transfer-Encoding: 8bit
ed5979
ed5979
RH-Author: Cédric Le Goater <clg@redhat.com>
ed5979
RH-MergeRequest: 141: s390x/pci: reset ISM passthrough devices on shutdown and system reset
ed5979
RH-Bugzilla: 2163701
ed5979
RH-Acked-by: Thomas Huth <thuth@redhat.com>
ed5979
RH-Acked-by: Cornelia Huck <cohuck@redhat.com>
ed5979
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
ed5979
RH-Commit: [3/3] c531352b9d57f51ba938d4c46ee19a5706ade697 (clegoate/qemu-kvm-c9s)
ed5979
ed5979
ISM device firmware stores unique state information that can
ed5979
can cause a wholesale unmap of the associated IOMMU (e.g. when
ed5979
we get a termination signal for QEMU) to trigger firmware errors
ed5979
because firmware believes we are attempting to invalidate entries
ed5979
that are still in-use by the guest OS (when in fact that guest is
ed5979
in the process of being terminated or rebooted).
ed5979
To alleviate this, register both a shutdown notifier (for unexpected
ed5979
termination cases e.g. virsh destroy) as well as a reset callback
ed5979
(for cases like guest OS reboot).  For each of these scenarios, trigger
ed5979
PCI device reset; this is enough to indicate to firmware that the IOMMU
ed5979
is no longer in-use by the guest OS, making it safe to invalidate any
ed5979
associated IOMMU entries.
ed5979
ed5979
Fixes: 15d0e7942d3b ("s390x/pci: don't fence interpreted devices without MSI-X")
ed5979
Signed-off-by: Matthew Rosato <mjrosato@linux.ibm.com>
ed5979
Message-Id: <20221209195700.263824-1-mjrosato@linux.ibm.com>
ed5979
Reviewed-by: Eric Farman <farman@linux.ibm.com>
ed5979
[thuth: Adjusted the hunk in s390-pci-vfio.c due to different context]
ed5979
Signed-off-by: Thomas Huth <thuth@redhat.com>
ed5979
(cherry picked from commit 03451953c79e6b31f7860ee0c35b28e181d573c1)
ed5979
Signed-off-by: Cédric Le Goater <clg@redhat.com>
ed5979
---
ed5979
 hw/s390x/s390-pci-bus.c         | 28 ++++++++++++++++++++++++++++
ed5979
 hw/s390x/s390-pci-vfio.c        |  2 ++
ed5979
 include/hw/s390x/s390-pci-bus.h |  5 +++++
ed5979
 3 files changed, 35 insertions(+)
ed5979
ed5979
diff --git a/hw/s390x/s390-pci-bus.c b/hw/s390x/s390-pci-bus.c
ed5979
index 977e7daa15..02751f3597 100644
ed5979
--- a/hw/s390x/s390-pci-bus.c
ed5979
+++ b/hw/s390x/s390-pci-bus.c
ed5979
@@ -24,6 +24,8 @@
ed5979
 #include "hw/pci/msi.h"
ed5979
 #include "qemu/error-report.h"
ed5979
 #include "qemu/module.h"
ed5979
+#include "sysemu/reset.h"
ed5979
+#include "sysemu/runstate.h"
ed5979
 
ed5979
 #ifndef DEBUG_S390PCI_BUS
ed5979
 #define DEBUG_S390PCI_BUS  0
ed5979
@@ -150,10 +152,30 @@ out:
ed5979
     psccb->header.response_code = cpu_to_be16(rc);
ed5979
 }
ed5979
 
ed5979
+static void s390_pci_shutdown_notifier(Notifier *n, void *opaque)
ed5979
+{
ed5979
+    S390PCIBusDevice *pbdev = container_of(n, S390PCIBusDevice,
ed5979
+                                           shutdown_notifier);
ed5979
+
ed5979
+    pci_device_reset(pbdev->pdev);
ed5979
+}
ed5979
+
ed5979
+static void s390_pci_reset_cb(void *opaque)
ed5979
+{
ed5979
+    S390PCIBusDevice *pbdev = opaque;
ed5979
+
ed5979
+    pci_device_reset(pbdev->pdev);
ed5979
+}
ed5979
+
ed5979
 static void s390_pci_perform_unplug(S390PCIBusDevice *pbdev)
ed5979
 {
ed5979
     HotplugHandler *hotplug_ctrl;
ed5979
 
ed5979
+    if (pbdev->pft == ZPCI_PFT_ISM) {
ed5979
+        notifier_remove(&pbdev->shutdown_notifier);
ed5979
+        qemu_unregister_reset(s390_pci_reset_cb, pbdev);
ed5979
+    }
ed5979
+
ed5979
     /* Unplug the PCI device */
ed5979
     if (pbdev->pdev) {
ed5979
         DeviceState *pdev = DEVICE(pbdev->pdev);
ed5979
@@ -1111,6 +1133,12 @@ static void s390_pcihost_plug(HotplugHandler *hotplug_dev, DeviceState *dev,
ed5979
                 pbdev->fh |= FH_SHM_VFIO;
ed5979
                 pbdev->forwarding_assist = false;
ed5979
             }
ed5979
+            /* Register shutdown notifier and reset callback for ISM devices */
ed5979
+            if (pbdev->pft == ZPCI_PFT_ISM) {
ed5979
+                pbdev->shutdown_notifier.notify = s390_pci_shutdown_notifier;
ed5979
+                qemu_register_shutdown_notifier(&pbdev->shutdown_notifier);
ed5979
+                qemu_register_reset(s390_pci_reset_cb, pbdev);
ed5979
+            }
ed5979
         } else {
ed5979
             pbdev->fh |= FH_SHM_EMUL;
ed5979
             /* Always intercept emulated devices */
ed5979
diff --git a/hw/s390x/s390-pci-vfio.c b/hw/s390x/s390-pci-vfio.c
ed5979
index f7bf36cec8..f51190d466 100644
ed5979
--- a/hw/s390x/s390-pci-vfio.c
ed5979
+++ b/hw/s390x/s390-pci-vfio.c
ed5979
@@ -124,6 +124,8 @@ static void s390_pci_read_base(S390PCIBusDevice *pbdev,
ed5979
     /* The following values remain 0 until we support other FMB formats */
ed5979
     pbdev->zpci_fn.fmbl = 0;
ed5979
     pbdev->zpci_fn.pft = 0;
ed5979
+    /* Store function type separately for type-specific behavior */
ed5979
+    pbdev->pft = cap->pft;
ed5979
 
ed5979
     /*
ed5979
      * If appropriate, reduce the size of the supported DMA aperture reported
ed5979
diff --git a/include/hw/s390x/s390-pci-bus.h b/include/hw/s390x/s390-pci-bus.h
ed5979
index 1c46e3a269..e0a9f9385b 100644
ed5979
--- a/include/hw/s390x/s390-pci-bus.h
ed5979
+++ b/include/hw/s390x/s390-pci-bus.h
ed5979
@@ -39,6 +39,9 @@
ed5979
 #define UID_CHECKING_ENABLED 0x01
ed5979
 #define ZPCI_DTSM 0x40
ed5979
 
ed5979
+/* zPCI Function Types */
ed5979
+#define ZPCI_PFT_ISM 5
ed5979
+
ed5979
 OBJECT_DECLARE_SIMPLE_TYPE(S390pciState, S390_PCI_HOST_BRIDGE)
ed5979
 OBJECT_DECLARE_SIMPLE_TYPE(S390PCIBus, S390_PCI_BUS)
ed5979
 OBJECT_DECLARE_SIMPLE_TYPE(S390PCIBusDevice, S390_PCI_DEVICE)
ed5979
@@ -344,6 +347,7 @@ struct S390PCIBusDevice {
ed5979
     uint16_t noi;
ed5979
     uint16_t maxstbl;
ed5979
     uint8_t sum;
ed5979
+    uint8_t pft;
ed5979
     S390PCIGroup *pci_group;
ed5979
     ClpRspQueryPci zpci_fn;
ed5979
     S390MsixInfo msix;
ed5979
@@ -352,6 +356,7 @@ struct S390PCIBusDevice {
ed5979
     MemoryRegion msix_notify_mr;
ed5979
     IndAddr *summary_ind;
ed5979
     IndAddr *indicator;
ed5979
+    Notifier shutdown_notifier;
ed5979
     bool pci_unplug_request_processed;
ed5979
     bool unplug_requested;
ed5979
     bool interp;
ed5979
-- 
ed5979
2.31.1
ed5979