blob: 0e9ca91abb1b0ed0bd0cb41f73d5ad0049993c71 [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;
Jason M. Billsc076bd72023-07-31 12:25:05 -0700152 break;
153 }
154 default:
155 {
156 std::cerr << "CPU buses not found for CPU Model: 0x" << std::hex
157 << static_cast<int>(model) << std::dec << "\n";
158 break;
Jason M. Bills5d049732019-10-25 15:55:15 -0700159 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700160 }
161 }
Andrei Kartashev8e966032021-07-02 20:04:30 +0300162 return cpuInfo.empty() ? resCode::resErr : resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700163}
164
165static bool isPECIAvailable(void)
166{
Jason M. Bills5d049732019-10-25 15:55:15 -0700167 for (size_t i = MIN_CLIENT_ADDR; i <= MAX_CLIENT_ADDR; i++)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700168 {
Jason M. Bills5d049732019-10-25 15:55:15 -0700169 if (peci_Ping(i) == PECI_CC_SUCCESS)
170 {
171 return true;
172 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700173 }
Jason M. Bills5d049732019-10-25 15:55:15 -0700174 return false;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700175}
176
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300177static resCode getDataFromPCIeConfig(const int& clientAddr, const int& bus,
178 const int& dev, const int& func,
179 const int& offset, const int& size,
180 uint32_t& pciData)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700181{
182 // PECI RdPCIConfig() currently only supports 4 byte reads, so adjust
183 // the offset and size to get the right data
184 static constexpr const int pciReadSize = 4;
185 int mod = offset % pciReadSize;
186 int pciOffset = offset - mod;
187 if (mod + size > pciReadSize)
188 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300189 return resCode::resErr;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700190 }
191
192 std::array<uint8_t, pciReadSize> data;
193 uint8_t cc;
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300194 int ret = PECI_CC_TIMEOUT;
Paul Fertserb08723d2022-12-16 11:49:08 +0000195 for (int index = 0; (index < 15) && (ret == PECI_CC_TIMEOUT); index++)
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300196 {
Jason M. Bills3b1665a2021-06-11 13:35:04 -0700197#ifdef USE_RDENDPOINTCFG
198 ret = peci_RdEndPointConfigPci(clientAddr, // CPU Address
199 0, // PCI Seg (use 0 for now)
200 bus, // PCI Bus
201 dev, // PCI Device
202 func, // PCI Function
203 pciOffset, // PCI Offset
204 pciReadSize, // PCI Read Size
205 data.data(), // PCI Read Data
206 &cc); // PECI Completion Code
207#else
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300208 ret = peci_RdPCIConfig(clientAddr, // CPU Address
Jason M. Billsd1e40602019-05-09 11:43:51 -0700209 bus, // PCI Bus
210 dev, // PCI Device
211 func, // PCI Function
212 pciOffset, // PCI Offset
213 data.data(), // PCI Read Data
214 &cc); // PECI Completion Code
Jason M. Bills3b1665a2021-06-11 13:35:04 -0700215#endif
Paul Fertser3b2afcb2022-12-16 12:17:15 +0000216 if constexpr (peci_pcie::debug)
217 {
218 std::cerr << "Request: bus " << bus << " dev " << dev << " func "
219 << func << " pciOffset " << pciOffset << " ret: " << ret
220 << " cc: " << static_cast<int>(cc) << "\n";
221 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300222 }
Paul Fertser541637c2022-12-02 13:42:01 +0000223 if (ret != PECI_CC_SUCCESS)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700224 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300225 return resCode::resErr;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700226 }
Paul Fertser541637c2022-12-02 13:42:01 +0000227 else if (cc != PECI_DEV_CC_SUCCESS)
228 {
229 return resCode::resSkip;
230 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700231
232 // Now build the requested data into a single number
233 pciData = 0;
234 for (int i = mod; i < mod + size; i++)
235 {
Jason M. Billse55832b2021-06-14 16:00:49 -0700236 pciData |= static_cast<uint32_t>(data[i]) << 8 * (i - mod);
Jason M. Billsd1e40602019-05-09 11:43:51 -0700237 }
238
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300239 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700240}
241
Andrei Kartashev6f552032021-05-11 21:25:29 +0300242static resCode getStringFromData(const int& size, const uint32_t& data,
243 std::string& res)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700244{
Jason M. Billsd1e40602019-05-09 11:43:51 -0700245 // And convert it to a string
246 std::stringstream dataStream;
247 dataStream << "0x" << std::hex << std::setfill('0') << std::setw(size * 2)
248 << data;
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300249 res = dataStream.str();
250 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700251}
252
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300253static resCode getVendorName(const int& clientAddr, const int& bus,
254 const int& dev, std::string& res)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700255{
256 static constexpr const int vendorIDOffset = 0x00;
257 static constexpr const int vendorIDSize = 2;
258
259 // Get the header type register from function 0
260 uint32_t vendorID = 0;
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300261 if (getDataFromPCIeConfig(clientAddr, bus, dev, 0, vendorIDOffset,
262 vendorIDSize, vendorID) != resCode::resOk)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700263 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300264 return resCode::resErr;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700265 }
266 // Get the vendor name or use Other if it doesn't exist
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300267 res = pciVendors.try_emplace(vendorID, otherVendor).first->second;
268 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700269}
270
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300271static resCode getDeviceClass(const int& clientAddr, const int& bus,
272 const int& dev, const int& func, std::string& res)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700273{
274 static constexpr const int baseClassOffset = 0x0b;
275 static constexpr const int baseClassSize = 1;
276
277 // Get the Device Base Class
278 uint32_t baseClass = 0;
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300279 if (getDataFromPCIeConfig(clientAddr, bus, dev, func, baseClassOffset,
280 baseClassSize, baseClass) != resCode::resOk)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700281 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300282 return resCode::resErr;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700283 }
284 // Get the base class name or use Other if it doesn't exist
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300285 res = pciDeviceClasses.try_emplace(baseClass, otherClass).first->second;
286 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700287}
288
Spencer Kubb5efe72021-09-02 16:11:14 +0800289static resCode getCapReading(const int& clientAddr, const int& bus,
290 const int& dev, uint32_t& capReading,
291 const int compareCapID, const int offsetAddress,
292 const int offsetLength)
293{
294 resCode error;
295 uint32_t capAddress = 0;
296 uint32_t capabilityID;
297 uint32_t nextCapPointer = peci_pcie::pointToCapStruct;
298
299 do
300 {
301 // Get capability address
302 error = getDataFromPCIeConfig(clientAddr, bus, dev, 0, nextCapPointer,
303 1, capAddress);
304 if (error != resCode::resOk)
305 {
306 return error;
307 }
308 // Capability struct address is a pointer which point to next capability
309 // struct, so if capability struct address is 0 means it doesn't have
310 // next capability struct.
311 if (capAddress == 0)
312 {
313 return resCode::resSkip;
314 }
315 // Get capability ID
316 error = getDataFromPCIeConfig(clientAddr, bus, dev, 0, capAddress, 1,
317 capabilityID);
318 if (error != resCode::resOk)
319 {
320 return error;
321 }
322 nextCapPointer = capAddress + peci_pcie::capPointerOffset;
323
Jason M. Billsc076bd72023-07-31 12:25:05 -0700324 } while (capabilityID != static_cast<uint32_t>(compareCapID));
Spencer Kubb5efe72021-09-02 16:11:14 +0800325 // Get capability reading.
326 error = getDataFromPCIeConfig(clientAddr, bus, dev, 0,
327 capAddress + offsetAddress, offsetLength,
328 capReading);
329 if (error != resCode::resOk)
330 {
331 return error;
332 }
333 return resCode::resOk;
334}
335
336static resCode getGenerationInUse(const int& clientAddr, const int& bus,
337 const int& dev, std::string& generationInUse)
338{
Willy Tu975faf72022-09-23 18:39:16 +0000339 static std::unordered_set<uint32_t> linkSpeedSkipMap;
Spencer Kubb5efe72021-09-02 16:11:14 +0800340 resCode error;
341 std::string res;
Spencer Kubb5efe72021-09-02 16:11:14 +0800342
343 // Capability ID 0x10(16) is PCI Express
344 constexpr int pcieCapID = 16;
345 constexpr int capLength = 2;
346 uint32_t linkStatus;
347
348 error = getCapReading(clientAddr, bus, dev, linkStatus, pcieCapID,
349 peci_pcie::linkStatusOffset, capLength);
350 if (error != resCode::resOk)
351 {
352 return error;
353 }
354
355 uint32_t linkSpeed = linkStatus & peci_pcie::maskOfCLS;
356
357 switch (linkSpeed)
358 {
359 case peci_pcie::pcieGen1:
360 generationInUse = "xyz.openbmc_project.Inventory.Item."
361 "PCIeSlot.Generations.Gen1";
362 error = resCode::resOk;
363 break;
364 case peci_pcie::pcieGen2:
365 generationInUse = "xyz.openbmc_project.Inventory.Item."
366 "PCIeSlot.Generations.Gen2";
367 error = resCode::resOk;
368 break;
369 case peci_pcie::pcieGen3:
370 generationInUse = "xyz.openbmc_project.Inventory.Item."
371 "PCIeSlot.Generations.Gen3";
372 error = resCode::resOk;
373 break;
374 case peci_pcie::pcieGen4:
375 generationInUse = "xyz.openbmc_project.Inventory.Item."
376 "PCIeSlot.Generations.Gen4";
377 error = resCode::resOk;
378 break;
379 case peci_pcie::pcieGen5:
380 generationInUse = "xyz.openbmc_project.Inventory.Item."
381 "PCIeSlot.Generations.Gen5";
382 error = resCode::resOk;
383 break;
384 default:
Willy Tu975faf72022-09-23 18:39:16 +0000385 if (!linkSpeedSkipMap.contains(linkSpeed))
386 {
387 std::cerr << "Link speed : " << linkSpeed
388 << " can not mapping to PCIe type list.\n";
389 linkSpeedSkipMap.emplace(linkSpeed);
390 }
Spencer Kubb5efe72021-09-02 16:11:14 +0800391 error = resCode::resSkip;
392 }
393 return error;
394}
395
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300396static resCode isMultiFunction(const int& clientAddr, const int& bus,
397 const int& dev, bool& res)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700398{
399 static constexpr const int headerTypeOffset = 0x0e;
400 static constexpr const int headerTypeSize = 1;
401 static constexpr const int multiFuncBit = 1 << 7;
402
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300403 res = false;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700404 // Get the header type register from function 0
405 uint32_t headerType = 0;
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300406 if (getDataFromPCIeConfig(clientAddr, bus, dev, 0, headerTypeOffset,
407 headerTypeSize, headerType) != resCode::resOk)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700408 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300409 return resCode::resErr;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700410 }
411 // Check if it's a multifunction device
412 if (headerType & multiFuncBit)
413 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300414 res = true;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700415 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300416 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700417}
418
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300419static resCode pcieFunctionExists(const int& clientAddr, const int& bus,
420 const int& dev, const int& func, bool& res)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700421{
422 constexpr const int pciIDOffset = 0;
423 constexpr const int pciIDSize = 4;
424 uint32_t pciID = 0;
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300425 res = false;
Paul Fertser541637c2022-12-02 13:42:01 +0000426
427 resCode error = getDataFromPCIeConfig(clientAddr, bus, dev, func,
428 pciIDOffset, pciIDSize, pciID);
429 if (error == resCode::resSkip)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700430 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300431 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700432 }
Paul Fertser541637c2022-12-02 13:42:01 +0000433 else if (error == resCode::resErr)
434 {
435 return resCode::resErr;
436 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700437
438 // if VID and DID are all 0s or 1s, then the device doesn't exist
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300439 if (pciID != 0x00000000 && pciID != 0xFFFFFFFF)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700440 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300441 res = true;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700442 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300443 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700444}
445
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300446static resCode pcieDeviceExists(const int& clientAddr, const int& bus,
447 const int& dev, bool& res)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700448{
449 // Check if this device exists by checking function 0
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300450 return pcieFunctionExists(clientAddr, bus, dev, 0, res);
Jason M. Billsd1e40602019-05-09 11:43:51 -0700451}
452
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300453static resCode setPCIeProperty(const int& clientAddr, const int& bus,
454 const int& dev, const std::string& propertyName,
455 const std::string& propertyValue)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700456{
Lakshmi Yadlapati4fe704c2023-04-07 08:23:04 -0500457 peci_pcie::PcieInterfaces pcieIfaces =
Jason M. Billsd1e40602019-05-09 11:43:51 -0700458 peci_pcie::pcieDeviceDBusMap[clientAddr][bus][dev];
459
Lakshmi Yadlapati4fe704c2023-04-07 08:23:04 -0500460 std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
461 if (propertyName == "Manufacturer")
462 {
463 iface = pcieIfaces.assetIface;
464 }
465 else
466 {
467 iface = pcieIfaces.deviceIface;
468 }
469
Jason M. Billsd1e40602019-05-09 11:43:51 -0700470 if (iface->is_initialized())
471 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300472 if (!iface->set_property(propertyName, propertyValue))
473 return resCode::resErr;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700474 }
475 else
476 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300477 if (!iface->register_property(propertyName, propertyValue))
478 return resCode::resErr;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700479 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300480 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700481}
482
483static void setDefaultPCIeFunctionProperties(const int& clientAddr,
484 const int& bus, const int& dev,
485 const int& func)
486{
487 // Set the function-specific properties
Andrei Kartashev6f552032021-05-11 21:25:29 +0300488 for (const auto& [name, offset, size] : peci_pcie::pciConfigInfo)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700489 {
490 setPCIeProperty(clientAddr, bus, dev,
491 "Function" + std::to_string(func) + std::string(name),
492 std::string());
493 }
494}
495
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300496static resCode setPCIeFunctionProperties(const int& clientAddr, const int& bus,
497 const int& dev, const int& func)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700498{
Andrei Kartashev6f552032021-05-11 21:25:29 +0300499 uint32_t data = 0;
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300500 std::string res;
Andrei Kartashev6f552032021-05-11 21:25:29 +0300501 resCode error;
502
503 for (const auto& [name, offset, size] : peci_pcie::pciConfigInfo)
504 {
505 if (offset < 0)
506 {
507 continue;
508 }
509
510 error = getDataFromPCIeConfig(clientAddr, bus, dev, func, offset, size,
511 data);
512 if (error != resCode::resOk)
513 {
514 return error;
515 }
516 getStringFromData(size, data, res);
517 setPCIeProperty(clientAddr, bus, dev,
518 "Function" + std::to_string(func) + std::string(name),
519 res);
520 }
521
Jason M. Billsd1e40602019-05-09 11:43:51 -0700522 // Set the function type always to physical for now
523 setPCIeProperty(clientAddr, bus, dev,
524 "Function" + std::to_string(func) +
525 std::string(peci_pcie::function::functionTypeName),
526 "Physical");
527
528 // Set the function Device Class
Andrei Kartashev6f552032021-05-11 21:25:29 +0300529 error = getDeviceClass(clientAddr, bus, dev, func, res);
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300530 if (error != resCode::resOk)
531 {
532 return error;
533 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700534 setPCIeProperty(clientAddr, bus, dev,
535 "Function" + std::to_string(func) +
536 std::string(peci_pcie::function::deviceClassName),
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300537 res);
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300538 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700539}
540
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300541static resCode setPCIeDeviceProperties(const int& clientAddr, const int& bus,
542 const int& dev)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700543{
544 // Set the device manufacturer
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300545 std::string manuf;
546 resCode error = getVendorName(clientAddr, bus, dev, manuf);
547 if (error != resCode::resOk)
548 {
549 return error;
550 }
551 setPCIeProperty(clientAddr, bus, dev, "Manufacturer", manuf);
Jason M. Billsd1e40602019-05-09 11:43:51 -0700552
553 // Set the device type
Patrick Williams42a9ac82023-05-10 07:51:22 -0500554 constexpr const char* deviceTypeName = "DeviceType";
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300555 bool multiFunc;
556 error = isMultiFunction(clientAddr, bus, dev, multiFunc);
557 if (error != resCode::resOk)
558 {
559 return error;
560 }
561 if (multiFunc)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700562 {
563 setPCIeProperty(clientAddr, bus, dev, deviceTypeName, "MultiFunction");
564 }
565 else
566 {
567 setPCIeProperty(clientAddr, bus, dev, deviceTypeName, "SingleFunction");
568 }
Spencer Kubb5efe72021-09-02 16:11:14 +0800569
570 // Set PCIe Generation
Patrick Williams42a9ac82023-05-10 07:51:22 -0500571 constexpr const char* generationInUseName = "GenerationInUse";
Spencer Kubb5efe72021-09-02 16:11:14 +0800572 std::string generationInUse;
573 error = getGenerationInUse(clientAddr, bus, dev, generationInUse);
574 if (error == resCode::resErr)
575 {
576 return error;
577 }
578 // "resSkip" status means it can't get the capability reading, such like
579 // this device is not PCI Express.
580 if (error == resCode::resSkip)
581 {
582 setPCIeProperty(clientAddr, bus, dev, generationInUseName, "");
583 return resCode::resOk;
584 }
585 setPCIeProperty(clientAddr, bus, dev, generationInUseName, generationInUse);
586
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300587 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700588}
589
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300590static resCode updatePCIeDevice(const int& clientAddr, const int& bus,
591 const int& dev)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700592{
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300593 if (setPCIeDeviceProperties(clientAddr, bus, dev) != resCode::resOk)
594 {
595 return resCode::resErr;
596 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700597
Nidhin MS8b18f522023-06-28 10:25:06 +0530598 bool multiFunc = false;
599 resCode error = isMultiFunction(clientAddr, bus, dev, multiFunc);
600 if (error != resCode::resOk)
601 {
602 return error;
603 }
604 // Functions greater than 0 should only be accessed on multi-function
605 // devices
606 int maxPCIFunctions = multiFunc ? peci_pcie::maxPCIFunctions : 1;
607
Jason M. Billsd1e40602019-05-09 11:43:51 -0700608 // Walk through and populate the functions for this device
Nidhin MS8b18f522023-06-28 10:25:06 +0530609 for (int func = 0; func < maxPCIFunctions; func++)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700610 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300611 bool res;
612 resCode error = pcieFunctionExists(clientAddr, bus, dev, func, res);
613 if (error != resCode::resOk)
614 {
615 return error;
616 }
617 if (res)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700618 {
619 // Set the properties for this function
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300620 if (setPCIeFunctionProperties(clientAddr, bus, dev, func) !=
621 resCode::resOk)
622 {
623 return resCode::resErr;
624 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700625 }
626 else
627 {
628 // Set default properties for unused functions
629 setDefaultPCIeFunctionProperties(clientAddr, bus, dev, func);
630 }
631 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300632 return resCode::resOk;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700633}
634
635static void removePCIeDevice(sdbusplus::asio::object_server& objServer,
636 const int& clientAddr, const int& bus,
637 const int& dev)
638{
Lakshmi Yadlapati4fe704c2023-04-07 08:23:04 -0500639 peci_pcie::PcieInterfaces ifaces =
Jason M. Billsd1e40602019-05-09 11:43:51 -0700640 peci_pcie::pcieDeviceDBusMap[clientAddr][bus][dev];
641
Lakshmi Yadlapati4fe704c2023-04-07 08:23:04 -0500642 objServer.remove_interface(ifaces.deviceIface);
643 objServer.remove_interface(ifaces.assetIface);
Jason M. Billsd1e40602019-05-09 11:43:51 -0700644
645 peci_pcie::pcieDeviceDBusMap[clientAddr][bus].erase(dev);
646 if (peci_pcie::pcieDeviceDBusMap[clientAddr][bus].empty())
647 {
648 peci_pcie::pcieDeviceDBusMap[clientAddr].erase(bus);
649 }
650 if (peci_pcie::pcieDeviceDBusMap[clientAddr].empty())
651 {
652 peci_pcie::pcieDeviceDBusMap.erase(clientAddr);
653 }
654}
655
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300656static resCode addPCIeDevice(sdbusplus::asio::object_server& objServer,
657 const int& clientAddr, const int& cpu,
658 const int& bus, const int& dev)
659{
660 std::string pathName = std::string(peci_pcie::peciPCIePath) + "/S" +
661 std::to_string(cpu) + "B" + std::to_string(bus) +
662 "D" + std::to_string(dev);
Lakshmi Yadlapati4fe704c2023-04-07 08:23:04 -0500663 peci_pcie::PcieInterfaces ifaces;
664 ifaces.deviceIface =
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300665 objServer.add_interface(pathName, peci_pcie::peciPCIeDeviceInterface);
Lakshmi Yadlapati4fe704c2023-04-07 08:23:04 -0500666 ifaces.assetIface =
667 objServer.add_interface(pathName, peci_pcie::peciPCIeAssetInterface);
668 peci_pcie::pcieDeviceDBusMap[clientAddr][bus][dev] = ifaces;
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300669
670 // Update the properties for the new device
671 if (updatePCIeDevice(clientAddr, bus, dev) != resCode::resOk)
672 {
673 removePCIeDevice(objServer, clientAddr, bus, dev);
674 return resCode::resErr;
675 }
676
Lakshmi Yadlapati4fe704c2023-04-07 08:23:04 -0500677 ifaces.deviceIface->initialize();
678 ifaces.assetIface->initialize();
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300679 return resCode::resOk;
680}
681
Jason M. Billsd1e40602019-05-09 11:43:51 -0700682static bool pcieDeviceInDBusMap(const int& clientAddr, const int& bus,
683 const int& dev)
684{
685 if (auto clientAddrIt = peci_pcie::pcieDeviceDBusMap.find(clientAddr);
686 clientAddrIt != peci_pcie::pcieDeviceDBusMap.end())
687 {
688 if (auto busIt = clientAddrIt->second.find(bus);
689 busIt != clientAddrIt->second.end())
690 {
691 if (auto devIt = busIt->second.find(dev);
692 devIt != busIt->second.end())
693 {
Lakshmi Yadlapati4fe704c2023-04-07 08:23:04 -0500694 if (devIt->second.deviceIface != nullptr ||
695 devIt->second.assetIface != nullptr)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700696 {
697 return true;
698 }
699 }
700 }
701 }
702 return false;
703}
704
Jason M. Billsc076bd72023-07-31 12:25:05 -0700705static resCode probePCIeDevice(sdbusplus::asio::object_server& objServer,
Andrei Kartashev8e966032021-07-02 20:04:30 +0300706 size_t addr, int cpu, int bus, int dev)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700707{
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300708 bool res;
Andrei Kartashev8e966032021-07-02 20:04:30 +0300709 resCode error = pcieDeviceExists(addr, bus, dev, res);
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300710 if (error != resCode::resOk)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700711 {
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300712 return error;
Jason M. Bills5d049732019-10-25 15:55:15 -0700713 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300714 if (res)
Jason M. Bills5d049732019-10-25 15:55:15 -0700715 {
Andrei Kartashev8e966032021-07-02 20:04:30 +0300716 if (pcieDeviceInDBusMap(addr, bus, dev))
Jason M. Billsd1e40602019-05-09 11:43:51 -0700717 {
718 // This device is already in D-Bus, so update it
Andrei Kartashev8e966032021-07-02 20:04:30 +0300719 if (updatePCIeDevice(addr, bus, dev) != resCode::resOk)
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300720 {
721 return resCode::resErr;
722 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700723 }
724 else
725 {
726 // This device is not in D-Bus, so add it
Andrei Kartashev8e966032021-07-02 20:04:30 +0300727 if (addPCIeDevice(objServer, addr, cpu, bus, dev) != resCode::resOk)
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300728 {
729 return resCode::resErr;
730 }
Jason M. Billsd1e40602019-05-09 11:43:51 -0700731 }
732 }
733 else
734 {
735 // If PECI is not available, then stop scanning
736 if (!isPECIAvailable())
737 {
Paul Fertser541637c2022-12-02 13:42:01 +0000738 return resCode::resErr;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700739 }
740
Andrei Kartashev8e966032021-07-02 20:04:30 +0300741 if (pcieDeviceInDBusMap(addr, bus, dev))
Jason M. Billsd1e40602019-05-09 11:43:51 -0700742 {
743 // This device is in D-Bus, so remove it
Andrei Kartashev8e966032021-07-02 20:04:30 +0300744 removePCIeDevice(objServer, addr, bus, dev);
Jason M. Billsd1e40602019-05-09 11:43:51 -0700745 }
746 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300747 return resCode::resOk;
748}
749
Ed Tanous985d9d92023-03-01 10:35:33 -0800750static void scanNextPCIeDevice(boost::asio::io_context& io,
Andrei Kartashev8e966032021-07-02 20:04:30 +0300751 sdbusplus::asio::object_server& objServer,
752 std::vector<CPUInfo>& cpuInfo, int cpu, int bus,
753 int dev);
Ed Tanous985d9d92023-03-01 10:35:33 -0800754static void scanPCIeDevice(boost::asio::io_context& io,
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300755 sdbusplus::asio::object_server& objServer,
756 std::vector<CPUInfo>& cpuInfo, int cpu, int bus,
757 int dev)
758{
Jason M. Billsc076bd72023-07-31 12:25:05 -0700759 if (cpu >= static_cast<int>(cpuInfo.size()))
Andrei Kartashev8e966032021-07-02 20:04:30 +0300760 {
761 std::cerr << "Request to scan CPU" << cpu
762 << " while CPU array has size " << cpuInfo.size() << "\n";
763 return;
764 }
765 auto& info = cpuInfo[cpu];
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300766 // Check if this is a CPU bus that we should skip
Andrei Kartashev8e966032021-07-02 20:04:30 +0300767 if (info.skipCpuBuses && info.cpuBusNums.count(bus))
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300768 {
769 std::cout << "Skipping CPU " << cpu << " Bus Number " << bus << "\n";
770 // Skip all the devices on this bus
771 dev = peci_pcie::maxPCIDevices;
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300772 }
Andrei Kartashev8e966032021-07-02 20:04:30 +0300773 else
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300774 {
Jason M. Billsc076bd72023-07-31 12:25:05 -0700775 if (probePCIeDevice(objServer, info.addr, cpu, bus, dev) !=
Andrei Kartashev8e966032021-07-02 20:04:30 +0300776 resCode::resOk)
777 {
778 std::cerr << "Failed to probe CPU " << cpu << " Bus " << bus
779 << " Device " << dev << "\n";
Paul Fertser541637c2022-12-02 13:42:01 +0000780 peci_pcie::abortScan = true;
Andrei Kartashev8e966032021-07-02 20:04:30 +0300781 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300782 }
783
Jason M. Bills5d049732019-10-25 15:55:15 -0700784 scanNextPCIeDevice(io, objServer, cpuInfo, cpu, bus, dev);
Andrei Kartashev8e966032021-07-02 20:04:30 +0300785 return;
Jason M. Bills5d049732019-10-25 15:55:15 -0700786}
Jason M. Billsd1e40602019-05-09 11:43:51 -0700787
Ed Tanous985d9d92023-03-01 10:35:33 -0800788static void scanNextPCIeDevice(boost::asio::io_context& io,
Jason M. Bills5d049732019-10-25 15:55:15 -0700789 sdbusplus::asio::object_server& objServer,
790 std::vector<CPUInfo>& cpuInfo, int cpu, int bus,
791 int dev)
792{
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700793 if (peci_pcie::abortScan)
794 {
Paul Fertser07343142022-12-01 21:04:57 +0000795 peci_pcie::scanInProgress = false;
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700796 std::cerr << "PCIe scan aborted\n";
797 return;
798 }
799
Jason M. Billsd1e40602019-05-09 11:43:51 -0700800 // PCIe Device scan completed, so move to the next device
801 if (++dev >= peci_pcie::maxPCIDevices)
802 {
803 // All devices scanned, so move to the next bus
804 dev = 0;
805 if (++bus >= peci_pcie::maxPCIBuses)
806 {
807 // All buses scanned, so move to the next CPU
808 bus = 0;
Jason M. Billsc076bd72023-07-31 12:25:05 -0700809 if (++cpu >= static_cast<int>(cpuInfo.size()))
Jason M. Billsd1e40602019-05-09 11:43:51 -0700810 {
811 // All CPUs scanned, so we're done
Paul Fertser07343142022-12-01 21:04:57 +0000812 peci_pcie::scanInProgress = false;
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700813 std::cerr << "PCIe scan completed\n";
Jason M. Billsd1e40602019-05-09 11:43:51 -0700814 return;
815 }
816 }
817 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300818 boost::asio::post(io, [&io, &objServer, &cpuInfo, cpu, bus, dev]() mutable {
Jason M. Bills5d049732019-10-25 15:55:15 -0700819 scanPCIeDevice(io, objServer, cpuInfo, cpu, bus, dev);
Jason M. Billsd1e40602019-05-09 11:43:51 -0700820 });
821}
822
Ed Tanous985d9d92023-03-01 10:35:33 -0800823static void startPCIeScan(boost::asio::io_context& io,
Paul Fertser07343142022-12-01 21:04:57 +0000824 sdbusplus::asio::object_server& objServer,
825 std::vector<CPUInfo>& cpuInfo)
826{
827 if (!peci_pcie::scanInProgress)
828 {
829 // get the PECI client address list
Paul Fertser3b2afcb2022-12-16 12:17:15 +0000830 std::cerr << "Getting map\n";
Paul Fertser07343142022-12-01 21:04:57 +0000831 if (getCPUBusMap(cpuInfo) != resCode::resOk)
832 {
Paul Fertser541637c2022-12-02 13:42:01 +0000833 peci_pcie::abortScan = true;
Paul Fertser07343142022-12-01 21:04:57 +0000834 return;
835 }
836 std::cerr << "PCIe scan started\n";
837 // scan PCIe starting from CPU 0, Bus 0, Device 0
838 peci_pcie::scanInProgress = true;
Paul Fertser541637c2022-12-02 13:42:01 +0000839 peci_pcie::abortScan = false;
Paul Fertser07343142022-12-01 21:04:57 +0000840 scanPCIeDevice(io, objServer, cpuInfo, 0, 0, 0);
841 }
842}
843
Jason M. Billsd1e40602019-05-09 11:43:51 -0700844static void peciAvailableCheck(boost::asio::steady_timer& peciWaitTimer,
Ed Tanous985d9d92023-03-01 10:35:33 -0800845 boost::asio::io_context& io,
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300846 sdbusplus::asio::object_server& objServer,
847 std::vector<CPUInfo>& cpuInfo)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700848{
Jason M. Bills5d049732019-10-25 15:55:15 -0700849 static bool lastPECIState = false;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700850 bool peciAvailable = isPECIAvailable();
Paul Fertser3b2afcb2022-12-16 12:17:15 +0000851 if constexpr (peci_pcie::debug)
852 {
853 std::cerr << "peciAvailableCheck " << peciAvailable << " "
854 << lastPECIState << " " << peci_pcie::abortScan << "\n";
855 }
Paul Fertser541637c2022-12-02 13:42:01 +0000856 if (peciAvailable && (!lastPECIState || peci_pcie::abortScan))
Jason M. Billsd1e40602019-05-09 11:43:51 -0700857 {
858 lastPECIState = true;
Paul Fertser541637c2022-12-02 13:42:01 +0000859 auto pcieTimeout = std::make_shared<boost::asio::steady_timer>(io);
Jason M. Billsd1e40602019-05-09 11:43:51 -0700860 constexpr const int pcieWaitTime = 60;
Paul Fertser3b2afcb2022-12-16 12:17:15 +0000861 if constexpr (peci_pcie::debug)
862 {
863 std::cerr << "Scanning in 60 seconds\n";
864 }
Paul Fertser541637c2022-12-02 13:42:01 +0000865 pcieTimeout->expires_after(std::chrono::seconds(pcieWaitTime));
866 pcieTimeout->async_wait([&io, &objServer, &cpuInfo, pcieTimeout](
867 const boost::system::error_code& ec) {
868 if (ec)
869 {
870 // operation_aborted is expected if timer is canceled
871 // before completion.
872 if (ec != boost::asio::error::operation_aborted)
Jason M. Billsd1e40602019-05-09 11:43:51 -0700873 {
Paul Fertser541637c2022-12-02 13:42:01 +0000874 std::cerr << "PECI PCIe async_wait failed " << ec;
Jason M. Billsd1e40602019-05-09 11:43:51 -0700875 }
Paul Fertser541637c2022-12-02 13:42:01 +0000876 lastPECIState = false;
877 return;
878 }
879 startPCIeScan(io, objServer, cpuInfo);
880 });
Jason M. Billsd1e40602019-05-09 11:43:51 -0700881 }
882 else if (!peciAvailable && lastPECIState)
883 {
884 lastPECIState = false;
885 }
886
887 peciWaitTimer.expires_after(
888 std::chrono::seconds(peci_pcie::peciCheckInterval));
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300889 peciWaitTimer.async_wait([&peciWaitTimer, &io, &objServer,
890 &cpuInfo](const boost::system::error_code& ec) {
Jason M. Billsd1e40602019-05-09 11:43:51 -0700891 if (ec)
892 {
893 // operation_aborted is expected if timer is canceled
894 // before completion.
895 if (ec != boost::asio::error::operation_aborted)
896 {
897 std::cerr << "PECI Available Check async_wait failed " << ec;
898 }
899 return;
900 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +0300901 peciAvailableCheck(peciWaitTimer, io, objServer, cpuInfo);
Jason M. Billsd1e40602019-05-09 11:43:51 -0700902 });
903}
904
Ed Tanous985d9d92023-03-01 10:35:33 -0800905static void waitForOSStandbyDelay(boost::asio::io_context& io,
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700906 sdbusplus::asio::object_server& objServer,
907 boost::asio::steady_timer& osStandbyTimer,
908 std::vector<CPUInfo>& cpuInfo)
909{
910 osStandbyTimer.expires_after(
911 std::chrono::seconds(peci_pcie::osStandbyDelaySeconds));
912
913 osStandbyTimer.async_wait(
914 [&io, &objServer, &cpuInfo](const boost::system::error_code& ec) {
Patrick Williams42a9ac82023-05-10 07:51:22 -0500915 if (ec == boost::asio::error::operation_aborted)
916 {
917 return; // we're being canceled
918 }
919 else if (ec)
920 {
921 std::cerr << "OS Standby async_wait failed: " << ec.value() << ": "
922 << ec.message() << "\n";
923 return;
924 }
925 startPCIeScan(io, objServer, cpuInfo);
926 });
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700927}
928
Jason M. Billsc076bd72023-07-31 12:25:05 -0700929[[maybe_unused]] static void
930 monitorOSStandby(boost::asio::io_context& io,
931 std::shared_ptr<sdbusplus::asio::connection> conn,
932 sdbusplus::asio::object_server& objServer,
933 boost::asio::steady_timer& osStandbyTimer,
934 std::vector<CPUInfo>& cpuInfo)
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700935{
936 std::cerr << "Start OperatingSystemState Monitor\n";
937
Patrick Williamsb2517082022-07-22 19:26:57 -0500938 static sdbusplus::bus::match_t osStateMatch(
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700939 *conn,
940 "type='signal',interface='org.freedesktop.DBus.Properties',member='"
941 "PropertiesChanged',arg0='xyz.openbmc_project.State.OperatingSystem."
942 "Status'",
943 [&io, &objServer, &osStandbyTimer,
Patrick Williamsb2517082022-07-22 19:26:57 -0500944 &cpuInfo](sdbusplus::message_t& msg) {
Patrick Williams42a9ac82023-05-10 07:51:22 -0500945 // Get the OS State from the message
946 std::string osStateInterface;
947 boost::container::flat_map<std::string, std::variant<std::string>>
948 propertiesChanged;
949 msg.read(osStateInterface, propertiesChanged);
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700950
Patrick Williams42a9ac82023-05-10 07:51:22 -0500951 for (const auto& [name, value] : propertiesChanged)
952 {
953 if (name == "OperatingSystemState")
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700954 {
Patrick Williams42a9ac82023-05-10 07:51:22 -0500955 const std::string* state = std::get_if<std::string>(&value);
956 if (state == nullptr)
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700957 {
Patrick Williams42a9ac82023-05-10 07:51:22 -0500958 std::cerr << "Unable to read OS state value\n";
959 return;
960 }
961 // Note: Short version of OperatingSystemState value is
962 // deprecated and would be removed in the future
963 if ((*state == "Standby") ||
964 (*state == "xyz.openbmc_project.State.OperatingSystem."
965 "Status.OSStatus.Standby"))
966 {
967 peci_pcie::abortScan = false;
968 waitForOSStandbyDelay(io, objServer, osStandbyTimer,
969 cpuInfo);
970 }
971 else if ((*state == "Inactive") ||
972 (*state == "xyz.openbmc_project.State.OperatingSystem."
973 "Status.OSStatus.Inactive"))
974 {
975 peci_pcie::abortScan = true;
976 osStandbyTimer.cancel();
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700977 }
978 }
Patrick Williams42a9ac82023-05-10 07:51:22 -0500979 }
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700980 });
981
982 // Check if the OS state is already available
983 conn->async_method_call(
984 [&io, &objServer, &osStandbyTimer,
985 &cpuInfo](boost::system::error_code ec,
986 const std::variant<std::string>& property) {
Patrick Williams42a9ac82023-05-10 07:51:22 -0500987 if (ec)
988 {
989 std::cerr << "error with OS state async_method_call\n";
990 return;
991 }
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700992
Patrick Williams42a9ac82023-05-10 07:51:22 -0500993 const std::string* state = std::get_if<std::string>(&property);
994 if (state == nullptr)
995 {
996 std::cerr << "Unable to read OS state value\n";
997 return;
998 }
Jason M. Billsee6d80b2021-06-11 07:37:30 -0700999
Patrick Williams42a9ac82023-05-10 07:51:22 -05001000 // If the OS state is in Standby, then BIOS is done and we can
1001 // continue. Otherwise, we just wait for the match
1002 // Note: Short version of OperatingSystemState value is deprecated
1003 // and would be removed in the future
Andrei Kartashev328685e2021-12-27 17:24:18 +03001004
Patrick Williams42a9ac82023-05-10 07:51:22 -05001005 if ((*state == "Standby") ||
1006 (*state == "xyz.openbmc_project.State.OperatingSystem.Status."
1007 "OSStatus.Standby"))
1008 {
1009 waitForOSStandbyDelay(io, objServer, osStandbyTimer, cpuInfo);
1010 }
Jason M. Billsee6d80b2021-06-11 07:37:30 -07001011 },
1012 "xyz.openbmc_project.State.OperatingSystem",
1013 "/xyz/openbmc_project/state/os", "org.freedesktop.DBus.Properties",
1014 "Get", "xyz.openbmc_project.State.OperatingSystem.Status",
1015 "OperatingSystemState");
1016}
1017
Jason M. Bills3570b9e2023-07-31 12:26:30 -07001018int main(int /*argc*/, char* /*argv*/[])
Jason M. Billsd1e40602019-05-09 11:43:51 -07001019{
1020 // setup connection to dbus
Ed Tanous985d9d92023-03-01 10:35:33 -08001021 boost::asio::io_context io;
Jason M. Billsd1e40602019-05-09 11:43:51 -07001022 std::shared_ptr<sdbusplus::asio::connection> conn =
1023 std::make_shared<sdbusplus::asio::connection>(io);
1024
1025 // PECI PCIe Object
1026 conn->request_name(peci_pcie::peciPCIeObject);
1027 sdbusplus::asio::object_server server =
1028 sdbusplus::asio::object_server(conn);
1029
Andrei Kartashevd570dfd2020-12-16 17:33:16 +03001030 // CPU map
1031 std::vector<CPUInfo> cpuInfo;
1032
Jason M. Billsee6d80b2021-06-11 07:37:30 -07001033#ifdef WAIT_FOR_OS_STANDBY
1034 boost::asio::steady_timer osStandbyTimer(io);
1035 monitorOSStandby(io, conn, server, osStandbyTimer, cpuInfo);
1036#else
Jason M. Billsd1e40602019-05-09 11:43:51 -07001037 // Start the PECI check loop
1038 boost::asio::steady_timer peciWaitTimer(
1039 io, std::chrono::seconds(peci_pcie::peciCheckInterval));
Andrei Kartashevd570dfd2020-12-16 17:33:16 +03001040 peciWaitTimer.async_wait([&peciWaitTimer, &io, &server,
1041 &cpuInfo](const boost::system::error_code& ec) {
Jason M. Billsd1e40602019-05-09 11:43:51 -07001042 if (ec)
1043 {
1044 // operation_aborted is expected if timer is canceled
1045 // before completion.
1046 if (ec != boost::asio::error::operation_aborted)
1047 {
1048 std::cerr << "PECI Available Check async_wait failed " << ec;
1049 }
1050 return;
1051 }
Andrei Kartashevd570dfd2020-12-16 17:33:16 +03001052 peciAvailableCheck(peciWaitTimer, io, server, cpuInfo);
Jason M. Billsd1e40602019-05-09 11:43:51 -07001053 });
Jason M. Billsee6d80b2021-06-11 07:37:30 -07001054#endif
Jason M. Billsd1e40602019-05-09 11:43:51 -07001055
1056 io.run();
1057
1058 return 0;
1059}