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::redfish::pcie_device::PcieDevice as PcieDeviceSchema;
25use crate::schema::redfish::pcie_device_collection::PcieDeviceCollection as PcieDeviceCollectionSchema;
26use crate::Error;
27use crate::NvBmc;
28use crate::Resource;
29use crate::ResourceProvidesStatus;
30use crate::ResourceSchema;
31use crate::ResourceStatusSchema;
32use nv_redfish_core::Bmc;
33use nv_redfish_core::NavProperty;
34use std::marker::PhantomData;
35use std::sync::Arc;
36use tagged_types::TaggedType;
37
38/// PCIe devices collection.
39///
40/// Provides functions to access collection members.
41pub struct PcieDeviceCollection<B: Bmc> {
42    bmc: NvBmc<B>,
43    collection: Arc<PcieDeviceCollectionSchema>,
44}
45
46impl<B: Bmc> PcieDeviceCollection<B> {
47    /// Create a new manager collection handle.
48    pub(crate) async fn new(
49        bmc: &NvBmc<B>,
50        nav: &NavProperty<PcieDeviceCollectionSchema>,
51    ) -> Result<Self, Error<B>> {
52        let collection = bmc.expand_property(nav).await?;
53        Ok(Self {
54            bmc: bmc.clone(),
55            collection,
56        })
57    }
58
59    /// List all managers available in this BMC.
60    ///
61    /// # Errors
62    ///
63    /// Returns an error if fetching manager data fails.
64    pub async fn members(&self) -> Result<Vec<PcieDevice<B>>, Error<B>> {
65        let mut members = Vec::new();
66        for m in &self.collection.members {
67            members.push(PcieDevice::new(&self.bmc, m).await?);
68        }
69        Ok(members)
70    }
71}
72
73#[doc(hidden)]
74pub enum PcieDeviceTag {}
75
76/// PCIe device manufacturer.
77pub type Manufacturer<T> = HardwareIdManufacturer<T, PcieDeviceTag>;
78
79/// PCIe device model.
80pub type Model<T> = HardwareIdModel<T, PcieDeviceTag>;
81
82/// PCIe device part number.
83pub type PartNumber<T> = HardwareIdPartNumber<T, PcieDeviceTag>;
84
85/// PCIe device serial number.
86pub type SerialNumber<T> = HardwareIdSerialNumber<T, PcieDeviceTag>;
87
88/// Firmware version of the PCIe device.
89pub type FirmwareVersion<T> = TaggedType<T, FirmwareVersionTag>;
90#[doc(hidden)]
91#[derive(tagged_types::Tag)]
92#[implement(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
93#[transparent(Debug, Display, Serialize, Deserialize)]
94#[capability(inner_access, cloned)]
95pub enum FirmwareVersionTag {}
96
97/// PCIe device.
98///
99/// Provides functions to access PCIe device data.
100pub struct PcieDevice<B: Bmc> {
101    data: Arc<PcieDeviceSchema>,
102    _marker: PhantomData<B>,
103}
104
105impl<B: Bmc> PcieDevice<B> {
106    /// Create a new log service handle.
107    pub(crate) async fn new(
108        bmc: &NvBmc<B>,
109        nav: &NavProperty<PcieDeviceSchema>,
110    ) -> Result<Self, Error<B>> {
111        nav.get(bmc.as_ref())
112            .await
113            .map_err(crate::Error::Bmc)
114            .map(|data| Self {
115                data,
116                _marker: PhantomData,
117            })
118    }
119
120    /// Get the raw schema data for this PCIe device.
121    #[must_use]
122    pub fn raw(&self) -> Arc<PcieDeviceSchema> {
123        self.data.clone()
124    }
125
126    /// Get hardware identifier of the PCIe device.
127    #[must_use]
128    pub fn hardware_id(&self) -> HardwareIdRef<'_, PcieDeviceTag> {
129        HardwareIdRef {
130            manufacturer: self
131                .data
132                .manufacturer
133                .as_ref()
134                .and_then(Option::as_ref)
135                .map(Manufacturer::new),
136            model: self
137                .data
138                .model
139                .as_ref()
140                .and_then(Option::as_ref)
141                .map(Model::new),
142            part_number: self
143                .data
144                .part_number
145                .as_ref()
146                .and_then(Option::as_ref)
147                .map(PartNumber::new),
148            serial_number: self
149                .data
150                .serial_number
151                .as_ref()
152                .and_then(Option::as_ref)
153                .map(SerialNumber::new),
154        }
155    }
156
157    /// The version of firmware for this PCIe device.
158    #[must_use]
159    pub fn firmware_version(&self) -> Option<FirmwareVersion<&String>> {
160        self.data
161            .firmware_version
162            .as_ref()
163            .and_then(Option::as_ref)
164            .map(FirmwareVersion::new)
165    }
166}
167
168impl<B: Bmc> Resource for PcieDevice<B> {
169    fn resource_ref(&self) -> &ResourceSchema {
170        &self.data.as_ref().base
171    }
172}
173
174impl<B: Bmc> ResourceProvidesStatus for PcieDevice<B> {
175    fn resource_status_ref(&self) -> Option<&ResourceStatusSchema> {
176        self.data.status.as_ref()
177    }
178}