blob: 5abf6ddd7d55cf26aa08d478b6aad25ca9cc0d1c [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
Chris Cain1be43372021-12-09 19:29:37 -060042 if (this->managerCallBack)
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +053043 {
Chris Cain1be43372021-12-09 19:29:37 -060044 this->managerCallBack(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 Cain1be43372021-12-09 19:29:37 -060050 if (pmode && device.master())
Chris Cain36f9cde2021-11-22 11:18:21 -060051 {
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.
Chris Cain1be43372021-12-09 19:29:37 -060063 if (this->managerCallBack)
Chris Cain36f9cde2021-11-22 11:18:21 -060064 {
Chris Cain1be43372021-12-09 19:29:37 -060065 this->managerCallBack(value);
Chris Cain36f9cde2021-11-22 11:18:21 -060066 }
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
Chris Cain1be43372021-12-09 19:29:37 -0600112 if (pmode && device.master())
113 {
114 // Prevent mode changes
115 pmode->setMasterActive(false);
116 }
Chris Cain36f9cde2021-11-22 11:18:21 -0600117#endif
118
Eddie Jamescbad2192021-10-07 09:39:39 -0500119 // This would deem OCC inactive
120 this->occActive(false);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530121
Eddie Jamescbad2192021-10-07 09:39:39 -0500122 // Reset the OCC
123 this->resetOCC();
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530124}
125
126// Sends message to host control command handler to reset OCC
127void Status::resetOCC()
128{
Chris Caina8857c52021-01-27 11:53:05 -0600129 log<level::INFO>(
130 fmt::format(">>Status::resetOCC() - requesting reset for OCC{}",
131 instance)
132 .c_str());
Tom Joseph00325232020-07-29 17:51:48 +0530133#ifdef PLDM
134 if (resetCallBack)
135 {
136 this->resetCallBack(instance);
137 }
138#else
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530139 constexpr auto CONTROL_HOST_PATH = "/org/open_power/control/host0";
140 constexpr auto CONTROL_HOST_INTF = "org.open_power.Control.Host";
141
142 // This will throw exception on failure
George Liuf3b75142021-06-10 11:22:50 +0800143 auto service = utils::getService(CONTROL_HOST_PATH, CONTROL_HOST_INTF);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530144
George Liuf3b75142021-06-10 11:22:50 +0800145 auto& bus = utils::getBus();
Gunnar Mills94df8c92018-09-14 14:50:03 -0500146 auto method = bus.new_method_call(service.c_str(), CONTROL_HOST_PATH,
147 CONTROL_HOST_INTF, "Execute");
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530148 // OCC Reset control command
Gunnar Mills94df8c92018-09-14 14:50:03 -0500149 method.append(convertForMessage(Control::Host::Command::OCCReset).c_str());
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530150
151 // OCC Sensor ID for callout reasons
Patrick Williamse0962702020-05-13 17:50:22 -0500152 method.append(std::variant<uint8_t>(std::get<0>(sensorMap.at(instance))));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530153 bus.call_noreply(method);
154 return;
Tom Joseph00325232020-07-29 17:51:48 +0530155#endif
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530156}
157
158// Handler called by Host control command handler to convey the
159// status of the executed command
160void Status::hostControlEvent(sdbusplus::message::message& msg)
161{
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530162 std::string cmdCompleted{};
163 std::string cmdStatus{};
164
165 msg.read(cmdCompleted, cmdStatus);
166
167 log<level::DEBUG>("Host control signal values",
Gunnar Mills94df8c92018-09-14 14:50:03 -0500168 entry("COMMAND=%s", cmdCompleted.c_str()),
169 entry("STATUS=%s", cmdStatus.c_str()));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530170
Gunnar Mills94df8c92018-09-14 14:50:03 -0500171 if (Control::Host::convertResultFromString(cmdStatus) !=
172 Control::Host::Result::Success)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530173 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500174 if (Control::Host::convertCommandFromString(cmdCompleted) ==
175 Control::Host::Command::OCCReset)
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530176 {
Gunnar Mills85e65202018-04-08 15:01:54 -0500177 // Must be a Timeout. Log an Error trace
Alexander Filippov1d69e192019-03-21 18:12:07 +0300178 log<level::ERR>(
179 "Error resetting the OCC.", entry("PATH=%s", path.c_str()),
180 entry("SENSORID=0x%X", std::get<0>(sensorMap.at(instance))));
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530181 }
182 }
183 return;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530184}
185
Chris Caina8857c52021-01-27 11:53:05 -0600186void Status::readOccState()
187{
188 unsigned int state;
189 const fs::path filename =
190 fs::path(DEV_PATH) /
191 fs::path(sysfsName + "." + std::to_string(instance + 1)) / "occ_state";
192
Chris Caina8857c52021-01-27 11:53:05 -0600193 std::ifstream file(filename, std::ios::in);
194 const int open_errno = errno;
195 if (file)
196 {
197 file >> state;
198 if (state != lastState)
199 {
200 // Trace OCC state changes
201 log<level::INFO>(
202 fmt::format("Status::readOccState: OCC{} state 0x{:02X}",
203 instance, state)
204 .c_str());
205 lastState = state;
Chris Cain78e86012021-03-04 16:15:31 -0600206
207#ifdef POWER10
Chris Cain17257672021-10-22 13:41:03 -0500208 if (OccState(state) == OccState::ACTIVE)
209 {
Chris Cain1be43372021-12-09 19:29:37 -0600210 if (pmode && device.master())
Chris Caina7b74dc2021-11-10 17:03:43 -0600211 {
Chris Cain6fa848a2022-01-24 14:54:38 -0600212 // Set the master OCC on the PowerMode object
213 pmode->setMasterOcc(path);
214 // Enable mode changes
Chris Cain36f9cde2021-11-22 11:18:21 -0600215 pmode->setMasterActive();
216
Chris Caina7b74dc2021-11-10 17:03:43 -0600217 // Special processing by master OCC when it goes active
218 occsWentActive();
219 }
220
Chris Cain17257672021-10-22 13:41:03 -0500221 CmdStatus status = sendAmbient();
222 if (status != CmdStatus::SUCCESS)
223 {
224 log<level::ERR>(
225 fmt::format(
226 "readOccState: Sending Ambient failed with status {}",
227 status)
228 .c_str());
229 }
230 }
Chris Caina7b74dc2021-11-10 17:03:43 -0600231
232 if (OccState(state) == OccState::SAFE)
233 {
234 // start safe delay timer (before requesting reset)
235 using namespace std::literals::chrono_literals;
236 safeStateDelayTimer.restartOnce(60s);
237 }
238 else if (safeStateDelayTimer.isEnabled())
239 {
240 // stop safe delay timer (no longer in SAFE state)
241 safeStateDelayTimer.setEnabled(false);
242 }
Chris Cain78e86012021-03-04 16:15:31 -0600243#endif
Chris Caina8857c52021-01-27 11:53:05 -0600244 }
245 file.close();
246 }
247 else
248 {
249 // If not able to read, OCC may be offline
250 log<level::DEBUG>(
251 fmt::format("Status::readOccState: open failed (errno={})",
252 open_errno)
253 .c_str());
254 lastState = 0;
255 }
256}
257
Chris Cain78e86012021-03-04 16:15:31 -0600258#ifdef POWER10
Chris Cain78e86012021-03-04 16:15:31 -0600259// Special processing that needs to happen once the OCCs change to ACTIVE state
260void Status::occsWentActive()
261{
262 CmdStatus status = CmdStatus::SUCCESS;
263
Chris Cain36f9cde2021-11-22 11:18:21 -0600264 status = pmode->sendModeChange();
Chris Cain78e86012021-03-04 16:15:31 -0600265 if (status != CmdStatus::SUCCESS)
266 {
George Liub5ca1012021-09-10 12:53:11 +0800267 log<level::ERR>(
268 fmt::format(
269 "Status::occsWentActive: OCC mode change failed with status {}",
270 status)
271 .c_str());
Chris Cain78e86012021-03-04 16:15:31 -0600272 }
273
Chris Cain36f9cde2021-11-22 11:18:21 -0600274 status = pmode->sendIpsData();
Chris Cain78e86012021-03-04 16:15:31 -0600275 if (status != CmdStatus::SUCCESS)
276 {
277 log<level::ERR>(
278 fmt::format(
George Liub5ca1012021-09-10 12:53:11 +0800279 "Status::occsWentActive: Sending Idle Power Save Config data failed with status {}",
Chris Cain78e86012021-03-04 16:15:31 -0600280 status)
281 .c_str());
282 }
283}
284
Chris Cain17257672021-10-22 13:41:03 -0500285// Send Ambient and Altitude to the OCC
286CmdStatus Status::sendAmbient(const uint8_t inTemp, const uint16_t inAltitude)
287{
288 CmdStatus status = CmdStatus::FAILURE;
289 bool ambientValid = true;
290 uint8_t ambientTemp = inTemp;
291 uint16_t altitude = inAltitude;
292
293 if (ambientTemp == 0xFF)
294 {
295 // Get latest readings from manager
296 manager.getAmbientData(ambientValid, ambientTemp, altitude);
297 log<level::DEBUG>(
298 fmt::format("sendAmbient: valid: {}, Ambient: {}C, altitude: {}m",
299 ambientValid, ambientTemp, altitude)
300 .c_str());
301 }
302
303 std::vector<std::uint8_t> cmd, rsp;
304 cmd.reserve(11);
305 cmd.push_back(uint8_t(CmdType::SEND_AMBIENT));
306 cmd.push_back(0x00); // Data Length (2 bytes)
307 cmd.push_back(0x08); //
308 cmd.push_back(0x00); // Version
309 cmd.push_back(ambientValid ? 0 : 0xFF); // Ambient Status
310 cmd.push_back(ambientTemp); // Ambient Temperature
311 cmd.push_back(altitude >> 8); // Altitude in meters (2 bytes)
312 cmd.push_back(altitude & 0xFF); //
313 cmd.push_back(0x00); // Reserved (3 bytes)
314 cmd.push_back(0x00);
315 cmd.push_back(0x00);
316 log<level::DEBUG>(fmt::format("sendAmbient: SEND_AMBIENT "
317 "command to OCC{} ({} bytes)",
318 instance, cmd.size())
319 .c_str());
320 status = occCmd.send(cmd, rsp);
321 if (status == CmdStatus::SUCCESS)
322 {
323 if (rsp.size() == 5)
324 {
325 if (RspStatus::SUCCESS != RspStatus(rsp[2]))
326 {
327 log<level::ERR>(
328 fmt::format(
329 "sendAmbient: SEND_AMBIENT failed with status 0x{:02X}",
330 rsp[2])
331 .c_str());
332 dump_hex(rsp);
333 status = CmdStatus::FAILURE;
334 }
335 }
336 else
337 {
338 log<level::ERR>("sendAmbient: INVALID SEND_AMBIENT response");
339 dump_hex(rsp);
340 status = CmdStatus::FAILURE;
341 }
342 }
343 else
344 {
345 if (status == CmdStatus::OPEN_FAILURE)
346 {
347 // OCC not active yet
348 status = CmdStatus::SUCCESS;
349 }
350 else
351 {
352 log<level::ERR>("sendAmbient: SEND_AMBIENT FAILED!");
353 }
354 }
355
356 return status;
357}
Chris Caina7b74dc2021-11-10 17:03:43 -0600358
359// Called when safe timer expires to determine if OCCs need to be reset
360void Status::safeStateDelayExpired()
361{
362 if (this->occActive())
363 {
364 log<level::INFO>(
365 fmt::format(
366 "safeStateDelayExpired: OCC{} is in SAFE state, requesting reset",
367 instance)
368 .c_str());
369 // Disable and reset to try recovering
370 deviceError();
371 }
372}
Chris Cain78e86012021-03-04 16:15:31 -0600373#endif // POWER10
374
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530375} // namespace occ
376} // namespace open_power