blob: 4b6e2765737cf44d32a1a5cee1e9db215aaa2a1d [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
8#include <phosphor-logging/log.hpp>
Chris Cain78e86012021-03-04 16:15:31 -06009
Chris Caine2d0a432022-03-28 11:08:49 -050010#include <filesystem>
Patrick Williams48002492024-02-13 21:43:32 -060011#include <format>
Chris Caine2d0a432022-03-28 11:08:49 -050012
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053013namespace open_power
14{
15namespace occ
16{
Chris Cain78e86012021-03-04 16:15:31 -060017
Chris Caina8857c52021-01-27 11:53:05 -060018using namespace phosphor::logging;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053019
Chris Cainc86d80f2023-05-04 15:49:18 -050020using ThrottleObj =
21 sdbusplus::xyz::openbmc_project::Control::Power::server::Throttle;
22
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053023// Handles updates to occActive property
24bool Status::occActive(bool value)
25{
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053026 if (value != this->occActive())
27 {
Patrick Williams48002492024-02-13 21:43:32 -060028 log<level::INFO>(std::format("Status::occActive OCC{} changed to {}",
Chris Caina8857c52021-01-27 11:53:05 -060029 instance, value)
30 .c_str());
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053031 if (value)
32 {
Chris Cainc86d80f2023-05-04 15:49:18 -050033 // Clear prior throttle reason (before setting device active)
34 updateThrottle(false, THROTTLED_ALL);
35
Eddie Jamesaced3092022-04-22 16:19:30 -050036 // Set the device active
37 device.setActive(true);
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053038
Chris Caina8857c52021-01-27 11:53:05 -060039 // Reset last OCC state
40 lastState = 0;
41
Chris Cainf0295f52024-09-12 15:41:14 -050042 // Start watching for errors (throttles, etc)
43 try
44 {
45 addErrorWatch();
46 }
47 catch (const OpenFailure& e)
48 {
49 // Failed to add watch for throttle events, request reset to try
50 // to recover comm
51 log<level::ERR>(
52 std::format(
53 "Status::occActive: Unable to add error watch(s) for OCC{} watch: {}",
54 instance, e.what())
55 .c_str());
56 deviceError(Error::Descriptor(OCC_COMM_ERROR_PATH));
57 return Base::Status::occActive(false);
58 }
59
60 // Update the OCC active sensor
61 Base::Status::occActive(value);
62
Chris Cain5d66a0a2022-02-09 08:52:10 -060063 if (device.master())
64 {
Chris Cain5d66a0a2022-02-09 08:52:10 -060065 // Update powercap bounds from OCC
Chris Cain40501a22022-03-14 17:33:27 -050066 manager.updatePcapBounds();
Chris Cain5d66a0a2022-02-09 08:52:10 -060067 }
68
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +053069 // Call into Manager to let know that we have bound
Chris Cain1be43372021-12-09 19:29:37 -060070 if (this->managerCallBack)
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +053071 {
Sheldon Bailey373af752022-02-21 15:14:00 -060072 this->managerCallBack(instance, value);
Edward A. James9fd2bdc2017-11-08 16:18:57 -060073 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053074 }
75 else
76 {
Chris Caina7b74dc2021-11-10 17:03:43 -060077#ifdef POWER10
Chris Cain1be43372021-12-09 19:29:37 -060078 if (pmode && device.master())
Chris Cain36f9cde2021-11-22 11:18:21 -060079 {
80 // Prevent mode changes
81 pmode->setMasterActive(false);
82 }
Chris Caina7b74dc2021-11-10 17:03:43 -060083 if (safeStateDelayTimer.isEnabled())
84 {
85 // stop safe delay timer
86 safeStateDelayTimer.setEnabled(false);
87 }
88#endif
Chris Cain36f9cde2021-11-22 11:18:21 -060089 // Call into Manager to let know that we will unbind.
Chris Cain1be43372021-12-09 19:29:37 -060090 if (this->managerCallBack)
Chris Cain36f9cde2021-11-22 11:18:21 -060091 {
Sheldon Bailey373af752022-02-21 15:14:00 -060092 this->managerCallBack(instance, value);
Chris Cain36f9cde2021-11-22 11:18:21 -060093 }
94
Edward A. James9fd2bdc2017-11-08 16:18:57 -060095 // Stop watching for errors
96 removeErrorWatch();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053097
Eddie Jamesaced3092022-04-22 16:19:30 -050098 // Set the device inactive
99 device.setActive(false);
Chris Cainc86d80f2023-05-04 15:49:18 -0500100
101 // Clear throttles (OCC not active after disabling device)
102 updateThrottle(false, THROTTLED_ALL);
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530103 }
104 }
Eddie Jamesaced3092022-04-22 16:19:30 -0500105 else if (value && !device.active())
Edward A. James5e177972017-10-25 15:50:31 -0500106 {
107 // Existing error watch is on a dead file descriptor.
Edward A. James9fd2bdc2017-11-08 16:18:57 -0600108 removeErrorWatch();
Edward A. James5e177972017-10-25 15:50:31 -0500109
110 /*
111 * In it's constructor, Status checks Device::bound() to see if OCC is
112 * active or not.
113 * Device::bound() checks for occX-dev0 directory.
114 * We will lose occX-dev0 directories during FSI rescan.
115 * So, if we start this application (and construct Status), and then
116 * later do FSI rescan, we will end up with occActive = true and device
117 * NOT bound. Lets correct that situation here.
118 */
Eddie Jamesaced3092022-04-22 16:19:30 -0500119 device.setActive(true);
Edward A. James5e177972017-10-25 15:50:31 -0500120
121 // Add error watch again
Chris Cainf0295f52024-09-12 15:41:14 -0500122 try
123 {
124 addErrorWatch();
125 }
126 catch (const OpenFailure& e)
127 {
128 // Failed to add watch for throttle events, request reset to try to
129 // recover comm
130 log<level::ERR>(
131 std::format(
132 "Status::occActive: Unable to add error watch(s) again for OCC{} watch: {}",
133 instance, e.what())
134 .c_str());
135 deviceError(Error::Descriptor(OCC_COMM_ERROR_PATH));
136 return Base::Status::occActive(false);
137 }
Edward A. James5e177972017-10-25 15:50:31 -0500138 }
Eddie Jamesaced3092022-04-22 16:19:30 -0500139 else if (!value && device.active())
Eddie James6d6d1b32019-04-22 10:45:08 -0500140 {
141 removeErrorWatch();
142
143 // In the event that the application never receives the active signal
144 // even though the OCC is active (this can occur if the BMC is rebooted
145 // with the host on, since the initial OCC driver probe will discover
146 // the OCCs), this application needs to be able to unbind the device
147 // when we get the OCC inactive signal.
Eddie Jamesaced3092022-04-22 16:19:30 -0500148 device.setActive(false);
Eddie James6d6d1b32019-04-22 10:45:08 -0500149 }
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530150 return Base::Status::occActive(value);
151}
152
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530153// Callback handler when a device error is reported.
Eddie James9789e712022-05-25 15:43:40 -0500154void Status::deviceError(Error::Descriptor d)
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530155{
Chris Cain36f9cde2021-11-22 11:18:21 -0600156#ifdef POWER10
Chris Cain1be43372021-12-09 19:29:37 -0600157 if (pmode && device.master())
158 {
159 // Prevent mode changes
160 pmode->setMasterActive(false);
161 }
Chris Cain36f9cde2021-11-22 11:18:21 -0600162#endif
163
Eddie James9789e712022-05-25 15:43:40 -0500164 if (d.log)
165 {
166 FFDC::createOCCResetPEL(instance, d.path, d.err, d.callout);
167 }
168
Eddie Jamescbad2192021-10-07 09:39:39 -0500169 // This would deem OCC inactive
170 this->occActive(false);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530171
Eddie Jamescbad2192021-10-07 09:39:39 -0500172 // Reset the OCC
173 this->resetOCC();
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530174}
175
176// Sends message to host control command handler to reset OCC
177void Status::resetOCC()
178{
Chris Caina8857c52021-01-27 11:53:05 -0600179 log<level::INFO>(
Patrick Williams48002492024-02-13 21:43:32 -0600180 std::format(">>Status::resetOCC() - requesting reset for OCC{}",
Chris Caina8857c52021-01-27 11:53:05 -0600181 instance)
182 .c_str());
Chris Cainf0295f52024-09-12 15:41:14 -0500183 this->occActive(false);
Tom Joseph00325232020-07-29 17:51:48 +0530184#ifdef PLDM
185 if (resetCallBack)
186 {
187 this->resetCallBack(instance);
188 }
189#else
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530190 constexpr auto CONTROL_HOST_PATH = "/org/open_power/control/host0";
191 constexpr auto CONTROL_HOST_INTF = "org.open_power.Control.Host";
192
193 // This will throw exception on failure
George Liuf3b75142021-06-10 11:22:50 +0800194 auto service = utils::getService(CONTROL_HOST_PATH, CONTROL_HOST_INTF);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530195
George Liuf3b75142021-06-10 11:22:50 +0800196 auto& bus = utils::getBus();
Gunnar Mills94df8c92018-09-14 14:50:03 -0500197 auto method = bus.new_method_call(service.c_str(), CONTROL_HOST_PATH,
198 CONTROL_HOST_INTF, "Execute");
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530199 // OCC Reset control command
Gunnar Mills94df8c92018-09-14 14:50:03 -0500200 method.append(convertForMessage(Control::Host::Command::OCCReset).c_str());
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530201
202 // OCC Sensor ID for callout reasons
Patrick Williamse0962702020-05-13 17:50:22 -0500203 method.append(std::variant<uint8_t>(std::get<0>(sensorMap.at(instance))));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530204 bus.call_noreply(method);
205 return;
Tom Joseph00325232020-07-29 17:51:48 +0530206#endif
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530207}
208
209// Handler called by Host control command handler to convey the
210// status of the executed command
Patrick Williamsaf408082022-07-22 19:26:54 -0500211void Status::hostControlEvent(sdbusplus::message_t& msg)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530212{
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530213 std::string cmdCompleted{};
214 std::string cmdStatus{};
215
216 msg.read(cmdCompleted, cmdStatus);
217
218 log<level::DEBUG>("Host control signal values",
Gunnar Mills94df8c92018-09-14 14:50:03 -0500219 entry("COMMAND=%s", cmdCompleted.c_str()),
220 entry("STATUS=%s", cmdStatus.c_str()));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530221
Gunnar Mills94df8c92018-09-14 14:50:03 -0500222 if (Control::Host::convertResultFromString(cmdStatus) !=
223 Control::Host::Result::Success)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530224 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500225 if (Control::Host::convertCommandFromString(cmdCompleted) ==
226 Control::Host::Command::OCCReset)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530227 {
Gunnar Mills85e65202018-04-08 15:01:54 -0500228 // Must be a Timeout. Log an Error trace
Alexander Filippov1d69e192019-03-21 18:12:07 +0300229 log<level::ERR>(
230 "Error resetting the OCC.", entry("PATH=%s", path.c_str()),
231 entry("SENSORID=0x%X", std::get<0>(sensorMap.at(instance))));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530232 }
233 }
234 return;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530235}
236
Sheldon Bailey373af752022-02-21 15:14:00 -0600237// Called from Manager::pollerTimerExpired() in preperation to POLL OCC.
Chris Caina8857c52021-01-27 11:53:05 -0600238void Status::readOccState()
239{
Chris Cainf0295f52024-09-12 15:41:14 -0500240 if (stateValid)
241 {
242 // Reset retry count (since state is good)
243 currentOccReadRetriesCount = occReadRetries;
244 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600245 occReadStateNow();
Chris Caina8857c52021-01-27 11:53:05 -0600246}
247
Chris Cain78e86012021-03-04 16:15:31 -0600248#ifdef POWER10
Chris Cain78e86012021-03-04 16:15:31 -0600249// Special processing that needs to happen once the OCCs change to ACTIVE state
250void Status::occsWentActive()
251{
252 CmdStatus status = CmdStatus::SUCCESS;
253
Chris Cain1fe436d2024-10-10 09:41:03 -0500254 // IPS data will get sent automatically after a mode change if the mode
255 // supports it.
256 pmode->needToSendIPS();
257
Chris Cain36f9cde2021-11-22 11:18:21 -0600258 status = pmode->sendModeChange();
Chris Cain78e86012021-03-04 16:15:31 -0600259 if (status != CmdStatus::SUCCESS)
260 {
George Liub5ca1012021-09-10 12:53:11 +0800261 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600262 std::format(
George Liub5ca1012021-09-10 12:53:11 +0800263 "Status::occsWentActive: OCC mode change failed with status {}",
264 status)
265 .c_str());
Chris Cainc567dc82022-04-01 15:09:17 -0500266
267 // Disable and reset to try recovering
268 deviceError();
Chris Cain78e86012021-03-04 16:15:31 -0600269 }
Chris Cain78e86012021-03-04 16:15:31 -0600270}
271
Chris Cain17257672021-10-22 13:41:03 -0500272// Send Ambient and Altitude to the OCC
273CmdStatus Status::sendAmbient(const uint8_t inTemp, const uint16_t inAltitude)
274{
275 CmdStatus status = CmdStatus::FAILURE;
276 bool ambientValid = true;
277 uint8_t ambientTemp = inTemp;
278 uint16_t altitude = inAltitude;
279
280 if (ambientTemp == 0xFF)
281 {
282 // Get latest readings from manager
283 manager.getAmbientData(ambientValid, ambientTemp, altitude);
284 log<level::DEBUG>(
Patrick Williams48002492024-02-13 21:43:32 -0600285 std::format("sendAmbient: valid: {}, Ambient: {}C, altitude: {}m",
Chris Cain17257672021-10-22 13:41:03 -0500286 ambientValid, ambientTemp, altitude)
287 .c_str());
288 }
289
290 std::vector<std::uint8_t> cmd, rsp;
291 cmd.reserve(11);
292 cmd.push_back(uint8_t(CmdType::SEND_AMBIENT));
293 cmd.push_back(0x00); // Data Length (2 bytes)
294 cmd.push_back(0x08); //
295 cmd.push_back(0x00); // Version
296 cmd.push_back(ambientValid ? 0 : 0xFF); // Ambient Status
297 cmd.push_back(ambientTemp); // Ambient Temperature
298 cmd.push_back(altitude >> 8); // Altitude in meters (2 bytes)
299 cmd.push_back(altitude & 0xFF); //
300 cmd.push_back(0x00); // Reserved (3 bytes)
301 cmd.push_back(0x00);
302 cmd.push_back(0x00);
Patrick Williams48002492024-02-13 21:43:32 -0600303 log<level::DEBUG>(std::format("sendAmbient: SEND_AMBIENT "
Chris Cain17257672021-10-22 13:41:03 -0500304 "command to OCC{} ({} bytes)",
305 instance, cmd.size())
306 .c_str());
307 status = occCmd.send(cmd, rsp);
308 if (status == CmdStatus::SUCCESS)
309 {
310 if (rsp.size() == 5)
311 {
312 if (RspStatus::SUCCESS != RspStatus(rsp[2]))
313 {
314 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600315 std::format(
Chris Cainc567dc82022-04-01 15:09:17 -0500316 "sendAmbient: SEND_AMBIENT failed with rspStatus 0x{:02X}",
Chris Cain17257672021-10-22 13:41:03 -0500317 rsp[2])
318 .c_str());
319 dump_hex(rsp);
320 status = CmdStatus::FAILURE;
321 }
322 }
323 else
324 {
Chris Cainc567dc82022-04-01 15:09:17 -0500325 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600326 std::format(
Chris Cainc567dc82022-04-01 15:09:17 -0500327 "sendAmbient: INVALID SEND_AMBIENT response length:{}",
328 rsp.size())
329 .c_str());
Chris Cain17257672021-10-22 13:41:03 -0500330 dump_hex(rsp);
331 status = CmdStatus::FAILURE;
332 }
333 }
334 else
335 {
Chris Cainc567dc82022-04-01 15:09:17 -0500336 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600337 std::format(
Chris Cainc567dc82022-04-01 15:09:17 -0500338 "sendAmbient: SEND_AMBIENT FAILED! with status 0x{:02X}",
339 status)
340 .c_str());
341
342 if (status == CmdStatus::COMM_FAILURE)
Chris Cain17257672021-10-22 13:41:03 -0500343 {
Chris Cainf0295f52024-09-12 15:41:14 -0500344 // Disable due to OCC comm failure and reset to try recovering
345 deviceError(Error::Descriptor(OCC_COMM_ERROR_PATH));
Chris Cain17257672021-10-22 13:41:03 -0500346 }
347 }
348
349 return status;
350}
Chris Caina7b74dc2021-11-10 17:03:43 -0600351
352// Called when safe timer expires to determine if OCCs need to be reset
353void Status::safeStateDelayExpired()
354{
355 if (this->occActive())
356 {
357 log<level::INFO>(
Patrick Williams48002492024-02-13 21:43:32 -0600358 std::format(
Chris Cainf0295f52024-09-12 15:41:14 -0500359 "safeStateDelayExpired: OCC{} state missing or not valid, requesting reset",
Chris Caina7b74dc2021-11-10 17:03:43 -0600360 instance)
361 .c_str());
362 // Disable and reset to try recovering
Eddie James9789e712022-05-25 15:43:40 -0500363 deviceError(Error::Descriptor(SAFE_ERROR_PATH));
Chris Caina7b74dc2021-11-10 17:03:43 -0600364 }
365}
Chris Cain78e86012021-03-04 16:15:31 -0600366#endif // POWER10
367
Chris Caine2d0a432022-03-28 11:08:49 -0500368fs::path Status::getHwmonPath()
Chris Cain5d66a0a2022-02-09 08:52:10 -0600369{
370 using namespace std::literals::string_literals;
371
Chris Caine2d0a432022-03-28 11:08:49 -0500372 if (!fs::exists(hwmonPath))
373 {
374 static bool tracedFail[8] = {0};
Chris Cain5d66a0a2022-02-09 08:52:10 -0600375
Chris Caine2d0a432022-03-28 11:08:49 -0500376 if (!hwmonPath.empty())
377 {
Chris Cainf0295f52024-09-12 15:41:14 -0500378 log<level::WARNING>(
Patrick Williams48002492024-02-13 21:43:32 -0600379 std::format("Status::getHwmonPath(): path no longer exists: {}",
Chris Caine2d0a432022-03-28 11:08:49 -0500380 hwmonPath.c_str())
381 .c_str());
382 hwmonPath.clear();
383 }
384
385 // Build the base HWMON path
Patrick Williamsd7542c82024-08-16 15:20:28 -0400386 fs::path prefixPath =
387 fs::path{OCC_HWMON_PATH + "occ-hwmon."s +
388 std::to_string(instance + 1) + "/hwmon/"s};
Chris Caine2d0a432022-03-28 11:08:49 -0500389
390 // Get the hwmonXX directory name
391 try
392 {
393 // there should only be one directory
394 const int numDirs = std::distance(
395 fs::directory_iterator(prefixPath), fs::directory_iterator{});
396 if (numDirs == 1)
397 {
398 hwmonPath = *fs::directory_iterator(prefixPath);
399 tracedFail[instance] = false;
400 }
401 else
402 {
403 if (!tracedFail[instance])
404 {
405 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600406 std::format(
Chris Caine2d0a432022-03-28 11:08:49 -0500407 "Status::getHwmonPath(): Found multiple ({}) hwmon paths!",
408 numDirs)
409 .c_str());
410 tracedFail[instance] = true;
411 }
412 }
413 }
414 catch (const fs::filesystem_error& e)
415 {
416 if (!tracedFail[instance])
417 {
418 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600419 std::format(
Chris Caine2d0a432022-03-28 11:08:49 -0500420 "Status::getHwmonPath(): error accessing {}: {}",
421 prefixPath.c_str(), e.what())
422 .c_str());
423 tracedFail[instance] = true;
424 }
425 }
426 }
427
428 return hwmonPath;
Chris Cain5d66a0a2022-02-09 08:52:10 -0600429}
430
Chris Cainf0295f52024-09-12 15:41:14 -0500431// Called to read state and handle any errors
Sheldon Bailey373af752022-02-21 15:14:00 -0600432void Status::occReadStateNow()
433{
434 unsigned int state;
435 const fs::path filename =
436 fs::path(DEV_PATH) /
437 fs::path(sysfsName + "." + std::to_string(instance + 1)) / "occ_state";
438
439 std::ifstream file;
440 bool goodFile = false;
441
442 // open file.
443 file.open(filename, std::ios::in);
444 const int openErrno = errno;
445
446 // File is open and state can be used.
447 if (file.is_open() && file.good())
448 {
449 goodFile = true;
450 file >> state;
Chris Cainf0295f52024-09-12 15:41:14 -0500451 // Read the error code (if any) to check status of the read
452 std::ios_base::iostate readState = file.rdstate();
453 if (readState)
454 {
455 // There was a failure reading the file
456 if (lastOccReadStatus != -1)
457 {
458 // Trace error bits
459 std::string errorBits = "";
460 if (readState & std::ios_base::eofbit)
461 {
462 errorBits += " EOF";
463 }
464 if (readState & std::ios_base::failbit)
465 {
466 errorBits += " failbit";
467 }
468 if (readState & std::ios_base::badbit)
469 {
470 errorBits += " badbit";
471 }
472 log<level::ERR>(
473 std::format(
474 "readOccState: Failed to read OCC{} state: Read error on I/O operation -{}",
475 instance, errorBits)
476 .c_str());
477 lastOccReadStatus = -1;
478 }
479 goodFile = false;
480 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600481
Chris Cainf0295f52024-09-12 15:41:14 -0500482 if (goodFile && (state != lastState))
Sheldon Bailey373af752022-02-21 15:14:00 -0600483 {
484 // Trace OCC state changes
485 log<level::INFO>(
Patrick Williams48002492024-02-13 21:43:32 -0600486 std::format(
Chris Cainbd551de2022-04-26 13:41:16 -0500487 "Status::readOccState: OCC{} state 0x{:02X} (lastState: 0x{:02X})",
488 instance, state, lastState)
Sheldon Bailey373af752022-02-21 15:14:00 -0600489 .c_str());
490 lastState = state;
491#ifdef POWER10
492 if (OccState(state) == OccState::ACTIVE)
493 {
494 if (pmode && device.master())
495 {
496 // Set the master OCC on the PowerMode object
497 pmode->setMasterOcc(path);
498 // Enable mode changes
499 pmode->setMasterActive();
500
501 // Special processing by master OCC when it goes active
502 occsWentActive();
503 }
504
505 CmdStatus status = sendAmbient();
506 if (status != CmdStatus::SUCCESS)
507 {
508 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600509 std::format(
Sheldon Bailey373af752022-02-21 15:14:00 -0600510 "readOccState: Sending Ambient failed with status {}",
511 status)
512 .c_str());
513 }
514 }
515
516 // If OCC in known Good State.
517 if ((OccState(state) == OccState::ACTIVE) ||
518 (OccState(state) == OccState::CHARACTERIZATION) ||
519 (OccState(state) == OccState::OBSERVATION))
520 {
521 // Good OCC State then sensors valid again
522 stateValid = true;
523
524 if (safeStateDelayTimer.isEnabled())
525 {
526 // stop safe delay timer (no longer in SAFE state)
527 safeStateDelayTimer.setEnabled(false);
528 }
529 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600530 else
531 {
Chris Cainf0295f52024-09-12 15:41:14 -0500532 // OCC is in SAFE or some other unsupported state
Sheldon Bailey373af752022-02-21 15:14:00 -0600533 if (!safeStateDelayTimer.isEnabled())
534 {
Chris Cainf0295f52024-09-12 15:41:14 -0500535 log<level::ERR>(
536 std::format(
537 "readOccState: Invalid OCC{} state of {}, starting safe state delay timer",
538 instance, state)
539 .c_str());
Sheldon Bailey373af752022-02-21 15:14:00 -0600540 // start safe delay timer (before requesting reset)
541 using namespace std::literals::chrono_literals;
542 safeStateDelayTimer.restartOnce(60s);
543 }
Chris Cainf0295f52024-09-12 15:41:14 -0500544 // Not a supported state (update sensors to NaN and not
545 // functional)
Sheldon Bailey373af752022-02-21 15:14:00 -0600546 stateValid = false;
547 }
548#else
549 // Before P10 state not checked, only used good file open.
550 stateValid = true;
551#endif
552 }
553 }
Chris Cainf0295f52024-09-12 15:41:14 -0500554#ifdef POWER10
555 else
556 {
557 // Unable to read state
558 stateValid = false;
559 }
560#endif
Sheldon Bailey373af752022-02-21 15:14:00 -0600561 file.close();
562
563 // if failed to Read a state or not a valid state -> Attempt retry
564 // after 1 Second delay if allowed.
565 if ((!goodFile) || (!stateValid))
566 {
567 if (!goodFile)
568 {
569 // If not able to read, OCC may be offline
Chris Cainf0295f52024-09-12 15:41:14 -0500570 if (openErrno != lastOccReadStatus)
571 {
572 log<level::ERR>(
573 std::format(
574 "Status::readOccState: open/read failed trying to read OCC{} state (open errno={})",
575 instance, openErrno)
576 .c_str());
577 lastOccReadStatus = openErrno;
578 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600579 }
580 else
581 {
582 // else this failed due to state not valid.
Chris Cainbd551de2022-04-26 13:41:16 -0500583 if (state != lastState)
584 {
585 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600586 std::format(
Chris Cainbd551de2022-04-26 13:41:16 -0500587 "Status::readOccState: OCC{} Invalid state 0x{:02X} (last state: 0x{:02X})",
588 instance, state, lastState)
589 .c_str());
590 }
Sheldon Bailey373af752022-02-21 15:14:00 -0600591 }
592
593#ifdef READ_OCC_SENSORS
Sheldon Baileyc8dd4592022-05-12 10:15:14 -0500594 manager.setSensorValueToNaN(instance);
Sheldon Bailey373af752022-02-21 15:14:00 -0600595#endif
596
597 // See occReadRetries for number of retry attempts.
598 if (currentOccReadRetriesCount > 0)
599 {
600 --currentOccReadRetriesCount;
Sheldon Bailey373af752022-02-21 15:14:00 -0600601 }
602 else
603 {
Chris Cainf0295f52024-09-12 15:41:14 -0500604 log<level::ERR>(
605 std::format("readOccState: failed to read OCC{} state!",
606 instance)
607 .c_str());
608
Sheldon Bailey373af752022-02-21 15:14:00 -0600609 // State could not be determined, set it to NO State.
610 lastState = 0;
611
612 // Disable the ability to send Failed actions until OCC is
613 // Active again.
614 stateValid = false;
615
Chris Cainf0295f52024-09-12 15:41:14 -0500616 // Disable due to OCC comm failure and reset to try recovering
617 deviceError(Error::Descriptor(OCC_COMM_ERROR_PATH));
618
619 // Reset retry count (for next attempt after recovery)
620 currentOccReadRetriesCount = occReadRetries;
621 }
622 }
623 else
624 {
625 if (lastOccReadStatus != 0)
626 {
627 log<level::INFO>(
628 std::format(
629 "Status::readOccState: successfully read OCC{} state: {}",
630 instance, state)
631 .c_str());
632 lastOccReadStatus = 0; // no error
Sheldon Bailey373af752022-02-21 15:14:00 -0600633 }
634 }
635}
636
Chris Cainc86d80f2023-05-04 15:49:18 -0500637// Update processor throttle status on dbus
638void Status::updateThrottle(const bool isThrottled, const uint8_t newReason)
639{
640 if (!throttleHandle)
641 {
642 return;
643 }
644
645 uint8_t newThrottleCause = throttleCause;
646
647 if (isThrottled) // throttled due to newReason
648 {
649 if ((newReason & throttleCause) == 0)
650 {
651 // set the bit(s) for passed in reason
652 newThrottleCause |= newReason;
653 }
654 // else no change
655 }
656 else // no longer throttled due to newReason
657 {
658 if ((newReason & throttleCause) != 0)
659 {
660 // clear the bit(s) for passed in reason
661 newThrottleCause &= ~newReason;
662 }
663 // else no change
664 }
665
666 if (newThrottleCause != throttleCause)
667 {
668 if (newThrottleCause == THROTTLED_NONE)
669 {
670 log<level::DEBUG>(
Patrick Williams48002492024-02-13 21:43:32 -0600671 std::format(
Chris Cainc86d80f2023-05-04 15:49:18 -0500672 "updateThrottle: OCC{} no longer throttled (prior reason: {})",
673 instance, throttleCause)
674 .c_str());
675 throttleCause = THROTTLED_NONE;
676 throttleHandle->throttled(false);
677 throttleHandle->throttleCauses({});
678 }
679 else
680 {
681 log<level::DEBUG>(
Patrick Williams48002492024-02-13 21:43:32 -0600682 std::format(
Chris Cainc86d80f2023-05-04 15:49:18 -0500683 "updateThrottle: OCC{} is throttled with reason {} (prior reason: {})",
684 instance, newThrottleCause, throttleCause)
685 .c_str());
686 throttleCause = newThrottleCause;
687
688 std::vector<ThrottleObj::ThrottleReasons> updatedCauses;
689 if (throttleCause & THROTTLED_POWER)
690 {
691 updatedCauses.push_back(
692 throttleHandle->ThrottleReasons::PowerLimit);
693 }
694 if (throttleCause & THROTTLED_THERMAL)
695 {
696 updatedCauses.push_back(
697 throttleHandle->ThrottleReasons::ThermalLimit);
698 }
699 if (throttleCause & THROTTLED_SAFE)
700 {
701 updatedCauses.push_back(
702 throttleHandle->ThrottleReasons::ManagementDetectedFault);
703 }
704 throttleHandle->throttleCauses(updatedCauses);
705 throttleHandle->throttled(true);
706 }
707 }
708 // else no change to throttle status
709}
710
711// Get processor path associated with this OCC
712void Status::readProcAssociation()
713{
714 std::string managingPath = path + "/power_managing";
715 log<level::DEBUG>(
Patrick Williams48002492024-02-13 21:43:32 -0600716 std::format("readProcAssociation: getting endpoints for {} ({})",
Chris Cainc86d80f2023-05-04 15:49:18 -0500717 managingPath, path)
718 .c_str());
719 try
720 {
721 utils::PropertyValue procPathProperty{};
722 procPathProperty = utils::getProperty(
723 managingPath, "xyz.openbmc_project.Association", "endpoints");
724 auto result = std::get<std::vector<std::string>>(procPathProperty);
725 if (result.size() > 0)
726 {
727 procPath = result[0];
728 log<level::INFO>(
Patrick Williams48002492024-02-13 21:43:32 -0600729 std::format("readProcAssociation: OCC{} has proc={}", instance,
Chris Cainc86d80f2023-05-04 15:49:18 -0500730 procPath.c_str())
731 .c_str());
732 }
733 else
734 {
735 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600736 std::format(
Chris Cainc86d80f2023-05-04 15:49:18 -0500737 "readProcAssociation: No processor associated with OCC{} / {}",
738 instance, path)
739 .c_str());
740 }
741 }
742 catch (const sdbusplus::exception_t& e)
743 {
744 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600745 std::format(
Chris Cainc86d80f2023-05-04 15:49:18 -0500746 "readProcAssociation: Unable to get proc assocated with {} - {}",
747 path, e.what())
748 .c_str());
749 procPath = {};
750 }
751}
752
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530753} // namespace occ
754} // namespace open_power