blob: 3fbe4bf585f1148168064b8adc8aecef88a76b94 [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;
Anthony Wilson32c532e2018-10-25 21:56:07 -050024using sdbusplus::exception::SdBusError;
Andrew Geissler2f60aae2019-09-12 13:25:21 -050025using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Josh D. King6db38222016-12-19 14:52:40 -060026
Anthony Wilsoneef31f82019-04-23 17:04:09 -050027constexpr auto obmcStandbyTarget = "multi-user.target";
Josh D. Kingd613b812016-12-19 16:47:45 -060028constexpr auto signalDone = "done";
Josh D. Kingd3e58472017-02-02 11:09:11 -060029constexpr auto activeState = "active";
Josh D. Kingd613b812016-12-19 16:47:45 -060030
Josh D. King5162a7b2016-12-19 16:15:00 -060031/* Map a transition to it's systemd target */
Andrew Geissler58a18012018-01-19 19:36:05 -080032const std::map<server::BMC::Transition, const char*> SYSTEMD_TABLE = {
33 {server::BMC::Transition::Reboot, "reboot.target"}};
Josh D. King5162a7b2016-12-19 16:15:00 -060034
Andrew Geissler58a18012018-01-19 19:36:05 -080035constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
36constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
37constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Josh D. Kingd3e58472017-02-02 11:09:11 -060038constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties";
Josh D. Kingd3e58472017-02-02 11:09:11 -060039
40void BMC::discoverInitialState()
41{
Patrick Williams2975e262020-05-13 18:01:09 -050042 std::variant<std::string> currentState;
Josh D. King2b5d8872017-02-21 13:37:17 -060043 sdbusplus::message::object_path unitTargetPath;
Josh D. Kingd3e58472017-02-02 11:09:11 -060044
Andrew Geissler58a18012018-01-19 19:36:05 -080045 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
46 SYSTEMD_INTERFACE, "GetUnit");
Josh D. King2b5d8872017-02-21 13:37:17 -060047
48 method.append(obmcStandbyTarget);
49
Anthony Wilson32c532e2018-10-25 21:56:07 -050050 try
Josh D. King2b5d8872017-02-21 13:37:17 -060051 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050052 auto result = this->bus.call(method);
53 result.read(unitTargetPath);
54 }
55 catch (const SdBusError& e)
56 {
57 log<level::ERR>("Error in GetUnit call", entry("ERROR=%s", e.what()));
Josh D. King2b5d8872017-02-21 13:37:17 -060058 return;
59 }
60
Andrew Geissler58a18012018-01-19 19:36:05 -080061 method = this->bus.new_method_call(
62 SYSTEMD_SERVICE,
63 static_cast<const std::string&>(unitTargetPath).c_str(),
64 SYSTEMD_PRP_INTERFACE, "Get");
Josh D. Kingd3e58472017-02-02 11:09:11 -060065
66 method.append("org.freedesktop.systemd1.Unit", "ActiveState");
67
Anthony Wilson32c532e2018-10-25 21:56:07 -050068 try
Josh D. King2b5d8872017-02-21 13:37:17 -060069 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050070 auto result = this->bus.call(method);
71
72 // Is obmc-standby.target active or inactive?
73 result.read(currentState);
74 }
75 catch (const SdBusError& e)
76 {
77 log<level::INFO>("Error in ActiveState Get",
78 entry("ERROR=%s", e.what()));
Josh D. King2b5d8872017-02-21 13:37:17 -060079 return;
80 }
Josh D. Kingd3e58472017-02-02 11:09:11 -060081
Patrick Williams37413dc2020-05-13 11:29:54 -050082 auto currentStateStr = std::get<std::string>(currentState);
Anthony Wilson32c532e2018-10-25 21:56:07 -050083 if (currentStateStr == activeState)
Josh D. Kingd3e58472017-02-02 11:09:11 -060084 {
85 log<level::INFO>("Setting the BMCState field",
Andrew Geissler58a18012018-01-19 19:36:05 -080086 entry("CURRENT_BMC_STATE=%s", "BMC_READY"));
Josh D. Kingd3e58472017-02-02 11:09:11 -060087 this->currentBMCState(BMCState::Ready);
88
Andrew Geissler58a18012018-01-19 19:36:05 -080089 // Unsubscribe so we stop processing all other signals
90 method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
91 SYSTEMD_INTERFACE, "Unsubscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -050092 try
93 {
94 this->bus.call(method);
95 this->stateSignal.release();
96 }
97 catch (const SdBusError& e)
98 {
99 log<level::INFO>("Error in Unsubscribe",
100 entry("ERROR=%s", e.what()));
101 }
Josh D. Kingd3e58472017-02-02 11:09:11 -0600102 }
103 else
104 {
105 log<level::INFO>("Setting the BMCState field",
Andrew Geissler58a18012018-01-19 19:36:05 -0800106 entry("CURRENT_BMC_STATE=%s", "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 }
122 catch (const SdBusError& e)
123 {
Andrew Geissler2f60aae2019-09-12 13:25:21 -0500124 log<level::ERR>("Failed to subscribe to systemd signals",
125 entry("ERR=%s", e.what()));
126 elog<InternalFailure>();
Anthony Wilson32c532e2018-10-25 21:56:07 -0500127 }
Josh D. King6db38222016-12-19 14:52:40 -0600128
129 return;
130}
131
Josh D. King5162a7b2016-12-19 16:15:00 -0600132void BMC::executeTransition(const Transition tranReq)
133{
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000134 // HardReboot does not shutdown any services and immediately transitions
135 // into the reboot process
136 if (server::BMC::Transition::HardReboot == tranReq)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500137 {
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000138 auto method = this->bus.new_method_call(
139 SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "Reboot");
140 try
141 {
142 this->bus.call(method);
143 }
144 catch (const SdBusError& e)
145 {
146 log<level::INFO>("Error in HardReboot",
147 entry("ERROR=%s", e.what()));
148 }
Anthony Wilson32c532e2018-10-25 21:56:07 -0500149 }
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000150 else
Anthony Wilson32c532e2018-10-25 21:56:07 -0500151 {
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000152 // Check to make sure it can be found
153 auto iter = SYSTEMD_TABLE.find(tranReq);
154 if (iter == SYSTEMD_TABLE.end())
155 return;
Anthony Wilson32c532e2018-10-25 21:56:07 -0500156
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000157 const auto& sysdUnit = iter->second;
158
159 auto method = this->bus.new_method_call(
160 SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "StartUnit");
161 // The only valid transition is reboot and that
162 // needs to be irreversible once started
163
164 method.append(sysdUnit, "replace-irreversibly");
165
166 try
167 {
168 this->bus.call(method);
169 }
170 catch (const SdBusError& e)
171 {
172 log<level::INFO>("Error in StartUnit - replace-irreversibly",
173 entry("ERROR=%s", e.what()));
174 }
175 }
Josh D. King5162a7b2016-12-19 16:15:00 -0600176 return;
177}
178
Patrick Williamsd32f8182017-05-05 15:55:24 -0500179int BMC::bmcStateChange(sdbusplus::message::message& msg)
Josh D. Kingd613b812016-12-19 16:47:45 -0600180{
Andrew Geissler58a18012018-01-19 19:36:05 -0800181 uint32_t newStateID{};
Josh D. Kingd613b812016-12-19 16:47:45 -0600182 sdbusplus::message::object_path newStateObjPath;
183 std::string newStateUnit{};
184 std::string newStateResult{};
185
Andrew Geissler58a18012018-01-19 19:36:05 -0800186 // Read the msg and populate each variable
Patrick Williamsd32f8182017-05-05 15:55:24 -0500187 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
Josh D. Kingd613b812016-12-19 16:47:45 -0600188
Andrew Geissler58a18012018-01-19 19:36:05 -0800189 // Caught the signal that indicates the BMC is now BMC_READY
190 if ((newStateUnit == obmcStandbyTarget) && (newStateResult == signalDone))
Josh D. Kingd613b812016-12-19 16:47:45 -0600191 {
192 log<level::INFO>("BMC_READY");
193 this->currentBMCState(BMCState::Ready);
194
Andrew Geissler58a18012018-01-19 19:36:05 -0800195 // Unsubscribe so we stop processing all other signals
196 auto method =
197 this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
198 SYSTEMD_INTERFACE, "Unsubscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500199
200 try
201 {
202 this->bus.call(method);
203 this->stateSignal.release();
204 }
205 catch (const SdBusError& e)
206 {
207 log<level::INFO>("Error in Unsubscribe",
208 entry("ERROR=%s", e.what()));
209 }
Josh D. Kingd613b812016-12-19 16:47:45 -0600210 }
211
212 return 0;
213}
214
Josh D. King6db38222016-12-19 14:52:40 -0600215BMC::Transition BMC::requestedBMCTransition(Transition value)
216{
Andrew Geissler58a18012018-01-19 19:36:05 -0800217 log<level::INFO>("Setting the RequestedBMCTransition field",
218 entry("REQUESTED_BMC_TRANSITION=0x%s",
219 convertForMessage(value).c_str()));
Josh D. King6db38222016-12-19 14:52:40 -0600220
Josh D. King5162a7b2016-12-19 16:15:00 -0600221 executeTransition(value);
222 return server::BMC::requestedBMCTransition(value);
Josh D. King6db38222016-12-19 14:52:40 -0600223}
224
Josh D. Kingd613b812016-12-19 16:47:45 -0600225BMC::BMCState BMC::currentBMCState(BMCState value)
226{
227 log<level::INFO>(
Andrew Geissler58a18012018-01-19 19:36:05 -0800228 "Setting the BMCState field",
229 entry("CURRENT_BMC_STATE=0x%s", convertForMessage(value).c_str()));
Josh D. Kingd613b812016-12-19 16:47:45 -0600230
231 return server::BMC::currentBMCState(value);
232}
233
Matt Spinlere6710b72018-07-12 16:05:55 -0500234uint64_t BMC::lastRebootTime() const
235{
236 using namespace std::chrono;
237 struct sysinfo info;
238
239 auto rc = sysinfo(&info);
240 assert(rc == 0);
241
242 // Since uptime is in seconds, also get the current time in seconds.
243 auto now = time_point_cast<seconds>(system_clock::now());
244 auto rebootTime = now - seconds(info.uptime);
245
246 return duration_cast<milliseconds>(rebootTime.time_since_epoch()).count();
247}
248
Josh D. Kingbdd9cb72016-12-19 11:13:43 -0600249} // namespace manager
250} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -0700251} // namespace phosphor