xref: /openbmc/bmcweb/redfish-core/include/utils/pcie_util.hpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #pragma once
4 
5 #include "async_resp.hpp"
6 #include "dbus_utility.hpp"
7 #include "error_messages.hpp"
8 #include "generated/enums/pcie_device.hpp"
9 #include "generated/enums/pcie_slots.hpp"
10 #include "http/utility.hpp"
11 #include "utils/collection.hpp"
12 
13 #include <boost/system/error_code.hpp>
14 #include <boost/url/format.hpp>
15 #include <nlohmann/json.hpp>
16 
17 #include <array>
18 #include <memory>
19 #include <optional>
20 #include <string>
21 #include <string_view>
22 
23 namespace redfish
24 {
25 namespace pcie_util
26 {
27 
28 /**
29  * @brief Populate the PCIe Device list from a GetSubTreePaths search of
30  *        inventory
31  *
32  * @param[i,o] asyncResp  Async response object
33  * @param[i]   Name   Key to store the list of PCIe devices in asyncResp
34  *
35  * @return void
36  */
37 
38 inline void
getPCIeDeviceList(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp,const nlohmann::json::json_pointer & jsonKeyName)39     getPCIeDeviceList(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
40                       const nlohmann::json::json_pointer& jsonKeyName)
41 {
42     static constexpr std::array<std::string_view, 1> pcieDeviceInterface = {
43         "xyz.openbmc_project.Inventory.Item.PCIeDevice"};
44     const boost::urls::url pcieDeviceUrl = boost::urls::format(
45         "/redfish/v1/Systems/{}/PCIeDevices", BMCWEB_REDFISH_SYSTEM_URI_NAME);
46 
47     collection_util::getCollectionToKey(
48         asyncResp, pcieDeviceUrl, pcieDeviceInterface,
49         "/xyz/openbmc_project/inventory", jsonKeyName);
50 }
51 
52 inline std::optional<pcie_slots::SlotTypes>
dbusSlotTypeToRf(const std::string & slotType)53     dbusSlotTypeToRf(const std::string& slotType)
54 {
55     if (slotType ==
56         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.FullLength")
57     {
58         return pcie_slots::SlotTypes::FullLength;
59     }
60     if (slotType ==
61         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.HalfLength")
62     {
63         return pcie_slots::SlotTypes::HalfLength;
64     }
65     if (slotType ==
66         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.LowProfile")
67     {
68         return pcie_slots::SlotTypes::LowProfile;
69     }
70     if (slotType ==
71         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.Mini")
72     {
73         return pcie_slots::SlotTypes::Mini;
74     }
75     if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.M_2")
76     {
77         return pcie_slots::SlotTypes::M2;
78     }
79     if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OEM")
80     {
81         return pcie_slots::SlotTypes::OEM;
82     }
83     if (slotType ==
84         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OCP3Small")
85     {
86         return pcie_slots::SlotTypes::OCP3Small;
87     }
88     if (slotType ==
89         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.OCP3Large")
90     {
91         return pcie_slots::SlotTypes::OCP3Large;
92     }
93     if (slotType == "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.U_2")
94     {
95         return pcie_slots::SlotTypes::U2;
96     }
97     if (slotType ==
98         "xyz.openbmc_project.Inventory.Item.PCIeSlot.SlotTypes.Unknown")
99     {
100         return std::nullopt;
101     }
102 
103     return pcie_slots::SlotTypes::Invalid;
104 }
105 
106 inline std::optional<pcie_device::PCIeTypes>
redfishPcieGenerationFromDbus(const std::string & generationInUse)107     redfishPcieGenerationFromDbus(const std::string& generationInUse)
108 {
109     if (generationInUse ==
110         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen1")
111     {
112         return pcie_device::PCIeTypes::Gen1;
113     }
114     if (generationInUse ==
115         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen2")
116     {
117         return pcie_device::PCIeTypes::Gen2;
118     }
119     if (generationInUse ==
120         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen3")
121     {
122         return pcie_device::PCIeTypes::Gen3;
123     }
124     if (generationInUse ==
125         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen4")
126     {
127         return pcie_device::PCIeTypes::Gen4;
128     }
129     if (generationInUse ==
130         "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Gen5")
131     {
132         return pcie_device::PCIeTypes::Gen5;
133     }
134     if (generationInUse.empty() ||
135         generationInUse ==
136             "xyz.openbmc_project.Inventory.Item.PCIeSlot.Generations.Unknown")
137     {
138         return std::nullopt;
139     }
140 
141     return pcie_device::PCIeTypes::Invalid;
142 }
143 
144 } // namespace pcie_util
145 } // namespace redfish
146