blob: ece84a31778797411a66b97cc52a9ab4bdc2dae1 [file] [log] [blame]
Chicago Duan7691cc22021-01-25 19:52:35 +08001#pragma once
2
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08003#include "app.hpp"
George Liue99073f2022-12-09 11:06:16 +08004#include "dbus_utility.hpp"
Chicago Duan7691cc22021-01-25 19:52:35 +08005#include "error_messages.hpp"
Ed Tanous4ce2c1b2023-02-10 11:57:54 -08006#include "generated/enums/pcie_slots.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08007#include "pcie.hpp"
8#include "registries/privilege_registry.hpp"
Chicago Duan7691cc22021-01-25 19:52:35 +08009#include "utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080010#include "utils/dbus_utils.hpp"
11#include "utils/json_utils.hpp"
Chicago Duan7691cc22021-01-25 19:52:35 +080012
George Liue99073f2022-12-09 11:06:16 +080013#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070014#include <boost/url/format.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020015#include <sdbusplus/asio/property.hpp>
16#include <sdbusplus/unpack_properties.hpp>
Chicago Duan7691cc22021-01-25 19:52:35 +080017
George Liue99073f2022-12-09 11:06:16 +080018#include <array>
19#include <string_view>
20
Chicago Duan7691cc22021-01-25 19:52:35 +080021namespace redfish
22{
23
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080024inline pcie_slots::SlotTypes dbusSlotTypeToRf(const std::string& slotType)
Chicago Duan7691cc22021-01-25 19:52:35 +080025{
26 if (slotType ==
27 "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.FullLength")
28 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080029 return pcie_slots::SlotTypes::FullLength;
Chicago Duan7691cc22021-01-25 19:52:35 +080030 }
31 if (slotType ==
32 "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.HalfLength")
33 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080034 return pcie_slots::SlotTypes::HalfLength;
Chicago Duan7691cc22021-01-25 19:52:35 +080035 }
36 if (slotType ==
37 "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.LowProfile")
38 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080039 return pcie_slots::SlotTypes::LowProfile;
Chicago Duan7691cc22021-01-25 19:52:35 +080040 }
41 if (slotType ==
42 "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.Mini")
43 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080044 return pcie_slots::SlotTypes::Mini;
Chicago Duan7691cc22021-01-25 19:52:35 +080045 }
46 if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.M_2")
47 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080048 return pcie_slots::SlotTypes::M2;
Chicago Duan7691cc22021-01-25 19:52:35 +080049 }
50 if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OEM")
51 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080052 return pcie_slots::SlotTypes::OEM;
Chicago Duan7691cc22021-01-25 19:52:35 +080053 }
54 if (slotType ==
55 "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OCP3Small")
56 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080057 return pcie_slots::SlotTypes::OCP3Small;
Chicago Duan7691cc22021-01-25 19:52:35 +080058 }
59 if (slotType ==
60 "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OCP3Large")
61 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080062 return pcie_slots::SlotTypes::OCP3Large;
Chicago Duan7691cc22021-01-25 19:52:35 +080063 }
64 if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.U_2")
65 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080066 return pcie_slots::SlotTypes::U2;
Chicago Duan7691cc22021-01-25 19:52:35 +080067 }
68
69 // Unknown or others
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080070 return pcie_slots::SlotTypes::Invalid;
Chicago Duan7691cc22021-01-25 19:52:35 +080071}
72
73inline void
74 onPcieSlotGetAllDone(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080075 const boost::system::error_code& ec,
Chicago Duan7691cc22021-01-25 19:52:35 +080076 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 Grobelnyd1bde9e2022-09-07 10:40:51 +020098 const std::string* generation = nullptr;
99 const size_t* lanes = nullptr;
100 const std::string* slotType = nullptr;
101 const bool* hotPluggable = nullptr;
Chicago Duan7691cc22021-01-25 19:52:35 +0800102
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200103 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 Duan7691cc22021-01-25 19:52:35 +0800112 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200113
114 if (generation != nullptr)
115 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700116 std::optional<pcie_device::PCIeTypes> pcieType =
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200117 redfishPcieGenerationFromDbus(*generation);
118 if (!pcieType)
119 {
120 messages::internalError(asyncResp->res);
121 return;
122 }
Lakshmi Yadlapatibbf372a2023-05-02 15:09:08 -0500123 if (*pcieType != pcie_device::PCIeTypes::Invalid)
124 {
125 slot["PCIeType"] = *pcieType;
126 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200127 }
128
129 if (lanes != nullptr)
130 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200131 slot["Lanes"] = *lanes;
132 }
133
134 if (slotType != nullptr)
135 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -0800136 pcie_slots::SlotTypes redfishSlotType = dbusSlotTypeToRf(*slotType);
137 if (redfishSlotType == pcie_slots::SlotTypes::Invalid)
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200138 {
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 Duan7691cc22021-01-25 19:52:35 +0800150 slots.emplace_back(std::move(slot));
151}
152
153inline void onMapperAssociationDone(
154 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
155 const std::string& chassisID, const std::string& pcieSlotPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800156 const std::string& connectionName, const boost::system::error_code& ec,
George Liu6c3e9452023-03-03 13:55:29 +0800157 const dbus::utility::MapperEndPoints& pcieSlotChassis)
Chicago Duan7691cc22021-01-25 19:52:35 +0800158{
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 Liu6c3e9452023-03-03 13:55:29 +0800171 if (pcieSlotChassis.size() != 1)
Chicago Duan7691cc22021-01-25 19:52:35 +0800172 {
173 BMCWEB_LOG_ERROR << "PCIe Slot association error! ";
174 messages::internalError(asyncResp->res);
175 return;
176 }
177
George Liu6c3e9452023-03-03 13:55:29 +0800178 sdbusplus::message::object_path path(pcieSlotChassis[0]);
Chicago Duan7691cc22021-01-25 19:52:35 +0800179 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 Grobelnyd1bde9e2022-09-07 10:40:51 +0200186 sdbusplus::asio::getAllProperties(
187 *crow::connections::systemBus, connectionName, pcieSlotPath,
188 "xyz.openbmc_project.Inventory.Item.PCIeSlot",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800189 [asyncResp](const boost::system::error_code& ec2,
Chicago Duan7691cc22021-01-25 19:52:35 +0800190 const dbus::utility::DBusPropertiesMap& propertiesList) {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -0800191 onPcieSlotGetAllDone(asyncResp, ec2, propertiesList);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200192 });
Chicago Duan7691cc22021-01-25 19:52:35 +0800193}
194
195inline void
196 onMapperSubtreeDone(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
197 const std::string& chassisID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800198 const boost::system::error_code& ec,
Chicago Duan7691cc22021-01-25 19:52:35 +0800199 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 Zhaod8a5d5d2022-08-05 16:21:51 +0800209 messages::resourceNotFound(asyncResp->res, "Chassis", chassisID);
Chicago Duan7691cc22021-01-25 19:52:35 +0800210 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 Tanousef4c65b2023-04-24 15:28:50 -0700218 asyncResp->res.jsonValue["@odata.id"] =
219 boost::urls::format("/redfish/v1/Chassis/{}/PCIeSlots", chassisID);
Chicago Duan7691cc22021-01-25 19:52:35 +0800220 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 Liu6c3e9452023-03-03 13:55:29 +0800235 dbus::utility::getAssociationEndPoints(
236 std::string{pcieSlotAssociationPath},
Chicago Duan7691cc22021-01-25 19:52:35 +0800237 [asyncResp, chassisID, pcieSlotPath, connectionName](
Ed Tanous4ce2c1b2023-02-10 11:57:54 -0800238 const boost::system::error_code& ec2,
George Liu6c3e9452023-03-03 13:55:29 +0800239 const dbus::utility::MapperEndPoints& endpoints) {
Chicago Duan7691cc22021-01-25 19:52:35 +0800240 onMapperAssociationDone(asyncResp, chassisID, pcieSlotPath,
Ed Tanous4ce2c1b2023-02-10 11:57:54 -0800241 connectionName, ec2, endpoints);
George Liu6c3e9452023-03-03 13:55:29 +0800242 });
Chicago Duan7691cc22021-01-25 19:52:35 +0800243 }
244 }
245}
246
247inline 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 Tanous2aacf852022-06-17 08:07:54 -0700252 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Chicago Duan7691cc22021-01-25 19:52:35 +0800253 {
254 return;
255 }
256
George Liue99073f2022-12-09 11:06:16 +0800257 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 Duan7691cc22021-01-25 19:52:35 +0800261 [asyncResp,
George Liue99073f2022-12-09 11:06:16 +0800262 chassisID](const boost::system::error_code& ec,
Chicago Duan7691cc22021-01-25 19:52:35 +0800263 const dbus::utility::MapperGetSubTreeResponse& subtree) {
264 onMapperSubtreeDone(asyncResp, chassisID, ec, subtree);
George Liue99073f2022-12-09 11:06:16 +0800265 });
Chicago Duan7691cc22021-01-25 19:52:35 +0800266}
267
268inline 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