blob: 49aaf3670bb193098de03b5b4f9327a519a35dd0 [file] [log] [blame]
Josh D. Kingbdd9cb72016-12-19 11:13:43 -06001#include "bmc_state_manager.hpp"
Andrew Geisslere426b582020-05-28 12:40:55 -05002
Andrew Geissler98e64e62022-01-25 16:02:56 -06003#include "utils.hpp"
Andrew Geissler2f60aae2019-09-12 13:25:21 -05004#include "xyz/openbmc_project/Common/error.hpp"
Josh D. Kingbdd9cb72016-12-19 11:13:43 -06005
Andrew Geissler98e64e62022-01-25 16:02:56 -06006#include <gpiod.h>
Andrew Geisslere426b582020-05-28 12:40:55 -05007#include <sys/sysinfo.h>
8
9#include <phosphor-logging/elog-errors.hpp>
Andrew Geissler429100a2021-09-09 12:50:24 -050010#include <phosphor-logging/lg2.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050011#include <sdbusplus/exception.hpp>
12
13#include <cassert>
Tim Lee2bfb1ef2021-03-17 09:50:35 +080014#include <filesystem>
15#include <fstream>
16#include <iostream>
Andrew Geisslere426b582020-05-28 12:40:55 -050017
Josh D. Kingbdd9cb72016-12-19 11:13:43 -060018namespace phosphor
19{
20namespace state
21{
22namespace manager
23{
24
Andrew Geissler429100a2021-09-09 12:50:24 -050025PHOSPHOR_LOG2_USING;
26
Josh D. King6db38222016-12-19 14:52:40 -060027// When you see server:: you know we're referencing our base class
28namespace server = sdbusplus::xyz::openbmc_project::State::server;
29
30using namespace phosphor::logging;
Andrew Geissler2f60aae2019-09-12 13:25:21 -050031using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Josh D. King6db38222016-12-19 14:52:40 -060032
Andrew Geissler9eb0e442022-02-18 10:21:15 -060033constexpr auto obmcQuiesceTarget = "obmc-bmc-service-quiesce@0.target";
Anthony Wilsoneef31f82019-04-23 17:04:09 -050034constexpr auto obmcStandbyTarget = "multi-user.target";
Josh D. Kingd613b812016-12-19 16:47:45 -060035constexpr auto signalDone = "done";
Josh D. Kingd3e58472017-02-02 11:09:11 -060036constexpr auto activeState = "active";
Josh D. Kingd613b812016-12-19 16:47:45 -060037
Josh D. King5162a7b2016-12-19 16:15:00 -060038/* Map a transition to it's systemd target */
Andrew Geissler58a18012018-01-19 19:36:05 -080039const std::map<server::BMC::Transition, const char*> SYSTEMD_TABLE = {
40 {server::BMC::Transition::Reboot, "reboot.target"}};
Josh D. King5162a7b2016-12-19 16:15:00 -060041
Andrew Geissler58a18012018-01-19 19:36:05 -080042constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
43constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
44constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Josh D. Kingd3e58472017-02-02 11:09:11 -060045constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties";
Josh D. Kingd3e58472017-02-02 11:09:11 -060046
Andrew Geissler2774c782022-02-17 16:57:14 -060047std::string BMC::getUnitState(const std::string& unitToCheck)
Josh D. Kingd3e58472017-02-02 11:09:11 -060048{
Patrick Williams2975e262020-05-13 18:01:09 -050049 std::variant<std::string> currentState;
Josh D. King2b5d8872017-02-21 13:37:17 -060050 sdbusplus::message::object_path unitTargetPath;
Josh D. Kingd3e58472017-02-02 11:09:11 -060051
Andrew Geissler58a18012018-01-19 19:36:05 -080052 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
53 SYSTEMD_INTERFACE, "GetUnit");
Josh D. King2b5d8872017-02-21 13:37:17 -060054
Andrew Geissler2774c782022-02-17 16:57:14 -060055 method.append(unitToCheck);
Josh D. King2b5d8872017-02-21 13:37:17 -060056
Anthony Wilson32c532e2018-10-25 21:56:07 -050057 try
Josh D. King2b5d8872017-02-21 13:37:17 -060058 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050059 auto result = this->bus.call(method);
60 result.read(unitTargetPath);
61 }
Patrick Williams0a675212021-09-02 09:49:43 -050062 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -050063 {
Andrew Geissler37d36312022-03-09 16:24:45 -060064 // Not all input units will have been loaded yet so just return an
65 // empty string if an exception is caught in this path
66 info("Unit {UNIT} not found: {ERROR}", "UNIT", unitToCheck, "ERROR", e);
Andrew Geissler2774c782022-02-17 16:57:14 -060067 return std::string{};
Josh D. King2b5d8872017-02-21 13:37:17 -060068 }
69
Andrew Geissler58a18012018-01-19 19:36:05 -080070 method = this->bus.new_method_call(
71 SYSTEMD_SERVICE,
72 static_cast<const std::string&>(unitTargetPath).c_str(),
73 SYSTEMD_PRP_INTERFACE, "Get");
Josh D. Kingd3e58472017-02-02 11:09:11 -060074
75 method.append("org.freedesktop.systemd1.Unit", "ActiveState");
76
Anthony Wilson32c532e2018-10-25 21:56:07 -050077 try
Josh D. King2b5d8872017-02-21 13:37:17 -060078 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050079 auto result = this->bus.call(method);
80
Andrew Geissler37d36312022-03-09 16:24:45 -060081 // Is input target active or inactive?
Anthony Wilson32c532e2018-10-25 21:56:07 -050082 result.read(currentState);
83 }
Patrick Williams0a675212021-09-02 09:49:43 -050084 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -050085 {
Andrew Geissler429100a2021-09-09 12:50:24 -050086 info("Error in ActiveState Get: {ERROR}", "ERROR", e);
Andrew Geissler2774c782022-02-17 16:57:14 -060087 return std::string{};
Josh D. King2b5d8872017-02-21 13:37:17 -060088 }
Andrew Geissler2774c782022-02-17 16:57:14 -060089 return (std::get<std::string>(currentState));
90}
Josh D. Kingd3e58472017-02-02 11:09:11 -060091
Andrew Geissler2774c782022-02-17 16:57:14 -060092void BMC::discoverInitialState()
93{
Andrew Geissler9eb0e442022-02-18 10:21:15 -060094
95 // First look to see if the BMC quiesce target is active
96 auto currentStateStr = getUnitState(obmcQuiesceTarget);
97 if (currentStateStr == activeState)
98 {
99 info("Setting the BMCState field to BMC_QUIESCED");
100 this->currentBMCState(BMCState::Quiesced);
101 return;
102 }
103
104 // If not quiesced, then check standby target
105 currentStateStr = getUnitState(obmcStandbyTarget);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500106 if (currentStateStr == activeState)
Josh D. Kingd3e58472017-02-02 11:09:11 -0600107 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500108 info("Setting the BMCState field to BMC_READY");
Josh D. Kingd3e58472017-02-02 11:09:11 -0600109 this->currentBMCState(BMCState::Ready);
Josh D. Kingd3e58472017-02-02 11:09:11 -0600110 }
111 else
112 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500113 info("Setting the BMCState field to BMC_NOTREADY");
Josh D. Kingd3e58472017-02-02 11:09:11 -0600114 this->currentBMCState(BMCState::NotReady);
115 }
116
117 return;
118}
119
Josh D. King6db38222016-12-19 14:52:40 -0600120void BMC::subscribeToSystemdSignals()
121{
Andrew Geissler58a18012018-01-19 19:36:05 -0800122 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
123 SYSTEMD_INTERFACE, "Subscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500124
125 try
126 {
127 this->bus.call(method);
128 }
Patrick Williams0a675212021-09-02 09:49:43 -0500129 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500130 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500131 error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", e);
Andrew Geissler2f60aae2019-09-12 13:25:21 -0500132 elog<InternalFailure>();
Anthony Wilson32c532e2018-10-25 21:56:07 -0500133 }
Josh D. King6db38222016-12-19 14:52:40 -0600134
135 return;
136}
137
Josh D. King5162a7b2016-12-19 16:15:00 -0600138void BMC::executeTransition(const Transition tranReq)
139{
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000140 // HardReboot does not shutdown any services and immediately transitions
141 // into the reboot process
142 if (server::BMC::Transition::HardReboot == tranReq)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500143 {
Andrew Geissler4ee59462022-04-28 16:58:26 -0400144 // Put BMC state not NotReady when issuing a BMC reboot
145 // and stop monitoring for state changes
146 this->currentBMCState(BMCState::NotReady);
147 this->stateSignal.reset();
148
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000149 auto method = this->bus.new_method_call(
150 SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "Reboot");
151 try
152 {
153 this->bus.call(method);
154 }
Patrick Williams0a675212021-09-02 09:49:43 -0500155 catch (const sdbusplus::exception::exception& e)
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000156 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500157 info("Error in HardReboot: {ERROR}", "ERROR", e);
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000158 }
Anthony Wilson32c532e2018-10-25 21:56:07 -0500159 }
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000160 else
Anthony Wilson32c532e2018-10-25 21:56:07 -0500161 {
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000162 // Check to make sure it can be found
163 auto iter = SYSTEMD_TABLE.find(tranReq);
164 if (iter == SYSTEMD_TABLE.end())
165 return;
Anthony Wilson32c532e2018-10-25 21:56:07 -0500166
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000167 const auto& sysdUnit = iter->second;
168
169 auto method = this->bus.new_method_call(
170 SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "StartUnit");
171 // The only valid transition is reboot and that
172 // needs to be irreversible once started
173
174 method.append(sysdUnit, "replace-irreversibly");
175
Andrew Geissler4ee59462022-04-28 16:58:26 -0400176 // Put BMC state not NotReady when issuing a BMC reboot
177 // and stop monitoring for state changes
178 this->currentBMCState(BMCState::NotReady);
179 this->stateSignal.reset();
180
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000181 try
182 {
183 this->bus.call(method);
184 }
Patrick Williams0a675212021-09-02 09:49:43 -0500185 catch (const sdbusplus::exception::exception& e)
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000186 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500187 info("Error in StartUnit - replace-irreversibly: {ERROR}", "ERROR",
188 e);
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000189 }
190 }
Josh D. King5162a7b2016-12-19 16:15:00 -0600191 return;
192}
193
Patrick Williamsd32f8182017-05-05 15:55:24 -0500194int BMC::bmcStateChange(sdbusplus::message::message& msg)
Josh D. Kingd613b812016-12-19 16:47:45 -0600195{
Andrew Geissler58a18012018-01-19 19:36:05 -0800196 uint32_t newStateID{};
Josh D. Kingd613b812016-12-19 16:47:45 -0600197 sdbusplus::message::object_path newStateObjPath;
198 std::string newStateUnit{};
199 std::string newStateResult{};
200
Andrew Geissler58a18012018-01-19 19:36:05 -0800201 // Read the msg and populate each variable
Patrick Williamsd32f8182017-05-05 15:55:24 -0500202 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
Josh D. Kingd613b812016-12-19 16:47:45 -0600203
Andrew Geissler9eb0e442022-02-18 10:21:15 -0600204 if ((newStateUnit == obmcQuiesceTarget) && (newStateResult == signalDone))
Josh D. Kingd613b812016-12-19 16:47:45 -0600205 {
Andrew Geissler9eb0e442022-02-18 10:21:15 -0600206 error("BMC has entered BMC_QUIESCED state");
207 this->currentBMCState(BMCState::Quiesced);
Josh D. Kingd613b812016-12-19 16:47:45 -0600208
Andrew Geissler9eb0e442022-02-18 10:21:15 -0600209 // There is no getting out of Quiesced once entered (other then BMC
210 // reboot) so stop watching for signals
Andrew Geissler58a18012018-01-19 19:36:05 -0800211 auto method =
212 this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
213 SYSTEMD_INTERFACE, "Unsubscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500214
215 try
216 {
217 this->bus.call(method);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500218 }
Patrick Williams0a675212021-09-02 09:49:43 -0500219 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500220 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500221 info("Error in Unsubscribe: {ERROR}", "ERROR", e);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500222 }
Andrew Geissler9eb0e442022-02-18 10:21:15 -0600223
Andrew Geissler71e538f2022-03-21 11:22:43 -0500224 // disable the system state change object as well
NodeMan978c26f0e2022-04-27 20:15:22 -0500225 this->stateSignal.reset();
Andrew Geissler71e538f2022-03-21 11:22:43 -0500226
Andrew Geissler9eb0e442022-02-18 10:21:15 -0600227 return 0;
228 }
229
230 // Caught the signal that indicates the BMC is now BMC_READY
231 if ((newStateUnit == obmcStandbyTarget) && (newStateResult == signalDone))
232 {
233 info("BMC_READY");
234 this->currentBMCState(BMCState::Ready);
Josh D. Kingd613b812016-12-19 16:47:45 -0600235 }
236
237 return 0;
238}
239
Josh D. King6db38222016-12-19 14:52:40 -0600240BMC::Transition BMC::requestedBMCTransition(Transition value)
241{
Andrew Geissler429100a2021-09-09 12:50:24 -0500242 info("Setting the RequestedBMCTransition field to "
243 "{REQUESTED_BMC_TRANSITION}",
244 "REQUESTED_BMC_TRANSITION", value);
Josh D. King6db38222016-12-19 14:52:40 -0600245
Josh D. King5162a7b2016-12-19 16:15:00 -0600246 executeTransition(value);
247 return server::BMC::requestedBMCTransition(value);
Josh D. King6db38222016-12-19 14:52:40 -0600248}
249
Josh D. Kingd613b812016-12-19 16:47:45 -0600250BMC::BMCState BMC::currentBMCState(BMCState value)
251{
Andrew Geissler429100a2021-09-09 12:50:24 -0500252
253 info("Setting the BMCState field to {CURRENT_BMC_STATE}",
254 "CURRENT_BMC_STATE", value);
Josh D. Kingd613b812016-12-19 16:47:45 -0600255
256 return server::BMC::currentBMCState(value);
257}
258
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800259BMC::RebootCause BMC::lastRebootCause(RebootCause value)
260{
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500261 info("Setting the RebootCause field to {LAST_REBOOT_CAUSE}",
262 "LAST_REBOOT_CAUSE", value);
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800263
264 return server::BMC::lastRebootCause(value);
265}
266
Matt Spinlere6710b72018-07-12 16:05:55 -0500267uint64_t BMC::lastRebootTime() const
268{
269 using namespace std::chrono;
270 struct sysinfo info;
271
272 auto rc = sysinfo(&info);
273 assert(rc == 0);
274
275 // Since uptime is in seconds, also get the current time in seconds.
276 auto now = time_point_cast<seconds>(system_clock::now());
277 auto rebootTime = now - seconds(info.uptime);
278
279 return duration_cast<milliseconds>(rebootTime.time_since_epoch()).count();
280}
281
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800282void BMC::discoverLastRebootCause()
283{
284 uint64_t bootReason = 0;
285 std::ifstream file;
286 auto bootstatusPath = "/sys/class/watchdog/watchdog0/bootstatus";
287
288 file.exceptions(std::ifstream::failbit | std::ifstream::badbit |
289 std::ifstream::eofbit);
290
291 try
292 {
293 file.open(bootstatusPath);
294 file >> bootReason;
295 }
296 catch (const std::exception& e)
297 {
298 auto rc = errno;
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500299 error("Failed to read sysfs file {FILE} with errno {ERRNO}", "FILE",
300 bootstatusPath, "ERRNO", rc);
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800301 }
302
303 switch (bootReason)
304 {
305 case WDIOF_EXTERN1:
306 this->lastRebootCause(RebootCause::Watchdog);
Andrew Geissler98e64e62022-01-25 16:02:56 -0600307 return;
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800308 case WDIOF_CARDRESET:
309 this->lastRebootCause(RebootCause::POR);
Andrew Geissler98e64e62022-01-25 16:02:56 -0600310 return;
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800311 default:
312 this->lastRebootCause(RebootCause::Unknown);
Andrew Geissler98e64e62022-01-25 16:02:56 -0600313 // Continue below to see if more details can be found
314 // on reason for reboot
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800315 break;
316 }
317
Andrew Geissler98e64e62022-01-25 16:02:56 -0600318 // If the above code could not detect a reason, look for a the
319 // reset-cause-pinhole gpio to see if it is the reason for the reboot
320 auto gpioval =
321 phosphor::state::manager::utils::getGpioValue("reset-cause-pinhole");
322
Andrew Geissler2e352a22022-03-02 11:31:40 -0600323 // A 0 indicates a pinhole reset occurred
324 if (0 == gpioval)
Andrew Geissler98e64e62022-01-25 16:02:56 -0600325 {
326 info("The BMC reset was caused by a pinhole reset");
327 this->lastRebootCause(RebootCause::PinholeReset);
Andrew Geisslera2a7e122022-01-26 13:30:18 -0600328
329 // Generate log telling user a pinhole reset has occurred
330 const std::string errorMsg = "xyz.openbmc_project.State.PinholeReset";
331 phosphor::state::manager::utils::createError(
332 this->bus, errorMsg,
333 sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
334 Notice);
Andrew Geissler98e64e62022-01-25 16:02:56 -0600335 }
336
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800337 return;
338}
339
Josh D. Kingbdd9cb72016-12-19 11:13:43 -0600340} // namespace manager
341} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -0700342} // namespace phosphor