blob: 0cb9b462df9ab7a6f5eb0f9f85defc85f6b88fbf [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
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +053041 // Call into Manager to let know that we have bound
Edward A. James9fd2bdc2017-11-08 16:18:57 -060042 if (this->callBack)
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +053043 {
44 this->callBack(value);
Edward A. James9fd2bdc2017-11-08 16:18:57 -060045 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053046 }
47 else
48 {
Chris Caina7b74dc2021-11-10 17:03:43 -060049#ifdef POWER10
Chris Cain36f9cde2021-11-22 11:18:21 -060050 if (device.master())
51 {
52 // Prevent mode changes
53 pmode->setMasterActive(false);
54 }
Chris Caina7b74dc2021-11-10 17:03:43 -060055 if (safeStateDelayTimer.isEnabled())
56 {
57 // stop safe delay timer
58 safeStateDelayTimer.setEnabled(false);
59 }
60#endif
61
Chris Cain36f9cde2021-11-22 11:18:21 -060062 // Call into Manager to let know that we will unbind.
63 if (this->callBack)
64 {
65 this->callBack(value);
66 }
67
Edward A. James9fd2bdc2017-11-08 16:18:57 -060068 // Stop watching for errors
69 removeErrorWatch();
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +053070
71 // Do the unbind.
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +053072 device.unBind();
73 }
74 }
Edward A. James5e177972017-10-25 15:50:31 -050075 else if (value && !device.bound())
76 {
77 // Existing error watch is on a dead file descriptor.
Edward A. James9fd2bdc2017-11-08 16:18:57 -060078 removeErrorWatch();
Edward A. James5e177972017-10-25 15:50:31 -050079
80 /*
81 * In it's constructor, Status checks Device::bound() to see if OCC is
82 * active or not.
83 * Device::bound() checks for occX-dev0 directory.
84 * We will lose occX-dev0 directories during FSI rescan.
85 * So, if we start this application (and construct Status), and then
86 * later do FSI rescan, we will end up with occActive = true and device
87 * NOT bound. Lets correct that situation here.
88 */
89 device.bind();
90
91 // Add error watch again
Edward A. James9fd2bdc2017-11-08 16:18:57 -060092 addErrorWatch();
Edward A. James5e177972017-10-25 15:50:31 -050093 }
Eddie James6d6d1b32019-04-22 10:45:08 -050094 else if (!value && device.bound())
95 {
96 removeErrorWatch();
97
98 // In the event that the application never receives the active signal
99 // even though the OCC is active (this can occur if the BMC is rebooted
100 // with the host on, since the initial OCC driver probe will discover
101 // the OCCs), this application needs to be able to unbind the device
102 // when we get the OCC inactive signal.
103 device.unBind();
104 }
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530105 return Base::Status::occActive(value);
106}
107
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530108// Callback handler when a device error is reported.
Eddie Jamescbad2192021-10-07 09:39:39 -0500109void Status::deviceError()
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530110{
Chris Cain36f9cde2021-11-22 11:18:21 -0600111#ifdef POWER10
112 // Prevent mode changes
113 pmode->setMasterActive(false);
114#endif
115
Eddie Jamescbad2192021-10-07 09:39:39 -0500116 // This would deem OCC inactive
117 this->occActive(false);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530118
Eddie Jamescbad2192021-10-07 09:39:39 -0500119 // Reset the OCC
120 this->resetOCC();
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530121}
122
123// Sends message to host control command handler to reset OCC
124void Status::resetOCC()
125{
Chris Caina8857c52021-01-27 11:53:05 -0600126 log<level::INFO>(
127 fmt::format(">>Status::resetOCC() - requesting reset for OCC{}",
128 instance)
129 .c_str());
Tom Joseph00325232020-07-29 17:51:48 +0530130#ifdef PLDM
131 if (resetCallBack)
132 {
133 this->resetCallBack(instance);
134 }
135#else
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530136 constexpr auto CONTROL_HOST_PATH = "/org/open_power/control/host0";
137 constexpr auto CONTROL_HOST_INTF = "org.open_power.Control.Host";
138
139 // This will throw exception on failure
George Liuf3b75142021-06-10 11:22:50 +0800140 auto service = utils::getService(CONTROL_HOST_PATH, CONTROL_HOST_INTF);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530141
George Liuf3b75142021-06-10 11:22:50 +0800142 auto& bus = utils::getBus();
Gunnar Mills94df8c92018-09-14 14:50:03 -0500143 auto method = bus.new_method_call(service.c_str(), CONTROL_HOST_PATH,
144 CONTROL_HOST_INTF, "Execute");
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530145 // OCC Reset control command
Gunnar Mills94df8c92018-09-14 14:50:03 -0500146 method.append(convertForMessage(Control::Host::Command::OCCReset).c_str());
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530147
148 // OCC Sensor ID for callout reasons
Patrick Williamse0962702020-05-13 17:50:22 -0500149 method.append(std::variant<uint8_t>(std::get<0>(sensorMap.at(instance))));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530150 bus.call_noreply(method);
151 return;
Tom Joseph00325232020-07-29 17:51:48 +0530152#endif
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530153}
154
155// Handler called by Host control command handler to convey the
156// status of the executed command
157void Status::hostControlEvent(sdbusplus::message::message& msg)
158{
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530159 std::string cmdCompleted{};
160 std::string cmdStatus{};
161
162 msg.read(cmdCompleted, cmdStatus);
163
164 log<level::DEBUG>("Host control signal values",
Gunnar Mills94df8c92018-09-14 14:50:03 -0500165 entry("COMMAND=%s", cmdCompleted.c_str()),
166 entry("STATUS=%s", cmdStatus.c_str()));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530167
Gunnar Mills94df8c92018-09-14 14:50:03 -0500168 if (Control::Host::convertResultFromString(cmdStatus) !=
169 Control::Host::Result::Success)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530170 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500171 if (Control::Host::convertCommandFromString(cmdCompleted) ==
172 Control::Host::Command::OCCReset)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530173 {
Gunnar Mills85e65202018-04-08 15:01:54 -0500174 // Must be a Timeout. Log an Error trace
Alexander Filippov1d69e192019-03-21 18:12:07 +0300175 log<level::ERR>(
176 "Error resetting the OCC.", entry("PATH=%s", path.c_str()),
177 entry("SENSORID=0x%X", std::get<0>(sensorMap.at(instance))));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530178 }
179 }
180 return;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530181}
182
Chris Caina8857c52021-01-27 11:53:05 -0600183void Status::readOccState()
184{
185 unsigned int state;
186 const fs::path filename =
187 fs::path(DEV_PATH) /
188 fs::path(sysfsName + "." + std::to_string(instance + 1)) / "occ_state";
189
Chris Caina8857c52021-01-27 11:53:05 -0600190 std::ifstream file(filename, std::ios::in);
191 const int open_errno = errno;
192 if (file)
193 {
194 file >> state;
195 if (state != lastState)
196 {
197 // Trace OCC state changes
198 log<level::INFO>(
199 fmt::format("Status::readOccState: OCC{} state 0x{:02X}",
200 instance, state)
201 .c_str());
202 lastState = state;
Chris Cain78e86012021-03-04 16:15:31 -0600203
204#ifdef POWER10
Chris Cain17257672021-10-22 13:41:03 -0500205 if (OccState(state) == OccState::ACTIVE)
206 {
Chris Caina7b74dc2021-11-10 17:03:43 -0600207 if (device.master())
208 {
Chris Cain6fa848a2022-01-24 14:54:38 -0600209 // Set the master OCC on the PowerMode object
210 pmode->setMasterOcc(path);
211 // Enable mode changes
Chris Cain36f9cde2021-11-22 11:18:21 -0600212 pmode->setMasterActive();
213
Chris Caina7b74dc2021-11-10 17:03:43 -0600214 // Special processing by master OCC when it goes active
215 occsWentActive();
216 }
217
Chris Cain17257672021-10-22 13:41:03 -0500218 CmdStatus status = sendAmbient();
219 if (status != CmdStatus::SUCCESS)
220 {
221 log<level::ERR>(
222 fmt::format(
223 "readOccState: Sending Ambient failed with status {}",
224 status)
225 .c_str());
226 }
227 }
Chris Caina7b74dc2021-11-10 17:03:43 -0600228
229 if (OccState(state) == OccState::SAFE)
230 {
231 // start safe delay timer (before requesting reset)
232 using namespace std::literals::chrono_literals;
233 safeStateDelayTimer.restartOnce(60s);
234 }
235 else if (safeStateDelayTimer.isEnabled())
236 {
237 // stop safe delay timer (no longer in SAFE state)
238 safeStateDelayTimer.setEnabled(false);
239 }
Chris Cain78e86012021-03-04 16:15:31 -0600240#endif
Chris Caina8857c52021-01-27 11:53:05 -0600241 }
242 file.close();
243 }
244 else
245 {
246 // If not able to read, OCC may be offline
247 log<level::DEBUG>(
248 fmt::format("Status::readOccState: open failed (errno={})",
249 open_errno)
250 .c_str());
251 lastState = 0;
252 }
253}
254
Chris Cain78e86012021-03-04 16:15:31 -0600255#ifdef POWER10
Chris Cain78e86012021-03-04 16:15:31 -0600256// Special processing that needs to happen once the OCCs change to ACTIVE state
257void Status::occsWentActive()
258{
259 CmdStatus status = CmdStatus::SUCCESS;
260
Chris Cain36f9cde2021-11-22 11:18:21 -0600261 status = pmode->sendModeChange();
Chris Cain78e86012021-03-04 16:15:31 -0600262 if (status != CmdStatus::SUCCESS)
263 {
George Liub5ca1012021-09-10 12:53:11 +0800264 log<level::ERR>(
265 fmt::format(
266 "Status::occsWentActive: OCC mode change failed with status {}",
267 status)
268 .c_str());
Chris Cain78e86012021-03-04 16:15:31 -0600269 }
270
Chris Cain36f9cde2021-11-22 11:18:21 -0600271 status = pmode->sendIpsData();
Chris Cain78e86012021-03-04 16:15:31 -0600272 if (status != CmdStatus::SUCCESS)
273 {
274 log<level::ERR>(
275 fmt::format(
George Liub5ca1012021-09-10 12:53:11 +0800276 "Status::occsWentActive: Sending Idle Power Save Config data failed with status {}",
Chris Cain78e86012021-03-04 16:15:31 -0600277 status)
278 .c_str());
279 }
280}
281
Chris Cain17257672021-10-22 13:41:03 -0500282// Send Ambient and Altitude to the OCC
283CmdStatus Status::sendAmbient(const uint8_t inTemp, const uint16_t inAltitude)
284{
285 CmdStatus status = CmdStatus::FAILURE;
286 bool ambientValid = true;
287 uint8_t ambientTemp = inTemp;
288 uint16_t altitude = inAltitude;
289
290 if (ambientTemp == 0xFF)
291 {
292 // Get latest readings from manager
293 manager.getAmbientData(ambientValid, ambientTemp, altitude);
294 log<level::DEBUG>(
295 fmt::format("sendAmbient: valid: {}, Ambient: {}C, altitude: {}m",
296 ambientValid, ambientTemp, altitude)
297 .c_str());
298 }
299
300 std::vector<std::uint8_t> cmd, rsp;
301 cmd.reserve(11);
302 cmd.push_back(uint8_t(CmdType::SEND_AMBIENT));
303 cmd.push_back(0x00); // Data Length (2 bytes)
304 cmd.push_back(0x08); //
305 cmd.push_back(0x00); // Version
306 cmd.push_back(ambientValid ? 0 : 0xFF); // Ambient Status
307 cmd.push_back(ambientTemp); // Ambient Temperature
308 cmd.push_back(altitude >> 8); // Altitude in meters (2 bytes)
309 cmd.push_back(altitude & 0xFF); //
310 cmd.push_back(0x00); // Reserved (3 bytes)
311 cmd.push_back(0x00);
312 cmd.push_back(0x00);
313 log<level::DEBUG>(fmt::format("sendAmbient: SEND_AMBIENT "
314 "command to OCC{} ({} bytes)",
315 instance, cmd.size())
316 .c_str());
317 status = occCmd.send(cmd, rsp);
318 if (status == CmdStatus::SUCCESS)
319 {
320 if (rsp.size() == 5)
321 {
322 if (RspStatus::SUCCESS != RspStatus(rsp[2]))
323 {
324 log<level::ERR>(
325 fmt::format(
326 "sendAmbient: SEND_AMBIENT failed with status 0x{:02X}",
327 rsp[2])
328 .c_str());
329 dump_hex(rsp);
330 status = CmdStatus::FAILURE;
331 }
332 }
333 else
334 {
335 log<level::ERR>("sendAmbient: INVALID SEND_AMBIENT response");
336 dump_hex(rsp);
337 status = CmdStatus::FAILURE;
338 }
339 }
340 else
341 {
342 if (status == CmdStatus::OPEN_FAILURE)
343 {
344 // OCC not active yet
345 status = CmdStatus::SUCCESS;
346 }
347 else
348 {
349 log<level::ERR>("sendAmbient: SEND_AMBIENT FAILED!");
350 }
351 }
352
353 return status;
354}
Chris Caina7b74dc2021-11-10 17:03:43 -0600355
356// Called when safe timer expires to determine if OCCs need to be reset
357void Status::safeStateDelayExpired()
358{
359 if (this->occActive())
360 {
361 log<level::INFO>(
362 fmt::format(
363 "safeStateDelayExpired: OCC{} is in SAFE state, requesting reset",
364 instance)
365 .c_str());
366 // Disable and reset to try recovering
367 deviceError();
368 }
369}
Chris Cain78e86012021-03-04 16:15:31 -0600370#endif // POWER10
371
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530372} // namespace occ
373} // namespace open_power