blob: cda1323905ed96efe2d395bf893e35c2b9b88e15 [file] [log] [blame]
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -08001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
17#pragma once
18
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080019#include "app.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080020#include "dbus_utility.hpp"
Ed Tanous0ec8b832022-03-14 14:56:47 -070021#include "generated/enums/pcie_device.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080022#include "query.hpp"
23#include "registries/privilege_registry.hpp"
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -060024#include "utils/collection.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080025#include "utils/dbus_utils.hpp"
Ed Tanous0ec8b832022-03-14 14:56:47 -070026
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -080027#include <boost/system/linux_error.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020028#include <sdbusplus/asio/property.hpp>
29#include <sdbusplus/unpack_properties.hpp>
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -080030
31namespace redfish
32{
33
Lakshmi Yadlapati94c3a102023-04-05 18:11:22 -050034static constexpr char const* inventoryPath = "/xyz/openbmc_project/inventory";
35static constexpr std::array<std::string_view, 1> pcieDeviceInterface = {
36 "xyz.openbmc_project.Inventory.Item.PCIeDevice"};
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -080037
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -060038static inline void handlePCIeDevicePath(
39 const std::string& pcieDeviceId,
40 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
41 const dbus::utility::MapperGetSubTreePathsResponse& pcieDevicePaths,
42 const std::function<void(const std::string& pcieDevicePath,
43 const std::string& service)>& callback)
44
45{
46 for (const std::string& pcieDevicePath : pcieDevicePaths)
47 {
48 std::string pciecDeviceName =
49 sdbusplus::message::object_path(pcieDevicePath).filename();
50 if (pciecDeviceName.empty() || pciecDeviceName != pcieDeviceId)
51 {
52 continue;
53 }
54
55 dbus::utility::getDbusObject(
56 pcieDevicePath, {},
57 [pcieDevicePath, aResp,
58 callback](const boost::system::error_code& ec,
59 const dbus::utility::MapperGetObject& object) {
60 if (ec || object.empty())
61 {
62 BMCWEB_LOG_ERROR << "DBUS response error " << ec;
63 messages::internalError(aResp->res);
64 return;
65 }
66 callback(pcieDevicePath, object.begin()->first);
67 });
68 return;
69 }
70
71 BMCWEB_LOG_WARNING << "PCIe Device not found";
72 messages::resourceNotFound(aResp->res, "PCIeDevice", pcieDeviceId);
73}
74
75static inline void getValidPCIeDevicePath(
76 const std::string& pcieDeviceId,
77 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
78 const std::function<void(const std::string& pcieDevicePath,
79 const std::string& service)>& callback)
80{
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -060081 dbus::utility::getSubTreePaths(
Lakshmi Yadlapati94c3a102023-04-05 18:11:22 -050082 inventoryPath, 0, pcieDeviceInterface,
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -060083 [pcieDeviceId, aResp,
84 callback](const boost::system::error_code& ec,
85 const dbus::utility::MapperGetSubTreePathsResponse&
86 pcieDevicePaths) {
87 if (ec)
88 {
89 BMCWEB_LOG_ERROR << "D-Bus response error on GetSubTree " << ec;
90 messages::internalError(aResp->res);
91 return;
92 }
93 handlePCIeDevicePath(pcieDeviceId, aResp, pcieDevicePaths, callback);
94 return;
95 });
96}
97
Ed Tanousb5a76932020-09-29 16:16:58 -070098static inline void
zhanghch058d1b46d2021-04-01 11:18:24 +080099 getPCIeDeviceList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanousb5a76932020-09-29 16:16:58 -0700100 const std::string& name)
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -0800101{
George Liu7a1dbc42022-12-07 16:03:22 +0800102 dbus::utility::getSubTreePaths(
Lakshmi Yadlapatidf7f12f2023-05-01 09:41:11 -0500103 inventoryPath, 1, pcieDeviceInterface,
George Liu7a1dbc42022-12-07 16:03:22 +0800104 [asyncResp, name](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800105 const dbus::utility::MapperGetSubTreePathsResponse&
106 pcieDevicePaths) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700107 if (ec)
108 {
109 BMCWEB_LOG_DEBUG << "no PCIe device paths found ec: "
110 << ec.message();
111 // Not an error, system just doesn't have PCIe info
112 return;
113 }
114 nlohmann::json& pcieDeviceList = asyncResp->res.jsonValue[name];
115 pcieDeviceList = nlohmann::json::array();
116 for (const std::string& pcieDevicePath : pcieDevicePaths)
117 {
118 size_t devStart = pcieDevicePath.rfind('/');
119 if (devStart == std::string::npos)
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -0800120 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700121 continue;
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -0800122 }
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -0800123
Ed Tanous002d39b2022-05-31 08:59:27 -0700124 std::string devName = pcieDevicePath.substr(devStart + 1);
125 if (devName.empty())
126 {
127 continue;
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -0800128 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700129 nlohmann::json::object_t pcieDevice;
Willy Tueddfc432022-09-26 16:46:38 +0000130 pcieDevice["@odata.id"] = crow::utility::urlFromPieces(
131 "redfish", "v1", "Systems", "system", "PCIeDevices", devName);
Ed Tanous002d39b2022-05-31 08:59:27 -0700132 pcieDeviceList.push_back(std::move(pcieDevice));
133 }
134 asyncResp->res.jsonValue[name + "@odata.count"] = pcieDeviceList.size();
George Liu7a1dbc42022-12-07 16:03:22 +0800135 });
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -0800136}
137
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600138static inline void handlePCIeDeviceCollectionGet(
139 crow::App& app, const crow::Request& req,
140 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
141 const std::string& systemName)
142{
143 if (!redfish::setUpRedfishRoute(app, req, aResp))
144 {
145 return;
146 }
147 if (systemName != "system")
148 {
149 messages::resourceNotFound(aResp->res, "ComputerSystem", systemName);
150 return;
151 }
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600152
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600153 aResp->res.addHeader(boost::beast::http::field::link,
154 "</redfish/v1/JsonSchemas/PCIeDeviceCollection/"
155 "PCIeDeviceCollection.json>; rel=describedby");
156 aResp->res.jsonValue["@odata.type"] =
157 "#PCIeDeviceCollection.PCIeDeviceCollection";
158 aResp->res.jsonValue["@odata.id"] =
159 "/redfish/v1/Systems/system/PCIeDevices";
160 aResp->res.jsonValue["Name"] = "PCIe Device Collection";
161 aResp->res.jsonValue["Description"] = "Collection of PCIe Devices";
162 aResp->res.jsonValue["Members"] = nlohmann::json::array();
163 aResp->res.jsonValue["Members@odata.count"] = 0;
164
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600165 collection_util::getCollectionMembers(
166 aResp, boost::urls::url("/redfish/v1/Systems/system/PCIeDevices"),
Lakshmi Yadlapati94c3a102023-04-05 18:11:22 -0500167 pcieDeviceInterface);
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600168}
169
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700170inline void requestRoutesSystemPCIeDeviceCollection(App& app)
Jason M. Billsadbe1922019-10-14 15:44:35 -0700171{
Jason M. Billsadbe1922019-10-14 15:44:35 -0700172 /**
173 * Functions triggers appropriate requests on DBus
174 */
Ed Tanous22d268c2022-05-19 09:39:07 -0700175 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/PCIeDevices/")
Ed Tanoused398212021-06-09 17:05:54 -0700176 .privileges(redfish::privileges::getPCIeDeviceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700177 .methods(boost::beast::http::verb::get)(
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600178 std::bind_front(handlePCIeDeviceCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700179}
180
Ed Tanous0ec8b832022-03-14 14:56:47 -0700181inline std::optional<pcie_device::PCIeTypes>
Spencer Ku62cd45a2021-11-22 16:41:25 +0800182 redfishPcieGenerationFromDbus(const std::string& generationInUse)
183{
184 if (generationInUse ==
185 "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen1")
186 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700187 return pcie_device::PCIeTypes::Gen1;
Spencer Ku62cd45a2021-11-22 16:41:25 +0800188 }
189 if (generationInUse ==
190 "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen2")
191 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700192 return pcie_device::PCIeTypes::Gen2;
Spencer Ku62cd45a2021-11-22 16:41:25 +0800193 }
194 if (generationInUse ==
195 "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen3")
196 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700197 return pcie_device::PCIeTypes::Gen3;
Spencer Ku62cd45a2021-11-22 16:41:25 +0800198 }
199 if (generationInUse ==
200 "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen4")
201 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700202 return pcie_device::PCIeTypes::Gen4;
Spencer Ku62cd45a2021-11-22 16:41:25 +0800203 }
204 if (generationInUse ==
205 "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen5")
206 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700207 return pcie_device::PCIeTypes::Gen5;
Spencer Ku62cd45a2021-11-22 16:41:25 +0800208 }
Ed Tanouse825cbc2022-06-17 11:39:22 -0700209 if (generationInUse.empty() ||
210 generationInUse ==
211 "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Unknown")
Spencer Ku62cd45a2021-11-22 16:41:25 +0800212 {
Ed Tanous0ec8b832022-03-14 14:56:47 -0700213 return pcie_device::PCIeTypes::Invalid;
Spencer Ku62cd45a2021-11-22 16:41:25 +0800214 }
215
216 // The value is not unknown or Gen1-5, need return an internal error.
217 return std::nullopt;
218}
219
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600220inline void getPCIeDeviceAsset(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
221 const std::string& pcieDevicePath,
222 const std::string& service)
223{
224 sdbusplus::asio::getAllProperties(
225 *crow::connections::systemBus, service, pcieDevicePath,
226 "xyz.openbmc_project.Inventory.Decorator.Asset",
227 [pcieDevicePath,
228 aResp{aResp}](const boost::system::error_code& ec,
229 const dbus::utility::DBusPropertiesMap& assetList) {
230 if (ec)
231 {
232 if (ec.value() != EBADR)
233 {
234 BMCWEB_LOG_ERROR << "DBUS response error for Properties"
235 << ec.value();
236 messages::internalError(aResp->res);
237 }
238 return;
239 }
240
241 const std::string* manufacturer = nullptr;
242 const std::string* model = nullptr;
243 const std::string* partNumber = nullptr;
244 const std::string* serialNumber = nullptr;
245 const std::string* sparePartNumber = nullptr;
246
247 const bool success = sdbusplus::unpackPropertiesNoThrow(
248 dbus_utils::UnpackErrorPrinter(), assetList, "Manufacturer",
249 manufacturer, "Model", model, "PartNumber", partNumber,
250 "SerialNumber", serialNumber, "SparePartNumber", sparePartNumber);
251
252 if (!success)
253 {
254 messages::internalError(aResp->res);
255 return;
256 }
257
258 if (manufacturer != nullptr)
259 {
260 aResp->res.jsonValue["Manufacturer"] = *manufacturer;
261 }
262 if (model != nullptr)
263 {
264 aResp->res.jsonValue["Model"] = *model;
265 }
266
267 if (partNumber != nullptr)
268 {
269 aResp->res.jsonValue["PartNumber"] = *partNumber;
270 }
271
272 if (serialNumber != nullptr)
273 {
274 aResp->res.jsonValue["SerialNumber"] = *serialNumber;
275 }
276
277 if (sparePartNumber != nullptr && !sparePartNumber->empty())
278 {
279 aResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
280 }
281 });
282}
283
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600284inline void addPCIeDeviceProperties(
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600285 crow::Response& resp, const std::string& pcieDeviceId,
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600286 const dbus::utility::DBusPropertiesMap& pcieDevProperties)
287{
288 const std::string* manufacturer = nullptr;
289 const std::string* deviceType = nullptr;
290 const std::string* generationInUse = nullptr;
291 const int64_t* lanesInUse = nullptr;
292
293 const bool success = sdbusplus::unpackPropertiesNoThrow(
294 dbus_utils::UnpackErrorPrinter(), pcieDevProperties, "DeviceType",
295 deviceType, "GenerationInUse", generationInUse, "LanesInUse",
296 lanesInUse, "Manufacturer", manufacturer);
297
298 if (!success)
299 {
300 messages::internalError(resp);
301 return;
302 }
303
304 if (deviceType != nullptr && !deviceType->empty())
305 {
306 resp.jsonValue["PCIeInterface"]["DeviceType"] = *deviceType;
307 }
308
309 if (generationInUse != nullptr)
310 {
311 std::optional<pcie_device::PCIeTypes> redfishGenerationInUse =
312 redfishPcieGenerationFromDbus(*generationInUse);
313
314 if (!redfishGenerationInUse)
315 {
316 messages::internalError(resp);
317 return;
318 }
319 if (*redfishGenerationInUse != pcie_device::PCIeTypes::Invalid)
320 {
321 resp.jsonValue["PCIeInterface"]["PCIeType"] =
322 *redfishGenerationInUse;
323 }
324 }
325
326 // The default value of LanesInUse is 0, and the field will be
327 // left as off if it is a default value.
328 if (lanesInUse != nullptr && *lanesInUse != 0)
329 {
330 resp.jsonValue["PCIeInterface"]["LanesInUse"] = *lanesInUse;
331 }
332
333 if (manufacturer != nullptr)
334 {
335 resp.jsonValue["PCIeInterface"]["Manufacturer"] = *manufacturer;
336 }
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600337
338 resp.jsonValue["PCIeFunctions"]["@odata.id"] = crow::utility::urlFromPieces(
339 "redfish", "v1", "Systems", "system", "PCIeDevices", pcieDeviceId,
340 "PCIeFunctions");
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600341}
342
343inline void getPCIeDeviceProperties(
344 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
345 const std::string& pcieDevicePath, const std::string& service,
346 const std::function<void(
347 const dbus::utility::DBusPropertiesMap& pcieDevProperties)>&& callback)
348{
349 sdbusplus::asio::getAllProperties(
350 *crow::connections::systemBus, service, pcieDevicePath,
351 "xyz.openbmc_project.Inventory.Item.PCIeDevice",
352 [aResp,
353 callback](const boost::system::error_code& ec,
354 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
355 if (ec)
356 {
357 if (ec.value() != EBADR)
358 {
359 BMCWEB_LOG_ERROR << "DBUS response error for Properties";
360 messages::internalError(aResp->res);
361 }
362 return;
363 }
364 callback(pcieDevProperties);
365 });
366}
367
368inline void addPCIeDeviceCommonProperties(
369 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
370 const std::string& pcieDeviceId)
371{
372 aResp->res.addHeader(
373 boost::beast::http::field::link,
374 "</redfish/v1/JsonSchemas/PCIeDevice/PCIeDevice.json>; rel=describedby");
375 aResp->res.jsonValue["@odata.type"] = "#PCIeDevice.v1_9_0.PCIeDevice";
376 aResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
377 "redfish", "v1", "Systems", "system", "PCIeDevices", pcieDeviceId);
378 aResp->res.jsonValue["Name"] = "PCIe Device";
379 aResp->res.jsonValue["Id"] = pcieDeviceId;
380}
381
382inline void handlePCIeDeviceGet(App& app, const crow::Request& req,
383 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
384 const std::string& systemName,
385 const std::string& pcieDeviceId)
386{
387 if (!redfish::setUpRedfishRoute(app, req, aResp))
388 {
389 return;
390 }
391 if (systemName != "system")
392 {
393 messages::resourceNotFound(aResp->res, "ComputerSystem", systemName);
394 return;
395 }
396
397 getValidPCIeDevicePath(
398 pcieDeviceId, aResp,
399 [aResp, pcieDeviceId](const std::string& pcieDevicePath,
400 const std::string& service) {
401 addPCIeDeviceCommonProperties(aResp, pcieDeviceId);
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600402 getPCIeDeviceAsset(aResp, pcieDevicePath, service);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600403 getPCIeDeviceProperties(
404 aResp, pcieDevicePath, service,
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600405 [aResp, pcieDeviceId](
406 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
407 addPCIeDeviceProperties(aResp->res, pcieDeviceId,
408 pcieDevProperties);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600409 });
410 });
411}
412
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700413inline void requestRoutesSystemPCIeDevice(App& app)
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -0800414{
Ed Tanous22d268c2022-05-19 09:39:07 -0700415 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/PCIeDevices/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700416 .privileges(redfish::privileges::getPCIeDevice)
Ed Tanous002d39b2022-05-31 08:59:27 -0700417 .methods(boost::beast::http::verb::get)(
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600418 std::bind_front(handlePCIeDeviceGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700419}
420
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600421inline void addPCIeFunctionList(
422 crow::Response& res, const std::string& pcieDeviceId,
423 const dbus::utility::DBusPropertiesMap& pcieDevProperties)
424{
425 nlohmann::json& pcieFunctionList = res.jsonValue["Members"];
426 pcieFunctionList = nlohmann::json::array();
427 static constexpr const int maxPciFunctionNum = 8;
428
429 for (int functionNum = 0; functionNum < maxPciFunctionNum; functionNum++)
430 {
431 // Check if this function exists by
432 // looking for a device ID
433 std::string devIDProperty =
434 "Function" + std::to_string(functionNum) + "DeviceId";
435 const std::string* property = nullptr;
436 for (const auto& propEntry : pcieDevProperties)
437 {
438 if (propEntry.first == devIDProperty)
439 {
440 property = std::get_if<std::string>(&propEntry.second);
441 break;
442 }
443 }
444 if (property == nullptr || property->empty())
445 {
446 continue;
447 }
448
449 nlohmann::json::object_t pcieFunction;
450 pcieFunction["@odata.id"] = crow::utility::urlFromPieces(
451 "redfish", "v1", "Systems", "system", "PCIeDevices", pcieDeviceId,
452 "PCIeFunctions", std::to_string(functionNum));
453 pcieFunctionList.push_back(std::move(pcieFunction));
454 }
455 res.jsonValue["PCIeFunctions@odata.count"] = pcieFunctionList.size();
456}
457
458inline void handlePCIeFunctionCollectionGet(
459 App& app, const crow::Request& req,
460 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
461 const std::string& pcieDeviceId)
462{
463 if (!redfish::setUpRedfishRoute(app, req, aResp))
464 {
465 return;
466 }
467
468 getValidPCIeDevicePath(
469 pcieDeviceId, aResp,
470 [aResp, pcieDeviceId](const std::string& pcieDevicePath,
471 const std::string& service) {
472 aResp->res.addHeader(
473 boost::beast::http::field::link,
474 "</redfish/v1/JsonSchemas/PCIeFunctionCollection/PCIeFunctionCollection.json>; rel=describedby");
475 aResp->res.jsonValue["@odata.type"] =
476 "#PCIeFunctionCollection.PCIeFunctionCollection";
477 aResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
478 "redfish", "v1", "Systems", "system", "PCIeDevices", pcieDeviceId,
479 "PCIeFunctions");
480 aResp->res.jsonValue["Name"] = "PCIe Function Collection";
481 aResp->res.jsonValue["Description"] =
482 "Collection of PCIe Functions for PCIe Device " + pcieDeviceId;
483 getPCIeDeviceProperties(
484 aResp, pcieDevicePath, service,
485 [aResp, pcieDeviceId](
486 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
487 addPCIeFunctionList(aResp->res, pcieDeviceId, pcieDevProperties);
488 });
489 });
490}
491
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700492inline void requestRoutesSystemPCIeFunctionCollection(App& app)
493{
494 /**
495 * Functions triggers appropriate requests on DBus
496 */
497 BMCWEB_ROUTE(app,
498 "/redfish/v1/Systems/system/PCIeDevices/<str>/PCIeFunctions/")
Ed Tanoused398212021-06-09 17:05:54 -0700499 .privileges(redfish::privileges::getPCIeFunctionCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -0700500 .methods(boost::beast::http::verb::get)(
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600501 std::bind_front(handlePCIeFunctionCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700502}
503
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600504inline bool validatePCIeFunctionId(
505 const std::string& pcieFunctionId,
506 const dbus::utility::DBusPropertiesMap& pcieDevProperties)
507{
508 std::string functionName = "Function" + pcieFunctionId;
509 std::string devIDProperty = functionName + "DeviceId";
510
511 const std::string* devIdProperty = nullptr;
512 for (const auto& property : pcieDevProperties)
513 {
514 if (property.first == devIDProperty)
515 {
516 devIdProperty = std::get_if<std::string>(&property.second);
517 break;
518 }
519 }
520 return (devIdProperty != nullptr && !devIdProperty->empty());
521}
522
523inline void addPCIeFunctionProperties(
524 crow::Response& resp, const std::string& pcieFunctionId,
525 const dbus::utility::DBusPropertiesMap& pcieDevProperties)
526{
527 std::string functionName = "Function" + pcieFunctionId;
528 if (!validatePCIeFunctionId(pcieFunctionId, pcieDevProperties))
529 {
530 messages::resourceNotFound(resp, "PCIeFunction", pcieFunctionId);
531 return;
532 }
533 for (const auto& property : pcieDevProperties)
534 {
535 const std::string* strProperty =
536 std::get_if<std::string>(&property.second);
537
538 if (property.first == functionName + "DeviceId")
539 {
540 resp.jsonValue["DeviceId"] = *strProperty;
541 }
542 if (property.first == functionName + "VendorId")
543 {
544 resp.jsonValue["VendorId"] = *strProperty;
545 }
546 // TODO: FunctionType and DeviceClass are Redfish enums. The D-Bus
547 // property strings should be mapped correctly to ensure these
548 // strings are Redfish enum values. For now just check for empty.
549 if (property.first == functionName + "FunctionType")
550 {
551 if (!strProperty->empty())
552 {
553 resp.jsonValue["FunctionType"] = *strProperty;
554 }
555 }
556 if (property.first == functionName + "DeviceClass")
557 {
558 if (!strProperty->empty())
559 {
560 resp.jsonValue["DeviceClass"] = *strProperty;
561 }
562 }
563 if (property.first == functionName + "ClassCode")
564 {
565 resp.jsonValue["ClassCode"] = *strProperty;
566 }
567 if (property.first == functionName + "RevisionId")
568 {
569 resp.jsonValue["RevisionId"] = *strProperty;
570 }
571 if (property.first == functionName + "SubsystemId")
572 {
573 resp.jsonValue["SubsystemId"] = *strProperty;
574 }
575 if (property.first == functionName + "SubsystemVendorId")
576 {
577 resp.jsonValue["SubsystemVendorId"] = *strProperty;
578 }
579 }
580}
581
582inline void addPCIeFunctionCommonProperties(crow::Response& resp,
583 const std::string& pcieDeviceId,
584 const std::string& pcieFunctionId)
585{
586 resp.addHeader(
587 boost::beast::http::field::link,
588 "</redfish/v1/JsonSchemas/PCIeFunction/PCIeFunction.json>; rel=describedby");
589 resp.jsonValue["@odata.type"] = "#PCIeFunction.v1_2_3.PCIeFunction";
590 resp.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
591 "redfish", "v1", "Systems", "system", "PCIeDevices", pcieDeviceId,
592 "PCIeFunctions", pcieFunctionId);
593 resp.jsonValue["Name"] = "PCIe Function";
594 resp.jsonValue["Id"] = pcieFunctionId;
595 resp.jsonValue["FunctionId"] = std::stoi(pcieFunctionId);
596 resp.jsonValue["Links"]["PCIeDevice"]["@odata.id"] =
597 crow::utility::urlFromPieces("redfish", "v1", "Systems", "system",
598 "PCIeDevices", pcieDeviceId);
599}
600
601inline void
602 handlePCIeFunctionGet(App& app, const crow::Request& req,
603 const std::shared_ptr<bmcweb::AsyncResp>& aResp,
604 const std::string& pcieDeviceId,
605 const std::string& pcieFunctionId)
606{
607 if (!redfish::setUpRedfishRoute(app, req, aResp))
608 {
609 return;
610 }
611
612 getValidPCIeDevicePath(
613 pcieDeviceId, aResp,
614 [aResp, pcieDeviceId, pcieFunctionId](const std::string& pcieDevicePath,
615 const std::string& service) {
616 getPCIeDeviceProperties(
617 aResp, pcieDevicePath, service,
618 [aResp, pcieDeviceId, pcieFunctionId](
619 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
620 addPCIeFunctionCommonProperties(aResp->res, pcieDeviceId,
621 pcieFunctionId);
622 addPCIeFunctionProperties(aResp->res, pcieFunctionId,
623 pcieDevProperties);
624 });
625 });
626}
627
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700628inline void requestRoutesSystemPCIeFunction(App& app)
629{
630 BMCWEB_ROUTE(
631 app,
632 "/redfish/v1/Systems/system/PCIeDevices/<str>/PCIeFunctions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700633 .privileges(redfish::privileges::getPCIeFunction)
Ed Tanous002d39b2022-05-31 08:59:27 -0700634 .methods(boost::beast::http::verb::get)(
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600635 std::bind_front(handlePCIeFunctionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700636}
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -0800637
638} // namespace redfish