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" 73ccb3adbSEd Tanous #include "pcie.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" 127691cc2fSChicago Duan 13e99073f5SGeorge Liu #include <boost/system/error_code.hpp> 14*ef4c65b7SEd Tanous #include <boost/url/format.hpp> 15d1bde9e5SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp> 16d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp> 177691cc2fSChicago Duan 18e99073f5SGeorge Liu #include <array> 19e99073f5SGeorge Liu #include <string_view> 20e99073f5SGeorge Liu 217691cc2fSChicago Duan namespace redfish 227691cc2fSChicago Duan { 237691cc2fSChicago Duan 244ce2c1b5SEd Tanous inline pcie_slots::SlotTypes dbusSlotTypeToRf(const std::string& slotType) 257691cc2fSChicago Duan { 267691cc2fSChicago Duan if (slotType == 277691cc2fSChicago Duan "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.FullLength") 287691cc2fSChicago Duan { 294ce2c1b5SEd Tanous return pcie_slots::SlotTypes::FullLength; 307691cc2fSChicago Duan } 317691cc2fSChicago Duan if (slotType == 327691cc2fSChicago Duan "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.HalfLength") 337691cc2fSChicago Duan { 344ce2c1b5SEd Tanous return pcie_slots::SlotTypes::HalfLength; 357691cc2fSChicago Duan } 367691cc2fSChicago Duan if (slotType == 377691cc2fSChicago Duan "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.LowProfile") 387691cc2fSChicago Duan { 394ce2c1b5SEd Tanous return pcie_slots::SlotTypes::LowProfile; 407691cc2fSChicago Duan } 417691cc2fSChicago Duan if (slotType == 427691cc2fSChicago Duan "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.Mini") 437691cc2fSChicago Duan { 444ce2c1b5SEd Tanous return pcie_slots::SlotTypes::Mini; 457691cc2fSChicago Duan } 467691cc2fSChicago Duan if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.M_2") 477691cc2fSChicago Duan { 484ce2c1b5SEd Tanous return pcie_slots::SlotTypes::M2; 497691cc2fSChicago Duan } 507691cc2fSChicago Duan if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OEM") 517691cc2fSChicago Duan { 524ce2c1b5SEd Tanous return pcie_slots::SlotTypes::OEM; 537691cc2fSChicago Duan } 547691cc2fSChicago Duan if (slotType == 557691cc2fSChicago Duan "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OCP3Small") 567691cc2fSChicago Duan { 574ce2c1b5SEd Tanous return pcie_slots::SlotTypes::OCP3Small; 587691cc2fSChicago Duan } 597691cc2fSChicago Duan if (slotType == 607691cc2fSChicago Duan "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OCP3Large") 617691cc2fSChicago Duan { 624ce2c1b5SEd Tanous return pcie_slots::SlotTypes::OCP3Large; 637691cc2fSChicago Duan } 647691cc2fSChicago Duan if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.U_2") 657691cc2fSChicago Duan { 664ce2c1b5SEd Tanous return pcie_slots::SlotTypes::U2; 677691cc2fSChicago Duan } 687691cc2fSChicago Duan 697691cc2fSChicago Duan // Unknown or others 704ce2c1b5SEd Tanous return pcie_slots::SlotTypes::Invalid; 717691cc2fSChicago Duan } 727691cc2fSChicago Duan 737691cc2fSChicago Duan inline void 747691cc2fSChicago Duan onPcieSlotGetAllDone(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 755e7e2dc5SEd Tanous const boost::system::error_code& ec, 767691cc2fSChicago Duan const dbus::utility::DBusPropertiesMap& propertiesList) 777691cc2fSChicago Duan { 787691cc2fSChicago Duan if (ec) 797691cc2fSChicago Duan { 807691cc2fSChicago Duan BMCWEB_LOG_ERROR << "Can't get PCIeSlot properties!"; 817691cc2fSChicago Duan messages::internalError(asyncResp->res); 827691cc2fSChicago Duan return; 837691cc2fSChicago Duan } 847691cc2fSChicago Duan 857691cc2fSChicago Duan nlohmann::json& slots = asyncResp->res.jsonValue["Slots"]; 867691cc2fSChicago Duan 877691cc2fSChicago Duan nlohmann::json::array_t* slotsPtr = 887691cc2fSChicago Duan slots.get_ptr<nlohmann::json::array_t*>(); 897691cc2fSChicago Duan if (slotsPtr == nullptr) 907691cc2fSChicago Duan { 917691cc2fSChicago Duan BMCWEB_LOG_ERROR << "Slots key isn't an array???"; 927691cc2fSChicago Duan messages::internalError(asyncResp->res); 937691cc2fSChicago Duan return; 947691cc2fSChicago Duan } 957691cc2fSChicago Duan 967691cc2fSChicago Duan nlohmann::json::object_t slot; 977691cc2fSChicago Duan 98d1bde9e5SKrzysztof Grobelny const std::string* generation = nullptr; 99d1bde9e5SKrzysztof Grobelny const size_t* lanes = nullptr; 100d1bde9e5SKrzysztof Grobelny const std::string* slotType = nullptr; 101d1bde9e5SKrzysztof Grobelny const bool* hotPluggable = nullptr; 1027691cc2fSChicago Duan 103d1bde9e5SKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 104d1bde9e5SKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), propertiesList, "Generation", 105d1bde9e5SKrzysztof Grobelny generation, "Lanes", lanes, "SlotType", slotType, "HotPluggable", 106d1bde9e5SKrzysztof Grobelny hotPluggable); 107d1bde9e5SKrzysztof Grobelny 108d1bde9e5SKrzysztof Grobelny if (!success) 1097691cc2fSChicago Duan { 1107691cc2fSChicago Duan messages::internalError(asyncResp->res); 1117691cc2fSChicago Duan return; 1127691cc2fSChicago Duan } 113d1bde9e5SKrzysztof Grobelny 114d1bde9e5SKrzysztof Grobelny if (generation != nullptr) 115d1bde9e5SKrzysztof Grobelny { 1160ec8b83dSEd Tanous std::optional<pcie_device::PCIeTypes> pcieType = 117d1bde9e5SKrzysztof Grobelny redfishPcieGenerationFromDbus(*generation); 1187691cc2fSChicago Duan if (!pcieType) 1197691cc2fSChicago Duan { 1207691cc2fSChicago Duan messages::internalError(asyncResp->res); 1217691cc2fSChicago Duan return; 1227691cc2fSChicago Duan } 123bbf372a6SLakshmi Yadlapati if (*pcieType != pcie_device::PCIeTypes::Invalid) 124bbf372a6SLakshmi Yadlapati { 125bbf372a6SLakshmi Yadlapati slot["PCIeType"] = *pcieType; 126bbf372a6SLakshmi Yadlapati } 1277691cc2fSChicago Duan } 128d1bde9e5SKrzysztof Grobelny 129d1bde9e5SKrzysztof Grobelny if (lanes != nullptr) 1307691cc2fSChicago Duan { 131d1bde9e5SKrzysztof Grobelny slot["Lanes"] = *lanes; 1327691cc2fSChicago Duan } 133d1bde9e5SKrzysztof Grobelny 134d1bde9e5SKrzysztof Grobelny if (slotType != nullptr) 1357691cc2fSChicago Duan { 1364ce2c1b5SEd Tanous pcie_slots::SlotTypes redfishSlotType = dbusSlotTypeToRf(*slotType); 1374ce2c1b5SEd Tanous if (redfishSlotType == pcie_slots::SlotTypes::Invalid) 1387691cc2fSChicago Duan { 1397691cc2fSChicago Duan messages::internalError(asyncResp->res); 1407691cc2fSChicago Duan return; 1417691cc2fSChicago Duan } 142d1bde9e5SKrzysztof Grobelny slot["SlotType"] = redfishSlotType; 1437691cc2fSChicago Duan } 144d1bde9e5SKrzysztof Grobelny 145d1bde9e5SKrzysztof Grobelny if (hotPluggable != nullptr) 1467691cc2fSChicago Duan { 147d1bde9e5SKrzysztof Grobelny slot["HotPluggable"] = *hotPluggable; 1487691cc2fSChicago Duan } 149d1bde9e5SKrzysztof Grobelny 1507691cc2fSChicago Duan slots.emplace_back(std::move(slot)); 1517691cc2fSChicago Duan } 1527691cc2fSChicago Duan 1537691cc2fSChicago Duan inline void onMapperAssociationDone( 1547691cc2fSChicago Duan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1557691cc2fSChicago Duan const std::string& chassisID, const std::string& pcieSlotPath, 1565e7e2dc5SEd Tanous const std::string& connectionName, const boost::system::error_code& ec, 1576c3e9451SGeorge Liu const dbus::utility::MapperEndPoints& pcieSlotChassis) 1587691cc2fSChicago Duan { 1597691cc2fSChicago Duan if (ec) 1607691cc2fSChicago Duan { 1617691cc2fSChicago Duan if (ec.value() == EBADR) 1627691cc2fSChicago Duan { 1637691cc2fSChicago Duan // This PCIeSlot have no chassis association. 1647691cc2fSChicago Duan return; 1657691cc2fSChicago Duan } 1667691cc2fSChicago Duan BMCWEB_LOG_ERROR << "DBUS response error"; 1677691cc2fSChicago Duan messages::internalError(asyncResp->res); 1687691cc2fSChicago Duan return; 1697691cc2fSChicago Duan } 1707691cc2fSChicago Duan 1716c3e9451SGeorge Liu if (pcieSlotChassis.size() != 1) 1727691cc2fSChicago Duan { 1737691cc2fSChicago Duan BMCWEB_LOG_ERROR << "PCIe Slot association error! "; 1747691cc2fSChicago Duan messages::internalError(asyncResp->res); 1757691cc2fSChicago Duan return; 1767691cc2fSChicago Duan } 1777691cc2fSChicago Duan 1786c3e9451SGeorge Liu sdbusplus::message::object_path path(pcieSlotChassis[0]); 1797691cc2fSChicago Duan std::string chassisName = path.filename(); 1807691cc2fSChicago Duan if (chassisName != chassisID) 1817691cc2fSChicago Duan { 1827691cc2fSChicago Duan // The pcie slot doesn't belong to the chassisID 1837691cc2fSChicago Duan return; 1847691cc2fSChicago Duan } 1857691cc2fSChicago Duan 186d1bde9e5SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 187d1bde9e5SKrzysztof Grobelny *crow::connections::systemBus, connectionName, pcieSlotPath, 188d1bde9e5SKrzysztof Grobelny "xyz.openbmc_project.Inventory.Item.PCIeSlot", 1895e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec2, 1907691cc2fSChicago Duan const dbus::utility::DBusPropertiesMap& propertiesList) { 1914ce2c1b5SEd Tanous onPcieSlotGetAllDone(asyncResp, ec2, propertiesList); 192d1bde9e5SKrzysztof Grobelny }); 1937691cc2fSChicago Duan } 1947691cc2fSChicago Duan 1957691cc2fSChicago Duan inline void 1967691cc2fSChicago Duan onMapperSubtreeDone(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1977691cc2fSChicago Duan const std::string& chassisID, 1985e7e2dc5SEd Tanous const boost::system::error_code& ec, 1997691cc2fSChicago Duan const dbus::utility::MapperGetSubTreeResponse& subtree) 2007691cc2fSChicago Duan { 2017691cc2fSChicago Duan if (ec) 2027691cc2fSChicago Duan { 2037691cc2fSChicago Duan BMCWEB_LOG_ERROR << "D-Bus response error on GetSubTree " << ec; 2047691cc2fSChicago Duan messages::internalError(asyncResp->res); 2057691cc2fSChicago Duan return; 2067691cc2fSChicago Duan } 2077691cc2fSChicago Duan if (subtree.empty()) 2087691cc2fSChicago Duan { 209d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "Chassis", chassisID); 2107691cc2fSChicago Duan return; 2117691cc2fSChicago Duan } 2127691cc2fSChicago Duan 2137691cc2fSChicago Duan BMCWEB_LOG_DEBUG << "Get properties for PCIeSlots associated to chassis = " 2147691cc2fSChicago Duan << chassisID; 2157691cc2fSChicago Duan 2167691cc2fSChicago Duan asyncResp->res.jsonValue["@odata.type"] = "#PCIeSlots.v1_4_1.PCIeSlots"; 2177691cc2fSChicago Duan asyncResp->res.jsonValue["Name"] = "PCIe Slot Information"; 218*ef4c65b7SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 219*ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/Chassis/{}/PCIeSlots", chassisID); 2207691cc2fSChicago Duan asyncResp->res.jsonValue["Id"] = "1"; 2217691cc2fSChicago Duan asyncResp->res.jsonValue["Slots"] = nlohmann::json::array(); 2227691cc2fSChicago Duan 2237691cc2fSChicago Duan for (const auto& pathServicePair : subtree) 2247691cc2fSChicago Duan { 2257691cc2fSChicago Duan const std::string& pcieSlotPath = pathServicePair.first; 2267691cc2fSChicago Duan for (const auto& connectionInterfacePair : pathServicePair.second) 2277691cc2fSChicago Duan { 2287691cc2fSChicago Duan const std::string& connectionName = connectionInterfacePair.first; 2297691cc2fSChicago Duan sdbusplus::message::object_path pcieSlotAssociationPath( 2307691cc2fSChicago Duan pcieSlotPath); 2317691cc2fSChicago Duan pcieSlotAssociationPath /= "chassis"; 2327691cc2fSChicago Duan 2337691cc2fSChicago Duan // The association of this PCIeSlot is used to determine whether 2347691cc2fSChicago Duan // it belongs to this ChassisID 2356c3e9451SGeorge Liu dbus::utility::getAssociationEndPoints( 2366c3e9451SGeorge Liu std::string{pcieSlotAssociationPath}, 2377691cc2fSChicago Duan [asyncResp, chassisID, pcieSlotPath, connectionName]( 2384ce2c1b5SEd Tanous const boost::system::error_code& ec2, 2396c3e9451SGeorge Liu const dbus::utility::MapperEndPoints& endpoints) { 2407691cc2fSChicago Duan onMapperAssociationDone(asyncResp, chassisID, pcieSlotPath, 2414ce2c1b5SEd Tanous connectionName, ec2, endpoints); 2426c3e9451SGeorge Liu }); 2437691cc2fSChicago Duan } 2447691cc2fSChicago Duan } 2457691cc2fSChicago Duan } 2467691cc2fSChicago Duan 2477691cc2fSChicago Duan inline void handlePCIeSlotCollectionGet( 2487691cc2fSChicago Duan crow::App& app, const crow::Request& req, 2497691cc2fSChicago Duan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2507691cc2fSChicago Duan const std::string& chassisID) 2517691cc2fSChicago Duan { 2522aacf856SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2537691cc2fSChicago Duan { 2547691cc2fSChicago Duan return; 2557691cc2fSChicago Duan } 2567691cc2fSChicago Duan 257e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 258e99073f5SGeorge Liu "xyz.openbmc_project.Inventory.Item.PCIeSlot"}; 259e99073f5SGeorge Liu dbus::utility::getSubTree( 260e99073f5SGeorge Liu "/xyz/openbmc_project/inventory", 0, interfaces, 2617691cc2fSChicago Duan [asyncResp, 262e99073f5SGeorge Liu chassisID](const boost::system::error_code& ec, 2637691cc2fSChicago Duan const dbus::utility::MapperGetSubTreeResponse& subtree) { 2647691cc2fSChicago Duan onMapperSubtreeDone(asyncResp, chassisID, ec, subtree); 265e99073f5SGeorge Liu }); 2667691cc2fSChicago Duan } 2677691cc2fSChicago Duan 2687691cc2fSChicago Duan inline void requestRoutesPCIeSlots(App& app) 2697691cc2fSChicago Duan { 2707691cc2fSChicago Duan BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PCIeSlots/") 2717691cc2fSChicago Duan .privileges(redfish::privileges::getPCIeSlots) 2727691cc2fSChicago Duan .methods(boost::beast::http::verb::get)( 2737691cc2fSChicago Duan std::bind_front(handlePCIeSlotCollectionGet, std::ref(app))); 2747691cc2fSChicago Duan } 2757691cc2fSChicago Duan 2767691cc2fSChicago Duan } // namespace redfish 277