From: Danilo Krummrich 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. Suggested-by: Danilo Krummrich Suggested-by: Zhi Wang Signed-off-by: Danilo Krummrich Signed-off-by: Zhi Wang Signed-off-by: Simon Song --- 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 Send for VfRegistration<'_, F> where for<'a> F::Of<'a>: Se unsafe impl Sync for VfRegistration<'_, F> where for<'a> F::Of<'a>: Send + Sync {} impl Device { + /// 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 { + // 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