| Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 | 
|  | 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 3 | #pragma once | 
|  | 4 |  | 
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 5 | #include "app.hpp" | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 6 | #include "async_resp.hpp" | 
| George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 7 | #include "dbus_utility.hpp" | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 8 | #include "error_messages.hpp" | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 9 | #include "generated/enums/pcie_device.hpp" | 
| Ed Tanous | 4ce2c1b | 2023-02-10 11:57:54 -0800 | [diff] [blame] | 10 | #include "generated/enums/pcie_slots.hpp" | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 11 | #include "http_request.hpp" | 
|  | 12 | #include "logging.hpp" | 
| Lakshmi Yadlapati | c49c329 | 2023-04-19 16:42:35 -0500 | [diff] [blame] | 13 | #include "query.hpp" | 
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 14 | #include "registries/privilege_registry.hpp" | 
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 15 | #include "utils/dbus_utils.hpp" | 
| Lakshmi Yadlapati | c49c329 | 2023-04-19 16:42:35 -0500 | [diff] [blame] | 16 | #include "utils/pcie_util.hpp" | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 17 |  | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 18 | #include <asm-generic/errno.h> | 
|  | 19 |  | 
|  | 20 | #include <boost/beast/http/verb.hpp> | 
| George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 21 | #include <boost/system/error_code.hpp> | 
| Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 22 | #include <boost/url/format.hpp> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 23 | #include <nlohmann/json.hpp> | 
|  | 24 | #include <sdbusplus/message/native_types.hpp> | 
| Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 25 | #include <sdbusplus/unpack_properties.hpp> | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 26 |  | 
| George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 27 | #include <array> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 28 | #include <cstddef> | 
|  | 29 | #include <functional> | 
|  | 30 | #include <memory> | 
|  | 31 | #include <optional> | 
|  | 32 | #include <string> | 
| George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 33 | #include <string_view> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 34 | #include <utility> | 
| George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 35 |  | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 36 | namespace redfish | 
|  | 37 | { | 
|  | 38 |  | 
| Patrick Williams | 504af5a | 2025-02-03 14:29:03 -0500 | [diff] [blame] | 39 | inline void onPcieSlotGetAllDone( | 
|  | 40 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, | 
|  | 41 | const boost::system::error_code& ec, | 
|  | 42 | const dbus::utility::DBusPropertiesMap& propertiesList) | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 43 | { | 
|  | 44 | if (ec) | 
|  | 45 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 46 | BMCWEB_LOG_ERROR("Can't get PCIeSlot properties!"); | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 47 | messages::internalError(asyncResp->res); | 
|  | 48 | return; | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | nlohmann::json& slots = asyncResp->res.jsonValue["Slots"]; | 
|  | 52 |  | 
|  | 53 | nlohmann::json::array_t* slotsPtr = | 
|  | 54 | slots.get_ptr<nlohmann::json::array_t*>(); | 
|  | 55 | if (slotsPtr == nullptr) | 
|  | 56 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 57 | BMCWEB_LOG_ERROR("Slots key isn't an array???"); | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 58 | messages::internalError(asyncResp->res); | 
|  | 59 | return; | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | nlohmann::json::object_t slot; | 
|  | 63 |  | 
| Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 64 | const std::string* generation = nullptr; | 
|  | 65 | const size_t* lanes = nullptr; | 
|  | 66 | const std::string* slotType = nullptr; | 
|  | 67 | const bool* hotPluggable = nullptr; | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 68 |  | 
| Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 69 | const bool success = sdbusplus::unpackPropertiesNoThrow( | 
|  | 70 | dbus_utils::UnpackErrorPrinter(), propertiesList, "Generation", | 
|  | 71 | generation, "Lanes", lanes, "SlotType", slotType, "HotPluggable", | 
|  | 72 | hotPluggable); | 
|  | 73 |  | 
|  | 74 | if (!success) | 
|  | 75 | { | 
|  | 76 | messages::internalError(asyncResp->res); | 
|  | 77 | return; | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 78 | } | 
| Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 79 |  | 
|  | 80 | if (generation != nullptr) | 
|  | 81 | { | 
| Ed Tanous | 0ec8b83 | 2022-03-14 14:56:47 -0700 | [diff] [blame] | 82 | std::optional<pcie_device::PCIeTypes> pcieType = | 
| Lakshmi Yadlapati | c49c329 | 2023-04-19 16:42:35 -0500 | [diff] [blame] | 83 | pcie_util::redfishPcieGenerationFromDbus(*generation); | 
| Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 84 | if (!pcieType) | 
|  | 85 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 86 | BMCWEB_LOG_WARNING("Unknown PCIe Slot Generation: {}", *generation); | 
| Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 87 | } | 
| Lakshmi Yadlapati | cf3b484 | 2023-06-27 02:36:53 -0500 | [diff] [blame] | 88 | else | 
| Lakshmi Yadlapati | bbf372a | 2023-05-02 15:09:08 -0500 | [diff] [blame] | 89 | { | 
| Lakshmi Yadlapati | cf3b484 | 2023-06-27 02:36:53 -0500 | [diff] [blame] | 90 | if (*pcieType == pcie_device::PCIeTypes::Invalid) | 
|  | 91 | { | 
|  | 92 | messages::internalError(asyncResp->res); | 
|  | 93 | return; | 
|  | 94 | } | 
| Lakshmi Yadlapati | bbf372a | 2023-05-02 15:09:08 -0500 | [diff] [blame] | 95 | slot["PCIeType"] = *pcieType; | 
|  | 96 | } | 
| Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 97 | } | 
|  | 98 |  | 
| Konstantin Aladyshev | 82f8032 | 2023-07-10 15:00:38 +0300 | [diff] [blame] | 99 | if (lanes != nullptr && *lanes != 0) | 
| Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 100 | { | 
| Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 101 | slot["Lanes"] = *lanes; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | if (slotType != nullptr) | 
|  | 105 | { | 
| Lakshmi Yadlapati | d87fdd9 | 2023-05-02 16:50:31 -0500 | [diff] [blame] | 106 | std::optional<pcie_slots::SlotTypes> redfishSlotType = | 
| Lakshmi Yadlapati | c49c329 | 2023-04-19 16:42:35 -0500 | [diff] [blame] | 107 | pcie_util::dbusSlotTypeToRf(*slotType); | 
| Lakshmi Yadlapati | d87fdd9 | 2023-05-02 16:50:31 -0500 | [diff] [blame] | 108 | if (!redfishSlotType) | 
| Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 109 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 110 | BMCWEB_LOG_WARNING("Unknown PCIe Slot Type: {}", *slotType); | 
| Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 111 | } | 
| Lakshmi Yadlapati | cf3b484 | 2023-06-27 02:36:53 -0500 | [diff] [blame] | 112 | else | 
| Lakshmi Yadlapati | d87fdd9 | 2023-05-02 16:50:31 -0500 | [diff] [blame] | 113 | { | 
| Lakshmi Yadlapati | cf3b484 | 2023-06-27 02:36:53 -0500 | [diff] [blame] | 114 | if (*redfishSlotType == pcie_slots::SlotTypes::Invalid) | 
|  | 115 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 116 | BMCWEB_LOG_ERROR("Unknown PCIe Slot Type: {}", *slotType); | 
| Lakshmi Yadlapati | cf3b484 | 2023-06-27 02:36:53 -0500 | [diff] [blame] | 117 | messages::internalError(asyncResp->res); | 
|  | 118 | return; | 
|  | 119 | } | 
| Lakshmi Yadlapati | d87fdd9 | 2023-05-02 16:50:31 -0500 | [diff] [blame] | 120 | slot["SlotType"] = *redfishSlotType; | 
|  | 121 | } | 
| Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 122 | } | 
|  | 123 |  | 
|  | 124 | if (hotPluggable != nullptr) | 
|  | 125 | { | 
|  | 126 | slot["HotPluggable"] = *hotPluggable; | 
|  | 127 | } | 
|  | 128 |  | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 129 | slots.emplace_back(std::move(slot)); | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | inline void onMapperAssociationDone( | 
|  | 133 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, | 
|  | 134 | const std::string& chassisID, const std::string& pcieSlotPath, | 
| Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 135 | const std::string& connectionName, const boost::system::error_code& ec, | 
| George Liu | 6c3e945 | 2023-03-03 13:55:29 +0800 | [diff] [blame] | 136 | const dbus::utility::MapperEndPoints& pcieSlotChassis) | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 137 | { | 
|  | 138 | if (ec) | 
|  | 139 | { | 
|  | 140 | if (ec.value() == EBADR) | 
|  | 141 | { | 
|  | 142 | // This PCIeSlot have no chassis association. | 
|  | 143 | return; | 
|  | 144 | } | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 145 | BMCWEB_LOG_ERROR("DBUS response error"); | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 146 | messages::internalError(asyncResp->res); | 
|  | 147 | return; | 
|  | 148 | } | 
|  | 149 |  | 
| George Liu | 6c3e945 | 2023-03-03 13:55:29 +0800 | [diff] [blame] | 150 | if (pcieSlotChassis.size() != 1) | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 151 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 152 | BMCWEB_LOG_ERROR("PCIe Slot association error! "); | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 153 | messages::internalError(asyncResp->res); | 
|  | 154 | return; | 
|  | 155 | } | 
|  | 156 |  | 
| George Liu | 6c3e945 | 2023-03-03 13:55:29 +0800 | [diff] [blame] | 157 | sdbusplus::message::object_path path(pcieSlotChassis[0]); | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 158 | std::string chassisName = path.filename(); | 
|  | 159 | if (chassisName != chassisID) | 
|  | 160 | { | 
|  | 161 | // The pcie slot doesn't belong to the chassisID | 
|  | 162 | return; | 
|  | 163 | } | 
|  | 164 |  | 
| Ed Tanous | deae6a7 | 2024-11-11 21:58:57 -0800 | [diff] [blame] | 165 | dbus::utility::getAllProperties( | 
|  | 166 | connectionName, pcieSlotPath, | 
| Krzysztof Grobelny | d1bde9e | 2022-09-07 10:40:51 +0200 | [diff] [blame] | 167 | "xyz.openbmc_project.Inventory.Item.PCIeSlot", | 
| Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 168 | [asyncResp](const boost::system::error_code& ec2, | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 169 | const dbus::utility::DBusPropertiesMap& propertiesList) { | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 170 | onPcieSlotGetAllDone(asyncResp, ec2, propertiesList); | 
|  | 171 | }); | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 172 | } | 
|  | 173 |  | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 174 | inline void onMapperSubtreeDone( | 
|  | 175 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, | 
|  | 176 | const std::string& chassisID, const boost::system::error_code& ec, | 
|  | 177 | const dbus::utility::MapperGetSubTreeResponse& subtree) | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 178 | { | 
|  | 179 | if (ec) | 
|  | 180 | { | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 181 | BMCWEB_LOG_ERROR("D-Bus response error on GetSubTree {}", ec); | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 182 | messages::internalError(asyncResp->res); | 
|  | 183 | return; | 
|  | 184 | } | 
|  | 185 | if (subtree.empty()) | 
|  | 186 | { | 
| Jiaqing Zhao | d8a5d5d | 2022-08-05 16:21:51 +0800 | [diff] [blame] | 187 | messages::resourceNotFound(asyncResp->res, "Chassis", chassisID); | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 188 | return; | 
|  | 189 | } | 
|  | 190 |  | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 191 | BMCWEB_LOG_DEBUG("Get properties for PCIeSlots associated to chassis = {}", | 
|  | 192 | chassisID); | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 193 |  | 
|  | 194 | asyncResp->res.jsonValue["@odata.type"] = "#PCIeSlots.v1_4_1.PCIeSlots"; | 
|  | 195 | asyncResp->res.jsonValue["Name"] = "PCIe Slot Information"; | 
| Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 196 | asyncResp->res.jsonValue["@odata.id"] = | 
|  | 197 | boost::urls::format("/redfish/v1/Chassis/{}/PCIeSlots", chassisID); | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 198 | asyncResp->res.jsonValue["Id"] = "1"; | 
|  | 199 | asyncResp->res.jsonValue["Slots"] = nlohmann::json::array(); | 
|  | 200 |  | 
|  | 201 | for (const auto& pathServicePair : subtree) | 
|  | 202 | { | 
|  | 203 | const std::string& pcieSlotPath = pathServicePair.first; | 
|  | 204 | for (const auto& connectionInterfacePair : pathServicePair.second) | 
|  | 205 | { | 
|  | 206 | const std::string& connectionName = connectionInterfacePair.first; | 
|  | 207 | sdbusplus::message::object_path pcieSlotAssociationPath( | 
|  | 208 | pcieSlotPath); | 
|  | 209 | pcieSlotAssociationPath /= "chassis"; | 
|  | 210 |  | 
|  | 211 | // The association of this PCIeSlot is used to determine whether | 
|  | 212 | // it belongs to this ChassisID | 
| George Liu | 6c3e945 | 2023-03-03 13:55:29 +0800 | [diff] [blame] | 213 | dbus::utility::getAssociationEndPoints( | 
|  | 214 | std::string{pcieSlotAssociationPath}, | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 215 | [asyncResp, chassisID, pcieSlotPath, connectionName]( | 
| Ed Tanous | 4ce2c1b | 2023-02-10 11:57:54 -0800 | [diff] [blame] | 216 | const boost::system::error_code& ec2, | 
| George Liu | 6c3e945 | 2023-03-03 13:55:29 +0800 | [diff] [blame] | 217 | const dbus::utility::MapperEndPoints& endpoints) { | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 218 | onMapperAssociationDone(asyncResp, chassisID, pcieSlotPath, | 
|  | 219 | connectionName, ec2, endpoints); | 
|  | 220 | }); | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 221 | } | 
|  | 222 | } | 
|  | 223 | } | 
|  | 224 |  | 
|  | 225 | inline void handlePCIeSlotCollectionGet( | 
|  | 226 | crow::App& app, const crow::Request& req, | 
|  | 227 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, | 
|  | 228 | const std::string& chassisID) | 
|  | 229 | { | 
| Ed Tanous | 2aacf85 | 2022-06-17 08:07:54 -0700 | [diff] [blame] | 230 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 231 | { | 
|  | 232 | return; | 
|  | 233 | } | 
|  | 234 |  | 
| George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 235 | constexpr std::array<std::string_view, 1> interfaces = { | 
|  | 236 | "xyz.openbmc_project.Inventory.Item.PCIeSlot"}; | 
|  | 237 | dbus::utility::getSubTree( | 
|  | 238 | "/xyz/openbmc_project/inventory", 0, interfaces, | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 239 | [asyncResp, | 
| George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 240 | chassisID](const boost::system::error_code& ec, | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 241 | const dbus::utility::MapperGetSubTreeResponse& subtree) { | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 242 | onMapperSubtreeDone(asyncResp, chassisID, ec, subtree); | 
|  | 243 | }); | 
| Chicago Duan | 7691cc2 | 2021-01-25 19:52:35 +0800 | [diff] [blame] | 244 | } | 
|  | 245 |  | 
|  | 246 | inline void requestRoutesPCIeSlots(App& app) | 
|  | 247 | { | 
|  | 248 | BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PCIeSlots/") | 
|  | 249 | .privileges(redfish::privileges::getPCIeSlots) | 
|  | 250 | .methods(boost::beast::http::verb::get)( | 
|  | 251 | std::bind_front(handlePCIeSlotCollectionGet, std::ref(app))); | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | } // namespace redfish |