Re: [PATCH v7 13/14] PCI: rzg3s-host: Re-enumerate the bus on PCIe link-state changes

From: Claudiu Beznea

Date: Wed Dec 02 2026 - 05:17:50 EST




On 11/30/26 09:28, Manivannan Sadhasivam wrote:
On Thu, Nov 26, 2026 at 07:40:41PM +0200, Claudiu Beznea wrote:


On 11/25/26 19:19, Manivannan Sadhasivam wrote:
On 2026-10-19 09:50 +0300, Claudiu Beznea wrote:
From: John Madieu <john.madieu.xa@xxxxxxxxxxxxxx>

The RZ/G3{E, S}, RZ/V2{H(P), N} PCIe controllers do not expose the
standard PCIe Slot Capability registers, so the generic pciehp driver
cannot be used. The only link-state signal the hardware provides is the
DL_UpDown bit in the PEIS0 event status register, which is raised on every
Data Link layer up/down transition.

Enable DL_UpDown in PEIE0 and hook up an interrupt handler so the driver
can react to link-state changes: a device that trains after boot gets
enumerated, and a device that disappears on link loss is removed. This
provides hotplug-like behavior without the PCI hotplug core, which is
unavailable for the reason above.

On a DL_UpDown event the handler acks the W1C status bit and inspects
PCSTAT1.DL_DOWN_STS:

- Link up: re-run max link speed negotiation, wait for the link to settle
and rescan the root bus. To cover the case where the device is replaced
or removed while the system is suspended add the
rzg3s_pcie_device_replaced(). It reads the vendor ID, device ID,
revision ID and class code of the device behind the root port and
compares them against the values cached in the corresponding
struct pci_dev, along with the Device Serial Number cached in
struct rzg3s_pcie_host::con_dev_dsn, mirroring pciehp_device_replaced().
If the device is detected as replaced (or no longer present),
pci_dev_set_disconnected() is called for all the devices on the bus
below the root port, so that the teardown of the old devices does not
end up accessing the newly connected device, and the devices are then
removed. If a link up/down cycle occurred while devices are on the bus
(fast link transitions coalesced into a single DL_UpDown event), the
devices are removed and re-enumerated even if the connected device did
not change.
- Link down: walk the bus in reverse and pci_stop_and_remove_bus_device()
each child.

Both paths take pci_lock_rescan_remove() to serialize against the PCI
core.

Link events are processed only after the controller has been fully
initialized.

Add rzg3s_pcie_pm_notifier() to avoid the link event interrupt interfering
with the suspend/resume of the other PCIe devices in the topology, and to
avoid deadlocks caused by pci_dev_lock() being taken from the link event
handler while suspend/resume is in progress. The notifier disables the link
event interrupt before suspend and enables it and rescans the bus, on
resume, to pick up devices that may have been plugged or unplugged while
the system was suspended. struct rzg3s_pcie_host::started was added to
avoid touching the controller registers in case the resume failed.

While at it, make probe tolerant of an absent device. Previously, if the
link failed to come up during rzg3s_pcie_host_setup(), probe tore the
controller back down and failed. Ignore the timeout returned by
rzg3s_pcie_wait_for_link_up(), leave the controller and refclk running,
and let the link-up path enumerate the device once it appears.


This patch is doing too much things at once which makes it hard to follow.
I can easily say that you've combined 4-5 patches into one.

If doing otherwise different bits of this functionality will not work as
expected. E.g., I you are referring to the code behind
rzg3s_pcie_device_replaced() and/or rzg3s_pcie_pm_notifier(): these were
pointed out by Sashiko and I tried to address them. W/o these the
functionality that the patch tried to cover will not fully work for suspend
to RAM. I tried to avoid sets like:

patch 1: add functionality X
patch 2: address corner case Y for functionality X


Why don't you have patches addressing the corner cases in preparation of the
functionality first?

The thing with this approach is that, in general, I will have to address a corner case for a functionality that is not in the tree, and sometime, when part of it could be done, it's not covering all the cases (or at least the ones that I may have thought about). There will also be Sashiko which will raise probably more defects (per individual patches) if following this approach. In the happiest case, that would lead to patches introducing code not used anywhere, which may lead, probably, to warnings. E.g., with the code in this patch:

- the code around rzg3s_pcie_wait_for_link_up() may be moved in a separate patch
but doing so, in case it returns an error there is nothing that could recover
the link; previously the failure of the rzg3s_pcie_wait_for_link_up() lead to
probe failure as well; so, with a patch like this, we could end up with a
probed driver, but non-functional
- the changes around rzg3s_pcie_pm_notifier() (as I can see it now) cannot be
moved into a preparatory patch; it depends on the patch introducing the link
up/down IRQ handling
- adding rzg3s_pcie_get_connected_dev(), rzg3s_pcie_cache_connected_dev_id(),
rzg3s_pcie_device_replaced() in a separate, preparatory patch will lead to a
patch containing unused code, which will give compilation warnings; maybe
to avoid compilation warning I can also integrated the call to
rzg3s_pcie_cache_connected_dev_id() in rzg3s_pcie_probe(). Adding also
rzg3s_pcie_device_replaced() in rzg3s_pcie_resume_noirq() would lead to a
broken, unrecoverable, state if it's going to be called (for the scenario that
it tries to cover)
- the code that checks and set the host->started could be added as a preparatory
patch

For the points above, I chose to have it like this.


And even worse, you
add code to remove devices in this patch and add recovery in the next patch.

This split was requested by you in the previous review round:
https://lore.kernel.org/all/a7koqtuhya3psx7mmest3io5h775jxscivumouv64kbu23pssv@xkkqondobxui


I'm not complaining about the split, but just the fact that you are removing the
devices despite recovering the link.

The initial design of this work (v1 from John) was to treat the link down/up events kind of like hotplug events, on link down to remove the devices, on link up to scan the bus. It worked on the initial platform where this was tested (RZ/G3E), but while testing it on more SoCs (especially on RZ/G3S) we found that this don't work for all the cases and having the .reset_root_port() call on link down solved the problem.


Please let me know if I misunderstood your request.

If
you are recovering the link, why do you need to throw the devices away?

The controller don't support native hotplug, but has only IRQ support for
link up/down events. The idea behind this work was to treat the IRQ link
up/down events as hotplug events.

The .reset_root_port() was added since it was noticed that on RZ/G3S, the
NVME connected to PCIE_SLOT don't work after a link down/up sequence w/o it.


Ideally, I would expect the link to retrain in the event of LDn and bring back
the devices to working state again.

On the boards I've tested this, we have the option to select b/w 3 PCIe endpoints being connected to the bus. We select this by writing different bits in a I2C devices. This I2C device does the muxing. As I mentioned in the cover letter of this series, this is how we select the muxing:

i2cset -y 1 0x18 0xf4 0x8 # Link down
i2cset -y 1 0x18 0xf4 0xc # Link up: PCIE_SLOT (NVMe Micron)
i2cset -y 1 0x18 0xf4 0x8 # Link down
i2cset -y 1 0x18 0xf4 0xd # Link up: M.2 Key B (NVMe Transcend)
i2cset -y 1 0x18 0xf4 0x8 # Link down
i2cset -y 1 0x18 0xf4 0xb # Link up: M.2 Key E (WiFi AX200)

At any time, a single endpoint could be connected to the bus. I haven't checked how this behaves, but my expectation is that if we don't remove the devices we may end up with stale devices connected on the bus?

I don't like removing the PCI devices and
adding them back. If you look at existing drivers supporting reset_root_port()
callback, none of them do these.

Do all these have native hotplug? Maybe they have it and this is handled by the code covering hotplug?

As far as I can tell from inspecting the code at drivers/pci/hotplug/pciehp_pci.c, the pciehp_configure_device()/pciehp_unconfigure_device() do the same and these functions are called when devices are added/removed to the system. We've tried to mimic the same behavior but we don't have any hotplug support from our hardware. If that is wrong and we shouldn't do this, then please let me know and I'll follow your suggestion.


But that being said, there is an inherent race in this approach.

Are you referring to the code from this patches or, in general, to the usage of .reset_root_port()?

Since the
recovery happens behind the back of the PCI client driver, it doesn't know when
not to poke the device. So if the driver tries to access the device when
recovery is being done (or before), then it can result in completion timeouts.
For addressing this issue, we need a callback that tells the drivers to quiesce,
but adding such callback is a separate discussion.

If I got this right, shouldn't pci_dev_lock() -> pci_cfg_access_lock() avoid this? But if so, I understand from you that this is not sufficient ?

Thank you,
Claudiu