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; |
Andrew Geissler | 2f60aae | 2019-09-12 13:25:21 -0500 | [diff] [blame] | 24 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 25 | |
Anthony Wilson | eef31f8 | 2019-04-23 17:04:09 -0500 | [diff] [blame] | 26 | constexpr auto obmcStandbyTarget = "multi-user.target"; |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 27 | constexpr auto signalDone = "done"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 28 | constexpr auto activeState = "active"; |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 29 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 30 | /* Map a transition to it's systemd target */ |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 31 | const std::map<server::BMC::Transition, const char*> SYSTEMD_TABLE = { |
| 32 | {server::BMC::Transition::Reboot, "reboot.target"}}; |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 33 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 34 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 35 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
| 36 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 37 | constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties"; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 38 | |
| 39 | void BMC::discoverInitialState() |
| 40 | { |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 41 | std::variant<std::string> currentState; |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 42 | sdbusplus::message::object_path unitTargetPath; |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 43 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 44 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 45 | SYSTEMD_INTERFACE, "GetUnit"); |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 46 | |
| 47 | method.append(obmcStandbyTarget); |
| 48 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 49 | try |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 50 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 51 | auto result = this->bus.call(method); |
| 52 | result.read(unitTargetPath); |
| 53 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame^] | 54 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 55 | { |
| 56 | 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] | 57 | return; |
| 58 | } |
| 59 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 60 | method = this->bus.new_method_call( |
| 61 | SYSTEMD_SERVICE, |
| 62 | static_cast<const std::string&>(unitTargetPath).c_str(), |
| 63 | SYSTEMD_PRP_INTERFACE, "Get"); |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 64 | |
| 65 | method.append("org.freedesktop.systemd1.Unit", "ActiveState"); |
| 66 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 67 | try |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 68 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 69 | auto result = this->bus.call(method); |
| 70 | |
| 71 | // Is obmc-standby.target active or inactive? |
| 72 | result.read(currentState); |
| 73 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame^] | 74 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 75 | { |
| 76 | log<level::INFO>("Error in ActiveState Get", |
| 77 | entry("ERROR=%s", e.what())); |
Josh D. King | 2b5d887 | 2017-02-21 13:37:17 -0600 | [diff] [blame] | 78 | return; |
| 79 | } |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 80 | |
Patrick Williams | 37413dc | 2020-05-13 11:29:54 -0500 | [diff] [blame] | 81 | auto currentStateStr = std::get<std::string>(currentState); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 82 | if (currentStateStr == activeState) |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 83 | { |
| 84 | log<level::INFO>("Setting the BMCState field", |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 85 | entry("CURRENT_BMC_STATE=%s", "BMC_READY")); |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 86 | this->currentBMCState(BMCState::Ready); |
| 87 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 88 | // Unsubscribe so we stop processing all other signals |
| 89 | method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 90 | SYSTEMD_INTERFACE, "Unsubscribe"); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 91 | try |
| 92 | { |
| 93 | this->bus.call(method); |
| 94 | this->stateSignal.release(); |
| 95 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame^] | 96 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 97 | { |
| 98 | log<level::INFO>("Error in Unsubscribe", |
| 99 | entry("ERROR=%s", e.what())); |
| 100 | } |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 101 | } |
| 102 | else |
| 103 | { |
| 104 | log<level::INFO>("Setting the BMCState field", |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 105 | entry("CURRENT_BMC_STATE=%s", "BMC_NOTREADY")); |
Josh D. King | d3e5847 | 2017-02-02 11:09:11 -0600 | [diff] [blame] | 106 | this->currentBMCState(BMCState::NotReady); |
| 107 | } |
| 108 | |
| 109 | return; |
| 110 | } |
| 111 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 112 | void BMC::subscribeToSystemdSignals() |
| 113 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 114 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 115 | SYSTEMD_INTERFACE, "Subscribe"); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 116 | |
| 117 | try |
| 118 | { |
| 119 | this->bus.call(method); |
| 120 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame^] | 121 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 122 | { |
Andrew Geissler | 2f60aae | 2019-09-12 13:25:21 -0500 | [diff] [blame] | 123 | log<level::ERR>("Failed to subscribe to systemd signals", |
| 124 | entry("ERR=%s", e.what())); |
| 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 | { |
| 145 | log<level::INFO>("Error in HardReboot", |
| 146 | entry("ERROR=%s", e.what())); |
| 147 | } |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 148 | } |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 149 | else |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 150 | { |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 151 | // Check to make sure it can be found |
| 152 | auto iter = SYSTEMD_TABLE.find(tranReq); |
| 153 | if (iter == SYSTEMD_TABLE.end()) |
| 154 | return; |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 155 | |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 156 | const auto& sysdUnit = iter->second; |
| 157 | |
| 158 | auto method = this->bus.new_method_call( |
| 159 | SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "StartUnit"); |
| 160 | // The only valid transition is reboot and that |
| 161 | // needs to be irreversible once started |
| 162 | |
| 163 | method.append(sysdUnit, "replace-irreversibly"); |
| 164 | |
| 165 | try |
| 166 | { |
| 167 | this->bus.call(method); |
| 168 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame^] | 169 | catch (const sdbusplus::exception::exception& e) |
Jayaprakash Mutyala | 44c223c | 2020-08-14 00:08:03 +0000 | [diff] [blame] | 170 | { |
| 171 | log<level::INFO>("Error in StartUnit - replace-irreversibly", |
| 172 | entry("ERROR=%s", e.what())); |
| 173 | } |
| 174 | } |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
Patrick Williams | d32f818 | 2017-05-05 15:55:24 -0500 | [diff] [blame] | 178 | int BMC::bmcStateChange(sdbusplus::message::message& msg) |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 179 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 180 | uint32_t newStateID{}; |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 181 | sdbusplus::message::object_path newStateObjPath; |
| 182 | std::string newStateUnit{}; |
| 183 | std::string newStateResult{}; |
| 184 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 185 | // Read the msg and populate each variable |
Patrick Williams | d32f818 | 2017-05-05 15:55:24 -0500 | [diff] [blame] | 186 | msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 187 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 188 | // Caught the signal that indicates the BMC is now BMC_READY |
| 189 | if ((newStateUnit == obmcStandbyTarget) && (newStateResult == signalDone)) |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 190 | { |
| 191 | log<level::INFO>("BMC_READY"); |
| 192 | this->currentBMCState(BMCState::Ready); |
| 193 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 194 | // Unsubscribe so we stop processing all other signals |
| 195 | auto method = |
| 196 | this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 197 | SYSTEMD_INTERFACE, "Unsubscribe"); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 198 | |
| 199 | try |
| 200 | { |
| 201 | this->bus.call(method); |
| 202 | this->stateSignal.release(); |
| 203 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame^] | 204 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 205 | { |
| 206 | log<level::INFO>("Error in Unsubscribe", |
| 207 | entry("ERROR=%s", e.what())); |
| 208 | } |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 214 | BMC::Transition BMC::requestedBMCTransition(Transition value) |
| 215 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 216 | log<level::INFO>("Setting the RequestedBMCTransition field", |
| 217 | entry("REQUESTED_BMC_TRANSITION=0x%s", |
| 218 | convertForMessage(value).c_str())); |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 219 | |
Josh D. King | 5162a7b | 2016-12-19 16:15:00 -0600 | [diff] [blame] | 220 | executeTransition(value); |
| 221 | return server::BMC::requestedBMCTransition(value); |
Josh D. King | 6db3822 | 2016-12-19 14:52:40 -0600 | [diff] [blame] | 222 | } |
| 223 | |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 224 | BMC::BMCState BMC::currentBMCState(BMCState value) |
| 225 | { |
| 226 | log<level::INFO>( |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 227 | "Setting the BMCState field", |
| 228 | entry("CURRENT_BMC_STATE=0x%s", convertForMessage(value).c_str())); |
Josh D. King | d613b81 | 2016-12-19 16:47:45 -0600 | [diff] [blame] | 229 | |
| 230 | return server::BMC::currentBMCState(value); |
| 231 | } |
| 232 | |
Matt Spinler | e6710b7 | 2018-07-12 16:05:55 -0500 | [diff] [blame] | 233 | uint64_t BMC::lastRebootTime() const |
| 234 | { |
| 235 | using namespace std::chrono; |
| 236 | struct sysinfo info; |
| 237 | |
| 238 | auto rc = sysinfo(&info); |
| 239 | assert(rc == 0); |
| 240 | |
| 241 | // Since uptime is in seconds, also get the current time in seconds. |
| 242 | auto now = time_point_cast<seconds>(system_clock::now()); |
| 243 | auto rebootTime = now - seconds(info.uptime); |
| 244 | |
| 245 | return duration_cast<milliseconds>(rebootTime.time_since_epoch()).count(); |
| 246 | } |
| 247 | |
Josh D. King | bdd9cb7 | 2016-12-19 11:13:43 -0600 | [diff] [blame] | 248 | } // namespace manager |
| 249 | } // namespace state |
Andrew Geissler | a965cf0 | 2018-08-31 08:37:05 -0700 | [diff] [blame] | 250 | } // namespace phosphor |