blob: 4dc231f67d34ffb46ef8b3b7d007faedd09b97e2 [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
406 setSensorValueToNonFunctional(instance);
407#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 Bailey373af752022-02-21 15:14:00 -0600667 setSensorValueToNonFunctional(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
Matt Spinlera26f1522021-08-25 15:50:20 -0500845 if (faultValue != 0)
Chicago Duanbb895cb2021-06-18 19:37:16 +0800846 {
Chris Cain5d66a0a2022-02-09 08:52:10 -0600847 dbus::OccDBusSensors::getOccDBus().setValue(
Matt Spinlera26f1522021-08-25 15:50:20 -0500848 sensorPath, std::numeric_limits<double>::quiet_NaN());
Chicago Duanbb895cb2021-06-18 19:37:16 +0800849
Chris Cain5d66a0a2022-02-09 08:52:10 -0600850 dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath,
851 false);
Chicago Duanbb895cb2021-06-18 19:37:16 +0800852
Matt Spinlera26f1522021-08-25 15:50:20 -0500853 continue;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800854 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500855
856 double tempValue{0};
857
858 try
Chicago Duanbb895cb2021-06-18 19:37:16 +0800859 {
Matt Spinlera26f1522021-08-25 15:50:20 -0500860 tempValue = readFile<double>(filePathString + inputSuffix);
Chicago Duanbb895cb2021-06-18 19:37:16 +0800861 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500862 catch (const std::system_error& e)
863 {
864 log<level::DEBUG>(
865 fmt::format("readTempSensors: Failed reading {}, errno = {}",
866 filePathString + inputSuffix, e.code().value())
867 .c_str());
Sheldon Baileycd0940b2022-04-26 14:24:05 -0500868
869 // if errno == EAGAIN(Resource temporarily unavailable) then set
870 // temp to 0, to avoid using old temp, and affecting FAN Control.
871 if (e.code().value() == EAGAIN)
872 {
873 tempValue = 0;
874 }
875 // else the errno would be something like
876 // EBADF(Bad file descriptor)
877 // or ENOENT(No such file or directory)
878 else
879 {
880 continue;
881 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500882 }
883
Chris Cain5d66a0a2022-02-09 08:52:10 -0600884 dbus::OccDBusSensors::getOccDBus().setValue(
Matt Spinlera26f1522021-08-25 15:50:20 -0500885 sensorPath, tempValue * std::pow(10, -3));
886
Chris Cain5d66a0a2022-02-09 08:52:10 -0600887 dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath,
888 true);
Matt Spinlera26f1522021-08-25 15:50:20 -0500889
Chris Cain6fa848a2022-01-24 14:54:38 -0600890 // At this point, the sensor will be created for sure.
891 if (existingSensors.find(sensorPath) == existingSensors.end())
892 {
Chris Cain5d66a0a2022-02-09 08:52:10 -0600893 dbus::OccDBusSensors::getOccDBus().setChassisAssociation(
894 sensorPath);
Chris Cain6fa848a2022-01-24 14:54:38 -0600895 }
896
Matt Spinlera26f1522021-08-25 15:50:20 -0500897 existingSensors[sensorPath] = id;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800898 }
899 return;
900}
901
902std::optional<std::string>
903 Manager::getPowerLabelFunctionID(const std::string& value)
904{
905 // If the value is "system", then the FunctionID is "system".
906 if (value == "system")
907 {
908 return value;
909 }
910
911 // If the value is not "system", then the label value have 3 numbers, of
912 // which we only care about the middle one:
913 // <sensor id>_<function id>_<apss channel>
914 // eg: The value is "0_10_5" , then the FunctionID is "10".
915 if (value.find("_") == std::string::npos)
916 {
917 return std::nullopt;
918 }
919
920 auto powerLabelValue = value.substr((value.find("_") + 1));
921
922 if (powerLabelValue.find("_") == std::string::npos)
923 {
924 return std::nullopt;
925 }
926
927 return powerLabelValue.substr(0, powerLabelValue.find("_"));
928}
929
930void Manager::readPowerSensors(const fs::path& path, uint32_t id)
931{
Chicago Duanbb895cb2021-06-18 19:37:16 +0800932 std::regex expr{"power\\d+_label$"}; // Example: power5_label
933 for (auto& file : fs::directory_iterator(path))
934 {
935 if (!std::regex_search(file.path().string(), expr))
936 {
937 continue;
938 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800939
Matt Spinlera26f1522021-08-25 15:50:20 -0500940 std::string labelValue;
941 try
942 {
943 labelValue = readFile<std::string>(file.path());
944 }
945 catch (const std::system_error& e)
946 {
947 log<level::DEBUG>(
948 fmt::format("readPowerSensors: Failed reading {}, errno = {}",
949 file.path().string(), e.code().value())
950 .c_str());
Chicago Duanbb895cb2021-06-18 19:37:16 +0800951 continue;
952 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800953
954 auto functionID = getPowerLabelFunctionID(labelValue);
955 if (functionID == std::nullopt)
956 {
957 continue;
958 }
959
960 const std::string& tempLabel = "label";
961 const std::string filePathString = file.path().string().substr(
962 0, file.path().string().length() - tempLabel.length());
963
964 std::string sensorPath = OCC_SENSORS_ROOT + std::string("/power/");
965
966 auto iter = powerSensorName.find(*functionID);
967 if (iter == powerSensorName.end())
968 {
969 continue;
970 }
971 sensorPath.append(iter->second);
972
Matt Spinlera26f1522021-08-25 15:50:20 -0500973 double tempValue{0};
974
975 try
Chicago Duanbb895cb2021-06-18 19:37:16 +0800976 {
Matt Spinlera26f1522021-08-25 15:50:20 -0500977 tempValue = readFile<double>(filePathString + inputSuffix);
Chicago Duanbb895cb2021-06-18 19:37:16 +0800978 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500979 catch (const std::system_error& e)
Chicago Duanbb895cb2021-06-18 19:37:16 +0800980 {
Chicago Duanbb895cb2021-06-18 19:37:16 +0800981 log<level::DEBUG>(
Chris Cain5d66a0a2022-02-09 08:52:10 -0600982 fmt::format("readPowerSensors: Failed reading {}, errno = {}",
Matt Spinlera26f1522021-08-25 15:50:20 -0500983 filePathString + inputSuffix, e.code().value())
Chicago Duanbb895cb2021-06-18 19:37:16 +0800984 .c_str());
Matt Spinlera26f1522021-08-25 15:50:20 -0500985 continue;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800986 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500987
Chris Cain5d66a0a2022-02-09 08:52:10 -0600988 dbus::OccDBusSensors::getOccDBus().setUnit(
Chris Caind84a8332022-01-13 08:58:45 -0600989 sensorPath, "xyz.openbmc_project.Sensor.Value.Unit.Watts");
990
Chris Cain5d66a0a2022-02-09 08:52:10 -0600991 dbus::OccDBusSensors::getOccDBus().setValue(
Matt Spinlera26f1522021-08-25 15:50:20 -0500992 sensorPath, tempValue * std::pow(10, -3) * std::pow(10, -3));
993
Chris Cain5d66a0a2022-02-09 08:52:10 -0600994 dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath,
995 true);
Matt Spinlera26f1522021-08-25 15:50:20 -0500996
Matt Spinler5901abd2021-09-23 13:50:03 -0500997 if (existingSensors.find(sensorPath) == existingSensors.end())
998 {
Chris Cain5d66a0a2022-02-09 08:52:10 -0600999 dbus::OccDBusSensors::getOccDBus().setChassisAssociation(
1000 sensorPath);
Matt Spinler5901abd2021-09-23 13:50:03 -05001001 }
1002
Matt Spinlera26f1522021-08-25 15:50:20 -05001003 existingSensors[sensorPath] = id;
Chicago Duanbb895cb2021-06-18 19:37:16 +08001004 }
1005 return;
1006}
1007
1008void Manager::setSensorValueToNaN(uint32_t id)
1009{
1010 for (const auto& [sensorPath, occId] : existingSensors)
1011 {
1012 if (occId == id)
1013 {
Chris Cain5d66a0a2022-02-09 08:52:10 -06001014 dbus::OccDBusSensors::getOccDBus().setValue(
Chicago Duanbb895cb2021-06-18 19:37:16 +08001015 sensorPath, std::numeric_limits<double>::quiet_NaN());
1016 }
1017 }
1018 return;
1019}
1020
Sheldon Bailey373af752022-02-21 15:14:00 -06001021void Manager::setSensorValueToNonFunctional(uint32_t id) const
1022{
1023 for (const auto& [sensorPath, occId] : existingSensors)
1024 {
1025 if (occId == id)
1026 {
1027 dbus::OccDBusSensors::getOccDBus().setValue(
1028 sensorPath, std::numeric_limits<double>::quiet_NaN());
1029
1030 dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath,
1031 false);
1032 }
1033 }
1034 return;
1035}
1036
Chris Cain5d66a0a2022-02-09 08:52:10 -06001037void Manager::getSensorValues(std::unique_ptr<Status>& occ)
Chicago Duanbb895cb2021-06-18 19:37:16 +08001038{
Chris Caine2d0a432022-03-28 11:08:49 -05001039 static bool tracedError[8] = {0};
1040 const fs::path sensorPath = occ->getHwmonPath();
Chris Cain5d66a0a2022-02-09 08:52:10 -06001041 const uint32_t id = occ->getOccInstanceID();
Chicago Duanbb895cb2021-06-18 19:37:16 +08001042
Chris Caine2d0a432022-03-28 11:08:49 -05001043 if (fs::exists(sensorPath))
Chicago Duanbb895cb2021-06-18 19:37:16 +08001044 {
Chris Caine2d0a432022-03-28 11:08:49 -05001045 // Read temperature sensors
1046 readTempSensors(sensorPath, id);
1047
1048 if (occ->isMasterOcc())
1049 {
1050 // Read power sensors
1051 readPowerSensors(sensorPath, id);
1052 }
1053 tracedError[id] = false;
1054 }
1055 else
1056 {
1057 if (!tracedError[id])
1058 {
1059 log<level::ERR>(
1060 fmt::format(
1061 "Manager::getSensorValues: OCC{} sensor path missing: {}",
1062 id, sensorPath.c_str())
1063 .c_str());
1064 tracedError[id] = true;
1065 }
Chicago Duanbb895cb2021-06-18 19:37:16 +08001066 }
1067
1068 return;
1069}
1070#endif
Chris Cain17257672021-10-22 13:41:03 -05001071
1072// Read the altitude from DBus
1073void Manager::readAltitude()
1074{
1075 static bool traceAltitudeErr = true;
1076
1077 utils::PropertyValue altitudeProperty{};
1078 try
1079 {
1080 altitudeProperty = utils::getProperty(ALTITUDE_PATH, ALTITUDE_INTERFACE,
1081 ALTITUDE_PROP);
1082 auto sensorVal = std::get<double>(altitudeProperty);
1083 if (sensorVal < 0xFFFF)
1084 {
1085 if (sensorVal < 0)
1086 {
1087 altitude = 0;
1088 }
1089 else
1090 {
1091 // Round to nearest meter
1092 altitude = uint16_t(sensorVal + 0.5);
1093 }
1094 log<level::DEBUG>(fmt::format("readAltitude: sensor={} ({}m)",
1095 sensorVal, altitude)
1096 .c_str());
1097 traceAltitudeErr = true;
1098 }
1099 else
1100 {
1101 if (traceAltitudeErr)
1102 {
1103 traceAltitudeErr = false;
1104 log<level::DEBUG>(
1105 fmt::format("Invalid altitude value: {}", sensorVal)
1106 .c_str());
1107 }
1108 }
1109 }
1110 catch (const sdbusplus::exception::exception& e)
1111 {
1112 if (traceAltitudeErr)
1113 {
1114 traceAltitudeErr = false;
1115 log<level::INFO>(
1116 fmt::format("Unable to read Altitude: {}", e.what()).c_str());
1117 }
1118 altitude = 0xFFFF; // not available
1119 }
1120}
1121
1122// Callback function when ambient temperature changes
1123void Manager::ambientCallback(sdbusplus::message::message& msg)
1124{
1125 double currentTemp = 0;
1126 uint8_t truncatedTemp = 0xFF;
1127 std::string msgSensor;
1128 std::map<std::string, std::variant<double>> msgData;
1129 msg.read(msgSensor, msgData);
1130
1131 auto valPropMap = msgData.find(AMBIENT_PROP);
1132 if (valPropMap == msgData.end())
1133 {
1134 log<level::DEBUG>("ambientCallback: Unknown ambient property changed");
1135 return;
1136 }
1137 currentTemp = std::get<double>(valPropMap->second);
1138 if (std::isnan(currentTemp))
1139 {
1140 truncatedTemp = 0xFF;
1141 }
1142 else
1143 {
1144 if (currentTemp < 0)
1145 {
1146 truncatedTemp = 0;
1147 }
1148 else
1149 {
1150 // Round to nearest degree C
1151 truncatedTemp = uint8_t(currentTemp + 0.5);
1152 }
1153 }
1154
1155 // If ambient changes, notify OCCs
1156 if (truncatedTemp != ambient)
1157 {
1158 log<level::DEBUG>(
1159 fmt::format("ambientCallback: Ambient change from {} to {}C",
1160 ambient, currentTemp)
1161 .c_str());
1162
1163 ambient = truncatedTemp;
1164 if (altitude == 0xFFFF)
1165 {
1166 // No altitude yet, try reading again
1167 readAltitude();
1168 }
1169
1170 log<level::DEBUG>(
1171 fmt::format("ambientCallback: Ambient: {}C, altitude: {}m", ambient,
1172 altitude)
1173 .c_str());
1174#ifdef POWER10
1175 // Send ambient and altitude to all OCCs
1176 for (auto& obj : statusObjects)
1177 {
1178 if (obj->occActive())
1179 {
1180 obj->sendAmbient(ambient, altitude);
1181 }
1182 }
1183#endif // POWER10
1184 }
1185}
1186
1187// return the current ambient and altitude readings
1188void Manager::getAmbientData(bool& ambientValid, uint8_t& ambientTemp,
1189 uint16_t& altitudeValue) const
1190{
1191 ambientValid = true;
1192 ambientTemp = ambient;
1193 altitudeValue = altitude;
1194
1195 if (ambient == 0xFF)
1196 {
1197 ambientValid = false;
1198 }
1199}
1200
Chris Caina7b74dc2021-11-10 17:03:43 -06001201#ifdef POWER10
Chris Cain7f89e4d2022-05-09 13:27:45 -05001202// Called when waitForAllOccsTimer expires
1203// After the first OCC goes active, this timer will be started (60 seconds)
Chris Caina7b74dc2021-11-10 17:03:43 -06001204void Manager::occsNotAllRunning()
1205{
Chris Caina7b74dc2021-11-10 17:03:43 -06001206 if (activeCount != statusObjects.size())
1207 {
1208 // Not all OCCs went active
1209 log<level::WARNING>(
1210 fmt::format(
1211 "occsNotAllRunning: Active OCC count ({}) does not match expected count ({})",
1212 activeCount, statusObjects.size())
1213 .c_str());
Chris Cain7f89e4d2022-05-09 13:27:45 -05001214 // Procs may be garded, so may be expected
Chris Caina7b74dc2021-11-10 17:03:43 -06001215 }
1216
1217 validateOccMaster();
1218}
1219#endif // POWER10
1220
1221// Verify single master OCC and start presence monitor
1222void Manager::validateOccMaster()
1223{
1224 int masterInstance = -1;
1225 for (auto& obj : statusObjects)
1226 {
Chris Cainbd551de2022-04-26 13:41:16 -05001227 auto instance = obj->getOccInstanceID();
Chris Cainbae4d072022-02-28 09:46:50 -06001228#ifdef POWER10
1229 if (!obj->occActive())
1230 {
1231 if (utils::isHostRunning())
1232 {
Chris Cainbd551de2022-04-26 13:41:16 -05001233 // Check if sensor was queued while waiting for discovery
1234 auto match = queuedActiveState.find(instance);
1235 if (match != queuedActiveState.end())
Chris Cainbae4d072022-02-28 09:46:50 -06001236 {
Chris Cain7f89e4d2022-05-09 13:27:45 -05001237 queuedActiveState.erase(match);
Chris Cainbae4d072022-02-28 09:46:50 -06001238 log<level::INFO>(
1239 fmt::format(
Chris Cainbd551de2022-04-26 13:41:16 -05001240 "validateOccMaster: OCC{} is ACTIVE (queued)",
1241 instance)
Chris Cainbae4d072022-02-28 09:46:50 -06001242 .c_str());
Chris Cainbd551de2022-04-26 13:41:16 -05001243 obj->occActive(true);
1244 }
1245 else
1246 {
1247 // OCC does not appear to be active yet, check active sensor
1248 pldmHandle->checkActiveSensor(instance);
1249 if (obj->occActive())
1250 {
1251 log<level::INFO>(
1252 fmt::format(
1253 "validateOccMaster: OCC{} is ACTIVE after reading sensor",
1254 instance)
1255 .c_str());
1256 }
Chris Cainbae4d072022-02-28 09:46:50 -06001257 }
1258 }
1259 else
1260 {
1261 log<level::WARNING>(
1262 fmt::format(
1263 "validateOccMaster: HOST is not running (OCC{})",
Chris Cainbd551de2022-04-26 13:41:16 -05001264 instance)
Chris Cainbae4d072022-02-28 09:46:50 -06001265 .c_str());
1266 return;
1267 }
1268 }
1269#endif // POWER10
1270
Chris Caina7b74dc2021-11-10 17:03:43 -06001271 if (obj->isMasterOcc())
1272 {
Chris Cain5d66a0a2022-02-09 08:52:10 -06001273 obj->addPresenceWatchMaster();
1274
Chris Caina7b74dc2021-11-10 17:03:43 -06001275 if (masterInstance == -1)
1276 {
Chris Cainbd551de2022-04-26 13:41:16 -05001277 masterInstance = instance;
Chris Caina7b74dc2021-11-10 17:03:43 -06001278 }
1279 else
1280 {
1281 log<level::ERR>(
1282 fmt::format(
1283 "validateOccMaster: Multiple OCC masters! ({} and {})",
Chris Cainbd551de2022-04-26 13:41:16 -05001284 masterInstance, instance)
Chris Caina7b74dc2021-11-10 17:03:43 -06001285 .c_str());
1286 // request reset
1287 obj->deviceError();
1288 }
1289 }
1290 }
Chris Cainbae4d072022-02-28 09:46:50 -06001291
Chris Caina7b74dc2021-11-10 17:03:43 -06001292 if (masterInstance < 0)
1293 {
Chris Cainbae4d072022-02-28 09:46:50 -06001294 log<level::ERR>(
1295 fmt::format("validateOccMaster: Master OCC not found! (of {} OCCs)",
1296 statusObjects.size())
1297 .c_str());
Chris Caina7b74dc2021-11-10 17:03:43 -06001298 // request reset
1299 statusObjects.front()->deviceError();
1300 }
1301 else
1302 {
1303 log<level::INFO>(
Chris Cain36f9cde2021-11-22 11:18:21 -06001304 fmt::format("validateOccMaster: OCC{} is master of {} OCCs",
1305 masterInstance, activeCount)
Chris Caina7b74dc2021-11-10 17:03:43 -06001306 .c_str());
1307 }
1308}
1309
Chris Cain40501a22022-03-14 17:33:27 -05001310void Manager::updatePcapBounds() const
1311{
1312 if (pcap)
1313 {
1314 pcap->updatePcapBounds();
1315 }
1316}
1317
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +05301318} // namespace occ
1319} // namespace open_power