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