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