Skip to contentSkip to conversation

Rust PCI capability infrastructure and SR-IOV support

PATCH Applied Zhi Wang 3 participants 5 messages Sep 6 – Oct 12

Subsystems (3)
lore
Share & tools
lore

Zhi Wang

author
#0 lore raw

v10 cover Rust PCI capability infrastructure and SR-IOV support

This is a follow-up to v9 [1].

The patch adds typed PCIe extended-capability lookup to the Rust PCI abstraction and an SR-IOV layout used by the nova-core vGPU boot work.

This series is based on v7.3-rc1. That base provides the existing num::casts helpers but not const_as!, so the const narrowing call uses the temporary turbofish form discussed during review. It can be converted as part of the subsequent const_as! migration before the old helper is removed.

Changes since v9:
- Renamed the internal BAR encoding types to drop the VF-specific prefix and added a TODO to move the common definitions into the PCI core abstraction. (Ilpo, Alexandre)
- Rebased onto v7.3-rc1.

[1] https://lore.kernel.org/rust-for-linux/[email protected]/

Zhi Wang (1):
rust: pci: add extended capability and SR-IOV support

 rust/helpers/pci.c     |   5 +
 rust/kernel/pci.rs     |   8 ++
 rust/kernel/pci/cap.rs | 292 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 305 insertions(+)
 create mode 100644 rust/kernel/pci/cap.rs

base-commit: cee9395acd8043be0644b25c34bfa86623f2b935

Signature · 1 line
2.53.0
author
#1 lore raw

PATCH v10 1/1 rust: pci: add extended capability and SR-IOV support

Rust PCI drivers have no typed interface for locating and accessing PCIe extended capabilities.

The SR-IOV extended capability describes VF topology and VF BARs. Expose this information through the Rust PCI abstraction so drivers can use the existing typed configuration-space accessors instead of raw bindings.

Define ExtCapability to associate a capability ID with a register layout, and add ConfigSpace::find_ext_capability() to locate and project that layout. Bound the view at the next capability or the end of extended configuration space. Add ExtSriovRegs and a decoded VF BAR iterator that reads and validates all six VF BAR register slots up front, yields decoded BAR addresses and widths in logical order, and keeps the raw configuration-space slot advancement internal. Since PCI_EXT_CAP_NEXT() is a function-like macro, expose it through a Rust helper.

Keep raw SR-IOV registers private because PCI core owns their state. Expose the current First VF Offset through a read-only accessor because the Nova GSP ABI needs the raw 16-bit First VF Offset in Routing ID space. The pci_iov_virtfn_devfn() helper instead returns the low eight bits of a calculated VF Routing ID.

---
 rust/helpers/pci.c     |   5 +
 rust/kernel/pci.rs     |   8 ++
 rust/kernel/pci/cap.rs | 292 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 305 insertions(+)
 create mode 100644 rust/kernel/pci/cap.rs
rust/helpers/pci.c +5 -0
index a714cc2bfb7a..502f56e02fcc 100644
@@ -29,6 +29,11 @@ __rust_helper unsigned int rust_helper_pci_irq_type(struct pci_dev *pdev)
 	return pci_irq_type(pdev);
 }
 
+__rust_helper u32 rust_helper_pci_ext_cap_next(u32 header)
+{
+	return PCI_EXT_CAP_NEXT(header);
+}
+
 #ifndef CONFIG_PCI_IOV
 __rust_helper unsigned int
 rust_helper_pci_sriov_get_totalvfs(struct pci_dev *pdev)
rust/kernel/pci.rs +8 -0
index 3ec897709e89..77c52fe59551 100644
@@ -32,10 +32,18 @@
     },
 };
 
+mod cap;
 mod id;
 mod io;
 mod irq;
 
+pub use self::cap::{
+    ExtCapId,
+    ExtCapability,
+    ExtSriovCapability,
+    ExtSriovRegs,
+    ExtSriovVfBar, //
+};
 pub use self::id::{
     Class,
     ClassMask,
rust/kernel/pci/cap.rs new file +292 -0

292 lines not on this page · raw message

Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH v10 1/1] rust: pci: add extended capability and SR-IOV support

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 22 lines

Rust PCI drivers have no typed interface for locating and accessing PCIe extended capabilities.

The SR-IOV extended capability describes VF topology and VF BARs. Expose this information through the Rust PCI abstraction so drivers can use the existing typed configuration-space accessors instead of raw bindings.

Define ExtCapability to associate a capability ID with a register layout, and add ConfigSpace::find_ext_capability() to locate and project that layout. Bound the view at the next capability or the end of extended configuration space. Add ExtSriovRegs and a decoded VF BAR iterator that reads and validates all six VF BAR register slots up front, yields decoded BAR addresses and widths in logical order, and keeps the raw configuration-space slot advancement internal. Since PCI_EXT_CAP_NEXT() is a function-like macro, expose it through a Rust helper.

Keep raw SR-IOV registers private because PCI core owns their state. Expose the current First VF Offset through a read-only accessor because the Nova GSP ABI needs the raw 16-bit First VF Offset in Routing ID space. The pci_iov_virtfn_devfn() helper instead returns the low eight bits of a calculated VF Routing ID.

Quoted 6 lines

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#3 lore raw

Re: [PATCH v10 1/1] rust: pci: add extended capability and SR-IOV support

In reply to Zhi Wang
Quoted 29 lines · Zhi Wang
rust/kernel/pci/cap.rs new file +29 -0
+    pub fn vf_bars(&self) -> Result<impl Iterator<Item = ExtSriovVfBar>> {
+        let slots: [u32; NUM_VF_BARS] =
+            core::array::from_fn(|slot| crate::io_read!(*self, .vf_bar[panic: slot]));
+        let mut slots = slots.into_iter();
+        let mut bars = [None; NUM_VF_BARS];
+        let mut count = 0;
+
+        let mut decode = || {
+            while let Some(low) = slots.next().map(BarLow::from) {
+                // SR-IOV VF BARs describe memory-space apertures; an I/O-space encoding is not
+                // valid for these registers.
+                if low.io_space() {
+                    return Err(EINVAL);
+                }
+
+                let low_address = u64::from(low.address()) << BarLow::ADDRESS_SHIFT;
+                let bar = match low.memory_type()? {
+                    BarMemoryType::Bits64 => ExtSriovVfBar {
+                        address: (u64::from(slots.next().ok_or(EINVAL)?) << 32) | low_address,
+                        is_64bit: true,
+                    },
+                    BarMemoryType::Bits32 => ExtSriovVfBar {
+                        address: low_address,
+                        is_64bit: false,
+                    },
+                };
+
+                bars[count] = Some(bar);
+                count += 1;

rust/kernel/pci/cap.rs line 291

Quoted 10 lines
rust/kernel/pci/cap.rs new file +10 -0
+            }
+
+            Ok(())
+        };
+
+        decode().inspect_err(|_| {
+            dev_err!(self.pdev, "invalid VF BAR encoding in SR-IOV capability\n");
+        })?;
+        Ok(bars.into_iter().flatten())
+    }

I don't mind too much, but can you please remind me why we decode the VF BAR registers and can't use the PCI core's VF resource information instead?

(I gave it a very quick try and using pci_bus_address() and pci_resource_flags() actually turned out worse, but that could be me spending less than 5 minutes on it. :)

maintainer Applied
#4 lore raw
In reply to Zhi Wang
Quoted 1 line · Zhi Wang

[PATCH v10 0/1] Rust PCI capability infrastructure and SR-IOV support

Applied, thanks!

Branch: driver-core-testing
Tree: git://git.kernel.org/pub/scm/linux/kernel/git/driver-core/driver-core.git

[1/1] rust: pci: add extended capability and SR-IOV support

      commit: d4ffc4896b3e

The patch will appear in the next linux-next integration (typically within 24 hours on weekdays).

The patch is in the driver-core-testing branch and will be promoted to driver-core-next after validation.