xref: /openbmc/bmcweb/features/redfish/lib/pcie.hpp (revision 973c1355f161c4f71d7c5e3e2016cc86881b73fa)
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                 }
218a9f68bb5STony Lee                 if (!redfishGenerationInUse->empty())
219d1bde9e5SKrzysztof Grobelny                 {
220d1bde9e5SKrzysztof Grobelny                     asyncResp->res.jsonValue["PCIeInterface"]["PCIeType"] =
221d1bde9e5SKrzysztof Grobelny                         *redfishGenerationInUse;
222d1bde9e5SKrzysztof Grobelny                 }
223a9f68bb5STony Lee             }
224d1bde9e5SKrzysztof Grobelny 
225d1bde9e5SKrzysztof Grobelny             if (manufacturer != nullptr)
226d1bde9e5SKrzysztof Grobelny             {
227d1bde9e5SKrzysztof Grobelny                 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
228d1bde9e5SKrzysztof Grobelny             }
229d1bde9e5SKrzysztof Grobelny 
230d1bde9e5SKrzysztof Grobelny             if (deviceType != nullptr)
231d1bde9e5SKrzysztof Grobelny             {
232d1bde9e5SKrzysztof Grobelny                 asyncResp->res.jsonValue["DeviceType"] = *deviceType;
233d1bde9e5SKrzysztof Grobelny             }
234d1bde9e5SKrzysztof Grobelny 
2351476687dSEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
2361476687dSEd Tanous                 "#PCIeDevice.v1_4_0.PCIeDevice";
2371476687dSEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2381476687dSEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + device;
2391476687dSEd Tanous             asyncResp->res.jsonValue["Name"] = "PCIe Device";
2401476687dSEd Tanous             asyncResp->res.jsonValue["Id"] = device;
2411476687dSEd Tanous 
2421476687dSEd Tanous             asyncResp->res.jsonValue["PCIeFunctions"]["@odata.id"] =
24345ca1b86SEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + device +
2441476687dSEd Tanous                 "/PCIeFunctions";
245dede6a98SJason M. Bills         };
246dede6a98SJason M. Bills         std::string escapedPath = std::string(pciePath) + "/" + device;
247dede6a98SJason M. Bills         dbus::utility::escapePathForDbus(escapedPath);
248d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
249d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, pcieService, escapedPath,
250d1bde9e5SKrzysztof Grobelny             pcieDeviceInterface, std::move(getPCIeDeviceCallback));
2517e860f15SJohn Edward Broadbent         });
252dede6a98SJason M. Bills }
253dede6a98SJason M. Bills 
2547e860f15SJohn Edward Broadbent inline void requestRoutesSystemPCIeFunctionCollection(App& app)
2557e860f15SJohn Edward Broadbent {
256dede6a98SJason M. Bills     /**
257dede6a98SJason M. Bills      * Functions triggers appropriate requests on DBus
258dede6a98SJason M. Bills      */
2597e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2607e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/PCIeDevices/<str>/PCIeFunctions/")
261ed398213SEd Tanous         .privileges(redfish::privileges::getPCIeFunctionCollection)
262002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
263002d39b4SEd Tanous             [&app](const crow::Request& req,
2647e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26545ca1b86SEd Tanous                    const std::string& device) {
2663ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
267dede6a98SJason M. Bills         {
26845ca1b86SEd Tanous             return;
26945ca1b86SEd Tanous         }
2701476687dSEd Tanous 
2711476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
2721476687dSEd Tanous             "#PCIeFunctionCollection.PCIeFunctionCollection";
2731476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2741476687dSEd Tanous             "/redfish/v1/Systems/system/PCIeDevices/" + device +
2751476687dSEd Tanous             "/PCIeFunctions";
2761476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "PCIe Function Collection";
2771476687dSEd Tanous         asyncResp->res.jsonValue["Description"] =
2781476687dSEd Tanous             "Collection of PCIe Functions for PCIe Device " + device;
279dede6a98SJason M. Bills 
280b9d36b47SEd Tanous         auto getPCIeDeviceCallback =
28145ca1b86SEd Tanous             [asyncResp, device](
28245ca1b86SEd Tanous                 const boost::system::error_code ec,
28345ca1b86SEd Tanous                 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
2847e860f15SJohn Edward Broadbent             if (ec)
2857e860f15SJohn Edward Broadbent             {
2867e860f15SJohn Edward Broadbent                 BMCWEB_LOG_DEBUG
287002d39b4SEd Tanous                     << "failed to get PCIe Device properties ec: " << ec.value()
288002d39b4SEd Tanous                     << ": " << ec.message();
28945ca1b86SEd Tanous                 if (ec.value() ==
29045ca1b86SEd Tanous                     boost::system::linux_error::bad_request_descriptor)
2917e860f15SJohn Edward Broadbent                 {
292002d39b4SEd Tanous                     messages::resourceNotFound(asyncResp->res, "PCIeDevice",
293002d39b4SEd Tanous                                                device);
2947e860f15SJohn Edward Broadbent                 }
2957e860f15SJohn Edward Broadbent                 else
2967e860f15SJohn Edward Broadbent                 {
2977e860f15SJohn Edward Broadbent                     messages::internalError(asyncResp->res);
2987e860f15SJohn Edward Broadbent                 }
2997e860f15SJohn Edward Broadbent                 return;
3007e860f15SJohn Edward Broadbent             }
3017e860f15SJohn Edward Broadbent 
3027e860f15SJohn Edward Broadbent             nlohmann::json& pcieFunctionList =
3037e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members"];
3047e860f15SJohn Edward Broadbent             pcieFunctionList = nlohmann::json::array();
3057e860f15SJohn Edward Broadbent             static constexpr const int maxPciFunctionNum = 8;
30645ca1b86SEd Tanous             for (int functionNum = 0; functionNum < maxPciFunctionNum;
30745ca1b86SEd Tanous                  functionNum++)
3087e860f15SJohn Edward Broadbent             {
309b9d36b47SEd Tanous                 // Check if this function exists by looking for a
310b9d36b47SEd Tanous                 // device ID
3117e860f15SJohn Edward Broadbent                 std::string devIDProperty =
312002d39b4SEd Tanous                     "Function" + std::to_string(functionNum) + "DeviceId";
313b9d36b47SEd Tanous                 const std::string* property = nullptr;
314b9d36b47SEd Tanous                 for (const auto& propEntry : pcieDevProperties)
3157e860f15SJohn Edward Broadbent                 {
316b9d36b47SEd Tanous                     if (propEntry.first == devIDProperty)
317b9d36b47SEd Tanous                     {
318002d39b4SEd Tanous                         property = std::get_if<std::string>(&propEntry.second);
319b9d36b47SEd Tanous                     }
320b9d36b47SEd Tanous                 }
321fb0ecc3eSNan Zhou                 if (property == nullptr || property->empty())
322b9d36b47SEd Tanous                 {
323e28ce0e6SNan Zhou                     continue;
324b9d36b47SEd Tanous                 }
3251476687dSEd Tanous                 nlohmann::json::object_t pcieFunction;
3261476687dSEd Tanous                 pcieFunction["@odata.id"] =
3271476687dSEd Tanous                     "/redfish/v1/Systems/system/PCIeDevices/" + device +
3281476687dSEd Tanous                     "/PCIeFunctions/" + std::to_string(functionNum);
3291476687dSEd Tanous                 pcieFunctionList.push_back(std::move(pcieFunction));
3307e860f15SJohn Edward Broadbent             }
331a818d15aSJiaqing Zhao             asyncResp->res.jsonValue["Members@odata.count"] =
3327e860f15SJohn Edward Broadbent                 pcieFunctionList.size();
3337e860f15SJohn Edward Broadbent         };
3347e860f15SJohn Edward Broadbent         std::string escapedPath = std::string(pciePath) + "/" + device;
3357e860f15SJohn Edward Broadbent         dbus::utility::escapePathForDbus(escapedPath);
336d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
337d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, pcieService, escapedPath,
338d1bde9e5SKrzysztof Grobelny             pcieDeviceInterface, std::move(getPCIeDeviceCallback));
3397e860f15SJohn Edward Broadbent         });
3407e860f15SJohn Edward Broadbent }
3417e860f15SJohn Edward Broadbent 
3427e860f15SJohn Edward Broadbent inline void requestRoutesSystemPCIeFunction(App& app)
3437e860f15SJohn Edward Broadbent {
3447e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
3457e860f15SJohn Edward Broadbent         app,
3467e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/PCIeDevices/<str>/PCIeFunctions/<str>/")
347ed398213SEd Tanous         .privileges(redfish::privileges::getPCIeFunction)
348002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
349002d39b4SEd Tanous             [&app](const crow::Request& req,
35045ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
351002d39b4SEd Tanous                    const std::string& device, const std::string& function) {
3523ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
35345ca1b86SEd Tanous         {
35445ca1b86SEd Tanous             return;
35545ca1b86SEd Tanous         }
356168e20c1SEd Tanous         auto getPCIeDeviceCallback =
357168e20c1SEd Tanous             [asyncResp, device, function](
3587e860f15SJohn Edward Broadbent                 const boost::system::error_code ec,
359b9d36b47SEd Tanous                 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
360dede6a98SJason M. Bills             if (ec)
361dede6a98SJason M. Bills             {
362dede6a98SJason M. Bills                 BMCWEB_LOG_DEBUG
363002d39b4SEd Tanous                     << "failed to get PCIe Device properties ec: " << ec.value()
364002d39b4SEd Tanous                     << ": " << ec.message();
365dede6a98SJason M. Bills                 if (ec.value() ==
366dede6a98SJason M. Bills                     boost::system::linux_error::bad_request_descriptor)
367dede6a98SJason M. Bills                 {
368002d39b4SEd Tanous                     messages::resourceNotFound(asyncResp->res, "PCIeDevice",
369002d39b4SEd Tanous                                                device);
370dede6a98SJason M. Bills                 }
371dede6a98SJason M. Bills                 else
372dede6a98SJason M. Bills                 {
373dede6a98SJason M. Bills                     messages::internalError(asyncResp->res);
374dede6a98SJason M. Bills                 }
375dede6a98SJason M. Bills                 return;
376dede6a98SJason M. Bills             }
377dede6a98SJason M. Bills 
3781476687dSEd Tanous             // Check if this function exists by looking for a device
3791476687dSEd Tanous             // ID
380b9d36b47SEd Tanous             std::string functionName = "Function" + function;
381b9d36b47SEd Tanous             std::string devIDProperty = functionName + "DeviceId";
382b9d36b47SEd Tanous 
383b9d36b47SEd Tanous             const std::string* devIdProperty = nullptr;
384b9d36b47SEd Tanous             for (const auto& property : pcieDevProperties)
385b9d36b47SEd Tanous             {
386b9d36b47SEd Tanous                 if (property.first == devIDProperty)
387b9d36b47SEd Tanous                 {
388002d39b4SEd Tanous                     devIdProperty = std::get_if<std::string>(&property.second);
389b9d36b47SEd Tanous                     continue;
390b9d36b47SEd Tanous                 }
391b9d36b47SEd Tanous             }
392*973c1355STony Lee             if (devIdProperty == nullptr || devIdProperty->empty())
393f5c9f8bdSJason M. Bills             {
394002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "PCIeFunction",
395002d39b4SEd Tanous                                            function);
396f5c9f8bdSJason M. Bills                 return;
397f5c9f8bdSJason M. Bills             }
398f5c9f8bdSJason M. Bills 
3991476687dSEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
4001476687dSEd Tanous                 "#PCIeFunction.v1_2_0.PCIeFunction";
4011476687dSEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
402168e20c1SEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + device +
4031476687dSEd Tanous                 "/PCIeFunctions/" + function;
4041476687dSEd Tanous             asyncResp->res.jsonValue["Name"] = "PCIe Function";
4051476687dSEd Tanous             asyncResp->res.jsonValue["Id"] = function;
406002d39b4SEd Tanous             asyncResp->res.jsonValue["FunctionId"] = std::stoi(function);
407002d39b4SEd Tanous             asyncResp->res.jsonValue["Links"]["PCIeDevice"]["@odata.id"] =
4081476687dSEd Tanous                 "/redfish/v1/Systems/system/PCIeDevices/" + device;
409f5c9f8bdSJason M. Bills 
410b9d36b47SEd Tanous             for (const auto& property : pcieDevProperties)
411f5c9f8bdSJason M. Bills             {
412b9d36b47SEd Tanous                 const std::string* strProperty =
413b9d36b47SEd Tanous                     std::get_if<std::string>(&property.second);
414b9d36b47SEd Tanous                 if (property.first == functionName + "DeviceId")
415f5c9f8bdSJason M. Bills                 {
416b9d36b47SEd Tanous                     asyncResp->res.jsonValue["DeviceId"] = *strProperty;
417f5c9f8bdSJason M. Bills                 }
418b9d36b47SEd Tanous                 if (property.first == functionName + "VendorId")
419f5c9f8bdSJason M. Bills                 {
420b9d36b47SEd Tanous                     asyncResp->res.jsonValue["VendorId"] = *strProperty;
421f5c9f8bdSJason M. Bills                 }
422b9d36b47SEd Tanous                 if (property.first == functionName + "FunctionType")
423f5c9f8bdSJason M. Bills                 {
424002d39b4SEd Tanous                     asyncResp->res.jsonValue["FunctionType"] = *strProperty;
425f5c9f8bdSJason M. Bills                 }
426b9d36b47SEd Tanous                 if (property.first == functionName + "DeviceClass")
427f5c9f8bdSJason M. Bills                 {
428002d39b4SEd Tanous                     asyncResp->res.jsonValue["DeviceClass"] = *strProperty;
429f5c9f8bdSJason M. Bills                 }
430b9d36b47SEd Tanous                 if (property.first == functionName + "ClassCode")
431f5c9f8bdSJason M. Bills                 {
432002d39b4SEd Tanous                     asyncResp->res.jsonValue["ClassCode"] = *strProperty;
433f5c9f8bdSJason M. Bills                 }
434b9d36b47SEd Tanous                 if (property.first == functionName + "RevisionId")
435f5c9f8bdSJason M. Bills                 {
436002d39b4SEd Tanous                     asyncResp->res.jsonValue["RevisionId"] = *strProperty;
437f5c9f8bdSJason M. Bills                 }
438b9d36b47SEd Tanous                 if (property.first == functionName + "SubsystemId")
439b9d36b47SEd Tanous                 {
440002d39b4SEd Tanous                     asyncResp->res.jsonValue["SubsystemId"] = *strProperty;
441b9d36b47SEd Tanous                 }
442002d39b4SEd Tanous                 if (property.first == functionName + "SubsystemVendorId")
443f5c9f8bdSJason M. Bills                 {
444168e20c1SEd Tanous                     asyncResp->res.jsonValue["SubsystemVendorId"] =
445b9d36b47SEd Tanous                         *strProperty;
446b9d36b47SEd Tanous                 }
447f5c9f8bdSJason M. Bills             }
448f5c9f8bdSJason M. Bills         };
449f5c9f8bdSJason M. Bills         std::string escapedPath = std::string(pciePath) + "/" + device;
450f5c9f8bdSJason M. Bills         dbus::utility::escapePathForDbus(escapedPath);
451d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
452d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, pcieService, escapedPath,
453d1bde9e5SKrzysztof Grobelny             pcieDeviceInterface, std::move(getPCIeDeviceCallback));
4547e860f15SJohn Edward Broadbent         });
455f5c9f8bdSJason M. Bills }
456f5c9f8bdSJason M. Bills 
457f5c9f8bdSJason M. Bills } // namespace redfish
458