blob: 62fef3ea95e33b9e39f66b1550116d908562f1b8 [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"
Lakshmi Yadlapatic49c3292023-04-19 16:42:35 -05007#include "query.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08008#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"
Lakshmi Yadlapatic49c3292023-04-19 16:42:35 -050012#include "utils/pcie_util.hpp"
Chicago Duan7691cc22021-01-25 19:52:35 +080013
George Liue99073f2022-12-09 11:06:16 +080014#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070015#include <boost/url/format.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020016#include <sdbusplus/asio/property.hpp>
17#include <sdbusplus/unpack_properties.hpp>
Chicago Duan7691cc22021-01-25 19:52:35 +080018
George Liue99073f2022-12-09 11:06:16 +080019#include <array>
20#include <string_view>
21
Chicago Duan7691cc22021-01-25 19:52:35 +080022namespace redfish
23{
24
Chicago Duan7691cc22021-01-25 19:52:35 +080025inline void
26 onPcieSlotGetAllDone(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080027 const boost::system::error_code& ec,
Chicago Duan7691cc22021-01-25 19:52:35 +080028 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 Grobelnyd1bde9e2022-09-07 10:40:51 +020050 const std::string* generation = nullptr;
51 const size_t* lanes = nullptr;
52 const std::string* slotType = nullptr;
53 const bool* hotPluggable = nullptr;
Chicago Duan7691cc22021-01-25 19:52:35 +080054
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020055 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 Duan7691cc22021-01-25 19:52:35 +080064 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020065
66 if (generation != nullptr)
67 {
Ed Tanous0ec8b832022-03-14 14:56:47 -070068 std::optional<pcie_device::PCIeTypes> pcieType =
Lakshmi Yadlapatic49c3292023-04-19 16:42:35 -050069 pcie_util::redfishPcieGenerationFromDbus(*generation);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020070 if (!pcieType)
71 {
Lakshmi Yadlapaticf3b4842023-06-27 02:36:53 -050072 BMCWEB_LOG_WARNING << "Unknown PCIe Slot Generation: "
73 << *generation;
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020074 }
Lakshmi Yadlapaticf3b4842023-06-27 02:36:53 -050075 else
Lakshmi Yadlapatibbf372a2023-05-02 15:09:08 -050076 {
Lakshmi Yadlapaticf3b4842023-06-27 02:36:53 -050077 if (*pcieType == pcie_device::PCIeTypes::Invalid)
78 {
79 messages::internalError(asyncResp->res);
80 return;
81 }
Lakshmi Yadlapatibbf372a2023-05-02 15:09:08 -050082 slot["PCIeType"] = *pcieType;
83 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020084 }
85
86 if (lanes != nullptr)
87 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020088 slot["Lanes"] = *lanes;
89 }
90
91 if (slotType != nullptr)
92 {
Lakshmi Yadlapatid87fdd92023-05-02 16:50:31 -050093 std::optional<pcie_slots::SlotTypes> redfishSlotType =
Lakshmi Yadlapatic49c3292023-04-19 16:42:35 -050094 pcie_util::dbusSlotTypeToRf(*slotType);
Lakshmi Yadlapatid87fdd92023-05-02 16:50:31 -050095 if (!redfishSlotType)
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020096 {
Lakshmi Yadlapaticf3b4842023-06-27 02:36:53 -050097 BMCWEB_LOG_WARNING << "Unknown PCIe Slot Type: " << *slotType;
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020098 }
Lakshmi Yadlapaticf3b4842023-06-27 02:36:53 -050099 else
Lakshmi Yadlapatid87fdd92023-05-02 16:50:31 -0500100 {
Lakshmi Yadlapaticf3b4842023-06-27 02:36:53 -0500101 if (*redfishSlotType == pcie_slots::SlotTypes::Invalid)
102 {
103 BMCWEB_LOG_ERROR << "Unknown PCIe Slot Type: " << *slotType;
104 messages::internalError(asyncResp->res);
105 return;
106 }
Lakshmi Yadlapatid87fdd92023-05-02 16:50:31 -0500107 slot["SlotType"] = *redfishSlotType;
108 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200109 }
110
111 if (hotPluggable != nullptr)
112 {
113 slot["HotPluggable"] = *hotPluggable;
114 }
115
Chicago Duan7691cc22021-01-25 19:52:35 +0800116 slots.emplace_back(std::move(slot));
117}
118
119inline void onMapperAssociationDone(
120 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
121 const std::string& chassisID, const std::string& pcieSlotPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800122 const std::string& connectionName, const boost::system::error_code& ec,
George Liu6c3e9452023-03-03 13:55:29 +0800123 const dbus::utility::MapperEndPoints& pcieSlotChassis)
Chicago Duan7691cc22021-01-25 19:52:35 +0800124{
125 if (ec)
126 {
127 if (ec.value() == EBADR)
128 {
129 // This PCIeSlot have no chassis association.
130 return;
131 }
132 BMCWEB_LOG_ERROR << "DBUS response error";
133 messages::internalError(asyncResp->res);
134 return;
135 }
136
George Liu6c3e9452023-03-03 13:55:29 +0800137 if (pcieSlotChassis.size() != 1)
Chicago Duan7691cc22021-01-25 19:52:35 +0800138 {
139 BMCWEB_LOG_ERROR << "PCIe Slot association error! ";
140 messages::internalError(asyncResp->res);
141 return;
142 }
143
George Liu6c3e9452023-03-03 13:55:29 +0800144 sdbusplus::message::object_path path(pcieSlotChassis[0]);
Chicago Duan7691cc22021-01-25 19:52:35 +0800145 std::string chassisName = path.filename();
146 if (chassisName != chassisID)
147 {
148 // The pcie slot doesn't belong to the chassisID
149 return;
150 }
151
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200152 sdbusplus::asio::getAllProperties(
153 *crow::connections::systemBus, connectionName, pcieSlotPath,
154 "xyz.openbmc_project.Inventory.Item.PCIeSlot",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800155 [asyncResp](const boost::system::error_code& ec2,
Chicago Duan7691cc22021-01-25 19:52:35 +0800156 const dbus::utility::DBusPropertiesMap& propertiesList) {
Ed Tanous4ce2c1b2023-02-10 11:57:54 -0800157 onPcieSlotGetAllDone(asyncResp, ec2, propertiesList);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200158 });
Chicago Duan7691cc22021-01-25 19:52:35 +0800159}
160
161inline void
162 onMapperSubtreeDone(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
163 const std::string& chassisID,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800164 const boost::system::error_code& ec,
Chicago Duan7691cc22021-01-25 19:52:35 +0800165 const dbus::utility::MapperGetSubTreeResponse& subtree)
166{
167 if (ec)
168 {
169 BMCWEB_LOG_ERROR << "D-Bus response error on GetSubTree " << ec;
170 messages::internalError(asyncResp->res);
171 return;
172 }
173 if (subtree.empty())
174 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800175 messages::resourceNotFound(asyncResp->res, "Chassis", chassisID);
Chicago Duan7691cc22021-01-25 19:52:35 +0800176 return;
177 }
178
179 BMCWEB_LOG_DEBUG << "Get properties for PCIeSlots associated to chassis = "
180 << chassisID;
181
182 asyncResp->res.jsonValue["@odata.type"] = "#PCIeSlots.v1_4_1.PCIeSlots";
183 asyncResp->res.jsonValue["Name"] = "PCIe Slot Information";
Ed Tanousef4c65b2023-04-24 15:28:50 -0700184 asyncResp->res.jsonValue["@odata.id"] =
185 boost::urls::format("/redfish/v1/Chassis/{}/PCIeSlots", chassisID);
Chicago Duan7691cc22021-01-25 19:52:35 +0800186 asyncResp->res.jsonValue["Id"] = "1";
187 asyncResp->res.jsonValue["Slots"] = nlohmann::json::array();
188
189 for (const auto& pathServicePair : subtree)
190 {
191 const std::string& pcieSlotPath = pathServicePair.first;
192 for (const auto& connectionInterfacePair : pathServicePair.second)
193 {
194 const std::string& connectionName = connectionInterfacePair.first;
195 sdbusplus::message::object_path pcieSlotAssociationPath(
196 pcieSlotPath);
197 pcieSlotAssociationPath /= "chassis";
198
199 // The association of this PCIeSlot is used to determine whether
200 // it belongs to this ChassisID
George Liu6c3e9452023-03-03 13:55:29 +0800201 dbus::utility::getAssociationEndPoints(
202 std::string{pcieSlotAssociationPath},
Chicago Duan7691cc22021-01-25 19:52:35 +0800203 [asyncResp, chassisID, pcieSlotPath, connectionName](
Ed Tanous4ce2c1b2023-02-10 11:57:54 -0800204 const boost::system::error_code& ec2,
George Liu6c3e9452023-03-03 13:55:29 +0800205 const dbus::utility::MapperEndPoints& endpoints) {
Chicago Duan7691cc22021-01-25 19:52:35 +0800206 onMapperAssociationDone(asyncResp, chassisID, pcieSlotPath,
Ed Tanous4ce2c1b2023-02-10 11:57:54 -0800207 connectionName, ec2, endpoints);
George Liu6c3e9452023-03-03 13:55:29 +0800208 });
Chicago Duan7691cc22021-01-25 19:52:35 +0800209 }
210 }
211}
212
213inline void handlePCIeSlotCollectionGet(
214 crow::App& app, const crow::Request& req,
215 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
216 const std::string& chassisID)
217{
Ed Tanous2aacf852022-06-17 08:07:54 -0700218 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Chicago Duan7691cc22021-01-25 19:52:35 +0800219 {
220 return;
221 }
222
George Liue99073f2022-12-09 11:06:16 +0800223 constexpr std::array<std::string_view, 1> interfaces = {
224 "xyz.openbmc_project.Inventory.Item.PCIeSlot"};
225 dbus::utility::getSubTree(
226 "/xyz/openbmc_project/inventory", 0, interfaces,
Chicago Duan7691cc22021-01-25 19:52:35 +0800227 [asyncResp,
George Liue99073f2022-12-09 11:06:16 +0800228 chassisID](const boost::system::error_code& ec,
Chicago Duan7691cc22021-01-25 19:52:35 +0800229 const dbus::utility::MapperGetSubTreeResponse& subtree) {
230 onMapperSubtreeDone(asyncResp, chassisID, ec, subtree);
George Liue99073f2022-12-09 11:06:16 +0800231 });
Chicago Duan7691cc22021-01-25 19:52:35 +0800232}
233
234inline void requestRoutesPCIeSlots(App& app)
235{
236 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PCIeSlots/")
237 .privileges(redfish::privileges::getPCIeSlots)
238 .methods(boost::beast::http::verb::get)(
239 std::bind_front(handlePCIeSlotCollectionGet, std::ref(app)));
240}
241
242} // namespace redfish