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