Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & |
| 3 | * AFFILIATES. All rights reserved. |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
| 7 | #include "NvidiaDeviceDiscovery.hpp" |
| 8 | |
| 9 | #include "NvidiaGpuDevice.hpp" |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 10 | #include "NvidiaSmaDevice.hpp" |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 11 | #include "Utils.hpp" |
| 12 | |
| 13 | #include <bits/basic_string.h> |
| 14 | |
| 15 | #include <MctpRequester.hpp> |
| 16 | #include <NvidiaGpuMctpVdm.hpp> |
| 17 | #include <OcpMctpVdm.hpp> |
| 18 | #include <boost/asio/io_context.hpp> |
| 19 | #include <boost/container/flat_map.hpp> |
| 20 | #include <phosphor-logging/lg2.hpp> |
| 21 | #include <sdbusplus/asio/connection.hpp> |
| 22 | #include <sdbusplus/asio/object_server.hpp> |
| 23 | #include <sdbusplus/message.hpp> |
| 24 | #include <sdbusplus/message/native_types.hpp> |
| 25 | |
| 26 | #include <algorithm> |
| 27 | #include <array> |
| 28 | #include <cstdint> |
| 29 | #include <memory> |
| 30 | #include <span> |
| 31 | #include <string> |
| 32 | #include <utility> |
| 33 | #include <variant> |
| 34 | #include <vector> |
| 35 | |
| 36 | void processQueryDeviceIdResponse( |
| 37 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
| 38 | boost::container::flat_map<std::string, std::shared_ptr<GpuDevice>>& |
| 39 | gpuDevices, |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 40 | boost::container::flat_map<std::string, std::shared_ptr<SmaDevice>>& |
| 41 | smaDevices, |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 42 | const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 43 | mctp::MctpRequester& mctpRequester, const SensorConfigs& configs, |
| 44 | const std::string& path, uint8_t eid, int sendRecvMsgResult, |
| 45 | std::span<uint8_t> queryDeviceIdentificationResponse) |
| 46 | { |
| 47 | if (sendRecvMsgResult != 0) |
| 48 | { |
| 49 | lg2::error( |
| 50 | "Error processing MCTP endpoint with eid {EID} : sending message over MCTP failed, rc={RC}", |
| 51 | "EID", eid, "RC", sendRecvMsgResult); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | ocp::accelerator_management::CompletionCode cc{}; |
| 56 | uint16_t reasonCode = 0; |
| 57 | uint8_t responseDeviceType = 0; |
| 58 | uint8_t responseInstanceId = 0; |
| 59 | |
| 60 | auto rc = gpu::decodeQueryDeviceIdentificationResponse( |
| 61 | queryDeviceIdentificationResponse, cc, reasonCode, responseDeviceType, |
| 62 | responseInstanceId); |
| 63 | |
| 64 | if (rc != 0 || cc != ocp::accelerator_management::CompletionCode::SUCCESS) |
| 65 | { |
| 66 | lg2::error( |
| 67 | "Error processing MCTP endpoint with eid {EID} : decode failed, rc={RC}, cc={CC}, reasonCode={RESC}", |
| 68 | "EID", eid, "RC", rc, "CC", cc, "RESC", reasonCode); |
| 69 | return; |
| 70 | } |
| 71 | |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 72 | switch (static_cast<gpu::DeviceIdentification>(responseDeviceType)) |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 73 | { |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 74 | case gpu::DeviceIdentification::DEVICE_GPU: |
| 75 | { |
| 76 | lg2::info( |
| 77 | "Found the GPU with EID {EID}, DeviceType {DEVTYPE}, InstanceId {IID}.", |
| 78 | "EID", eid, "DEVTYPE", responseDeviceType, "IID", |
| 79 | responseInstanceId); |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 80 | |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 81 | auto gpuName = configs.name + '_' + |
| 82 | std::to_string(responseInstanceId); |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 83 | |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 84 | gpuDevices[gpuName] = |
| 85 | std::make_shared<GpuDevice>(configs, gpuName, path, conn, eid, |
| 86 | io, mctpRequester, objectServer); |
| 87 | break; |
| 88 | } |
| 89 | |
| 90 | case gpu::DeviceIdentification::DEVICE_SMA: |
| 91 | { |
| 92 | lg2::info( |
| 93 | "Found the SMA Device with EID {EID}, DeviceType {DEVTYPE}, InstanceId {IID}.", |
| 94 | "EID", eid, "DEVTYPE", responseDeviceType, "IID", |
| 95 | responseInstanceId); |
| 96 | |
| 97 | auto smaName = configs.name + "_SMA_" + |
| 98 | std::to_string(responseInstanceId); |
| 99 | |
| 100 | smaDevices[smaName] = |
| 101 | std::make_shared<SmaDevice>(configs, smaName, path, conn, eid, |
| 102 | io, mctpRequester, objectServer); |
| 103 | break; |
| 104 | } |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | void queryDeviceIdentification( |
| 109 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
| 110 | boost::container::flat_map<std::string, std::shared_ptr<GpuDevice>>& |
| 111 | gpuDevices, |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 112 | boost::container::flat_map<std::string, std::shared_ptr<SmaDevice>>& |
| 113 | smaDevices, |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 114 | const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 115 | mctp::MctpRequester& mctpRequester, const SensorConfigs& configs, |
| 116 | const std::string& path, uint8_t eid) |
| 117 | { |
| 118 | auto queryDeviceIdentificationRequest = std::make_shared< |
| 119 | std::array<uint8_t, sizeof(gpu::QueryDeviceIdentificationRequest)>>(); |
| 120 | |
| 121 | auto queryDeviceIdentificationResponse = std::make_shared< |
| 122 | std::array<uint8_t, sizeof(gpu::QueryDeviceIdentificationResponse)>>(); |
| 123 | |
| 124 | auto rc = gpu::encodeQueryDeviceIdentificationRequest( |
| 125 | 0, *queryDeviceIdentificationRequest); |
| 126 | if (rc != 0) |
| 127 | { |
| 128 | lg2::error( |
| 129 | "Error processing MCTP endpoint with eid {EID} : encode failed, rc={RC}", |
| 130 | "EID", eid, "RC", rc); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | mctpRequester.sendRecvMsg( |
| 135 | eid, *queryDeviceIdentificationRequest, |
| 136 | *queryDeviceIdentificationResponse, |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 137 | [&io, &objectServer, &gpuDevices, &smaDevices, conn, &mctpRequester, |
| 138 | configs, path, eid, queryDeviceIdentificationRequest, |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 139 | queryDeviceIdentificationResponse](int sendRecvMsgResult) { |
| 140 | processQueryDeviceIdResponse( |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 141 | io, objectServer, gpuDevices, smaDevices, conn, mctpRequester, |
| 142 | configs, path, eid, sendRecvMsgResult, |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 143 | *queryDeviceIdentificationResponse); |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | void processEndpoint( |
| 148 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
| 149 | boost::container::flat_map<std::string, std::shared_ptr<GpuDevice>>& |
| 150 | gpuDevices, |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 151 | boost::container::flat_map<std::string, std::shared_ptr<SmaDevice>>& |
| 152 | smaDevices, |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 153 | const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 154 | mctp::MctpRequester& mctpRequester, const SensorConfigs& configs, |
| 155 | const std::string& path, const boost::system::error_code& ec, |
| 156 | const SensorBaseConfigMap& endpoint) |
| 157 | { |
| 158 | if (ec) |
| 159 | { |
| 160 | lg2::error("Error processing MCTP endpoint: Error:{ERROR}", "ERROR", |
| 161 | ec.message()); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | auto hasEid = endpoint.find("EID"); |
| 166 | uint8_t eid{}; |
| 167 | |
| 168 | if (hasEid != endpoint.end()) |
| 169 | { |
| 170 | const auto* eidPtr = std::get_if<uint8_t>(&hasEid->second); |
| 171 | if (eidPtr != nullptr) |
| 172 | { |
| 173 | eid = *eidPtr; |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | lg2::error( |
| 178 | "Error processing MCTP endpoint: Property EID does not have valid type."); |
| 179 | return; |
| 180 | } |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | lg2::error( |
| 185 | "Error processing MCTP endpoint: Property EID not found in the configuration."); |
| 186 | return; |
| 187 | } |
| 188 | |
| 189 | auto hasMctpTypes = endpoint.find("SupportedMessageTypes"); |
| 190 | std::vector<uint8_t> mctpTypes{}; |
| 191 | |
| 192 | if (hasMctpTypes != endpoint.end()) |
| 193 | { |
| 194 | const auto* mctpTypePtr = |
| 195 | std::get_if<std::vector<uint8_t>>(&hasMctpTypes->second); |
| 196 | if (mctpTypePtr != nullptr) |
| 197 | { |
| 198 | mctpTypes = *mctpTypePtr; |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | lg2::error( |
| 203 | "Error processing MCTP endpoint with eid {EID} : Property SupportedMessageTypes does not have valid type.", |
| 204 | "EID", eid); |
| 205 | return; |
| 206 | } |
| 207 | } |
| 208 | else |
| 209 | { |
| 210 | lg2::error( |
| 211 | "Error processing MCTP endpoint with eid {EID} : Property SupportedMessageTypes not found in the configuration.", |
| 212 | "EID", eid); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | if (std::find(mctpTypes.begin(), mctpTypes.end(), |
| 217 | ocp::accelerator_management::messageType) != mctpTypes.end()) |
| 218 | { |
| 219 | lg2::info("Found OCP MCTP VDM Endpoint with ID {EID}", "EID", eid); |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 220 | queryDeviceIdentification(io, objectServer, gpuDevices, smaDevices, |
| 221 | conn, mctpRequester, configs, path, eid); |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | void queryEndpoints( |
| 226 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
| 227 | boost::container::flat_map<std::string, std::shared_ptr<GpuDevice>>& |
| 228 | gpuDevices, |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 229 | boost::container::flat_map<std::string, std::shared_ptr<SmaDevice>>& |
| 230 | smaDevices, |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 231 | const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 232 | mctp::MctpRequester& mctpRequester, const SensorConfigs& configs, |
| 233 | const std::string& path, const boost::system::error_code& ec, |
| 234 | const GetSubTreeType& ret) |
| 235 | { |
| 236 | if (ec) |
| 237 | { |
| 238 | lg2::error("Error processing MCTP endpoints: {ERROR}", "ERROR", |
| 239 | ec.message()); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | if (ret.empty()) |
| 244 | { |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | for (const auto& [objPath, services] : ret) |
| 249 | { |
| 250 | for (const auto& [service, ifaces] : services) |
| 251 | { |
| 252 | for (const auto& iface : ifaces) |
| 253 | { |
| 254 | if (iface == "xyz.openbmc_project.MCTP.Endpoint") |
| 255 | { |
| 256 | conn->async_method_call( |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 257 | [&io, &objectServer, &gpuDevices, &smaDevices, conn, |
| 258 | &mctpRequester, configs, |
| 259 | path](const boost::system::error_code& ec, |
| 260 | const SensorBaseConfigMap& endpoint) { |
| 261 | processEndpoint(io, objectServer, gpuDevices, |
| 262 | smaDevices, conn, mctpRequester, |
| 263 | configs, path, ec, endpoint); |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 264 | }, |
| 265 | service, objPath, "org.freedesktop.DBus.Properties", |
| 266 | "GetAll", iface); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | void discoverDevices( |
| 274 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
| 275 | boost::container::flat_map<std::string, std::shared_ptr<GpuDevice>>& |
| 276 | gpuDevices, |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 277 | boost::container::flat_map<std::string, std::shared_ptr<SmaDevice>>& |
| 278 | smaDevices, |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 279 | const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 280 | mctp::MctpRequester& mctpRequester, const SensorConfigs& configs, |
| 281 | const std::string& path) |
| 282 | { |
| 283 | std::string searchPath{"/au/com/codeconstruct/"}; |
| 284 | std::vector<std::string> ifaceList{{"xyz.openbmc_project.MCTP.Endpoint"}}; |
| 285 | |
| 286 | conn->async_method_call( |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 287 | [&io, &objectServer, &gpuDevices, &smaDevices, conn, &mctpRequester, |
| 288 | configs, |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 289 | path](const boost::system::error_code& ec, const GetSubTreeType& ret) { |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 290 | queryEndpoints(io, objectServer, gpuDevices, smaDevices, conn, |
| 291 | mctpRequester, configs, path, ec, ret); |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 292 | }, |
| 293 | "xyz.openbmc_project.ObjectMapper", |
| 294 | "/xyz/openbmc_project/object_mapper", |
| 295 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", searchPath, 0, |
| 296 | ifaceList); |
| 297 | } |
| 298 | |
| 299 | void processSensorConfigs( |
| 300 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
| 301 | boost::container::flat_map<std::string, std::shared_ptr<GpuDevice>>& |
| 302 | gpuDevices, |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 303 | boost::container::flat_map<std::string, std::shared_ptr<SmaDevice>>& |
| 304 | smaDevices, |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 305 | const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 306 | mctp::MctpRequester& mctpRequester, const ManagedObjectType& resp) |
| 307 | { |
| 308 | for (const auto& [path, interfaces] : resp) |
| 309 | { |
| 310 | for (const auto& [intf, cfg] : interfaces) |
| 311 | { |
| 312 | if (intf != configInterfaceName(deviceType)) |
| 313 | { |
| 314 | continue; |
| 315 | } |
| 316 | |
| 317 | SensorConfigs configs; |
| 318 | |
| 319 | configs.name = loadVariant<std::string>(cfg, "Name"); |
| 320 | |
| 321 | configs.pollRate = loadVariant<uint64_t>(cfg, "PollRate"); |
| 322 | |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 323 | discoverDevices(io, objectServer, gpuDevices, smaDevices, |
| 324 | dbusConnection, mctpRequester, configs, path); |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 325 | |
| 326 | lg2::info( |
| 327 | "Detected configuration {NAME} of type {TYPE} at path: {PATH}.", |
| 328 | "NAME", configs.name, "TYPE", deviceType, "PATH", path); |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | void createSensors( |
| 334 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
| 335 | boost::container::flat_map<std::string, std::shared_ptr<GpuDevice>>& |
| 336 | gpuDevices, |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 337 | boost::container::flat_map<std::string, std::shared_ptr<SmaDevice>>& |
| 338 | smaDevices, |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 339 | const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 340 | mctp::MctpRequester& mctpRequester) |
| 341 | { |
| 342 | if (!dbusConnection) |
| 343 | { |
| 344 | lg2::error("Connection not created"); |
| 345 | return; |
| 346 | } |
| 347 | dbusConnection->async_method_call( |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 348 | [&gpuDevices, &smaDevices, &mctpRequester, dbusConnection, &io, |
| 349 | &objectServer](boost::system::error_code ec, |
| 350 | const ManagedObjectType& resp) { |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 351 | if (ec) |
| 352 | { |
| 353 | lg2::error("Error contacting entity manager"); |
| 354 | return; |
| 355 | } |
| 356 | |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 357 | processSensorConfigs(io, objectServer, gpuDevices, smaDevices, |
| 358 | dbusConnection, mctpRequester, resp); |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 359 | }, |
| 360 | entityManagerName, "/xyz/openbmc_project/inventory", |
| 361 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 362 | } |
| 363 | |
| 364 | void interfaceRemoved( |
| 365 | sdbusplus::message_t& message, |
| 366 | boost::container::flat_map<std::string, std::shared_ptr<GpuDevice>>& |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 367 | gpuDevices, |
| 368 | boost::container::flat_map<std::string, std::shared_ptr<SmaDevice>>& |
| 369 | smaDevices) |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 370 | { |
| 371 | if (message.is_method_error()) |
| 372 | { |
| 373 | lg2::error("interfacesRemoved callback method error"); |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | sdbusplus::message::object_path removedPath; |
| 378 | std::vector<std::string> interfaces; |
| 379 | |
| 380 | message.read(removedPath, interfaces); |
| 381 | |
| 382 | // If the xyz.openbmc_project.Confguration.X interface was removed |
| 383 | // for one or more sensors, delete those sensor objects. |
| 384 | auto sensorIt = gpuDevices.begin(); |
| 385 | while (sensorIt != gpuDevices.end()) |
| 386 | { |
| 387 | if ((sensorIt->second->getPath() == removedPath) && |
| 388 | (std::find(interfaces.begin(), interfaces.end(), |
| 389 | configInterfaceName(deviceType)) != interfaces.end())) |
| 390 | { |
| 391 | sensorIt = gpuDevices.erase(sensorIt); |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | sensorIt++; |
| 396 | } |
| 397 | } |
Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 398 | |
| 399 | auto smaSensorIt = smaDevices.begin(); |
| 400 | while (smaSensorIt != smaDevices.end()) |
| 401 | { |
| 402 | if ((smaSensorIt->second->getPath() == removedPath) && |
| 403 | (std::find(interfaces.begin(), interfaces.end(), |
| 404 | configInterfaceName(deviceType)) != interfaces.end())) |
| 405 | { |
| 406 | smaSensorIt = smaDevices.erase(smaSensorIt); |
| 407 | } |
| 408 | else |
| 409 | { |
| 410 | smaSensorIt++; |
| 411 | } |
| 412 | } |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 413 | } |