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