Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 1 | #include "bmc_state_manager.hpp" |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 2 | |
Andrew Geissler | 2f60aae | 2019-09-12 13:25:21 -0500 | [diff] [blame] | 3 | #include "xyz/openbmc_project/Common/error.hpp" |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 4 | |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 5 | #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> |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame^] | 12 | #include <filesystem> |
| 13 | #include <fstream> |
| 14 | #include <iostream> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 15 | |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 16 | namespace phosphor |
| 17 | { |
| 18 | namespace state |
| 19 | { |
| 20 | namespace manager |
| 21 | { |
| 22 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 23 | // When you see server:: you know we're referencing our base class |
| 24 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
| 25 | |
| 26 | using namespace phosphor::logging; |
Andrew Geissler | 2f60aae | 2019-09-12 13:25:21 -0500 | [diff] [blame] | 27 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 28 | |
Anthony Wilson | eef31f8 | 2019-04-23 17:04:09 -0500 | [diff] [blame] | 29 | constexpr auto obmcStandbyTarget = "multi-user.target"; |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 30 | constexpr auto signalDone = "done"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 31 | constexpr auto activeState = "active"; |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 32 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 33 | /* Map a transition to it's systemd target */ |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 34 | const std::map<server::BMC::Transition, const char*> SYSTEMD_TABLE = { |
| 35 | {server::BMC::Transition::Reboot, "reboot.target"}}; |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 36 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 37 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 38 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
| 39 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 40 | constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 41 | |
| 42 | void BMC::discoverInitialState() |
| 43 | { |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 44 | std::variant<std::string> currentState; |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 45 | sdbusplus::message::object_path unitTargetPath; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 46 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 47 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 48 | SYSTEMD_INTERFACE, "GetUnit"); |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 49 | |
| 50 | method.append(obmcStandbyTarget); |
| 51 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 52 | try |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 53 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 54 | auto result = this->bus.call(method); |
| 55 | result.read(unitTargetPath); |
| 56 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 57 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 58 | { |
| 59 | log<level::ERR>("Error in GetUnit call", entry("ERROR=%s", e.what())); |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 60 | return; |
| 61 | } |
| 62 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 63 | method = this->bus.new_method_call( |
| 64 | SYSTEMD_SERVICE, |
| 65 | static_cast<const std::string&>(unitTargetPath).c_str(), |
| 66 | SYSTEMD_PRP_INTERFACE, "Get"); |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 67 | |
| 68 | method.append("org.freedesktop.systemd1.Unit", "ActiveState"); |
| 69 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 70 | try |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 71 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 72 | auto result = this->bus.call(method); |
| 73 | |
| 74 | // Is obmc-standby.target active or inactive? |
| 75 | result.read(currentState); |
| 76 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 77 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 78 | { |
| 79 | log<level::INFO>("Error in ActiveState Get", |
| 80 | entry("ERROR=%s", e.what())); |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 81 | return; |
| 82 | } |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 83 | |
Patrick Williams | 37413dc | 2020-05-13 11:29:54 -0500 | [diff] [blame] | 84 | auto currentStateStr = std::get<std::string>(currentState); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 85 | if (currentStateStr == activeState) |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 86 | { |
| 87 | log<level::INFO>("Setting the BMCState field", |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 88 | entry("CURRENT_BMC_STATE=%s", "BMC_READY")); |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 89 | this->currentBMCState(BMCState::Ready); |
| 90 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 91 | // 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 Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 94 | try |
| 95 | { |
| 96 | this->bus.call(method); |
| 97 | this->stateSignal.release(); |
| 98 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 99 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 100 | { |
| 101 | log<level::INFO>("Error in Unsubscribe", |
| 102 | entry("ERROR=%s", e.what())); |
| 103 | } |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 104 | } |
| 105 | else |
| 106 | { |
| 107 | log<level::INFO>("Setting the BMCState field", |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 108 | entry("CURRENT_BMC_STATE=%s", "BMC_NOTREADY")); |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 109 | this->currentBMCState(BMCState::NotReady); |
| 110 | } |
| 111 | |
| 112 | return; |
| 113 | } |
| 114 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 115 | void BMC::subscribeToSystemdSignals() |
| 116 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 117 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 118 | SYSTEMD_INTERFACE, "Subscribe"); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 119 | |
| 120 | try |
| 121 | { |
| 122 | this->bus.call(method); |
| 123 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 124 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 125 | { |
Andrew Geissler | 2f60aae | 2019-09-12 13:25:21 -0500 | [diff] [blame] | 126 | log<level::ERR>("Failed to subscribe to systemd signals", |
| 127 | entry("ERR=%s", e.what())); |
| 128 | elog<InternalFailure>(); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 129 | } |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 130 | |
| 131 | return; |
| 132 | } |
| 133 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 134 | void BMC::executeTransition(const Transition tranReq) |
| 135 | { |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 136 | // HardReboot does not shutdown any services and immediately transitions |
| 137 | // into the reboot process |
| 138 | if (server::BMC::Transition::HardReboot == tranReq) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 139 | { |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 140 | auto method = this->bus.new_method_call( |
| 141 | SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "Reboot"); |
| 142 | try |
| 143 | { |
| 144 | this->bus.call(method); |
| 145 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 146 | catch (const sdbusplus::exception::exception& e) |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 147 | { |
| 148 | log<level::INFO>("Error in HardReboot", |
| 149 | entry("ERROR=%s", e.what())); |
| 150 | } |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 151 | } |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 152 | else |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 153 | { |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 154 | // Check to make sure it can be found |
| 155 | auto iter = SYSTEMD_TABLE.find(tranReq); |
| 156 | if (iter == SYSTEMD_TABLE.end()) |
| 157 | return; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 158 | |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 159 | const auto& sysdUnit = iter->second; |
| 160 | |
| 161 | auto method = this->bus.new_method_call( |
| 162 | SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "StartUnit"); |
| 163 | // The only valid transition is reboot and that |
| 164 | // needs to be irreversible once started |
| 165 | |
| 166 | method.append(sysdUnit, "replace-irreversibly"); |
| 167 | |
| 168 | try |
| 169 | { |
| 170 | this->bus.call(method); |
| 171 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 172 | catch (const sdbusplus::exception::exception& e) |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 173 | { |
| 174 | log<level::INFO>("Error in StartUnit - replace-irreversibly", |
| 175 | entry("ERROR=%s", e.what())); |
| 176 | } |
| 177 | } |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | |
Patrick Williams | d32f818 | 2017-05-05 15:55:24 -0500 | [diff] [blame] | 181 | int BMC::bmcStateChange(sdbusplus::message::message& msg) |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 182 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 183 | uint32_t newStateID{}; |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 184 | sdbusplus::message::object_path newStateObjPath; |
| 185 | std::string newStateUnit{}; |
| 186 | std::string newStateResult{}; |
| 187 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 188 | // Read the msg and populate each variable |
Patrick Williams | d32f818 | 2017-05-05 15:55:24 -0500 | [diff] [blame] | 189 | msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 190 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 191 | // Caught the signal that indicates the BMC is now BMC_READY |
| 192 | if ((newStateUnit == obmcStandbyTarget) && (newStateResult == signalDone)) |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 193 | { |
| 194 | log<level::INFO>("BMC_READY"); |
| 195 | this->currentBMCState(BMCState::Ready); |
| 196 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 197 | // Unsubscribe so we stop processing all other signals |
| 198 | auto method = |
| 199 | this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 200 | SYSTEMD_INTERFACE, "Unsubscribe"); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 201 | |
| 202 | try |
| 203 | { |
| 204 | this->bus.call(method); |
| 205 | this->stateSignal.release(); |
| 206 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 207 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 208 | { |
| 209 | log<level::INFO>("Error in Unsubscribe", |
| 210 | entry("ERROR=%s", e.what())); |
| 211 | } |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 217 | BMC::Transition BMC::requestedBMCTransition(Transition value) |
| 218 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 219 | log<level::INFO>("Setting the RequestedBMCTransition field", |
| 220 | entry("REQUESTED_BMC_TRANSITION=0x%s", |
| 221 | convertForMessage(value).c_str())); |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 222 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 223 | executeTransition(value); |
| 224 | return server::BMC::requestedBMCTransition(value); |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 225 | } |
| 226 | |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 227 | BMC::BMCState BMC::currentBMCState(BMCState value) |
| 228 | { |
| 229 | log<level::INFO>( |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 230 | "Setting the BMCState field", |
| 231 | entry("CURRENT_BMC_STATE=0x%s", convertForMessage(value).c_str())); |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 232 | |
| 233 | return server::BMC::currentBMCState(value); |
| 234 | } |
| 235 | |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame^] | 236 | BMC::RebootCause BMC::lastRebootCause(RebootCause value) |
| 237 | { |
| 238 | log<level::INFO>( |
| 239 | "Setting the RebootCause field", |
| 240 | entry("LAST_REBOOT_CAUSE=0x%s", convertForMessage(value).c_str())); |
| 241 | |
| 242 | return server::BMC::lastRebootCause(value); |
| 243 | } |
| 244 | |
Matt Spinler | e6710b7 | 2018-07-12 16:05:55 -0500 | [diff] [blame] | 245 | uint64_t BMC::lastRebootTime() const |
| 246 | { |
| 247 | using namespace std::chrono; |
| 248 | struct sysinfo info; |
| 249 | |
| 250 | auto rc = sysinfo(&info); |
| 251 | assert(rc == 0); |
| 252 | |
| 253 | // Since uptime is in seconds, also get the current time in seconds. |
| 254 | auto now = time_point_cast<seconds>(system_clock::now()); |
| 255 | auto rebootTime = now - seconds(info.uptime); |
| 256 | |
| 257 | return duration_cast<milliseconds>(rebootTime.time_since_epoch()).count(); |
| 258 | } |
| 259 | |
Tim Lee | 2bfb1ef | 2021-03-17 09:50:35 +0800 | [diff] [blame^] | 260 | void BMC::discoverLastRebootCause() |
| 261 | { |
| 262 | uint64_t bootReason = 0; |
| 263 | std::ifstream file; |
| 264 | auto bootstatusPath = "/sys/class/watchdog/watchdog0/bootstatus"; |
| 265 | |
| 266 | file.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
| 267 | std::ifstream::eofbit); |
| 268 | |
| 269 | try |
| 270 | { |
| 271 | file.open(bootstatusPath); |
| 272 | file >> bootReason; |
| 273 | } |
| 274 | catch (const std::exception& e) |
| 275 | { |
| 276 | auto rc = errno; |
| 277 | log<level::ERR>((std::string("Failed to read sysfs file " |
| 278 | "errno=") + |
| 279 | std::to_string(rc) + " FILENAME=" + bootstatusPath) |
| 280 | .c_str()); |
| 281 | } |
| 282 | |
| 283 | switch (bootReason) |
| 284 | { |
| 285 | case WDIOF_EXTERN1: |
| 286 | this->lastRebootCause(RebootCause::Watchdog); |
| 287 | break; |
| 288 | case WDIOF_CARDRESET: |
| 289 | this->lastRebootCause(RebootCause::POR); |
| 290 | break; |
| 291 | default: |
| 292 | this->lastRebootCause(RebootCause::Unknown); |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | return; |
| 297 | } |
| 298 | |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 299 | } // namespace manager |
| 300 | } // namespace state |
Andrew Geissler | a965cf0 | 2018-08-31 08:37:05 -0700 | [diff] [blame] | 301 | } // namespace phosphor |