xref: /openbmc/bmcweb/features/redfish/lib/pcie.hpp (revision 3ccb3adb9a14783f6bef601506de9f8bcae22d51)
1f5c9f8bdSJason M. Bills /*
2f5c9f8bdSJason M. Bills // Copyright (c) 2018 Intel Corporation
3f5c9f8bdSJason M. Bills //
4f5c9f8bdSJason M. Bills // Licensed under the Apache License, Version 2.0 (the "License");
5f5c9f8bdSJason M. Bills // you may not use this file except in compliance with the License.
6f5c9f8bdSJason M. Bills // You may obtain a copy of the License at
7f5c9f8bdSJason M. Bills //
8f5c9f8bdSJason M. Bills //      http://www.apache.org/licenses/LICENSE-2.0
9f5c9f8bdSJason M. Bills //
10f5c9f8bdSJason M. Bills // Unless required by applicable law or agreed to in writing, software
11f5c9f8bdSJason M. Bills // distributed under the License is distributed on an "AS IS" BASIS,
12f5c9f8bdSJason M. Bills // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f5c9f8bdSJason M. Bills // See the License for the specific language governing permissions and
14f5c9f8bdSJason M. Bills // limitations under the License.
15f5c9f8bdSJason M. Bills */
16f5c9f8bdSJason M. Bills 
17f5c9f8bdSJason M. Bills #pragma once
18f5c9f8bdSJason M. Bills 
19*3ccb3adbSEd Tanous #include "app.hpp"
207a1dbc48SGeorge Liu #include "dbus_utility.hpp"
210ec8b83dSEd Tanous #include "generated/enums/pcie_device.hpp"
22*3ccb3adbSEd Tanous #include "query.hpp"
23*3ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
24*3ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
250ec8b83dSEd Tanous 
26f5c9f8bdSJason M. Bills #include <boost/system/linux_error.hpp>
27d1bde9e5SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
28d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
29f5c9f8bdSJason M. Bills 
30f5c9f8bdSJason M. Bills namespace redfish
31f5c9f8bdSJason M. Bills {
32f5c9f8bdSJason M. Bills 
33f5c9f8bdSJason M. Bills static constexpr char const* pcieService = "xyz.openbmc_project.PCIe";
34f5c9f8bdSJason M. Bills static constexpr char const* pciePath = "/xyz/openbmc_project/PCIe";
35f5c9f8bdSJason M. Bills static constexpr char const* pcieDeviceInterface =
36f5c9f8bdSJason M. Bills     "xyz.openbmc_project.PCIe.Device";
37f5c9f8bdSJason M. Bills 
38b5a76932SEd Tanous static inline void
398d1b46d7Szhanghch05     getPCIeDeviceList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
40adbe192aSJason M. Bills                       const std::string& name)
41f5c9f8bdSJason M. Bills {
427a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
437a1dbc48SGeorge Liu         pciePath, 1, {},
447a1dbc48SGeorge Liu         [asyncResp, name](const boost::system::error_code& ec,
45b9d36b47SEd Tanous                           const dbus::utility::MapperGetSubTreePathsResponse&
46b9d36b47SEd Tanous                               pcieDevicePaths) {
47f5c9f8bdSJason M. Bills         if (ec)
48f5c9f8bdSJason M. Bills         {
49a2730f01SAndrew Geissler             BMCWEB_LOG_DEBUG << "no PCIe device paths found ec: "
50f5c9f8bdSJason M. Bills                              << ec.message();
51a2730f01SAndrew Geissler             // Not an error, system just doesn't have PCIe info
52f5c9f8bdSJason M. Bills             return;
53f5c9f8bdSJason M. Bills         }
54adbe192aSJason M. Bills         nlohmann::json& pcieDeviceList = asyncResp->res.jsonValue[name];
55f5c9f8bdSJason M. Bills         pcieDeviceList = nlohmann::json::array();
56f5c9f8bdSJason M. Bills         for (const std::string& pcieDevicePath : pcieDevicePaths)
57f5c9f8bdSJason M. Bills         {
583174e4dfSEd Tanous             size_t devStart = pcieDevicePath.rfind('/');
59f5c9f8bdSJason M. Bills             if (devStart == std::string::npos)
60f5c9f8bdSJason M. Bills             {
61f5c9f8bdSJason M. Bills                 continue;
62f5c9f8bdSJason M. Bills             }
63f5c9f8bdSJason M. Bills 
64f5c9f8bdSJason M. Bills             std::string devName = pcieDevicePath.substr(devStart + 1);
65f5c9f8bdSJason M. Bills             if (devName.empty())
66f5c9f8bdSJason M. Bills             {
67f5c9f8bdSJason M. Bills                 continue;
68f5c9f8bdSJason M. Bills             }
691476687dSEd Tanous             nlohmann::json::object_t pcieDevice;
701476687dSEd Tanous             pcieDevice["@odata.id"] =
711476687dSEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + devName;
721476687dSEd Tanous             pcieDeviceList.push_back(std::move(pcieDevice));
73f5c9f8bdSJason M. Bills         }
74002d39b4SEd Tanous         asyncResp->res.jsonValue[name + "@odata.count"] = pcieDeviceList.size();
757a1dbc48SGeorge Liu         });
76f5c9f8bdSJason M. Bills }
77f5c9f8bdSJason M. Bills 
787e860f15SJohn Edward Broadbent inline void requestRoutesSystemPCIeDeviceCollection(App& app)
79adbe192aSJason M. Bills {
80adbe192aSJason M. Bills     /**
81adbe192aSJason M. Bills      * Functions triggers appropriate requests on DBus
82adbe192aSJason M. Bills      */
8322d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/PCIeDevices/")
84ed398213SEd Tanous         .privileges(redfish::privileges::getPCIeDeviceCollection)
857e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
8645ca1b86SEd Tanous             [&app](const crow::Request& req,
8722d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8822d268cbSEd Tanous                    const std::string& systemName) {
893ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
90adbe192aSJason M. Bills         {
9145ca1b86SEd Tanous             return;
9245ca1b86SEd Tanous         }
9322d268cbSEd Tanous         if (systemName != "system")
9422d268cbSEd Tanous         {
9522d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
9622d268cbSEd Tanous                                        systemName);
9722d268cbSEd Tanous             return;
9822d268cbSEd Tanous         }
991476687dSEd Tanous 
1001476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
1011476687dSEd Tanous             "#PCIeDeviceCollection.PCIeDeviceCollection";
1021476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1031476687dSEd Tanous             "/redfish/v1/Systems/system/PCIeDevices";
1041476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "PCIe Device Collection";
105002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "Collection of PCIe Devices";
1061476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
1071476687dSEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = 0;
108adbe192aSJason M. Bills         getPCIeDeviceList(asyncResp, "Members");
1097e860f15SJohn Edward Broadbent         });
110f5c9f8bdSJason M. Bills }
111f5c9f8bdSJason M. Bills 
1120ec8b83dSEd Tanous inline std::optional<pcie_device::PCIeTypes>
11362cd45afSSpencer Ku     redfishPcieGenerationFromDbus(const std::string& generationInUse)
11462cd45afSSpencer Ku {
11562cd45afSSpencer Ku     if (generationInUse ==
11662cd45afSSpencer Ku         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen1")
11762cd45afSSpencer Ku     {
1180ec8b83dSEd Tanous         return pcie_device::PCIeTypes::Gen1;
11962cd45afSSpencer Ku     }
12062cd45afSSpencer Ku     if (generationInUse ==
12162cd45afSSpencer Ku         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen2")
12262cd45afSSpencer Ku     {
1230ec8b83dSEd Tanous         return pcie_device::PCIeTypes::Gen2;
12462cd45afSSpencer Ku     }
12562cd45afSSpencer Ku     if (generationInUse ==
12662cd45afSSpencer Ku         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen3")
12762cd45afSSpencer Ku     {
1280ec8b83dSEd Tanous         return pcie_device::PCIeTypes::Gen3;
12962cd45afSSpencer Ku     }
13062cd45afSSpencer Ku     if (generationInUse ==
13162cd45afSSpencer Ku         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen4")
13262cd45afSSpencer Ku     {
1330ec8b83dSEd Tanous         return pcie_device::PCIeTypes::Gen4;
13462cd45afSSpencer Ku     }
13562cd45afSSpencer Ku     if (generationInUse ==
13662cd45afSSpencer Ku         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen5")
13762cd45afSSpencer Ku     {
1380ec8b83dSEd Tanous         return pcie_device::PCIeTypes::Gen5;
13962cd45afSSpencer Ku     }
140e825cbc8SEd Tanous     if (generationInUse.empty() ||
141e825cbc8SEd Tanous         generationInUse ==
14262cd45afSSpencer Ku             "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Unknown")
14362cd45afSSpencer Ku     {
1440ec8b83dSEd Tanous         return pcie_device::PCIeTypes::Invalid;
14562cd45afSSpencer Ku     }
14662cd45afSSpencer Ku 
14762cd45afSSpencer Ku     // The value is not unknown or Gen1-5, need return an internal error.
14862cd45afSSpencer Ku     return std::nullopt;
14962cd45afSSpencer Ku }
15062cd45afSSpencer Ku 
1517e860f15SJohn Edward Broadbent inline void requestRoutesSystemPCIeDevice(App& app)
152f5c9f8bdSJason M. Bills {
15322d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/PCIeDevices/<str>/")
154ed398213SEd Tanous         .privileges(redfish::privileges::getPCIeDevice)
155002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
156002d39b4SEd Tanous             [&app](const crow::Request& req,
1577e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
15822d268cbSEd Tanous                    const std::string& systemName, const std::string& device) {
1593ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1607e860f15SJohn Edward Broadbent         {
16145ca1b86SEd Tanous             return;
16245ca1b86SEd Tanous         }
16322d268cbSEd Tanous         if (systemName != "system")
16422d268cbSEd Tanous         {
16522d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
16622d268cbSEd Tanous                                        systemName);
16722d268cbSEd Tanous             return;
16822d268cbSEd Tanous         }
16922d268cbSEd Tanous 
170168e20c1SEd Tanous         auto getPCIeDeviceCallback =
17145ca1b86SEd Tanous             [asyncResp, device](
17245ca1b86SEd Tanous                 const boost::system::error_code ec,
17345ca1b86SEd Tanous                 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
174f5c9f8bdSJason M. Bills             if (ec)
175f5c9f8bdSJason M. Bills             {
176f5c9f8bdSJason M. Bills                 BMCWEB_LOG_DEBUG
177002d39b4SEd Tanous                     << "failed to get PCIe Device properties ec: " << ec.value()
178002d39b4SEd Tanous                     << ": " << ec.message();
17945ca1b86SEd Tanous                 if (ec.value() ==
18045ca1b86SEd Tanous                     boost::system::linux_error::bad_request_descriptor)
181f5c9f8bdSJason M. Bills                 {
182002d39b4SEd Tanous                     messages::resourceNotFound(asyncResp->res, "PCIeDevice",
183002d39b4SEd Tanous                                                device);
184f5c9f8bdSJason M. Bills                 }
185f5c9f8bdSJason M. Bills                 else
186f5c9f8bdSJason M. Bills                 {
187f5c9f8bdSJason M. Bills                     messages::internalError(asyncResp->res);
188f5c9f8bdSJason M. Bills                 }
189f5c9f8bdSJason M. Bills                 return;
190f5c9f8bdSJason M. Bills             }
191f5c9f8bdSJason M. Bills 
192d1bde9e5SKrzysztof Grobelny             const std::string* manufacturer = nullptr;
193d1bde9e5SKrzysztof Grobelny             const std::string* deviceType = nullptr;
194d1bde9e5SKrzysztof Grobelny             const std::string* generationInUse = nullptr;
195d1bde9e5SKrzysztof Grobelny 
196d1bde9e5SKrzysztof Grobelny             const bool success = sdbusplus::unpackPropertiesNoThrow(
197d1bde9e5SKrzysztof Grobelny                 dbus_utils::UnpackErrorPrinter(), pcieDevProperties,
198d1bde9e5SKrzysztof Grobelny                 "Manufacturer", manufacturer, "DeviceType", deviceType,
199d1bde9e5SKrzysztof Grobelny                 "GenerationInUse", generationInUse);
200d1bde9e5SKrzysztof Grobelny 
201d1bde9e5SKrzysztof Grobelny             if (!success)
202d1bde9e5SKrzysztof Grobelny             {
203d1bde9e5SKrzysztof Grobelny                 messages::internalError(asyncResp->res);
204d1bde9e5SKrzysztof Grobelny                 return;
205d1bde9e5SKrzysztof Grobelny             }
206d1bde9e5SKrzysztof Grobelny 
207d1bde9e5SKrzysztof Grobelny             if (generationInUse != nullptr)
208d1bde9e5SKrzysztof Grobelny             {
2090ec8b83dSEd Tanous                 std::optional<pcie_device::PCIeTypes> redfishGenerationInUse =
210d1bde9e5SKrzysztof Grobelny                     redfishPcieGenerationFromDbus(*generationInUse);
211d1bde9e5SKrzysztof Grobelny                 if (!redfishGenerationInUse)
212d1bde9e5SKrzysztof Grobelny                 {
213d1bde9e5SKrzysztof Grobelny                     messages::internalError(asyncResp->res);
214d1bde9e5SKrzysztof Grobelny                     return;
215d1bde9e5SKrzysztof Grobelny                 }
2160ec8b83dSEd Tanous                 if (*redfishGenerationInUse != pcie_device::PCIeTypes::Invalid)
217d1bde9e5SKrzysztof Grobelny                 {
218d1bde9e5SKrzysztof Grobelny                     asyncResp->res.jsonValue["PCIeInterface"]["PCIeType"] =
219d1bde9e5SKrzysztof Grobelny                         *redfishGenerationInUse;
220d1bde9e5SKrzysztof Grobelny                 }
221a9f68bb5STony Lee             }
222d1bde9e5SKrzysztof Grobelny 
223d1bde9e5SKrzysztof Grobelny             if (manufacturer != nullptr)
224d1bde9e5SKrzysztof Grobelny             {
225d1bde9e5SKrzysztof Grobelny                 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
226d1bde9e5SKrzysztof Grobelny             }
227d1bde9e5SKrzysztof Grobelny 
228d1bde9e5SKrzysztof Grobelny             if (deviceType != nullptr)
229d1bde9e5SKrzysztof Grobelny             {
230d1bde9e5SKrzysztof Grobelny                 asyncResp->res.jsonValue["DeviceType"] = *deviceType;
231d1bde9e5SKrzysztof Grobelny             }
232d1bde9e5SKrzysztof Grobelny 
2331476687dSEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
2341476687dSEd Tanous                 "#PCIeDevice.v1_4_0.PCIeDevice";
2351476687dSEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2361476687dSEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + device;
2371476687dSEd Tanous             asyncResp->res.jsonValue["Name"] = "PCIe Device";
2381476687dSEd Tanous             asyncResp->res.jsonValue["Id"] = device;
2391476687dSEd Tanous 
2401476687dSEd Tanous             asyncResp->res.jsonValue["PCIeFunctions"]["@odata.id"] =
24145ca1b86SEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + device +
2421476687dSEd Tanous                 "/PCIeFunctions";
243dede6a98SJason M. Bills         };
244dede6a98SJason M. Bills         std::string escapedPath = std::string(pciePath) + "/" + device;
245dede6a98SJason M. Bills         dbus::utility::escapePathForDbus(escapedPath);
246d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
247d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, pcieService, escapedPath,
248d1bde9e5SKrzysztof Grobelny             pcieDeviceInterface, std::move(getPCIeDeviceCallback));
2497e860f15SJohn Edward Broadbent         });
250dede6a98SJason M. Bills }
251dede6a98SJason M. Bills 
2527e860f15SJohn Edward Broadbent inline void requestRoutesSystemPCIeFunctionCollection(App& app)
2537e860f15SJohn Edward Broadbent {
254dede6a98SJason M. Bills     /**
255dede6a98SJason M. Bills      * Functions triggers appropriate requests on DBus
256dede6a98SJason M. Bills      */
2577e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2587e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/PCIeDevices/<str>/PCIeFunctions/")
259ed398213SEd Tanous         .privileges(redfish::privileges::getPCIeFunctionCollection)
260002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
261002d39b4SEd Tanous             [&app](const crow::Request& req,
2627e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26345ca1b86SEd Tanous                    const std::string& device) {
2643ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
265dede6a98SJason M. Bills         {
26645ca1b86SEd Tanous             return;
26745ca1b86SEd Tanous         }
2681476687dSEd Tanous 
2691476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
2701476687dSEd Tanous             "#PCIeFunctionCollection.PCIeFunctionCollection";
2711476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2721476687dSEd Tanous             "/redfish/v1/Systems/system/PCIeDevices/" + device +
2731476687dSEd Tanous             "/PCIeFunctions";
2741476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "PCIe Function Collection";
2751476687dSEd Tanous         asyncResp->res.jsonValue["Description"] =
2761476687dSEd Tanous             "Collection of PCIe Functions for PCIe Device " + device;
277dede6a98SJason M. Bills 
278b9d36b47SEd Tanous         auto getPCIeDeviceCallback =
27945ca1b86SEd Tanous             [asyncResp, device](
28045ca1b86SEd Tanous                 const boost::system::error_code ec,
28145ca1b86SEd Tanous                 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
2827e860f15SJohn Edward Broadbent             if (ec)
2837e860f15SJohn Edward Broadbent             {
2847e860f15SJohn Edward Broadbent                 BMCWEB_LOG_DEBUG
285002d39b4SEd Tanous                     << "failed to get PCIe Device properties ec: " << ec.value()
286002d39b4SEd Tanous                     << ": " << ec.message();
28745ca1b86SEd Tanous                 if (ec.value() ==
28845ca1b86SEd Tanous                     boost::system::linux_error::bad_request_descriptor)
2897e860f15SJohn Edward Broadbent                 {
290002d39b4SEd Tanous                     messages::resourceNotFound(asyncResp->res, "PCIeDevice",
291002d39b4SEd Tanous                                                device);
2927e860f15SJohn Edward Broadbent                 }
2937e860f15SJohn Edward Broadbent                 else
2947e860f15SJohn Edward Broadbent                 {
2957e860f15SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
2967e860f15SJohn Edward Broadbent                 }
2977e860f15SJohn Edward Broadbent                 return;
2987e860f15SJohn Edward Broadbent             }
2997e860f15SJohn Edward Broadbent 
3007e860f15SJohn Edward Broadbent             nlohmann::json& pcieFunctionList =
3017e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members"];
3027e860f15SJohn Edward Broadbent             pcieFunctionList = nlohmann::json::array();
3037e860f15SJohn Edward Broadbent             static constexpr const int maxPciFunctionNum = 8;
30445ca1b86SEd Tanous             for (int functionNum = 0; functionNum < maxPciFunctionNum;
30545ca1b86SEd Tanous                  functionNum++)
3067e860f15SJohn Edward Broadbent             {
307b9d36b47SEd Tanous                 // Check if this function exists by looking for a
308b9d36b47SEd Tanous                 // device ID
3097e860f15SJohn Edward Broadbent                 std::string devIDProperty =
310002d39b4SEd Tanous                     "Function" + std::to_string(functionNum) + "DeviceId";
311b9d36b47SEd Tanous                 const std::string* property = nullptr;
312b9d36b47SEd Tanous                 for (const auto& propEntry : pcieDevProperties)
3137e860f15SJohn Edward Broadbent                 {
314b9d36b47SEd Tanous                     if (propEntry.first == devIDProperty)
315b9d36b47SEd Tanous                     {
316002d39b4SEd Tanous                         property = std::get_if<std::string>(&propEntry.second);
317b9d36b47SEd Tanous                     }
318b9d36b47SEd Tanous                 }
319fb0ecc3eSNan Zhou                 if (property == nullptr || property->empty())
320b9d36b47SEd Tanous                 {
321e28ce0e6SNan Zhou                     continue;
322b9d36b47SEd Tanous                 }
3231476687dSEd Tanous                 nlohmann::json::object_t pcieFunction;
3241476687dSEd Tanous                 pcieFunction["@odata.id"] =
3251476687dSEd Tanous                     "/redfish/v1/Systems/system/PCIeDevices/" + device +
3261476687dSEd Tanous                     "/PCIeFunctions/" + std::to_string(functionNum);
3271476687dSEd Tanous                 pcieFunctionList.push_back(std::move(pcieFunction));
3287e860f15SJohn Edward Broadbent             }
329a818d15aSJiaqing Zhao             asyncResp->res.jsonValue["Members@odata.count"] =
3307e860f15SJohn Edward Broadbent                 pcieFunctionList.size();
3317e860f15SJohn Edward Broadbent         };
3327e860f15SJohn Edward Broadbent         std::string escapedPath = std::string(pciePath) + "/" + device;
3337e860f15SJohn Edward Broadbent         dbus::utility::escapePathForDbus(escapedPath);
334d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
335d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, pcieService, escapedPath,
336d1bde9e5SKrzysztof Grobelny             pcieDeviceInterface, std::move(getPCIeDeviceCallback));
3377e860f15SJohn Edward Broadbent         });
3387e860f15SJohn Edward Broadbent }
3397e860f15SJohn Edward Broadbent 
3407e860f15SJohn Edward Broadbent inline void requestRoutesSystemPCIeFunction(App& app)
3417e860f15SJohn Edward Broadbent {
3427e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
3437e860f15SJohn Edward Broadbent         app,
3447e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/PCIeDevices/<str>/PCIeFunctions/<str>/")
345ed398213SEd Tanous         .privileges(redfish::privileges::getPCIeFunction)
346002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
347002d39b4SEd Tanous             [&app](const crow::Request& req,
34845ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
349002d39b4SEd Tanous                    const std::string& device, const std::string& function) {
3503ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
35145ca1b86SEd Tanous         {
35245ca1b86SEd Tanous             return;
35345ca1b86SEd Tanous         }
354168e20c1SEd Tanous         auto getPCIeDeviceCallback =
355168e20c1SEd Tanous             [asyncResp, device, function](
3567e860f15SJohn Edward Broadbent                 const boost::system::error_code ec,
357b9d36b47SEd Tanous                 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
358dede6a98SJason M. Bills             if (ec)
359dede6a98SJason M. Bills             {
360dede6a98SJason M. Bills                 BMCWEB_LOG_DEBUG
361002d39b4SEd Tanous                     << "failed to get PCIe Device properties ec: " << ec.value()
362002d39b4SEd Tanous                     << ": " << ec.message();
363dede6a98SJason M. Bills                 if (ec.value() ==
364dede6a98SJason M. Bills                     boost::system::linux_error::bad_request_descriptor)
365dede6a98SJason M. Bills                 {
366002d39b4SEd Tanous                     messages::resourceNotFound(asyncResp->res, "PCIeDevice",
367002d39b4SEd Tanous                                                device);
368dede6a98SJason M. Bills                 }
369dede6a98SJason M. Bills                 else
370dede6a98SJason M. Bills                 {
371dede6a98SJason M. Bills                     messages::internalError(asyncResp->res);
372dede6a98SJason M. Bills                 }
373dede6a98SJason M. Bills                 return;
374dede6a98SJason M. Bills             }
375dede6a98SJason M. Bills 
3761476687dSEd Tanous             // Check if this function exists by looking for a device
3771476687dSEd Tanous             // ID
378b9d36b47SEd Tanous             std::string functionName = "Function" + function;
379b9d36b47SEd Tanous             std::string devIDProperty = functionName + "DeviceId";
380b9d36b47SEd Tanous 
381b9d36b47SEd Tanous             const std::string* devIdProperty = nullptr;
382b9d36b47SEd Tanous             for (const auto& property : pcieDevProperties)
383b9d36b47SEd Tanous             {
384b9d36b47SEd Tanous                 if (property.first == devIDProperty)
385b9d36b47SEd Tanous                 {
386002d39b4SEd Tanous                     devIdProperty = std::get_if<std::string>(&property.second);
387b9d36b47SEd Tanous                     continue;
388b9d36b47SEd Tanous                 }
389b9d36b47SEd Tanous             }
390973c1355STony Lee             if (devIdProperty == nullptr || devIdProperty->empty())
391f5c9f8bdSJason M. Bills             {
392002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "PCIeFunction",
393002d39b4SEd Tanous                                            function);
394f5c9f8bdSJason M. Bills                 return;
395f5c9f8bdSJason M. Bills             }
396f5c9f8bdSJason M. Bills 
3971476687dSEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
3981476687dSEd Tanous                 "#PCIeFunction.v1_2_0.PCIeFunction";
3991476687dSEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
400168e20c1SEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + device +
4011476687dSEd Tanous                 "/PCIeFunctions/" + function;
4021476687dSEd Tanous             asyncResp->res.jsonValue["Name"] = "PCIe Function";
4031476687dSEd Tanous             asyncResp->res.jsonValue["Id"] = function;
404002d39b4SEd Tanous             asyncResp->res.jsonValue["FunctionId"] = std::stoi(function);
405002d39b4SEd Tanous             asyncResp->res.jsonValue["Links"]["PCIeDevice"]["@odata.id"] =
4061476687dSEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + device;
407f5c9f8bdSJason M. Bills 
408b9d36b47SEd Tanous             for (const auto& property : pcieDevProperties)
409f5c9f8bdSJason M. Bills             {
410b9d36b47SEd Tanous                 const std::string* strProperty =
411b9d36b47SEd Tanous                     std::get_if<std::string>(&property.second);
412b9d36b47SEd Tanous                 if (property.first == functionName + "DeviceId")
413f5c9f8bdSJason M. Bills                 {
414b9d36b47SEd Tanous                     asyncResp->res.jsonValue["DeviceId"] = *strProperty;
415f5c9f8bdSJason M. Bills                 }
416b9d36b47SEd Tanous                 if (property.first == functionName + "VendorId")
417f5c9f8bdSJason M. Bills                 {
418b9d36b47SEd Tanous                     asyncResp->res.jsonValue["VendorId"] = *strProperty;
419f5c9f8bdSJason M. Bills                 }
420b9d36b47SEd Tanous                 if (property.first == functionName + "FunctionType")
421f5c9f8bdSJason M. Bills                 {
422002d39b4SEd Tanous                     asyncResp->res.jsonValue["FunctionType"] = *strProperty;
423f5c9f8bdSJason M. Bills                 }
424b9d36b47SEd Tanous                 if (property.first == functionName + "DeviceClass")
425f5c9f8bdSJason M. Bills                 {
426002d39b4SEd Tanous                     asyncResp->res.jsonValue["DeviceClass"] = *strProperty;
427f5c9f8bdSJason M. Bills                 }
428b9d36b47SEd Tanous                 if (property.first == functionName + "ClassCode")
429f5c9f8bdSJason M. Bills                 {
430002d39b4SEd Tanous                     asyncResp->res.jsonValue["ClassCode"] = *strProperty;
431f5c9f8bdSJason M. Bills                 }
432b9d36b47SEd Tanous                 if (property.first == functionName + "RevisionId")
433f5c9f8bdSJason M. Bills                 {
434002d39b4SEd Tanous                     asyncResp->res.jsonValue["RevisionId"] = *strProperty;
435f5c9f8bdSJason M. Bills                 }
436b9d36b47SEd Tanous                 if (property.first == functionName + "SubsystemId")
437b9d36b47SEd Tanous                 {
438002d39b4SEd Tanous                     asyncResp->res.jsonValue["SubsystemId"] = *strProperty;
439b9d36b47SEd Tanous                 }
440002d39b4SEd Tanous                 if (property.first == functionName + "SubsystemVendorId")
441f5c9f8bdSJason M. Bills                 {
442168e20c1SEd Tanous                     asyncResp->res.jsonValue["SubsystemVendorId"] =
443b9d36b47SEd Tanous                         *strProperty;
444b9d36b47SEd Tanous                 }
445f5c9f8bdSJason M. Bills             }
446f5c9f8bdSJason M. Bills         };
447f5c9f8bdSJason M. Bills         std::string escapedPath = std::string(pciePath) + "/" + device;
448f5c9f8bdSJason M. Bills         dbus::utility::escapePathForDbus(escapedPath);
449d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
450d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, pcieService, escapedPath,
451d1bde9e5SKrzysztof Grobelny             pcieDeviceInterface, std::move(getPCIeDeviceCallback));
4527e860f15SJohn Edward Broadbent         });
453f5c9f8bdSJason M. Bills }
454f5c9f8bdSJason M. Bills 
455f5c9f8bdSJason M. Bills } // namespace redfish
456