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> |
| 12 | |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 13 | namespace phosphor |
| 14 | { |
| 15 | namespace state |
| 16 | { |
| 17 | namespace manager |
| 18 | { |
| 19 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 20 | // When you see server:: you know we're referencing our base class |
| 21 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
| 22 | |
| 23 | using namespace phosphor::logging; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 24 | using sdbusplus::exception::SdBusError; |
Andrew Geissler | 2f60aae | 2019-09-12 13:25:21 -0500 | [diff] [blame] | 25 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 26 | |
Anthony Wilson | eef31f8 | 2019-04-23 17:04:09 -0500 | [diff] [blame] | 27 | constexpr auto obmcStandbyTarget = "multi-user.target"; |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 28 | constexpr auto signalDone = "done"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 29 | constexpr auto activeState = "active"; |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 30 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 31 | /* Map a transition to it's systemd target */ |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 32 | const std::map<server::BMC::Transition, const char*> SYSTEMD_TABLE = { |
| 33 | {server::BMC::Transition::Reboot, "reboot.target"}}; |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 34 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 35 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 36 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
| 37 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 38 | constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 39 | |
| 40 | void BMC::discoverInitialState() |
| 41 | { |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 42 | std::variant<std::string> currentState; |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 43 | sdbusplus::message::object_path unitTargetPath; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 44 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 45 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 46 | SYSTEMD_INTERFACE, "GetUnit"); |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 47 | |
| 48 | method.append(obmcStandbyTarget); |
| 49 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 50 | try |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 51 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 52 | 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. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 58 | return; |
| 59 | } |
| 60 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 61 | 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. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 65 | |
| 66 | method.append("org.freedesktop.systemd1.Unit", "ActiveState"); |
| 67 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 68 | try |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 69 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 70 | 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. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 79 | return; |
| 80 | } |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 81 | |
Patrick Williams | 37413dc | 2020-05-13 11:29:54 -0500 | [diff] [blame] | 82 | auto currentStateStr = std::get<std::string>(currentState); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 83 | if (currentStateStr == activeState) |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 84 | { |
| 85 | log<level::INFO>("Setting the BMCState field", |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 86 | entry("CURRENT_BMC_STATE=%s", "BMC_READY")); |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 87 | this->currentBMCState(BMCState::Ready); |
| 88 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 89 | // 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 Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 92 | 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. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 102 | } |
| 103 | else |
| 104 | { |
| 105 | log<level::INFO>("Setting the BMCState field", |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 106 | entry("CURRENT_BMC_STATE=%s", "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 | } |
| 122 | catch (const SdBusError& e) |
| 123 | { |
Andrew Geissler | 2f60aae | 2019-09-12 13:25:21 -0500 | [diff] [blame] | 124 | log<level::ERR>("Failed to subscribe to systemd signals", |
| 125 | entry("ERR=%s", e.what())); |
| 126 | elog<InternalFailure>(); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 127 | } |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 128 | |
| 129 | return; |
| 130 | } |
| 131 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 132 | void BMC::executeTransition(const Transition tranReq) |
| 133 | { |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 134 | // HardReboot does not shutdown any services and immediately transitions |
| 135 | // into the reboot process |
| 136 | if (server::BMC::Transition::HardReboot == tranReq) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 137 | { |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 138 | 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 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 | else |
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 | // Check to make sure it can be found |
| 153 | auto iter = SYSTEMD_TABLE.find(tranReq); |
| 154 | if (iter == SYSTEMD_TABLE.end()) |
| 155 | return; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 156 | |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 157 | 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. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | |
Patrick Williams | d32f818 | 2017-05-05 15:55:24 -0500 | [diff] [blame] | 179 | int BMC::bmcStateChange(sdbusplus::message::message& msg) |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 180 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 181 | uint32_t newStateID{}; |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 182 | sdbusplus::message::object_path newStateObjPath; |
| 183 | std::string newStateUnit{}; |
| 184 | std::string newStateResult{}; |
| 185 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 186 | // Read the msg and populate each variable |
Patrick Williams | d32f818 | 2017-05-05 15:55:24 -0500 | [diff] [blame] | 187 | msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 188 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 189 | // Caught the signal that indicates the BMC is now BMC_READY |
| 190 | if ((newStateUnit == obmcStandbyTarget) && (newStateResult == signalDone)) |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 191 | { |
| 192 | log<level::INFO>("BMC_READY"); |
| 193 | this->currentBMCState(BMCState::Ready); |
| 194 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 195 | // 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 Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 199 | |
| 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. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 215 | BMC::Transition BMC::requestedBMCTransition(Transition value) |
| 216 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 217 | log<level::INFO>("Setting the RequestedBMCTransition field", |
| 218 | entry("REQUESTED_BMC_TRANSITION=0x%s", |
| 219 | convertForMessage(value).c_str())); |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 220 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 221 | executeTransition(value); |
| 222 | return server::BMC::requestedBMCTransition(value); |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 223 | } |
| 224 | |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 225 | BMC::BMCState BMC::currentBMCState(BMCState value) |
| 226 | { |
| 227 | log<level::INFO>( |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 228 | "Setting the BMCState field", |
| 229 | entry("CURRENT_BMC_STATE=0x%s", convertForMessage(value).c_str())); |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 230 | |
| 231 | return server::BMC::currentBMCState(value); |
| 232 | } |
| 233 | |
Matt Spinler | e6710b7 | 2018-07-12 16:05:55 -0500 | [diff] [blame] | 234 | uint64_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. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 249 | } // namespace manager |
| 250 | } // namespace state |
Andrew Geissler | a965cf0 | 2018-08-31 08:37:05 -0700 | [diff] [blame] | 251 | } // namespace phosphor |