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