140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0 240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors 37691cc2fSChicago Duan #pragma once 47691cc2fSChicago Duan 53ccb3adbSEd Tanous #include "app.hpp" 6d7857201SEd Tanous #include "async_resp.hpp" 7e99073f5SGeorge Liu #include "dbus_utility.hpp" 87691cc2fSChicago Duan #include "error_messages.hpp" 9d7857201SEd Tanous #include "generated/enums/pcie_device.hpp" 104ce2c1b5SEd Tanous #include "generated/enums/pcie_slots.hpp" 11d7857201SEd Tanous #include "http_request.hpp" 12d7857201SEd Tanous #include "logging.hpp" 13c49c329dSLakshmi Yadlapati #include "query.hpp" 143ccb3adbSEd Tanous #include "registries/privilege_registry.hpp" 153ccb3adbSEd Tanous #include "utils/dbus_utils.hpp" 16c49c329dSLakshmi Yadlapati #include "utils/pcie_util.hpp" 177691cc2fSChicago Duan 18d7857201SEd Tanous #include <asm-generic/errno.h> 19d7857201SEd Tanous 20d7857201SEd Tanous #include <boost/beast/http/verb.hpp> 21e99073f5SGeorge Liu #include <boost/system/error_code.hpp> 22ef4c65b7SEd Tanous #include <boost/url/format.hpp> 23d7857201SEd Tanous #include <nlohmann/json.hpp> 24d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp> 25d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp> 267691cc2fSChicago Duan 27e99073f5SGeorge Liu #include <array> 28d7857201SEd Tanous #include <cstddef> 29d7857201SEd Tanous #include <functional> 30d7857201SEd Tanous #include <memory> 31d7857201SEd Tanous #include <optional> 32d7857201SEd Tanous #include <string> 33e99073f5SGeorge Liu #include <string_view> 34d7857201SEd Tanous #include <utility> 35e99073f5SGeorge Liu 367691cc2fSChicago Duan namespace redfish 377691cc2fSChicago Duan { 387691cc2fSChicago Duan 39*504af5a0SPatrick Williams inline void onPcieSlotGetAllDone( 40*504af5a0SPatrick Williams const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 415e7e2dc5SEd Tanous const boost::system::error_code& ec, 427691cc2fSChicago Duan const dbus::utility::DBusPropertiesMap& propertiesList) 437691cc2fSChicago Duan { 447691cc2fSChicago Duan if (ec) 457691cc2fSChicago Duan { 4662598e31SEd Tanous BMCWEB_LOG_ERROR("Can't get PCIeSlot properties!"); 477691cc2fSChicago Duan messages::internalError(asyncResp->res); 487691cc2fSChicago Duan return; 497691cc2fSChicago Duan } 507691cc2fSChicago Duan 517691cc2fSChicago Duan nlohmann::json& slots = asyncResp->res.jsonValue["Slots"]; 527691cc2fSChicago Duan 537691cc2fSChicago Duan nlohmann::json::array_t* slotsPtr = 547691cc2fSChicago Duan slots.get_ptr<nlohmann::json::array_t*>(); 557691cc2fSChicago Duan if (slotsPtr == nullptr) 567691cc2fSChicago Duan { 5762598e31SEd Tanous BMCWEB_LOG_ERROR("Slots key isn't an array???"); 587691cc2fSChicago Duan messages::internalError(asyncResp->res); 597691cc2fSChicago Duan return; 607691cc2fSChicago Duan } 617691cc2fSChicago Duan 627691cc2fSChicago Duan nlohmann::json::object_t slot; 637691cc2fSChicago Duan 64d1bde9e5SKrzysztof Grobelny const std::string* generation = nullptr; 65d1bde9e5SKrzysztof Grobelny const size_t* lanes = nullptr; 66d1bde9e5SKrzysztof Grobelny const std::string* slotType = nullptr; 67d1bde9e5SKrzysztof Grobelny const bool* hotPluggable = nullptr; 687691cc2fSChicago Duan 69d1bde9e5SKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 70d1bde9e5SKrzysztof Grobelny dbus_utils::UnpackErrorPrinter(), propertiesList, "Generation", 71d1bde9e5SKrzysztof Grobelny generation, "Lanes", lanes, "SlotType", slotType, "HotPluggable", 72d1bde9e5SKrzysztof Grobelny hotPluggable); 73d1bde9e5SKrzysztof Grobelny 74d1bde9e5SKrzysztof Grobelny if (!success) 757691cc2fSChicago Duan { 767691cc2fSChicago Duan messages::internalError(asyncResp->res); 777691cc2fSChicago Duan return; 787691cc2fSChicago Duan } 79d1bde9e5SKrzysztof Grobelny 80d1bde9e5SKrzysztof Grobelny if (generation != nullptr) 81d1bde9e5SKrzysztof Grobelny { 820ec8b83dSEd Tanous std::optional<pcie_device::PCIeTypes> pcieType = 83c49c329dSLakshmi Yadlapati pcie_util::redfishPcieGenerationFromDbus(*generation); 847691cc2fSChicago Duan if (!pcieType) 857691cc2fSChicago Duan { 8662598e31SEd Tanous BMCWEB_LOG_WARNING("Unknown PCIe Slot Generation: {}", *generation); 87cf3b484eSLakshmi Yadlapati } 88cf3b484eSLakshmi Yadlapati else 89cf3b484eSLakshmi Yadlapati { 90cf3b484eSLakshmi Yadlapati if (*pcieType == pcie_device::PCIeTypes::Invalid) 91cf3b484eSLakshmi Yadlapati { 927691cc2fSChicago Duan messages::internalError(asyncResp->res); 937691cc2fSChicago Duan return; 947691cc2fSChicago Duan } 95bbf372a6SLakshmi Yadlapati slot["PCIeType"] = *pcieType; 96bbf372a6SLakshmi Yadlapati } 977691cc2fSChicago Duan } 98d1bde9e5SKrzysztof Grobelny 9982f80326SKonstantin Aladyshev if (lanes != nullptr && *lanes != 0) 1007691cc2fSChicago Duan { 101d1bde9e5SKrzysztof Grobelny slot["Lanes"] = *lanes; 1027691cc2fSChicago Duan } 103d1bde9e5SKrzysztof Grobelny 104d1bde9e5SKrzysztof Grobelny if (slotType != nullptr) 1057691cc2fSChicago Duan { 106d87fdd9eSLakshmi Yadlapati std::optional<pcie_slots::SlotTypes> redfishSlotType = 107c49c329dSLakshmi Yadlapati pcie_util::dbusSlotTypeToRf(*slotType); 108d87fdd9eSLakshmi Yadlapati if (!redfishSlotType) 1097691cc2fSChicago Duan { 11062598e31SEd Tanous BMCWEB_LOG_WARNING("Unknown PCIe Slot Type: {}", *slotType); 111cf3b484eSLakshmi Yadlapati } 112cf3b484eSLakshmi Yadlapati else 113cf3b484eSLakshmi Yadlapati { 114cf3b484eSLakshmi Yadlapati if (*redfishSlotType == pcie_slots::SlotTypes::Invalid) 115cf3b484eSLakshmi Yadlapati { 11662598e31SEd Tanous BMCWEB_LOG_ERROR("Unknown PCIe Slot Type: {}", *slotType); 1177691cc2fSChicago Duan messages::internalError(asyncResp->res); 1187691cc2fSChicago Duan return; 1197691cc2fSChicago Duan } 120d87fdd9eSLakshmi Yadlapati slot["SlotType"] = *redfishSlotType; 121d87fdd9eSLakshmi Yadlapati } 1227691cc2fSChicago Duan } 123d1bde9e5SKrzysztof Grobelny 124d1bde9e5SKrzysztof Grobelny if (hotPluggable != nullptr) 1257691cc2fSChicago Duan { 126d1bde9e5SKrzysztof Grobelny slot["HotPluggable"] = *hotPluggable; 1277691cc2fSChicago Duan } 128d1bde9e5SKrzysztof Grobelny 1297691cc2fSChicago Duan slots.emplace_back(std::move(slot)); 1307691cc2fSChicago Duan } 1317691cc2fSChicago Duan 1327691cc2fSChicago Duan inline void onMapperAssociationDone( 1337691cc2fSChicago Duan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1347691cc2fSChicago Duan const std::string& chassisID, const std::string& pcieSlotPath, 1355e7e2dc5SEd Tanous const std::string& connectionName, const boost::system::error_code& ec, 1366c3e9451SGeorge Liu const dbus::utility::MapperEndPoints& pcieSlotChassis) 1377691cc2fSChicago Duan { 1387691cc2fSChicago Duan if (ec) 1397691cc2fSChicago Duan { 1407691cc2fSChicago Duan if (ec.value() == EBADR) 1417691cc2fSChicago Duan { 1427691cc2fSChicago Duan // This PCIeSlot have no chassis association. 1437691cc2fSChicago Duan return; 1447691cc2fSChicago Duan } 14562598e31SEd Tanous BMCWEB_LOG_ERROR("DBUS response error"); 1467691cc2fSChicago Duan messages::internalError(asyncResp->res); 1477691cc2fSChicago Duan return; 1487691cc2fSChicago Duan } 1497691cc2fSChicago Duan 1506c3e9451SGeorge Liu if (pcieSlotChassis.size() != 1) 1517691cc2fSChicago Duan { 15262598e31SEd Tanous BMCWEB_LOG_ERROR("PCIe Slot association error! "); 1537691cc2fSChicago Duan messages::internalError(asyncResp->res); 1547691cc2fSChicago Duan return; 1557691cc2fSChicago Duan } 1567691cc2fSChicago Duan 1576c3e9451SGeorge Liu sdbusplus::message::object_path path(pcieSlotChassis[0]); 1587691cc2fSChicago Duan std::string chassisName = path.filename(); 1597691cc2fSChicago Duan if (chassisName != chassisID) 1607691cc2fSChicago Duan { 1617691cc2fSChicago Duan // The pcie slot doesn't belong to the chassisID 1627691cc2fSChicago Duan return; 1637691cc2fSChicago Duan } 1647691cc2fSChicago Duan 165deae6a78SEd Tanous dbus::utility::getAllProperties( 166deae6a78SEd Tanous connectionName, pcieSlotPath, 167d1bde9e5SKrzysztof Grobelny "xyz.openbmc_project.Inventory.Item.PCIeSlot", 1685e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec2, 1697691cc2fSChicago Duan const dbus::utility::DBusPropertiesMap& propertiesList) { 1704ce2c1b5SEd Tanous onPcieSlotGetAllDone(asyncResp, ec2, propertiesList); 171d1bde9e5SKrzysztof Grobelny }); 1727691cc2fSChicago Duan } 1737691cc2fSChicago Duan 174bd79bce8SPatrick Williams inline void onMapperSubtreeDone( 175bd79bce8SPatrick Williams const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 176bd79bce8SPatrick Williams const std::string& chassisID, const boost::system::error_code& ec, 1777691cc2fSChicago Duan const dbus::utility::MapperGetSubTreeResponse& subtree) 1787691cc2fSChicago Duan { 1797691cc2fSChicago Duan if (ec) 1807691cc2fSChicago Duan { 18162598e31SEd Tanous BMCWEB_LOG_ERROR("D-Bus response error on GetSubTree {}", ec); 1827691cc2fSChicago Duan messages::internalError(asyncResp->res); 1837691cc2fSChicago Duan return; 1847691cc2fSChicago Duan } 1857691cc2fSChicago Duan if (subtree.empty()) 1867691cc2fSChicago Duan { 187d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "Chassis", chassisID); 1887691cc2fSChicago Duan return; 1897691cc2fSChicago Duan } 1907691cc2fSChicago Duan 19162598e31SEd Tanous BMCWEB_LOG_DEBUG("Get properties for PCIeSlots associated to chassis = {}", 19262598e31SEd Tanous chassisID); 1937691cc2fSChicago Duan 1947691cc2fSChicago Duan asyncResp->res.jsonValue["@odata.type"] = "#PCIeSlots.v1_4_1.PCIeSlots"; 1957691cc2fSChicago Duan asyncResp->res.jsonValue["Name"] = "PCIe Slot Information"; 196ef4c65b7SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 197ef4c65b7SEd Tanous boost::urls::format("/redfish/v1/Chassis/{}/PCIeSlots", chassisID); 1987691cc2fSChicago Duan asyncResp->res.jsonValue["Id"] = "1"; 1997691cc2fSChicago Duan asyncResp->res.jsonValue["Slots"] = nlohmann::json::array(); 2007691cc2fSChicago Duan 2017691cc2fSChicago Duan for (const auto& pathServicePair : subtree) 2027691cc2fSChicago Duan { 2037691cc2fSChicago Duan const std::string& pcieSlotPath = pathServicePair.first; 2047691cc2fSChicago Duan for (const auto& connectionInterfacePair : pathServicePair.second) 2057691cc2fSChicago Duan { 2067691cc2fSChicago Duan const std::string& connectionName = connectionInterfacePair.first; 2077691cc2fSChicago Duan sdbusplus::message::object_path pcieSlotAssociationPath( 2087691cc2fSChicago Duan pcieSlotPath); 2097691cc2fSChicago Duan pcieSlotAssociationPath /= "chassis"; 2107691cc2fSChicago Duan 2117691cc2fSChicago Duan // The association of this PCIeSlot is used to determine whether 2127691cc2fSChicago Duan // it belongs to this ChassisID 2136c3e9451SGeorge Liu dbus::utility::getAssociationEndPoints( 2146c3e9451SGeorge Liu std::string{pcieSlotAssociationPath}, 2157691cc2fSChicago Duan [asyncResp, chassisID, pcieSlotPath, connectionName]( 2164ce2c1b5SEd Tanous const boost::system::error_code& ec2, 2176c3e9451SGeorge Liu const dbus::utility::MapperEndPoints& endpoints) { 2187691cc2fSChicago Duan onMapperAssociationDone(asyncResp, chassisID, pcieSlotPath, 2194ce2c1b5SEd Tanous connectionName, ec2, endpoints); 2206c3e9451SGeorge Liu }); 2217691cc2fSChicago Duan } 2227691cc2fSChicago Duan } 2237691cc2fSChicago Duan } 2247691cc2fSChicago Duan 2257691cc2fSChicago Duan inline void handlePCIeSlotCollectionGet( 2267691cc2fSChicago Duan crow::App& app, const crow::Request& req, 2277691cc2fSChicago Duan const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2287691cc2fSChicago Duan const std::string& chassisID) 2297691cc2fSChicago Duan { 2302aacf856SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 2317691cc2fSChicago Duan { 2327691cc2fSChicago Duan return; 2337691cc2fSChicago Duan } 2347691cc2fSChicago Duan 235e99073f5SGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 236e99073f5SGeorge Liu "xyz.openbmc_project.Inventory.Item.PCIeSlot"}; 237e99073f5SGeorge Liu dbus::utility::getSubTree( 238e99073f5SGeorge Liu "/xyz/openbmc_project/inventory", 0, interfaces, 2397691cc2fSChicago Duan [asyncResp, 240e99073f5SGeorge Liu chassisID](const boost::system::error_code& ec, 2417691cc2fSChicago Duan const dbus::utility::MapperGetSubTreeResponse& subtree) { 2427691cc2fSChicago Duan onMapperSubtreeDone(asyncResp, chassisID, ec, subtree); 243e99073f5SGeorge Liu }); 2447691cc2fSChicago Duan } 2457691cc2fSChicago Duan 2467691cc2fSChicago Duan inline void requestRoutesPCIeSlots(App& app) 2477691cc2fSChicago Duan { 2487691cc2fSChicago Duan BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/PCIeSlots/") 2497691cc2fSChicago Duan .privileges(redfish::privileges::getPCIeSlots) 2507691cc2fSChicago Duan .methods(boost::beast::http::verb::get)( 2517691cc2fSChicago Duan std::bind_front(handlePCIeSlotCollectionGet, std::ref(app))); 2527691cc2fSChicago Duan } 2537691cc2fSChicago Duan 2547691cc2fSChicago Duan } // namespace redfish 255