1*40e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
2*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
37691cc2fSChicago Duan #pragma once
47691cc2fSChicago Duan
53ccb3adbSEd Tanous #include "app.hpp"
6e99073f5SGeorge Liu #include "dbus_utility.hpp"
77691cc2fSChicago Duan #include "error_messages.hpp"
84ce2c1b5SEd Tanous #include "generated/enums/pcie_slots.hpp"
9c49c329dSLakshmi Yadlapati #include "query.hpp"
103ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
117691cc2fSChicago Duan #include "utility.hpp"
123ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
133ccb3adbSEd Tanous #include "utils/json_utils.hpp"
14c49c329dSLakshmi Yadlapati #include "utils/pcie_util.hpp"
157691cc2fSChicago Duan
16e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
17ef4c65b7SEd Tanous #include <boost/url/format.hpp>
18d1bde9e5SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
19d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
207691cc2fSChicago Duan
21e99073f5SGeorge Liu #include <array>
22e99073f5SGeorge Liu #include <string_view>
23e99073f5SGeorge Liu
247691cc2fSChicago Duan namespace redfish
257691cc2fSChicago Duan {
267691cc2fSChicago Duan
277691cc2fSChicago Duan inline void
onPcieSlotGetAllDone(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const boost::system::error_code & ec,const dbus::utility::DBusPropertiesMap & propertiesList)287691cc2fSChicago Duan onPcieSlotGetAllDone(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
295e7e2dc5SEd Tanous const boost::system::error_code& ec,
307691cc2fSChicago Duan const dbus::utility::DBusPropertiesMap& propertiesList)
317691cc2fSChicago Duan {
327691cc2fSChicago Duan if (ec)
337691cc2fSChicago Duan {
3462598e31SEd Tanous BMCWEB_LOG_ERROR("Can't get PCIeSlot properties!");
357691cc2fSChicago Duan messages::internalError(asyncResp->res);
367691cc2fSChicago Duan return;
377691cc2fSChicago Duan }
387691cc2fSChicago Duan
397691cc2fSChicago Duan nlohmann::json& slots = asyncResp->res.jsonValue["Slots"];
407691cc2fSChicago Duan
417691cc2fSChicago Duan nlohmann::json::array_t* slotsPtr =
427691cc2fSChicago Duan slots.get_ptr<nlohmann::json::array_t*>();
437691cc2fSChicago Duan if (slotsPtr == nullptr)
447691cc2fSChicago Duan {
4562598e31SEd Tanous BMCWEB_LOG_ERROR("Slots key isn't an array???");
467691cc2fSChicago Duan messages::internalError(asyncResp->res);
477691cc2fSChicago Duan return;
487691cc2fSChicago Duan }
497691cc2fSChicago Duan
507691cc2fSChicago Duan nlohmann::json::object_t slot;
517691cc2fSChicago Duan
52d1bde9e5SKrzysztof Grobelny const std::string* generation = nullptr;
53d1bde9e5SKrzysztof Grobelny const size_t* lanes = nullptr;
54d1bde9e5SKrzysztof Grobelny const std::string* slotType = nullptr;
55d1bde9e5SKrzysztof Grobelny const bool* hotPluggable = nullptr;
567691cc2fSChicago Duan
57d1bde9e5SKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow(
58d1bde9e5SKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), propertiesList, "Generation",
59d1bde9e5SKrzysztof Grobelny generation, "Lanes", lanes, "SlotType", slotType, "HotPluggable",
60d1bde9e5SKrzysztof Grobelny hotPluggable);
61d1bde9e5SKrzysztof Grobelny
62d1bde9e5SKrzysztof Grobelny if (!success)
637691cc2fSChicago Duan {
647691cc2fSChicago Duan messages::internalError(asyncResp->res);
657691cc2fSChicago Duan return;
667691cc2fSChicago Duan }
67d1bde9e5SKrzysztof Grobelny
68d1bde9e5SKrzysztof Grobelny if (generation != nullptr)
69d1bde9e5SKrzysztof Grobelny {
700ec8b83dSEd Tanous std::optional<pcie_device::PCIeTypes> pcieType =
71c49c329dSLakshmi Yadlapati pcie_util::redfishPcieGenerationFromDbus(*generation);
727691cc2fSChicago Duan if (!pcieType)
737691cc2fSChicago Duan {
7462598e31SEd Tanous BMCWEB_LOG_WARNING("Unknown PCIe Slot Generation: {}", *generation);
75cf3b484eSLakshmi Yadlapati }
76cf3b484eSLakshmi Yadlapati else
77cf3b484eSLakshmi Yadlapati {
78cf3b484eSLakshmi Yadlapati if (*pcieType == pcie_device::PCIeTypes::Invalid)
79cf3b484eSLakshmi Yadlapati {
807691cc2fSChicago Duan messages::internalError(asyncResp->res);
817691cc2fSChicago Duan return;
827691cc2fSChicago Duan }
83bbf372a6SLakshmi Yadlapati slot["PCIeType"] = *pcieType;
84bbf372a6SLakshmi Yadlapati }
857691cc2fSChicago Duan }
86d1bde9e5SKrzysztof Grobelny
8782f80326SKonstantin Aladyshev if (lanes != nullptr && *lanes != 0)
887691cc2fSChicago Duan {
89d1bde9e5SKrzysztof Grobelny slot["Lanes"] = *lanes;
907691cc2fSChicago Duan }
91d1bde9e5SKrzysztof Grobelny
92d1bde9e5SKrzysztof Grobelny if (slotType != nullptr)
937691cc2fSChicago Duan {
94d87fdd9eSLakshmi Yadlapati std::optional<pcie_slots::SlotTypes> redfishSlotType =
95c49c329dSLakshmi Yadlapati pcie_util::dbusSlotTypeToRf(*slotType);
96d87fdd9eSLakshmi Yadlapati if (!redfishSlotType)
977691cc2fSChicago Duan {
9862598e31SEd Tanous BMCWEB_LOG_WARNING("Unknown PCIe Slot Type: {}", *slotType);
99cf3b484eSLakshmi Yadlapati }
100cf3b484eSLakshmi Yadlapati else
101cf3b484eSLakshmi Yadlapati {
102cf3b484eSLakshmi Yadlapati if (*redfishSlotType == pcie_slots::SlotTypes::Invalid)
103cf3b484eSLakshmi Yadlapati {
10462598e31SEd Tanous BMCWEB_LOG_ERROR("Unknown PCIe Slot Type: {}", *slotType);
1057691cc2fSChicago Duan messages::internalError(asyncResp->res);
1067691cc2fSChicago Duan return;
1077691cc2fSChicago Duan }
108d87fdd9eSLakshmi Yadlapati slot["SlotType"] = *redfishSlotType;
109d87fdd9eSLakshmi Yadlapati }
1107691cc2fSChicago Duan }
111d1bde9e5SKrzysztof Grobelny
112d1bde9e5SKrzysztof Grobelny if (hotPluggable != nullptr)
1137691cc2fSChicago Duan {
114d1bde9e5SKrzysztof Grobelny slot["HotPluggable"] = *hotPluggable;
1157691cc2fSChicago Duan }
116d1bde9e5SKrzysztof Grobelny
1177691cc2fSChicago Duan slots.emplace_back(std::move(slot));
1187691cc2fSChicago Duan }
1197691cc2fSChicago Duan
onMapperAssociationDone(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & chassisID,const std::string & pcieSlotPath,const std::string & connectionName,const boost::system::error_code & ec,const dbus::utility::MapperEndPoints & pcieSlotChassis)1207691cc2fSChicago Duan inline void onMapperAssociationDone(
1217691cc2fSChicago Duan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1227691cc2fSChicago Duan const std::string& chassisID, const std::string& pcieSlotPath,
1235e7e2dc5SEd Tanous const std::string& connectionName, const boost::system::error_code& ec,
1246c3e9451SGeorge Liu const dbus::utility::MapperEndPoints& pcieSlotChassis)
1257691cc2fSChicago Duan {
1267691cc2fSChicago Duan if (ec)
1277691cc2fSChicago Duan {
1287691cc2fSChicago Duan if (ec.value() == EBADR)
1297691cc2fSChicago Duan {
1307691cc2fSChicago Duan // This PCIeSlot have no chassis association.
1317691cc2fSChicago Duan return;
1327691cc2fSChicago Duan }
13362598e31SEd Tanous BMCWEB_LOG_ERROR("DBUS response error");
1347691cc2fSChicago Duan messages::internalError(asyncResp->res);
1357691cc2fSChicago Duan return;
1367691cc2fSChicago Duan }
1377691cc2fSChicago Duan
1386c3e9451SGeorge Liu if (pcieSlotChassis.size() != 1)
1397691cc2fSChicago Duan {
14062598e31SEd Tanous BMCWEB_LOG_ERROR("PCIe Slot association error! ");
1417691cc2fSChicago Duan messages::internalError(asyncResp->res);
1427691cc2fSChicago Duan return;
1437691cc2fSChicago Duan }
1447691cc2fSChicago Duan
1456c3e9451SGeorge Liu sdbusplus::message::object_path path(pcieSlotChassis[0]);
1467691cc2fSChicago Duan std::string chassisName = path.filename();
1477691cc2fSChicago Duan if (chassisName != chassisID)
1487691cc2fSChicago Duan {
1497691cc2fSChicago Duan // The pcie slot doesn't belong to the chassisID
1507691cc2fSChicago Duan return;
1517691cc2fSChicago Duan }
1527691cc2fSChicago Duan
153deae6a78SEd Tanous dbus::utility::getAllProperties(
154deae6a78SEd Tanous connectionName, pcieSlotPath,
155d1bde9e5SKrzysztof Grobelny "xyz.openbmc_project.Inventory.Item.PCIeSlot",
1565e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec2,
1577691cc2fSChicago Duan const dbus::utility::DBusPropertiesMap& propertiesList) {
1584ce2c1b5SEd Tanous onPcieSlotGetAllDone(asyncResp, ec2, propertiesList);
159d1bde9e5SKrzysztof Grobelny });
1607691cc2fSChicago Duan }
1617691cc2fSChicago Duan
onMapperSubtreeDone(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & chassisID,const boost::system::error_code & ec,const dbus::utility::MapperGetSubTreeResponse & subtree)162bd79bce8SPatrick Williams inline void onMapperSubtreeDone(
163bd79bce8SPatrick Williams const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
164bd79bce8SPatrick Williams const std::string& chassisID, const boost::system::error_code& ec,
1657691cc2fSChicago Duan const dbus::utility::MapperGetSubTreeResponse& subtree)
1667691cc2fSChicago Duan {
1677691cc2fSChicago Duan if (ec)
1687691cc2fSChicago Duan {
16962598e31SEd Tanous BMCWEB_LOG_ERROR("D-Bus response error on GetSubTree {}", ec);
1707691cc2fSChicago Duan messages::internalError(asyncResp->res);
1717691cc2fSChicago Duan return;
1727691cc2fSChicago Duan }
1737691cc2fSChicago Duan if (subtree.empty())
1747691cc2fSChicago Duan {
175d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "Chassis", chassisID);
1767691cc2fSChicago Duan return;
1777691cc2fSChicago Duan }
1787691cc2fSChicago Duan
17962598e31SEd Tanous BMCWEB_LOG_DEBUG("Get properties for PCIeSlots associated to chassis = {}",
18062598e31SEd Tanous chassisID);
1817691cc2fSChicago Duan
1827691cc2fSChicago Duan asyncResp->res.jsonValue["@odata.type"] = "#PCIeSlots.v1_4_1.PCIeSlots";
1837691cc2fSChicago Duan asyncResp->res.jsonValue["Name"] = "PCIe Slot Information";
184ef4c65b7SEd Tanous asyncResp->res.jsonValue["@odata.id"] =
185ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/Chassis/{}/PCIeSlots", chassisID);
1867691cc2fSChicago Duan asyncResp->res.jsonValue["Id"] = "1";
1877691cc2fSChicago Duan asyncResp->res.jsonValue["Slots"] = nlohmann::json::array();
1887691cc2fSChicago Duan
1897691cc2fSChicago Duan for (const auto& pathServicePair : subtree)
1907691cc2fSChicago Duan {
1917691cc2fSChicago Duan const std::string& pcieSlotPath = pathServicePair.first;
1927691cc2fSChicago Duan for (const auto& connectionInterfacePair : pathServicePair.second)
1937691cc2fSChicago Duan {
1947691cc2fSChicago Duan const std::string& connectionName = connectionInterfacePair.first;
1957691cc2fSChicago Duan sdbusplus::message::object_path pcieSlotAssociationPath(
1967691cc2fSChicago Duan pcieSlotPath);
1977691cc2fSChicago Duan pcieSlotAssociationPath /= "chassis";
1987691cc2fSChicago Duan
1997691cc2fSChicago Duan // The association of this PCIeSlot is used to determine whether
2007691cc2fSChicago Duan // it belongs to this ChassisID
2016c3e9451SGeorge Liu dbus::utility::getAssociationEndPoints(
2026c3e9451SGeorge Liu std::string{pcieSlotAssociationPath},
2037691cc2fSChicago Duan [asyncResp, chassisID, pcieSlotPath, connectionName](
2044ce2c1b5SEd Tanous const boost::system::error_code& ec2,
2056c3e9451SGeorge Liu const dbus::utility::MapperEndPoints& endpoints) {
2067691cc2fSChicago Duan onMapperAssociationDone(asyncResp, chassisID, pcieSlotPath,
2074ce2c1b5SEd Tanous connectionName, ec2, endpoints);
2086c3e9451SGeorge Liu });
2097691cc2fSChicago Duan }
2107691cc2fSChicago Duan }
2117691cc2fSChicago Duan }
2127691cc2fSChicago Duan
handlePCIeSlotCollectionGet(crow::App & app,const crow::Request & req,const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const std::string & chassisID)2137691cc2fSChicago Duan inline void handlePCIeSlotCollectionGet(
2147691cc2fSChicago Duan crow::App& app, const crow::Request& req,
2157691cc2fSChicago Duan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2167691cc2fSChicago Duan const std::string& chassisID)
2177691cc2fSChicago Duan {
2182aacf856SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2197691cc2fSChicago Duan {
2207691cc2fSChicago Duan return;
2217691cc2fSChicago Duan }
2227691cc2fSChicago Duan
223e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> interfaces = {
224e99073f5SGeorge Liu "xyz.openbmc_project.Inventory.Item.PCIeSlot"};
225e99073f5SGeorge Liu dbus::utility::getSubTree(
226e99073f5SGeorge Liu "/xyz/openbmc_project/inventory", 0, interfaces,
2277691cc2fSChicago Duan [asyncResp,
228e99073f5SGeorge Liu chassisID](const boost::system::error_code& ec,
2297691cc2fSChicago Duan const dbus::utility::MapperGetSubTreeResponse& subtree) {
2307691cc2fSChicago Duan onMapperSubtreeDone(asyncResp, chassisID, ec, subtree);
231e99073f5SGeorge Liu });
2327691cc2fSChicago Duan }
2337691cc2fSChicago Duan
requestRoutesPCIeSlots(App & app)2347691cc2fSChicago Duan inline void requestRoutesPCIeSlots(App& app)
2357691cc2fSChicago Duan {
2367691cc2fSChicago Duan BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PCIeSlots/")
2377691cc2fSChicago Duan .privileges(redfish::privileges::getPCIeSlots)
2387691cc2fSChicago Duan .methods(boost::beast::http::verb::get)(
2397691cc2fSChicago Duan std::bind_front(handlePCIeSlotCollectionGet, std::ref(app)));
2407691cc2fSChicago Duan }
2417691cc2fSChicago Duan
2427691cc2fSChicago Duan } // namespace redfish
243