blob: 53769941120cfc191577e52ba9a88dd6b43ab466 [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
8#include <phosphor-logging/elog-errors.hpp>
Andrew Geissler429100a2021-09-09 12:50:24 -05009#include <phosphor-logging/lg2.hpp>
Andrew Geisslere426b582020-05-28 12:40:55 -050010#include <sdbusplus/exception.hpp>
11
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
Patrick Williams7e969cb2023-08-23 16:24:23 -050026namespace server = sdbusplus::server::xyz::openbmc_project::state;
Josh D. King6db38222016-12-19 14:52:40 -060027
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
Andrew Geissler9eb0e442022-02-18 10:21:15 -060031constexpr auto obmcQuiesceTarget = "obmc-bmc-service-quiesce@0.target";
Anthony Wilsoneef31f82019-04-23 17:04:09 -050032constexpr auto obmcStandbyTarget = "multi-user.target";
Josh D. Kingd613b812016-12-19 16:47:45 -060033constexpr auto signalDone = "done";
Josh D. Kingd3e58472017-02-02 11:09:11 -060034constexpr auto activeState = "active";
Josh D. Kingd613b812016-12-19 16:47:45 -060035
Josh D. King5162a7b2016-12-19 16:15:00 -060036/* Map a transition to it's systemd target */
Andrew Geissler58a18012018-01-19 19:36:05 -080037const std::map<server::BMC::Transition, const char*> SYSTEMD_TABLE = {
38 {server::BMC::Transition::Reboot, "reboot.target"}};
Josh D. King5162a7b2016-12-19 16:15:00 -060039
Andrew Geissler58a18012018-01-19 19:36:05 -080040constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
41constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
42constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Josh D. Kingd3e58472017-02-02 11:09:11 -060043constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties";
Josh D. Kingd3e58472017-02-02 11:09:11 -060044
Andrew Geissler2774c782022-02-17 16:57:14 -060045std::string BMC::getUnitState(const std::string& unitToCheck)
Josh D. Kingd3e58472017-02-02 11:09:11 -060046{
Patrick Williams2975e262020-05-13 18:01:09 -050047 std::variant<std::string> currentState;
Josh D. King2b5d8872017-02-21 13:37:17 -060048 sdbusplus::message::object_path unitTargetPath;
Josh D. Kingd3e58472017-02-02 11:09:11 -060049
Andrew Geissler58a18012018-01-19 19:36:05 -080050 auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
51 SYSTEMD_INTERFACE, "GetUnit");
Josh D. King2b5d8872017-02-21 13:37:17 -060052
Andrew Geissler2774c782022-02-17 16:57:14 -060053 method.append(unitToCheck);
Josh D. King2b5d8872017-02-21 13:37:17 -060054
Anthony Wilson32c532e2018-10-25 21:56:07 -050055 try
Josh D. King2b5d8872017-02-21 13:37:17 -060056 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050057 auto result = this->bus.call(method);
58 result.read(unitTargetPath);
59 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -050060 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -050061 {
Andrew Geissler37d36312022-03-09 16:24:45 -060062 // Not all input units will have been loaded yet so just return an
63 // empty string if an exception is caught in this path
64 info("Unit {UNIT} not found: {ERROR}", "UNIT", unitToCheck, "ERROR", e);
Andrew Geissler2774c782022-02-17 16:57:14 -060065 return std::string{};
Josh D. King2b5d8872017-02-21 13:37:17 -060066 }
67
Andrew Geissler58a18012018-01-19 19:36:05 -080068 method = this->bus.new_method_call(
69 SYSTEMD_SERVICE,
70 static_cast<const std::string&>(unitTargetPath).c_str(),
71 SYSTEMD_PRP_INTERFACE, "Get");
Josh D. Kingd3e58472017-02-02 11:09:11 -060072
73 method.append("org.freedesktop.systemd1.Unit", "ActiveState");
74
Anthony Wilson32c532e2018-10-25 21:56:07 -050075 try
Josh D. King2b5d8872017-02-21 13:37:17 -060076 {
Anthony Wilson32c532e2018-10-25 21:56:07 -050077 auto result = this->bus.call(method);
78
Andrew Geissler37d36312022-03-09 16:24:45 -060079 // Is input target active or inactive?
Anthony Wilson32c532e2018-10-25 21:56:07 -050080 result.read(currentState);
81 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -050082 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -050083 {
Andrew Geissler429100a2021-09-09 12:50:24 -050084 info("Error in ActiveState Get: {ERROR}", "ERROR", e);
Andrew Geissler2774c782022-02-17 16:57:14 -060085 return std::string{};
Josh D. King2b5d8872017-02-21 13:37:17 -060086 }
Andrew Geissler2774c782022-02-17 16:57:14 -060087 return (std::get<std::string>(currentState));
88}
Josh D. Kingd3e58472017-02-02 11:09:11 -060089
Andrew Geissler2774c782022-02-17 16:57:14 -060090void BMC::discoverInitialState()
91{
Andrew Geissler9eb0e442022-02-18 10:21:15 -060092 // First look to see if the BMC quiesce target is active
93 auto currentStateStr = getUnitState(obmcQuiesceTarget);
94 if (currentStateStr == activeState)
95 {
96 info("Setting the BMCState field to BMC_QUIESCED");
97 this->currentBMCState(BMCState::Quiesced);
98 return;
99 }
100
101 // If not quiesced, then check standby target
102 currentStateStr = getUnitState(obmcStandbyTarget);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500103 if (currentStateStr == activeState)
Josh D. Kingd3e58472017-02-02 11:09:11 -0600104 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500105 info("Setting the BMCState field to BMC_READY");
Josh D. Kingd3e58472017-02-02 11:09:11 -0600106 this->currentBMCState(BMCState::Ready);
Josh D. Kingd3e58472017-02-02 11:09:11 -0600107 }
108 else
109 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500110 info("Setting the BMCState field to BMC_NOTREADY");
Josh D. Kingd3e58472017-02-02 11:09:11 -0600111 this->currentBMCState(BMCState::NotReady);
112 }
113
114 return;
115}
116
Josh D. King5162a7b2016-12-19 16:15:00 -0600117void BMC::executeTransition(const Transition tranReq)
118{
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000119 // HardReboot does not shutdown any services and immediately transitions
120 // into the reboot process
121 if (server::BMC::Transition::HardReboot == tranReq)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500122 {
Andrew Geissler4ee59462022-04-28 16:58:26 -0400123 // Put BMC state not NotReady when issuing a BMC reboot
124 // and stop monitoring for state changes
125 this->currentBMCState(BMCState::NotReady);
126 this->stateSignal.reset();
127
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000128 auto method = this->bus.new_method_call(
129 SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "Reboot");
130 try
131 {
132 this->bus.call(method);
133 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500134 catch (const sdbusplus::exception_t& e)
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000135 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500136 info("Error in HardReboot: {ERROR}", "ERROR", e);
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000137 }
Anthony Wilson32c532e2018-10-25 21:56:07 -0500138 }
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000139 else
Anthony Wilson32c532e2018-10-25 21:56:07 -0500140 {
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000141 // Check to make sure it can be found
142 auto iter = SYSTEMD_TABLE.find(tranReq);
143 if (iter == SYSTEMD_TABLE.end())
Pavithra Barithayab594ac12024-06-21 12:09:04 -0500144 {
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000145 return;
Pavithra Barithayab594ac12024-06-21 12:09:04 -0500146 }
Anthony Wilson32c532e2018-10-25 21:56:07 -0500147
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000148 const auto& sysdUnit = iter->second;
149
150 auto method = this->bus.new_method_call(
151 SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "StartUnit");
152 // The only valid transition is reboot and that
153 // needs to be irreversible once started
154
155 method.append(sysdUnit, "replace-irreversibly");
156
Andrew Geissler4ee59462022-04-28 16:58:26 -0400157 // Put BMC state not NotReady when issuing a BMC reboot
158 // and stop monitoring for state changes
159 this->currentBMCState(BMCState::NotReady);
160 this->stateSignal.reset();
161
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000162 try
163 {
164 this->bus.call(method);
165 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500166 catch (const sdbusplus::exception_t& e)
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000167 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500168 info("Error in StartUnit - replace-irreversibly: {ERROR}", "ERROR",
169 e);
Jayaprakash Mutyala44c223c2020-08-14 00:08:03 +0000170 }
171 }
Josh D. King5162a7b2016-12-19 16:15:00 -0600172 return;
173}
174
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500175int BMC::bmcStateChange(sdbusplus::message_t& msg)
Josh D. Kingd613b812016-12-19 16:47:45 -0600176{
Andrew Geissler58a18012018-01-19 19:36:05 -0800177 uint32_t newStateID{};
Josh D. Kingd613b812016-12-19 16:47:45 -0600178 sdbusplus::message::object_path newStateObjPath;
179 std::string newStateUnit{};
180 std::string newStateResult{};
181
Andrew Geissler58a18012018-01-19 19:36:05 -0800182 // Read the msg and populate each variable
Patrick Williamsd32f8182017-05-05 15:55:24 -0500183 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
Josh D. Kingd613b812016-12-19 16:47:45 -0600184
Andrew Geissler9eb0e442022-02-18 10:21:15 -0600185 if ((newStateUnit == obmcQuiesceTarget) && (newStateResult == signalDone))
Josh D. Kingd613b812016-12-19 16:47:45 -0600186 {
Andrew Geissler9eb0e442022-02-18 10:21:15 -0600187 error("BMC has entered BMC_QUIESCED state");
188 this->currentBMCState(BMCState::Quiesced);
Josh D. Kingd613b812016-12-19 16:47:45 -0600189
Andrew Geissler9eb0e442022-02-18 10:21:15 -0600190 // There is no getting out of Quiesced once entered (other then BMC
191 // reboot) so stop watching for signals
Andrew Geissler58a18012018-01-19 19:36:05 -0800192 auto method =
193 this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
194 SYSTEMD_INTERFACE, "Unsubscribe");
Anthony Wilson32c532e2018-10-25 21:56:07 -0500195
196 try
197 {
198 this->bus.call(method);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500199 }
Patrick Williamsf053e6f2022-07-22 19:26:54 -0500200 catch (const sdbusplus::exception_t& e)
Anthony Wilson32c532e2018-10-25 21:56:07 -0500201 {
Andrew Geissler429100a2021-09-09 12:50:24 -0500202 info("Error in Unsubscribe: {ERROR}", "ERROR", e);
Anthony Wilson32c532e2018-10-25 21:56:07 -0500203 }
Andrew Geissler9eb0e442022-02-18 10:21:15 -0600204
Andrew Geissler71e538f2022-03-21 11:22:43 -0500205 // disable the system state change object as well
NodeMan978c26f0e2022-04-27 20:15:22 -0500206 this->stateSignal.reset();
Andrew Geissler71e538f2022-03-21 11:22:43 -0500207
Andrew Geissler9eb0e442022-02-18 10:21:15 -0600208 return 0;
209 }
210
211 // Caught the signal that indicates the BMC is now BMC_READY
212 if ((newStateUnit == obmcStandbyTarget) && (newStateResult == signalDone))
213 {
214 info("BMC_READY");
215 this->currentBMCState(BMCState::Ready);
Josh D. Kingd613b812016-12-19 16:47:45 -0600216 }
217
218 return 0;
219}
220
Josh D. King6db38222016-12-19 14:52:40 -0600221BMC::Transition BMC::requestedBMCTransition(Transition value)
222{
Andrew Geissler429100a2021-09-09 12:50:24 -0500223 info("Setting the RequestedBMCTransition field to "
224 "{REQUESTED_BMC_TRANSITION}",
225 "REQUESTED_BMC_TRANSITION", value);
Josh D. King6db38222016-12-19 14:52:40 -0600226
Josh D. King5162a7b2016-12-19 16:15:00 -0600227 executeTransition(value);
228 return server::BMC::requestedBMCTransition(value);
Josh D. King6db38222016-12-19 14:52:40 -0600229}
230
Josh D. Kingd613b812016-12-19 16:47:45 -0600231BMC::BMCState BMC::currentBMCState(BMCState value)
232{
Andrew Geissler429100a2021-09-09 12:50:24 -0500233 info("Setting the BMCState field to {CURRENT_BMC_STATE}",
234 "CURRENT_BMC_STATE", value);
Josh D. Kingd613b812016-12-19 16:47:45 -0600235
236 return server::BMC::currentBMCState(value);
237}
238
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800239BMC::RebootCause BMC::lastRebootCause(RebootCause value)
240{
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500241 info("Setting the RebootCause field to {LAST_REBOOT_CAUSE}",
242 "LAST_REBOOT_CAUSE", value);
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800243
244 return server::BMC::lastRebootCause(value);
245}
246
Willy Tubd1eebd2023-10-05 12:14:20 -0700247void BMC::updateLastRebootTime()
248{
249 using namespace std::chrono;
250 struct sysinfo info;
251
252 auto rc = sysinfo(&info);
253 assert(rc == 0);
254 // Since uptime is in seconds, also get the current time in seconds.
255 auto now = time_point_cast<seconds>(system_clock::now());
256 auto rebootTimeTs = now - seconds(info.uptime);
257 rebootTime =
258 duration_cast<milliseconds>(rebootTimeTs.time_since_epoch()).count();
Willy Tu47120842024-01-09 14:04:03 -0800259 server::BMC::lastRebootTime(rebootTime);
Willy Tubd1eebd2023-10-05 12:14:20 -0700260}
261
Matt Spinlere6710b72018-07-12 16:05:55 -0500262uint64_t BMC::lastRebootTime() const
263{
Willy Tu564eb4f2023-09-07 16:02:46 -0700264 return rebootTime;
Matt Spinlere6710b72018-07-12 16:05:55 -0500265}
266
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800267void BMC::discoverLastRebootCause()
268{
269 uint64_t bootReason = 0;
270 std::ifstream file;
Pavithra Barithaya319eda42024-06-21 11:54:43 -0500271 const auto* bootstatusPath = "/sys/class/watchdog/watchdog0/bootstatus";
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800272
273 file.exceptions(std::ifstream::failbit | std::ifstream::badbit |
274 std::ifstream::eofbit);
275
276 try
277 {
278 file.open(bootstatusPath);
279 file >> bootReason;
280 }
281 catch (const std::exception& e)
282 {
283 auto rc = errno;
Andrew Geissler8ffdb262021-09-20 15:25:19 -0500284 error("Failed to read sysfs file {FILE} with errno {ERRNO}", "FILE",
285 bootstatusPath, "ERRNO", rc);
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800286 }
287
288 switch (bootReason)
289 {
290 case WDIOF_EXTERN1:
Tim Lee4ab59212024-09-05 09:51:02 +0800291 this->lastRebootCause(RebootCause::Software);
Andrew Geissler98e64e62022-01-25 16:02:56 -0600292 return;
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800293 case WDIOF_CARDRESET:
Tim Lee4ab59212024-09-05 09:51:02 +0800294 this->lastRebootCause(RebootCause::Watchdog);
Andrew Geissler98e64e62022-01-25 16:02:56 -0600295 return;
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800296 default:
Tim Lee4ab59212024-09-05 09:51:02 +0800297 this->lastRebootCause(RebootCause::POR);
Andrew Geissler98e64e62022-01-25 16:02:56 -0600298 // Continue below to see if more details can be found
299 // on reason for reboot
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800300 break;
301 }
302
Andrew Geissler98e64e62022-01-25 16:02:56 -0600303 // If the above code could not detect a reason, look for a the
304 // reset-cause-pinhole gpio to see if it is the reason for the reboot
305 auto gpioval =
306 phosphor::state::manager::utils::getGpioValue("reset-cause-pinhole");
307
Andrew Geissler2e352a22022-03-02 11:31:40 -0600308 // A 0 indicates a pinhole reset occurred
309 if (0 == gpioval)
Andrew Geissler98e64e62022-01-25 16:02:56 -0600310 {
311 info("The BMC reset was caused by a pinhole reset");
312 this->lastRebootCause(RebootCause::PinholeReset);
Andrew Geisslera2a7e122022-01-26 13:30:18 -0600313
314 // Generate log telling user a pinhole reset has occurred
315 const std::string errorMsg = "xyz.openbmc_project.State.PinholeReset";
316 phosphor::state::manager::utils::createError(
317 this->bus, errorMsg,
Patrick Williams7e969cb2023-08-23 16:24:23 -0500318 sdbusplus::server::xyz::openbmc_project::logging::Entry::Level::
Andrew Geisslera2a7e122022-01-26 13:30:18 -0600319 Notice);
Andrew Geissler2038e492023-06-16 15:32:58 -0400320 return;
321 }
322
323 // If we still haven't found a reason, see if we lost AC power
324 // Note that a pinhole reset will remove AC power to the chassis
325 // on some systems so we always want to look for the pinhole reset
326 // first as that would be the main reason AC power was lost.
327 size_t chassisId = 0;
328 if (phosphor::state::manager::utils::checkACLoss(chassisId))
329 {
330 this->lastRebootCause(RebootCause::POR);
Andrew Geissler98e64e62022-01-25 16:02:56 -0600331 }
332
Tim Lee2bfb1ef2021-03-17 09:50:35 +0800333 return;
334}
335
Josh D. Kingbdd9cb72016-12-19 11:13:43 -0600336} // namespace manager
337} // namespace state
Andrew Geisslera965cf02018-08-31 08:37:05 -0700338} // namespace phosphor