[PATCH v2 07/12] rust: pci: add an error_detected callback
From: Simon Song
Date: Fri Oct 23 2026 - 11:57:34 EST
VFIO PCI variant drivers will use error_detected() to notify userspace
of PCI errors through vfio-pci-core.
Adding error_detected() to pci::Driver lets VFIO variants reuse the PCI
adapter's error callback dispatch. A following patch removes the
duplicate vfio::pci::Adapter.
Suggested-by: Zhi Wang <zhiw@xxxxxxxxxx>
Signed-off-by: Simon Song <xinmengs@xxxxxxxxxx>
---
rust/kernel/pci.rs | 33 +++++++++++++++++++++++++++++++--
rust/kernel/vfio/pci.rs | 6 ++++++
2 files changed, 37 insertions(+), 2 deletions(-)
diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index 1951b3c8a5c..ddc28099087 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -109,7 +109,7 @@ unsafe fn register(
(*pdrv.get()).remove = Some(Self::remove_callback);
(*pdrv.get()).id_table = T::ID_TABLE.as_ptr();
(*pdrv.get()).driver_managed_dma = T::DRIVER_MANAGED_DMA;
- if T::HAS_RESET_PREPARE || T::HAS_RESET_DONE {
+ if T::HAS_ERROR_DETECTED || T::HAS_RESET_PREPARE || T::HAS_RESET_DONE {
(*pdrv.get()).err_handler = &Self::ERROR_HANDLERS;
}
#[cfg(CONFIG_PCI_IOV)]
@@ -223,8 +223,27 @@ extern "C" fn reset_done_callback(pdev: *mut bindings::pci_dev) {
};
}
+ extern "C" fn error_detected_callback(
+ pdev: *mut bindings::pci_dev,
+ state: bindings::pci_channel_state_t,
+ ) -> bindings::pci_ers_result_t {
+ // SAFETY: PCI error recovery holds the device lock.
+ let dev = unsafe { &*pdev.cast::<Device<device::CoreInternal<'_>>>() };
+ // SAFETY: The device lock prevents removal of installed driver data.
+ unsafe {
+ Self::callback_data_with(&raw mut (*pdev).dev, |data| {
+ T::error_detected(dev, data, state)
+ })
+ }
+ .unwrap_or(bindings::pci_ers_result_PCI_ERS_RESULT_CAN_RECOVER)
+ }
+
const ERROR_HANDLERS: bindings::pci_error_handlers = bindings::pci_error_handlers {
- error_detected: None,
+ error_detected: if T::HAS_ERROR_DETECTED {
+ Some(Self::error_detected_callback)
+ } else {
+ None
+ },
mmio_enabled: None,
slot_reset: None,
reset_prepare: if T::HAS_RESET_PREPARE {
@@ -461,6 +480,16 @@ fn reset_done<'bound>(dev: &Device<device::Core<'_>>, this: Pin<&Self::Data<'bou
build_error!(crate::error::VTABLE_DEFAULT_ERROR)
}
+ /// Handle a PCI error and return the PCI recovery result, with the device lock held.
+ fn error_detected<'bound>(
+ dev: &Device<device::Core<'_>>,
+ this: Pin<&Self::Data<'bound>>,
+ state: bindings::pci_channel_state_t,
+ ) -> bindings::pci_ers_result_t {
+ let _ = (dev, this, state);
+ build_error!(crate::error::VTABLE_DEFAULT_ERROR)
+ }
+
/// Enables Single Root I/O Virtualization (SR-IOV) for a PF.
///
/// Called when userspace writes a nonzero VF count to `sriov_numvfs`. The token carries the
diff --git a/rust/kernel/vfio/pci.rs b/rust/kernel/vfio/pci.rs
index db30dbce6a8..0fbc6a920dc 100644
--- a/rust/kernel/vfio/pci.rs
+++ b/rust/kernel/vfio/pci.rs
@@ -1096,6 +1096,12 @@ pub fn reset_done(&self) -> Result {
// SAFETY: This owner keeps the pinned registration data alive.
unsafe { self.dev.registration_data_with(T::reset_done) }
}
+
+ /// Signal the VFIO error interrupt and return the PCI recovery result.
+ pub fn error_detected(&self) -> bindings::pci_ers_result_t {
+ // SAFETY: This owner keeps the initialized VFIO core device alive.
+ unsafe { bindings::vfio_pci_core_aer_err_detected(self.dev.core_device()) }
+ }
}
impl<T: Operations> Drop for Registration<'_, T> {
--
2.43.7