pcie_flr() clears volatile PCI configuration state (BARs, command
register, MSI/MSI-X, PCIe capability registers) and, as documented in
pci(9), leaves saving and restoring configuration space to its callers.
pci_reset_child() previously only invoked BUS_RESET_PREPARE() and
BUS_RESET_POST() on the fallback pci_power_reset() path, skipping both
whenever pcie_flr() succeeded.
Plain devctl reset was unaffected because pci_suspend_child() and
pci_resume_child() save and restore configuration space on their own.
On devctl reset -d (DEVF_RESET_DETACH), however,
device_probe_and_attach() re-attaches the driver with hardware BARs
cleared to zero. Because pci_alloc_multi_resource() reuses the existing
RLE_RESERVED resource list entries from boot enumeration, it does not
reprogram the hardware BARs; activating memory resources enables
PCIM_CMD_MEMEN against zeroed BARs and MMIO reads return 0xffffffff.
Move BUS_RESET_PREPARE() and BUS_RESET_POST() to bracket both pcie_flr()
and the pci_power_reset() fallback in pci_reset_child(), and guard
pci_save_state() in pci_reset_prepare() with !device_is_suspended(child)
and PCI_POWERSTATE_D0 so it does not overwrite valid D0 state already
saved by pci_suspend_child(), pci_child_detached(), or
pci_probe_nomatch(). This guard also protects bus-level resets in
pcib_reset_child(), where bus_helper_reset_prepare() calls
BUS_RESET_PREPARE() on each child after suspending or detaching it.
This change also includes two separate but closely related power-state
fixes in pci_reset_child():
- Wake non-D0 devices to PCI_POWERSTATE_D0 after BUS_RESET_PREPARE() and before attempting the reset. In D3cold, config-space reads in pcie_flr() return 0xffffffff (causing a 1000 ms pending-transaction timeout stall), and as Linux's pci_dev_save_and_disable() notes, PM registers default to D0 after reset anyway. For devices already in D0 (including devices without the power management capability), this is a no-op; on non-FLR devices with power management reset without -d under hw.pci.do_power_suspend=1, waking before the reset adds two extra power-state transitions (D3 -> D0 -> D3) relative to the prior code, where suspend/restore already cycled D0 -> D3 -> D0.
- On DEVF_RESET_DETACH, if the child remains unattached and in D0 after device_probe_and_attach(), call pci_cfg_save(..., 1) (matching pci_resume_child()) to preserve hw.pci.do_power_nodriver power-down behavior.
Tested on 16.0-CURRENT (INVARIANTS/WITNESS) on Google Compute Engine
arm64 (c4a-highmem-96-metal, c4a-standard-32 VM) and amd64
(c4-standard-288-metal, c4-standard-32 VM) using an in-development
gve(4) driver with no driver-side PCI state restoration workaround:
- Unpatched control kernels on both arm64 and amd64 bare metal confirmed devctl reset -d wipes BARs, PCIR_COMMAND, and PCIe capability state on FLR-capable endpoints, causing gve(4) re-attach to fail with ETIMEDOUT.
- With the patch, devctl reset -d and devctl reset preserved BARs, PCIR_COMMAND, PMCSR, MSI-X, and PCIe capability registers across hw.pci.do_power_suspend={0,1}, hw.pci.do_power_nodriver={0,3}, already-detached/D3 endpoints, driverless endpoints (verifying config-space and D3 state preservation), repeated reset loops on gve(4) with live traffic and incrementing MSI-X interrupts, and pcib_reset_child() bus resets on arm64.
- DTrace fbt tracing confirmed pci_reset_prepare() skips saving when suspended or in D3, non-D0 endpoints wake to D0 before the reset, and non-FLR VM devices without power management (virtio_pci(4), emulated gve(4)) fall back cleanly to pci_power_reset() with no WITNESS regressions.
Fixes: 5db2a4a8129d ("Implement resets for PCI buses and PCIe bridges.")
Reviewed-by: Jasper Tran O'Leary <jtranoleary@google.com>
Signed-off-by: Tim Hostetler <thostet@google.com>