blob: fb39a573479297433a16c9d7d858d2ee0bf0d457 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Chicago Duan7691cc22021-01-25 19:52:35 +08003#pragma once
4
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08005#include "app.hpp"
George Liue99073f2022-12-09 11:06:16 +08006#include "dbus_utility.hpp"
Chicago Duan7691cc22021-01-25 19:52:35 +08007#include "error_messages.hpp"
Ed Tanous4ce2c1b2023-02-10 11:57:54 -08008#include "generated/enums/pcie_slots.hpp"
Lakshmi Yadlapatic49c3292023-04-19 16:42:35 -05009#include "query.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080010#include "registries/privilege_registry.hpp"
Chicago Duan7691cc22021-01-25 19:52:35 +080011#include "utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080012#include "utils/dbus_utils.hpp"
13#include "utils/json_utils.hpp"
Lakshmi Yadlapatic49c3292023-04-19 16:42:35 -050014#include "utils/pcie_util.hpp"
Chicago Duan7691cc22021-01-25 19:52:35 +080015
George Liue99073f2022-12-09 11:06:16 +080016#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070017#include <boost/url/format.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020018#include <sdbusplus/asio/property.hpp>
19#include <sdbusplus/unpack_properties.hpp>
Chicago Duan7691cc22021-01-25 19:52:35 +080020
George Liue99073f2022-12-09 11:06:16 +080021#include <array>
22#include <string_view>
23
Chicago Duan7691cc22021-01-25 19:52:35 +080024namespace redfish
25{
26
Chicago Duan7691cc22021-01-25 19:52:35 +080027inline void
28 onPcieSlotGetAllDone(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080029 const boost::system::error_code& ec,
Chicago Duan7691cc22021-01-25 19:52:35 +080030 const dbus::utility::DBusPropertiesMap& propertiesList)
31{
32 if (ec)
33 {
Ed Tanous62598e32023-07-17 17:06:25 -070034 BMCWEB_LOG_ERROR("Can't get PCIeSlot properties!");
Chicago Duan7691cc22021-01-25 19:52:35 +080035 messages::internalError(asyncResp->res);
36 return;
37 }
38
39 nlohmann::json& slots = asyncResp->res.jsonValue["Slots"];
40
41 nlohmann::json::array_t* slotsPtr =
42 slots.get_ptr<nlohmann::json::array_t*>();
43 if (slotsPtr == nullptr)
44 {
Ed Tanous62598e32023-07-17 17:06:25 -070045 BMCWEB_LOG_ERROR("Slots key isn't an array???");
Chicago Duan7691cc22021-01-25 19:52:35 +080046 messages::internalError(asyncResp->res);
47 return;
48 }
49
50 nlohmann::json::object_t slot;
51
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020052 const std::string* generation = nullptr;
53 const size_t* lanes = nullptr;
54 const std::string* slotType = nullptr;
55 const bool* hotPluggable = nullptr;
Chicago Duan7691cc22021-01-25 19:52:35 +080056
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020057 const bool success = sdbusplus::unpackPropertiesNoThrow(
58 dbus_utils::UnpackErrorPrinter(), propertiesList, "Generation",
59 generation, "Lanes", lanes, "SlotType", slotType, "HotPluggable",
60 hotPluggable);
61
62 if (!success)
63 {
64 messages::internalError(asyncResp->res);
65 return;
Chicago Duan7691cc22021-01-25 19:52:35 +080066 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020067
68 if (generation != nullptr)
69 {
Ed Tanous0ec8b832022-03-14 14:56:47 -070070 std::optional<pcie_device::PCIeTypes> pcieType =
Lakshmi Yadlapatic49c3292023-04-19 16:42:35 -050071 pcie_util::redfishPcieGenerationFromDbus(*generation);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020072 if (!pcieType)
73 {
Ed Tanous62598e32023-07-17 17:06:25 -070074 BMCWEB_LOG_WARNING("Unknown PCIe Slot Generation: {}", *generation);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020075 }
Lakshmi Yadlapaticf3b4842023-06-27 02:36:53 -050076 else
Lakshmi Yadlapatibbf372a2023-05-02 15:09:08 -050077 {
Lakshmi Yadlapaticf3b4842023-06-27 02:36:53 -050078 if (*pcieType == pcie_device::PCIeTypes::Invalid)
79 {
80 messages::internalError(asyncResp->res);
81 return;
82 }
Lakshmi Yadlapatibbf372a2023-05-02 15:09:08 -050083 slot["PCIeType"] = *pcieType;
84 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020085 }
86
Konstantin Aladyshev82f80322023-07-10 15:00:38 +030087 if (lanes != nullptr && *lanes != 0)
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020088 {
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020089 slot["Lanes"] = *lanes;
90 }
91
92 if (slotType != nullptr)
93 {
Lakshmi Yadlapatid87fdd92023-05-02 16:50:31 -050094 std::optional<pcie_slots::SlotTypes> redfishSlotType =
Lakshmi Yadlapatic49c3292023-04-19 16:42:35 -050095 pcie_util::dbusSlotTypeToRf(*slotType);
Lakshmi Yadlapatid87fdd92023-05-02 16:50:31 -050096 if (!redfishSlotType)
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020097 {
Ed Tanous62598e32023-07-17 17:06:25 -070098 BMCWEB_LOG_WARNING("Unknown PCIe Slot Type: {}", *slotType);
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020099 }
Lakshmi Yadlapaticf3b4842023-06-27 02:36:53 -0500100 else
Lakshmi Yadlapatid87fdd92023-05-02 16:50:31 -0500101 {
Lakshmi Yadlapaticf3b4842023-06-27 02:36:53 -0500102 if (*redfishSlotType == pcie_slots::SlotTypes::Invalid)
103 {
Ed Tanous62598e32023-07-17 17:06:25 -0700104 BMCWEB_LOG_ERROR("Unknown PCIe Slot Type: {}", *slotType);
Lakshmi Yadlapaticf3b4842023-06-27 02:36:53 -0500105 messages::internalError(asyncResp->res);
106 return;
107 }
Lakshmi Yadlapatid87fdd92023-05-02 16:50:31 -0500108 slot["SlotType"] = *redfishSlotType;
109 }
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200110 }
111
112 if (hotPluggable != nullptr)
113 {
114 slot["HotPluggable"] = *hotPluggable;
115 }
116
Chicago Duan7691cc22021-01-25 19:52:35 +0800117 slots.emplace_back(std::move(slot));
118}
119
120inline void onMapperAssociationDone(
121 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
122 const std::string& chassisID, const std::string& pcieSlotPath,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800123 const std::string& connectionName, const boost::system::error_code& ec,
George Liu6c3e9452023-03-03 13:55:29 +0800124 const dbus::utility::MapperEndPoints& pcieSlotChassis)
Chicago Duan7691cc22021-01-25 19:52:35 +0800125{
126 if (ec)
127 {
128 if (ec.value() == EBADR)
129 {
130 // This PCIeSlot have no chassis association.
131 return;
132 }
Ed Tanous62598e32023-07-17 17:06:25 -0700133 BMCWEB_LOG_ERROR("DBUS response error");
Chicago Duan7691cc22021-01-25 19:52:35 +0800134 messages::internalError(asyncResp->res);
135 return;
136 }
137
George Liu6c3e9452023-03-03 13:55:29 +0800138 if (pcieSlotChassis.size() != 1)
Chicago Duan7691cc22021-01-25 19:52:35 +0800139 {
Ed Tanous62598e32023-07-17 17:06:25 -0700140 BMCWEB_LOG_ERROR("PCIe Slot association error! ");
Chicago Duan7691cc22021-01-25 19:52:35 +0800141 messages::internalError(asyncResp->res);
142 return;
143 }
144
George Liu6c3e9452023-03-03 13:55:29 +0800145 sdbusplus::message::object_path path(pcieSlotChassis[0]);
Chicago Duan7691cc22021-01-25 19:52:35 +0800146 std::string chassisName = path.filename();
147 if (chassisName != chassisID)
148 {
149 // The pcie slot doesn't belong to the chassisID
150 return;
151 }
152
Ed Tanousdeae6a72024-11-11 21:58:57 -0800153 dbus::utility::getAllProperties(
154 connectionName, pcieSlotPath,
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +0200155 "xyz.openbmc_project.Inventory.Item.PCIeSlot",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800156 [asyncResp](const boost::system::error_code& ec2,
Chicago Duan7691cc22021-01-25 19:52:35 +0800157 const dbus::utility::DBusPropertiesMap& propertiesList) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400158 onPcieSlotGetAllDone(asyncResp, ec2, propertiesList);
159 });
Chicago Duan7691cc22021-01-25 19:52:35 +0800160}
161
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400162inline void onMapperSubtreeDone(
163 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
164 const std::string& chassisID, const boost::system::error_code& ec,
165 const dbus::utility::MapperGetSubTreeResponse& subtree)
Chicago Duan7691cc22021-01-25 19:52:35 +0800166{
167 if (ec)
168 {
Ed Tanous62598e32023-07-17 17:06:25 -0700169 BMCWEB_LOG_ERROR("D-Bus response error on GetSubTree {}", ec);
Chicago Duan7691cc22021-01-25 19:52:35 +0800170 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
Ed Tanous62598e32023-07-17 17:06:25 -0700179 BMCWEB_LOG_DEBUG("Get properties for PCIeSlots associated to chassis = {}",
180 chassisID);
Chicago Duan7691cc22021-01-25 19:52:35 +0800181
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) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400206 onMapperAssociationDone(asyncResp, chassisID, pcieSlotPath,
207 connectionName, ec2, endpoints);
208 });
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) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400230 onMapperSubtreeDone(asyncResp, chassisID, ec, subtree);
231 });
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