rust: pci: Introduce PCIe error handler support and sample usage
Subsystems (5)
cover rust: pci: Introduce PCIe error handler support and sample usage
Hi all,
This RFC patchset introduces basic PCIe Advanced Error Reporting (AER) support for Rust PCI drivers and provides a simple example to demonstrate it's usage.
The first patch adds the necessary infrastructure in the Rust PCI layer to support device-specific PCI error handlers, mirroring the existing C struct pci_error_handlers callbacks.
The second patch updates the Rust PCI sample driver to implement a set of dummy error handlers. These callbacks simply print messages and return predefined results, serving as a guide for future Rust PCI driver authors who want to handle PCIe AER events safely.
This series is an RFC for discussion and not intended for merging yet.
---
Guangbo Cui (2):
rust: pci: add PCIe bus error handler support
sample: rust: pci: implement dummy error handlers to demonstrate usage
rust/kernel/pci.rs | 11 ++ rust/kernel/pci/err.rs | 273 ++++++++++++++++++++++++++ samples/rust/rust_dma.rs | 1 + samples/rust/rust_driver_auxiliary.rs | 2 + samples/rust/rust_driver_pci.rs | 47 ++++- 5 files changed, 333 insertions(+), 1 deletion(-) create mode 100644 rust/kernel/pci/err.rs
Signature · 1 line
2.43.0
RFC PATCH 1/2 rust: pci: add PCIe bus error handler support
This patch introduces Rust abstractions for PCIe Advanced Error Reporting (AER) and general PCI bus error handling. It provides safe bindings for the struct pci_error_handlers callbacks, allowing Rust PCI drivers to implement device-specific recovery logic for events such as MMIO restoration, slot reset, and fatal/correctable error notifications.
The new ErrorHandler trait defines the equivalent of the C pci_error_handlers interface, including all callback methods, along with Rust enums ErsResult and ChannelState that mirror the kernel's pci_ers_result_t and pci_channel_state_t.
This enables fully type-safe integration between Rust PCI drivers and the Linux PCI error recovery infrastructure.
Existing Rust PCI sample drivers are updated to specify a default ErrorHandler = () until they implement custom error handling.
--- rust/kernel/pci.rs | 11 ++ rust/kernel/pci/err.rs | 273 ++++++++++++++++++++++++++ samples/rust/rust_dma.rs | 1 + samples/rust/rust_driver_auxiliary.rs | 2 + samples/rust/rust_driver_pci.rs | 2 + 5 files changed, 289 insertions(+) create mode 100644 rust/kernel/pci/err.rs
rust/kernel/pci.rs +11 -0
@@ -24,8 +24,10 @@ };use kernel::prelude::*; +mod err;mod id; +pub use self::err::{ChannelState ,ErrorHandler ,ErsResult };pub use self::id::{Class ,ClassMask ,Vendor };/// An adapter for the registration of PCI drivers.
@@ -47,6 +49,7 @@ unsafe fn register( (*pdrv.get ()).probe =Some (Self ::probe_callback); (*pdrv.get ()).remove =Some (Self ::remove_callback); (*pdrv.get ()).id_table =T ::ID_TABLE .as_ptr (); + (*pdrv.get ()).err_handler = err::ErrorHandlerVTable ::<T ::ErrorHandler >::vtable_ptr (); }// SAFETY: `pdrv` is guaranteed to be a valid `RegType`.
@@ -265,6 +268,14 @@ pub trait Driver: Send {// ``` type IdInfo : 'static; +/// The PCI error handler implementation for this driver. +// TODO: Use `associated_type_defaults` once stabilized: +// +// ``` +// type ErrorHandler: err::ErrorHandler = (); +// ``` +type ErrorHandler : err::ErrorHandler ; +/// The table of device ids supported by the driver. const ID_TABLE :IdTable <Self ::IdInfo >;
rust/kernel/pci/err.rs new file +273 -0
273 lines not on this page · raw message
samples/rust/rust_dma.rs +1 -0
@@ -53,6 +53,7 @@ unsafe impl kernel::transmute::FromBytes for MyStruct {}impl pci::Driver for DmaSampleDriver {type IdInfo = (); +type ErrorHandler = ();const ID_TABLE : pci::IdTable <Self ::IdInfo > = &PCI_TABLE ;fn probe (pdev: &pci::Device <Core >, _info: &Self ::IdInfo ) ->Result <Pin <KBox <Self >>> {
samples/rust/rust_driver_auxiliary.rs +2 -0
@@ -56,6 +56,8 @@ struct ParentDriver {impl pci::Driver for ParentDriver {type IdInfo = (); +type ErrorHandler = (); +const ID_TABLE : pci::IdTable <Self ::IdInfo > = &PCI_TABLE ;fn probe (pdev: &pci::Device <Core >, _info: &Self ::IdInfo ) ->Result <Pin <KBox <Self >>> {
samples/rust/rust_driver_pci.rs +2 -0
@@ -63,6 +63,8 @@ fn testdev(index: &TestIndex, bar: &Bar0) -> Result<u32> {impl pci::Driver for SampleDriver {type IdInfo =TestIndex ; +type ErrorHandler = (); +const ID_TABLE : pci::IdTable <Self ::IdInfo > = &PCI_TABLE ;fn probe (pdev: &pci::Device <Core >, info: &Self ::IdInfo ) ->Result <Pin <KBox <Self >>> {
Signature · 1 line
2.43.0
RFC PATCH 2/2 sample: rust: pci: implement dummy error handlers to demonstrate usage
In reply to Guangbo CuiThis patch adds a dummy implementation of PCIe error handlers to the Rust sample PCI driver. It demonstrates how to implement the pci::ErrorHandler trait.
--- samples/rust/rust_driver_pci.rs | 47 +++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-)
samples/rust/rust_driver_pci.rs +45 -2
@@ -4,7 +4,17 @@//! //! To make this driver probe, QEMU must be run with `-device pci-testdev`. -use kernel::{c_str, device::Core , devres::Devres , pci, prelude::*, sync::aref::ARef }; +use kernel::{ + c_str, + device::{ +Bound , +Core ,// + }, + devres::Devres , + pci, + prelude::*, + sync::aref::ARef , +};struct Regs ;
@@ -63,7 +73,7 @@ fn testdev(index: &TestIndex, bar: &Bar0) -> Result<u32> {impl pci::Driver for SampleDriver {type IdInfo =TestIndex ; -type ErrorHandler = (); +type ErrorHandler =Self ;const ID_TABLE : pci::IdTable <Self ::IdInfo > = &PCI_TABLE ;
@@ -106,6 +116,39 @@ fn unbind(pdev: &pci::Device<Core>, this: Pin<&Self>) { } } +#[vtable] +impl pci::ErrorHandler for SampleDriver { +type Driver =Self ; + +fn error_detected ( + pdev: &pci::Device <Bound >, + error: pci::ChannelState , + _this:Pin <&Self ::Driver >, + ) -> pci::ErsResult { +dev_info! (pdev.as_ref (),"error detected.\n" ); + +match error { + pci::ChannelState ::PermanentFailure => { +dev_err! (pdev.as_ref (),"Permanent failure detected.\n" ); + pci::ErsResult ::Disconnect + } + _ => { +dev_info! (pdev.as_ref (),"Attempting recovery from error.\n" ); + pci::ErsResult ::NeedReset + } + } + } + +fn slot_reset (pdev: &pci::Device <Bound >, _this:Pin <&Self ::Driver >) -> pci::ErsResult { +dev_info! (pdev.as_ref (),"slot reset.\n" ); + pci::ErsResult ::Recovered + } + +fn resume (pdev: &pci::Device <Bound >, _this:Pin <&Self ::Driver >) { +dev_info! (pdev.as_ref (),"resume.\n" ); + } +} +#[pinned_drop] impl PinnedDrop for SampleDriver {fn drop (self:Pin <&mut Self >) {
Signature · 1 line
2.43.0
Danilo Krummrich
maintainerQuoted 10 lines · Guangbo Cui
Hi all,
This RFC patchset introduces basic PCIe Advanced Error Reporting (AER) support for Rust PCI drivers and provides a simple example to demonstrate it's usage.
The first patch adds the necessary infrastructure in the Rust PCI layer to support device-specific PCI error handlers, mirroring the existing C
struct pci_error_handlerscallbacks.
Quoted 6 lines
The second patch updates the Rust PCI sample driver to implement a set of dummy error handlers. These callbacks simply print messages and return predefined results, serving as a guide for future Rust PCI driver authors who want to handle PCIe AER events safely.
This series is an RFC for discussion and not intended for merging yet.
Thanks for the series, I will have a detailed look in a few weeks.
I think this will be useful in the nova-core driver for instance. However, I wonder if you have another use-case you wrote this code for?
Thanks,
Danilo
Quoted 4 lines · Danilo Krummrich
Thanks for the series, I will have a detailed look in a few weeks.
I think this will be useful in the nova-core driver for instance. However, I wonder if you have another use-case you wrote this code for?
Hi Danilo,
Thanks for the feedback.
At the moment I don’t have a production driver making use of these callbacks, only a few toy-level drivers I wrote for experimentation.
The main motivation for this RFC was that the Rust PCI layer currently lacks parity with the C pci_error_handlers interface. Some parts of the PCI subsystem (AER, recovery paths) need these callbacks, so having the Rust side prepared seems useful for future drivers.
The sample driver is only meant to demonstrate the API surface and help discuss the interface design. If the approach looks reasonable, I’m happy to iterate on it or refine the API based on the feedback.
Best regards,
Guangbo