xref: /openbmc/bmcweb/features/redfish/lib/pcie.hpp (revision e28ce0e6787bda84a6d8fabee12cbb6cea94018c)
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 
197e860f15SJohn Edward Broadbent #include <app.hpp>
20f5c9f8bdSJason M. Bills #include <boost/system/linux_error.hpp>
21168e20c1SEd Tanous #include <dbus_utility.hpp>
2245ca1b86SEd Tanous #include <query.hpp>
23ed398213SEd Tanous #include <registries/privilege_registry.hpp>
24d1bde9e5SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
25d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
26d1bde9e5SKrzysztof Grobelny #include <utils/dbus_utils.hpp>
27f5c9f8bdSJason M. Bills 
28f5c9f8bdSJason M. Bills namespace redfish
29f5c9f8bdSJason M. Bills {
30f5c9f8bdSJason M. Bills 
31f5c9f8bdSJason M. Bills static constexpr char const* pcieService = "xyz.openbmc_project.PCIe";
32f5c9f8bdSJason M. Bills static constexpr char const* pciePath = "/xyz/openbmc_project/PCIe";
33f5c9f8bdSJason M. Bills static constexpr char const* pcieDeviceInterface =
34f5c9f8bdSJason M. Bills     "xyz.openbmc_project.PCIe.Device";
35f5c9f8bdSJason M. Bills 
36b5a76932SEd Tanous static inline void
378d1b46d7Szhanghch05     getPCIeDeviceList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
38adbe192aSJason M. Bills                       const std::string& name)
39f5c9f8bdSJason M. Bills {
40b9d36b47SEd Tanous     auto getPCIeMapCallback =
41b9d36b47SEd Tanous         [asyncResp, name](const boost::system::error_code ec,
42b9d36b47SEd Tanous                           const dbus::utility::MapperGetSubTreePathsResponse&
43b9d36b47SEd Tanous                               pcieDevicePaths) {
44f5c9f8bdSJason M. Bills         if (ec)
45f5c9f8bdSJason M. Bills         {
46a2730f01SAndrew Geissler             BMCWEB_LOG_DEBUG << "no PCIe device paths found ec: "
47f5c9f8bdSJason M. Bills                              << ec.message();
48a2730f01SAndrew Geissler             // Not an error, system just doesn't have PCIe info
49f5c9f8bdSJason M. Bills             return;
50f5c9f8bdSJason M. Bills         }
51adbe192aSJason M. Bills         nlohmann::json& pcieDeviceList = asyncResp->res.jsonValue[name];
52f5c9f8bdSJason M. Bills         pcieDeviceList = nlohmann::json::array();
53f5c9f8bdSJason M. Bills         for (const std::string& pcieDevicePath : pcieDevicePaths)
54f5c9f8bdSJason M. Bills         {
553174e4dfSEd Tanous             size_t devStart = pcieDevicePath.rfind('/');
56f5c9f8bdSJason M. Bills             if (devStart == std::string::npos)
57f5c9f8bdSJason M. Bills             {
58f5c9f8bdSJason M. Bills                 continue;
59f5c9f8bdSJason M. Bills             }
60f5c9f8bdSJason M. Bills 
61f5c9f8bdSJason M. Bills             std::string devName = pcieDevicePath.substr(devStart + 1);
62f5c9f8bdSJason M. Bills             if (devName.empty())
63f5c9f8bdSJason M. Bills             {
64f5c9f8bdSJason M. Bills                 continue;
65f5c9f8bdSJason M. Bills             }
661476687dSEd Tanous             nlohmann::json::object_t pcieDevice;
671476687dSEd Tanous             pcieDevice["@odata.id"] =
681476687dSEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + devName;
691476687dSEd Tanous             pcieDeviceList.push_back(std::move(pcieDevice));
70f5c9f8bdSJason M. Bills         }
71002d39b4SEd Tanous         asyncResp->res.jsonValue[name + "@odata.count"] = pcieDeviceList.size();
72f5c9f8bdSJason M. Bills     };
73f5c9f8bdSJason M. Bills     crow::connections::systemBus->async_method_call(
74f5c9f8bdSJason M. Bills         std::move(getPCIeMapCallback), "xyz.openbmc_project.ObjectMapper",
75f5c9f8bdSJason M. Bills         "/xyz/openbmc_project/object_mapper",
76f5c9f8bdSJason M. Bills         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
77f5c9f8bdSJason M. Bills         std::string(pciePath) + "/", 1, std::array<std::string, 0>());
78f5c9f8bdSJason M. Bills }
79f5c9f8bdSJason M. Bills 
807e860f15SJohn Edward Broadbent inline void requestRoutesSystemPCIeDeviceCollection(App& app)
81adbe192aSJason M. Bills {
82adbe192aSJason M. Bills     /**
83adbe192aSJason M. Bills      * Functions triggers appropriate requests on DBus
84adbe192aSJason M. Bills      */
8522d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/PCIeDevices/")
86ed398213SEd Tanous         .privileges(redfish::privileges::getPCIeDeviceCollection)
877e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
8845ca1b86SEd Tanous             [&app](const crow::Request& req,
8922d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9022d268cbSEd Tanous                    const std::string& systemName) {
913ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
92adbe192aSJason M. Bills         {
9345ca1b86SEd Tanous             return;
9445ca1b86SEd Tanous         }
9522d268cbSEd Tanous         if (systemName != "system")
9622d268cbSEd Tanous         {
9722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
9822d268cbSEd Tanous                                        systemName);
9922d268cbSEd Tanous             return;
10022d268cbSEd Tanous         }
1011476687dSEd Tanous 
1021476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
1031476687dSEd Tanous             "#PCIeDeviceCollection.PCIeDeviceCollection";
1041476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1051476687dSEd Tanous             "/redfish/v1/Systems/system/PCIeDevices";
1061476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "PCIe Device Collection";
107002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "Collection of PCIe Devices";
1081476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
1091476687dSEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = 0;
110adbe192aSJason M. Bills         getPCIeDeviceList(asyncResp, "Members");
1117e860f15SJohn Edward Broadbent         });
112f5c9f8bdSJason M. Bills }
113f5c9f8bdSJason M. Bills 
11462cd45afSSpencer Ku inline std::optional<std::string>
11562cd45afSSpencer Ku     redfishPcieGenerationFromDbus(const std::string& generationInUse)
11662cd45afSSpencer Ku {
11762cd45afSSpencer Ku     if (generationInUse ==
11862cd45afSSpencer Ku         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen1")
11962cd45afSSpencer Ku     {
12062cd45afSSpencer Ku         return "Gen1";
12162cd45afSSpencer Ku     }
12262cd45afSSpencer Ku     if (generationInUse ==
12362cd45afSSpencer Ku         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen2")
12462cd45afSSpencer Ku     {
12562cd45afSSpencer Ku         return "Gen2";
12662cd45afSSpencer Ku     }
12762cd45afSSpencer Ku     if (generationInUse ==
12862cd45afSSpencer Ku         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen3")
12962cd45afSSpencer Ku     {
13062cd45afSSpencer Ku         return "Gen3";
13162cd45afSSpencer Ku     }
13262cd45afSSpencer Ku     if (generationInUse ==
13362cd45afSSpencer Ku         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen4")
13462cd45afSSpencer Ku     {
13562cd45afSSpencer Ku         return "Gen4";
13662cd45afSSpencer Ku     }
13762cd45afSSpencer Ku     if (generationInUse ==
13862cd45afSSpencer Ku         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen5")
13962cd45afSSpencer Ku     {
14062cd45afSSpencer Ku         return "Gen5";
14162cd45afSSpencer Ku     }
142e825cbc8SEd Tanous     if (generationInUse.empty() ||
143e825cbc8SEd Tanous         generationInUse ==
14462cd45afSSpencer Ku             "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Unknown")
14562cd45afSSpencer Ku     {
14662cd45afSSpencer Ku         return "";
14762cd45afSSpencer Ku     }
14862cd45afSSpencer Ku 
14962cd45afSSpencer Ku     // The value is not unknown or Gen1-5, need return an internal error.
15062cd45afSSpencer Ku     return std::nullopt;
15162cd45afSSpencer Ku }
15262cd45afSSpencer Ku 
1537e860f15SJohn Edward Broadbent inline void requestRoutesSystemPCIeDevice(App& app)
154f5c9f8bdSJason M. Bills {
15522d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/PCIeDevices/<str>/")
156ed398213SEd Tanous         .privileges(redfish::privileges::getPCIeDevice)
157002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
158002d39b4SEd Tanous             [&app](const crow::Request& req,
1597e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16022d268cbSEd Tanous                    const std::string& systemName, const std::string& device) {
1613ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1627e860f15SJohn Edward Broadbent         {
16345ca1b86SEd Tanous             return;
16445ca1b86SEd Tanous         }
16522d268cbSEd Tanous         if (systemName != "system")
16622d268cbSEd Tanous         {
16722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
16822d268cbSEd Tanous                                        systemName);
16922d268cbSEd Tanous             return;
17022d268cbSEd Tanous         }
17122d268cbSEd Tanous 
172168e20c1SEd Tanous         auto getPCIeDeviceCallback =
17345ca1b86SEd Tanous             [asyncResp, device](
17445ca1b86SEd Tanous                 const boost::system::error_code ec,
17545ca1b86SEd Tanous                 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
176f5c9f8bdSJason M. Bills             if (ec)
177f5c9f8bdSJason M. Bills             {
178f5c9f8bdSJason M. Bills                 BMCWEB_LOG_DEBUG
179002d39b4SEd Tanous                     << "failed to get PCIe Device properties ec: " << ec.value()
180002d39b4SEd Tanous                     << ": " << ec.message();
18145ca1b86SEd Tanous                 if (ec.value() ==
18245ca1b86SEd Tanous                     boost::system::linux_error::bad_request_descriptor)
183f5c9f8bdSJason M. Bills                 {
184002d39b4SEd Tanous                     messages::resourceNotFound(asyncResp->res, "PCIeDevice",
185002d39b4SEd Tanous                                                device);
186f5c9f8bdSJason M. Bills                 }
187f5c9f8bdSJason M. Bills                 else
188f5c9f8bdSJason M. Bills                 {
189f5c9f8bdSJason M. Bills                     messages::internalError(asyncResp->res);
190f5c9f8bdSJason M. Bills                 }
191f5c9f8bdSJason M. Bills                 return;
192f5c9f8bdSJason M. Bills             }
193f5c9f8bdSJason M. Bills 
194d1bde9e5SKrzysztof Grobelny             const std::string* manufacturer = nullptr;
195d1bde9e5SKrzysztof Grobelny             const std::string* deviceType = nullptr;
196d1bde9e5SKrzysztof Grobelny             const std::string* generationInUse = nullptr;
197d1bde9e5SKrzysztof Grobelny 
198d1bde9e5SKrzysztof Grobelny             const bool success = sdbusplus::unpackPropertiesNoThrow(
199d1bde9e5SKrzysztof Grobelny                 dbus_utils::UnpackErrorPrinter(), pcieDevProperties,
200d1bde9e5SKrzysztof Grobelny                 "Manufacturer", manufacturer, "DeviceType", deviceType,
201d1bde9e5SKrzysztof Grobelny                 "GenerationInUse", generationInUse);
202d1bde9e5SKrzysztof Grobelny 
203d1bde9e5SKrzysztof Grobelny             if (!success)
204d1bde9e5SKrzysztof Grobelny             {
205d1bde9e5SKrzysztof Grobelny                 messages::internalError(asyncResp->res);
206d1bde9e5SKrzysztof Grobelny                 return;
207d1bde9e5SKrzysztof Grobelny             }
208d1bde9e5SKrzysztof Grobelny 
209d1bde9e5SKrzysztof Grobelny             if (generationInUse != nullptr)
210d1bde9e5SKrzysztof Grobelny             {
211d1bde9e5SKrzysztof Grobelny                 std::optional<std::string> redfishGenerationInUse =
212d1bde9e5SKrzysztof Grobelny                     redfishPcieGenerationFromDbus(*generationInUse);
213d1bde9e5SKrzysztof Grobelny                 if (!redfishGenerationInUse)
214d1bde9e5SKrzysztof Grobelny                 {
215d1bde9e5SKrzysztof Grobelny                     messages::internalError(asyncResp->res);
216d1bde9e5SKrzysztof Grobelny                     return;
217d1bde9e5SKrzysztof Grobelny                 }
218d1bde9e5SKrzysztof Grobelny                 if (redfishGenerationInUse->empty())
219d1bde9e5SKrzysztof Grobelny                 {
220d1bde9e5SKrzysztof Grobelny                     // unknown, no need to handle
221d1bde9e5SKrzysztof Grobelny                     return;
222d1bde9e5SKrzysztof Grobelny                 }
223d1bde9e5SKrzysztof Grobelny                 asyncResp->res.jsonValue["PCIeInterface"]["PCIeType"] =
224d1bde9e5SKrzysztof Grobelny                     *redfishGenerationInUse;
225d1bde9e5SKrzysztof Grobelny             }
226d1bde9e5SKrzysztof Grobelny 
227d1bde9e5SKrzysztof Grobelny             if (manufacturer != nullptr)
228d1bde9e5SKrzysztof Grobelny             {
229d1bde9e5SKrzysztof Grobelny                 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
230d1bde9e5SKrzysztof Grobelny             }
231d1bde9e5SKrzysztof Grobelny 
232d1bde9e5SKrzysztof Grobelny             if (deviceType != nullptr)
233d1bde9e5SKrzysztof Grobelny             {
234d1bde9e5SKrzysztof Grobelny                 asyncResp->res.jsonValue["DeviceType"] = *deviceType;
235d1bde9e5SKrzysztof Grobelny             }
236d1bde9e5SKrzysztof Grobelny 
2371476687dSEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
2381476687dSEd Tanous                 "#PCIeDevice.v1_4_0.PCIeDevice";
2391476687dSEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2401476687dSEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + device;
2411476687dSEd Tanous             asyncResp->res.jsonValue["Name"] = "PCIe Device";
2421476687dSEd Tanous             asyncResp->res.jsonValue["Id"] = device;
2431476687dSEd Tanous 
2441476687dSEd Tanous             asyncResp->res.jsonValue["PCIeFunctions"]["@odata.id"] =
24545ca1b86SEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + device +
2461476687dSEd Tanous                 "/PCIeFunctions";
247dede6a98SJason M. Bills         };
248dede6a98SJason M. Bills         std::string escapedPath = std::string(pciePath) + "/" + device;
249dede6a98SJason M. Bills         dbus::utility::escapePathForDbus(escapedPath);
250d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
251d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, pcieService, escapedPath,
252d1bde9e5SKrzysztof Grobelny             pcieDeviceInterface, std::move(getPCIeDeviceCallback));
2537e860f15SJohn Edward Broadbent         });
254dede6a98SJason M. Bills }
255dede6a98SJason M. Bills 
2567e860f15SJohn Edward Broadbent inline void requestRoutesSystemPCIeFunctionCollection(App& app)
2577e860f15SJohn Edward Broadbent {
258dede6a98SJason M. Bills     /**
259dede6a98SJason M. Bills      * Functions triggers appropriate requests on DBus
260dede6a98SJason M. Bills      */
2617e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2627e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/PCIeDevices/<str>/PCIeFunctions/")
263ed398213SEd Tanous         .privileges(redfish::privileges::getPCIeFunctionCollection)
264002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
265002d39b4SEd Tanous             [&app](const crow::Request& req,
2667e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26745ca1b86SEd Tanous                    const std::string& device) {
2683ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
269dede6a98SJason M. Bills         {
27045ca1b86SEd Tanous             return;
27145ca1b86SEd Tanous         }
2721476687dSEd Tanous 
2731476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
2741476687dSEd Tanous             "#PCIeFunctionCollection.PCIeFunctionCollection";
2751476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2761476687dSEd Tanous             "/redfish/v1/Systems/system/PCIeDevices/" + device +
2771476687dSEd Tanous             "/PCIeFunctions";
2781476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "PCIe Function Collection";
2791476687dSEd Tanous         asyncResp->res.jsonValue["Description"] =
2801476687dSEd Tanous             "Collection of PCIe Functions for PCIe Device " + device;
281dede6a98SJason M. Bills 
282b9d36b47SEd Tanous         auto getPCIeDeviceCallback =
28345ca1b86SEd Tanous             [asyncResp, device](
28445ca1b86SEd Tanous                 const boost::system::error_code ec,
28545ca1b86SEd Tanous                 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
2867e860f15SJohn Edward Broadbent             if (ec)
2877e860f15SJohn Edward Broadbent             {
2887e860f15SJohn Edward Broadbent                 BMCWEB_LOG_DEBUG
289002d39b4SEd Tanous                     << "failed to get PCIe Device properties ec: " << ec.value()
290002d39b4SEd Tanous                     << ": " << ec.message();
29145ca1b86SEd Tanous                 if (ec.value() ==
29245ca1b86SEd Tanous                     boost::system::linux_error::bad_request_descriptor)
2937e860f15SJohn Edward Broadbent                 {
294002d39b4SEd Tanous                     messages::resourceNotFound(asyncResp->res, "PCIeDevice",
295002d39b4SEd Tanous                                                device);
2967e860f15SJohn Edward Broadbent                 }
2977e860f15SJohn Edward Broadbent                 else
2987e860f15SJohn Edward Broadbent                 {
2997e860f15SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
3007e860f15SJohn Edward Broadbent                 }
3017e860f15SJohn Edward Broadbent                 return;
3027e860f15SJohn Edward Broadbent             }
3037e860f15SJohn Edward Broadbent 
3047e860f15SJohn Edward Broadbent             nlohmann::json& pcieFunctionList =
3057e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members"];
3067e860f15SJohn Edward Broadbent             pcieFunctionList = nlohmann::json::array();
3077e860f15SJohn Edward Broadbent             static constexpr const int maxPciFunctionNum = 8;
30845ca1b86SEd Tanous             for (int functionNum = 0; functionNum < maxPciFunctionNum;
30945ca1b86SEd Tanous                  functionNum++)
3107e860f15SJohn Edward Broadbent             {
311b9d36b47SEd Tanous                 // Check if this function exists by looking for a
312b9d36b47SEd Tanous                 // device ID
3137e860f15SJohn Edward Broadbent                 std::string devIDProperty =
314002d39b4SEd Tanous                     "Function" + std::to_string(functionNum) + "DeviceId";
315b9d36b47SEd Tanous                 const std::string* property = nullptr;
316b9d36b47SEd Tanous                 for (const auto& propEntry : pcieDevProperties)
3177e860f15SJohn Edward Broadbent                 {
318b9d36b47SEd Tanous                     if (propEntry.first == devIDProperty)
319b9d36b47SEd Tanous                     {
320002d39b4SEd Tanous                         property = std::get_if<std::string>(&propEntry.second);
321b9d36b47SEd Tanous                     }
322b9d36b47SEd Tanous                 }
323b9d36b47SEd Tanous                 if (property == nullptr || !property->empty())
324b9d36b47SEd Tanous                 {
325*e28ce0e6SNan Zhou                     continue;
326b9d36b47SEd Tanous                 }
3271476687dSEd Tanous                 nlohmann::json::object_t pcieFunction;
3281476687dSEd Tanous                 pcieFunction["@odata.id"] =
3291476687dSEd Tanous                     "/redfish/v1/Systems/system/PCIeDevices/" + device +
3301476687dSEd Tanous                     "/PCIeFunctions/" + std::to_string(functionNum);
3311476687dSEd Tanous                 pcieFunctionList.push_back(std::move(pcieFunction));
3327e860f15SJohn Edward Broadbent             }
333a818d15aSJiaqing Zhao             asyncResp->res.jsonValue["Members@odata.count"] =
3347e860f15SJohn Edward Broadbent                 pcieFunctionList.size();
3357e860f15SJohn Edward Broadbent         };
3367e860f15SJohn Edward Broadbent         std::string escapedPath = std::string(pciePath) + "/" + device;
3377e860f15SJohn Edward Broadbent         dbus::utility::escapePathForDbus(escapedPath);
338d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
339d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, pcieService, escapedPath,
340d1bde9e5SKrzysztof Grobelny             pcieDeviceInterface, std::move(getPCIeDeviceCallback));
3417e860f15SJohn Edward Broadbent         });
3427e860f15SJohn Edward Broadbent }
3437e860f15SJohn Edward Broadbent 
3447e860f15SJohn Edward Broadbent inline void requestRoutesSystemPCIeFunction(App& app)
3457e860f15SJohn Edward Broadbent {
3467e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
3477e860f15SJohn Edward Broadbent         app,
3487e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/PCIeDevices/<str>/PCIeFunctions/<str>/")
349ed398213SEd Tanous         .privileges(redfish::privileges::getPCIeFunction)
350002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
351002d39b4SEd Tanous             [&app](const crow::Request& req,
35245ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
353002d39b4SEd Tanous                    const std::string& device, const std::string& function) {
3543ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
35545ca1b86SEd Tanous         {
35645ca1b86SEd Tanous             return;
35745ca1b86SEd Tanous         }
358168e20c1SEd Tanous         auto getPCIeDeviceCallback =
359168e20c1SEd Tanous             [asyncResp, device, function](
3607e860f15SJohn Edward Broadbent                 const boost::system::error_code ec,
361b9d36b47SEd Tanous                 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
362dede6a98SJason M. Bills             if (ec)
363dede6a98SJason M. Bills             {
364dede6a98SJason M. Bills                 BMCWEB_LOG_DEBUG
365002d39b4SEd Tanous                     << "failed to get PCIe Device properties ec: " << ec.value()
366002d39b4SEd Tanous                     << ": " << ec.message();
367dede6a98SJason M. Bills                 if (ec.value() ==
368dede6a98SJason M. Bills                     boost::system::linux_error::bad_request_descriptor)
369dede6a98SJason M. Bills                 {
370002d39b4SEd Tanous                     messages::resourceNotFound(asyncResp->res, "PCIeDevice",
371002d39b4SEd Tanous                                                device);
372dede6a98SJason M. Bills                 }
373dede6a98SJason M. Bills                 else
374dede6a98SJason M. Bills                 {
375dede6a98SJason M. Bills                     messages::internalError(asyncResp->res);
376dede6a98SJason M. Bills                 }
377dede6a98SJason M. Bills                 return;
378dede6a98SJason M. Bills             }
379dede6a98SJason M. Bills 
3801476687dSEd Tanous             // Check if this function exists by looking for a device
3811476687dSEd Tanous             // ID
382b9d36b47SEd Tanous             std::string functionName = "Function" + function;
383b9d36b47SEd Tanous             std::string devIDProperty = functionName + "DeviceId";
384b9d36b47SEd Tanous 
385b9d36b47SEd Tanous             const std::string* devIdProperty = nullptr;
386b9d36b47SEd Tanous             for (const auto& property : pcieDevProperties)
387b9d36b47SEd Tanous             {
388b9d36b47SEd Tanous                 if (property.first == devIDProperty)
389b9d36b47SEd Tanous                 {
390002d39b4SEd Tanous                     devIdProperty = std::get_if<std::string>(&property.second);
391b9d36b47SEd Tanous                     continue;
392b9d36b47SEd Tanous                 }
393b9d36b47SEd Tanous             }
394b9d36b47SEd Tanous             if (devIdProperty == nullptr || !devIdProperty->empty())
395f5c9f8bdSJason M. Bills             {
396002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "PCIeFunction",
397002d39b4SEd Tanous                                            function);
398f5c9f8bdSJason M. Bills                 return;
399f5c9f8bdSJason M. Bills             }
400f5c9f8bdSJason M. Bills 
4011476687dSEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
4021476687dSEd Tanous                 "#PCIeFunction.v1_2_0.PCIeFunction";
4031476687dSEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
404168e20c1SEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + device +
4051476687dSEd Tanous                 "/PCIeFunctions/" + function;
4061476687dSEd Tanous             asyncResp->res.jsonValue["Name"] = "PCIe Function";
4071476687dSEd Tanous             asyncResp->res.jsonValue["Id"] = function;
408002d39b4SEd Tanous             asyncResp->res.jsonValue["FunctionId"] = std::stoi(function);
409002d39b4SEd Tanous             asyncResp->res.jsonValue["Links"]["PCIeDevice"]["@odata.id"] =
4101476687dSEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + device;
411f5c9f8bdSJason M. Bills 
412b9d36b47SEd Tanous             for (const auto& property : pcieDevProperties)
413f5c9f8bdSJason M. Bills             {
414b9d36b47SEd Tanous                 const std::string* strProperty =
415b9d36b47SEd Tanous                     std::get_if<std::string>(&property.second);
416b9d36b47SEd Tanous                 if (property.first == functionName + "DeviceId")
417f5c9f8bdSJason M. Bills                 {
418b9d36b47SEd Tanous                     asyncResp->res.jsonValue["DeviceId"] = *strProperty;
419f5c9f8bdSJason M. Bills                 }
420b9d36b47SEd Tanous                 if (property.first == functionName + "VendorId")
421f5c9f8bdSJason M. Bills                 {
422b9d36b47SEd Tanous                     asyncResp->res.jsonValue["VendorId"] = *strProperty;
423f5c9f8bdSJason M. Bills                 }
424b9d36b47SEd Tanous                 if (property.first == functionName + "FunctionType")
425f5c9f8bdSJason M. Bills                 {
426002d39b4SEd Tanous                     asyncResp->res.jsonValue["FunctionType"] = *strProperty;
427f5c9f8bdSJason M. Bills                 }
428b9d36b47SEd Tanous                 if (property.first == functionName + "DeviceClass")
429f5c9f8bdSJason M. Bills                 {
430002d39b4SEd Tanous                     asyncResp->res.jsonValue["DeviceClass"] = *strProperty;
431f5c9f8bdSJason M. Bills                 }
432b9d36b47SEd Tanous                 if (property.first == functionName + "ClassCode")
433f5c9f8bdSJason M. Bills                 {
434002d39b4SEd Tanous                     asyncResp->res.jsonValue["ClassCode"] = *strProperty;
435f5c9f8bdSJason M. Bills                 }
436b9d36b47SEd Tanous                 if (property.first == functionName + "RevisionId")
437f5c9f8bdSJason M. Bills                 {
438002d39b4SEd Tanous                     asyncResp->res.jsonValue["RevisionId"] = *strProperty;
439f5c9f8bdSJason M. Bills                 }
440b9d36b47SEd Tanous                 if (property.first == functionName + "SubsystemId")
441b9d36b47SEd Tanous                 {
442002d39b4SEd Tanous                     asyncResp->res.jsonValue["SubsystemId"] = *strProperty;
443b9d36b47SEd Tanous                 }
444002d39b4SEd Tanous                 if (property.first == functionName + "SubsystemVendorId")
445f5c9f8bdSJason M. Bills                 {
446168e20c1SEd Tanous                     asyncResp->res.jsonValue["SubsystemVendorId"] =
447b9d36b47SEd Tanous                         *strProperty;
448b9d36b47SEd Tanous                 }
449f5c9f8bdSJason M. Bills             }
450f5c9f8bdSJason M. Bills         };
451f5c9f8bdSJason M. Bills         std::string escapedPath = std::string(pciePath) + "/" + device;
452f5c9f8bdSJason M. Bills         dbus::utility::escapePathForDbus(escapedPath);
453d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
454d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, pcieService, escapedPath,
455d1bde9e5SKrzysztof Grobelny             pcieDeviceInterface, std::move(getPCIeDeviceCallback));
4567e860f15SJohn Edward Broadbent         });
457f5c9f8bdSJason M. Bills }
458f5c9f8bdSJason M. Bills 
459f5c9f8bdSJason M. Bills } // namespace redfish
460