blob: 4be7c939ea8847bd1e920a435f6fb4648be33072 [file] [log] [blame]
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +05301#include "occ_status.hpp"
Gunnar Mills94df8c92018-09-14 14:50:03 -05002
Chris Cain17257672021-10-22 13:41:03 -05003#include "occ_manager.hpp"
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +05304#include "occ_sensor.hpp"
Chris Cain78e86012021-03-04 16:15:31 -06005#include "powermode.hpp"
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +05306#include "utils.hpp"
Gunnar Mills94df8c92018-09-14 14:50:03 -05007
Chris Cain37abe9b2024-10-31 17:20:31 -05008#include <phosphor-logging/lg2.hpp>
Chris Cain78e86012021-03-04 16:15:31 -06009
Chris Caine2d0a432022-03-28 11:08:49 -050010#include <filesystem>
11
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053012namespace open_power
13{
14namespace occ
15{
Chris Cain78e86012021-03-04 16:15:31 -060016
Chris Caina8857c52021-01-27 11:53:05 -060017using namespace phosphor::logging;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053018
Chris Cainc86d80f2023-05-04 15:49:18 -050019using ThrottleObj =
20 sdbusplus::xyz::openbmc_project::Control::Power::server::Throttle;
21
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053022// Handles updates to occActive property
23bool Status::occActive(bool value)
24{
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053025 if (value != this->occActive())
26 {
Chris Cain37abe9b2024-10-31 17:20:31 -050027 lg2::info("Status::occActive OCC{INST} changed to {STATE}", "INST",
28 instance, "STATE", value);
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053029 if (value)
30 {
Chris Cainf7881502025-04-16 14:48:30 -050031 // OCC is active
Chris Cainc86d80f2023-05-04 15:49:18 -050032 // Clear prior throttle reason (before setting device active)
33 updateThrottle(false, THROTTLED_ALL);
34
Eddie Jamesaced3092022-04-22 16:19:30 -050035 // Set the device active
36 device.setActive(true);
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053037
Chris Caina8857c52021-01-27 11:53:05 -060038 // Reset last OCC state
39 lastState = 0;
40
Chris Cainf0295f52024-09-12 15:41:14 -050041 // Start watching for errors (throttles, etc)
42 try
43 {
44 addErrorWatch();
45 }
46 catch (const OpenFailure& e)
47 {
48 // Failed to add watch for throttle events, request reset to try
49 // to recover comm
Chris Cain37abe9b2024-10-31 17:20:31 -050050 lg2::error(
51 "Status::occActive: Unable to add error watch(s) for OCC{INST} watch: {ERROR}",
52 "INST", instance, "ERROR", e.what());
Chris Cainf0295f52024-09-12 15:41:14 -050053 deviceError(Error::Descriptor(OCC_COMM_ERROR_PATH));
54 return Base::Status::occActive(false);
55 }
56
57 // Update the OCC active sensor
58 Base::Status::occActive(value);
59
Chris Cain5d66a0a2022-02-09 08:52:10 -060060 if (device.master())
61 {
Chris Cain5d66a0a2022-02-09 08:52:10 -060062 // Update powercap bounds from OCC
Chris Cain40501a22022-03-14 17:33:27 -050063 manager.updatePcapBounds();
Chris Cain5d66a0a2022-02-09 08:52:10 -060064 }
65
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +053066 // Call into Manager to let know that we have bound
Chris Cain1be43372021-12-09 19:29:37 -060067 if (this->managerCallBack)
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +053068 {
Sheldon Bailey373af752022-02-21 15:14:00 -060069 this->managerCallBack(instance, value);
Edward A. James9fd2bdc2017-11-08 16:18:57 -060070 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053071 }
72 else
73 {
Chris Cainf7881502025-04-16 14:48:30 -050074 // OCC is no longer active
Chris Cainf7881502025-04-16 14:48:30 -050075 if (sensorsValid)
76 {
77 sensorsValid = false;
78 // Sensors not supported (update to NaN and not functional)
79 manager.setSensorValueToNaN(instance);
80 }
81
Chris Cain1be43372021-12-09 19:29:37 -060082 if (pmode && device.master())
Chris Cain36f9cde2021-11-22 11:18:21 -060083 {
84 // Prevent mode changes
85 pmode->setMasterActive(false);
86 }
Chris Caina7b74dc2021-11-10 17:03:43 -060087 if (safeStateDelayTimer.isEnabled())
88 {
89 // stop safe delay timer
90 safeStateDelayTimer.setEnabled(false);
91 }
Sheldon Bailey16a5adb2025-06-10 14:10:06 -050092
Chris Cain36f9cde2021-11-22 11:18:21 -060093 // Call into Manager to let know that we will unbind.
Chris Cain1be43372021-12-09 19:29:37 -060094 if (this->managerCallBack)
Chris Cain36f9cde2021-11-22 11:18:21 -060095 {
Sheldon Bailey373af752022-02-21 15:14:00 -060096 this->managerCallBack(instance, value);
Chris Cain36f9cde2021-11-22 11:18:21 -060097 }
98
Edward A. James9fd2bdc2017-11-08 16:18:57 -060099 // Stop watching for errors
100 removeErrorWatch();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530101
Eddie Jamesaced3092022-04-22 16:19:30 -0500102 // Set the device inactive
103 device.setActive(false);
Chris Cainc86d80f2023-05-04 15:49:18 -0500104
105 // Clear throttles (OCC not active after disabling device)
106 updateThrottle(false, THROTTLED_ALL);
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530107 }
108 }
Eddie Jamesaced3092022-04-22 16:19:30 -0500109 else if (value && !device.active())
Edward A. James5e177972017-10-25 15:50:31 -0500110 {
111 // Existing error watch is on a dead file descriptor.
Edward A. James9fd2bdc2017-11-08 16:18:57 -0600112 removeErrorWatch();
Edward A. James5e177972017-10-25 15:50:31 -0500113
114 /*
115 * In it's constructor, Status checks Device::bound() to see if OCC is
116 * active or not.
117 * Device::bound() checks for occX-dev0 directory.
118 * We will lose occX-dev0 directories during FSI rescan.
119 * So, if we start this application (and construct Status), and then
120 * later do FSI rescan, we will end up with occActive = true and device
121 * NOT bound. Lets correct that situation here.
122 */
Eddie Jamesaced3092022-04-22 16:19:30 -0500123 device.setActive(true);
Edward A. James5e177972017-10-25 15:50:31 -0500124
125 // Add error watch again
Chris Cainf0295f52024-09-12 15:41:14 -0500126 try
127 {
128 addErrorWatch();
129 }
130 catch (const OpenFailure& e)
131 {
132 // Failed to add watch for throttle events, request reset to try to
133 // recover comm
Chris Cain37abe9b2024-10-31 17:20:31 -0500134 lg2::error(
135 "Status::occActive: Unable to add error watch(s) again for OCC{INST} watch: {ERROR}",
136 "INST", instance, "ERROR", e.what());
Chris Cainf0295f52024-09-12 15:41:14 -0500137 deviceError(Error::Descriptor(OCC_COMM_ERROR_PATH));
138 return Base::Status::occActive(false);
139 }
Edward A. James5e177972017-10-25 15:50:31 -0500140 }
Eddie Jamesaced3092022-04-22 16:19:30 -0500141 else if (!value && device.active())
Eddie James6d6d1b32019-04-22 10:45:08 -0500142 {
143 removeErrorWatch();
144
145 // In the event that the application never receives the active signal
146 // even though the OCC is active (this can occur if the BMC is rebooted
147 // with the host on, since the initial OCC driver probe will discover
148 // the OCCs), this application needs to be able to unbind the device
149 // when we get the OCC inactive signal.
Eddie Jamesaced3092022-04-22 16:19:30 -0500150 device.setActive(false);
Eddie James6d6d1b32019-04-22 10:45:08 -0500151 }
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530152 return Base::Status::occActive(value);
153}
154
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530155// Callback handler when a device error is reported.
Eddie James9789e712022-05-25 15:43:40 -0500156void Status::deviceError(Error::Descriptor d)
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530157{
Chris Cain1be43372021-12-09 19:29:37 -0600158 if (pmode && device.master())
159 {
160 // Prevent mode changes
161 pmode->setMasterActive(false);
162 }
Chris Cain36f9cde2021-11-22 11:18:21 -0600163
Eddie James9789e712022-05-25 15:43:40 -0500164 if (d.log)
165 {
Chris Cain3ece5b92025-01-10 16:06:31 -0600166 FFDC::createOCCResetPEL(instance, d.path, d.err, d.callout,
167 d.isInventoryCallout);
Eddie James9789e712022-05-25 15:43:40 -0500168 }
169
Eddie Jamescbad2192021-10-07 09:39:39 -0500170 // This would deem OCC inactive
171 this->occActive(false);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530172
Eddie Jamescbad2192021-10-07 09:39:39 -0500173 // Reset the OCC
174 this->resetOCC();
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530175}
176
177// Sends message to host control command handler to reset OCC
178void Status::resetOCC()
179{
Chris Cain37abe9b2024-10-31 17:20:31 -0500180 lg2::info(">>Status::resetOCC() - requesting reset for OCC{INST}", "INST",
181 instance);
Chris Cainf0295f52024-09-12 15:41:14 -0500182 this->occActive(false);
Tom Joseph00325232020-07-29 17:51:48 +0530183 if (resetCallBack)
184 {
185 this->resetCallBack(instance);
186 }
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530187}
188
189// Handler called by Host control command handler to convey the
190// status of the executed command
Patrick Williamsaf408082022-07-22 19:26:54 -0500191void Status::hostControlEvent(sdbusplus::message_t& msg)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530192{
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530193 std::string cmdCompleted{};
194 std::string cmdStatus{};
195
196 msg.read(cmdCompleted, cmdStatus);
197
Chris Cain37abe9b2024-10-31 17:20:31 -0500198 lg2::debug("Host control signal values: command={CMD}, status={STATUS}",
199 "CMD", cmdCompleted, "STATUS", cmdStatus);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530200
Gunnar Mills94df8c92018-09-14 14:50:03 -0500201 if (Control::Host::convertResultFromString(cmdStatus) !=
202 Control::Host::Result::Success)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530203 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500204 if (Control::Host::convertCommandFromString(cmdCompleted) ==
205 Control::Host::Command::OCCReset)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530206 {
Gunnar Mills85e65202018-04-08 15:01:54 -0500207 // Must be a Timeout. Log an Error trace
Chris Cain37abe9b2024-10-31 17:20:31 -0500208 lg2::error(
209 "Error resetting the OCC: path={PATH}, sensorid={SENSOR}",
210 "PATH", path, "SENSOR", std::get<0>(sensorMap.at(instance)));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530211 }
212 }
213 return;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530214}
215
Sheldon Bailey373af752022-02-21 15:14:00 -0600216// Called from Manager::pollerTimerExpired() in preperation to POLL OCC.
Chris Caina8857c52021-01-27 11:53:05 -0600217void Status::readOccState()
218{
Chris Cainf0295f52024-09-12 15:41:14 -0500219 if (stateValid)
220 {
221 // Reset retry count (since state is good)
222 currentOccReadRetriesCount = occReadRetries;
223 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600224 occReadStateNow();
Chris Caina8857c52021-01-27 11:53:05 -0600225}
226
Chris Cain78e86012021-03-04 16:15:31 -0600227// Special processing that needs to happen once the OCCs change to ACTIVE state
228void Status::occsWentActive()
229{
230 CmdStatus status = CmdStatus::SUCCESS;
231
Chris Cain1fe436d2024-10-10 09:41:03 -0500232 // IPS data will get sent automatically after a mode change if the mode
233 // supports it.
234 pmode->needToSendIPS();
235
Chris Cain36f9cde2021-11-22 11:18:21 -0600236 status = pmode->sendModeChange();
Chris Cain78e86012021-03-04 16:15:31 -0600237 if (status != CmdStatus::SUCCESS)
238 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500239 lg2::error(
240 "Status::occsWentActive: OCC mode change failed with status {STATUS}",
241 "STATUS", status);
Chris Cainc567dc82022-04-01 15:09:17 -0500242
243 // Disable and reset to try recovering
244 deviceError();
Chris Cain78e86012021-03-04 16:15:31 -0600245 }
Chris Cain78e86012021-03-04 16:15:31 -0600246}
247
Chris Cain17257672021-10-22 13:41:03 -0500248// Send Ambient and Altitude to the OCC
249CmdStatus Status::sendAmbient(const uint8_t inTemp, const uint16_t inAltitude)
250{
251 CmdStatus status = CmdStatus::FAILURE;
252 bool ambientValid = true;
253 uint8_t ambientTemp = inTemp;
254 uint16_t altitude = inAltitude;
255
256 if (ambientTemp == 0xFF)
257 {
258 // Get latest readings from manager
259 manager.getAmbientData(ambientValid, ambientTemp, altitude);
Chris Cain37abe9b2024-10-31 17:20:31 -0500260 lg2::debug(
261 "sendAmbient: valid: {VALID}, Ambient: {TEMP}C, altitude: {ALT}m",
262 "VALID", ambientValid, "TEMP", ambientTemp, "ALT", altitude);
Chris Cain17257672021-10-22 13:41:03 -0500263 }
264
265 std::vector<std::uint8_t> cmd, rsp;
266 cmd.reserve(11);
267 cmd.push_back(uint8_t(CmdType::SEND_AMBIENT));
268 cmd.push_back(0x00); // Data Length (2 bytes)
269 cmd.push_back(0x08); //
270 cmd.push_back(0x00); // Version
271 cmd.push_back(ambientValid ? 0 : 0xFF); // Ambient Status
272 cmd.push_back(ambientTemp); // Ambient Temperature
273 cmd.push_back(altitude >> 8); // Altitude in meters (2 bytes)
274 cmd.push_back(altitude & 0xFF); //
275 cmd.push_back(0x00); // Reserved (3 bytes)
276 cmd.push_back(0x00);
277 cmd.push_back(0x00);
Chris Cain37abe9b2024-10-31 17:20:31 -0500278 lg2::debug("sendAmbient: SEND_AMBIENT "
279 "command to OCC{INST} ({SIZE} bytes)",
280 "INST", instance, "SIZE", cmd.size());
Chris Cain17257672021-10-22 13:41:03 -0500281 status = occCmd.send(cmd, rsp);
282 if (status == CmdStatus::SUCCESS)
283 {
284 if (rsp.size() == 5)
285 {
286 if (RspStatus::SUCCESS != RspStatus(rsp[2]))
287 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500288 lg2::error(
289 "sendAmbient: SEND_AMBIENT failed with rspStatus {STATUS}",
290 "STATUS", lg2::hex, rsp[2]);
Chris Cain17257672021-10-22 13:41:03 -0500291 dump_hex(rsp);
292 status = CmdStatus::FAILURE;
293 }
294 }
295 else
296 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500297 lg2::error(
298 "sendAmbient: INVALID SEND_AMBIENT response length:{SIZE}",
299 "SIZE", rsp.size());
Chris Cain17257672021-10-22 13:41:03 -0500300 dump_hex(rsp);
301 status = CmdStatus::FAILURE;
302 }
303 }
304 else
305 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500306 lg2::error("sendAmbient: SEND_AMBIENT FAILED! with status {STATUS}",
307 "STATUS", lg2::hex, uint8_t(status));
Chris Cainc567dc82022-04-01 15:09:17 -0500308
309 if (status == CmdStatus::COMM_FAILURE)
Chris Cain17257672021-10-22 13:41:03 -0500310 {
Chris Cainf0295f52024-09-12 15:41:14 -0500311 // Disable due to OCC comm failure and reset to try recovering
312 deviceError(Error::Descriptor(OCC_COMM_ERROR_PATH));
Chris Cain17257672021-10-22 13:41:03 -0500313 }
314 }
315
316 return status;
317}
Chris Caina7b74dc2021-11-10 17:03:43 -0600318
319// Called when safe timer expires to determine if OCCs need to be reset
320void Status::safeStateDelayExpired()
321{
322 if (this->occActive())
323 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500324 lg2::info(
325 "safeStateDelayExpired: OCC{INST} state missing or not valid, requesting reset",
326 "INST", instance);
Chris Caina7b74dc2021-11-10 17:03:43 -0600327 // Disable and reset to try recovering
Eddie James9789e712022-05-25 15:43:40 -0500328 deviceError(Error::Descriptor(SAFE_ERROR_PATH));
Chris Caina7b74dc2021-11-10 17:03:43 -0600329 }
330}
Chris Cain78e86012021-03-04 16:15:31 -0600331
Chris Caine2d0a432022-03-28 11:08:49 -0500332fs::path Status::getHwmonPath()
Chris Cain5d66a0a2022-02-09 08:52:10 -0600333{
334 using namespace std::literals::string_literals;
335
Chris Caine2d0a432022-03-28 11:08:49 -0500336 if (!fs::exists(hwmonPath))
337 {
338 static bool tracedFail[8] = {0};
Chris Cain5d66a0a2022-02-09 08:52:10 -0600339
Chris Caine2d0a432022-03-28 11:08:49 -0500340 if (!hwmonPath.empty())
341 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500342 lg2::warning(
343 "Status::getHwmonPath(): path no longer exists: {PATH}", "PATH",
344 hwmonPath);
Chris Caine2d0a432022-03-28 11:08:49 -0500345 hwmonPath.clear();
346 }
347
348 // Build the base HWMON path
Patrick Williamsd7542c82024-08-16 15:20:28 -0400349 fs::path prefixPath =
350 fs::path{OCC_HWMON_PATH + "occ-hwmon."s +
351 std::to_string(instance + 1) + "/hwmon/"s};
Chris Caine2d0a432022-03-28 11:08:49 -0500352
353 // Get the hwmonXX directory name
354 try
355 {
356 // there should only be one directory
357 const int numDirs = std::distance(
358 fs::directory_iterator(prefixPath), fs::directory_iterator{});
359 if (numDirs == 1)
360 {
361 hwmonPath = *fs::directory_iterator(prefixPath);
362 tracedFail[instance] = false;
363 }
364 else
365 {
366 if (!tracedFail[instance])
367 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500368 lg2::error(
369 "Status::getHwmonPath(): Found multiple ({NUM}) hwmon paths!",
370 "NUM", numDirs);
Chris Caine2d0a432022-03-28 11:08:49 -0500371 tracedFail[instance] = true;
372 }
373 }
374 }
375 catch (const fs::filesystem_error& e)
376 {
377 if (!tracedFail[instance])
378 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500379 lg2::error(
380 "Status::getHwmonPath(): error accessing {PATH}: {ERROR}",
381 "PATH", prefixPath, "ERROR", e.what());
Chris Caine2d0a432022-03-28 11:08:49 -0500382 tracedFail[instance] = true;
383 }
384 }
385 }
386
387 return hwmonPath;
Chris Cain5d66a0a2022-02-09 08:52:10 -0600388}
389
Chris Cainf0295f52024-09-12 15:41:14 -0500390// Called to read state and handle any errors
Sheldon Bailey373af752022-02-21 15:14:00 -0600391void Status::occReadStateNow()
392{
393 unsigned int state;
394 const fs::path filename =
395 fs::path(DEV_PATH) /
396 fs::path(sysfsName + "." + std::to_string(instance + 1)) / "occ_state";
397
398 std::ifstream file;
Chris Cainf7881502025-04-16 14:48:30 -0500399 bool stateWasRead = false;
Sheldon Bailey373af752022-02-21 15:14:00 -0600400
401 // open file.
402 file.open(filename, std::ios::in);
403 const int openErrno = errno;
404
405 // File is open and state can be used.
406 if (file.is_open() && file.good())
407 {
Chris Cainf7881502025-04-16 14:48:30 -0500408 stateWasRead = true;
Sheldon Bailey373af752022-02-21 15:14:00 -0600409 file >> state;
Chris Cainf0295f52024-09-12 15:41:14 -0500410 // Read the error code (if any) to check status of the read
411 std::ios_base::iostate readState = file.rdstate();
412 if (readState)
413 {
414 // There was a failure reading the file
415 if (lastOccReadStatus != -1)
416 {
417 // Trace error bits
418 std::string errorBits = "";
419 if (readState & std::ios_base::eofbit)
420 {
421 errorBits += " EOF";
422 }
423 if (readState & std::ios_base::failbit)
424 {
425 errorBits += " failbit";
426 }
427 if (readState & std::ios_base::badbit)
428 {
429 errorBits += " badbit";
430 }
Chris Cain37abe9b2024-10-31 17:20:31 -0500431 lg2::error(
432 "readOccState: Failed to read OCC{INST} state: Read error on I/O operation - {ERROR}",
433 "INST", instance, "ERROR", errorBits);
Chris Cainf0295f52024-09-12 15:41:14 -0500434 lastOccReadStatus = -1;
435 }
Chris Cainf7881502025-04-16 14:48:30 -0500436 stateWasRead = false;
Chris Cainf0295f52024-09-12 15:41:14 -0500437 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600438
Chris Cainf7881502025-04-16 14:48:30 -0500439 if (stateWasRead && (state != lastState))
Sheldon Bailey373af752022-02-21 15:14:00 -0600440 {
441 // Trace OCC state changes
Chris Cain37abe9b2024-10-31 17:20:31 -0500442 lg2::info(
443 "Status::readOccState: OCC{INST} state {STATE} (lastState: {PRIOR})",
444 "INST", instance, "STATE", lg2::hex, state, "PRIOR", lg2::hex,
445 lastState);
Sheldon Bailey373af752022-02-21 15:14:00 -0600446 lastState = state;
Sheldon Bailey16a5adb2025-06-10 14:10:06 -0500447
Sheldon Bailey373af752022-02-21 15:14:00 -0600448 if (OccState(state) == OccState::ACTIVE)
449 {
450 if (pmode && device.master())
451 {
452 // Set the master OCC on the PowerMode object
453 pmode->setMasterOcc(path);
454 // Enable mode changes
455 pmode->setMasterActive();
456
457 // Special processing by master OCC when it goes active
458 occsWentActive();
459 }
460
461 CmdStatus status = sendAmbient();
462 if (status != CmdStatus::SUCCESS)
463 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500464 lg2::error(
465 "readOccState: Sending Ambient failed with status {STATUS}",
466 "STATUS", status);
Sheldon Bailey373af752022-02-21 15:14:00 -0600467 }
468 }
469
470 // If OCC in known Good State.
471 if ((OccState(state) == OccState::ACTIVE) ||
472 (OccState(state) == OccState::CHARACTERIZATION) ||
473 (OccState(state) == OccState::OBSERVATION))
474 {
475 // Good OCC State then sensors valid again
476 stateValid = true;
Chris Cainf7881502025-04-16 14:48:30 -0500477 sensorsValid = true;
Sheldon Bailey373af752022-02-21 15:14:00 -0600478
479 if (safeStateDelayTimer.isEnabled())
480 {
481 // stop safe delay timer (no longer in SAFE state)
482 safeStateDelayTimer.setEnabled(false);
483 }
484 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600485 else
486 {
Chris Cainf0295f52024-09-12 15:41:14 -0500487 // OCC is in SAFE or some other unsupported state
Sheldon Bailey373af752022-02-21 15:14:00 -0600488 if (!safeStateDelayTimer.isEnabled())
489 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500490 lg2::error(
Chris Cainf7881502025-04-16 14:48:30 -0500491 "readOccState: Invalid OCC{INST} state of {STATE} (last state: {PRIOR}), starting safe state delay timer",
492 "INST", instance, "STATE", lg2::hex, state, "PRIOR",
493 lg2::hex, lastState);
Sheldon Bailey373af752022-02-21 15:14:00 -0600494 // start safe delay timer (before requesting reset)
495 using namespace std::literals::chrono_literals;
496 safeStateDelayTimer.restartOnce(60s);
497 }
Chris Cainf7881502025-04-16 14:48:30 -0500498
499 if (sensorsValid)
500 {
501 sensorsValid = false;
502 // Sensors not supported (update to NaN and not functional)
503 manager.setSensorValueToNaN(instance);
504 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600505 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600506 }
507 }
Chris Cainf0295f52024-09-12 15:41:14 -0500508 else
509 {
510 // Unable to read state
511 stateValid = false;
512 }
Sheldon Bailey16a5adb2025-06-10 14:10:06 -0500513
Sheldon Bailey373af752022-02-21 15:14:00 -0600514 file.close();
515
Chris Cainf7881502025-04-16 14:48:30 -0500516 // if failed to read the OCC state -> Attempt retry
517 if (!stateWasRead)
Sheldon Bailey373af752022-02-21 15:14:00 -0600518 {
Chris Cainf7881502025-04-16 14:48:30 -0500519 if (sensorsValid)
520 {
521 sensorsValid = false;
522 manager.setSensorValueToNaN(instance);
523 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600524
Chris Cainf7881502025-04-16 14:48:30 -0500525 // If not able to read, OCC may be offline
526 if (openErrno != lastOccReadStatus)
527 {
528 lg2::error(
529 "Status::readOccState: open/read failed trying to read OCC{INST} state (open errno={ERROR})",
530 "INST", instance, "ERROR", openErrno);
531 lastOccReadStatus = openErrno;
532 }
533
Sheldon Bailey373af752022-02-21 15:14:00 -0600534 // See occReadRetries for number of retry attempts.
535 if (currentOccReadRetriesCount > 0)
536 {
537 --currentOccReadRetriesCount;
Sheldon Bailey373af752022-02-21 15:14:00 -0600538 }
539 else
540 {
Chris Cainf7881502025-04-16 14:48:30 -0500541 lg2::error(
542 "readOccState: failed to read OCC{INST} state! (last state: {PRIOR})",
543 "INST", instance, "PRIOR", lg2::hex, lastState);
Chris Cainf0295f52024-09-12 15:41:14 -0500544
Sheldon Bailey373af752022-02-21 15:14:00 -0600545 // State could not be determined, set it to NO State.
546 lastState = 0;
547
548 // Disable the ability to send Failed actions until OCC is
549 // Active again.
550 stateValid = false;
551
Chris Cainf0295f52024-09-12 15:41:14 -0500552 // Disable due to OCC comm failure and reset to try recovering
Chris Cain3ece5b92025-01-10 16:06:31 -0600553 // (processor callout will be added)
554 deviceError(Error::Descriptor(OCC_COMM_ERROR_PATH, ECOMM,
555 procPath.c_str(), true));
Chris Cainf0295f52024-09-12 15:41:14 -0500556
557 // Reset retry count (for next attempt after recovery)
558 currentOccReadRetriesCount = occReadRetries;
559 }
560 }
Chris Cainf7881502025-04-16 14:48:30 -0500561 else if (lastOccReadStatus != 0)
Chris Cainf0295f52024-09-12 15:41:14 -0500562 {
Chris Cainf7881502025-04-16 14:48:30 -0500563 lg2::info("readOccState: successfully read OCC{INST} state: {STATE}",
564 "INST", instance, "STATE", state);
565 lastOccReadStatus = 0; // no error
Sheldon Bailey373af752022-02-21 15:14:00 -0600566 }
567}
568
Chris Cainc86d80f2023-05-04 15:49:18 -0500569// Update processor throttle status on dbus
570void Status::updateThrottle(const bool isThrottled, const uint8_t newReason)
571{
572 if (!throttleHandle)
573 {
574 return;
575 }
576
577 uint8_t newThrottleCause = throttleCause;
578
579 if (isThrottled) // throttled due to newReason
580 {
581 if ((newReason & throttleCause) == 0)
582 {
583 // set the bit(s) for passed in reason
584 newThrottleCause |= newReason;
585 }
586 // else no change
587 }
588 else // no longer throttled due to newReason
589 {
590 if ((newReason & throttleCause) != 0)
591 {
592 // clear the bit(s) for passed in reason
593 newThrottleCause &= ~newReason;
594 }
595 // else no change
596 }
597
598 if (newThrottleCause != throttleCause)
599 {
600 if (newThrottleCause == THROTTLED_NONE)
601 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500602 lg2::debug(
603 "updateThrottle: OCC{INST} no longer throttled (prior reason: {REASON})",
604 "INST", instance, "REASON", throttleCause);
Chris Cainc86d80f2023-05-04 15:49:18 -0500605 throttleCause = THROTTLED_NONE;
606 throttleHandle->throttled(false);
607 throttleHandle->throttleCauses({});
608 }
609 else
610 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500611 lg2::debug(
612 "updateThrottle: OCC{INST} is throttled with reason {REASON} (prior reason: {PRIOR})",
613 "INST", instance, "REASON", newThrottleCause, "PRIOR",
614 throttleCause);
Chris Cainc86d80f2023-05-04 15:49:18 -0500615 throttleCause = newThrottleCause;
616
617 std::vector<ThrottleObj::ThrottleReasons> updatedCauses;
618 if (throttleCause & THROTTLED_POWER)
619 {
620 updatedCauses.push_back(
621 throttleHandle->ThrottleReasons::PowerLimit);
622 }
623 if (throttleCause & THROTTLED_THERMAL)
624 {
625 updatedCauses.push_back(
626 throttleHandle->ThrottleReasons::ThermalLimit);
627 }
628 if (throttleCause & THROTTLED_SAFE)
629 {
630 updatedCauses.push_back(
631 throttleHandle->ThrottleReasons::ManagementDetectedFault);
632 }
633 throttleHandle->throttleCauses(updatedCauses);
634 throttleHandle->throttled(true);
635 }
636 }
637 // else no change to throttle status
638}
639
640// Get processor path associated with this OCC
641void Status::readProcAssociation()
642{
643 std::string managingPath = path + "/power_managing";
Chris Cain37abe9b2024-10-31 17:20:31 -0500644 lg2::debug("readProcAssociation: getting endpoints for {MANAGE} ({PATH})",
645 "MANAGE", managingPath, "PATH", path);
Chris Cainc86d80f2023-05-04 15:49:18 -0500646 try
647 {
648 utils::PropertyValue procPathProperty{};
649 procPathProperty = utils::getProperty(
650 managingPath, "xyz.openbmc_project.Association", "endpoints");
651 auto result = std::get<std::vector<std::string>>(procPathProperty);
652 if (result.size() > 0)
653 {
654 procPath = result[0];
Chris Cain37abe9b2024-10-31 17:20:31 -0500655 lg2::info("readProcAssociation: OCC{INST} has proc={PATH}", "INST",
656 instance, "PATH", procPath);
Chris Cainc86d80f2023-05-04 15:49:18 -0500657 }
658 else
659 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500660 lg2::error(
661 "readProcAssociation: No processor associated with OCC{INST} / {PATH}",
662 "INST", instance, "PATH", path);
Chris Cainc86d80f2023-05-04 15:49:18 -0500663 }
664 }
665 catch (const sdbusplus::exception_t& e)
666 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500667 lg2::error(
668 "readProcAssociation: Unable to get proc assocated with {PATH} - {ERROR}",
669 "PATH", path, "ERROR", e.what());
Chris Cainc86d80f2023-05-04 15:49:18 -0500670 procPath = {};
671 }
672}
673
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530674} // namespace occ
675} // namespace open_power