blob: 0541d8198c246ba421a18682efd3d55e091ef05f [file] [log] [blame]
Rohit PAI0a888262025-06-11 08:52:29 +05301#include "Inventory.hpp"
2
3#include "Utils.hpp"
4
Rohit PAIada6baa2025-07-01 18:26:19 +05305#include <MctpRequester.hpp>
Rohit PAI0a888262025-06-11 08:52:29 +05306#include <NvidiaGpuMctpVdm.hpp>
Rohit PAIada6baa2025-07-01 18:26:19 +05307#include <OcpMctpVdm.hpp>
8#include <boost/asio/io_context.hpp>
Rohit PAIfb64f062025-06-13 18:20:02 +05309#include <boost/uuid/uuid.hpp>
10#include <boost/uuid/uuid_io.hpp>
Rohit PAI0a888262025-06-11 08:52:29 +053011#include <phosphor-logging/lg2.hpp>
12#include <sdbusplus/asio/connection.hpp>
13#include <sdbusplus/asio/object_server.hpp>
14
Rohit PAIfb64f062025-06-13 18:20:02 +053015#include <algorithm>
Rohit PAIada6baa2025-07-01 18:26:19 +053016#include <cstdint>
Rohit PAI0a888262025-06-11 08:52:29 +053017#include <memory>
Rohit PAIada6baa2025-07-01 18:26:19 +053018#include <optional>
Marc Olberdingd0125c92025-10-08 14:37:19 -070019#include <span>
Rohit PAI0a888262025-06-11 08:52:29 +053020#include <string>
Marc Olberdingd0125c92025-10-08 14:37:19 -070021#include <system_error>
Rohit PAIada6baa2025-07-01 18:26:19 +053022#include <unordered_map>
23#include <variant>
Rohit PAIfb64f062025-06-13 18:20:02 +053024#include <vector>
Rohit PAI0a888262025-06-11 08:52:29 +053025
26constexpr const char* inventoryPrefix = "/xyz/openbmc_project/inventory/";
27constexpr const char* acceleratorIfaceName =
28 "xyz.openbmc_project.Inventory.Item.Accelerator";
Rohit PAIada6baa2025-07-01 18:26:19 +053029static constexpr const char* assetIfaceName =
30 "xyz.openbmc_project.Inventory.Decorator.Asset";
Rohit PAIfb64f062025-06-13 18:20:02 +053031static constexpr const char* uuidIfaceName = "xyz.openbmc_project.Common.UUID";
Rohit PAI0ad57102025-06-13 19:29:20 +053032static constexpr const char* revisionIfaceName =
33 "xyz.openbmc_project.Inventory.Decorator.Revision";
Rohit PAI0a888262025-06-11 08:52:29 +053034
35Inventory::Inventory(
36 const std::shared_ptr<sdbusplus::asio::connection>& /*conn*/,
37 sdbusplus::asio::object_server& objectServer,
Rohit PAIada6baa2025-07-01 18:26:19 +053038 const std::string& inventoryName, mctp::MctpRequester& mctpRequester,
39 const gpu::DeviceIdentification deviceTypeIn, const uint8_t eid,
40 boost::asio::io_context& io) :
41 name(escapeName(inventoryName)), mctpRequester(mctpRequester),
42 deviceType(deviceTypeIn), eid(eid), retryTimer(io)
Rohit PAI0a888262025-06-11 08:52:29 +053043{
Rohit PAIada6baa2025-07-01 18:26:19 +053044 std::string path = inventoryPrefix + name;
Rohit PAIfb64f062025-06-13 18:20:02 +053045
Rohit PAIada6baa2025-07-01 18:26:19 +053046 assetIface = objectServer.add_interface(path, assetIfaceName);
47 assetIface->register_property("Manufacturer", std::string("NVIDIA"));
48 // Register properties which need to be fetched from the device
49 registerProperty(gpu::InventoryPropertyId::SERIAL_NUMBER, assetIface,
50 "SerialNumber");
51 registerProperty(gpu::InventoryPropertyId::BOARD_PART_NUMBER, assetIface,
52 "PartNumber");
Rohit PAI0ad57102025-06-13 19:29:20 +053053 registerProperty(gpu::InventoryPropertyId::MARKETING_NAME, assetIface,
54 "Model");
Rohit PAIada6baa2025-07-01 18:26:19 +053055 assetIface->initialize();
56
Rohit PAIfb64f062025-06-13 18:20:02 +053057 uuidInterface = objectServer.add_interface(path, uuidIfaceName);
58 registerProperty(gpu::InventoryPropertyId::DEVICE_GUID, uuidInterface,
59 "UUID");
60 uuidInterface->initialize();
61
Rohit PAI0ad57102025-06-13 19:29:20 +053062 revisionIface = objectServer.add_interface(path, revisionIfaceName);
63 registerProperty(gpu::InventoryPropertyId::DEVICE_PART_NUMBER,
64 revisionIface, "Version");
65 revisionIface->initialize();
66
Rohit PAIada6baa2025-07-01 18:26:19 +053067 // Static properties
Rohit PAI0a888262025-06-11 08:52:29 +053068 if (deviceType == gpu::DeviceIdentification::DEVICE_GPU)
69 {
Rohit PAIada6baa2025-07-01 18:26:19 +053070 acceleratorInterface =
71 objectServer.add_interface(path, acceleratorIfaceName);
72 acceleratorInterface->register_property("Type", std::string("GPU"));
73 acceleratorInterface->initialize();
74 }
75
76 processNextProperty();
77}
78
79void Inventory::registerProperty(
80 gpu::InventoryPropertyId propertyId,
81 const std::shared_ptr<sdbusplus::asio::dbus_interface>& interface,
82 const std::string& propertyName)
83{
84 if (interface)
85 {
86 interface->register_property(propertyName, std::string{});
87 properties[propertyId] = {interface, propertyName, 0, true};
88 }
89}
90
91void Inventory::processInventoryProperty(gpu::InventoryPropertyId propertyId)
92{
93 auto it = properties.find(propertyId);
94 if (it != properties.end())
95 {
96 markPropertyPending(it);
97 std::optional<gpu::InventoryPropertyId> nextProperty =
98 getNextPendingProperty();
99 if (nextProperty && *nextProperty == propertyId)
Rohit PAI0a888262025-06-11 08:52:29 +0530100 {
Rohit PAIada6baa2025-07-01 18:26:19 +0530101 processNextProperty();
Rohit PAI0a888262025-06-11 08:52:29 +0530102 }
Rohit PAIada6baa2025-07-01 18:26:19 +0530103 }
104}
105
106void Inventory::markPropertyPending(
107 std::unordered_map<gpu::InventoryPropertyId, PropertyInfo>::iterator it)
108{
109 it->second.isPending = true;
110 it->second.retryCount = 0;
111}
112
113void Inventory::markPropertyProcessed(
114 std::unordered_map<gpu::InventoryPropertyId, PropertyInfo>::iterator it)
115{
116 it->second.isPending = false;
117}
118
119std::optional<gpu::InventoryPropertyId> Inventory::getNextPendingProperty()
120 const
121{
122 for (const auto& [propertyId, info] : properties)
123 {
124 if (info.isPending)
125 {
126 return propertyId;
127 }
128 }
129 return std::nullopt;
130}
131
132void Inventory::sendInventoryPropertyRequest(
133 gpu::InventoryPropertyId propertyId)
134{
135 int rc = gpu::encodeGetInventoryInformationRequest(
Marc Olberdingd0125c92025-10-08 14:37:19 -0700136 0, static_cast<uint8_t>(propertyId), requestBuffer);
Rohit PAIada6baa2025-07-01 18:26:19 +0530137 if (rc != 0)
138 {
139 lg2::error(
140 "Failed to encode property ID {PROP_ID} request for {NAME}: rc={RC}",
141 "PROP_ID", static_cast<uint8_t>(propertyId), "NAME", name, "RC",
142 rc);
143 return;
144 }
145
146 lg2::info(
147 "Sending inventory request for property ID {PROP_ID} to EID {EID} for {NAME}",
148 "PROP_ID", static_cast<uint8_t>(propertyId), "EID", eid, "NAME", name);
149
Marc Olberdingd0125c92025-10-08 14:37:19 -0700150 mctpRequester.sendRecvMsg(
151 eid, requestBuffer,
152 [this, propertyId](const std::error_code& result,
153 std::span<const uint8_t> buffer) {
154 this->handleInventoryPropertyResponse(propertyId, result, buffer);
155 });
Rohit PAIada6baa2025-07-01 18:26:19 +0530156}
157
158void Inventory::handleInventoryPropertyResponse(
Marc Olberdingd0125c92025-10-08 14:37:19 -0700159 gpu::InventoryPropertyId propertyId, const std::error_code& ec,
160 std::span<const uint8_t> buffer)
Rohit PAIada6baa2025-07-01 18:26:19 +0530161{
162 auto it = properties.find(propertyId);
163 if (it == properties.end())
164 {
165 lg2::error("Property ID {PROP_ID} for {NAME} not found", "PROP_ID",
166 static_cast<uint8_t>(propertyId), "NAME", name);
167 processNextProperty();
168 return;
169 }
170
171 bool success = false;
Marc Olberdingd0125c92025-10-08 14:37:19 -0700172 if (!ec)
Rohit PAIada6baa2025-07-01 18:26:19 +0530173 {
174 ocp::accelerator_management::CompletionCode cc{};
175 uint16_t reasonCode = 0;
176 gpu::InventoryValue info;
177 int rc = gpu::decodeGetInventoryInformationResponse(
Marc Olberdingd0125c92025-10-08 14:37:19 -0700178 buffer, cc, reasonCode, propertyId, info);
Rohit PAIada6baa2025-07-01 18:26:19 +0530179
180 lg2::info(
181 "Response for property ID {PROP_ID} from {NAME}, sendRecvMsgResult: {RESULT}, decode_rc: {RC}, completion_code: {CC}, reason_code: {REASON}",
182 "PROP_ID", static_cast<uint8_t>(propertyId), "NAME", name, "RESULT",
Marc Olberdingd0125c92025-10-08 14:37:19 -0700183 ec.message(), "RC", rc, "CC", static_cast<uint8_t>(cc), "REASON",
184 reasonCode);
Rohit PAIada6baa2025-07-01 18:26:19 +0530185
186 if (rc == 0 &&
Rohit PAIfb64f062025-06-13 18:20:02 +0530187 cc == ocp::accelerator_management::CompletionCode::SUCCESS)
Rohit PAIada6baa2025-07-01 18:26:19 +0530188 {
Rohit PAIfb64f062025-06-13 18:20:02 +0530189 std::string value;
190
191 // Handle different property types based on property ID
192 switch (propertyId)
193 {
194 case gpu::InventoryPropertyId::BOARD_PART_NUMBER:
195 case gpu::InventoryPropertyId::SERIAL_NUMBER:
196 case gpu::InventoryPropertyId::MARKETING_NAME:
197 case gpu::InventoryPropertyId::DEVICE_PART_NUMBER:
198 if (std::holds_alternative<std::string>(info))
199 {
200 value = std::get<std::string>(info);
201 }
202 else
203 {
204 lg2::error(
205 "Property ID {PROP_ID} for {NAME} expected string but got different type",
206 "PROP_ID", static_cast<uint8_t>(propertyId), "NAME",
207 name);
208 break;
209 }
210 break;
211
212 case gpu::InventoryPropertyId::DEVICE_GUID:
213 if (std::holds_alternative<std::vector<uint8_t>>(info))
214 {
215 const auto& guidBytes =
216 std::get<std::vector<uint8_t>>(info);
217 if (guidBytes.size() >= 16)
218 {
219 boost::uuids::uuid uuid;
220 std::copy(guidBytes.begin(), guidBytes.begin() + 16,
221 uuid.begin());
222 value = boost::uuids::to_string(uuid);
223 }
224 else
225 {
226 lg2::error(
227 "Property ID {PROP_ID} for {NAME} GUID size {SIZE} is less than 16 bytes",
228 "PROP_ID", static_cast<uint8_t>(propertyId),
229 "NAME", name, "SIZE", guidBytes.size());
230 break;
231 }
232 }
233 else
234 {
235 lg2::error(
236 "Property ID {PROP_ID} for {NAME} expected vector<uint8_t> but got different type",
237 "PROP_ID", static_cast<uint8_t>(propertyId), "NAME",
238 name);
239 break;
240 }
241 break;
242
243 default:
244 lg2::error("Unsupported property ID {PROP_ID} for {NAME}",
245 "PROP_ID", static_cast<uint8_t>(propertyId),
246 "NAME", name);
247 break;
248 }
249
250 if (!value.empty())
251 {
252 it->second.interface->set_property(it->second.propertyName,
253 value);
254 lg2::info(
255 "Successfully received property ID {PROP_ID} for {NAME} with value: {VALUE}",
256 "PROP_ID", static_cast<uint8_t>(propertyId), "NAME", name,
257 "VALUE", value);
258 success = true;
259 }
Rohit PAIada6baa2025-07-01 18:26:19 +0530260 }
261 }
262
263 if (!success)
264 {
265 it->second.retryCount++;
266 if (it->second.retryCount >= maxRetryAttempts)
Rohit PAI0a888262025-06-11 08:52:29 +0530267 {
268 lg2::error(
Rohit PAIada6baa2025-07-01 18:26:19 +0530269 "Property ID {PROP_ID} for {NAME} failed after {ATTEMPTS} attempts",
270 "PROP_ID", static_cast<uint8_t>(propertyId), "NAME", name,
271 "ATTEMPTS", maxRetryAttempts);
272 markPropertyProcessed(it);
Rohit PAI0a888262025-06-11 08:52:29 +0530273 }
Rohit PAIada6baa2025-07-01 18:26:19 +0530274 else
275 {
276 retryTimer.expires_after(retryDelay);
277 retryTimer.async_wait([this](const boost::system::error_code& ec) {
278 if (ec)
279 {
280 lg2::error("Retry timer error for {NAME}: {ERROR}", "NAME",
281 name, "ERROR", ec.message());
282 return;
283 }
284 this->processNextProperty();
285 });
286 return;
287 }
288 }
289 else
290 {
291 markPropertyProcessed(it);
292 }
293
294 processNextProperty();
295}
296
297void Inventory::processNextProperty()
298{
299 std::optional<gpu::InventoryPropertyId> nextProperty =
300 getNextPendingProperty();
301 if (nextProperty)
302 {
303 sendInventoryPropertyRequest(*nextProperty);
304 }
305 else
306 {
307 lg2::info("No pending properties found to process for {NAME}", "NAME",
308 name);
Rohit PAI0a888262025-06-11 08:52:29 +0530309 }
310}