xref: /openbmc/bmcweb/features/redfish/lib/pcie.hpp (revision 703f67418ae1915e49ac32d475de872da3ab2819)
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 
193ccb3adbSEd Tanous #include "app.hpp"
207a1dbc48SGeorge Liu #include "dbus_utility.hpp"
210ec8b83dSEd Tanous #include "generated/enums/pcie_device.hpp"
223ccb3adbSEd Tanous #include "query.hpp"
233ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
243ccb3adbSEd 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;
195*703f6741SMyung Bae             const size_t* lanesInUse = nullptr;
196d1bde9e5SKrzysztof Grobelny 
197d1bde9e5SKrzysztof Grobelny             const bool success = sdbusplus::unpackPropertiesNoThrow(
198d1bde9e5SKrzysztof Grobelny                 dbus_utils::UnpackErrorPrinter(), pcieDevProperties,
199d1bde9e5SKrzysztof Grobelny                 "Manufacturer", manufacturer, "DeviceType", deviceType,
200*703f6741SMyung Bae                 "LanesInUse", lanesInUse, "GenerationInUse", generationInUse);
201d1bde9e5SKrzysztof Grobelny 
202d1bde9e5SKrzysztof Grobelny             if (!success)
203d1bde9e5SKrzysztof Grobelny             {
204d1bde9e5SKrzysztof Grobelny                 messages::internalError(asyncResp->res);
205d1bde9e5SKrzysztof Grobelny                 return;
206d1bde9e5SKrzysztof Grobelny             }
207d1bde9e5SKrzysztof Grobelny 
208*703f6741SMyung Bae             // The default value of LanesInUse is 0, and the field will be
209*703f6741SMyung Bae             // left as off if it is a default value.
210*703f6741SMyung Bae             if (lanesInUse != nullptr && *lanesInUse != 0)
211*703f6741SMyung Bae             {
212*703f6741SMyung Bae                 asyncResp->res.jsonValue["PCIeInterface"]["LanesInUse"] =
213*703f6741SMyung Bae                     *lanesInUse;
214*703f6741SMyung Bae             }
215*703f6741SMyung Bae 
216d1bde9e5SKrzysztof Grobelny             if (generationInUse != nullptr)
217d1bde9e5SKrzysztof Grobelny             {
2180ec8b83dSEd Tanous                 std::optional<pcie_device::PCIeTypes> redfishGenerationInUse =
219d1bde9e5SKrzysztof Grobelny                     redfishPcieGenerationFromDbus(*generationInUse);
220d1bde9e5SKrzysztof Grobelny                 if (!redfishGenerationInUse)
221d1bde9e5SKrzysztof Grobelny                 {
222d1bde9e5SKrzysztof Grobelny                     messages::internalError(asyncResp->res);
223d1bde9e5SKrzysztof Grobelny                     return;
224d1bde9e5SKrzysztof Grobelny                 }
2250ec8b83dSEd Tanous                 if (*redfishGenerationInUse != pcie_device::PCIeTypes::Invalid)
226d1bde9e5SKrzysztof Grobelny                 {
227d1bde9e5SKrzysztof Grobelny                     asyncResp->res.jsonValue["PCIeInterface"]["PCIeType"] =
228d1bde9e5SKrzysztof Grobelny                         *redfishGenerationInUse;
229d1bde9e5SKrzysztof Grobelny                 }
230a9f68bb5STony Lee             }
231d1bde9e5SKrzysztof Grobelny 
232d1bde9e5SKrzysztof Grobelny             if (manufacturer != nullptr)
233d1bde9e5SKrzysztof Grobelny             {
234d1bde9e5SKrzysztof Grobelny                 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
235d1bde9e5SKrzysztof Grobelny             }
236d1bde9e5SKrzysztof Grobelny 
237d1bde9e5SKrzysztof Grobelny             if (deviceType != nullptr)
238d1bde9e5SKrzysztof Grobelny             {
239d1bde9e5SKrzysztof Grobelny                 asyncResp->res.jsonValue["DeviceType"] = *deviceType;
240d1bde9e5SKrzysztof Grobelny             }
241d1bde9e5SKrzysztof Grobelny 
2421476687dSEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
2431476687dSEd Tanous                 "#PCIeDevice.v1_4_0.PCIeDevice";
2441476687dSEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2451476687dSEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + device;
2461476687dSEd Tanous             asyncResp->res.jsonValue["Name"] = "PCIe Device";
2471476687dSEd Tanous             asyncResp->res.jsonValue["Id"] = device;
2481476687dSEd Tanous 
2491476687dSEd Tanous             asyncResp->res.jsonValue["PCIeFunctions"]["@odata.id"] =
25045ca1b86SEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + device +
2511476687dSEd Tanous                 "/PCIeFunctions";
252dede6a98SJason M. Bills         };
253dede6a98SJason M. Bills         std::string escapedPath = std::string(pciePath) + "/" + device;
254dede6a98SJason M. Bills         dbus::utility::escapePathForDbus(escapedPath);
255d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
256d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, pcieService, escapedPath,
257d1bde9e5SKrzysztof Grobelny             pcieDeviceInterface, std::move(getPCIeDeviceCallback));
2587e860f15SJohn Edward Broadbent         });
259dede6a98SJason M. Bills }
260dede6a98SJason M. Bills 
2617e860f15SJohn Edward Broadbent inline void requestRoutesSystemPCIeFunctionCollection(App& app)
2627e860f15SJohn Edward Broadbent {
263dede6a98SJason M. Bills     /**
264dede6a98SJason M. Bills      * Functions triggers appropriate requests on DBus
265dede6a98SJason M. Bills      */
2667e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2677e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/PCIeDevices/<str>/PCIeFunctions/")
268ed398213SEd Tanous         .privileges(redfish::privileges::getPCIeFunctionCollection)
269002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
270002d39b4SEd Tanous             [&app](const crow::Request& req,
2717e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
27245ca1b86SEd Tanous                    const std::string& device) {
2733ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
274dede6a98SJason M. Bills         {
27545ca1b86SEd Tanous             return;
27645ca1b86SEd Tanous         }
2771476687dSEd Tanous 
2781476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
2791476687dSEd Tanous             "#PCIeFunctionCollection.PCIeFunctionCollection";
2801476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2811476687dSEd Tanous             "/redfish/v1/Systems/system/PCIeDevices/" + device +
2821476687dSEd Tanous             "/PCIeFunctions";
2831476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "PCIe Function Collection";
2841476687dSEd Tanous         asyncResp->res.jsonValue["Description"] =
2851476687dSEd Tanous             "Collection of PCIe Functions for PCIe Device " + device;
286dede6a98SJason M. Bills 
287b9d36b47SEd Tanous         auto getPCIeDeviceCallback =
28845ca1b86SEd Tanous             [asyncResp, device](
28945ca1b86SEd Tanous                 const boost::system::error_code ec,
29045ca1b86SEd Tanous                 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
2917e860f15SJohn Edward Broadbent             if (ec)
2927e860f15SJohn Edward Broadbent             {
2937e860f15SJohn Edward Broadbent                 BMCWEB_LOG_DEBUG
294002d39b4SEd Tanous                     << "failed to get PCIe Device properties ec: " << ec.value()
295002d39b4SEd Tanous                     << ": " << ec.message();
29645ca1b86SEd Tanous                 if (ec.value() ==
29745ca1b86SEd Tanous                     boost::system::linux_error::bad_request_descriptor)
2987e860f15SJohn Edward Broadbent                 {
299002d39b4SEd Tanous                     messages::resourceNotFound(asyncResp->res, "PCIeDevice",
300002d39b4SEd Tanous                                                device);
3017e860f15SJohn Edward Broadbent                 }
3027e860f15SJohn Edward Broadbent                 else
3037e860f15SJohn Edward Broadbent                 {
3047e860f15SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
3057e860f15SJohn Edward Broadbent                 }
3067e860f15SJohn Edward Broadbent                 return;
3077e860f15SJohn Edward Broadbent             }
3087e860f15SJohn Edward Broadbent 
3097e860f15SJohn Edward Broadbent             nlohmann::json& pcieFunctionList =
3107e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members"];
3117e860f15SJohn Edward Broadbent             pcieFunctionList = nlohmann::json::array();
3127e860f15SJohn Edward Broadbent             static constexpr const int maxPciFunctionNum = 8;
31345ca1b86SEd Tanous             for (int functionNum = 0; functionNum < maxPciFunctionNum;
31445ca1b86SEd Tanous                  functionNum++)
3157e860f15SJohn Edward Broadbent             {
316b9d36b47SEd Tanous                 // Check if this function exists by looking for a
317b9d36b47SEd Tanous                 // device ID
3187e860f15SJohn Edward Broadbent                 std::string devIDProperty =
319002d39b4SEd Tanous                     "Function" + std::to_string(functionNum) + "DeviceId";
320b9d36b47SEd Tanous                 const std::string* property = nullptr;
321b9d36b47SEd Tanous                 for (const auto& propEntry : pcieDevProperties)
3227e860f15SJohn Edward Broadbent                 {
323b9d36b47SEd Tanous                     if (propEntry.first == devIDProperty)
324b9d36b47SEd Tanous                     {
325002d39b4SEd Tanous                         property = std::get_if<std::string>(&propEntry.second);
326b9d36b47SEd Tanous                     }
327b9d36b47SEd Tanous                 }
328fb0ecc3eSNan Zhou                 if (property == nullptr || property->empty())
329b9d36b47SEd Tanous                 {
330e28ce0e6SNan Zhou                     continue;
331b9d36b47SEd Tanous                 }
3321476687dSEd Tanous                 nlohmann::json::object_t pcieFunction;
3331476687dSEd Tanous                 pcieFunction["@odata.id"] =
3341476687dSEd Tanous                     "/redfish/v1/Systems/system/PCIeDevices/" + device +
3351476687dSEd Tanous                     "/PCIeFunctions/" + std::to_string(functionNum);
3361476687dSEd Tanous                 pcieFunctionList.push_back(std::move(pcieFunction));
3377e860f15SJohn Edward Broadbent             }
338a818d15aSJiaqing Zhao             asyncResp->res.jsonValue["Members@odata.count"] =
3397e860f15SJohn Edward Broadbent                 pcieFunctionList.size();
3407e860f15SJohn Edward Broadbent         };
3417e860f15SJohn Edward Broadbent         std::string escapedPath = std::string(pciePath) + "/" + device;
3427e860f15SJohn Edward Broadbent         dbus::utility::escapePathForDbus(escapedPath);
343d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
344d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, pcieService, escapedPath,
345d1bde9e5SKrzysztof Grobelny             pcieDeviceInterface, std::move(getPCIeDeviceCallback));
3467e860f15SJohn Edward Broadbent         });
3477e860f15SJohn Edward Broadbent }
3487e860f15SJohn Edward Broadbent 
3497e860f15SJohn Edward Broadbent inline void requestRoutesSystemPCIeFunction(App& app)
3507e860f15SJohn Edward Broadbent {
3517e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
3527e860f15SJohn Edward Broadbent         app,
3537e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/PCIeDevices/<str>/PCIeFunctions/<str>/")
354ed398213SEd Tanous         .privileges(redfish::privileges::getPCIeFunction)
355002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
356002d39b4SEd Tanous             [&app](const crow::Request& req,
35745ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
358002d39b4SEd Tanous                    const std::string& device, const std::string& function) {
3593ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
36045ca1b86SEd Tanous         {
36145ca1b86SEd Tanous             return;
36245ca1b86SEd Tanous         }
363168e20c1SEd Tanous         auto getPCIeDeviceCallback =
364168e20c1SEd Tanous             [asyncResp, device, function](
3657e860f15SJohn Edward Broadbent                 const boost::system::error_code ec,
366b9d36b47SEd Tanous                 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
367dede6a98SJason M. Bills             if (ec)
368dede6a98SJason M. Bills             {
369dede6a98SJason M. Bills                 BMCWEB_LOG_DEBUG
370002d39b4SEd Tanous                     << "failed to get PCIe Device properties ec: " << ec.value()
371002d39b4SEd Tanous                     << ": " << ec.message();
372dede6a98SJason M. Bills                 if (ec.value() ==
373dede6a98SJason M. Bills                     boost::system::linux_error::bad_request_descriptor)
374dede6a98SJason M. Bills                 {
375002d39b4SEd Tanous                     messages::resourceNotFound(asyncResp->res, "PCIeDevice",
376002d39b4SEd Tanous                                                device);
377dede6a98SJason M. Bills                 }
378dede6a98SJason M. Bills                 else
379dede6a98SJason M. Bills                 {
380dede6a98SJason M. Bills                     messages::internalError(asyncResp->res);
381dede6a98SJason M. Bills                 }
382dede6a98SJason M. Bills                 return;
383dede6a98SJason M. Bills             }
384dede6a98SJason M. Bills 
3851476687dSEd Tanous             // Check if this function exists by looking for a device
3861476687dSEd Tanous             // ID
387b9d36b47SEd Tanous             std::string functionName = "Function" + function;
388b9d36b47SEd Tanous             std::string devIDProperty = functionName + "DeviceId";
389b9d36b47SEd Tanous 
390b9d36b47SEd Tanous             const std::string* devIdProperty = nullptr;
391b9d36b47SEd Tanous             for (const auto& property : pcieDevProperties)
392b9d36b47SEd Tanous             {
393b9d36b47SEd Tanous                 if (property.first == devIDProperty)
394b9d36b47SEd Tanous                 {
395002d39b4SEd Tanous                     devIdProperty = std::get_if<std::string>(&property.second);
396b9d36b47SEd Tanous                     continue;
397b9d36b47SEd Tanous                 }
398b9d36b47SEd Tanous             }
399973c1355STony Lee             if (devIdProperty == nullptr || devIdProperty->empty())
400f5c9f8bdSJason M. Bills             {
401002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "PCIeFunction",
402002d39b4SEd Tanous                                            function);
403f5c9f8bdSJason M. Bills                 return;
404f5c9f8bdSJason M. Bills             }
405f5c9f8bdSJason M. Bills 
4061476687dSEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
4071476687dSEd Tanous                 "#PCIeFunction.v1_2_0.PCIeFunction";
4081476687dSEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
409168e20c1SEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + device +
4101476687dSEd Tanous                 "/PCIeFunctions/" + function;
4111476687dSEd Tanous             asyncResp->res.jsonValue["Name"] = "PCIe Function";
4121476687dSEd Tanous             asyncResp->res.jsonValue["Id"] = function;
413002d39b4SEd Tanous             asyncResp->res.jsonValue["FunctionId"] = std::stoi(function);
414002d39b4SEd Tanous             asyncResp->res.jsonValue["Links"]["PCIeDevice"]["@odata.id"] =
4151476687dSEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + device;
416f5c9f8bdSJason M. Bills 
417b9d36b47SEd Tanous             for (const auto& property : pcieDevProperties)
418f5c9f8bdSJason M. Bills             {
419b9d36b47SEd Tanous                 const std::string* strProperty =
420b9d36b47SEd Tanous                     std::get_if<std::string>(&property.second);
421b9d36b47SEd Tanous                 if (property.first == functionName + "DeviceId")
422f5c9f8bdSJason M. Bills                 {
423b9d36b47SEd Tanous                     asyncResp->res.jsonValue["DeviceId"] = *strProperty;
424f5c9f8bdSJason M. Bills                 }
425b9d36b47SEd Tanous                 if (property.first == functionName + "VendorId")
426f5c9f8bdSJason M. Bills                 {
427b9d36b47SEd Tanous                     asyncResp->res.jsonValue["VendorId"] = *strProperty;
428f5c9f8bdSJason M. Bills                 }
429b9d36b47SEd Tanous                 if (property.first == functionName + "FunctionType")
430f5c9f8bdSJason M. Bills                 {
431002d39b4SEd Tanous                     asyncResp->res.jsonValue["FunctionType"] = *strProperty;
432f5c9f8bdSJason M. Bills                 }
433b9d36b47SEd Tanous                 if (property.first == functionName + "DeviceClass")
434f5c9f8bdSJason M. Bills                 {
435002d39b4SEd Tanous                     asyncResp->res.jsonValue["DeviceClass"] = *strProperty;
436f5c9f8bdSJason M. Bills                 }
437b9d36b47SEd Tanous                 if (property.first == functionName + "ClassCode")
438f5c9f8bdSJason M. Bills                 {
439002d39b4SEd Tanous                     asyncResp->res.jsonValue["ClassCode"] = *strProperty;
440f5c9f8bdSJason M. Bills                 }
441b9d36b47SEd Tanous                 if (property.first == functionName + "RevisionId")
442f5c9f8bdSJason M. Bills                 {
443002d39b4SEd Tanous                     asyncResp->res.jsonValue["RevisionId"] = *strProperty;
444f5c9f8bdSJason M. Bills                 }
445b9d36b47SEd Tanous                 if (property.first == functionName + "SubsystemId")
446b9d36b47SEd Tanous                 {
447002d39b4SEd Tanous                     asyncResp->res.jsonValue["SubsystemId"] = *strProperty;
448b9d36b47SEd Tanous                 }
449002d39b4SEd Tanous                 if (property.first == functionName + "SubsystemVendorId")
450f5c9f8bdSJason M. Bills                 {
451168e20c1SEd Tanous                     asyncResp->res.jsonValue["SubsystemVendorId"] =
452b9d36b47SEd Tanous                         *strProperty;
453b9d36b47SEd Tanous                 }
454f5c9f8bdSJason M. Bills             }
455f5c9f8bdSJason M. Bills         };
456f5c9f8bdSJason M. Bills         std::string escapedPath = std::string(pciePath) + "/" + device;
457f5c9f8bdSJason M. Bills         dbus::utility::escapePathForDbus(escapedPath);
458d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
459d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, pcieService, escapedPath,
460d1bde9e5SKrzysztof Grobelny             pcieDeviceInterface, std::move(getPCIeDeviceCallback));
4617e860f15SJohn Edward Broadbent         });
462f5c9f8bdSJason M. Bills }
463f5c9f8bdSJason M. Bills 
464f5c9f8bdSJason M. Bills } // namespace redfish
465