blob: 32b4e6fcfec1f14b720e568eca703220bf4c3b1f [file] [log] [blame]
Jason M. Billsd1e40602019-05-09 11:43:51 -07001/*
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 Tanous985d9d92023-03-01 10:35:33 -080022#include <boost/asio/io_context.hpp>
Jason M. Billsbce86a62020-10-08 16:03:47 -070023#include <boost/asio/steady_timer.hpp>
Jason M. Billsd1e40602019-05-09 11:43:51 -070024#include <boost/container/flat_map.hpp>
25#include <boost/container/flat_set.hpp>
26#include <sdbusplus/asio/object_server.hpp>
Jason M. Billsee6d80b2021-06-11 07:37:30 -070027#include <sdbusplus/bus/match.hpp>
Jason M. Billsd1e40602019-05-09 11:43:51 -070028
29#include <iomanip>
30#include <iostream>
31#include <set>
32#include <sstream>
Willy Tu975faf72022-09-23 18:39:16 +000033#include <unordered_set>
Jason M. Billsd1e40602019-05-09 11:43:51 -070034
35namespace peci_pcie
36{
Lakshmi Yadlapati4fe704c2023-04-07 08:23:04 -050037
38struct PcieInterfaces
39{
40 std::shared_ptr<sdbusplus::asio::dbus_interface> deviceIface;
41 std::shared_ptr<sdbusplus::asio::dbus_interface> assetIface;
42};
43
Jason M. Billsd1e40602019-05-09 11:43:51 -070044static boost::container::flat_map<
45 int, boost::container::flat_map<
Lakshmi Yadlapati4fe704c2023-04-07 08:23:04 -050046 int, boost::container::flat_map<int, PcieInterfaces>>>
Jason M. Billsd1e40602019-05-09 11:43:51 -070047 pcieDeviceDBusMap;
48
Jason M. Billsee6d80b2021-06-11 07:37:30 -070049static bool abortScan;
Paul Fertser07343142022-12-01 21:04:57 +000050static bool scanInProgress;
Jason M. Billsee6d80b2021-06-11 07:37:30 -070051
Paul Fertser3b2afcb2022-12-16 12:17:15 +000052constexpr const bool debug = false;
53
Jason M. Billsd1e40602019-05-09 11:43:51 -070054namespace function
55{
Patrick Williams42a9ac82023-05-10 07:51:22 -050056static constexpr const char* functionTypeName = "FunctionType";
57static constexpr const char* deviceClassName = "DeviceClass";
58static constexpr const char* vendorIdName = "VendorId";
59static constexpr const char* deviceIdName = "DeviceId";
60static constexpr const char* classCodeName = "ClassCode";
61static constexpr const char* revisionIdName = "RevisionId";
62static constexpr const char* subsystemIdName = "SubsystemId";
63static constexpr const char* subsystemVendorIdName = "SubsystemVendorId";
Jason M. Billsd1e40602019-05-09 11:43:51 -070064} // namespace function
Andrei Kartashev6f552032021-05-11 21:25:29 +030065
66static 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. Billsd1e40602019-05-09 11:43:51 -070076} // namespace peci_pcie
77
Andrei Kartashevd570dfd2020-12-16 17:33:16 +030078enum class resCode
79{
80 resOk,
Spencer Kubb5efe72021-09-02 16:11:14 +080081 resSkip,
Andrei Kartashevd570dfd2020-12-16 17:33:16 +030082 resErr
83};
84
Jason M. Bills5d049732019-10-25 15:55:15 -070085struct CPUInfo
Jason M. Billsd1e40602019-05-09 11:43:51 -070086{
Jason M. Bills5d049732019-10-25 15:55:15 -070087 size_t addr;
88 bool skipCpuBuses;
89 boost::container::flat_set<size_t> cpuBusNums;
90};
91
92// PECI Client Address Map
Andrei Kartashev8e966032021-07-02 20:04:30 +030093static resCode getCPUBusMap(std::vector<CPUInfo>& cpuInfo)
Jason M. Bills5d049732019-10-25 15:55:15 -070094{
Andrei Kartashevd570dfd2020-12-16 17:33:16 +030095 cpuInfo.clear();
Andrei Kartashev8e966032021-07-02 20:04:30 +030096 for (size_t addr = MIN_CLIENT_ADDR; addr <= MAX_CLIENT_ADDR; addr++)
Jason M. Billsd1e40602019-05-09 11:43:51 -070097 {
Andrei Kartashev8e966032021-07-02 20:04:30 +030098 if (peci_Ping(addr) != PECI_CC_SUCCESS)
Jason M. Billsd1e40602019-05-09 11:43:51 -070099 {
Andrei Kartashev8e966032021-07-02 20:04:30 +0300100 continue;
Jason M. Bills5d049732019-10-25 15:55:15 -0700101 }
Jason M. Bills5d049732019-10-25 15:55:15 -0700102
Andrei Kartashev8e966032021-07-02 20:04:30 +0300103 auto& cpu = cpuInfo.emplace_back(CPUInfo{addr, false, {}});
Jason M. Bills5d049732019-10-25 15:55:15 -0700104 uint8_t cc = 0;
105 CPUModel model{};
106 uint8_t stepping = 0;
Andrei Kartashev8e966032021-07-02 20:04:30 +0300107 if (peci_GetCPUID(addr, &model, &stepping, &cc) != PECI_CC_SUCCESS)
Jason M. Bills5d049732019-10-25 15:55:15 -0700108 {
109 std::cerr << "Cannot get CPUID!\n";
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300110 return resCode::resErr;
Jason M. Bills5d049732019-10-25 15:55:15 -0700111 }
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 Kartashev8e966032021-07-02 20:04:30 +0300120 if (peci_RdPCIConfigLocal(addr, 0, 8, 2, 0xCC, 4,
Jason M. Bills5d049732019-10-25 15:55:15 -0700121 (uint8_t*)&cpuBusNum,
122 &cc) != PECI_CC_SUCCESS)
123 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300124 return resCode::resErr;
Jason M. Bills5d049732019-10-25 15:55:15 -0700125 }
126 uint32_t cpuBusNum1 = 0;
Andrei Kartashev8e966032021-07-02 20:04:30 +0300127 if (peci_RdPCIConfigLocal(addr, 0, 8, 2, 0xD0, 4,
Jason M. Bills5d049732019-10-25 15:55:15 -0700128 (uint8_t*)&cpuBusNum1,
129 &cc) != PECI_CC_SUCCESS)
130 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300131 return resCode::resErr;
Jason M. Bills5d049732019-10-25 15:55:15 -0700132 }
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 Weiss9fa54b52020-09-02 21:20:33 +0000149 cpuBusNum1 >>= 8;
Jason M. Bills5d049732019-10-25 15:55:15 -0700150 }
151 cpu.skipCpuBuses = true;
152 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700153 }
154 }
Andrei Kartashev8e966032021-07-02 20:04:30 +0300155 return cpuInfo.empty() ? resCode::resErr : resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700156}
157
158static bool isPECIAvailable(void)
159{
Jason M. Bills5d049732019-10-25 15:55:15 -0700160 for (size_t i = MIN_CLIENT_ADDR; i <= MAX_CLIENT_ADDR; i++)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700161 {
Jason M. Bills5d049732019-10-25 15:55:15 -0700162 if (peci_Ping(i) == PECI_CC_SUCCESS)
163 {
164 return true;
165 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700166 }
Jason M. Bills5d049732019-10-25 15:55:15 -0700167 return false;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700168}
169
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300170static 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. Billsd1e40602019-05-09 11:43:51 -0700174{
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 Kartashevd570dfd2020-12-16 17:33:16 +0300182 return resCode::resErr;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700183 }
184
185 std::array<uint8_t, pciReadSize> data;
186 uint8_t cc;
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300187 int ret = PECI_CC_TIMEOUT;
Paul Fertserb08723d2022-12-16 11:49:08 +0000188 for (int index = 0; (index < 15) && (ret == PECI_CC_TIMEOUT); index++)
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300189 {
Jason M. Bills3b1665a2021-06-11 13:35:04 -0700190#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 Kartashevd570dfd2020-12-16 17:33:16 +0300201 ret = peci_RdPCIConfig(clientAddr, // CPU Address
Jason M. Billsd1e40602019-05-09 11:43:51 -0700202 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. Bills3b1665a2021-06-11 13:35:04 -0700208#endif
Paul Fertser3b2afcb2022-12-16 12:17:15 +0000209 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 Kartashevd570dfd2020-12-16 17:33:16 +0300215 }
Paul Fertser541637c2022-12-02 13:42:01 +0000216 if (ret != PECI_CC_SUCCESS)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700217 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300218 return resCode::resErr;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700219 }
Paul Fertser541637c2022-12-02 13:42:01 +0000220 else if (cc != PECI_DEV_CC_SUCCESS)
221 {
222 return resCode::resSkip;
223 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700224
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. Billse55832b2021-06-14 16:00:49 -0700229 pciData |= static_cast<uint32_t>(data[i]) << 8 * (i - mod);
Jason M. Billsd1e40602019-05-09 11:43:51 -0700230 }
231
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300232 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700233}
234
Andrei Kartashev6f552032021-05-11 21:25:29 +0300235static resCode getStringFromData(const int& size, const uint32_t& data,
236 std::string& res)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700237{
Jason M. Billsd1e40602019-05-09 11:43:51 -0700238 // 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 Kartashevd570dfd2020-12-16 17:33:16 +0300242 res = dataStream.str();
243 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700244}
245
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300246static resCode getVendorName(const int& clientAddr, const int& bus,
247 const int& dev, std::string& res)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700248{
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 Kartashevd570dfd2020-12-16 17:33:16 +0300254 if (getDataFromPCIeConfig(clientAddr, bus, dev, 0, vendorIDOffset,
255 vendorIDSize, vendorID) != resCode::resOk)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700256 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300257 return resCode::resErr;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700258 }
259 // Get the vendor name or use Other if it doesn't exist
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300260 res = pciVendors.try_emplace(vendorID, otherVendor).first->second;
261 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700262}
263
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300264static resCode getDeviceClass(const int& clientAddr, const int& bus,
265 const int& dev, const int& func, std::string& res)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700266{
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 Kartashevd570dfd2020-12-16 17:33:16 +0300272 if (getDataFromPCIeConfig(clientAddr, bus, dev, func, baseClassOffset,
273 baseClassSize, baseClass) != resCode::resOk)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700274 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300275 return resCode::resErr;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700276 }
277 // Get the base class name or use Other if it doesn't exist
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300278 res = pciDeviceClasses.try_emplace(baseClass, otherClass).first->second;
279 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700280}
281
Spencer Kubb5efe72021-09-02 16:11:14 +0800282static 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
329static resCode getGenerationInUse(const int& clientAddr, const int& bus,
330 const int& dev, std::string& generationInUse)
331{
Willy Tu975faf72022-09-23 18:39:16 +0000332 static std::unordered_set<uint32_t> linkSpeedSkipMap;
Spencer Kubb5efe72021-09-02 16:11:14 +0800333 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 Tu975faf72022-09-23 18:39:16 +0000379 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 Kubb5efe72021-09-02 16:11:14 +0800385 error = resCode::resSkip;
386 }
387 return error;
388}
389
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300390static resCode isMultiFunction(const int& clientAddr, const int& bus,
391 const int& dev, bool& res)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700392{
393 static constexpr const int headerTypeOffset = 0x0e;
394 static constexpr const int headerTypeSize = 1;
395 static constexpr const int multiFuncBit = 1 << 7;
396
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300397 res = false;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700398 // Get the header type register from function 0
399 uint32_t headerType = 0;
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300400 if (getDataFromPCIeConfig(clientAddr, bus, dev, 0, headerTypeOffset,
401 headerTypeSize, headerType) != resCode::resOk)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700402 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300403 return resCode::resErr;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700404 }
405 // Check if it's a multifunction device
406 if (headerType & multiFuncBit)
407 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300408 res = true;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700409 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300410 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700411}
412
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300413static resCode pcieFunctionExists(const int& clientAddr, const int& bus,
414 const int& dev, const int& func, bool& res)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700415{
416 constexpr const int pciIDOffset = 0;
417 constexpr const int pciIDSize = 4;
418 uint32_t pciID = 0;
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300419 res = false;
Paul Fertser541637c2022-12-02 13:42:01 +0000420
421 resCode error = getDataFromPCIeConfig(clientAddr, bus, dev, func,
422 pciIDOffset, pciIDSize, pciID);
423 if (error == resCode::resSkip)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700424 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300425 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700426 }
Paul Fertser541637c2022-12-02 13:42:01 +0000427 else if (error == resCode::resErr)
428 {
429 return resCode::resErr;
430 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700431
432 // if VID and DID are all 0s or 1s, then the device doesn't exist
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300433 if (pciID != 0x00000000 && pciID != 0xFFFFFFFF)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700434 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300435 res = true;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700436 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300437 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700438}
439
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300440static resCode pcieDeviceExists(const int& clientAddr, const int& bus,
441 const int& dev, bool& res)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700442{
443 // Check if this device exists by checking function 0
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300444 return pcieFunctionExists(clientAddr, bus, dev, 0, res);
Jason M. Billsd1e40602019-05-09 11:43:51 -0700445}
446
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300447static resCode setPCIeProperty(const int& clientAddr, const int& bus,
448 const int& dev, const std::string& propertyName,
449 const std::string& propertyValue)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700450{
Lakshmi Yadlapati4fe704c2023-04-07 08:23:04 -0500451 peci_pcie::PcieInterfaces pcieIfaces =
Jason M. Billsd1e40602019-05-09 11:43:51 -0700452 peci_pcie::pcieDeviceDBusMap[clientAddr][bus][dev];
453
Lakshmi Yadlapati4fe704c2023-04-07 08:23:04 -0500454 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. Billsd1e40602019-05-09 11:43:51 -0700464 if (iface->is_initialized())
465 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300466 if (!iface->set_property(propertyName, propertyValue))
467 return resCode::resErr;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700468 }
469 else
470 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300471 if (!iface->register_property(propertyName, propertyValue))
472 return resCode::resErr;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700473 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300474 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700475}
476
477static void setDefaultPCIeFunctionProperties(const int& clientAddr,
478 const int& bus, const int& dev,
479 const int& func)
480{
481 // Set the function-specific properties
Andrei Kartashev6f552032021-05-11 21:25:29 +0300482 for (const auto& [name, offset, size] : peci_pcie::pciConfigInfo)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700483 {
484 setPCIeProperty(clientAddr, bus, dev,
485 "Function" + std::to_string(func) + std::string(name),
486 std::string());
487 }
488}
489
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300490static resCode setPCIeFunctionProperties(const int& clientAddr, const int& bus,
491 const int& dev, const int& func)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700492{
Andrei Kartashev6f552032021-05-11 21:25:29 +0300493 uint32_t data = 0;
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300494 std::string res;
Andrei Kartashev6f552032021-05-11 21:25:29 +0300495 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. Billsd1e40602019-05-09 11:43:51 -0700516 // 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 Kartashev6f552032021-05-11 21:25:29 +0300523 error = getDeviceClass(clientAddr, bus, dev, func, res);
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300524 if (error != resCode::resOk)
525 {
526 return error;
527 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700528 setPCIeProperty(clientAddr, bus, dev,
529 "Function" + std::to_string(func) +
530 std::string(peci_pcie::function::deviceClassName),
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300531 res);
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300532 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700533}
534
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300535static resCode setPCIeDeviceProperties(const int& clientAddr, const int& bus,
536 const int& dev)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700537{
538 // Set the device manufacturer
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300539 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. Billsd1e40602019-05-09 11:43:51 -0700546
547 // Set the device type
Patrick Williams42a9ac82023-05-10 07:51:22 -0500548 constexpr const char* deviceTypeName = "DeviceType";
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300549 bool multiFunc;
550 error = isMultiFunction(clientAddr, bus, dev, multiFunc);
551 if (error != resCode::resOk)
552 {
553 return error;
554 }
555 if (multiFunc)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700556 {
557 setPCIeProperty(clientAddr, bus, dev, deviceTypeName, "MultiFunction");
558 }
559 else
560 {
561 setPCIeProperty(clientAddr, bus, dev, deviceTypeName, "SingleFunction");
562 }
Spencer Kubb5efe72021-09-02 16:11:14 +0800563
564 // Set PCIe Generation
Patrick Williams42a9ac82023-05-10 07:51:22 -0500565 constexpr const char* generationInUseName = "GenerationInUse";
Spencer Kubb5efe72021-09-02 16:11:14 +0800566 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 Kartashevd570dfd2020-12-16 17:33:16 +0300581 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700582}
583
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300584static resCode updatePCIeDevice(const int& clientAddr, const int& bus,
585 const int& dev)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700586{
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300587 if (setPCIeDeviceProperties(clientAddr, bus, dev) != resCode::resOk)
588 {
589 return resCode::resErr;
590 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700591
592 // Walk through and populate the functions for this device
593 for (int func = 0; func < peci_pcie::maxPCIFunctions; func++)
594 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300595 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. Billsd1e40602019-05-09 11:43:51 -0700602 {
603 // Set the properties for this function
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300604 if (setPCIeFunctionProperties(clientAddr, bus, dev, func) !=
605 resCode::resOk)
606 {
607 return resCode::resErr;
608 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700609 }
610 else
611 {
612 // Set default properties for unused functions
613 setDefaultPCIeFunctionProperties(clientAddr, bus, dev, func);
614 }
615 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300616 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700617}
618
619static void removePCIeDevice(sdbusplus::asio::object_server& objServer,
620 const int& clientAddr, const int& bus,
621 const int& dev)
622{
Lakshmi Yadlapati4fe704c2023-04-07 08:23:04 -0500623 peci_pcie::PcieInterfaces ifaces =
Jason M. Billsd1e40602019-05-09 11:43:51 -0700624 peci_pcie::pcieDeviceDBusMap[clientAddr][bus][dev];
625
Lakshmi Yadlapati4fe704c2023-04-07 08:23:04 -0500626 objServer.remove_interface(ifaces.deviceIface);
627 objServer.remove_interface(ifaces.assetIface);
Jason M. Billsd1e40602019-05-09 11:43:51 -0700628
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 Kartashevd570dfd2020-12-16 17:33:16 +0300640static 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 Yadlapati4fe704c2023-04-07 08:23:04 -0500647 peci_pcie::PcieInterfaces ifaces;
648 ifaces.deviceIface =
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300649 objServer.add_interface(pathName, peci_pcie::peciPCIeDeviceInterface);
Lakshmi Yadlapati4fe704c2023-04-07 08:23:04 -0500650 ifaces.assetIface =
651 objServer.add_interface(pathName, peci_pcie::peciPCIeAssetInterface);
652 peci_pcie::pcieDeviceDBusMap[clientAddr][bus][dev] = ifaces;
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300653
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 Yadlapati4fe704c2023-04-07 08:23:04 -0500661 ifaces.deviceIface->initialize();
662 ifaces.assetIface->initialize();
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300663 return resCode::resOk;
664}
665
Jason M. Billsd1e40602019-05-09 11:43:51 -0700666static 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 Yadlapati4fe704c2023-04-07 08:23:04 -0500678 if (devIt->second.deviceIface != nullptr ||
679 devIt->second.assetIface != nullptr)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700680 {
681 return true;
682 }
683 }
684 }
685 }
686 return false;
687}
688
Ed Tanous985d9d92023-03-01 10:35:33 -0800689static resCode probePCIeDevice(boost::asio::io_context& io,
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300690 sdbusplus::asio::object_server& objServer,
Andrei Kartashev8e966032021-07-02 20:04:30 +0300691 size_t addr, int cpu, int bus, int dev)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700692{
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300693 bool res;
Andrei Kartashev8e966032021-07-02 20:04:30 +0300694 resCode error = pcieDeviceExists(addr, bus, dev, res);
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300695 if (error != resCode::resOk)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700696 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300697 return error;
Jason M. Bills5d049732019-10-25 15:55:15 -0700698 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300699 if (res)
Jason M. Bills5d049732019-10-25 15:55:15 -0700700 {
Andrei Kartashev8e966032021-07-02 20:04:30 +0300701 if (pcieDeviceInDBusMap(addr, bus, dev))
Jason M. Billsd1e40602019-05-09 11:43:51 -0700702 {
703 // This device is already in D-Bus, so update it
Andrei Kartashev8e966032021-07-02 20:04:30 +0300704 if (updatePCIeDevice(addr, bus, dev) != resCode::resOk)
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300705 {
706 return resCode::resErr;
707 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700708 }
709 else
710 {
711 // This device is not in D-Bus, so add it
Andrei Kartashev8e966032021-07-02 20:04:30 +0300712 if (addPCIeDevice(objServer, addr, cpu, bus, dev) != resCode::resOk)
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300713 {
714 return resCode::resErr;
715 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700716 }
717 }
718 else
719 {
720 // If PECI is not available, then stop scanning
721 if (!isPECIAvailable())
722 {
Paul Fertser541637c2022-12-02 13:42:01 +0000723 return resCode::resErr;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700724 }
725
Andrei Kartashev8e966032021-07-02 20:04:30 +0300726 if (pcieDeviceInDBusMap(addr, bus, dev))
Jason M. Billsd1e40602019-05-09 11:43:51 -0700727 {
728 // This device is in D-Bus, so remove it
Andrei Kartashev8e966032021-07-02 20:04:30 +0300729 removePCIeDevice(objServer, addr, bus, dev);
Jason M. Billsd1e40602019-05-09 11:43:51 -0700730 }
731 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300732 return resCode::resOk;
733}
734
Ed Tanous985d9d92023-03-01 10:35:33 -0800735static void scanNextPCIeDevice(boost::asio::io_context& io,
Andrei Kartashev8e966032021-07-02 20:04:30 +0300736 sdbusplus::asio::object_server& objServer,
737 std::vector<CPUInfo>& cpuInfo, int cpu, int bus,
738 int dev);
Ed Tanous985d9d92023-03-01 10:35:33 -0800739static void scanPCIeDevice(boost::asio::io_context& io,
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300740 sdbusplus::asio::object_server& objServer,
741 std::vector<CPUInfo>& cpuInfo, int cpu, int bus,
742 int dev)
743{
Andrei Kartashev8e966032021-07-02 20:04:30 +0300744 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 Kartashevd570dfd2020-12-16 17:33:16 +0300751 // Check if this is a CPU bus that we should skip
Andrei Kartashev8e966032021-07-02 20:04:30 +0300752 if (info.skipCpuBuses && info.cpuBusNums.count(bus))
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300753 {
754 std::cout << "Skipping CPU " << cpu << " Bus Number " << bus << "\n";
755 // Skip all the devices on this bus
756 dev = peci_pcie::maxPCIDevices;
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300757 }
Andrei Kartashev8e966032021-07-02 20:04:30 +0300758 else
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300759 {
Andrei Kartashev8e966032021-07-02 20:04:30 +0300760 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 Fertser541637c2022-12-02 13:42:01 +0000765 peci_pcie::abortScan = true;
Andrei Kartashev8e966032021-07-02 20:04:30 +0300766 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300767 }
768
Jason M. Bills5d049732019-10-25 15:55:15 -0700769 scanNextPCIeDevice(io, objServer, cpuInfo, cpu, bus, dev);
Andrei Kartashev8e966032021-07-02 20:04:30 +0300770 return;
Jason M. Bills5d049732019-10-25 15:55:15 -0700771}
Jason M. Billsd1e40602019-05-09 11:43:51 -0700772
Ed Tanous985d9d92023-03-01 10:35:33 -0800773static void scanNextPCIeDevice(boost::asio::io_context& io,
Jason M. Bills5d049732019-10-25 15:55:15 -0700774 sdbusplus::asio::object_server& objServer,
775 std::vector<CPUInfo>& cpuInfo, int cpu, int bus,
776 int dev)
777{
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700778 if (peci_pcie::abortScan)
779 {
Paul Fertser07343142022-12-01 21:04:57 +0000780 peci_pcie::scanInProgress = false;
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700781 std::cerr << "PCIe scan aborted\n";
782 return;
783 }
784
Jason M. Billsd1e40602019-05-09 11:43:51 -0700785 // 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. Bills5d049732019-10-25 15:55:15 -0700794 if (++cpu >= cpuInfo.size())
Jason M. Billsd1e40602019-05-09 11:43:51 -0700795 {
796 // All CPUs scanned, so we're done
Paul Fertser07343142022-12-01 21:04:57 +0000797 peci_pcie::scanInProgress = false;
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700798 std::cerr << "PCIe scan completed\n";
Jason M. Billsd1e40602019-05-09 11:43:51 -0700799 return;
800 }
801 }
802 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300803 boost::asio::post(io, [&io, &objServer, &cpuInfo, cpu, bus, dev]() mutable {
Jason M. Bills5d049732019-10-25 15:55:15 -0700804 scanPCIeDevice(io, objServer, cpuInfo, cpu, bus, dev);
Jason M. Billsd1e40602019-05-09 11:43:51 -0700805 });
806}
807
Ed Tanous985d9d92023-03-01 10:35:33 -0800808static void startPCIeScan(boost::asio::io_context& io,
Paul Fertser07343142022-12-01 21:04:57 +0000809 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 Fertser3b2afcb2022-12-16 12:17:15 +0000815 std::cerr << "Getting map\n";
Paul Fertser07343142022-12-01 21:04:57 +0000816 if (getCPUBusMap(cpuInfo) != resCode::resOk)
817 {
Paul Fertser541637c2022-12-02 13:42:01 +0000818 peci_pcie::abortScan = true;
Paul Fertser07343142022-12-01 21:04:57 +0000819 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 Fertser541637c2022-12-02 13:42:01 +0000824 peci_pcie::abortScan = false;
Paul Fertser07343142022-12-01 21:04:57 +0000825 scanPCIeDevice(io, objServer, cpuInfo, 0, 0, 0);
826 }
827}
828
Jason M. Billsd1e40602019-05-09 11:43:51 -0700829static void peciAvailableCheck(boost::asio::steady_timer& peciWaitTimer,
Ed Tanous985d9d92023-03-01 10:35:33 -0800830 boost::asio::io_context& io,
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300831 sdbusplus::asio::object_server& objServer,
832 std::vector<CPUInfo>& cpuInfo)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700833{
Jason M. Bills5d049732019-10-25 15:55:15 -0700834 static bool lastPECIState = false;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700835 bool peciAvailable = isPECIAvailable();
Paul Fertser3b2afcb2022-12-16 12:17:15 +0000836 if constexpr (peci_pcie::debug)
837 {
838 std::cerr << "peciAvailableCheck " << peciAvailable << " "
839 << lastPECIState << " " << peci_pcie::abortScan << "\n";
840 }
Paul Fertser541637c2022-12-02 13:42:01 +0000841 if (peciAvailable && (!lastPECIState || peci_pcie::abortScan))
Jason M. Billsd1e40602019-05-09 11:43:51 -0700842 {
843 lastPECIState = true;
Paul Fertser541637c2022-12-02 13:42:01 +0000844 auto pcieTimeout = std::make_shared<boost::asio::steady_timer>(io);
Jason M. Billsd1e40602019-05-09 11:43:51 -0700845 constexpr const int pcieWaitTime = 60;
Paul Fertser3b2afcb2022-12-16 12:17:15 +0000846 if constexpr (peci_pcie::debug)
847 {
848 std::cerr << "Scanning in 60 seconds\n";
849 }
Paul Fertser541637c2022-12-02 13:42:01 +0000850 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. Billsd1e40602019-05-09 11:43:51 -0700858 {
Paul Fertser541637c2022-12-02 13:42:01 +0000859 std::cerr << "PECI PCIe async_wait failed " << ec;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700860 }
Paul Fertser541637c2022-12-02 13:42:01 +0000861 lastPECIState = false;
862 return;
863 }
864 startPCIeScan(io, objServer, cpuInfo);
865 });
Jason M. Billsd1e40602019-05-09 11:43:51 -0700866 }
867 else if (!peciAvailable && lastPECIState)
868 {
869 lastPECIState = false;
870 }
871
872 peciWaitTimer.expires_after(
873 std::chrono::seconds(peci_pcie::peciCheckInterval));
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300874 peciWaitTimer.async_wait([&peciWaitTimer, &io, &objServer,
875 &cpuInfo](const boost::system::error_code& ec) {
Jason M. Billsd1e40602019-05-09 11:43:51 -0700876 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 Kartashevd570dfd2020-12-16 17:33:16 +0300886 peciAvailableCheck(peciWaitTimer, io, objServer, cpuInfo);
Jason M. Billsd1e40602019-05-09 11:43:51 -0700887 });
888}
889
Ed Tanous985d9d92023-03-01 10:35:33 -0800890static void waitForOSStandbyDelay(boost::asio::io_context& io,
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700891 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 Williams42a9ac82023-05-10 07:51:22 -0500900 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. Billsee6d80b2021-06-11 07:37:30 -0700912}
913
Ed Tanous985d9d92023-03-01 10:35:33 -0800914static void monitorOSStandby(boost::asio::io_context& io,
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700915 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 Williamsb2517082022-07-22 19:26:57 -0500922 static sdbusplus::bus::match_t osStateMatch(
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700923 *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 Williamsb2517082022-07-22 19:26:57 -0500928 &cpuInfo](sdbusplus::message_t& msg) {
Patrick Williams42a9ac82023-05-10 07:51:22 -0500929 // 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. Billsee6d80b2021-06-11 07:37:30 -0700934
Patrick Williams42a9ac82023-05-10 07:51:22 -0500935 for (const auto& [name, value] : propertiesChanged)
936 {
937 if (name == "OperatingSystemState")
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700938 {
Patrick Williams42a9ac82023-05-10 07:51:22 -0500939 const std::string* state = std::get_if<std::string>(&value);
940 if (state == nullptr)
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700941 {
Patrick Williams42a9ac82023-05-10 07:51:22 -0500942 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. Billsee6d80b2021-06-11 07:37:30 -0700961 }
962 }
Patrick Williams42a9ac82023-05-10 07:51:22 -0500963 }
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700964 });
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 Williams42a9ac82023-05-10 07:51:22 -0500971 if (ec)
972 {
973 std::cerr << "error with OS state async_method_call\n";
974 return;
975 }
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700976
Patrick Williams42a9ac82023-05-10 07:51:22 -0500977 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. Billsee6d80b2021-06-11 07:37:30 -0700983
Patrick Williams42a9ac82023-05-10 07:51:22 -0500984 // 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 Kartashev328685e2021-12-27 17:24:18 +0300988
Patrick Williams42a9ac82023-05-10 07:51:22 -0500989 if ((*state == "Standby") ||
990 (*state == "xyz.openbmc_project.State.OperatingSystem.Status."
991 "OSStatus.Standby"))
992 {
993 waitForOSStandbyDelay(io, objServer, osStandbyTimer, cpuInfo);
994 }
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700995 },
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. Billsd1e40602019-05-09 11:43:51 -07001002int main(int argc, char* argv[])
1003{
1004 // setup connection to dbus
Ed Tanous985d9d92023-03-01 10:35:33 -08001005 boost::asio::io_context io;
Jason M. Billsd1e40602019-05-09 11:43:51 -07001006 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 Kartashevd570dfd2020-12-16 17:33:16 +03001014 // CPU map
1015 std::vector<CPUInfo> cpuInfo;
1016
Jason M. Billsee6d80b2021-06-11 07:37:30 -07001017#ifdef WAIT_FOR_OS_STANDBY
1018 boost::asio::steady_timer osStandbyTimer(io);
1019 monitorOSStandby(io, conn, server, osStandbyTimer, cpuInfo);
1020#else
Jason M. Billsd1e40602019-05-09 11:43:51 -07001021 // Start the PECI check loop
1022 boost::asio::steady_timer peciWaitTimer(
1023 io, std::chrono::seconds(peci_pcie::peciCheckInterval));
Andrei Kartashevd570dfd2020-12-16 17:33:16 +03001024 peciWaitTimer.async_wait([&peciWaitTimer, &io, &server,
1025 &cpuInfo](const boost::system::error_code& ec) {
Jason M. Billsd1e40602019-05-09 11:43:51 -07001026 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 Kartashevd570dfd2020-12-16 17:33:16 +03001036 peciAvailableCheck(peciWaitTimer, io, server, cpuInfo);
Jason M. Billsd1e40602019-05-09 11:43:51 -07001037 });
Jason M. Billsee6d80b2021-06-11 07:37:30 -07001038#endif
Jason M. Billsd1e40602019-05-09 11:43:51 -07001039
1040 io.run();
1041
1042 return 0;
1043}