blob: 18301bfa3aa2b2997c6b9a519089fe98faa6960a [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>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020014#include <sdbusplus/asio/property.hpp>
15#include <sdbusplus/unpack_properties.hpp>
Chicago Duan7691cc22021-01-25 19:52:35 +080016
George Liue99073f2022-12-09 11:06:16 +080017#include <array>
18#include <string_view>
19
Chicago Duan7691cc22021-01-25 19:52:35 +080020namespace redfish
21{
22
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080023inline pcie_slots::SlotTypes dbusSlotTypeToRf(const std::string& slotType)
Chicago Duan7691cc22021-01-25 19:52:35 +080024{
25 if (slotType ==
26 "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.FullLength")
27 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080028 return pcie_slots::SlotTypes::FullLength;
Chicago Duan7691cc22021-01-25 19:52:35 +080029 }
30 if (slotType ==
31 "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.HalfLength")
32 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080033 return pcie_slots::SlotTypes::HalfLength;
Chicago Duan7691cc22021-01-25 19:52:35 +080034 }
35 if (slotType ==
36 "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.LowProfile")
37 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080038 return pcie_slots::SlotTypes::LowProfile;
Chicago Duan7691cc22021-01-25 19:52:35 +080039 }
40 if (slotType ==
41 "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.Mini")
42 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080043 return pcie_slots::SlotTypes::Mini;
Chicago Duan7691cc22021-01-25 19:52:35 +080044 }
45 if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.M_2")
46 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080047 return pcie_slots::SlotTypes::M2;
Chicago Duan7691cc22021-01-25 19:52:35 +080048 }
49 if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OEM")
50 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080051 return pcie_slots::SlotTypes::OEM;
Chicago Duan7691cc22021-01-25 19:52:35 +080052 }
53 if (slotType ==
54 "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OCP3Small")
55 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080056 return pcie_slots::SlotTypes::OCP3Small;
Chicago Duan7691cc22021-01-25 19:52:35 +080057 }
58 if (slotType ==
59 "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OCP3Large")
60 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080061 return pcie_slots::SlotTypes::OCP3Large;
Chicago Duan7691cc22021-01-25 19:52:35 +080062 }
63 if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.U_2")
64 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080065 return pcie_slots::SlotTypes::U2;
Chicago Duan7691cc22021-01-25 19:52:35 +080066 }
67
68 // Unknown or others
Ed Tanous4ce2c1b2023-02-10 11:57:54 -080069 return pcie_slots::SlotTypes::Invalid;
Chicago Duan7691cc22021-01-25 19:52:35 +080070}
71
72inline void
73 onPcieSlotGetAllDone(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080074 const boost::system::error_code& ec,
Chicago Duan7691cc22021-01-25 19:52:35 +080075 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 Grobelnyd1bde9e2022-09-07 10:40:51 +020097 const std::string* generation = nullptr;
98 const size_t* lanes = nullptr;
99 const std::string* slotType = nullptr;
100 const bool* hotPluggable = nullptr;
Chicago Duan7691cc22021-01-25 19:52:35 +0800101
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200102 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 Duan7691cc22021-01-25 19:52:35 +0800111 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200112
113 if (generation != nullptr)
114 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700115 std::optional<pcie_device::PCIeTypes> pcieType =
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200116 redfishPcieGenerationFromDbus(*generation);
117 if (!pcieType)
118 {
119 messages::internalError(asyncResp->res);
120 return;
121 }
Lakshmi Yadlapatibbf372a2023-05-02 15:09:08 -0500122 if (*pcieType != pcie_device::PCIeTypes::Invalid)
123 {
124 slot["PCIeType"] = *pcieType;
125 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200126 }
127
128 if (lanes != nullptr)
129 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200130 slot["Lanes"] = *lanes;
131 }
132
133 if (slotType != nullptr)
134 {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -0800135 pcie_slots::SlotTypes redfishSlotType = dbusSlotTypeToRf(*slotType);
136 if (redfishSlotType == pcie_slots::SlotTypes::Invalid)
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200137 {
138 messages::internalError(asyncResp->res);
139 return;
140 }
141 slot["SlotType"] = redfishSlotType;
142 }
143
144 if (hotPluggable != nullptr)
145 {
146 slot["HotPluggable"] = *hotPluggable;
147 }
148
Chicago Duan7691cc22021-01-25 19:52:35 +0800149 slots.emplace_back(std::move(slot));
150}
151
152inline void onMapperAssociationDone(
153 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
154 const std::string& chassisID, const std::string& pcieSlotPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800155 const std::string& connectionName, const boost::system::error_code& ec,
George Liu6c3e9452023-03-03 13:55:29 +0800156 const dbus::utility::MapperEndPoints& pcieSlotChassis)
Chicago Duan7691cc22021-01-25 19:52:35 +0800157{
158 if (ec)
159 {
160 if (ec.value() == EBADR)
161 {
162 // This PCIeSlot have no chassis association.
163 return;
164 }
165 BMCWEB_LOG_ERROR << "DBUS response error";
166 messages::internalError(asyncResp->res);
167 return;
168 }
169
George Liu6c3e9452023-03-03 13:55:29 +0800170 if (pcieSlotChassis.size() != 1)
Chicago Duan7691cc22021-01-25 19:52:35 +0800171 {
172 BMCWEB_LOG_ERROR << "PCIe Slot association error! ";
173 messages::internalError(asyncResp->res);
174 return;
175 }
176
George Liu6c3e9452023-03-03 13:55:29 +0800177 sdbusplus::message::object_path path(pcieSlotChassis[0]);
Chicago Duan7691cc22021-01-25 19:52:35 +0800178 std::string chassisName = path.filename();
179 if (chassisName != chassisID)
180 {
181 // The pcie slot doesn't belong to the chassisID
182 return;
183 }
184
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200185 sdbusplus::asio::getAllProperties(
186 *crow::connections::systemBus, connectionName, pcieSlotPath,
187 "xyz.openbmc_project.Inventory.Item.PCIeSlot",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800188 [asyncResp](const boost::system::error_code& ec2,
Chicago Duan7691cc22021-01-25 19:52:35 +0800189 const dbus::utility::DBusPropertiesMap& propertiesList) {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -0800190 onPcieSlotGetAllDone(asyncResp, ec2, propertiesList);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200191 });
Chicago Duan7691cc22021-01-25 19:52:35 +0800192}
193
194inline void
195 onMapperSubtreeDone(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
196 const std::string& chassisID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800197 const boost::system::error_code& ec,
Chicago Duan7691cc22021-01-25 19:52:35 +0800198 const dbus::utility::MapperGetSubTreeResponse& subtree)
199{
200 if (ec)
201 {
202 BMCWEB_LOG_ERROR << "D-Bus response error on GetSubTree " << ec;
203 messages::internalError(asyncResp->res);
204 return;
205 }
206 if (subtree.empty())
207 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800208 messages::resourceNotFound(asyncResp->res, "Chassis", chassisID);
Chicago Duan7691cc22021-01-25 19:52:35 +0800209 return;
210 }
211
212 BMCWEB_LOG_DEBUG << "Get properties for PCIeSlots associated to chassis = "
213 << chassisID;
214
215 asyncResp->res.jsonValue["@odata.type"] = "#PCIeSlots.v1_4_1.PCIeSlots";
216 asyncResp->res.jsonValue["Name"] = "PCIe Slot Information";
217 asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
218 "redfish", "v1", "Chassis", chassisID, "PCIeSlots");
219 asyncResp->res.jsonValue["Id"] = "1";
220 asyncResp->res.jsonValue["Slots"] = nlohmann::json::array();
221
222 for (const auto& pathServicePair : subtree)
223 {
224 const std::string& pcieSlotPath = pathServicePair.first;
225 for (const auto& connectionInterfacePair : pathServicePair.second)
226 {
227 const std::string& connectionName = connectionInterfacePair.first;
228 sdbusplus::message::object_path pcieSlotAssociationPath(
229 pcieSlotPath);
230 pcieSlotAssociationPath /= "chassis";
231
232 // The association of this PCIeSlot is used to determine whether
233 // it belongs to this ChassisID
George Liu6c3e9452023-03-03 13:55:29 +0800234 dbus::utility::getAssociationEndPoints(
235 std::string{pcieSlotAssociationPath},
Chicago Duan7691cc22021-01-25 19:52:35 +0800236 [asyncResp, chassisID, pcieSlotPath, connectionName](
Ed Tanous4ce2c1b2023-02-10 11:57:54 -0800237 const boost::system::error_code& ec2,
George Liu6c3e9452023-03-03 13:55:29 +0800238 const dbus::utility::MapperEndPoints& endpoints) {
Chicago Duan7691cc22021-01-25 19:52:35 +0800239 onMapperAssociationDone(asyncResp, chassisID, pcieSlotPath,
Ed Tanous4ce2c1b2023-02-10 11:57:54 -0800240 connectionName, ec2, endpoints);
George Liu6c3e9452023-03-03 13:55:29 +0800241 });
Chicago Duan7691cc22021-01-25 19:52:35 +0800242 }
243 }
244}
245
246inline void handlePCIeSlotCollectionGet(
247 crow::App& app, const crow::Request& req,
248 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
249 const std::string& chassisID)
250{
Ed Tanous2aacf852022-06-17 08:07:54 -0700251 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Chicago Duan7691cc22021-01-25 19:52:35 +0800252 {
253 return;
254 }
255
George Liue99073f2022-12-09 11:06:16 +0800256 constexpr std::array<std::string_view, 1> interfaces = {
257 "xyz.openbmc_project.Inventory.Item.PCIeSlot"};
258 dbus::utility::getSubTree(
259 "/xyz/openbmc_project/inventory", 0, interfaces,
Chicago Duan7691cc22021-01-25 19:52:35 +0800260 [asyncResp,
George Liue99073f2022-12-09 11:06:16 +0800261 chassisID](const boost::system::error_code& ec,
Chicago Duan7691cc22021-01-25 19:52:35 +0800262 const dbus::utility::MapperGetSubTreeResponse& subtree) {
263 onMapperSubtreeDone(asyncResp, chassisID, ec, subtree);
George Liue99073f2022-12-09 11:06:16 +0800264 });
Chicago Duan7691cc22021-01-25 19:52:35 +0800265}
266
267inline void requestRoutesPCIeSlots(App& app)
268{
269 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PCIeSlots/")
270 .privileges(redfish::privileges::getPCIeSlots)
271 .methods(boost::beast::http::verb::get)(
272 std::bind_front(handlePCIeSlotCollectionGet, std::ref(app)));
273}
274
275} // namespace redfish