blob: ed4440338058307e06a72f439bb228f48cd6809c [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 Caina7b74dc2021-11-10 17:03:43 -060075#ifdef POWER10
Chris Cainf7881502025-04-16 14:48:30 -050076 if (sensorsValid)
77 {
78 sensorsValid = false;
79 // Sensors not supported (update to NaN and not functional)
80 manager.setSensorValueToNaN(instance);
81 }
82
Chris Cain1be43372021-12-09 19:29:37 -060083 if (pmode && device.master())
Chris Cain36f9cde2021-11-22 11:18:21 -060084 {
85 // Prevent mode changes
86 pmode->setMasterActive(false);
87 }
Chris Caina7b74dc2021-11-10 17:03:43 -060088 if (safeStateDelayTimer.isEnabled())
89 {
90 // stop safe delay timer
91 safeStateDelayTimer.setEnabled(false);
92 }
93#endif
Chris Cain36f9cde2021-11-22 11:18:21 -060094 // Call into Manager to let know that we will unbind.
Chris Cain1be43372021-12-09 19:29:37 -060095 if (this->managerCallBack)
Chris Cain36f9cde2021-11-22 11:18:21 -060096 {
Sheldon Bailey373af752022-02-21 15:14:00 -060097 this->managerCallBack(instance, value);
Chris Cain36f9cde2021-11-22 11:18:21 -060098 }
99
Edward A. James9fd2bdc2017-11-08 16:18:57 -0600100 // Stop watching for errors
101 removeErrorWatch();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530102
Eddie Jamesaced3092022-04-22 16:19:30 -0500103 // Set the device inactive
104 device.setActive(false);
Chris Cainc86d80f2023-05-04 15:49:18 -0500105
106 // Clear throttles (OCC not active after disabling device)
107 updateThrottle(false, THROTTLED_ALL);
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530108 }
109 }
Eddie Jamesaced3092022-04-22 16:19:30 -0500110 else if (value && !device.active())
Edward A. James5e177972017-10-25 15:50:31 -0500111 {
112 // Existing error watch is on a dead file descriptor.
Edward A. James9fd2bdc2017-11-08 16:18:57 -0600113 removeErrorWatch();
Edward A. James5e177972017-10-25 15:50:31 -0500114
115 /*
116 * In it's constructor, Status checks Device::bound() to see if OCC is
117 * active or not.
118 * Device::bound() checks for occX-dev0 directory.
119 * We will lose occX-dev0 directories during FSI rescan.
120 * So, if we start this application (and construct Status), and then
121 * later do FSI rescan, we will end up with occActive = true and device
122 * NOT bound. Lets correct that situation here.
123 */
Eddie Jamesaced3092022-04-22 16:19:30 -0500124 device.setActive(true);
Edward A. James5e177972017-10-25 15:50:31 -0500125
126 // Add error watch again
Chris Cainf0295f52024-09-12 15:41:14 -0500127 try
128 {
129 addErrorWatch();
130 }
131 catch (const OpenFailure& e)
132 {
133 // Failed to add watch for throttle events, request reset to try to
134 // recover comm
Chris Cain37abe9b2024-10-31 17:20:31 -0500135 lg2::error(
136 "Status::occActive: Unable to add error watch(s) again for OCC{INST} watch: {ERROR}",
137 "INST", instance, "ERROR", e.what());
Chris Cainf0295f52024-09-12 15:41:14 -0500138 deviceError(Error::Descriptor(OCC_COMM_ERROR_PATH));
139 return Base::Status::occActive(false);
140 }
Edward A. James5e177972017-10-25 15:50:31 -0500141 }
Eddie Jamesaced3092022-04-22 16:19:30 -0500142 else if (!value && device.active())
Eddie James6d6d1b32019-04-22 10:45:08 -0500143 {
144 removeErrorWatch();
145
146 // In the event that the application never receives the active signal
147 // even though the OCC is active (this can occur if the BMC is rebooted
148 // with the host on, since the initial OCC driver probe will discover
149 // the OCCs), this application needs to be able to unbind the device
150 // when we get the OCC inactive signal.
Eddie Jamesaced3092022-04-22 16:19:30 -0500151 device.setActive(false);
Eddie James6d6d1b32019-04-22 10:45:08 -0500152 }
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530153 return Base::Status::occActive(value);
154}
155
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530156// Callback handler when a device error is reported.
Eddie James9789e712022-05-25 15:43:40 -0500157void Status::deviceError(Error::Descriptor d)
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530158{
Chris Cain36f9cde2021-11-22 11:18:21 -0600159#ifdef POWER10
Chris Cain1be43372021-12-09 19:29:37 -0600160 if (pmode && device.master())
161 {
162 // Prevent mode changes
163 pmode->setMasterActive(false);
164 }
Chris Cain36f9cde2021-11-22 11:18:21 -0600165#endif
166
Eddie James9789e712022-05-25 15:43:40 -0500167 if (d.log)
168 {
Chris Cain3ece5b92025-01-10 16:06:31 -0600169 FFDC::createOCCResetPEL(instance, d.path, d.err, d.callout,
170 d.isInventoryCallout);
Eddie James9789e712022-05-25 15:43:40 -0500171 }
172
Eddie Jamescbad2192021-10-07 09:39:39 -0500173 // This would deem OCC inactive
174 this->occActive(false);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530175
Eddie Jamescbad2192021-10-07 09:39:39 -0500176 // Reset the OCC
177 this->resetOCC();
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530178}
179
180// Sends message to host control command handler to reset OCC
181void Status::resetOCC()
182{
Chris Cain37abe9b2024-10-31 17:20:31 -0500183 lg2::info(">>Status::resetOCC() - requesting reset for OCC{INST}", "INST",
184 instance);
Chris Cainf0295f52024-09-12 15:41:14 -0500185 this->occActive(false);
Tom Joseph00325232020-07-29 17:51:48 +0530186#ifdef PLDM
187 if (resetCallBack)
188 {
189 this->resetCallBack(instance);
190 }
191#else
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530192 constexpr auto CONTROL_HOST_PATH = "/org/open_power/control/host0";
193 constexpr auto CONTROL_HOST_INTF = "org.open_power.Control.Host";
194
195 // This will throw exception on failure
George Liuf3b75142021-06-10 11:22:50 +0800196 auto service = utils::getService(CONTROL_HOST_PATH, CONTROL_HOST_INTF);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530197
George Liuf3b75142021-06-10 11:22:50 +0800198 auto& bus = utils::getBus();
Gunnar Mills94df8c92018-09-14 14:50:03 -0500199 auto method = bus.new_method_call(service.c_str(), CONTROL_HOST_PATH,
200 CONTROL_HOST_INTF, "Execute");
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530201 // OCC Reset control command
Gunnar Mills94df8c92018-09-14 14:50:03 -0500202 method.append(convertForMessage(Control::Host::Command::OCCReset).c_str());
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530203
204 // OCC Sensor ID for callout reasons
Patrick Williamse0962702020-05-13 17:50:22 -0500205 method.append(std::variant<uint8_t>(std::get<0>(sensorMap.at(instance))));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530206 bus.call_noreply(method);
207 return;
Tom Joseph00325232020-07-29 17:51:48 +0530208#endif
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530209}
210
211// Handler called by Host control command handler to convey the
212// status of the executed command
Patrick Williamsaf408082022-07-22 19:26:54 -0500213void Status::hostControlEvent(sdbusplus::message_t& msg)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530214{
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530215 std::string cmdCompleted{};
216 std::string cmdStatus{};
217
218 msg.read(cmdCompleted, cmdStatus);
219
Chris Cain37abe9b2024-10-31 17:20:31 -0500220 lg2::debug("Host control signal values: command={CMD}, status={STATUS}",
221 "CMD", cmdCompleted, "STATUS", cmdStatus);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530222
Gunnar Mills94df8c92018-09-14 14:50:03 -0500223 if (Control::Host::convertResultFromString(cmdStatus) !=
224 Control::Host::Result::Success)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530225 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500226 if (Control::Host::convertCommandFromString(cmdCompleted) ==
227 Control::Host::Command::OCCReset)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530228 {
Gunnar Mills85e65202018-04-08 15:01:54 -0500229 // Must be a Timeout. Log an Error trace
Chris Cain37abe9b2024-10-31 17:20:31 -0500230 lg2::error(
231 "Error resetting the OCC: path={PATH}, sensorid={SENSOR}",
232 "PATH", path, "SENSOR", std::get<0>(sensorMap.at(instance)));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530233 }
234 }
235 return;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530236}
237
Sheldon Bailey373af752022-02-21 15:14:00 -0600238// Called from Manager::pollerTimerExpired() in preperation to POLL OCC.
Chris Caina8857c52021-01-27 11:53:05 -0600239void Status::readOccState()
240{
Chris Cainf0295f52024-09-12 15:41:14 -0500241 if (stateValid)
242 {
243 // Reset retry count (since state is good)
244 currentOccReadRetriesCount = occReadRetries;
245 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600246 occReadStateNow();
Chris Caina8857c52021-01-27 11:53:05 -0600247}
248
Chris Cain78e86012021-03-04 16:15:31 -0600249#ifdef POWER10
Chris Cain78e86012021-03-04 16:15:31 -0600250// Special processing that needs to happen once the OCCs change to ACTIVE state
251void Status::occsWentActive()
252{
253 CmdStatus status = CmdStatus::SUCCESS;
254
Chris Cain1fe436d2024-10-10 09:41:03 -0500255 // IPS data will get sent automatically after a mode change if the mode
256 // supports it.
257 pmode->needToSendIPS();
258
Chris Cain36f9cde2021-11-22 11:18:21 -0600259 status = pmode->sendModeChange();
Chris Cain78e86012021-03-04 16:15:31 -0600260 if (status != CmdStatus::SUCCESS)
261 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500262 lg2::error(
263 "Status::occsWentActive: OCC mode change failed with status {STATUS}",
264 "STATUS", status);
Chris Cainc567dc82022-04-01 15:09:17 -0500265
266 // Disable and reset to try recovering
267 deviceError();
Chris Cain78e86012021-03-04 16:15:31 -0600268 }
Chris Cain78e86012021-03-04 16:15:31 -0600269}
270
Chris Cain17257672021-10-22 13:41:03 -0500271// Send Ambient and Altitude to the OCC
272CmdStatus Status::sendAmbient(const uint8_t inTemp, const uint16_t inAltitude)
273{
274 CmdStatus status = CmdStatus::FAILURE;
275 bool ambientValid = true;
276 uint8_t ambientTemp = inTemp;
277 uint16_t altitude = inAltitude;
278
279 if (ambientTemp == 0xFF)
280 {
281 // Get latest readings from manager
282 manager.getAmbientData(ambientValid, ambientTemp, altitude);
Chris Cain37abe9b2024-10-31 17:20:31 -0500283 lg2::debug(
284 "sendAmbient: valid: {VALID}, Ambient: {TEMP}C, altitude: {ALT}m",
285 "VALID", ambientValid, "TEMP", ambientTemp, "ALT", altitude);
Chris Cain17257672021-10-22 13:41:03 -0500286 }
287
288 std::vector<std::uint8_t> cmd, rsp;
289 cmd.reserve(11);
290 cmd.push_back(uint8_t(CmdType::SEND_AMBIENT));
291 cmd.push_back(0x00); // Data Length (2 bytes)
292 cmd.push_back(0x08); //
293 cmd.push_back(0x00); // Version
294 cmd.push_back(ambientValid ? 0 : 0xFF); // Ambient Status
295 cmd.push_back(ambientTemp); // Ambient Temperature
296 cmd.push_back(altitude >> 8); // Altitude in meters (2 bytes)
297 cmd.push_back(altitude & 0xFF); //
298 cmd.push_back(0x00); // Reserved (3 bytes)
299 cmd.push_back(0x00);
300 cmd.push_back(0x00);
Chris Cain37abe9b2024-10-31 17:20:31 -0500301 lg2::debug("sendAmbient: SEND_AMBIENT "
302 "command to OCC{INST} ({SIZE} bytes)",
303 "INST", instance, "SIZE", cmd.size());
Chris Cain17257672021-10-22 13:41:03 -0500304 status = occCmd.send(cmd, rsp);
305 if (status == CmdStatus::SUCCESS)
306 {
307 if (rsp.size() == 5)
308 {
309 if (RspStatus::SUCCESS != RspStatus(rsp[2]))
310 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500311 lg2::error(
312 "sendAmbient: SEND_AMBIENT failed with rspStatus {STATUS}",
313 "STATUS", lg2::hex, rsp[2]);
Chris Cain17257672021-10-22 13:41:03 -0500314 dump_hex(rsp);
315 status = CmdStatus::FAILURE;
316 }
317 }
318 else
319 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500320 lg2::error(
321 "sendAmbient: INVALID SEND_AMBIENT response length:{SIZE}",
322 "SIZE", rsp.size());
Chris Cain17257672021-10-22 13:41:03 -0500323 dump_hex(rsp);
324 status = CmdStatus::FAILURE;
325 }
326 }
327 else
328 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500329 lg2::error("sendAmbient: SEND_AMBIENT FAILED! with status {STATUS}",
330 "STATUS", lg2::hex, uint8_t(status));
Chris Cainc567dc82022-04-01 15:09:17 -0500331
332 if (status == CmdStatus::COMM_FAILURE)
Chris Cain17257672021-10-22 13:41:03 -0500333 {
Chris Cainf0295f52024-09-12 15:41:14 -0500334 // Disable due to OCC comm failure and reset to try recovering
335 deviceError(Error::Descriptor(OCC_COMM_ERROR_PATH));
Chris Cain17257672021-10-22 13:41:03 -0500336 }
337 }
338
339 return status;
340}
Chris Caina7b74dc2021-11-10 17:03:43 -0600341
342// Called when safe timer expires to determine if OCCs need to be reset
343void Status::safeStateDelayExpired()
344{
345 if (this->occActive())
346 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500347 lg2::info(
348 "safeStateDelayExpired: OCC{INST} state missing or not valid, requesting reset",
349 "INST", instance);
Chris Caina7b74dc2021-11-10 17:03:43 -0600350 // Disable and reset to try recovering
Eddie James9789e712022-05-25 15:43:40 -0500351 deviceError(Error::Descriptor(SAFE_ERROR_PATH));
Chris Caina7b74dc2021-11-10 17:03:43 -0600352 }
353}
Chris Cain78e86012021-03-04 16:15:31 -0600354#endif // POWER10
355
Chris Caine2d0a432022-03-28 11:08:49 -0500356fs::path Status::getHwmonPath()
Chris Cain5d66a0a2022-02-09 08:52:10 -0600357{
358 using namespace std::literals::string_literals;
359
Chris Caine2d0a432022-03-28 11:08:49 -0500360 if (!fs::exists(hwmonPath))
361 {
362 static bool tracedFail[8] = {0};
Chris Cain5d66a0a2022-02-09 08:52:10 -0600363
Chris Caine2d0a432022-03-28 11:08:49 -0500364 if (!hwmonPath.empty())
365 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500366 lg2::warning(
367 "Status::getHwmonPath(): path no longer exists: {PATH}", "PATH",
368 hwmonPath);
Chris Caine2d0a432022-03-28 11:08:49 -0500369 hwmonPath.clear();
370 }
371
372 // Build the base HWMON path
Patrick Williamsd7542c82024-08-16 15:20:28 -0400373 fs::path prefixPath =
374 fs::path{OCC_HWMON_PATH + "occ-hwmon."s +
375 std::to_string(instance + 1) + "/hwmon/"s};
Chris Caine2d0a432022-03-28 11:08:49 -0500376
377 // Get the hwmonXX directory name
378 try
379 {
380 // there should only be one directory
381 const int numDirs = std::distance(
382 fs::directory_iterator(prefixPath), fs::directory_iterator{});
383 if (numDirs == 1)
384 {
385 hwmonPath = *fs::directory_iterator(prefixPath);
386 tracedFail[instance] = false;
387 }
388 else
389 {
390 if (!tracedFail[instance])
391 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500392 lg2::error(
393 "Status::getHwmonPath(): Found multiple ({NUM}) hwmon paths!",
394 "NUM", numDirs);
Chris Caine2d0a432022-03-28 11:08:49 -0500395 tracedFail[instance] = true;
396 }
397 }
398 }
399 catch (const fs::filesystem_error& e)
400 {
401 if (!tracedFail[instance])
402 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500403 lg2::error(
404 "Status::getHwmonPath(): error accessing {PATH}: {ERROR}",
405 "PATH", prefixPath, "ERROR", e.what());
Chris Caine2d0a432022-03-28 11:08:49 -0500406 tracedFail[instance] = true;
407 }
408 }
409 }
410
411 return hwmonPath;
Chris Cain5d66a0a2022-02-09 08:52:10 -0600412}
413
Chris Cainf0295f52024-09-12 15:41:14 -0500414// Called to read state and handle any errors
Sheldon Bailey373af752022-02-21 15:14:00 -0600415void Status::occReadStateNow()
416{
417 unsigned int state;
418 const fs::path filename =
419 fs::path(DEV_PATH) /
420 fs::path(sysfsName + "." + std::to_string(instance + 1)) / "occ_state";
421
422 std::ifstream file;
Chris Cainf7881502025-04-16 14:48:30 -0500423 bool stateWasRead = false;
Sheldon Bailey373af752022-02-21 15:14:00 -0600424
425 // open file.
426 file.open(filename, std::ios::in);
427 const int openErrno = errno;
428
429 // File is open and state can be used.
430 if (file.is_open() && file.good())
431 {
Chris Cainf7881502025-04-16 14:48:30 -0500432 stateWasRead = true;
Sheldon Bailey373af752022-02-21 15:14:00 -0600433 file >> state;
Chris Cainf0295f52024-09-12 15:41:14 -0500434 // Read the error code (if any) to check status of the read
435 std::ios_base::iostate readState = file.rdstate();
436 if (readState)
437 {
438 // There was a failure reading the file
439 if (lastOccReadStatus != -1)
440 {
441 // Trace error bits
442 std::string errorBits = "";
443 if (readState & std::ios_base::eofbit)
444 {
445 errorBits += " EOF";
446 }
447 if (readState & std::ios_base::failbit)
448 {
449 errorBits += " failbit";
450 }
451 if (readState & std::ios_base::badbit)
452 {
453 errorBits += " badbit";
454 }
Chris Cain37abe9b2024-10-31 17:20:31 -0500455 lg2::error(
456 "readOccState: Failed to read OCC{INST} state: Read error on I/O operation - {ERROR}",
457 "INST", instance, "ERROR", errorBits);
Chris Cainf0295f52024-09-12 15:41:14 -0500458 lastOccReadStatus = -1;
459 }
Chris Cainf7881502025-04-16 14:48:30 -0500460 stateWasRead = false;
Chris Cainf0295f52024-09-12 15:41:14 -0500461 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600462
Chris Cainf7881502025-04-16 14:48:30 -0500463 if (stateWasRead && (state != lastState))
Sheldon Bailey373af752022-02-21 15:14:00 -0600464 {
465 // Trace OCC state changes
Chris Cain37abe9b2024-10-31 17:20:31 -0500466 lg2::info(
467 "Status::readOccState: OCC{INST} state {STATE} (lastState: {PRIOR})",
468 "INST", instance, "STATE", lg2::hex, state, "PRIOR", lg2::hex,
469 lastState);
Sheldon Bailey373af752022-02-21 15:14:00 -0600470 lastState = state;
471#ifdef POWER10
472 if (OccState(state) == OccState::ACTIVE)
473 {
474 if (pmode && device.master())
475 {
476 // Set the master OCC on the PowerMode object
477 pmode->setMasterOcc(path);
478 // Enable mode changes
479 pmode->setMasterActive();
480
481 // Special processing by master OCC when it goes active
482 occsWentActive();
483 }
484
485 CmdStatus status = sendAmbient();
486 if (status != CmdStatus::SUCCESS)
487 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500488 lg2::error(
489 "readOccState: Sending Ambient failed with status {STATUS}",
490 "STATUS", status);
Sheldon Bailey373af752022-02-21 15:14:00 -0600491 }
492 }
493
494 // If OCC in known Good State.
495 if ((OccState(state) == OccState::ACTIVE) ||
496 (OccState(state) == OccState::CHARACTERIZATION) ||
497 (OccState(state) == OccState::OBSERVATION))
498 {
499 // Good OCC State then sensors valid again
500 stateValid = true;
Chris Cainf7881502025-04-16 14:48:30 -0500501 sensorsValid = true;
Sheldon Bailey373af752022-02-21 15:14:00 -0600502
503 if (safeStateDelayTimer.isEnabled())
504 {
505 // stop safe delay timer (no longer in SAFE state)
506 safeStateDelayTimer.setEnabled(false);
507 }
508 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600509 else
510 {
Chris Cainf0295f52024-09-12 15:41:14 -0500511 // OCC is in SAFE or some other unsupported state
Sheldon Bailey373af752022-02-21 15:14:00 -0600512 if (!safeStateDelayTimer.isEnabled())
513 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500514 lg2::error(
Chris Cainf7881502025-04-16 14:48:30 -0500515 "readOccState: Invalid OCC{INST} state of {STATE} (last state: {PRIOR}), starting safe state delay timer",
516 "INST", instance, "STATE", lg2::hex, state, "PRIOR",
517 lg2::hex, lastState);
Sheldon Bailey373af752022-02-21 15:14:00 -0600518 // start safe delay timer (before requesting reset)
519 using namespace std::literals::chrono_literals;
520 safeStateDelayTimer.restartOnce(60s);
521 }
Chris Cainf7881502025-04-16 14:48:30 -0500522
523 if (sensorsValid)
524 {
525 sensorsValid = false;
526 // Sensors not supported (update to NaN and not functional)
527 manager.setSensorValueToNaN(instance);
528 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600529 }
530#else
531 // Before P10 state not checked, only used good file open.
532 stateValid = true;
533#endif
534 }
535 }
Chris Cainf0295f52024-09-12 15:41:14 -0500536#ifdef POWER10
537 else
538 {
539 // Unable to read state
540 stateValid = false;
541 }
542#endif
Sheldon Bailey373af752022-02-21 15:14:00 -0600543 file.close();
544
Chris Cainf7881502025-04-16 14:48:30 -0500545 // if failed to read the OCC state -> Attempt retry
546 if (!stateWasRead)
Sheldon Bailey373af752022-02-21 15:14:00 -0600547 {
Sheldon Bailey373af752022-02-21 15:14:00 -0600548#ifdef READ_OCC_SENSORS
Chris Cainf7881502025-04-16 14:48:30 -0500549 if (sensorsValid)
550 {
551 sensorsValid = false;
552 manager.setSensorValueToNaN(instance);
553 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600554#endif
555
Chris Cainf7881502025-04-16 14:48:30 -0500556 // If not able to read, OCC may be offline
557 if (openErrno != lastOccReadStatus)
558 {
559 lg2::error(
560 "Status::readOccState: open/read failed trying to read OCC{INST} state (open errno={ERROR})",
561 "INST", instance, "ERROR", openErrno);
562 lastOccReadStatus = openErrno;
563 }
564
Sheldon Bailey373af752022-02-21 15:14:00 -0600565 // See occReadRetries for number of retry attempts.
566 if (currentOccReadRetriesCount > 0)
567 {
568 --currentOccReadRetriesCount;
Sheldon Bailey373af752022-02-21 15:14:00 -0600569 }
570 else
571 {
Chris Cainf7881502025-04-16 14:48:30 -0500572 lg2::error(
573 "readOccState: failed to read OCC{INST} state! (last state: {PRIOR})",
574 "INST", instance, "PRIOR", lg2::hex, lastState);
Chris Cainf0295f52024-09-12 15:41:14 -0500575
Sheldon Bailey373af752022-02-21 15:14:00 -0600576 // State could not be determined, set it to NO State.
577 lastState = 0;
578
579 // Disable the ability to send Failed actions until OCC is
580 // Active again.
581 stateValid = false;
582
Chris Cainf0295f52024-09-12 15:41:14 -0500583 // Disable due to OCC comm failure and reset to try recovering
Chris Cain3ece5b92025-01-10 16:06:31 -0600584 // (processor callout will be added)
585 deviceError(Error::Descriptor(OCC_COMM_ERROR_PATH, ECOMM,
586 procPath.c_str(), true));
Chris Cainf0295f52024-09-12 15:41:14 -0500587
588 // Reset retry count (for next attempt after recovery)
589 currentOccReadRetriesCount = occReadRetries;
590 }
591 }
Chris Cainf7881502025-04-16 14:48:30 -0500592 else if (lastOccReadStatus != 0)
Chris Cainf0295f52024-09-12 15:41:14 -0500593 {
Chris Cainf7881502025-04-16 14:48:30 -0500594 lg2::info("readOccState: successfully read OCC{INST} state: {STATE}",
595 "INST", instance, "STATE", state);
596 lastOccReadStatus = 0; // no error
Sheldon Bailey373af752022-02-21 15:14:00 -0600597 }
598}
599
Chris Cainc86d80f2023-05-04 15:49:18 -0500600// Update processor throttle status on dbus
601void Status::updateThrottle(const bool isThrottled, const uint8_t newReason)
602{
603 if (!throttleHandle)
604 {
605 return;
606 }
607
608 uint8_t newThrottleCause = throttleCause;
609
610 if (isThrottled) // throttled due to newReason
611 {
612 if ((newReason & throttleCause) == 0)
613 {
614 // set the bit(s) for passed in reason
615 newThrottleCause |= newReason;
616 }
617 // else no change
618 }
619 else // no longer throttled due to newReason
620 {
621 if ((newReason & throttleCause) != 0)
622 {
623 // clear the bit(s) for passed in reason
624 newThrottleCause &= ~newReason;
625 }
626 // else no change
627 }
628
629 if (newThrottleCause != throttleCause)
630 {
631 if (newThrottleCause == THROTTLED_NONE)
632 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500633 lg2::debug(
634 "updateThrottle: OCC{INST} no longer throttled (prior reason: {REASON})",
635 "INST", instance, "REASON", throttleCause);
Chris Cainc86d80f2023-05-04 15:49:18 -0500636 throttleCause = THROTTLED_NONE;
637 throttleHandle->throttled(false);
638 throttleHandle->throttleCauses({});
639 }
640 else
641 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500642 lg2::debug(
643 "updateThrottle: OCC{INST} is throttled with reason {REASON} (prior reason: {PRIOR})",
644 "INST", instance, "REASON", newThrottleCause, "PRIOR",
645 throttleCause);
Chris Cainc86d80f2023-05-04 15:49:18 -0500646 throttleCause = newThrottleCause;
647
648 std::vector<ThrottleObj::ThrottleReasons> updatedCauses;
649 if (throttleCause & THROTTLED_POWER)
650 {
651 updatedCauses.push_back(
652 throttleHandle->ThrottleReasons::PowerLimit);
653 }
654 if (throttleCause & THROTTLED_THERMAL)
655 {
656 updatedCauses.push_back(
657 throttleHandle->ThrottleReasons::ThermalLimit);
658 }
659 if (throttleCause & THROTTLED_SAFE)
660 {
661 updatedCauses.push_back(
662 throttleHandle->ThrottleReasons::ManagementDetectedFault);
663 }
664 throttleHandle->throttleCauses(updatedCauses);
665 throttleHandle->throttled(true);
666 }
667 }
668 // else no change to throttle status
669}
670
671// Get processor path associated with this OCC
672void Status::readProcAssociation()
673{
674 std::string managingPath = path + "/power_managing";
Chris Cain37abe9b2024-10-31 17:20:31 -0500675 lg2::debug("readProcAssociation: getting endpoints for {MANAGE} ({PATH})",
676 "MANAGE", managingPath, "PATH", path);
Chris Cainc86d80f2023-05-04 15:49:18 -0500677 try
678 {
679 utils::PropertyValue procPathProperty{};
680 procPathProperty = utils::getProperty(
681 managingPath, "xyz.openbmc_project.Association", "endpoints");
682 auto result = std::get<std::vector<std::string>>(procPathProperty);
683 if (result.size() > 0)
684 {
685 procPath = result[0];
Chris Cain37abe9b2024-10-31 17:20:31 -0500686 lg2::info("readProcAssociation: OCC{INST} has proc={PATH}", "INST",
687 instance, "PATH", procPath);
Chris Cainc86d80f2023-05-04 15:49:18 -0500688 }
689 else
690 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500691 lg2::error(
692 "readProcAssociation: No processor associated with OCC{INST} / {PATH}",
693 "INST", instance, "PATH", path);
Chris Cainc86d80f2023-05-04 15:49:18 -0500694 }
695 }
696 catch (const sdbusplus::exception_t& e)
697 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500698 lg2::error(
699 "readProcAssociation: Unable to get proc assocated with {PATH} - {ERROR}",
700 "PATH", path, "ERROR", e.what());
Chris Cainc86d80f2023-05-04 15:49:18 -0500701 procPath = {};
702 }
703}
704
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530705} // namespace occ
706} // namespace open_power