diff options
Diffstat (limited to 'rust/kernel/pci.rs')
| -rw-r--r-- | rust/kernel/pci.rs | 100 | 
1 files changed, 60 insertions, 40 deletions
| diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs index 8435f8132e38..887ee611b553 100644 --- a/rust/kernel/pci.rs +++ b/rust/kernel/pci.rs @@ -5,16 +5,15 @@  //! C header: [`include/linux/pci.h`](srctree/include/linux/pci.h)  use crate::{ -    alloc::flags::*,      bindings, container_of, device, -    device_id::RawDeviceId, +    device_id::{RawDeviceId, RawDeviceIdIndex},      devres::Devres,      driver, -    error::{to_result, Result}, +    error::{from_result, to_result, Result},      io::Io,      io::IoRaw,      str::CStr, -    types::{ARef, ForeignOwnable, Opaque}, +    types::{ARef, Opaque},      ThisModule,  };  use core::{ @@ -66,41 +65,40 @@ impl<T: Driver + 'static> Adapter<T> {          // `struct pci_dev`.          //          // INVARIANT: `pdev` is valid for the duration of `probe_callback()`. -        let pdev = unsafe { &*pdev.cast::<Device<device::Core>>() }; +        let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal>>() }; -        // SAFETY: `DeviceId` is a `#[repr(transparent)` wrapper of `struct pci_device_id` and +        // SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `struct pci_device_id` and          // does not add additional invariants, so it's safe to transmute.          let id = unsafe { &*id.cast::<DeviceId>() };          let info = T::ID_TABLE.info(id.index()); -        match T::probe(pdev, info) { -            Ok(data) => { -                // Let the `struct pci_dev` own a reference of the driver's private data. -                // SAFETY: By the type invariant `pdev.as_raw` returns a valid pointer to a -                // `struct pci_dev`. -                unsafe { bindings::pci_set_drvdata(pdev.as_raw(), data.into_foreign() as _) }; -            } -            Err(err) => return Error::to_errno(err), -        } +        from_result(|| { +            let data = T::probe(pdev, info)?; -        0 +            pdev.as_ref().set_drvdata(data); +            Ok(0) +        })      }      extern "C" fn remove_callback(pdev: *mut bindings::pci_dev) {          // SAFETY: The PCI bus only ever calls the remove callback with a valid pointer to a          // `struct pci_dev`. -        let ptr = unsafe { bindings::pci_get_drvdata(pdev) }.cast(); +        // +        // INVARIANT: `pdev` is valid for the duration of `remove_callback()`. +        let pdev = unsafe { &*pdev.cast::<Device<device::CoreInternal>>() };          // SAFETY: `remove_callback` is only ever called after a successful call to -        // `probe_callback`, hence it's guaranteed that `ptr` points to a valid and initialized -        // `KBox<T>` pointer created through `KBox::into_foreign`. -        let _ = unsafe { KBox::<T>::from_foreign(ptr) }; +        // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called +        // and stored a `Pin<KBox<T>>`. +        let data = unsafe { pdev.as_ref().drvdata_obtain::<Pin<KBox<T>>>() }; + +        T::unbind(pdev, data.as_ref());      }  }  /// Declares a kernel module that exposes a single PCI driver.  /// -/// # Example +/// # Examples  ///  ///```ignore  /// kernel::module_pci_driver! { @@ -161,17 +159,18 @@ impl DeviceId {      }  } -// SAFETY: -// * `DeviceId` is a `#[repr(transparent)` wrapper of `pci_device_id` and does not add -//   additional invariants, so it's safe to transmute to `RawType`. -// * `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field. +// SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `pci_device_id` and does not add +// additional invariants, so it's safe to transmute to `RawType`.  unsafe impl RawDeviceId for DeviceId {      type RawType = bindings::pci_device_id; +} +// SAFETY: `DRIVER_DATA_OFFSET` is the offset to the `driver_data` field. +unsafe impl RawDeviceIdIndex for DeviceId {      const DRIVER_DATA_OFFSET: usize = core::mem::offset_of!(bindings::pci_device_id, driver_data);      fn index(&self) -> usize { -        self.0.driver_data as _ +        self.0.driver_data      }  } @@ -194,7 +193,7 @@ macro_rules! pci_device_table {  /// The PCI driver trait.  /// -/// # Example +/// # Examples  ///  ///```  /// # use kernel::{bindings, device::Core, pci}; @@ -206,7 +205,10 @@ macro_rules! pci_device_table {  ///     MODULE_PCI_TABLE,  ///     <MyDriver as pci::Driver>::IdInfo,  ///     [ -///         (pci::DeviceId::from_id(bindings::PCI_VENDOR_ID_REDHAT, bindings::PCI_ANY_ID as _), ()) +///         ( +///             pci::DeviceId::from_id(bindings::PCI_VENDOR_ID_REDHAT, bindings::PCI_ANY_ID as u32), +///             (), +///         )  ///     ]  /// );  /// @@ -241,6 +243,20 @@ pub trait Driver: Send {      /// Called when a new platform device is added or discovered.      /// Implementers should attempt to initialize the device here.      fn probe(dev: &Device<device::Core>, id_info: &Self::IdInfo) -> Result<Pin<KBox<Self>>>; + +    /// Platform driver unbind. +    /// +    /// Called when a [`Device`] is unbound from its bound [`Driver`]. Implementing this callback +    /// is optional. +    /// +    /// This callback serves as a place for drivers to perform teardown operations that require a +    /// `&Device<Core>` or `&Device<Bound>` reference. For instance, drivers may try to perform I/O +    /// operations to gracefully tear down the device. +    /// +    /// Otherwise, release operations for driver resources should be performed in `Self::drop`. +    fn unbind(dev: &Device<device::Core>, this: Pin<&Self>) { +        let _ = (dev, this); +    }  }  /// The PCI device representation. @@ -251,7 +267,8 @@ pub trait Driver: Send {  ///  /// # Invariants  /// -/// A [`Device`] instance represents a valid `struct device` created by the C portion of the kernel. +/// A [`Device`] instance represents a valid `struct pci_dev` created by the C portion of the +/// kernel.  #[repr(transparent)]  pub struct Device<Ctx: device::DeviceContext = device::Normal>(      Opaque<bindings::pci_dev>, @@ -330,7 +347,7 @@ impl<const SIZE: usize> Bar<SIZE> {          // `ioptr` is valid by the safety requirements.          // `num` is valid by the safety requirements.          unsafe { -            bindings::pci_iounmap(pdev.as_raw(), ioptr as _); +            bindings::pci_iounmap(pdev.as_raw(), ioptr as *mut kernel::ffi::c_void);              bindings::pci_release_region(pdev.as_raw(), num);          }      } @@ -398,19 +415,20 @@ impl Device {  impl Device<device::Bound> {      /// Mapps an entire PCI-BAR after performing a region-request on it. I/O operation bound checks      /// can be performed on compile time for offsets (plus the requested type size) < SIZE. -    pub fn iomap_region_sized<const SIZE: usize>( -        &self, +    pub fn iomap_region_sized<'a, const SIZE: usize>( +        &'a self,          bar: u32, -        name: &CStr, -    ) -> Result<Devres<Bar<SIZE>>> { -        let bar = Bar::<SIZE>::new(self, bar, name)?; -        let devres = Devres::new(self.as_ref(), bar, GFP_KERNEL)?; - -        Ok(devres) +        name: &'a CStr, +    ) -> impl PinInit<Devres<Bar<SIZE>>, Error> + 'a { +        Devres::new(self.as_ref(), Bar::<SIZE>::new(self, bar, name))      }      /// Mapps an entire PCI-BAR after performing a region-request on it. -    pub fn iomap_region(&self, bar: u32, name: &CStr) -> Result<Devres<Bar>> { +    pub fn iomap_region<'a>( +        &'a self, +        bar: u32, +        name: &'a CStr, +    ) -> impl PinInit<Devres<Bar>, Error> + 'a {          self.iomap_region_sized::<0>(bar, name)      }  } @@ -434,6 +452,8 @@ impl Device<device::Core> {  kernel::impl_device_context_deref!(unsafe { Device });  kernel::impl_device_context_into_aref!(Device); +impl crate::dma::Device for Device<device::Core> {} +  // SAFETY: Instances of `Device` are always reference-counted.  unsafe impl crate::types::AlwaysRefCounted for Device {      fn inc_ref(&self) { @@ -454,7 +474,7 @@ impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {          let dev = unsafe { addr_of_mut!((*self.as_raw()).dev) };          // SAFETY: `dev` points to a valid `struct device`. -        unsafe { device::Device::as_ref(dev) } +        unsafe { device::Device::from_raw(dev) }      }  } | 
