On Tue, Dec 01, 2026 at 04:16:20PM +0900, Alexandre Courbot wrote:
> The return type of `dev_id` has a well-defined bit layout encoding
> the bus, device and function IDs of the PCI device. Add a `Bdf` bitfield
> defining that layout and make `dev_id` return it so users can benefit
> from it and are not tempted to define their own BDF type.
> 
> Also update the current two users of `dev_id`.
> 
> Signed-off-by: Alexandre Courbot <[email protected]>
> Acked-by: Danilo Krummrich <[email protected]>
> Reviewed-by: Gary Guo <[email protected]>
> Reviewed-by: Danilo Krummrich <[email protected]>
> ---
>  drivers/gpu/nova-core/gsp/fw/commands.rs |  2 +-
>  rust/kernel/pci.rs                       | 34 
> ++++++++++++++++++++++++++++++--
>  samples/rust/rust_driver_sriov/pf.rs     |  2 +-
>  3 files changed, 34 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/gpu/nova-core/gsp/fw/commands.rs 
> b/drivers/gpu/nova-core/gsp/fw/commands.rs
> index d6926709a368..dd658c5fa429 100644
> --- a/drivers/gpu/nova-core/gsp/fw/commands.rs
> +++ b/drivers/gpu/nova-core/gsp/fw/commands.rs
> @@ -89,7 +89,7 @@ pub(crate) fn init<'a>(
>              gpuPhysAddr: dev.resource_start(0)?,
>              gpuPhysFbAddr: dev.resource_start(1)?,
>              gpuPhysInstAddr: dev.resource_start(3)?,
> -            nvDomainBusDeviceFunc: u64::from(dev.dev_id()),
> +            nvDomainBusDeviceFunc: u64::from(dev.dev_id().into_raw()),
>  
>              // Using TASK_SIZE in r535_gsp_rpc_set_system_info() seems wrong 
> because
>              // TASK_SIZE is per-task. That's probably a design issue in 
> GSP-RM though.
> diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
> index 04a6d240afb2..078da469d98a 100644
> --- a/rust/kernel/pci.rs
> +++ b/rust/kernel/pci.rs
> @@ -6,6 +6,7 @@
>  
>  use crate::{
>      bindings,
> +    bitfield,
>      container_of,
>      device,
>      device_id::{
> @@ -17,6 +18,7 @@
>          from_result,
>          to_result, //
>      },
> +    fmt,
>      io::resource,
>      prelude::*,
>      str::CStr,
> @@ -279,6 +281,34 @@ macro_rules! module_pci_driver {
>  };
>  }
>  
> +bitfield! {
> +    /// Bus-Device-Function ID of a PCI device.
> +    ///
> +    /// Uses the same layout as the C `PCI_DEVID` macro.
> +    #[doc(alias = "PCI_DEVID")]
> +    pub struct Bdf(u16) {
> +        /// Bus number.
> +        15:8 bus;
> +        /// Device number.
> +        7:3 device;
> +        /// Function number.
> +        2:0 function;

If it comes to the next iteration, can you just drop those comments?
They explain nothing, only bloat the sources.

Other than that,

Acked-by: Yury Norov <[email protected]>

> +    }
> +}
> +
> +/// Formats the ID as `bus:device.function`, like the corresponding part of 
> C's `pci_name()`.
> +impl fmt::Display for Bdf {
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        write!(
> +            f,
> +            "{:02x}:{:02x}.{}",
> +            self.bus(),
> +            self.device(),
> +            self.function()
> +        )
> +    }
> +}
> +
>  /// Abstraction for the PCI device ID structure ([`struct pci_device_id`]).
>  ///
>  /// [`struct pci_device_id`]: 
> https://docs.kernel.org/PCI/pci.html#c.pci_device_id
> @@ -700,10 +730,10 @@ pub fn revision_id(&self) -> u8 {
>  
>      /// Returns the PCI bus device/function.
>      #[inline]
> -    pub fn dev_id(&self) -> u16 {
> +    pub fn dev_id(&self) -> Bdf {
>          // SAFETY: By its type invariant `self.as_raw` is always a valid 
> pointer to a
>          // `struct pci_dev`.
> -        unsafe { bindings::pci_dev_id(self.as_raw()) }
> +        Bdf::from_raw(unsafe { bindings::pci_dev_id(self.as_raw()) })
>      }
>  
>      /// Returns the PCI subsystem vendor ID.
> diff --git a/samples/rust/rust_driver_sriov/pf.rs 
> b/samples/rust/rust_driver_sriov/pf.rs
> index 8f9958683528..c367c891abf4 100644
> --- a/samples/rust/rust_driver_sriov/pf.rs
> +++ b/samples/rust/rust_driver_sriov/pf.rs
> @@ -34,7 +34,7 @@ pub(crate) fn submit(vf: &pci::Device<Bound>) -> 
> Result<u64> {
>  
>          dev_info!(
>              pf.pdev,
> -            "Handle PF request {} from VF devfn {:#x}.\n",
> +            "Handle PF request {} from VF {}.\n",
>              request,
>              vf.dev_id()
>          );
> 
> -- 
> 2.56.1

Reply via email to