From: John Hubbard <jhubbard@nvidia.com>
To: Alexandre Courbot <acourbot@nvidia.com>,
Danilo Krummrich <dakr@kernel.org>
Cc: "Joel Fernandes" <joelagnelf@nvidia.com>,
"Timur Tabi" <ttabi@nvidia.com>,
"Alistair Popple" <apopple@nvidia.com>,
"David Airlie" <airlied@gmail.com>,
"Simona Vetter" <simona@ffwll.ch>,
"Bjorn Helgaas" <bhelgaas@google.com>,
"Krzysztof Wilczyński" <kwilczynski@kernel.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Alex Gaynor" <alex.gaynor@gmail.com>,
"Boqun Feng" <boqun.feng@gmail.com>,
"Gary Guo" <gary@garyguo.net>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Benno Lossin" <lossin@kernel.org>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
nouveau@lists.freedesktop.org, linux-pci@vger.kernel.org,
rust-for-linux@vger.kernel.org,
LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v2 1/3] rust: pci: provide access to PCI Class, subclass, implementation values
Date: Mon, 18 Aug 2025 11:49:43 -0700 [thread overview]
Message-ID: <a82172da-a029-4208-b92d-e68320ed31f4@nvidia.com> (raw)
In-Reply-To: <DC5JTQ7VHA65.2SNDMXYIKB9GO@nvidia.com>
On 8/18/25 5:23 AM, Alexandre Courbot wrote:
> On Mon Aug 18, 2025 at 10:33 AM JST, John Hubbard wrote:
...
>> diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
>> index 887ee611b553..9caa1d342d52 100644
>> --- a/rust/kernel/pci.rs
>> +++ b/rust/kernel/pci.rs
>> @@ -23,6 +23,179 @@
>> };
>> use kernel::prelude::*;
>>
>> +macro_rules! define_all_pci_classes {
>> + (
>> + $($variant:ident = $binding:expr,)+
>> + ) => {
>> + /// Converts a PCI class constant to 24-bit format.
>> + ///
>> + /// Many device drivers use only the upper 16 bits (base class and subclass), but some
>> + /// use the full 24 bits. In order to support both cases, store the class code as a 24-bit
>> + /// value, where 16-bit values are shifted up 8 bits.
>> + const fn to_24bit_class(val: u32) -> u32 {
>> + if val > 0xFFFF { val } else { val << 8 }
>> + }
>
> This convenience function doesn't look like it needs to be defined
> inside the macro.
>
>> +
>> + /// PCI device class codes.
>> + ///
>> + /// Each entry contains the full 24-bit PCI class code (base class in bits 23-16, subclass
>> + /// in bits 15-8, programming interface in bits 7-0).
>> + ///
>> + #[derive(Debug, Clone, Copy, PartialEq, Eq)]
>> + #[repr(transparent)]
>> + pub struct Class(u32);
>
> Same for this struct.
>
>> +
>> + impl Class {
>> + $(
>> + #[allow(missing_docs)]
>> + pub const $variant: Self = Self(to_24bit_class($binding));
>> + )+
>
> This obviously needs to be part of the macro...
>
>> +
>> + /// Match the full class code.
>> + pub const MASK_FULL: u32 = 0xffffff;
>> +
>> + /// Match the upper 16 bits of the class code (base class and subclass only).
>> + pub const MASK_CLASS_SUBCLASS: u32 = 0xffff00;
>
> But these two definitions (and `as_u32` below) can again be part of
> their own impl block outside of it.
>
>> +
>> + /// Create a `Class` from the raw class code value, or `None` if the value doesn't
>> + /// match any known class.
>> + pub fn from_u32(value: u32) -> Option<Self> {
>> + match value {
>> + $(x if x == Self::$variant.0 => Some(Self::$variant),)+
>> + _ => None,
>> + }
>> + }
>> +
>> + /// Get the raw 24-bit class code value.
>> + pub const fn as_u32(self) -> u32 {
>> + self.0
>> + }
>> + }
>
> So I believe that if you move the declarations that do not depend on the
> macro parameters outside of if, the macro's body could be limited to the
> following:
>
> impl Class {
> $(
> #[allow(missing_docs)]
> pub const $variant: Self = Self(to_24bit_class($binding));
> )+
>
> /// Create a `Class` from the raw class code value, or `None` if the value doesn't
> /// match any known class.
> pub fn from_u32(value: u32) -> Option<Self> {
> match value {
> $(x if x == Self::$variant.0 => Some(Self::$variant),)+
> _ => None,
> }
> }
>
> Shorter macros are generally considered better. :)
All of the above comments make sense, I'll make those changes. Thanks for the
review!
thanks,
--
John Hubbard
next prev parent reply other threads:[~2025-08-18 18:49 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-18 1:33 [PATCH v2 0/3] rust, nova-core: PCI Class, Vendor support John Hubbard
2025-08-18 1:33 ` [PATCH v2 1/3] rust: pci: provide access to PCI Class, subclass, implementation values John Hubbard
2025-08-18 12:23 ` Alexandre Courbot
2025-08-18 18:49 ` John Hubbard [this message]
2025-08-18 14:57 ` Danilo Krummrich
2025-08-18 18:58 ` John Hubbard
2025-08-20 3:48 ` Elle Rhumsaa
2025-08-20 17:22 ` John Hubbard
2025-08-22 1:03 ` Miguel Ojeda
2025-08-18 1:33 ` [PATCH v2 2/3] gpu: nova-core: avoid probing non-display/compute PCI functions John Hubbard
2025-08-20 3:50 ` Elle Rhumsaa
2025-08-18 1:33 ` [PATCH v2 3/3] rust: pci: provide access to PCI Vendor values John Hubbard
2025-08-18 16:06 ` Danilo Krummrich
2025-08-18 18:59 ` John Hubbard
2025-08-20 3:54 ` Elle Rhumsaa
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=a82172da-a029-4208-b92d-e68320ed31f4@nvidia.com \
--to=jhubbard@nvidia.com \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=airlied@gmail.com \
--cc=alex.gaynor@gmail.com \
--cc=aliceryhl@google.com \
--cc=apopple@nvidia.com \
--cc=bhelgaas@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun.feng@gmail.com \
--cc=dakr@kernel.org \
--cc=gary@garyguo.net \
--cc=joelagnelf@nvidia.com \
--cc=kwilczynski@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=nouveau@lists.freedesktop.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=simona@ffwll.ch \
--cc=tmgross@umich.edu \
--cc=ttabi@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®