Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 3 | #include "config.h" |
| 4 | |
Andrew Geissler | 928bbf1 | 2023-02-14 13:30:14 -0600 | [diff] [blame] | 5 | #include "utils.hpp" |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 6 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 7 | #include <cereal/cereal.hpp> |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 8 | #include <sdbusplus/bus.hpp> |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 9 | #include <sdeventplus/clock.hpp> |
| 10 | #include <sdeventplus/event.hpp> |
| 11 | #include <sdeventplus/utility/timer.hpp> |
Patrick Williams | 9a286db | 2024-01-17 06:29:47 -0600 | [diff] [blame] | 12 | #include <xyz/openbmc_project/State/Chassis/server.hpp> |
| 13 | #include <xyz/openbmc_project/State/PowerOnHours/server.hpp> |
Andrew Geissler | e426b58 | 2020-05-28 12:40:55 -0500 | [diff] [blame] | 14 | |
| 15 | #include <chrono> |
Patrick Williams | 6ed41ea | 2022-04-05 15:50:21 -0500 | [diff] [blame] | 16 | #include <filesystem> |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 17 | |
| 18 | namespace phosphor |
| 19 | { |
| 20 | namespace state |
| 21 | { |
| 22 | namespace manager |
| 23 | { |
| 24 | |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 25 | using ChassisInherit = sdbusplus::server::object_t< |
Patrick Williams | 7e969cb | 2023-08-23 16:24:23 -0500 | [diff] [blame] | 26 | sdbusplus::server::xyz::openbmc_project::state::Chassis, |
| 27 | sdbusplus::server::xyz::openbmc_project::state::PowerOnHours>; |
Patrick Williams | 8f8ba39 | 2017-05-05 15:47:39 -0500 | [diff] [blame] | 28 | namespace sdbusRule = sdbusplus::bus::match::rules; |
Patrick Williams | 6ed41ea | 2022-04-05 15:50:21 -0500 | [diff] [blame] | 29 | namespace fs = std::filesystem; |
Patrick Williams | 8f8ba39 | 2017-05-05 15:47:39 -0500 | [diff] [blame] | 30 | |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 31 | /** @class Chassis |
| 32 | * @brief OpenBMC chassis state management implementation. |
| 33 | * @details A concrete implementation for xyz.openbmc_project.State.Chassis |
| 34 | * DBus API. |
| 35 | */ |
Patrick Williams | 8f8ba39 | 2017-05-05 15:47:39 -0500 | [diff] [blame] | 36 | class Chassis : public ChassisInherit |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 37 | { |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 38 | public: |
| 39 | /** @brief Constructs Chassis State Manager |
| 40 | * |
| 41 | * @note This constructor passes 'true' to the base class in order to |
| 42 | * defer dbus object registration until we can run |
| 43 | * determineInitialState() and set our properties |
| 44 | * |
| 45 | * @param[in] bus - The Dbus bus object |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 46 | * @param[in] objPath - The Dbus object path |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 47 | * @param[in] id - Chassis id |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 48 | */ |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 49 | Chassis(sdbusplus::bus_t& bus, const char* objPath, size_t id) : |
Patrick Williams | 7607074 | 2022-04-05 15:20:12 -0500 | [diff] [blame] | 50 | ChassisInherit(bus, objPath, ChassisInherit::action::defer_emit), |
| 51 | bus(bus), |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 52 | systemdSignals( |
| 53 | bus, |
| 54 | sdbusRule::type::signal() + sdbusRule::member("JobRemoved") + |
| 55 | sdbusRule::path("/org/freedesktop/systemd1") + |
| 56 | sdbusRule::interface("org.freedesktop.systemd1.Manager"), |
William A. Kennington III | bd28f02 | 2022-11-22 17:11:49 -0800 | [diff] [blame] | 57 | [this](sdbusplus::message_t& m) { sysStateChange(m); }), |
| 58 | id(id), |
| 59 | pohTimer( |
| 60 | sdeventplus::Event::get_default(), [this](auto&) { pohCallback(); }, |
| 61 | std::chrono::hours{1}, std::chrono::minutes{1}) |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 62 | { |
Andrew Geissler | 928bbf1 | 2023-02-14 13:30:14 -0600 | [diff] [blame] | 63 | utils::subscribeToSystemdSignals(bus); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 64 | |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 65 | createSystemdTargetTable(); |
| 66 | |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 67 | restoreChassisStateChangeTime(); |
| 68 | |
Andrew Geissler | 77a9183 | 2022-05-11 12:11:03 -0400 | [diff] [blame] | 69 | // No default in PDI so start at Good, skip D-Bus signal for now |
| 70 | currentPowerStatus(PowerStatus::Good, true); |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 71 | determineInitialState(); |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 72 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 73 | restorePOHCounter(); // restore POHCounter from persisted file |
| 74 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 75 | // We deferred this until we could get our property correct |
| 76 | this->emit_object_added(); |
| 77 | } |
Andrew Geissler | dff50ed | 2016-12-13 20:39:04 -0600 | [diff] [blame] | 78 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 79 | /** @brief Set value of RequestedPowerTransition */ |
| 80 | Transition requestedPowerTransition(Transition value) override; |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 81 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 82 | /** @brief Set value of CurrentPowerState */ |
| 83 | PowerState currentPowerState(PowerState value) override; |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 84 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 85 | /** @brief Get value of POHCounter */ |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 86 | using ChassisInherit::pohCounter; |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 87 | |
| 88 | /** @brief Increment POHCounter if Chassis Power state is ON */ |
| 89 | void startPOHCounter(); |
| 90 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 91 | private: |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 92 | /** @brief Create systemd target instance names and mapping table */ |
| 93 | void createSystemdTargetTable(); |
| 94 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 95 | /** @brief Determine initial chassis state and set internally */ |
| 96 | void determineInitialState(); |
Andrew Geissler | 0029a5d | 2017-01-24 14:48:35 -0600 | [diff] [blame] | 97 | |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 98 | /** @brief Determine status of power into system by examining all the |
| 99 | * power-related interfaces of interest |
| 100 | */ |
Andrew Geissler | 8b1f862 | 2022-01-28 16:37:07 -0600 | [diff] [blame] | 101 | void determineStatusOfPower(); |
| 102 | |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 103 | /** @brief Determine status of power provided by an Uninterruptible Power |
| 104 | * Supply into the system |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 105 | * |
| 106 | * @return True if UPS power is good, false otherwise |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 107 | */ |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 108 | bool determineStatusOfUPSPower(); |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 109 | |
| 110 | /** @brief Determine status of power provided by the power supply units into |
| 111 | * the system |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 112 | * |
| 113 | * @return True if PSU power is good, false otherwise |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 114 | */ |
Andrew Geissler | 66aacdc | 2022-05-11 08:31:47 -0400 | [diff] [blame] | 115 | bool determineStatusOfPSUPower(); |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 116 | |
Matthew Barth | be6efab | 2022-03-01 13:21:45 -0600 | [diff] [blame] | 117 | /** @brief Start the systemd unit requested |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 118 | * |
Matthew Barth | be6efab | 2022-03-01 13:21:45 -0600 | [diff] [blame] | 119 | * This function calls `StartUnit` on the systemd unit given. |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 120 | * |
Matthew Barth | be6efab | 2022-03-01 13:21:45 -0600 | [diff] [blame] | 121 | * @param[in] sysdUnit - Systemd unit |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 122 | */ |
Matthew Barth | be6efab | 2022-03-01 13:21:45 -0600 | [diff] [blame] | 123 | void startUnit(const std::string& sysdUnit); |
Andrew Geissler | ce80f24 | 2017-01-24 13:25:33 -0600 | [diff] [blame] | 124 | |
Andrew Geissler | 77a9183 | 2022-05-11 12:11:03 -0400 | [diff] [blame] | 125 | /** @brief Restart the systemd unit requested |
| 126 | * |
| 127 | * This function calls `RestartUnit` on the systemd unit given. |
| 128 | * This is useful when needing to restart a service that is already running |
| 129 | * |
| 130 | * @param[in] sysdUnit - Systemd unit to restart |
| 131 | */ |
| 132 | void restartUnit(const std::string& sysdUnit); |
| 133 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 134 | /** |
| 135 | * @brief Determine if target is active |
| 136 | * |
| 137 | * This function determines if the target is active and |
| 138 | * helps prevent misleading log recorded states. |
| 139 | * |
| 140 | * @param[in] target - Target string to check on |
| 141 | * |
| 142 | * @return boolean corresponding to state active |
| 143 | **/ |
| 144 | bool stateActive(const std::string& target); |
Josh D. King | 697474c | 2017-03-02 11:15:55 -0600 | [diff] [blame] | 145 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 146 | /** @brief Check if systemd state change is relevant to this object |
| 147 | * |
| 148 | * Instance specific interface to handle the detected systemd state |
| 149 | * change |
| 150 | * |
| 151 | * @param[in] msg - Data associated with subscribed signal |
| 152 | * |
| 153 | */ |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 154 | int sysStateChange(sdbusplus::message_t& msg); |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 155 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 156 | /** @brief Persistent sdbusplus DBus connection. */ |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 157 | sdbusplus::bus_t& bus; |
Andrew Geissler | 2ec3a7e | 2016-12-13 22:01:28 -0600 | [diff] [blame] | 158 | |
Andrew Geissler | 58a1801 | 2018-01-19 19:36:05 -0800 | [diff] [blame] | 159 | /** @brief Used to subscribe to dbus systemd signals **/ |
| 160 | sdbusplus::bus::match_t systemdSignals; |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 161 | |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 162 | /** @brief Watch for any changes to UPS properties **/ |
Andrew Geissler | 2cf2a26 | 2022-02-02 14:38:41 -0600 | [diff] [blame] | 163 | std::unique_ptr<sdbusplus::bus::match_t> uPowerPropChangeSignal; |
| 164 | |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 165 | /** @brief Watch for any changes to PowerSystemInputs properties **/ |
| 166 | std::unique_ptr<sdbusplus::bus::match_t> powerSysInputsPropChangeSignal; |
| 167 | |
Potin Lai | 70f36d8 | 2022-03-15 10:25:39 +0800 | [diff] [blame] | 168 | /** @brief Chassis id. **/ |
| 169 | const size_t id = 0; |
| 170 | |
| 171 | /** @brief Transition state to systemd target mapping table. **/ |
| 172 | std::map<Transition, std::string> systemdTargetTable; |
| 173 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 174 | /** @brief Used to Set value of POHCounter */ |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 175 | uint32_t pohCounter(uint32_t value) override; |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 176 | |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 177 | /** @brief Used by the timer to update the POHCounter */ |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 178 | void pohCallback(); |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 179 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 180 | /** @brief Used to restore POHCounter value from persisted file */ |
| 181 | void restorePOHCounter(); |
| 182 | |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 183 | /** @brief Serialize and persist requested POH counter. |
| 184 | * |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 185 | * @return fs::path - pathname of persisted requested POH counter. |
| 186 | */ |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 187 | fs::path serializePOH(); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 188 | |
Matt Spinler | 8195784 | 2018-07-11 10:37:12 -0500 | [diff] [blame] | 189 | /** @brief Deserialize a persisted requested POH counter. |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 190 | * |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 191 | * @param[in] retCounter - deserialized POH counter value |
| 192 | * |
| 193 | * @return bool - true if the deserialization was successful, false |
| 194 | * otherwise. |
| 195 | */ |
Allen.Wang | ba182f0 | 2022-03-23 19:01:53 +0800 | [diff] [blame] | 196 | bool deserializePOH(uint32_t& retCounter); |
Nagaraju Goruganti | cb781fe | 2018-04-06 13:41:01 -0500 | [diff] [blame] | 197 | |
Matt Spinler | 9eab986 | 2018-07-11 14:13:52 -0500 | [diff] [blame] | 198 | /** @brief Sets the LastStateChangeTime property and persists it. */ |
| 199 | void setStateChangeTime(); |
| 200 | |
| 201 | /** @brief Serialize the last power state change time. |
| 202 | * |
| 203 | * Save the time the state changed and the state itself. |
| 204 | * The state needs to be saved as well so that during rediscovery |
| 205 | * on reboots there's a way to know not to update the time again. |
| 206 | */ |
| 207 | void serializeStateChangeTime(); |
| 208 | |
| 209 | /** @brief Deserialize the last power state change time. |
| 210 | * |
| 211 | * @param[out] time - Deserialized time |
| 212 | * @param[out] state - Deserialized power state |
| 213 | * |
| 214 | * @return bool - true if successful, false otherwise. |
| 215 | */ |
| 216 | bool deserializeStateChangeTime(uint64_t& time, PowerState& state); |
| 217 | |
| 218 | /** @brief Restores the power state change time. |
| 219 | * |
| 220 | * The time is loaded into the LastStateChangeTime D-Bus property. |
| 221 | * On the very first start after this code has been applied but |
| 222 | * before the state has changed, the LastStateChangeTime value |
| 223 | * will be zero. |
| 224 | */ |
| 225 | void restoreChassisStateChangeTime(); |
| 226 | |
William A. Kennington III | d998f82 | 2018-10-17 23:17:57 -0700 | [diff] [blame] | 227 | /** @brief Timer used for tracking power on hours */ |
Patrick Williams | 45a1ed7 | 2021-04-30 21:02:43 -0500 | [diff] [blame] | 228 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> pohTimer; |
Ben Tyner | 2c36e5a | 2021-07-12 14:56:49 -0500 | [diff] [blame] | 229 | |
| 230 | /** @brief Function to check for a standby voltage regulator fault |
| 231 | * |
| 232 | * Determine if a standby voltage regulator fault was detected and |
| 233 | * return true or false accordingly. |
| 234 | * |
| 235 | * @return true if fault detected, else false |
| 236 | */ |
Pavithra Barithaya | a1c0e5c | 2024-06-21 12:39:38 -0500 | [diff] [blame] | 237 | static bool standbyVoltageRegulatorFault(); |
Andrew Geissler | 2cf2a26 | 2022-02-02 14:38:41 -0600 | [diff] [blame] | 238 | |
| 239 | /** @brief Process UPS property changes |
| 240 | * |
| 241 | * Instance specific interface to monitor for changes to the UPS |
| 242 | * properties which may impact CurrentPowerStatus |
| 243 | * |
| 244 | * @param[in] msg - Data associated with subscribed signal |
| 245 | * |
| 246 | */ |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 247 | void uPowerChangeEvent(sdbusplus::message_t& msg); |
Adriana Kobylak | 396ed8a | 2022-03-22 15:45:41 +0000 | [diff] [blame] | 248 | |
| 249 | /** @brief Process PowerSystemInputs property changes |
| 250 | * |
| 251 | * Instance specific interface to monitor for changes to the |
| 252 | * PowerSystemInputs properties which may impact CurrentPowerStatus |
| 253 | * |
| 254 | * @param[in] msg - Data associated with subscribed signal |
| 255 | * |
| 256 | */ |
Patrick Williams | f053e6f | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 257 | void powerSysInputsChangeEvent(sdbusplus::message_t& msg); |
Andrew Geissler | a90a31a | 2016-12-13 16:16:28 -0600 | [diff] [blame] | 258 | }; |
| 259 | |
| 260 | } // namespace manager |
| 261 | } // namespace state |
| 262 | } // namespace phosphor |