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