[PATCH v6 1/5] rust: pci: provide access to PCI Class and Class-related items

Alexandre Courbot acourbot at nvidia.com
Mon Aug 25 12:17:28 UTC 2025


Hi John,

On Fri Aug 22, 2025 at 11:03 AM JST, John Hubbard wrote:
> Allow callers to write Class::STORAGE_SCSI instead of
> bindings::PCI_CLASS_STORAGE_SCSI, for example.
>
> New APIs:
>     Class::STORAGE_SCSI, Class::NETWORK_ETHERNET, etc.
>     Class::as_raw()
>     Class: From<u32> for Class
>     ClassMask: Full, ClassSubclass
>     Device::pci_class()
>
> Cc: Danilo Krummrich <dakr at kernel.org>
> Cc: Alexandre Courbot <acourbot at nvidia.com>
> Cc: Elle Rhumsaa <elle at weathered-steel.dev>
> Signed-off-by: John Hubbard <jhubbard at nvidia.com>

Just one question about the 16 and 24 bit representations below.

<snip>
> +macro_rules! define_all_pci_classes {
> +    (
> +        $($variant:ident = $binding:expr,)+
> +    ) => {
> +
> +        impl Class {
> +            $(
> +                #[allow(missing_docs)]
> +                pub const $variant: Self = Self(Self::to_24bit_class($binding));
> +            )+
> +        }
> +
> +        /// Convert a raw 24-bit class code value to a `Class`.
> +        impl From<u32> for Class {
> +            fn from(value: u32) -> Self {
> +                match value {
> +                    $(x if x == Self::$variant.0 => Self::$variant,)+
> +                    _ => Self::UNKNOWN,
> +                }
> +            }

Should we normalize `value` to 24 bits (i.e. call `to_24bit_class`)
before doing the match? The constants we compare against are all
normalized, but if we pass a 16-bit class to this method the result will
be `UNKNOWN`, unless I missed something.

Being able to store a class as either a 16-bit or 24-bit representation
in the same type also opens the door to bugs, which we can avoid if we
always normalize to 24-bit and make the class/subclass representation
accessible through a convenience method only.

> +        }
> +    };
> +}
> +
> +/// Once constructed, a `Class` contains a valid PCI Class code.
> +impl Class {
> +    /// Create a new Class from a raw 24-bit class code.
> +    pub fn new(class_code: u32) -> Self {
> +        Self::from(class_code)
> +    }

Do we need a `new` method when the `From` implementation does exactly
the same thing and has the same signature?



More information about the Nouveau mailing list