blob: b5c6c15df113ce4a00d641cc5af7b320be8a6e05 [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 Tanous3ccb3ad2023-01-13 17:40:03 -080021#include "query.hpp"
22#include "registries/privilege_registry.hpp"
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -060023#include "utils/collection.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080024#include "utils/dbus_utils.hpp"
Lakshmi Yadlapatic49c3292023-04-19 16:42:35 -050025#include "utils/pcie_util.hpp"
Ed Tanous0ec8b832022-03-14 14:56:47 -070026
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -080027#include <boost/system/linux_error.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070028#include <boost/url/format.hpp>
Krzysztof Grobelnyd1bde9e2022-09-07 10:40:51 +020029#include <sdbusplus/asio/property.hpp>
30#include <sdbusplus/unpack_properties.hpp>
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -080031
32namespace redfish
33{
34
Patrick Williams89492a12023-05-10 07:51:34 -050035static constexpr const char* inventoryPath = "/xyz/openbmc_project/inventory";
Lakshmi Yadlapati94c3a102023-04-05 18:11:22 -050036static constexpr std::array<std::string_view, 1> pcieDeviceInterface = {
37 "xyz.openbmc_project.Inventory.Item.PCIeDevice"};
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -080038
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -060039static inline void handlePCIeDevicePath(
40 const std::string& pcieDeviceId,
Ed Tanousac106bf2023-06-07 09:24:59 -070041 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -060042 const dbus::utility::MapperGetSubTreePathsResponse& pcieDevicePaths,
43 const std::function<void(const std::string& pcieDevicePath,
44 const std::string& service)>& callback)
45
46{
47 for (const std::string& pcieDevicePath : pcieDevicePaths)
48 {
49 std::string pciecDeviceName =
50 sdbusplus::message::object_path(pcieDevicePath).filename();
51 if (pciecDeviceName.empty() || pciecDeviceName != pcieDeviceId)
52 {
53 continue;
54 }
55
56 dbus::utility::getDbusObject(
57 pcieDevicePath, {},
Ed Tanousac106bf2023-06-07 09:24:59 -070058 [pcieDevicePath, asyncResp,
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -060059 callback](const boost::system::error_code& ec,
60 const dbus::utility::MapperGetObject& object) {
61 if (ec || object.empty())
62 {
63 BMCWEB_LOG_ERROR << "DBUS response error " << ec;
Ed Tanousac106bf2023-06-07 09:24:59 -070064 messages::internalError(asyncResp->res);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -060065 return;
66 }
67 callback(pcieDevicePath, object.begin()->first);
68 });
69 return;
70 }
71
72 BMCWEB_LOG_WARNING << "PCIe Device not found";
Ed Tanousac106bf2023-06-07 09:24:59 -070073 messages::resourceNotFound(asyncResp->res, "PCIeDevice", pcieDeviceId);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -060074}
75
76static inline void getValidPCIeDevicePath(
77 const std::string& pcieDeviceId,
Ed Tanousac106bf2023-06-07 09:24:59 -070078 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -060079 const std::function<void(const std::string& pcieDevicePath,
80 const std::string& service)>& callback)
81{
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -060082 dbus::utility::getSubTreePaths(
Lakshmi Yadlapati94c3a102023-04-05 18:11:22 -050083 inventoryPath, 0, pcieDeviceInterface,
Ed Tanousac106bf2023-06-07 09:24:59 -070084 [pcieDeviceId, asyncResp,
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -060085 callback](const boost::system::error_code& ec,
86 const dbus::utility::MapperGetSubTreePathsResponse&
87 pcieDevicePaths) {
88 if (ec)
89 {
90 BMCWEB_LOG_ERROR << "D-Bus response error on GetSubTree " << ec;
Ed Tanousac106bf2023-06-07 09:24:59 -070091 messages::internalError(asyncResp->res);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -060092 return;
93 }
Ed Tanousac106bf2023-06-07 09:24:59 -070094 handlePCIeDevicePath(pcieDeviceId, asyncResp, pcieDevicePaths,
95 callback);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -060096 return;
97 });
98}
99
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600100static inline void handlePCIeDeviceCollectionGet(
101 crow::App& app, const crow::Request& req,
Ed Tanousac106bf2023-06-07 09:24:59 -0700102 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600103 const std::string& systemName)
104{
Ed Tanousac106bf2023-06-07 09:24:59 -0700105 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600106 {
107 return;
108 }
109 if (systemName != "system")
110 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700111 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
112 systemName);
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600113 return;
114 }
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600115
Ed Tanousac106bf2023-06-07 09:24:59 -0700116 asyncResp->res.addHeader(boost::beast::http::field::link,
117 "</redfish/v1/JsonSchemas/PCIeDeviceCollection/"
118 "PCIeDeviceCollection.json>; rel=describedby");
119 asyncResp->res.jsonValue["@odata.type"] =
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600120 "#PCIeDeviceCollection.PCIeDeviceCollection";
Ed Tanousac106bf2023-06-07 09:24:59 -0700121 asyncResp->res.jsonValue["@odata.id"] =
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600122 "/redfish/v1/Systems/system/PCIeDevices";
Ed Tanousac106bf2023-06-07 09:24:59 -0700123 asyncResp->res.jsonValue["Name"] = "PCIe Device Collection";
124 asyncResp->res.jsonValue["Description"] = "Collection of PCIe Devices";
125 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
126 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600127
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600128 collection_util::getCollectionMembers(
Ed Tanousac106bf2023-06-07 09:24:59 -0700129 asyncResp, boost::urls::url("/redfish/v1/Systems/system/PCIeDevices"),
Lakshmi Yadlapati94c3a102023-04-05 18:11:22 -0500130 pcieDeviceInterface);
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600131}
132
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700133inline void requestRoutesSystemPCIeDeviceCollection(App& app)
Jason M. Billsadbe1922019-10-14 15:44:35 -0700134{
Jason M. Billsadbe1922019-10-14 15:44:35 -0700135 /**
136 * Functions triggers appropriate requests on DBus
137 */
Ed Tanous22d268c2022-05-19 09:39:07 -0700138 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/PCIeDevices/")
Ed Tanoused398212021-06-09 17:05:54 -0700139 .privileges(redfish::privileges::getPCIeDeviceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700140 .methods(boost::beast::http::verb::get)(
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600141 std::bind_front(handlePCIeDeviceCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700142}
143
Ed Tanousac106bf2023-06-07 09:24:59 -0700144inline void
145 getPCIeDeviceState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
146 const std::string& pcieDevicePath,
147 const std::string& service)
Lakshmi Yadlapatic6bb3282023-04-12 16:56:49 -0500148{
149 sdbusplus::asio::getProperty<bool>(
150 *crow::connections::systemBus, service, pcieDevicePath,
151 "xyz.openbmc_project.Inventory.Item", "Present",
Ed Tanousac106bf2023-06-07 09:24:59 -0700152 [asyncResp](const boost::system::error_code& ec, const bool value) {
Lakshmi Yadlapatic6bb3282023-04-12 16:56:49 -0500153 if (ec)
154 {
155 if (ec.value() != EBADR)
156 {
157 BMCWEB_LOG_ERROR << "DBUS response error for State";
Ed Tanousac106bf2023-06-07 09:24:59 -0700158 messages::internalError(asyncResp->res);
Lakshmi Yadlapatic6bb3282023-04-12 16:56:49 -0500159 }
160 return;
161 }
162
163 if (!value)
164 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700165 asyncResp->res.jsonValue["Status"]["State"] = "Absent";
Lakshmi Yadlapatic6bb3282023-04-12 16:56:49 -0500166 }
167 });
168}
169
Ed Tanousac106bf2023-06-07 09:24:59 -0700170inline void
171 getPCIeDeviceAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
172 const std::string& pcieDevicePath,
173 const std::string& service)
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600174{
175 sdbusplus::asio::getAllProperties(
176 *crow::connections::systemBus, service, pcieDevicePath,
177 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanousac106bf2023-06-07 09:24:59 -0700178 [pcieDevicePath, asyncResp{asyncResp}](
179 const boost::system::error_code& ec,
180 const dbus::utility::DBusPropertiesMap& assetList) {
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600181 if (ec)
182 {
183 if (ec.value() != EBADR)
184 {
185 BMCWEB_LOG_ERROR << "DBUS response error for Properties"
186 << ec.value();
Ed Tanousac106bf2023-06-07 09:24:59 -0700187 messages::internalError(asyncResp->res);
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600188 }
189 return;
190 }
191
192 const std::string* manufacturer = nullptr;
193 const std::string* model = nullptr;
194 const std::string* partNumber = nullptr;
195 const std::string* serialNumber = nullptr;
196 const std::string* sparePartNumber = nullptr;
197
198 const bool success = sdbusplus::unpackPropertiesNoThrow(
199 dbus_utils::UnpackErrorPrinter(), assetList, "Manufacturer",
200 manufacturer, "Model", model, "PartNumber", partNumber,
201 "SerialNumber", serialNumber, "SparePartNumber", sparePartNumber);
202
203 if (!success)
204 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700205 messages::internalError(asyncResp->res);
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600206 return;
207 }
208
209 if (manufacturer != nullptr)
210 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700211 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600212 }
213 if (model != nullptr)
214 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700215 asyncResp->res.jsonValue["Model"] = *model;
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600216 }
217
218 if (partNumber != nullptr)
219 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700220 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600221 }
222
223 if (serialNumber != nullptr)
224 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700225 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600226 }
227
228 if (sparePartNumber != nullptr && !sparePartNumber->empty())
229 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700230 asyncResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600231 }
232 });
233}
234
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600235inline void addPCIeDeviceProperties(
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600236 crow::Response& resp, const std::string& pcieDeviceId,
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600237 const dbus::utility::DBusPropertiesMap& pcieDevProperties)
238{
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600239 const std::string* deviceType = nullptr;
240 const std::string* generationInUse = nullptr;
241 const int64_t* lanesInUse = nullptr;
242
243 const bool success = sdbusplus::unpackPropertiesNoThrow(
244 dbus_utils::UnpackErrorPrinter(), pcieDevProperties, "DeviceType",
245 deviceType, "GenerationInUse", generationInUse, "LanesInUse",
Lakshmi Yadlapatibad2c4a2023-04-07 09:14:37 -0500246 lanesInUse);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600247
248 if (!success)
249 {
250 messages::internalError(resp);
251 return;
252 }
253
254 if (deviceType != nullptr && !deviceType->empty())
255 {
256 resp.jsonValue["PCIeInterface"]["DeviceType"] = *deviceType;
257 }
258
259 if (generationInUse != nullptr)
260 {
261 std::optional<pcie_device::PCIeTypes> redfishGenerationInUse =
Lakshmi Yadlapatic49c3292023-04-19 16:42:35 -0500262 pcie_util::redfishPcieGenerationFromDbus(*generationInUse);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600263
264 if (!redfishGenerationInUse)
265 {
266 messages::internalError(resp);
267 return;
268 }
269 if (*redfishGenerationInUse != pcie_device::PCIeTypes::Invalid)
270 {
271 resp.jsonValue["PCIeInterface"]["PCIeType"] =
272 *redfishGenerationInUse;
273 }
274 }
275
276 // The default value of LanesInUse is 0, and the field will be
277 // left as off if it is a default value.
278 if (lanesInUse != nullptr && *lanesInUse != 0)
279 {
280 resp.jsonValue["PCIeInterface"]["LanesInUse"] = *lanesInUse;
281 }
282
Ed Tanousef4c65b2023-04-24 15:28:50 -0700283 resp.jsonValue["PCIeFunctions"]["@odata.id"] = boost::urls::format(
284 "/redfish/v1/Systems/system/PCIeDevices/{}/PCIeFunctions",
285 pcieDeviceId);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600286}
287
288inline void getPCIeDeviceProperties(
Ed Tanousac106bf2023-06-07 09:24:59 -0700289 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600290 const std::string& pcieDevicePath, const std::string& service,
291 const std::function<void(
292 const dbus::utility::DBusPropertiesMap& pcieDevProperties)>&& callback)
293{
294 sdbusplus::asio::getAllProperties(
295 *crow::connections::systemBus, service, pcieDevicePath,
296 "xyz.openbmc_project.Inventory.Item.PCIeDevice",
Ed Tanousac106bf2023-06-07 09:24:59 -0700297 [asyncResp,
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600298 callback](const boost::system::error_code& ec,
299 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
300 if (ec)
301 {
302 if (ec.value() != EBADR)
303 {
304 BMCWEB_LOG_ERROR << "DBUS response error for Properties";
Ed Tanousac106bf2023-06-07 09:24:59 -0700305 messages::internalError(asyncResp->res);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600306 }
307 return;
308 }
309 callback(pcieDevProperties);
310 });
311}
312
313inline void addPCIeDeviceCommonProperties(
Ed Tanousac106bf2023-06-07 09:24:59 -0700314 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600315 const std::string& pcieDeviceId)
316{
Ed Tanousac106bf2023-06-07 09:24:59 -0700317 asyncResp->res.addHeader(
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600318 boost::beast::http::field::link,
319 "</redfish/v1/JsonSchemas/PCIeDevice/PCIeDevice.json>; rel=describedby");
Ed Tanousac106bf2023-06-07 09:24:59 -0700320 asyncResp->res.jsonValue["@odata.type"] = "#PCIeDevice.v1_9_0.PCIeDevice";
321 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
Ed Tanousef4c65b2023-04-24 15:28:50 -0700322 "/redfish/v1/Systems/system/PCIeDevices/{}", pcieDeviceId);
Ed Tanousac106bf2023-06-07 09:24:59 -0700323 asyncResp->res.jsonValue["Name"] = "PCIe Device";
324 asyncResp->res.jsonValue["Id"] = pcieDeviceId;
325 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600326}
327
Ed Tanousac106bf2023-06-07 09:24:59 -0700328inline void
329 handlePCIeDeviceGet(App& app, const crow::Request& req,
330 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
331 const std::string& systemName,
332 const std::string& pcieDeviceId)
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600333{
Ed Tanousac106bf2023-06-07 09:24:59 -0700334 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600335 {
336 return;
337 }
338 if (systemName != "system")
339 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700340 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
341 systemName);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600342 return;
343 }
344
345 getValidPCIeDevicePath(
Ed Tanousac106bf2023-06-07 09:24:59 -0700346 pcieDeviceId, asyncResp,
347 [asyncResp, pcieDeviceId](const std::string& pcieDevicePath,
348 const std::string& service) {
349 addPCIeDeviceCommonProperties(asyncResp, pcieDeviceId);
350 getPCIeDeviceAsset(asyncResp, pcieDevicePath, service);
351 getPCIeDeviceState(asyncResp, pcieDevicePath, service);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600352 getPCIeDeviceProperties(
Ed Tanousac106bf2023-06-07 09:24:59 -0700353 asyncResp, pcieDevicePath, service,
354 [asyncResp, pcieDeviceId](
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600355 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
Ed Tanousac106bf2023-06-07 09:24:59 -0700356 addPCIeDeviceProperties(asyncResp->res, pcieDeviceId,
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600357 pcieDevProperties);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600358 });
359 });
360}
361
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700362inline void requestRoutesSystemPCIeDevice(App& app)
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -0800363{
Ed Tanous22d268c2022-05-19 09:39:07 -0700364 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/PCIeDevices/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700365 .privileges(redfish::privileges::getPCIeDevice)
Ed Tanous002d39b2022-05-31 08:59:27 -0700366 .methods(boost::beast::http::verb::get)(
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600367 std::bind_front(handlePCIeDeviceGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700368}
369
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600370inline void addPCIeFunctionList(
371 crow::Response& res, const std::string& pcieDeviceId,
372 const dbus::utility::DBusPropertiesMap& pcieDevProperties)
373{
374 nlohmann::json& pcieFunctionList = res.jsonValue["Members"];
375 pcieFunctionList = nlohmann::json::array();
376 static constexpr const int maxPciFunctionNum = 8;
377
378 for (int functionNum = 0; functionNum < maxPciFunctionNum; functionNum++)
379 {
380 // Check if this function exists by
381 // looking for a device ID
Patrick Williams89492a12023-05-10 07:51:34 -0500382 std::string devIDProperty = "Function" + std::to_string(functionNum) +
383 "DeviceId";
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600384 const std::string* property = nullptr;
385 for (const auto& propEntry : pcieDevProperties)
386 {
387 if (propEntry.first == devIDProperty)
388 {
389 property = std::get_if<std::string>(&propEntry.second);
390 break;
391 }
392 }
393 if (property == nullptr || property->empty())
394 {
395 continue;
396 }
397
398 nlohmann::json::object_t pcieFunction;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700399 pcieFunction["@odata.id"] = boost::urls::format(
400 "/redfish/v1/Systems/system/PCIeDevices/{}/PCIeFunctions/{}",
401 pcieDeviceId, std::to_string(functionNum));
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500402 pcieFunctionList.emplace_back(std::move(pcieFunction));
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600403 }
404 res.jsonValue["PCIeFunctions@odata.count"] = pcieFunctionList.size();
405}
406
407inline void handlePCIeFunctionCollectionGet(
408 App& app, const crow::Request& req,
Ed Tanousac106bf2023-06-07 09:24:59 -0700409 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600410 const std::string& pcieDeviceId)
411{
Ed Tanousac106bf2023-06-07 09:24:59 -0700412 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600413 {
414 return;
415 }
416
417 getValidPCIeDevicePath(
Ed Tanousac106bf2023-06-07 09:24:59 -0700418 pcieDeviceId, asyncResp,
419 [asyncResp, pcieDeviceId](const std::string& pcieDevicePath,
420 const std::string& service) {
421 asyncResp->res.addHeader(
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600422 boost::beast::http::field::link,
423 "</redfish/v1/JsonSchemas/PCIeFunctionCollection/PCIeFunctionCollection.json>; rel=describedby");
Ed Tanousac106bf2023-06-07 09:24:59 -0700424 asyncResp->res.jsonValue["@odata.type"] =
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600425 "#PCIeFunctionCollection.PCIeFunctionCollection";
Ed Tanousac106bf2023-06-07 09:24:59 -0700426 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
Ed Tanousef4c65b2023-04-24 15:28:50 -0700427 "/redfish/v1/Systems/system/PCIeDevices/{}/PCIeFunctions",
428 pcieDeviceId);
Ed Tanousac106bf2023-06-07 09:24:59 -0700429 asyncResp->res.jsonValue["Name"] = "PCIe Function Collection";
430 asyncResp->res.jsonValue["Description"] =
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600431 "Collection of PCIe Functions for PCIe Device " + pcieDeviceId;
432 getPCIeDeviceProperties(
Ed Tanousac106bf2023-06-07 09:24:59 -0700433 asyncResp, pcieDevicePath, service,
434 [asyncResp, pcieDeviceId](
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600435 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
Ed Tanousac106bf2023-06-07 09:24:59 -0700436 addPCIeFunctionList(asyncResp->res, pcieDeviceId,
437 pcieDevProperties);
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600438 });
439 });
440}
441
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700442inline void requestRoutesSystemPCIeFunctionCollection(App& app)
443{
444 /**
445 * Functions triggers appropriate requests on DBus
446 */
447 BMCWEB_ROUTE(app,
448 "/redfish/v1/Systems/system/PCIeDevices/<str>/PCIeFunctions/")
Ed Tanoused398212021-06-09 17:05:54 -0700449 .privileges(redfish::privileges::getPCIeFunctionCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -0700450 .methods(boost::beast::http::verb::get)(
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600451 std::bind_front(handlePCIeFunctionCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700452}
453
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600454inline bool validatePCIeFunctionId(
Myung Baed5e74b82023-05-31 11:28:02 -0500455 uint64_t pcieFunctionId,
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600456 const dbus::utility::DBusPropertiesMap& pcieDevProperties)
457{
Myung Baed5e74b82023-05-31 11:28:02 -0500458 std::string functionName = "Function" + std::to_string(pcieFunctionId);
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600459 std::string devIDProperty = functionName + "DeviceId";
460
461 const std::string* devIdProperty = nullptr;
462 for (const auto& property : pcieDevProperties)
463 {
464 if (property.first == devIDProperty)
465 {
466 devIdProperty = std::get_if<std::string>(&property.second);
467 break;
468 }
469 }
470 return (devIdProperty != nullptr && !devIdProperty->empty());
471}
472
473inline void addPCIeFunctionProperties(
Ed Tanouse14742c2023-05-31 10:27:49 -0700474 crow::Response& resp, uint64_t pcieFunctionId,
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600475 const dbus::utility::DBusPropertiesMap& pcieDevProperties)
476{
Ed Tanouse14742c2023-05-31 10:27:49 -0700477 std::string functionName = "Function" + std::to_string(pcieFunctionId);
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600478 for (const auto& property : pcieDevProperties)
479 {
480 const std::string* strProperty =
481 std::get_if<std::string>(&property.second);
482
483 if (property.first == functionName + "DeviceId")
484 {
485 resp.jsonValue["DeviceId"] = *strProperty;
486 }
487 if (property.first == functionName + "VendorId")
488 {
489 resp.jsonValue["VendorId"] = *strProperty;
490 }
491 // TODO: FunctionType and DeviceClass are Redfish enums. The D-Bus
492 // property strings should be mapped correctly to ensure these
493 // strings are Redfish enum values. For now just check for empty.
494 if (property.first == functionName + "FunctionType")
495 {
496 if (!strProperty->empty())
497 {
498 resp.jsonValue["FunctionType"] = *strProperty;
499 }
500 }
501 if (property.first == functionName + "DeviceClass")
502 {
503 if (!strProperty->empty())
504 {
505 resp.jsonValue["DeviceClass"] = *strProperty;
506 }
507 }
508 if (property.first == functionName + "ClassCode")
509 {
510 resp.jsonValue["ClassCode"] = *strProperty;
511 }
512 if (property.first == functionName + "RevisionId")
513 {
514 resp.jsonValue["RevisionId"] = *strProperty;
515 }
516 if (property.first == functionName + "SubsystemId")
517 {
518 resp.jsonValue["SubsystemId"] = *strProperty;
519 }
520 if (property.first == functionName + "SubsystemVendorId")
521 {
522 resp.jsonValue["SubsystemVendorId"] = *strProperty;
523 }
524 }
525}
526
527inline void addPCIeFunctionCommonProperties(crow::Response& resp,
528 const std::string& pcieDeviceId,
Ed Tanouse14742c2023-05-31 10:27:49 -0700529 uint64_t pcieFunctionId)
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600530{
531 resp.addHeader(
532 boost::beast::http::field::link,
533 "</redfish/v1/JsonSchemas/PCIeFunction/PCIeFunction.json>; rel=describedby");
534 resp.jsonValue["@odata.type"] = "#PCIeFunction.v1_2_3.PCIeFunction";
Ed Tanousef4c65b2023-04-24 15:28:50 -0700535 resp.jsonValue["@odata.id"] = boost::urls::format(
536 "/redfish/v1/Systems/system/PCIeDevices/{}/PCIeFunctions/{}",
537 pcieDeviceId, pcieFunctionId);
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600538 resp.jsonValue["Name"] = "PCIe Function";
Ed Tanouse14742c2023-05-31 10:27:49 -0700539 resp.jsonValue["Id"] = std::to_string(pcieFunctionId);
540 resp.jsonValue["FunctionId"] = pcieFunctionId;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700541 resp.jsonValue["Links"]["PCIeDevice"]["@odata.id"] = boost::urls::format(
542 "/redfish/v1/Systems/system/PCIeDevices/{}", pcieDeviceId);
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600543}
544
545inline void
546 handlePCIeFunctionGet(App& app, const crow::Request& req,
Ed Tanousac106bf2023-06-07 09:24:59 -0700547 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600548 const std::string& pcieDeviceId,
Ed Tanouse14742c2023-05-31 10:27:49 -0700549 const std::string& pcieFunctionIdStr)
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600550{
Ed Tanousac106bf2023-06-07 09:24:59 -0700551 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600552 {
553 return;
554 }
Ed Tanouse14742c2023-05-31 10:27:49 -0700555 uint64_t pcieFunctionId = 0;
556 std::from_chars_result result = std::from_chars(
557 &*pcieFunctionIdStr.begin(), &*pcieFunctionIdStr.end(), pcieFunctionId);
558 if (result.ec != std::errc{} || result.ptr != &*pcieFunctionIdStr.end())
559 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700560 messages::resourceNotFound(asyncResp->res, "PCIeFunction",
Ed Tanouse14742c2023-05-31 10:27:49 -0700561 pcieFunctionIdStr);
562 return;
563 }
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600564
Ed Tanousac106bf2023-06-07 09:24:59 -0700565 getValidPCIeDevicePath(pcieDeviceId, asyncResp,
566 [asyncResp, pcieDeviceId,
567 pcieFunctionId](const std::string& pcieDevicePath,
568 const std::string& service) {
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600569 getPCIeDeviceProperties(
Ed Tanousac106bf2023-06-07 09:24:59 -0700570 asyncResp, pcieDevicePath, service,
571 [asyncResp, pcieDeviceId, pcieFunctionId](
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600572 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
Ed Tanousac106bf2023-06-07 09:24:59 -0700573 addPCIeFunctionCommonProperties(asyncResp->res, pcieDeviceId,
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600574 pcieFunctionId);
Ed Tanousac106bf2023-06-07 09:24:59 -0700575 addPCIeFunctionProperties(asyncResp->res, pcieFunctionId,
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600576 pcieDevProperties);
577 });
Ed Tanousac106bf2023-06-07 09:24:59 -0700578 });
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600579}
580
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700581inline void requestRoutesSystemPCIeFunction(App& app)
582{
583 BMCWEB_ROUTE(
584 app,
585 "/redfish/v1/Systems/system/PCIeDevices/<str>/PCIeFunctions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700586 .privileges(redfish::privileges::getPCIeFunction)
Ed Tanous002d39b2022-05-31 08:59:27 -0700587 .methods(boost::beast::http::verb::get)(
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600588 std::bind_front(handlePCIeFunctionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700589}
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -0800590
591} // namespace redfish