Jason M. Bills | f5c9f8b | 2018-12-18 16:51:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 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 | |
| 17 | #pragma once |
| 18 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 19 | #include "app.hpp" |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 20 | #include "dbus_utility.hpp" |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 21 | #include "generated/enums/pcie_device.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 22 | #include "query.hpp" |
| 23 | #include "registries/privilege_registry.hpp" |
Lakshmi Yadlapati | b38fa2a | 2023-03-10 16:19:46 -0600 | [diff] [blame] | 24 | #include "utils/collection.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 25 | #include "utils/dbus_utils.hpp" |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 26 | |
Jason M. Bills | f5c9f8b | 2018-12-18 16:51:18 -0800 | [diff] [blame] | 27 | #include <boost/system/linux_error.hpp> |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 28 | #include <boost/url/format.hpp> |
Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 29 | #include <sdbusplus/asio/property.hpp> |
| 30 | #include <sdbusplus/unpack_properties.hpp> |
Jason M. Bills | f5c9f8b | 2018-12-18 16:51:18 -0800 | [diff] [blame] | 31 | |
| 32 | namespace redfish |
| 33 | { |
| 34 | |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 35 | static constexpr const char* inventoryPath = "/xyz/openbmc_project/inventory"; |
Lakshmi Yadlapati | 94c3a10 | 2023-04-05 18:11:22 -0500 | [diff] [blame] | 36 | static constexpr std::array<std::string_view, 1> pcieDeviceInterface = { |
| 37 | "xyz.openbmc_project.Inventory.Item.PCIeDevice"}; |
Jason M. Bills | f5c9f8b | 2018-12-18 16:51:18 -0800 | [diff] [blame] | 38 | |
Lakshmi Yadlapati | 543f9a7 | 2023-03-10 17:06:05 -0600 | [diff] [blame] | 39 | static inline void handlePCIeDevicePath( |
| 40 | const std::string& pcieDeviceId, |
| 41 | const std::shared_ptr<bmcweb::AsyncResp>& aResp, |
| 42 | const dbus::utility::MapperGetSubTreePathsResponse& pcieDevicePaths, |
| 43 | const std::function<void(const std::string& pcieDevicePath, |
| 44 | const std::string& service)>& callback) |
| 45 | |
| 46 | { |
| 47 | for (const std::string& pcieDevicePath : pcieDevicePaths) |
| 48 | { |
| 49 | std::string pciecDeviceName = |
| 50 | sdbusplus::message::object_path(pcieDevicePath).filename(); |
| 51 | if (pciecDeviceName.empty() || pciecDeviceName != pcieDeviceId) |
| 52 | { |
| 53 | continue; |
| 54 | } |
| 55 | |
| 56 | dbus::utility::getDbusObject( |
| 57 | pcieDevicePath, {}, |
| 58 | [pcieDevicePath, aResp, |
| 59 | callback](const boost::system::error_code& ec, |
| 60 | const dbus::utility::MapperGetObject& object) { |
| 61 | if (ec || object.empty()) |
| 62 | { |
| 63 | BMCWEB_LOG_ERROR << "DBUS response error " << ec; |
| 64 | messages::internalError(aResp->res); |
| 65 | return; |
| 66 | } |
| 67 | callback(pcieDevicePath, object.begin()->first); |
| 68 | }); |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | BMCWEB_LOG_WARNING << "PCIe Device not found"; |
| 73 | messages::resourceNotFound(aResp->res, "PCIeDevice", pcieDeviceId); |
| 74 | } |
| 75 | |
| 76 | static inline void getValidPCIeDevicePath( |
| 77 | const std::string& pcieDeviceId, |
| 78 | const std::shared_ptr<bmcweb::AsyncResp>& aResp, |
| 79 | const std::function<void(const std::string& pcieDevicePath, |
| 80 | const std::string& service)>& callback) |
| 81 | { |
Lakshmi Yadlapati | 543f9a7 | 2023-03-10 17:06:05 -0600 | [diff] [blame] | 82 | dbus::utility::getSubTreePaths( |
Lakshmi Yadlapati | 94c3a10 | 2023-04-05 18:11:22 -0500 | [diff] [blame] | 83 | inventoryPath, 0, pcieDeviceInterface, |
Lakshmi Yadlapati | 543f9a7 | 2023-03-10 17:06:05 -0600 | [diff] [blame] | 84 | [pcieDeviceId, aResp, |
| 85 | callback](const boost::system::error_code& ec, |
| 86 | const dbus::utility::MapperGetSubTreePathsResponse& |
| 87 | pcieDevicePaths) { |
| 88 | if (ec) |
| 89 | { |
| 90 | BMCWEB_LOG_ERROR << "D-Bus response error on GetSubTree " << ec; |
| 91 | messages::internalError(aResp->res); |
| 92 | return; |
| 93 | } |
| 94 | handlePCIeDevicePath(pcieDeviceId, aResp, pcieDevicePaths, callback); |
| 95 | return; |
| 96 | }); |
| 97 | } |
| 98 | |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 99 | static inline void |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 100 | getPCIeDeviceList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 101 | const std::string& name) |
Jason M. Bills | f5c9f8b | 2018-12-18 16:51:18 -0800 | [diff] [blame] | 102 | { |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 103 | dbus::utility::getSubTreePaths( |
Lakshmi Yadlapati | 38e3d67 | 2023-05-02 09:26:33 -0500 | [diff] [blame] | 104 | inventoryPath, 0, pcieDeviceInterface, |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 105 | [asyncResp, name](const boost::system::error_code& ec, |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 106 | const dbus::utility::MapperGetSubTreePathsResponse& |
| 107 | pcieDevicePaths) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 108 | if (ec) |
| 109 | { |
| 110 | BMCWEB_LOG_DEBUG << "no PCIe device paths found ec: " |
| 111 | << ec.message(); |
| 112 | // Not an error, system just doesn't have PCIe info |
| 113 | return; |
| 114 | } |
| 115 | nlohmann::json& pcieDeviceList = asyncResp->res.jsonValue[name]; |
| 116 | pcieDeviceList = nlohmann::json::array(); |
| 117 | for (const std::string& pcieDevicePath : pcieDevicePaths) |
| 118 | { |
| 119 | size_t devStart = pcieDevicePath.rfind('/'); |
| 120 | if (devStart == std::string::npos) |
Jason M. Bills | f5c9f8b | 2018-12-18 16:51:18 -0800 | [diff] [blame] | 121 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 122 | continue; |
Jason M. Bills | f5c9f8b | 2018-12-18 16:51:18 -0800 | [diff] [blame] | 123 | } |
Jason M. Bills | f5c9f8b | 2018-12-18 16:51:18 -0800 | [diff] [blame] | 124 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 125 | std::string devName = pcieDevicePath.substr(devStart + 1); |
| 126 | if (devName.empty()) |
| 127 | { |
| 128 | continue; |
Jason M. Bills | f5c9f8b | 2018-12-18 16:51:18 -0800 | [diff] [blame] | 129 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 130 | nlohmann::json::object_t pcieDevice; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 131 | pcieDevice["@odata.id"] = boost::urls::format( |
| 132 | "/redfish/v1/Systems/system/PCIeDevices/{}", devName); |
Patrick Williams | b2ba307 | 2023-05-12 10:27:39 -0500 | [diff] [blame] | 133 | pcieDeviceList.emplace_back(std::move(pcieDevice)); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 134 | } |
| 135 | asyncResp->res.jsonValue[name + "@odata.count"] = pcieDeviceList.size(); |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 136 | }); |
Jason M. Bills | f5c9f8b | 2018-12-18 16:51:18 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Lakshmi Yadlapati | b38fa2a | 2023-03-10 16:19:46 -0600 | [diff] [blame] | 139 | static inline void handlePCIeDeviceCollectionGet( |
| 140 | crow::App& app, const crow::Request& req, |
| 141 | const std::shared_ptr<bmcweb::AsyncResp>& aResp, |
| 142 | const std::string& systemName) |
| 143 | { |
| 144 | if (!redfish::setUpRedfishRoute(app, req, aResp)) |
| 145 | { |
| 146 | return; |
| 147 | } |
| 148 | if (systemName != "system") |
| 149 | { |
| 150 | messages::resourceNotFound(aResp->res, "ComputerSystem", systemName); |
| 151 | return; |
| 152 | } |
Lakshmi Yadlapati | 543f9a7 | 2023-03-10 17:06:05 -0600 | [diff] [blame] | 153 | |
Lakshmi Yadlapati | b38fa2a | 2023-03-10 16:19:46 -0600 | [diff] [blame] | 154 | aResp->res.addHeader(boost::beast::http::field::link, |
| 155 | "</redfish/v1/JsonSchemas/PCIeDeviceCollection/" |
| 156 | "PCIeDeviceCollection.json>; rel=describedby"); |
| 157 | aResp->res.jsonValue["@odata.type"] = |
| 158 | "#PCIeDeviceCollection.PCIeDeviceCollection"; |
| 159 | aResp->res.jsonValue["@odata.id"] = |
| 160 | "/redfish/v1/Systems/system/PCIeDevices"; |
| 161 | aResp->res.jsonValue["Name"] = "PCIe Device Collection"; |
| 162 | aResp->res.jsonValue["Description"] = "Collection of PCIe Devices"; |
| 163 | aResp->res.jsonValue["Members"] = nlohmann::json::array(); |
| 164 | aResp->res.jsonValue["Members@odata.count"] = 0; |
| 165 | |
Lakshmi Yadlapati | b38fa2a | 2023-03-10 16:19:46 -0600 | [diff] [blame] | 166 | collection_util::getCollectionMembers( |
| 167 | aResp, boost::urls::url("/redfish/v1/Systems/system/PCIeDevices"), |
Lakshmi Yadlapati | 94c3a10 | 2023-04-05 18:11:22 -0500 | [diff] [blame] | 168 | pcieDeviceInterface); |
Lakshmi Yadlapati | b38fa2a | 2023-03-10 16:19:46 -0600 | [diff] [blame] | 169 | } |
| 170 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 171 | inline void requestRoutesSystemPCIeDeviceCollection(App& app) |
Jason M. Bills | adbe192 | 2019-10-14 15:44:35 -0700 | [diff] [blame] | 172 | { |
Jason M. Bills | adbe192 | 2019-10-14 15:44:35 -0700 | [diff] [blame] | 173 | /** |
| 174 | * Functions triggers appropriate requests on DBus |
| 175 | */ |
Ed Tanous | 22d268c | 2022-05-19 09:39:07 -0700 | [diff] [blame] | 176 | BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/PCIeDevices/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 177 | .privileges(redfish::privileges::getPCIeDeviceCollection) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 178 | .methods(boost::beast::http::verb::get)( |
Lakshmi Yadlapati | b38fa2a | 2023-03-10 16:19:46 -0600 | [diff] [blame] | 179 | std::bind_front(handlePCIeDeviceCollectionGet, std::ref(app))); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 182 | inline std::optional<pcie_device::PCIeTypes> |
Spencer Ku | 62cd45a | 2021-11-22 16:41:25 +0800 | [diff] [blame] | 183 | redfishPcieGenerationFromDbus(const std::string& generationInUse) |
| 184 | { |
| 185 | if (generationInUse == |
| 186 | "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen1") |
| 187 | { |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 188 | return pcie_device::PCIeTypes::Gen1; |
Spencer Ku | 62cd45a | 2021-11-22 16:41:25 +0800 | [diff] [blame] | 189 | } |
| 190 | if (generationInUse == |
| 191 | "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen2") |
| 192 | { |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 193 | return pcie_device::PCIeTypes::Gen2; |
Spencer Ku | 62cd45a | 2021-11-22 16:41:25 +0800 | [diff] [blame] | 194 | } |
| 195 | if (generationInUse == |
| 196 | "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen3") |
| 197 | { |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 198 | return pcie_device::PCIeTypes::Gen3; |
Spencer Ku | 62cd45a | 2021-11-22 16:41:25 +0800 | [diff] [blame] | 199 | } |
| 200 | if (generationInUse == |
| 201 | "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen4") |
| 202 | { |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 203 | return pcie_device::PCIeTypes::Gen4; |
Spencer Ku | 62cd45a | 2021-11-22 16:41:25 +0800 | [diff] [blame] | 204 | } |
| 205 | if (generationInUse == |
| 206 | "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen5") |
| 207 | { |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 208 | return pcie_device::PCIeTypes::Gen5; |
Spencer Ku | 62cd45a | 2021-11-22 16:41:25 +0800 | [diff] [blame] | 209 | } |
Ed Tanous | e825cbc | 2022-06-17 11:39:22 -0700 | [diff] [blame] | 210 | if (generationInUse.empty() || |
| 211 | generationInUse == |
| 212 | "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Unknown") |
Spencer Ku | 62cd45a | 2021-11-22 16:41:25 +0800 | [diff] [blame] | 213 | { |
Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 214 | return pcie_device::PCIeTypes::Invalid; |
Spencer Ku | 62cd45a | 2021-11-22 16:41:25 +0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // The value is not unknown or Gen1-5, need return an internal error. |
| 218 | return std::nullopt; |
| 219 | } |
| 220 | |
Lakshmi Yadlapati | c6bb328 | 2023-04-12 16:56:49 -0500 | [diff] [blame] | 221 | inline void getPCIeDeviceState(const std::shared_ptr<bmcweb::AsyncResp>& aResp, |
| 222 | const std::string& pcieDevicePath, |
| 223 | const std::string& service) |
| 224 | { |
| 225 | sdbusplus::asio::getProperty<bool>( |
| 226 | *crow::connections::systemBus, service, pcieDevicePath, |
| 227 | "xyz.openbmc_project.Inventory.Item", "Present", |
| 228 | [aResp](const boost::system::error_code& ec, const bool value) { |
| 229 | if (ec) |
| 230 | { |
| 231 | if (ec.value() != EBADR) |
| 232 | { |
| 233 | BMCWEB_LOG_ERROR << "DBUS response error for State"; |
| 234 | messages::internalError(aResp->res); |
| 235 | } |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | if (!value) |
| 240 | { |
| 241 | aResp->res.jsonValue["Status"]["State"] = "Absent"; |
| 242 | } |
| 243 | }); |
| 244 | } |
| 245 | |
SunnySrivastava1984 | 913e773 | 2021-01-27 06:23:24 -0600 | [diff] [blame] | 246 | inline void getPCIeDeviceAsset(const std::shared_ptr<bmcweb::AsyncResp>& aResp, |
| 247 | const std::string& pcieDevicePath, |
| 248 | const std::string& service) |
| 249 | { |
| 250 | sdbusplus::asio::getAllProperties( |
| 251 | *crow::connections::systemBus, service, pcieDevicePath, |
| 252 | "xyz.openbmc_project.Inventory.Decorator.Asset", |
| 253 | [pcieDevicePath, |
| 254 | aResp{aResp}](const boost::system::error_code& ec, |
| 255 | const dbus::utility::DBusPropertiesMap& assetList) { |
| 256 | if (ec) |
| 257 | { |
| 258 | if (ec.value() != EBADR) |
| 259 | { |
| 260 | BMCWEB_LOG_ERROR << "DBUS response error for Properties" |
| 261 | << ec.value(); |
| 262 | messages::internalError(aResp->res); |
| 263 | } |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | const std::string* manufacturer = nullptr; |
| 268 | const std::string* model = nullptr; |
| 269 | const std::string* partNumber = nullptr; |
| 270 | const std::string* serialNumber = nullptr; |
| 271 | const std::string* sparePartNumber = nullptr; |
| 272 | |
| 273 | const bool success = sdbusplus::unpackPropertiesNoThrow( |
| 274 | dbus_utils::UnpackErrorPrinter(), assetList, "Manufacturer", |
| 275 | manufacturer, "Model", model, "PartNumber", partNumber, |
| 276 | "SerialNumber", serialNumber, "SparePartNumber", sparePartNumber); |
| 277 | |
| 278 | if (!success) |
| 279 | { |
| 280 | messages::internalError(aResp->res); |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | if (manufacturer != nullptr) |
| 285 | { |
| 286 | aResp->res.jsonValue["Manufacturer"] = *manufacturer; |
| 287 | } |
| 288 | if (model != nullptr) |
| 289 | { |
| 290 | aResp->res.jsonValue["Model"] = *model; |
| 291 | } |
| 292 | |
| 293 | if (partNumber != nullptr) |
| 294 | { |
| 295 | aResp->res.jsonValue["PartNumber"] = *partNumber; |
| 296 | } |
| 297 | |
| 298 | if (serialNumber != nullptr) |
| 299 | { |
| 300 | aResp->res.jsonValue["SerialNumber"] = *serialNumber; |
| 301 | } |
| 302 | |
| 303 | if (sparePartNumber != nullptr && !sparePartNumber->empty()) |
| 304 | { |
| 305 | aResp->res.jsonValue["SparePartNumber"] = *sparePartNumber; |
| 306 | } |
| 307 | }); |
| 308 | } |
| 309 | |
Lakshmi Yadlapati | 543f9a7 | 2023-03-10 17:06:05 -0600 | [diff] [blame] | 310 | inline void addPCIeDeviceProperties( |
Lakshmi Yadlapati | 35ad613 | 2023-03-10 22:31:49 -0600 | [diff] [blame] | 311 | crow::Response& resp, const std::string& pcieDeviceId, |
Lakshmi Yadlapati | 543f9a7 | 2023-03-10 17:06:05 -0600 | [diff] [blame] | 312 | const dbus::utility::DBusPropertiesMap& pcieDevProperties) |
| 313 | { |
Lakshmi Yadlapati | 543f9a7 | 2023-03-10 17:06:05 -0600 | [diff] [blame] | 314 | const std::string* deviceType = nullptr; |
| 315 | const std::string* generationInUse = nullptr; |
| 316 | const int64_t* lanesInUse = nullptr; |
| 317 | |
| 318 | const bool success = sdbusplus::unpackPropertiesNoThrow( |
| 319 | dbus_utils::UnpackErrorPrinter(), pcieDevProperties, "DeviceType", |
| 320 | deviceType, "GenerationInUse", generationInUse, "LanesInUse", |
Lakshmi Yadlapati | bad2c4a | 2023-04-07 09:14:37 -0500 | [diff] [blame] | 321 | lanesInUse); |
Lakshmi Yadlapati | 543f9a7 | 2023-03-10 17:06:05 -0600 | [diff] [blame] | 322 | |
| 323 | if (!success) |
| 324 | { |
| 325 | messages::internalError(resp); |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | if (deviceType != nullptr && !deviceType->empty()) |
| 330 | { |
| 331 | resp.jsonValue["PCIeInterface"]["DeviceType"] = *deviceType; |
| 332 | } |
| 333 | |
| 334 | if (generationInUse != nullptr) |
| 335 | { |
| 336 | std::optional<pcie_device::PCIeTypes> redfishGenerationInUse = |
| 337 | redfishPcieGenerationFromDbus(*generationInUse); |
| 338 | |
| 339 | if (!redfishGenerationInUse) |
| 340 | { |
| 341 | messages::internalError(resp); |
| 342 | return; |
| 343 | } |
| 344 | if (*redfishGenerationInUse != pcie_device::PCIeTypes::Invalid) |
| 345 | { |
| 346 | resp.jsonValue["PCIeInterface"]["PCIeType"] = |
| 347 | *redfishGenerationInUse; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | // The default value of LanesInUse is 0, and the field will be |
| 352 | // left as off if it is a default value. |
| 353 | if (lanesInUse != nullptr && *lanesInUse != 0) |
| 354 | { |
| 355 | resp.jsonValue["PCIeInterface"]["LanesInUse"] = *lanesInUse; |
| 356 | } |
| 357 | |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 358 | resp.jsonValue["PCIeFunctions"]["@odata.id"] = boost::urls::format( |
| 359 | "/redfish/v1/Systems/system/PCIeDevices/{}/PCIeFunctions", |
| 360 | pcieDeviceId); |
Lakshmi Yadlapati | 543f9a7 | 2023-03-10 17:06:05 -0600 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | inline void getPCIeDeviceProperties( |
| 364 | const std::shared_ptr<bmcweb::AsyncResp>& aResp, |
| 365 | const std::string& pcieDevicePath, const std::string& service, |
| 366 | const std::function<void( |
| 367 | const dbus::utility::DBusPropertiesMap& pcieDevProperties)>&& callback) |
| 368 | { |
| 369 | sdbusplus::asio::getAllProperties( |
| 370 | *crow::connections::systemBus, service, pcieDevicePath, |
| 371 | "xyz.openbmc_project.Inventory.Item.PCIeDevice", |
| 372 | [aResp, |
| 373 | callback](const boost::system::error_code& ec, |
| 374 | const dbus::utility::DBusPropertiesMap& pcieDevProperties) { |
| 375 | if (ec) |
| 376 | { |
| 377 | if (ec.value() != EBADR) |
| 378 | { |
| 379 | BMCWEB_LOG_ERROR << "DBUS response error for Properties"; |
| 380 | messages::internalError(aResp->res); |
| 381 | } |
| 382 | return; |
| 383 | } |
| 384 | callback(pcieDevProperties); |
| 385 | }); |
| 386 | } |
| 387 | |
| 388 | inline void addPCIeDeviceCommonProperties( |
| 389 | const std::shared_ptr<bmcweb::AsyncResp>& aResp, |
| 390 | const std::string& pcieDeviceId) |
| 391 | { |
| 392 | aResp->res.addHeader( |
| 393 | boost::beast::http::field::link, |
| 394 | "</redfish/v1/JsonSchemas/PCIeDevice/PCIeDevice.json>; rel=describedby"); |
| 395 | aResp->res.jsonValue["@odata.type"] = "#PCIeDevice.v1_9_0.PCIeDevice"; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 396 | aResp->res.jsonValue["@odata.id"] = boost::urls::format( |
| 397 | "/redfish/v1/Systems/system/PCIeDevices/{}", pcieDeviceId); |
Lakshmi Yadlapati | 543f9a7 | 2023-03-10 17:06:05 -0600 | [diff] [blame] | 398 | aResp->res.jsonValue["Name"] = "PCIe Device"; |
| 399 | aResp->res.jsonValue["Id"] = pcieDeviceId; |
Lakshmi Yadlapati | c6bb328 | 2023-04-12 16:56:49 -0500 | [diff] [blame] | 400 | aResp->res.jsonValue["Status"]["State"] = "Enabled"; |
Lakshmi Yadlapati | 543f9a7 | 2023-03-10 17:06:05 -0600 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | inline void handlePCIeDeviceGet(App& app, const crow::Request& req, |
| 404 | const std::shared_ptr<bmcweb::AsyncResp>& aResp, |
| 405 | const std::string& systemName, |
| 406 | const std::string& pcieDeviceId) |
| 407 | { |
| 408 | if (!redfish::setUpRedfishRoute(app, req, aResp)) |
| 409 | { |
| 410 | return; |
| 411 | } |
| 412 | if (systemName != "system") |
| 413 | { |
| 414 | messages::resourceNotFound(aResp->res, "ComputerSystem", systemName); |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | getValidPCIeDevicePath( |
| 419 | pcieDeviceId, aResp, |
| 420 | [aResp, pcieDeviceId](const std::string& pcieDevicePath, |
| 421 | const std::string& service) { |
| 422 | addPCIeDeviceCommonProperties(aResp, pcieDeviceId); |
SunnySrivastava1984 | 913e773 | 2021-01-27 06:23:24 -0600 | [diff] [blame] | 423 | getPCIeDeviceAsset(aResp, pcieDevicePath, service); |
Lakshmi Yadlapati | c6bb328 | 2023-04-12 16:56:49 -0500 | [diff] [blame] | 424 | getPCIeDeviceState(aResp, pcieDevicePath, service); |
Lakshmi Yadlapati | 543f9a7 | 2023-03-10 17:06:05 -0600 | [diff] [blame] | 425 | getPCIeDeviceProperties( |
| 426 | aResp, pcieDevicePath, service, |
Lakshmi Yadlapati | 35ad613 | 2023-03-10 22:31:49 -0600 | [diff] [blame] | 427 | [aResp, pcieDeviceId]( |
| 428 | const dbus::utility::DBusPropertiesMap& pcieDevProperties) { |
| 429 | addPCIeDeviceProperties(aResp->res, pcieDeviceId, |
| 430 | pcieDevProperties); |
Lakshmi Yadlapati | 543f9a7 | 2023-03-10 17:06:05 -0600 | [diff] [blame] | 431 | }); |
| 432 | }); |
| 433 | } |
| 434 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 435 | inline void requestRoutesSystemPCIeDevice(App& app) |
Jason M. Bills | f5c9f8b | 2018-12-18 16:51:18 -0800 | [diff] [blame] | 436 | { |
Ed Tanous | 22d268c | 2022-05-19 09:39:07 -0700 | [diff] [blame] | 437 | BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/PCIeDevices/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 438 | .privileges(redfish::privileges::getPCIeDevice) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 439 | .methods(boost::beast::http::verb::get)( |
Lakshmi Yadlapati | 543f9a7 | 2023-03-10 17:06:05 -0600 | [diff] [blame] | 440 | std::bind_front(handlePCIeDeviceGet, std::ref(app))); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Lakshmi Yadlapati | 35ad613 | 2023-03-10 22:31:49 -0600 | [diff] [blame] | 443 | inline void addPCIeFunctionList( |
| 444 | crow::Response& res, const std::string& pcieDeviceId, |
| 445 | const dbus::utility::DBusPropertiesMap& pcieDevProperties) |
| 446 | { |
| 447 | nlohmann::json& pcieFunctionList = res.jsonValue["Members"]; |
| 448 | pcieFunctionList = nlohmann::json::array(); |
| 449 | static constexpr const int maxPciFunctionNum = 8; |
| 450 | |
| 451 | for (int functionNum = 0; functionNum < maxPciFunctionNum; functionNum++) |
| 452 | { |
| 453 | // Check if this function exists by |
| 454 | // looking for a device ID |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 455 | std::string devIDProperty = "Function" + std::to_string(functionNum) + |
| 456 | "DeviceId"; |
Lakshmi Yadlapati | 35ad613 | 2023-03-10 22:31:49 -0600 | [diff] [blame] | 457 | const std::string* property = nullptr; |
| 458 | for (const auto& propEntry : pcieDevProperties) |
| 459 | { |
| 460 | if (propEntry.first == devIDProperty) |
| 461 | { |
| 462 | property = std::get_if<std::string>(&propEntry.second); |
| 463 | break; |
| 464 | } |
| 465 | } |
| 466 | if (property == nullptr || property->empty()) |
| 467 | { |
| 468 | continue; |
| 469 | } |
| 470 | |
| 471 | nlohmann::json::object_t pcieFunction; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 472 | pcieFunction["@odata.id"] = boost::urls::format( |
| 473 | "/redfish/v1/Systems/system/PCIeDevices/{}/PCIeFunctions/{}", |
| 474 | pcieDeviceId, std::to_string(functionNum)); |
Patrick Williams | b2ba307 | 2023-05-12 10:27:39 -0500 | [diff] [blame] | 475 | pcieFunctionList.emplace_back(std::move(pcieFunction)); |
Lakshmi Yadlapati | 35ad613 | 2023-03-10 22:31:49 -0600 | [diff] [blame] | 476 | } |
| 477 | res.jsonValue["PCIeFunctions@odata.count"] = pcieFunctionList.size(); |
| 478 | } |
| 479 | |
| 480 | inline void handlePCIeFunctionCollectionGet( |
| 481 | App& app, const crow::Request& req, |
| 482 | const std::shared_ptr<bmcweb::AsyncResp>& aResp, |
| 483 | const std::string& pcieDeviceId) |
| 484 | { |
| 485 | if (!redfish::setUpRedfishRoute(app, req, aResp)) |
| 486 | { |
| 487 | return; |
| 488 | } |
| 489 | |
| 490 | getValidPCIeDevicePath( |
| 491 | pcieDeviceId, aResp, |
| 492 | [aResp, pcieDeviceId](const std::string& pcieDevicePath, |
| 493 | const std::string& service) { |
| 494 | aResp->res.addHeader( |
| 495 | boost::beast::http::field::link, |
| 496 | "</redfish/v1/JsonSchemas/PCIeFunctionCollection/PCIeFunctionCollection.json>; rel=describedby"); |
| 497 | aResp->res.jsonValue["@odata.type"] = |
| 498 | "#PCIeFunctionCollection.PCIeFunctionCollection"; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 499 | aResp->res.jsonValue["@odata.id"] = boost::urls::format( |
| 500 | "/redfish/v1/Systems/system/PCIeDevices/{}/PCIeFunctions", |
| 501 | pcieDeviceId); |
Lakshmi Yadlapati | 35ad613 | 2023-03-10 22:31:49 -0600 | [diff] [blame] | 502 | aResp->res.jsonValue["Name"] = "PCIe Function Collection"; |
| 503 | aResp->res.jsonValue["Description"] = |
| 504 | "Collection of PCIe Functions for PCIe Device " + pcieDeviceId; |
| 505 | getPCIeDeviceProperties( |
| 506 | aResp, pcieDevicePath, service, |
| 507 | [aResp, pcieDeviceId]( |
| 508 | const dbus::utility::DBusPropertiesMap& pcieDevProperties) { |
| 509 | addPCIeFunctionList(aResp->res, pcieDeviceId, pcieDevProperties); |
| 510 | }); |
| 511 | }); |
| 512 | } |
| 513 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 514 | inline void requestRoutesSystemPCIeFunctionCollection(App& app) |
| 515 | { |
| 516 | /** |
| 517 | * Functions triggers appropriate requests on DBus |
| 518 | */ |
| 519 | BMCWEB_ROUTE(app, |
| 520 | "/redfish/v1/Systems/system/PCIeDevices/<str>/PCIeFunctions/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 521 | .privileges(redfish::privileges::getPCIeFunctionCollection) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 522 | .methods(boost::beast::http::verb::get)( |
Lakshmi Yadlapati | 35ad613 | 2023-03-10 22:31:49 -0600 | [diff] [blame] | 523 | std::bind_front(handlePCIeFunctionCollectionGet, std::ref(app))); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Lakshmi Yadlapati | 727a046 | 2023-03-10 23:49:00 -0600 | [diff] [blame] | 526 | inline bool validatePCIeFunctionId( |
Myung Bae | d5e74b8 | 2023-05-31 11:28:02 -0500 | [diff] [blame] | 527 | uint64_t pcieFunctionId, |
Lakshmi Yadlapati | 727a046 | 2023-03-10 23:49:00 -0600 | [diff] [blame] | 528 | const dbus::utility::DBusPropertiesMap& pcieDevProperties) |
| 529 | { |
Myung Bae | d5e74b8 | 2023-05-31 11:28:02 -0500 | [diff] [blame] | 530 | std::string functionName = "Function" + std::to_string(pcieFunctionId); |
Lakshmi Yadlapati | 727a046 | 2023-03-10 23:49:00 -0600 | [diff] [blame] | 531 | std::string devIDProperty = functionName + "DeviceId"; |
| 532 | |
| 533 | const std::string* devIdProperty = nullptr; |
| 534 | for (const auto& property : pcieDevProperties) |
| 535 | { |
| 536 | if (property.first == devIDProperty) |
| 537 | { |
| 538 | devIdProperty = std::get_if<std::string>(&property.second); |
| 539 | break; |
| 540 | } |
| 541 | } |
| 542 | return (devIdProperty != nullptr && !devIdProperty->empty()); |
| 543 | } |
| 544 | |
| 545 | inline void addPCIeFunctionProperties( |
Ed Tanous | e14742c | 2023-05-31 10:27:49 -0700 | [diff] [blame] | 546 | crow::Response& resp, uint64_t pcieFunctionId, |
Lakshmi Yadlapati | 727a046 | 2023-03-10 23:49:00 -0600 | [diff] [blame] | 547 | const dbus::utility::DBusPropertiesMap& pcieDevProperties) |
| 548 | { |
Ed Tanous | e14742c | 2023-05-31 10:27:49 -0700 | [diff] [blame] | 549 | std::string functionName = "Function" + std::to_string(pcieFunctionId); |
Lakshmi Yadlapati | 727a046 | 2023-03-10 23:49:00 -0600 | [diff] [blame] | 550 | for (const auto& property : pcieDevProperties) |
| 551 | { |
| 552 | const std::string* strProperty = |
| 553 | std::get_if<std::string>(&property.second); |
| 554 | |
| 555 | if (property.first == functionName + "DeviceId") |
| 556 | { |
| 557 | resp.jsonValue["DeviceId"] = *strProperty; |
| 558 | } |
| 559 | if (property.first == functionName + "VendorId") |
| 560 | { |
| 561 | resp.jsonValue["VendorId"] = *strProperty; |
| 562 | } |
| 563 | // TODO: FunctionType and DeviceClass are Redfish enums. The D-Bus |
| 564 | // property strings should be mapped correctly to ensure these |
| 565 | // strings are Redfish enum values. For now just check for empty. |
| 566 | if (property.first == functionName + "FunctionType") |
| 567 | { |
| 568 | if (!strProperty->empty()) |
| 569 | { |
| 570 | resp.jsonValue["FunctionType"] = *strProperty; |
| 571 | } |
| 572 | } |
| 573 | if (property.first == functionName + "DeviceClass") |
| 574 | { |
| 575 | if (!strProperty->empty()) |
| 576 | { |
| 577 | resp.jsonValue["DeviceClass"] = *strProperty; |
| 578 | } |
| 579 | } |
| 580 | if (property.first == functionName + "ClassCode") |
| 581 | { |
| 582 | resp.jsonValue["ClassCode"] = *strProperty; |
| 583 | } |
| 584 | if (property.first == functionName + "RevisionId") |
| 585 | { |
| 586 | resp.jsonValue["RevisionId"] = *strProperty; |
| 587 | } |
| 588 | if (property.first == functionName + "SubsystemId") |
| 589 | { |
| 590 | resp.jsonValue["SubsystemId"] = *strProperty; |
| 591 | } |
| 592 | if (property.first == functionName + "SubsystemVendorId") |
| 593 | { |
| 594 | resp.jsonValue["SubsystemVendorId"] = *strProperty; |
| 595 | } |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | inline void addPCIeFunctionCommonProperties(crow::Response& resp, |
| 600 | const std::string& pcieDeviceId, |
Ed Tanous | e14742c | 2023-05-31 10:27:49 -0700 | [diff] [blame] | 601 | uint64_t pcieFunctionId) |
Lakshmi Yadlapati | 727a046 | 2023-03-10 23:49:00 -0600 | [diff] [blame] | 602 | { |
| 603 | resp.addHeader( |
| 604 | boost::beast::http::field::link, |
| 605 | "</redfish/v1/JsonSchemas/PCIeFunction/PCIeFunction.json>; rel=describedby"); |
| 606 | resp.jsonValue["@odata.type"] = "#PCIeFunction.v1_2_3.PCIeFunction"; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 607 | resp.jsonValue["@odata.id"] = boost::urls::format( |
| 608 | "/redfish/v1/Systems/system/PCIeDevices/{}/PCIeFunctions/{}", |
| 609 | pcieDeviceId, pcieFunctionId); |
Lakshmi Yadlapati | 727a046 | 2023-03-10 23:49:00 -0600 | [diff] [blame] | 610 | resp.jsonValue["Name"] = "PCIe Function"; |
Ed Tanous | e14742c | 2023-05-31 10:27:49 -0700 | [diff] [blame] | 611 | resp.jsonValue["Id"] = std::to_string(pcieFunctionId); |
| 612 | resp.jsonValue["FunctionId"] = pcieFunctionId; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 613 | resp.jsonValue["Links"]["PCIeDevice"]["@odata.id"] = boost::urls::format( |
| 614 | "/redfish/v1/Systems/system/PCIeDevices/{}", pcieDeviceId); |
Lakshmi Yadlapati | 727a046 | 2023-03-10 23:49:00 -0600 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | inline void |
| 618 | handlePCIeFunctionGet(App& app, const crow::Request& req, |
| 619 | const std::shared_ptr<bmcweb::AsyncResp>& aResp, |
| 620 | const std::string& pcieDeviceId, |
Ed Tanous | e14742c | 2023-05-31 10:27:49 -0700 | [diff] [blame] | 621 | const std::string& pcieFunctionIdStr) |
Lakshmi Yadlapati | 727a046 | 2023-03-10 23:49:00 -0600 | [diff] [blame] | 622 | { |
| 623 | if (!redfish::setUpRedfishRoute(app, req, aResp)) |
| 624 | { |
| 625 | return; |
| 626 | } |
Ed Tanous | e14742c | 2023-05-31 10:27:49 -0700 | [diff] [blame] | 627 | uint64_t pcieFunctionId = 0; |
| 628 | std::from_chars_result result = std::from_chars( |
| 629 | &*pcieFunctionIdStr.begin(), &*pcieFunctionIdStr.end(), pcieFunctionId); |
| 630 | if (result.ec != std::errc{} || result.ptr != &*pcieFunctionIdStr.end()) |
| 631 | { |
| 632 | messages::resourceNotFound(aResp->res, "PCIeFunction", |
| 633 | pcieFunctionIdStr); |
| 634 | return; |
| 635 | } |
Lakshmi Yadlapati | 727a046 | 2023-03-10 23:49:00 -0600 | [diff] [blame] | 636 | |
| 637 | getValidPCIeDevicePath( |
| 638 | pcieDeviceId, aResp, |
| 639 | [aResp, pcieDeviceId, pcieFunctionId](const std::string& pcieDevicePath, |
| 640 | const std::string& service) { |
| 641 | getPCIeDeviceProperties( |
| 642 | aResp, pcieDevicePath, service, |
| 643 | [aResp, pcieDeviceId, pcieFunctionId]( |
| 644 | const dbus::utility::DBusPropertiesMap& pcieDevProperties) { |
Myung Bae | d5e74b8 | 2023-05-31 11:28:02 -0500 | [diff] [blame] | 645 | if (!validatePCIeFunctionId(pcieFunctionId, pcieDevProperties)) |
| 646 | { |
| 647 | messages::resourceNotFound(aResp->res, "PCIeFunction", |
| 648 | std::to_string(pcieFunctionId)); |
| 649 | return; |
| 650 | } |
Lakshmi Yadlapati | 727a046 | 2023-03-10 23:49:00 -0600 | [diff] [blame] | 651 | addPCIeFunctionCommonProperties(aResp->res, pcieDeviceId, |
| 652 | pcieFunctionId); |
| 653 | addPCIeFunctionProperties(aResp->res, pcieFunctionId, |
| 654 | pcieDevProperties); |
| 655 | }); |
| 656 | }); |
| 657 | } |
| 658 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 659 | inline void requestRoutesSystemPCIeFunction(App& app) |
| 660 | { |
| 661 | BMCWEB_ROUTE( |
| 662 | app, |
| 663 | "/redfish/v1/Systems/system/PCIeDevices/<str>/PCIeFunctions/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 664 | .privileges(redfish::privileges::getPCIeFunction) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 665 | .methods(boost::beast::http::verb::get)( |
Lakshmi Yadlapati | 727a046 | 2023-03-10 23:49:00 -0600 | [diff] [blame] | 666 | std::bind_front(handlePCIeFunctionGet, std::ref(app))); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 667 | } |
Jason M. Bills | f5c9f8b | 2018-12-18 16:51:18 -0800 | [diff] [blame] | 668 | |
| 669 | } // namespace redfish |