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