blob: faab43f2b2d0de2ded3f701771c79c5bc860f1b1 [file] [log] [blame]
Thang Tran9f381522024-10-18 10:11:01 +07001#include "config.h"
2
Josh D. Kingbdd9cb72016-12-19 11:13:43 -06003#include "bmc_state_manager.hpp"
Andrew Geisslere426b582020-05-28 12:40:55 -05004
Andrew Geissler98e64e62022-01-25 16:02:56 -06005#include "utils.hpp"
Andrew Geissler2f60aae2019-09-12 13:25:21 -05006#include "xyz/openbmc_project/Common/error.hpp"
Josh D. Kingbdd9cb72016-12-19 11:13:43 -06007
Andrew Geissler98e64e62022-01-25 16:02:56 -06008#include <gpiod.h>
Andrew Geisslere426b582020-05-28 12:40:55 -05009
10#include <phosphor-logging/elog-errors.hpp>
Andrew Geissler429100a2021-09-09 12:50:24 -050011#include <phosphor-logging/lg2.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050012#include <sdbusplus/exception.hpp>
13
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
Patrick Williams7e969cb2023-08-23 16:24:23 -050028namespace server = sdbusplus::server::xyz::openbmc_project::state;
Josh D. King6db38222016-12-19 14:52:40 -060029
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 Williamsf053e6f2022-07-22 19:26:54 -050062 catch (const sdbusplus::exception_t& 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 Williamsf053e6f2022-07-22 19:26:54 -050084 catch (const sdbusplus::exception_t& 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 // First look to see if the BMC quiesce target is active
95 auto currentStateStr = getUnitState(obmcQuiesceTarget);
96 if (currentStateStr == activeState)
97 {
98 info("Setting the BMCState field to BMC_QUIESCED");
99 this->currentBMCState(BMCState::Quiesced);
100 return;
101 }
102
103 // If not quiesced, then check standby target
104 currentStateStr = getUnitState(obmcStandbyTarget);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500105 if (currentStateStr == activeState)
Josh D. Kingd3e58472017-02-02 11:09:11 -0600106 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500107 info("Setting the BMCState field to BMC_READY");
Josh D. Kingd3e58472017-02-02 11:09:11 -0600108 this->currentBMCState(BMCState::Ready);
Josh D. Kingd3e58472017-02-02 11:09:11 -0600109 }
110 else
111 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500112 info("Setting the BMCState field to BMC_NOTREADY");
Josh D. Kingd3e58472017-02-02 11:09:11 -0600113 this->currentBMCState(BMCState::NotReady);
114 }
115
116 return;
117}
118
Josh D. King5162a7b2016-12-19 16:15:00 -0600119void BMC::executeTransition(const Transition tranReq)
120{
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000121 // HardReboot does not shutdown any services and immediately transitions
122 // into the reboot process
123 if (server::BMC::Transition::HardReboot == tranReq)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500124 {
Andrew Geissler4ee59462022-04-28 16:58:26 -0400125 // Put BMC state not NotReady when issuing a BMC reboot
126 // and stop monitoring for state changes
127 this->currentBMCState(BMCState::NotReady);
128 this->stateSignal.reset();
129
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000130 auto method = this->bus.new_method_call(
131 SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "Reboot");
132 try
133 {
134 this->bus.call(method);
135 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500136 catch (const sdbusplus::exception_t& e)
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000137 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500138 info("Error in HardReboot: {ERROR}", "ERROR", e);
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000139 }
Anthony Wilson32c532e2018-10-25 21:56:07 -0500140 }
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000141 else
Anthony Wilson32c532e2018-10-25 21:56:07 -0500142 {
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000143 // Check to make sure it can be found
144 auto iter = SYSTEMD_TABLE.find(tranReq);
145 if (iter == SYSTEMD_TABLE.end())
Pavithra Barithayab594ac12024-06-21 12:09:04 -0500146 {
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000147 return;
Pavithra Barithayab594ac12024-06-21 12:09:04 -0500148 }
Anthony Wilson32c532e2018-10-25 21:56:07 -0500149
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000150 const auto& sysdUnit = iter->second;
151
152 auto method = this->bus.new_method_call(
153 SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "StartUnit");
154 // The only valid transition is reboot and that
155 // needs to be irreversible once started
156
157 method.append(sysdUnit, "replace-irreversibly");
158
Andrew Geissler4ee59462022-04-28 16:58:26 -0400159 // Put BMC state not NotReady when issuing a BMC reboot
160 // and stop monitoring for state changes
161 this->currentBMCState(BMCState::NotReady);
162 this->stateSignal.reset();
163
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000164 try
165 {
166 this->bus.call(method);
167 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500168 catch (const sdbusplus::exception_t& 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 Williamsf053e6f2022-07-22 19:26:54 -0500177int BMC::bmcStateChange(sdbusplus::message_t& 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 Geissler9eb0e442022-02-18 10:21:15 -0600187 if ((newStateUnit == obmcQuiesceTarget) && (newStateResult == signalDone))
Josh D. Kingd613b812016-12-19 16:47:45 -0600188 {
Andrew Geissler9eb0e442022-02-18 10:21:15 -0600189 error("BMC has entered BMC_QUIESCED state");
190 this->currentBMCState(BMCState::Quiesced);
Josh D. Kingd613b812016-12-19 16:47:45 -0600191
Andrew Geissler9eb0e442022-02-18 10:21:15 -0600192 // There is no getting out of Quiesced once entered (other then BMC
193 // reboot) so stop watching for signals
Andrew Geissler58a18012018-01-19 19:36:05 -0800194 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);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500201 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500202 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500203 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500204 info("Error in Unsubscribe: {ERROR}", "ERROR", e);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500205 }
Andrew Geissler9eb0e442022-02-18 10:21:15 -0600206
Andrew Geissler71e538f2022-03-21 11:22:43 -0500207 // disable the system state change object as well
NodeMan978c26f0e2022-04-27 20:15:22 -0500208 this->stateSignal.reset();
Andrew Geissler71e538f2022-03-21 11:22:43 -0500209
Andrew Geissler9eb0e442022-02-18 10:21:15 -0600210 return 0;
211 }
212
213 // Caught the signal that indicates the BMC is now BMC_READY
214 if ((newStateUnit == obmcStandbyTarget) && (newStateResult == signalDone))
215 {
216 info("BMC_READY");
217 this->currentBMCState(BMCState::Ready);
Josh D. Kingd613b812016-12-19 16:47:45 -0600218 }
219
220 return 0;
221}
222
Josh D. King6db38222016-12-19 14:52:40 -0600223BMC::Transition BMC::requestedBMCTransition(Transition value)
224{
Andrew Geissler429100a2021-09-09 12:50:24 -0500225 info("Setting the RequestedBMCTransition field to "
226 "{REQUESTED_BMC_TRANSITION}",
227 "REQUESTED_BMC_TRANSITION", value);
Josh D. King6db38222016-12-19 14:52:40 -0600228
Thang Tran9f381522024-10-18 10:11:01 +0700229#ifdef CHECK_FWUPDATE_BEFORE_DO_TRANSITION
230 /*
231 * Do not do transition when the any firmware being updated
232 */
233 if ((server::BMC::Transition::Reboot == value) &&
234 (phosphor::state::manager::utils::isFirmwareUpdating(this->bus)))
235 {
236 info("Firmware being updated, reject the transition request");
237 throw sdbusplus::xyz::openbmc_project::Common::Error::Unavailable();
238 }
239#endif // CHECK_FWUPDATE_BEFORE_DO_TRANSITION
240
Josh D. King5162a7b2016-12-19 16:15:00 -0600241 executeTransition(value);
242 return server::BMC::requestedBMCTransition(value);
Josh D. King6db38222016-12-19 14:52:40 -0600243}
244
Josh D. Kingd613b812016-12-19 16:47:45 -0600245BMC::BMCState BMC::currentBMCState(BMCState value)
246{
Andrew Geissler429100a2021-09-09 12:50:24 -0500247 info("Setting the BMCState field to {CURRENT_BMC_STATE}",
248 "CURRENT_BMC_STATE", value);
Josh D. Kingd613b812016-12-19 16:47:45 -0600249
250 return server::BMC::currentBMCState(value);
251}
252
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800253BMC::RebootCause BMC::lastRebootCause(RebootCause value)
254{
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500255 info("Setting the RebootCause field to {LAST_REBOOT_CAUSE}",
256 "LAST_REBOOT_CAUSE", value);
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800257
258 return server::BMC::lastRebootCause(value);
259}
260
Willy Tubd1eebd2023-10-05 12:14:20 -0700261void BMC::updateLastRebootTime()
262{
263 using namespace std::chrono;
264 struct sysinfo info;
265
266 auto rc = sysinfo(&info);
267 assert(rc == 0);
268 // Since uptime is in seconds, also get the current time in seconds.
269 auto now = time_point_cast<seconds>(system_clock::now());
270 auto rebootTimeTs = now - seconds(info.uptime);
271 rebootTime =
272 duration_cast<milliseconds>(rebootTimeTs.time_since_epoch()).count();
Willy Tu47120842024-01-09 14:04:03 -0800273 server::BMC::lastRebootTime(rebootTime);
Willy Tubd1eebd2023-10-05 12:14:20 -0700274}
275
Matt Spinlere6710b72018-07-12 16:05:55 -0500276uint64_t BMC::lastRebootTime() const
277{
Willy Tu564eb4f2023-09-07 16:02:46 -0700278 return rebootTime;
Matt Spinlere6710b72018-07-12 16:05:55 -0500279}
280
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800281void BMC::discoverLastRebootCause()
282{
283 uint64_t bootReason = 0;
284 std::ifstream file;
Pavithra Barithaya319eda42024-06-21 11:54:43 -0500285 const auto* bootstatusPath = "/sys/class/watchdog/watchdog0/bootstatus";
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800286
287 file.exceptions(std::ifstream::failbit | std::ifstream::badbit |
288 std::ifstream::eofbit);
289
290 try
291 {
292 file.open(bootstatusPath);
293 file >> bootReason;
294 }
295 catch (const std::exception& e)
296 {
297 auto rc = errno;
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500298 error("Failed to read sysfs file {FILE} with errno {ERRNO}", "FILE",
299 bootstatusPath, "ERRNO", rc);
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800300 }
301
302 switch (bootReason)
303 {
304 case WDIOF_EXTERN1:
Tim Lee4ab59212024-09-05 09:51:02 +0800305 this->lastRebootCause(RebootCause::Software);
Andrew Geissler98e64e62022-01-25 16:02:56 -0600306 return;
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800307 case WDIOF_CARDRESET:
Tim Lee4ab59212024-09-05 09:51:02 +0800308 this->lastRebootCause(RebootCause::Watchdog);
Andrew Geissler98e64e62022-01-25 16:02:56 -0600309 return;
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800310 default:
Tim Lee4ab59212024-09-05 09:51:02 +0800311 this->lastRebootCause(RebootCause::POR);
Andrew Geissler98e64e62022-01-25 16:02:56 -0600312 // Continue below to see if more details can be found
313 // on reason for reboot
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800314 break;
315 }
316
Andrew Geissler98e64e62022-01-25 16:02:56 -0600317 // If the above code could not detect a reason, look for a the
318 // reset-cause-pinhole gpio to see if it is the reason for the reboot
319 auto gpioval =
320 phosphor::state::manager::utils::getGpioValue("reset-cause-pinhole");
321
Andrew Geissler2e352a22022-03-02 11:31:40 -0600322 // A 0 indicates a pinhole reset occurred
323 if (0 == gpioval)
Andrew Geissler98e64e62022-01-25 16:02:56 -0600324 {
325 info("The BMC reset was caused by a pinhole reset");
326 this->lastRebootCause(RebootCause::PinholeReset);
Andrew Geisslera2a7e122022-01-26 13:30:18 -0600327
328 // Generate log telling user a pinhole reset has occurred
329 const std::string errorMsg = "xyz.openbmc_project.State.PinholeReset";
330 phosphor::state::manager::utils::createError(
331 this->bus, errorMsg,
Patrick Williams7e969cb2023-08-23 16:24:23 -0500332 sdbusplus::server::xyz::openbmc_project::logging::Entry::Level::
Andrew Geisslera2a7e122022-01-26 13:30:18 -0600333 Notice);
Andrew Geissler2038e492023-06-16 15:32:58 -0400334 return;
335 }
336
337 // If we still haven't found a reason, see if we lost AC power
338 // Note that a pinhole reset will remove AC power to the chassis
339 // on some systems so we always want to look for the pinhole reset
340 // first as that would be the main reason AC power was lost.
341 size_t chassisId = 0;
342 if (phosphor::state::manager::utils::checkACLoss(chassisId))
343 {
344 this->lastRebootCause(RebootCause::POR);
Andrew Geissler98e64e62022-01-25 16:02:56 -0600345 }
346
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800347 return;
348}
349
Josh D. Kingbdd9cb72016-12-19 11:13:43 -0600350} // namespace manager
351} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -0700352} // namespace phosphor