blob: 1efb1e5802533a63a9b19851dd32340892d1da7f [file] [log] [blame]
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +05301#include "config.h"
2
Gunnar Mills94df8c92018-09-14 14:50:03 -05003#include "occ_manager.hpp"
4
5#include "i2c_occ.hpp"
Chicago Duanbb895cb2021-06-18 19:37:16 +08006#include "occ_dbus.hpp"
Gunnar Mills94df8c92018-09-14 14:50:03 -05007#include "utils.hpp"
8
George Liub5ca1012021-09-10 12:53:11 +08009#include <phosphor-logging/elog-errors.hpp>
10#include <phosphor-logging/log.hpp>
11#include <xyz/openbmc_project/Common/error.hpp>
12
Matt Spinlerd267cec2021-09-01 14:49:19 -050013#include <chrono>
Chicago Duanbb895cb2021-06-18 19:37:16 +080014#include <cmath>
George Liubcef3b42021-09-10 12:39:02 +080015#include <filesystem>
Chris Cain36f9cde2021-11-22 11:18:21 -060016#include <fstream>
Chicago Duanbb895cb2021-06-18 19:37:16 +080017#include <regex>
Gunnar Mills94df8c92018-09-14 14:50:03 -050018
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053019namespace open_power
20{
21namespace occ
22{
23
Matt Spinler8b8abee2021-08-25 15:18:21 -050024constexpr uint32_t fruTypeNotAvailable = 0xFF;
Matt Spinlera26f1522021-08-25 15:50:20 -050025constexpr auto fruTypeSuffix = "fru_type";
26constexpr auto faultSuffix = "fault";
27constexpr auto inputSuffix = "input";
Matt Spinlerace67d82021-10-18 13:41:57 -050028constexpr auto maxSuffix = "max";
Matt Spinler8b8abee2021-08-25 15:18:21 -050029
Chris Cain1718fd82022-02-16 16:39:50 -060030const auto HOST_ON_FILE = "/run/openbmc/host@0-on";
31
Chris Caina8857c52021-01-27 11:53:05 -060032using namespace phosphor::logging;
Chris Caina7b74dc2021-11-10 17:03:43 -060033using namespace std::literals::chrono_literals;
Chris Caina8857c52021-01-27 11:53:05 -060034
Matt Spinlera26f1522021-08-25 15:50:20 -050035template <typename T>
36T readFile(const std::string& path)
37{
38 std::ifstream ifs;
39 ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit |
40 std::ifstream::eofbit);
41 T data;
42
43 try
44 {
45 ifs.open(path);
46 ifs >> data;
47 ifs.close();
48 }
49 catch (const std::exception& e)
50 {
51 auto err = errno;
52 throw std::system_error(err, std::generic_category());
53 }
54
55 return data;
56}
57
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053058void Manager::findAndCreateObjects()
59{
Matt Spinlerd267cec2021-09-01 14:49:19 -050060#ifndef POWER10
Deepak Kodihalli370f06b2017-10-25 04:26:07 -050061 for (auto id = 0; id < MAX_CPUS; ++id)
62 {
Deepak Kodihalli30417a12017-12-04 00:54:01 -060063 // Create one occ per cpu
64 auto occ = std::string(OCC_NAME) + std::to_string(id);
65 createObjects(occ);
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +053066 }
Matt Spinlerd267cec2021-09-01 14:49:19 -050067#else
Chris Cain613dc902022-04-08 09:56:22 -050068 if (!pmode)
69 {
70 // Create the power mode object
71 pmode = std::make_unique<powermode::PowerMode>(
72 *this, powermode::PMODE_PATH, powermode::PIPS_PATH, event);
73 }
74
Chris Cain1718fd82022-02-16 16:39:50 -060075 if (!fs::exists(HOST_ON_FILE))
Matt Spinlerd267cec2021-09-01 14:49:19 -050076 {
Chris Cainbae4d072022-02-28 09:46:50 -060077 static bool statusObjCreated = false;
78 if (!statusObjCreated)
Chris Cain1718fd82022-02-16 16:39:50 -060079 {
Chris Cainbae4d072022-02-28 09:46:50 -060080 // Create the OCCs based on on the /dev/occX devices
81 auto occs = findOCCsInDev();
Chris Cain1718fd82022-02-16 16:39:50 -060082
Chris Cainbae4d072022-02-28 09:46:50 -060083 if (occs.empty() || (prevOCCSearch.size() != occs.size()))
Chris Cain1718fd82022-02-16 16:39:50 -060084 {
Chris Cainbae4d072022-02-28 09:46:50 -060085 // Something changed or no OCCs yet, try again in 10s.
86 // Note on the first pass prevOCCSearch will be empty,
87 // so there will be at least one delay to give things
88 // a chance to settle.
89 prevOCCSearch = occs;
90
91 log<level::INFO>(
92 fmt::format(
93 "Manager::findAndCreateObjects(): Waiting for OCCs (currently {})",
94 occs.size())
95 .c_str());
96
97 discoverTimer->restartOnce(10s);
98 }
99 else
100 {
101 // All OCCs appear to be available, create status objects
102
103 // createObjects requires OCC0 first.
104 std::sort(occs.begin(), occs.end());
105
106 log<level::INFO>(
107 fmt::format(
108 "Manager::findAndCreateObjects(): Creating {} OCC Status Objects",
109 occs.size())
110 .c_str());
111 for (auto id : occs)
112 {
113 createObjects(std::string(OCC_NAME) + std::to_string(id));
114 }
115 statusObjCreated = true;
Chris Cain6d8f37a2022-04-29 13:46:01 -0500116 waitingForAllOccActiveSensors = true;
Chris Cainbae4d072022-02-28 09:46:50 -0600117 }
118 }
119
Chris Cain6d8f37a2022-04-29 13:46:01 -0500120 if (statusObjCreated && waitingForAllOccActiveSensors)
Chris Cainbae4d072022-02-28 09:46:50 -0600121 {
122 static bool tracedHostWait = false;
123 if (utils::isHostRunning())
124 {
125 if (tracedHostWait)
126 {
127 log<level::INFO>(
128 "Manager::findAndCreateObjects(): Host is running");
129 tracedHostWait = false;
130 }
Chris Cainbae4d072022-02-28 09:46:50 -0600131 checkAllActiveSensors();
132 }
133 else
134 {
135 if (!tracedHostWait)
136 {
137 log<level::INFO>(
138 "Manager::findAndCreateObjects(): Waiting for host to start");
139 tracedHostWait = true;
140 }
141 discoverTimer->restartOnce(30s);
Chris Cain1718fd82022-02-16 16:39:50 -0600142 }
143 }
Matt Spinlerd267cec2021-09-01 14:49:19 -0500144 }
145 else
146 {
Chris Cain1718fd82022-02-16 16:39:50 -0600147 log<level::INFO>(
148 fmt::format(
149 "Manager::findAndCreateObjects(): Waiting for {} to complete...",
150 HOST_ON_FILE)
151 .c_str());
152 discoverTimer->restartOnce(10s);
Matt Spinlerd267cec2021-09-01 14:49:19 -0500153 }
154#endif
155}
156
Chris Cainbae4d072022-02-28 09:46:50 -0600157#ifdef POWER10
158// Check if all occActive sensors are available
159void Manager::checkAllActiveSensors()
160{
161 static bool allActiveSensorAvailable = false;
162 static bool tracedSensorWait = false;
163
164 // Start with the assumption that all are available
165 allActiveSensorAvailable = true;
166 for (auto& obj : statusObjects)
167 {
Chris Cain7f89e4d2022-05-09 13:27:45 -0500168 if (!obj->occActive())
Chris Cainbae4d072022-02-28 09:46:50 -0600169 {
Chris Cain7f89e4d2022-05-09 13:27:45 -0500170 if (!obj->getPldmSensorReceived())
Chris Cainbae4d072022-02-28 09:46:50 -0600171 {
Chris Cain7f89e4d2022-05-09 13:27:45 -0500172 auto instance = obj->getOccInstanceID();
173 // Check if sensor was queued while waiting for discovery
174 auto match = queuedActiveState.find(instance);
175 if (match != queuedActiveState.end())
Chris Cainbd551de2022-04-26 13:41:16 -0500176 {
Chris Cain7f89e4d2022-05-09 13:27:45 -0500177 queuedActiveState.erase(match);
Chris Cainbd551de2022-04-26 13:41:16 -0500178 log<level::INFO>(
179 fmt::format(
Chris Cain7f89e4d2022-05-09 13:27:45 -0500180 "checkAllActiveSensors(): OCC{} is ACTIVE (queued)",
Chris Cainbd551de2022-04-26 13:41:16 -0500181 instance)
182 .c_str());
Chris Cain7f89e4d2022-05-09 13:27:45 -0500183 obj->occActive(true);
Chris Cainbd551de2022-04-26 13:41:16 -0500184 }
Chris Cain7f89e4d2022-05-09 13:27:45 -0500185 else
186 {
187 allActiveSensorAvailable = false;
188 if (!tracedSensorWait)
189 {
190 log<level::INFO>(
191 fmt::format(
192 "checkAllActiveSensors(): Waiting on OCC{} Active sensor",
193 instance)
194 .c_str());
195 tracedSensorWait = true;
196 }
197 pldmHandle->checkActiveSensor(obj->getOccInstanceID());
198 break;
199 }
Chris Cainbd551de2022-04-26 13:41:16 -0500200 }
Chris Cainbae4d072022-02-28 09:46:50 -0600201 }
202 }
203
204 if (allActiveSensorAvailable)
205 {
206 // All sensors were found, disable the discovery timer
Chris Cain7f89e4d2022-05-09 13:27:45 -0500207 if (discoverTimer->isEnabled())
208 {
209 discoverTimer.reset();
210 }
Chris Cainbae4d072022-02-28 09:46:50 -0600211
Chris Cain7f89e4d2022-05-09 13:27:45 -0500212 if (waitingForAllOccActiveSensors)
213 {
214 log<level::INFO>(
215 "checkAllActiveSensors(): OCC Active sensors are available");
216 waitingForAllOccActiveSensors = false;
217 }
218 queuedActiveState.clear();
Chris Cainbae4d072022-02-28 09:46:50 -0600219 tracedSensorWait = false;
220 }
221 else
222 {
223 // Not all sensors were available, so keep waiting
224 if (!tracedSensorWait)
225 {
226 log<level::INFO>(
Chris Cainbd551de2022-04-26 13:41:16 -0500227 "checkAllActiveSensors(): Waiting for OCC Active sensors to become available");
Chris Cainbae4d072022-02-28 09:46:50 -0600228 tracedSensorWait = true;
229 }
Chris Cain7f89e4d2022-05-09 13:27:45 -0500230 if (discoverTimer->isEnabled())
231 {
232 discoverTimer->restartOnce(10s);
233 }
Chris Cainbae4d072022-02-28 09:46:50 -0600234 }
235}
236#endif
237
Matt Spinlerd267cec2021-09-01 14:49:19 -0500238std::vector<int> Manager::findOCCsInDev()
239{
240 std::vector<int> occs;
241 std::regex expr{R"(occ(\d+)$)"};
242
243 for (auto& file : fs::directory_iterator("/dev"))
244 {
245 std::smatch match;
246 std::string path{file.path().string()};
247 if (std::regex_search(path, match, expr))
248 {
249 auto num = std::stoi(match[1].str());
250
251 // /dev numbering starts at 1, ours starts at 0.
252 occs.push_back(num - 1);
253 }
254 }
255
256 return occs;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530257}
258
259int Manager::cpuCreated(sdbusplus::message::message& msg)
260{
George Liubcef3b42021-09-10 12:39:02 +0800261 namespace fs = std::filesystem;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530262
263 sdbusplus::message::object_path o;
264 msg.read(o);
265 fs::path cpuPath(std::string(std::move(o)));
266
267 auto name = cpuPath.filename().string();
268 auto index = name.find(CPU_NAME);
269 name.replace(index, std::strlen(CPU_NAME), OCC_NAME);
270
271 createObjects(name);
272
273 return 0;
274}
275
276void Manager::createObjects(const std::string& occ)
277{
278 auto path = fs::path(OCC_CONTROL_ROOT) / occ;
279
Gunnar Mills94df8c92018-09-14 14:50:03 -0500280 statusObjects.emplace_back(std::make_unique<Status>(
George Liuf3b75142021-06-10 11:22:50 +0800281 event, path.c_str(), *this,
Chris Cain36f9cde2021-11-22 11:18:21 -0600282#ifdef POWER10
283 pmode,
284#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -0500285 std::bind(std::mem_fn(&Manager::statusCallBack), this,
Sheldon Bailey373af752022-02-21 15:14:00 -0600286 std::placeholders::_1, std::placeholders::_2)
Tom Joseph00325232020-07-29 17:51:48 +0530287#ifdef PLDM
288 ,
289 std::bind(std::mem_fn(&pldm::Interface::resetOCC), pldmHandle.get(),
290 std::placeholders::_1)
291#endif
292 ));
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530293
Chris Cain40501a22022-03-14 17:33:27 -0500294 // Create the power cap monitor object
295 if (!pcap)
296 {
297 pcap = std::make_unique<open_power::occ::powercap::PowerCap>(
298 *statusObjects.back());
299 }
300
Chris Cain36f9cde2021-11-22 11:18:21 -0600301 if (statusObjects.back()->isMasterOcc())
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530302 {
Chris Cain36f9cde2021-11-22 11:18:21 -0600303 log<level::INFO>(
304 fmt::format("Manager::createObjects(): OCC{} is the master",
305 statusObjects.back()->getOccInstanceID())
306 .c_str());
307 _pollTimer->setEnabled(false);
308
Chris Cain78e86012021-03-04 16:15:31 -0600309#ifdef POWER10
Chris Cain6fa848a2022-01-24 14:54:38 -0600310 // Set the master OCC on the PowerMode object
311 pmode->setMasterOcc(path);
Chris Cain78e86012021-03-04 16:15:31 -0600312#endif
Chris Cain36f9cde2021-11-22 11:18:21 -0600313 }
314
315 passThroughObjects.emplace_back(std::make_unique<PassThrough>(path.c_str()
316#ifdef POWER10
317 ,
318 pmode
319#endif
320 ));
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530321}
322
Sheldon Bailey373af752022-02-21 15:14:00 -0600323void Manager::statusCallBack(instanceID instance, bool status)
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530324{
Gunnar Mills94df8c92018-09-14 14:50:03 -0500325 using InternalFailure =
326 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530327
328 // At this time, it won't happen but keeping it
329 // here just in case something changes in the future
330 if ((activeCount == 0) && (!status))
331 {
Sheldon Bailey373af752022-02-21 15:14:00 -0600332 log<level::ERR>(
333 fmt::format("Invalid update on OCCActive with OCC{}", instance)
334 .c_str());
335
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530336 elog<InternalFailure>();
337 }
338
Chris Caina7b74dc2021-11-10 17:03:43 -0600339 if (status == true)
Eddie Jamesdae2d942017-12-20 10:50:03 -0600340 {
Chris Caina7b74dc2021-11-10 17:03:43 -0600341 // OCC went active
342 ++activeCount;
343
344#ifdef POWER10
345 if (activeCount == 1)
Eddie Jamesdae2d942017-12-20 10:50:03 -0600346 {
Chris Caina7b74dc2021-11-10 17:03:43 -0600347 // First OCC went active (allow some time for all OCCs to go active)
Chris Cainbd551de2022-04-26 13:41:16 -0500348 waitForAllOccsTimer->restartOnce(60s);
Matt Spinler53f68142021-08-25 15:47:31 -0500349 }
350#endif
Chris Caina7b74dc2021-11-10 17:03:43 -0600351
352 if (activeCount == statusObjects.size())
353 {
354#ifdef POWER10
355 // All OCCs are now running
356 if (waitForAllOccsTimer->isEnabled())
357 {
358 // stop occ wait timer
359 waitForAllOccsTimer->setEnabled(false);
360 }
361#endif
362
363 // Verify master OCC and start presence monitor
364 validateOccMaster();
365 }
366
367 // Start poll timer if not already started
368 if (!_pollTimer->isEnabled())
369 {
370 log<level::INFO>(
Chris Cain36f9cde2021-11-22 11:18:21 -0600371 fmt::format("Manager: OCCs will be polled every {} seconds",
372 pollInterval)
Chris Caina7b74dc2021-11-10 17:03:43 -0600373 .c_str());
374
375 // Send poll and start OCC poll timer
376 pollerTimerExpired();
377 }
378 }
379 else
380 {
381 // OCC went away
382 --activeCount;
383
384 if (activeCount == 0)
385 {
386 // No OCCs are running
387
388 // Stop OCC poll timer
389 if (_pollTimer->isEnabled())
390 {
391 log<level::INFO>(
392 "Manager::statusCallBack(): OCCs are not running, stopping poll timer");
393 _pollTimer->setEnabled(false);
394 }
395
396#ifdef POWER10
397 // stop wait timer
398 if (waitForAllOccsTimer->isEnabled())
399 {
400 waitForAllOccsTimer->setEnabled(false);
401 }
402#endif
Chris Caina7b74dc2021-11-10 17:03:43 -0600403 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600404#ifdef READ_OCC_SENSORS
405 // Clear OCC sensors
Sheldon Baileyc8dd4592022-05-12 10:15:14 -0500406 setSensorValueToNaN(instance);
Sheldon Bailey373af752022-02-21 15:14:00 -0600407#endif
Chris Caina8857c52021-01-27 11:53:05 -0600408 }
Chris Cainbae4d072022-02-28 09:46:50 -0600409
410#ifdef POWER10
411 if (waitingForAllOccActiveSensors)
412 {
Chris Cain6d8f37a2022-04-29 13:46:01 -0500413 if (utils::isHostRunning())
414 {
415 checkAllActiveSensors();
416 }
Chris Cainbae4d072022-02-28 09:46:50 -0600417 }
418#endif
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530419}
420
421#ifdef I2C_OCC
422void Manager::initStatusObjects()
423{
424 // Make sure we have a valid path string
425 static_assert(sizeof(DEV_PATH) != 0);
426
427 auto deviceNames = i2c_occ::getOccHwmonDevices(DEV_PATH);
428 for (auto& name : deviceNames)
429 {
430 i2c_occ::i2cToDbus(name);
Lei YUb5259a12017-09-01 16:22:40 +0800431 name = std::string(OCC_NAME) + '_' + name;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530432 auto path = fs::path(OCC_CONTROL_ROOT) / name;
433 statusObjects.emplace_back(
George Liuf3b75142021-06-10 11:22:50 +0800434 std::make_unique<Status>(event, path.c_str(), *this));
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530435 }
Chris Cain40501a22022-03-14 17:33:27 -0500436 // The first device is master occ
437 pcap = std::make_unique<open_power::occ::powercap::PowerCap>(
438 *statusObjects.front());
Chris Cain78e86012021-03-04 16:15:31 -0600439#ifdef POWER10
Chris Cain5d66a0a2022-02-09 08:52:10 -0600440 pmode = std::make_unique<powermode::PowerMode>(*this, powermode::PMODE_PATH,
441 powermode::PIPS_PATH);
Chris Cain6fa848a2022-01-24 14:54:38 -0600442 // Set the master OCC on the PowerMode object
443 pmode->setMasterOcc(path);
Chris Cain78e86012021-03-04 16:15:31 -0600444#endif
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530445}
446#endif
447
Tom Joseph815f9f52020-07-27 12:12:13 +0530448#ifdef PLDM
Eddie Jamescbad2192021-10-07 09:39:39 -0500449void Manager::sbeTimeout(unsigned int instance)
450{
Eddie James2a751d72022-03-04 09:16:12 -0600451 auto obj = std::find_if(statusObjects.begin(), statusObjects.end(),
452 [instance](const auto& obj) {
453 return instance == obj->getOccInstanceID();
454 });
Eddie Jamescbad2192021-10-07 09:39:39 -0500455
Eddie Jamescb018da2022-03-05 11:49:37 -0600456 if (obj != statusObjects.end() && (*obj)->occActive())
Eddie James2a751d72022-03-04 09:16:12 -0600457 {
Chris Cainbae4d072022-02-28 09:46:50 -0600458 log<level::INFO>(
459 fmt::format("SBE timeout, requesting HRESET (OCC{})", instance)
460 .c_str());
Eddie Jamescbad2192021-10-07 09:39:39 -0500461
Eddie James2a751d72022-03-04 09:16:12 -0600462 setSBEState(instance, SBE_STATE_NOT_USABLE);
463
464 pldmHandle->sendHRESET(instance);
465 }
Eddie Jamescbad2192021-10-07 09:39:39 -0500466}
467
Tom Joseph815f9f52020-07-27 12:12:13 +0530468bool Manager::updateOCCActive(instanceID instance, bool status)
469{
Chris Cain7e374fb2022-04-07 09:47:23 -0500470 auto obj = std::find_if(statusObjects.begin(), statusObjects.end(),
471 [instance](const auto& obj) {
472 return instance == obj->getOccInstanceID();
473 });
474
475 if (obj != statusObjects.end())
476 {
Chris Cain733b2012022-05-04 11:54:06 -0500477 (*obj)->setPldmSensorReceived(true);
Chris Cain7e374fb2022-04-07 09:47:23 -0500478 return (*obj)->occActive(status);
479 }
480 else
481 {
482 log<level::WARNING>(
483 fmt::format(
484 "Manager::updateOCCActive: No status object to update for OCC{} (active={})",
485 instance, status)
486 .c_str());
Chris Cainbd551de2022-04-26 13:41:16 -0500487 if (status == true)
488 {
489 // OCC went active
490 queuedActiveState.insert(instance);
491 }
492 else
493 {
494 auto match = queuedActiveState.find(instance);
495 if (match != queuedActiveState.end())
496 {
497 // OCC was disabled
498 queuedActiveState.erase(match);
499 }
500 }
Chris Cain7e374fb2022-04-07 09:47:23 -0500501 return false;
502 }
Tom Joseph815f9f52020-07-27 12:12:13 +0530503}
Eddie Jamescbad2192021-10-07 09:39:39 -0500504
505void Manager::sbeHRESETResult(instanceID instance, bool success)
506{
507 if (success)
508 {
Chris Cainbae4d072022-02-28 09:46:50 -0600509 log<level::INFO>(
510 fmt::format("HRESET succeeded (OCC{})", instance).c_str());
Eddie Jamescbad2192021-10-07 09:39:39 -0500511
512 setSBEState(instance, SBE_STATE_BOOTED);
513
514 return;
515 }
516
517 setSBEState(instance, SBE_STATE_FAILED);
518
519 if (sbeCanDump(instance))
520 {
Chris Cainbae4d072022-02-28 09:46:50 -0600521 log<level::INFO>(
522 fmt::format("HRESET failed (OCC{}), triggering SBE dump", instance)
523 .c_str());
Eddie Jamescbad2192021-10-07 09:39:39 -0500524
525 auto& bus = utils::getBus();
526 uint32_t src6 = instance << 16;
527 uint32_t logId =
528 FFDC::createPEL("org.open_power.Processor.Error.SbeChipOpTimeout",
529 src6, "SBE command timeout");
530
531 try
532 {
George Liuf3a4a692021-12-28 13:59:51 +0800533 constexpr auto path = "/org/openpower/dump";
534 constexpr auto interface = "xyz.openbmc_project.Dump.Create";
535 constexpr auto function = "CreateDump";
536
Eddie Jamescbad2192021-10-07 09:39:39 -0500537 std::string service = utils::getService(path, interface);
538 auto method =
539 bus.new_method_call(service.c_str(), path, interface, function);
540
541 std::map<std::string, std::variant<std::string, uint64_t>>
542 createParams{
543 {"com.ibm.Dump.Create.CreateParameters.ErrorLogId",
544 uint64_t(logId)},
545 {"com.ibm.Dump.Create.CreateParameters.DumpType",
546 "com.ibm.Dump.Create.DumpType.SBE"},
547 {"com.ibm.Dump.Create.CreateParameters.FailingUnitId",
548 uint64_t(instance)},
549 };
550
551 method.append(createParams);
552
553 auto response = bus.call(method);
554 }
555 catch (const sdbusplus::exception::exception& e)
556 {
557 constexpr auto ERROR_DUMP_DISABLED =
558 "xyz.openbmc_project.Dump.Create.Error.Disabled";
559 if (e.name() == ERROR_DUMP_DISABLED)
560 {
561 log<level::INFO>("Dump is disabled, skipping");
562 }
563 else
564 {
565 log<level::ERR>("Dump failed");
566 }
567 }
568 }
569}
570
571bool Manager::sbeCanDump(unsigned int instance)
572{
573 struct pdbg_target* proc = getPdbgTarget(instance);
574
575 if (!proc)
576 {
577 // allow the dump in the error case
578 return true;
579 }
580
581 try
582 {
583 if (!openpower::phal::sbe::isDumpAllowed(proc))
584 {
585 return false;
586 }
587
588 if (openpower::phal::pdbg::isSbeVitalAttnActive(proc))
589 {
590 return false;
591 }
592 }
593 catch (openpower::phal::exception::SbeError& e)
594 {
595 log<level::INFO>("Failed to query SBE state");
596 }
597
598 // allow the dump in the error case
599 return true;
600}
601
602void Manager::setSBEState(unsigned int instance, enum sbe_state state)
603{
604 struct pdbg_target* proc = getPdbgTarget(instance);
605
606 if (!proc)
607 {
608 return;
609 }
610
611 try
612 {
613 openpower::phal::sbe::setState(proc, state);
614 }
615 catch (const openpower::phal::exception::SbeError& e)
616 {
617 log<level::ERR>("Failed to set SBE state");
618 }
619}
620
621struct pdbg_target* Manager::getPdbgTarget(unsigned int instance)
622{
623 if (!pdbgInitialized)
624 {
625 try
626 {
627 openpower::phal::pdbg::init();
628 pdbgInitialized = true;
629 }
630 catch (const openpower::phal::exception::PdbgError& e)
631 {
632 log<level::ERR>("pdbg initialization failed");
633 return nullptr;
634 }
635 }
636
637 struct pdbg_target* proc = nullptr;
638 pdbg_for_each_class_target("proc", proc)
639 {
640 if (pdbg_target_index(proc) == instance)
641 {
642 return proc;
643 }
644 }
645
646 log<level::ERR>("Failed to get pdbg target");
647 return nullptr;
648}
Tom Joseph815f9f52020-07-27 12:12:13 +0530649#endif
650
Chris Caina8857c52021-01-27 11:53:05 -0600651void Manager::pollerTimerExpired()
652{
Chris Caina8857c52021-01-27 11:53:05 -0600653 if (!_pollTimer)
654 {
655 log<level::ERR>(
656 "Manager::pollerTimerExpired() ERROR: Timer not defined");
657 return;
658 }
659
660 for (auto& obj : statusObjects)
661 {
Chris Caina7b74dc2021-11-10 17:03:43 -0600662 if (!obj->occActive())
663 {
664 // OCC is not running yet
665#ifdef READ_OCC_SENSORS
Chris Cain5d66a0a2022-02-09 08:52:10 -0600666 auto id = obj->getOccInstanceID();
Sheldon Baileyc8dd4592022-05-12 10:15:14 -0500667 setSensorValueToNaN(id);
Chris Caina7b74dc2021-11-10 17:03:43 -0600668#endif
669 continue;
670 }
671
Chris Caina8857c52021-01-27 11:53:05 -0600672 // Read sysfs to force kernel to poll OCC
673 obj->readOccState();
Chicago Duanbb895cb2021-06-18 19:37:16 +0800674
675#ifdef READ_OCC_SENSORS
676 // Read occ sensor values
Chris Cain5d66a0a2022-02-09 08:52:10 -0600677 getSensorValues(obj);
Chicago Duanbb895cb2021-06-18 19:37:16 +0800678#endif
Chris Caina8857c52021-01-27 11:53:05 -0600679 }
680
Chris Caina7b74dc2021-11-10 17:03:43 -0600681 if (activeCount > 0)
682 {
683 // Restart OCC poll timer
684 _pollTimer->restartOnce(std::chrono::seconds(pollInterval));
685 }
686 else
687 {
688 // No OCCs running, so poll timer will not be restarted
689 log<level::INFO>(
690 fmt::format(
691 "Manager::pollerTimerExpired: poll timer will not be restarted")
692 .c_str());
693 }
Chris Caina8857c52021-01-27 11:53:05 -0600694}
695
Chicago Duanbb895cb2021-06-18 19:37:16 +0800696#ifdef READ_OCC_SENSORS
697void Manager::readTempSensors(const fs::path& path, uint32_t id)
698{
Chicago Duanbb895cb2021-06-18 19:37:16 +0800699 std::regex expr{"temp\\d+_label$"}; // Example: temp5_label
700 for (auto& file : fs::directory_iterator(path))
701 {
702 if (!std::regex_search(file.path().string(), expr))
703 {
704 continue;
705 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800706
Matt Spinlera26f1522021-08-25 15:50:20 -0500707 uint32_t labelValue{0};
708
709 try
710 {
711 labelValue = readFile<uint32_t>(file.path());
712 }
713 catch (const std::system_error& e)
714 {
715 log<level::DEBUG>(
716 fmt::format("readTempSensors: Failed reading {}, errno = {}",
717 file.path().string(), e.code().value())
718 .c_str());
Chicago Duanbb895cb2021-06-18 19:37:16 +0800719 continue;
720 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800721
722 const std::string& tempLabel = "label";
723 const std::string filePathString = file.path().string().substr(
724 0, file.path().string().length() - tempLabel.length());
Matt Spinlera26f1522021-08-25 15:50:20 -0500725
726 uint32_t fruTypeValue{0};
727 try
Chicago Duanbb895cb2021-06-18 19:37:16 +0800728 {
Matt Spinlera26f1522021-08-25 15:50:20 -0500729 fruTypeValue = readFile<uint32_t>(filePathString + fruTypeSuffix);
730 }
731 catch (const std::system_error& e)
732 {
Chicago Duanbb895cb2021-06-18 19:37:16 +0800733 log<level::DEBUG>(
Matt Spinlera26f1522021-08-25 15:50:20 -0500734 fmt::format("readTempSensors: Failed reading {}, errno = {}",
735 filePathString + fruTypeSuffix, e.code().value())
Chicago Duanbb895cb2021-06-18 19:37:16 +0800736 .c_str());
737 continue;
738 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800739
740 std::string sensorPath =
741 OCC_SENSORS_ROOT + std::string("/temperature/");
742
Matt Spinlerace67d82021-10-18 13:41:57 -0500743 std::string dvfsTempPath;
744
Chicago Duanbb895cb2021-06-18 19:37:16 +0800745 if (fruTypeValue == VRMVdd)
746 {
747 sensorPath.append("vrm_vdd" + std::to_string(id) + "_temp");
748 }
Matt Spinlerace67d82021-10-18 13:41:57 -0500749 else if (fruTypeValue == processorIoRing)
750 {
751 sensorPath.append("proc" + std::to_string(id) + "_ioring_temp");
752 dvfsTempPath = std::string{OCC_SENSORS_ROOT} + "/temperature/proc" +
753 std::to_string(id) + "_ioring_dvfs_temp";
754 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800755 else
756 {
Matt Spinler14d14022021-08-25 15:38:29 -0500757 uint16_t type = (labelValue & 0xFF000000) >> 24;
758 uint16_t instanceID = labelValue & 0x0000FFFF;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800759
760 if (type == OCC_DIMM_TEMP_SENSOR_TYPE)
761 {
Matt Spinler8b8abee2021-08-25 15:18:21 -0500762 if (fruTypeValue == fruTypeNotAvailable)
763 {
764 // Not all DIMM related temps are available to read
765 // (no _input file in this case)
766 continue;
767 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800768 auto iter = dimmTempSensorName.find(fruTypeValue);
769 if (iter == dimmTempSensorName.end())
770 {
George Liub5ca1012021-09-10 12:53:11 +0800771 log<level::ERR>(
772 fmt::format(
773 "readTempSensors: Fru type error! fruTypeValue = {}) ",
774 fruTypeValue)
775 .c_str());
Chicago Duanbb895cb2021-06-18 19:37:16 +0800776 continue;
777 }
778
779 sensorPath.append("dimm" + std::to_string(instanceID) +
780 iter->second);
781 }
782 else if (type == OCC_CPU_TEMP_SENSOR_TYPE)
783 {
Matt Spinlerace67d82021-10-18 13:41:57 -0500784 if (fruTypeValue == processorCore)
Chicago Duanbb895cb2021-06-18 19:37:16 +0800785 {
Matt Spinlerace67d82021-10-18 13:41:57 -0500786 // The OCC reports small core temps, of which there are
787 // two per big core. All current P10 systems are in big
788 // core mode, so use a big core name.
789 uint16_t coreNum = instanceID / 2;
790 uint16_t tempNum = instanceID % 2;
791 sensorPath.append("proc" + std::to_string(id) + "_core" +
792 std::to_string(coreNum) + "_" +
793 std::to_string(tempNum) + "_temp");
794
795 dvfsTempPath = std::string{OCC_SENSORS_ROOT} +
796 "/temperature/proc" + std::to_string(id) +
797 "_core_dvfs_temp";
798 }
799 else
800 {
Chicago Duanbb895cb2021-06-18 19:37:16 +0800801 continue;
802 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800803 }
804 else
805 {
806 continue;
807 }
808 }
809
Matt Spinlerace67d82021-10-18 13:41:57 -0500810 // The dvfs temp file only needs to be read once per chip per type.
811 if (!dvfsTempPath.empty() &&
812 !dbus::OccDBusSensors::getOccDBus().hasDvfsTemp(dvfsTempPath))
813 {
814 try
815 {
816 auto dvfsValue = readFile<double>(filePathString + maxSuffix);
817
818 dbus::OccDBusSensors::getOccDBus().setDvfsTemp(
819 dvfsTempPath, dvfsValue * std::pow(10, -3));
820 }
821 catch (const std::system_error& e)
822 {
823 log<level::DEBUG>(
824 fmt::format(
825 "readTempSensors: Failed reading {}, errno = {}",
826 filePathString + maxSuffix, e.code().value())
827 .c_str());
828 }
829 }
830
Matt Spinlera26f1522021-08-25 15:50:20 -0500831 uint32_t faultValue{0};
832 try
Chicago Duanbb895cb2021-06-18 19:37:16 +0800833 {
Matt Spinlera26f1522021-08-25 15:50:20 -0500834 faultValue = readFile<uint32_t>(filePathString + faultSuffix);
835 }
836 catch (const std::system_error& e)
837 {
838 log<level::DEBUG>(
839 fmt::format("readTempSensors: Failed reading {}, errno = {}",
840 filePathString + faultSuffix, e.code().value())
841 .c_str());
842 continue;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800843 }
844
Sheldon Baileyc8dd4592022-05-12 10:15:14 -0500845 // NOTE: if OCC sends back 0xFF kernal sets this fault value to 1.
Matt Spinlera26f1522021-08-25 15:50:20 -0500846 if (faultValue != 0)
Chicago Duanbb895cb2021-06-18 19:37:16 +0800847 {
Chris Cain5d66a0a2022-02-09 08:52:10 -0600848 dbus::OccDBusSensors::getOccDBus().setValue(
Matt Spinlera26f1522021-08-25 15:50:20 -0500849 sensorPath, std::numeric_limits<double>::quiet_NaN());
Chicago Duanbb895cb2021-06-18 19:37:16 +0800850
Chris Cain5d66a0a2022-02-09 08:52:10 -0600851 dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath,
852 false);
Chicago Duanbb895cb2021-06-18 19:37:16 +0800853
Matt Spinlera26f1522021-08-25 15:50:20 -0500854 continue;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800855 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500856
857 double tempValue{0};
858
859 try
Chicago Duanbb895cb2021-06-18 19:37:16 +0800860 {
Matt Spinlera26f1522021-08-25 15:50:20 -0500861 tempValue = readFile<double>(filePathString + inputSuffix);
Chicago Duanbb895cb2021-06-18 19:37:16 +0800862 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500863 catch (const std::system_error& e)
864 {
865 log<level::DEBUG>(
866 fmt::format("readTempSensors: Failed reading {}, errno = {}",
867 filePathString + inputSuffix, e.code().value())
868 .c_str());
Sheldon Baileycd0940b2022-04-26 14:24:05 -0500869
870 // if errno == EAGAIN(Resource temporarily unavailable) then set
871 // temp to 0, to avoid using old temp, and affecting FAN Control.
872 if (e.code().value() == EAGAIN)
873 {
874 tempValue = 0;
875 }
876 // else the errno would be something like
877 // EBADF(Bad file descriptor)
878 // or ENOENT(No such file or directory)
879 else
880 {
881 continue;
882 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500883 }
884
Chris Cain5d66a0a2022-02-09 08:52:10 -0600885 dbus::OccDBusSensors::getOccDBus().setValue(
Matt Spinlera26f1522021-08-25 15:50:20 -0500886 sensorPath, tempValue * std::pow(10, -3));
887
Chris Cain5d66a0a2022-02-09 08:52:10 -0600888 dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath,
889 true);
Matt Spinlera26f1522021-08-25 15:50:20 -0500890
Chris Cain6fa848a2022-01-24 14:54:38 -0600891 // At this point, the sensor will be created for sure.
892 if (existingSensors.find(sensorPath) == existingSensors.end())
893 {
Chris Cain5d66a0a2022-02-09 08:52:10 -0600894 dbus::OccDBusSensors::getOccDBus().setChassisAssociation(
895 sensorPath);
Chris Cain6fa848a2022-01-24 14:54:38 -0600896 }
897
Matt Spinlera26f1522021-08-25 15:50:20 -0500898 existingSensors[sensorPath] = id;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800899 }
900 return;
901}
902
903std::optional<std::string>
904 Manager::getPowerLabelFunctionID(const std::string& value)
905{
906 // If the value is "system", then the FunctionID is "system".
907 if (value == "system")
908 {
909 return value;
910 }
911
912 // If the value is not "system", then the label value have 3 numbers, of
913 // which we only care about the middle one:
914 // <sensor id>_<function id>_<apss channel>
915 // eg: The value is "0_10_5" , then the FunctionID is "10".
916 if (value.find("_") == std::string::npos)
917 {
918 return std::nullopt;
919 }
920
921 auto powerLabelValue = value.substr((value.find("_") + 1));
922
923 if (powerLabelValue.find("_") == std::string::npos)
924 {
925 return std::nullopt;
926 }
927
928 return powerLabelValue.substr(0, powerLabelValue.find("_"));
929}
930
931void Manager::readPowerSensors(const fs::path& path, uint32_t id)
932{
Chicago Duanbb895cb2021-06-18 19:37:16 +0800933 std::regex expr{"power\\d+_label$"}; // Example: power5_label
934 for (auto& file : fs::directory_iterator(path))
935 {
936 if (!std::regex_search(file.path().string(), expr))
937 {
938 continue;
939 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800940
Matt Spinlera26f1522021-08-25 15:50:20 -0500941 std::string labelValue;
942 try
943 {
944 labelValue = readFile<std::string>(file.path());
945 }
946 catch (const std::system_error& e)
947 {
948 log<level::DEBUG>(
949 fmt::format("readPowerSensors: Failed reading {}, errno = {}",
950 file.path().string(), e.code().value())
951 .c_str());
Chicago Duanbb895cb2021-06-18 19:37:16 +0800952 continue;
953 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800954
955 auto functionID = getPowerLabelFunctionID(labelValue);
956 if (functionID == std::nullopt)
957 {
958 continue;
959 }
960
961 const std::string& tempLabel = "label";
962 const std::string filePathString = file.path().string().substr(
963 0, file.path().string().length() - tempLabel.length());
964
965 std::string sensorPath = OCC_SENSORS_ROOT + std::string("/power/");
966
967 auto iter = powerSensorName.find(*functionID);
968 if (iter == powerSensorName.end())
969 {
970 continue;
971 }
972 sensorPath.append(iter->second);
973
Matt Spinlera26f1522021-08-25 15:50:20 -0500974 double tempValue{0};
975
976 try
Chicago Duanbb895cb2021-06-18 19:37:16 +0800977 {
Matt Spinlera26f1522021-08-25 15:50:20 -0500978 tempValue = readFile<double>(filePathString + inputSuffix);
Chicago Duanbb895cb2021-06-18 19:37:16 +0800979 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500980 catch (const std::system_error& e)
Chicago Duanbb895cb2021-06-18 19:37:16 +0800981 {
Chicago Duanbb895cb2021-06-18 19:37:16 +0800982 log<level::DEBUG>(
Chris Cain5d66a0a2022-02-09 08:52:10 -0600983 fmt::format("readPowerSensors: Failed reading {}, errno = {}",
Matt Spinlera26f1522021-08-25 15:50:20 -0500984 filePathString + inputSuffix, e.code().value())
Chicago Duanbb895cb2021-06-18 19:37:16 +0800985 .c_str());
Matt Spinlera26f1522021-08-25 15:50:20 -0500986 continue;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800987 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500988
Chris Cain5d66a0a2022-02-09 08:52:10 -0600989 dbus::OccDBusSensors::getOccDBus().setUnit(
Chris Caind84a8332022-01-13 08:58:45 -0600990 sensorPath, "xyz.openbmc_project.Sensor.Value.Unit.Watts");
991
Chris Cain5d66a0a2022-02-09 08:52:10 -0600992 dbus::OccDBusSensors::getOccDBus().setValue(
Matt Spinlera26f1522021-08-25 15:50:20 -0500993 sensorPath, tempValue * std::pow(10, -3) * std::pow(10, -3));
994
Chris Cain5d66a0a2022-02-09 08:52:10 -0600995 dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath,
996 true);
Matt Spinlera26f1522021-08-25 15:50:20 -0500997
Matt Spinler5901abd2021-09-23 13:50:03 -0500998 if (existingSensors.find(sensorPath) == existingSensors.end())
999 {
Chris Cain5d66a0a2022-02-09 08:52:10 -06001000 dbus::OccDBusSensors::getOccDBus().setChassisAssociation(
1001 sensorPath);
Matt Spinler5901abd2021-09-23 13:50:03 -05001002 }
1003
Matt Spinlera26f1522021-08-25 15:50:20 -05001004 existingSensors[sensorPath] = id;
Chicago Duanbb895cb2021-06-18 19:37:16 +08001005 }
1006 return;
1007}
1008
Sheldon Baileyc8dd4592022-05-12 10:15:14 -05001009void Manager::setSensorValueToNaN(uint32_t id) const
Chicago Duanbb895cb2021-06-18 19:37:16 +08001010{
1011 for (const auto& [sensorPath, occId] : existingSensors)
1012 {
1013 if (occId == id)
1014 {
Chris Cain5d66a0a2022-02-09 08:52:10 -06001015 dbus::OccDBusSensors::getOccDBus().setValue(
Chicago Duanbb895cb2021-06-18 19:37:16 +08001016 sensorPath, std::numeric_limits<double>::quiet_NaN());
Sheldon Baileyc8dd4592022-05-12 10:15:14 -05001017
1018 dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath,
1019 true);
Chicago Duanbb895cb2021-06-18 19:37:16 +08001020 }
1021 }
1022 return;
1023}
1024
Sheldon Bailey373af752022-02-21 15:14:00 -06001025void Manager::setSensorValueToNonFunctional(uint32_t id) const
1026{
1027 for (const auto& [sensorPath, occId] : existingSensors)
1028 {
1029 if (occId == id)
1030 {
1031 dbus::OccDBusSensors::getOccDBus().setValue(
1032 sensorPath, std::numeric_limits<double>::quiet_NaN());
1033
1034 dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath,
1035 false);
1036 }
1037 }
1038 return;
1039}
1040
Chris Cain5d66a0a2022-02-09 08:52:10 -06001041void Manager::getSensorValues(std::unique_ptr<Status>& occ)
Chicago Duanbb895cb2021-06-18 19:37:16 +08001042{
Chris Caine2d0a432022-03-28 11:08:49 -05001043 static bool tracedError[8] = {0};
1044 const fs::path sensorPath = occ->getHwmonPath();
Chris Cain5d66a0a2022-02-09 08:52:10 -06001045 const uint32_t id = occ->getOccInstanceID();
Chicago Duanbb895cb2021-06-18 19:37:16 +08001046
Chris Caine2d0a432022-03-28 11:08:49 -05001047 if (fs::exists(sensorPath))
Chicago Duanbb895cb2021-06-18 19:37:16 +08001048 {
Chris Caine2d0a432022-03-28 11:08:49 -05001049 // Read temperature sensors
1050 readTempSensors(sensorPath, id);
1051
1052 if (occ->isMasterOcc())
1053 {
1054 // Read power sensors
1055 readPowerSensors(sensorPath, id);
1056 }
1057 tracedError[id] = false;
1058 }
1059 else
1060 {
1061 if (!tracedError[id])
1062 {
1063 log<level::ERR>(
1064 fmt::format(
1065 "Manager::getSensorValues: OCC{} sensor path missing: {}",
1066 id, sensorPath.c_str())
1067 .c_str());
1068 tracedError[id] = true;
1069 }
Chicago Duanbb895cb2021-06-18 19:37:16 +08001070 }
1071
1072 return;
1073}
1074#endif
Chris Cain17257672021-10-22 13:41:03 -05001075
1076// Read the altitude from DBus
1077void Manager::readAltitude()
1078{
1079 static bool traceAltitudeErr = true;
1080
1081 utils::PropertyValue altitudeProperty{};
1082 try
1083 {
1084 altitudeProperty = utils::getProperty(ALTITUDE_PATH, ALTITUDE_INTERFACE,
1085 ALTITUDE_PROP);
1086 auto sensorVal = std::get<double>(altitudeProperty);
1087 if (sensorVal < 0xFFFF)
1088 {
1089 if (sensorVal < 0)
1090 {
1091 altitude = 0;
1092 }
1093 else
1094 {
1095 // Round to nearest meter
1096 altitude = uint16_t(sensorVal + 0.5);
1097 }
1098 log<level::DEBUG>(fmt::format("readAltitude: sensor={} ({}m)",
1099 sensorVal, altitude)
1100 .c_str());
1101 traceAltitudeErr = true;
1102 }
1103 else
1104 {
1105 if (traceAltitudeErr)
1106 {
1107 traceAltitudeErr = false;
1108 log<level::DEBUG>(
1109 fmt::format("Invalid altitude value: {}", sensorVal)
1110 .c_str());
1111 }
1112 }
1113 }
1114 catch (const sdbusplus::exception::exception& e)
1115 {
1116 if (traceAltitudeErr)
1117 {
1118 traceAltitudeErr = false;
1119 log<level::INFO>(
1120 fmt::format("Unable to read Altitude: {}", e.what()).c_str());
1121 }
1122 altitude = 0xFFFF; // not available
1123 }
1124}
1125
1126// Callback function when ambient temperature changes
1127void Manager::ambientCallback(sdbusplus::message::message& msg)
1128{
1129 double currentTemp = 0;
1130 uint8_t truncatedTemp = 0xFF;
1131 std::string msgSensor;
1132 std::map<std::string, std::variant<double>> msgData;
1133 msg.read(msgSensor, msgData);
1134
1135 auto valPropMap = msgData.find(AMBIENT_PROP);
1136 if (valPropMap == msgData.end())
1137 {
1138 log<level::DEBUG>("ambientCallback: Unknown ambient property changed");
1139 return;
1140 }
1141 currentTemp = std::get<double>(valPropMap->second);
1142 if (std::isnan(currentTemp))
1143 {
1144 truncatedTemp = 0xFF;
1145 }
1146 else
1147 {
1148 if (currentTemp < 0)
1149 {
1150 truncatedTemp = 0;
1151 }
1152 else
1153 {
1154 // Round to nearest degree C
1155 truncatedTemp = uint8_t(currentTemp + 0.5);
1156 }
1157 }
1158
1159 // If ambient changes, notify OCCs
1160 if (truncatedTemp != ambient)
1161 {
1162 log<level::DEBUG>(
1163 fmt::format("ambientCallback: Ambient change from {} to {}C",
1164 ambient, currentTemp)
1165 .c_str());
1166
1167 ambient = truncatedTemp;
1168 if (altitude == 0xFFFF)
1169 {
1170 // No altitude yet, try reading again
1171 readAltitude();
1172 }
1173
1174 log<level::DEBUG>(
1175 fmt::format("ambientCallback: Ambient: {}C, altitude: {}m", ambient,
1176 altitude)
1177 .c_str());
1178#ifdef POWER10
1179 // Send ambient and altitude to all OCCs
1180 for (auto& obj : statusObjects)
1181 {
1182 if (obj->occActive())
1183 {
1184 obj->sendAmbient(ambient, altitude);
1185 }
1186 }
1187#endif // POWER10
1188 }
1189}
1190
1191// return the current ambient and altitude readings
1192void Manager::getAmbientData(bool& ambientValid, uint8_t& ambientTemp,
1193 uint16_t& altitudeValue) const
1194{
1195 ambientValid = true;
1196 ambientTemp = ambient;
1197 altitudeValue = altitude;
1198
1199 if (ambient == 0xFF)
1200 {
1201 ambientValid = false;
1202 }
1203}
1204
Chris Caina7b74dc2021-11-10 17:03:43 -06001205#ifdef POWER10
Chris Cain7f89e4d2022-05-09 13:27:45 -05001206// Called when waitForAllOccsTimer expires
1207// After the first OCC goes active, this timer will be started (60 seconds)
Chris Caina7b74dc2021-11-10 17:03:43 -06001208void Manager::occsNotAllRunning()
1209{
Chris Caina7b74dc2021-11-10 17:03:43 -06001210 if (activeCount != statusObjects.size())
1211 {
1212 // Not all OCCs went active
1213 log<level::WARNING>(
1214 fmt::format(
1215 "occsNotAllRunning: Active OCC count ({}) does not match expected count ({})",
1216 activeCount, statusObjects.size())
1217 .c_str());
Chris Cain7f89e4d2022-05-09 13:27:45 -05001218 // Procs may be garded, so may be expected
Chris Caina7b74dc2021-11-10 17:03:43 -06001219 }
1220
1221 validateOccMaster();
1222}
1223#endif // POWER10
1224
1225// Verify single master OCC and start presence monitor
1226void Manager::validateOccMaster()
1227{
1228 int masterInstance = -1;
1229 for (auto& obj : statusObjects)
1230 {
Chris Cainbd551de2022-04-26 13:41:16 -05001231 auto instance = obj->getOccInstanceID();
Chris Cainbae4d072022-02-28 09:46:50 -06001232#ifdef POWER10
1233 if (!obj->occActive())
1234 {
1235 if (utils::isHostRunning())
1236 {
Chris Cainbd551de2022-04-26 13:41:16 -05001237 // Check if sensor was queued while waiting for discovery
1238 auto match = queuedActiveState.find(instance);
1239 if (match != queuedActiveState.end())
Chris Cainbae4d072022-02-28 09:46:50 -06001240 {
Chris Cain7f89e4d2022-05-09 13:27:45 -05001241 queuedActiveState.erase(match);
Chris Cainbae4d072022-02-28 09:46:50 -06001242 log<level::INFO>(
1243 fmt::format(
Chris Cainbd551de2022-04-26 13:41:16 -05001244 "validateOccMaster: OCC{} is ACTIVE (queued)",
1245 instance)
Chris Cainbae4d072022-02-28 09:46:50 -06001246 .c_str());
Chris Cainbd551de2022-04-26 13:41:16 -05001247 obj->occActive(true);
1248 }
1249 else
1250 {
1251 // OCC does not appear to be active yet, check active sensor
1252 pldmHandle->checkActiveSensor(instance);
1253 if (obj->occActive())
1254 {
1255 log<level::INFO>(
1256 fmt::format(
1257 "validateOccMaster: OCC{} is ACTIVE after reading sensor",
1258 instance)
1259 .c_str());
1260 }
Chris Cainbae4d072022-02-28 09:46:50 -06001261 }
1262 }
1263 else
1264 {
1265 log<level::WARNING>(
1266 fmt::format(
1267 "validateOccMaster: HOST is not running (OCC{})",
Chris Cainbd551de2022-04-26 13:41:16 -05001268 instance)
Chris Cainbae4d072022-02-28 09:46:50 -06001269 .c_str());
1270 return;
1271 }
1272 }
1273#endif // POWER10
1274
Chris Caina7b74dc2021-11-10 17:03:43 -06001275 if (obj->isMasterOcc())
1276 {
Chris Cain5d66a0a2022-02-09 08:52:10 -06001277 obj->addPresenceWatchMaster();
1278
Chris Caina7b74dc2021-11-10 17:03:43 -06001279 if (masterInstance == -1)
1280 {
Chris Cainbd551de2022-04-26 13:41:16 -05001281 masterInstance = instance;
Chris Caina7b74dc2021-11-10 17:03:43 -06001282 }
1283 else
1284 {
1285 log<level::ERR>(
1286 fmt::format(
1287 "validateOccMaster: Multiple OCC masters! ({} and {})",
Chris Cainbd551de2022-04-26 13:41:16 -05001288 masterInstance, instance)
Chris Caina7b74dc2021-11-10 17:03:43 -06001289 .c_str());
1290 // request reset
Eddie James9789e712022-05-25 15:43:40 -05001291 obj->deviceError(Error::Descriptor(PRESENCE_ERROR_PATH));
Chris Caina7b74dc2021-11-10 17:03:43 -06001292 }
1293 }
1294 }
Chris Cainbae4d072022-02-28 09:46:50 -06001295
Chris Caina7b74dc2021-11-10 17:03:43 -06001296 if (masterInstance < 0)
1297 {
Chris Cainbae4d072022-02-28 09:46:50 -06001298 log<level::ERR>(
1299 fmt::format("validateOccMaster: Master OCC not found! (of {} OCCs)",
1300 statusObjects.size())
1301 .c_str());
Chris Caina7b74dc2021-11-10 17:03:43 -06001302 // request reset
Eddie James9789e712022-05-25 15:43:40 -05001303 statusObjects.front()->deviceError(
1304 Error::Descriptor(PRESENCE_ERROR_PATH));
Chris Caina7b74dc2021-11-10 17:03:43 -06001305 }
1306 else
1307 {
1308 log<level::INFO>(
Chris Cain36f9cde2021-11-22 11:18:21 -06001309 fmt::format("validateOccMaster: OCC{} is master of {} OCCs",
1310 masterInstance, activeCount)
Chris Caina7b74dc2021-11-10 17:03:43 -06001311 .c_str());
1312 }
1313}
1314
Chris Cain40501a22022-03-14 17:33:27 -05001315void Manager::updatePcapBounds() const
1316{
1317 if (pcap)
1318 {
1319 pcap->updatePcapBounds();
1320 }
1321}
1322
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +05301323} // namespace occ
1324} // namespace open_power