blob: c664d1157f458c5230c30bb2d377d1e606f13ed7 [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 Cainc86d80f2023-05-04 15:49:18 -050031 // Clear prior throttle reason (before setting device active)
32 updateThrottle(false, THROTTLED_ALL);
33
Eddie Jamesaced3092022-04-22 16:19:30 -050034 // Set the device active
35 device.setActive(true);
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053036
Chris Caina8857c52021-01-27 11:53:05 -060037 // Reset last OCC state
38 lastState = 0;
39
Chris Cainf0295f52024-09-12 15:41:14 -050040 // Start watching for errors (throttles, etc)
41 try
42 {
43 addErrorWatch();
44 }
45 catch (const OpenFailure& e)
46 {
47 // Failed to add watch for throttle events, request reset to try
48 // to recover comm
Chris Cain37abe9b2024-10-31 17:20:31 -050049 lg2::error(
50 "Status::occActive: Unable to add error watch(s) for OCC{INST} watch: {ERROR}",
51 "INST", instance, "ERROR", e.what());
Chris Cainf0295f52024-09-12 15:41:14 -050052 deviceError(Error::Descriptor(OCC_COMM_ERROR_PATH));
53 return Base::Status::occActive(false);
54 }
55
56 // Update the OCC active sensor
57 Base::Status::occActive(value);
58
Chris Cain5d66a0a2022-02-09 08:52:10 -060059 if (device.master())
60 {
Chris Cain5d66a0a2022-02-09 08:52:10 -060061 // Update powercap bounds from OCC
Chris Cain40501a22022-03-14 17:33:27 -050062 manager.updatePcapBounds();
Chris Cain5d66a0a2022-02-09 08:52:10 -060063 }
64
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +053065 // Call into Manager to let know that we have bound
Chris Cain1be43372021-12-09 19:29:37 -060066 if (this->managerCallBack)
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +053067 {
Sheldon Bailey373af752022-02-21 15:14:00 -060068 this->managerCallBack(instance, value);
Edward A. James9fd2bdc2017-11-08 16:18:57 -060069 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053070 }
71 else
72 {
Chris Caina7b74dc2021-11-10 17:03:43 -060073#ifdef POWER10
Chris Cain1be43372021-12-09 19:29:37 -060074 if (pmode && device.master())
Chris Cain36f9cde2021-11-22 11:18:21 -060075 {
76 // Prevent mode changes
77 pmode->setMasterActive(false);
78 }
Chris Caina7b74dc2021-11-10 17:03:43 -060079 if (safeStateDelayTimer.isEnabled())
80 {
81 // stop safe delay timer
82 safeStateDelayTimer.setEnabled(false);
83 }
84#endif
Chris Cain36f9cde2021-11-22 11:18:21 -060085 // Call into Manager to let know that we will unbind.
Chris Cain1be43372021-12-09 19:29:37 -060086 if (this->managerCallBack)
Chris Cain36f9cde2021-11-22 11:18:21 -060087 {
Sheldon Bailey373af752022-02-21 15:14:00 -060088 this->managerCallBack(instance, value);
Chris Cain36f9cde2021-11-22 11:18:21 -060089 }
90
Edward A. James9fd2bdc2017-11-08 16:18:57 -060091 // Stop watching for errors
92 removeErrorWatch();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053093
Eddie Jamesaced3092022-04-22 16:19:30 -050094 // Set the device inactive
95 device.setActive(false);
Chris Cainc86d80f2023-05-04 15:49:18 -050096
97 // Clear throttles (OCC not active after disabling device)
98 updateThrottle(false, THROTTLED_ALL);
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053099 }
100 }
Eddie Jamesaced3092022-04-22 16:19:30 -0500101 else if (value && !device.active())
Edward A. James5e177972017-10-25 15:50:31 -0500102 {
103 // Existing error watch is on a dead file descriptor.
Edward A. James9fd2bdc2017-11-08 16:18:57 -0600104 removeErrorWatch();
Edward A. James5e177972017-10-25 15:50:31 -0500105
106 /*
107 * In it's constructor, Status checks Device::bound() to see if OCC is
108 * active or not.
109 * Device::bound() checks for occX-dev0 directory.
110 * We will lose occX-dev0 directories during FSI rescan.
111 * So, if we start this application (and construct Status), and then
112 * later do FSI rescan, we will end up with occActive = true and device
113 * NOT bound. Lets correct that situation here.
114 */
Eddie Jamesaced3092022-04-22 16:19:30 -0500115 device.setActive(true);
Edward A. James5e177972017-10-25 15:50:31 -0500116
117 // Add error watch again
Chris Cainf0295f52024-09-12 15:41:14 -0500118 try
119 {
120 addErrorWatch();
121 }
122 catch (const OpenFailure& e)
123 {
124 // Failed to add watch for throttle events, request reset to try to
125 // recover comm
Chris Cain37abe9b2024-10-31 17:20:31 -0500126 lg2::error(
127 "Status::occActive: Unable to add error watch(s) again for OCC{INST} watch: {ERROR}",
128 "INST", instance, "ERROR", e.what());
Chris Cainf0295f52024-09-12 15:41:14 -0500129 deviceError(Error::Descriptor(OCC_COMM_ERROR_PATH));
130 return Base::Status::occActive(false);
131 }
Edward A. James5e177972017-10-25 15:50:31 -0500132 }
Eddie Jamesaced3092022-04-22 16:19:30 -0500133 else if (!value && device.active())
Eddie James6d6d1b32019-04-22 10:45:08 -0500134 {
135 removeErrorWatch();
136
137 // In the event that the application never receives the active signal
138 // even though the OCC is active (this can occur if the BMC is rebooted
139 // with the host on, since the initial OCC driver probe will discover
140 // the OCCs), this application needs to be able to unbind the device
141 // when we get the OCC inactive signal.
Eddie Jamesaced3092022-04-22 16:19:30 -0500142 device.setActive(false);
Eddie James6d6d1b32019-04-22 10:45:08 -0500143 }
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530144 return Base::Status::occActive(value);
145}
146
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530147// Callback handler when a device error is reported.
Eddie James9789e712022-05-25 15:43:40 -0500148void Status::deviceError(Error::Descriptor d)
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530149{
Chris Cain36f9cde2021-11-22 11:18:21 -0600150#ifdef POWER10
Chris Cain1be43372021-12-09 19:29:37 -0600151 if (pmode && device.master())
152 {
153 // Prevent mode changes
154 pmode->setMasterActive(false);
155 }
Chris Cain36f9cde2021-11-22 11:18:21 -0600156#endif
157
Eddie James9789e712022-05-25 15:43:40 -0500158 if (d.log)
159 {
160 FFDC::createOCCResetPEL(instance, d.path, d.err, d.callout);
161 }
162
Eddie Jamescbad2192021-10-07 09:39:39 -0500163 // This would deem OCC inactive
164 this->occActive(false);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530165
Eddie Jamescbad2192021-10-07 09:39:39 -0500166 // Reset the OCC
167 this->resetOCC();
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530168}
169
170// Sends message to host control command handler to reset OCC
171void Status::resetOCC()
172{
Chris Cain37abe9b2024-10-31 17:20:31 -0500173 lg2::info(">>Status::resetOCC() - requesting reset for OCC{INST}", "INST",
174 instance);
Chris Cainf0295f52024-09-12 15:41:14 -0500175 this->occActive(false);
Tom Joseph00325232020-07-29 17:51:48 +0530176#ifdef PLDM
177 if (resetCallBack)
178 {
179 this->resetCallBack(instance);
180 }
181#else
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530182 constexpr auto CONTROL_HOST_PATH = "/org/open_power/control/host0";
183 constexpr auto CONTROL_HOST_INTF = "org.open_power.Control.Host";
184
185 // This will throw exception on failure
George Liuf3b75142021-06-10 11:22:50 +0800186 auto service = utils::getService(CONTROL_HOST_PATH, CONTROL_HOST_INTF);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530187
George Liuf3b75142021-06-10 11:22:50 +0800188 auto& bus = utils::getBus();
Gunnar Mills94df8c92018-09-14 14:50:03 -0500189 auto method = bus.new_method_call(service.c_str(), CONTROL_HOST_PATH,
190 CONTROL_HOST_INTF, "Execute");
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530191 // OCC Reset control command
Gunnar Mills94df8c92018-09-14 14:50:03 -0500192 method.append(convertForMessage(Control::Host::Command::OCCReset).c_str());
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530193
194 // OCC Sensor ID for callout reasons
Patrick Williamse0962702020-05-13 17:50:22 -0500195 method.append(std::variant<uint8_t>(std::get<0>(sensorMap.at(instance))));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530196 bus.call_noreply(method);
197 return;
Tom Joseph00325232020-07-29 17:51:48 +0530198#endif
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530199}
200
201// Handler called by Host control command handler to convey the
202// status of the executed command
Patrick Williamsaf408082022-07-22 19:26:54 -0500203void Status::hostControlEvent(sdbusplus::message_t& msg)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530204{
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530205 std::string cmdCompleted{};
206 std::string cmdStatus{};
207
208 msg.read(cmdCompleted, cmdStatus);
209
Chris Cain37abe9b2024-10-31 17:20:31 -0500210 lg2::debug("Host control signal values: command={CMD}, status={STATUS}",
211 "CMD", cmdCompleted, "STATUS", cmdStatus);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530212
Gunnar Mills94df8c92018-09-14 14:50:03 -0500213 if (Control::Host::convertResultFromString(cmdStatus) !=
214 Control::Host::Result::Success)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530215 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500216 if (Control::Host::convertCommandFromString(cmdCompleted) ==
217 Control::Host::Command::OCCReset)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530218 {
Gunnar Mills85e65202018-04-08 15:01:54 -0500219 // Must be a Timeout. Log an Error trace
Chris Cain37abe9b2024-10-31 17:20:31 -0500220 lg2::error(
221 "Error resetting the OCC: path={PATH}, sensorid={SENSOR}",
222 "PATH", path, "SENSOR", std::get<0>(sensorMap.at(instance)));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530223 }
224 }
225 return;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530226}
227
Sheldon Bailey373af752022-02-21 15:14:00 -0600228// Called from Manager::pollerTimerExpired() in preperation to POLL OCC.
Chris Caina8857c52021-01-27 11:53:05 -0600229void Status::readOccState()
230{
Chris Cainf0295f52024-09-12 15:41:14 -0500231 if (stateValid)
232 {
233 // Reset retry count (since state is good)
234 currentOccReadRetriesCount = occReadRetries;
235 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600236 occReadStateNow();
Chris Caina8857c52021-01-27 11:53:05 -0600237}
238
Chris Cain78e86012021-03-04 16:15:31 -0600239#ifdef POWER10
Chris Cain78e86012021-03-04 16:15:31 -0600240// Special processing that needs to happen once the OCCs change to ACTIVE state
241void Status::occsWentActive()
242{
243 CmdStatus status = CmdStatus::SUCCESS;
244
Chris Cain1fe436d2024-10-10 09:41:03 -0500245 // IPS data will get sent automatically after a mode change if the mode
246 // supports it.
247 pmode->needToSendIPS();
248
Chris Cain36f9cde2021-11-22 11:18:21 -0600249 status = pmode->sendModeChange();
Chris Cain78e86012021-03-04 16:15:31 -0600250 if (status != CmdStatus::SUCCESS)
251 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500252 lg2::error(
253 "Status::occsWentActive: OCC mode change failed with status {STATUS}",
254 "STATUS", status);
Chris Cainc567dc82022-04-01 15:09:17 -0500255
256 // Disable and reset to try recovering
257 deviceError();
Chris Cain78e86012021-03-04 16:15:31 -0600258 }
Chris Cain78e86012021-03-04 16:15:31 -0600259}
260
Chris Cain17257672021-10-22 13:41:03 -0500261// Send Ambient and Altitude to the OCC
262CmdStatus Status::sendAmbient(const uint8_t inTemp, const uint16_t inAltitude)
263{
264 CmdStatus status = CmdStatus::FAILURE;
265 bool ambientValid = true;
266 uint8_t ambientTemp = inTemp;
267 uint16_t altitude = inAltitude;
268
269 if (ambientTemp == 0xFF)
270 {
271 // Get latest readings from manager
272 manager.getAmbientData(ambientValid, ambientTemp, altitude);
Chris Cain37abe9b2024-10-31 17:20:31 -0500273 lg2::debug(
274 "sendAmbient: valid: {VALID}, Ambient: {TEMP}C, altitude: {ALT}m",
275 "VALID", ambientValid, "TEMP", ambientTemp, "ALT", altitude);
Chris Cain17257672021-10-22 13:41:03 -0500276 }
277
278 std::vector<std::uint8_t> cmd, rsp;
279 cmd.reserve(11);
280 cmd.push_back(uint8_t(CmdType::SEND_AMBIENT));
281 cmd.push_back(0x00); // Data Length (2 bytes)
282 cmd.push_back(0x08); //
283 cmd.push_back(0x00); // Version
284 cmd.push_back(ambientValid ? 0 : 0xFF); // Ambient Status
285 cmd.push_back(ambientTemp); // Ambient Temperature
286 cmd.push_back(altitude >> 8); // Altitude in meters (2 bytes)
287 cmd.push_back(altitude & 0xFF); //
288 cmd.push_back(0x00); // Reserved (3 bytes)
289 cmd.push_back(0x00);
290 cmd.push_back(0x00);
Chris Cain37abe9b2024-10-31 17:20:31 -0500291 lg2::debug("sendAmbient: SEND_AMBIENT "
292 "command to OCC{INST} ({SIZE} bytes)",
293 "INST", instance, "SIZE", cmd.size());
Chris Cain17257672021-10-22 13:41:03 -0500294 status = occCmd.send(cmd, rsp);
295 if (status == CmdStatus::SUCCESS)
296 {
297 if (rsp.size() == 5)
298 {
299 if (RspStatus::SUCCESS != RspStatus(rsp[2]))
300 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500301 lg2::error(
302 "sendAmbient: SEND_AMBIENT failed with rspStatus {STATUS}",
303 "STATUS", lg2::hex, rsp[2]);
Chris Cain17257672021-10-22 13:41:03 -0500304 dump_hex(rsp);
305 status = CmdStatus::FAILURE;
306 }
307 }
308 else
309 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500310 lg2::error(
311 "sendAmbient: INVALID SEND_AMBIENT response length:{SIZE}",
312 "SIZE", rsp.size());
Chris Cain17257672021-10-22 13:41:03 -0500313 dump_hex(rsp);
314 status = CmdStatus::FAILURE;
315 }
316 }
317 else
318 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500319 lg2::error("sendAmbient: SEND_AMBIENT FAILED! with status {STATUS}",
320 "STATUS", lg2::hex, uint8_t(status));
Chris Cainc567dc82022-04-01 15:09:17 -0500321
322 if (status == CmdStatus::COMM_FAILURE)
Chris Cain17257672021-10-22 13:41:03 -0500323 {
Chris Cainf0295f52024-09-12 15:41:14 -0500324 // Disable due to OCC comm failure and reset to try recovering
325 deviceError(Error::Descriptor(OCC_COMM_ERROR_PATH));
Chris Cain17257672021-10-22 13:41:03 -0500326 }
327 }
328
329 return status;
330}
Chris Caina7b74dc2021-11-10 17:03:43 -0600331
332// Called when safe timer expires to determine if OCCs need to be reset
333void Status::safeStateDelayExpired()
334{
335 if (this->occActive())
336 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500337 lg2::info(
338 "safeStateDelayExpired: OCC{INST} state missing or not valid, requesting reset",
339 "INST", instance);
Chris Caina7b74dc2021-11-10 17:03:43 -0600340 // Disable and reset to try recovering
Eddie James9789e712022-05-25 15:43:40 -0500341 deviceError(Error::Descriptor(SAFE_ERROR_PATH));
Chris Caina7b74dc2021-11-10 17:03:43 -0600342 }
343}
Chris Cain78e86012021-03-04 16:15:31 -0600344#endif // POWER10
345
Chris Caine2d0a432022-03-28 11:08:49 -0500346fs::path Status::getHwmonPath()
Chris Cain5d66a0a2022-02-09 08:52:10 -0600347{
348 using namespace std::literals::string_literals;
349
Chris Caine2d0a432022-03-28 11:08:49 -0500350 if (!fs::exists(hwmonPath))
351 {
352 static bool tracedFail[8] = {0};
Chris Cain5d66a0a2022-02-09 08:52:10 -0600353
Chris Caine2d0a432022-03-28 11:08:49 -0500354 if (!hwmonPath.empty())
355 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500356 lg2::warning(
357 "Status::getHwmonPath(): path no longer exists: {PATH}", "PATH",
358 hwmonPath);
Chris Caine2d0a432022-03-28 11:08:49 -0500359 hwmonPath.clear();
360 }
361
362 // Build the base HWMON path
Patrick Williamsd7542c82024-08-16 15:20:28 -0400363 fs::path prefixPath =
364 fs::path{OCC_HWMON_PATH + "occ-hwmon."s +
365 std::to_string(instance + 1) + "/hwmon/"s};
Chris Caine2d0a432022-03-28 11:08:49 -0500366
367 // Get the hwmonXX directory name
368 try
369 {
370 // there should only be one directory
371 const int numDirs = std::distance(
372 fs::directory_iterator(prefixPath), fs::directory_iterator{});
373 if (numDirs == 1)
374 {
375 hwmonPath = *fs::directory_iterator(prefixPath);
376 tracedFail[instance] = false;
377 }
378 else
379 {
380 if (!tracedFail[instance])
381 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500382 lg2::error(
383 "Status::getHwmonPath(): Found multiple ({NUM}) hwmon paths!",
384 "NUM", numDirs);
Chris Caine2d0a432022-03-28 11:08:49 -0500385 tracedFail[instance] = true;
386 }
387 }
388 }
389 catch (const fs::filesystem_error& e)
390 {
391 if (!tracedFail[instance])
392 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500393 lg2::error(
394 "Status::getHwmonPath(): error accessing {PATH}: {ERROR}",
395 "PATH", prefixPath, "ERROR", e.what());
Chris Caine2d0a432022-03-28 11:08:49 -0500396 tracedFail[instance] = true;
397 }
398 }
399 }
400
401 return hwmonPath;
Chris Cain5d66a0a2022-02-09 08:52:10 -0600402}
403
Chris Cainf0295f52024-09-12 15:41:14 -0500404// Called to read state and handle any errors
Sheldon Bailey373af752022-02-21 15:14:00 -0600405void Status::occReadStateNow()
406{
407 unsigned int state;
408 const fs::path filename =
409 fs::path(DEV_PATH) /
410 fs::path(sysfsName + "." + std::to_string(instance + 1)) / "occ_state";
411
412 std::ifstream file;
413 bool goodFile = false;
414
415 // open file.
416 file.open(filename, std::ios::in);
417 const int openErrno = errno;
418
419 // File is open and state can be used.
420 if (file.is_open() && file.good())
421 {
422 goodFile = true;
423 file >> state;
Chris Cainf0295f52024-09-12 15:41:14 -0500424 // Read the error code (if any) to check status of the read
425 std::ios_base::iostate readState = file.rdstate();
426 if (readState)
427 {
428 // There was a failure reading the file
429 if (lastOccReadStatus != -1)
430 {
431 // Trace error bits
432 std::string errorBits = "";
433 if (readState & std::ios_base::eofbit)
434 {
435 errorBits += " EOF";
436 }
437 if (readState & std::ios_base::failbit)
438 {
439 errorBits += " failbit";
440 }
441 if (readState & std::ios_base::badbit)
442 {
443 errorBits += " badbit";
444 }
Chris Cain37abe9b2024-10-31 17:20:31 -0500445 lg2::error(
446 "readOccState: Failed to read OCC{INST} state: Read error on I/O operation - {ERROR}",
447 "INST", instance, "ERROR", errorBits);
Chris Cainf0295f52024-09-12 15:41:14 -0500448 lastOccReadStatus = -1;
449 }
450 goodFile = false;
451 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600452
Chris Cainf0295f52024-09-12 15:41:14 -0500453 if (goodFile && (state != lastState))
Sheldon Bailey373af752022-02-21 15:14:00 -0600454 {
455 // Trace OCC state changes
Chris Cain37abe9b2024-10-31 17:20:31 -0500456 lg2::info(
457 "Status::readOccState: OCC{INST} state {STATE} (lastState: {PRIOR})",
458 "INST", instance, "STATE", lg2::hex, state, "PRIOR", lg2::hex,
459 lastState);
Sheldon Bailey373af752022-02-21 15:14:00 -0600460 lastState = state;
461#ifdef POWER10
462 if (OccState(state) == OccState::ACTIVE)
463 {
464 if (pmode && device.master())
465 {
466 // Set the master OCC on the PowerMode object
467 pmode->setMasterOcc(path);
468 // Enable mode changes
469 pmode->setMasterActive();
470
471 // Special processing by master OCC when it goes active
472 occsWentActive();
473 }
474
475 CmdStatus status = sendAmbient();
476 if (status != CmdStatus::SUCCESS)
477 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500478 lg2::error(
479 "readOccState: Sending Ambient failed with status {STATUS}",
480 "STATUS", status);
Sheldon Bailey373af752022-02-21 15:14:00 -0600481 }
482 }
483
484 // If OCC in known Good State.
485 if ((OccState(state) == OccState::ACTIVE) ||
486 (OccState(state) == OccState::CHARACTERIZATION) ||
487 (OccState(state) == OccState::OBSERVATION))
488 {
489 // Good OCC State then sensors valid again
490 stateValid = true;
491
492 if (safeStateDelayTimer.isEnabled())
493 {
494 // stop safe delay timer (no longer in SAFE state)
495 safeStateDelayTimer.setEnabled(false);
496 }
497 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600498 else
499 {
Chris Cainf0295f52024-09-12 15:41:14 -0500500 // OCC is in SAFE or some other unsupported state
Sheldon Bailey373af752022-02-21 15:14:00 -0600501 if (!safeStateDelayTimer.isEnabled())
502 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500503 lg2::error(
504 "readOccState: Invalid OCC{INST} state of {STATE}, starting safe state delay timer",
505 "INST", instance, "STATE", state);
Sheldon Bailey373af752022-02-21 15:14:00 -0600506 // start safe delay timer (before requesting reset)
507 using namespace std::literals::chrono_literals;
508 safeStateDelayTimer.restartOnce(60s);
509 }
Chris Cainf0295f52024-09-12 15:41:14 -0500510 // Not a supported state (update sensors to NaN and not
511 // functional)
Sheldon Bailey373af752022-02-21 15:14:00 -0600512 stateValid = false;
513 }
514#else
515 // Before P10 state not checked, only used good file open.
516 stateValid = true;
517#endif
518 }
519 }
Chris Cainf0295f52024-09-12 15:41:14 -0500520#ifdef POWER10
521 else
522 {
523 // Unable to read state
524 stateValid = false;
525 }
526#endif
Sheldon Bailey373af752022-02-21 15:14:00 -0600527 file.close();
528
529 // if failed to Read a state or not a valid state -> Attempt retry
530 // after 1 Second delay if allowed.
531 if ((!goodFile) || (!stateValid))
532 {
533 if (!goodFile)
534 {
535 // If not able to read, OCC may be offline
Chris Cainf0295f52024-09-12 15:41:14 -0500536 if (openErrno != lastOccReadStatus)
537 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500538 lg2::error(
539 "Status::readOccState: open/read failed trying to read OCC{INST} state (open errno={ERROR})",
540 "INST", instance, "ERROR", openErrno);
Chris Cainf0295f52024-09-12 15:41:14 -0500541 lastOccReadStatus = openErrno;
542 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600543 }
544 else
545 {
546 // else this failed due to state not valid.
Chris Cainbd551de2022-04-26 13:41:16 -0500547 if (state != lastState)
548 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500549 lg2::error(
550 "Status::readOccState: OCC{INST} Invalid state {STATE} (last state: {PRIOR})",
551 "INST", instance, "STATE", lg2::hex, state, "PRIOR",
552 lg2::hex, lastState);
Chris Cainbd551de2022-04-26 13:41:16 -0500553 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600554 }
555
556#ifdef READ_OCC_SENSORS
Sheldon Baileyc8dd4592022-05-12 10:15:14 -0500557 manager.setSensorValueToNaN(instance);
Sheldon Bailey373af752022-02-21 15:14:00 -0600558#endif
559
560 // See occReadRetries for number of retry attempts.
561 if (currentOccReadRetriesCount > 0)
562 {
563 --currentOccReadRetriesCount;
Sheldon Bailey373af752022-02-21 15:14:00 -0600564 }
565 else
566 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500567 lg2::error("readOccState: failed to read OCC{INST} state!", "INST",
568 instance);
Chris Cainf0295f52024-09-12 15:41:14 -0500569
Sheldon Bailey373af752022-02-21 15:14:00 -0600570 // State could not be determined, set it to NO State.
571 lastState = 0;
572
573 // Disable the ability to send Failed actions until OCC is
574 // Active again.
575 stateValid = false;
576
Chris Cainf0295f52024-09-12 15:41:14 -0500577 // Disable due to OCC comm failure and reset to try recovering
578 deviceError(Error::Descriptor(OCC_COMM_ERROR_PATH));
579
580 // Reset retry count (for next attempt after recovery)
581 currentOccReadRetriesCount = occReadRetries;
582 }
583 }
584 else
585 {
586 if (lastOccReadStatus != 0)
587 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500588 lg2::info(
589 "Status::readOccState: successfully read OCC{INST} state: {STATE}",
590 "INST", instance, "STATE", state);
Chris Cainf0295f52024-09-12 15:41:14 -0500591 lastOccReadStatus = 0; // no error
Sheldon Bailey373af752022-02-21 15:14:00 -0600592 }
593 }
594}
595
Chris Cainc86d80f2023-05-04 15:49:18 -0500596// Update processor throttle status on dbus
597void Status::updateThrottle(const bool isThrottled, const uint8_t newReason)
598{
599 if (!throttleHandle)
600 {
601 return;
602 }
603
604 uint8_t newThrottleCause = throttleCause;
605
606 if (isThrottled) // throttled due to newReason
607 {
608 if ((newReason & throttleCause) == 0)
609 {
610 // set the bit(s) for passed in reason
611 newThrottleCause |= newReason;
612 }
613 // else no change
614 }
615 else // no longer throttled due to newReason
616 {
617 if ((newReason & throttleCause) != 0)
618 {
619 // clear the bit(s) for passed in reason
620 newThrottleCause &= ~newReason;
621 }
622 // else no change
623 }
624
625 if (newThrottleCause != throttleCause)
626 {
627 if (newThrottleCause == THROTTLED_NONE)
628 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500629 lg2::debug(
630 "updateThrottle: OCC{INST} no longer throttled (prior reason: {REASON})",
631 "INST", instance, "REASON", throttleCause);
Chris Cainc86d80f2023-05-04 15:49:18 -0500632 throttleCause = THROTTLED_NONE;
633 throttleHandle->throttled(false);
634 throttleHandle->throttleCauses({});
635 }
636 else
637 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500638 lg2::debug(
639 "updateThrottle: OCC{INST} is throttled with reason {REASON} (prior reason: {PRIOR})",
640 "INST", instance, "REASON", newThrottleCause, "PRIOR",
641 throttleCause);
Chris Cainc86d80f2023-05-04 15:49:18 -0500642 throttleCause = newThrottleCause;
643
644 std::vector<ThrottleObj::ThrottleReasons> updatedCauses;
645 if (throttleCause & THROTTLED_POWER)
646 {
647 updatedCauses.push_back(
648 throttleHandle->ThrottleReasons::PowerLimit);
649 }
650 if (throttleCause & THROTTLED_THERMAL)
651 {
652 updatedCauses.push_back(
653 throttleHandle->ThrottleReasons::ThermalLimit);
654 }
655 if (throttleCause & THROTTLED_SAFE)
656 {
657 updatedCauses.push_back(
658 throttleHandle->ThrottleReasons::ManagementDetectedFault);
659 }
660 throttleHandle->throttleCauses(updatedCauses);
661 throttleHandle->throttled(true);
662 }
663 }
664 // else no change to throttle status
665}
666
667// Get processor path associated with this OCC
668void Status::readProcAssociation()
669{
670 std::string managingPath = path + "/power_managing";
Chris Cain37abe9b2024-10-31 17:20:31 -0500671 lg2::debug("readProcAssociation: getting endpoints for {MANAGE} ({PATH})",
672 "MANAGE", managingPath, "PATH", path);
Chris Cainc86d80f2023-05-04 15:49:18 -0500673 try
674 {
675 utils::PropertyValue procPathProperty{};
676 procPathProperty = utils::getProperty(
677 managingPath, "xyz.openbmc_project.Association", "endpoints");
678 auto result = std::get<std::vector<std::string>>(procPathProperty);
679 if (result.size() > 0)
680 {
681 procPath = result[0];
Chris Cain37abe9b2024-10-31 17:20:31 -0500682 lg2::info("readProcAssociation: OCC{INST} has proc={PATH}", "INST",
683 instance, "PATH", procPath);
Chris Cainc86d80f2023-05-04 15:49:18 -0500684 }
685 else
686 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500687 lg2::error(
688 "readProcAssociation: No processor associated with OCC{INST} / {PATH}",
689 "INST", instance, "PATH", path);
Chris Cainc86d80f2023-05-04 15:49:18 -0500690 }
691 }
692 catch (const sdbusplus::exception_t& e)
693 {
Chris Cain37abe9b2024-10-31 17:20:31 -0500694 lg2::error(
695 "readProcAssociation: Unable to get proc assocated with {PATH} - {ERROR}",
696 "PATH", path, "ERROR", e.what());
Chris Cainc86d80f2023-05-04 15:49:18 -0500697 procPath = {};
698 }
699}
700
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530701} // namespace occ
702} // namespace open_power