Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "chassis_state_manager.hpp" |
| 4 | |
| 5 | #include "xyz/openbmc_project/Common/error.hpp" |
| 6 | #include "xyz/openbmc_project/State/Shutdown/Power/error.hpp" |
| 7 | |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 8 | #include <gpiod.h> |
| 9 | |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 10 | #include <cereal/archives/json.hpp> |
| 11 | #include <phosphor-logging/elog-errors.hpp> |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 12 | #include <phosphor-logging/lg2.hpp> |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 13 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 14 | #include <sdbusplus/exception.hpp> |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 15 | #include <sdeventplus/event.hpp> |
| 16 | #include <sdeventplus/exception.hpp> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 17 | |
Andrew Geissler | f2b22e8 | 2021-03-12 14:47:03 -0600 | [diff] [blame] | 18 | #include <filesystem> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 19 | #include <fstream> |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 20 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 21 | namespace phosphor |
| 22 | { |
| 23 | namespace state |
| 24 | { |
| 25 | namespace manager |
| 26 | { |
| 27 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 28 | PHOSPHOR_LOG2_USING; |
| 29 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 30 | // When you see server:: you know we're referencing our base class |
| 31 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
| 32 | |
| 33 | using namespace phosphor::logging; |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 34 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Aatir Manzur | 27115ae | 2019-07-23 16:25:38 -0500 | [diff] [blame] | 35 | using sdbusplus::xyz::openbmc_project::State::Shutdown::Power::Error::Blackout; |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 36 | using sdbusplus::xyz::openbmc_project::State::Shutdown::Power::Error::Regulator; |
Josh D. King | 6838ea9 | 2017-04-11 13:39:18 -0500 | [diff] [blame] | 37 | constexpr auto CHASSIS_STATE_POWEROFF_TGT = "obmc-chassis-poweroff@0.target"; |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 38 | constexpr auto CHASSIS_STATE_HARD_POWEROFF_TGT = |
| 39 | "obmc-chassis-hard-poweroff@0.target"; |
Josh D. King | 6838ea9 | 2017-04-11 13:39:18 -0500 | [diff] [blame] | 40 | constexpr auto CHASSIS_STATE_POWERON_TGT = "obmc-chassis-poweron@0.target"; |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 41 | |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 42 | constexpr auto ACTIVE_STATE = "active"; |
| 43 | constexpr auto ACTIVATING_STATE = "activating"; |
| 44 | |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 45 | /* Map a transition to it's systemd target */ |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 46 | const std::map<server::Chassis::Transition, std::string> SYSTEMD_TARGET_TABLE = |
| 47 | { |
Andrew Geissler | 8cf2f9a | 2017-07-21 11:58:04 -0500 | [diff] [blame] | 48 | // Use the hard off target to ensure we shutdown immediately |
| 49 | {server::Chassis::Transition::Off, CHASSIS_STATE_HARD_POWEROFF_TGT}, |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 50 | {server::Chassis::Transition::On, CHASSIS_STATE_POWERON_TGT}}; |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 51 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 52 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 53 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 54 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 55 | |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 56 | constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties"; |
| 57 | constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit"; |
| 58 | |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 59 | void Chassis::subscribeToSystemdSignals() |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 60 | { |
Andrew Geissler | 3a30b05 | 2019-05-14 15:54:37 -0500 | [diff] [blame] | 61 | try |
| 62 | { |
| 63 | auto method = this->bus.new_method_call( |
| 64 | SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "Subscribe"); |
Andrew Geissler | 5b95027 | 2019-05-24 12:27:51 -0500 | [diff] [blame] | 65 | this->bus.call(method); |
Andrew Geissler | 3a30b05 | 2019-05-14 15:54:37 -0500 | [diff] [blame] | 66 | } |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 67 | catch (const sdbusplus::exception::exception& e) |
Andrew Geissler | 3a30b05 | 2019-05-14 15:54:37 -0500 | [diff] [blame] | 68 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 69 | error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", e); |
Andrew Geissler | 3a30b05 | 2019-05-14 15:54:37 -0500 | [diff] [blame] | 70 | elog<InternalFailure>(); |
| 71 | } |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 72 | |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 73 | return; |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 74 | } |
| 75 | |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 76 | // TODO - Will be rewritten once sdbusplus client bindings are in place |
| 77 | // and persistent storage design is in place and sdbusplus |
| 78 | // has read property function |
| 79 | void Chassis::determineInitialState() |
| 80 | { |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 81 | std::variant<int> pgood = -1; |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 82 | auto method = this->bus.new_method_call( |
| 83 | "org.openbmc.control.Power", "/org/openbmc/control/power0", |
| 84 | "org.freedesktop.DBus.Properties", "Get"); |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 85 | |
| 86 | method.append("org.openbmc.control.Power", "pgood"); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 87 | try |
| 88 | { |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 89 | auto reply = this->bus.call(method); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 90 | reply.read(pgood); |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 91 | |
Patrick Williams | 37413dc | 2020-05-13 11:29:54 -0500 | [diff] [blame] | 92 | if (std::get<int>(pgood) == 1) |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 93 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 94 | info("Initial Chassis State will be On"); |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 95 | server::Chassis::currentPowerState(PowerState::On); |
| 96 | server::Chassis::requestedPowerTransition(Transition::On); |
| 97 | return; |
| 98 | } |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 99 | else |
| 100 | { |
| 101 | // The system is off. If we think it should be on then |
| 102 | // we probably lost AC while up, so set a new state |
| 103 | // change time. |
| 104 | uint64_t lastTime; |
| 105 | PowerState lastState; |
| 106 | |
| 107 | if (deserializeStateChangeTime(lastTime, lastState)) |
| 108 | { |
| 109 | if (lastState == PowerState::On) |
| 110 | { |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 111 | if (standbyVoltageRegulatorFault()) |
| 112 | { |
| 113 | report<Regulator>(); |
| 114 | } |
| 115 | else |
| 116 | { |
| 117 | report<Blackout>(); |
| 118 | } |
| 119 | |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 120 | setStateChangeTime(); |
| 121 | } |
| 122 | } |
| 123 | } |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 124 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 125 | catch (const sdbusplus::exception::exception& e) |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 126 | { |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 127 | // It's acceptable for the pgood state service to not be available |
| 128 | // since it will notify us of the pgood state when it comes up. |
| 129 | if (e.name() != nullptr && |
| 130 | strcmp("org.freedesktop.DBus.Error.ServiceUnknown", e.name()) == 0) |
| 131 | { |
| 132 | goto fail; |
| 133 | } |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 134 | |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 135 | // Only log for unexpected error types. |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 136 | error("Error performing call to get pgood: {ERROR}", "ERROR", e); |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 137 | goto fail; |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 138 | } |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 139 | |
| 140 | fail: |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 141 | info("Initial Chassis State will be Off"); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 142 | server::Chassis::currentPowerState(PowerState::Off); |
| 143 | server::Chassis::requestedPowerTransition(Transition::Off); |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 144 | |
| 145 | return; |
| 146 | } |
| 147 | |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 148 | void Chassis::executeTransition(Transition tranReq) |
| 149 | { |
| 150 | auto sysdTarget = SYSTEMD_TARGET_TABLE.find(tranReq)->second; |
| 151 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 152 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 153 | SYSTEMD_INTERFACE, "StartUnit"); |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 154 | |
| 155 | method.append(sysdTarget); |
| 156 | method.append("replace"); |
| 157 | |
| 158 | this->bus.call_noreply(method); |
| 159 | |
| 160 | return; |
| 161 | } |
| 162 | |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 163 | bool Chassis::stateActive(const std::string& target) |
| 164 | { |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 165 | std::variant<std::string> currentState; |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 166 | sdbusplus::message::object_path unitTargetPath; |
| 167 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 168 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 169 | SYSTEMD_INTERFACE, "GetUnit"); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 170 | |
| 171 | method.append(target); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 172 | |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 173 | try |
| 174 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 175 | auto result = this->bus.call(method); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 176 | result.read(unitTargetPath); |
| 177 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 178 | catch (const sdbusplus::exception::exception& e) |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 179 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 180 | error("Error in GetUnit call: {ERROR}", "ERROR", e); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 181 | return false; |
| 182 | } |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 183 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 184 | method = this->bus.new_method_call( |
| 185 | SYSTEMD_SERVICE, |
| 186 | static_cast<const std::string&>(unitTargetPath).c_str(), |
| 187 | SYSTEMD_PROPERTY_IFACE, "Get"); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 188 | |
| 189 | method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState"); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 190 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 191 | try |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 192 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 193 | auto result = this->bus.call(method); |
| 194 | result.read(currentState); |
| 195 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 196 | catch (const sdbusplus::exception::exception& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 197 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 198 | error("Error in ActiveState Get: {ERROR}", "ERROR", e); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 199 | return false; |
| 200 | } |
| 201 | |
Patrick Williams | 37413dc | 2020-05-13 11:29:54 -0500 | [diff] [blame] | 202 | const auto& currentStateStr = std::get<std::string>(currentState); |
William A. Kennington III | 7a0689a | 2018-11-12 17:19:33 -0800 | [diff] [blame] | 203 | return currentStateStr == ACTIVE_STATE || |
| 204 | currentStateStr == ACTIVATING_STATE; |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 205 | } |
| 206 | |
Patrick Williams | 8f8ba39 | 2017-05-05 15:47:39 -0500 | [diff] [blame] | 207 | int Chassis::sysStateChange(sdbusplus::message::message& msg) |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 208 | { |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 209 | sdbusplus::message::object_path newStateObjPath; |
| 210 | std::string newStateUnit{}; |
| 211 | std::string newStateResult{}; |
| 212 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 213 | // Read the msg and populate each variable |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 214 | try |
| 215 | { |
Andrew Geissler | 9b8af4f | 2019-09-12 14:19:14 -0500 | [diff] [blame] | 216 | // newStateID is a throwaway that is needed in order to read the |
| 217 | // parameters that are useful out of the dbus message |
| 218 | uint32_t newStateID{}; |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 219 | msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
| 220 | } |
Patrick Williams | 0a67521 | 2021-09-02 09:49:43 -0500 | [diff] [blame] | 221 | catch (const sdbusplus::exception::exception& e) |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 222 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 223 | error("Error in state change - bad encoding: {ERROR} {REPLY_SIG}", |
| 224 | "ERROR", e, "REPLY_SIG", msg.get_signature()); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 225 | return 0; |
| 226 | } |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 227 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 228 | if ((newStateUnit == CHASSIS_STATE_POWEROFF_TGT) && |
| 229 | (newStateResult == "done") && (!stateActive(CHASSIS_STATE_POWERON_TGT))) |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 230 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 231 | info("Received signal that power OFF is complete"); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 232 | this->currentPowerState(server::Chassis::PowerState::Off); |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 233 | this->setStateChangeTime(); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 234 | } |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 235 | else if ((newStateUnit == CHASSIS_STATE_POWERON_TGT) && |
| 236 | (newStateResult == "done") && |
| 237 | (stateActive(CHASSIS_STATE_POWERON_TGT))) |
| 238 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 239 | info("Received signal that power ON is complete"); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 240 | this->currentPowerState(server::Chassis::PowerState::On); |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 241 | this->setStateChangeTime(); |
Andrew Geissler | f2b22e8 | 2021-03-12 14:47:03 -0600 | [diff] [blame] | 242 | |
| 243 | // Remove temporary file which is utilized for scenarios where the |
| 244 | // BMC is rebooted while the chassis power is still on. |
| 245 | // This file is used to indicate to chassis related systemd services |
| 246 | // that the chassis is already on and they should skip running. |
| 247 | // Once the chassis state is back to on we can clear this file. |
| 248 | auto size = std::snprintf(nullptr, 0, CHASSIS_ON_FILE, 0); |
| 249 | size++; // null |
| 250 | std::unique_ptr<char[]> chassisFile(new char[size]); |
| 251 | std::snprintf(chassisFile.get(), size, CHASSIS_ON_FILE, 0); |
| 252 | if (std::filesystem::exists(chassisFile.get())) |
| 253 | { |
| 254 | std::filesystem::remove(chassisFile.get()); |
| 255 | } |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 256 | } |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 261 | Chassis::Transition Chassis::requestedPowerTransition(Transition value) |
| 262 | { |
| 263 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 264 | info("Change to Chassis Requested Power State: {REQ_POWER_TRAN}", |
| 265 | "REQ_POWER_TRAN", value); |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 266 | executeTransition(value); |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 267 | return server::Chassis::requestedPowerTransition(value); |
| 268 | } |
| 269 | |
| 270 | Chassis::PowerState Chassis::currentPowerState(PowerState value) |
| 271 | { |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 272 | PowerState chassisPowerState; |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 273 | info("Change to Chassis Power State: {CUR_POWER_STATE}", "CUR_POWER_STATE", |
| 274 | value); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 275 | |
| 276 | chassisPowerState = server::Chassis::currentPowerState(value); |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 277 | pohTimer.setEnabled(chassisPowerState == PowerState::On); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 278 | return chassisPowerState; |
| 279 | } |
| 280 | |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 281 | uint32_t Chassis::pohCounter(uint32_t value) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 282 | { |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 283 | if (value != pohCounter()) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 284 | { |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 285 | ChassisInherit::pohCounter(value); |
Matt Spinler | 8195784 | 2018-07-11 10:37:12 -0500 | [diff] [blame] | 286 | serializePOH(); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 287 | } |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 288 | return pohCounter(); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 289 | } |
| 290 | |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 291 | void Chassis::pohCallback() |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 292 | { |
| 293 | if (ChassisInherit::currentPowerState() == PowerState::On) |
| 294 | { |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 295 | pohCounter(pohCounter() + 1); |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 299 | void Chassis::restorePOHCounter() |
| 300 | { |
| 301 | uint32_t counter; |
Matt Spinler | 8195784 | 2018-07-11 10:37:12 -0500 | [diff] [blame] | 302 | if (!deserializePOH(POH_COUNTER_PERSIST_PATH, counter)) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 303 | { |
| 304 | // set to default value |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 305 | pohCounter(0); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 306 | } |
| 307 | else |
| 308 | { |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 309 | pohCounter(counter); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Matt Spinler | 8195784 | 2018-07-11 10:37:12 -0500 | [diff] [blame] | 313 | fs::path Chassis::serializePOH(const fs::path& path) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 314 | { |
| 315 | std::ofstream os(path.c_str(), std::ios::binary); |
| 316 | cereal::JSONOutputArchive oarchive(os); |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 317 | oarchive(pohCounter()); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 318 | return path; |
| 319 | } |
| 320 | |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 321 | bool Chassis::deserializePOH(const fs::path& path, uint32_t& pohCounter) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 322 | { |
| 323 | try |
| 324 | { |
| 325 | if (fs::exists(path)) |
| 326 | { |
| 327 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 328 | cereal::JSONInputArchive iarchive(is); |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 329 | iarchive(pohCounter); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 330 | return true; |
| 331 | } |
| 332 | return false; |
| 333 | } |
Patrick Williams | 8583b3b | 2021-10-06 12:19:20 -0500 | [diff] [blame^] | 334 | catch (const cereal::Exception& e) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 335 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 336 | error("deserialize exception: {ERROR}", "ERROR", e); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 337 | fs::remove(path); |
| 338 | return false; |
| 339 | } |
| 340 | catch (const fs::filesystem_error& e) |
| 341 | { |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | return false; |
| 346 | } |
| 347 | |
| 348 | void Chassis::startPOHCounter() |
| 349 | { |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 350 | auto dir = fs::path(POH_COUNTER_PERSIST_PATH).parent_path(); |
| 351 | fs::create_directories(dir); |
| 352 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 353 | try |
| 354 | { |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 355 | auto event = sdeventplus::Event::get_default(); |
| 356 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 357 | event.loop(); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 358 | } |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 359 | catch (const sdeventplus::SdEventError& e) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 360 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 361 | error("Error occurred during the sdeventplus loop: {ERROR}", "ERROR", |
| 362 | e); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 363 | phosphor::logging::commit<InternalFailure>(); |
| 364 | } |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 365 | } |
| 366 | |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 367 | void Chassis::serializeStateChangeTime() |
| 368 | { |
| 369 | fs::path path{CHASSIS_STATE_CHANGE_PERSIST_PATH}; |
| 370 | std::ofstream os(path.c_str(), std::ios::binary); |
| 371 | cereal::JSONOutputArchive oarchive(os); |
| 372 | |
| 373 | oarchive(ChassisInherit::lastStateChangeTime(), |
| 374 | ChassisInherit::currentPowerState()); |
| 375 | } |
| 376 | |
| 377 | bool Chassis::deserializeStateChangeTime(uint64_t& time, PowerState& state) |
| 378 | { |
| 379 | fs::path path{CHASSIS_STATE_CHANGE_PERSIST_PATH}; |
| 380 | |
| 381 | try |
| 382 | { |
| 383 | if (fs::exists(path)) |
| 384 | { |
| 385 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 386 | cereal::JSONInputArchive iarchive(is); |
| 387 | iarchive(time, state); |
| 388 | return true; |
| 389 | } |
| 390 | } |
Patrick Williams | 8583b3b | 2021-10-06 12:19:20 -0500 | [diff] [blame^] | 391 | catch (const std::exception& e) |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 392 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 393 | error("deserialize exception: {ERROR}", "ERROR", e); |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 394 | fs::remove(path); |
| 395 | } |
| 396 | |
| 397 | return false; |
| 398 | } |
| 399 | |
| 400 | void Chassis::restoreChassisStateChangeTime() |
| 401 | { |
| 402 | uint64_t time; |
| 403 | PowerState state; |
| 404 | |
| 405 | if (!deserializeStateChangeTime(time, state)) |
| 406 | { |
| 407 | ChassisInherit::lastStateChangeTime(0); |
| 408 | } |
| 409 | else |
| 410 | { |
| 411 | ChassisInherit::lastStateChangeTime(time); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | void Chassis::setStateChangeTime() |
| 416 | { |
| 417 | using namespace std::chrono; |
| 418 | uint64_t lastTime; |
| 419 | PowerState lastState; |
| 420 | |
| 421 | auto now = |
| 422 | duration_cast<milliseconds>(system_clock::now().time_since_epoch()) |
| 423 | .count(); |
| 424 | |
| 425 | // If power is on when the BMC is rebooted, this function will get called |
| 426 | // because sysStateChange() runs. Since the power state didn't change |
| 427 | // in this case, neither should the state change time, so check that |
| 428 | // the power state actually did change here. |
| 429 | if (deserializeStateChangeTime(lastTime, lastState)) |
| 430 | { |
| 431 | if (lastState == ChassisInherit::currentPowerState()) |
| 432 | { |
| 433 | return; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | ChassisInherit::lastStateChangeTime(now); |
| 438 | serializeStateChangeTime(); |
| 439 | } |
| 440 | |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 441 | bool Chassis::standbyVoltageRegulatorFault() |
| 442 | { |
| 443 | bool regulatorFault = false; |
| 444 | |
| 445 | // find standby voltage regulator fault via gpio |
| 446 | gpiod_line* line = gpiod_line_find("regulator-standby-faulted"); |
| 447 | |
| 448 | if (nullptr != line) |
| 449 | { |
| 450 | // take ownership of gpio |
| 451 | if (0 != gpiod_line_request_input(line, "chassis")) |
| 452 | { |
| 453 | error("Failed request for regulator-standby-faulted GPIO"); |
| 454 | } |
| 455 | else |
| 456 | { |
| 457 | // get gpio value |
| 458 | auto gpioval = gpiod_line_get_value(line); |
| 459 | |
| 460 | // release ownership of gpio |
| 461 | gpiod_line_close_chip(line); |
| 462 | |
| 463 | if (-1 == gpioval) |
| 464 | { |
| 465 | error("Failed reading regulator-standby-faulted GPIO"); |
| 466 | } |
| 467 | |
| 468 | if (1 == gpioval) |
| 469 | { |
| 470 | info("Detected standby voltage regulator fault"); |
| 471 | regulatorFault = true; |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | return regulatorFault; |
| 476 | } |
| 477 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 478 | } // namespace manager |
| 479 | } // namespace state |
Andrew Geissler | a965cf0 | 2018-08-31 08:37:05 -0700 | [diff] [blame] | 480 | } // namespace phosphor |