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