17691cc2fSChicago Duan #pragma once
27691cc2fSChicago Duan 
33ccb3adbSEd Tanous #include "app.hpp"
4e99073f5SGeorge Liu #include "dbus_utility.hpp"
57691cc2fSChicago Duan #include "error_messages.hpp"
64ce2c1b5SEd Tanous #include "generated/enums/pcie_slots.hpp"
7c49c329dSLakshmi Yadlapati #include "query.hpp"
83ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
97691cc2fSChicago Duan #include "utility.hpp"
103ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
113ccb3adbSEd Tanous #include "utils/json_utils.hpp"
12c49c329dSLakshmi Yadlapati #include "utils/pcie_util.hpp"
137691cc2fSChicago Duan 
14e99073f5SGeorge Liu #include <boost/system/error_code.hpp>
15ef4c65b7SEd Tanous #include <boost/url/format.hpp>
16d1bde9e5SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
17d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
187691cc2fSChicago Duan 
19e99073f5SGeorge Liu #include <array>
20e99073f5SGeorge Liu #include <string_view>
21e99073f5SGeorge Liu 
227691cc2fSChicago Duan namespace redfish
237691cc2fSChicago Duan {
247691cc2fSChicago Duan 
257691cc2fSChicago Duan inline void
267691cc2fSChicago Duan     onPcieSlotGetAllDone(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
275e7e2dc5SEd Tanous                          const boost::system::error_code& ec,
287691cc2fSChicago Duan                          const dbus::utility::DBusPropertiesMap& propertiesList)
297691cc2fSChicago Duan {
307691cc2fSChicago Duan     if (ec)
317691cc2fSChicago Duan     {
327691cc2fSChicago Duan         BMCWEB_LOG_ERROR << "Can't get PCIeSlot properties!";
337691cc2fSChicago Duan         messages::internalError(asyncResp->res);
347691cc2fSChicago Duan         return;
357691cc2fSChicago Duan     }
367691cc2fSChicago Duan 
377691cc2fSChicago Duan     nlohmann::json& slots = asyncResp->res.jsonValue["Slots"];
387691cc2fSChicago Duan 
397691cc2fSChicago Duan     nlohmann::json::array_t* slotsPtr =
407691cc2fSChicago Duan         slots.get_ptr<nlohmann::json::array_t*>();
417691cc2fSChicago Duan     if (slotsPtr == nullptr)
427691cc2fSChicago Duan     {
437691cc2fSChicago Duan         BMCWEB_LOG_ERROR << "Slots key isn't an array???";
447691cc2fSChicago Duan         messages::internalError(asyncResp->res);
457691cc2fSChicago Duan         return;
467691cc2fSChicago Duan     }
477691cc2fSChicago Duan 
487691cc2fSChicago Duan     nlohmann::json::object_t slot;
497691cc2fSChicago Duan 
50d1bde9e5SKrzysztof Grobelny     const std::string* generation = nullptr;
51d1bde9e5SKrzysztof Grobelny     const size_t* lanes = nullptr;
52d1bde9e5SKrzysztof Grobelny     const std::string* slotType = nullptr;
53d1bde9e5SKrzysztof Grobelny     const bool* hotPluggable = nullptr;
547691cc2fSChicago Duan 
55d1bde9e5SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
56d1bde9e5SKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), propertiesList, "Generation",
57d1bde9e5SKrzysztof Grobelny         generation, "Lanes", lanes, "SlotType", slotType, "HotPluggable",
58d1bde9e5SKrzysztof Grobelny         hotPluggable);
59d1bde9e5SKrzysztof Grobelny 
60d1bde9e5SKrzysztof Grobelny     if (!success)
617691cc2fSChicago Duan     {
627691cc2fSChicago Duan         messages::internalError(asyncResp->res);
637691cc2fSChicago Duan         return;
647691cc2fSChicago Duan     }
65d1bde9e5SKrzysztof Grobelny 
66d1bde9e5SKrzysztof Grobelny     if (generation != nullptr)
67d1bde9e5SKrzysztof Grobelny     {
680ec8b83dSEd Tanous         std::optional<pcie_device::PCIeTypes> pcieType =
69c49c329dSLakshmi Yadlapati             pcie_util::redfishPcieGenerationFromDbus(*generation);
707691cc2fSChicago Duan         if (!pcieType)
717691cc2fSChicago Duan         {
72*cf3b484eSLakshmi Yadlapati             BMCWEB_LOG_WARNING << "Unknown PCIe Slot Generation: "
73*cf3b484eSLakshmi Yadlapati                                << *generation;
74*cf3b484eSLakshmi Yadlapati         }
75*cf3b484eSLakshmi Yadlapati         else
76*cf3b484eSLakshmi Yadlapati         {
77*cf3b484eSLakshmi Yadlapati             if (*pcieType == pcie_device::PCIeTypes::Invalid)
78*cf3b484eSLakshmi Yadlapati             {
797691cc2fSChicago Duan                 messages::internalError(asyncResp->res);
807691cc2fSChicago Duan                 return;
817691cc2fSChicago Duan             }
82bbf372a6SLakshmi Yadlapati             slot["PCIeType"] = *pcieType;
83bbf372a6SLakshmi Yadlapati         }
847691cc2fSChicago Duan     }
85d1bde9e5SKrzysztof Grobelny 
86d1bde9e5SKrzysztof Grobelny     if (lanes != nullptr)
877691cc2fSChicago Duan     {
88d1bde9e5SKrzysztof Grobelny         slot["Lanes"] = *lanes;
897691cc2fSChicago Duan     }
90d1bde9e5SKrzysztof Grobelny 
91d1bde9e5SKrzysztof Grobelny     if (slotType != nullptr)
927691cc2fSChicago Duan     {
93d87fdd9eSLakshmi Yadlapati         std::optional<pcie_slots::SlotTypes> redfishSlotType =
94c49c329dSLakshmi Yadlapati             pcie_util::dbusSlotTypeToRf(*slotType);
95d87fdd9eSLakshmi Yadlapati         if (!redfishSlotType)
967691cc2fSChicago Duan         {
97*cf3b484eSLakshmi Yadlapati             BMCWEB_LOG_WARNING << "Unknown PCIe Slot Type: " << *slotType;
98*cf3b484eSLakshmi Yadlapati         }
99*cf3b484eSLakshmi Yadlapati         else
100*cf3b484eSLakshmi Yadlapati         {
101*cf3b484eSLakshmi Yadlapati             if (*redfishSlotType == pcie_slots::SlotTypes::Invalid)
102*cf3b484eSLakshmi Yadlapati             {
103*cf3b484eSLakshmi Yadlapati                 BMCWEB_LOG_ERROR << "Unknown PCIe Slot Type: " << *slotType;
1047691cc2fSChicago Duan                 messages::internalError(asyncResp->res);
1057691cc2fSChicago Duan                 return;
1067691cc2fSChicago Duan             }
107d87fdd9eSLakshmi Yadlapati             slot["SlotType"] = *redfishSlotType;
108d87fdd9eSLakshmi Yadlapati         }
1097691cc2fSChicago Duan     }
110d1bde9e5SKrzysztof Grobelny 
111d1bde9e5SKrzysztof Grobelny     if (hotPluggable != nullptr)
1127691cc2fSChicago Duan     {
113d1bde9e5SKrzysztof Grobelny         slot["HotPluggable"] = *hotPluggable;
1147691cc2fSChicago Duan     }
115d1bde9e5SKrzysztof Grobelny 
1167691cc2fSChicago Duan     slots.emplace_back(std::move(slot));
1177691cc2fSChicago Duan }
1187691cc2fSChicago Duan 
1197691cc2fSChicago Duan inline void onMapperAssociationDone(
1207691cc2fSChicago Duan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1217691cc2fSChicago Duan     const std::string& chassisID, const std::string& pcieSlotPath,
1225e7e2dc5SEd Tanous     const std::string& connectionName, const boost::system::error_code& ec,
1236c3e9451SGeorge Liu     const dbus::utility::MapperEndPoints& pcieSlotChassis)
1247691cc2fSChicago Duan {
1257691cc2fSChicago Duan     if (ec)
1267691cc2fSChicago Duan     {
1277691cc2fSChicago Duan         if (ec.value() == EBADR)
1287691cc2fSChicago Duan         {
1297691cc2fSChicago Duan             // This PCIeSlot have no chassis association.
1307691cc2fSChicago Duan             return;
1317691cc2fSChicago Duan         }
1327691cc2fSChicago Duan         BMCWEB_LOG_ERROR << "DBUS response error";
1337691cc2fSChicago Duan         messages::internalError(asyncResp->res);
1347691cc2fSChicago Duan         return;
1357691cc2fSChicago Duan     }
1367691cc2fSChicago Duan 
1376c3e9451SGeorge Liu     if (pcieSlotChassis.size() != 1)
1387691cc2fSChicago Duan     {
1397691cc2fSChicago Duan         BMCWEB_LOG_ERROR << "PCIe Slot association error! ";
1407691cc2fSChicago Duan         messages::internalError(asyncResp->res);
1417691cc2fSChicago Duan         return;
1427691cc2fSChicago Duan     }
1437691cc2fSChicago Duan 
1446c3e9451SGeorge Liu     sdbusplus::message::object_path path(pcieSlotChassis[0]);
1457691cc2fSChicago Duan     std::string chassisName = path.filename();
1467691cc2fSChicago Duan     if (chassisName != chassisID)
1477691cc2fSChicago Duan     {
1487691cc2fSChicago Duan         // The pcie slot doesn't belong to the chassisID
1497691cc2fSChicago Duan         return;
1507691cc2fSChicago Duan     }
1517691cc2fSChicago Duan 
152d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
153d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, connectionName, pcieSlotPath,
154d1bde9e5SKrzysztof Grobelny         "xyz.openbmc_project.Inventory.Item.PCIeSlot",
1555e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec2,
1567691cc2fSChicago Duan                     const dbus::utility::DBusPropertiesMap& propertiesList) {
1574ce2c1b5SEd Tanous         onPcieSlotGetAllDone(asyncResp, ec2, propertiesList);
158d1bde9e5SKrzysztof Grobelny         });
1597691cc2fSChicago Duan }
1607691cc2fSChicago Duan 
1617691cc2fSChicago Duan inline void
1627691cc2fSChicago Duan     onMapperSubtreeDone(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1637691cc2fSChicago Duan                         const std::string& chassisID,
1645e7e2dc5SEd Tanous                         const boost::system::error_code& ec,
1657691cc2fSChicago Duan                         const dbus::utility::MapperGetSubTreeResponse& subtree)
1667691cc2fSChicago Duan {
1677691cc2fSChicago Duan     if (ec)
1687691cc2fSChicago Duan     {
1697691cc2fSChicago Duan         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 
1797691cc2fSChicago Duan     BMCWEB_LOG_DEBUG << "Get properties for PCIeSlots associated to chassis = "
1807691cc2fSChicago Duan                      << 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 
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 
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