blob: c3c1c7d1e62016489009233e0afabfe685b942bc [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 Geissler2f60aae2019-09-12 13:25:21 -05003#include "xyz/openbmc_project/Common/error.hpp"
Josh D. Kingbdd9cb72016-12-19 11:13:43 -06004
Andrew Geisslere426b582020-05-28 12:40:55 -05005#include <sys/sysinfo.h>
6
7#include <phosphor-logging/elog-errors.hpp>
Andrew Geissler429100a2021-09-09 12:50:24 -05008#include <phosphor-logging/lg2.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -05009#include <sdbusplus/exception.hpp>
10
11#include <cassert>
Tim Lee2bfb1ef2021-03-17 09:50:35 +080012#include <filesystem>
13#include <fstream>
14#include <iostream>
Andrew Geisslere426b582020-05-28 12:40:55 -050015
Josh D. Kingbdd9cb72016-12-19 11:13:43 -060016namespace phosphor
17{
18namespace state
19{
20namespace manager
21{
22
Andrew Geissler429100a2021-09-09 12:50:24 -050023PHOSPHOR_LOG2_USING;
24
Josh D. King6db38222016-12-19 14:52:40 -060025// When you see server:: you know we're referencing our base class
26namespace server = sdbusplus::xyz::openbmc_project::State::server;
27
28using namespace phosphor::logging;
Andrew Geissler2f60aae2019-09-12 13:25:21 -050029using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Josh D. King6db38222016-12-19 14:52:40 -060030
Anthony Wilsoneef31f82019-04-23 17:04:09 -050031constexpr auto obmcStandbyTarget = "multi-user.target";
Josh D. Kingd613b812016-12-19 16:47:45 -060032constexpr auto signalDone = "done";
Josh D. Kingd3e58472017-02-02 11:09:11 -060033constexpr auto activeState = "active";
Josh D. Kingd613b812016-12-19 16:47:45 -060034
Josh D. King5162a7b2016-12-19 16:15:00 -060035/* Map a transition to it's systemd target */
Andrew Geissler58a18012018-01-19 19:36:05 -080036const std::map<server::BMC::Transition, const char*> SYSTEMD_TABLE = {
37 {server::BMC::Transition::Reboot, "reboot.target"}};
Josh D. King5162a7b2016-12-19 16:15:00 -060038
Andrew Geissler58a18012018-01-19 19:36:05 -080039constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
40constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
41constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Josh D. Kingd3e58472017-02-02 11:09:11 -060042constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties";
Josh D. Kingd3e58472017-02-02 11:09:11 -060043
44void BMC::discoverInitialState()
45{
Patrick Williams2975e262020-05-13 18:01:09 -050046 std::variant<std::string> currentState;
Josh D. King2b5d8872017-02-21 13:37:17 -060047 sdbusplus::message::object_path unitTargetPath;
Josh D. Kingd3e58472017-02-02 11:09:11 -060048
Andrew Geissler58a18012018-01-19 19:36:05 -080049 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
50 SYSTEMD_INTERFACE, "GetUnit");
Josh D. King2b5d8872017-02-21 13:37:17 -060051
52 method.append(obmcStandbyTarget);
53
Anthony Wilson32c532e2018-10-25 21:56:07 -050054 try
Josh D. King2b5d8872017-02-21 13:37:17 -060055 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050056 auto result = this->bus.call(method);
57 result.read(unitTargetPath);
58 }
Patrick Williams0a675212021-09-02 09:49:43 -050059 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -050060 {
Andrew Geissler429100a2021-09-09 12:50:24 -050061 error("Error in GetUnit call: {ERROR}", "ERROR", e);
Josh D. King2b5d8872017-02-21 13:37:17 -060062 return;
63 }
64
Andrew Geissler58a18012018-01-19 19:36:05 -080065 method = this->bus.new_method_call(
66 SYSTEMD_SERVICE,
67 static_cast<const std::string&>(unitTargetPath).c_str(),
68 SYSTEMD_PRP_INTERFACE, "Get");
Josh D. Kingd3e58472017-02-02 11:09:11 -060069
70 method.append("org.freedesktop.systemd1.Unit", "ActiveState");
71
Anthony Wilson32c532e2018-10-25 21:56:07 -050072 try
Josh D. King2b5d8872017-02-21 13:37:17 -060073 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050074 auto result = this->bus.call(method);
75
76 // Is obmc-standby.target active or inactive?
77 result.read(currentState);
78 }
Patrick Williams0a675212021-09-02 09:49:43 -050079 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -050080 {
Andrew Geissler429100a2021-09-09 12:50:24 -050081 info("Error in ActiveState Get: {ERROR}", "ERROR", e);
Josh D. King2b5d8872017-02-21 13:37:17 -060082 return;
83 }
Josh D. Kingd3e58472017-02-02 11:09:11 -060084
Patrick Williams37413dc2020-05-13 11:29:54 -050085 auto currentStateStr = std::get<std::string>(currentState);
Anthony Wilson32c532e2018-10-25 21:56:07 -050086 if (currentStateStr == activeState)
Josh D. Kingd3e58472017-02-02 11:09:11 -060087 {
Andrew Geissler429100a2021-09-09 12:50:24 -050088 info("Setting the BMCState field to BMC_READY");
Josh D. Kingd3e58472017-02-02 11:09:11 -060089 this->currentBMCState(BMCState::Ready);
90
Andrew Geissler58a18012018-01-19 19:36:05 -080091 // Unsubscribe so we stop processing all other signals
92 method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
93 SYSTEMD_INTERFACE, "Unsubscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -050094 try
95 {
96 this->bus.call(method);
97 this->stateSignal.release();
98 }
Patrick Williams0a675212021-09-02 09:49:43 -050099 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500100 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500101 info("Error in Unsubscribe: {ERROR}", "ERROR", e);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500102 }
Josh D. Kingd3e58472017-02-02 11:09:11 -0600103 }
104 else
105 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500106 info("Setting the BMCState field to BMC_NOTREADY");
Josh D. Kingd3e58472017-02-02 11:09:11 -0600107 this->currentBMCState(BMCState::NotReady);
108 }
109
110 return;
111}
112
Josh D. King6db38222016-12-19 14:52:40 -0600113void BMC::subscribeToSystemdSignals()
114{
Andrew Geissler58a18012018-01-19 19:36:05 -0800115 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
116 SYSTEMD_INTERFACE, "Subscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500117
118 try
119 {
120 this->bus.call(method);
121 }
Patrick Williams0a675212021-09-02 09:49:43 -0500122 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500123 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500124 error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", e);
Andrew Geissler2f60aae2019-09-12 13:25:21 -0500125 elog<InternalFailure>();
Anthony Wilson32c532e2018-10-25 21:56:07 -0500126 }
Josh D. King6db38222016-12-19 14:52:40 -0600127
128 return;
129}
130
Josh D. King5162a7b2016-12-19 16:15:00 -0600131void BMC::executeTransition(const Transition tranReq)
132{
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000133 // HardReboot does not shutdown any services and immediately transitions
134 // into the reboot process
135 if (server::BMC::Transition::HardReboot == tranReq)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500136 {
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000137 auto method = this->bus.new_method_call(
138 SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "Reboot");
139 try
140 {
141 this->bus.call(method);
142 }
Patrick Williams0a675212021-09-02 09:49:43 -0500143 catch (const sdbusplus::exception::exception& e)
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000144 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500145 info("Error in HardReboot: {ERROR}", "ERROR", e);
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000146 }
Anthony Wilson32c532e2018-10-25 21:56:07 -0500147 }
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000148 else
Anthony Wilson32c532e2018-10-25 21:56:07 -0500149 {
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000150 // Check to make sure it can be found
151 auto iter = SYSTEMD_TABLE.find(tranReq);
152 if (iter == SYSTEMD_TABLE.end())
153 return;
Anthony Wilson32c532e2018-10-25 21:56:07 -0500154
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000155 const auto& sysdUnit = iter->second;
156
157 auto method = this->bus.new_method_call(
158 SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "StartUnit");
159 // The only valid transition is reboot and that
160 // needs to be irreversible once started
161
162 method.append(sysdUnit, "replace-irreversibly");
163
164 try
165 {
166 this->bus.call(method);
167 }
Patrick Williams0a675212021-09-02 09:49:43 -0500168 catch (const sdbusplus::exception::exception& e)
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000169 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500170 info("Error in StartUnit - replace-irreversibly: {ERROR}", "ERROR",
171 e);
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000172 }
173 }
Josh D. King5162a7b2016-12-19 16:15:00 -0600174 return;
175}
176
Patrick Williamsd32f8182017-05-05 15:55:24 -0500177int BMC::bmcStateChange(sdbusplus::message::message& msg)
Josh D. Kingd613b812016-12-19 16:47:45 -0600178{
Andrew Geissler58a18012018-01-19 19:36:05 -0800179 uint32_t newStateID{};
Josh D. Kingd613b812016-12-19 16:47:45 -0600180 sdbusplus::message::object_path newStateObjPath;
181 std::string newStateUnit{};
182 std::string newStateResult{};
183
Andrew Geissler58a18012018-01-19 19:36:05 -0800184 // Read the msg and populate each variable
Patrick Williamsd32f8182017-05-05 15:55:24 -0500185 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
Josh D. Kingd613b812016-12-19 16:47:45 -0600186
Andrew Geissler58a18012018-01-19 19:36:05 -0800187 // Caught the signal that indicates the BMC is now BMC_READY
188 if ((newStateUnit == obmcStandbyTarget) && (newStateResult == signalDone))
Josh D. Kingd613b812016-12-19 16:47:45 -0600189 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500190 info("BMC_READY");
Josh D. Kingd613b812016-12-19 16:47:45 -0600191 this->currentBMCState(BMCState::Ready);
192
Andrew Geissler58a18012018-01-19 19:36:05 -0800193 // Unsubscribe so we stop processing all other signals
194 auto method =
195 this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
196 SYSTEMD_INTERFACE, "Unsubscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500197
198 try
199 {
200 this->bus.call(method);
201 this->stateSignal.release();
202 }
Patrick Williams0a675212021-09-02 09:49:43 -0500203 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500204 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500205 info("Error in Unsubscribe: {ERROR}", "ERROR", e);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500206 }
Josh D. Kingd613b812016-12-19 16:47:45 -0600207 }
208
209 return 0;
210}
211
Josh D. King6db38222016-12-19 14:52:40 -0600212BMC::Transition BMC::requestedBMCTransition(Transition value)
213{
Andrew Geissler429100a2021-09-09 12:50:24 -0500214 info("Setting the RequestedBMCTransition field to "
215 "{REQUESTED_BMC_TRANSITION}",
216 "REQUESTED_BMC_TRANSITION", value);
Josh D. King6db38222016-12-19 14:52:40 -0600217
Josh D. King5162a7b2016-12-19 16:15:00 -0600218 executeTransition(value);
219 return server::BMC::requestedBMCTransition(value);
Josh D. King6db38222016-12-19 14:52:40 -0600220}
221
Josh D. Kingd613b812016-12-19 16:47:45 -0600222BMC::BMCState BMC::currentBMCState(BMCState value)
223{
Andrew Geissler429100a2021-09-09 12:50:24 -0500224
225 info("Setting the BMCState field to {CURRENT_BMC_STATE}",
226 "CURRENT_BMC_STATE", value);
Josh D. Kingd613b812016-12-19 16:47:45 -0600227
228 return server::BMC::currentBMCState(value);
229}
230
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800231BMC::RebootCause BMC::lastRebootCause(RebootCause value)
232{
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500233 info("Setting the RebootCause field to {LAST_REBOOT_CAUSE}",
234 "LAST_REBOOT_CAUSE", value);
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800235
236 return server::BMC::lastRebootCause(value);
237}
238
Matt Spinlere6710b72018-07-12 16:05:55 -0500239uint64_t BMC::lastRebootTime() const
240{
241 using namespace std::chrono;
242 struct sysinfo info;
243
244 auto rc = sysinfo(&info);
245 assert(rc == 0);
246
247 // Since uptime is in seconds, also get the current time in seconds.
248 auto now = time_point_cast<seconds>(system_clock::now());
249 auto rebootTime = now - seconds(info.uptime);
250
251 return duration_cast<milliseconds>(rebootTime.time_since_epoch()).count();
252}
253
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800254void BMC::discoverLastRebootCause()
255{
256 uint64_t bootReason = 0;
257 std::ifstream file;
258 auto bootstatusPath = "/sys/class/watchdog/watchdog0/bootstatus";
259
260 file.exceptions(std::ifstream::failbit | std::ifstream::badbit |
261 std::ifstream::eofbit);
262
263 try
264 {
265 file.open(bootstatusPath);
266 file >> bootReason;
267 }
268 catch (const std::exception& e)
269 {
270 auto rc = errno;
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500271 error("Failed to read sysfs file {FILE} with errno {ERRNO}", "FILE",
272 bootstatusPath, "ERRNO", rc);
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800273 }
274
275 switch (bootReason)
276 {
277 case WDIOF_EXTERN1:
278 this->lastRebootCause(RebootCause::Watchdog);
279 break;
280 case WDIOF_CARDRESET:
281 this->lastRebootCause(RebootCause::POR);
282 break;
283 default:
284 this->lastRebootCause(RebootCause::Unknown);
285 break;
286 }
287
288 return;
289}
290
Josh D. Kingbdd9cb72016-12-19 11:13:43 -0600291} // namespace manager
292} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -0700293} // namespace phosphor