Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "chassis_state_manager.hpp" |
| 4 | |
Andrew Geissler | f8ae6a0 | 2022-01-21 17:00:20 -0600 | [diff] [blame] | 5 | #include "utils.hpp" |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 6 | #include "xyz/openbmc_project/Common/error.hpp" |
| 7 | #include "xyz/openbmc_project/State/Shutdown/Power/error.hpp" |
| 8 | |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 9 | #include <fmt/format.h> |
NodeMan97 | bcbee4a | 2022-05-27 15:28:45 -0500 | [diff] [blame] | 10 | #include <fmt/printf.h> |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 11 | |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 12 | #include <cereal/archives/json.hpp> |
| 13 | #include <phosphor-logging/elog-errors.hpp> |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 14 | #include <phosphor-logging/lg2.hpp> |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 15 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 16 | #include <sdbusplus/exception.hpp> |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 17 | #include <sdeventplus/event.hpp> |
| 18 | #include <sdeventplus/exception.hpp> |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 19 | #include <xyz/openbmc_project/State/Decorator/PowerSystemInputs/server.hpp> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 20 | |
Andrew Geissler | f2b22e8 | 2021-03-12 14:47:03 -0600 | [diff] [blame] | 21 | #include <filesystem> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 22 | #include <fstream> |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 23 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 24 | namespace phosphor |
| 25 | { |
| 26 | namespace state |
| 27 | { |
| 28 | namespace manager |
| 29 | { |
| 30 | |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 31 | PHOSPHOR_LOG2_USING; |
| 32 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 33 | // When you see server:: you know we're referencing our base class |
| 34 | namespace server = sdbusplus::xyz::openbmc_project::State::server; |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 35 | namespace decoratorServer = |
| 36 | sdbusplus::xyz::openbmc_project::State::Decorator::server; |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 37 | |
| 38 | using namespace phosphor::logging; |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 39 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Aatir Manzur | 27115ae | 2019-07-23 16:25:38 -0500 | [diff] [blame] | 40 | using sdbusplus::xyz::openbmc_project::State::Shutdown::Power::Error::Blackout; |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 41 | using sdbusplus::xyz::openbmc_project::State::Shutdown::Power::Error::Regulator; |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 42 | constexpr auto CHASSIS_STATE_POWEROFF_TGT_FMT = |
| 43 | "obmc-chassis-poweroff@{}.target"; |
| 44 | constexpr auto CHASSIS_STATE_HARD_POWEROFF_TGT_FMT = |
| 45 | "obmc-chassis-hard-poweroff@{}.target"; |
| 46 | constexpr auto CHASSIS_STATE_POWERON_TGT_FMT = "obmc-chassis-poweron@{}.target"; |
Corey_Hardesty | 7e0e4e9 | 2022-06-22 11:11:55 -0500 | [diff] [blame^] | 47 | constexpr auto CHASSIS_BLACKOUT_TGT_FMT = "obmc-chassis-blackout@{}.target"; |
Andrew Geissler | 77a9183 | 2022-05-11 12:11:03 -0400 | [diff] [blame] | 48 | constexpr auto AUTO_POWER_RESTORE_SVC_FMT = |
| 49 | "phosphor-discover-system-state@{}.service"; |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 50 | constexpr auto ACTIVE_STATE = "active"; |
| 51 | constexpr auto ACTIVATING_STATE = "activating"; |
| 52 | |
Andrew Geissler | 2cf2a26 | 2022-02-02 14:38:41 -0600 | [diff] [blame] | 53 | // Details at https://upower.freedesktop.org/docs/Device.html |
| 54 | constexpr uint TYPE_UPS = 3; |
| 55 | constexpr uint STATE_FULLY_CHARGED = 4; |
| 56 | constexpr uint BATTERY_LVL_FULL = 8; |
| 57 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 58 | constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 59 | constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1"; |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 60 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 61 | |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 62 | constexpr auto SYSTEMD_PROPERTY_IFACE = "org.freedesktop.DBus.Properties"; |
| 63 | constexpr auto SYSTEMD_INTERFACE_UNIT = "org.freedesktop.systemd1.Unit"; |
| 64 | |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 65 | constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper"; |
| 66 | constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper"; |
| 67 | constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper"; |
| 68 | constexpr auto UPOWER_INTERFACE = "org.freedesktop.UPower.Device"; |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 69 | constexpr auto POWERSYSINPUTS_INTERFACE = |
| 70 | "xyz.openbmc_project.State.Decorator.PowerSystemInputs"; |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 71 | constexpr auto PROPERTY_INTERFACE = "org.freedesktop.DBus.Properties"; |
| 72 | |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 73 | void Chassis::subscribeToSystemdSignals() |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 74 | { |
Andrew Geissler | 3a30b05 | 2019-05-14 15:54:37 -0500 | [diff] [blame] | 75 | try |
| 76 | { |
| 77 | auto method = this->bus.new_method_call( |
| 78 | SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "Subscribe"); |
Andrew Geissler | 5b95027 | 2019-05-24 12:27:51 -0500 | [diff] [blame] | 79 | this->bus.call(method); |
Andrew Geissler | 3a30b05 | 2019-05-14 15:54:37 -0500 | [diff] [blame] | 80 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 81 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | 3a30b05 | 2019-05-14 15:54:37 -0500 | [diff] [blame] | 82 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 83 | error("Failed to subscribe to systemd signals: {ERROR}", "ERROR", e); |
Andrew Geissler | 3a30b05 | 2019-05-14 15:54:37 -0500 | [diff] [blame] | 84 | elog<InternalFailure>(); |
| 85 | } |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 86 | |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 87 | return; |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 88 | } |
| 89 | |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 90 | void Chassis::createSystemdTargetTable() |
| 91 | { |
| 92 | systemdTargetTable = { |
| 93 | // Use the hard off target to ensure we shutdown immediately |
| 94 | {Transition::Off, fmt::format(CHASSIS_STATE_HARD_POWEROFF_TGT_FMT, id)}, |
| 95 | {Transition::On, fmt::format(CHASSIS_STATE_POWERON_TGT_FMT, id)}}; |
| 96 | } |
| 97 | |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 98 | // TODO - Will be rewritten once sdbusplus client bindings are in place |
| 99 | // and persistent storage design is in place and sdbusplus |
| 100 | // has read property function |
| 101 | void Chassis::determineInitialState() |
| 102 | { |
Andrew Geissler | 2cf2a26 | 2022-02-02 14:38:41 -0600 | [diff] [blame] | 103 | // Monitor for any properties changed signals on UPower device path |
| 104 | uPowerPropChangeSignal = std::make_unique<sdbusplus::bus::match_t>( |
| 105 | bus, |
| 106 | sdbusplus::bus::match::rules::propertiesChangedNamespace( |
| 107 | "/org/freedesktop/UPower", UPOWER_INTERFACE), |
| 108 | [this](auto& msg) { this->uPowerChangeEvent(msg); }); |
| 109 | |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 110 | // Monitor for any properties changed signals on PowerSystemInputs |
| 111 | powerSysInputsPropChangeSignal = std::make_unique<sdbusplus::bus::match_t>( |
| 112 | bus, |
| 113 | sdbusplus::bus::match::rules::propertiesChangedNamespace( |
| 114 | fmt::format( |
| 115 | "/xyz/openbmc_project/power/power_supplies/chassis{}/psus", id), |
| 116 | POWERSYSINPUTS_INTERFACE), |
| 117 | [this](auto& msg) { this->powerSysInputsChangeEvent(msg); }); |
| 118 | |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 119 | determineStatusOfPower(); |
| 120 | |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 121 | std::variant<int> pgood = -1; |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 122 | auto method = this->bus.new_method_call( |
| 123 | "org.openbmc.control.Power", "/org/openbmc/control/power0", |
| 124 | "org.freedesktop.DBus.Properties", "Get"); |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 125 | |
| 126 | method.append("org.openbmc.control.Power", "pgood"); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 127 | try |
| 128 | { |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 129 | auto reply = this->bus.call(method); |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 130 | reply.read(pgood); |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 131 | |
Patrick Williams | 37413dc | 2020-05-13 11:29:54 -0500 | [diff] [blame] | 132 | if (std::get<int>(pgood) == 1) |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 133 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 134 | info("Initial Chassis State will be On"); |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 135 | server::Chassis::currentPowerState(PowerState::On); |
| 136 | server::Chassis::requestedPowerTransition(Transition::On); |
| 137 | return; |
| 138 | } |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 139 | else |
| 140 | { |
| 141 | // The system is off. If we think it should be on then |
| 142 | // we probably lost AC while up, so set a new state |
| 143 | // change time. |
| 144 | uint64_t lastTime; |
| 145 | PowerState lastState; |
| 146 | |
| 147 | if (deserializeStateChangeTime(lastTime, lastState)) |
| 148 | { |
Andrew Geissler | 7ed3623 | 2022-01-26 13:49:28 -0600 | [diff] [blame] | 149 | // If power was on before the BMC reboot and the reboot reason |
| 150 | // was not a pinhole reset, log an error |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 151 | if (lastState == PowerState::On) |
| 152 | { |
Andrew Geissler | 7ed3623 | 2022-01-26 13:49:28 -0600 | [diff] [blame] | 153 | info( |
| 154 | "Chassis power was on before the BMC reboot and it is off now"); |
Matthew Barth | be6efab | 2022-03-01 13:21:45 -0600 | [diff] [blame] | 155 | |
| 156 | // Reset host sensors since system is off now |
Corey_Hardesty | 7e0e4e9 | 2022-06-22 11:11:55 -0500 | [diff] [blame^] | 157 | // Ensure Power Leds are off. |
| 158 | startUnit(fmt::format(CHASSIS_BLACKOUT_TGT_FMT, id)); |
Matthew Barth | be6efab | 2022-03-01 13:21:45 -0600 | [diff] [blame] | 159 | |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 160 | setStateChangeTime(); |
NodeMan97 | bcbee4a | 2022-05-27 15:28:45 -0500 | [diff] [blame] | 161 | // Generate file indicating AC loss occurred |
| 162 | std::string chassisLostPowerFileFmt = |
| 163 | fmt::sprintf(CHASSIS_LOST_POWER_FILE, id); |
| 164 | fs::create_directories(BASE_FILE_DIR); |
| 165 | fs::path chassisPowerLossFile{chassisLostPowerFileFmt}; |
| 166 | std::ofstream outfile(chassisPowerLossFile); |
| 167 | outfile.close(); |
Andrew Geissler | 7ed3623 | 2022-01-26 13:49:28 -0600 | [diff] [blame] | 168 | |
Brandon Wyman | 4fe860f | 2022-03-16 00:56:53 +0000 | [diff] [blame] | 169 | // 0 indicates pinhole reset. 1 is NOT pinhole reset |
Andrew Geissler | 7ed3623 | 2022-01-26 13:49:28 -0600 | [diff] [blame] | 170 | if (phosphor::state::manager::utils::getGpioValue( |
Brandon Wyman | 4fe860f | 2022-03-16 00:56:53 +0000 | [diff] [blame] | 171 | "reset-cause-pinhole") != 0) |
Andrew Geissler | 7ed3623 | 2022-01-26 13:49:28 -0600 | [diff] [blame] | 172 | { |
| 173 | if (standbyVoltageRegulatorFault()) |
| 174 | { |
| 175 | report<Regulator>(); |
| 176 | } |
| 177 | else |
| 178 | { |
Mike Capps | 073fa2b | 2022-03-15 16:06:45 -0400 | [diff] [blame] | 179 | report<Blackout>(Entry::Level::Critical); |
Andrew Geissler | 7ed3623 | 2022-01-26 13:49:28 -0600 | [diff] [blame] | 180 | } |
| 181 | } |
Brandon Wyman | 4fe860f | 2022-03-16 00:56:53 +0000 | [diff] [blame] | 182 | else |
| 183 | { |
| 184 | info("Pinhole reset"); |
| 185 | } |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | } |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 189 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 190 | catch (const sdbusplus::exception_t& e) |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 191 | { |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 192 | // It's acceptable for the pgood state service to not be available |
| 193 | // since it will notify us of the pgood state when it comes up. |
| 194 | if (e.name() != nullptr && |
| 195 | strcmp("org.freedesktop.DBus.Error.ServiceUnknown", e.name()) == 0) |
| 196 | { |
| 197 | goto fail; |
| 198 | } |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 199 | |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 200 | // Only log for unexpected error types. |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 201 | error("Error performing call to get pgood: {ERROR}", "ERROR", e); |
William A. Kennington III | 9a2f37c | 2018-06-28 16:18:37 -0700 | [diff] [blame] | 202 | goto fail; |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 203 | } |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 204 | |
| 205 | fail: |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 206 | info("Initial Chassis State will be Off"); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 207 | server::Chassis::currentPowerState(PowerState::Off); |
| 208 | server::Chassis::requestedPowerTransition(Transition::Off); |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 209 | |
| 210 | return; |
| 211 | } |
| 212 | |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 213 | void Chassis::determineStatusOfPower() |
| 214 | { |
Andrew Geissler | 77a9183 | 2022-05-11 12:11:03 -0400 | [diff] [blame] | 215 | auto initialPowerStatus = server::Chassis::currentPowerStatus(); |
| 216 | |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 217 | bool powerGood = determineStatusOfUPSPower(); |
| 218 | if (!powerGood) |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 219 | { |
| 220 | return; |
| 221 | } |
| 222 | |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 223 | powerGood = determineStatusOfPSUPower(); |
| 224 | if (powerGood) |
| 225 | { |
| 226 | // All checks passed, set power status to good |
| 227 | server::Chassis::currentPowerStatus(PowerStatus::Good); |
Andrew Geissler | 77a9183 | 2022-05-11 12:11:03 -0400 | [diff] [blame] | 228 | |
| 229 | // If power status transitioned from bad to good and chassis power is |
| 230 | // off then call Auto Power Restart to see if the system should auto |
| 231 | // power on now that power status is good |
| 232 | if ((initialPowerStatus != PowerStatus::Good) && |
| 233 | (server::Chassis::currentPowerState() == PowerState::Off)) |
| 234 | { |
| 235 | info("power status transitioned from {START_PWR_STATE} to Good and " |
| 236 | "chassis power is off, calling APR", |
| 237 | "START_PWR_STATE", initialPowerStatus); |
| 238 | restartUnit(fmt::format(AUTO_POWER_RESTORE_SVC_FMT, this->id)); |
| 239 | } |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 240 | } |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 243 | bool Chassis::determineStatusOfUPSPower() |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 244 | { |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 245 | // Find all implementations of the UPower interface |
| 246 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 247 | MAPPER_INTERFACE, "GetSubTree"); |
| 248 | |
| 249 | mapper.append("/", 0, std::vector<std::string>({UPOWER_INTERFACE})); |
| 250 | |
| 251 | std::map<std::string, std::map<std::string, std::vector<std::string>>> |
| 252 | mapperResponse; |
| 253 | |
| 254 | try |
| 255 | { |
| 256 | auto mapperResponseMsg = bus.call(mapper); |
| 257 | mapperResponseMsg.read(mapperResponse); |
| 258 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 259 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 260 | { |
| 261 | error("Error in mapper GetSubTree call for UPS: {ERROR}", "ERROR", e); |
| 262 | throw; |
| 263 | } |
| 264 | |
| 265 | if (mapperResponse.empty()) |
| 266 | { |
| 267 | debug("No UPower devices found in system"); |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // Iterate through all returned Upower interfaces and look for UPS's |
| 271 | for (const auto& [path, services] : mapperResponse) |
| 272 | { |
| 273 | for (const auto& serviceIter : services) |
| 274 | { |
| 275 | const std::string& service = serviceIter.first; |
| 276 | |
| 277 | try |
| 278 | { |
| 279 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 280 | PROPERTY_INTERFACE, "GetAll"); |
| 281 | method.append(UPOWER_INTERFACE); |
| 282 | |
| 283 | auto response = bus.call(method); |
| 284 | using Property = std::string; |
| 285 | using Value = std::variant<bool, uint>; |
| 286 | using PropertyMap = std::map<Property, Value>; |
| 287 | PropertyMap properties; |
| 288 | response.read(properties); |
| 289 | |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 290 | if (std::get<uint>(properties["Type"]) != TYPE_UPS) |
| 291 | { |
| 292 | info("UPower device {OBJ_PATH} is not a UPS device", |
| 293 | "OBJ_PATH", path); |
| 294 | continue; |
| 295 | } |
| 296 | |
Andrew Geissler | 2cf2a26 | 2022-02-02 14:38:41 -0600 | [diff] [blame] | 297 | if (std::get<bool>(properties["IsPresent"]) != true) |
| 298 | { |
| 299 | // There is a UPS detected but it is not officially |
| 300 | // "present" yet. Monitor it for state change. |
| 301 | info("UPower device {OBJ_PATH} is not present", "OBJ_PATH", |
| 302 | path); |
| 303 | continue; |
| 304 | } |
| 305 | |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 306 | if (std::get<uint>(properties["State"]) == STATE_FULLY_CHARGED) |
| 307 | { |
| 308 | info("UPS is fully charged"); |
| 309 | } |
| 310 | else |
| 311 | { |
| 312 | info("UPS is not fully charged: {UPS_STATE}", "UPS_STATE", |
| 313 | std::get<uint>(properties["State"])); |
| 314 | server::Chassis::currentPowerStatus( |
| 315 | PowerStatus::UninterruptiblePowerSupply); |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 316 | return false; |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | if (std::get<uint>(properties["BatteryLevel"]) == |
| 320 | BATTERY_LVL_FULL) |
| 321 | { |
| 322 | info("UPS Battery Level is Full"); |
Andrew Geissler | 2cf2a26 | 2022-02-02 14:38:41 -0600 | [diff] [blame] | 323 | // Only one UPS per system, we've found it and it's all |
| 324 | // good so exit function |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 325 | return true; |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 326 | } |
| 327 | else |
| 328 | { |
| 329 | info("UPS Battery Level is Low: {UPS_BAT_LEVEL}", |
| 330 | "UPS_BAT_LEVEL", |
| 331 | std::get<uint>(properties["BatteryLevel"])); |
| 332 | server::Chassis::currentPowerStatus( |
| 333 | PowerStatus::UninterruptiblePowerSupply); |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 334 | return false; |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 335 | } |
| 336 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 337 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 338 | { |
| 339 | error("Error reading UPS property, error: {ERROR}, " |
| 340 | "service: {SERVICE} path: {PATH}", |
| 341 | "ERROR", e, "SERVICE", service, "PATH", path); |
| 342 | throw; |
| 343 | } |
| 344 | } |
| 345 | } |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 346 | return true; |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 347 | } |
| 348 | |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 349 | bool Chassis::determineStatusOfPSUPower() |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 350 | { |
| 351 | // Find all implementations of the PowerSystemInputs interface |
| 352 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 353 | MAPPER_INTERFACE, "GetSubTree"); |
| 354 | |
| 355 | mapper.append("/", 0, std::vector<std::string>({POWERSYSINPUTS_INTERFACE})); |
| 356 | |
| 357 | std::map<std::string, std::map<std::string, std::vector<std::string>>> |
| 358 | mapperResponse; |
| 359 | |
| 360 | try |
| 361 | { |
| 362 | auto mapperResponseMsg = bus.call(mapper); |
| 363 | mapperResponseMsg.read(mapperResponse); |
| 364 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 365 | catch (const sdbusplus::exception_t& e) |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 366 | { |
| 367 | error("Error in mapper GetSubTree call for PowerSystemInputs: {ERROR}", |
| 368 | "ERROR", e); |
| 369 | throw; |
| 370 | } |
| 371 | |
| 372 | for (const auto& [path, services] : mapperResponse) |
| 373 | { |
| 374 | for (const auto& serviceIter : services) |
| 375 | { |
| 376 | const std::string& service = serviceIter.first; |
| 377 | |
| 378 | try |
| 379 | { |
| 380 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 381 | PROPERTY_INTERFACE, "GetAll"); |
| 382 | method.append(POWERSYSINPUTS_INTERFACE); |
| 383 | |
| 384 | auto response = bus.call(method); |
| 385 | using Property = std::string; |
| 386 | using Value = std::variant<std::string>; |
| 387 | using PropertyMap = std::map<Property, Value>; |
| 388 | PropertyMap properties; |
| 389 | response.read(properties); |
| 390 | |
| 391 | auto statusStr = std::get<std::string>(properties["Status"]); |
| 392 | auto status = |
| 393 | decoratorServer::PowerSystemInputs::convertStatusFromString( |
| 394 | statusStr); |
| 395 | |
| 396 | if (status == decoratorServer::PowerSystemInputs::Status::Fault) |
| 397 | { |
| 398 | info("Power System Inputs status is in Fault state"); |
| 399 | server::Chassis::currentPowerStatus(PowerStatus::BrownOut); |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 400 | return false; |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 403 | catch (const sdbusplus::exception_t& e) |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 404 | { |
| 405 | error( |
| 406 | "Error reading Power System Inputs property, error: {ERROR}, " |
| 407 | "service: {SERVICE} path: {PATH}", |
| 408 | "ERROR", e, "SERVICE", service, "PATH", path); |
| 409 | throw; |
| 410 | } |
| 411 | } |
| 412 | } |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 413 | return true; |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 416 | void Chassis::uPowerChangeEvent(sdbusplus::message_t& msg) |
Andrew Geissler | 2cf2a26 | 2022-02-02 14:38:41 -0600 | [diff] [blame] | 417 | { |
| 418 | debug("UPS Property Change Event Triggered"); |
| 419 | std::string statusInterface; |
| 420 | std::map<std::string, std::variant<uint, bool>> msgData; |
| 421 | msg.read(statusInterface, msgData); |
| 422 | |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 423 | // If the change is to any of the properties we are interested in, then call |
| 424 | // determineStatusOfPower(), which looks at all the power-related |
| 425 | // interfaces, to see if a power status change is needed |
Andrew Geissler | 2cf2a26 | 2022-02-02 14:38:41 -0600 | [diff] [blame] | 426 | auto propertyMap = msgData.find("IsPresent"); |
| 427 | if (propertyMap != msgData.end()) |
| 428 | { |
| 429 | info("UPS presence changed to {UPS_PRES_INFO}", "UPS_PRES_INFO", |
| 430 | std::get<bool>(propertyMap->second)); |
| 431 | determineStatusOfPower(); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | propertyMap = msgData.find("State"); |
| 436 | if (propertyMap != msgData.end()) |
| 437 | { |
| 438 | info("UPS State changed to {UPS_STATE}", "UPS_STATE", |
| 439 | std::get<uint>(propertyMap->second)); |
| 440 | determineStatusOfPower(); |
| 441 | return; |
| 442 | } |
| 443 | |
| 444 | propertyMap = msgData.find("BatteryLevel"); |
| 445 | if (propertyMap != msgData.end()) |
| 446 | { |
| 447 | info("UPS BatteryLevel changed to {UPS_BAT_LEVEL}", "UPS_BAT_LEVEL", |
| 448 | std::get<uint>(propertyMap->second)); |
| 449 | determineStatusOfPower(); |
| 450 | return; |
| 451 | } |
| 452 | return; |
| 453 | } |
| 454 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 455 | void Chassis::powerSysInputsChangeEvent(sdbusplus::message_t& msg) |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 456 | { |
| 457 | debug("Power System Inputs Property Change Event Triggered"); |
| 458 | std::string statusInterface; |
| 459 | std::map<std::string, std::variant<std::string>> msgData; |
| 460 | msg.read(statusInterface, msgData); |
| 461 | |
| 462 | // If the change is to any of the properties we are interested in, then call |
| 463 | // determineStatusOfPower(), which looks at all the power-related |
| 464 | // interfaces, to see if a power status change is needed |
| 465 | auto propertyMap = msgData.find("Status"); |
| 466 | if (propertyMap != msgData.end()) |
| 467 | { |
| 468 | info("Power System Inputs status changed to {POWER_SYS_INPUT_STATUS}", |
| 469 | "POWER_SYS_INPUT_STATUS", |
| 470 | std::get<std::string>(propertyMap->second)); |
| 471 | determineStatusOfPower(); |
| 472 | return; |
| 473 | } |
| 474 | return; |
| 475 | } |
| 476 | |
Matthew Barth | be6efab | 2022-03-01 13:21:45 -0600 | [diff] [blame] | 477 | void Chassis::startUnit(const std::string& sysdUnit) |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 478 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 479 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 480 | SYSTEMD_INTERFACE, "StartUnit"); |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 481 | |
Matthew Barth | be6efab | 2022-03-01 13:21:45 -0600 | [diff] [blame] | 482 | method.append(sysdUnit); |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 483 | method.append("replace"); |
| 484 | |
| 485 | this->bus.call_noreply(method); |
| 486 | |
| 487 | return; |
| 488 | } |
| 489 | |
Andrew Geissler | 77a9183 | 2022-05-11 12:11:03 -0400 | [diff] [blame] | 490 | void Chassis::restartUnit(const std::string& sysdUnit) |
| 491 | { |
| 492 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 493 | SYSTEMD_INTERFACE, "RestartUnit"); |
| 494 | |
| 495 | method.append(sysdUnit); |
| 496 | method.append("replace"); |
| 497 | |
| 498 | this->bus.call_noreply(method); |
| 499 | |
| 500 | return; |
| 501 | } |
| 502 | |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 503 | bool Chassis::stateActive(const std::string& target) |
| 504 | { |
Patrick Williams | 2975e26 | 2020-05-13 18:01:09 -0500 | [diff] [blame] | 505 | std::variant<std::string> currentState; |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 506 | sdbusplus::message::object_path unitTargetPath; |
| 507 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 508 | auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, |
| 509 | SYSTEMD_INTERFACE, "GetUnit"); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 510 | |
| 511 | method.append(target); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 512 | |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 513 | try |
| 514 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 515 | auto result = this->bus.call(method); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 516 | result.read(unitTargetPath); |
| 517 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 518 | catch (const sdbusplus::exception_t& e) |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 519 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 520 | error("Error in GetUnit call: {ERROR}", "ERROR", e); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 521 | return false; |
| 522 | } |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 523 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 524 | method = this->bus.new_method_call( |
| 525 | SYSTEMD_SERVICE, |
| 526 | static_cast<const std::string&>(unitTargetPath).c_str(), |
| 527 | SYSTEMD_PROPERTY_IFACE, "Get"); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 528 | |
| 529 | method.append(SYSTEMD_INTERFACE_UNIT, "ActiveState"); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 530 | |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 531 | try |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 532 | { |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 533 | auto result = this->bus.call(method); |
| 534 | result.read(currentState); |
| 535 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 536 | catch (const sdbusplus::exception_t& e) |
Anthony Wilson | 32c532e | 2018-10-25 21:56:07 -0500 | [diff] [blame] | 537 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 538 | error("Error in ActiveState Get: {ERROR}", "ERROR", e); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 539 | return false; |
| 540 | } |
| 541 | |
Patrick Williams | 37413dc | 2020-05-13 11:29:54 -0500 | [diff] [blame] | 542 | const auto& currentStateStr = std::get<std::string>(currentState); |
William A. Kennington III | 7a0689a | 2018-11-12 17:19:33 -0800 | [diff] [blame] | 543 | return currentStateStr == ACTIVE_STATE || |
| 544 | currentStateStr == ACTIVATING_STATE; |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 545 | } |
| 546 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 547 | int Chassis::sysStateChange(sdbusplus::message_t& msg) |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 548 | { |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 549 | sdbusplus::message::object_path newStateObjPath; |
| 550 | std::string newStateUnit{}; |
| 551 | std::string newStateResult{}; |
| 552 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 553 | // Read the msg and populate each variable |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 554 | try |
| 555 | { |
Andrew Geissler | 9b8af4f | 2019-09-12 14:19:14 -0500 | [diff] [blame] | 556 | // newStateID is a throwaway that is needed in order to read the |
| 557 | // parameters that are useful out of the dbus message |
| 558 | uint32_t newStateID{}; |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 559 | msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
| 560 | } |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 561 | catch (const sdbusplus::exception_t& e) |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 562 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 563 | error("Error in state change - bad encoding: {ERROR} {REPLY_SIG}", |
| 564 | "ERROR", e, "REPLY_SIG", msg.get_signature()); |
William A. Kennington III | 09568ff | 2018-05-11 00:03:12 -0700 | [diff] [blame] | 565 | return 0; |
| 566 | } |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 567 | |
Allen.Wang | c089562 | 2022-03-23 15:46:47 +0800 | [diff] [blame] | 568 | if ((newStateUnit == fmt::format(CHASSIS_STATE_POWEROFF_TGT_FMT, id)) && |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 569 | (newStateResult == "done") && |
| 570 | (!stateActive(systemdTargetTable[Transition::On]))) |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 571 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 572 | info("Received signal that power OFF is complete"); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 573 | this->currentPowerState(server::Chassis::PowerState::Off); |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 574 | this->setStateChangeTime(); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 575 | } |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 576 | else if ((newStateUnit == systemdTargetTable[Transition::On]) && |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 577 | (newStateResult == "done") && |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 578 | (stateActive(systemdTargetTable[Transition::On]))) |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 579 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 580 | info("Received signal that power ON is complete"); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 581 | this->currentPowerState(server::Chassis::PowerState::On); |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 582 | this->setStateChangeTime(); |
Andrew Geissler | f2b22e8 | 2021-03-12 14:47:03 -0600 | [diff] [blame] | 583 | |
| 584 | // Remove temporary file which is utilized for scenarios where the |
| 585 | // BMC is rebooted while the chassis power is still on. |
| 586 | // This file is used to indicate to chassis related systemd services |
| 587 | // that the chassis is already on and they should skip running. |
| 588 | // Once the chassis state is back to on we can clear this file. |
| 589 | auto size = std::snprintf(nullptr, 0, CHASSIS_ON_FILE, 0); |
| 590 | size++; // null |
| 591 | std::unique_ptr<char[]> chassisFile(new char[size]); |
| 592 | std::snprintf(chassisFile.get(), size, CHASSIS_ON_FILE, 0); |
| 593 | if (std::filesystem::exists(chassisFile.get())) |
| 594 | { |
| 595 | std::filesystem::remove(chassisFile.get()); |
| 596 | } |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 597 | } |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 602 | Chassis::Transition Chassis::requestedPowerTransition(Transition value) |
| 603 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 604 | info("Change to Chassis Requested Power State: {REQ_POWER_TRAN}", |
| 605 | "REQ_POWER_TRAN", value); |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 606 | startUnit(systemdTargetTable.find(value)->second); |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 607 | return server::Chassis::requestedPowerTransition(value); |
| 608 | } |
| 609 | |
| 610 | Chassis::PowerState Chassis::currentPowerState(PowerState value) |
| 611 | { |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 612 | PowerState chassisPowerState; |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 613 | info("Change to Chassis Power State: {CUR_POWER_STATE}", "CUR_POWER_STATE", |
| 614 | value); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 615 | |
| 616 | chassisPowerState = server::Chassis::currentPowerState(value); |
shamim ali | 6d582a8 | 2022-03-15 18:19:32 +0530 | [diff] [blame] | 617 | if (chassisPowerState == PowerState::On) |
| 618 | { |
| 619 | pohTimer.resetRemaining(); |
| 620 | } |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 621 | return chassisPowerState; |
| 622 | } |
| 623 | |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 624 | uint32_t Chassis::pohCounter(uint32_t value) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 625 | { |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 626 | if (value != pohCounter()) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 627 | { |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 628 | ChassisInherit::pohCounter(value); |
Matt Spinler | 8195784 | 2018-07-11 10:37:12 -0500 | [diff] [blame] | 629 | serializePOH(); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 630 | } |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 631 | return pohCounter(); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 632 | } |
| 633 | |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 634 | void Chassis::pohCallback() |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 635 | { |
| 636 | if (ChassisInherit::currentPowerState() == PowerState::On) |
| 637 | { |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 638 | pohCounter(pohCounter() + 1); |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 642 | void Chassis::restorePOHCounter() |
| 643 | { |
| 644 | uint32_t counter; |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 645 | if (!deserializePOH(counter)) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 646 | { |
| 647 | // set to default value |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 648 | pohCounter(0); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 649 | } |
| 650 | else |
| 651 | { |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 652 | pohCounter(counter); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 656 | fs::path Chassis::serializePOH() |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 657 | { |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 658 | fs::path path{fmt::format(POH_COUNTER_PERSIST_PATH, id)}; |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 659 | std::ofstream os(path.c_str(), std::ios::binary); |
| 660 | cereal::JSONOutputArchive oarchive(os); |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 661 | oarchive(pohCounter()); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 662 | return path; |
| 663 | } |
| 664 | |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 665 | bool Chassis::deserializePOH(uint32_t& pohCounter) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 666 | { |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 667 | fs::path path{fmt::format(POH_COUNTER_PERSIST_PATH, id)}; |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 668 | try |
| 669 | { |
| 670 | if (fs::exists(path)) |
| 671 | { |
| 672 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 673 | cereal::JSONInputArchive iarchive(is); |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 674 | iarchive(pohCounter); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 675 | return true; |
| 676 | } |
| 677 | return false; |
| 678 | } |
Patrick Williams | 8583b3b | 2021-10-06 12:19:20 -0500 | [diff] [blame] | 679 | catch (const cereal::Exception& e) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 680 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 681 | error("deserialize exception: {ERROR}", "ERROR", e); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 682 | fs::remove(path); |
| 683 | return false; |
| 684 | } |
| 685 | catch (const fs::filesystem_error& e) |
| 686 | { |
| 687 | return false; |
| 688 | } |
| 689 | |
| 690 | return false; |
| 691 | } |
| 692 | |
| 693 | void Chassis::startPOHCounter() |
| 694 | { |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 695 | auto dir = fs::path(POH_COUNTER_PERSIST_PATH).parent_path(); |
| 696 | fs::create_directories(dir); |
| 697 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 698 | try |
| 699 | { |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 700 | auto event = sdeventplus::Event::get_default(); |
| 701 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 702 | event.loop(); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 703 | } |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 704 | catch (const sdeventplus::SdEventError& e) |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 705 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 706 | error("Error occurred during the sdeventplus loop: {ERROR}", "ERROR", |
| 707 | e); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 708 | phosphor::logging::commit<InternalFailure>(); |
| 709 | } |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 710 | } |
| 711 | |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 712 | void Chassis::serializeStateChangeTime() |
| 713 | { |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 714 | fs::path path{fmt::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, id)}; |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 715 | std::ofstream os(path.c_str(), std::ios::binary); |
| 716 | cereal::JSONOutputArchive oarchive(os); |
| 717 | |
| 718 | oarchive(ChassisInherit::lastStateChangeTime(), |
| 719 | ChassisInherit::currentPowerState()); |
| 720 | } |
| 721 | |
| 722 | bool Chassis::deserializeStateChangeTime(uint64_t& time, PowerState& state) |
| 723 | { |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 724 | fs::path path{fmt::format(CHASSIS_STATE_CHANGE_PERSIST_PATH, id)}; |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 725 | |
| 726 | try |
| 727 | { |
| 728 | if (fs::exists(path)) |
| 729 | { |
| 730 | std::ifstream is(path.c_str(), std::ios::in | std::ios::binary); |
| 731 | cereal::JSONInputArchive iarchive(is); |
| 732 | iarchive(time, state); |
| 733 | return true; |
| 734 | } |
| 735 | } |
Patrick Williams | 8583b3b | 2021-10-06 12:19:20 -0500 | [diff] [blame] | 736 | catch (const std::exception& e) |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 737 | { |
Andrew Geissler | 8ffdb26 | 2021-09-20 15:25:19 -0500 | [diff] [blame] | 738 | error("deserialize exception: {ERROR}", "ERROR", e); |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 739 | fs::remove(path); |
| 740 | } |
| 741 | |
| 742 | return false; |
| 743 | } |
| 744 | |
| 745 | void Chassis::restoreChassisStateChangeTime() |
| 746 | { |
| 747 | uint64_t time; |
| 748 | PowerState state; |
| 749 | |
| 750 | if (!deserializeStateChangeTime(time, state)) |
| 751 | { |
| 752 | ChassisInherit::lastStateChangeTime(0); |
| 753 | } |
| 754 | else |
| 755 | { |
| 756 | ChassisInherit::lastStateChangeTime(time); |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | void Chassis::setStateChangeTime() |
| 761 | { |
| 762 | using namespace std::chrono; |
| 763 | uint64_t lastTime; |
| 764 | PowerState lastState; |
| 765 | |
| 766 | auto now = |
| 767 | duration_cast<milliseconds>(system_clock::now().time_since_epoch()) |
| 768 | .count(); |
| 769 | |
| 770 | // If power is on when the BMC is rebooted, this function will get called |
| 771 | // because sysStateChange() runs. Since the power state didn't change |
| 772 | // in this case, neither should the state change time, so check that |
| 773 | // the power state actually did change here. |
| 774 | if (deserializeStateChangeTime(lastTime, lastState)) |
| 775 | { |
| 776 | if (lastState == ChassisInherit::currentPowerState()) |
| 777 | { |
| 778 | return; |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | ChassisInherit::lastStateChangeTime(now); |
| 783 | serializeStateChangeTime(); |
| 784 | } |
| 785 | |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 786 | bool Chassis::standbyVoltageRegulatorFault() |
| 787 | { |
| 788 | bool regulatorFault = false; |
| 789 | |
Andrew Geissler | f8ae6a0 | 2022-01-21 17:00:20 -0600 | [diff] [blame] | 790 | // find standby voltage regulator fault via gpiog |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 791 | |
Andrew Geissler | f8ae6a0 | 2022-01-21 17:00:20 -0600 | [diff] [blame] | 792 | auto gpioval = utils::getGpioValue("regulator-standby-faulted"); |
| 793 | |
| 794 | if (-1 == gpioval) |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 795 | { |
Andrew Geissler | f8ae6a0 | 2022-01-21 17:00:20 -0600 | [diff] [blame] | 796 | error("Failed reading regulator-standby-faulted GPIO"); |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 797 | } |
Andrew Geissler | f8ae6a0 | 2022-01-21 17:00:20 -0600 | [diff] [blame] | 798 | |
| 799 | if (1 == gpioval) |
| 800 | { |
| 801 | info("Detected standby voltage regulator fault"); |
| 802 | regulatorFault = true; |
| 803 | } |
| 804 | |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 805 | return regulatorFault; |
| 806 | } |
| 807 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 808 | } // namespace manager |
| 809 | } // namespace state |
Andrew Geissler | a965cf0 | 2018-08-31 08:37:05 -0700 | [diff] [blame] | 810 | } // namespace phosphor |