From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 69A523594F; Mon, 25 Aug 2025 12:47:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756126030; cv=none; b=tPk4OgrNL7sGbMf/PBB+lCZK9bRuKrQPHsXjdcjphgfRpuLUyR8cvgx/SaBs9E+4D4CG0V0tGD/EHIe+/448E2oeJWSjU0wm5r/s+C4Zhly+latAOVJctn7YqDrKmonmpS6N8LtIIizyM0L8dzO0R+9Gh8yhpbycGqnLjsACWBE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756126030; c=relaxed/simple; bh=HGYdcWZgKIhpwHPJu1s1DccprdUr4BAstL5WhCEEtgA=; h=Mime-Version:Content-Type:Date:Message-Id:Subject:Cc:To:From: References:In-Reply-To; b=M+P1fejZ8qZk/QSn48RJzbJtCcFXSYRTjJvqbNY2cw9QDyg0RA4kCrw6Z3Di3DCYDRLF7NtAVtDvAhsP2jOBw/4AELO5CTBTzps2FrYy0mOObbieGmF4VGN4fZOQCfzASxnWDlsZ/VUtXxtWDrAdqrkikJz9lg9DoJnMCtu4PHw= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iRcvpEov; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iRcvpEov" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 274E8C4CEED; Mon, 25 Aug 2025 12:47:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1756126030; bh=HGYdcWZgKIhpwHPJu1s1DccprdUr4BAstL5WhCEEtgA=; h=Date:Subject:Cc:To:From:References:In-Reply-To:From; b=iRcvpEovPSlpoqn7pESiOIcG3vbOMQiFFi1c8JCLsHmBfd6BEtH34oKXORDpzEap7 iVZo+N5qdpf27netp92LZjqDWI37lW63fnddViaI9g8LrpQM4UnNLiag6AXyge8K/U 9whieV9UY0RBha9YdcRDO8wT8JxKzGWovanQrG5w/C0SwmNesXwbp4cJvPCsviK+JG 7kUgRtSSWDx9LBHmmXLuKhqGx4e/CDyikC8nFOQ8E6jOSxjoJ1EfTAmKnKLiv+wQ7r +u90sKqjqdFI0C9Dk9NNTB7/e9/yt8NV2TDzAs1uhbzgCmIaZRJt1laYh1Df4YzaUZ flKsi2/aiyZ/A== Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 25 Aug 2025 14:47:04 +0200 Message-Id: Subject: Re: [PATCH v6 2/5] rust: pci: provide access to PCI Vendor values Cc: "John Hubbard" , "Joel Fernandes" , "Timur Tabi" , "Alistair Popple" , "David Airlie" , "Simona Vetter" , "Bjorn Helgaas" , =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= , "Miguel Ojeda" , "Alex Gaynor" , "Boqun Feng" , "Gary Guo" , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , "Benno Lossin" , "Andreas Hindborg" , "Alice Ryhl" , "Trevor Gross" , , , , "LKML" , "Elle Rhumsaa" To: "Alexandre Courbot" From: "Danilo Krummrich" References: <20250822020354.357406-1-jhubbard@nvidia.com> <20250822020354.357406-3-jhubbard@nvidia.com> In-Reply-To: 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 =3D $binding:expr,)+ >> + ) =3D> { >> + >> + impl Vendor { >> + $( >> + #[allow(missing_docs)] >> + pub const $variant: Self =3D Self($binding as u32); >> + )+ >> + } >> + >> + /// Convert a raw 16-bit vendor ID to a `Vendor`. >> + impl From for Vendor { >> + fn from(value: u32) -> Self { >> + match value { >> + $(x if x =3D=3D Self::$variant.0 =3D> Self::$varian= t,)+ >> + _ =3D> 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 =3D > 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 o= nly 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.