[PATCH v3 2/9] rust: pci: expose the VF index within its PF
From: Simon Song
Date: Thu Nov 19 2026 - 16:52:24 EST
From: Danilo Krummrich <dakr@xxxxxxxxxx>
Nova identifies a vGPU instance by the VF's index within its PF. Expose
pci_iov_vf_id() through Device::vf_id() so a Rust VF driver can obtain
that index, with an error for devices that are not VFs.
Because enabling or disabling SR-IOV can update the PF's VF offset and
stride, from which the index is computed, restrict vf_id() to bound
devices, Device<device::Bound>.
Suggested-by: Danilo Krummrich <dakr@xxxxxxxxxx>
Suggested-by: Zhi Wang <zhiw@xxxxxxxxxx>
Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
Signed-off-by: Zhi Wang <zhiw@xxxxxxxxxx>
Signed-off-by: Simon Song <xinmengs@xxxxxxxxxx>
---
rust/helpers/pci.c | 5 +++++
rust/kernel/pci/iov.rs | 11 +++++++++++
2 files changed, 16 insertions(+)
diff --git a/rust/helpers/pci.c b/rust/helpers/pci.c
index 57c28ddeeb9b..1e677ebfe0d4 100644
--- a/rust/helpers/pci.c
+++ b/rust/helpers/pci.c
@@ -41,6 +41,11 @@ __rust_helper u32 rust_helper_pci_ext_cap_next(u32 header)
}
#ifndef CONFIG_PCI_IOV
+__rust_helper int rust_helper_pci_iov_vf_id(struct pci_dev *pdev)
+{
+ return pci_iov_vf_id(pdev);
+}
+
__rust_helper int rust_helper_pci_enable_sriov(struct pci_dev *pdev,
int nr_virtfn)
{
diff --git a/rust/kernel/pci/iov.rs b/rust/kernel/pci/iov.rs
index d0db9d4a0f9b..5bb1340c9150 100644
--- a/rust/kernel/pci/iov.rs
+++ b/rust/kernel/pci/iov.rs
@@ -379,6 +379,17 @@ unsafe impl<F: ForLt> Send for VfRegistration<'_, F> where for<'a> F::Of<'a>: Se
unsafe impl<F: ForLt> Sync for VfRegistration<'_, F> where for<'a> F::Of<'a>: Send + Sync {}
impl Device<device::Bound> {
+ /// Return the zero-based VF index within its PF, or an error for a non-VF device.
+ ///
+ /// Returns [`ENOSYS`] when `CONFIG_PCI_IOV` is disabled.
+ #[inline]
+ pub fn vf_id(&self) -> Result<u16> {
+ // SAFETY: `self.as_raw()` points to a live PCI device; the helper checks VF membership.
+ let id = unsafe { bindings::pci_iov_vf_id(self.as_raw()) };
+ to_result(id)?;
+ Ok(id.try_into()?)
+ }
+
/// Internal helper: reads the `vf_registration_data_rust` pointer from the
/// PF, checks the `TypeId`, and returns a pinned reference.
///
--
2.43.7