Skip to main content

nv_redfish/
pcie_device.rs

1// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! PCIe devices
17//!
18
19use crate::hardware_id::HardwareIdRef;
20use crate::hardware_id::Manufacturer as HardwareIdManufacturer;
21use crate::hardware_id::Model as HardwareIdModel;
22use crate::hardware_id::PartNumber as HardwareIdPartNumber;
23use crate::hardware_id::SerialNumber as HardwareIdSerialNumber;
24use crate::schema::pcie_device::PcieDevice as PcieDeviceSchema;
25#[cfg(feature = "chassis")]
26use crate::schema::pcie_device_collection::PcieDeviceCollection as PcieDeviceCollectionSchema;
27#[cfg(feature = "chassis")]
28use crate::Error;
29#[cfg(feature = "chassis")]
30use crate::NvBmc;
31use crate::Resource;
32use crate::ResourceProvidesStatus;
33use crate::ResourceSchema;
34use crate::ResourceStatusSchema;
35use nv_redfish_core::Bmc;
36#[cfg(feature = "chassis")]
37use nv_redfish_core::NavProperty;
38use std::marker::PhantomData;
39use std::sync::Arc;
40use tagged_types::TaggedType;
41
42/// PCIe devices collection.
43///
44/// Provides functions to access collection members.
45#[cfg(feature = "chassis")]
46pub struct PcieDeviceCollection<B: Bmc> {
47    bmc: NvBmc<B>,
48    collection: Arc<PcieDeviceCollectionSchema>,
49}
50
51#[cfg(feature = "chassis")]
52impl<B: Bmc> PcieDeviceCollection<B> {
53    /// Create a new manager collection handle.
54    pub(crate) async fn new(
55        bmc: &NvBmc<B>,
56        nav: &NavProperty<PcieDeviceCollectionSchema>,
57    ) -> Result<Self, Error<B>> {
58        let collection = bmc.expand_property(nav).await?;
59        Ok(Self {
60            bmc: bmc.clone(),
61            collection,
62        })
63    }
64
65    /// List all managers available in this BMC.
66    ///
67    /// # Errors
68    ///
69    /// Returns an error if fetching manager data fails.
70    pub async fn members(&self) -> Result<Vec<PcieDevice<B>>, Error<B>> {
71        let mut members = Vec::new();
72        for m in &self.collection.members {
73            members.push(PcieDevice::new(&self.bmc, m).await?);
74        }
75        Ok(members)
76    }
77}
78
79#[doc(hidden)]
80pub enum PcieDeviceTag {}
81
82/// PCIe device manufacturer.
83pub type Manufacturer<T> = HardwareIdManufacturer<T, PcieDeviceTag>;
84
85/// PCIe device model.
86pub type Model<T> = HardwareIdModel<T, PcieDeviceTag>;
87
88/// PCIe device part number.
89pub type PartNumber<T> = HardwareIdPartNumber<T, PcieDeviceTag>;
90
91/// PCIe device serial number.
92pub type SerialNumber<T> = HardwareIdSerialNumber<T, PcieDeviceTag>;
93
94/// Firmware version of the PCIe device.
95pub type FirmwareVersion<T> = TaggedType<T, FirmwareVersionTag>;
96#[doc(hidden)]
97#[derive(tagged_types::Tag)]
98#[implement(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
99#[transparent(Debug, Display, Serialize, Deserialize)]
100#[capability(inner_access, cloned)]
101pub enum FirmwareVersionTag {}
102
103/// PCIe device.
104///
105/// Provides functions to access PCIe device data.
106pub struct PcieDevice<B: Bmc> {
107    data: Arc<PcieDeviceSchema>,
108    _marker: PhantomData<B>,
109}
110
111impl<B: Bmc> PcieDevice<B> {
112    /// Create a new log service handle.
113    #[cfg(feature = "chassis")]
114    pub(crate) async fn new(
115        bmc: &NvBmc<B>,
116        nav: &NavProperty<PcieDeviceSchema>,
117    ) -> Result<Self, Error<B>> {
118        nav.get(bmc.as_ref())
119            .await
120            .map_err(crate::Error::Bmc)
121            .map(|data| Self {
122                data,
123                _marker: PhantomData,
124            })
125    }
126
127    /// Get the raw schema data for this PCIe device.
128    #[must_use]
129    pub fn raw(&self) -> Arc<PcieDeviceSchema> {
130        self.data.clone()
131    }
132
133    /// Get hardware identifier of the PCIe device.
134    #[must_use]
135    pub fn hardware_id(&self) -> HardwareIdRef<'_, PcieDeviceTag> {
136        HardwareIdRef {
137            manufacturer: self
138                .data
139                .manufacturer
140                .as_ref()
141                .and_then(Option::as_deref)
142                .map(Manufacturer::new),
143            model: self
144                .data
145                .model
146                .as_ref()
147                .and_then(Option::as_deref)
148                .map(Model::new),
149            part_number: self
150                .data
151                .part_number
152                .as_ref()
153                .and_then(Option::as_deref)
154                .map(PartNumber::new),
155            serial_number: self
156                .data
157                .serial_number
158                .as_ref()
159                .and_then(Option::as_deref)
160                .map(SerialNumber::new),
161        }
162    }
163
164    /// The version of firmware for this PCIe device.
165    #[must_use]
166    pub fn firmware_version(&self) -> Option<FirmwareVersion<&str>> {
167        self.data
168            .firmware_version
169            .as_ref()
170            .and_then(Option::as_ref)
171            .map(String::as_str)
172            .map(FirmwareVersion::new)
173    }
174}
175
176impl<B: Bmc> Resource for PcieDevice<B> {
177    fn resource_ref(&self) -> &ResourceSchema {
178        &self.data.as_ref().base
179    }
180}
181
182impl<B: Bmc> ResourceProvidesStatus for PcieDevice<B> {
183    fn resource_status_ref(&self) -> Option<&ResourceStatusSchema> {
184        self.data.status.as_ref()
185    }
186}