xref: /linux/rust/kernel/pci.rs (revision d4ffc4896b3e709c6a5d7e5a78a1fddfc774fa25)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 //! Abstractions for the PCI bus.
4 //!
5 //! C header: [`include/linux/pci.h`](srctree/include/linux/pci.h)
6 
7 use crate::{
8     bindings,
9     container_of,
10     device,
11     device_id::{
12         RawDeviceId,
13         RawDeviceIdIndex, //
14     },
15     driver,
16     error::{
17         from_result,
18         to_result, //
19     },
20     prelude::*,
21     str::CStr,
22     types::Opaque,
23     ThisModule, //
24 };
25 use core::{
26     marker::PhantomData,
27     mem::offset_of,
28     num::NonZero,
29     ptr::{
30         addr_of_mut,
31         NonNull, //
32     },
33 };
34 
35 mod cap;
36 mod id;
37 mod io;
38 mod irq;
39 
40 pub use self::cap::{
41     ExtCapId,
42     ExtCapability,
43     ExtSriovCapability,
44     ExtSriovRegs,
45     ExtSriovVfBar, //
46 };
47 pub use self::id::{
48     Class,
49     ClassMask,
50     Vendor, //
51 };
52 pub use self::io::{
53     Bar,
54     ConfigSpace,
55     ConfigSpaceSize,
56     DevresBar,
57     Extended,
58     Normal, //
59 };
60 pub use self::irq::{
61     IrqType,
62     IrqTypes,
63     IrqVector,
64     IrqVectorRegistration, //
65 };
66 
67 /// An adapter for the registration of PCI drivers.
68 pub struct Adapter<T: Driver>(T);
69 
70 // SAFETY:
71 // - `bindings::pci_driver` is a C type declared as `repr(C)`.
72 // - `T::Data` is the type of the driver's device private data.
73 // - `struct pci_driver` embeds a `struct device_driver`.
74 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
75 unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
76     type DriverType = bindings::pci_driver;
77     type DriverData<'bound> = T::Data<'bound>;
78     const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
79 }
80 
81 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
82 // a preceding call to `register` has been successful.
83 unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
84     unsafe fn register(
85         pdrv: &Opaque<Self::DriverType>,
86         name: &'static CStr,
87         module: &'static ThisModule,
88     ) -> Result {
89         // SAFETY: It's safe to set the fields of `struct pci_driver` on initialization.
90         unsafe {
91             (*pdrv.get()).name = name.as_char_ptr();
92             (*pdrv.get()).probe = Some(Self::probe_callback);
93             (*pdrv.get()).remove = Some(Self::remove_callback);
94             (*pdrv.get()).id_table = T::ID_TABLE.as_ptr();
95         }
96 
97         // SAFETY: `pdrv` is guaranteed to be a valid `DriverType`.
98         to_result(unsafe {
99             bindings::__pci_register_driver(pdrv.get(), module.as_ptr(), name.as_char_ptr())
100         })
101     }
102 
103     unsafe fn unregister(pdrv: &Opaque<Self::DriverType>) {
104         // SAFETY: `pdrv` is guaranteed to be a valid `DriverType`.
105         unsafe { bindings::pci_unregister_driver(pdrv.get()) }
106     }
107 }
108 
109 impl<T: Driver> Adapter<T> {
110     extern "C" fn probe_callback(
111         pdev: *mut bindings::pci_dev,
112         id: *const bindings::pci_device_id,
113     ) -> c_int {
114         // SAFETY: The PCI bus only ever calls the probe callback with a valid pointer to a
115         // `struct pci_dev`.
116         //
117         // INVARIANT: `pdev` is valid for the duration of `probe_callback()`.
118         let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal<'_>>>() };
119 
120         // SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `struct pci_device_id` and
121         // does not add additional invariants, so it's safe to transmute.
122         let id = unsafe { &*id.cast::<DeviceId>() };
123 
124         // SAFETY: `id` comes from `T::ID_TABLE` which is of type `IdArray<_, T::IdInfo>` or
125         // `pci_device_id_any` which has 0 as driver_data. It can also come from dynamic IDs, which
126         // will ensure that `driver_data` exists in `T::ID_TABLE`.
127         let info = unsafe { id.info_unchecked_opt::<T::IdInfo>() };
128 
129         from_result(|| {
130             let data = T::probe(pdev, info);
131 
132             pdev.as_ref().set_drvdata(data)?;
133             Ok(0)
134         })
135     }
136 
137     extern "C" fn remove_callback(pdev: *mut bindings::pci_dev) {
138         // SAFETY: The PCI bus only ever calls the remove callback with a valid pointer to a
139         // `struct pci_dev`.
140         //
141         // INVARIANT: `pdev` is valid for the duration of `remove_callback()`.
142         let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal<'_>>>() };
143 
144         // SAFETY: `remove_callback` is only ever called after a successful call to
145         // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
146         // and stored a `Pin<KBox<T::Data<'_>>>`.
147         let data = unsafe { pdev.as_ref().drvdata_borrow::<T::Data<'_>>() };
148 
149         T::unbind(pdev, data);
150     }
151 }
152 
153 /// Declares a kernel module that exposes a single PCI driver.
154 ///
155 /// # Examples
156 ///
157 ///```ignore
158 /// kernel::module_pci_driver! {
159 ///     type: MyDriver,
160 ///     name: "Module name",
161 ///     authors: ["Author name"],
162 ///     description: "Description",
163 ///     license: "GPL v2",
164 /// }
165 ///```
166 #[macro_export]
167 macro_rules! module_pci_driver {
168 ($($f:tt)*) => {
169     $crate::module_driver!(<T>, $crate::pci::Adapter<T>, { $($f)* });
170 };
171 }
172 
173 /// Abstraction for the PCI device ID structure ([`struct pci_device_id`]).
174 ///
175 /// [`struct pci_device_id`]: https://docs.kernel.org/PCI/pci.html#c.pci_device_id
176 #[repr(transparent)]
177 #[derive(Clone, Copy)]
178 pub struct DeviceId(bindings::pci_device_id);
179 
180 impl DeviceId {
181     const PCI_ANY_ID: u32 = !0;
182 
183     /// Equivalent to C's `PCI_DEVICE` macro.
184     ///
185     /// Create a new `pci::DeviceId` from a vendor and device ID.
186     #[inline]
187     pub const fn from_id(vendor: Vendor, device: u32) -> Self {
188         Self(bindings::pci_device_id {
189             vendor: vendor.as_raw() as u32,
190             device,
191             subvendor: DeviceId::PCI_ANY_ID,
192             subdevice: DeviceId::PCI_ANY_ID,
193             class: 0,
194             class_mask: 0,
195             driver_data: 0,
196             override_only: 0,
197         })
198     }
199 
200     /// Equivalent to C's `PCI_DEVICE_CLASS` macro.
201     ///
202     /// Create a new `pci::DeviceId` from a class number and mask.
203     #[inline]
204     pub const fn from_class(class: u32, class_mask: u32) -> Self {
205         Self(bindings::pci_device_id {
206             vendor: DeviceId::PCI_ANY_ID,
207             device: DeviceId::PCI_ANY_ID,
208             subvendor: DeviceId::PCI_ANY_ID,
209             subdevice: DeviceId::PCI_ANY_ID,
210             class,
211             class_mask,
212             driver_data: 0,
213             override_only: 0,
214         })
215     }
216 
217     /// Create a new [`DeviceId`] from a class number, mask, and specific vendor.
218     ///
219     /// This is more targeted than [`DeviceId::from_class`]: in addition to matching by [`Vendor`],
220     /// it also matches the PCI [`Class`] (up to the entire 24 bits, depending on the
221     /// [`ClassMask`]).
222     #[inline]
223     pub const fn from_class_and_vendor(
224         class: Class,
225         class_mask: ClassMask,
226         vendor: Vendor,
227     ) -> Self {
228         Self(bindings::pci_device_id {
229             vendor: vendor.as_raw() as u32,
230             device: DeviceId::PCI_ANY_ID,
231             subvendor: DeviceId::PCI_ANY_ID,
232             subdevice: DeviceId::PCI_ANY_ID,
233             class: class.as_raw(),
234             class_mask: class_mask.as_raw(),
235             driver_data: 0,
236             override_only: 0,
237         })
238     }
239 }
240 
241 // SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `pci_device_id` and does not add
242 // additional invariants, so it's safe to transmute to `RawType`.
243 unsafe impl RawDeviceId for DeviceId {
244     type RawType = bindings::pci_device_id;
245 }
246 
247 // SAFETY: `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field.
248 unsafe impl RawDeviceIdIndex for DeviceId {
249     const DRIVER_DATA_OFFSET: usize = core::mem::offset_of!(bindings::pci_device_id, driver_data);
250 }
251 
252 /// `IdTable` type for PCI.
253 pub type IdTable<T> = &'static dyn kernel::device_id::IdTable<DeviceId, T>;
254 
255 /// Create a PCI `IdTable` with its alias for modpost.
256 #[macro_export]
257 macro_rules! pci_device_table {
258     ($($tt:tt)*) => {
259         $crate::module_device_table!("pci", $crate::pci::DeviceId, $($tt)*);
260     };
261 }
262 
263 /// The PCI driver trait.
264 ///
265 /// # Examples
266 ///
267 ///```
268 /// # use kernel::{bindings, device::Core, pci};
269 ///
270 /// struct MyDriver;
271 ///
272 /// kernel::pci_device_table!(
273 ///     PCI_TABLE,
274 ///     <MyDriver as pci::Driver>::IdInfo,
275 ///     [
276 ///         (
277 ///             pci::DeviceId::from_id(pci::Vendor::REDHAT, bindings::PCI_ANY_ID as u32),
278 ///             (),
279 ///         )
280 ///     ]
281 /// );
282 ///
283 /// impl pci::Driver for MyDriver {
284 ///     type IdInfo = ();
285 ///     type Data<'bound> = Self;
286 ///     const ID_TABLE: pci::IdTable<Self::IdInfo> = &PCI_TABLE;
287 ///
288 ///     fn probe<'bound>(
289 ///         _pdev: &'bound pci::Device<Core<'_>>,
290 ///         _id_info: Option<&'bound Self::IdInfo>,
291 ///     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
292 ///         Err(ENODEV)
293 ///     }
294 /// }
295 ///```
296 /// Drivers must implement this trait in order to get a PCI driver registered. Please refer to the
297 /// `Adapter` documentation for an example.
298 pub trait Driver {
299     /// The type holding information about each device id supported by the driver.
300     // TODO: Use `associated_type_defaults` once stabilized:
301     //
302     // ```
303     // type IdInfo: 'static = ();
304     // ```
305     type IdInfo: 'static;
306 
307     /// The type of the driver's bus device private data.
308     type Data<'bound>: Send + 'bound;
309 
310     /// The table of device ids supported by the driver.
311     const ID_TABLE: IdTable<Self::IdInfo>;
312 
313     /// PCI driver probe.
314     ///
315     /// Called when a new pci device is added or discovered. Implementers should
316     /// attempt to initialize the device here.
317     fn probe<'bound>(
318         dev: &'bound Device<device::Core<'_>>,
319         id_info: Option<&'bound Self::IdInfo>,
320     ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound;
321 
322     /// PCI driver unbind.
323     ///
324     /// Called when a [`Device`] is unbound from its bound [`Driver`]. Implementing this callback
325     /// is optional.
326     ///
327     /// This callback serves as a place for drivers to perform teardown operations that require a
328     /// `&Device<Core>` or `&Device<Bound>` reference. For instance, drivers may try to perform I/O
329     /// operations to gracefully tear down the device.
330     ///
331     /// Otherwise, release operations for driver resources should be performed in `Drop`.
332     fn unbind<'bound>(dev: &'bound Device<device::Core<'_>>, this: Pin<&Self::Data<'bound>>) {
333         let _ = (dev, this);
334     }
335 }
336 
337 /// The PCI device representation.
338 ///
339 /// This structure represents the Rust abstraction for a C `struct pci_dev`. The implementation
340 /// abstracts the usage of an already existing C `struct pci_dev` within Rust code that we get
341 /// passed from the C side.
342 ///
343 /// # Invariants
344 ///
345 /// A [`Device`] instance represents a valid `struct pci_dev` created by the C portion of the
346 /// kernel.
347 #[repr(transparent)]
348 pub struct Device<Ctx: device::DeviceContext = device::Normal>(
349     Opaque<bindings::pci_dev>,
350     PhantomData<Ctx>,
351 );
352 
353 impl<Ctx: device::DeviceContext> Device<Ctx> {
354     #[inline]
355     fn as_raw(&self) -> *mut bindings::pci_dev {
356         self.0.get()
357     }
358 }
359 
360 impl Device {
361     /// Returns the PCI vendor ID as [`Vendor`].
362     ///
363     /// # Examples
364     ///
365     /// ```
366     /// # use kernel::{device::Core, pci::{self, Vendor}, prelude::*};
367     /// fn log_device_info(pdev: &pci::Device<Core<'_>>) -> Result {
368     ///     // Get an instance of `Vendor`.
369     ///     let vendor = pdev.vendor_id();
370     ///     dev_info!(
371     ///         pdev,
372     ///         "Device: Vendor={}, Device=0x{:x}\n",
373     ///         vendor,
374     ///         pdev.device_id()
375     ///     );
376     ///     Ok(())
377     /// }
378     /// ```
379     #[inline]
380     pub fn vendor_id(&self) -> Vendor {
381         // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`.
382         let vendor_id = unsafe { (*self.as_raw()).vendor };
383         Vendor::from_raw(vendor_id)
384     }
385 
386     /// Returns the PCI device ID.
387     #[inline]
388     pub fn device_id(&self) -> u16 {
389         // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a
390         // `struct pci_dev`.
391         unsafe { (*self.as_raw()).device }
392     }
393 
394     /// Returns the PCI revision ID.
395     #[inline]
396     pub fn revision_id(&self) -> u8 {
397         // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a
398         // `struct pci_dev`.
399         unsafe { (*self.as_raw()).revision }
400     }
401 
402     /// Returns the PCI bus device/function.
403     #[inline]
404     pub fn dev_id(&self) -> u16 {
405         // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a
406         // `struct pci_dev`.
407         unsafe { bindings::pci_dev_id(self.as_raw()) }
408     }
409 
410     /// Returns the PCI subsystem vendor ID.
411     #[inline]
412     pub fn subsystem_vendor_id(&self) -> u16 {
413         // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a
414         // `struct pci_dev`.
415         unsafe { (*self.as_raw()).subsystem_vendor }
416     }
417 
418     /// Returns the PCI subsystem device ID.
419     #[inline]
420     pub fn subsystem_device_id(&self) -> u16 {
421         // SAFETY: By its type invariant `self.as_raw` is always a valid pointer to a
422         // `struct pci_dev`.
423         unsafe { (*self.as_raw()).subsystem_device }
424     }
425 
426     /// Returns the start of the given PCI BAR resource.
427     pub fn resource_start(&self, bar: u32) -> Result<bindings::resource_size_t> {
428         if !Bar::index_is_valid(bar) {
429             return Err(EINVAL);
430         }
431 
432         // SAFETY:
433         // - `bar` is a valid bar number, as guaranteed by the above call to `Bar::index_is_valid`,
434         // - by its type invariant `self.as_raw` is always a valid pointer to a `struct pci_dev`.
435         Ok(unsafe { bindings::pci_resource_start(self.as_raw(), bar.try_into()?) })
436     }
437 
438     /// Returns the size of the given PCI BAR resource.
439     pub fn resource_len(&self, bar: u32) -> Result<bindings::resource_size_t> {
440         if !Bar::index_is_valid(bar) {
441             return Err(EINVAL);
442         }
443 
444         // SAFETY:
445         // - `bar` is a valid bar number, as guaranteed by the above call to `Bar::index_is_valid`,
446         // - by its type invariant `self.as_raw` is always a valid pointer to a `struct pci_dev`.
447         Ok(unsafe { bindings::pci_resource_len(self.as_raw(), bar.try_into()?) })
448     }
449 
450     /// Returns the PCI class as a `Class` struct.
451     #[inline]
452     pub fn pci_class(&self) -> Class {
453         // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`.
454         Class::from_raw(unsafe { (*self.as_raw()).class })
455     }
456 }
457 
458 impl<'a> Device<device::Core<'a>> {
459     /// Returns the total number of VFs, or [`None`] if SR-IOV is not available.
460     #[inline]
461     pub fn sriov_get_totalvfs(&self) -> Option<NonZero<u16>> {
462         // SAFETY: `self.as_raw()` is a valid pointer to a `struct pci_dev`.
463         let total_vfs = unsafe { bindings::pci_sriov_get_totalvfs(self.as_raw()) };
464 
465         // CAST: The C function returns `unsigned int`, but the value originates
466         // from TotalVFs/driver_max_VFs (which are defined as `u16`), so this cast
467         // cannot truncate.
468         NonZero::new(total_vfs as u16)
469     }
470 
471     /// Enable memory resources for this device.
472     pub fn enable_device_mem(&self) -> Result {
473         // SAFETY: `self.as_raw` is guaranteed to be a pointer to a valid `struct pci_dev`.
474         to_result(unsafe { bindings::pci_enable_device_mem(self.as_raw()) })
475     }
476 
477     /// Enable bus-mastering for this device.
478     #[inline]
479     pub fn set_master(&self) {
480         // SAFETY: `self.as_raw` is guaranteed to be a pointer to a valid `struct pci_dev`.
481         unsafe { bindings::pci_set_master(self.as_raw()) };
482     }
483 }
484 
485 // SAFETY: `pci::Device` is a transparent wrapper of `struct pci_dev`.
486 // The offset is guaranteed to point to a valid device field inside `pci::Device`.
487 unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for Device<Ctx> {
488     const OFFSET: usize = offset_of!(bindings::pci_dev, dev);
489 }
490 
491 // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic
492 // argument.
493 kernel::impl_device_context_deref!(unsafe { Device });
494 kernel::impl_device_context_into_aref!(Device);
495 
496 impl<'a> crate::dma::Device<'a> for Device<device::Core<'a>> {}
497 
498 // SAFETY: Instances of `Device` are always reference-counted.
499 unsafe impl crate::sync::aref::AlwaysRefCounted for Device {
500     #[inline]
501     fn inc_ref(&self) {
502         // SAFETY: The existence of a shared reference guarantees that the refcount is non-zero.
503         unsafe { bindings::pci_dev_get(self.as_raw()) };
504     }
505 
506     #[inline]
507     unsafe fn dec_ref(obj: NonNull<Self>) {
508         // SAFETY: The safety requirements guarantee that the refcount is non-zero.
509         unsafe { bindings::pci_dev_put(obj.cast().as_ptr()) }
510     }
511 }
512 
513 impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
514     fn as_ref(&self) -> &device::Device<Ctx> {
515         // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid
516         // `struct pci_dev`.
517         let dev = unsafe { addr_of_mut!((*self.as_raw()).dev) };
518 
519         // SAFETY: `dev` points to a valid `struct device`.
520         unsafe { device::Device::from_raw(dev) }
521     }
522 }
523 
524 impl<Ctx: device::DeviceContext> TryFrom<&device::Device<Ctx>> for &Device<Ctx> {
525     type Error = kernel::error::Error;
526 
527     fn try_from(dev: &device::Device<Ctx>) -> Result<Self, Self::Error> {
528         // SAFETY: By the type invariant of `Device`, `dev.as_raw()` is a valid pointer to a
529         // `struct device`.
530         if !unsafe { bindings::dev_is_pci(dev.as_raw()) } {
531             return Err(EINVAL);
532         }
533 
534         // SAFETY: We've just verified that the bus type of `dev` equals `bindings::pci_bus_type`,
535         // hence `dev` must be embedded in a valid `struct pci_dev` as guaranteed by the
536         // corresponding C code.
537         let pdev = unsafe { container_of!(dev.as_raw(), bindings::pci_dev, dev) };
538 
539         // SAFETY: `pdev` is a valid pointer to a `struct pci_dev`.
540         Ok(unsafe { &*pdev.cast() })
541     }
542 }
543 
544 // SAFETY: A `Device` is always reference-counted and can be released from any thread.
545 unsafe impl Send for Device {}
546 
547 // SAFETY: `Device` can be shared among threads because all methods of `Device`
548 // (i.e. `Device<Normal>) are thread safe.
549 unsafe impl Sync for Device {}
550 
551 // SAFETY: Same as `Device<Normal>` -- the underlying `struct pci_dev` is the same;
552 // `Bound` is a zero-sized type-state marker that does not affect thread safety.
553 unsafe impl Sync for Device<device::Bound> {}
554