[PATCH v6 04/11] rust: pci: add num_vf(), to return number of VFs

From: Simon Song

Date: Wed Nov 11 2026 - 17:36:12 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>
Reviewed-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
Signed-off-by: Simon Song <xinmengs@xxxxxxxxxx>
---
rust/helpers/pci.c | 5 +++++
rust/kernel/pci/iov.rs | 13 +++++++++++++
2 files changed, 18 insertions(+)

diff --git a/rust/helpers/pci.c b/rust/helpers/pci.c
index a87b5a77efef..57c28ddeeb9b 100644
--- a/rust/helpers/pci.c
+++ b/rust/helpers/pci.c
@@ -52,6 +52,11 @@ __rust_helper void rust_helper_pci_disable_sriov(struct pci_dev *pdev)
pci_disable_sriov(pdev);
}

+__rust_helper int rust_helper_pci_num_vf(struct pci_dev *pdev)
+{
+ return pci_num_vf(pdev);
+}
+
__rust_helper unsigned int
rust_helper_pci_sriov_get_totalvfs(struct pci_dev *pdev)
{
diff --git a/rust/kernel/pci/iov.rs b/rust/kernel/pci/iov.rs
index 18e42b2d3965..78e279169be1 100644
--- a/rust/kernel/pci/iov.rs
+++ b/rust/kernel/pci/iov.rs
@@ -11,6 +11,19 @@
prelude::*, //
};

+impl Device<device::Core<'_>> {
+ /// Returns the number of Virtual Functions (VF) enabled for a Physical Function (PF).
+ ///
+ /// Returns zero when `CONFIG_PCI_IOV` is disabled.
+ #[inline]
+ 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.43.7