rust, nova-core: PCI Class, Vendor support
Subsystems (8)
v7 cover rust, nova-core: PCI Class, Vendor support
Changes since v6:
* Applied changes from Danilo's and Alex's and Elle's reviews (thanks!):
* Rebased onto driver-core-next, which is here:
https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core.git
* Changed pci::Vendor to be a u16, instead of a u32.
* Inlined all of the tiniest functions.
* Changed from Class/Vendor new(), to from_raw().
* Made from_raw() only accessible to super, which in this case is the pci module.
* Restored infallible operations. That causes Alex's request for the following reasonable behavior to work once again:
from_raw(0x10de).as_raw() == 0x10de
* Added a new patch, to inline the remaining PCI operations. This provides consistent inline choices throughout pci.rs.
Changes since v5:
* Applied changes from Danilo's review (thanks!):
* Split the nova-core patch into two patches, for nova and pci.
* Added rust/kernel/pci/ to MAINTAINERS.
Changes since v4:
* Applied changes from Danilo's and Alex's review (thanks!):
* Reorganized the patches so that the Nova changes consume the results of Class and Vendor upgrades, all in one shot.
* Made Class and Vendor types get constructed infallibly.
* This was all somewhat disruptive, and also required one more patch in order to properly separate the various steps. But I think it is all correct now. And CLIPPY=1 builds cleanly too.
* Elle Rhumsaa provided a Reviewed-by for v4 (thanks!), but due to the churn in v5 here, I thought it best to not add that tag to v5 yet. Instead, I have directly Cc'd Elle on the patches for now.
Changes since v3:
* Applied changes from Danilo's review (thanks!):
* Moved Class and Vendor to a new pci/id.rs file.
* Added ClassMask, to constrain callers to use only the two valid masks.
* Removed pci_class_code_raw()
* Changed Class and Vendor .as_u32() to .as_raw(), because after Danilo's comment I looked around rust/kernel and learned that .as_raw() is the overwhelmingly used convention.
* Changed vendor_id() to return a Vendor instance directly.
* Also, validated Vendor during construction, just as is done with Class. Both of these items are expected to match known values, even for new devices, so that's a reasonable move.
Changes since v2:
* Applied changes from Danilo's and Alex's review (thanks!):
* Moved everything possible out of the new define_all_pci_classes!() and define_all_pci_vendors!() macros.
* Used "impl TryFrom<u32> for Class/Vendor", instead of .from_u32().
* Made the new DeviceId methods infallible.
* Upgraded DeviceId::from_id() to accept a Vendor struct.
* Changed the names to be a little clearer:
* class_code_raw() --> pci_class_code_raw()
* class_enum() --> pci_class()
* Added doctests for the items that are not yet used in real drivers.
v2 is here:
https://lore.kernel.org/[email protected]
Changes since v1:
1) Use the pci_device_table for filtering, instead of open-coding filters in the .probe() callback.
2) Add PCI Class (class, subclass, implementation) and PCI Vendor to Rust for Linux.
3) Rebased onto the latest nova-next branch, which is here:
https://gitlab.freedesktop.org/drm/nova.git
v1 is here:
https://lore.kernel.org/[email protected]
John Hubbard (6):
rust: pci: provide access to PCI Class and Class-related items
rust: pci: provide access to PCI Vendor values
rust: pci: add DeviceId::from_class_and_vendor() method
gpu: nova-core: avoid probing non-display/compute PCI functions
rust: pci: use pci::Vendor instead of bindings::PCI_VENDOR_ID_*
rust: pci: inline several tiny functions
MAINTAINERS | 1 + drivers/gpu/nova-core/driver.rs | 33 +- rust/kernel/pci.rs | 55 ++- rust/kernel/pci/id.rs | 577 ++++++++++++++++++++++++++ samples/rust/rust_dma.rs | 6 +- samples/rust/rust_driver_auxiliary.rs | 12 +- samples/rust/rust_driver_pci.rs | 9 +- 7 files changed, 665 insertions(+), 28 deletions(-) create mode 100644 rust/kernel/pci/id.rs
base-commit: 8f5ae30d69d7543eee0d70083daf4de8fe15d585
Signature · 1 line
2.51.0
PATCH v7 1/6 rust: pci: provide access to PCI Class and Class-related items
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::from_raw() -- Only callable from pci module. Class::as_raw()
ClassMask: Full, ClassSubclass Device::pci_class()
--- MAINTAINERS | 1 + rust/kernel/pci.rs | 11 ++ rust/kernel/pci/id.rs | 232 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 244 insertions(+) create mode 100644 rust/kernel/pci/id.rs
MAINTAINERS +1 -0
@@ -19532,6 +19532,7 @@ C: irc://irc.oftc.net/linux-pci T: git git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git F: rust/helpers/pci.c F: rust/kernel/pci.rs +F: rust/kernel/pci/ F: samples/rust/rust_driver_pci.rs PCIE BANDWIDTH CONTROLLER
rust/kernel/pci.rs +11 -0
@@ -23,6 +23,10 @@ };use kernel::prelude::*; +mod id; + +pub use self::id::{Class ,ClassMask }; +/// An adapter for the registration of PCI drivers. pub struct Adapter <T :Driver >(T );
@@ -410,6 +414,13 @@ pub fn resource_len(&self, bar: u32) -> Result<bindings::resource_size_t> {// - by its type invariant `self.as_raw` is always a valid pointer to a `struct pci_dev`. Ok (unsafe { bindings::pci_resource_len (self.as_raw (), bar.try_into ()?) }) } + +/// Returns the PCI class as a `Class` struct. +#[inline] +pub fn pci_class (&self) ->Class { +// SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. +Class ::from_raw (unsafe { (*self.as_raw ()).class }) + } }impl Device <device::Bound > {
rust/kernel/pci/id.rs new file +232 -0
232 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v7 2/6 rust: pci: provide access to PCI Vendor values
In reply to John HubbardThis allows callers to write Vendor::SOME_COMPANY instead of bindings::PCI_VENDOR_ID_SOME_COMPANY.
New APIs:
Vendor::SOME_COMPANY
Vendor::from_raw() -- Only accessible from the pci (parent) module. Vendor::as_raw()
Vendor: fmt::Display for Vendor
--- rust/kernel/pci.rs | 2 +- rust/kernel/pci/id.rs | 349 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 349 insertions(+), 2 deletions(-)
rust/kernel/pci.rs +1 -1
@@ -25,7 +25,7 @@mod id; -pub use self::id::{Class ,ClassMask }; +pub use self::id::{Class ,ClassMask ,Vendor };/// An adapter for the registration of PCI drivers. pub struct Adapter <T :Driver >(T );
rust/kernel/pci/id.rs +348 -1
364 lines not on this page · raw message
Signature · 1 line
2.51.0
Alexandre Courbot
maintainerRe: [PATCH v7 2/6] rust: pci: provide access to PCI Vendor values
Quoted 69 lines · John Hubbard
This allows callers to write
Vendor::SOME_COMPANYinstead ofbindings::PCI_VENDOR_ID_SOME_COMPANY.New APIs:
Vendor::SOME_COMPANY
Vendor::from_raw()-- Only accessible from the pci (parent) module.Vendor::as_raw()
Vendor:fmt::Displayfor VendorCc Danilo Krummrich <[email protected]>Cc Alexandre Courbot <[email protected]>Cc Elle Rhumsaa <[email protected]>Signed-off-by John Hubbard <[email protected]>--- rust/kernel/pci.rs | 2 +- rust/kernel/pci/id.rs | 349 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 349 insertions(+), 2 deletions(-)rust/kernel/pci.rs +1
-1@@ -25,7 +25,7 @@mod id; -pub use self::id::{Class ,ClassMask }; +pub use self::id::{Class ,ClassMask ,Vendor };/// An adapter for the registration of PCI drivers. pub struct Adapter <T :Driver >(T );rust/kernel/pci/id.rs +22
-1@@ -2,7 +2,7 @@//! PCI device identifiers and related types. //! -//! This module contains PCI class codes and supporting types. +//! This module contains PCI class codes, Vendor IDs, and supporting types. use crate ::{bindings, error::code::EINVAL , error::Error , prelude::*};use core::fmt;@@ -109,6 +109,69 @@ fn try_from(value: u32) -> Result<Self, Self::Error> { } } +/// PCI vendor IDs. +/// +/// Each entry contains the 16-bit PCI vendor ID as assigned by the PCI SIG. +/// +/// # Examples +/// +/// ``` +/// # use kernel::{device::Core, pci::{self, Vendor}, prelude::*}; +/// fn log_device_info(pdev: &pci::Device<Core>) -> Result<()> { +/// // Compare raw vendor ID with known vendor constant +/// let vendor_id = pdev.vendor_id(); +/// if vendor_id == Vendor::NVIDIA.as_raw() { +/// dev_info!( +/// pdev.as_ref(), +/// "Found NVIDIA device: 0x{:x}\n", +/// pdev.device_id() +/// ); +/// } +/// Ok(()) +/// } +/// ```
rust/kernel/pci/id.rs fn try_from(value: u32) -> Result<Self, Self::Error> { · line 142
Quoted 10 lines
rust/kernel/pci/id.rs +10
-0+#[derive( Debug , Clone , Copy , PartialEq , Eq )] +#[repr(transparent)] +pub struct Vendor (u16 ); + +macro_rules! define_all_pci_vendors { + ( + $($variant:ident = $binding:expr,)+ + ) => { + +impl Vendor {
Why the blank line here? (same for the define_all_pci_classes in the previous patch).
Quoted 21 lines
rust/kernel/pci/id.rs +21
-0+ $( + #[allow(missing_docs)] + pub const $variant: Self = Self($binding as u16); + )+ + } + }; +} + +/// Once constructed, a `Vendor` contains a valid PCI Vendor ID. +impl Vendor { +/// Create a Vendor from a raw 16-bit vendor ID. +/// Only accessible from the parent pci module. +#[expect(dead_code)] +#[inline] +pub (super )fn from_raw (vendor_id:u16 ) ->Self { +Self (vendor_id) + } + +/// Get the raw 16-bit vendor ID value. +#[inline] +pub const fn as_raw (self) ->u16 {
rust/kernel/pci/id.rs fn try_from(value: u32) -> Result<Self, Self::Error> { · line 173
Quoted 10 lines
rust/kernel/pci/id.rs +10
-0+ self.0 + } +} + +impl fmt::Display for Vendor { +#[inline] +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { +write! (f,"0x{:04x}" , self.0 ) + } +}
Possibly an exercice for a future patch, but do we want to display the vendor name if it is defined, rather than its hex code (which is more the job of Debug)? We could leverage the macro above to do that. The same should be doable for the PCI classes.
I suspect strings for all the names already exist on the C side, in which case we would want to reuse them instead of defining new ones.
Note that I don't think this needs to be done for this series - it's just a thought as I was looking at this Display implementation that looks more like a Debug one.
Danilo Krummrich
maintainerRe: [PATCH v7 2/6] rust: pci: provide access to PCI Vendor values
Quoted 121 lines · Alexandre Courbot
Quoted 69 lines · John Hubbard
This allows callers to write
Vendor::SOME_COMPANYinstead ofbindings::PCI_VENDOR_ID_SOME_COMPANY.New APIs:
Vendor::SOME_COMPANY
Vendor::from_raw()-- Only accessible from the pci (parent) module.Vendor::as_raw()
Vendor:fmt::Displayfor VendorCc Danilo Krummrich <[email protected]>Cc Alexandre Courbot <[email protected]>Cc Elle Rhumsaa <[email protected]>Signed-off-by John Hubbard <[email protected]>--- rust/kernel/pci.rs | 2 +- rust/kernel/pci/id.rs | 349 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 349 insertions(+), 2 deletions(-)rust/kernel/pci.rs +1
-1@@ -25,7 +25,7 @@mod id; -pub use self::id::{Class ,ClassMask }; +pub use self::id::{Class ,ClassMask ,Vendor };/// An adapter for the registration of PCI drivers. pub struct Adapter <T :Driver >(T );rust/kernel/pci/id.rs +22
-1@@ -2,7 +2,7 @@//! PCI device identifiers and related types. //! -//! This module contains PCI class codes and supporting types. +//! This module contains PCI class codes, Vendor IDs, and supporting types. use crate ::{bindings, error::code::EINVAL , error::Error , prelude::*};use core::fmt;@@ -109,6 +109,69 @@ fn try_from(value: u32) -> Result<Self, Self::Error> { } } +/// PCI vendor IDs. +/// +/// Each entry contains the 16-bit PCI vendor ID as assigned by the PCI SIG. +/// +/// # Examples +/// +/// ``` +/// # use kernel::{device::Core, pci::{self, Vendor}, prelude::*}; +/// fn log_device_info(pdev: &pci::Device<Core>) -> Result<()> { +/// // Compare raw vendor ID with known vendor constant +/// let vendor_id = pdev.vendor_id(); +/// if vendor_id == Vendor::NVIDIA.as_raw() { +/// dev_info!( +/// pdev.as_ref(), +/// "Found NVIDIA device: 0x{:x}\n", +/// pdev.device_id() +/// ); +/// } +/// Ok(()) +/// } +/// ``` rust/kernel/pci/id.rs fn try_from(value: u32) -> Result<Self, Self::Error> { · line 142
Quoted 10 lines
rust/kernel/pci/id.rs +10
-0+#[derive( Debug , Clone , Copy , PartialEq , Eq )] +#[repr(transparent)] +pub struct Vendor (u16 ); + +macro_rules! define_all_pci_vendors { + ( + $($variant:ident = $binding:expr,)+ + ) => { + +impl Vendor {Why the blank line here? (same for the
define_all_pci_classesin the previous patch).Quoted 21 lines
rust/kernel/pci/id.rs +21
-0+ $( + #[allow(missing_docs)] + pub const $variant: Self = Self($binding as u16); + )+ + } + }; +} + +/// Once constructed, a `Vendor` contains a valid PCI Vendor ID. +impl Vendor { +/// Create a Vendor from a raw 16-bit vendor ID. +/// Only accessible from the parent pci module. +#[expect(dead_code)] +#[inline] +pub (super )fn from_raw (vendor_id:u16 ) ->Self { +Self (vendor_id) + } + +/// Get the raw 16-bit vendor ID value. +#[inline] +pub const fn as_raw (self) ->u16 {rust/kernel/pci/id.rs fn try_from(value: u32) -> Result<Self, Self::Error> { · line 173
Quoted 10 lines
rust/kernel/pci/id.rs +10
-0+ self.0 + } +} + +impl fmt::Display for Vendor { +#[inline] +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { +write! (f,"0x{:04x}" , self.0 ) + } +}Possibly an exercice for a future patch, but do we want to display the vendor name if it is defined, rather than its hex code (which is more the job of
Debug)? We could leverage the macro above to do that. The same should be doable for the PCI classes.
Quoted 6 lines
I suspect strings for all the names already exist on the C side, in which case we would want to reuse them instead of defining new ones.
Note that I don't think this needs to be done for this series - it's just a thought as I was looking at this
Displayimplementation that looks more like aDebugone.
Yeah, this can be addressed subsequently; it might make sense to align Display and Debug though. Currently, Vendor simply derives Debug, resulting in the decimal value to be printed.
Re: [PATCH v7 2/6] rust: pci: provide access to PCI Vendor values
In reply to Alexandre CourbotQuoted 1 line · Alexandre Courbot
On Wed Aug 27, 2025 at 8:12 AM JST, John Hubbard wrote:
...
Quoted 9 lines
rust/kernel/pci/id.rs fn try_from(value: u32) -> Result<Self, Self::Error> { · line 142
Quoted 6 lines
rust/kernel/pci/id.rs +6
-0+macro_rules! define_all_pci_vendors { + ( + $($variant:ident = $binding:expr,)+ + ) => { + +impl Vendor {Why the blank line here? (same for the
define_all_pci_classesin the previous patch).
Removed in both places, thanks!
...
Quoted 12 lines
rust/kernel/pci/id.rs fn try_from(value: u32) -> Result<Self, Self::Error> { · line 173
Quoted 6 lines
rust/kernel/pci/id.rs +6
-0+impl fmt::Display for Vendor { +#[inline] +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { +write! (f,"0x{:04x}" , self.0 ) + } +}Possibly an exercice for a future patch, but do we want to display the vendor name if it is defined, rather than its hex code (which is more the job of
Debug)? We could leverage the macro above to do that. The same should be doable for the PCI classes.
Quoted 2 lines
I suspect strings for all the names already exist on the C side, in which case we would want to reuse them instead of defining new ones.
A reasonable suspicion, but actually, they do not yet exist. So I think we could just add them on the Rust side.
Quoted 4 lines
Note that I don't think this needs to be done for this series - it's just a thought as I was looking at this
Displayimplementation that looks more like aDebugone.
Good points. Yes, I can do a follow-up patch.
thanks,
Signature · 1 line
John Hubbard
PATCH v7 3/6 rust: pci: add DeviceId::from_class_and_vendor() method
In reply to John HubbardAdd a new method to create PCI DeviceIds that match both a specific vendor and PCI class. This is more targeted than the existing from_class() method as it filters on both vendor and class criteria.
--- rust/kernel/pci.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
rust/kernel/pci.rs +22 -0
@@ -161,6 +161,28 @@ pub const fn from_class(class: u32, class_mask: u32) -> Self { override_only: 0, }) } + +/// Create a new `pci::DeviceId` from a class number, mask, and specific vendor. +/// +/// This is more targeted than [`DeviceId::from_class`]: in addition to matching by Vendor, it +/// also matches the PCI Class (up to the entire 24 bits, depending on the mask). +#[inline] +pub const fn from_class_and_vendor ( + class:Class , + class_mask:ClassMask , + vendor:Vendor , + ) ->Self { +Self (bindings::pci_device_id { + vendor: vendor.as_raw ()as u32 , + device:DeviceId ::PCI_ANY_ID , + subvendor:DeviceId ::PCI_ANY_ID , + subdevice:DeviceId ::PCI_ANY_ID , + class: class.as_raw (), + class_mask: class_mask.as_raw (), + driver_data:0 , + override_only:0 , + }) + } }// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `pci_device_id` and does not add
Signature · 1 line
2.51.0
PATCH v7 4/6 gpu: nova-core: avoid probing non-display/compute PCI functions
In reply to John HubbardNovaCore has so far been too imprecise about figuring out if .probe() has found a supported PCI PF (Physical Function). By that I mean:
.probe() sets up BAR0 (which involves a lot of very careful devres and Device<Bound> details behind the scenes). And then if it is dealing with a non-supported device such as the .1 audio PF on many GPUs, it fails out due to an unexpected BAR0 size. We have been fortunate that the BAR0 sizes are different.
Really, we should be filtering on PCI class ID instead. These days I think we can confidently pick out Nova's supported PF's via PCI class ID. And if not, then we'll revisit.
The approach here is to filter on "Display VGA" or "Display 3D", which is how PCI class IDs express "this is a modern GPU's PF".
--- drivers/gpu/nova-core/driver.rs | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-)
drivers/gpu/nova-core/driver.rs +28 -5
@@ -1,6 +1,14 @@// SPDX-License-Identifier: GPL-2.0 -use kernel::{auxiliary, bindings, c_str, device::Core , pci, prelude::*, sizes::SZ_16M , sync::Arc }; +use kernel::{ + auxiliary, c_str, + device::Core , + pci, + pci::{Class ,ClassMask ,Vendor }, + prelude::*, + sizes::SZ_16M , + sync::Arc , +};use crate ::gpu::Gpu ;
@@ -18,10 +26,25 @@ pub(crate) struct NovaCore { PCI_TABLE, MODULE_PCI_TABLE, <NovaCore as pci::Driver>::IdInfo, - [( - pci::DeviceId ::from_id (bindings::PCI_VENDOR_ID_NVIDIA , bindings::PCI_ANY_ID as u32 ), - () - )] + [ +// Modern NVIDIA GPUs will show up as either VGA or 3D controllers. + ( + pci::DeviceId::from_class_and_vendor( + Class::DISPLAY_VGA, + ClassMask::ClassSubclass, + Vendor::NVIDIA + ), + () + ), + ( + pci::DeviceId ::from_class_and_vendor ( +Class ::DISPLAY_3D , +ClassMask ::ClassSubclass , +Vendor ::NVIDIA + ), + () + ), + ] );impl pci::Driver for NovaCore {
Signature · 1 line
2.51.0
PATCH v7 5/6 rust: pci: use pci::Vendor instead of bindings::PCI_VENDOR_ID_*
In reply to John HubbardChange Device::vendor_id() to return a Vendor type, and change DeviceId::from_id() to accept a Vendor type.
Use the new pci::Vendor in the various Rust for Linux callers who were previously using bindings::PCI_VENDOR_ID_*.
Doing so also allows removing "use kernel::bindings" entirely from most of the affected files here.
Also, mark vendor_id() as inline.
--- rust/kernel/pci.rs | 16 +++++++++------- rust/kernel/pci/id.rs | 18 ++++++++---------- samples/rust/rust_dma.rs | 6 +----- samples/rust/rust_driver_auxiliary.rs | 12 +++++------- samples/rust/rust_driver_pci.rs | 9 +++++---- 5 files changed, 28 insertions(+), 33 deletions(-)
rust/kernel/pci.rs +9 -7
@@ -132,10 +132,10 @@ impl DeviceId {/// Equivalent to C's `PCI_DEVICE` macro. /// -/// Create a new `pci::DeviceId` from a vendor and device ID number. -pub const fn from_id (vendor:u32 , device:u32 ) ->Self { +/// Create a new `pci::DeviceId` from a vendor and device ID. +pub const fn from_id (vendor:Vendor , device:u32 ) ->Self {Self (bindings::pci_device_id { - vendor, + vendor: vendor.as_raw ()as u32 , device, subvendor:DeviceId ::PCI_ANY_ID , subdevice:DeviceId ::PCI_ANY_ID ,
@@ -232,7 +232,7 @@ macro_rules! pci_device_table {/// <MyDriver as pci::Driver>::IdInfo, /// [ /// ( -/// pci::DeviceId::from_id(bindings::PCI_VENDOR_ID_REDHAT, bindings::PCI_ANY_ID as u32), +/// pci::DeviceId::from_id(pci::Vendor::REDHAT, bindings::PCI_ANY_ID as u32), /// (), /// ) /// ]
@@ -413,10 +413,12 @@ fn as_raw(&self) -> *mut bindings::pci_dev { }impl Device { -/// Returns the PCI vendor ID. -pub fn vendor_id (&self) ->u16 { +/// Returns the PCI vendor ID as a validated Vendor. +#[inline] +pub fn vendor_id (&self) ->Vendor {// SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. -unsafe { (*self.as_raw ()).vendor } +let vendor_id =unsafe { (*self.as_raw ()).vendor }; +Vendor ::from_raw (vendor_id) }/// Returns the PCI device ID.
rust/kernel/pci/id.rs +8 -10
@@ -118,15 +118,14 @@ fn try_from(value: u32) -> Result<Self, Self::Error> {/// ``` /// # use kernel::{device::Core, pci::{self, Vendor}, prelude::*}; /// fn log_device_info(pdev: &pci::Device<Core>) -> Result<()> { -/// // Compare raw vendor ID with known vendor constant -/// let vendor_id = pdev.vendor_id(); -/// if vendor_id == Vendor::NVIDIA.as_raw() { -/// dev_info!( -/// pdev.as_ref(), -/// "Found NVIDIA device: 0x{:x}\n", -/// pdev.device_id() -/// ); -/// } +/// // Get the validated PCI vendor ID +/// let vendor = pdev.vendor_id(); +/// dev_info!( +/// pdev.as_ref(), +/// "Device: Vendor={}, Device=0x{:x}\n", +/// vendor, +/// pdev.device_id() +/// ); /// Ok(()) /// } /// ```
@@ -152,7 +151,6 @@ impl Vendor {impl Vendor {/// Create a Vendor from a raw 16-bit vendor ID. /// Only accessible from the parent pci module. -#[expect(dead_code)] #[inline] pub (super )fn from_raw (vendor_id:u16 ) ->Self {Self (vendor_id)
samples/rust/rust_dma.rs +1 -5
@@ -5,7 +5,6 @@//! To make this driver probe, QEMU must be run with `-device pci-testdev`. use kernel::{ - bindings, device::Core , dma::{CoherentAllocation ,Device ,DmaMask }, pci,
@@ -45,10 +44,7 @@ unsafe impl kernel::transmute::FromBytes for MyStruct {} PCI_TABLE, MODULE_PCI_TABLE, <DmaSampleDriver as pci::Driver>::IdInfo, - [( - pci::DeviceId ::from_id (bindings::PCI_VENDOR_ID_REDHAT ,0x5 ), - () - )] + [(pci::DeviceId ::from_id (pci::Vendor ::REDHAT ,0x5 ), ())] );impl pci::Driver for DmaSampleDriver {
samples/rust/rust_driver_auxiliary.rs +5 -7
@@ -5,7 +5,7 @@//! To make this driver probe, QEMU must be run with `-device pci-testdev`. use kernel::{ - auxiliary, bindings, c_str, device::Core , driver, error::Error , pci, prelude::*,InPlaceModule , + auxiliary, c_str, device::Core , driver, error::Error , pci, prelude::*,InPlaceModule , };use pin_init::PinInit ;
@@ -50,10 +50,7 @@ struct ParentDriver { PCI_TABLE, MODULE_PCI_TABLE, <ParentDriver as pci::Driver>::IdInfo, - [( - pci::DeviceId ::from_id (bindings::PCI_VENDOR_ID_REDHAT ,0x5 ), - () - )] + [(pci::DeviceId ::from_id (pci::Vendor ::REDHAT ,0x5 ), ())] );impl pci::Driver for ParentDriver {
@@ -81,11 +78,12 @@ fn connect(adev: &auxiliary::Device) -> Result<()> {let parent = adev.parent ().ok_or (EINVAL )?;let pdev: &pci::Device = parent.try_into ()?; +let vendor = pdev.vendor_id ();dev_info! ( adev.as_ref (), -"Connect auxiliary {} with parent: VendorID={:#x}, DeviceID={:#x}\n" , +"Connect auxiliary {} with parent: VendorID={}, DeviceID={:#x}\n" , adev.id (), - pdev.vendor_id (), + vendor, pdev.device_id () );
samples/rust/rust_driver_pci.rs +5 -4
@@ -4,7 +4,7 @@//! //! To make this driver probe, QEMU must be run with `-device pci-testdev`. -use kernel::{bindings, c_str, device::Core , devres::Devres , pci, prelude::*, types::ARef }; +use kernel::{c_str, device::Core , devres::Devres , pci, prelude::*, types::ARef };struct Regs ;
@@ -38,7 +38,7 @@ struct SampleDriver { MODULE_PCI_TABLE, <SampleDriver as pci::Driver>::IdInfo, [( - pci::DeviceId ::from_id (bindings::PCI_VENDOR_ID_REDHAT ,0x5 ), + pci::DeviceId ::from_id (pci::Vendor ::REDHAT ,0x5 ),TestIndex ::NO_EVENTFD )] );
@@ -66,10 +66,11 @@ impl pci::Driver for SampleDriver {const ID_TABLE : pci::IdTable <Self ::IdInfo > = &PCI_TABLE ;fn probe (pdev: &pci::Device <Core >, info: &Self ::IdInfo ) ->Result <Pin <KBox <Self >>> { +let vendor = pdev.vendor_id ();dev_dbg! ( pdev.as_ref (), -"Probe Rust PCI driver sample (PCI ID: 0x{:x}, 0x{:x}).\n" , - pdev.vendor_id (), +"Probe Rust PCI driver sample (PCI ID: {}, 0x{:x}).\n" , + vendor, pdev.device_id () );
Signature · 1 line
2.51.0
Alexandre Courbot
maintainerRe: [PATCH v7 5/6] rust: pci: use pci::Vendor instead of bindings::PCI_VENDOR_ID_*
On Wed Aug 27, 2025 at 8:12 AM JST, John Hubbard wrote:
<snip>
Quoted 15 lines
rust/kernel/pci/id.rs +0
-7@@ -118,15 +118,14 @@ fn try_from(value: u32) -> Result<Self, Self::Error> {/// ``` /// # use kernel::{device::Core, pci::{self, Vendor}, prelude::*}; /// fn log_device_info(pdev: &pci::Device<Core>) -> Result<()> { -/// // Compare raw vendor ID with known vendor constant -/// let vendor_id = pdev.vendor_id(); -/// if vendor_id == Vendor::NVIDIA.as_raw() { -/// dev_info!( -/// pdev.as_ref(), -/// "Found NVIDIA device: 0x{:x}\n", -/// pdev.device_id()
rust/kernel/pci/id.rs fn try_from(value: u32) -> Result<Self, Self::Error> { · line 128
Quoted 10 lines
rust/kernel/pci/id.rs +8
-2-/// ); -/// } +/// // Get the validated PCI vendor ID +/// let vendor = pdev.vendor_id(); +/// dev_info!( +/// pdev.as_ref(), +/// "Device: Vendor={}, Device=0x{:x}\n", +/// vendor, +/// pdev.device_id() +/// );
Why not use this new example starting from patch 2, which introduced the previous code that this patch removes?
Danilo Krummrich
maintainerRe: [PATCH v7 5/6] rust: pci: use pci::Vendor instead of bindings::PCI_VENDOR_ID_*
Quoted 22 lines · Alexandre Courbot
On Wed Aug 27, 2025 at 8:12 AM JST, John Hubbard wrote:
<snip>Quoted 20 lines
rust/kernel/pci/id.rs +3
-9@@ -118,15 +118,14 @@ fn try_from(value: u32) -> Result<Self, Self::Error> {/// ``` /// # use kernel::{device::Core, pci::{self, Vendor}, prelude::*}; /// fn log_device_info(pdev: &pci::Device<Core>) -> Result<()> { -/// // Compare raw vendor ID with known vendor constant -/// let vendor_id = pdev.vendor_id(); -/// if vendor_id == Vendor::NVIDIA.as_raw() { -/// dev_info!( -/// pdev.as_ref(), -/// "Found NVIDIA device: 0x{:x}\n", -/// pdev.device_id() -/// ); -/// } +/// // Get the validated PCI vendor ID +/// let vendor = pdev.vendor_id(); +/// dev_info!(
Quoted 8 lines
rust/kernel/pci/id.rs fn try_from(value: u32) -> Result<Self, Self::Error> { · line 128
Quoted 5 lines
rust/kernel/pci/id.rs +5
-0+/// pdev.as_ref(), +/// "Device: Vendor={}, Device=0x{:x}\n", +/// vendor, +/// pdev.device_id() +/// ); Why not use this new example starting from patch 2, which introduced the previous code that this patch removes?
I think that's because in v2 vendor_id() still returns the raw value. I think it makes a little more sense if this patch simply introduces the example as an example for vendor_id() itself.
I think struct Vendor does not necessarily need an example by itself.
Re: [PATCH v7 5/6] rust: pci: use pci::Vendor instead of bindings::PCI_VENDOR_ID_*
Quoted 32 lines · Danilo Krummrich
Quoted 22 lines · Alexandre Courbot
On Wed Aug 27, 2025 at 8:12 AM JST, John Hubbard wrote:
<snip>Quoted 20 lines
rust/kernel/pci/id.rs +3
-9@@ -118,15 +118,14 @@ fn try_from(value: u32) -> Result<Self, Self::Error> {/// ``` /// # use kernel::{device::Core, pci::{self, Vendor}, prelude::*}; /// fn log_device_info(pdev: &pci::Device<Core>) -> Result<()> { -/// // Compare raw vendor ID with known vendor constant -/// let vendor_id = pdev.vendor_id(); -/// if vendor_id == Vendor::NVIDIA.as_raw() { -/// dev_info!( -/// pdev.as_ref(), -/// "Found NVIDIA device: 0x{:x}\n", -/// pdev.device_id() -/// ); -/// } +/// // Get the validated PCI vendor ID +/// let vendor = pdev.vendor_id(); +/// dev_info!( Quoted 8 lines
rust/kernel/pci/id.rs fn try_from(value: u32) -> Result<Self, Self::Error> { · line 128
Quoted 5 lines
rust/kernel/pci/id.rs +5
-0+/// pdev.as_ref(), +/// "Device: Vendor={}, Device=0x{:x}\n", +/// vendor, +/// pdev.device_id() +/// ); Why not use this new example starting from patch 2, which introduced the previous code that this patch removes?
Quoted 1 line
I think that's because in v2
vendor_id()still returns the raw value. I think it
That is correct.
Quoted 4 lines
makes a little more sense if this patch simply introduces the example as an example for
vendor_id()itself.I think struct Vendor does not necessarily need an example by itself.
I'm not quite sure if you are asking for a change to this patch? The example already exercises .vendor_id(), so...?
thanks,
Signature · 1 line
John Hubbard
Danilo Krummrich
maintainerRe: [PATCH v7 5/6] rust: pci: use pci::Vendor instead of bindings::PCI_VENDOR_ID_*
Quoted 37 lines · John Hubbard
Quoted 32 lines · Danilo Krummrich
Quoted 22 lines · Alexandre Courbot
On Wed Aug 27, 2025 at 8:12 AM JST, John Hubbard wrote:
<snip>Quoted 20 lines
rust/kernel/pci/id.rs +3
-9@@ -118,15 +118,14 @@ fn try_from(value: u32) -> Result<Self, Self::Error> {/// ``` /// # use kernel::{device::Core, pci::{self, Vendor}, prelude::*}; /// fn log_device_info(pdev: &pci::Device<Core>) -> Result<()> { -/// // Compare raw vendor ID with known vendor constant -/// let vendor_id = pdev.vendor_id(); -/// if vendor_id == Vendor::NVIDIA.as_raw() { -/// dev_info!( -/// pdev.as_ref(), -/// "Found NVIDIA device: 0x{:x}\n", -/// pdev.device_id() -/// ); -/// } +/// // Get the validated PCI vendor ID +/// let vendor = pdev.vendor_id(); +/// dev_info!( Quoted 8 lines
rust/kernel/pci/id.rs fn try_from(value: u32) -> Result<Self, Self::Error> { · line 128
Quoted 5 lines
rust/kernel/pci/id.rs +5
-0+/// pdev.as_ref(), +/// "Device: Vendor={}, Device=0x{:x}\n", +/// vendor, +/// pdev.device_id() +/// ); Why not use this new example starting from patch 2, which introduced the previous code that this patch removes?
Quoted 1 line
I think that's because in v2
vendor_id()still returns the raw value. I think itThat is correct.
Quoted 7 lines
Quoted 4 lines
makes a little more sense if this patch simply introduces the example as an example for
vendor_id()itself.I think struct Vendor does not necessarily need an example by itself.
I'm not quite sure if you are asking for a change to this patch? The example already exercises .vendor_id(), so...?
Yes, I think the example above should be on the vendor_id() method rather than on the Vendor struct and should be introduced by this patch.
Re: [PATCH v7 5/6] rust: pci: use pci::Vendor instead of bindings::PCI_VENDOR_ID_*
Quoted 8 lines · Danilo Krummrich
Quoted 7 lines · John Hubbard
Quoted 6 lines · Danilo Krummrich
Quoted 2 lines · Alexandre Courbot
On Wed Aug 27, 2025 at 8:12 AM JST, John Hubbard wrote:
<snip>makes a little more sense if this patch simply introduces the example as an example for
vendor_id()itself.
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
I think struct Vendor does not necessarily need an example by itself.
I'm not quite sure if you are asking for a change to this patch? The example already exercises .vendor_id(), so...?
Yes, I think the example above should be on the
vendor_id()method rather than on the Vendor struct and should be introduced by this patch.
ohhh, so located directly above it. I finally get it. haha. I'll do that, thanks. :)
thanks,
Signature · 1 line
John Hubbard
PATCH v7 6/6 rust: pci: inline several tiny functions
In reply to John HubbardSeveral previous commits added Vendor and Class functionality. As part of that, the new functions were inlined where appropriate. But that left this file with inconsistent use of inlining. Fix that by inlining the remaining items that should be.
--- rust/kernel/pci.rs | 6 ++++++ 1 file changed, 6 insertions(+)
rust/kernel/pci.rs +6 -0
@@ -133,6 +133,7 @@ impl DeviceId {/// Equivalent to C's `PCI_DEVICE` macro. /// /// Create a new `pci::DeviceId` from a vendor and device ID. +#[inline] pub const fn from_id (vendor:Vendor , device:u32 ) ->Self {Self (bindings::pci_device_id { vendor: vendor.as_raw ()as u32 ,
@@ -149,6 +150,7 @@ pub const fn from_id(vendor: Vendor, device: u32) -> Self {/// Equivalent to C's `PCI_DEVICE_CLASS` macro. /// /// Create a new `pci::DeviceId` from a class number and mask. +#[inline] pub const fn from_class (class:u32 , class_mask:u32 ) ->Self {Self (bindings::pci_device_id { vendor:DeviceId ::PCI_ANY_ID ,
@@ -385,6 +387,7 @@ fn release(&self) { }impl Bar { +#[inline] fn index_is_valid (index:u32 ) ->bool {// A `struct pci_dev` owns an array of resources with at most `PCI_NUM_RESOURCES` entries. index < bindings::PCI_NUM_RESOURCES
@@ -407,6 +410,7 @@ fn deref(&self) -> &Self::Target { }impl <Ctx : device::DeviceContext >Device <Ctx > { +#[inline] fn as_raw (&self) -> *mut bindings::pci_dev { self.0 .get () }
@@ -422,6 +426,7 @@ pub fn vendor_id(&self) -> Vendor { }/// Returns the PCI device ID. +#[inline] pub fn device_id (&self) ->u16 {// SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`. unsafe { (*self.as_raw ()).device }
@@ -476,6 +481,7 @@ pub fn enable_device_mem(&self) -> Result { }/// Enable bus-mastering for this device. +#[inline] pub fn set_master (&self) {// SAFETY: `self.as_raw` is guaranteed to be a pointer to a valid `struct pci_dev`. unsafe { bindings::pci_set_master (self.as_raw ()) };
Signature · 1 line
2.51.0
Quoted 13 lines · John Hubbard
Changes since v6:
* Applied changes from Danilo's and Alex's and Elle's reviews (thanks!):
* Rebased onto driver-core-next, which is here:
https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/driver-core.git
* Changedpci::Vendorto be a u16, instead of a u32.
* Inlined all of the tiniest functions.
* Changed from Class/Vendornew(), tofrom_raw().
* Madefrom_raw()only accessible to super, which in this case is the pci module.
* Restored infallible operations. That causes Alex's request for the following reasonable behavior to work once again:
Quoted 4 lines
from_raw(0x10de).as_raw() ==
0x10de* Added a new patch, to inline the remaining PCI operations. This provides consistent inline choices throughout
pci.rs.
I am far from a PCI expert, but regardless of bus considerations the code appears to make sense to me (particularly since it removes some uses of the bindings module!). Thus, and FWIW,