[PATCH v4 2/6] rust: pci: add sriov_get_totalvfs() helper
From: Zhi Wang
Date: Thu Jul 09 2026 - 11:14:09 EST
Expose pci_sriov_get_totalvfs() to Rust PCI drivers so they can query
how many SR-IOV VFs a device supports.
Use a conditional C helper because the !CONFIG_PCI_IOV version of
pci_sriov_get_totalvfs() is a static inline function and is therefore
not emitted into the Rust bindings. Return Option<NonZero<u16>> so Rust
callers must handle the zero value that represents unavailable SR-IOV.
Cc: Alexandre Courbot <acourbot@xxxxxxxxxx>
Cc: Bjorn Helgaas <bhelgaas@xxxxxxxxxx>
Cc: David Laight <david.laight.linux@xxxxxxxxx>
Cc: Gary Guo <gary@xxxxxxxxxxx>
Cc: linux-pci@xxxxxxxxxxxxxxx
Link: https://lore.kernel.org/all/DJHPRE4TGGT8.BUTMYOF5YE05@xxxxxxxxxx/
Signed-off-by: Zhi Wang <zhiw@xxxxxxxxxx>
---
rust/helpers/pci.c | 8 ++++++++
rust/kernel/pci.rs | 13 +++++++++++++
2 files changed, 21 insertions(+)
diff --git a/rust/helpers/pci.c b/rust/helpers/pci.c
index e44905317d75..4ebf256dff23 100644
--- a/rust/helpers/pci.c
+++ b/rust/helpers/pci.c
@@ -24,6 +24,14 @@ __rust_helper bool rust_helper_dev_is_pci(const struct device *dev)
return dev_is_pci(dev);
}
+#ifndef CONFIG_PCI_IOV
+__rust_helper unsigned int
+rust_helper_pci_sriov_get_totalvfs(struct pci_dev *pdev)
+{
+ return pci_sriov_get_totalvfs(pdev);
+}
+#endif
+
#ifndef CONFIG_PCI_MSI
__rust_helper int rust_helper_pci_alloc_irq_vectors(struct pci_dev *dev,
unsigned int min_vecs,
diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index 5071cae6543f..280e84201d9a 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -25,6 +25,7 @@
use core::{
marker::PhantomData,
mem::offset_of,
+ num::NonZero,
ptr::{
addr_of_mut,
NonNull, //
@@ -450,6 +451,18 @@ pub fn pci_class(&self) -> Class {
// SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`.
Class::from_raw(unsafe { (*self.as_raw()).class })
}
+
+ /// Returns the total number of VFs, or [`None`] if SR-IOV is not available.
+ #[inline]
+ pub fn sriov_get_totalvfs(&self) -> Option<NonZero<u16>> {
+ // SAFETY: `self.as_raw()` is a valid pointer to a `struct pci_dev`.
+ let total_vfs = unsafe { bindings::pci_sriov_get_totalvfs(self.as_raw()) };
+
+ // CAST: The C function returns `unsigned int`, but the value originates
+ // from TotalVFs/driver_max_VFs (which are defined as `u16`), so this cast
+ // cannot truncate.
+ NonZero::new(total_vfs as u16)
+ }
}
impl<'a> Device<device::Core<'a>> {
--
2.51.0