blob: fb15f8f5523872a04175a89dcbff34ac007f441d [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>
8#include <phosphor-logging/log.hpp>
9#include <sdbusplus/exception.hpp>
10
11#include <cassert>
12
Josh D. Kingbdd9cb72016-12-19 11:13:43 -060013namespace phosphor
14{
15namespace state
16{
17namespace manager
18{
19
Josh D. King6db38222016-12-19 14:52:40 -060020// When you see server:: you know we're referencing our base class
21namespace server = sdbusplus::xyz::openbmc_project::State::server;
22
23using namespace phosphor::logging;
Andrew Geissler2f60aae2019-09-12 13:25:21 -050024using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Josh D. King6db38222016-12-19 14:52:40 -060025
Anthony Wilsoneef31f82019-04-23 17:04:09 -050026constexpr auto obmcStandbyTarget = "multi-user.target";
Josh D. Kingd613b812016-12-19 16:47:45 -060027constexpr auto signalDone = "done";
Josh D. Kingd3e58472017-02-02 11:09:11 -060028constexpr auto activeState = "active";
Josh D. Kingd613b812016-12-19 16:47:45 -060029
Josh D. King5162a7b2016-12-19 16:15:00 -060030/* Map a transition to it's systemd target */
Andrew Geissler58a18012018-01-19 19:36:05 -080031const std::map<server::BMC::Transition, const char*> SYSTEMD_TABLE = {
32 {server::BMC::Transition::Reboot, "reboot.target"}};
Josh D. King5162a7b2016-12-19 16:15:00 -060033
Andrew Geissler58a18012018-01-19 19:36:05 -080034constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
35constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
36constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Josh D. Kingd3e58472017-02-02 11:09:11 -060037constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties";
Josh D. Kingd3e58472017-02-02 11:09:11 -060038
39void BMC::discoverInitialState()
40{
Patrick Williams2975e262020-05-13 18:01:09 -050041 std::variant<std::string> currentState;
Josh D. King2b5d8872017-02-21 13:37:17 -060042 sdbusplus::message::object_path unitTargetPath;
Josh D. Kingd3e58472017-02-02 11:09:11 -060043
Andrew Geissler58a18012018-01-19 19:36:05 -080044 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
45 SYSTEMD_INTERFACE, "GetUnit");
Josh D. King2b5d8872017-02-21 13:37:17 -060046
47 method.append(obmcStandbyTarget);
48
Anthony Wilson32c532e2018-10-25 21:56:07 -050049 try
Josh D. King2b5d8872017-02-21 13:37:17 -060050 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050051 auto result = this->bus.call(method);
52 result.read(unitTargetPath);
53 }
Patrick Williams0a675212021-09-02 09:49:43 -050054 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -050055 {
56 log<level::ERR>("Error in GetUnit call", entry("ERROR=%s", e.what()));
Josh D. King2b5d8872017-02-21 13:37:17 -060057 return;
58 }
59
Andrew Geissler58a18012018-01-19 19:36:05 -080060 method = this->bus.new_method_call(
61 SYSTEMD_SERVICE,
62 static_cast<const std::string&>(unitTargetPath).c_str(),
63 SYSTEMD_PRP_INTERFACE, "Get");
Josh D. Kingd3e58472017-02-02 11:09:11 -060064
65 method.append("org.freedesktop.systemd1.Unit", "ActiveState");
66
Anthony Wilson32c532e2018-10-25 21:56:07 -050067 try
Josh D. King2b5d8872017-02-21 13:37:17 -060068 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050069 auto result = this->bus.call(method);
70
71 // Is obmc-standby.target active or inactive?
72 result.read(currentState);
73 }
Patrick Williams0a675212021-09-02 09:49:43 -050074 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -050075 {
76 log<level::INFO>("Error in ActiveState Get",
77 entry("ERROR=%s", e.what()));
Josh D. King2b5d8872017-02-21 13:37:17 -060078 return;
79 }
Josh D. Kingd3e58472017-02-02 11:09:11 -060080
Patrick Williams37413dc2020-05-13 11:29:54 -050081 auto currentStateStr = std::get<std::string>(currentState);
Anthony Wilson32c532e2018-10-25 21:56:07 -050082 if (currentStateStr == activeState)
Josh D. Kingd3e58472017-02-02 11:09:11 -060083 {
84 log<level::INFO>("Setting the BMCState field",
Andrew Geissler58a18012018-01-19 19:36:05 -080085 entry("CURRENT_BMC_STATE=%s", "BMC_READY"));
Josh D. Kingd3e58472017-02-02 11:09:11 -060086 this->currentBMCState(BMCState::Ready);
87
Andrew Geissler58a18012018-01-19 19:36:05 -080088 // Unsubscribe so we stop processing all other signals
89 method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
90 SYSTEMD_INTERFACE, "Unsubscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -050091 try
92 {
93 this->bus.call(method);
94 this->stateSignal.release();
95 }
Patrick Williams0a675212021-09-02 09:49:43 -050096 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -050097 {
98 log<level::INFO>("Error in Unsubscribe",
99 entry("ERROR=%s", e.what()));
100 }
Josh D. Kingd3e58472017-02-02 11:09:11 -0600101 }
102 else
103 {
104 log<level::INFO>("Setting the BMCState field",
Andrew Geissler58a18012018-01-19 19:36:05 -0800105 entry("CURRENT_BMC_STATE=%s", "BMC_NOTREADY"));
Josh D. Kingd3e58472017-02-02 11:09:11 -0600106 this->currentBMCState(BMCState::NotReady);
107 }
108
109 return;
110}
111
Josh D. King6db38222016-12-19 14:52:40 -0600112void BMC::subscribeToSystemdSignals()
113{
Andrew Geissler58a18012018-01-19 19:36:05 -0800114 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
115 SYSTEMD_INTERFACE, "Subscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500116
117 try
118 {
119 this->bus.call(method);
120 }
Patrick Williams0a675212021-09-02 09:49:43 -0500121 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500122 {
Andrew Geissler2f60aae2019-09-12 13:25:21 -0500123 log<level::ERR>("Failed to subscribe to systemd signals",
124 entry("ERR=%s", e.what()));
125 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 {
145 log<level::INFO>("Error in HardReboot",
146 entry("ERROR=%s", e.what()));
147 }
Anthony Wilson32c532e2018-10-25 21:56:07 -0500148 }
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000149 else
Anthony Wilson32c532e2018-10-25 21:56:07 -0500150 {
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000151 // Check to make sure it can be found
152 auto iter = SYSTEMD_TABLE.find(tranReq);
153 if (iter == SYSTEMD_TABLE.end())
154 return;
Anthony Wilson32c532e2018-10-25 21:56:07 -0500155
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000156 const auto& sysdUnit = iter->second;
157
158 auto method = this->bus.new_method_call(
159 SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "StartUnit");
160 // The only valid transition is reboot and that
161 // needs to be irreversible once started
162
163 method.append(sysdUnit, "replace-irreversibly");
164
165 try
166 {
167 this->bus.call(method);
168 }
Patrick Williams0a675212021-09-02 09:49:43 -0500169 catch (const sdbusplus::exception::exception& e)
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000170 {
171 log<level::INFO>("Error in StartUnit - replace-irreversibly",
172 entry("ERROR=%s", e.what()));
173 }
174 }
Josh D. King5162a7b2016-12-19 16:15:00 -0600175 return;
176}
177
Patrick Williamsd32f8182017-05-05 15:55:24 -0500178int BMC::bmcStateChange(sdbusplus::message::message& msg)
Josh D. Kingd613b812016-12-19 16:47:45 -0600179{
Andrew Geissler58a18012018-01-19 19:36:05 -0800180 uint32_t newStateID{};
Josh D. Kingd613b812016-12-19 16:47:45 -0600181 sdbusplus::message::object_path newStateObjPath;
182 std::string newStateUnit{};
183 std::string newStateResult{};
184
Andrew Geissler58a18012018-01-19 19:36:05 -0800185 // Read the msg and populate each variable
Patrick Williamsd32f8182017-05-05 15:55:24 -0500186 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
Josh D. Kingd613b812016-12-19 16:47:45 -0600187
Andrew Geissler58a18012018-01-19 19:36:05 -0800188 // Caught the signal that indicates the BMC is now BMC_READY
189 if ((newStateUnit == obmcStandbyTarget) && (newStateResult == signalDone))
Josh D. Kingd613b812016-12-19 16:47:45 -0600190 {
191 log<level::INFO>("BMC_READY");
192 this->currentBMCState(BMCState::Ready);
193
Andrew Geissler58a18012018-01-19 19:36:05 -0800194 // Unsubscribe so we stop processing all other signals
195 auto method =
196 this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
197 SYSTEMD_INTERFACE, "Unsubscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500198
199 try
200 {
201 this->bus.call(method);
202 this->stateSignal.release();
203 }
Patrick Williams0a675212021-09-02 09:49:43 -0500204 catch (const sdbusplus::exception::exception& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500205 {
206 log<level::INFO>("Error in Unsubscribe",
207 entry("ERROR=%s", e.what()));
208 }
Josh D. Kingd613b812016-12-19 16:47:45 -0600209 }
210
211 return 0;
212}
213
Josh D. King6db38222016-12-19 14:52:40 -0600214BMC::Transition BMC::requestedBMCTransition(Transition value)
215{
Andrew Geissler58a18012018-01-19 19:36:05 -0800216 log<level::INFO>("Setting the RequestedBMCTransition field",
217 entry("REQUESTED_BMC_TRANSITION=0x%s",
218 convertForMessage(value).c_str()));
Josh D. King6db38222016-12-19 14:52:40 -0600219
Josh D. King5162a7b2016-12-19 16:15:00 -0600220 executeTransition(value);
221 return server::BMC::requestedBMCTransition(value);
Josh D. King6db38222016-12-19 14:52:40 -0600222}
223
Josh D. Kingd613b812016-12-19 16:47:45 -0600224BMC::BMCState BMC::currentBMCState(BMCState value)
225{
226 log<level::INFO>(
Andrew Geissler58a18012018-01-19 19:36:05 -0800227 "Setting the BMCState field",
228 entry("CURRENT_BMC_STATE=0x%s", convertForMessage(value).c_str()));
Josh D. Kingd613b812016-12-19 16:47:45 -0600229
230 return server::BMC::currentBMCState(value);
231}
232
Matt Spinlere6710b72018-07-12 16:05:55 -0500233uint64_t BMC::lastRebootTime() const
234{
235 using namespace std::chrono;
236 struct sysinfo info;
237
238 auto rc = sysinfo(&info);
239 assert(rc == 0);
240
241 // Since uptime is in seconds, also get the current time in seconds.
242 auto now = time_point_cast<seconds>(system_clock::now());
243 auto rebootTime = now - seconds(info.uptime);
244
245 return duration_cast<milliseconds>(rebootTime.time_since_epoch()).count();
246}
247
Josh D. Kingbdd9cb72016-12-19 11:13:43 -0600248} // namespace manager
249} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -0700250} // namespace phosphor