Skip to content
systemcoding.dev
Kernels

Recover From PCIe Link Faults in a Driver With err_handler Callbacks

Implement struct pci_error_handlers in a Linux 6.12 driver: error_detected, slot_reset and resume, how the AER core drives them and what each must restore.

6 min read
Summarize in AI
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, C and FreeBSD logos.

A PCI Express link can fail while a machine runs: a signal glitch corrupts a packet, an endpoint stops responding, a downstream port detects a fatal error. On a driver that does nothing about it, the outcome is a device that returns all ones forever, I/O that hangs, and a reboot to clear it. The kernel has a recovery protocol for this, driven by the Advanced Error Reporting core, and a driver takes part by providing a struct pci_error_handlers. This post implements the callbacks on Linux 6.12, explains the sequence the core runs, and lists what each callback must and must not do. It extends the PCI driver skeleton, and the surprise-removal case it is often confused with is covered in the hotplug post.

The protocol is specified in the kernel's PCI Error Recovery document, and AER itself in the AER HOWTO.

Error classes

AER sorts errors by severity, and the severity decides what the core does:

ClassMeaningAction
CorrectableThe hardware fixed it, for example a bad TLP that was retransmittedCounted and logged, no driver involvement
Uncorrectable, non-fatalA transaction failed but the link still worksRecovery is attempted
Uncorrectable, fatalThe link is unreliableA reset of the link is required

Correctable errors need no code of yours. For the other two, the core walks the affected devices and calls the driver's handlers. Check the counters first, since they cost nothing to read:

Shell
cat /sys/bus/pci/devices/0000:02:00.0/aer_dev_correctable
cat /sys/bus/pci/devices/0000:02:00.0/aer_dev_nonfatal
Text
RxErr 0
BadTLP 3
BadDLLP 0
Rollover 0
Timeout 0
NonFatalErr 0
CorrIntErr 0
HeaderOF 0
TOTAL_ERR_COR 3

A handful of BadTLP entries over days is normal. A count that climbs by the thousand points at a bad connector or riser.

The handler table

c
static const struct pci_error_handlers sample_err_handler = {
	.error_detected	= sample_error_detected,
	.mmio_enabled	= sample_mmio_enabled,
	.slot_reset	= sample_slot_reset,
	.resume		= sample_resume,
};

static struct pci_driver sample_driver = {
	.name		= "sample_pci",
	.id_table	= sample_ids,
	.probe		= sample_probe,
	.remove		= sample_remove,
	.err_handler	= &sample_err_handler,
};

The core calls the four callbacks in a fixed order, and each answers with a pci_ers_result_t that tells it how to proceed:

StepCallbackWhenWhat the driver does
1error_detectedAn error was foundStop I/O, say whether a reset is needed
2mmio_enabledThe link works again, no reset yetCheck the device, optionally recover without a reset
3slot_resetAfter the slot was resetReinitialize the device
4resumeRecovery succeededRestart I/O

mmio_enabled is called only if error_detected returned PCI_ERS_RESULT_CAN_RECOVER, and slot_reset only if some handler asked for a reset with PCI_ERS_RESULT_NEED_RESET.

error_detected

The first callback receives the channel state, which tells you how bad things are:

c
static pci_ers_result_t sample_error_detected(struct pci_dev *pdev,
					      pci_channel_state_t state)
{
	struct sample *s = pci_get_drvdata(pdev);

	dev_warn(&pdev->dev, "PCI error detected, state %d\n", state);

	sample_stop_io(s);

	if (state == pci_channel_io_perm_failure)
		return PCI_ERS_RESULT_DISCONNECT;

	if (state == pci_channel_io_normal)
		return PCI_ERS_RESULT_CAN_RECOVER;

	pci_disable_device(pdev);
	return PCI_ERS_RESULT_NEED_RESET;
}

The states mean the following. pci_channel_io_normal says the link still works and only a transaction failed, so MMIO is usable. pci_channel_io_frozen says the slot is frozen: the platform has isolated the device, and any MMIO read returns all ones and any write is dropped. pci_channel_io_perm_failure says the device is gone for good.

Two rules apply in this callback. First, stop all I/O: halt your queues, cancel timers and work items, and prevent new submissions, but do not touch the device registers if the state is frozen, since every access fails. Second, never sleep for a long time or take locks that user threads hold while waiting on the device. The return value picks the next step: DISCONNECT removes the device from the system, NEED_RESET asks for a slot reset, and CAN_RECOVER skips the reset.

mmio_enabled

If every driver on the slot answered CAN_RECOVER, the core re-enables MMIO and calls this next. Registers can be read again, and a driver that can recover without a reset uses the call to check and clear the error state in the device:

c
static pci_ers_result_t sample_mmio_enabled(struct pci_dev *pdev)
{
	struct sample *s = pci_get_drvdata(pdev);

	if (ioread32(s->regs + REG_ID) == 0xffffffff)
		return PCI_ERS_RESULT_NEED_RESET;

	sample_clear_error(s);
	return PCI_ERS_RESULT_RECOVERED;
}

The all-ones test is the standard probe for a dead link. A driver with no way to recover in place can return PCI_ERS_RESULT_NEED_RESET here too, and the core proceeds to a reset.

slot_reset

After the slot has been reset, the device is in its power-on state. The configuration space is gone, the BARs are unprogrammed and the driver's private state in the device is lost. The core restores the standard configuration space for you only if you saved it, which is why probe should call pci_save_state() once after the device is set up:

c
pci_save_state(pdev);	/* at the end of probe */

The callback then restores it and rebuilds the device:

c
static pci_ers_result_t sample_slot_reset(struct pci_dev *pdev)
{
	struct sample *s = pci_get_drvdata(pdev);

	if (pci_enable_device_mem(pdev)) {
		dev_err(&pdev->dev, "cannot re-enable device after reset\n");
		return PCI_ERS_RESULT_DISCONNECT;
	}

	pci_restore_state(pdev);
	pci_save_state(pdev);
	pci_set_master(pdev);

	if (sample_hw_init(s))
		return PCI_ERS_RESULT_DISCONNECT;

	return PCI_ERS_RESULT_RECOVERED;
}

The sequence matters. pci_enable_device_mem() re-enables memory decoding, pci_restore_state() writes back the saved BARs and command register, saving again preserves the restored copy for a later failure, and pci_set_master() turns bus mastering on if the device does DMA. After that the driver reprograms everything the hardware lost, such as ring base addresses and interrupt masks. Returning PCI_ERS_RESULT_DISCONNECT gives up on the device.

resume

The last callback runs when every device on the slot reported RECOVERED. The device is alive and the registers are valid, and the driver restarts what it stopped in error_detected:

c
static void sample_resume(struct pci_dev *pdev)
{
	struct sample *s = pci_get_drvdata(pdev);

	sample_start_io(s);
	dev_info(&pdev->dev, "recovered from PCIe error\n");
}

On a successful recovery the log shows the platform's account of the event followed by the driver's line:

Text
pcieport 0000:00:1c.0: AER: Uncorrected (Non-Fatal) error received: 0000:02:00.0
sample_pci 0000:02:00.0: PCI error detected, state 1
pcieport 0000:00:1c.0: AER: device recovery successful
sample_pci 0000:02:00.0: recovered from PCIe error

State 1 is pci_channel_io_normal in the kernel's numbering, which is why mmio_enabled ran and no reset happened.

Things that go wrong

A driver with no err_handler is not ignored: for a fatal error the core resets the slot anyway, and the device comes back with nothing configured and no driver aware of it. The visible symptom is a device that stops working after a log line saying recovery was successful. If the device matters, supply the callbacks.

Work items and interrupts that survive error_detected cause the worst bugs, because they run against a frozen device and read all ones as valid data. A completion handler that parses 0xffffffff as a packet length is the typical result. Stop and flush the workers inside the callback.

The reset itself can be tested without a real fault by using the AER error injection facility, aer-inject from the kernel's drivers/pci/pcie/aer_inject.c, on a test machine. Its availability depends on CONFIG_PCIEAER_INJECT, and it injects errors through a character device. That is the only practical way to exercise slot_reset, and a recovery path that has never run should be assumed broken.

Also inHardware
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.