[RFC 5/6] rust: pci: add helper to find extended capability

Zhi Wang posted 6 patches 2 months, 1 week ago
There is a newer version of this series
[RFC 5/6] rust: pci: add helper to find extended capability
Posted by Zhi Wang 2 months, 1 week ago
Add a safe wrapper for `pci_find_ext_capability()` that returns an
`Option<u16>` indicating the offset of a given PCIe extended capability.

This allows Rust drivers to query extended capabilities without dealing
with raw pointers or integer return codes. The method returns `None`
when the capability is not present.

Signed-off-by: Zhi Wang <zhiw@nvidia.com>
---
 rust/kernel/pci.rs | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/rust/kernel/pci.rs b/rust/kernel/pci.rs
index 2f94b370fc99..5d9c5eef5c85 100644
--- a/rust/kernel/pci.rs
+++ b/rust/kernel/pci.rs
@@ -477,6 +477,13 @@ pub fn cfg_size(&self) -> i32 {
         // SAFETY: `self.as_raw` is a valid pointer to a `struct pci_dev`.
         unsafe { (*self.as_raw()).cfg_size }
     }
+
+    /// Find the extended capability
+    pub fn find_ext_capability(&self, cap: i32) -> Option<u16> {
+        // SAFETY: `self.as_raw()` is a valid pointer to a `struct pci_dev`.
+        let offset = unsafe { bindings::pci_find_ext_capability(self.as_raw(), cap) };
+        (offset != 0).then(|| offset as u16)
+    }
 }
 
 impl Device<device::Bound> {
-- 
2.47.3