Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2019 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 | #include "peci_pcie.hpp" |
| 18 | |
| 19 | #include "pciDeviceClass.hpp" |
| 20 | #include "pciVendors.hpp" |
| 21 | |
Ed Tanous | 985d9d9 | 2023-03-01 10:35:33 -0800 | [diff] [blame] | 22 | #include <boost/asio/io_context.hpp> |
Jason M. Bills | bce86a6 | 2020-10-08 16:03:47 -0700 | [diff] [blame] | 23 | #include <boost/asio/steady_timer.hpp> |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 24 | #include <boost/container/flat_map.hpp> |
| 25 | #include <boost/container/flat_set.hpp> |
| 26 | #include <sdbusplus/asio/object_server.hpp> |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 27 | #include <sdbusplus/bus/match.hpp> |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 28 | |
| 29 | #include <iomanip> |
| 30 | #include <iostream> |
| 31 | #include <set> |
| 32 | #include <sstream> |
Willy Tu | 975faf7 | 2022-09-23 18:39:16 +0000 | [diff] [blame] | 33 | #include <unordered_set> |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 34 | |
| 35 | namespace peci_pcie |
| 36 | { |
Lakshmi Yadlapati | 4fe704c | 2023-04-07 08:23:04 -0500 | [diff] [blame] | 37 | |
| 38 | struct PcieInterfaces |
| 39 | { |
| 40 | std::shared_ptr<sdbusplus::asio::dbus_interface> deviceIface; |
| 41 | std::shared_ptr<sdbusplus::asio::dbus_interface> assetIface; |
| 42 | }; |
| 43 | |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 44 | static boost::container::flat_map< |
| 45 | int, boost::container::flat_map< |
Lakshmi Yadlapati | 4fe704c | 2023-04-07 08:23:04 -0500 | [diff] [blame] | 46 | int, boost::container::flat_map<int, PcieInterfaces>>> |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 47 | pcieDeviceDBusMap; |
| 48 | |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 49 | static bool abortScan; |
Paul Fertser | 0734314 | 2022-12-01 21:04:57 +0000 | [diff] [blame] | 50 | static bool scanInProgress; |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 51 | |
Paul Fertser | 3b2afcb | 2022-12-16 12:17:15 +0000 | [diff] [blame] | 52 | constexpr const bool debug = false; |
| 53 | |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 54 | namespace function |
| 55 | { |
Patrick Williams | 42a9ac8 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 56 | static constexpr const char* functionTypeName = "FunctionType"; |
| 57 | static constexpr const char* deviceClassName = "DeviceClass"; |
| 58 | static constexpr const char* vendorIdName = "VendorId"; |
| 59 | static constexpr const char* deviceIdName = "DeviceId"; |
| 60 | static constexpr const char* classCodeName = "ClassCode"; |
| 61 | static constexpr const char* revisionIdName = "RevisionId"; |
| 62 | static constexpr const char* subsystemIdName = "SubsystemId"; |
| 63 | static constexpr const char* subsystemVendorIdName = "SubsystemVendorId"; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 64 | } // namespace function |
Andrei Kartashev | 6f55203 | 2021-05-11 21:25:29 +0300 | [diff] [blame] | 65 | |
| 66 | static constexpr const std::array pciConfigInfo{ |
| 67 | std::tuple<const char*, int, int>{function::functionTypeName, -1, -1}, |
| 68 | std::tuple<const char*, int, int>{function::deviceClassName, -1, -1}, |
| 69 | std::tuple<const char*, int, int>{function::vendorIdName, 0, 2}, |
| 70 | std::tuple<const char*, int, int>{function::deviceIdName, 2, 2}, |
| 71 | std::tuple<const char*, int, int>{function::classCodeName, 9, 3}, |
| 72 | std::tuple<const char*, int, int>{function::revisionIdName, 8, 1}, |
| 73 | std::tuple<const char*, int, int>{function::subsystemIdName, 0x2e, 2}, |
| 74 | std::tuple<const char*, int, int>{function::subsystemVendorIdName, 0x2c, |
| 75 | 2}}; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 76 | } // namespace peci_pcie |
| 77 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 78 | enum class resCode |
| 79 | { |
| 80 | resOk, |
Spencer Ku | bb5efe7 | 2021-09-02 16:11:14 +0800 | [diff] [blame] | 81 | resSkip, |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 82 | resErr |
| 83 | }; |
| 84 | |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 85 | struct CPUInfo |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 86 | { |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 87 | size_t addr; |
| 88 | bool skipCpuBuses; |
| 89 | boost::container::flat_set<size_t> cpuBusNums; |
| 90 | }; |
| 91 | |
| 92 | // PECI Client Address Map |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 93 | static resCode getCPUBusMap(std::vector<CPUInfo>& cpuInfo) |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 94 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 95 | cpuInfo.clear(); |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 96 | for (size_t addr = MIN_CLIENT_ADDR; addr <= MAX_CLIENT_ADDR; addr++) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 97 | { |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 98 | if (peci_Ping(addr) != PECI_CC_SUCCESS) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 99 | { |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 100 | continue; |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 101 | } |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 102 | |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 103 | auto& cpu = cpuInfo.emplace_back(CPUInfo{addr, false, {}}); |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 104 | uint8_t cc = 0; |
| 105 | CPUModel model{}; |
| 106 | uint8_t stepping = 0; |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 107 | if (peci_GetCPUID(addr, &model, &stepping, &cc) != PECI_CC_SUCCESS) |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 108 | { |
| 109 | std::cerr << "Cannot get CPUID!\n"; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 110 | return resCode::resErr; |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | switch (model) |
| 114 | { |
Jason M. Bills | 9af905e | 2024-04-01 13:16:41 -0700 | [diff] [blame] | 115 | case skylake: |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 116 | { |
| 117 | // Get the assigned CPU bus numbers from CPUBUSNO and CPUBUSNO1 |
| 118 | // (B(0) D8 F2 offsets CCh and D0h) |
| 119 | uint32_t cpuBusNum = 0; |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 120 | if (peci_RdPCIConfigLocal(addr, 0, 8, 2, 0xCC, 4, |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 121 | (uint8_t*)&cpuBusNum, &cc) != |
| 122 | PECI_CC_SUCCESS) |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 123 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 124 | return resCode::resErr; |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 125 | } |
| 126 | uint32_t cpuBusNum1 = 0; |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 127 | if (peci_RdPCIConfigLocal(addr, 0, 8, 2, 0xD0, 4, |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 128 | (uint8_t*)&cpuBusNum1, &cc) != |
| 129 | PECI_CC_SUCCESS) |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 130 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 131 | return resCode::resErr; |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // Add the CPU bus numbers to the set for this CPU |
| 135 | while (cpuBusNum) |
| 136 | { |
| 137 | // Get the LSB |
| 138 | size_t busNum = cpuBusNum & 0xFF; |
| 139 | cpu.cpuBusNums.insert(busNum); |
| 140 | // Shift right by one byte |
| 141 | cpuBusNum >>= 8; |
| 142 | } |
| 143 | while (cpuBusNum1) |
| 144 | { |
| 145 | // Get the LSB |
| 146 | size_t busNum = cpuBusNum1 & 0xFF; |
| 147 | cpu.cpuBusNums.insert(busNum); |
| 148 | // Shift right by one byte |
Zev Weiss | 9fa54b5 | 2020-09-02 21:20:33 +0000 | [diff] [blame] | 149 | cpuBusNum1 >>= 8; |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 150 | } |
| 151 | cpu.skipCpuBuses = true; |
Jason M. Bills | c076bd7 | 2023-07-31 12:25:05 -0700 | [diff] [blame] | 152 | break; |
| 153 | } |
| 154 | default: |
| 155 | { |
| 156 | std::cerr << "CPU buses not found for CPU Model: 0x" << std::hex |
| 157 | << static_cast<int>(model) << std::dec << "\n"; |
| 158 | break; |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 159 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 160 | } |
| 161 | } |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 162 | return cpuInfo.empty() ? resCode::resErr : resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static bool isPECIAvailable(void) |
| 166 | { |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 167 | for (size_t i = MIN_CLIENT_ADDR; i <= MAX_CLIENT_ADDR; i++) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 168 | { |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 169 | if (peci_Ping(i) == PECI_CC_SUCCESS) |
| 170 | { |
| 171 | return true; |
| 172 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 173 | } |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 174 | return false; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 177 | static resCode getDataFromPCIeConfig( |
| 178 | const int& clientAddr, const int& bus, const int& dev, const int& func, |
| 179 | const int& offset, const int& size, uint32_t& pciData) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 180 | { |
| 181 | // PECI RdPCIConfig() currently only supports 4 byte reads, so adjust |
| 182 | // the offset and size to get the right data |
| 183 | static constexpr const int pciReadSize = 4; |
| 184 | int mod = offset % pciReadSize; |
| 185 | int pciOffset = offset - mod; |
| 186 | if (mod + size > pciReadSize) |
| 187 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 188 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | std::array<uint8_t, pciReadSize> data; |
| 192 | uint8_t cc; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 193 | int ret = PECI_CC_TIMEOUT; |
Paul Fertser | b08723d | 2022-12-16 11:49:08 +0000 | [diff] [blame] | 194 | for (int index = 0; (index < 15) && (ret == PECI_CC_TIMEOUT); index++) |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 195 | { |
Jason M. Bills | 3b1665a | 2021-06-11 13:35:04 -0700 | [diff] [blame] | 196 | #ifdef USE_RDENDPOINTCFG |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 197 | ret = peci_RdEndPointConfigPci( |
| 198 | clientAddr, // CPU Address |
| 199 | 0, // PCI Seg (use 0 for now) |
| 200 | bus, // PCI Bus |
| 201 | dev, // PCI Device |
| 202 | func, // PCI Function |
| 203 | pciOffset, // PCI Offset |
| 204 | pciReadSize, // PCI Read Size |
| 205 | data.data(), // PCI Read Data |
| 206 | &cc); // PECI Completion Code |
Jason M. Bills | 3b1665a | 2021-06-11 13:35:04 -0700 | [diff] [blame] | 207 | #else |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 208 | ret = peci_RdPCIConfig( |
| 209 | clientAddr, // CPU Address |
| 210 | bus, // PCI Bus |
| 211 | dev, // PCI Device |
| 212 | func, // PCI Function |
| 213 | pciOffset, // PCI Offset |
| 214 | data.data(), // PCI Read Data |
| 215 | &cc); // PECI Completion Code |
Jason M. Bills | 3b1665a | 2021-06-11 13:35:04 -0700 | [diff] [blame] | 216 | #endif |
Paul Fertser | 3b2afcb | 2022-12-16 12:17:15 +0000 | [diff] [blame] | 217 | if constexpr (peci_pcie::debug) |
| 218 | { |
| 219 | std::cerr << "Request: bus " << bus << " dev " << dev << " func " |
| 220 | << func << " pciOffset " << pciOffset << " ret: " << ret |
| 221 | << " cc: " << static_cast<int>(cc) << "\n"; |
| 222 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 223 | } |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 224 | if (ret != PECI_CC_SUCCESS) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 225 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 226 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 227 | } |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 228 | else if (cc != PECI_DEV_CC_SUCCESS) |
| 229 | { |
| 230 | return resCode::resSkip; |
| 231 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 232 | |
| 233 | // Now build the requested data into a single number |
| 234 | pciData = 0; |
| 235 | for (int i = mod; i < mod + size; i++) |
| 236 | { |
Jason M. Bills | e55832b | 2021-06-14 16:00:49 -0700 | [diff] [blame] | 237 | pciData |= static_cast<uint32_t>(data[i]) << 8 * (i - mod); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 240 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Andrei Kartashev | 6f55203 | 2021-05-11 21:25:29 +0300 | [diff] [blame] | 243 | static resCode getStringFromData(const int& size, const uint32_t& data, |
| 244 | std::string& res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 245 | { |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 246 | // And convert it to a string |
| 247 | std::stringstream dataStream; |
| 248 | dataStream << "0x" << std::hex << std::setfill('0') << std::setw(size * 2) |
| 249 | << data; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 250 | res = dataStream.str(); |
| 251 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 254 | static resCode getVendorName(const int& clientAddr, const int& bus, |
| 255 | const int& dev, std::string& res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 256 | { |
| 257 | static constexpr const int vendorIDOffset = 0x00; |
| 258 | static constexpr const int vendorIDSize = 2; |
| 259 | |
| 260 | // Get the header type register from function 0 |
| 261 | uint32_t vendorID = 0; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 262 | if (getDataFromPCIeConfig(clientAddr, bus, dev, 0, vendorIDOffset, |
| 263 | vendorIDSize, vendorID) != resCode::resOk) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 264 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 265 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 266 | } |
| 267 | // Get the vendor name or use Other if it doesn't exist |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 268 | res = pciVendors.try_emplace(vendorID, otherVendor).first->second; |
| 269 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 272 | static resCode getDeviceClass(const int& clientAddr, const int& bus, |
| 273 | const int& dev, const int& func, std::string& res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 274 | { |
| 275 | static constexpr const int baseClassOffset = 0x0b; |
| 276 | static constexpr const int baseClassSize = 1; |
| 277 | |
| 278 | // Get the Device Base Class |
| 279 | uint32_t baseClass = 0; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 280 | if (getDataFromPCIeConfig(clientAddr, bus, dev, func, baseClassOffset, |
| 281 | baseClassSize, baseClass) != resCode::resOk) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 282 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 283 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 284 | } |
| 285 | // Get the base class name or use Other if it doesn't exist |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 286 | res = pciDeviceClasses.try_emplace(baseClass, otherClass).first->second; |
| 287 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 288 | } |
| 289 | |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 290 | static resCode getCapReading( |
| 291 | const int& clientAddr, const int& bus, const int& dev, uint32_t& capReading, |
| 292 | const int compareCapID, const int offsetAddress, const int offsetLength) |
Spencer Ku | bb5efe7 | 2021-09-02 16:11:14 +0800 | [diff] [blame] | 293 | { |
| 294 | resCode error; |
| 295 | uint32_t capAddress = 0; |
| 296 | uint32_t capabilityID; |
| 297 | uint32_t nextCapPointer = peci_pcie::pointToCapStruct; |
| 298 | |
| 299 | do |
| 300 | { |
| 301 | // Get capability address |
| 302 | error = getDataFromPCIeConfig(clientAddr, bus, dev, 0, nextCapPointer, |
| 303 | 1, capAddress); |
| 304 | if (error != resCode::resOk) |
| 305 | { |
| 306 | return error; |
| 307 | } |
| 308 | // Capability struct address is a pointer which point to next capability |
| 309 | // struct, so if capability struct address is 0 means it doesn't have |
| 310 | // next capability struct. |
| 311 | if (capAddress == 0) |
| 312 | { |
| 313 | return resCode::resSkip; |
| 314 | } |
| 315 | // Get capability ID |
| 316 | error = getDataFromPCIeConfig(clientAddr, bus, dev, 0, capAddress, 1, |
| 317 | capabilityID); |
| 318 | if (error != resCode::resOk) |
| 319 | { |
| 320 | return error; |
| 321 | } |
| 322 | nextCapPointer = capAddress + peci_pcie::capPointerOffset; |
| 323 | |
Jason M. Bills | c076bd7 | 2023-07-31 12:25:05 -0700 | [diff] [blame] | 324 | } while (capabilityID != static_cast<uint32_t>(compareCapID)); |
Spencer Ku | bb5efe7 | 2021-09-02 16:11:14 +0800 | [diff] [blame] | 325 | // Get capability reading. |
| 326 | error = getDataFromPCIeConfig(clientAddr, bus, dev, 0, |
| 327 | capAddress + offsetAddress, offsetLength, |
| 328 | capReading); |
| 329 | if (error != resCode::resOk) |
| 330 | { |
| 331 | return error; |
| 332 | } |
| 333 | return resCode::resOk; |
| 334 | } |
| 335 | |
| 336 | static resCode getGenerationInUse(const int& clientAddr, const int& bus, |
| 337 | const int& dev, std::string& generationInUse) |
| 338 | { |
Willy Tu | 975faf7 | 2022-09-23 18:39:16 +0000 | [diff] [blame] | 339 | static std::unordered_set<uint32_t> linkSpeedSkipMap; |
Spencer Ku | bb5efe7 | 2021-09-02 16:11:14 +0800 | [diff] [blame] | 340 | resCode error; |
| 341 | std::string res; |
Spencer Ku | bb5efe7 | 2021-09-02 16:11:14 +0800 | [diff] [blame] | 342 | |
| 343 | // Capability ID 0x10(16) is PCI Express |
| 344 | constexpr int pcieCapID = 16; |
| 345 | constexpr int capLength = 2; |
| 346 | uint32_t linkStatus; |
| 347 | |
| 348 | error = getCapReading(clientAddr, bus, dev, linkStatus, pcieCapID, |
| 349 | peci_pcie::linkStatusOffset, capLength); |
| 350 | if (error != resCode::resOk) |
| 351 | { |
| 352 | return error; |
| 353 | } |
| 354 | |
| 355 | uint32_t linkSpeed = linkStatus & peci_pcie::maskOfCLS; |
| 356 | |
| 357 | switch (linkSpeed) |
| 358 | { |
| 359 | case peci_pcie::pcieGen1: |
| 360 | generationInUse = "xyz.openbmc_project.Inventory.Item." |
| 361 | "PCIeSlot.Generations.Gen1"; |
| 362 | error = resCode::resOk; |
| 363 | break; |
| 364 | case peci_pcie::pcieGen2: |
| 365 | generationInUse = "xyz.openbmc_project.Inventory.Item." |
| 366 | "PCIeSlot.Generations.Gen2"; |
| 367 | error = resCode::resOk; |
| 368 | break; |
| 369 | case peci_pcie::pcieGen3: |
| 370 | generationInUse = "xyz.openbmc_project.Inventory.Item." |
| 371 | "PCIeSlot.Generations.Gen3"; |
| 372 | error = resCode::resOk; |
| 373 | break; |
| 374 | case peci_pcie::pcieGen4: |
| 375 | generationInUse = "xyz.openbmc_project.Inventory.Item." |
| 376 | "PCIeSlot.Generations.Gen4"; |
| 377 | error = resCode::resOk; |
| 378 | break; |
| 379 | case peci_pcie::pcieGen5: |
| 380 | generationInUse = "xyz.openbmc_project.Inventory.Item." |
| 381 | "PCIeSlot.Generations.Gen5"; |
| 382 | error = resCode::resOk; |
| 383 | break; |
| 384 | default: |
Willy Tu | 975faf7 | 2022-09-23 18:39:16 +0000 | [diff] [blame] | 385 | if (!linkSpeedSkipMap.contains(linkSpeed)) |
| 386 | { |
| 387 | std::cerr << "Link speed : " << linkSpeed |
| 388 | << " can not mapping to PCIe type list.\n"; |
| 389 | linkSpeedSkipMap.emplace(linkSpeed); |
| 390 | } |
Spencer Ku | bb5efe7 | 2021-09-02 16:11:14 +0800 | [diff] [blame] | 391 | error = resCode::resSkip; |
| 392 | } |
| 393 | return error; |
| 394 | } |
| 395 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 396 | static resCode isMultiFunction(const int& clientAddr, const int& bus, |
| 397 | const int& dev, bool& res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 398 | { |
| 399 | static constexpr const int headerTypeOffset = 0x0e; |
| 400 | static constexpr const int headerTypeSize = 1; |
| 401 | static constexpr const int multiFuncBit = 1 << 7; |
| 402 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 403 | res = false; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 404 | // Get the header type register from function 0 |
| 405 | uint32_t headerType = 0; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 406 | if (getDataFromPCIeConfig(clientAddr, bus, dev, 0, headerTypeOffset, |
| 407 | headerTypeSize, headerType) != resCode::resOk) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 408 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 409 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 410 | } |
| 411 | // Check if it's a multifunction device |
| 412 | if (headerType & multiFuncBit) |
| 413 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 414 | res = true; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 415 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 416 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 419 | static resCode pcieFunctionExists(const int& clientAddr, const int& bus, |
| 420 | const int& dev, const int& func, bool& res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 421 | { |
| 422 | constexpr const int pciIDOffset = 0; |
| 423 | constexpr const int pciIDSize = 4; |
| 424 | uint32_t pciID = 0; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 425 | res = false; |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 426 | |
| 427 | resCode error = getDataFromPCIeConfig(clientAddr, bus, dev, func, |
| 428 | pciIDOffset, pciIDSize, pciID); |
| 429 | if (error == resCode::resSkip) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 430 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 431 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 432 | } |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 433 | else if (error == resCode::resErr) |
| 434 | { |
| 435 | return resCode::resErr; |
| 436 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 437 | |
| 438 | // if VID and DID are all 0s or 1s, then the device doesn't exist |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 439 | if (pciID != 0x00000000 && pciID != 0xFFFFFFFF) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 440 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 441 | res = true; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 442 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 443 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 446 | static resCode pcieDeviceExists(const int& clientAddr, const int& bus, |
| 447 | const int& dev, bool& res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 448 | { |
| 449 | // Check if this device exists by checking function 0 |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 450 | return pcieFunctionExists(clientAddr, bus, dev, 0, res); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 453 | static resCode setPCIeProperty(const int& clientAddr, const int& bus, |
| 454 | const int& dev, const std::string& propertyName, |
| 455 | const std::string& propertyValue) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 456 | { |
Lakshmi Yadlapati | 4fe704c | 2023-04-07 08:23:04 -0500 | [diff] [blame] | 457 | peci_pcie::PcieInterfaces pcieIfaces = |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 458 | peci_pcie::pcieDeviceDBusMap[clientAddr][bus][dev]; |
| 459 | |
Lakshmi Yadlapati | 4fe704c | 2023-04-07 08:23:04 -0500 | [diff] [blame] | 460 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface; |
| 461 | if (propertyName == "Manufacturer") |
| 462 | { |
| 463 | iface = pcieIfaces.assetIface; |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | iface = pcieIfaces.deviceIface; |
| 468 | } |
| 469 | |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 470 | if (iface->is_initialized()) |
| 471 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 472 | if (!iface->set_property(propertyName, propertyValue)) |
| 473 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 474 | } |
| 475 | else |
| 476 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 477 | if (!iface->register_property(propertyName, propertyValue)) |
| 478 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 479 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 480 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 483 | static void setDefaultPCIeFunctionProperties( |
| 484 | const int& clientAddr, const int& bus, const int& dev, const int& func) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 485 | { |
| 486 | // Set the function-specific properties |
Andrei Kartashev | 6f55203 | 2021-05-11 21:25:29 +0300 | [diff] [blame] | 487 | for (const auto& [name, offset, size] : peci_pcie::pciConfigInfo) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 488 | { |
| 489 | setPCIeProperty(clientAddr, bus, dev, |
| 490 | "Function" + std::to_string(func) + std::string(name), |
| 491 | std::string()); |
| 492 | } |
| 493 | } |
| 494 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 495 | static resCode setPCIeFunctionProperties(const int& clientAddr, const int& bus, |
| 496 | const int& dev, const int& func) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 497 | { |
Andrei Kartashev | 6f55203 | 2021-05-11 21:25:29 +0300 | [diff] [blame] | 498 | uint32_t data = 0; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 499 | std::string res; |
Andrei Kartashev | 6f55203 | 2021-05-11 21:25:29 +0300 | [diff] [blame] | 500 | resCode error; |
| 501 | |
| 502 | for (const auto& [name, offset, size] : peci_pcie::pciConfigInfo) |
| 503 | { |
| 504 | if (offset < 0) |
| 505 | { |
| 506 | continue; |
| 507 | } |
| 508 | |
| 509 | error = getDataFromPCIeConfig(clientAddr, bus, dev, func, offset, size, |
| 510 | data); |
| 511 | if (error != resCode::resOk) |
| 512 | { |
| 513 | return error; |
| 514 | } |
| 515 | getStringFromData(size, data, res); |
| 516 | setPCIeProperty(clientAddr, bus, dev, |
| 517 | "Function" + std::to_string(func) + std::string(name), |
| 518 | res); |
| 519 | } |
| 520 | |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 521 | // Set the function type always to physical for now |
| 522 | setPCIeProperty(clientAddr, bus, dev, |
| 523 | "Function" + std::to_string(func) + |
| 524 | std::string(peci_pcie::function::functionTypeName), |
| 525 | "Physical"); |
| 526 | |
| 527 | // Set the function Device Class |
Andrei Kartashev | 6f55203 | 2021-05-11 21:25:29 +0300 | [diff] [blame] | 528 | error = getDeviceClass(clientAddr, bus, dev, func, res); |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 529 | if (error != resCode::resOk) |
| 530 | { |
| 531 | return error; |
| 532 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 533 | setPCIeProperty(clientAddr, bus, dev, |
| 534 | "Function" + std::to_string(func) + |
| 535 | std::string(peci_pcie::function::deviceClassName), |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 536 | res); |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 537 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 540 | static resCode setPCIeDeviceProperties(const int& clientAddr, const int& bus, |
| 541 | const int& dev) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 542 | { |
| 543 | // Set the device manufacturer |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 544 | std::string manuf; |
| 545 | resCode error = getVendorName(clientAddr, bus, dev, manuf); |
| 546 | if (error != resCode::resOk) |
| 547 | { |
| 548 | return error; |
| 549 | } |
| 550 | setPCIeProperty(clientAddr, bus, dev, "Manufacturer", manuf); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 551 | |
| 552 | // Set the device type |
Patrick Williams | 42a9ac8 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 553 | constexpr const char* deviceTypeName = "DeviceType"; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 554 | bool multiFunc; |
| 555 | error = isMultiFunction(clientAddr, bus, dev, multiFunc); |
| 556 | if (error != resCode::resOk) |
| 557 | { |
| 558 | return error; |
| 559 | } |
| 560 | if (multiFunc) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 561 | { |
Thang Tran | ec7361d | 2025-07-23 10:20:48 +0700 | [diff] [blame] | 562 | setPCIeProperty( |
| 563 | clientAddr, bus, dev, deviceTypeName, |
| 564 | "xyz.openbmc_project.Inventory.Item.PCIeDevice.DeviceTypes.MultiFunction"); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 565 | } |
| 566 | else |
| 567 | { |
Thang Tran | ec7361d | 2025-07-23 10:20:48 +0700 | [diff] [blame] | 568 | setPCIeProperty( |
| 569 | clientAddr, bus, dev, deviceTypeName, |
| 570 | "xyz.openbmc_project.Inventory.Item.PCIeDevice.DeviceTypes.SingleFunction"); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 571 | } |
Spencer Ku | bb5efe7 | 2021-09-02 16:11:14 +0800 | [diff] [blame] | 572 | |
| 573 | // Set PCIe Generation |
Patrick Williams | 42a9ac8 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 574 | constexpr const char* generationInUseName = "GenerationInUse"; |
Spencer Ku | bb5efe7 | 2021-09-02 16:11:14 +0800 | [diff] [blame] | 575 | std::string generationInUse; |
| 576 | error = getGenerationInUse(clientAddr, bus, dev, generationInUse); |
| 577 | if (error == resCode::resErr) |
| 578 | { |
| 579 | return error; |
| 580 | } |
| 581 | // "resSkip" status means it can't get the capability reading, such like |
| 582 | // this device is not PCI Express. |
| 583 | if (error == resCode::resSkip) |
| 584 | { |
| 585 | setPCIeProperty(clientAddr, bus, dev, generationInUseName, ""); |
| 586 | return resCode::resOk; |
| 587 | } |
| 588 | setPCIeProperty(clientAddr, bus, dev, generationInUseName, generationInUse); |
| 589 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 590 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 593 | static resCode updatePCIeDevice(const int& clientAddr, const int& bus, |
| 594 | const int& dev) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 595 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 596 | if (setPCIeDeviceProperties(clientAddr, bus, dev) != resCode::resOk) |
| 597 | { |
| 598 | return resCode::resErr; |
| 599 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 600 | |
Nidhin MS | 8b18f52 | 2023-06-28 10:25:06 +0530 | [diff] [blame] | 601 | bool multiFunc = false; |
| 602 | resCode error = isMultiFunction(clientAddr, bus, dev, multiFunc); |
| 603 | if (error != resCode::resOk) |
| 604 | { |
| 605 | return error; |
| 606 | } |
| 607 | // Functions greater than 0 should only be accessed on multi-function |
| 608 | // devices |
| 609 | int maxPCIFunctions = multiFunc ? peci_pcie::maxPCIFunctions : 1; |
| 610 | |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 611 | // Walk through and populate the functions for this device |
Nidhin MS | 8b18f52 | 2023-06-28 10:25:06 +0530 | [diff] [blame] | 612 | for (int func = 0; func < maxPCIFunctions; func++) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 613 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 614 | bool res; |
| 615 | resCode error = pcieFunctionExists(clientAddr, bus, dev, func, res); |
| 616 | if (error != resCode::resOk) |
| 617 | { |
| 618 | return error; |
| 619 | } |
| 620 | if (res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 621 | { |
| 622 | // Set the properties for this function |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 623 | if (setPCIeFunctionProperties(clientAddr, bus, dev, func) != |
| 624 | resCode::resOk) |
| 625 | { |
| 626 | return resCode::resErr; |
| 627 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 628 | } |
| 629 | else |
| 630 | { |
| 631 | // Set default properties for unused functions |
| 632 | setDefaultPCIeFunctionProperties(clientAddr, bus, dev, func); |
| 633 | } |
| 634 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 635 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | static void removePCIeDevice(sdbusplus::asio::object_server& objServer, |
| 639 | const int& clientAddr, const int& bus, |
| 640 | const int& dev) |
| 641 | { |
Lakshmi Yadlapati | 4fe704c | 2023-04-07 08:23:04 -0500 | [diff] [blame] | 642 | peci_pcie::PcieInterfaces ifaces = |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 643 | peci_pcie::pcieDeviceDBusMap[clientAddr][bus][dev]; |
| 644 | |
Lakshmi Yadlapati | 4fe704c | 2023-04-07 08:23:04 -0500 | [diff] [blame] | 645 | objServer.remove_interface(ifaces.deviceIface); |
| 646 | objServer.remove_interface(ifaces.assetIface); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 647 | |
| 648 | peci_pcie::pcieDeviceDBusMap[clientAddr][bus].erase(dev); |
| 649 | if (peci_pcie::pcieDeviceDBusMap[clientAddr][bus].empty()) |
| 650 | { |
| 651 | peci_pcie::pcieDeviceDBusMap[clientAddr].erase(bus); |
| 652 | } |
| 653 | if (peci_pcie::pcieDeviceDBusMap[clientAddr].empty()) |
| 654 | { |
| 655 | peci_pcie::pcieDeviceDBusMap.erase(clientAddr); |
| 656 | } |
| 657 | } |
| 658 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 659 | static resCode addPCIeDevice(sdbusplus::asio::object_server& objServer, |
| 660 | const int& clientAddr, const int& cpu, |
| 661 | const int& bus, const int& dev) |
| 662 | { |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 663 | std::string pathName = |
| 664 | std::string(peci_pcie::peciPCIePath) + "/S" + std::to_string(cpu) + |
| 665 | "B" + std::to_string(bus) + "D" + std::to_string(dev); |
Lakshmi Yadlapati | 4fe704c | 2023-04-07 08:23:04 -0500 | [diff] [blame] | 666 | peci_pcie::PcieInterfaces ifaces; |
| 667 | ifaces.deviceIface = |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 668 | objServer.add_interface(pathName, peci_pcie::peciPCIeDeviceInterface); |
Lakshmi Yadlapati | 4fe704c | 2023-04-07 08:23:04 -0500 | [diff] [blame] | 669 | ifaces.assetIface = |
| 670 | objServer.add_interface(pathName, peci_pcie::peciPCIeAssetInterface); |
| 671 | peci_pcie::pcieDeviceDBusMap[clientAddr][bus][dev] = ifaces; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 672 | |
| 673 | // Update the properties for the new device |
| 674 | if (updatePCIeDevice(clientAddr, bus, dev) != resCode::resOk) |
| 675 | { |
| 676 | removePCIeDevice(objServer, clientAddr, bus, dev); |
| 677 | return resCode::resErr; |
| 678 | } |
| 679 | |
Lakshmi Yadlapati | 4fe704c | 2023-04-07 08:23:04 -0500 | [diff] [blame] | 680 | ifaces.deviceIface->initialize(); |
| 681 | ifaces.assetIface->initialize(); |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 682 | return resCode::resOk; |
| 683 | } |
| 684 | |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 685 | static bool pcieDeviceInDBusMap(const int& clientAddr, const int& bus, |
| 686 | const int& dev) |
| 687 | { |
| 688 | if (auto clientAddrIt = peci_pcie::pcieDeviceDBusMap.find(clientAddr); |
| 689 | clientAddrIt != peci_pcie::pcieDeviceDBusMap.end()) |
| 690 | { |
| 691 | if (auto busIt = clientAddrIt->second.find(bus); |
| 692 | busIt != clientAddrIt->second.end()) |
| 693 | { |
| 694 | if (auto devIt = busIt->second.find(dev); |
| 695 | devIt != busIt->second.end()) |
| 696 | { |
Lakshmi Yadlapati | 4fe704c | 2023-04-07 08:23:04 -0500 | [diff] [blame] | 697 | if (devIt->second.deviceIface != nullptr || |
| 698 | devIt->second.assetIface != nullptr) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 699 | { |
| 700 | return true; |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | return false; |
| 706 | } |
| 707 | |
Jason M. Bills | c076bd7 | 2023-07-31 12:25:05 -0700 | [diff] [blame] | 708 | static resCode probePCIeDevice(sdbusplus::asio::object_server& objServer, |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 709 | size_t addr, int cpu, int bus, int dev) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 710 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 711 | bool res; |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 712 | resCode error = pcieDeviceExists(addr, bus, dev, res); |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 713 | if (error != resCode::resOk) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 714 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 715 | return error; |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 716 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 717 | if (res) |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 718 | { |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 719 | if (pcieDeviceInDBusMap(addr, bus, dev)) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 720 | { |
| 721 | // This device is already in D-Bus, so update it |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 722 | if (updatePCIeDevice(addr, bus, dev) != resCode::resOk) |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 723 | { |
| 724 | return resCode::resErr; |
| 725 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 726 | } |
| 727 | else |
| 728 | { |
| 729 | // This device is not in D-Bus, so add it |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 730 | if (addPCIeDevice(objServer, addr, cpu, bus, dev) != resCode::resOk) |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 731 | { |
| 732 | return resCode::resErr; |
| 733 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 734 | } |
| 735 | } |
| 736 | else |
| 737 | { |
| 738 | // If PECI is not available, then stop scanning |
| 739 | if (!isPECIAvailable()) |
| 740 | { |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 741 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 742 | } |
| 743 | |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 744 | if (pcieDeviceInDBusMap(addr, bus, dev)) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 745 | { |
| 746 | // This device is in D-Bus, so remove it |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 747 | removePCIeDevice(objServer, addr, bus, dev); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 748 | } |
| 749 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 750 | return resCode::resOk; |
| 751 | } |
| 752 | |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 753 | static void scanNextPCIeDevice( |
| 754 | boost::asio::io_context& io, sdbusplus::asio::object_server& objServer, |
| 755 | std::vector<CPUInfo>& cpuInfo, int cpu, int bus, int dev); |
Ed Tanous | 985d9d9 | 2023-03-01 10:35:33 -0800 | [diff] [blame] | 756 | static void scanPCIeDevice(boost::asio::io_context& io, |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 757 | sdbusplus::asio::object_server& objServer, |
| 758 | std::vector<CPUInfo>& cpuInfo, int cpu, int bus, |
| 759 | int dev) |
| 760 | { |
Jason M. Bills | c076bd7 | 2023-07-31 12:25:05 -0700 | [diff] [blame] | 761 | if (cpu >= static_cast<int>(cpuInfo.size())) |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 762 | { |
| 763 | std::cerr << "Request to scan CPU" << cpu |
| 764 | << " while CPU array has size " << cpuInfo.size() << "\n"; |
| 765 | return; |
| 766 | } |
| 767 | auto& info = cpuInfo[cpu]; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 768 | // Check if this is a CPU bus that we should skip |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 769 | if (info.skipCpuBuses && info.cpuBusNums.count(bus)) |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 770 | { |
| 771 | std::cout << "Skipping CPU " << cpu << " Bus Number " << bus << "\n"; |
| 772 | // Skip all the devices on this bus |
| 773 | dev = peci_pcie::maxPCIDevices; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 774 | } |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 775 | else |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 776 | { |
Jason M. Bills | c076bd7 | 2023-07-31 12:25:05 -0700 | [diff] [blame] | 777 | if (probePCIeDevice(objServer, info.addr, cpu, bus, dev) != |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 778 | resCode::resOk) |
| 779 | { |
| 780 | std::cerr << "Failed to probe CPU " << cpu << " Bus " << bus |
| 781 | << " Device " << dev << "\n"; |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 782 | peci_pcie::abortScan = true; |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 783 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 784 | } |
| 785 | |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 786 | scanNextPCIeDevice(io, objServer, cpuInfo, cpu, bus, dev); |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 787 | return; |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 788 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 789 | |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 790 | static void scanNextPCIeDevice( |
| 791 | boost::asio::io_context& io, sdbusplus::asio::object_server& objServer, |
| 792 | std::vector<CPUInfo>& cpuInfo, int cpu, int bus, int dev) |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 793 | { |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 794 | if (peci_pcie::abortScan) |
| 795 | { |
Paul Fertser | 0734314 | 2022-12-01 21:04:57 +0000 | [diff] [blame] | 796 | peci_pcie::scanInProgress = false; |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 797 | std::cerr << "PCIe scan aborted\n"; |
| 798 | return; |
| 799 | } |
| 800 | |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 801 | // PCIe Device scan completed, so move to the next device |
| 802 | if (++dev >= peci_pcie::maxPCIDevices) |
| 803 | { |
| 804 | // All devices scanned, so move to the next bus |
| 805 | dev = 0; |
| 806 | if (++bus >= peci_pcie::maxPCIBuses) |
| 807 | { |
| 808 | // All buses scanned, so move to the next CPU |
| 809 | bus = 0; |
Jason M. Bills | c076bd7 | 2023-07-31 12:25:05 -0700 | [diff] [blame] | 810 | if (++cpu >= static_cast<int>(cpuInfo.size())) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 811 | { |
| 812 | // All CPUs scanned, so we're done |
Paul Fertser | 0734314 | 2022-12-01 21:04:57 +0000 | [diff] [blame] | 813 | peci_pcie::scanInProgress = false; |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 814 | std::cerr << "PCIe scan completed\n"; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 815 | return; |
| 816 | } |
| 817 | } |
| 818 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 819 | boost::asio::post(io, [&io, &objServer, &cpuInfo, cpu, bus, dev]() mutable { |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 820 | scanPCIeDevice(io, objServer, cpuInfo, cpu, bus, dev); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 821 | }); |
| 822 | } |
| 823 | |
Ed Tanous | 985d9d9 | 2023-03-01 10:35:33 -0800 | [diff] [blame] | 824 | static void startPCIeScan(boost::asio::io_context& io, |
Paul Fertser | 0734314 | 2022-12-01 21:04:57 +0000 | [diff] [blame] | 825 | sdbusplus::asio::object_server& objServer, |
| 826 | std::vector<CPUInfo>& cpuInfo) |
| 827 | { |
| 828 | if (!peci_pcie::scanInProgress) |
| 829 | { |
| 830 | // get the PECI client address list |
Paul Fertser | 3b2afcb | 2022-12-16 12:17:15 +0000 | [diff] [blame] | 831 | std::cerr << "Getting map\n"; |
Paul Fertser | 0734314 | 2022-12-01 21:04:57 +0000 | [diff] [blame] | 832 | if (getCPUBusMap(cpuInfo) != resCode::resOk) |
| 833 | { |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 834 | peci_pcie::abortScan = true; |
Paul Fertser | 0734314 | 2022-12-01 21:04:57 +0000 | [diff] [blame] | 835 | return; |
| 836 | } |
| 837 | std::cerr << "PCIe scan started\n"; |
| 838 | // scan PCIe starting from CPU 0, Bus 0, Device 0 |
| 839 | peci_pcie::scanInProgress = true; |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 840 | peci_pcie::abortScan = false; |
Paul Fertser | 0734314 | 2022-12-01 21:04:57 +0000 | [diff] [blame] | 841 | scanPCIeDevice(io, objServer, cpuInfo, 0, 0, 0); |
| 842 | } |
| 843 | } |
| 844 | |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 845 | static void peciAvailableCheck( |
| 846 | boost::asio::steady_timer& peciWaitTimer, boost::asio::io_context& io, |
| 847 | sdbusplus::asio::object_server& objServer, std::vector<CPUInfo>& cpuInfo) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 848 | { |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 849 | static bool lastPECIState = false; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 850 | bool peciAvailable = isPECIAvailable(); |
Paul Fertser | 3b2afcb | 2022-12-16 12:17:15 +0000 | [diff] [blame] | 851 | if constexpr (peci_pcie::debug) |
| 852 | { |
| 853 | std::cerr << "peciAvailableCheck " << peciAvailable << " " |
| 854 | << lastPECIState << " " << peci_pcie::abortScan << "\n"; |
| 855 | } |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 856 | if (peciAvailable && (!lastPECIState || peci_pcie::abortScan)) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 857 | { |
| 858 | lastPECIState = true; |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 859 | auto pcieTimeout = std::make_shared<boost::asio::steady_timer>(io); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 860 | constexpr const int pcieWaitTime = 60; |
Paul Fertser | 3b2afcb | 2022-12-16 12:17:15 +0000 | [diff] [blame] | 861 | if constexpr (peci_pcie::debug) |
| 862 | { |
| 863 | std::cerr << "Scanning in 60 seconds\n"; |
| 864 | } |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 865 | pcieTimeout->expires_after(std::chrono::seconds(pcieWaitTime)); |
| 866 | pcieTimeout->async_wait([&io, &objServer, &cpuInfo, pcieTimeout]( |
| 867 | const boost::system::error_code& ec) { |
| 868 | if (ec) |
| 869 | { |
| 870 | // operation_aborted is expected if timer is canceled |
| 871 | // before completion. |
| 872 | if (ec != boost::asio::error::operation_aborted) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 873 | { |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 874 | std::cerr << "PECI PCIe async_wait failed " << ec; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 875 | } |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 876 | lastPECIState = false; |
| 877 | return; |
| 878 | } |
| 879 | startPCIeScan(io, objServer, cpuInfo); |
| 880 | }); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 881 | } |
| 882 | else if (!peciAvailable && lastPECIState) |
| 883 | { |
| 884 | lastPECIState = false; |
| 885 | } |
| 886 | |
| 887 | peciWaitTimer.expires_after( |
| 888 | std::chrono::seconds(peci_pcie::peciCheckInterval)); |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 889 | peciWaitTimer.async_wait([&peciWaitTimer, &io, &objServer, |
| 890 | &cpuInfo](const boost::system::error_code& ec) { |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 891 | if (ec) |
| 892 | { |
| 893 | // operation_aborted is expected if timer is canceled |
| 894 | // before completion. |
| 895 | if (ec != boost::asio::error::operation_aborted) |
| 896 | { |
| 897 | std::cerr << "PECI Available Check async_wait failed " << ec; |
| 898 | } |
| 899 | return; |
| 900 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 901 | peciAvailableCheck(peciWaitTimer, io, objServer, cpuInfo); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 902 | }); |
| 903 | } |
| 904 | |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 905 | static void waitForOSStandbyDelay( |
| 906 | boost::asio::io_context& io, sdbusplus::asio::object_server& objServer, |
| 907 | boost::asio::steady_timer& osStandbyTimer, std::vector<CPUInfo>& cpuInfo) |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 908 | { |
| 909 | osStandbyTimer.expires_after( |
| 910 | std::chrono::seconds(peci_pcie::osStandbyDelaySeconds)); |
| 911 | |
| 912 | osStandbyTimer.async_wait( |
| 913 | [&io, &objServer, &cpuInfo](const boost::system::error_code& ec) { |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 914 | if (ec == boost::asio::error::operation_aborted) |
| 915 | { |
| 916 | return; // we're being canceled |
| 917 | } |
| 918 | else if (ec) |
| 919 | { |
| 920 | std::cerr << "OS Standby async_wait failed: " << ec.value() |
| 921 | << ": " << ec.message() << "\n"; |
| 922 | return; |
| 923 | } |
| 924 | startPCIeScan(io, objServer, cpuInfo); |
| 925 | }); |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 928 | [[maybe_unused]] static void monitorOSStandby( |
| 929 | boost::asio::io_context& io, |
| 930 | std::shared_ptr<sdbusplus::asio::connection> conn, |
| 931 | sdbusplus::asio::object_server& objServer, |
| 932 | boost::asio::steady_timer& osStandbyTimer, std::vector<CPUInfo>& cpuInfo) |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 933 | { |
| 934 | std::cerr << "Start OperatingSystemState Monitor\n"; |
| 935 | |
Patrick Williams | b251708 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 936 | static sdbusplus::bus::match_t osStateMatch( |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 937 | *conn, |
| 938 | "type='signal',interface='org.freedesktop.DBus.Properties',member='" |
| 939 | "PropertiesChanged',arg0='xyz.openbmc_project.State.OperatingSystem." |
| 940 | "Status'", |
| 941 | [&io, &objServer, &osStandbyTimer, |
Patrick Williams | b251708 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 942 | &cpuInfo](sdbusplus::message_t& msg) { |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 943 | // Get the OS State from the message |
| 944 | std::string osStateInterface; |
| 945 | boost::container::flat_map<std::string, std::variant<std::string>> |
| 946 | propertiesChanged; |
| 947 | msg.read(osStateInterface, propertiesChanged); |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 948 | |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 949 | for (const auto& [name, value] : propertiesChanged) |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 950 | { |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 951 | if (name == "OperatingSystemState") |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 952 | { |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 953 | const std::string* state = std::get_if<std::string>(&value); |
| 954 | if (state == nullptr) |
| 955 | { |
| 956 | std::cerr << "Unable to read OS state value\n"; |
| 957 | return; |
| 958 | } |
| 959 | // Note: Short version of OperatingSystemState value is |
| 960 | // deprecated and would be removed in the future |
| 961 | if ((*state == "Standby") || |
| 962 | (*state == "xyz.openbmc_project.State.OperatingSystem." |
| 963 | "Status.OSStatus.Standby")) |
| 964 | { |
| 965 | peci_pcie::abortScan = false; |
| 966 | waitForOSStandbyDelay(io, objServer, osStandbyTimer, |
| 967 | cpuInfo); |
| 968 | } |
| 969 | else if ((*state == "Inactive") || |
| 970 | (*state == |
| 971 | "xyz.openbmc_project.State.OperatingSystem." |
| 972 | "Status.OSStatus.Inactive")) |
| 973 | { |
| 974 | peci_pcie::abortScan = true; |
| 975 | osStandbyTimer.cancel(); |
| 976 | } |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 977 | } |
| 978 | } |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 979 | }); |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 980 | |
| 981 | // Check if the OS state is already available |
| 982 | conn->async_method_call( |
| 983 | [&io, &objServer, &osStandbyTimer, |
| 984 | &cpuInfo](boost::system::error_code ec, |
| 985 | const std::variant<std::string>& property) { |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 986 | if (ec) |
| 987 | { |
| 988 | std::cerr << "error with OS state async_method_call\n"; |
| 989 | return; |
| 990 | } |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 991 | |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 992 | const std::string* state = std::get_if<std::string>(&property); |
| 993 | if (state == nullptr) |
| 994 | { |
| 995 | std::cerr << "Unable to read OS state value\n"; |
| 996 | return; |
| 997 | } |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 998 | |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 999 | // If the OS state is in Standby, then BIOS is done and we can |
| 1000 | // continue. Otherwise, we just wait for the match |
| 1001 | // Note: Short version of OperatingSystemState value is deprecated |
| 1002 | // and would be removed in the future |
Andrei Kartashev | 328685e | 2021-12-27 17:24:18 +0300 | [diff] [blame] | 1003 | |
Patrick Williams | 39c664c | 2024-08-16 15:21:58 -0400 | [diff] [blame] | 1004 | if ((*state == "Standby") || |
| 1005 | (*state == "xyz.openbmc_project.State.OperatingSystem.Status." |
| 1006 | "OSStatus.Standby")) |
| 1007 | { |
| 1008 | waitForOSStandbyDelay(io, objServer, osStandbyTimer, cpuInfo); |
| 1009 | } |
| 1010 | }, |
Potin Lai | f3f9deb | 2024-02-26 14:34:38 +0800 | [diff] [blame] | 1011 | "xyz.openbmc_project.State.Host0", "/xyz/openbmc_project/state/host0", |
| 1012 | "org.freedesktop.DBus.Properties", "Get", |
| 1013 | "xyz.openbmc_project.State.OperatingSystem.Status", |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 1014 | "OperatingSystemState"); |
| 1015 | } |
| 1016 | |
Jason M. Bills | 3570b9e | 2023-07-31 12:26:30 -0700 | [diff] [blame] | 1017 | int main(int /*argc*/, char* /*argv*/[]) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 1018 | { |
| 1019 | // setup connection to dbus |
Ed Tanous | 985d9d9 | 2023-03-01 10:35:33 -0800 | [diff] [blame] | 1020 | boost::asio::io_context io; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 1021 | std::shared_ptr<sdbusplus::asio::connection> conn = |
| 1022 | std::make_shared<sdbusplus::asio::connection>(io); |
| 1023 | |
| 1024 | // PECI PCIe Object |
| 1025 | conn->request_name(peci_pcie::peciPCIeObject); |
| 1026 | sdbusplus::asio::object_server server = |
| 1027 | sdbusplus::asio::object_server(conn); |
| 1028 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 1029 | // CPU map |
| 1030 | std::vector<CPUInfo> cpuInfo; |
| 1031 | |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 1032 | #ifdef WAIT_FOR_OS_STANDBY |
| 1033 | boost::asio::steady_timer osStandbyTimer(io); |
| 1034 | monitorOSStandby(io, conn, server, osStandbyTimer, cpuInfo); |
| 1035 | #else |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 1036 | // Start the PECI check loop |
| 1037 | boost::asio::steady_timer peciWaitTimer( |
| 1038 | io, std::chrono::seconds(peci_pcie::peciCheckInterval)); |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 1039 | peciWaitTimer.async_wait([&peciWaitTimer, &io, &server, |
| 1040 | &cpuInfo](const boost::system::error_code& ec) { |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 1041 | if (ec) |
| 1042 | { |
| 1043 | // operation_aborted is expected if timer is canceled |
| 1044 | // before completion. |
| 1045 | if (ec != boost::asio::error::operation_aborted) |
| 1046 | { |
| 1047 | std::cerr << "PECI Available Check async_wait failed " << ec; |
| 1048 | } |
| 1049 | return; |
| 1050 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 1051 | peciAvailableCheck(peciWaitTimer, io, server, cpuInfo); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 1052 | }); |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 1053 | #endif |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 1054 | |
| 1055 | io.run(); |
| 1056 | |
| 1057 | return 0; |
| 1058 | } |