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