Skip to contentSkip to conversation

Bug fixes for EEH on VFIO PCI devices

PATCH In review Gavin Shan 3 participants 14 messages Mar 26, 2015 – Apr 1, 2015

lore
Share & tools
lore

Gavin Shan

author
#0 lore raw

v3 cover Bug fixes for EEH on VFIO PCI devices

The patches are fixing 2 issues for EEH on VFIO PCI devices. PATCH[1/2] clears stale MSIx table of VFIO PCI devices when asserting fundamental or hot PE reset so that their MSIx tables can be restored properly after reset to avoid recursive EEH error. PATCH[2/2] clears PE frozen state in case the guest hits excessive EEH errors. With the fix, the VFIO PCI devices are expected to work again after rebooting guest.

Changelog
=========
v2 -> v3:
* Introduced vfio_eeh_pe_reset(), which is called from spapr_pci_vfio.c when asserting PE reset, replaces vfio_container_eeh_event() to clear stale MSIx tables.
* Droped the patch "VFIO: Disable INTx interrupt on EEH reset", which is caused by KVM bugs on host side.
v1 -> v2:
* vfio_container_eeh_event() stub for !CONFIG_PCI and separate error message for this function. Dropped vfio_put_group() on NULL group
* Disabling INTx interrupt, instead of clearing INTx pending flag during PE reset.

Gavin Shan (2):
VFIO: Clear stale MSIx table during EEH reset
sPAPR: Reenable EEH functionality on reboot

 hw/ppc/spapr_pci_vfio.c | 27 ++++++++++++++++++++++-----
 hw/vfio/Makefile.objs   |  6 +++++-
 hw/vfio/pci-stub.c      | 16 ++++++++++++++++
 hw/vfio/pci.c           | 36 ++++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio.h  |  2 ++
 5 files changed, 81 insertions(+), 6 deletions(-)
 create mode 100644 hw/vfio/pci-stub.c
Signature · 1 line
1.8.3.2
author
#1 lore raw

PATCH v3 1/2 VFIO: Clear stale MSIx table during EEH reset

The PCI device MSIx table is cleaned out in hardware after EEH PE reset. However, we still hold the stale MSIx entries in QEMU, which should be cleared accordingly. Otherwise, we will run into another (recursive) EEH error and the PCI devices contained in the PE have to be offlined exceptionally.

The patch introduces function vfio_eeh_pe_reset(), which is called by sPAPR when asserting hot or fundamental reset, to clear stale MSIx table before EEH PE reset so that MSIx table could be restored properly after EEH PE reset.

---
 hw/ppc/spapr_pci_vfio.c | 13 +++++++++----
 hw/vfio/Makefile.objs   |  6 +++++-
 hw/vfio/pci-stub.c      | 16 ++++++++++++++++
 hw/vfio/pci.c           | 36 ++++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio.h  |  2 ++
 5 files changed, 68 insertions(+), 5 deletions(-)
 create mode 100644 hw/vfio/pci-stub.c
hw/ppc/spapr_pci_vfio.c +9 -4
index 99a1be5..6fa3afe 100644
@@ -151,19 +151,24 @@ static int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
     switch (option) {
     case RTAS_SLOT_RESET_DEACTIVATE:
         op.op = VFIO_EEH_PE_RESET_DEACTIVATE;
+        ret = vfio_container_ioctl(&svphb->phb.iommu_as,
+                                   svphb->iommugroupid,
+                                   VFIO_EEH_PE_OP, &op);
         break;
     case RTAS_SLOT_RESET_HOT:
-        op.op = VFIO_EEH_PE_RESET_HOT;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_HOT);
         break;
     case RTAS_SLOT_RESET_FUNDAMENTAL:
-        op.op = VFIO_EEH_PE_RESET_FUNDAMENTAL;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_FUNDAMENTAL);
         break;
     default:
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&svphb->phb.iommu_as, svphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
hw/vfio/Makefile.objs +5 -1
index e31f30e..1b8a065 100644
@@ -1,4 +1,8 @@
 ifeq ($(CONFIG_LINUX), y)
 obj-$(CONFIG_SOFTMMU) += common.o
-obj-$(CONFIG_PCI) += pci.o
+ifeq ($(CONFIG_PCI), y)
+obj-y += pci.o
+else
+obj-y += pci-stub.o
+endif
 endif
hw/vfio/pci-stub.c new file +16 -0
new file mode 100644
index 0000000..f317c1e
@@ -0,0 +1,16 @@
+/*
+ * To include the file on !CONFIG_PCI
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ */
+
+#include <linux/vfio.h>
+
+#include "exec/memory.h"
+#include "hw/vfio/vfio.h"
+
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
+{
+    return -1;
+}
hw/vfio/pci.c +36 -0
index 6b80539..d0fd4b4 100644
@@ -3319,6 +3319,42 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
     vdev->req_enabled = false;
 }
 
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
+{
+    VFIOGroup *group;
+    VFIODevice *vbasedev;
+    VFIOPCIDevice *vdev;
+    struct vfio_eeh_pe_op op = {
+        .argsz = sizeof(op),
+        .op = option
+    };
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        error_report("vfio: group %d not found\n", groupid);
+        return -1;
+    }
+
+    /*
+     * The MSIx table will be cleaned out by reset. We need
+     * disable it so that it can be reenabled properly. Also,
+     * the cached MSIx table should be cleared as it's not
+     * reflecting the contents in hardware.
+     */
+    QLIST_FOREACH(vbasedev, &group->device_list, next) {
+        vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+        if (msix_enabled(&vdev->pdev)) {
+            vfio_disable_msix(vdev);
+        }
+
+        msix_reset(&vdev->pdev);
+    }
+
+    vfio_put_group(group);
+
+    return vfio_container_ioctl(as, groupid, VFIO_EEH_PE_OP, &op);
+}
+
 static int vfio_initfn(PCIDevice *pdev)
 {
     VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev);
include/hw/vfio/vfio.h +2 -0
index 0b26cd8..52de277 100644
@@ -5,5 +5,7 @@
 
 extern int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
                                 int req, void *param);
+extern int vfio_eeh_pe_reset(AddressSpace *as,
+                             int32_t groupid, uint32_t option);
 
 #endif
Signature · 1 line
1.8.3.2
#2 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] VFIO: Clear stale MSIx table during EEH reset

Quoted 80 lines · Gavin Shan

The PCI device MSIx table is cleaned out in hardware after EEH PE reset. However, we still hold the stale MSIx entries in QEMU, which should be cleared accordingly. Otherwise, we will run into another (recursive) EEH error and the PCI devices contained in the PE have to be offlined exceptionally.

The patch introduces function vfio_eeh_pe_reset(), which is called by sPAPR when asserting hot or fundamental reset, to clear stale MSIx table before EEH PE reset so that MSIx table could be restored properly after EEH PE reset.

---
 hw/ppc/spapr_pci_vfio.c | 13 +++++++++----
 hw/vfio/Makefile.objs   |  6 +++++-
 hw/vfio/pci-stub.c      | 16 ++++++++++++++++
 hw/vfio/pci.c           | 36 ++++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio.h  |  2 ++
 5 files changed, 68 insertions(+), 5 deletions(-)
 create mode 100644 hw/vfio/pci-stub.c
hw/ppc/spapr_pci_vfio.c +9 -4
index 99a1be5..6fa3afe 100644
@@ -151,19 +151,24 @@ static int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
     switch (option) {
     case RTAS_SLOT_RESET_DEACTIVATE:
         op.op = VFIO_EEH_PE_RESET_DEACTIVATE;
+        ret = vfio_container_ioctl(&svphb->phb.iommu_as,
+                                   svphb->iommugroupid,
+                                   VFIO_EEH_PE_OP, &op);
         break;
     case RTAS_SLOT_RESET_HOT:
-        op.op = VFIO_EEH_PE_RESET_HOT;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_HOT);
         break;
     case RTAS_SLOT_RESET_FUNDAMENTAL:
-        op.op = VFIO_EEH_PE_RESET_FUNDAMENTAL;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_FUNDAMENTAL);
         break;
     default:
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&svphb->phb.iommu_as, svphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
hw/vfio/Makefile.objs +5 -1
index e31f30e..1b8a065 100644
@@ -1,4 +1,8 @@
 ifeq ($(CONFIG_LINUX), y)
 obj-$(CONFIG_SOFTMMU) += common.o
-obj-$(CONFIG_PCI) += pci.o
+ifeq ($(CONFIG_PCI), y)
+obj-y += pci.o
+else
+obj-y += pci-stub.o
+endif
 endif
hw/vfio/pci-stub.c new file +6 -0
new file mode 100644
index 0000000..f317c1e
@@ -0,0 +1,16 @@
+/*
+ * To include the file on !CONFIG_PCI
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ */

hw/vfio/pci-stub.c line 16

Quoted 10 lines
hw/vfio/pci-stub.c new file +10 -0
+
+#include <linux/vfio.h>
+
+#include "exec/memory.h"
+#include "hw/vfio/vfio.h"
+
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
+{
+    return -1;
+}

This doesn't seem quite right. AFAICT the only caller of vfio_eeh_pe_reset() is in spapr_pci_vfio.c, which is only built if CONFIG_PCI is enabled. So if there needed to be !PCI stubs, I'd expect them further up the call stack.

Quoted 25 lines
hw/vfio/pci-stub.c new file +25 -0
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
+{
+    VFIOGroup *group;
+    VFIODevice *vbasedev;
+    VFIOPCIDevice *vdev;
+    struct vfio_eeh_pe_op op = {
+        .argsz = sizeof(op),
+        .op = option
+    };
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        error_report("vfio: group %d not found\n", groupid);
+        return -1;
+    }
+
+    /*
+     * The MSIx table will be cleaned out by reset. We need
+     * disable it so that it can be reenabled properly. Also,
+     * the cached MSIx table should be cleared as it's not
+     * reflecting the contents in hardware.
+     */
+    QLIST_FOREACH(vbasedev, &group->device_list, next) {
+        vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+        if (msix_enabled(&vdev->pdev)) {

hw/vfio/pci-stub.c

Quoted 10 lines
hw/vfio/pci-stub.c new file +10 -0
+            vfio_disable_msix(vdev);
+        }
+
+        msix_reset(&vdev->pdev);
+    }
+
+    vfio_put_group(group);
+
+    return vfio_container_ioctl(as, groupid, VFIO_EEH_PE_OP, &op);
+}

This is much better than the vfio_eeh_event stuff().

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
author
#3 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] VFIO: Clear stale MSIx table during EEH reset

Quoted 88 lines · David Gibson
Quoted 87 lines · Gavin Shan

The PCI device MSIx table is cleaned out in hardware after EEH PE reset. However, we still hold the stale MSIx entries in QEMU, which should be cleared accordingly. Otherwise, we will run into another (recursive) EEH error and the PCI devices contained in the PE have to be offlined exceptionally.

The patch introduces function vfio_eeh_pe_reset(), which is called by sPAPR when asserting hot or fundamental reset, to clear stale MSIx table before EEH PE reset so that MSIx table could be restored properly after EEH PE reset.

---
 hw/ppc/spapr_pci_vfio.c | 13 +++++++++----
 hw/vfio/Makefile.objs   |  6 +++++-
 hw/vfio/pci-stub.c      | 16 ++++++++++++++++
 hw/vfio/pci.c           | 36 ++++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio.h  |  2 ++
 5 files changed, 68 insertions(+), 5 deletions(-)
 create mode 100644 hw/vfio/pci-stub.c
hw/ppc/spapr_pci_vfio.c +9 -4
index 99a1be5..6fa3afe 100644
@@ -151,19 +151,24 @@ static int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
     switch (option) {
     case RTAS_SLOT_RESET_DEACTIVATE:
         op.op = VFIO_EEH_PE_RESET_DEACTIVATE;
+        ret = vfio_container_ioctl(&svphb->phb.iommu_as,
+                                   svphb->iommugroupid,
+                                   VFIO_EEH_PE_OP, &op);
         break;
     case RTAS_SLOT_RESET_HOT:
-        op.op = VFIO_EEH_PE_RESET_HOT;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_HOT);
         break;
     case RTAS_SLOT_RESET_FUNDAMENTAL:
-        op.op = VFIO_EEH_PE_RESET_FUNDAMENTAL;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_FUNDAMENTAL);
         break;
     default:
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&svphb->phb.iommu_as, svphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
hw/vfio/Makefile.objs +5 -1
index e31f30e..1b8a065 100644
@@ -1,4 +1,8 @@
 ifeq ($(CONFIG_LINUX), y)
 obj-$(CONFIG_SOFTMMU) += common.o
-obj-$(CONFIG_PCI) += pci.o
+ifeq ($(CONFIG_PCI), y)
+obj-y += pci.o
+else
+obj-y += pci-stub.o
+endif
 endif
hw/vfio/pci-stub.c new file +13 -0
new file mode 100644
index 0000000..f317c1e
@@ -0,0 +1,16 @@
+/*
+ * To include the file on !CONFIG_PCI
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ */
+
+#include <linux/vfio.h>
+
+#include "exec/memory.h"
+#include "hw/vfio/vfio.h"
+
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
Quoted 9 lines

hw/vfio/pci-stub.c line 16

Quoted 3 lines
hw/vfio/pci-stub.c new file +3 -0
+{
+    return -1;
+}

This doesn't seem quite right. AFAICT the only caller of vfio_eeh_pe_reset() is in spapr_pci_vfio.c, which is only built if CONFIG_PCI is enabled. So if there needed to be !PCI stubs, I'd expect them further up the call stack.

Or we simply drop the stub for !CONFIG_PCI if Alex.W agree. Alex, what's your opnion?

Thanks,
Gavin

Quoted 43 lines
Quoted 25 lines
hw/vfio/pci-stub.c new file +25 -0
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
+{
+    VFIOGroup *group;
+    VFIODevice *vbasedev;
+    VFIOPCIDevice *vdev;
+    struct vfio_eeh_pe_op op = {
+        .argsz = sizeof(op),
+        .op = option
+    };
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        error_report("vfio: group %d not found\n", groupid);
+        return -1;
+    }
+
+    /*
+     * The MSIx table will be cleaned out by reset. We need
+     * disable it so that it can be reenabled properly. Also,
+     * the cached MSIx table should be cleared as it's not
+     * reflecting the contents in hardware.
+     */
+    QLIST_FOREACH(vbasedev, &group->device_list, next) {
+        vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+        if (msix_enabled(&vdev->pdev)) {

hw/vfio/pci-stub.c

Quoted 10 lines
hw/vfio/pci-stub.c new file +10 -0
+            vfio_disable_msix(vdev);
+        }
+
+        msix_reset(&vdev->pdev);
+    }
+
+    vfio_put_group(group);
+
+    return vfio_container_ioctl(as, groupid, VFIO_EEH_PE_OP, &op);
+}

This is much better than the vfio_eeh_event stuff().

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#4 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] VFIO: Clear stale MSIx table during EEH reset

In reply to Gavin Shan
Quoted 26 lines · Gavin Shan

The PCI device MSIx table is cleaned out in hardware after EEH PE reset. However, we still hold the stale MSIx entries in QEMU, which should be cleared accordingly. Otherwise, we will run into another (recursive) EEH error and the PCI devices contained in the PE have to be offlined exceptionally.

The patch introduces function vfio_eeh_pe_reset(), which is called by sPAPR when asserting hot or fundamental reset, to clear stale MSIx table before EEH PE reset so that MSIx table could be restored properly after EEH PE reset.

---
 hw/ppc/spapr_pci_vfio.c | 13 +++++++++----
 hw/vfio/Makefile.objs   |  6 +++++-
 hw/vfio/pci-stub.c      | 16 ++++++++++++++++
 hw/vfio/pci.c           | 36 ++++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio.h  |  2 ++
 5 files changed, 68 insertions(+), 5 deletions(-)
 create mode 100644 hw/vfio/pci-stub.c
hw/ppc/spapr_pci_vfio.c +0 -0
index 99a1be5..6fa3afe 100644

hw/ppc/spapr_pci_vfio.c static int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option) · line 156

Quoted 6 lines
hw/ppc/spapr_pci_vfio.c +3 -0
@@ -151,19 +151,24 @@ static int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
     switch (option) {
     case RTAS_SLOT_RESET_DEACTIVATE:
         op.op = VFIO_EEH_PE_RESET_DEACTIVATE;
+        ret = vfio_container_ioctl(&svphb->phb.iommu_as,
+                                   svphb->iommugroupid,
+                                   VFIO_EEH_PE_OP, &op);

For consistency, I think all the reset operations should go through vfio_eeh_pe_reset(), even though in this case it won't do more than call vfio_container_ioctl().

Quoted 47 lines
hw/ppc/spapr_pci_vfio.c +6 -4
         break;
     case RTAS_SLOT_RESET_HOT:
-        op.op = VFIO_EEH_PE_RESET_HOT;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_HOT);
         break;
     case RTAS_SLOT_RESET_FUNDAMENTAL:
-        op.op = VFIO_EEH_PE_RESET_FUNDAMENTAL;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_FUNDAMENTAL);
         break;
     default:
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&svphb->phb.iommu_as, svphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
hw/vfio/Makefile.objs +5 -1
index e31f30e..1b8a065 100644
@@ -1,4 +1,8 @@
 ifeq ($(CONFIG_LINUX), y)
 obj-$(CONFIG_SOFTMMU) += common.o
-obj-$(CONFIG_PCI) += pci.o
+ifeq ($(CONFIG_PCI), y)
+obj-y += pci.o
+else
+obj-y += pci-stub.o
+endif
 endif
hw/vfio/pci-stub.c new file +5 -0
new file mode 100644
index 0000000..f317c1e
@@ -0,0 +1,16 @@
+/*
+ * To include the file on !CONFIG_PCI
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.

hw/vfio/pci-stub.c line 15

Quoted 10 lines
hw/vfio/pci-stub.c new file +10 -0
+ */
+
+#include <linux/vfio.h>
+
+#include "exec/memory.h"
+#include "hw/vfio/vfio.h"
+
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
+{
+    return -1;

Probably should have assert(0) here - this should never be called if !CONFIG_PCI.

Quoted 60 lines
hw/vfio/pci-stub.c new file +1 -0
+}
hw/vfio/pci.c +36 -0
index 6b80539..d0fd4b4 100644
@@ -3319,6 +3319,42 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
     vdev->req_enabled = false;
 }
 
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
+{
+    VFIOGroup *group;
+    VFIODevice *vbasedev;
+    VFIOPCIDevice *vdev;
+    struct vfio_eeh_pe_op op = {
+        .argsz = sizeof(op),
+        .op = option
+    };
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        error_report("vfio: group %d not found\n", groupid);
+        return -1;
+    }
+
+    /*
+     * The MSIx table will be cleaned out by reset. We need
+     * disable it so that it can be reenabled properly. Also,
+     * the cached MSIx table should be cleared as it's not
+     * reflecting the contents in hardware.
+     */
+    QLIST_FOREACH(vbasedev, &group->device_list, next) {
+        vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+        if (msix_enabled(&vdev->pdev)) {
+            vfio_disable_msix(vdev);
+        }
+
+        msix_reset(&vdev->pdev);
+    }
+
+    vfio_put_group(group);
+
+    return vfio_container_ioctl(as, groupid, VFIO_EEH_PE_OP, &op);
+}
+
 static int vfio_initfn(PCIDevice *pdev)
 {
     VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev);
include/hw/vfio/vfio.h +2 -0
index 0b26cd8..52de277 100644
@@ -5,5 +5,7 @@
 
 extern int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
                                 int req, void *param);
+extern int vfio_eeh_pe_reset(AddressSpace *as,
+                             int32_t groupid, uint32_t option);
 
 #endif
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
author
#5 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] VFIO: Clear stale MSIx table during EEH reset

Quoted 22 lines · David Gibson
Quoted 21 lines · Gavin Shan

The PCI device MSIx table is cleaned out in hardware after EEH PE reset. However, we still hold the stale MSIx entries in QEMU, which should be cleared accordingly. Otherwise, we will run into another (recursive) EEH error and the PCI devices contained in the PE have to be offlined exceptionally.

The patch introduces function vfio_eeh_pe_reset(), which is called by sPAPR when asserting hot or fundamental reset, to clear stale MSIx table before EEH PE reset so that MSIx table could be restored properly after EEH PE reset.

---
 hw/ppc/spapr_pci_vfio.c | 13 +++++++++----
 hw/vfio/Makefile.objs   |  6 +++++-
 hw/vfio/pci-stub.c      | 16 ++++++++++++++++
 hw/vfio/pci.c           | 36 ++++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio.h  |  2 ++
 5 files changed, 68 insertions(+), 5 deletions(-)
 create mode 100644 hw/vfio/pci-stub.c
Quoted 16 lines

hw/ppc/spapr_pci_vfio.c static int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option) · line 156

Quoted 11 lines
hw/ppc/spapr_pci_vfio.c +3 -0
index 99a1be5..6fa3afe 100644
@@ -151,19 +151,24 @@ static int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
     switch (option) {
     case RTAS_SLOT_RESET_DEACTIVATE:
         op.op = VFIO_EEH_PE_RESET_DEACTIVATE;
+        ret = vfio_container_ioctl(&svphb->phb.iommu_as,
+                                   svphb->iommugroupid,
+                                   VFIO_EEH_PE_OP, &op);

For consistency, I think all the reset operations should go through vfio_eeh_pe_reset(), even though in this case it won't do more than call vfio_container_ioctl().

Fair enough. I'll fix :-)

Quoted 51 lines
Quoted 51 lines
hw/ppc/spapr_pci_vfio.c +6 -4
         break;
     case RTAS_SLOT_RESET_HOT:
-        op.op = VFIO_EEH_PE_RESET_HOT;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_HOT);
         break;
     case RTAS_SLOT_RESET_FUNDAMENTAL:
-        op.op = VFIO_EEH_PE_RESET_FUNDAMENTAL;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_FUNDAMENTAL);
         break;
     default:
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&svphb->phb.iommu_as, svphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
hw/vfio/Makefile.objs +5 -1
index e31f30e..1b8a065 100644
@@ -1,4 +1,8 @@
 ifeq ($(CONFIG_LINUX), y)
 obj-$(CONFIG_SOFTMMU) += common.o
-obj-$(CONFIG_PCI) += pci.o
+ifeq ($(CONFIG_PCI), y)
+obj-y += pci.o
+else
+obj-y += pci-stub.o
+endif
 endif
hw/vfio/pci-stub.c new file +9 -0
new file mode 100644
index 0000000..f317c1e
@@ -0,0 +1,16 @@
+/*
+ * To include the file on !CONFIG_PCI
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ */
+
+#include <linux/vfio.h>
+
Quoted 9 lines

hw/vfio/pci-stub.c line 15

Quoted 6 lines
hw/vfio/pci-stub.c new file +6 -0
+#include "exec/memory.h"
+#include "hw/vfio/vfio.h"
+
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
+{
+    return -1;

Probably should have assert(0) here - this should never be called if !CONFIG_PCI.

Indeed, assert(0) would be better. I just replied to ask for dropping the stub for !CONFIG_PCI if you and Alex.W agree.

Thanks,
Gavin

Quoted 66 lines
Quoted 60 lines
hw/vfio/pci-stub.c new file +1 -0
+}
hw/vfio/pci.c +36 -0
index 6b80539..d0fd4b4 100644
@@ -3319,6 +3319,42 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
     vdev->req_enabled = false;
 }
 
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
+{
+    VFIOGroup *group;
+    VFIODevice *vbasedev;
+    VFIOPCIDevice *vdev;
+    struct vfio_eeh_pe_op op = {
+        .argsz = sizeof(op),
+        .op = option
+    };
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        error_report("vfio: group %d not found\n", groupid);
+        return -1;
+    }
+
+    /*
+     * The MSIx table will be cleaned out by reset. We need
+     * disable it so that it can be reenabled properly. Also,
+     * the cached MSIx table should be cleared as it's not
+     * reflecting the contents in hardware.
+     */
+    QLIST_FOREACH(vbasedev, &group->device_list, next) {
+        vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+        if (msix_enabled(&vdev->pdev)) {
+            vfio_disable_msix(vdev);
+        }
+
+        msix_reset(&vdev->pdev);
+    }
+
+    vfio_put_group(group);
+
+    return vfio_container_ioctl(as, groupid, VFIO_EEH_PE_OP, &op);
+}
+
 static int vfio_initfn(PCIDevice *pdev)
 {
     VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev);
include/hw/vfio/vfio.h +2 -0
index 0b26cd8..52de277 100644
@@ -5,5 +5,7 @@
 
 extern int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
                                 int req, void *param);
+extern int vfio_eeh_pe_reset(AddressSpace *as,
+                             int32_t groupid, uint32_t option);
 
 #endif
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#6 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] VFIO: Clear stale MSIx table during EEH reset

Quoted 104 lines · Gavin Shan
Quoted 22 lines · David Gibson
Quoted 21 lines · Gavin Shan

The PCI device MSIx table is cleaned out in hardware after EEH PE reset. However, we still hold the stale MSIx entries in QEMU, which should be cleared accordingly. Otherwise, we will run into another (recursive) EEH error and the PCI devices contained in the PE have to be offlined exceptionally.

The patch introduces function vfio_eeh_pe_reset(), which is called by sPAPR when asserting hot or fundamental reset, to clear stale MSIx table before EEH PE reset so that MSIx table could be restored properly after EEH PE reset.

---
 hw/ppc/spapr_pci_vfio.c | 13 +++++++++----
 hw/vfio/Makefile.objs   |  6 +++++-
 hw/vfio/pci-stub.c      | 16 ++++++++++++++++
 hw/vfio/pci.c           | 36 ++++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio.h  |  2 ++
 5 files changed, 68 insertions(+), 5 deletions(-)
 create mode 100644 hw/vfio/pci-stub.c
Quoted 16 lines

hw/ppc/spapr_pci_vfio.c static int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option) · line 156

Quoted 11 lines
hw/ppc/spapr_pci_vfio.c +3 -0
index 99a1be5..6fa3afe 100644
@@ -151,19 +151,24 @@ static int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
     switch (option) {
     case RTAS_SLOT_RESET_DEACTIVATE:
         op.op = VFIO_EEH_PE_RESET_DEACTIVATE;
+        ret = vfio_container_ioctl(&svphb->phb.iommu_as,
+                                   svphb->iommugroupid,
+                                   VFIO_EEH_PE_OP, &op);

For consistency, I think all the reset operations should go through vfio_eeh_pe_reset(), even though in this case it won't do more than call vfio_container_ioctl().

Fair enough. I'll fix :-)

Quoted 51 lines
Quoted 51 lines
hw/ppc/spapr_pci_vfio.c +6 -4
         break;
     case RTAS_SLOT_RESET_HOT:
-        op.op = VFIO_EEH_PE_RESET_HOT;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_HOT);
         break;
     case RTAS_SLOT_RESET_FUNDAMENTAL:
-        op.op = VFIO_EEH_PE_RESET_FUNDAMENTAL;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_FUNDAMENTAL);
         break;
     default:
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&svphb->phb.iommu_as, svphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
hw/vfio/Makefile.objs +5 -1
index e31f30e..1b8a065 100644
@@ -1,4 +1,8 @@
 ifeq ($(CONFIG_LINUX), y)
 obj-$(CONFIG_SOFTMMU) += common.o
-obj-$(CONFIG_PCI) += pci.o
+ifeq ($(CONFIG_PCI), y)
+obj-y += pci.o
+else
+obj-y += pci-stub.o
+endif
 endif
hw/vfio/pci-stub.c new file +9 -0
new file mode 100644
index 0000000..f317c1e
@@ -0,0 +1,16 @@
+/*
+ * To include the file on !CONFIG_PCI
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.
+ */
+
+#include <linux/vfio.h>
+
Quoted 9 lines

hw/vfio/pci-stub.c line 15

Quoted 6 lines
hw/vfio/pci-stub.c new file +6 -0
+#include "exec/memory.h"
+#include "hw/vfio/vfio.h"
+
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
+{
+    return -1;

Probably should have assert(0) here - this should never be called if !CONFIG_PCI.

Quoted 2 lines

Indeed, assert(0) would be better. I just replied to ask for dropping the stub for !CONFIG_PCI if you and Alex.W agree.

I certainly don't see the reason for the stub, it was only suggested before because a previous version had the callout in hw/vfio/common.c

Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
hw/vfio/pci-stub.c new file +1 -0
+}
hw/vfio/pci.c +27 -0
index 6b80539..d0fd4b4 100644
@@ -3319,6 +3319,42 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
     vdev->req_enabled = false;
 }
 
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
+{
+    VFIOGroup *group;
+    VFIODevice *vbasedev;
+    VFIOPCIDevice *vdev;
+    struct vfio_eeh_pe_op op = {
+        .argsz = sizeof(op),
+        .op = option
+    };
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        error_report("vfio: group %d not found\n", groupid);
+        return -1;
+    }
+
+    /*
+     * The MSIx table will be cleaned out by reset. We need
+     * disable it so that it can be reenabled properly. Also,
+     * the cached MSIx table should be cleared as it's not
+     * reflecting the contents in hardware.
+     */
+    QLIST_FOREACH(vbasedev, &group->device_list, next) {
+        vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+        if (msix_enabled(&vdev->pdev)) {
+            vfio_disable_msix(vdev);
+        }
Quoted 8 lines
Quoted 8 lines

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 3356

Quoted 8 lines
hw/vfio/pci.c +8 -0
+
+        msix_reset(&vdev->pdev);
+    }
+
+    vfio_put_group(group);
+
+    return vfio_container_ioctl(as, groupid, VFIO_EEH_PE_OP, &op);
+}

So all you're trying to do here is find the devices in the PE and disable/reset MSI-X, but do you really need yet another ugly callback into vfio to do that? Isn't it possible to find the devices based on the address space or PCI topology? If we have EEH emulation, don't you also want to do this for emulated devices? The vfio_disable_msix() call could be replaced by the equivalent config space access to make it look like the guest disabled MSI-X.

Quoted 24 lines
Quoted 22 lines
Quoted 16 lines
hw/vfio/pci.c +1 -0
+
 static int vfio_initfn(PCIDevice *pdev)
 {
     VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev);
include/hw/vfio/vfio.h +2 -0
index 0b26cd8..52de277 100644
@@ -5,5 +5,7 @@
 
 extern int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
                                 int req, void *param);
+extern int vfio_eeh_pe_reset(AddressSpace *as,
+                             int32_t groupid, uint32_t option);
 
 #endif
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
author
#7 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] VFIO: Clear stale MSIx table during EEH reset

Quoted 105 lines · Alex Williamson
Quoted 104 lines · Gavin Shan
Quoted 22 lines · David Gibson
Quoted 21 lines · Gavin Shan

The PCI device MSIx table is cleaned out in hardware after EEH PE reset. However, we still hold the stale MSIx entries in QEMU, which should be cleared accordingly. Otherwise, we will run into another (recursive) EEH error and the PCI devices contained in the PE have to be offlined exceptionally.

The patch introduces function vfio_eeh_pe_reset(), which is called by sPAPR when asserting hot or fundamental reset, to clear stale MSIx table before EEH PE reset so that MSIx table could be restored properly after EEH PE reset.

---
 hw/ppc/spapr_pci_vfio.c | 13 +++++++++----
 hw/vfio/Makefile.objs   |  6 +++++-
 hw/vfio/pci-stub.c      | 16 ++++++++++++++++
 hw/vfio/pci.c           | 36 ++++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio.h  |  2 ++
 5 files changed, 68 insertions(+), 5 deletions(-)
 create mode 100644 hw/vfio/pci-stub.c
Quoted 16 lines

hw/ppc/spapr_pci_vfio.c static int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option) · line 156

Quoted 11 lines
hw/ppc/spapr_pci_vfio.c +3 -0
index 99a1be5..6fa3afe 100644
@@ -151,19 +151,24 @@ static int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
     switch (option) {
     case RTAS_SLOT_RESET_DEACTIVATE:
         op.op = VFIO_EEH_PE_RESET_DEACTIVATE;
+        ret = vfio_container_ioctl(&svphb->phb.iommu_as,
+                                   svphb->iommugroupid,
+                                   VFIO_EEH_PE_OP, &op);

For consistency, I think all the reset operations should go through vfio_eeh_pe_reset(), even though in this case it won't do more than call vfio_container_ioctl().

Fair enough. I'll fix :-)

Quoted 60 lines
Quoted 47 lines
hw/ppc/spapr_pci_vfio.c +6 -4
         break;
     case RTAS_SLOT_RESET_HOT:
-        op.op = VFIO_EEH_PE_RESET_HOT;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_HOT);
         break;
     case RTAS_SLOT_RESET_FUNDAMENTAL:
-        op.op = VFIO_EEH_PE_RESET_FUNDAMENTAL;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_FUNDAMENTAL);
         break;
     default:
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&svphb->phb.iommu_as, svphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
hw/vfio/Makefile.objs +5 -1
index e31f30e..1b8a065 100644
@@ -1,4 +1,8 @@
 ifeq ($(CONFIG_LINUX), y)
 obj-$(CONFIG_SOFTMMU) += common.o
-obj-$(CONFIG_PCI) += pci.o
+ifeq ($(CONFIG_PCI), y)
+obj-y += pci.o
+else
+obj-y += pci-stub.o
+endif
 endif
hw/vfio/pci-stub.c new file +5 -0
new file mode 100644
index 0000000..f317c1e
@@ -0,0 +1,16 @@
+/*
+ * To include the file on !CONFIG_PCI
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.

hw/vfio/pci-stub.c line 15

Quoted 10 lines
hw/vfio/pci-stub.c new file +10 -0
+ */
+
+#include <linux/vfio.h>
+
+#include "exec/memory.h"
+#include "hw/vfio/vfio.h"
+
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
+{
+    return -1;

Probably should have assert(0) here - this should never be called if !CONFIG_PCI.

Quoted 6 lines
Quoted 2 lines

Indeed, assert(0) would be better. I just replied to ask for dropping the stub for !CONFIG_PCI if you and Alex.W agree.

I certainly don't see the reason for the stub, it was only suggested before because a previous version had the callout in hw/vfio/common.c

Ok. I'll drop it in next revision.

Quoted 45 lines
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
hw/vfio/pci-stub.c new file +1 -0
+}
hw/vfio/pci.c +27 -0
index 6b80539..d0fd4b4 100644
@@ -3319,6 +3319,42 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
     vdev->req_enabled = false;
 }
 
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
+{
+    VFIOGroup *group;
+    VFIODevice *vbasedev;
+    VFIOPCIDevice *vdev;
+    struct vfio_eeh_pe_op op = {
+        .argsz = sizeof(op),
+        .op = option
+    };
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        error_report("vfio: group %d not found\n", groupid);
+        return -1;
+    }
+
+    /*
+     * The MSIx table will be cleaned out by reset. We need
+     * disable it so that it can be reenabled properly. Also,
+     * the cached MSIx table should be cleared as it's not
+     * reflecting the contents in hardware.
+     */
+    QLIST_FOREACH(vbasedev, &group->device_list, next) {
+        vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+        if (msix_enabled(&vdev->pdev)) {
+            vfio_disable_msix(vdev);
+        }
Quoted 8 lines
Quoted 8 lines

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 3356

Quoted 8 lines
hw/vfio/pci.c +8 -0
+
+        msix_reset(&vdev->pdev);
+    }
+
+    vfio_put_group(group);
+
+    return vfio_container_ioctl(as, groupid, VFIO_EEH_PE_OP, &op);
+}
Quoted 8 lines

So all you're trying to do here is find the devices in the PE and disable/reset MSI-X, but do you really need yet another ugly callback into vfio to do that? Isn't it possible to find the devices based on the address space or PCI topology? If we have EEH emulation, don't you also want to do this for emulated devices? The vfio_disable_msix() call could be replaced by the equivalent config space access to make it look like the guest disabled MSI-X.

EEH for emulated PCI device is out of scope for now, which depends on fully emulated IBM's PHB. The PE reset is requested by guest and guest is aware of losing MSIx table after that.

I'm not sure I'm following your suggestion, but yes, the VFIO PCI devices can be identified by checking its class string with help of some QOM helper functions. So I guess you are suggesting something as follows, which would make the code a bit cleaner.

- In hw/ppc/spapr_pci_vfio.c::spapr_phb_vfio_eeh_reset(), check all PCI devices hooked to the PHB and if it's a VFIO PCI device, disable MSIx interrupt by clearing MSIX_ENABLE in the config space and cleaning out the MSIx table if MSIx interrupt has been enabled on the PCI device.

Thanks,
Gavin

Quoted 27 lines
Quoted 24 lines
Quoted 22 lines
Quoted 16 lines
hw/vfio/pci.c +1 -0
+
 static int vfio_initfn(PCIDevice *pdev)
 {
     VFIOPCIDevice *vdev = DO_UPCAST(VFIOPCIDevice, pdev, pdev);
include/hw/vfio/vfio.h +2 -0
index 0b26cd8..52de277 100644
@@ -5,5 +5,7 @@
 
 extern int vfio_container_ioctl(AddressSpace *as, int32_t groupid,
                                 int req, void *param);
+extern int vfio_eeh_pe_reset(AddressSpace *as,
+                             int32_t groupid, uint32_t option);
 
 #endif
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#8 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] VFIO: Clear stale MSIx table during EEH reset

Quoted 178 lines · Gavin Shan
Quoted 105 lines · Alex Williamson
Quoted 104 lines · Gavin Shan
Quoted 22 lines · David Gibson
Quoted 21 lines · Gavin Shan

The PCI device MSIx table is cleaned out in hardware after EEH PE reset. However, we still hold the stale MSIx entries in QEMU, which should be cleared accordingly. Otherwise, we will run into another (recursive) EEH error and the PCI devices contained in the PE have to be offlined exceptionally.

The patch introduces function vfio_eeh_pe_reset(), which is called by sPAPR when asserting hot or fundamental reset, to clear stale MSIx table before EEH PE reset so that MSIx table could be restored properly after EEH PE reset.

---
 hw/ppc/spapr_pci_vfio.c | 13 +++++++++----
 hw/vfio/Makefile.objs   |  6 +++++-
 hw/vfio/pci-stub.c      | 16 ++++++++++++++++
 hw/vfio/pci.c           | 36 ++++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio.h  |  2 ++
 5 files changed, 68 insertions(+), 5 deletions(-)
 create mode 100644 hw/vfio/pci-stub.c
Quoted 16 lines

hw/ppc/spapr_pci_vfio.c static int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option) · line 156

Quoted 11 lines
hw/ppc/spapr_pci_vfio.c +3 -0
index 99a1be5..6fa3afe 100644
@@ -151,19 +151,24 @@ static int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
     switch (option) {
     case RTAS_SLOT_RESET_DEACTIVATE:
         op.op = VFIO_EEH_PE_RESET_DEACTIVATE;
+        ret = vfio_container_ioctl(&svphb->phb.iommu_as,
+                                   svphb->iommugroupid,
+                                   VFIO_EEH_PE_OP, &op);

For consistency, I think all the reset operations should go through vfio_eeh_pe_reset(), even though in this case it won't do more than call vfio_container_ioctl().

Fair enough. I'll fix :-)

Quoted 60 lines
Quoted 47 lines
hw/ppc/spapr_pci_vfio.c +6 -4
         break;
     case RTAS_SLOT_RESET_HOT:
-        op.op = VFIO_EEH_PE_RESET_HOT;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_HOT);
         break;
     case RTAS_SLOT_RESET_FUNDAMENTAL:
-        op.op = VFIO_EEH_PE_RESET_FUNDAMENTAL;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_FUNDAMENTAL);
         break;
     default:
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&svphb->phb.iommu_as, svphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
hw/vfio/Makefile.objs +5 -1
index e31f30e..1b8a065 100644
@@ -1,4 +1,8 @@
 ifeq ($(CONFIG_LINUX), y)
 obj-$(CONFIG_SOFTMMU) += common.o
-obj-$(CONFIG_PCI) += pci.o
+ifeq ($(CONFIG_PCI), y)
+obj-y += pci.o
+else
+obj-y += pci-stub.o
+endif
 endif
hw/vfio/pci-stub.c new file +5 -0
new file mode 100644
index 0000000..f317c1e
@@ -0,0 +1,16 @@
+/*
+ * To include the file on !CONFIG_PCI
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.

hw/vfio/pci-stub.c line 15

Quoted 10 lines
hw/vfio/pci-stub.c new file +10 -0
+ */
+
+#include <linux/vfio.h>
+
+#include "exec/memory.h"
+#include "hw/vfio/vfio.h"
+
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
+{
+    return -1;

Probably should have assert(0) here - this should never be called if !CONFIG_PCI.

Quoted 6 lines
Quoted 2 lines

Indeed, assert(0) would be better. I just replied to ask for dropping the stub for !CONFIG_PCI if you and Alex.W agree.

I certainly don't see the reason for the stub, it was only suggested before because a previous version had the callout in hw/vfio/common.c

Ok. I'll drop it in next revision.

Quoted 45 lines
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
hw/vfio/pci-stub.c new file +1 -0
+}
hw/vfio/pci.c +27 -0
index 6b80539..d0fd4b4 100644
@@ -3319,6 +3319,42 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
     vdev->req_enabled = false;
 }
 
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
+{
+    VFIOGroup *group;
+    VFIODevice *vbasedev;
+    VFIOPCIDevice *vdev;
+    struct vfio_eeh_pe_op op = {
+        .argsz = sizeof(op),
+        .op = option
+    };
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        error_report("vfio: group %d not found\n", groupid);
+        return -1;
+    }
+
+    /*
+     * The MSIx table will be cleaned out by reset. We need
+     * disable it so that it can be reenabled properly. Also,
+     * the cached MSIx table should be cleared as it's not
+     * reflecting the contents in hardware.
+     */
+    QLIST_FOREACH(vbasedev, &group->device_list, next) {
+        vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+        if (msix_enabled(&vdev->pdev)) {
+            vfio_disable_msix(vdev);
+        }
Quoted 8 lines
Quoted 8 lines

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 3356

Quoted 8 lines
hw/vfio/pci.c +8 -0
+
+        msix_reset(&vdev->pdev);
+    }
+
+    vfio_put_group(group);
+
+    return vfio_container_ioctl(as, groupid, VFIO_EEH_PE_OP, &op);
+}
Quoted 8 lines

So all you're trying to do here is find the devices in the PE and disable/reset MSI-X, but do you really need yet another ugly callback into vfio to do that? Isn't it possible to find the devices based on the address space or PCI topology? If we have EEH emulation, don't you also want to do this for emulated devices? The vfio_disable_msix() call could be replaced by the equivalent config space access to make it look like the guest disabled MSI-X.

EEH for emulated PCI device is out of scope for now, which depends on fully emulated IBM's PHB. The PE reset is requested by guest and guest is aware of losing MSIx table after that.

I'm not sure I'm following your suggestion, but yes, the VFIO PCI devices can be identified by checking its class string with help of some QOM helper functions. So I guess you are suggesting something as follows, which would make the code a bit cleaner.

Quoted 4 lines

- In hw/ppc/spapr_pci_vfio.c::spapr_phb_vfio_eeh_reset(), check all PCI devices hooked to the PHB and if it's a VFIO PCI device, disable MSIx interrupt by clearing MSIX_ENABLE in the config space and cleaning out the MSIx table if MSIx interrupt has been enabled on the PCI device.

That's what I'm suggesting, but why do you even need to check whether the subordinate device is vfio? I imagine you can't mix emulated and vfio devices behind a phb, but even if you could, what's the harm in doing the same MSI-X reset on emulated devices? You don't need to support EEH on emulated, but you also don't need to handle vfio uniquely here. Thanks,

Alex

author
#9 lore raw

Re: [Qemu-devel] [PATCH v3 1/2] VFIO: Clear stale MSIx table during EEH reset

Quoted 184 lines · Alex Williamson
Quoted 178 lines · Gavin Shan
Quoted 105 lines · Alex Williamson
Quoted 104 lines · Gavin Shan
Quoted 22 lines · David Gibson
Quoted 21 lines · Gavin Shan

The PCI device MSIx table is cleaned out in hardware after EEH PE reset. However, we still hold the stale MSIx entries in QEMU, which should be cleared accordingly. Otherwise, we will run into another (recursive) EEH error and the PCI devices contained in the PE have to be offlined exceptionally.

The patch introduces function vfio_eeh_pe_reset(), which is called by sPAPR when asserting hot or fundamental reset, to clear stale MSIx table before EEH PE reset so that MSIx table could be restored properly after EEH PE reset.

---
 hw/ppc/spapr_pci_vfio.c | 13 +++++++++----
 hw/vfio/Makefile.objs   |  6 +++++-
 hw/vfio/pci-stub.c      | 16 ++++++++++++++++
 hw/vfio/pci.c           | 36 ++++++++++++++++++++++++++++++++++++
 include/hw/vfio/vfio.h  |  2 ++
 5 files changed, 68 insertions(+), 5 deletions(-)
 create mode 100644 hw/vfio/pci-stub.c
Quoted 16 lines

hw/ppc/spapr_pci_vfio.c static int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option) · line 156

Quoted 11 lines
hw/ppc/spapr_pci_vfio.c +3 -0
index 99a1be5..6fa3afe 100644
@@ -151,19 +151,24 @@ static int spapr_phb_vfio_eeh_reset(sPAPRPHBState *sphb, int option)
     switch (option) {
     case RTAS_SLOT_RESET_DEACTIVATE:
         op.op = VFIO_EEH_PE_RESET_DEACTIVATE;
+        ret = vfio_container_ioctl(&svphb->phb.iommu_as,
+                                   svphb->iommugroupid,
+                                   VFIO_EEH_PE_OP, &op);

For consistency, I think all the reset operations should go through vfio_eeh_pe_reset(), even though in this case it won't do more than call vfio_container_ioctl().

Fair enough. I'll fix :-)

Quoted 60 lines
Quoted 47 lines
hw/ppc/spapr_pci_vfio.c +6 -4
         break;
     case RTAS_SLOT_RESET_HOT:
-        op.op = VFIO_EEH_PE_RESET_HOT;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_HOT);
         break;
     case RTAS_SLOT_RESET_FUNDAMENTAL:
-        op.op = VFIO_EEH_PE_RESET_FUNDAMENTAL;
+        ret = vfio_eeh_pe_reset(&svphb->phb.iommu_as,
+                                svphb->iommugroupid,
+                                VFIO_EEH_PE_RESET_FUNDAMENTAL);
         break;
     default:
         return RTAS_OUT_PARAM_ERROR;
     }
 
-    ret = vfio_container_ioctl(&svphb->phb.iommu_as, svphb->iommugroupid,
-                               VFIO_EEH_PE_OP, &op);
     if (ret < 0) {
         return RTAS_OUT_HW_ERROR;
     }
hw/vfio/Makefile.objs +5 -1
index e31f30e..1b8a065 100644
@@ -1,4 +1,8 @@
 ifeq ($(CONFIG_LINUX), y)
 obj-$(CONFIG_SOFTMMU) += common.o
-obj-$(CONFIG_PCI) += pci.o
+ifeq ($(CONFIG_PCI), y)
+obj-y += pci.o
+else
+obj-y += pci-stub.o
+endif
 endif
hw/vfio/pci-stub.c new file +5 -0
new file mode 100644
index 0000000..f317c1e
@@ -0,0 +1,16 @@
+/*
+ * To include the file on !CONFIG_PCI
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2.  See
+ * the COPYING file in the top-level directory.

hw/vfio/pci-stub.c line 15

Quoted 10 lines
hw/vfio/pci-stub.c new file +10 -0
+ */
+
+#include <linux/vfio.h>
+
+#include "exec/memory.h"
+#include "hw/vfio/vfio.h"
+
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
+{
+    return -1;

Probably should have assert(0) here - this should never be called if !CONFIG_PCI.

Quoted 6 lines
Quoted 2 lines

Indeed, assert(0) would be better. I just replied to ask for dropping the stub for !CONFIG_PCI if you and Alex.W agree.

I certainly don't see the reason for the stub, it was only suggested before because a previous version had the callout in hw/vfio/common.c

Ok. I'll drop it in next revision.

Quoted 45 lines
Quoted 36 lines
Quoted 36 lines
Quoted 36 lines
hw/vfio/pci-stub.c new file +1 -0
+}
hw/vfio/pci.c +27 -0
index 6b80539..d0fd4b4 100644
@@ -3319,6 +3319,42 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)
     vdev->req_enabled = false;
 }
 
+int vfio_eeh_pe_reset(AddressSpace *as, int32_t groupid, uint32_t option)
+{
+    VFIOGroup *group;
+    VFIODevice *vbasedev;
+    VFIOPCIDevice *vdev;
+    struct vfio_eeh_pe_op op = {
+        .argsz = sizeof(op),
+        .op = option
+    };
+
+    group = vfio_get_group(groupid, as);
+    if (!group) {
+        error_report("vfio: group %d not found\n", groupid);
+        return -1;
+    }
+
+    /*
+     * The MSIx table will be cleaned out by reset. We need
+     * disable it so that it can be reenabled properly. Also,
+     * the cached MSIx table should be cleared as it's not
+     * reflecting the contents in hardware.
+     */
+    QLIST_FOREACH(vbasedev, &group->device_list, next) {
+        vdev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
+        if (msix_enabled(&vdev->pdev)) {
+            vfio_disable_msix(vdev);
+        }
Quoted 8 lines
Quoted 8 lines

hw/vfio/pci.c static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) · line 3356

Quoted 8 lines
hw/vfio/pci.c +8 -0
+
+        msix_reset(&vdev->pdev);
+    }
+
+    vfio_put_group(group);
+
+    return vfio_container_ioctl(as, groupid, VFIO_EEH_PE_OP, &op);
+}
Quoted 8 lines

So all you're trying to do here is find the devices in the PE and disable/reset MSI-X, but do you really need yet another ugly callback into vfio to do that? Isn't it possible to find the devices based on the address space or PCI topology? If we have EEH emulation, don't you also want to do this for emulated devices? The vfio_disable_msix() call could be replaced by the equivalent config space access to make it look like the guest disabled MSI-X.

EEH for emulated PCI device is out of scope for now, which depends on fully emulated IBM's PHB. The PE reset is requested by guest and guest is aware of losing MSIx table after that.

I'm not sure I'm following your suggestion, but yes, the VFIO PCI devices can be identified by checking its class string with help of some QOM helper functions. So I guess you are suggesting something as follows, which would make the code a bit cleaner.

Quoted 4 lines

- In hw/ppc/spapr_pci_vfio.c::spapr_phb_vfio_eeh_reset(), check all PCI devices hooked to the PHB and if it's a VFIO PCI device, disable MSIx interrupt by clearing MSIX_ENABLE in the config space and cleaning out the MSIx table if MSIx interrupt has been enabled on the PCI device.

Quoted 7 lines

That's what I'm suggesting, but why do you even need to check whether the subordinate device is vfio? I imagine you can't mix emulated and vfio devices behind a phb, but even if you could, what's the harm in doing the same MSI-X reset on emulated devices? You don't need to support EEH on emulated, but you also don't need to handle vfio uniquely here. Thanks,

Thanks for confirm. Yes, it's fair enough since EEH is platform unique feature. I'll move the logic to sPAPR platform.

The VFIO PCI devices could be hooked to PCI bus, which is leaded from emulated PCI bridge. It might be harmless to reset MSIx table for the upstream emulatd bridge, but pointless as it's not part of the PE, on which we're applying the reset. However, I doubt if a emulated PCI bridge needs MSIx at all.

Thanks,
Gavin

Quoted 2 lines

Alex

author
#10 lore raw

PATCH v3 2/2 sPAPR: Reenable EEH functionality on reboot

In reply to Gavin Shan

When rebooting the guest, some PEs might be in frozen state. The contained PCI devices won't work properly if their frozen states aren't cleared in time. One case running into this situation would be maximal EEH error times encountered in the guest.

The patch reenables the EEH functinality on PEs on PHB's reset callback, which will clear their frozen states if needed.

---
 hw/ppc/spapr_pci_vfio.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
hw/ppc/spapr_pci_vfio.c +13 -1
index 6fa3afe..25c1b3e 100644
@@ -71,9 +71,21 @@ static void spapr_phb_vfio_finish_realize(sPAPRPHBState *sphb, Error **errp)
                                 spapr_tce_get_iommu(tcet));
 }
 
+/*
+ * The PE might be in frozen state. To reenable the EEH
+ * functionality on it will clean the frozen state, which
+ * ensures that the contained PCI devices will work properly.
+ */
 static void spapr_phb_vfio_reset(DeviceState *qdev)
 {
-    /* Do nothing */
+    sPAPRPHBVFIOState *svphb = SPAPR_PCI_VFIO_HOST_BRIDGE(qdev);
+    struct vfio_eeh_pe_op op = {
+        .argsz = sizeof(op),
+        .op = VFIO_EEH_PE_ENABLE
+    };
+
+    vfio_container_ioctl(&svphb->phb.iommu_as,
+                         svphb->iommugroupid, VFIO_EEH_PE_OP, &op);
 }
 
 static int spapr_phb_vfio_eeh_set_option(sPAPRPHBState *sphb,
Signature · 1 line
1.8.3.2
Reviewed-by on v3 2/2
#11 lore raw

Re: [Qemu-devel] [PATCH 2/2] sPAPR: Reenable EEH functionality on reboot

Quoted 9 lines · Gavin Shan

When rebooting the guest, some PEs might be in frozen state. The contained PCI devices won't work properly if their frozen states aren't cleared in time. One case running into this situation would be maximal EEH error times encountered in the guest.

The patch reenables the EEH functinality on PEs on PHB's reset callback, which will clear their frozen states if needed.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
Reviewed-by on v3 2/2
#12 lore raw

Re: [Qemu-devel] [PATCH 2/2] sPAPR: Reenable EEH functionality on reboot

In reply to Gavin Shan
Quoted 9 lines · Gavin Shan

When rebooting the guest, some PEs might be in frozen state. The contained PCI devices won't work properly if their frozen states aren't cleared in time. One case running into this situation would be maximal EEH error times encountered in the guest.

The patch reenables the EEH functinality on PEs on PHB's reset callback, which will clear their frozen states if needed.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
author
#13 lore raw

Re: [Qemu-devel] [PATCH 2/2] sPAPR: Reenable EEH functionality on reboot

Quoted 6 lines · David Gibson
Quoted 5 lines · Gavin Shan

When rebooting the guest, some PEs might be in frozen state. The contained PCI devices won't work properly if their frozen states aren't cleared in time. One case running into this situation would be maximal EEH error times encountered in the guest.

Quoted 7 lines
Quoted 4 lines

The patch reenables the EEH functinality on PEs on PHB's reset callback, which will clear their frozen states if needed.

Thanks for your review, David.

Quoted 5 lines
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson