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 { 32*62598e31SEd Tanous 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 { 43*62598e31SEd Tanous 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*62598e31SEd Tanous BMCWEB_LOG_WARNING("Unknown PCIe Slot Generation: {}", *generation); 73cf3b484eSLakshmi Yadlapati } 74cf3b484eSLakshmi Yadlapati else 75cf3b484eSLakshmi Yadlapati { 76cf3b484eSLakshmi Yadlapati if (*pcieType == pcie_device::PCIeTypes::Invalid) 77cf3b484eSLakshmi Yadlapati { 787691cc2fSChicago Duan messages::internalError(asyncResp->res); 797691cc2fSChicago Duan return; 807691cc2fSChicago Duan } 81bbf372a6SLakshmi Yadlapati slot["PCIeType"] = *pcieType; 82bbf372a6SLakshmi Yadlapati } 837691cc2fSChicago Duan } 84d1bde9e5SKrzysztof Grobelny 8582f80326SKonstantin Aladyshev if (lanes != nullptr && *lanes != 0) 867691cc2fSChicago Duan { 87d1bde9e5SKrzysztof Grobelny slot["Lanes"] = *lanes; 887691cc2fSChicago Duan } 89d1bde9e5SKrzysztof Grobelny 90d1bde9e5SKrzysztof Grobelny if (slotType != nullptr) 917691cc2fSChicago Duan { 92d87fdd9eSLakshmi Yadlapati std::optional<pcie_slots::SlotTypes> redfishSlotType = 93c49c329dSLakshmi Yadlapati pcie_util::dbusSlotTypeToRf(*slotType); 94d87fdd9eSLakshmi Yadlapati if (!redfishSlotType) 957691cc2fSChicago Duan { 96*62598e31SEd Tanous BMCWEB_LOG_WARNING("Unknown PCIe Slot Type: {}", *slotType); 97cf3b484eSLakshmi Yadlapati } 98cf3b484eSLakshmi Yadlapati else 99cf3b484eSLakshmi Yadlapati { 100cf3b484eSLakshmi Yadlapati if (*redfishSlotType == pcie_slots::SlotTypes::Invalid) 101cf3b484eSLakshmi Yadlapati { 102*62598e31SEd Tanous BMCWEB_LOG_ERROR("Unknown PCIe Slot Type: {}", *slotType); 1037691cc2fSChicago Duan messages::internalError(asyncResp->res); 1047691cc2fSChicago Duan return; 1057691cc2fSChicago Duan } 106d87fdd9eSLakshmi Yadlapati slot["SlotType"] = *redfishSlotType; 107d87fdd9eSLakshmi Yadlapati } 1087691cc2fSChicago Duan } 109d1bde9e5SKrzysztof Grobelny 110d1bde9e5SKrzysztof Grobelny if (hotPluggable != nullptr) 1117691cc2fSChicago Duan { 112d1bde9e5SKrzysztof Grobelny slot["HotPluggable"] = *hotPluggable; 1137691cc2fSChicago Duan } 114d1bde9e5SKrzysztof Grobelny 1157691cc2fSChicago Duan slots.emplace_back(std::move(slot)); 1167691cc2fSChicago Duan } 1177691cc2fSChicago Duan 1187691cc2fSChicago Duan inline void onMapperAssociationDone( 1197691cc2fSChicago Duan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1207691cc2fSChicago Duan const std::string& chassisID, const std::string& pcieSlotPath, 1215e7e2dc5SEd Tanous const std::string& connectionName, const boost::system::error_code& ec, 1226c3e9451SGeorge Liu const dbus::utility::MapperEndPoints& pcieSlotChassis) 1237691cc2fSChicago Duan { 1247691cc2fSChicago Duan if (ec) 1257691cc2fSChicago Duan { 1267691cc2fSChicago Duan if (ec.value() == EBADR) 1277691cc2fSChicago Duan { 1287691cc2fSChicago Duan // This PCIeSlot have no chassis association. 1297691cc2fSChicago Duan return; 1307691cc2fSChicago Duan } 131*62598e31SEd Tanous BMCWEB_LOG_ERROR("DBUS response error"); 1327691cc2fSChicago Duan messages::internalError(asyncResp->res); 1337691cc2fSChicago Duan return; 1347691cc2fSChicago Duan } 1357691cc2fSChicago Duan 1366c3e9451SGeorge Liu if (pcieSlotChassis.size() != 1) 1377691cc2fSChicago Duan { 138*62598e31SEd Tanous BMCWEB_LOG_ERROR("PCIe Slot association error! "); 1397691cc2fSChicago Duan messages::internalError(asyncResp->res); 1407691cc2fSChicago Duan return; 1417691cc2fSChicago Duan } 1427691cc2fSChicago Duan 1436c3e9451SGeorge Liu sdbusplus::message::object_path path(pcieSlotChassis[0]); 1447691cc2fSChicago Duan std::string chassisName = path.filename(); 1457691cc2fSChicago Duan if (chassisName != chassisID) 1467691cc2fSChicago Duan { 1477691cc2fSChicago Duan // The pcie slot doesn't belong to the chassisID 1487691cc2fSChicago Duan return; 1497691cc2fSChicago Duan } 1507691cc2fSChicago Duan 151d1bde9e5SKrzysztof Grobelny sdbusplus::asio::getAllProperties( 152d1bde9e5SKrzysztof Grobelny *crow::connections::systemBus, connectionName, pcieSlotPath, 153d1bde9e5SKrzysztof Grobelny "xyz.openbmc_project.Inventory.Item.PCIeSlot", 1545e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec2, 1557691cc2fSChicago Duan const dbus::utility::DBusPropertiesMap& propertiesList) { 1564ce2c1b5SEd Tanous onPcieSlotGetAllDone(asyncResp, ec2, propertiesList); 157d1bde9e5SKrzysztof Grobelny }); 1587691cc2fSChicago Duan } 1597691cc2fSChicago Duan 1607691cc2fSChicago Duan inline void 1617691cc2fSChicago Duan onMapperSubtreeDone(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1627691cc2fSChicago Duan const std::string& chassisID, 1635e7e2dc5SEd Tanous const boost::system::error_code& ec, 1647691cc2fSChicago Duan const dbus::utility::MapperGetSubTreeResponse& subtree) 1657691cc2fSChicago Duan { 1667691cc2fSChicago Duan if (ec) 1677691cc2fSChicago Duan { 168*62598e31SEd Tanous BMCWEB_LOG_ERROR("D-Bus response error on GetSubTree {}", ec); 1697691cc2fSChicago Duan messages::internalError(asyncResp->res); 1707691cc2fSChicago Duan return; 1717691cc2fSChicago Duan } 1727691cc2fSChicago Duan if (subtree.empty()) 1737691cc2fSChicago Duan { 174d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "Chassis", chassisID); 1757691cc2fSChicago Duan return; 1767691cc2fSChicago Duan } 1777691cc2fSChicago Duan 178*62598e31SEd Tanous BMCWEB_LOG_DEBUG("Get properties for PCIeSlots associated to chassis = {}", 179*62598e31SEd Tanous chassisID); 1807691cc2fSChicago Duan 1817691cc2fSChicago Duan asyncResp->res.jsonValue["@odata.type"] = "#PCIeSlots.v1_4_1.PCIeSlots"; 1827691cc2fSChicago Duan asyncResp->res.jsonValue["Name"] = "PCIe Slot Information"; 183ef4c65b7SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 184ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/Chassis/{}/PCIeSlots", chassisID); 1857691cc2fSChicago Duan asyncResp->res.jsonValue["Id"] = "1"; 1867691cc2fSChicago Duan asyncResp->res.jsonValue["Slots"] = nlohmann::json::array(); 1877691cc2fSChicago Duan 1887691cc2fSChicago Duan for (const auto& pathServicePair : subtree) 1897691cc2fSChicago Duan { 1907691cc2fSChicago Duan const std::string& pcieSlotPath = pathServicePair.first; 1917691cc2fSChicago Duan for (const auto& connectionInterfacePair : pathServicePair.second) 1927691cc2fSChicago Duan { 1937691cc2fSChicago Duan const std::string& connectionName = connectionInterfacePair.first; 1947691cc2fSChicago Duan sdbusplus::message::object_path pcieSlotAssociationPath( 1957691cc2fSChicago Duan pcieSlotPath); 1967691cc2fSChicago Duan pcieSlotAssociationPath /= "chassis"; 1977691cc2fSChicago Duan 1987691cc2fSChicago Duan // The association of this PCIeSlot is used to determine whether 1997691cc2fSChicago Duan // it belongs to this ChassisID 2006c3e9451SGeorge Liu dbus::utility::getAssociationEndPoints( 2016c3e9451SGeorge Liu std::string{pcieSlotAssociationPath}, 2027691cc2fSChicago Duan [asyncResp, chassisID, pcieSlotPath, connectionName]( 2034ce2c1b5SEd Tanous const boost::system::error_code& ec2, 2046c3e9451SGeorge Liu const dbus::utility::MapperEndPoints& endpoints) { 2057691cc2fSChicago Duan onMapperAssociationDone(asyncResp, chassisID, pcieSlotPath, 2064ce2c1b5SEd Tanous connectionName, ec2, endpoints); 2076c3e9451SGeorge Liu }); 2087691cc2fSChicago Duan } 2097691cc2fSChicago Duan } 2107691cc2fSChicago Duan } 2117691cc2fSChicago Duan 2127691cc2fSChicago Duan inline void handlePCIeSlotCollectionGet( 2137691cc2fSChicago Duan crow::App& app, const crow::Request& req, 2147691cc2fSChicago Duan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2157691cc2fSChicago Duan const std::string& chassisID) 2167691cc2fSChicago Duan { 2172aacf856SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2187691cc2fSChicago Duan { 2197691cc2fSChicago Duan return; 2207691cc2fSChicago Duan } 2217691cc2fSChicago Duan 222e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 223e99073f5SGeorge Liu "xyz.openbmc_project.Inventory.Item.PCIeSlot"}; 224e99073f5SGeorge Liu dbus::utility::getSubTree( 225e99073f5SGeorge Liu "/xyz/openbmc_project/inventory", 0, interfaces, 2267691cc2fSChicago Duan [asyncResp, 227e99073f5SGeorge Liu chassisID](const boost::system::error_code& ec, 2287691cc2fSChicago Duan const dbus::utility::MapperGetSubTreeResponse& subtree) { 2297691cc2fSChicago Duan onMapperSubtreeDone(asyncResp, chassisID, ec, subtree); 230e99073f5SGeorge Liu }); 2317691cc2fSChicago Duan } 2327691cc2fSChicago Duan 2337691cc2fSChicago Duan inline void requestRoutesPCIeSlots(App& app) 2347691cc2fSChicago Duan { 2357691cc2fSChicago Duan BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PCIeSlots/") 2367691cc2fSChicago Duan .privileges(redfish::privileges::getPCIeSlots) 2377691cc2fSChicago Duan .methods(boost::beast::http::verb::get)( 2387691cc2fSChicago Duan std::bind_front(handlePCIeSlotCollectionGet, std::ref(app))); 2397691cc2fSChicago Duan } 2407691cc2fSChicago Duan 2417691cc2fSChicago Duan } // namespace redfish 242