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