[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <DCBIPY9UJTT4.ETBXLTRGJWHO@kernel.org>
Date: Mon, 25 Aug 2025 14:47:04 +0200
From: "Danilo Krummrich" <dakr@...nel.org>
To: "Alexandre Courbot" <acourbot@...dia.com>
Cc: "John Hubbard" <jhubbard@...dia.com>, "Joel Fernandes"
<joelagnelf@...dia.com>, "Timur Tabi" <ttabi@...dia.com>, "Alistair Popple"
<apopple@...dia.com>, "David Airlie" <airlied@...il.com>, "Simona Vetter"
<simona@...ll.ch>, "Bjorn Helgaas" <bhelgaas@...gle.com>,
Krzysztof Wilczyński <kwilczynski@...nel.org>, "Miguel
Ojeda" <ojeda@...nel.org>, "Alex Gaynor" <alex.gaynor@...il.com>, "Boqun
Feng" <boqun.feng@...il.com>, "Gary Guo" <gary@...yguo.net>,
Björn Roy Baron <bjorn3_gh@...tonmail.com>, "Benno Lossin"
<lossin@...nel.org>, "Andreas Hindborg" <a.hindborg@...nel.org>, "Alice
Ryhl" <aliceryhl@...gle.com>, "Trevor Gross" <tmgross@...ch.edu>,
<nouveau@...ts.freedesktop.org>, <linux-pci@...r.kernel.org>,
<rust-for-linux@...r.kernel.org>, "LKML" <linux-kernel@...r.kernel.org>,
"Elle Rhumsaa" <elle@...thered-steel.dev>
Subject: Re: [PATCH v6 2/5] rust: pci: provide access to PCI Vendor values
On Mon Aug 25, 2025 at 2:33 PM CEST, Alexandre Courbot wrote:
>> +#[derive(Debug, Clone, Copy, PartialEq, Eq)]
>> +#[repr(transparent)]
>> +pub struct Vendor(u32);
>> +
>> +macro_rules! define_all_pci_vendors {
>> + (
>> + $($variant:ident = $binding:expr,)+
>> + ) => {
>> +
>> + impl Vendor {
>> + $(
>> + #[allow(missing_docs)]
>> + pub const $variant: Self = Self($binding as u32);
>> + )+
>> + }
>> +
>> + /// Convert a raw 16-bit vendor ID to a `Vendor`.
>> + impl From<u32> for Vendor {
>> + fn from(value: u32) -> Self {
>> + match value {
>> + $(x if x == Self::$variant.0 => Self::$variant,)+
>> + _ => Self::UNKNOWN,
>> + }
>> + }
>
> Naive question from someone with a device tree background and almost no
> PCI experience: one consequence of using `From` here is that if I create
> an non-registered Vendor value (e.g. `let vendor =
> Vendor::from(0xf0f0)`), then do `vendor.as_raw()`, I won't get the value
> passed initially but the one for `UNKNOWN`, e.g. `0xffff`. Are we ok
> with this?
I think that's fine, since we shouldn't actually hit this. Drivers should only
ever use the pre-defined constants of Vendor; consequently the
Device::vendor_id() can't return UNKNOWN either.
So, I think the From impl is not ideal, since we can't limit its visibility. In
order to improve this, I suggest to use Vendor::new() directly in the macro, and
make Vendor::new() private. The same goes for Class, I guess.
Powered by blists - more mailing lists