xref: /openbmc/bmcweb/features/redfish/lib/pcie.hpp (revision ac106bf6d10841a25088ed14105a73636ba71519)
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"
24b38fa2abSLakshmi Yadlapati #include "utils/collection.hpp"
253ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
260ec8b83dSEd Tanous 
27f5c9f8bdSJason M. Bills #include <boost/system/linux_error.hpp>
28ef4c65b7SEd Tanous #include <boost/url/format.hpp>
29d1bde9e5SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
30d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
31f5c9f8bdSJason M. Bills 
32f5c9f8bdSJason M. Bills namespace redfish
33f5c9f8bdSJason M. Bills {
34f5c9f8bdSJason M. Bills 
3589492a15SPatrick Williams static constexpr const char* inventoryPath = "/xyz/openbmc_project/inventory";
3694c3a10bSLakshmi Yadlapati static constexpr std::array<std::string_view, 1> pcieDeviceInterface = {
3794c3a10bSLakshmi Yadlapati     "xyz.openbmc_project.Inventory.Item.PCIeDevice"};
38f5c9f8bdSJason M. Bills 
39543f9a75SLakshmi Yadlapati static inline void handlePCIeDevicePath(
40543f9a75SLakshmi Yadlapati     const std::string& pcieDeviceId,
41*ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
42543f9a75SLakshmi Yadlapati     const dbus::utility::MapperGetSubTreePathsResponse& pcieDevicePaths,
43543f9a75SLakshmi Yadlapati     const std::function<void(const std::string& pcieDevicePath,
44543f9a75SLakshmi Yadlapati                              const std::string& service)>& callback)
45543f9a75SLakshmi Yadlapati 
46543f9a75SLakshmi Yadlapati {
47543f9a75SLakshmi Yadlapati     for (const std::string& pcieDevicePath : pcieDevicePaths)
48543f9a75SLakshmi Yadlapati     {
49543f9a75SLakshmi Yadlapati         std::string pciecDeviceName =
50543f9a75SLakshmi Yadlapati             sdbusplus::message::object_path(pcieDevicePath).filename();
51543f9a75SLakshmi Yadlapati         if (pciecDeviceName.empty() || pciecDeviceName != pcieDeviceId)
52543f9a75SLakshmi Yadlapati         {
53543f9a75SLakshmi Yadlapati             continue;
54543f9a75SLakshmi Yadlapati         }
55543f9a75SLakshmi Yadlapati 
56543f9a75SLakshmi Yadlapati         dbus::utility::getDbusObject(
57543f9a75SLakshmi Yadlapati             pcieDevicePath, {},
58*ac106bf6SEd Tanous             [pcieDevicePath, asyncResp,
59543f9a75SLakshmi Yadlapati              callback](const boost::system::error_code& ec,
60543f9a75SLakshmi Yadlapati                        const dbus::utility::MapperGetObject& object) {
61543f9a75SLakshmi Yadlapati             if (ec || object.empty())
62543f9a75SLakshmi Yadlapati             {
63543f9a75SLakshmi Yadlapati                 BMCWEB_LOG_ERROR << "DBUS response error " << ec;
64*ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
65543f9a75SLakshmi Yadlapati                 return;
66543f9a75SLakshmi Yadlapati             }
67543f9a75SLakshmi Yadlapati             callback(pcieDevicePath, object.begin()->first);
68543f9a75SLakshmi Yadlapati             });
69543f9a75SLakshmi Yadlapati         return;
70543f9a75SLakshmi Yadlapati     }
71543f9a75SLakshmi Yadlapati 
72543f9a75SLakshmi Yadlapati     BMCWEB_LOG_WARNING << "PCIe Device not found";
73*ac106bf6SEd Tanous     messages::resourceNotFound(asyncResp->res, "PCIeDevice", pcieDeviceId);
74543f9a75SLakshmi Yadlapati }
75543f9a75SLakshmi Yadlapati 
76543f9a75SLakshmi Yadlapati static inline void getValidPCIeDevicePath(
77543f9a75SLakshmi Yadlapati     const std::string& pcieDeviceId,
78*ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
79543f9a75SLakshmi Yadlapati     const std::function<void(const std::string& pcieDevicePath,
80543f9a75SLakshmi Yadlapati                              const std::string& service)>& callback)
81543f9a75SLakshmi Yadlapati {
82543f9a75SLakshmi Yadlapati     dbus::utility::getSubTreePaths(
8394c3a10bSLakshmi Yadlapati         inventoryPath, 0, pcieDeviceInterface,
84*ac106bf6SEd Tanous         [pcieDeviceId, asyncResp,
85543f9a75SLakshmi Yadlapati          callback](const boost::system::error_code& ec,
86543f9a75SLakshmi Yadlapati                    const dbus::utility::MapperGetSubTreePathsResponse&
87543f9a75SLakshmi Yadlapati                        pcieDevicePaths) {
88543f9a75SLakshmi Yadlapati         if (ec)
89543f9a75SLakshmi Yadlapati         {
90543f9a75SLakshmi Yadlapati             BMCWEB_LOG_ERROR << "D-Bus response error on GetSubTree " << ec;
91*ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
92543f9a75SLakshmi Yadlapati             return;
93543f9a75SLakshmi Yadlapati         }
94*ac106bf6SEd Tanous         handlePCIeDevicePath(pcieDeviceId, asyncResp, pcieDevicePaths,
95*ac106bf6SEd Tanous                              callback);
96543f9a75SLakshmi Yadlapati         return;
97543f9a75SLakshmi Yadlapati         });
98543f9a75SLakshmi Yadlapati }
99543f9a75SLakshmi Yadlapati 
100b38fa2abSLakshmi Yadlapati static inline void handlePCIeDeviceCollectionGet(
101b38fa2abSLakshmi Yadlapati     crow::App& app, const crow::Request& req,
102*ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
103b38fa2abSLakshmi Yadlapati     const std::string& systemName)
104b38fa2abSLakshmi Yadlapati {
105*ac106bf6SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
106b38fa2abSLakshmi Yadlapati     {
107b38fa2abSLakshmi Yadlapati         return;
108b38fa2abSLakshmi Yadlapati     }
109b38fa2abSLakshmi Yadlapati     if (systemName != "system")
110b38fa2abSLakshmi Yadlapati     {
111*ac106bf6SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
112*ac106bf6SEd Tanous                                    systemName);
113b38fa2abSLakshmi Yadlapati         return;
114b38fa2abSLakshmi Yadlapati     }
115543f9a75SLakshmi Yadlapati 
116*ac106bf6SEd Tanous     asyncResp->res.addHeader(boost::beast::http::field::link,
117b38fa2abSLakshmi Yadlapati                              "</redfish/v1/JsonSchemas/PCIeDeviceCollection/"
118b38fa2abSLakshmi Yadlapati                              "PCIeDeviceCollection.json>; rel=describedby");
119*ac106bf6SEd Tanous     asyncResp->res.jsonValue["@odata.type"] =
120b38fa2abSLakshmi Yadlapati         "#PCIeDeviceCollection.PCIeDeviceCollection";
121*ac106bf6SEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
122b38fa2abSLakshmi Yadlapati         "/redfish/v1/Systems/system/PCIeDevices";
123*ac106bf6SEd Tanous     asyncResp->res.jsonValue["Name"] = "PCIe Device Collection";
124*ac106bf6SEd Tanous     asyncResp->res.jsonValue["Description"] = "Collection of PCIe Devices";
125*ac106bf6SEd Tanous     asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
126*ac106bf6SEd Tanous     asyncResp->res.jsonValue["Members@odata.count"] = 0;
127b38fa2abSLakshmi Yadlapati 
128b38fa2abSLakshmi Yadlapati     collection_util::getCollectionMembers(
129*ac106bf6SEd Tanous         asyncResp, boost::urls::url("/redfish/v1/Systems/system/PCIeDevices"),
13094c3a10bSLakshmi Yadlapati         pcieDeviceInterface);
131b38fa2abSLakshmi Yadlapati }
132b38fa2abSLakshmi Yadlapati 
1337e860f15SJohn Edward Broadbent inline void requestRoutesSystemPCIeDeviceCollection(App& app)
134adbe192aSJason M. Bills {
135adbe192aSJason M. Bills     /**
136adbe192aSJason M. Bills      * Functions triggers appropriate requests on DBus
137adbe192aSJason M. Bills      */
13822d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/PCIeDevices/")
139ed398213SEd Tanous         .privileges(redfish::privileges::getPCIeDeviceCollection)
1407e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
141b38fa2abSLakshmi Yadlapati             std::bind_front(handlePCIeDeviceCollectionGet, std::ref(app)));
142f5c9f8bdSJason M. Bills }
143f5c9f8bdSJason M. Bills 
1440ec8b83dSEd Tanous inline std::optional<pcie_device::PCIeTypes>
14562cd45afSSpencer Ku     redfishPcieGenerationFromDbus(const std::string& generationInUse)
14662cd45afSSpencer Ku {
14762cd45afSSpencer Ku     if (generationInUse ==
14862cd45afSSpencer Ku         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen1")
14962cd45afSSpencer Ku     {
1500ec8b83dSEd Tanous         return pcie_device::PCIeTypes::Gen1;
15162cd45afSSpencer Ku     }
15262cd45afSSpencer Ku     if (generationInUse ==
15362cd45afSSpencer Ku         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen2")
15462cd45afSSpencer Ku     {
1550ec8b83dSEd Tanous         return pcie_device::PCIeTypes::Gen2;
15662cd45afSSpencer Ku     }
15762cd45afSSpencer Ku     if (generationInUse ==
15862cd45afSSpencer Ku         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen3")
15962cd45afSSpencer Ku     {
1600ec8b83dSEd Tanous         return pcie_device::PCIeTypes::Gen3;
16162cd45afSSpencer Ku     }
16262cd45afSSpencer Ku     if (generationInUse ==
16362cd45afSSpencer Ku         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen4")
16462cd45afSSpencer Ku     {
1650ec8b83dSEd Tanous         return pcie_device::PCIeTypes::Gen4;
16662cd45afSSpencer Ku     }
16762cd45afSSpencer Ku     if (generationInUse ==
16862cd45afSSpencer Ku         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen5")
16962cd45afSSpencer Ku     {
1700ec8b83dSEd Tanous         return pcie_device::PCIeTypes::Gen5;
17162cd45afSSpencer Ku     }
172e825cbc8SEd Tanous     if (generationInUse.empty() ||
173e825cbc8SEd Tanous         generationInUse ==
17462cd45afSSpencer Ku             "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Unknown")
17562cd45afSSpencer Ku     {
1760ec8b83dSEd Tanous         return pcie_device::PCIeTypes::Invalid;
17762cd45afSSpencer Ku     }
17862cd45afSSpencer Ku 
17962cd45afSSpencer Ku     // The value is not unknown or Gen1-5, need return an internal error.
18062cd45afSSpencer Ku     return std::nullopt;
18162cd45afSSpencer Ku }
18262cd45afSSpencer Ku 
183*ac106bf6SEd Tanous inline void
184*ac106bf6SEd Tanous     getPCIeDeviceState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
185c6bb3285SLakshmi Yadlapati                        const std::string& pcieDevicePath,
186c6bb3285SLakshmi Yadlapati                        const std::string& service)
187c6bb3285SLakshmi Yadlapati {
188c6bb3285SLakshmi Yadlapati     sdbusplus::asio::getProperty<bool>(
189c6bb3285SLakshmi Yadlapati         *crow::connections::systemBus, service, pcieDevicePath,
190c6bb3285SLakshmi Yadlapati         "xyz.openbmc_project.Inventory.Item", "Present",
191*ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec, const bool value) {
192c6bb3285SLakshmi Yadlapati         if (ec)
193c6bb3285SLakshmi Yadlapati         {
194c6bb3285SLakshmi Yadlapati             if (ec.value() != EBADR)
195c6bb3285SLakshmi Yadlapati             {
196c6bb3285SLakshmi Yadlapati                 BMCWEB_LOG_ERROR << "DBUS response error for State";
197*ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
198c6bb3285SLakshmi Yadlapati             }
199c6bb3285SLakshmi Yadlapati             return;
200c6bb3285SLakshmi Yadlapati         }
201c6bb3285SLakshmi Yadlapati 
202c6bb3285SLakshmi Yadlapati         if (!value)
203c6bb3285SLakshmi Yadlapati         {
204*ac106bf6SEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "Absent";
205c6bb3285SLakshmi Yadlapati         }
206c6bb3285SLakshmi Yadlapati         });
207c6bb3285SLakshmi Yadlapati }
208c6bb3285SLakshmi Yadlapati 
209*ac106bf6SEd Tanous inline void
210*ac106bf6SEd Tanous     getPCIeDeviceAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
211913e7732SSunnySrivastava1984                        const std::string& pcieDevicePath,
212913e7732SSunnySrivastava1984                        const std::string& service)
213913e7732SSunnySrivastava1984 {
214913e7732SSunnySrivastava1984     sdbusplus::asio::getAllProperties(
215913e7732SSunnySrivastava1984         *crow::connections::systemBus, service, pcieDevicePath,
216913e7732SSunnySrivastava1984         "xyz.openbmc_project.Inventory.Decorator.Asset",
217*ac106bf6SEd Tanous         [pcieDevicePath, asyncResp{asyncResp}](
218*ac106bf6SEd Tanous             const boost::system::error_code& ec,
219913e7732SSunnySrivastava1984             const dbus::utility::DBusPropertiesMap& assetList) {
220913e7732SSunnySrivastava1984         if (ec)
221913e7732SSunnySrivastava1984         {
222913e7732SSunnySrivastava1984             if (ec.value() != EBADR)
223913e7732SSunnySrivastava1984             {
224913e7732SSunnySrivastava1984                 BMCWEB_LOG_ERROR << "DBUS response error for Properties"
225913e7732SSunnySrivastava1984                                  << ec.value();
226*ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
227913e7732SSunnySrivastava1984             }
228913e7732SSunnySrivastava1984             return;
229913e7732SSunnySrivastava1984         }
230913e7732SSunnySrivastava1984 
231913e7732SSunnySrivastava1984         const std::string* manufacturer = nullptr;
232913e7732SSunnySrivastava1984         const std::string* model = nullptr;
233913e7732SSunnySrivastava1984         const std::string* partNumber = nullptr;
234913e7732SSunnySrivastava1984         const std::string* serialNumber = nullptr;
235913e7732SSunnySrivastava1984         const std::string* sparePartNumber = nullptr;
236913e7732SSunnySrivastava1984 
237913e7732SSunnySrivastava1984         const bool success = sdbusplus::unpackPropertiesNoThrow(
238913e7732SSunnySrivastava1984             dbus_utils::UnpackErrorPrinter(), assetList, "Manufacturer",
239913e7732SSunnySrivastava1984             manufacturer, "Model", model, "PartNumber", partNumber,
240913e7732SSunnySrivastava1984             "SerialNumber", serialNumber, "SparePartNumber", sparePartNumber);
241913e7732SSunnySrivastava1984 
242913e7732SSunnySrivastava1984         if (!success)
243913e7732SSunnySrivastava1984         {
244*ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
245913e7732SSunnySrivastava1984             return;
246913e7732SSunnySrivastava1984         }
247913e7732SSunnySrivastava1984 
248913e7732SSunnySrivastava1984         if (manufacturer != nullptr)
249913e7732SSunnySrivastava1984         {
250*ac106bf6SEd Tanous             asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
251913e7732SSunnySrivastava1984         }
252913e7732SSunnySrivastava1984         if (model != nullptr)
253913e7732SSunnySrivastava1984         {
254*ac106bf6SEd Tanous             asyncResp->res.jsonValue["Model"] = *model;
255913e7732SSunnySrivastava1984         }
256913e7732SSunnySrivastava1984 
257913e7732SSunnySrivastava1984         if (partNumber != nullptr)
258913e7732SSunnySrivastava1984         {
259*ac106bf6SEd Tanous             asyncResp->res.jsonValue["PartNumber"] = *partNumber;
260913e7732SSunnySrivastava1984         }
261913e7732SSunnySrivastava1984 
262913e7732SSunnySrivastava1984         if (serialNumber != nullptr)
263913e7732SSunnySrivastava1984         {
264*ac106bf6SEd Tanous             asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
265913e7732SSunnySrivastava1984         }
266913e7732SSunnySrivastava1984 
267913e7732SSunnySrivastava1984         if (sparePartNumber != nullptr && !sparePartNumber->empty())
268913e7732SSunnySrivastava1984         {
269*ac106bf6SEd Tanous             asyncResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
270913e7732SSunnySrivastava1984         }
271913e7732SSunnySrivastava1984         });
272913e7732SSunnySrivastava1984 }
273913e7732SSunnySrivastava1984 
274543f9a75SLakshmi Yadlapati inline void addPCIeDeviceProperties(
27535ad613dSLakshmi Yadlapati     crow::Response& resp, const std::string& pcieDeviceId,
276543f9a75SLakshmi Yadlapati     const dbus::utility::DBusPropertiesMap& pcieDevProperties)
277f5c9f8bdSJason M. Bills {
278d1bde9e5SKrzysztof Grobelny     const std::string* deviceType = nullptr;
279d1bde9e5SKrzysztof Grobelny     const std::string* generationInUse = nullptr;
280543f9a75SLakshmi Yadlapati     const int64_t* lanesInUse = nullptr;
281d1bde9e5SKrzysztof Grobelny 
282d1bde9e5SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
283543f9a75SLakshmi Yadlapati         dbus_utils::UnpackErrorPrinter(), pcieDevProperties, "DeviceType",
284543f9a75SLakshmi Yadlapati         deviceType, "GenerationInUse", generationInUse, "LanesInUse",
285bad2c4a9SLakshmi Yadlapati         lanesInUse);
286d1bde9e5SKrzysztof Grobelny 
287d1bde9e5SKrzysztof Grobelny     if (!success)
288d1bde9e5SKrzysztof Grobelny     {
289543f9a75SLakshmi Yadlapati         messages::internalError(resp);
290d1bde9e5SKrzysztof Grobelny         return;
291d1bde9e5SKrzysztof Grobelny     }
292d1bde9e5SKrzysztof Grobelny 
293543f9a75SLakshmi Yadlapati     if (deviceType != nullptr && !deviceType->empty())
294703f6741SMyung Bae     {
295543f9a75SLakshmi Yadlapati         resp.jsonValue["PCIeInterface"]["DeviceType"] = *deviceType;
296703f6741SMyung Bae     }
297703f6741SMyung Bae 
298d1bde9e5SKrzysztof Grobelny     if (generationInUse != nullptr)
299d1bde9e5SKrzysztof Grobelny     {
3000ec8b83dSEd Tanous         std::optional<pcie_device::PCIeTypes> redfishGenerationInUse =
301d1bde9e5SKrzysztof Grobelny             redfishPcieGenerationFromDbus(*generationInUse);
302543f9a75SLakshmi Yadlapati 
303d1bde9e5SKrzysztof Grobelny         if (!redfishGenerationInUse)
304d1bde9e5SKrzysztof Grobelny         {
305543f9a75SLakshmi Yadlapati             messages::internalError(resp);
306d1bde9e5SKrzysztof Grobelny             return;
307d1bde9e5SKrzysztof Grobelny         }
3080ec8b83dSEd Tanous         if (*redfishGenerationInUse != pcie_device::PCIeTypes::Invalid)
309d1bde9e5SKrzysztof Grobelny         {
310543f9a75SLakshmi Yadlapati             resp.jsonValue["PCIeInterface"]["PCIeType"] =
311d1bde9e5SKrzysztof Grobelny                 *redfishGenerationInUse;
312d1bde9e5SKrzysztof Grobelny         }
313a9f68bb5STony Lee     }
314d1bde9e5SKrzysztof Grobelny 
315543f9a75SLakshmi Yadlapati     // The default value of LanesInUse is 0, and the field will be
316543f9a75SLakshmi Yadlapati     // left as off if it is a default value.
317543f9a75SLakshmi Yadlapati     if (lanesInUse != nullptr && *lanesInUse != 0)
318543f9a75SLakshmi Yadlapati     {
319543f9a75SLakshmi Yadlapati         resp.jsonValue["PCIeInterface"]["LanesInUse"] = *lanesInUse;
320543f9a75SLakshmi Yadlapati     }
321543f9a75SLakshmi Yadlapati 
322ef4c65b7SEd Tanous     resp.jsonValue["PCIeFunctions"]["@odata.id"] = boost::urls::format(
323ef4c65b7SEd Tanous         "/redfish/v1/Systems/system/PCIeDevices/{}/PCIeFunctions",
324ef4c65b7SEd Tanous         pcieDeviceId);
325d1bde9e5SKrzysztof Grobelny }
326d1bde9e5SKrzysztof Grobelny 
327543f9a75SLakshmi Yadlapati inline void getPCIeDeviceProperties(
328*ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
329543f9a75SLakshmi Yadlapati     const std::string& pcieDevicePath, const std::string& service,
330543f9a75SLakshmi Yadlapati     const std::function<void(
331543f9a75SLakshmi Yadlapati         const dbus::utility::DBusPropertiesMap& pcieDevProperties)>&& callback)
332d1bde9e5SKrzysztof Grobelny {
333543f9a75SLakshmi Yadlapati     sdbusplus::asio::getAllProperties(
334543f9a75SLakshmi Yadlapati         *crow::connections::systemBus, service, pcieDevicePath,
335543f9a75SLakshmi Yadlapati         "xyz.openbmc_project.Inventory.Item.PCIeDevice",
336*ac106bf6SEd Tanous         [asyncResp,
337543f9a75SLakshmi Yadlapati          callback](const boost::system::error_code& ec,
338543f9a75SLakshmi Yadlapati                    const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
339543f9a75SLakshmi Yadlapati         if (ec)
340543f9a75SLakshmi Yadlapati         {
341543f9a75SLakshmi Yadlapati             if (ec.value() != EBADR)
342543f9a75SLakshmi Yadlapati             {
343543f9a75SLakshmi Yadlapati                 BMCWEB_LOG_ERROR << "DBUS response error for Properties";
344*ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
345543f9a75SLakshmi Yadlapati             }
346543f9a75SLakshmi Yadlapati             return;
347543f9a75SLakshmi Yadlapati         }
348543f9a75SLakshmi Yadlapati         callback(pcieDevProperties);
349543f9a75SLakshmi Yadlapati         });
350d1bde9e5SKrzysztof Grobelny }
351d1bde9e5SKrzysztof Grobelny 
352543f9a75SLakshmi Yadlapati inline void addPCIeDeviceCommonProperties(
353*ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
354543f9a75SLakshmi Yadlapati     const std::string& pcieDeviceId)
355543f9a75SLakshmi Yadlapati {
356*ac106bf6SEd Tanous     asyncResp->res.addHeader(
357543f9a75SLakshmi Yadlapati         boost::beast::http::field::link,
358543f9a75SLakshmi Yadlapati         "</redfish/v1/JsonSchemas/PCIeDevice/PCIeDevice.json>; rel=describedby");
359*ac106bf6SEd Tanous     asyncResp->res.jsonValue["@odata.type"] = "#PCIeDevice.v1_9_0.PCIeDevice";
360*ac106bf6SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
361ef4c65b7SEd Tanous         "/redfish/v1/Systems/system/PCIeDevices/{}", pcieDeviceId);
362*ac106bf6SEd Tanous     asyncResp->res.jsonValue["Name"] = "PCIe Device";
363*ac106bf6SEd Tanous     asyncResp->res.jsonValue["Id"] = pcieDeviceId;
364*ac106bf6SEd Tanous     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
365543f9a75SLakshmi Yadlapati }
3661476687dSEd Tanous 
367*ac106bf6SEd Tanous inline void
368*ac106bf6SEd Tanous     handlePCIeDeviceGet(App& app, const crow::Request& req,
369*ac106bf6SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
370543f9a75SLakshmi Yadlapati                         const std::string& systemName,
371543f9a75SLakshmi Yadlapati                         const std::string& pcieDeviceId)
372543f9a75SLakshmi Yadlapati {
373*ac106bf6SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
374543f9a75SLakshmi Yadlapati     {
375543f9a75SLakshmi Yadlapati         return;
376543f9a75SLakshmi Yadlapati     }
377543f9a75SLakshmi Yadlapati     if (systemName != "system")
378543f9a75SLakshmi Yadlapati     {
379*ac106bf6SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
380*ac106bf6SEd Tanous                                    systemName);
381543f9a75SLakshmi Yadlapati         return;
382543f9a75SLakshmi Yadlapati     }
383543f9a75SLakshmi Yadlapati 
384543f9a75SLakshmi Yadlapati     getValidPCIeDevicePath(
385*ac106bf6SEd Tanous         pcieDeviceId, asyncResp,
386*ac106bf6SEd Tanous         [asyncResp, pcieDeviceId](const std::string& pcieDevicePath,
387543f9a75SLakshmi Yadlapati                                   const std::string& service) {
388*ac106bf6SEd Tanous         addPCIeDeviceCommonProperties(asyncResp, pcieDeviceId);
389*ac106bf6SEd Tanous         getPCIeDeviceAsset(asyncResp, pcieDevicePath, service);
390*ac106bf6SEd Tanous         getPCIeDeviceState(asyncResp, pcieDevicePath, service);
391543f9a75SLakshmi Yadlapati         getPCIeDeviceProperties(
392*ac106bf6SEd Tanous             asyncResp, pcieDevicePath, service,
393*ac106bf6SEd Tanous             [asyncResp, pcieDeviceId](
39435ad613dSLakshmi Yadlapati                 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
395*ac106bf6SEd Tanous             addPCIeDeviceProperties(asyncResp->res, pcieDeviceId,
39635ad613dSLakshmi Yadlapati                                     pcieDevProperties);
3977e860f15SJohn Edward Broadbent             });
398543f9a75SLakshmi Yadlapati         });
399543f9a75SLakshmi Yadlapati }
400543f9a75SLakshmi Yadlapati 
401543f9a75SLakshmi Yadlapati inline void requestRoutesSystemPCIeDevice(App& app)
402543f9a75SLakshmi Yadlapati {
403543f9a75SLakshmi Yadlapati     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/PCIeDevices/<str>/")
404543f9a75SLakshmi Yadlapati         .privileges(redfish::privileges::getPCIeDevice)
405543f9a75SLakshmi Yadlapati         .methods(boost::beast::http::verb::get)(
406543f9a75SLakshmi Yadlapati             std::bind_front(handlePCIeDeviceGet, std::ref(app)));
407dede6a98SJason M. Bills }
408dede6a98SJason M. Bills 
40935ad613dSLakshmi Yadlapati inline void addPCIeFunctionList(
41035ad613dSLakshmi Yadlapati     crow::Response& res, const std::string& pcieDeviceId,
41135ad613dSLakshmi Yadlapati     const dbus::utility::DBusPropertiesMap& pcieDevProperties)
41235ad613dSLakshmi Yadlapati {
41335ad613dSLakshmi Yadlapati     nlohmann::json& pcieFunctionList = res.jsonValue["Members"];
41435ad613dSLakshmi Yadlapati     pcieFunctionList = nlohmann::json::array();
41535ad613dSLakshmi Yadlapati     static constexpr const int maxPciFunctionNum = 8;
41635ad613dSLakshmi Yadlapati 
41735ad613dSLakshmi Yadlapati     for (int functionNum = 0; functionNum < maxPciFunctionNum; functionNum++)
41835ad613dSLakshmi Yadlapati     {
41935ad613dSLakshmi Yadlapati         // Check if this function exists by
42035ad613dSLakshmi Yadlapati         // looking for a device ID
42189492a15SPatrick Williams         std::string devIDProperty = "Function" + std::to_string(functionNum) +
42289492a15SPatrick Williams                                     "DeviceId";
42335ad613dSLakshmi Yadlapati         const std::string* property = nullptr;
42435ad613dSLakshmi Yadlapati         for (const auto& propEntry : pcieDevProperties)
42535ad613dSLakshmi Yadlapati         {
42635ad613dSLakshmi Yadlapati             if (propEntry.first == devIDProperty)
42735ad613dSLakshmi Yadlapati             {
42835ad613dSLakshmi Yadlapati                 property = std::get_if<std::string>(&propEntry.second);
42935ad613dSLakshmi Yadlapati                 break;
43035ad613dSLakshmi Yadlapati             }
43135ad613dSLakshmi Yadlapati         }
43235ad613dSLakshmi Yadlapati         if (property == nullptr || property->empty())
43335ad613dSLakshmi Yadlapati         {
43435ad613dSLakshmi Yadlapati             continue;
43535ad613dSLakshmi Yadlapati         }
43635ad613dSLakshmi Yadlapati 
43735ad613dSLakshmi Yadlapati         nlohmann::json::object_t pcieFunction;
438ef4c65b7SEd Tanous         pcieFunction["@odata.id"] = boost::urls::format(
439ef4c65b7SEd Tanous             "/redfish/v1/Systems/system/PCIeDevices/{}/PCIeFunctions/{}",
440ef4c65b7SEd Tanous             pcieDeviceId, std::to_string(functionNum));
441b2ba3072SPatrick Williams         pcieFunctionList.emplace_back(std::move(pcieFunction));
44235ad613dSLakshmi Yadlapati     }
44335ad613dSLakshmi Yadlapati     res.jsonValue["PCIeFunctions@odata.count"] = pcieFunctionList.size();
44435ad613dSLakshmi Yadlapati }
44535ad613dSLakshmi Yadlapati 
44635ad613dSLakshmi Yadlapati inline void handlePCIeFunctionCollectionGet(
44735ad613dSLakshmi Yadlapati     App& app, const crow::Request& req,
448*ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
44935ad613dSLakshmi Yadlapati     const std::string& pcieDeviceId)
45035ad613dSLakshmi Yadlapati {
451*ac106bf6SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
45235ad613dSLakshmi Yadlapati     {
45335ad613dSLakshmi Yadlapati         return;
45435ad613dSLakshmi Yadlapati     }
45535ad613dSLakshmi Yadlapati 
45635ad613dSLakshmi Yadlapati     getValidPCIeDevicePath(
457*ac106bf6SEd Tanous         pcieDeviceId, asyncResp,
458*ac106bf6SEd Tanous         [asyncResp, pcieDeviceId](const std::string& pcieDevicePath,
45935ad613dSLakshmi Yadlapati                                   const std::string& service) {
460*ac106bf6SEd Tanous         asyncResp->res.addHeader(
46135ad613dSLakshmi Yadlapati             boost::beast::http::field::link,
46235ad613dSLakshmi Yadlapati             "</redfish/v1/JsonSchemas/PCIeFunctionCollection/PCIeFunctionCollection.json>; rel=describedby");
463*ac106bf6SEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
46435ad613dSLakshmi Yadlapati             "#PCIeFunctionCollection.PCIeFunctionCollection";
465*ac106bf6SEd Tanous         asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
466ef4c65b7SEd Tanous             "/redfish/v1/Systems/system/PCIeDevices/{}/PCIeFunctions",
467ef4c65b7SEd Tanous             pcieDeviceId);
468*ac106bf6SEd Tanous         asyncResp->res.jsonValue["Name"] = "PCIe Function Collection";
469*ac106bf6SEd Tanous         asyncResp->res.jsonValue["Description"] =
47035ad613dSLakshmi Yadlapati             "Collection of PCIe Functions for PCIe Device " + pcieDeviceId;
47135ad613dSLakshmi Yadlapati         getPCIeDeviceProperties(
472*ac106bf6SEd Tanous             asyncResp, pcieDevicePath, service,
473*ac106bf6SEd Tanous             [asyncResp, pcieDeviceId](
47435ad613dSLakshmi Yadlapati                 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
475*ac106bf6SEd Tanous             addPCIeFunctionList(asyncResp->res, pcieDeviceId,
476*ac106bf6SEd Tanous                                 pcieDevProperties);
47735ad613dSLakshmi Yadlapati             });
47835ad613dSLakshmi Yadlapati         });
47935ad613dSLakshmi Yadlapati }
48035ad613dSLakshmi Yadlapati 
4817e860f15SJohn Edward Broadbent inline void requestRoutesSystemPCIeFunctionCollection(App& app)
4827e860f15SJohn Edward Broadbent {
483dede6a98SJason M. Bills     /**
484dede6a98SJason M. Bills      * Functions triggers appropriate requests on DBus
485dede6a98SJason M. Bills      */
4867e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
4877e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/PCIeDevices/<str>/PCIeFunctions/")
488ed398213SEd Tanous         .privileges(redfish::privileges::getPCIeFunctionCollection)
489002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
49035ad613dSLakshmi Yadlapati             std::bind_front(handlePCIeFunctionCollectionGet, std::ref(app)));
4917e860f15SJohn Edward Broadbent }
4927e860f15SJohn Edward Broadbent 
493727a046cSLakshmi Yadlapati inline bool validatePCIeFunctionId(
494d5e74b80SMyung Bae     uint64_t pcieFunctionId,
495727a046cSLakshmi Yadlapati     const dbus::utility::DBusPropertiesMap& pcieDevProperties)
4967e860f15SJohn Edward Broadbent {
497d5e74b80SMyung Bae     std::string functionName = "Function" + std::to_string(pcieFunctionId);
498b9d36b47SEd Tanous     std::string devIDProperty = functionName + "DeviceId";
499b9d36b47SEd Tanous 
500b9d36b47SEd Tanous     const std::string* devIdProperty = nullptr;
501b9d36b47SEd Tanous     for (const auto& property : pcieDevProperties)
502b9d36b47SEd Tanous     {
503b9d36b47SEd Tanous         if (property.first == devIDProperty)
504b9d36b47SEd Tanous         {
505002d39b4SEd Tanous             devIdProperty = std::get_if<std::string>(&property.second);
506727a046cSLakshmi Yadlapati             break;
507b9d36b47SEd Tanous         }
508b9d36b47SEd Tanous     }
509727a046cSLakshmi Yadlapati     return (devIdProperty != nullptr && !devIdProperty->empty());
510727a046cSLakshmi Yadlapati }
511727a046cSLakshmi Yadlapati 
512727a046cSLakshmi Yadlapati inline void addPCIeFunctionProperties(
513e14742caSEd Tanous     crow::Response& resp, uint64_t pcieFunctionId,
514727a046cSLakshmi Yadlapati     const dbus::utility::DBusPropertiesMap& pcieDevProperties)
515f5c9f8bdSJason M. Bills {
516e14742caSEd Tanous     std::string functionName = "Function" + std::to_string(pcieFunctionId);
517b9d36b47SEd Tanous     for (const auto& property : pcieDevProperties)
518f5c9f8bdSJason M. Bills     {
519b9d36b47SEd Tanous         const std::string* strProperty =
520b9d36b47SEd Tanous             std::get_if<std::string>(&property.second);
521727a046cSLakshmi Yadlapati 
522b9d36b47SEd Tanous         if (property.first == functionName + "DeviceId")
523f5c9f8bdSJason M. Bills         {
524727a046cSLakshmi Yadlapati             resp.jsonValue["DeviceId"] = *strProperty;
525f5c9f8bdSJason M. Bills         }
526b9d36b47SEd Tanous         if (property.first == functionName + "VendorId")
527f5c9f8bdSJason M. Bills         {
528727a046cSLakshmi Yadlapati             resp.jsonValue["VendorId"] = *strProperty;
529f5c9f8bdSJason M. Bills         }
530727a046cSLakshmi Yadlapati         // TODO: FunctionType and DeviceClass are Redfish enums. The D-Bus
531727a046cSLakshmi Yadlapati         // property strings should be mapped correctly to ensure these
532727a046cSLakshmi Yadlapati         // strings are Redfish enum values. For now just check for empty.
533b9d36b47SEd Tanous         if (property.first == functionName + "FunctionType")
534f5c9f8bdSJason M. Bills         {
535727a046cSLakshmi Yadlapati             if (!strProperty->empty())
536727a046cSLakshmi Yadlapati             {
537727a046cSLakshmi Yadlapati                 resp.jsonValue["FunctionType"] = *strProperty;
538727a046cSLakshmi Yadlapati             }
539f5c9f8bdSJason M. Bills         }
540b9d36b47SEd Tanous         if (property.first == functionName + "DeviceClass")
541f5c9f8bdSJason M. Bills         {
542727a046cSLakshmi Yadlapati             if (!strProperty->empty())
543727a046cSLakshmi Yadlapati             {
544727a046cSLakshmi Yadlapati                 resp.jsonValue["DeviceClass"] = *strProperty;
545727a046cSLakshmi Yadlapati             }
546f5c9f8bdSJason M. Bills         }
547b9d36b47SEd Tanous         if (property.first == functionName + "ClassCode")
548f5c9f8bdSJason M. Bills         {
549727a046cSLakshmi Yadlapati             resp.jsonValue["ClassCode"] = *strProperty;
550f5c9f8bdSJason M. Bills         }
551b9d36b47SEd Tanous         if (property.first == functionName + "RevisionId")
552f5c9f8bdSJason M. Bills         {
553727a046cSLakshmi Yadlapati             resp.jsonValue["RevisionId"] = *strProperty;
554f5c9f8bdSJason M. Bills         }
555b9d36b47SEd Tanous         if (property.first == functionName + "SubsystemId")
556b9d36b47SEd Tanous         {
557727a046cSLakshmi Yadlapati             resp.jsonValue["SubsystemId"] = *strProperty;
558b9d36b47SEd Tanous         }
559002d39b4SEd Tanous         if (property.first == functionName + "SubsystemVendorId")
560f5c9f8bdSJason M. Bills         {
561727a046cSLakshmi Yadlapati             resp.jsonValue["SubsystemVendorId"] = *strProperty;
562b9d36b47SEd Tanous         }
563f5c9f8bdSJason M. Bills     }
564727a046cSLakshmi Yadlapati }
565727a046cSLakshmi Yadlapati 
566727a046cSLakshmi Yadlapati inline void addPCIeFunctionCommonProperties(crow::Response& resp,
567727a046cSLakshmi Yadlapati                                             const std::string& pcieDeviceId,
568e14742caSEd Tanous                                             uint64_t pcieFunctionId)
569727a046cSLakshmi Yadlapati {
570727a046cSLakshmi Yadlapati     resp.addHeader(
571727a046cSLakshmi Yadlapati         boost::beast::http::field::link,
572727a046cSLakshmi Yadlapati         "</redfish/v1/JsonSchemas/PCIeFunction/PCIeFunction.json>; rel=describedby");
573727a046cSLakshmi Yadlapati     resp.jsonValue["@odata.type"] = "#PCIeFunction.v1_2_3.PCIeFunction";
574ef4c65b7SEd Tanous     resp.jsonValue["@odata.id"] = boost::urls::format(
575ef4c65b7SEd Tanous         "/redfish/v1/Systems/system/PCIeDevices/{}/PCIeFunctions/{}",
576ef4c65b7SEd Tanous         pcieDeviceId, pcieFunctionId);
577727a046cSLakshmi Yadlapati     resp.jsonValue["Name"] = "PCIe Function";
578e14742caSEd Tanous     resp.jsonValue["Id"] = std::to_string(pcieFunctionId);
579e14742caSEd Tanous     resp.jsonValue["FunctionId"] = pcieFunctionId;
580ef4c65b7SEd Tanous     resp.jsonValue["Links"]["PCIeDevice"]["@odata.id"] = boost::urls::format(
581ef4c65b7SEd Tanous         "/redfish/v1/Systems/system/PCIeDevices/{}", pcieDeviceId);
582727a046cSLakshmi Yadlapati }
583727a046cSLakshmi Yadlapati 
584727a046cSLakshmi Yadlapati inline void
585727a046cSLakshmi Yadlapati     handlePCIeFunctionGet(App& app, const crow::Request& req,
586*ac106bf6SEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
587727a046cSLakshmi Yadlapati                           const std::string& pcieDeviceId,
588e14742caSEd Tanous                           const std::string& pcieFunctionIdStr)
589727a046cSLakshmi Yadlapati {
590*ac106bf6SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
591727a046cSLakshmi Yadlapati     {
592727a046cSLakshmi Yadlapati         return;
593727a046cSLakshmi Yadlapati     }
594e14742caSEd Tanous     uint64_t pcieFunctionId = 0;
595e14742caSEd Tanous     std::from_chars_result result = std::from_chars(
596e14742caSEd Tanous         &*pcieFunctionIdStr.begin(), &*pcieFunctionIdStr.end(), pcieFunctionId);
597e14742caSEd Tanous     if (result.ec != std::errc{} || result.ptr != &*pcieFunctionIdStr.end())
598e14742caSEd Tanous     {
599*ac106bf6SEd Tanous         messages::resourceNotFound(asyncResp->res, "PCIeFunction",
600e14742caSEd Tanous                                    pcieFunctionIdStr);
601e14742caSEd Tanous         return;
602e14742caSEd Tanous     }
603727a046cSLakshmi Yadlapati 
604*ac106bf6SEd Tanous     getValidPCIeDevicePath(pcieDeviceId, asyncResp,
605*ac106bf6SEd Tanous                            [asyncResp, pcieDeviceId,
606*ac106bf6SEd Tanous                             pcieFunctionId](const std::string& pcieDevicePath,
607727a046cSLakshmi Yadlapati                                             const std::string& service) {
608727a046cSLakshmi Yadlapati         getPCIeDeviceProperties(
609*ac106bf6SEd Tanous             asyncResp, pcieDevicePath, service,
610*ac106bf6SEd Tanous             [asyncResp, pcieDeviceId, pcieFunctionId](
611727a046cSLakshmi Yadlapati                 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
612*ac106bf6SEd Tanous             addPCIeFunctionCommonProperties(asyncResp->res, pcieDeviceId,
613727a046cSLakshmi Yadlapati                                             pcieFunctionId);
614*ac106bf6SEd Tanous             addPCIeFunctionProperties(asyncResp->res, pcieFunctionId,
615727a046cSLakshmi Yadlapati                                       pcieDevProperties);
6167e860f15SJohn Edward Broadbent             });
617727a046cSLakshmi Yadlapati     });
618727a046cSLakshmi Yadlapati }
619727a046cSLakshmi Yadlapati 
620727a046cSLakshmi Yadlapati inline void requestRoutesSystemPCIeFunction(App& app)
621727a046cSLakshmi Yadlapati {
622727a046cSLakshmi Yadlapati     BMCWEB_ROUTE(
623727a046cSLakshmi Yadlapati         app,
624727a046cSLakshmi Yadlapati         "/redfish/v1/Systems/system/PCIeDevices/<str>/PCIeFunctions/<str>/")
625727a046cSLakshmi Yadlapati         .privileges(redfish::privileges::getPCIeFunction)
626727a046cSLakshmi Yadlapati         .methods(boost::beast::http::verb::get)(
627727a046cSLakshmi Yadlapati             std::bind_front(handlePCIeFunctionGet, std::ref(app)));
628f5c9f8bdSJason M. Bills }
629f5c9f8bdSJason M. Bills 
630f5c9f8bdSJason M. Bills } // namespace redfish
631