Re: [PATCH v4 2/6] rust: pci: add sriov_get_totalvfs() helper
From: Danilo Krummrich
Date: Thu Jul 09 2026 - 19:00:24 EST
On Thu Jul 9, 2026 at 5:02 PM CEST, Zhi Wang wrote:
> + /// 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>> {
It is not a problem yet, but sriov_get_totalvfs() should go into the
Device<Core> impl above.
Once we also have sriov_set_totalvfs() this would otherwise be a potential data
race.