Re: [PATCH v3 2/8] rust: pci: add sriov_get_totalvfs() helper
From: Zhi Wang
Date: Thu Jul 09 2026 - 10:33:25 EST
On Wed, 08 Jul 2026 23:41:20 +0900
"Alexandre Courbot" <acourbot@xxxxxxxxxx> wrote:
> On Wed Jul 8, 2026 at 11:03 PM JST, Alexandre Courbot wrote:
> > On Wed Jul 1, 2026 at 3:26 PM JST, Zhi Wang wrote:
> >> Expose pci_sriov_get_totalvfs() to Rust PCI drivers so they can
> >> query how many SR-IOV VFs a device supports.
> >>
> >> Cc: Alexandre Courbot <acourbot@xxxxxxxxxx>
> >> Cc: Bjorn Helgaas <bhelgaas@xxxxxxxxxx>
> >> Cc: David Laight <david.laight.linux@xxxxxxxxx>
> >> Cc: Gary Guo <gary@xxxxxxxxxxx>
> >> Cc: linux-pci@xxxxxxxxxxxxxxx
> >> Link:
> >> https://lore.kernel.org/all/DJHPRE4TGGT8.BUTMYOF5YE05@xxxxxxxxxx/
> >> Signed-off-by: Zhi Wang <zhiw@xxxxxxxxxx> ---
> >> rust/kernel/pci.rs | 11 +++++++++++
> >> 1 file changed, 11 insertions(+)
> >>
> >> diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
> >> index 5071cae6543f..21c51981c02e 100644
> >> --- a/rust/kernel/pci.rs
> >> +++ b/rust/kernel/pci.rs
> >> @@ -450,6 +450,17 @@ pub fn pci_class(&self) -> Class {
> >> // SAFETY: `self.as_raw` is a valid pointer to a `struct
> >> pci_dev`. Class::from_raw(unsafe { (*self.as_raw()).class })
> >> }
> >> +
> >> + /// Returns the total number of VFs, or 0 if SR-IOV is not
> >> available.
> >> + #[inline]
> >> + pub fn sriov_get_totalvfs(&self) -> 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 helper returns `unsigned int`, but the
> >> value originates
> >> + // from TotalVFs/driver_max_VFs, so this cast cannot
> >> truncate.
> >
> > nit: "from TotalVFs/driver_max_VFs (which are defined as `u16`),
> > ..."
> >
> > With that,
> >
> > Reviewed-by: Alexandre Courbot <acourbot@xxxxxxxxxx>
>
> Sorry, need to take my tag back. :( Build is failing if
> `CONFIG_PCI_IOV` is not set:
>
> error[E0425]: cannot find function `pci_sriov_get_totalvfs` in
> crate `bindings` --> ../rust/kernel/pci.rs:458:44
> |
> 458 | let total_vfs = unsafe {
> bindings::pci_sriov_get_totalvfs(self.as_raw()) }; |
> ^^^^^^^^^^^^^^^^^^^^^^ not found in
> `bindings`
>
> Looks like you need to add a helper in `rust/helpers/pci.c` as the
> inline C function used when `CONFIG_PCI_IOV` is not set can not be
> invoked by Rust.
Add it in v4.