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" 7*c49c329dSLakshmi 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" 12*c49c329dSLakshmi 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 = 69*c49c329dSLakshmi Yadlapati pcie_util::redfishPcieGenerationFromDbus(*generation); 707691cc2fSChicago Duan if (!pcieType) 717691cc2fSChicago Duan { 727691cc2fSChicago Duan messages::internalError(asyncResp->res); 737691cc2fSChicago Duan return; 747691cc2fSChicago Duan } 75bbf372a6SLakshmi Yadlapati if (*pcieType != pcie_device::PCIeTypes::Invalid) 76bbf372a6SLakshmi Yadlapati { 77bbf372a6SLakshmi Yadlapati slot["PCIeType"] = *pcieType; 78bbf372a6SLakshmi Yadlapati } 797691cc2fSChicago Duan } 80d1bde9e5SKrzysztof Grobelny 81d1bde9e5SKrzysztof Grobelny if (lanes != nullptr) 827691cc2fSChicago Duan { 83d1bde9e5SKrzysztof Grobelny slot["Lanes"] = *lanes; 847691cc2fSChicago Duan } 85d1bde9e5SKrzysztof Grobelny 86d1bde9e5SKrzysztof Grobelny if (slotType != nullptr) 877691cc2fSChicago Duan { 88d87fdd9eSLakshmi Yadlapati std::optional<pcie_slots::SlotTypes> redfishSlotType = 89*c49c329dSLakshmi Yadlapati pcie_util::dbusSlotTypeToRf(*slotType); 90d87fdd9eSLakshmi Yadlapati if (!redfishSlotType) 917691cc2fSChicago Duan { 927691cc2fSChicago Duan messages::internalError(asyncResp->res); 937691cc2fSChicago Duan return; 947691cc2fSChicago Duan } 95d87fdd9eSLakshmi Yadlapati if (*redfishSlotType != pcie_slots::SlotTypes::Invalid) 96d87fdd9eSLakshmi Yadlapati { 97d87fdd9eSLakshmi Yadlapati slot["SlotType"] = *redfishSlotType; 98d87fdd9eSLakshmi Yadlapati } 997691cc2fSChicago Duan } 100d1bde9e5SKrzysztof Grobelny 101d1bde9e5SKrzysztof Grobelny if (hotPluggable != nullptr) 1027691cc2fSChicago Duan { 103d1bde9e5SKrzysztof Grobelny slot["HotPluggable"] = *hotPluggable; 1047691cc2fSChicago Duan } 105d1bde9e5SKrzysztof Grobelny 1067691cc2fSChicago Duan slots.emplace_back(std::move(slot)); 1077691cc2fSChicago Duan } 1087691cc2fSChicago Duan 1097691cc2fSChicago Duan inline void onMapperAssociationDone( 1107691cc2fSChicago Duan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1117691cc2fSChicago Duan const std::string& chassisID, const std::string& pcieSlotPath, 1125e7e2dc5SEd Tanous const std::string& connectionName, const boost::system::error_code& ec, 1136c3e9451SGeorge Liu const dbus::utility::MapperEndPoints& pcieSlotChassis) 1147691cc2fSChicago Duan { 1157691cc2fSChicago Duan if (ec) 1167691cc2fSChicago Duan { 1177691cc2fSChicago Duan if (ec.value() == EBADR) 1187691cc2fSChicago Duan { 1197691cc2fSChicago Duan // This PCIeSlot have no chassis association. 1207691cc2fSChicago Duan return; 1217691cc2fSChicago Duan } 1227691cc2fSChicago Duan BMCWEB_LOG_ERROR << "DBUS response error"; 1237691cc2fSChicago Duan messages::internalError(asyncResp->res); 1247691cc2fSChicago Duan return; 1257691cc2fSChicago Duan } 1267691cc2fSChicago Duan 1276c3e9451SGeorge Liu if (pcieSlotChassis.size() != 1) 1287691cc2fSChicago Duan { 1297691cc2fSChicago Duan BMCWEB_LOG_ERROR << "PCIe Slot association error! "; 1307691cc2fSChicago Duan messages::internalError(asyncResp->res); 1317691cc2fSChicago Duan return; 1327691cc2fSChicago Duan } 1337691cc2fSChicago Duan 1346c3e9451SGeorge Liu sdbusplus::message::object_path path(pcieSlotChassis[0]); 1357691cc2fSChicago Duan std::string chassisName = path.filename(); 1367691cc2fSChicago Duan if (chassisName != chassisID) 1377691cc2fSChicago Duan { 1387691cc2fSChicago Duan // The pcie slot doesn't belong to the chassisID 1397691cc2fSChicago Duan return; 1407691cc2fSChicago Duan } 1417691cc2fSChicago Duan 142d1bde9e5SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 143d1bde9e5SKrzysztof Grobelny *crow::connections::systemBus, connectionName, pcieSlotPath, 144d1bde9e5SKrzysztof Grobelny "xyz.openbmc_project.Inventory.Item.PCIeSlot", 1455e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec2, 1467691cc2fSChicago Duan const dbus::utility::DBusPropertiesMap& propertiesList) { 1474ce2c1b5SEd Tanous onPcieSlotGetAllDone(asyncResp, ec2, propertiesList); 148d1bde9e5SKrzysztof Grobelny }); 1497691cc2fSChicago Duan } 1507691cc2fSChicago Duan 1517691cc2fSChicago Duan inline void 1527691cc2fSChicago Duan onMapperSubtreeDone(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1537691cc2fSChicago Duan const std::string& chassisID, 1545e7e2dc5SEd Tanous const boost::system::error_code& ec, 1557691cc2fSChicago Duan const dbus::utility::MapperGetSubTreeResponse& subtree) 1567691cc2fSChicago Duan { 1577691cc2fSChicago Duan if (ec) 1587691cc2fSChicago Duan { 1597691cc2fSChicago Duan BMCWEB_LOG_ERROR << "D-Bus response error on GetSubTree " << ec; 1607691cc2fSChicago Duan messages::internalError(asyncResp->res); 1617691cc2fSChicago Duan return; 1627691cc2fSChicago Duan } 1637691cc2fSChicago Duan if (subtree.empty()) 1647691cc2fSChicago Duan { 165d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "Chassis", chassisID); 1667691cc2fSChicago Duan return; 1677691cc2fSChicago Duan } 1687691cc2fSChicago Duan 1697691cc2fSChicago Duan BMCWEB_LOG_DEBUG << "Get properties for PCIeSlots associated to chassis = " 1707691cc2fSChicago Duan << chassisID; 1717691cc2fSChicago Duan 1727691cc2fSChicago Duan asyncResp->res.jsonValue["@odata.type"] = "#PCIeSlots.v1_4_1.PCIeSlots"; 1737691cc2fSChicago Duan asyncResp->res.jsonValue["Name"] = "PCIe Slot Information"; 174ef4c65b7SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 175ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/Chassis/{}/PCIeSlots", chassisID); 1767691cc2fSChicago Duan asyncResp->res.jsonValue["Id"] = "1"; 1777691cc2fSChicago Duan asyncResp->res.jsonValue["Slots"] = nlohmann::json::array(); 1787691cc2fSChicago Duan 1797691cc2fSChicago Duan for (const auto& pathServicePair : subtree) 1807691cc2fSChicago Duan { 1817691cc2fSChicago Duan const std::string& pcieSlotPath = pathServicePair.first; 1827691cc2fSChicago Duan for (const auto& connectionInterfacePair : pathServicePair.second) 1837691cc2fSChicago Duan { 1847691cc2fSChicago Duan const std::string& connectionName = connectionInterfacePair.first; 1857691cc2fSChicago Duan sdbusplus::message::object_path pcieSlotAssociationPath( 1867691cc2fSChicago Duan pcieSlotPath); 1877691cc2fSChicago Duan pcieSlotAssociationPath /= "chassis"; 1887691cc2fSChicago Duan 1897691cc2fSChicago Duan // The association of this PCIeSlot is used to determine whether 1907691cc2fSChicago Duan // it belongs to this ChassisID 1916c3e9451SGeorge Liu dbus::utility::getAssociationEndPoints( 1926c3e9451SGeorge Liu std::string{pcieSlotAssociationPath}, 1937691cc2fSChicago Duan [asyncResp, chassisID, pcieSlotPath, connectionName]( 1944ce2c1b5SEd Tanous const boost::system::error_code& ec2, 1956c3e9451SGeorge Liu const dbus::utility::MapperEndPoints& endpoints) { 1967691cc2fSChicago Duan onMapperAssociationDone(asyncResp, chassisID, pcieSlotPath, 1974ce2c1b5SEd Tanous connectionName, ec2, endpoints); 1986c3e9451SGeorge Liu }); 1997691cc2fSChicago Duan } 2007691cc2fSChicago Duan } 2017691cc2fSChicago Duan } 2027691cc2fSChicago Duan 2037691cc2fSChicago Duan inline void handlePCIeSlotCollectionGet( 2047691cc2fSChicago Duan crow::App& app, const crow::Request& req, 2057691cc2fSChicago Duan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2067691cc2fSChicago Duan const std::string& chassisID) 2077691cc2fSChicago Duan { 2082aacf856SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2097691cc2fSChicago Duan { 2107691cc2fSChicago Duan return; 2117691cc2fSChicago Duan } 2127691cc2fSChicago Duan 213e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 214e99073f5SGeorge Liu "xyz.openbmc_project.Inventory.Item.PCIeSlot"}; 215e99073f5SGeorge Liu dbus::utility::getSubTree( 216e99073f5SGeorge Liu "/xyz/openbmc_project/inventory", 0, interfaces, 2177691cc2fSChicago Duan [asyncResp, 218e99073f5SGeorge Liu chassisID](const boost::system::error_code& ec, 2197691cc2fSChicago Duan const dbus::utility::MapperGetSubTreeResponse& subtree) { 2207691cc2fSChicago Duan onMapperSubtreeDone(asyncResp, chassisID, ec, subtree); 221e99073f5SGeorge Liu }); 2227691cc2fSChicago Duan } 2237691cc2fSChicago Duan 2247691cc2fSChicago Duan inline void requestRoutesPCIeSlots(App& app) 2257691cc2fSChicago Duan { 2267691cc2fSChicago Duan BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PCIeSlots/") 2277691cc2fSChicago Duan .privileges(redfish::privileges::getPCIeSlots) 2287691cc2fSChicago Duan .methods(boost::beast::http::verb::get)( 2297691cc2fSChicago Duan std::bind_front(handlePCIeSlotCollectionGet, std::ref(app))); 2307691cc2fSChicago Duan } 2317691cc2fSChicago Duan 2327691cc2fSChicago Duan } // namespace redfish 233