blob: 9bd83eaaa08904994596997cb6e57e7e7bf19ac8 [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 Cainc86d80f2023-05-04 15:49:18 -0500117
118 // Find/update the processor path associated with each OCC
119 for (auto& obj : statusObjects)
120 {
121 obj->updateProcAssociation();
122 }
Chris Cainbae4d072022-02-28 09:46:50 -0600123 }
124 }
125
Chris Cain6d8f37a2022-04-29 13:46:01 -0500126 if (statusObjCreated && waitingForAllOccActiveSensors)
Chris Cainbae4d072022-02-28 09:46:50 -0600127 {
128 static bool tracedHostWait = false;
129 if (utils::isHostRunning())
130 {
131 if (tracedHostWait)
132 {
133 log<level::INFO>(
134 "Manager::findAndCreateObjects(): Host is running");
135 tracedHostWait = false;
136 }
Chris Cainbae4d072022-02-28 09:46:50 -0600137 checkAllActiveSensors();
138 }
139 else
140 {
141 if (!tracedHostWait)
142 {
143 log<level::INFO>(
144 "Manager::findAndCreateObjects(): Waiting for host to start");
145 tracedHostWait = true;
146 }
147 discoverTimer->restartOnce(30s);
Chris Cain1718fd82022-02-16 16:39:50 -0600148 }
149 }
Matt Spinlerd267cec2021-09-01 14:49:19 -0500150 }
151 else
152 {
Chris Cain1718fd82022-02-16 16:39:50 -0600153 log<level::INFO>(
154 fmt::format(
155 "Manager::findAndCreateObjects(): Waiting for {} to complete...",
156 HOST_ON_FILE)
157 .c_str());
158 discoverTimer->restartOnce(10s);
Matt Spinlerd267cec2021-09-01 14:49:19 -0500159 }
160#endif
161}
162
Chris Cainbae4d072022-02-28 09:46:50 -0600163#ifdef POWER10
164// Check if all occActive sensors are available
165void Manager::checkAllActiveSensors()
166{
167 static bool allActiveSensorAvailable = false;
168 static bool tracedSensorWait = false;
Chris Cain082a6ca2023-03-21 10:27:26 -0500169 static bool waitingForHost = false;
Chris Cainbae4d072022-02-28 09:46:50 -0600170
Chris Cain082a6ca2023-03-21 10:27:26 -0500171 if (open_power::occ::utils::isHostRunning())
Chris Cainbae4d072022-02-28 09:46:50 -0600172 {
Chris Cain082a6ca2023-03-21 10:27:26 -0500173 if (waitingForHost)
Chris Cainbae4d072022-02-28 09:46:50 -0600174 {
Chris Cain082a6ca2023-03-21 10:27:26 -0500175 waitingForHost = false;
176 log<level::INFO>("checkAllActiveSensors(): Host is now running");
177 }
178
179 // Start with the assumption that all are available
180 allActiveSensorAvailable = true;
181 for (auto& obj : statusObjects)
182 {
183 if ((!obj->occActive()) && (!obj->getPldmSensorReceived()))
Chris Cainbae4d072022-02-28 09:46:50 -0600184 {
Chris Cain7f89e4d2022-05-09 13:27:45 -0500185 auto instance = obj->getOccInstanceID();
186 // Check if sensor was queued while waiting for discovery
187 auto match = queuedActiveState.find(instance);
188 if (match != queuedActiveState.end())
Chris Cainbd551de2022-04-26 13:41:16 -0500189 {
Chris Cain7f89e4d2022-05-09 13:27:45 -0500190 queuedActiveState.erase(match);
Chris Cainbd551de2022-04-26 13:41:16 -0500191 log<level::INFO>(
192 fmt::format(
Chris Cain7f89e4d2022-05-09 13:27:45 -0500193 "checkAllActiveSensors(): OCC{} is ACTIVE (queued)",
Chris Cainbd551de2022-04-26 13:41:16 -0500194 instance)
195 .c_str());
Chris Cain7f89e4d2022-05-09 13:27:45 -0500196 obj->occActive(true);
Chris Cainbd551de2022-04-26 13:41:16 -0500197 }
Chris Cain7f89e4d2022-05-09 13:27:45 -0500198 else
199 {
200 allActiveSensorAvailable = false;
201 if (!tracedSensorWait)
202 {
203 log<level::INFO>(
204 fmt::format(
205 "checkAllActiveSensors(): Waiting on OCC{} Active sensor",
206 instance)
207 .c_str());
208 tracedSensorWait = true;
209 }
210 pldmHandle->checkActiveSensor(obj->getOccInstanceID());
211 break;
212 }
Chris Cainbd551de2022-04-26 13:41:16 -0500213 }
Chris Cainbae4d072022-02-28 09:46:50 -0600214 }
215 }
Chris Cain082a6ca2023-03-21 10:27:26 -0500216 else
217 {
218 if (!waitingForHost)
219 {
220 waitingForHost = true;
221 log<level::INFO>(
222 "checkAllActiveSensors(): Waiting for host to start");
223 }
224 }
Chris Cainbae4d072022-02-28 09:46:50 -0600225
226 if (allActiveSensorAvailable)
227 {
228 // All sensors were found, disable the discovery timer
Chris Cain7f89e4d2022-05-09 13:27:45 -0500229 if (discoverTimer->isEnabled())
230 {
Chris Cainf55f91a2022-05-27 13:40:15 -0500231 discoverTimer->setEnabled(false);
Chris Cain7f89e4d2022-05-09 13:27:45 -0500232 }
Chris Cainbae4d072022-02-28 09:46:50 -0600233
Chris Cain7f89e4d2022-05-09 13:27:45 -0500234 if (waitingForAllOccActiveSensors)
235 {
236 log<level::INFO>(
237 "checkAllActiveSensors(): OCC Active sensors are available");
238 waitingForAllOccActiveSensors = false;
239 }
240 queuedActiveState.clear();
Chris Cainbae4d072022-02-28 09:46:50 -0600241 tracedSensorWait = false;
242 }
243 else
244 {
245 // Not all sensors were available, so keep waiting
246 if (!tracedSensorWait)
247 {
248 log<level::INFO>(
Chris Cainbd551de2022-04-26 13:41:16 -0500249 "checkAllActiveSensors(): Waiting for OCC Active sensors to become available");
Chris Cainbae4d072022-02-28 09:46:50 -0600250 tracedSensorWait = true;
251 }
Chris Cainf55f91a2022-05-27 13:40:15 -0500252 discoverTimer->restartOnce(10s);
Chris Cainbae4d072022-02-28 09:46:50 -0600253 }
254}
255#endif
256
Matt Spinlerd267cec2021-09-01 14:49:19 -0500257std::vector<int> Manager::findOCCsInDev()
258{
259 std::vector<int> occs;
260 std::regex expr{R"(occ(\d+)$)"};
261
262 for (auto& file : fs::directory_iterator("/dev"))
263 {
264 std::smatch match;
265 std::string path{file.path().string()};
266 if (std::regex_search(path, match, expr))
267 {
268 auto num = std::stoi(match[1].str());
269
270 // /dev numbering starts at 1, ours starts at 0.
271 occs.push_back(num - 1);
272 }
273 }
274
275 return occs;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530276}
277
Patrick Williamsaf408082022-07-22 19:26:54 -0500278int Manager::cpuCreated(sdbusplus::message_t& msg)
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530279{
George Liubcef3b42021-09-10 12:39:02 +0800280 namespace fs = std::filesystem;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530281
282 sdbusplus::message::object_path o;
283 msg.read(o);
284 fs::path cpuPath(std::string(std::move(o)));
285
286 auto name = cpuPath.filename().string();
287 auto index = name.find(CPU_NAME);
288 name.replace(index, std::strlen(CPU_NAME), OCC_NAME);
289
290 createObjects(name);
291
292 return 0;
293}
294
295void Manager::createObjects(const std::string& occ)
296{
297 auto path = fs::path(OCC_CONTROL_ROOT) / occ;
298
Gunnar Mills94df8c92018-09-14 14:50:03 -0500299 statusObjects.emplace_back(std::make_unique<Status>(
George Liuf3b75142021-06-10 11:22:50 +0800300 event, path.c_str(), *this,
Chris Cain36f9cde2021-11-22 11:18:21 -0600301#ifdef POWER10
302 pmode,
303#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -0500304 std::bind(std::mem_fn(&Manager::statusCallBack), this,
Sheldon Bailey373af752022-02-21 15:14:00 -0600305 std::placeholders::_1, std::placeholders::_2)
Tom Joseph00325232020-07-29 17:51:48 +0530306#ifdef PLDM
307 ,
308 std::bind(std::mem_fn(&pldm::Interface::resetOCC), pldmHandle.get(),
309 std::placeholders::_1)
310#endif
311 ));
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530312
Chris Cain40501a22022-03-14 17:33:27 -0500313 // Create the power cap monitor object
314 if (!pcap)
315 {
316 pcap = std::make_unique<open_power::occ::powercap::PowerCap>(
317 *statusObjects.back());
318 }
319
Chris Cain36f9cde2021-11-22 11:18:21 -0600320 if (statusObjects.back()->isMasterOcc())
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530321 {
Chris Cain36f9cde2021-11-22 11:18:21 -0600322 log<level::INFO>(
323 fmt::format("Manager::createObjects(): OCC{} is the master",
324 statusObjects.back()->getOccInstanceID())
325 .c_str());
326 _pollTimer->setEnabled(false);
327
Chris Cain78e86012021-03-04 16:15:31 -0600328#ifdef POWER10
Chris Cain6fa848a2022-01-24 14:54:38 -0600329 // Set the master OCC on the PowerMode object
330 pmode->setMasterOcc(path);
Chris Cain78e86012021-03-04 16:15:31 -0600331#endif
Chris Cain36f9cde2021-11-22 11:18:21 -0600332 }
333
334 passThroughObjects.emplace_back(std::make_unique<PassThrough>(path.c_str()
335#ifdef POWER10
336 ,
337 pmode
338#endif
339 ));
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530340}
341
Sheldon Bailey373af752022-02-21 15:14:00 -0600342void Manager::statusCallBack(instanceID instance, bool status)
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530343{
Chris Caina7b74dc2021-11-10 17:03:43 -0600344 if (status == true)
Eddie Jamesdae2d942017-12-20 10:50:03 -0600345 {
Chris Caina7b74dc2021-11-10 17:03:43 -0600346 // OCC went active
347 ++activeCount;
348
349#ifdef POWER10
350 if (activeCount == 1)
Eddie Jamesdae2d942017-12-20 10:50:03 -0600351 {
Chris Caina7b74dc2021-11-10 17:03:43 -0600352 // First OCC went active (allow some time for all OCCs to go active)
Chris Cainbd551de2022-04-26 13:41:16 -0500353 waitForAllOccsTimer->restartOnce(60s);
Matt Spinler53f68142021-08-25 15:47:31 -0500354 }
355#endif
Chris Caina7b74dc2021-11-10 17:03:43 -0600356
357 if (activeCount == statusObjects.size())
358 {
359#ifdef POWER10
360 // All OCCs are now running
361 if (waitForAllOccsTimer->isEnabled())
362 {
363 // stop occ wait timer
364 waitForAllOccsTimer->setEnabled(false);
365 }
366#endif
367
368 // Verify master OCC and start presence monitor
369 validateOccMaster();
370 }
371
372 // Start poll timer if not already started
373 if (!_pollTimer->isEnabled())
374 {
375 log<level::INFO>(
Chris Cain36f9cde2021-11-22 11:18:21 -0600376 fmt::format("Manager: OCCs will be polled every {} seconds",
377 pollInterval)
Chris Caina7b74dc2021-11-10 17:03:43 -0600378 .c_str());
379
380 // Send poll and start OCC poll timer
381 pollerTimerExpired();
382 }
383 }
384 else
385 {
386 // OCC went away
Chris Cain082a6ca2023-03-21 10:27:26 -0500387 if (activeCount > 0)
388 {
389 --activeCount;
390 }
391 else
392 {
393 log<level::ERR>(
394 fmt::format("OCC{} disabled, but currently no active OCCs",
395 instance)
396 .c_str());
397 }
Chris Caina7b74dc2021-11-10 17:03:43 -0600398
399 if (activeCount == 0)
400 {
401 // No OCCs are running
402
403 // Stop OCC poll timer
404 if (_pollTimer->isEnabled())
405 {
406 log<level::INFO>(
407 "Manager::statusCallBack(): OCCs are not running, stopping poll timer");
408 _pollTimer->setEnabled(false);
409 }
410
411#ifdef POWER10
412 // stop wait timer
413 if (waitForAllOccsTimer->isEnabled())
414 {
415 waitForAllOccsTimer->setEnabled(false);
416 }
417#endif
Chris Caina7b74dc2021-11-10 17:03:43 -0600418 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600419#ifdef READ_OCC_SENSORS
420 // Clear OCC sensors
Sheldon Baileyc8dd4592022-05-12 10:15:14 -0500421 setSensorValueToNaN(instance);
Sheldon Bailey373af752022-02-21 15:14:00 -0600422#endif
Chris Caina8857c52021-01-27 11:53:05 -0600423 }
Chris Cainbae4d072022-02-28 09:46:50 -0600424
425#ifdef POWER10
426 if (waitingForAllOccActiveSensors)
427 {
Chris Cain6d8f37a2022-04-29 13:46:01 -0500428 if (utils::isHostRunning())
429 {
430 checkAllActiveSensors();
431 }
Chris Cainbae4d072022-02-28 09:46:50 -0600432 }
433#endif
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530434}
435
436#ifdef I2C_OCC
437void Manager::initStatusObjects()
438{
439 // Make sure we have a valid path string
440 static_assert(sizeof(DEV_PATH) != 0);
441
442 auto deviceNames = i2c_occ::getOccHwmonDevices(DEV_PATH);
443 for (auto& name : deviceNames)
444 {
445 i2c_occ::i2cToDbus(name);
Lei YUb5259a12017-09-01 16:22:40 +0800446 name = std::string(OCC_NAME) + '_' + name;
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530447 auto path = fs::path(OCC_CONTROL_ROOT) / name;
448 statusObjects.emplace_back(
George Liuf3b75142021-06-10 11:22:50 +0800449 std::make_unique<Status>(event, path.c_str(), *this));
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530450 }
Chris Cain40501a22022-03-14 17:33:27 -0500451 // The first device is master occ
452 pcap = std::make_unique<open_power::occ::powercap::PowerCap>(
453 *statusObjects.front());
Chris Cain78e86012021-03-04 16:15:31 -0600454#ifdef POWER10
Chris Cain5d66a0a2022-02-09 08:52:10 -0600455 pmode = std::make_unique<powermode::PowerMode>(*this, powermode::PMODE_PATH,
456 powermode::PIPS_PATH);
Chris Cain6fa848a2022-01-24 14:54:38 -0600457 // Set the master OCC on the PowerMode object
458 pmode->setMasterOcc(path);
Chris Cain78e86012021-03-04 16:15:31 -0600459#endif
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +0530460}
461#endif
462
Tom Joseph815f9f52020-07-27 12:12:13 +0530463#ifdef PLDM
Eddie Jamescbad2192021-10-07 09:39:39 -0500464void Manager::sbeTimeout(unsigned int instance)
465{
Eddie James2a751d72022-03-04 09:16:12 -0600466 auto obj = std::find_if(statusObjects.begin(), statusObjects.end(),
467 [instance](const auto& obj) {
Patrick Williamsa49c9872023-05-10 07:50:35 -0500468 return instance == obj->getOccInstanceID();
469 });
Eddie Jamescbad2192021-10-07 09:39:39 -0500470
Eddie Jamescb018da2022-03-05 11:49:37 -0600471 if (obj != statusObjects.end() && (*obj)->occActive())
Eddie James2a751d72022-03-04 09:16:12 -0600472 {
Chris Cainbae4d072022-02-28 09:46:50 -0600473 log<level::INFO>(
474 fmt::format("SBE timeout, requesting HRESET (OCC{})", instance)
475 .c_str());
Eddie Jamescbad2192021-10-07 09:39:39 -0500476
Eddie James2a751d72022-03-04 09:16:12 -0600477 setSBEState(instance, SBE_STATE_NOT_USABLE);
478
479 pldmHandle->sendHRESET(instance);
480 }
Eddie Jamescbad2192021-10-07 09:39:39 -0500481}
482
Tom Joseph815f9f52020-07-27 12:12:13 +0530483bool Manager::updateOCCActive(instanceID instance, bool status)
484{
Chris Cain7e374fb2022-04-07 09:47:23 -0500485 auto obj = std::find_if(statusObjects.begin(), statusObjects.end(),
486 [instance](const auto& obj) {
Patrick Williamsa49c9872023-05-10 07:50:35 -0500487 return instance == obj->getOccInstanceID();
488 });
Chris Cain7e374fb2022-04-07 09:47:23 -0500489
Chris Cain082a6ca2023-03-21 10:27:26 -0500490 const bool hostRunning = open_power::occ::utils::isHostRunning();
Chris Cain7e374fb2022-04-07 09:47:23 -0500491 if (obj != statusObjects.end())
492 {
Chris Cain082a6ca2023-03-21 10:27:26 -0500493 if (!hostRunning && (status == true))
494 {
495 log<level::WARNING>(
496 fmt::format(
497 "updateOCCActive: Host is not running yet (OCC{} active={}), clearing sensor received",
498 instance, status)
499 .c_str());
500 (*obj)->setPldmSensorReceived(false);
501 if (!waitingForAllOccActiveSensors)
502 {
503 log<level::INFO>(
504 "updateOCCActive: Waiting for Host and all OCC Active Sensors");
505 waitingForAllOccActiveSensors = true;
506 }
507 discoverTimer->restartOnce(30s);
508 return false;
509 }
510 else
511 {
512 log<level::INFO>(fmt::format("updateOCCActive: OCC{} active={}",
513 instance, status)
514 .c_str());
515 (*obj)->setPldmSensorReceived(true);
516 return (*obj)->occActive(status);
517 }
Chris Cain7e374fb2022-04-07 09:47:23 -0500518 }
519 else
520 {
Chris Cain082a6ca2023-03-21 10:27:26 -0500521 if (hostRunning)
522 {
523 log<level::WARNING>(
524 fmt::format(
525 "updateOCCActive: No status object to update for OCC{} (active={})",
526 instance, status)
527 .c_str());
528 }
529 else
530 {
531 if (status == true)
532 {
533 log<level::WARNING>(
534 fmt::format(
535 "updateOCCActive: No status objects and Host is not running yet (OCC{} active={})",
536 instance, status)
537 .c_str());
538 }
539 }
Chris Cainbd551de2022-04-26 13:41:16 -0500540 if (status == true)
541 {
542 // OCC went active
543 queuedActiveState.insert(instance);
544 }
545 else
546 {
547 auto match = queuedActiveState.find(instance);
548 if (match != queuedActiveState.end())
549 {
550 // OCC was disabled
551 queuedActiveState.erase(match);
552 }
553 }
Chris Cain7e374fb2022-04-07 09:47:23 -0500554 return false;
555 }
Tom Joseph815f9f52020-07-27 12:12:13 +0530556}
Eddie Jamescbad2192021-10-07 09:39:39 -0500557
Sheldon Bailey31a2f132022-05-20 11:31:52 -0500558// Called upon pldm event To set powermode Safe Mode State for system.
559void Manager::updateOccSafeMode(bool safeMode)
560{
561#ifdef POWER10
562 pmode->updateDbusSafeMode(safeMode);
563#endif
Chris Cainc86d80f2023-05-04 15:49:18 -0500564 // Update the processor throttle status on dbus
565 for (auto& obj : statusObjects)
566 {
567 obj->updateThrottle(safeMode, THROTTLED_SAFE);
568 }
Sheldon Bailey31a2f132022-05-20 11:31:52 -0500569}
570
Eddie Jamescbad2192021-10-07 09:39:39 -0500571void Manager::sbeHRESETResult(instanceID instance, bool success)
572{
573 if (success)
574 {
Chris Cainbae4d072022-02-28 09:46:50 -0600575 log<level::INFO>(
576 fmt::format("HRESET succeeded (OCC{})", instance).c_str());
Eddie Jamescbad2192021-10-07 09:39:39 -0500577
578 setSBEState(instance, SBE_STATE_BOOTED);
579
580 return;
581 }
582
583 setSBEState(instance, SBE_STATE_FAILED);
584
585 if (sbeCanDump(instance))
586 {
Chris Cainbae4d072022-02-28 09:46:50 -0600587 log<level::INFO>(
588 fmt::format("HRESET failed (OCC{}), triggering SBE dump", instance)
589 .c_str());
Eddie Jamescbad2192021-10-07 09:39:39 -0500590
591 auto& bus = utils::getBus();
592 uint32_t src6 = instance << 16;
593 uint32_t logId =
594 FFDC::createPEL("org.open_power.Processor.Error.SbeChipOpTimeout",
595 src6, "SBE command timeout");
596
597 try
598 {
George Liuf3a4a692021-12-28 13:59:51 +0800599 constexpr auto path = "/org/openpower/dump";
600 constexpr auto interface = "xyz.openbmc_project.Dump.Create";
601 constexpr auto function = "CreateDump";
602
Eddie Jamescbad2192021-10-07 09:39:39 -0500603 std::string service = utils::getService(path, interface);
Patrick Williamsa49c9872023-05-10 07:50:35 -0500604 auto method = bus.new_method_call(service.c_str(), path, interface,
605 function);
Eddie Jamescbad2192021-10-07 09:39:39 -0500606
607 std::map<std::string, std::variant<std::string, uint64_t>>
608 createParams{
609 {"com.ibm.Dump.Create.CreateParameters.ErrorLogId",
610 uint64_t(logId)},
611 {"com.ibm.Dump.Create.CreateParameters.DumpType",
612 "com.ibm.Dump.Create.DumpType.SBE"},
613 {"com.ibm.Dump.Create.CreateParameters.FailingUnitId",
614 uint64_t(instance)},
615 };
616
617 method.append(createParams);
618
619 auto response = bus.call(method);
620 }
Patrick Williamsaf408082022-07-22 19:26:54 -0500621 catch (const sdbusplus::exception_t& e)
Eddie Jamescbad2192021-10-07 09:39:39 -0500622 {
623 constexpr auto ERROR_DUMP_DISABLED =
624 "xyz.openbmc_project.Dump.Create.Error.Disabled";
625 if (e.name() == ERROR_DUMP_DISABLED)
626 {
627 log<level::INFO>("Dump is disabled, skipping");
628 }
629 else
630 {
631 log<level::ERR>("Dump failed");
632 }
633 }
634 }
635}
636
637bool Manager::sbeCanDump(unsigned int instance)
638{
639 struct pdbg_target* proc = getPdbgTarget(instance);
640
641 if (!proc)
642 {
643 // allow the dump in the error case
644 return true;
645 }
646
647 try
648 {
649 if (!openpower::phal::sbe::isDumpAllowed(proc))
650 {
651 return false;
652 }
653
654 if (openpower::phal::pdbg::isSbeVitalAttnActive(proc))
655 {
656 return false;
657 }
658 }
659 catch (openpower::phal::exception::SbeError& e)
660 {
661 log<level::INFO>("Failed to query SBE state");
662 }
663
664 // allow the dump in the error case
665 return true;
666}
667
668void Manager::setSBEState(unsigned int instance, enum sbe_state state)
669{
670 struct pdbg_target* proc = getPdbgTarget(instance);
671
672 if (!proc)
673 {
674 return;
675 }
676
677 try
678 {
679 openpower::phal::sbe::setState(proc, state);
680 }
681 catch (const openpower::phal::exception::SbeError& e)
682 {
683 log<level::ERR>("Failed to set SBE state");
684 }
685}
686
687struct pdbg_target* Manager::getPdbgTarget(unsigned int instance)
688{
689 if (!pdbgInitialized)
690 {
691 try
692 {
693 openpower::phal::pdbg::init();
694 pdbgInitialized = true;
695 }
696 catch (const openpower::phal::exception::PdbgError& e)
697 {
698 log<level::ERR>("pdbg initialization failed");
699 return nullptr;
700 }
701 }
702
703 struct pdbg_target* proc = nullptr;
704 pdbg_for_each_class_target("proc", proc)
705 {
706 if (pdbg_target_index(proc) == instance)
707 {
708 return proc;
709 }
710 }
711
712 log<level::ERR>("Failed to get pdbg target");
713 return nullptr;
714}
Tom Joseph815f9f52020-07-27 12:12:13 +0530715#endif
716
Chris Caina8857c52021-01-27 11:53:05 -0600717void Manager::pollerTimerExpired()
718{
Chris Caina8857c52021-01-27 11:53:05 -0600719 if (!_pollTimer)
720 {
721 log<level::ERR>(
722 "Manager::pollerTimerExpired() ERROR: Timer not defined");
723 return;
724 }
725
726 for (auto& obj : statusObjects)
727 {
Chris Caina7b74dc2021-11-10 17:03:43 -0600728 if (!obj->occActive())
729 {
730 // OCC is not running yet
731#ifdef READ_OCC_SENSORS
Chris Cain5d66a0a2022-02-09 08:52:10 -0600732 auto id = obj->getOccInstanceID();
Sheldon Baileyc8dd4592022-05-12 10:15:14 -0500733 setSensorValueToNaN(id);
Chris Caina7b74dc2021-11-10 17:03:43 -0600734#endif
735 continue;
736 }
737
Chris Caina8857c52021-01-27 11:53:05 -0600738 // Read sysfs to force kernel to poll OCC
739 obj->readOccState();
Chicago Duanbb895cb2021-06-18 19:37:16 +0800740
741#ifdef READ_OCC_SENSORS
742 // Read occ sensor values
Chris Cain5d66a0a2022-02-09 08:52:10 -0600743 getSensorValues(obj);
Chicago Duanbb895cb2021-06-18 19:37:16 +0800744#endif
Chris Caina8857c52021-01-27 11:53:05 -0600745 }
746
Chris Caina7b74dc2021-11-10 17:03:43 -0600747 if (activeCount > 0)
748 {
749 // Restart OCC poll timer
750 _pollTimer->restartOnce(std::chrono::seconds(pollInterval));
751 }
752 else
753 {
754 // No OCCs running, so poll timer will not be restarted
755 log<level::INFO>(
756 fmt::format(
757 "Manager::pollerTimerExpired: poll timer will not be restarted")
758 .c_str());
759 }
Chris Caina8857c52021-01-27 11:53:05 -0600760}
761
Chicago Duanbb895cb2021-06-18 19:37:16 +0800762#ifdef READ_OCC_SENSORS
763void Manager::readTempSensors(const fs::path& path, uint32_t id)
764{
Matt Spinler818cc8d2023-10-23 11:43:39 -0500765 // There may be more than one sensor with the same FRU type
766 // and label so make two passes: the first to read the temps
767 // from sysfs, and the second to put them on D-Bus after
768 // resolving any conflicts.
769 std::map<std::string, double> sensorData;
770
Chicago Duanbb895cb2021-06-18 19:37:16 +0800771 std::regex expr{"temp\\d+_label$"}; // Example: temp5_label
772 for (auto& file : fs::directory_iterator(path))
773 {
774 if (!std::regex_search(file.path().string(), expr))
775 {
776 continue;
777 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800778
Matt Spinlera26f1522021-08-25 15:50:20 -0500779 uint32_t labelValue{0};
780
781 try
782 {
783 labelValue = readFile<uint32_t>(file.path());
784 }
785 catch (const std::system_error& e)
786 {
787 log<level::DEBUG>(
788 fmt::format("readTempSensors: Failed reading {}, errno = {}",
789 file.path().string(), e.code().value())
790 .c_str());
Chicago Duanbb895cb2021-06-18 19:37:16 +0800791 continue;
792 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800793
794 const std::string& tempLabel = "label";
795 const std::string filePathString = file.path().string().substr(
796 0, file.path().string().length() - tempLabel.length());
Matt Spinlera26f1522021-08-25 15:50:20 -0500797
798 uint32_t fruTypeValue{0};
799 try
Chicago Duanbb895cb2021-06-18 19:37:16 +0800800 {
Matt Spinlera26f1522021-08-25 15:50:20 -0500801 fruTypeValue = readFile<uint32_t>(filePathString + fruTypeSuffix);
802 }
803 catch (const std::system_error& e)
804 {
Chicago Duanbb895cb2021-06-18 19:37:16 +0800805 log<level::DEBUG>(
Matt Spinlera26f1522021-08-25 15:50:20 -0500806 fmt::format("readTempSensors: Failed reading {}, errno = {}",
807 filePathString + fruTypeSuffix, e.code().value())
Chicago Duanbb895cb2021-06-18 19:37:16 +0800808 .c_str());
809 continue;
810 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800811
Patrick Williamsa49c9872023-05-10 07:50:35 -0500812 std::string sensorPath = OCC_SENSORS_ROOT +
813 std::string("/temperature/");
Chicago Duanbb895cb2021-06-18 19:37:16 +0800814
Matt Spinlerace67d82021-10-18 13:41:57 -0500815 std::string dvfsTempPath;
816
Chicago Duanbb895cb2021-06-18 19:37:16 +0800817 if (fruTypeValue == VRMVdd)
818 {
819 sensorPath.append("vrm_vdd" + std::to_string(id) + "_temp");
820 }
Matt Spinlerace67d82021-10-18 13:41:57 -0500821 else if (fruTypeValue == processorIoRing)
822 {
823 sensorPath.append("proc" + std::to_string(id) + "_ioring_temp");
824 dvfsTempPath = std::string{OCC_SENSORS_ROOT} + "/temperature/proc" +
825 std::to_string(id) + "_ioring_dvfs_temp";
826 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800827 else
828 {
Matt Spinler14d14022021-08-25 15:38:29 -0500829 uint16_t type = (labelValue & 0xFF000000) >> 24;
830 uint16_t instanceID = labelValue & 0x0000FFFF;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800831
832 if (type == OCC_DIMM_TEMP_SENSOR_TYPE)
833 {
Matt Spinler8b8abee2021-08-25 15:18:21 -0500834 if (fruTypeValue == fruTypeNotAvailable)
835 {
836 // Not all DIMM related temps are available to read
837 // (no _input file in this case)
838 continue;
839 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800840 auto iter = dimmTempSensorName.find(fruTypeValue);
841 if (iter == dimmTempSensorName.end())
842 {
George Liub5ca1012021-09-10 12:53:11 +0800843 log<level::ERR>(
844 fmt::format(
845 "readTempSensors: Fru type error! fruTypeValue = {}) ",
846 fruTypeValue)
847 .c_str());
Chicago Duanbb895cb2021-06-18 19:37:16 +0800848 continue;
849 }
850
851 sensorPath.append("dimm" + std::to_string(instanceID) +
852 iter->second);
Matt Spinlerad8f4522023-10-25 11:14:46 -0500853
854 dvfsTempPath = std::string{OCC_SENSORS_ROOT} + "/temperature/" +
855 dimmDVFSSensorName.at(fruTypeValue);
Chicago Duanbb895cb2021-06-18 19:37:16 +0800856 }
857 else if (type == OCC_CPU_TEMP_SENSOR_TYPE)
858 {
Matt Spinlerace67d82021-10-18 13:41:57 -0500859 if (fruTypeValue == processorCore)
Chicago Duanbb895cb2021-06-18 19:37:16 +0800860 {
Matt Spinlerace67d82021-10-18 13:41:57 -0500861 // The OCC reports small core temps, of which there are
862 // two per big core. All current P10 systems are in big
863 // core mode, so use a big core name.
864 uint16_t coreNum = instanceID / 2;
865 uint16_t tempNum = instanceID % 2;
866 sensorPath.append("proc" + std::to_string(id) + "_core" +
867 std::to_string(coreNum) + "_" +
868 std::to_string(tempNum) + "_temp");
869
870 dvfsTempPath = std::string{OCC_SENSORS_ROOT} +
871 "/temperature/proc" + std::to_string(id) +
872 "_core_dvfs_temp";
873 }
874 else
875 {
Chicago Duanbb895cb2021-06-18 19:37:16 +0800876 continue;
877 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800878 }
879 else
880 {
881 continue;
882 }
883 }
884
Matt Spinlerace67d82021-10-18 13:41:57 -0500885 // The dvfs temp file only needs to be read once per chip per type.
886 if (!dvfsTempPath.empty() &&
887 !dbus::OccDBusSensors::getOccDBus().hasDvfsTemp(dvfsTempPath))
888 {
889 try
890 {
891 auto dvfsValue = readFile<double>(filePathString + maxSuffix);
892
893 dbus::OccDBusSensors::getOccDBus().setDvfsTemp(
894 dvfsTempPath, dvfsValue * std::pow(10, -3));
895 }
896 catch (const std::system_error& e)
897 {
898 log<level::DEBUG>(
899 fmt::format(
900 "readTempSensors: Failed reading {}, errno = {}",
901 filePathString + maxSuffix, e.code().value())
902 .c_str());
903 }
904 }
905
Matt Spinlera26f1522021-08-25 15:50:20 -0500906 uint32_t faultValue{0};
907 try
Chicago Duanbb895cb2021-06-18 19:37:16 +0800908 {
Matt Spinlera26f1522021-08-25 15:50:20 -0500909 faultValue = readFile<uint32_t>(filePathString + faultSuffix);
910 }
911 catch (const std::system_error& e)
912 {
913 log<level::DEBUG>(
914 fmt::format("readTempSensors: Failed reading {}, errno = {}",
915 filePathString + faultSuffix, e.code().value())
916 .c_str());
917 continue;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800918 }
919
Sheldon Baileyc8dd4592022-05-12 10:15:14 -0500920 // NOTE: if OCC sends back 0xFF kernal sets this fault value to 1.
Matt Spinlera26f1522021-08-25 15:50:20 -0500921 if (faultValue != 0)
Chicago Duanbb895cb2021-06-18 19:37:16 +0800922 {
Matt Spinler818cc8d2023-10-23 11:43:39 -0500923 // For cases when there are multiple readings per fru type/label,
924 // don't overwrite a good value with an NaN.
925 if (!sensorData.contains(sensorPath))
926 {
927 sensorData[sensorPath] =
928 std::numeric_limits<double>::quiet_NaN();
929 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500930 continue;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800931 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500932
933 double tempValue{0};
934
935 try
Chicago Duanbb895cb2021-06-18 19:37:16 +0800936 {
Matt Spinlera26f1522021-08-25 15:50:20 -0500937 tempValue = readFile<double>(filePathString + inputSuffix);
Chicago Duanbb895cb2021-06-18 19:37:16 +0800938 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500939 catch (const std::system_error& e)
940 {
941 log<level::DEBUG>(
942 fmt::format("readTempSensors: Failed reading {}, errno = {}",
943 filePathString + inputSuffix, e.code().value())
944 .c_str());
Sheldon Baileycd0940b2022-04-26 14:24:05 -0500945
946 // if errno == EAGAIN(Resource temporarily unavailable) then set
947 // temp to 0, to avoid using old temp, and affecting FAN Control.
948 if (e.code().value() == EAGAIN)
949 {
950 tempValue = 0;
951 }
952 // else the errno would be something like
953 // EBADF(Bad file descriptor)
954 // or ENOENT(No such file or directory)
955 else
956 {
957 continue;
958 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500959 }
960
Matt Spinler818cc8d2023-10-23 11:43:39 -0500961 // If this object path already has a value, only overwite
962 // it if the previous one was an NaN or a smaller value.
963 auto existing = sensorData.find(sensorPath);
964 if (existing != sensorData.end())
965 {
966 if (std::isnan(existing->second) || (tempValue > existing->second))
967 {
968 existing->second = tempValue;
969 }
970 }
971 else
972 {
973 sensorData[sensorPath] = tempValue;
974 }
975 }
Matt Spinlera26f1522021-08-25 15:50:20 -0500976
Matt Spinler818cc8d2023-10-23 11:43:39 -0500977 // Now publish the values on D-Bus.
978 for (const auto& [objectPath, value] : sensorData)
979 {
980 dbus::OccDBusSensors::getOccDBus().setValue(objectPath,
981 value * std::pow(10, -3));
Matt Spinlera26f1522021-08-25 15:50:20 -0500982
Matt Spinler818cc8d2023-10-23 11:43:39 -0500983 dbus::OccDBusSensors::getOccDBus().setOperationalStatus(
984 objectPath, !std::isnan(value));
985
986 if (existingSensors.find(objectPath) == existingSensors.end())
Chris Cain6fa848a2022-01-24 14:54:38 -0600987 {
Chris Cain5d66a0a2022-02-09 08:52:10 -0600988 dbus::OccDBusSensors::getOccDBus().setChassisAssociation(
Matt Spinler818cc8d2023-10-23 11:43:39 -0500989 objectPath);
Chris Cain6fa848a2022-01-24 14:54:38 -0600990 }
991
Matt Spinler818cc8d2023-10-23 11:43:39 -0500992 existingSensors[objectPath] = id;
Chicago Duanbb895cb2021-06-18 19:37:16 +0800993 }
Chicago Duanbb895cb2021-06-18 19:37:16 +0800994}
995
996std::optional<std::string>
997 Manager::getPowerLabelFunctionID(const std::string& value)
998{
999 // If the value is "system", then the FunctionID is "system".
1000 if (value == "system")
1001 {
1002 return value;
1003 }
1004
1005 // If the value is not "system", then the label value have 3 numbers, of
1006 // which we only care about the middle one:
1007 // <sensor id>_<function id>_<apss channel>
1008 // eg: The value is "0_10_5" , then the FunctionID is "10".
1009 if (value.find("_") == std::string::npos)
1010 {
1011 return std::nullopt;
1012 }
1013
1014 auto powerLabelValue = value.substr((value.find("_") + 1));
1015
1016 if (powerLabelValue.find("_") == std::string::npos)
1017 {
1018 return std::nullopt;
1019 }
1020
1021 return powerLabelValue.substr(0, powerLabelValue.find("_"));
1022}
1023
1024void Manager::readPowerSensors(const fs::path& path, uint32_t id)
1025{
Chicago Duanbb895cb2021-06-18 19:37:16 +08001026 std::regex expr{"power\\d+_label$"}; // Example: power5_label
1027 for (auto& file : fs::directory_iterator(path))
1028 {
1029 if (!std::regex_search(file.path().string(), expr))
1030 {
1031 continue;
1032 }
Chicago Duanbb895cb2021-06-18 19:37:16 +08001033
Matt Spinlera26f1522021-08-25 15:50:20 -05001034 std::string labelValue;
1035 try
1036 {
1037 labelValue = readFile<std::string>(file.path());
1038 }
1039 catch (const std::system_error& e)
1040 {
1041 log<level::DEBUG>(
1042 fmt::format("readPowerSensors: Failed reading {}, errno = {}",
1043 file.path().string(), e.code().value())
1044 .c_str());
Chicago Duanbb895cb2021-06-18 19:37:16 +08001045 continue;
1046 }
Chicago Duanbb895cb2021-06-18 19:37:16 +08001047
1048 auto functionID = getPowerLabelFunctionID(labelValue);
1049 if (functionID == std::nullopt)
1050 {
1051 continue;
1052 }
1053
1054 const std::string& tempLabel = "label";
1055 const std::string filePathString = file.path().string().substr(
1056 0, file.path().string().length() - tempLabel.length());
1057
1058 std::string sensorPath = OCC_SENSORS_ROOT + std::string("/power/");
1059
1060 auto iter = powerSensorName.find(*functionID);
1061 if (iter == powerSensorName.end())
1062 {
1063 continue;
1064 }
1065 sensorPath.append(iter->second);
1066
Matt Spinlera26f1522021-08-25 15:50:20 -05001067 double tempValue{0};
1068
1069 try
Chicago Duanbb895cb2021-06-18 19:37:16 +08001070 {
Matt Spinlera26f1522021-08-25 15:50:20 -05001071 tempValue = readFile<double>(filePathString + inputSuffix);
Chicago Duanbb895cb2021-06-18 19:37:16 +08001072 }
Matt Spinlera26f1522021-08-25 15:50:20 -05001073 catch (const std::system_error& e)
Chicago Duanbb895cb2021-06-18 19:37:16 +08001074 {
Chicago Duanbb895cb2021-06-18 19:37:16 +08001075 log<level::DEBUG>(
Chris Cain5d66a0a2022-02-09 08:52:10 -06001076 fmt::format("readPowerSensors: Failed reading {}, errno = {}",
Matt Spinlera26f1522021-08-25 15:50:20 -05001077 filePathString + inputSuffix, e.code().value())
Chicago Duanbb895cb2021-06-18 19:37:16 +08001078 .c_str());
Matt Spinlera26f1522021-08-25 15:50:20 -05001079 continue;
Chicago Duanbb895cb2021-06-18 19:37:16 +08001080 }
Matt Spinlera26f1522021-08-25 15:50:20 -05001081
Chris Cain5d66a0a2022-02-09 08:52:10 -06001082 dbus::OccDBusSensors::getOccDBus().setUnit(
Chris Caind84a8332022-01-13 08:58:45 -06001083 sensorPath, "xyz.openbmc_project.Sensor.Value.Unit.Watts");
1084
Chris Cain5d66a0a2022-02-09 08:52:10 -06001085 dbus::OccDBusSensors::getOccDBus().setValue(
Matt Spinlera26f1522021-08-25 15:50:20 -05001086 sensorPath, tempValue * std::pow(10, -3) * std::pow(10, -3));
1087
Chris Cain5d66a0a2022-02-09 08:52:10 -06001088 dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath,
1089 true);
Matt Spinlera26f1522021-08-25 15:50:20 -05001090
Matt Spinler5901abd2021-09-23 13:50:03 -05001091 if (existingSensors.find(sensorPath) == existingSensors.end())
1092 {
Chris Cain5d66a0a2022-02-09 08:52:10 -06001093 dbus::OccDBusSensors::getOccDBus().setChassisAssociation(
1094 sensorPath);
Matt Spinler5901abd2021-09-23 13:50:03 -05001095 }
1096
Matt Spinlera26f1522021-08-25 15:50:20 -05001097 existingSensors[sensorPath] = id;
Chicago Duanbb895cb2021-06-18 19:37:16 +08001098 }
1099 return;
1100}
1101
Sheldon Baileyc8dd4592022-05-12 10:15:14 -05001102void Manager::setSensorValueToNaN(uint32_t id) const
Chicago Duanbb895cb2021-06-18 19:37:16 +08001103{
1104 for (const auto& [sensorPath, occId] : existingSensors)
1105 {
1106 if (occId == id)
1107 {
Chris Cain5d66a0a2022-02-09 08:52:10 -06001108 dbus::OccDBusSensors::getOccDBus().setValue(
Chicago Duanbb895cb2021-06-18 19:37:16 +08001109 sensorPath, std::numeric_limits<double>::quiet_NaN());
Sheldon Baileyc8dd4592022-05-12 10:15:14 -05001110
1111 dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath,
1112 true);
Chicago Duanbb895cb2021-06-18 19:37:16 +08001113 }
1114 }
1115 return;
1116}
1117
Sheldon Bailey373af752022-02-21 15:14:00 -06001118void Manager::setSensorValueToNonFunctional(uint32_t id) const
1119{
1120 for (const auto& [sensorPath, occId] : existingSensors)
1121 {
1122 if (occId == id)
1123 {
1124 dbus::OccDBusSensors::getOccDBus().setValue(
1125 sensorPath, std::numeric_limits<double>::quiet_NaN());
1126
1127 dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath,
1128 false);
1129 }
1130 }
1131 return;
1132}
1133
Chris Cain5d66a0a2022-02-09 08:52:10 -06001134void Manager::getSensorValues(std::unique_ptr<Status>& occ)
Chicago Duanbb895cb2021-06-18 19:37:16 +08001135{
Chris Caine2d0a432022-03-28 11:08:49 -05001136 static bool tracedError[8] = {0};
1137 const fs::path sensorPath = occ->getHwmonPath();
Chris Cain5d66a0a2022-02-09 08:52:10 -06001138 const uint32_t id = occ->getOccInstanceID();
Chicago Duanbb895cb2021-06-18 19:37:16 +08001139
Chris Caine2d0a432022-03-28 11:08:49 -05001140 if (fs::exists(sensorPath))
Chicago Duanbb895cb2021-06-18 19:37:16 +08001141 {
Chris Caine2d0a432022-03-28 11:08:49 -05001142 // Read temperature sensors
1143 readTempSensors(sensorPath, id);
1144
1145 if (occ->isMasterOcc())
1146 {
1147 // Read power sensors
1148 readPowerSensors(sensorPath, id);
1149 }
1150 tracedError[id] = false;
1151 }
1152 else
1153 {
1154 if (!tracedError[id])
1155 {
1156 log<level::ERR>(
1157 fmt::format(
1158 "Manager::getSensorValues: OCC{} sensor path missing: {}",
1159 id, sensorPath.c_str())
1160 .c_str());
1161 tracedError[id] = true;
1162 }
Chicago Duanbb895cb2021-06-18 19:37:16 +08001163 }
1164
1165 return;
1166}
1167#endif
Chris Cain17257672021-10-22 13:41:03 -05001168
1169// Read the altitude from DBus
1170void Manager::readAltitude()
1171{
1172 static bool traceAltitudeErr = true;
1173
1174 utils::PropertyValue altitudeProperty{};
1175 try
1176 {
1177 altitudeProperty = utils::getProperty(ALTITUDE_PATH, ALTITUDE_INTERFACE,
1178 ALTITUDE_PROP);
1179 auto sensorVal = std::get<double>(altitudeProperty);
1180 if (sensorVal < 0xFFFF)
1181 {
1182 if (sensorVal < 0)
1183 {
1184 altitude = 0;
1185 }
1186 else
1187 {
1188 // Round to nearest meter
1189 altitude = uint16_t(sensorVal + 0.5);
1190 }
1191 log<level::DEBUG>(fmt::format("readAltitude: sensor={} ({}m)",
1192 sensorVal, altitude)
1193 .c_str());
1194 traceAltitudeErr = true;
1195 }
1196 else
1197 {
1198 if (traceAltitudeErr)
1199 {
1200 traceAltitudeErr = false;
1201 log<level::DEBUG>(
1202 fmt::format("Invalid altitude value: {}", sensorVal)
1203 .c_str());
1204 }
1205 }
1206 }
Patrick Williamsaf408082022-07-22 19:26:54 -05001207 catch (const sdbusplus::exception_t& e)
Chris Cain17257672021-10-22 13:41:03 -05001208 {
1209 if (traceAltitudeErr)
1210 {
1211 traceAltitudeErr = false;
1212 log<level::INFO>(
1213 fmt::format("Unable to read Altitude: {}", e.what()).c_str());
1214 }
1215 altitude = 0xFFFF; // not available
1216 }
1217}
1218
1219// Callback function when ambient temperature changes
Patrick Williamsaf408082022-07-22 19:26:54 -05001220void Manager::ambientCallback(sdbusplus::message_t& msg)
Chris Cain17257672021-10-22 13:41:03 -05001221{
1222 double currentTemp = 0;
1223 uint8_t truncatedTemp = 0xFF;
1224 std::string msgSensor;
1225 std::map<std::string, std::variant<double>> msgData;
1226 msg.read(msgSensor, msgData);
1227
1228 auto valPropMap = msgData.find(AMBIENT_PROP);
1229 if (valPropMap == msgData.end())
1230 {
1231 log<level::DEBUG>("ambientCallback: Unknown ambient property changed");
1232 return;
1233 }
1234 currentTemp = std::get<double>(valPropMap->second);
1235 if (std::isnan(currentTemp))
1236 {
1237 truncatedTemp = 0xFF;
1238 }
1239 else
1240 {
1241 if (currentTemp < 0)
1242 {
1243 truncatedTemp = 0;
1244 }
1245 else
1246 {
1247 // Round to nearest degree C
1248 truncatedTemp = uint8_t(currentTemp + 0.5);
1249 }
1250 }
1251
1252 // If ambient changes, notify OCCs
1253 if (truncatedTemp != ambient)
1254 {
1255 log<level::DEBUG>(
1256 fmt::format("ambientCallback: Ambient change from {} to {}C",
1257 ambient, currentTemp)
1258 .c_str());
1259
1260 ambient = truncatedTemp;
1261 if (altitude == 0xFFFF)
1262 {
1263 // No altitude yet, try reading again
1264 readAltitude();
1265 }
1266
1267 log<level::DEBUG>(
1268 fmt::format("ambientCallback: Ambient: {}C, altitude: {}m", ambient,
1269 altitude)
1270 .c_str());
1271#ifdef POWER10
1272 // Send ambient and altitude to all OCCs
1273 for (auto& obj : statusObjects)
1274 {
1275 if (obj->occActive())
1276 {
1277 obj->sendAmbient(ambient, altitude);
1278 }
1279 }
1280#endif // POWER10
1281 }
1282}
1283
1284// return the current ambient and altitude readings
1285void Manager::getAmbientData(bool& ambientValid, uint8_t& ambientTemp,
1286 uint16_t& altitudeValue) const
1287{
1288 ambientValid = true;
1289 ambientTemp = ambient;
1290 altitudeValue = altitude;
1291
1292 if (ambient == 0xFF)
1293 {
1294 ambientValid = false;
1295 }
1296}
1297
Chris Caina7b74dc2021-11-10 17:03:43 -06001298#ifdef POWER10
Chris Cain7f89e4d2022-05-09 13:27:45 -05001299// Called when waitForAllOccsTimer expires
1300// After the first OCC goes active, this timer will be started (60 seconds)
Chris Caina7b74dc2021-11-10 17:03:43 -06001301void Manager::occsNotAllRunning()
1302{
Chris Caina7b74dc2021-11-10 17:03:43 -06001303 if (activeCount != statusObjects.size())
1304 {
1305 // Not all OCCs went active
1306 log<level::WARNING>(
1307 fmt::format(
1308 "occsNotAllRunning: Active OCC count ({}) does not match expected count ({})",
1309 activeCount, statusObjects.size())
1310 .c_str());
Chris Cain7f89e4d2022-05-09 13:27:45 -05001311 // Procs may be garded, so may be expected
Chris Caina7b74dc2021-11-10 17:03:43 -06001312 }
1313
1314 validateOccMaster();
1315}
1316#endif // POWER10
1317
1318// Verify single master OCC and start presence monitor
1319void Manager::validateOccMaster()
1320{
1321 int masterInstance = -1;
1322 for (auto& obj : statusObjects)
1323 {
Chris Cainbd551de2022-04-26 13:41:16 -05001324 auto instance = obj->getOccInstanceID();
Chris Cainbae4d072022-02-28 09:46:50 -06001325#ifdef POWER10
1326 if (!obj->occActive())
1327 {
1328 if (utils::isHostRunning())
1329 {
Chris Cainbd551de2022-04-26 13:41:16 -05001330 // Check if sensor was queued while waiting for discovery
1331 auto match = queuedActiveState.find(instance);
1332 if (match != queuedActiveState.end())
Chris Cainbae4d072022-02-28 09:46:50 -06001333 {
Chris Cain7f89e4d2022-05-09 13:27:45 -05001334 queuedActiveState.erase(match);
Chris Cainbae4d072022-02-28 09:46:50 -06001335 log<level::INFO>(
1336 fmt::format(
Chris Cainbd551de2022-04-26 13:41:16 -05001337 "validateOccMaster: OCC{} is ACTIVE (queued)",
1338 instance)
Chris Cainbae4d072022-02-28 09:46:50 -06001339 .c_str());
Chris Cainbd551de2022-04-26 13:41:16 -05001340 obj->occActive(true);
1341 }
1342 else
1343 {
1344 // OCC does not appear to be active yet, check active sensor
1345 pldmHandle->checkActiveSensor(instance);
1346 if (obj->occActive())
1347 {
1348 log<level::INFO>(
1349 fmt::format(
1350 "validateOccMaster: OCC{} is ACTIVE after reading sensor",
1351 instance)
1352 .c_str());
1353 }
Chris Cainbae4d072022-02-28 09:46:50 -06001354 }
1355 }
1356 else
1357 {
1358 log<level::WARNING>(
1359 fmt::format(
1360 "validateOccMaster: HOST is not running (OCC{})",
Chris Cainbd551de2022-04-26 13:41:16 -05001361 instance)
Chris Cainbae4d072022-02-28 09:46:50 -06001362 .c_str());
1363 return;
1364 }
1365 }
1366#endif // POWER10
1367
Chris Caina7b74dc2021-11-10 17:03:43 -06001368 if (obj->isMasterOcc())
1369 {
Chris Cain5d66a0a2022-02-09 08:52:10 -06001370 obj->addPresenceWatchMaster();
1371
Chris Caina7b74dc2021-11-10 17:03:43 -06001372 if (masterInstance == -1)
1373 {
Chris Cainbd551de2022-04-26 13:41:16 -05001374 masterInstance = instance;
Chris Caina7b74dc2021-11-10 17:03:43 -06001375 }
1376 else
1377 {
1378 log<level::ERR>(
1379 fmt::format(
1380 "validateOccMaster: Multiple OCC masters! ({} and {})",
Chris Cainbd551de2022-04-26 13:41:16 -05001381 masterInstance, instance)
Chris Caina7b74dc2021-11-10 17:03:43 -06001382 .c_str());
1383 // request reset
Eddie James9789e712022-05-25 15:43:40 -05001384 obj->deviceError(Error::Descriptor(PRESENCE_ERROR_PATH));
Chris Caina7b74dc2021-11-10 17:03:43 -06001385 }
1386 }
1387 }
Chris Cainbae4d072022-02-28 09:46:50 -06001388
Chris Caina7b74dc2021-11-10 17:03:43 -06001389 if (masterInstance < 0)
1390 {
Chris Cainbae4d072022-02-28 09:46:50 -06001391 log<level::ERR>(
1392 fmt::format("validateOccMaster: Master OCC not found! (of {} OCCs)",
1393 statusObjects.size())
1394 .c_str());
Chris Caina7b74dc2021-11-10 17:03:43 -06001395 // request reset
Eddie James9789e712022-05-25 15:43:40 -05001396 statusObjects.front()->deviceError(
1397 Error::Descriptor(PRESENCE_ERROR_PATH));
Chris Caina7b74dc2021-11-10 17:03:43 -06001398 }
1399 else
1400 {
1401 log<level::INFO>(
Chris Cain36f9cde2021-11-22 11:18:21 -06001402 fmt::format("validateOccMaster: OCC{} is master of {} OCCs",
1403 masterInstance, activeCount)
Chris Caina7b74dc2021-11-10 17:03:43 -06001404 .c_str());
Sheldon Bailey31a2f132022-05-20 11:31:52 -05001405#ifdef POWER10
1406 pmode->updateDbusSafeMode(false);
1407#endif
Chris Caina7b74dc2021-11-10 17:03:43 -06001408 }
1409}
1410
Chris Cain40501a22022-03-14 17:33:27 -05001411void Manager::updatePcapBounds() const
1412{
1413 if (pcap)
1414 {
1415 pcap->updatePcapBounds();
1416 }
1417}
1418
Vishwanatha Subbannadfc7ec72017-09-07 18:18:01 +05301419} // namespace occ
1420} // namespace open_power