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 | { |
| 115 | case skx: |
| 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, |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 121 | (uint8_t*)&cpuBusNum, |
| 122 | &cc) != PECI_CC_SUCCESS) |
| 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, |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 128 | (uint8_t*)&cpuBusNum1, |
| 129 | &cc) != PECI_CC_SUCCESS) |
| 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 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 177 | static resCode getDataFromPCIeConfig(const int& clientAddr, const int& bus, |
| 178 | const int& dev, const int& func, |
| 179 | const int& offset, const int& size, |
| 180 | uint32_t& pciData) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 181 | { |
| 182 | // PECI RdPCIConfig() currently only supports 4 byte reads, so adjust |
| 183 | // the offset and size to get the right data |
| 184 | static constexpr const int pciReadSize = 4; |
| 185 | int mod = offset % pciReadSize; |
| 186 | int pciOffset = offset - mod; |
| 187 | if (mod + size > pciReadSize) |
| 188 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 189 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | std::array<uint8_t, pciReadSize> data; |
| 193 | uint8_t cc; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 194 | int ret = PECI_CC_TIMEOUT; |
Paul Fertser | b08723d | 2022-12-16 11:49:08 +0000 | [diff] [blame] | 195 | for (int index = 0; (index < 15) && (ret == PECI_CC_TIMEOUT); index++) |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 196 | { |
Jason M. Bills | 3b1665a | 2021-06-11 13:35:04 -0700 | [diff] [blame] | 197 | #ifdef USE_RDENDPOINTCFG |
| 198 | ret = peci_RdEndPointConfigPci(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 |
| 207 | #else |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 208 | ret = peci_RdPCIConfig(clientAddr, // CPU Address |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 209 | bus, // PCI Bus |
| 210 | dev, // PCI Device |
| 211 | func, // PCI Function |
| 212 | pciOffset, // PCI Offset |
| 213 | data.data(), // PCI Read Data |
| 214 | &cc); // PECI Completion Code |
Jason M. Bills | 3b1665a | 2021-06-11 13:35:04 -0700 | [diff] [blame] | 215 | #endif |
Paul Fertser | 3b2afcb | 2022-12-16 12:17:15 +0000 | [diff] [blame] | 216 | if constexpr (peci_pcie::debug) |
| 217 | { |
| 218 | std::cerr << "Request: bus " << bus << " dev " << dev << " func " |
| 219 | << func << " pciOffset " << pciOffset << " ret: " << ret |
| 220 | << " cc: " << static_cast<int>(cc) << "\n"; |
| 221 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 222 | } |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 223 | if (ret != PECI_CC_SUCCESS) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 224 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 225 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 226 | } |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 227 | else if (cc != PECI_DEV_CC_SUCCESS) |
| 228 | { |
| 229 | return resCode::resSkip; |
| 230 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 231 | |
| 232 | // Now build the requested data into a single number |
| 233 | pciData = 0; |
| 234 | for (int i = mod; i < mod + size; i++) |
| 235 | { |
Jason M. Bills | e55832b | 2021-06-14 16:00:49 -0700 | [diff] [blame] | 236 | pciData |= static_cast<uint32_t>(data[i]) << 8 * (i - mod); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 239 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Andrei Kartashev | 6f55203 | 2021-05-11 21:25:29 +0300 | [diff] [blame] | 242 | static resCode getStringFromData(const int& size, const uint32_t& data, |
| 243 | std::string& res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 244 | { |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 245 | // And convert it to a string |
| 246 | std::stringstream dataStream; |
| 247 | dataStream << "0x" << std::hex << std::setfill('0') << std::setw(size * 2) |
| 248 | << data; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 249 | res = dataStream.str(); |
| 250 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 253 | static resCode getVendorName(const int& clientAddr, const int& bus, |
| 254 | const int& dev, std::string& res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 255 | { |
| 256 | static constexpr const int vendorIDOffset = 0x00; |
| 257 | static constexpr const int vendorIDSize = 2; |
| 258 | |
| 259 | // Get the header type register from function 0 |
| 260 | uint32_t vendorID = 0; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 261 | if (getDataFromPCIeConfig(clientAddr, bus, dev, 0, vendorIDOffset, |
| 262 | vendorIDSize, vendorID) != resCode::resOk) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 263 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 264 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 265 | } |
| 266 | // Get the vendor name or use Other if it doesn't exist |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 267 | res = pciVendors.try_emplace(vendorID, otherVendor).first->second; |
| 268 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 271 | static resCode getDeviceClass(const int& clientAddr, const int& bus, |
| 272 | const int& dev, const int& func, std::string& res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 273 | { |
| 274 | static constexpr const int baseClassOffset = 0x0b; |
| 275 | static constexpr const int baseClassSize = 1; |
| 276 | |
| 277 | // Get the Device Base Class |
| 278 | uint32_t baseClass = 0; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 279 | if (getDataFromPCIeConfig(clientAddr, bus, dev, func, baseClassOffset, |
| 280 | baseClassSize, baseClass) != resCode::resOk) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 281 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 282 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 283 | } |
| 284 | // 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] | 285 | res = pciDeviceClasses.try_emplace(baseClass, otherClass).first->second; |
| 286 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Spencer Ku | bb5efe7 | 2021-09-02 16:11:14 +0800 | [diff] [blame] | 289 | static resCode getCapReading(const int& clientAddr, const int& bus, |
| 290 | const int& dev, uint32_t& capReading, |
| 291 | const int compareCapID, const int offsetAddress, |
| 292 | const int offsetLength) |
| 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 | |
| 483 | static void setDefaultPCIeFunctionProperties(const int& clientAddr, |
| 484 | const int& bus, const int& dev, |
| 485 | const int& func) |
| 486 | { |
| 487 | // Set the function-specific properties |
Andrei Kartashev | 6f55203 | 2021-05-11 21:25:29 +0300 | [diff] [blame] | 488 | for (const auto& [name, offset, size] : peci_pcie::pciConfigInfo) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 489 | { |
| 490 | setPCIeProperty(clientAddr, bus, dev, |
| 491 | "Function" + std::to_string(func) + std::string(name), |
| 492 | std::string()); |
| 493 | } |
| 494 | } |
| 495 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 496 | static resCode setPCIeFunctionProperties(const int& clientAddr, const int& bus, |
| 497 | const int& dev, const int& func) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 498 | { |
Andrei Kartashev | 6f55203 | 2021-05-11 21:25:29 +0300 | [diff] [blame] | 499 | uint32_t data = 0; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 500 | std::string res; |
Andrei Kartashev | 6f55203 | 2021-05-11 21:25:29 +0300 | [diff] [blame] | 501 | resCode error; |
| 502 | |
| 503 | for (const auto& [name, offset, size] : peci_pcie::pciConfigInfo) |
| 504 | { |
| 505 | if (offset < 0) |
| 506 | { |
| 507 | continue; |
| 508 | } |
| 509 | |
| 510 | error = getDataFromPCIeConfig(clientAddr, bus, dev, func, offset, size, |
| 511 | data); |
| 512 | if (error != resCode::resOk) |
| 513 | { |
| 514 | return error; |
| 515 | } |
| 516 | getStringFromData(size, data, res); |
| 517 | setPCIeProperty(clientAddr, bus, dev, |
| 518 | "Function" + std::to_string(func) + std::string(name), |
| 519 | res); |
| 520 | } |
| 521 | |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 522 | // Set the function type always to physical for now |
| 523 | setPCIeProperty(clientAddr, bus, dev, |
| 524 | "Function" + std::to_string(func) + |
| 525 | std::string(peci_pcie::function::functionTypeName), |
| 526 | "Physical"); |
| 527 | |
| 528 | // Set the function Device Class |
Andrei Kartashev | 6f55203 | 2021-05-11 21:25:29 +0300 | [diff] [blame] | 529 | error = getDeviceClass(clientAddr, bus, dev, func, res); |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 530 | if (error != resCode::resOk) |
| 531 | { |
| 532 | return error; |
| 533 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 534 | setPCIeProperty(clientAddr, bus, dev, |
| 535 | "Function" + std::to_string(func) + |
| 536 | std::string(peci_pcie::function::deviceClassName), |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 537 | res); |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 538 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 541 | static resCode setPCIeDeviceProperties(const int& clientAddr, const int& bus, |
| 542 | const int& dev) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 543 | { |
| 544 | // Set the device manufacturer |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 545 | std::string manuf; |
| 546 | resCode error = getVendorName(clientAddr, bus, dev, manuf); |
| 547 | if (error != resCode::resOk) |
| 548 | { |
| 549 | return error; |
| 550 | } |
| 551 | setPCIeProperty(clientAddr, bus, dev, "Manufacturer", manuf); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 552 | |
| 553 | // Set the device type |
Patrick Williams | 42a9ac8 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 554 | constexpr const char* deviceTypeName = "DeviceType"; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 555 | bool multiFunc; |
| 556 | error = isMultiFunction(clientAddr, bus, dev, multiFunc); |
| 557 | if (error != resCode::resOk) |
| 558 | { |
| 559 | return error; |
| 560 | } |
| 561 | if (multiFunc) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 562 | { |
| 563 | setPCIeProperty(clientAddr, bus, dev, deviceTypeName, "MultiFunction"); |
| 564 | } |
| 565 | else |
| 566 | { |
| 567 | setPCIeProperty(clientAddr, bus, dev, deviceTypeName, "SingleFunction"); |
| 568 | } |
Spencer Ku | bb5efe7 | 2021-09-02 16:11:14 +0800 | [diff] [blame] | 569 | |
| 570 | // Set PCIe Generation |
Patrick Williams | 42a9ac8 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 571 | constexpr const char* generationInUseName = "GenerationInUse"; |
Spencer Ku | bb5efe7 | 2021-09-02 16:11:14 +0800 | [diff] [blame] | 572 | std::string generationInUse; |
| 573 | error = getGenerationInUse(clientAddr, bus, dev, generationInUse); |
| 574 | if (error == resCode::resErr) |
| 575 | { |
| 576 | return error; |
| 577 | } |
| 578 | // "resSkip" status means it can't get the capability reading, such like |
| 579 | // this device is not PCI Express. |
| 580 | if (error == resCode::resSkip) |
| 581 | { |
| 582 | setPCIeProperty(clientAddr, bus, dev, generationInUseName, ""); |
| 583 | return resCode::resOk; |
| 584 | } |
| 585 | setPCIeProperty(clientAddr, bus, dev, generationInUseName, generationInUse); |
| 586 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 587 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 590 | static resCode updatePCIeDevice(const int& clientAddr, const int& bus, |
| 591 | const int& dev) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 592 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 593 | if (setPCIeDeviceProperties(clientAddr, bus, dev) != resCode::resOk) |
| 594 | { |
| 595 | return resCode::resErr; |
| 596 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 597 | |
Nidhin MS | 8b18f52 | 2023-06-28 10:25:06 +0530 | [diff] [blame] | 598 | bool multiFunc = false; |
| 599 | resCode error = isMultiFunction(clientAddr, bus, dev, multiFunc); |
| 600 | if (error != resCode::resOk) |
| 601 | { |
| 602 | return error; |
| 603 | } |
| 604 | // Functions greater than 0 should only be accessed on multi-function |
| 605 | // devices |
| 606 | int maxPCIFunctions = multiFunc ? peci_pcie::maxPCIFunctions : 1; |
| 607 | |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 608 | // Walk through and populate the functions for this device |
Nidhin MS | 8b18f52 | 2023-06-28 10:25:06 +0530 | [diff] [blame] | 609 | for (int func = 0; func < maxPCIFunctions; func++) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 610 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 611 | bool res; |
| 612 | resCode error = pcieFunctionExists(clientAddr, bus, dev, func, res); |
| 613 | if (error != resCode::resOk) |
| 614 | { |
| 615 | return error; |
| 616 | } |
| 617 | if (res) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 618 | { |
| 619 | // Set the properties for this function |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 620 | if (setPCIeFunctionProperties(clientAddr, bus, dev, func) != |
| 621 | resCode::resOk) |
| 622 | { |
| 623 | return resCode::resErr; |
| 624 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 625 | } |
| 626 | else |
| 627 | { |
| 628 | // Set default properties for unused functions |
| 629 | setDefaultPCIeFunctionProperties(clientAddr, bus, dev, func); |
| 630 | } |
| 631 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 632 | return resCode::resOk; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | static void removePCIeDevice(sdbusplus::asio::object_server& objServer, |
| 636 | const int& clientAddr, const int& bus, |
| 637 | const int& dev) |
| 638 | { |
Lakshmi Yadlapati | 4fe704c | 2023-04-07 08:23:04 -0500 | [diff] [blame] | 639 | peci_pcie::PcieInterfaces ifaces = |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 640 | peci_pcie::pcieDeviceDBusMap[clientAddr][bus][dev]; |
| 641 | |
Lakshmi Yadlapati | 4fe704c | 2023-04-07 08:23:04 -0500 | [diff] [blame] | 642 | objServer.remove_interface(ifaces.deviceIface); |
| 643 | objServer.remove_interface(ifaces.assetIface); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 644 | |
| 645 | peci_pcie::pcieDeviceDBusMap[clientAddr][bus].erase(dev); |
| 646 | if (peci_pcie::pcieDeviceDBusMap[clientAddr][bus].empty()) |
| 647 | { |
| 648 | peci_pcie::pcieDeviceDBusMap[clientAddr].erase(bus); |
| 649 | } |
| 650 | if (peci_pcie::pcieDeviceDBusMap[clientAddr].empty()) |
| 651 | { |
| 652 | peci_pcie::pcieDeviceDBusMap.erase(clientAddr); |
| 653 | } |
| 654 | } |
| 655 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 656 | static resCode addPCIeDevice(sdbusplus::asio::object_server& objServer, |
| 657 | const int& clientAddr, const int& cpu, |
| 658 | const int& bus, const int& dev) |
| 659 | { |
| 660 | std::string pathName = std::string(peci_pcie::peciPCIePath) + "/S" + |
| 661 | std::to_string(cpu) + "B" + std::to_string(bus) + |
| 662 | "D" + std::to_string(dev); |
Lakshmi Yadlapati | 4fe704c | 2023-04-07 08:23:04 -0500 | [diff] [blame] | 663 | peci_pcie::PcieInterfaces ifaces; |
| 664 | ifaces.deviceIface = |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 665 | objServer.add_interface(pathName, peci_pcie::peciPCIeDeviceInterface); |
Lakshmi Yadlapati | 4fe704c | 2023-04-07 08:23:04 -0500 | [diff] [blame] | 666 | ifaces.assetIface = |
| 667 | objServer.add_interface(pathName, peci_pcie::peciPCIeAssetInterface); |
| 668 | peci_pcie::pcieDeviceDBusMap[clientAddr][bus][dev] = ifaces; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 669 | |
| 670 | // Update the properties for the new device |
| 671 | if (updatePCIeDevice(clientAddr, bus, dev) != resCode::resOk) |
| 672 | { |
| 673 | removePCIeDevice(objServer, clientAddr, bus, dev); |
| 674 | return resCode::resErr; |
| 675 | } |
| 676 | |
Lakshmi Yadlapati | 4fe704c | 2023-04-07 08:23:04 -0500 | [diff] [blame] | 677 | ifaces.deviceIface->initialize(); |
| 678 | ifaces.assetIface->initialize(); |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 679 | return resCode::resOk; |
| 680 | } |
| 681 | |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 682 | static bool pcieDeviceInDBusMap(const int& clientAddr, const int& bus, |
| 683 | const int& dev) |
| 684 | { |
| 685 | if (auto clientAddrIt = peci_pcie::pcieDeviceDBusMap.find(clientAddr); |
| 686 | clientAddrIt != peci_pcie::pcieDeviceDBusMap.end()) |
| 687 | { |
| 688 | if (auto busIt = clientAddrIt->second.find(bus); |
| 689 | busIt != clientAddrIt->second.end()) |
| 690 | { |
| 691 | if (auto devIt = busIt->second.find(dev); |
| 692 | devIt != busIt->second.end()) |
| 693 | { |
Lakshmi Yadlapati | 4fe704c | 2023-04-07 08:23:04 -0500 | [diff] [blame] | 694 | if (devIt->second.deviceIface != nullptr || |
| 695 | devIt->second.assetIface != nullptr) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 696 | { |
| 697 | return true; |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | return false; |
| 703 | } |
| 704 | |
Jason M. Bills | c076bd7 | 2023-07-31 12:25:05 -0700 | [diff] [blame^] | 705 | static resCode probePCIeDevice(sdbusplus::asio::object_server& objServer, |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 706 | size_t addr, int cpu, int bus, int dev) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 707 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 708 | bool res; |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 709 | resCode error = pcieDeviceExists(addr, bus, dev, res); |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 710 | if (error != resCode::resOk) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 711 | { |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 712 | return error; |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 713 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 714 | if (res) |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 715 | { |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 716 | if (pcieDeviceInDBusMap(addr, bus, dev)) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 717 | { |
| 718 | // This device is already in D-Bus, so update it |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 719 | if (updatePCIeDevice(addr, bus, dev) != resCode::resOk) |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 720 | { |
| 721 | return resCode::resErr; |
| 722 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 723 | } |
| 724 | else |
| 725 | { |
| 726 | // This device is not in D-Bus, so add it |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 727 | if (addPCIeDevice(objServer, addr, cpu, bus, dev) != resCode::resOk) |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 728 | { |
| 729 | return resCode::resErr; |
| 730 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | else |
| 734 | { |
| 735 | // If PECI is not available, then stop scanning |
| 736 | if (!isPECIAvailable()) |
| 737 | { |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 738 | return resCode::resErr; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 741 | if (pcieDeviceInDBusMap(addr, bus, dev)) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 742 | { |
| 743 | // This device is in D-Bus, so remove it |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 744 | removePCIeDevice(objServer, addr, bus, dev); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 745 | } |
| 746 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 747 | return resCode::resOk; |
| 748 | } |
| 749 | |
Ed Tanous | 985d9d9 | 2023-03-01 10:35:33 -0800 | [diff] [blame] | 750 | static void scanNextPCIeDevice(boost::asio::io_context& io, |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 751 | sdbusplus::asio::object_server& objServer, |
| 752 | std::vector<CPUInfo>& cpuInfo, int cpu, int bus, |
| 753 | int dev); |
Ed Tanous | 985d9d9 | 2023-03-01 10:35:33 -0800 | [diff] [blame] | 754 | static void scanPCIeDevice(boost::asio::io_context& io, |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 755 | sdbusplus::asio::object_server& objServer, |
| 756 | std::vector<CPUInfo>& cpuInfo, int cpu, int bus, |
| 757 | int dev) |
| 758 | { |
Jason M. Bills | c076bd7 | 2023-07-31 12:25:05 -0700 | [diff] [blame^] | 759 | if (cpu >= static_cast<int>(cpuInfo.size())) |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 760 | { |
| 761 | std::cerr << "Request to scan CPU" << cpu |
| 762 | << " while CPU array has size " << cpuInfo.size() << "\n"; |
| 763 | return; |
| 764 | } |
| 765 | auto& info = cpuInfo[cpu]; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 766 | // Check if this is a CPU bus that we should skip |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 767 | if (info.skipCpuBuses && info.cpuBusNums.count(bus)) |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 768 | { |
| 769 | std::cout << "Skipping CPU " << cpu << " Bus Number " << bus << "\n"; |
| 770 | // Skip all the devices on this bus |
| 771 | dev = peci_pcie::maxPCIDevices; |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 772 | } |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 773 | else |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 774 | { |
Jason M. Bills | c076bd7 | 2023-07-31 12:25:05 -0700 | [diff] [blame^] | 775 | if (probePCIeDevice(objServer, info.addr, cpu, bus, dev) != |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 776 | resCode::resOk) |
| 777 | { |
| 778 | std::cerr << "Failed to probe CPU " << cpu << " Bus " << bus |
| 779 | << " Device " << dev << "\n"; |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 780 | peci_pcie::abortScan = true; |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 781 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 782 | } |
| 783 | |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 784 | scanNextPCIeDevice(io, objServer, cpuInfo, cpu, bus, dev); |
Andrei Kartashev | 8e96603 | 2021-07-02 20:04:30 +0300 | [diff] [blame] | 785 | return; |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 786 | } |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 787 | |
Ed Tanous | 985d9d9 | 2023-03-01 10:35:33 -0800 | [diff] [blame] | 788 | static void scanNextPCIeDevice(boost::asio::io_context& io, |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 789 | sdbusplus::asio::object_server& objServer, |
| 790 | std::vector<CPUInfo>& cpuInfo, int cpu, int bus, |
| 791 | int dev) |
| 792 | { |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 793 | if (peci_pcie::abortScan) |
| 794 | { |
Paul Fertser | 0734314 | 2022-12-01 21:04:57 +0000 | [diff] [blame] | 795 | peci_pcie::scanInProgress = false; |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 796 | std::cerr << "PCIe scan aborted\n"; |
| 797 | return; |
| 798 | } |
| 799 | |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 800 | // PCIe Device scan completed, so move to the next device |
| 801 | if (++dev >= peci_pcie::maxPCIDevices) |
| 802 | { |
| 803 | // All devices scanned, so move to the next bus |
| 804 | dev = 0; |
| 805 | if (++bus >= peci_pcie::maxPCIBuses) |
| 806 | { |
| 807 | // All buses scanned, so move to the next CPU |
| 808 | bus = 0; |
Jason M. Bills | c076bd7 | 2023-07-31 12:25:05 -0700 | [diff] [blame^] | 809 | if (++cpu >= static_cast<int>(cpuInfo.size())) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 810 | { |
| 811 | // All CPUs scanned, so we're done |
Paul Fertser | 0734314 | 2022-12-01 21:04:57 +0000 | [diff] [blame] | 812 | peci_pcie::scanInProgress = false; |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 813 | std::cerr << "PCIe scan completed\n"; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 814 | return; |
| 815 | } |
| 816 | } |
| 817 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 818 | boost::asio::post(io, [&io, &objServer, &cpuInfo, cpu, bus, dev]() mutable { |
Jason M. Bills | 5d04973 | 2019-10-25 15:55:15 -0700 | [diff] [blame] | 819 | scanPCIeDevice(io, objServer, cpuInfo, cpu, bus, dev); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 820 | }); |
| 821 | } |
| 822 | |
Ed Tanous | 985d9d9 | 2023-03-01 10:35:33 -0800 | [diff] [blame] | 823 | static void startPCIeScan(boost::asio::io_context& io, |
Paul Fertser | 0734314 | 2022-12-01 21:04:57 +0000 | [diff] [blame] | 824 | sdbusplus::asio::object_server& objServer, |
| 825 | std::vector<CPUInfo>& cpuInfo) |
| 826 | { |
| 827 | if (!peci_pcie::scanInProgress) |
| 828 | { |
| 829 | // get the PECI client address list |
Paul Fertser | 3b2afcb | 2022-12-16 12:17:15 +0000 | [diff] [blame] | 830 | std::cerr << "Getting map\n"; |
Paul Fertser | 0734314 | 2022-12-01 21:04:57 +0000 | [diff] [blame] | 831 | if (getCPUBusMap(cpuInfo) != resCode::resOk) |
| 832 | { |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 833 | peci_pcie::abortScan = true; |
Paul Fertser | 0734314 | 2022-12-01 21:04:57 +0000 | [diff] [blame] | 834 | return; |
| 835 | } |
| 836 | std::cerr << "PCIe scan started\n"; |
| 837 | // scan PCIe starting from CPU 0, Bus 0, Device 0 |
| 838 | peci_pcie::scanInProgress = true; |
Paul Fertser | 541637c | 2022-12-02 13:42:01 +0000 | [diff] [blame] | 839 | peci_pcie::abortScan = false; |
Paul Fertser | 0734314 | 2022-12-01 21:04:57 +0000 | [diff] [blame] | 840 | scanPCIeDevice(io, objServer, cpuInfo, 0, 0, 0); |
| 841 | } |
| 842 | } |
| 843 | |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 844 | static void peciAvailableCheck(boost::asio::steady_timer& peciWaitTimer, |
Ed Tanous | 985d9d9 | 2023-03-01 10:35:33 -0800 | [diff] [blame] | 845 | boost::asio::io_context& io, |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 846 | sdbusplus::asio::object_server& objServer, |
| 847 | 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 | |
Ed Tanous | 985d9d9 | 2023-03-01 10:35:33 -0800 | [diff] [blame] | 905 | static void waitForOSStandbyDelay(boost::asio::io_context& io, |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 906 | sdbusplus::asio::object_server& objServer, |
| 907 | boost::asio::steady_timer& osStandbyTimer, |
| 908 | std::vector<CPUInfo>& cpuInfo) |
| 909 | { |
| 910 | osStandbyTimer.expires_after( |
| 911 | std::chrono::seconds(peci_pcie::osStandbyDelaySeconds)); |
| 912 | |
| 913 | osStandbyTimer.async_wait( |
| 914 | [&io, &objServer, &cpuInfo](const boost::system::error_code& ec) { |
Patrick Williams | 42a9ac8 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 915 | if (ec == boost::asio::error::operation_aborted) |
| 916 | { |
| 917 | return; // we're being canceled |
| 918 | } |
| 919 | else if (ec) |
| 920 | { |
| 921 | std::cerr << "OS Standby async_wait failed: " << ec.value() << ": " |
| 922 | << ec.message() << "\n"; |
| 923 | return; |
| 924 | } |
| 925 | startPCIeScan(io, objServer, cpuInfo); |
| 926 | }); |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 927 | } |
| 928 | |
Jason M. Bills | c076bd7 | 2023-07-31 12:25:05 -0700 | [diff] [blame^] | 929 | [[maybe_unused]] static void |
| 930 | monitorOSStandby(boost::asio::io_context& io, |
| 931 | std::shared_ptr<sdbusplus::asio::connection> conn, |
| 932 | sdbusplus::asio::object_server& objServer, |
| 933 | boost::asio::steady_timer& osStandbyTimer, |
| 934 | std::vector<CPUInfo>& cpuInfo) |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 935 | { |
| 936 | std::cerr << "Start OperatingSystemState Monitor\n"; |
| 937 | |
Patrick Williams | b251708 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 938 | static sdbusplus::bus::match_t osStateMatch( |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 939 | *conn, |
| 940 | "type='signal',interface='org.freedesktop.DBus.Properties',member='" |
| 941 | "PropertiesChanged',arg0='xyz.openbmc_project.State.OperatingSystem." |
| 942 | "Status'", |
| 943 | [&io, &objServer, &osStandbyTimer, |
Patrick Williams | b251708 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 944 | &cpuInfo](sdbusplus::message_t& msg) { |
Patrick Williams | 42a9ac8 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 945 | // Get the OS State from the message |
| 946 | std::string osStateInterface; |
| 947 | boost::container::flat_map<std::string, std::variant<std::string>> |
| 948 | propertiesChanged; |
| 949 | msg.read(osStateInterface, propertiesChanged); |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 950 | |
Patrick Williams | 42a9ac8 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 951 | for (const auto& [name, value] : propertiesChanged) |
| 952 | { |
| 953 | if (name == "OperatingSystemState") |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 954 | { |
Patrick Williams | 42a9ac8 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 955 | const std::string* state = std::get_if<std::string>(&value); |
| 956 | if (state == nullptr) |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 957 | { |
Patrick Williams | 42a9ac8 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 958 | std::cerr << "Unable to read OS state value\n"; |
| 959 | return; |
| 960 | } |
| 961 | // Note: Short version of OperatingSystemState value is |
| 962 | // deprecated and would be removed in the future |
| 963 | if ((*state == "Standby") || |
| 964 | (*state == "xyz.openbmc_project.State.OperatingSystem." |
| 965 | "Status.OSStatus.Standby")) |
| 966 | { |
| 967 | peci_pcie::abortScan = false; |
| 968 | waitForOSStandbyDelay(io, objServer, osStandbyTimer, |
| 969 | cpuInfo); |
| 970 | } |
| 971 | else if ((*state == "Inactive") || |
| 972 | (*state == "xyz.openbmc_project.State.OperatingSystem." |
| 973 | "Status.OSStatus.Inactive")) |
| 974 | { |
| 975 | peci_pcie::abortScan = true; |
| 976 | osStandbyTimer.cancel(); |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 977 | } |
| 978 | } |
Patrick Williams | 42a9ac8 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 979 | } |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 980 | }); |
| 981 | |
| 982 | // Check if the OS state is already available |
| 983 | conn->async_method_call( |
| 984 | [&io, &objServer, &osStandbyTimer, |
| 985 | &cpuInfo](boost::system::error_code ec, |
| 986 | const std::variant<std::string>& property) { |
Patrick Williams | 42a9ac8 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 987 | if (ec) |
| 988 | { |
| 989 | std::cerr << "error with OS state async_method_call\n"; |
| 990 | return; |
| 991 | } |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 992 | |
Patrick Williams | 42a9ac8 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 993 | const std::string* state = std::get_if<std::string>(&property); |
| 994 | if (state == nullptr) |
| 995 | { |
| 996 | std::cerr << "Unable to read OS state value\n"; |
| 997 | return; |
| 998 | } |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 999 | |
Patrick Williams | 42a9ac8 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 1000 | // If the OS state is in Standby, then BIOS is done and we can |
| 1001 | // continue. Otherwise, we just wait for the match |
| 1002 | // Note: Short version of OperatingSystemState value is deprecated |
| 1003 | // and would be removed in the future |
Andrei Kartashev | 328685e | 2021-12-27 17:24:18 +0300 | [diff] [blame] | 1004 | |
Patrick Williams | 42a9ac8 | 2023-05-10 07:51:22 -0500 | [diff] [blame] | 1005 | if ((*state == "Standby") || |
| 1006 | (*state == "xyz.openbmc_project.State.OperatingSystem.Status." |
| 1007 | "OSStatus.Standby")) |
| 1008 | { |
| 1009 | waitForOSStandbyDelay(io, objServer, osStandbyTimer, cpuInfo); |
| 1010 | } |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 1011 | }, |
| 1012 | "xyz.openbmc_project.State.OperatingSystem", |
| 1013 | "/xyz/openbmc_project/state/os", "org.freedesktop.DBus.Properties", |
| 1014 | "Get", "xyz.openbmc_project.State.OperatingSystem.Status", |
| 1015 | "OperatingSystemState"); |
| 1016 | } |
| 1017 | |
Jason M. Bills | c076bd7 | 2023-07-31 12:25:05 -0700 | [diff] [blame^] | 1018 | int main(int /*argc*/, char** /*argv*/) |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 1019 | { |
| 1020 | // setup connection to dbus |
Ed Tanous | 985d9d9 | 2023-03-01 10:35:33 -0800 | [diff] [blame] | 1021 | boost::asio::io_context io; |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 1022 | std::shared_ptr<sdbusplus::asio::connection> conn = |
| 1023 | std::make_shared<sdbusplus::asio::connection>(io); |
| 1024 | |
| 1025 | // PECI PCIe Object |
| 1026 | conn->request_name(peci_pcie::peciPCIeObject); |
| 1027 | sdbusplus::asio::object_server server = |
| 1028 | sdbusplus::asio::object_server(conn); |
| 1029 | |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 1030 | // CPU map |
| 1031 | std::vector<CPUInfo> cpuInfo; |
| 1032 | |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 1033 | #ifdef WAIT_FOR_OS_STANDBY |
| 1034 | boost::asio::steady_timer osStandbyTimer(io); |
| 1035 | monitorOSStandby(io, conn, server, osStandbyTimer, cpuInfo); |
| 1036 | #else |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 1037 | // Start the PECI check loop |
| 1038 | boost::asio::steady_timer peciWaitTimer( |
| 1039 | io, std::chrono::seconds(peci_pcie::peciCheckInterval)); |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 1040 | peciWaitTimer.async_wait([&peciWaitTimer, &io, &server, |
| 1041 | &cpuInfo](const boost::system::error_code& ec) { |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 1042 | if (ec) |
| 1043 | { |
| 1044 | // operation_aborted is expected if timer is canceled |
| 1045 | // before completion. |
| 1046 | if (ec != boost::asio::error::operation_aborted) |
| 1047 | { |
| 1048 | std::cerr << "PECI Available Check async_wait failed " << ec; |
| 1049 | } |
| 1050 | return; |
| 1051 | } |
Andrei Kartashev | d570dfd | 2020-12-16 17:33:16 +0300 | [diff] [blame] | 1052 | peciAvailableCheck(peciWaitTimer, io, server, cpuInfo); |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 1053 | }); |
Jason M. Bills | ee6d80b | 2021-06-11 07:37:30 -0700 | [diff] [blame] | 1054 | #endif |
Jason M. Bills | d1e4060 | 2019-05-09 11:43:51 -0700 | [diff] [blame] | 1055 | |
| 1056 | io.run(); |
| 1057 | |
| 1058 | return 0; |
| 1059 | } |