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