blob: 1da440cc2202aed27187ce30334ff402ac6a70fd [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 Cain36f9cde2021-11-22 11:18:21 -0600209 // Prevent mode changes
210 pmode->setMasterActive();
211
Chris Caina7b74dc2021-11-10 17:03:43 -0600212 // Special processing by master OCC when it goes active
213 occsWentActive();
214 }
215
Chris Cain17257672021-10-22 13:41:03 -0500216 CmdStatus status = sendAmbient();
217 if (status != CmdStatus::SUCCESS)
218 {
219 log<level::ERR>(
220 fmt::format(
221 "readOccState: Sending Ambient failed with status {}",
222 status)
223 .c_str());
224 }
225 }
Chris Caina7b74dc2021-11-10 17:03:43 -0600226
227 if (OccState(state) == OccState::SAFE)
228 {
229 // start safe delay timer (before requesting reset)
230 using namespace std::literals::chrono_literals;
231 safeStateDelayTimer.restartOnce(60s);
232 }
233 else if (safeStateDelayTimer.isEnabled())
234 {
235 // stop safe delay timer (no longer in SAFE state)
236 safeStateDelayTimer.setEnabled(false);
237 }
Chris Cain78e86012021-03-04 16:15:31 -0600238#endif
Chris Caina8857c52021-01-27 11:53:05 -0600239 }
240 file.close();
241 }
242 else
243 {
244 // If not able to read, OCC may be offline
245 log<level::DEBUG>(
246 fmt::format("Status::readOccState: open failed (errno={})",
247 open_errno)
248 .c_str());
249 lastState = 0;
250 }
251}
252
Chris Cain78e86012021-03-04 16:15:31 -0600253#ifdef POWER10
Chris Cain78e86012021-03-04 16:15:31 -0600254// Special processing that needs to happen once the OCCs change to ACTIVE state
255void Status::occsWentActive()
256{
257 CmdStatus status = CmdStatus::SUCCESS;
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 {
George Liub5ca1012021-09-10 12:53:11 +0800262 log<level::ERR>(
263 fmt::format(
264 "Status::occsWentActive: OCC mode change failed with status {}",
265 status)
266 .c_str());
Chris Cain78e86012021-03-04 16:15:31 -0600267 }
268
Chris Cain36f9cde2021-11-22 11:18:21 -0600269 status = pmode->sendIpsData();
Chris Cain78e86012021-03-04 16:15:31 -0600270 if (status != CmdStatus::SUCCESS)
271 {
272 log<level::ERR>(
273 fmt::format(
George Liub5ca1012021-09-10 12:53:11 +0800274 "Status::occsWentActive: Sending Idle Power Save Config data failed with status {}",
Chris Cain78e86012021-03-04 16:15:31 -0600275 status)
276 .c_str());
277 }
278}
279
Chris Cain17257672021-10-22 13:41:03 -0500280// Send Ambient and Altitude to the OCC
281CmdStatus Status::sendAmbient(const uint8_t inTemp, const uint16_t inAltitude)
282{
283 CmdStatus status = CmdStatus::FAILURE;
284 bool ambientValid = true;
285 uint8_t ambientTemp = inTemp;
286 uint16_t altitude = inAltitude;
287
288 if (ambientTemp == 0xFF)
289 {
290 // Get latest readings from manager
291 manager.getAmbientData(ambientValid, ambientTemp, altitude);
292 log<level::DEBUG>(
293 fmt::format("sendAmbient: valid: {}, Ambient: {}C, altitude: {}m",
294 ambientValid, ambientTemp, altitude)
295 .c_str());
296 }
297
298 std::vector<std::uint8_t> cmd, rsp;
299 cmd.reserve(11);
300 cmd.push_back(uint8_t(CmdType::SEND_AMBIENT));
301 cmd.push_back(0x00); // Data Length (2 bytes)
302 cmd.push_back(0x08); //
303 cmd.push_back(0x00); // Version
304 cmd.push_back(ambientValid ? 0 : 0xFF); // Ambient Status
305 cmd.push_back(ambientTemp); // Ambient Temperature
306 cmd.push_back(altitude >> 8); // Altitude in meters (2 bytes)
307 cmd.push_back(altitude & 0xFF); //
308 cmd.push_back(0x00); // Reserved (3 bytes)
309 cmd.push_back(0x00);
310 cmd.push_back(0x00);
311 log<level::DEBUG>(fmt::format("sendAmbient: SEND_AMBIENT "
312 "command to OCC{} ({} bytes)",
313 instance, cmd.size())
314 .c_str());
315 status = occCmd.send(cmd, rsp);
316 if (status == CmdStatus::SUCCESS)
317 {
318 if (rsp.size() == 5)
319 {
320 if (RspStatus::SUCCESS != RspStatus(rsp[2]))
321 {
322 log<level::ERR>(
323 fmt::format(
324 "sendAmbient: SEND_AMBIENT failed with status 0x{:02X}",
325 rsp[2])
326 .c_str());
327 dump_hex(rsp);
328 status = CmdStatus::FAILURE;
329 }
330 }
331 else
332 {
333 log<level::ERR>("sendAmbient: INVALID SEND_AMBIENT response");
334 dump_hex(rsp);
335 status = CmdStatus::FAILURE;
336 }
337 }
338 else
339 {
340 if (status == CmdStatus::OPEN_FAILURE)
341 {
342 // OCC not active yet
343 status = CmdStatus::SUCCESS;
344 }
345 else
346 {
347 log<level::ERR>("sendAmbient: SEND_AMBIENT FAILED!");
348 }
349 }
350
351 return status;
352}
Chris Caina7b74dc2021-11-10 17:03:43 -0600353
354// Called when safe timer expires to determine if OCCs need to be reset
355void Status::safeStateDelayExpired()
356{
357 if (this->occActive())
358 {
359 log<level::INFO>(
360 fmt::format(
361 "safeStateDelayExpired: OCC{} is in SAFE state, requesting reset",
362 instance)
363 .c_str());
364 // Disable and reset to try recovering
365 deviceError();
366 }
367}
Chris Cain78e86012021-03-04 16:15:31 -0600368#endif // POWER10
369
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530370} // namespace occ
371} // namespace open_power