blob: b63c850479eef4accf63a407c0f9f9526d6041e9 [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 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800109 if constexpr (bmcwebEnableMultiHost)
110 {
111 // Option currently returns no systems. TBD
112 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
113 systemName);
114 return;
115 }
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600116 if (systemName != "system")
117 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700118 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
119 systemName);
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600120 return;
121 }
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600122
Ed Tanousac106bf2023-06-07 09:24:59 -0700123 asyncResp->res.addHeader(boost::beast::http::field::link,
124 "</redfish/v1/JsonSchemas/PCIeDeviceCollection/"
125 "PCIeDeviceCollection.json>; rel=describedby");
126 asyncResp->res.jsonValue["@odata.type"] =
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600127 "#PCIeDeviceCollection.PCIeDeviceCollection";
Ed Tanousac106bf2023-06-07 09:24:59 -0700128 asyncResp->res.jsonValue["@odata.id"] =
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600129 "/redfish/v1/Systems/system/PCIeDevices";
Ed Tanousac106bf2023-06-07 09:24:59 -0700130 asyncResp->res.jsonValue["Name"] = "PCIe Device Collection";
131 asyncResp->res.jsonValue["Description"] = "Collection of PCIe Devices";
132 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
133 asyncResp->res.jsonValue["Members@odata.count"] = 0;
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600134
Lakshmi Yadlapati9e9325e2023-05-02 10:30:44 -0500135 pcie_util::getPCIeDeviceList(asyncResp, "Members");
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600136}
137
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700138inline void requestRoutesSystemPCIeDeviceCollection(App& app)
Jason M. Billsadbe1922019-10-14 15:44:35 -0700139{
Jason M. Billsadbe1922019-10-14 15:44:35 -0700140 /**
141 * Functions triggers appropriate requests on DBus
142 */
Ed Tanous22d268c2022-05-19 09:39:07 -0700143 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/PCIeDevices/")
Ed Tanoused398212021-06-09 17:05:54 -0700144 .privileges(redfish::privileges::getPCIeDeviceCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700145 .methods(boost::beast::http::verb::get)(
Lakshmi Yadlapatib38fa2a2023-03-10 16:19:46 -0600146 std::bind_front(handlePCIeDeviceCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700147}
148
Ed Tanousac106bf2023-06-07 09:24:59 -0700149inline void
Lakshmi Yadlapatie164f1b2023-04-12 17:01:34 -0500150 getPCIeDeviceHealth(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
151 const std::string& pcieDevicePath,
152 const std::string& service)
153{
154 sdbusplus::asio::getProperty<bool>(
155 *crow::connections::systemBus, service, pcieDevicePath,
156 "xyz.openbmc_project.State.Decorator.OperationalStatus", "Functional",
157 [asyncResp](const boost::system::error_code& ec, const bool value) {
158 if (ec)
159 {
160 if (ec.value() != EBADR)
161 {
162 BMCWEB_LOG_ERROR << "DBUS response error for Health "
163 << ec.value();
164 messages::internalError(asyncResp->res);
165 }
166 return;
167 }
168
169 if (!value)
170 {
171 asyncResp->res.jsonValue["Status"]["Health"] = "Critical";
172 }
173 });
174}
175
176inline void
Ed Tanousac106bf2023-06-07 09:24:59 -0700177 getPCIeDeviceState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
178 const std::string& pcieDevicePath,
179 const std::string& service)
Lakshmi Yadlapatic6bb3282023-04-12 16:56:49 -0500180{
181 sdbusplus::asio::getProperty<bool>(
182 *crow::connections::systemBus, service, pcieDevicePath,
183 "xyz.openbmc_project.Inventory.Item", "Present",
Ed Tanousac106bf2023-06-07 09:24:59 -0700184 [asyncResp](const boost::system::error_code& ec, const bool value) {
Lakshmi Yadlapatic6bb3282023-04-12 16:56:49 -0500185 if (ec)
186 {
187 if (ec.value() != EBADR)
188 {
189 BMCWEB_LOG_ERROR << "DBUS response error for State";
Ed Tanousac106bf2023-06-07 09:24:59 -0700190 messages::internalError(asyncResp->res);
Lakshmi Yadlapatic6bb3282023-04-12 16:56:49 -0500191 }
192 return;
193 }
194
195 if (!value)
196 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700197 asyncResp->res.jsonValue["Status"]["State"] = "Absent";
Lakshmi Yadlapatic6bb3282023-04-12 16:56:49 -0500198 }
199 });
200}
201
Ed Tanousac106bf2023-06-07 09:24:59 -0700202inline void
203 getPCIeDeviceAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
204 const std::string& pcieDevicePath,
205 const std::string& service)
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600206{
207 sdbusplus::asio::getAllProperties(
208 *crow::connections::systemBus, service, pcieDevicePath,
209 "xyz.openbmc_project.Inventory.Decorator.Asset",
Ed Tanousac106bf2023-06-07 09:24:59 -0700210 [pcieDevicePath, asyncResp{asyncResp}](
211 const boost::system::error_code& ec,
212 const dbus::utility::DBusPropertiesMap& assetList) {
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600213 if (ec)
214 {
215 if (ec.value() != EBADR)
216 {
217 BMCWEB_LOG_ERROR << "DBUS response error for Properties"
218 << ec.value();
Ed Tanousac106bf2023-06-07 09:24:59 -0700219 messages::internalError(asyncResp->res);
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600220 }
221 return;
222 }
223
224 const std::string* manufacturer = nullptr;
225 const std::string* model = nullptr;
226 const std::string* partNumber = nullptr;
227 const std::string* serialNumber = nullptr;
228 const std::string* sparePartNumber = nullptr;
229
230 const bool success = sdbusplus::unpackPropertiesNoThrow(
231 dbus_utils::UnpackErrorPrinter(), assetList, "Manufacturer",
232 manufacturer, "Model", model, "PartNumber", partNumber,
233 "SerialNumber", serialNumber, "SparePartNumber", sparePartNumber);
234
235 if (!success)
236 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700237 messages::internalError(asyncResp->res);
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600238 return;
239 }
240
241 if (manufacturer != nullptr)
242 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700243 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600244 }
245 if (model != nullptr)
246 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700247 asyncResp->res.jsonValue["Model"] = *model;
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600248 }
249
250 if (partNumber != nullptr)
251 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700252 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600253 }
254
255 if (serialNumber != nullptr)
256 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700257 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600258 }
259
260 if (sparePartNumber != nullptr && !sparePartNumber->empty())
261 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700262 asyncResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
SunnySrivastava1984913e7732021-01-27 06:23:24 -0600263 }
264 });
265}
266
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600267inline void addPCIeDeviceProperties(
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600268 crow::Response& resp, const std::string& pcieDeviceId,
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600269 const dbus::utility::DBusPropertiesMap& pcieDevProperties)
270{
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600271 const std::string* deviceType = nullptr;
272 const std::string* generationInUse = nullptr;
273 const int64_t* lanesInUse = nullptr;
274
275 const bool success = sdbusplus::unpackPropertiesNoThrow(
276 dbus_utils::UnpackErrorPrinter(), pcieDevProperties, "DeviceType",
277 deviceType, "GenerationInUse", generationInUse, "LanesInUse",
Lakshmi Yadlapatibad2c4a2023-04-07 09:14:37 -0500278 lanesInUse);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600279
280 if (!success)
281 {
282 messages::internalError(resp);
283 return;
284 }
285
286 if (deviceType != nullptr && !deviceType->empty())
287 {
288 resp.jsonValue["PCIeInterface"]["DeviceType"] = *deviceType;
289 }
290
291 if (generationInUse != nullptr)
292 {
293 std::optional<pcie_device::PCIeTypes> redfishGenerationInUse =
Lakshmi Yadlapatic49c3292023-04-19 16:42:35 -0500294 pcie_util::redfishPcieGenerationFromDbus(*generationInUse);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600295
296 if (!redfishGenerationInUse)
297 {
298 messages::internalError(resp);
299 return;
300 }
301 if (*redfishGenerationInUse != pcie_device::PCIeTypes::Invalid)
302 {
303 resp.jsonValue["PCIeInterface"]["PCIeType"] =
304 *redfishGenerationInUse;
305 }
306 }
307
308 // The default value of LanesInUse is 0, and the field will be
309 // left as off if it is a default value.
310 if (lanesInUse != nullptr && *lanesInUse != 0)
311 {
312 resp.jsonValue["PCIeInterface"]["LanesInUse"] = *lanesInUse;
313 }
314
Ed Tanousef4c65b2023-04-24 15:28:50 -0700315 resp.jsonValue["PCIeFunctions"]["@odata.id"] = boost::urls::format(
316 "/redfish/v1/Systems/system/PCIeDevices/{}/PCIeFunctions",
317 pcieDeviceId);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600318}
319
320inline void getPCIeDeviceProperties(
Ed Tanousac106bf2023-06-07 09:24:59 -0700321 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600322 const std::string& pcieDevicePath, const std::string& service,
323 const std::function<void(
324 const dbus::utility::DBusPropertiesMap& pcieDevProperties)>&& callback)
325{
326 sdbusplus::asio::getAllProperties(
327 *crow::connections::systemBus, service, pcieDevicePath,
328 "xyz.openbmc_project.Inventory.Item.PCIeDevice",
Ed Tanousac106bf2023-06-07 09:24:59 -0700329 [asyncResp,
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600330 callback](const boost::system::error_code& ec,
331 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
332 if (ec)
333 {
334 if (ec.value() != EBADR)
335 {
336 BMCWEB_LOG_ERROR << "DBUS response error for Properties";
Ed Tanousac106bf2023-06-07 09:24:59 -0700337 messages::internalError(asyncResp->res);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600338 }
339 return;
340 }
341 callback(pcieDevProperties);
342 });
343}
344
345inline void addPCIeDeviceCommonProperties(
Ed Tanousac106bf2023-06-07 09:24:59 -0700346 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600347 const std::string& pcieDeviceId)
348{
Ed Tanousac106bf2023-06-07 09:24:59 -0700349 asyncResp->res.addHeader(
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600350 boost::beast::http::field::link,
351 "</redfish/v1/JsonSchemas/PCIeDevice/PCIeDevice.json>; rel=describedby");
Ed Tanousac106bf2023-06-07 09:24:59 -0700352 asyncResp->res.jsonValue["@odata.type"] = "#PCIeDevice.v1_9_0.PCIeDevice";
353 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
Ed Tanousef4c65b2023-04-24 15:28:50 -0700354 "/redfish/v1/Systems/system/PCIeDevices/{}", pcieDeviceId);
Ed Tanousac106bf2023-06-07 09:24:59 -0700355 asyncResp->res.jsonValue["Name"] = "PCIe Device";
356 asyncResp->res.jsonValue["Id"] = pcieDeviceId;
357 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
Lakshmi Yadlapatie164f1b2023-04-12 17:01:34 -0500358 asyncResp->res.jsonValue["Status"]["Health"] = "OK";
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600359}
360
Ed Tanousac106bf2023-06-07 09:24:59 -0700361inline void
362 handlePCIeDeviceGet(App& app, const crow::Request& req,
363 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
364 const std::string& systemName,
365 const std::string& pcieDeviceId)
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600366{
Ed Tanousac106bf2023-06-07 09:24:59 -0700367 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600368 {
369 return;
370 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800371 if constexpr (bmcwebEnableMultiHost)
372 {
373 // Option currently returns no systems. TBD
374 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
375 systemName);
376 return;
377 }
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600378 if (systemName != "system")
379 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700380 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
381 systemName);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600382 return;
383 }
384
385 getValidPCIeDevicePath(
Ed Tanousac106bf2023-06-07 09:24:59 -0700386 pcieDeviceId, asyncResp,
387 [asyncResp, pcieDeviceId](const std::string& pcieDevicePath,
388 const std::string& service) {
389 addPCIeDeviceCommonProperties(asyncResp, pcieDeviceId);
390 getPCIeDeviceAsset(asyncResp, pcieDevicePath, service);
391 getPCIeDeviceState(asyncResp, pcieDevicePath, service);
Lakshmi Yadlapatie164f1b2023-04-12 17:01:34 -0500392 getPCIeDeviceHealth(asyncResp, pcieDevicePath, service);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600393 getPCIeDeviceProperties(
Ed Tanousac106bf2023-06-07 09:24:59 -0700394 asyncResp, pcieDevicePath, service,
395 [asyncResp, pcieDeviceId](
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600396 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
Ed Tanousac106bf2023-06-07 09:24:59 -0700397 addPCIeDeviceProperties(asyncResp->res, pcieDeviceId,
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600398 pcieDevProperties);
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600399 });
400 });
401}
402
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700403inline void requestRoutesSystemPCIeDevice(App& app)
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -0800404{
Ed Tanous22d268c2022-05-19 09:39:07 -0700405 BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/PCIeDevices/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700406 .privileges(redfish::privileges::getPCIeDevice)
Ed Tanous002d39b2022-05-31 08:59:27 -0700407 .methods(boost::beast::http::verb::get)(
Lakshmi Yadlapati543f9a72023-03-10 17:06:05 -0600408 std::bind_front(handlePCIeDeviceGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700409}
410
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600411inline void addPCIeFunctionList(
412 crow::Response& res, const std::string& pcieDeviceId,
413 const dbus::utility::DBusPropertiesMap& pcieDevProperties)
414{
415 nlohmann::json& pcieFunctionList = res.jsonValue["Members"];
416 pcieFunctionList = nlohmann::json::array();
417 static constexpr const int maxPciFunctionNum = 8;
418
419 for (int functionNum = 0; functionNum < maxPciFunctionNum; functionNum++)
420 {
421 // Check if this function exists by
422 // looking for a device ID
Patrick Williams89492a12023-05-10 07:51:34 -0500423 std::string devIDProperty = "Function" + std::to_string(functionNum) +
424 "DeviceId";
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600425 const std::string* property = nullptr;
426 for (const auto& propEntry : pcieDevProperties)
427 {
428 if (propEntry.first == devIDProperty)
429 {
430 property = std::get_if<std::string>(&propEntry.second);
431 break;
432 }
433 }
434 if (property == nullptr || property->empty())
435 {
436 continue;
437 }
438
439 nlohmann::json::object_t pcieFunction;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700440 pcieFunction["@odata.id"] = boost::urls::format(
441 "/redfish/v1/Systems/system/PCIeDevices/{}/PCIeFunctions/{}",
442 pcieDeviceId, std::to_string(functionNum));
Patrick Williamsb2ba3072023-05-12 10:27:39 -0500443 pcieFunctionList.emplace_back(std::move(pcieFunction));
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600444 }
445 res.jsonValue["PCIeFunctions@odata.count"] = pcieFunctionList.size();
446}
447
448inline void handlePCIeFunctionCollectionGet(
449 App& app, const crow::Request& req,
Ed Tanousac106bf2023-06-07 09:24:59 -0700450 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800451 const std::string& systemName, const std::string& pcieDeviceId)
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600452{
Ed Tanousac106bf2023-06-07 09:24:59 -0700453 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600454 {
455 return;
456 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800457 if constexpr (bmcwebEnableMultiHost)
458 {
459 // Option currently returns no systems. TBD
460 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
461 systemName);
462 return;
463 }
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600464
465 getValidPCIeDevicePath(
Ed Tanousac106bf2023-06-07 09:24:59 -0700466 pcieDeviceId, asyncResp,
467 [asyncResp, pcieDeviceId](const std::string& pcieDevicePath,
468 const std::string& service) {
469 asyncResp->res.addHeader(
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600470 boost::beast::http::field::link,
471 "</redfish/v1/JsonSchemas/PCIeFunctionCollection/PCIeFunctionCollection.json>; rel=describedby");
Ed Tanousac106bf2023-06-07 09:24:59 -0700472 asyncResp->res.jsonValue["@odata.type"] =
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600473 "#PCIeFunctionCollection.PCIeFunctionCollection";
Ed Tanousac106bf2023-06-07 09:24:59 -0700474 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
Ed Tanousef4c65b2023-04-24 15:28:50 -0700475 "/redfish/v1/Systems/system/PCIeDevices/{}/PCIeFunctions",
476 pcieDeviceId);
Ed Tanousac106bf2023-06-07 09:24:59 -0700477 asyncResp->res.jsonValue["Name"] = "PCIe Function Collection";
478 asyncResp->res.jsonValue["Description"] =
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600479 "Collection of PCIe Functions for PCIe Device " + pcieDeviceId;
480 getPCIeDeviceProperties(
Ed Tanousac106bf2023-06-07 09:24:59 -0700481 asyncResp, pcieDevicePath, service,
482 [asyncResp, pcieDeviceId](
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600483 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
Ed Tanousac106bf2023-06-07 09:24:59 -0700484 addPCIeFunctionList(asyncResp->res, pcieDeviceId,
485 pcieDevProperties);
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600486 });
487 });
488}
489
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700490inline void requestRoutesSystemPCIeFunctionCollection(App& app)
491{
492 /**
493 * Functions triggers appropriate requests on DBus
494 */
495 BMCWEB_ROUTE(app,
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800496 "/redfish/v1/Systems/<str>/PCIeDevices/<str>/PCIeFunctions/")
Ed Tanoused398212021-06-09 17:05:54 -0700497 .privileges(redfish::privileges::getPCIeFunctionCollection)
Ed Tanous002d39b2022-05-31 08:59:27 -0700498 .methods(boost::beast::http::verb::get)(
Lakshmi Yadlapati35ad6132023-03-10 22:31:49 -0600499 std::bind_front(handlePCIeFunctionCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700500}
501
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600502inline bool validatePCIeFunctionId(
Myung Baed5e74b82023-05-31 11:28:02 -0500503 uint64_t pcieFunctionId,
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600504 const dbus::utility::DBusPropertiesMap& pcieDevProperties)
505{
Myung Baed5e74b82023-05-31 11:28:02 -0500506 std::string functionName = "Function" + std::to_string(pcieFunctionId);
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600507 std::string devIDProperty = functionName + "DeviceId";
508
509 const std::string* devIdProperty = nullptr;
510 for (const auto& property : pcieDevProperties)
511 {
512 if (property.first == devIDProperty)
513 {
514 devIdProperty = std::get_if<std::string>(&property.second);
515 break;
516 }
517 }
518 return (devIdProperty != nullptr && !devIdProperty->empty());
519}
520
521inline void addPCIeFunctionProperties(
Ed Tanouse14742c2023-05-31 10:27:49 -0700522 crow::Response& resp, uint64_t pcieFunctionId,
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600523 const dbus::utility::DBusPropertiesMap& pcieDevProperties)
524{
Ed Tanouse14742c2023-05-31 10:27:49 -0700525 std::string functionName = "Function" + std::to_string(pcieFunctionId);
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600526 for (const auto& property : pcieDevProperties)
527 {
528 const std::string* strProperty =
529 std::get_if<std::string>(&property.second);
530
531 if (property.first == functionName + "DeviceId")
532 {
533 resp.jsonValue["DeviceId"] = *strProperty;
534 }
535 if (property.first == functionName + "VendorId")
536 {
537 resp.jsonValue["VendorId"] = *strProperty;
538 }
539 // TODO: FunctionType and DeviceClass are Redfish enums. The D-Bus
540 // property strings should be mapped correctly to ensure these
541 // strings are Redfish enum values. For now just check for empty.
542 if (property.first == functionName + "FunctionType")
543 {
544 if (!strProperty->empty())
545 {
546 resp.jsonValue["FunctionType"] = *strProperty;
547 }
548 }
549 if (property.first == functionName + "DeviceClass")
550 {
551 if (!strProperty->empty())
552 {
553 resp.jsonValue["DeviceClass"] = *strProperty;
554 }
555 }
556 if (property.first == functionName + "ClassCode")
557 {
558 resp.jsonValue["ClassCode"] = *strProperty;
559 }
560 if (property.first == functionName + "RevisionId")
561 {
562 resp.jsonValue["RevisionId"] = *strProperty;
563 }
564 if (property.first == functionName + "SubsystemId")
565 {
566 resp.jsonValue["SubsystemId"] = *strProperty;
567 }
568 if (property.first == functionName + "SubsystemVendorId")
569 {
570 resp.jsonValue["SubsystemVendorId"] = *strProperty;
571 }
572 }
573}
574
575inline void addPCIeFunctionCommonProperties(crow::Response& resp,
576 const std::string& pcieDeviceId,
Ed Tanouse14742c2023-05-31 10:27:49 -0700577 uint64_t pcieFunctionId)
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600578{
579 resp.addHeader(
580 boost::beast::http::field::link,
581 "</redfish/v1/JsonSchemas/PCIeFunction/PCIeFunction.json>; rel=describedby");
582 resp.jsonValue["@odata.type"] = "#PCIeFunction.v1_2_3.PCIeFunction";
Ed Tanousef4c65b2023-04-24 15:28:50 -0700583 resp.jsonValue["@odata.id"] = boost::urls::format(
584 "/redfish/v1/Systems/system/PCIeDevices/{}/PCIeFunctions/{}",
Lakshmi Yadlapati768a1432023-06-14 12:45:54 -0500585 pcieDeviceId, std::to_string(pcieFunctionId));
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600586 resp.jsonValue["Name"] = "PCIe Function";
Ed Tanouse14742c2023-05-31 10:27:49 -0700587 resp.jsonValue["Id"] = std::to_string(pcieFunctionId);
588 resp.jsonValue["FunctionId"] = pcieFunctionId;
Ed Tanousef4c65b2023-04-24 15:28:50 -0700589 resp.jsonValue["Links"]["PCIeDevice"]["@odata.id"] = boost::urls::format(
590 "/redfish/v1/Systems/system/PCIeDevices/{}", pcieDeviceId);
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600591}
592
593inline void
594 handlePCIeFunctionGet(App& app, const crow::Request& req,
Ed Tanousac106bf2023-06-07 09:24:59 -0700595 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800596 const std::string& systemName,
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600597 const std::string& pcieDeviceId,
Ed Tanouse14742c2023-05-31 10:27:49 -0700598 const std::string& pcieFunctionIdStr)
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600599{
Ed Tanousac106bf2023-06-07 09:24:59 -0700600 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600601 {
602 return;
603 }
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800604 if constexpr (bmcwebEnableMultiHost)
605 {
606 // Option currently returns no systems. TBD
607 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
608 systemName);
609 return;
610 }
611 if (systemName != "system")
612 {
613 messages::resourceNotFound(asyncResp->res, "ComputerSystem",
614 systemName);
615 return;
616 }
617
Ed Tanouse14742c2023-05-31 10:27:49 -0700618 uint64_t pcieFunctionId = 0;
619 std::from_chars_result result = std::from_chars(
620 &*pcieFunctionIdStr.begin(), &*pcieFunctionIdStr.end(), pcieFunctionId);
621 if (result.ec != std::errc{} || result.ptr != &*pcieFunctionIdStr.end())
622 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700623 messages::resourceNotFound(asyncResp->res, "PCIeFunction",
Ed Tanouse14742c2023-05-31 10:27:49 -0700624 pcieFunctionIdStr);
625 return;
626 }
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600627
Ed Tanousac106bf2023-06-07 09:24:59 -0700628 getValidPCIeDevicePath(pcieDeviceId, asyncResp,
629 [asyncResp, pcieDeviceId,
630 pcieFunctionId](const std::string& pcieDevicePath,
631 const std::string& service) {
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600632 getPCIeDeviceProperties(
Ed Tanousac106bf2023-06-07 09:24:59 -0700633 asyncResp, pcieDevicePath, service,
634 [asyncResp, pcieDeviceId, pcieFunctionId](
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600635 const dbus::utility::DBusPropertiesMap& pcieDevProperties) {
Ed Tanousac106bf2023-06-07 09:24:59 -0700636 addPCIeFunctionCommonProperties(asyncResp->res, pcieDeviceId,
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600637 pcieFunctionId);
Ed Tanousac106bf2023-06-07 09:24:59 -0700638 addPCIeFunctionProperties(asyncResp->res, pcieFunctionId,
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600639 pcieDevProperties);
640 });
Ed Tanousac106bf2023-06-07 09:24:59 -0700641 });
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600642}
643
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700644inline void requestRoutesSystemPCIeFunction(App& app)
645{
646 BMCWEB_ROUTE(
Ed Tanous7f3e84a2022-12-28 16:22:54 -0800647 app, "/redfish/v1/Systems/<str>/PCIeDevices/<str>/PCIeFunctions/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700648 .privileges(redfish::privileges::getPCIeFunction)
Ed Tanous002d39b2022-05-31 08:59:27 -0700649 .methods(boost::beast::http::verb::get)(
Lakshmi Yadlapati727a0462023-03-10 23:49:00 -0600650 std::bind_front(handlePCIeFunctionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700651}
Jason M. Billsf5c9f8b2018-12-18 16:51:18 -0800652
653} // namespace redfish