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