PCI: pnv_php: Fix surprise plug detection and recovery
authorTimothy Pearson <tpearson@raptorengineering.com>
Tue, 15 Jul 2025 21:39:06 +0000 (16:39 -0500)
committerMadhavan Srinivasan <maddy@linux.ibm.com>
Sat, 26 Jul 2025 07:39:15 +0000 (13:09 +0530)
The existing PowerNV hotplug code did not handle surprise plug events
correctly, leading to a complete failure of the hotplug system after device
removal and a required reboot to detect new devices.

This comes down to two issues:

 1) When a device is surprise removed, often the bridge upstream
    port will cause a PE freeze on the PHB.  If this freeze is not
    cleared, the MSI interrupts from the bridge hotplug notification
    logic will not be received by the kernel, stalling all plug events
    on all slots associated with the PE.

 2) When a device is removed from a slot, regardless of surprise or
    programmatic removal, the associated PHB/PE ls left frozen.
    If this freeze is not cleared via a fundamental reset, skiboot
    is unable to clear the freeze and cannot retrain / rescan the
    slot.  This also requires a reboot to clear the freeze and redetect
    the device in the slot.

Issue the appropriate unfreeze and rescan commands on hotplug events,
and don't oops on hotplug if pci_bus_to_OF_node() returns NULL.

Signed-off-by: Timothy Pearson <tpearson@raptorengineering.com>
[bhelgaas: tidy comments]
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
Signed-off-by: Madhavan Srinivasan <maddy@linux.ibm.com>
Link: https://patch.msgid.link/171044224.1359864.1752615546988.JavaMail.zimbra@raptorengineeringinc.com
arch/powerpc/kernel/pci-hotplug.c
drivers/pci/hotplug/pnv_php.c

index 9ea74973d78d5a20e619877d2d2d66a07c62fb4e..6f444d0822d8208d7a3078e51c5f1ca407c28f0a 100644 (file)
@@ -141,6 +141,9 @@ void pci_hp_add_devices(struct pci_bus *bus)
        struct pci_controller *phb;
        struct device_node *dn = pci_bus_to_OF_node(bus);
 
+       if (!dn)
+               return;
+
        phb = pci_bus_to_host(bus);
 
        mode = PCI_PROBE_NORMAL;
index 5476c9e7760d4df62329a841e401916b2a5a451c..4f85e7fe29ec2339b7467ea63629411dffdd902c 100644 (file)
@@ -4,12 +4,14 @@
  *
  * Copyright Gavin Shan, IBM Corporation 2016.
  * Copyright (C) 2025 Raptor Engineering, LLC
+ * Copyright (C) 2025 Raptor Computing Systems, LLC
  */
 
 #include <linux/bitfield.h>
 #include <linux/libfdt.h>
 #include <linux/module.h>
 #include <linux/pci.h>
+#include <linux/delay.h>
 #include <linux/pci_hotplug.h>
 #include <linux/of_fdt.h>
 
@@ -469,6 +471,61 @@ static int pnv_php_set_attention_state(struct hotplug_slot *slot, u8 state)
        return 0;
 }
 
+static int pnv_php_activate_slot(struct pnv_php_slot *php_slot,
+                                struct hotplug_slot *slot)
+{
+       int ret, i;
+
+       /*
+        * Issue initial slot activation command to firmware
+        *
+        * Firmware will power slot on, attempt to train the link, and
+        * discover any downstream devices. If this process fails, firmware
+        * will return an error code and an invalid device tree. Failure
+        * can be caused for multiple reasons, including a faulty
+        * downstream device, poor connection to the downstream device, or
+        * a previously latched PHB fence.  On failure, issue fundamental
+        * reset up to three times before aborting.
+        */
+       ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_ON);
+       if (ret) {
+               SLOT_WARN(
+                       php_slot,
+                       "PCI slot activation failed with error code %d, possible frozen PHB",
+                       ret);
+               SLOT_WARN(
+                       php_slot,
+                       "Attempting complete PHB reset before retrying slot activation\n");
+               for (i = 0; i < 3; i++) {
+                       /*
+                        * Slot activation failed, PHB may be fenced from a
+                        * prior device failure.
+                        *
+                        * Use the OPAL fundamental reset call to both try a
+                        * device reset and clear any potentially active PHB
+                        * fence / freeze.
+                        */
+                       SLOT_WARN(php_slot, "Try %d...\n", i + 1);
+                       pci_set_pcie_reset_state(php_slot->pdev,
+                                                pcie_warm_reset);
+                       msleep(250);
+                       pci_set_pcie_reset_state(php_slot->pdev,
+                                                pcie_deassert_reset);
+
+                       ret = pnv_php_set_slot_power_state(
+                               slot, OPAL_PCI_SLOT_POWER_ON);
+                       if (!ret)
+                               break;
+               }
+
+               if (i >= 3)
+                       SLOT_WARN(php_slot,
+                                 "Failed to bring slot online, aborting!\n");
+       }
+
+       return ret;
+}
+
 static int pnv_php_enable(struct pnv_php_slot *php_slot, bool rescan)
 {
        struct hotplug_slot *slot = &php_slot->slot;
@@ -531,7 +588,7 @@ static int pnv_php_enable(struct pnv_php_slot *php_slot, bool rescan)
                goto scan;
 
        /* Power is off, turn it on and then scan the slot */
-       ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_ON);
+       ret = pnv_php_activate_slot(php_slot, slot);
        if (ret)
                return ret;
 
@@ -838,16 +895,63 @@ static int pnv_php_enable_msix(struct pnv_php_slot *php_slot)
        return entry.vector;
 }
 
+static void
+pnv_php_detect_clear_suprise_removal_freeze(struct pnv_php_slot *php_slot)
+{
+       struct pci_dev *pdev = php_slot->pdev;
+       struct eeh_dev *edev;
+       struct eeh_pe *pe;
+       int i, rc;
+
+       /*
+        * When a device is surprise removed from a downstream bridge slot,
+        * the upstream bridge port can still end up frozen due to related EEH
+        * events, which will in turn block the MSI interrupts for slot hotplug
+        * detection.
+        *
+        * Detect and thaw any frozen upstream PE after slot deactivation.
+        */
+       edev = pci_dev_to_eeh_dev(pdev);
+       pe = edev ? edev->pe : NULL;
+       rc = eeh_pe_get_state(pe);
+       if ((rc == -ENODEV) || (rc == -ENOENT)) {
+               SLOT_WARN(
+                       php_slot,
+                       "Upstream bridge PE state unknown, hotplug detect may fail\n");
+       } else {
+               if (pe->state & EEH_PE_ISOLATED) {
+                       SLOT_WARN(
+                               php_slot,
+                               "Upstream bridge PE %02x frozen, thawing...\n",
+                               pe->addr);
+                       for (i = 0; i < 3; i++)
+                               if (!eeh_unfreeze_pe(pe))
+                                       break;
+                       if (i >= 3)
+                               SLOT_WARN(
+                                       php_slot,
+                                       "Unable to thaw PE %02x, hotplug detect will fail!\n",
+                                       pe->addr);
+                       else
+                               SLOT_WARN(php_slot,
+                                         "PE %02x thawed successfully\n",
+                                         pe->addr);
+               }
+       }
+}
+
 static void pnv_php_event_handler(struct work_struct *work)
 {
        struct pnv_php_event *event =
                container_of(work, struct pnv_php_event, work);
        struct pnv_php_slot *php_slot = event->php_slot;
 
-       if (event->added)
+       if (event->added) {
                pnv_php_enable_slot(&php_slot->slot);
-       else
+       } else {
                pnv_php_disable_slot(&php_slot->slot);
+               pnv_php_detect_clear_suprise_removal_freeze(php_slot);
+       }
 
        kfree(event);
 }