blob: 95a7729d233cf3ce08f3fed7b958057c8b52b7db [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 Caina8857c52021-01-27 11:53:05 -06008#include <fmt/core.h>
9
Chris Cain78e86012021-03-04 16:15:31 -060010#ifdef POWER10
11#include <com/ibm/Host/Target/server.hpp>
12#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -050013#include <phosphor-logging/log.hpp>
Chris Cain78e86012021-03-04 16:15:31 -060014
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053015namespace open_power
16{
17namespace occ
18{
Chris Cain78e86012021-03-04 16:15:31 -060019
Chris Caina8857c52021-01-27 11:53:05 -060020using namespace phosphor::logging;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053021
22// 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 Caina8857c52021-01-27 11:53:05 -060027 log<level::INFO>(fmt::format("Status::occActive OCC{} changed to {}",
28 instance, value)
29 .c_str());
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053030 if (value)
31 {
32 // Bind the device
33 device.bind();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053034
Edward A. James9fd2bdc2017-11-08 16:18:57 -060035 // Start watching for errors
36 addErrorWatch();
37
Chris Caina8857c52021-01-27 11:53:05 -060038 // Reset last OCC state
39 lastState = 0;
40
Chris Cain5d66a0a2022-02-09 08:52:10 -060041 if (device.master())
42 {
Chris Cain5d66a0a2022-02-09 08:52:10 -060043 // Update powercap bounds from OCC
Chris Cain40501a22022-03-14 17:33:27 -050044 manager.updatePcapBounds();
Chris Cain5d66a0a2022-02-09 08:52:10 -060045 }
46
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +053047 // Call into Manager to let know that we have bound
Chris Cain1be43372021-12-09 19:29:37 -060048 if (this->managerCallBack)
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +053049 {
Chris Cain1be43372021-12-09 19:29:37 -060050 this->managerCallBack(value);
Edward A. James9fd2bdc2017-11-08 16:18:57 -060051 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053052 }
53 else
54 {
Chris Caina7b74dc2021-11-10 17:03:43 -060055#ifdef POWER10
Chris Cain1be43372021-12-09 19:29:37 -060056 if (pmode && device.master())
Chris Cain36f9cde2021-11-22 11:18:21 -060057 {
58 // Prevent mode changes
59 pmode->setMasterActive(false);
60 }
Chris Caina7b74dc2021-11-10 17:03:43 -060061 if (safeStateDelayTimer.isEnabled())
62 {
63 // stop safe delay timer
64 safeStateDelayTimer.setEnabled(false);
65 }
66#endif
67
Chris Cain36f9cde2021-11-22 11:18:21 -060068 // Call into Manager to let know that we will unbind.
Chris Cain1be43372021-12-09 19:29:37 -060069 if (this->managerCallBack)
Chris Cain36f9cde2021-11-22 11:18:21 -060070 {
Chris Cain1be43372021-12-09 19:29:37 -060071 this->managerCallBack(value);
Chris Cain36f9cde2021-11-22 11:18:21 -060072 }
73
Edward A. James9fd2bdc2017-11-08 16:18:57 -060074 // Stop watching for errors
75 removeErrorWatch();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053076
77 // Do the unbind.
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053078 device.unBind();
79 }
80 }
Edward A. James5e177972017-10-25 15:50:31 -050081 else if (value && !device.bound())
82 {
83 // Existing error watch is on a dead file descriptor.
Edward A. James9fd2bdc2017-11-08 16:18:57 -060084 removeErrorWatch();
Edward A. James5e177972017-10-25 15:50:31 -050085
86 /*
87 * In it's constructor, Status checks Device::bound() to see if OCC is
88 * active or not.
89 * Device::bound() checks for occX-dev0 directory.
90 * We will lose occX-dev0 directories during FSI rescan.
91 * So, if we start this application (and construct Status), and then
92 * later do FSI rescan, we will end up with occActive = true and device
93 * NOT bound. Lets correct that situation here.
94 */
95 device.bind();
96
97 // Add error watch again
Edward A. James9fd2bdc2017-11-08 16:18:57 -060098 addErrorWatch();
Edward A. James5e177972017-10-25 15:50:31 -050099 }
Eddie James6d6d1b32019-04-22 10:45:08 -0500100 else if (!value && device.bound())
101 {
102 removeErrorWatch();
103
104 // In the event that the application never receives the active signal
105 // even though the OCC is active (this can occur if the BMC is rebooted
106 // with the host on, since the initial OCC driver probe will discover
107 // the OCCs), this application needs to be able to unbind the device
108 // when we get the OCC inactive signal.
109 device.unBind();
110 }
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530111 return Base::Status::occActive(value);
112}
113
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530114// Callback handler when a device error is reported.
Eddie Jamescbad2192021-10-07 09:39:39 -0500115void Status::deviceError()
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530116{
Chris Cain36f9cde2021-11-22 11:18:21 -0600117#ifdef POWER10
Chris Cain1be43372021-12-09 19:29:37 -0600118 if (pmode && device.master())
119 {
120 // Prevent mode changes
121 pmode->setMasterActive(false);
122 }
Chris Cain36f9cde2021-11-22 11:18:21 -0600123#endif
124
Eddie Jamescbad2192021-10-07 09:39:39 -0500125 // This would deem OCC inactive
126 this->occActive(false);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530127
Eddie Jamescbad2192021-10-07 09:39:39 -0500128 // Reset the OCC
129 this->resetOCC();
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530130}
131
132// Sends message to host control command handler to reset OCC
133void Status::resetOCC()
134{
Chris Caina8857c52021-01-27 11:53:05 -0600135 log<level::INFO>(
136 fmt::format(">>Status::resetOCC() - requesting reset for OCC{}",
137 instance)
138 .c_str());
Tom Joseph00325232020-07-29 17:51:48 +0530139#ifdef PLDM
140 if (resetCallBack)
141 {
142 this->resetCallBack(instance);
143 }
144#else
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530145 constexpr auto CONTROL_HOST_PATH = "/org/open_power/control/host0";
146 constexpr auto CONTROL_HOST_INTF = "org.open_power.Control.Host";
147
148 // This will throw exception on failure
George Liuf3b75142021-06-10 11:22:50 +0800149 auto service = utils::getService(CONTROL_HOST_PATH, CONTROL_HOST_INTF);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530150
George Liuf3b75142021-06-10 11:22:50 +0800151 auto& bus = utils::getBus();
Gunnar Mills94df8c92018-09-14 14:50:03 -0500152 auto method = bus.new_method_call(service.c_str(), CONTROL_HOST_PATH,
153 CONTROL_HOST_INTF, "Execute");
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530154 // OCC Reset control command
Gunnar Mills94df8c92018-09-14 14:50:03 -0500155 method.append(convertForMessage(Control::Host::Command::OCCReset).c_str());
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530156
157 // OCC Sensor ID for callout reasons
Patrick Williamse0962702020-05-13 17:50:22 -0500158 method.append(std::variant<uint8_t>(std::get<0>(sensorMap.at(instance))));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530159 bus.call_noreply(method);
160 return;
Tom Joseph00325232020-07-29 17:51:48 +0530161#endif
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530162}
163
164// Handler called by Host control command handler to convey the
165// status of the executed command
166void Status::hostControlEvent(sdbusplus::message::message& msg)
167{
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530168 std::string cmdCompleted{};
169 std::string cmdStatus{};
170
171 msg.read(cmdCompleted, cmdStatus);
172
173 log<level::DEBUG>("Host control signal values",
Gunnar Mills94df8c92018-09-14 14:50:03 -0500174 entry("COMMAND=%s", cmdCompleted.c_str()),
175 entry("STATUS=%s", cmdStatus.c_str()));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530176
Gunnar Mills94df8c92018-09-14 14:50:03 -0500177 if (Control::Host::convertResultFromString(cmdStatus) !=
178 Control::Host::Result::Success)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530179 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500180 if (Control::Host::convertCommandFromString(cmdCompleted) ==
181 Control::Host::Command::OCCReset)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530182 {
Gunnar Mills85e65202018-04-08 15:01:54 -0500183 // Must be a Timeout. Log an Error trace
Alexander Filippov1d69e192019-03-21 18:12:07 +0300184 log<level::ERR>(
185 "Error resetting the OCC.", entry("PATH=%s", path.c_str()),
186 entry("SENSORID=0x%X", std::get<0>(sensorMap.at(instance))));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530187 }
188 }
189 return;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530190}
191
Chris Caina8857c52021-01-27 11:53:05 -0600192void Status::readOccState()
193{
194 unsigned int state;
195 const fs::path filename =
196 fs::path(DEV_PATH) /
197 fs::path(sysfsName + "." + std::to_string(instance + 1)) / "occ_state";
198
Chris Caina8857c52021-01-27 11:53:05 -0600199 std::ifstream file(filename, std::ios::in);
200 const int open_errno = errno;
201 if (file)
202 {
203 file >> state;
204 if (state != lastState)
205 {
206 // Trace OCC state changes
207 log<level::INFO>(
208 fmt::format("Status::readOccState: OCC{} state 0x{:02X}",
209 instance, state)
210 .c_str());
Chris Cain5d66a0a2022-02-09 08:52:10 -0600211 if (state & 0xFFFFFFF8)
212 {
213 log<level::ERR>(
214 fmt::format("Status::readOccState: INVALID STATE from {}!!",
215 filename.c_str())
216 .c_str());
217 }
Chris Caina8857c52021-01-27 11:53:05 -0600218 lastState = state;
Chris Cain78e86012021-03-04 16:15:31 -0600219
220#ifdef POWER10
Chris Cain17257672021-10-22 13:41:03 -0500221 if (OccState(state) == OccState::ACTIVE)
222 {
Chris Cain1be43372021-12-09 19:29:37 -0600223 if (pmode && device.master())
Chris Caina7b74dc2021-11-10 17:03:43 -0600224 {
Chris Cain6fa848a2022-01-24 14:54:38 -0600225 // Set the master OCC on the PowerMode object
226 pmode->setMasterOcc(path);
227 // Enable mode changes
Chris Cain36f9cde2021-11-22 11:18:21 -0600228 pmode->setMasterActive();
229
Chris Caina7b74dc2021-11-10 17:03:43 -0600230 // Special processing by master OCC when it goes active
231 occsWentActive();
232 }
233
Chris Cain17257672021-10-22 13:41:03 -0500234 CmdStatus status = sendAmbient();
235 if (status != CmdStatus::SUCCESS)
236 {
237 log<level::ERR>(
238 fmt::format(
239 "readOccState: Sending Ambient failed with status {}",
240 status)
241 .c_str());
242 }
243 }
Chris Caina7b74dc2021-11-10 17:03:43 -0600244
245 if (OccState(state) == OccState::SAFE)
246 {
247 // start safe delay timer (before requesting reset)
248 using namespace std::literals::chrono_literals;
249 safeStateDelayTimer.restartOnce(60s);
250 }
251 else if (safeStateDelayTimer.isEnabled())
252 {
253 // stop safe delay timer (no longer in SAFE state)
254 safeStateDelayTimer.setEnabled(false);
255 }
Chris Cain78e86012021-03-04 16:15:31 -0600256#endif
Chris Caina8857c52021-01-27 11:53:05 -0600257 }
258 file.close();
259 }
260 else
261 {
262 // If not able to read, OCC may be offline
263 log<level::DEBUG>(
264 fmt::format("Status::readOccState: open failed (errno={})",
265 open_errno)
266 .c_str());
267 lastState = 0;
268 }
269}
270
Chris Cain78e86012021-03-04 16:15:31 -0600271#ifdef POWER10
Chris Cain78e86012021-03-04 16:15:31 -0600272// Special processing that needs to happen once the OCCs change to ACTIVE state
273void Status::occsWentActive()
274{
275 CmdStatus status = CmdStatus::SUCCESS;
276
Chris Cain36f9cde2021-11-22 11:18:21 -0600277 status = pmode->sendModeChange();
Chris Cain78e86012021-03-04 16:15:31 -0600278 if (status != CmdStatus::SUCCESS)
279 {
George Liub5ca1012021-09-10 12:53:11 +0800280 log<level::ERR>(
281 fmt::format(
282 "Status::occsWentActive: OCC mode change failed with status {}",
283 status)
284 .c_str());
Chris Cain78e86012021-03-04 16:15:31 -0600285 }
286
Chris Cain36f9cde2021-11-22 11:18:21 -0600287 status = pmode->sendIpsData();
Chris Cain78e86012021-03-04 16:15:31 -0600288 if (status != CmdStatus::SUCCESS)
289 {
290 log<level::ERR>(
291 fmt::format(
George Liub5ca1012021-09-10 12:53:11 +0800292 "Status::occsWentActive: Sending Idle Power Save Config data failed with status {}",
Chris Cain78e86012021-03-04 16:15:31 -0600293 status)
294 .c_str());
295 }
296}
297
Chris Cain17257672021-10-22 13:41:03 -0500298// Send Ambient and Altitude to the OCC
299CmdStatus Status::sendAmbient(const uint8_t inTemp, const uint16_t inAltitude)
300{
301 CmdStatus status = CmdStatus::FAILURE;
302 bool ambientValid = true;
303 uint8_t ambientTemp = inTemp;
304 uint16_t altitude = inAltitude;
305
306 if (ambientTemp == 0xFF)
307 {
308 // Get latest readings from manager
309 manager.getAmbientData(ambientValid, ambientTemp, altitude);
310 log<level::DEBUG>(
311 fmt::format("sendAmbient: valid: {}, Ambient: {}C, altitude: {}m",
312 ambientValid, ambientTemp, altitude)
313 .c_str());
314 }
315
316 std::vector<std::uint8_t> cmd, rsp;
317 cmd.reserve(11);
318 cmd.push_back(uint8_t(CmdType::SEND_AMBIENT));
319 cmd.push_back(0x00); // Data Length (2 bytes)
320 cmd.push_back(0x08); //
321 cmd.push_back(0x00); // Version
322 cmd.push_back(ambientValid ? 0 : 0xFF); // Ambient Status
323 cmd.push_back(ambientTemp); // Ambient Temperature
324 cmd.push_back(altitude >> 8); // Altitude in meters (2 bytes)
325 cmd.push_back(altitude & 0xFF); //
326 cmd.push_back(0x00); // Reserved (3 bytes)
327 cmd.push_back(0x00);
328 cmd.push_back(0x00);
329 log<level::DEBUG>(fmt::format("sendAmbient: SEND_AMBIENT "
330 "command to OCC{} ({} bytes)",
331 instance, cmd.size())
332 .c_str());
333 status = occCmd.send(cmd, rsp);
334 if (status == CmdStatus::SUCCESS)
335 {
336 if (rsp.size() == 5)
337 {
338 if (RspStatus::SUCCESS != RspStatus(rsp[2]))
339 {
340 log<level::ERR>(
341 fmt::format(
342 "sendAmbient: SEND_AMBIENT failed with status 0x{:02X}",
343 rsp[2])
344 .c_str());
345 dump_hex(rsp);
346 status = CmdStatus::FAILURE;
347 }
348 }
349 else
350 {
351 log<level::ERR>("sendAmbient: INVALID SEND_AMBIENT response");
352 dump_hex(rsp);
353 status = CmdStatus::FAILURE;
354 }
355 }
356 else
357 {
358 if (status == CmdStatus::OPEN_FAILURE)
359 {
360 // OCC not active yet
361 status = CmdStatus::SUCCESS;
362 }
363 else
364 {
365 log<level::ERR>("sendAmbient: SEND_AMBIENT FAILED!");
366 }
367 }
368
369 return status;
370}
Chris Caina7b74dc2021-11-10 17:03:43 -0600371
372// Called when safe timer expires to determine if OCCs need to be reset
373void Status::safeStateDelayExpired()
374{
375 if (this->occActive())
376 {
377 log<level::INFO>(
378 fmt::format(
379 "safeStateDelayExpired: OCC{} is in SAFE state, requesting reset",
380 instance)
381 .c_str());
382 // Disable and reset to try recovering
383 deviceError();
384 }
385}
Chris Cain78e86012021-03-04 16:15:31 -0600386#endif // POWER10
387
Chris Cain5d66a0a2022-02-09 08:52:10 -0600388fs::path Status::getHwmonPath() const
389{
390 using namespace std::literals::string_literals;
391
392 // Build the base HWMON path
393 fs::path prefixPath = fs::path{OCC_HWMON_PATH + "occ-hwmon."s +
394 std::to_string(instance + 1) + "/hwmon/"s};
395 // Get the hwmonXX directory name, there better only be 1 dir
396 assert(std::distance(fs::directory_iterator(prefixPath),
397 fs::directory_iterator{}) == 1);
398
399 return *fs::directory_iterator(prefixPath);
400}
401
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530402} // namespace occ
403} // namespace open_power