Skip to main content

nv_redfish/oem/nvidia/bluefield/
nvidia_computer_system.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//! Support NVIDIA Bluefield ComputerSystem OEM extension.
17
18use crate::oem::nvidia::bluefield::schema::redfish::nvidia_computer_system::NvidiaComputerSystem as NvidiaComputerSystemSchema;
19use crate::patch_support::JsonValue;
20use crate::patch_support::Payload;
21use crate::schema::redfish::resource::Oem as ResourceOemSchema;
22use crate::Error;
23use crate::NvBmc;
24use nv_redfish_core::Bmc;
25use nv_redfish_core::NavProperty;
26use serde::Deserialize;
27use std::marker::PhantomData;
28use std::sync::Arc;
29use tagged_types::TaggedType;
30
31#[derive(Deserialize)]
32struct Oem {
33    #[serde(rename = "Nvidia")]
34    nvidia: Option<NavProperty<NvidiaComputerSystemSchema>>,
35}
36
37#[doc(inline)]
38pub use crate::oem::nvidia::bluefield::schema::redfish::nvidia_computer_system::Mode;
39
40/// Base MAC address of the Bluefield DPU as reported by the device.
41pub type BaseMac<T> = TaggedType<T, BaseMacTag>;
42#[doc(hidden)]
43#[derive(tagged_types::Tag)]
44#[implement(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
45#[transparent(Debug, Display, FromStr, Serialize, Deserialize)]
46#[capability(inner_access, cloned)]
47pub enum BaseMacTag {}
48
49/// Represents a NVIDIA extension of computer system in the BMC.
50///
51/// Provides access to system information and sub-resources such as processors.
52pub struct NvidiaComputerSystem<B: Bmc> {
53    data: Arc<NvidiaComputerSystemSchema>,
54    _marker: PhantomData<B>,
55}
56
57impl<B: Bmc> NvidiaComputerSystem<B> {
58    /// Create a new computer system handle.
59    pub(crate) async fn new(
60        bmc: &NvBmc<B>,
61        oem: &ResourceOemSchema,
62    ) -> Result<Option<Self>, Error<B>> {
63        let oem: Oem =
64            serde_json::from_value(oem.additional_properties.clone()).map_err(Error::Json)?;
65        if let Some(nav) = oem.nvidia {
66            // We need nav.to_reference() here because in Bluefield-3
67            // sometimes provides not fully expanded object with some
68            // other navigation properties. to_reference() ignores it
69            // and forces to expand again.
70            Payload::get(
71                bmc.as_ref(),
72                &nav.to_reference(),
73                append_odata_id_if_missing,
74            )
75            .await
76            .map(|data| {
77                Some(Self {
78                    data,
79                    _marker: PhantomData,
80                })
81            })
82        } else {
83            Ok(None)
84        }
85    }
86
87    /// Get the raw schema data for this NVIDIA computer system.
88    ///
89    /// Returns an `Arc` to the underlying schema, allowing cheap cloning
90    /// and sharing of the data.
91    #[must_use]
92    pub fn raw(&self) -> Arc<NvidiaComputerSystemSchema> {
93        self.data.clone()
94    }
95
96    /// Get base MAC address of the device.
97    #[must_use]
98    pub fn base_mac(&self) -> Option<BaseMac<&str>> {
99        self.data.base_mac.as_deref().map(BaseMac::new)
100    }
101
102    /// Get mode of the Bluefield device.
103    ///
104    /// Getting mode from directly from OEM extension is supported
105    /// only by Bluefield 3.
106    #[must_use]
107    pub fn mode(&self) -> Option<Mode> {
108        self.data.mode
109    }
110}
111
112// This patch is needed to fix response without `@odata.id` field.
113// This behavior was observed in BlueField-3 SmartNIC Main Card with
114// firmware: BF-24.07-14.
115fn append_odata_id_if_missing(v: JsonValue) -> JsonValue {
116    if let JsonValue::Object(mut obj) = v {
117        obj.entry("@odata.id").or_insert(JsonValue::String(
118            "/redfish/v1/Systems/Bluefield/Oem/Nvidia".into(),
119        ));
120        JsonValue::Object(obj)
121    } else {
122        v
123    }
124}