[PATCH v4 4/9] rust: pci: add num_vf(), to return number of VFs

From: Zhi Wang

Date: Sun Oct 04 2026 - 08:10:36 EST


From: Peter Colberg <peter@xxxxxxxxxxx>

Add a method to return the number of Virtual Functions (VF) enabled for
a Physical Function (PF).

Expose the query in the PCI core callback context and return u16,
matching the type of the VF count stored by the PCI core.

Signed-off-by: Peter Colberg <peter@xxxxxxxxxxx>
Signed-off-by: Zhi Wang <zhiw@xxxxxxxxxx>
---
rust/kernel/pci/iov.rs | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/rust/kernel/pci/iov.rs b/rust/kernel/pci/iov.rs
index 616ac40b8bcd..4ed6ba658e81 100644
--- a/rust/kernel/pci/iov.rs
+++ b/rust/kernel/pci/iov.rs
@@ -20,6 +20,16 @@ pub fn is_virtfn(&self) -> bool {
}
}

+impl Device<device::Core<'_>> {
+ /// Returns the number of Virtual Functions (VF) enabled for a Physical Function (PF).
+ pub fn num_vf(&self) -> u16 {
+ // SAFETY: `self.as_raw()` is valid and this call runs in the PCI core callback context.
+ unsafe { bindings::pci_num_vf(self.as_raw()) }
+ .try_into()
+ .expect("pdev->sriov->num_VFs is u16")
+ }
+}
+
impl Device<device::CoreInternal<'_>> {
/// Enable the Single Root I/O Virtualization (SR-IOV) capability for this device,
/// where `nr_virtfn` is number of Virtual Functions (VF) to enable.
--
2.53.0