Skip to contentSkip to conversation

rust, nova-core: PCI Class, Vendor support

PATCH Superseded John Hubbard 3 participants 10 messages Aug 20, 2025

Subsystems (8)
DRM drivers PCI Rust DRM Rust +4 more subsystems

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v4 cover rust, nova-core: PCI Class, Vendor support

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 (3):
rust: pci: provide access to PCI Class and Class-related items
gpu: nova-core: avoid probing non-display/compute PCI functions
rust: pci: provide access to PCI Vendor values

 drivers/gpu/nova-core/driver.rs       |  33 +-
 rust/kernel/pci.rs                    |  48 +-
 rust/kernel/pci/id.rs                 | 602 ++++++++++++++++++++++++++
 samples/rust/rust_dma.rs              |   6 +-
 samples/rust/rust_driver_auxiliary.rs |  12 +-
 samples/rust/rust_driver_pci.rs       |   9 +-
 6 files changed, 682 insertions(+), 28 deletions(-)
 create mode 100644 rust/kernel/pci/id.rs

base-commit: 0dfc23ac2c5b3a62ab27fb9b6ee582b4bb5ce33c

Signature · 1 line
2.50.1
#1 lore raw

PATCH v4 1/3 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::as_raw()
Class: TryFrom<u32> for Class
ClassMask: Full, ClassSubclass DeviceId::from_class_and_vendor() Device::pci_class()

Cc Danilo Krummrich <[email protected]>
Cc Alexandre Courbot <[email protected]>
---
 rust/kernel/pci.rs    |  28 +++++
 rust/kernel/pci/id.rs | 247 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 275 insertions(+)
 create mode 100644 rust/kernel/pci/id.rs
rust/kernel/pci.rs +28 -0
index 887ee611b553..ee887d616320 100644
@@ -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);
 
@@ -157,6 +161,23 @@ 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).
+    pub const fn from_class_and_vendor(class: Class, class_mask: ClassMask, vendor: u32) -> Self {
+        Self(bindings::pci_device_id {
+            vendor,
+            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
@@ -410,6 +431,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.
+    /// Returns an error if the class code is not recognized.
+    pub fn pci_class(&self) -> Result<Class> {
+        // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`.
+        Class::new(unsafe { (*self.as_raw()).class })
+    }
 }
 
 impl Device<device::Bound> {
rust/kernel/pci/id.rs new file +247 -0

247 lines not on this page · raw message

Signature · 1 line
2.50.1
maintainer
#2 lore raw

Re: [PATCH v4 1/3] rust: pci: provide access to PCI Class and Class-related items

Quoted 14 lines · John Hubbard

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: TryFrom<u32> for Class
ClassMask: Full, ClassSubclass DeviceId::from_class_and_vendor() Device::pci_class()

Cc Danilo Krummrich <[email protected]>
Cc Alexandre Courbot <[email protected]>

rust/kernel/pci.rs pub fn resource_len(&self, bar: u32) -> Result<bindings::resource_size_t> { · line 440

Quoted 6 lines
rust/kernel/pci.rs +6 -0
+    /// Returns the PCI class as a `Class` struct.
+    /// Returns an error if the class code is not recognized.
+    pub fn pci_class(&self) -> Result<Class> {
+        // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`.
+        Class::new(unsafe { (*self.as_raw()).class })
+    }

I think all this turned out very nice!

One thing to reconsider would be whether we really want this to be fallible.

It's probably better to define a pci::Class::UNKNOWN and implement

	impl From<u32> for Class {
	    fn from(value: u32) -> Self {
	        match value {
	            $(x if x == Self::$variant.0 => Self::$variant,)+
	            _ => Self::UNKNOWN,
	        }
	    }
	}

instead.

#3 lore raw

Re: [PATCH v4 1/3] rust: pci: provide access to PCI Class and Class-related items

Quoted 37 lines · Danilo Krummrich
Quoted 14 lines · John Hubbard

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: TryFrom<u32> for Class
ClassMask: Full, ClassSubclass DeviceId::from_class_and_vendor() Device::pci_class()

Cc Danilo Krummrich <[email protected]>
Cc Alexandre Courbot <[email protected]>

rust/kernel/pci.rs pub fn resource_len(&self, bar: u32) -> Result<bindings::resource_size_t> { · line 440

Quoted 6 lines
rust/kernel/pci.rs +6 -0
+    /// Returns the PCI class as a `Class` struct.
+    /// Returns an error if the class code is not recognized.
+    pub fn pci_class(&self) -> Result<Class> {
+        // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`.
+        Class::new(unsafe { (*self.as_raw()).class })
+    }

I think all this turned out very nice!

One thing to reconsider would be whether we really want this to be fallible.

It's probably better to define a pci::Class::UNKNOWN and implement

	impl From<u32> for Class {
	    fn from(value: u32) -> Self {
	        match value {
	            $(x if x == Self::$variant.0 => Self::$variant,)+
	            _ => Self::UNKNOWN,
	        }
	    }
	}
Quoted 1 line

instead.

Yes, I went back and forth on whether Class and Vendor should be fallible, and finally settled on the wrong choice. haha :)

thanks,

Signature · 1 line
John Hubbard
#4 lore raw

PATCH v4 2/3 gpu: nova-core: avoid probing non-display/compute PCI functions

In reply to John Hubbard

NovaCore 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".

Cc Danilo Krummrich <[email protected]>
Cc Alexandre Courbot <[email protected]>
---
 drivers/gpu/nova-core/driver.rs | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)
drivers/gpu/nova-core/driver.rs +23 -5
index 274989ea1fb4..b60c9defa9d1 100644
@@ -1,6 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 
-use kernel::{auxiliary, bindings, c_str, device::Core, pci, prelude::*, sizes::SZ_16M, sync::Arc};
+use kernel::{
+    auxiliary, bindings, c_str, device::Core, pci, pci::Class, pci::ClassMask, prelude::*,
+    sizes::SZ_16M, sync::Arc,
+};
 
 use crate::gpu::Gpu;
 
@@ -18,10 +21,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,
+                bindings::PCI_VENDOR_ID_NVIDIA
+            ),
+            ()
+        ),
+        (
+            pci::DeviceId::from_class_and_vendor(
+                Class::DISPLAY_3D,
+                ClassMask::ClassSubclass,
+                bindings::PCI_VENDOR_ID_NVIDIA
+            ),
+            ()
+        ),
+    ]
 );
 
 impl pci::Driver for NovaCore {
Signature · 1 line
2.50.1
#5 lore raw

Re: [PATCH v4 2/3] gpu: nova-core: avoid probing non-display/compute PCI functions

Quoted 51 lines · John Hubbard

NovaCore 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".

Cc Danilo Krummrich <[email protected]>
Cc Alexandre Courbot <[email protected]>
---
 drivers/gpu/nova-core/driver.rs | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)
drivers/gpu/nova-core/driver.rs +10 -5
index 274989ea1fb4..b60c9defa9d1 100644
@@ -1,6 +1,9 @@
 // SPDX-License-Identifier: GPL-2.0
 
-use kernel::{auxiliary, bindings, c_str, device::Core, pci, prelude::*, sizes::SZ_16M, sync::Arc};
+use kernel::{
+    auxiliary, bindings, c_str, device::Core, pci, pci::Class, pci::ClassMask, prelude::*,
+    sizes::SZ_16M, sync::Arc,
+};
 
 use crate::gpu::Gpu;
 
@@ -18,10 +21,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,

drivers/gpu/nova-core/driver.rs pub(crate) struct NovaCore { · line 39

Quoted 10 lines
drivers/gpu/nova-core/driver.rs +10 -0
+                bindings::PCI_VENDOR_ID_NVIDIA
+            ),
+            ()
+        ),
+        (
+            pci::DeviceId::from_class_and_vendor(
+                Class::DISPLAY_3D,
+                ClassMask::ClassSubclass,
+                bindings::PCI_VENDOR_ID_NVIDIA
+            ),

This is making use of from_class_and_vendor, which is modified in the next patch, requiring to modify this part of the file again. How about switching this patch with 3/3 so we only modify the nova-core code once?

I also wonder if we want to merge 1/3 and (the current) 3/3, since 1/3 alone leaves from_class_and_vendor into some intermediate state that nobody will ever get a chance to use anyway, and one doesn't really make sense without the other. WDYT?

maintainer
#6 lore raw

Re: [PATCH v4 2/3] gpu: nova-core: avoid probing non-display/compute PCI functions

Quoted 3 lines · Alexandre Courbot

This is making use of from_class_and_vendor, which is modified in the next patch, requiring to modify this part of the file again. How about switching this patch with 3/3 so we only modify the nova-core code once?

I think that makes sense.

Quoted 4 lines

I also wonder if we want to merge 1/3 and (the current) 3/3, since 1/3 alone leaves from_class_and_vendor into some intermediate state that nobody will ever get a chance to use anyway, and one doesn't really make sense without the other. WDYT?

Let's not merge them please, the intermediate state is not that bad, currently we deal with raw integers for representing vendor IDs as well. So, patch 1 is an improvement even by itself.

#7 lore raw

Re: [PATCH v4 2/3] gpu: nova-core: avoid probing non-display/compute PCI functions

Quoted 6 lines · Danilo Krummrich
Quoted 3 lines · Alexandre Courbot

This is making use of from_class_and_vendor, which is modified in the next patch, requiring to modify this part of the file again. How about switching this patch with 3/3 so we only modify the nova-core code once?

I think that makes sense.

Sure.

Quoted 9 lines
Quoted 5 lines

I also wonder if we want to merge 1/3 and (the current) 3/3, since 1/3 alone leaves from_class_and_vendor into some intermediate state that nobody will ever get a chance to use anyway, and one doesn't really make sense without the other. WDYT?

Let's not merge them please, the intermediate state is not that bad, currently we deal with raw integers for representing vendor IDs as well. So, patch 1 is an improvement even by itself.

OK.

thanks,

Signature · 1 line
John Hubbard
#8 lore raw

PATCH v4 3/3 rust: pci: provide access to PCI Vendor values

In reply to John Hubbard

This allows callers to write Vendor::SOME_COMPANY instead of bindings::PCI_VENDOR_ID_SOME_COMPANY.

It also allows removing "use kernel::bindings" entirely from most of the affected files here.

Apply this to the various Rust for Linux callers who were previously using bindings::PCI_VENDOR_ID_*.

New APIs:
Vendor::SOME_COMPANY
Vendor::as_raw()
Vendor: TryFrom<u32> for Vendor DeviceId::from_class_and_vendor() now takes Vendor type

Cc Danilo Krummrich <[email protected]>
Cc Alexandre Courbot <[email protected]>
---
 drivers/gpu/nova-core/driver.rs       |  13 +-
 rust/kernel/pci.rs                    |  26 +-
 rust/kernel/pci/id.rs                 | 357 +++++++++++++++++++++++++-
 samples/rust/rust_dma.rs              |   6 +-
 samples/rust/rust_driver_auxiliary.rs |  12 +-
 samples/rust/rust_driver_pci.rs       |   9 +-
 6 files changed, 392 insertions(+), 31 deletions(-)
drivers/gpu/nova-core/driver.rs +9 -4
index b60c9defa9d1..5d23a91f51dd 100644
@@ -1,8 +1,13 @@
 // SPDX-License-Identifier: GPL-2.0
 
 use kernel::{
-    auxiliary, bindings, c_str, device::Core, pci, pci::Class, pci::ClassMask, prelude::*,
-    sizes::SZ_16M, sync::Arc,
+    auxiliary, c_str,
+    device::Core,
+    pci,
+    pci::{Class, ClassMask, Vendor},
+    prelude::*,
+    sizes::SZ_16M,
+    sync::Arc,
 };
 
 use crate::gpu::Gpu;
@@ -27,7 +32,7 @@ pub(crate) struct NovaCore {
             pci::DeviceId::from_class_and_vendor(
                 Class::DISPLAY_VGA,
                 ClassMask::ClassSubclass,
-                bindings::PCI_VENDOR_ID_NVIDIA
+                Vendor::NVIDIA
             ),
             ()
         ),
@@ -35,7 +40,7 @@ pub(crate) struct NovaCore {
             pci::DeviceId::from_class_and_vendor(
                 Class::DISPLAY_3D,
                 ClassMask::ClassSubclass,
-                bindings::PCI_VENDOR_ID_NVIDIA
+                Vendor::NVIDIA
             ),
             ()
         ),
rust/kernel/pci.rs +16 -10
index ee887d616320..c677e7f2c4c9 100644
@@ -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);
@@ -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(),
             device,
             subvendor: DeviceId::PCI_ANY_ID,
             subdevice: DeviceId::PCI_ANY_ID,
@@ -166,9 +166,13 @@ pub const fn from_class(class: u32, class_mask: u32) -> Self {
     ///
     /// 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).
-    pub const fn from_class_and_vendor(class: Class, class_mask: ClassMask, vendor: u32) -> Self {
+    pub const fn from_class_and_vendor(
+        class: Class,
+        class_mask: ClassMask,
+        vendor: Vendor,
+    ) -> Self {
         Self(bindings::pci_device_id {
-            vendor,
+            vendor: vendor.as_raw(),
             device: DeviceId::PCI_ANY_ID,
             subvendor: DeviceId::PCI_ANY_ID,
             subdevice: DeviceId::PCI_ANY_ID,
@@ -227,7 +231,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),
 ///             (),
 ///         )
 ///     ]
@@ -408,10 +412,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.
+    /// Returns an error if the vendor ID is not recognized.
+    pub fn vendor_id(&self) -> Result<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::try_from(vendor_id as u32)
     }
 
     /// Returns the PCI device ID.
rust/kernel/pci/id.rs +356 -1

372 lines not on this page · raw message

samples/rust/rust_dma.rs +1 -5
index c5e7cce68654..f3385c4a7e5b 100644
@@ -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
index f2a820683fc3..1191dd56e924 100644
@@ -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
index 606946ff4d7f..fe30a280cfa8 100644
@@ -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.50.1
maintainer
#9 lore raw

Re: [PATCH v4 3/3] rust: pci: provide access to PCI Vendor values

rust/kernel/pci.rs fn as_raw(&self) -> *mut bindings::pci_dev { · line 421

Quoted 11 lines · John Hubbard

impl Device {

rust/kernel/pci.rs +5 -3
-    /// Returns the PCI vendor ID.
-    pub fn vendor_id(&self) -> u16 {
+    /// Returns the PCI vendor ID as a validated Vendor.
+    /// Returns an error if the vendor ID is not recognized.
+    pub fn vendor_id(&self) -> Result<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::try_from(vendor_id as u32)
     }

Same as for Class, I think we just want Vendor::UNKNOWN.