Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018-2019 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 16 | #include <sys/sysinfo.h> |
| 17 | #include <systemd/sd-journal.h> |
| 18 | |
Jason M. Bills | e63dea0 | 2020-08-27 12:07:35 -0700 | [diff] [blame] | 19 | #include <boost/asio/io_service.hpp> |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 20 | #include <boost/asio/posix/stream_descriptor.hpp> |
Jason M. Bills | e63dea0 | 2020-08-27 12:07:35 -0700 | [diff] [blame] | 21 | #include <boost/asio/steady_timer.hpp> |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 22 | #include <boost/container/flat_map.hpp> |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 23 | #include <boost/container/flat_set.hpp> |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 24 | #include <gpiod.hpp> |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 25 | #include <nlohmann/json.hpp> |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 26 | #include <phosphor-logging/lg2.hpp> |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 27 | #include <sdbusplus/asio/object_server.hpp> |
Vijay Khemka | 2b6f442 | 2020-05-29 11:13:23 -0700 | [diff] [blame] | 28 | |
| 29 | #include <filesystem> |
| 30 | #include <fstream> |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 31 | #include <string_view> |
| 32 | |
| 33 | namespace power_control |
| 34 | { |
| 35 | static boost::asio::io_service io; |
| 36 | std::shared_ptr<sdbusplus::asio::connection> conn; |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 37 | |
| 38 | static std::string node = "0"; |
| 39 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 40 | enum class DbusConfigType |
| 41 | { |
| 42 | name = 1, |
| 43 | path, |
| 44 | interface, |
| 45 | property |
| 46 | }; |
| 47 | boost::container::flat_map<DbusConfigType, std::string> dbusParams = { |
| 48 | {DbusConfigType::name, "DbusName"}, |
| 49 | {DbusConfigType::path, "Path"}, |
| 50 | {DbusConfigType::interface, "Interface"}, |
| 51 | {DbusConfigType::property, "Property"}}; |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 52 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 53 | enum class ConfigType |
| 54 | { |
| 55 | GPIO = 1, |
| 56 | DBUS |
| 57 | }; |
| 58 | |
| 59 | struct ConfigData |
| 60 | { |
| 61 | std::string name; |
| 62 | std::string lineName; |
| 63 | std::string dbusName; |
| 64 | std::string path; |
| 65 | std::string interface; |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 66 | bool polarity; |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 67 | ConfigType type; |
| 68 | }; |
| 69 | |
| 70 | static ConfigData powerOutConfig; |
| 71 | static ConfigData powerOkConfig; |
| 72 | static ConfigData resetOutConfig; |
| 73 | static ConfigData nmiOutConfig; |
| 74 | static ConfigData sioPwrGoodConfig; |
| 75 | static ConfigData sioOnControlConfig; |
| 76 | static ConfigData sioS5Config; |
| 77 | static ConfigData postCompleteConfig; |
| 78 | static ConfigData powerButtonConfig; |
| 79 | static ConfigData resetButtonConfig; |
| 80 | static ConfigData idButtonConfig; |
| 81 | static ConfigData nmiButtonConfig; |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 82 | static ConfigData slotPowerConfig; |
| 83 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 84 | // map for storing list of gpio parameters whose config are to be read from x86 |
| 85 | // power control json config |
| 86 | boost::container::flat_map<std::string, ConfigData*> powerSignalMap = { |
| 87 | {"PowerOut", &powerOutConfig}, |
| 88 | {"PowerOk", &powerOkConfig}, |
| 89 | {"ResetOut", &resetOutConfig}, |
| 90 | {"NMIOut", &nmiOutConfig}, |
| 91 | {"SioPowerGood", &sioPwrGoodConfig}, |
| 92 | {"SioOnControl", &sioOnControlConfig}, |
| 93 | {"SIOS5", &sioS5Config}, |
| 94 | {"PostComplete", &postCompleteConfig}, |
| 95 | {"PowerButton", &powerButtonConfig}, |
| 96 | {"ResetButton", &resetButtonConfig}, |
| 97 | {"IdButton", &idButtonConfig}, |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 98 | {"NMIButton", &nmiButtonConfig}, |
| 99 | {"SlotPower", &slotPowerConfig}}; |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 100 | |
| 101 | static std::string hostDbusName = "xyz.openbmc_project.State.Host"; |
| 102 | static std::string chassisDbusName = "xyz.openbmc_project.State.Chassis"; |
| 103 | static std::string osDbusName = "xyz.openbmc_project.State.OperatingSystem"; |
| 104 | static std::string buttonDbusName = "xyz.openbmc_project.Chassis.Buttons"; |
| 105 | static std::string nmiDbusName = "xyz.openbmc_project.Control.Host.NMI"; |
| 106 | static std::string rstCauseDbusName = |
| 107 | "xyz.openbmc_project.Control.Host.RestartCause"; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 108 | static std::shared_ptr<sdbusplus::asio::dbus_interface> hostIface; |
| 109 | static std::shared_ptr<sdbusplus::asio::dbus_interface> chassisIface; |
Vijay Khemka | 04175c2 | 2020-10-09 14:28:11 -0700 | [diff] [blame] | 110 | #ifdef CHASSIS_SYSTEM_RESET |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 111 | static std::shared_ptr<sdbusplus::asio::dbus_interface> chassisSysIface; |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 112 | static std::shared_ptr<sdbusplus::asio::dbus_interface> chassisSlotIface; |
Vijay Khemka | 04175c2 | 2020-10-09 14:28:11 -0700 | [diff] [blame] | 113 | #endif |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 114 | static std::shared_ptr<sdbusplus::asio::dbus_interface> powerButtonIface; |
| 115 | static std::shared_ptr<sdbusplus::asio::dbus_interface> resetButtonIface; |
| 116 | static std::shared_ptr<sdbusplus::asio::dbus_interface> nmiButtonIface; |
| 117 | static std::shared_ptr<sdbusplus::asio::dbus_interface> osIface; |
| 118 | static std::shared_ptr<sdbusplus::asio::dbus_interface> idButtonIface; |
Chen Yugang | 174ec66 | 2019-08-19 19:58:49 +0800 | [diff] [blame] | 119 | static std::shared_ptr<sdbusplus::asio::dbus_interface> nmiOutIface; |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 120 | static std::shared_ptr<sdbusplus::asio::dbus_interface> restartCauseIface; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 121 | |
| 122 | static gpiod::line powerButtonMask; |
| 123 | static gpiod::line resetButtonMask; |
| 124 | static bool nmiButtonMasked = false; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 125 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 126 | // This map contains all timer values that are to be read from json config |
| 127 | boost::container::flat_map<std::string, int> TimerMap = { |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 128 | {"PowerPulseMs", 200}, |
| 129 | {"ForceOffPulseMs", 15000}, |
| 130 | {"ResetPulseMs", 500}, |
| 131 | {"PowerCycleMs", 5000}, |
| 132 | {"SioPowerGoodWatchdogMs", 1000}, |
| 133 | {"PsPowerOKWatchdogMs", 8000}, |
| 134 | {"GracefulPowerOffS", (5 * 60)}, |
| 135 | {"WarmResetCheckMs", 500}, |
| 136 | {"PowerOffSaveMs", 7000}, |
| 137 | {"SlotPowerCycleMs", 200}}; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 138 | const static std::filesystem::path powerControlDir = "/var/lib/power-control"; |
| 139 | const static constexpr std::string_view powerStateFile = "power-state"; |
| 140 | |
| 141 | static bool nmiEnabled = true; |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 142 | static bool sioEnabled = true; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 143 | |
| 144 | // Timers |
| 145 | // Time holding GPIOs asserted |
| 146 | static boost::asio::steady_timer gpioAssertTimer(io); |
| 147 | // Time between off and on during a power cycle |
| 148 | static boost::asio::steady_timer powerCycleTimer(io); |
| 149 | // Time OS gracefully powering off |
| 150 | static boost::asio::steady_timer gracefulPowerOffTimer(io); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 151 | // Time the warm reset check |
| 152 | static boost::asio::steady_timer warmResetCheckTimer(io); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 153 | // Time power supply power OK assertion on power-on |
| 154 | static boost::asio::steady_timer psPowerOKWatchdogTimer(io); |
| 155 | // Time SIO power good assertion on power-on |
| 156 | static boost::asio::steady_timer sioPowerGoodWatchdogTimer(io); |
| 157 | // Time power-off state save for power loss tracking |
| 158 | static boost::asio::steady_timer powerStateSaveTimer(io); |
| 159 | // POH timer |
| 160 | static boost::asio::steady_timer pohCounterTimer(io); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 161 | // Time when to allow restart cause updates |
| 162 | static boost::asio::steady_timer restartCauseTimer(io); |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 163 | static boost::asio::steady_timer slotPowerCycleTimer(io); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 164 | |
| 165 | // GPIO Lines and Event Descriptors |
| 166 | static gpiod::line psPowerOKLine; |
| 167 | static boost::asio::posix::stream_descriptor psPowerOKEvent(io); |
| 168 | static gpiod::line sioPowerGoodLine; |
| 169 | static boost::asio::posix::stream_descriptor sioPowerGoodEvent(io); |
| 170 | static gpiod::line sioOnControlLine; |
| 171 | static boost::asio::posix::stream_descriptor sioOnControlEvent(io); |
| 172 | static gpiod::line sioS5Line; |
| 173 | static boost::asio::posix::stream_descriptor sioS5Event(io); |
| 174 | static gpiod::line powerButtonLine; |
| 175 | static boost::asio::posix::stream_descriptor powerButtonEvent(io); |
| 176 | static gpiod::line resetButtonLine; |
| 177 | static boost::asio::posix::stream_descriptor resetButtonEvent(io); |
| 178 | static gpiod::line nmiButtonLine; |
| 179 | static boost::asio::posix::stream_descriptor nmiButtonEvent(io); |
| 180 | static gpiod::line idButtonLine; |
| 181 | static boost::asio::posix::stream_descriptor idButtonEvent(io); |
| 182 | static gpiod::line postCompleteLine; |
| 183 | static boost::asio::posix::stream_descriptor postCompleteEvent(io); |
| 184 | static gpiod::line nmiOutLine; |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 185 | static gpiod::line slotPowerLine; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 186 | |
| 187 | static constexpr uint8_t beepPowerFail = 8; |
| 188 | |
| 189 | static void beep(const uint8_t& beepPriority) |
| 190 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 191 | lg2::info("Beep with priority: {BEEP_PRIORITY}", "BEEP_PRIORITY", |
| 192 | beepPriority); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 193 | |
| 194 | conn->async_method_call( |
| 195 | [](boost::system::error_code ec) { |
| 196 | if (ec) |
| 197 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 198 | lg2::error( |
| 199 | "beep returned error with async_method_call (ec = {ERROR_MSG})", |
| 200 | "ERROR_MSG", ec.message()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 201 | return; |
| 202 | } |
| 203 | }, |
| 204 | "xyz.openbmc_project.BeepCode", "/xyz/openbmc_project/BeepCode", |
| 205 | "xyz.openbmc_project.BeepCode", "Beep", uint8_t(beepPriority)); |
| 206 | } |
| 207 | |
| 208 | enum class PowerState |
| 209 | { |
| 210 | on, |
| 211 | waitForPSPowerOK, |
| 212 | waitForSIOPowerGood, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 213 | off, |
| 214 | transitionToOff, |
| 215 | gracefulTransitionToOff, |
| 216 | cycleOff, |
| 217 | transitionToCycleOff, |
| 218 | gracefulTransitionToCycleOff, |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 219 | checkForWarmReset, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 220 | }; |
| 221 | static PowerState powerState; |
| 222 | static std::string getPowerStateName(PowerState state) |
| 223 | { |
| 224 | switch (state) |
| 225 | { |
| 226 | case PowerState::on: |
| 227 | return "On"; |
| 228 | break; |
| 229 | case PowerState::waitForPSPowerOK: |
| 230 | return "Wait for Power Supply Power OK"; |
| 231 | break; |
| 232 | case PowerState::waitForSIOPowerGood: |
| 233 | return "Wait for SIO Power Good"; |
| 234 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 235 | case PowerState::off: |
| 236 | return "Off"; |
| 237 | break; |
| 238 | case PowerState::transitionToOff: |
| 239 | return "Transition to Off"; |
| 240 | break; |
| 241 | case PowerState::gracefulTransitionToOff: |
| 242 | return "Graceful Transition to Off"; |
| 243 | break; |
| 244 | case PowerState::cycleOff: |
| 245 | return "Power Cycle Off"; |
| 246 | break; |
| 247 | case PowerState::transitionToCycleOff: |
| 248 | return "Transition to Power Cycle Off"; |
| 249 | break; |
| 250 | case PowerState::gracefulTransitionToCycleOff: |
| 251 | return "Graceful Transition to Power Cycle Off"; |
| 252 | break; |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 253 | case PowerState::checkForWarmReset: |
| 254 | return "Check for Warm Reset"; |
| 255 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 256 | default: |
| 257 | return "unknown state: " + std::to_string(static_cast<int>(state)); |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | static void logStateTransition(const PowerState state) |
| 262 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 263 | lg2::info("Host{HOST}: Moving to \"{STATE}\" state", "HOST", node, "STATE", |
| 264 | getPowerStateName(state)); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | enum class Event |
| 268 | { |
| 269 | psPowerOKAssert, |
| 270 | psPowerOKDeAssert, |
| 271 | sioPowerGoodAssert, |
| 272 | sioPowerGoodDeAssert, |
| 273 | sioS5Assert, |
| 274 | sioS5DeAssert, |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 275 | pltRstAssert, |
| 276 | pltRstDeAssert, |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 277 | postCompleteAssert, |
| 278 | postCompleteDeAssert, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 279 | powerButtonPressed, |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 280 | resetButtonPressed, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 281 | powerCycleTimerExpired, |
| 282 | psPowerOKWatchdogTimerExpired, |
| 283 | sioPowerGoodWatchdogTimerExpired, |
| 284 | gracefulPowerOffTimerExpired, |
| 285 | powerOnRequest, |
| 286 | powerOffRequest, |
| 287 | powerCycleRequest, |
| 288 | resetRequest, |
| 289 | gracefulPowerOffRequest, |
| 290 | gracefulPowerCycleRequest, |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 291 | warmResetDetected, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 292 | }; |
| 293 | static std::string getEventName(Event event) |
| 294 | { |
| 295 | switch (event) |
| 296 | { |
| 297 | case Event::psPowerOKAssert: |
| 298 | return "power supply power OK assert"; |
| 299 | break; |
| 300 | case Event::psPowerOKDeAssert: |
| 301 | return "power supply power OK de-assert"; |
| 302 | break; |
| 303 | case Event::sioPowerGoodAssert: |
| 304 | return "SIO power good assert"; |
| 305 | break; |
| 306 | case Event::sioPowerGoodDeAssert: |
| 307 | return "SIO power good de-assert"; |
| 308 | break; |
| 309 | case Event::sioS5Assert: |
| 310 | return "SIO S5 assert"; |
| 311 | break; |
| 312 | case Event::sioS5DeAssert: |
| 313 | return "SIO S5 de-assert"; |
| 314 | break; |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 315 | case Event::pltRstAssert: |
| 316 | return "PLT_RST assert"; |
| 317 | break; |
| 318 | case Event::pltRstDeAssert: |
| 319 | return "PLT_RST de-assert"; |
| 320 | break; |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 321 | case Event::postCompleteAssert: |
| 322 | return "POST Complete assert"; |
| 323 | break; |
| 324 | case Event::postCompleteDeAssert: |
| 325 | return "POST Complete de-assert"; |
| 326 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 327 | case Event::powerButtonPressed: |
| 328 | return "power button pressed"; |
| 329 | break; |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 330 | case Event::resetButtonPressed: |
| 331 | return "reset button pressed"; |
| 332 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 333 | case Event::powerCycleTimerExpired: |
| 334 | return "power cycle timer expired"; |
| 335 | break; |
| 336 | case Event::psPowerOKWatchdogTimerExpired: |
| 337 | return "power supply power OK watchdog timer expired"; |
| 338 | break; |
| 339 | case Event::sioPowerGoodWatchdogTimerExpired: |
| 340 | return "SIO power good watchdog timer expired"; |
| 341 | break; |
| 342 | case Event::gracefulPowerOffTimerExpired: |
| 343 | return "graceful power-off timer expired"; |
| 344 | break; |
| 345 | case Event::powerOnRequest: |
| 346 | return "power-on request"; |
| 347 | break; |
| 348 | case Event::powerOffRequest: |
| 349 | return "power-off request"; |
| 350 | break; |
| 351 | case Event::powerCycleRequest: |
| 352 | return "power-cycle request"; |
| 353 | break; |
| 354 | case Event::resetRequest: |
| 355 | return "reset request"; |
| 356 | break; |
| 357 | case Event::gracefulPowerOffRequest: |
| 358 | return "graceful power-off request"; |
| 359 | break; |
| 360 | case Event::gracefulPowerCycleRequest: |
| 361 | return "graceful power-cycle request"; |
| 362 | break; |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 363 | case Event::warmResetDetected: |
| 364 | return "warm reset detected"; |
| 365 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 366 | default: |
| 367 | return "unknown event: " + std::to_string(static_cast<int>(event)); |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | static void logEvent(const std::string_view stateHandler, const Event event) |
| 372 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 373 | lg2::info("{STATE_HANDLER}: {EVENT} event received", "STATE_HANDLER", |
| 374 | stateHandler, "EVENT", getEventName(event)); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | // Power state handlers |
| 378 | static void powerStateOn(const Event event); |
| 379 | static void powerStateWaitForPSPowerOK(const Event event); |
| 380 | static void powerStateWaitForSIOPowerGood(const Event event); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 381 | static void powerStateOff(const Event event); |
| 382 | static void powerStateTransitionToOff(const Event event); |
| 383 | static void powerStateGracefulTransitionToOff(const Event event); |
| 384 | static void powerStateCycleOff(const Event event); |
| 385 | static void powerStateTransitionToCycleOff(const Event event); |
| 386 | static void powerStateGracefulTransitionToCycleOff(const Event event); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 387 | static void powerStateCheckForWarmReset(const Event event); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 388 | |
| 389 | static std::function<void(const Event)> getPowerStateHandler(PowerState state) |
| 390 | { |
| 391 | switch (state) |
| 392 | { |
| 393 | case PowerState::on: |
| 394 | return powerStateOn; |
| 395 | break; |
| 396 | case PowerState::waitForPSPowerOK: |
| 397 | return powerStateWaitForPSPowerOK; |
| 398 | break; |
| 399 | case PowerState::waitForSIOPowerGood: |
| 400 | return powerStateWaitForSIOPowerGood; |
| 401 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 402 | case PowerState::off: |
| 403 | return powerStateOff; |
| 404 | break; |
| 405 | case PowerState::transitionToOff: |
| 406 | return powerStateTransitionToOff; |
| 407 | break; |
| 408 | case PowerState::gracefulTransitionToOff: |
| 409 | return powerStateGracefulTransitionToOff; |
| 410 | break; |
| 411 | case PowerState::cycleOff: |
| 412 | return powerStateCycleOff; |
| 413 | break; |
| 414 | case PowerState::transitionToCycleOff: |
| 415 | return powerStateTransitionToCycleOff; |
| 416 | break; |
| 417 | case PowerState::gracefulTransitionToCycleOff: |
| 418 | return powerStateGracefulTransitionToCycleOff; |
| 419 | break; |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 420 | case PowerState::checkForWarmReset: |
| 421 | return powerStateCheckForWarmReset; |
| 422 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 423 | default: |
Zev Weiss | 047bcb5 | 2020-08-20 21:28:11 +0000 | [diff] [blame] | 424 | return nullptr; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 425 | break; |
| 426 | } |
| 427 | }; |
| 428 | |
| 429 | static void sendPowerControlEvent(const Event event) |
| 430 | { |
| 431 | std::function<void(const Event)> handler = getPowerStateHandler(powerState); |
| 432 | if (handler == nullptr) |
| 433 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 434 | lg2::error("Failed to find handler for power state: {STATE}", "STATE", |
| 435 | static_cast<int>(powerState)); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 436 | return; |
| 437 | } |
| 438 | handler(event); |
| 439 | } |
| 440 | |
| 441 | static uint64_t getCurrentTimeMs() |
| 442 | { |
| 443 | struct timespec time = {}; |
| 444 | |
| 445 | if (clock_gettime(CLOCK_REALTIME, &time) < 0) |
| 446 | { |
| 447 | return 0; |
| 448 | } |
| 449 | uint64_t currentTimeMs = static_cast<uint64_t>(time.tv_sec) * 1000; |
| 450 | currentTimeMs += static_cast<uint64_t>(time.tv_nsec) / 1000 / 1000; |
| 451 | |
| 452 | return currentTimeMs; |
| 453 | } |
| 454 | |
| 455 | static constexpr std::string_view getHostState(const PowerState state) |
| 456 | { |
| 457 | switch (state) |
| 458 | { |
| 459 | case PowerState::on: |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 460 | case PowerState::gracefulTransitionToOff: |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 461 | case PowerState::gracefulTransitionToCycleOff: |
| 462 | return "xyz.openbmc_project.State.Host.HostState.Running"; |
| 463 | break; |
| 464 | case PowerState::waitForPSPowerOK: |
| 465 | case PowerState::waitForSIOPowerGood: |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 466 | case PowerState::off: |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 467 | case PowerState::transitionToOff: |
| 468 | case PowerState::transitionToCycleOff: |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 469 | case PowerState::cycleOff: |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 470 | case PowerState::checkForWarmReset: |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 471 | return "xyz.openbmc_project.State.Host.HostState.Off"; |
| 472 | break; |
| 473 | default: |
| 474 | return ""; |
| 475 | break; |
| 476 | } |
| 477 | }; |
| 478 | static constexpr std::string_view getChassisState(const PowerState state) |
| 479 | { |
| 480 | switch (state) |
| 481 | { |
| 482 | case PowerState::on: |
| 483 | case PowerState::transitionToOff: |
| 484 | case PowerState::gracefulTransitionToOff: |
| 485 | case PowerState::transitionToCycleOff: |
| 486 | case PowerState::gracefulTransitionToCycleOff: |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 487 | case PowerState::checkForWarmReset: |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 488 | return "xyz.openbmc_project.State.Chassis.PowerState.On"; |
| 489 | break; |
| 490 | case PowerState::waitForPSPowerOK: |
| 491 | case PowerState::waitForSIOPowerGood: |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 492 | case PowerState::off: |
| 493 | case PowerState::cycleOff: |
| 494 | return "xyz.openbmc_project.State.Chassis.PowerState.Off"; |
| 495 | break; |
| 496 | default: |
| 497 | return ""; |
| 498 | break; |
| 499 | } |
| 500 | }; |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 501 | #ifdef CHASSIS_SYSTEM_RESET |
| 502 | enum class SlotPowerState |
| 503 | { |
| 504 | on, |
| 505 | off, |
| 506 | }; |
| 507 | static SlotPowerState slotPowerState; |
| 508 | static constexpr std::string_view getSlotState(const SlotPowerState state) |
| 509 | { |
| 510 | switch (state) |
| 511 | { |
| 512 | case SlotPowerState::on: |
| 513 | return "xyz.openbmc_project.State.Chassis.PowerState.On"; |
| 514 | break; |
| 515 | case SlotPowerState::off: |
| 516 | return "xyz.openbmc_project.State.Chassis.PowerState.Off"; |
| 517 | break; |
| 518 | default: |
| 519 | return ""; |
| 520 | break; |
| 521 | } |
| 522 | }; |
| 523 | static void setSlotPowerState(const SlotPowerState state) |
| 524 | { |
| 525 | slotPowerState = state; |
| 526 | chassisSlotIface->set_property("CurrentPowerState", |
| 527 | std::string(getSlotState(slotPowerState))); |
| 528 | chassisSlotIface->set_property("LastStateChangeTime", getCurrentTimeMs()); |
| 529 | } |
| 530 | #endif |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 531 | static void savePowerState(const PowerState state) |
| 532 | { |
| 533 | powerStateSaveTimer.expires_after( |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 534 | std::chrono::milliseconds(TimerMap["PowerOffSaveMs"])); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 535 | powerStateSaveTimer.async_wait([state](const boost::system::error_code ec) { |
| 536 | if (ec) |
| 537 | { |
| 538 | // operation_aborted is expected if timer is canceled before |
| 539 | // completion. |
| 540 | if (ec != boost::asio::error::operation_aborted) |
| 541 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 542 | lg2::error("Power-state save async_wait failed: {ERROR_MSG}", |
| 543 | "ERROR_MSG", ec.message()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 544 | } |
| 545 | return; |
| 546 | } |
| 547 | std::ofstream powerStateStream(powerControlDir / powerStateFile); |
| 548 | powerStateStream << getChassisState(state); |
| 549 | }); |
| 550 | } |
| 551 | static void setPowerState(const PowerState state) |
| 552 | { |
| 553 | powerState = state; |
| 554 | logStateTransition(state); |
| 555 | |
| 556 | hostIface->set_property("CurrentHostState", |
| 557 | std::string(getHostState(powerState))); |
| 558 | |
| 559 | chassisIface->set_property("CurrentPowerState", |
| 560 | std::string(getChassisState(powerState))); |
| 561 | chassisIface->set_property("LastStateChangeTime", getCurrentTimeMs()); |
| 562 | |
| 563 | // Save the power state for the restore policy |
| 564 | savePowerState(state); |
| 565 | } |
| 566 | |
| 567 | enum class RestartCause |
| 568 | { |
| 569 | command, |
| 570 | resetButton, |
| 571 | powerButton, |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 572 | watchdog, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 573 | powerPolicyOn, |
| 574 | powerPolicyRestore, |
| 575 | softReset, |
| 576 | }; |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 577 | static boost::container::flat_set<RestartCause> causeSet; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 578 | static std::string getRestartCause(RestartCause cause) |
| 579 | { |
| 580 | switch (cause) |
| 581 | { |
| 582 | case RestartCause::command: |
| 583 | return "xyz.openbmc_project.State.Host.RestartCause.IpmiCommand"; |
| 584 | break; |
| 585 | case RestartCause::resetButton: |
| 586 | return "xyz.openbmc_project.State.Host.RestartCause.ResetButton"; |
| 587 | break; |
| 588 | case RestartCause::powerButton: |
| 589 | return "xyz.openbmc_project.State.Host.RestartCause.PowerButton"; |
| 590 | break; |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 591 | case RestartCause::watchdog: |
| 592 | return "xyz.openbmc_project.State.Host.RestartCause.WatchdogTimer"; |
| 593 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 594 | case RestartCause::powerPolicyOn: |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 595 | return "xyz.openbmc_project.State.Host.RestartCause.PowerPolicyAlwaysOn"; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 596 | break; |
| 597 | case RestartCause::powerPolicyRestore: |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 598 | return "xyz.openbmc_project.State.Host.RestartCause.PowerPolicyPreviousState"; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 599 | break; |
| 600 | case RestartCause::softReset: |
| 601 | return "xyz.openbmc_project.State.Host.RestartCause.SoftReset"; |
| 602 | break; |
| 603 | default: |
| 604 | return "xyz.openbmc_project.State.Host.RestartCause.Unknown"; |
| 605 | break; |
| 606 | } |
| 607 | } |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 608 | static void addRestartCause(const RestartCause cause) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 609 | { |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 610 | // Add this to the set of causes for this restart |
| 611 | causeSet.insert(cause); |
| 612 | } |
| 613 | static void clearRestartCause() |
| 614 | { |
| 615 | // Clear the set for the next restart |
| 616 | causeSet.clear(); |
| 617 | } |
| 618 | static void setRestartCauseProperty(const std::string& cause) |
| 619 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 620 | lg2::info("RestartCause set to {RESTART_CAUSE}", "RESTART_CAUSE", cause); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 621 | restartCauseIface->set_property("RestartCause", cause); |
| 622 | } |
Rashmi RV | 89f6131 | 2020-01-22 15:41:50 +0530 | [diff] [blame] | 623 | |
| 624 | static void resetACBootProperty() |
| 625 | { |
| 626 | if ((causeSet.contains(RestartCause::command)) || |
| 627 | (causeSet.contains(RestartCause::softReset))) |
| 628 | { |
| 629 | conn->async_method_call( |
| 630 | [](boost::system::error_code ec) { |
| 631 | if (ec) |
| 632 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 633 | lg2::error("failed to reset ACBoot property"); |
Rashmi RV | 89f6131 | 2020-01-22 15:41:50 +0530 | [diff] [blame] | 634 | } |
| 635 | }, |
| 636 | "xyz.openbmc_project.Settings", |
| 637 | "/xyz/openbmc_project/control/host0/ac_boot", |
| 638 | "org.freedesktop.DBus.Properties", "Set", |
| 639 | "xyz.openbmc_project.Common.ACBoot", "ACBoot", |
| 640 | std::variant<std::string>{"False"}); |
| 641 | } |
| 642 | } |
| 643 | |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 644 | static void setRestartCause() |
| 645 | { |
| 646 | // Determine the actual restart cause based on the set of causes |
| 647 | std::string restartCause = |
| 648 | "xyz.openbmc_project.State.Host.RestartCause.Unknown"; |
| 649 | if (causeSet.contains(RestartCause::watchdog)) |
| 650 | { |
| 651 | restartCause = getRestartCause(RestartCause::watchdog); |
| 652 | } |
| 653 | else if (causeSet.contains(RestartCause::command)) |
| 654 | { |
| 655 | restartCause = getRestartCause(RestartCause::command); |
| 656 | } |
| 657 | else if (causeSet.contains(RestartCause::resetButton)) |
| 658 | { |
| 659 | restartCause = getRestartCause(RestartCause::resetButton); |
| 660 | } |
| 661 | else if (causeSet.contains(RestartCause::powerButton)) |
| 662 | { |
| 663 | restartCause = getRestartCause(RestartCause::powerButton); |
| 664 | } |
| 665 | else if (causeSet.contains(RestartCause::powerPolicyOn)) |
| 666 | { |
| 667 | restartCause = getRestartCause(RestartCause::powerPolicyOn); |
| 668 | } |
| 669 | else if (causeSet.contains(RestartCause::powerPolicyRestore)) |
| 670 | { |
| 671 | restartCause = getRestartCause(RestartCause::powerPolicyRestore); |
| 672 | } |
| 673 | else if (causeSet.contains(RestartCause::softReset)) |
| 674 | { |
| 675 | restartCause = getRestartCause(RestartCause::softReset); |
| 676 | } |
| 677 | |
| 678 | setRestartCauseProperty(restartCause); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Jason M. Bills | 6c2ad36 | 2019-08-01 16:53:35 -0700 | [diff] [blame] | 681 | static void systemPowerGoodFailedLog() |
| 682 | { |
| 683 | sd_journal_send( |
| 684 | "MESSAGE=PowerControl: system power good failed to assert (VR failure)", |
| 685 | "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 686 | "OpenBMC.0.1.SystemPowerGoodFailed", "REDFISH_MESSAGE_ARGS=%d", |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 687 | TimerMap["SioPowerGoodWatchdogMs"], NULL); |
Jason M. Bills | 6c2ad36 | 2019-08-01 16:53:35 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | static void psPowerOKFailedLog() |
| 691 | { |
| 692 | sd_journal_send( |
| 693 | "MESSAGE=PowerControl: power supply power good failed to assert", |
| 694 | "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 695 | "OpenBMC.0.1.PowerSupplyPowerGoodFailed", "REDFISH_MESSAGE_ARGS=%d", |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 696 | TimerMap["PsPowerOKWatchdogMs"], NULL); |
Jason M. Bills | 6c2ad36 | 2019-08-01 16:53:35 -0700 | [diff] [blame] | 697 | } |
| 698 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 699 | static void powerRestorePolicyLog() |
| 700 | { |
| 701 | sd_journal_send("MESSAGE=PowerControl: power restore policy applied", |
| 702 | "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 703 | "OpenBMC.0.1.PowerRestorePolicyApplied", NULL); |
| 704 | } |
| 705 | |
| 706 | static void powerButtonPressLog() |
| 707 | { |
| 708 | sd_journal_send("MESSAGE=PowerControl: power button pressed", "PRIORITY=%i", |
| 709 | LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 710 | "OpenBMC.0.1.PowerButtonPressed", NULL); |
| 711 | } |
| 712 | |
| 713 | static void resetButtonPressLog() |
| 714 | { |
| 715 | sd_journal_send("MESSAGE=PowerControl: reset button pressed", "PRIORITY=%i", |
| 716 | LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 717 | "OpenBMC.0.1.ResetButtonPressed", NULL); |
| 718 | } |
| 719 | |
| 720 | static void nmiButtonPressLog() |
| 721 | { |
| 722 | sd_journal_send("MESSAGE=PowerControl: NMI button pressed", "PRIORITY=%i", |
| 723 | LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 724 | "OpenBMC.0.1.NMIButtonPressed", NULL); |
| 725 | } |
| 726 | |
| 727 | static void nmiDiagIntLog() |
| 728 | { |
| 729 | sd_journal_send("MESSAGE=PowerControl: NMI Diagnostic Interrupt", |
| 730 | "PRIORITY=%i", LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 731 | "OpenBMC.0.1.NMIDiagnosticInterrupt", NULL); |
| 732 | } |
| 733 | |
| 734 | static int initializePowerStateStorage() |
| 735 | { |
| 736 | // create the power control directory if it doesn't exist |
| 737 | std::error_code ec; |
| 738 | if (!(std::filesystem::create_directories(powerControlDir, ec))) |
| 739 | { |
| 740 | if (ec.value() != 0) |
| 741 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 742 | lg2::error("failed to create {DIR_NAME}: {ERROR_MSG}", "DIR_NAME", |
| 743 | powerControlDir.string(), "ERROR_MSG", ec.message()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 744 | return -1; |
| 745 | } |
| 746 | } |
| 747 | // Create the power state file if it doesn't exist |
| 748 | if (!std::filesystem::exists(powerControlDir / powerStateFile)) |
| 749 | { |
| 750 | std::ofstream powerStateStream(powerControlDir / powerStateFile); |
| 751 | powerStateStream << getChassisState(powerState); |
| 752 | } |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | static bool wasPowerDropped() |
| 757 | { |
| 758 | std::ifstream powerStateStream(powerControlDir / powerStateFile); |
| 759 | if (!powerStateStream.is_open()) |
| 760 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 761 | lg2::error("Failed to open power state file"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 762 | return false; |
| 763 | } |
| 764 | |
| 765 | std::string state; |
| 766 | std::getline(powerStateStream, state); |
| 767 | return state == "xyz.openbmc_project.State.Chassis.PowerState.On"; |
| 768 | } |
| 769 | |
| 770 | static void invokePowerRestorePolicy(const std::string& policy) |
| 771 | { |
| 772 | // Async events may call this twice, but we only want to run once |
| 773 | static bool policyInvoked = false; |
| 774 | if (policyInvoked) |
| 775 | { |
| 776 | return; |
| 777 | } |
| 778 | policyInvoked = true; |
| 779 | |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 780 | lg2::info("Power restore delay expired, invoking {POLICY}", "POLICY", |
| 781 | policy); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 782 | if (policy == |
| 783 | "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.AlwaysOn") |
| 784 | { |
| 785 | sendPowerControlEvent(Event::powerOnRequest); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 786 | setRestartCauseProperty(getRestartCause(RestartCause::powerPolicyOn)); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 787 | } |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 788 | else if (policy == |
| 789 | "xyz.openbmc_project.Control.Power.RestorePolicy.Policy.Restore") |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 790 | { |
| 791 | if (wasPowerDropped()) |
| 792 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 793 | lg2::info("Power was dropped, restoring Host On state"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 794 | sendPowerControlEvent(Event::powerOnRequest); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 795 | setRestartCauseProperty( |
| 796 | getRestartCause(RestartCause::powerPolicyRestore)); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 797 | } |
| 798 | else |
| 799 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 800 | lg2::info("No power drop, restoring Host Off state"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 801 | } |
| 802 | } |
Jason M. Bills | 94ce8eb | 2019-09-30 10:13:25 -0700 | [diff] [blame] | 803 | // We're done with the previous power state for the restore policy, so store |
| 804 | // the current state |
| 805 | savePowerState(powerState); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | static void powerRestorePolicyDelay(int delay) |
| 809 | { |
| 810 | // Async events may call this twice, but we only want to run once |
| 811 | static bool delayStarted = false; |
| 812 | if (delayStarted) |
| 813 | { |
| 814 | return; |
| 815 | } |
| 816 | delayStarted = true; |
| 817 | // Calculate the delay from now to meet the requested delay |
| 818 | // Subtract the approximate uboot time |
| 819 | static constexpr const int ubootSeconds = 20; |
| 820 | delay -= ubootSeconds; |
| 821 | // Subtract the time since boot |
| 822 | struct sysinfo info = {}; |
| 823 | if (sysinfo(&info) == 0) |
| 824 | { |
| 825 | delay -= info.uptime; |
| 826 | } |
| 827 | // 0 is the minimum delay |
| 828 | delay = std::max(delay, 0); |
| 829 | |
| 830 | static boost::asio::steady_timer powerRestorePolicyTimer(io); |
| 831 | powerRestorePolicyTimer.expires_after(std::chrono::seconds(delay)); |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 832 | lg2::info("Power restore delay of {DELAY} seconds started", "DELAY", delay); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 833 | powerRestorePolicyTimer.async_wait([](const boost::system::error_code ec) { |
| 834 | if (ec) |
| 835 | { |
| 836 | // operation_aborted is expected if timer is canceled before |
| 837 | // completion. |
| 838 | if (ec != boost::asio::error::operation_aborted) |
| 839 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 840 | lg2::error( |
| 841 | "power restore policy async_wait failed: {ERROR_MSG}", |
| 842 | "ERROR_MSG", ec.message()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 843 | } |
| 844 | return; |
| 845 | } |
| 846 | // Get Power Restore Policy |
| 847 | // In case PowerRestorePolicy is not available, set a match for it |
| 848 | static std::unique_ptr<sdbusplus::bus::match::match> |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 849 | powerRestorePolicyMatch = |
| 850 | std::make_unique<sdbusplus::bus::match::match>( |
| 851 | *conn, |
| 852 | "type='signal',interface='org.freedesktop.DBus.Properties'," |
| 853 | "member='PropertiesChanged',arg0namespace='xyz.openbmc_" |
| 854 | "project.Control.Power.RestorePolicy'", |
| 855 | [](sdbusplus::message::message& msg) { |
| 856 | std::string interfaceName; |
| 857 | boost::container::flat_map<std::string, |
| 858 | std::variant<std::string>> |
| 859 | propertiesChanged; |
| 860 | std::string policy; |
| 861 | try |
| 862 | { |
| 863 | msg.read(interfaceName, propertiesChanged); |
| 864 | policy = std::get<std::string>( |
| 865 | propertiesChanged.begin()->second); |
| 866 | } |
| 867 | catch (const std::exception& e) |
| 868 | { |
| 869 | lg2::error( |
| 870 | "Unable to read restore policy value: {ERROR}", |
| 871 | "ERROR", e); |
| 872 | powerRestorePolicyMatch.reset(); |
| 873 | return; |
| 874 | } |
| 875 | invokePowerRestorePolicy(policy); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 876 | powerRestorePolicyMatch.reset(); |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 877 | }); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 878 | |
| 879 | // Check if it's already on DBus |
| 880 | conn->async_method_call( |
| 881 | [](boost::system::error_code ec, |
| 882 | const std::variant<std::string>& policyProperty) { |
| 883 | if (ec) |
| 884 | { |
| 885 | return; |
| 886 | } |
| 887 | powerRestorePolicyMatch.reset(); |
| 888 | const std::string* policy = |
| 889 | std::get_if<std::string>(&policyProperty); |
| 890 | if (policy == nullptr) |
| 891 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 892 | lg2::error("Unable to read power restore policy value"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 893 | return; |
| 894 | } |
| 895 | invokePowerRestorePolicy(*policy); |
| 896 | }, |
| 897 | "xyz.openbmc_project.Settings", |
| 898 | "/xyz/openbmc_project/control/host0/power_restore_policy", |
| 899 | "org.freedesktop.DBus.Properties", "Get", |
| 900 | "xyz.openbmc_project.Control.Power.RestorePolicy", |
| 901 | "PowerRestorePolicy"); |
| 902 | }); |
| 903 | } |
| 904 | |
| 905 | static void powerRestorePolicyStart() |
| 906 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 907 | lg2::info("Power restore policy started"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 908 | powerRestorePolicyLog(); |
| 909 | |
| 910 | // Get the desired delay time |
| 911 | // In case PowerRestoreDelay is not available, set a match for it |
| 912 | static std::unique_ptr<sdbusplus::bus::match::match> |
| 913 | powerRestoreDelayMatch = std::make_unique<sdbusplus::bus::match::match>( |
| 914 | *conn, |
| 915 | "type='signal',interface='org.freedesktop.DBus.Properties',member='" |
| 916 | "PropertiesChanged',arg0namespace='xyz.openbmc_project.Control." |
| 917 | "Power.RestoreDelay'", |
| 918 | [](sdbusplus::message::message& msg) { |
| 919 | std::string interfaceName; |
| 920 | boost::container::flat_map<std::string, std::variant<uint16_t>> |
| 921 | propertiesChanged; |
| 922 | int delay = 0; |
| 923 | try |
| 924 | { |
| 925 | msg.read(interfaceName, propertiesChanged); |
| 926 | delay = |
| 927 | std::get<uint16_t>(propertiesChanged.begin()->second); |
| 928 | } |
Patrick Williams | f3a33b4 | 2021-10-06 12:37:26 -0500 | [diff] [blame] | 929 | catch (const std::exception& e) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 930 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 931 | lg2::error( |
| 932 | "Unable to read power restore delay value: {ERROR}", |
| 933 | "ERROR", e); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 934 | powerRestoreDelayMatch.reset(); |
| 935 | return; |
| 936 | } |
| 937 | powerRestorePolicyDelay(delay); |
| 938 | powerRestoreDelayMatch.reset(); |
| 939 | }); |
| 940 | |
| 941 | // Check if it's already on DBus |
| 942 | conn->async_method_call( |
| 943 | [](boost::system::error_code ec, |
| 944 | const std::variant<uint16_t>& delayProperty) { |
| 945 | if (ec) |
| 946 | { |
| 947 | return; |
| 948 | } |
| 949 | powerRestoreDelayMatch.reset(); |
| 950 | const uint16_t* delay = std::get_if<uint16_t>(&delayProperty); |
| 951 | if (delay == nullptr) |
| 952 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 953 | lg2::error("Unable to read power restore delay value"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 954 | return; |
| 955 | } |
| 956 | powerRestorePolicyDelay(*delay); |
| 957 | }, |
| 958 | "xyz.openbmc_project.Settings", |
| 959 | "/xyz/openbmc_project/control/power_restore_delay", |
| 960 | "org.freedesktop.DBus.Properties", "Get", |
| 961 | "xyz.openbmc_project.Control.Power.RestoreDelay", "PowerRestoreDelay"); |
| 962 | } |
| 963 | |
| 964 | static void powerRestorePolicyCheck() |
| 965 | { |
| 966 | // In case ACBoot is not available, set a match for it |
| 967 | static std::unique_ptr<sdbusplus::bus::match::match> acBootMatch = |
| 968 | std::make_unique<sdbusplus::bus::match::match>( |
| 969 | *conn, |
| 970 | "type='signal',interface='org.freedesktop.DBus.Properties',member='" |
| 971 | "PropertiesChanged',arg0namespace='xyz.openbmc_project.Common." |
| 972 | "ACBoot'", |
| 973 | [](sdbusplus::message::message& msg) { |
| 974 | std::string interfaceName; |
| 975 | boost::container::flat_map<std::string, |
| 976 | std::variant<std::string>> |
| 977 | propertiesChanged; |
| 978 | std::string acBoot; |
| 979 | try |
| 980 | { |
| 981 | msg.read(interfaceName, propertiesChanged); |
| 982 | acBoot = std::get<std::string>( |
| 983 | propertiesChanged.begin()->second); |
| 984 | } |
Patrick Williams | f3a33b4 | 2021-10-06 12:37:26 -0500 | [diff] [blame] | 985 | catch (const std::exception& e) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 986 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 987 | lg2::error("Unable to read AC Boot status: {ERROR}", |
| 988 | "ERROR", e); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 989 | acBootMatch.reset(); |
| 990 | return; |
| 991 | } |
| 992 | if (acBoot == "Unknown") |
| 993 | { |
| 994 | return; |
| 995 | } |
| 996 | if (acBoot == "True") |
| 997 | { |
| 998 | // Start the Power Restore policy |
| 999 | powerRestorePolicyStart(); |
| 1000 | } |
| 1001 | acBootMatch.reset(); |
| 1002 | }); |
| 1003 | |
| 1004 | // Check if it's already on DBus |
| 1005 | conn->async_method_call( |
| 1006 | [](boost::system::error_code ec, |
| 1007 | const std::variant<std::string>& acBootProperty) { |
| 1008 | if (ec) |
| 1009 | { |
| 1010 | return; |
| 1011 | } |
| 1012 | const std::string* acBoot = |
| 1013 | std::get_if<std::string>(&acBootProperty); |
| 1014 | if (acBoot == nullptr) |
| 1015 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1016 | lg2::error("Unable to read AC Boot status"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1017 | return; |
| 1018 | } |
| 1019 | if (*acBoot == "Unknown") |
| 1020 | { |
| 1021 | return; |
| 1022 | } |
| 1023 | if (*acBoot == "True") |
| 1024 | { |
| 1025 | // Start the Power Restore policy |
| 1026 | powerRestorePolicyStart(); |
| 1027 | } |
| 1028 | acBootMatch.reset(); |
| 1029 | }, |
| 1030 | "xyz.openbmc_project.Settings", |
| 1031 | "/xyz/openbmc_project/control/host0/ac_boot", |
| 1032 | "org.freedesktop.DBus.Properties", "Get", |
| 1033 | "xyz.openbmc_project.Common.ACBoot", "ACBoot"); |
| 1034 | } |
| 1035 | |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 1036 | static void waitForGPIOEvent(const std::string& name, |
| 1037 | const std::function<void(bool)>& eventHandler, |
| 1038 | gpiod::line& line, |
| 1039 | boost::asio::posix::stream_descriptor& event) |
| 1040 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1041 | event.async_wait(boost::asio::posix::stream_descriptor::wait_read, |
| 1042 | [&name, eventHandler, &line, |
| 1043 | &event](const boost::system::error_code ec) { |
| 1044 | if (ec) |
| 1045 | { |
| 1046 | lg2::error( |
| 1047 | "{GPIO_NAME} fd handler error: {ERROR_MSG}", |
| 1048 | "GPIO_NAME", name, "ERROR_MSG", ec.message()); |
| 1049 | // TODO: throw here to force power-control to |
| 1050 | // restart? |
| 1051 | return; |
| 1052 | } |
| 1053 | gpiod::line_event line_event = line.event_read(); |
| 1054 | eventHandler(line_event.event_type == |
| 1055 | gpiod::line_event::RISING_EDGE); |
| 1056 | waitForGPIOEvent(name, eventHandler, line, event); |
| 1057 | }); |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 1058 | } |
| 1059 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1060 | static bool requestGPIOEvents( |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 1061 | const std::string& name, const std::function<void(bool)>& handler, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1062 | gpiod::line& gpioLine, |
| 1063 | boost::asio::posix::stream_descriptor& gpioEventDescriptor) |
| 1064 | { |
| 1065 | // Find the GPIO line |
| 1066 | gpioLine = gpiod::find_line(name); |
| 1067 | if (!gpioLine) |
| 1068 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1069 | lg2::error("Failed to find the {GPIO_NAME} line", "GPIO_NAME", name); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1070 | return false; |
| 1071 | } |
| 1072 | |
| 1073 | try |
| 1074 | { |
| 1075 | gpioLine.request( |
Jason M. Bills | 9fd8ac2 | 2021-10-21 15:14:17 -0700 | [diff] [blame] | 1076 | {"power-control", gpiod::line_request::EVENT_BOTH_EDGES, {}}); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1077 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1078 | catch (const std::exception& e) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1079 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1080 | lg2::error("Failed to request events for {GPIO_NAME}: {ERROR}", |
| 1081 | "GPIO_NAME", name, "ERROR", e); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1082 | return false; |
| 1083 | } |
| 1084 | |
| 1085 | int gpioLineFd = gpioLine.event_get_fd(); |
| 1086 | if (gpioLineFd < 0) |
| 1087 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1088 | lg2::error("Failed to get {GPIO_NAME} fd", "GPIO_NAME", name); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1089 | return false; |
| 1090 | } |
| 1091 | |
| 1092 | gpioEventDescriptor.assign(gpioLineFd); |
| 1093 | |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 1094 | waitForGPIOEvent(name, handler, gpioLine, gpioEventDescriptor); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1095 | return true; |
| 1096 | } |
| 1097 | |
| 1098 | static bool setGPIOOutput(const std::string& name, const int value, |
| 1099 | gpiod::line& gpioLine) |
| 1100 | { |
| 1101 | // Find the GPIO line |
| 1102 | gpioLine = gpiod::find_line(name); |
| 1103 | if (!gpioLine) |
| 1104 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1105 | lg2::error("Failed to find the {GPIO_NAME} line", "GPIO_NAME", name); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1106 | return false; |
| 1107 | } |
| 1108 | |
| 1109 | // Request GPIO output to specified value |
| 1110 | try |
| 1111 | { |
Jason M. Bills | 9fd8ac2 | 2021-10-21 15:14:17 -0700 | [diff] [blame] | 1112 | gpioLine.request( |
| 1113 | {__FUNCTION__, gpiod::line_request::DIRECTION_OUTPUT, {}}, value); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1114 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1115 | catch (const std::exception& e) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1116 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1117 | lg2::error("Failed to request {GPIO_NAME} output: {ERROR}", "GPIO_NAME", |
| 1118 | name, "ERROR", e); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1119 | return false; |
| 1120 | } |
| 1121 | |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1122 | lg2::info("{GPIO_NAME} set to {GPIO_VALUE}", "GPIO_NAME", name, |
| 1123 | "GPIO_VALUE", value); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1124 | return true; |
| 1125 | } |
| 1126 | |
| 1127 | static int setMaskedGPIOOutputForMs(gpiod::line& maskedGPIOLine, |
| 1128 | const std::string& name, const int value, |
| 1129 | const int durationMs) |
| 1130 | { |
| 1131 | // Set the masked GPIO line to the specified value |
| 1132 | maskedGPIOLine.set_value(value); |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1133 | lg2::info("{GPIO_NAME} set to {GPIO_VALUE}", "GPIO_NAME", name, |
| 1134 | "GPIO_VALUE", value); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1135 | gpioAssertTimer.expires_after(std::chrono::milliseconds(durationMs)); |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1136 | gpioAssertTimer.async_wait( |
| 1137 | [maskedGPIOLine, value, name](const boost::system::error_code ec) { |
| 1138 | // Set the masked GPIO line back to the opposite value |
| 1139 | maskedGPIOLine.set_value(!value); |
| 1140 | lg2::info("{GPIO_NAME} released", "GPIO_NAME", name); |
| 1141 | if (ec) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1142 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1143 | // operation_aborted is expected if timer is canceled before |
| 1144 | // completion. |
| 1145 | if (ec != boost::asio::error::operation_aborted) |
| 1146 | { |
| 1147 | lg2::error("{GPIO_NAME} async_wait failed: {ERROR_MSG}", |
| 1148 | "GPIO_NAME", name, "ERROR_MSG", ec.message()); |
| 1149 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1150 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1151 | }); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1152 | return 0; |
| 1153 | } |
| 1154 | |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 1155 | static int setGPIOOutputForMs(const ConfigData& config, const int value, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1156 | const int durationMs) |
| 1157 | { |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 1158 | // If the requested GPIO is masked, use the mask line to set the output |
| 1159 | if (powerButtonMask && config.lineName == powerOutConfig.lineName) |
| 1160 | { |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1161 | return setMaskedGPIOOutputForMs(powerButtonMask, config.lineName, value, |
| 1162 | durationMs); |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 1163 | } |
| 1164 | if (resetButtonMask && config.lineName == resetOutConfig.lineName) |
| 1165 | { |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1166 | return setMaskedGPIOOutputForMs(resetButtonMask, config.lineName, value, |
| 1167 | durationMs); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1168 | } |
| 1169 | |
| 1170 | // No mask set, so request and set the GPIO normally |
| 1171 | gpiod::line gpioLine; |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1172 | if (!setGPIOOutput(config.lineName, value, gpioLine)) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1173 | { |
| 1174 | return -1; |
| 1175 | } |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 1176 | const std::string name = config.lineName; |
Jean-Marie Verdun | 2c495cf | 2021-09-17 21:42:23 -0400 | [diff] [blame] | 1177 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1178 | gpioAssertTimer.expires_after(std::chrono::milliseconds(durationMs)); |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1179 | gpioAssertTimer.async_wait( |
| 1180 | [gpioLine, value, name](const boost::system::error_code ec) { |
| 1181 | // Set the GPIO line back to the opposite value |
| 1182 | gpioLine.set_value(!value); |
| 1183 | lg2::info("{GPIO_NAME} released", "GPIO_NAME", name); |
| 1184 | if (ec) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1185 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1186 | // operation_aborted is expected if timer is canceled before |
| 1187 | // completion. |
| 1188 | if (ec != boost::asio::error::operation_aborted) |
| 1189 | { |
| 1190 | lg2::error("{GPIO_NAME} async_wait failed: {ERROR_MSG}", |
| 1191 | "GPIO_NAME", name, "ERROR_MSG", ec.message()); |
| 1192 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1193 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1194 | }); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1195 | return 0; |
| 1196 | } |
| 1197 | |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1198 | static int assertGPIOForMs(const ConfigData& config, const int durationMs) |
| 1199 | { |
| 1200 | return setGPIOOutputForMs(config, config.polarity, durationMs); |
| 1201 | } |
| 1202 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1203 | static void powerOn() |
| 1204 | { |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1205 | assertGPIOForMs(powerOutConfig, TimerMap["PowerPulseMs"]); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1206 | } |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 1207 | #ifdef CHASSIS_SYSTEM_RESET |
| 1208 | static int slotPowerOn() |
| 1209 | { |
| 1210 | if (power_control::slotPowerState != power_control::SlotPowerState::on) |
| 1211 | { |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1212 | |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 1213 | slotPowerLine.set_value(1); |
| 1214 | |
| 1215 | if (slotPowerLine.get_value() > 0) |
| 1216 | { |
| 1217 | setSlotPowerState(SlotPowerState::on); |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1218 | lg2::info("Slot Power is switched On\n"); |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 1219 | } |
| 1220 | else |
| 1221 | { |
| 1222 | return -1; |
| 1223 | } |
| 1224 | } |
| 1225 | else |
| 1226 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1227 | lg2::info("Slot Power is already in 'On' state\n"); |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 1228 | return -1; |
| 1229 | } |
| 1230 | return 0; |
| 1231 | } |
| 1232 | static int slotPowerOff() |
| 1233 | { |
| 1234 | if (power_control::slotPowerState != power_control::SlotPowerState::off) |
| 1235 | { |
| 1236 | slotPowerLine.set_value(0); |
| 1237 | |
| 1238 | if (!(slotPowerLine.get_value() > 0)) |
| 1239 | { |
| 1240 | setSlotPowerState(SlotPowerState::off); |
| 1241 | setPowerState(PowerState::off); |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1242 | lg2::info("Slot Power is switched Off\n"); |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 1243 | } |
| 1244 | else |
| 1245 | { |
| 1246 | return -1; |
| 1247 | } |
| 1248 | } |
| 1249 | else |
| 1250 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1251 | lg2::info("Slot Power is already in 'Off' state\n"); |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 1252 | return -1; |
| 1253 | } |
| 1254 | return 0; |
| 1255 | } |
| 1256 | static void slotPowerCycle() |
| 1257 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1258 | lg2::info("Slot Power Cycle started\n"); |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 1259 | slotPowerOff(); |
| 1260 | slotPowerCycleTimer.expires_after( |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 1261 | std::chrono::milliseconds(TimerMap["SlotPowerCycleMs"])); |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 1262 | slotPowerCycleTimer.async_wait([](const boost::system::error_code ec) { |
| 1263 | if (ec) |
| 1264 | { |
| 1265 | if (ec != boost::asio::error::operation_aborted) |
| 1266 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1267 | lg2::error( |
| 1268 | "Slot Power cycle timer async_wait failed: {ERROR_MSG}", |
| 1269 | "ERROR_MSG", ec.message()); |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 1270 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1271 | lg2::info("Slot Power cycle timer canceled\n"); |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 1272 | return; |
| 1273 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1274 | lg2::info("Slot Power cycle timer completed\n"); |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 1275 | slotPowerOn(); |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1276 | lg2::info("Slot Power Cycle Completed\n"); |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 1277 | }); |
| 1278 | } |
| 1279 | #endif |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1280 | static void gracefulPowerOff() |
| 1281 | { |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1282 | assertGPIOForMs(powerOutConfig, TimerMap["PowerPulseMs"]); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | static void forcePowerOff() |
| 1286 | { |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1287 | if (assertGPIOForMs(powerOutConfig, TimerMap["ForceOffPulseMs"]) < 0) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1288 | { |
| 1289 | return; |
| 1290 | } |
| 1291 | |
Jason M. Bills | c6961b6 | 2021-10-21 14:08:02 -0700 | [diff] [blame] | 1292 | // If the force off timer expires, then the power-button override failed |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1293 | gpioAssertTimer.async_wait([](const boost::system::error_code ec) { |
| 1294 | if (ec) |
| 1295 | { |
| 1296 | // operation_aborted is expected if timer is canceled before |
| 1297 | // completion. |
| 1298 | if (ec != boost::asio::error::operation_aborted) |
| 1299 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1300 | lg2::error("Force power off async_wait failed: {ERROR_MSG}", |
| 1301 | "ERROR_MSG", ec.message()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1302 | } |
| 1303 | return; |
| 1304 | } |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1305 | |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1306 | lg2::error("Power-button override failed. Not sure what to do now."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1307 | }); |
| 1308 | } |
| 1309 | |
| 1310 | static void reset() |
| 1311 | { |
Jason M. Bills | c8c90c8 | 2021-09-22 14:34:04 -0700 | [diff] [blame] | 1312 | assertGPIOForMs(resetOutConfig, TimerMap["ResetPulseMs"]); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | static void gracefulPowerOffTimerStart() |
| 1316 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1317 | lg2::info("Graceful power-off timer started"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1318 | gracefulPowerOffTimer.expires_after( |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 1319 | std::chrono::seconds(TimerMap["GracefulPowerOffS"])); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1320 | gracefulPowerOffTimer.async_wait([](const boost::system::error_code ec) { |
| 1321 | if (ec) |
| 1322 | { |
| 1323 | // operation_aborted is expected if timer is canceled before |
| 1324 | // completion. |
| 1325 | if (ec != boost::asio::error::operation_aborted) |
| 1326 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1327 | lg2::error("Graceful power-off async_wait failed: {ERROR_MSG}", |
| 1328 | "ERROR_MSG", ec.message()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1329 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1330 | lg2::info("Graceful power-off timer canceled"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1331 | return; |
| 1332 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1333 | lg2::info("Graceful power-off timer completed"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1334 | sendPowerControlEvent(Event::gracefulPowerOffTimerExpired); |
| 1335 | }); |
| 1336 | } |
| 1337 | |
| 1338 | static void powerCycleTimerStart() |
| 1339 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1340 | lg2::info("Power-cycle timer started"); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 1341 | powerCycleTimer.expires_after( |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 1342 | std::chrono::milliseconds(TimerMap["PowerCycleMs"])); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1343 | powerCycleTimer.async_wait([](const boost::system::error_code ec) { |
| 1344 | if (ec) |
| 1345 | { |
| 1346 | // operation_aborted is expected if timer is canceled before |
| 1347 | // completion. |
| 1348 | if (ec != boost::asio::error::operation_aborted) |
| 1349 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1350 | lg2::error("Power-cycle async_wait failed: {ERROR_MSG}", |
| 1351 | "ERROR_MSG", ec.message()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1352 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1353 | lg2::info("Power-cycle timer canceled"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1354 | return; |
| 1355 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1356 | lg2::info("Power-cycle timer completed"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1357 | sendPowerControlEvent(Event::powerCycleTimerExpired); |
| 1358 | }); |
| 1359 | } |
| 1360 | |
| 1361 | static void psPowerOKWatchdogTimerStart() |
| 1362 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1363 | lg2::info("power supply power OK watchdog timer started"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1364 | psPowerOKWatchdogTimer.expires_after( |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 1365 | std::chrono::milliseconds(TimerMap["PsPowerOKWatchdogMs"])); |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1366 | psPowerOKWatchdogTimer.async_wait([](const boost::system::error_code ec) { |
| 1367 | if (ec) |
| 1368 | { |
| 1369 | // operation_aborted is expected if timer is canceled before |
| 1370 | // completion. |
| 1371 | if (ec != boost::asio::error::operation_aborted) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1372 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1373 | lg2::error( |
| 1374 | "power supply power OK watchdog async_wait failed: {ERROR_MSG}", |
| 1375 | "ERROR_MSG", ec.message()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1376 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1377 | lg2::info("power supply power OK watchdog timer canceled"); |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1378 | return; |
| 1379 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1380 | lg2::info("power supply power OK watchdog timer expired"); |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1381 | sendPowerControlEvent(Event::psPowerOKWatchdogTimerExpired); |
| 1382 | }); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1385 | static void warmResetCheckTimerStart() |
| 1386 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1387 | lg2::info("Warm reset check timer started"); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1388 | warmResetCheckTimer.expires_after( |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 1389 | std::chrono::milliseconds(TimerMap["WarmResetCheckMs"])); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1390 | warmResetCheckTimer.async_wait([](const boost::system::error_code ec) { |
| 1391 | if (ec) |
| 1392 | { |
| 1393 | // operation_aborted is expected if timer is canceled before |
| 1394 | // completion. |
| 1395 | if (ec != boost::asio::error::operation_aborted) |
| 1396 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1397 | lg2::error("Warm reset check async_wait failed: {ERROR_MSG}", |
| 1398 | "ERROR_MSG", ec.message()); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1399 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1400 | lg2::info("Warm reset check timer canceled"); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1401 | return; |
| 1402 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1403 | lg2::info("Warm reset check timer completed"); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1404 | sendPowerControlEvent(Event::warmResetDetected); |
| 1405 | }); |
| 1406 | } |
| 1407 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1408 | static void pohCounterTimerStart() |
| 1409 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1410 | lg2::info("POH timer started"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1411 | // Set the time-out as 1 hour, to align with POH command in ipmid |
| 1412 | pohCounterTimer.expires_after(std::chrono::hours(1)); |
| 1413 | pohCounterTimer.async_wait([](const boost::system::error_code& ec) { |
| 1414 | if (ec) |
| 1415 | { |
| 1416 | // operation_aborted is expected if timer is canceled before |
| 1417 | // completion. |
| 1418 | if (ec != boost::asio::error::operation_aborted) |
| 1419 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1420 | lg2::error("POH timer async_wait failed: {ERROR_MSG}", |
| 1421 | "ERROR_MSG", ec.message()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1422 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1423 | lg2::info("POH timer canceled"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1424 | return; |
| 1425 | } |
| 1426 | |
| 1427 | if (getHostState(powerState) != |
| 1428 | "xyz.openbmc_project.State.Host.HostState.Running") |
| 1429 | { |
| 1430 | return; |
| 1431 | } |
| 1432 | |
| 1433 | conn->async_method_call( |
| 1434 | [](boost::system::error_code ec, |
| 1435 | const std::variant<uint32_t>& pohCounterProperty) { |
| 1436 | if (ec) |
| 1437 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1438 | lg2::error("error getting poh counter"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1439 | return; |
| 1440 | } |
| 1441 | const uint32_t* pohCounter = |
| 1442 | std::get_if<uint32_t>(&pohCounterProperty); |
| 1443 | if (pohCounter == nullptr) |
| 1444 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1445 | lg2::error("unable to read poh counter"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1446 | return; |
| 1447 | } |
| 1448 | |
| 1449 | conn->async_method_call( |
| 1450 | [](boost::system::error_code ec) { |
| 1451 | if (ec) |
| 1452 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1453 | lg2::error("failed to set poh counter"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1454 | } |
| 1455 | }, |
| 1456 | "xyz.openbmc_project.Settings", |
| 1457 | "/xyz/openbmc_project/state/chassis0", |
| 1458 | "org.freedesktop.DBus.Properties", "Set", |
| 1459 | "xyz.openbmc_project.State.PowerOnHours", "POHCounter", |
| 1460 | std::variant<uint32_t>(*pohCounter + 1)); |
| 1461 | }, |
| 1462 | "xyz.openbmc_project.Settings", |
| 1463 | "/xyz/openbmc_project/state/chassis0", |
| 1464 | "org.freedesktop.DBus.Properties", "Get", |
| 1465 | "xyz.openbmc_project.State.PowerOnHours", "POHCounter"); |
| 1466 | |
| 1467 | pohCounterTimerStart(); |
| 1468 | }); |
| 1469 | } |
| 1470 | |
| 1471 | static void currentHostStateMonitor() |
| 1472 | { |
Yong Li | 8d66021 | 2019-12-27 10:18:10 +0800 | [diff] [blame] | 1473 | if (getHostState(powerState) == |
| 1474 | "xyz.openbmc_project.State.Host.HostState.Running") |
| 1475 | { |
| 1476 | pohCounterTimerStart(); |
| 1477 | // Clear the restart cause set for the next restart |
| 1478 | clearRestartCause(); |
| 1479 | } |
| 1480 | else |
| 1481 | { |
| 1482 | pohCounterTimer.cancel(); |
| 1483 | // Set the restart cause set for this restart |
| 1484 | setRestartCause(); |
| 1485 | } |
| 1486 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1487 | static auto match = sdbusplus::bus::match::match( |
| 1488 | *conn, |
| 1489 | "type='signal',member='PropertiesChanged', " |
| 1490 | "interface='org.freedesktop.DBus.Properties', " |
Jason M. Bills | 6a6485a | 2020-07-24 14:07:07 -0700 | [diff] [blame] | 1491 | "arg0='xyz.openbmc_project.State.Host'", |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1492 | [](sdbusplus::message::message& message) { |
| 1493 | std::string intfName; |
| 1494 | std::map<std::string, std::variant<std::string>> properties; |
| 1495 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1496 | try |
| 1497 | { |
Jason M. Bills | 6a6485a | 2020-07-24 14:07:07 -0700 | [diff] [blame] | 1498 | message.read(intfName, properties); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1499 | } |
Patrick Williams | f3a33b4 | 2021-10-06 12:37:26 -0500 | [diff] [blame] | 1500 | catch (const std::exception& e) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1501 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1502 | lg2::error("Unable to read host state: {ERROR}", "ERROR", e); |
Jason M. Bills | 6a6485a | 2020-07-24 14:07:07 -0700 | [diff] [blame] | 1503 | return; |
| 1504 | } |
| 1505 | if (properties.empty()) |
| 1506 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1507 | lg2::error("ERROR: Empty PropertiesChanged signal received"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1508 | return; |
| 1509 | } |
| 1510 | |
Jason M. Bills | 6a6485a | 2020-07-24 14:07:07 -0700 | [diff] [blame] | 1511 | // We only want to check for CurrentHostState |
| 1512 | if (properties.begin()->first != "CurrentHostState") |
| 1513 | { |
| 1514 | return; |
| 1515 | } |
| 1516 | std::string* currentHostState = |
| 1517 | std::get_if<std::string>(&(properties.begin()->second)); |
| 1518 | if (currentHostState == nullptr) |
| 1519 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1520 | lg2::error("{PROPERTY} property invalid", "PROPERTY", |
| 1521 | properties.begin()->first); |
Jason M. Bills | 6a6485a | 2020-07-24 14:07:07 -0700 | [diff] [blame] | 1522 | return; |
| 1523 | } |
| 1524 | |
| 1525 | if (*currentHostState == |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1526 | "xyz.openbmc_project.State.Host.HostState.Running") |
| 1527 | { |
| 1528 | pohCounterTimerStart(); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 1529 | // Clear the restart cause set for the next restart |
| 1530 | clearRestartCause(); |
Yong Li | 8d66021 | 2019-12-27 10:18:10 +0800 | [diff] [blame] | 1531 | sd_journal_send("MESSAGE=Host system DC power is on", |
| 1532 | "PRIORITY=%i", LOG_INFO, |
| 1533 | "REDFISH_MESSAGE_ID=%s", |
| 1534 | "OpenBMC.0.1.DCPowerOn", NULL); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1535 | } |
| 1536 | else |
| 1537 | { |
| 1538 | pohCounterTimer.cancel(); |
AppaRao Puli | 8f5cb6a | 2020-01-14 02:47:29 +0530 | [diff] [blame] | 1539 | // POST_COMPLETE GPIO event is not working in some platforms |
| 1540 | // when power state is changed to OFF. This resulted in |
| 1541 | // 'OperatingSystemState' to stay at 'Standby', even though |
| 1542 | // system is OFF. Set 'OperatingSystemState' to 'Inactive' |
| 1543 | // if HostState is trurned to OFF. |
| 1544 | osIface->set_property("OperatingSystemState", |
| 1545 | std::string("Inactive")); |
| 1546 | |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 1547 | // Set the restart cause set for this restart |
| 1548 | setRestartCause(); |
Rashmi RV | 89f6131 | 2020-01-22 15:41:50 +0530 | [diff] [blame] | 1549 | resetACBootProperty(); |
Yong Li | 8d66021 | 2019-12-27 10:18:10 +0800 | [diff] [blame] | 1550 | sd_journal_send("MESSAGE=Host system DC power is off", |
| 1551 | "PRIORITY=%i", LOG_INFO, |
| 1552 | "REDFISH_MESSAGE_ID=%s", |
| 1553 | "OpenBMC.0.1.DCPowerOff", NULL); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1554 | } |
| 1555 | }); |
| 1556 | } |
| 1557 | |
| 1558 | static void sioPowerGoodWatchdogTimerStart() |
| 1559 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1560 | lg2::info("SIO power good watchdog timer started"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1561 | sioPowerGoodWatchdogTimer.expires_after( |
Jason M. Bills | aeefe04 | 2021-09-08 14:56:11 -0700 | [diff] [blame] | 1562 | std::chrono::milliseconds(TimerMap["SioPowerGoodWatchdogMs"])); |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1563 | sioPowerGoodWatchdogTimer.async_wait([](const boost::system::error_code |
| 1564 | ec) { |
| 1565 | if (ec) |
| 1566 | { |
| 1567 | // operation_aborted is expected if timer is canceled before |
| 1568 | // completion. |
| 1569 | if (ec != boost::asio::error::operation_aborted) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1570 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1571 | lg2::error( |
| 1572 | "SIO power good watchdog async_wait failed: {ERROR_MSG}", |
| 1573 | "ERROR_MSG", ec.message()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1574 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1575 | lg2::info("SIO power good watchdog timer canceled"); |
| 1576 | return; |
| 1577 | } |
| 1578 | lg2::info("SIO power good watchdog timer completed"); |
| 1579 | sendPowerControlEvent(Event::sioPowerGoodWatchdogTimerExpired); |
| 1580 | }); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | static void powerStateOn(const Event event) |
| 1584 | { |
| 1585 | logEvent(__FUNCTION__, event); |
| 1586 | switch (event) |
| 1587 | { |
| 1588 | case Event::psPowerOKDeAssert: |
| 1589 | setPowerState(PowerState::off); |
| 1590 | // DC power is unexpectedly lost, beep |
| 1591 | beep(beepPowerFail); |
| 1592 | break; |
| 1593 | case Event::sioS5Assert: |
| 1594 | setPowerState(PowerState::transitionToOff); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 1595 | addRestartCause(RestartCause::softReset); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1596 | break; |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 1597 | #if USE_PLT_RST |
| 1598 | case Event::pltRstAssert: |
| 1599 | #else |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1600 | case Event::postCompleteDeAssert: |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 1601 | #endif |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1602 | setPowerState(PowerState::checkForWarmReset); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 1603 | addRestartCause(RestartCause::softReset); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1604 | warmResetCheckTimerStart(); |
| 1605 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1606 | case Event::powerButtonPressed: |
| 1607 | setPowerState(PowerState::gracefulTransitionToOff); |
| 1608 | gracefulPowerOffTimerStart(); |
| 1609 | break; |
| 1610 | case Event::powerOffRequest: |
| 1611 | setPowerState(PowerState::transitionToOff); |
| 1612 | forcePowerOff(); |
| 1613 | break; |
| 1614 | case Event::gracefulPowerOffRequest: |
| 1615 | setPowerState(PowerState::gracefulTransitionToOff); |
| 1616 | gracefulPowerOffTimerStart(); |
| 1617 | gracefulPowerOff(); |
| 1618 | break; |
| 1619 | case Event::powerCycleRequest: |
| 1620 | setPowerState(PowerState::transitionToCycleOff); |
| 1621 | forcePowerOff(); |
| 1622 | break; |
| 1623 | case Event::gracefulPowerCycleRequest: |
| 1624 | setPowerState(PowerState::gracefulTransitionToCycleOff); |
| 1625 | gracefulPowerOffTimerStart(); |
| 1626 | gracefulPowerOff(); |
| 1627 | break; |
| 1628 | case Event::resetRequest: |
| 1629 | reset(); |
| 1630 | break; |
| 1631 | default: |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1632 | lg2::info("No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1633 | break; |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | static void powerStateWaitForPSPowerOK(const Event event) |
| 1638 | { |
| 1639 | logEvent(__FUNCTION__, event); |
| 1640 | switch (event) |
| 1641 | { |
| 1642 | case Event::psPowerOKAssert: |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1643 | { |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1644 | // Cancel any GPIO assertions held during the transition |
| 1645 | gpioAssertTimer.cancel(); |
| 1646 | psPowerOKWatchdogTimer.cancel(); |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1647 | if (sioEnabled == true) |
| 1648 | { |
| 1649 | sioPowerGoodWatchdogTimerStart(); |
| 1650 | setPowerState(PowerState::waitForSIOPowerGood); |
| 1651 | } |
| 1652 | else |
| 1653 | { |
| 1654 | setPowerState(PowerState::on); |
| 1655 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1656 | break; |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1657 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1658 | case Event::psPowerOKWatchdogTimerExpired: |
Jason M. Bills | 273d789 | 2020-06-17 14:46:57 -0700 | [diff] [blame] | 1659 | setPowerState(PowerState::off); |
Jason M. Bills | 6c2ad36 | 2019-08-01 16:53:35 -0700 | [diff] [blame] | 1660 | psPowerOKFailedLog(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1661 | break; |
Vijay Khemka | 0eef6b6 | 2019-10-22 12:22:52 -0700 | [diff] [blame] | 1662 | case Event::sioPowerGoodAssert: |
| 1663 | psPowerOKWatchdogTimer.cancel(); |
| 1664 | setPowerState(PowerState::on); |
| 1665 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1666 | default: |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1667 | lg2::info("No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1668 | break; |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | static void powerStateWaitForSIOPowerGood(const Event event) |
| 1673 | { |
| 1674 | logEvent(__FUNCTION__, event); |
| 1675 | switch (event) |
| 1676 | { |
| 1677 | case Event::sioPowerGoodAssert: |
| 1678 | sioPowerGoodWatchdogTimer.cancel(); |
| 1679 | setPowerState(PowerState::on); |
| 1680 | break; |
| 1681 | case Event::sioPowerGoodWatchdogTimerExpired: |
Jason M. Bills | 273d789 | 2020-06-17 14:46:57 -0700 | [diff] [blame] | 1682 | setPowerState(PowerState::off); |
Jason M. Bills | 6c2ad36 | 2019-08-01 16:53:35 -0700 | [diff] [blame] | 1683 | systemPowerGoodFailedLog(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1684 | break; |
| 1685 | default: |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1686 | lg2::info("No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1687 | break; |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | static void powerStateOff(const Event event) |
| 1692 | { |
| 1693 | logEvent(__FUNCTION__, event); |
| 1694 | switch (event) |
| 1695 | { |
| 1696 | case Event::psPowerOKAssert: |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1697 | { |
| 1698 | if (sioEnabled == true) |
| 1699 | { |
Jason M. Bills | 7e27d3d | 2021-09-08 14:51:09 -0700 | [diff] [blame] | 1700 | sioPowerGoodWatchdogTimerStart(); |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1701 | setPowerState(PowerState::waitForSIOPowerGood); |
| 1702 | } |
| 1703 | else |
| 1704 | { |
| 1705 | setPowerState(PowerState::on); |
| 1706 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1707 | break; |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1708 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1709 | case Event::sioS5DeAssert: |
| 1710 | setPowerState(PowerState::waitForPSPowerOK); |
| 1711 | break; |
Jason M. Bills | 273d789 | 2020-06-17 14:46:57 -0700 | [diff] [blame] | 1712 | case Event::sioPowerGoodAssert: |
| 1713 | setPowerState(PowerState::on); |
| 1714 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1715 | case Event::powerButtonPressed: |
| 1716 | psPowerOKWatchdogTimerStart(); |
| 1717 | setPowerState(PowerState::waitForPSPowerOK); |
| 1718 | break; |
| 1719 | case Event::powerOnRequest: |
| 1720 | psPowerOKWatchdogTimerStart(); |
| 1721 | setPowerState(PowerState::waitForPSPowerOK); |
| 1722 | powerOn(); |
| 1723 | break; |
| 1724 | default: |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1725 | lg2::info("No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1726 | break; |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | static void powerStateTransitionToOff(const Event event) |
| 1731 | { |
| 1732 | logEvent(__FUNCTION__, event); |
| 1733 | switch (event) |
| 1734 | { |
| 1735 | case Event::psPowerOKDeAssert: |
| 1736 | // Cancel any GPIO assertions held during the transition |
| 1737 | gpioAssertTimer.cancel(); |
| 1738 | setPowerState(PowerState::off); |
| 1739 | break; |
| 1740 | default: |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1741 | lg2::info("No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1742 | break; |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | static void powerStateGracefulTransitionToOff(const Event event) |
| 1747 | { |
| 1748 | logEvent(__FUNCTION__, event); |
| 1749 | switch (event) |
| 1750 | { |
| 1751 | case Event::psPowerOKDeAssert: |
| 1752 | gracefulPowerOffTimer.cancel(); |
| 1753 | setPowerState(PowerState::off); |
| 1754 | break; |
| 1755 | case Event::gracefulPowerOffTimerExpired: |
| 1756 | setPowerState(PowerState::on); |
| 1757 | break; |
Jason M. Bills | 22e0bec | 2021-03-04 12:54:04 -0800 | [diff] [blame] | 1758 | case Event::powerOffRequest: |
| 1759 | gracefulPowerOffTimer.cancel(); |
| 1760 | setPowerState(PowerState::transitionToOff); |
| 1761 | forcePowerOff(); |
| 1762 | break; |
| 1763 | case Event::powerCycleRequest: |
| 1764 | gracefulPowerOffTimer.cancel(); |
| 1765 | setPowerState(PowerState::transitionToCycleOff); |
| 1766 | forcePowerOff(); |
| 1767 | break; |
| 1768 | case Event::resetRequest: |
| 1769 | gracefulPowerOffTimer.cancel(); |
| 1770 | setPowerState(PowerState::on); |
| 1771 | reset(); |
| 1772 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1773 | default: |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1774 | lg2::info("No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1775 | break; |
| 1776 | } |
| 1777 | } |
| 1778 | |
| 1779 | static void powerStateCycleOff(const Event event) |
| 1780 | { |
| 1781 | logEvent(__FUNCTION__, event); |
| 1782 | switch (event) |
| 1783 | { |
Jason M. Bills | 35aa665 | 2020-04-30 16:24:55 -0700 | [diff] [blame] | 1784 | case Event::psPowerOKAssert: |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1785 | { |
Jason M. Bills | 35aa665 | 2020-04-30 16:24:55 -0700 | [diff] [blame] | 1786 | powerCycleTimer.cancel(); |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1787 | if (sioEnabled == true) |
| 1788 | { |
Jason M. Bills | 7e27d3d | 2021-09-08 14:51:09 -0700 | [diff] [blame] | 1789 | sioPowerGoodWatchdogTimerStart(); |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1790 | setPowerState(PowerState::waitForSIOPowerGood); |
| 1791 | } |
| 1792 | else |
| 1793 | { |
| 1794 | setPowerState(PowerState::on); |
| 1795 | } |
Jason M. Bills | 35aa665 | 2020-04-30 16:24:55 -0700 | [diff] [blame] | 1796 | break; |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 1797 | } |
Jason M. Bills | 35aa665 | 2020-04-30 16:24:55 -0700 | [diff] [blame] | 1798 | case Event::sioS5DeAssert: |
| 1799 | powerCycleTimer.cancel(); |
| 1800 | setPowerState(PowerState::waitForPSPowerOK); |
| 1801 | break; |
| 1802 | case Event::powerButtonPressed: |
| 1803 | powerCycleTimer.cancel(); |
| 1804 | psPowerOKWatchdogTimerStart(); |
| 1805 | setPowerState(PowerState::waitForPSPowerOK); |
| 1806 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1807 | case Event::powerCycleTimerExpired: |
| 1808 | psPowerOKWatchdogTimerStart(); |
| 1809 | setPowerState(PowerState::waitForPSPowerOK); |
| 1810 | powerOn(); |
| 1811 | break; |
| 1812 | default: |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1813 | lg2::info("No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1814 | break; |
| 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | static void powerStateTransitionToCycleOff(const Event event) |
| 1819 | { |
| 1820 | logEvent(__FUNCTION__, event); |
| 1821 | switch (event) |
| 1822 | { |
| 1823 | case Event::psPowerOKDeAssert: |
| 1824 | // Cancel any GPIO assertions held during the transition |
| 1825 | gpioAssertTimer.cancel(); |
| 1826 | setPowerState(PowerState::cycleOff); |
| 1827 | powerCycleTimerStart(); |
| 1828 | break; |
| 1829 | default: |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1830 | lg2::info("No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1831 | break; |
| 1832 | } |
| 1833 | } |
| 1834 | |
| 1835 | static void powerStateGracefulTransitionToCycleOff(const Event event) |
| 1836 | { |
| 1837 | logEvent(__FUNCTION__, event); |
| 1838 | switch (event) |
| 1839 | { |
| 1840 | case Event::psPowerOKDeAssert: |
| 1841 | gracefulPowerOffTimer.cancel(); |
| 1842 | setPowerState(PowerState::cycleOff); |
| 1843 | powerCycleTimerStart(); |
| 1844 | break; |
| 1845 | case Event::gracefulPowerOffTimerExpired: |
| 1846 | setPowerState(PowerState::on); |
| 1847 | break; |
Jason M. Bills | 22e0bec | 2021-03-04 12:54:04 -0800 | [diff] [blame] | 1848 | case Event::powerOffRequest: |
| 1849 | gracefulPowerOffTimer.cancel(); |
| 1850 | setPowerState(PowerState::transitionToOff); |
| 1851 | forcePowerOff(); |
| 1852 | break; |
| 1853 | case Event::powerCycleRequest: |
| 1854 | gracefulPowerOffTimer.cancel(); |
| 1855 | setPowerState(PowerState::transitionToCycleOff); |
| 1856 | forcePowerOff(); |
| 1857 | break; |
| 1858 | case Event::resetRequest: |
| 1859 | gracefulPowerOffTimer.cancel(); |
| 1860 | setPowerState(PowerState::on); |
| 1861 | reset(); |
| 1862 | break; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1863 | default: |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1864 | lg2::info("No action taken."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1865 | break; |
| 1866 | } |
| 1867 | } |
| 1868 | |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1869 | static void powerStateCheckForWarmReset(const Event event) |
| 1870 | { |
| 1871 | logEvent(__FUNCTION__, event); |
| 1872 | switch (event) |
| 1873 | { |
| 1874 | case Event::sioS5Assert: |
| 1875 | warmResetCheckTimer.cancel(); |
| 1876 | setPowerState(PowerState::transitionToOff); |
| 1877 | break; |
| 1878 | case Event::warmResetDetected: |
| 1879 | setPowerState(PowerState::on); |
| 1880 | break; |
P.K. Lee | 344dae8 | 2019-11-27 16:35:05 +0800 | [diff] [blame] | 1881 | case Event::psPowerOKDeAssert: |
| 1882 | warmResetCheckTimer.cancel(); |
| 1883 | setPowerState(PowerState::off); |
| 1884 | // DC power is unexpectedly lost, beep |
| 1885 | beep(beepPowerFail); |
| 1886 | break; |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1887 | default: |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1888 | lg2::info("No action taken."); |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1889 | break; |
| 1890 | } |
| 1891 | } |
| 1892 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 1893 | static void psPowerOKHandler(bool state) |
| 1894 | { |
| 1895 | Event powerControlEvent = |
| 1896 | state ? Event::psPowerOKAssert : Event::psPowerOKDeAssert; |
| 1897 | sendPowerControlEvent(powerControlEvent); |
| 1898 | } |
| 1899 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 1900 | static void sioPowerGoodHandler(bool state) |
| 1901 | { |
| 1902 | Event powerControlEvent = |
| 1903 | state ? Event::sioPowerGoodAssert : Event::sioPowerGoodDeAssert; |
| 1904 | sendPowerControlEvent(powerControlEvent); |
| 1905 | } |
| 1906 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 1907 | static void sioOnControlHandler(bool state) |
| 1908 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1909 | lg2::info("SIO_ONCONTROL value changed: {VALUE}", "VALUE", |
| 1910 | static_cast<int>(state)); |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 1911 | } |
| 1912 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 1913 | static void sioS5Handler(bool state) |
| 1914 | { |
| 1915 | Event powerControlEvent = state ? Event::sioS5DeAssert : Event::sioS5Assert; |
| 1916 | sendPowerControlEvent(powerControlEvent); |
| 1917 | } |
| 1918 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 1919 | static void powerButtonHandler(bool state) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1920 | { |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 1921 | powerButtonIface->set_property("ButtonPressed", !state); |
| 1922 | if (!state) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1923 | { |
| 1924 | powerButtonPressLog(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1925 | if (!powerButtonMask) |
| 1926 | { |
| 1927 | sendPowerControlEvent(Event::powerButtonPressed); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 1928 | addRestartCause(RestartCause::powerButton); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1929 | } |
| 1930 | else |
| 1931 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1932 | lg2::info("power button press masked"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1933 | } |
| 1934 | } |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 1935 | } |
| 1936 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 1937 | static void resetButtonHandler(bool state) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1938 | { |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 1939 | resetButtonIface->set_property("ButtonPressed", !state); |
| 1940 | if (!state) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1941 | { |
| 1942 | resetButtonPressLog(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1943 | if (!resetButtonMask) |
| 1944 | { |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 1945 | sendPowerControlEvent(Event::resetButtonPressed); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 1946 | addRestartCause(RestartCause::resetButton); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1947 | } |
| 1948 | else |
| 1949 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1950 | lg2::info("reset button press masked"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1951 | } |
| 1952 | } |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 1953 | } |
| 1954 | |
Vijay Khemka | 04175c2 | 2020-10-09 14:28:11 -0700 | [diff] [blame] | 1955 | #ifdef CHASSIS_SYSTEM_RESET |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 1956 | static constexpr auto systemdBusname = "org.freedesktop.systemd1"; |
| 1957 | static constexpr auto systemdPath = "/org/freedesktop/systemd1"; |
| 1958 | static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager"; |
| 1959 | static constexpr auto systemTargetName = "chassis-system-reset.target"; |
| 1960 | |
| 1961 | void systemReset() |
| 1962 | { |
| 1963 | conn->async_method_call( |
| 1964 | [](boost::system::error_code ec) { |
| 1965 | if (ec) |
| 1966 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1967 | lg2::error("Failed to call chassis system reset: {ERR}", "ERR", |
| 1968 | ec.message()); |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 1969 | } |
| 1970 | }, |
| 1971 | systemdBusname, systemdPath, systemdInterface, "StartUnit", |
| 1972 | systemTargetName, "replace"); |
| 1973 | } |
Vijay Khemka | 04175c2 | 2020-10-09 14:28:11 -0700 | [diff] [blame] | 1974 | #endif |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 1975 | |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 1976 | static void nmiSetEnableProperty(bool value) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1977 | { |
| 1978 | conn->async_method_call( |
| 1979 | [](boost::system::error_code ec) { |
| 1980 | if (ec) |
| 1981 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1982 | lg2::error("failed to set NMI source"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1983 | } |
| 1984 | }, |
Chen Yugang | 303bd58 | 2019-11-01 08:45:06 +0800 | [diff] [blame] | 1985 | "xyz.openbmc_project.Settings", |
| 1986 | "/xyz/openbmc_project/Chassis/Control/NMISource", |
| 1987 | "org.freedesktop.DBus.Properties", "Set", |
| 1988 | "xyz.openbmc_project.Chassis.Control.NMISource", "Enabled", |
| 1989 | std::variant<bool>{value}); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | static void nmiReset(void) |
| 1993 | { |
| 1994 | static constexpr const uint8_t value = 1; |
| 1995 | const static constexpr int nmiOutPulseTimeMs = 200; |
| 1996 | |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1997 | lg2::info("NMI out action"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 1998 | nmiOutLine.set_value(value); |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 1999 | lg2::info("{GPIO_NAME} set to {GPIO_VALUE}", "GPIO_NAME", |
| 2000 | nmiOutConfig.lineName, "GPIO_VALUE", value); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2001 | gpioAssertTimer.expires_after(std::chrono::milliseconds(nmiOutPulseTimeMs)); |
| 2002 | gpioAssertTimer.async_wait([](const boost::system::error_code ec) { |
| 2003 | // restore the NMI_OUT GPIO line back to the opposite value |
| 2004 | nmiOutLine.set_value(!value); |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2005 | lg2::info("{GPIO_NAME} released", "GPIO_NAME", nmiOutConfig.lineName); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2006 | if (ec) |
| 2007 | { |
| 2008 | // operation_aborted is expected if timer is canceled before |
| 2009 | // completion. |
| 2010 | if (ec != boost::asio::error::operation_aborted) |
| 2011 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2012 | lg2::error("{GPIO_NAME} async_wait failed: {ERROR_MSG}", |
| 2013 | "GPIO_NAME", nmiOutConfig.lineName, "ERROR_MSG", |
| 2014 | ec.message()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2015 | } |
| 2016 | } |
| 2017 | }); |
| 2018 | // log to redfish |
| 2019 | nmiDiagIntLog(); |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2020 | lg2::info("NMI out action completed"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2021 | // reset Enable Property |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2022 | nmiSetEnableProperty(false); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | static void nmiSourcePropertyMonitor(void) |
| 2026 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2027 | lg2::info("NMI Source Property Monitor"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2028 | |
| 2029 | static std::unique_ptr<sdbusplus::bus::match::match> nmiSourceMatch = |
| 2030 | std::make_unique<sdbusplus::bus::match::match>( |
| 2031 | *conn, |
| 2032 | "type='signal',interface='org.freedesktop.DBus.Properties'," |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2033 | "member='PropertiesChanged'," |
| 2034 | "arg0namespace='xyz.openbmc_project.Chassis.Control.NMISource'", |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2035 | [](sdbusplus::message::message& msg) { |
| 2036 | std::string interfaceName; |
| 2037 | boost::container::flat_map<std::string, |
| 2038 | std::variant<bool, std::string>> |
| 2039 | propertiesChanged; |
| 2040 | std::string state; |
| 2041 | bool value = true; |
| 2042 | try |
| 2043 | { |
| 2044 | msg.read(interfaceName, propertiesChanged); |
| 2045 | if (propertiesChanged.begin()->first == "Enabled") |
| 2046 | { |
| 2047 | value = |
| 2048 | std::get<bool>(propertiesChanged.begin()->second); |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2049 | lg2::info( |
| 2050 | "NMI Enabled propertiesChanged value: {VALUE}", |
| 2051 | "VALUE", value); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2052 | nmiEnabled = value; |
| 2053 | if (nmiEnabled) |
| 2054 | { |
| 2055 | nmiReset(); |
| 2056 | } |
| 2057 | } |
| 2058 | } |
Patrick Williams | f3a33b4 | 2021-10-06 12:37:26 -0500 | [diff] [blame] | 2059 | catch (const std::exception& e) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2060 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2061 | lg2::error("Unable to read NMI source: {ERROR}", "ERROR", |
| 2062 | e); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2063 | return; |
| 2064 | } |
| 2065 | }); |
| 2066 | } |
| 2067 | |
| 2068 | static void setNmiSource() |
| 2069 | { |
| 2070 | conn->async_method_call( |
| 2071 | [](boost::system::error_code ec) { |
| 2072 | if (ec) |
| 2073 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2074 | lg2::error("failed to set NMI source"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2075 | } |
| 2076 | }, |
Chen Yugang | 303bd58 | 2019-11-01 08:45:06 +0800 | [diff] [blame] | 2077 | "xyz.openbmc_project.Settings", |
| 2078 | "/xyz/openbmc_project/Chassis/Control/NMISource", |
| 2079 | "org.freedesktop.DBus.Properties", "Set", |
| 2080 | "xyz.openbmc_project.Chassis.Control.NMISource", "BMCSource", |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 2081 | std::variant<std::string>{ |
| 2082 | "xyz.openbmc_project.Chassis.Control.NMISource.BMCSourceSignal.FpBtn"}); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2083 | // set Enable Property |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2084 | nmiSetEnableProperty(true); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2085 | } |
| 2086 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2087 | static void nmiButtonHandler(bool state) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2088 | { |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2089 | nmiButtonIface->set_property("ButtonPressed", !state); |
| 2090 | if (!state) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2091 | { |
| 2092 | nmiButtonPressLog(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2093 | if (nmiButtonMasked) |
| 2094 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2095 | lg2::info("NMI button press masked"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2096 | } |
| 2097 | else |
| 2098 | { |
| 2099 | setNmiSource(); |
| 2100 | } |
| 2101 | } |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2102 | } |
| 2103 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2104 | static void idButtonHandler(bool state) |
| 2105 | { |
| 2106 | idButtonIface->set_property("ButtonPressed", !state); |
| 2107 | } |
| 2108 | |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2109 | static void pltRstHandler(bool pltRst) |
| 2110 | { |
| 2111 | if (pltRst) |
| 2112 | { |
| 2113 | sendPowerControlEvent(Event::pltRstDeAssert); |
| 2114 | } |
| 2115 | else |
| 2116 | { |
| 2117 | sendPowerControlEvent(Event::pltRstAssert); |
| 2118 | } |
| 2119 | } |
| 2120 | |
Jason M. Bills | 9fd8ac2 | 2021-10-21 15:14:17 -0700 | [diff] [blame] | 2121 | [[maybe_unused]] static void hostMiscHandler(sdbusplus::message::message& msg) |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2122 | { |
| 2123 | std::string interfaceName; |
| 2124 | boost::container::flat_map<std::string, std::variant<bool>> |
| 2125 | propertiesChanged; |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2126 | try |
| 2127 | { |
| 2128 | msg.read(interfaceName, propertiesChanged); |
| 2129 | } |
Patrick Williams | f3a33b4 | 2021-10-06 12:37:26 -0500 | [diff] [blame] | 2130 | catch (const std::exception& e) |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2131 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2132 | lg2::error("Unable to read Host Misc status: {ERROR}", "ERROR", e); |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2133 | return; |
| 2134 | } |
| 2135 | if (propertiesChanged.empty()) |
| 2136 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2137 | lg2::error("ERROR: Empty Host.Misc PropertiesChanged signal received"); |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2138 | return; |
| 2139 | } |
| 2140 | |
| 2141 | for (auto& [property, value] : propertiesChanged) |
| 2142 | { |
| 2143 | if (property == "ESpiPlatformReset") |
| 2144 | { |
| 2145 | bool* pltRst = std::get_if<bool>(&value); |
| 2146 | if (pltRst == nullptr) |
| 2147 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2148 | lg2::error("{PROPERTY} property invalid", "PROPERTY", property); |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2149 | return; |
| 2150 | } |
| 2151 | pltRstHandler(*pltRst); |
| 2152 | } |
| 2153 | } |
| 2154 | } |
| 2155 | |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2156 | static void postCompleteHandler(bool state) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2157 | { |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2158 | if (!state) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2159 | { |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 2160 | sendPowerControlEvent(Event::postCompleteAssert); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2161 | osIface->set_property("OperatingSystemState", std::string("Standby")); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2162 | } |
| 2163 | else |
| 2164 | { |
Jason M. Bills | e9a9e2d | 2019-08-08 14:01:19 -0700 | [diff] [blame] | 2165 | sendPowerControlEvent(Event::postCompleteDeAssert); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2166 | osIface->set_property("OperatingSystemState", std::string("Inactive")); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2167 | } |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2168 | } |
| 2169 | |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2170 | static int loadConfigValues() |
| 2171 | { |
| 2172 | const std::string configFilePath = |
| 2173 | "/usr/share/x86-power-control/power-config-host" + power_control::node + |
| 2174 | ".json"; |
| 2175 | std::ifstream configFile(configFilePath.c_str()); |
| 2176 | if (!configFile.is_open()) |
| 2177 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2178 | lg2::error("loadConfigValues: Cannot open config path \'{PATH}\'", |
| 2179 | "PATH", configFilePath); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2180 | return -1; |
| 2181 | } |
Zev Weiss | 1aa08b2 | 2021-09-15 17:06:20 -0500 | [diff] [blame] | 2182 | auto jsonData = nlohmann::json::parse(configFile, nullptr, true, true); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2183 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2184 | if (jsonData.is_discarded()) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2185 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2186 | lg2::error("Power config readings JSON parser failure"); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2187 | return -1; |
| 2188 | } |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2189 | auto gpios = jsonData["gpio_configs"]; |
| 2190 | auto timers = jsonData["timing_configs"]; |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2191 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2192 | ConfigData* tempGpioData; |
| 2193 | |
| 2194 | for (nlohmann::json& gpioConfig : gpios) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2195 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2196 | if (!gpioConfig.contains("Name")) |
| 2197 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2198 | lg2::error("The 'Name' field must be defined in Json file"); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2199 | return -1; |
| 2200 | } |
| 2201 | |
| 2202 | // Iterate through the powersignal map to check if the gpio json config |
| 2203 | // entry is valid |
| 2204 | std::string gpioName = gpioConfig["Name"]; |
| 2205 | auto signalMapIter = powerSignalMap.find(gpioName); |
| 2206 | if (signalMapIter == powerSignalMap.end()) |
| 2207 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2208 | lg2::error( |
| 2209 | "{GPIO_NAME} is not a recognized power-control signal name", |
| 2210 | "GPIO_NAME", gpioName); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2211 | return -1; |
| 2212 | } |
| 2213 | |
| 2214 | // assign the power signal name to the corresponding structure reference |
| 2215 | // from map then fillup the structure with coressponding json config |
| 2216 | // value |
| 2217 | tempGpioData = signalMapIter->second; |
| 2218 | tempGpioData->name = gpioName; |
| 2219 | |
| 2220 | if (!gpioConfig.contains("Type")) |
| 2221 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2222 | lg2::error("The \'Type\' field must be defined in Json file"); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2223 | return -1; |
| 2224 | } |
| 2225 | |
| 2226 | std::string signalType = gpioConfig["Type"]; |
| 2227 | if (signalType == "GPIO") |
| 2228 | { |
| 2229 | tempGpioData->type = ConfigType::GPIO; |
| 2230 | } |
| 2231 | else if (signalType == "DBUS") |
| 2232 | { |
| 2233 | tempGpioData->type = ConfigType::DBUS; |
| 2234 | } |
| 2235 | else |
| 2236 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2237 | lg2::error("{TYPE} is not a recognized power-control signal type", |
| 2238 | "TYPE", signalType); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2239 | return -1; |
| 2240 | } |
| 2241 | |
| 2242 | if (tempGpioData->type == ConfigType::GPIO) |
| 2243 | { |
| 2244 | if (gpioConfig.contains("LineName")) |
| 2245 | { |
| 2246 | tempGpioData->lineName = gpioConfig["LineName"]; |
| 2247 | } |
| 2248 | else |
| 2249 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2250 | lg2::error( |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 2251 | "The \'LineName\' field must be defined for GPIO configuration"); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2252 | return -1; |
| 2253 | } |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 2254 | if (gpioConfig.contains("Polarity")) |
| 2255 | { |
| 2256 | std::string polarity = gpioConfig["Polarity"]; |
| 2257 | if (polarity == "ActiveLow") |
| 2258 | { |
| 2259 | tempGpioData->polarity = false; |
| 2260 | } |
| 2261 | else if (polarity == "ActiveHigh") |
| 2262 | { |
| 2263 | tempGpioData->polarity = true; |
| 2264 | } |
| 2265 | else |
| 2266 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2267 | lg2::error( |
| 2268 | "Polarity defined but not properly setup. Please only ActiveHigh or ActiveLow. Currently set to {POLARITY}", |
| 2269 | "POLARITY", polarity); |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 2270 | return -1; |
| 2271 | } |
| 2272 | } |
| 2273 | else |
| 2274 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2275 | lg2::error("Polarity field not found for {GPIO_NAME}", |
| 2276 | "GPIO_NAME", tempGpioData->lineName); |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 2277 | return -1; |
| 2278 | } |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2279 | } |
| 2280 | else |
| 2281 | { |
| 2282 | // if dbus based gpio config is defined read and update the dbus |
| 2283 | // params corresponding to the gpio config instance |
| 2284 | for (auto& [key, dbusParamName] : dbusParams) |
| 2285 | { |
Logananth Sundararaj | a430804 | 2021-10-20 11:52:05 +0530 | [diff] [blame] | 2286 | if (!gpioConfig.contains(dbusParamName)) |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2287 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2288 | lg2::error( |
| 2289 | "The {DBUS_NAME} field must be defined for Dbus configuration ", |
| 2290 | "DBUS_NAME", dbusParamName); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2291 | return -1; |
| 2292 | } |
| 2293 | } |
Logananth Sundararaj | a430804 | 2021-10-20 11:52:05 +0530 | [diff] [blame] | 2294 | tempGpioData->dbusName = |
| 2295 | gpioConfig[dbusParams[DbusConfigType::name]]; |
| 2296 | tempGpioData->path = gpioConfig[dbusParams[DbusConfigType::path]]; |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2297 | tempGpioData->interface = |
Logananth Sundararaj | a430804 | 2021-10-20 11:52:05 +0530 | [diff] [blame] | 2298 | gpioConfig[dbusParams[DbusConfigType::interface]]; |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2299 | tempGpioData->lineName = |
Logananth Sundararaj | a430804 | 2021-10-20 11:52:05 +0530 | [diff] [blame] | 2300 | gpioConfig[dbusParams[DbusConfigType::property]]; |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2301 | } |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2302 | } |
| 2303 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2304 | // read and store the timer values from json config to Timer Map |
| 2305 | for (auto& [key, timerValue] : TimerMap) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2306 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2307 | if (timers.contains(key.c_str())) |
| 2308 | { |
| 2309 | timerValue = timers[key.c_str()]; |
| 2310 | } |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2311 | } |
| 2312 | |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2313 | return 0; |
| 2314 | } |
Zev Weiss | a8f116a | 2021-09-01 21:08:30 -0500 | [diff] [blame] | 2315 | |
| 2316 | static bool getDbusMsgGPIOState(sdbusplus::message::message& msg, |
| 2317 | const std::string& lineName, bool& value) |
| 2318 | { |
| 2319 | std::string thresholdInterface; |
| 2320 | std::string event; |
| 2321 | boost::container::flat_map<std::string, std::variant<bool>> |
| 2322 | propertiesChanged; |
| 2323 | try |
| 2324 | { |
| 2325 | msg.read(thresholdInterface, propertiesChanged); |
| 2326 | if (propertiesChanged.empty()) |
| 2327 | { |
| 2328 | return false; |
| 2329 | } |
| 2330 | |
| 2331 | event = propertiesChanged.begin()->first; |
| 2332 | if (event.empty() || event != lineName) |
| 2333 | { |
| 2334 | return false; |
| 2335 | } |
| 2336 | |
| 2337 | value = std::get<bool>(propertiesChanged.begin()->second); |
| 2338 | return true; |
| 2339 | } |
Patrick Williams | f3a33b4 | 2021-10-06 12:37:26 -0500 | [diff] [blame] | 2340 | catch (const std::exception& e) |
Zev Weiss | a8f116a | 2021-09-01 21:08:30 -0500 | [diff] [blame] | 2341 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2342 | lg2::error( |
| 2343 | "exception while reading dbus property \'{DBUS_NAME}\': {ERROR}", |
| 2344 | "DBUS_NAME", lineName, "ERROR", e); |
Zev Weiss | a8f116a | 2021-09-01 21:08:30 -0500 | [diff] [blame] | 2345 | return false; |
| 2346 | } |
| 2347 | } |
| 2348 | |
| 2349 | static sdbusplus::bus::match::match |
| 2350 | dbusGPIOMatcher(const ConfigData& cfg, std::function<void(bool)> onMatch) |
| 2351 | { |
| 2352 | auto pulseEventMatcherCallback = |
| 2353 | [&cfg, onMatch](sdbusplus::message::message& msg) { |
| 2354 | bool value = false; |
| 2355 | if (!getDbusMsgGPIOState(msg, cfg.lineName, value)) |
| 2356 | { |
| 2357 | return; |
| 2358 | } |
| 2359 | onMatch(value); |
| 2360 | }; |
| 2361 | |
| 2362 | return sdbusplus::bus::match::match( |
| 2363 | static_cast<sdbusplus::bus::bus&>(*conn), |
| 2364 | "type='signal',interface='org.freedesktop.DBus.Properties',member='" |
| 2365 | "PropertiesChanged',arg0='" + |
Logananth Sundararaj | 85e111e | 2021-11-11 13:13:13 +0530 | [diff] [blame] | 2366 | cfg.interface + "'", |
Zev Weiss | a8f116a | 2021-09-01 21:08:30 -0500 | [diff] [blame] | 2367 | std::move(pulseEventMatcherCallback)); |
| 2368 | } |
| 2369 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2370 | int getProperty(ConfigData& configData) |
| 2371 | { |
| 2372 | auto method = conn->new_method_call( |
| 2373 | configData.dbusName.c_str(), configData.path.c_str(), |
| 2374 | "org.freedesktop.DBus.Properties", "Get"); |
| 2375 | method.append(configData.interface.c_str(), configData.lineName.c_str()); |
| 2376 | |
| 2377 | auto reply = conn->call(method); |
| 2378 | if (reply.is_method_error()) |
| 2379 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2380 | lg2::error( |
| 2381 | "Error reading {PROPERTY} D-Bus property on interface {INTERFACE} and path {PATH}", |
| 2382 | "PROPERTY", configData.lineName, "INTERFACE", configData.interface, |
| 2383 | "PATH", configData.path); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2384 | return -1; |
| 2385 | } |
Logananth Sundararaj | 85e111e | 2021-11-11 13:13:13 +0530 | [diff] [blame] | 2386 | std::variant<bool> resp; |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2387 | reply.read(resp); |
Logananth Sundararaj | 85e111e | 2021-11-11 13:13:13 +0530 | [diff] [blame] | 2388 | auto respValue = std::get_if<bool>(&resp); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2389 | if (!respValue) |
| 2390 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2391 | lg2::error("Error: {PROPERTY} D-Bus property is not the expected type", |
| 2392 | "PROPERTY", configData.lineName); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2393 | return -1; |
| 2394 | } |
| 2395 | return (*respValue); |
| 2396 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2397 | } // namespace power_control |
| 2398 | |
| 2399 | int main(int argc, char* argv[]) |
| 2400 | { |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2401 | using namespace power_control; |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2402 | |
| 2403 | if (argc > 1) |
| 2404 | { |
| 2405 | node = argv[1]; |
| 2406 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2407 | lg2::info("Start Chassis power control service for host : {NODE}", "NODE", |
| 2408 | node); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2409 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2410 | conn = std::make_shared<sdbusplus::asio::connection>(io); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2411 | |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2412 | // Load GPIO's through json config file |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2413 | if (loadConfigValues() == -1) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2414 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2415 | lg2::error("Host{NODE}: Error in Parsing...", "NODE", node); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2416 | } |
Naveen Moses | ec972d8 | 2021-07-16 21:19:23 +0530 | [diff] [blame] | 2417 | /* Currently for single host based systems additional busname is added |
| 2418 | with "0" at the end of the name ex : xyz.openbmc_project.State.Host0. |
| 2419 | Going forward for single hosts the old bus name without zero numbering |
| 2420 | will be removed when all other applications adapted to the |
| 2421 | bus name with zero numbering (xyz.openbmc_project.State.Host0). */ |
| 2422 | |
| 2423 | if (node == "0") |
| 2424 | { |
| 2425 | // Request all the dbus names |
| 2426 | conn->request_name(hostDbusName.c_str()); |
| 2427 | conn->request_name(chassisDbusName.c_str()); |
| 2428 | conn->request_name(osDbusName.c_str()); |
| 2429 | conn->request_name(buttonDbusName.c_str()); |
| 2430 | conn->request_name(nmiDbusName.c_str()); |
| 2431 | conn->request_name(rstCauseDbusName.c_str()); |
| 2432 | } |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2433 | |
Zev Weiss | c4005bd | 2021-09-01 22:30:23 -0500 | [diff] [blame] | 2434 | hostDbusName += node; |
| 2435 | chassisDbusName += node; |
| 2436 | osDbusName += node; |
| 2437 | buttonDbusName += node; |
| 2438 | nmiDbusName += node; |
| 2439 | rstCauseDbusName += node; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2440 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2441 | // Request all the dbus names |
| 2442 | conn->request_name(hostDbusName.c_str()); |
| 2443 | conn->request_name(chassisDbusName.c_str()); |
| 2444 | conn->request_name(osDbusName.c_str()); |
| 2445 | conn->request_name(buttonDbusName.c_str()); |
| 2446 | conn->request_name(nmiDbusName.c_str()); |
| 2447 | conn->request_name(rstCauseDbusName.c_str()); |
| 2448 | |
| 2449 | if (sioPwrGoodConfig.lineName.empty() || |
| 2450 | sioOnControlConfig.lineName.empty() || sioS5Config.lineName.empty()) |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 2451 | { |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2452 | sioEnabled = false; |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2453 | lg2::info("SIO control GPIOs not defined, disable SIO support."); |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 2454 | } |
| 2455 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2456 | // Request PS_PWROK GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2457 | if (powerOkConfig.type == ConfigType::GPIO) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2458 | { |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2459 | if (!requestGPIOEvents(powerOkConfig.lineName, psPowerOKHandler, |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2460 | psPowerOKLine, psPowerOKEvent)) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2461 | { |
| 2462 | return -1; |
| 2463 | } |
| 2464 | } |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2465 | else if (powerOkConfig.type == ConfigType::DBUS) |
| 2466 | { |
| 2467 | |
| 2468 | static sdbusplus::bus::match::match powerOkEventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2469 | power_control::dbusGPIOMatcher(powerOkConfig, psPowerOKHandler); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2470 | } |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2471 | else |
| 2472 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2473 | lg2::error("PowerOk name should be configured from json config file"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2474 | return -1; |
| 2475 | } |
| 2476 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2477 | if (sioEnabled == true) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2478 | { |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 2479 | // Request SIO_POWER_GOOD GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2480 | if (sioPwrGoodConfig.type == ConfigType::GPIO) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2481 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2482 | if (!requestGPIOEvents(sioPwrGoodConfig.lineName, |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2483 | sioPowerGoodHandler, sioPowerGoodLine, |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2484 | sioPowerGoodEvent)) |
| 2485 | { |
| 2486 | return -1; |
| 2487 | } |
| 2488 | } |
| 2489 | else if (sioPwrGoodConfig.type == ConfigType::DBUS) |
| 2490 | { |
| 2491 | static sdbusplus::bus::match::match sioPwrGoodEventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2492 | power_control::dbusGPIOMatcher(sioPwrGoodConfig, |
| 2493 | sioPowerGoodHandler); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2494 | } |
| 2495 | else |
| 2496 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2497 | lg2::error( |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2498 | "sioPwrGood name should be configured from json config file"); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2499 | return -1; |
| 2500 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2501 | |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 2502 | // Request SIO_ONCONTROL GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2503 | if (sioOnControlConfig.type == ConfigType::GPIO) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2504 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2505 | if (!requestGPIOEvents(sioOnControlConfig.lineName, |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2506 | sioOnControlHandler, sioOnControlLine, |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2507 | sioOnControlEvent)) |
| 2508 | { |
| 2509 | return -1; |
| 2510 | } |
| 2511 | } |
| 2512 | else if (sioOnControlConfig.type == ConfigType::DBUS) |
| 2513 | { |
| 2514 | static sdbusplus::bus::match::match sioOnControlEventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2515 | power_control::dbusGPIOMatcher(sioOnControlConfig, |
| 2516 | sioOnControlHandler); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2517 | } |
| 2518 | else |
| 2519 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2520 | lg2::error( |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 2521 | "sioOnControl name should be configured from jsonconfig file\n"); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2522 | return -1; |
| 2523 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2524 | |
Priyatharshan P | 19c47a3 | 2020-08-12 18:16:43 +0530 | [diff] [blame] | 2525 | // Request SIO_S5 GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2526 | if (sioS5Config.type == ConfigType::GPIO) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2527 | { |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2528 | if (!requestGPIOEvents(sioS5Config.lineName, sioS5Handler, |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2529 | sioS5Line, sioS5Event)) |
| 2530 | { |
| 2531 | return -1; |
| 2532 | } |
| 2533 | } |
| 2534 | else if (sioS5Config.type == ConfigType::DBUS) |
| 2535 | { |
| 2536 | static sdbusplus::bus::match::match sioS5EventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2537 | power_control::dbusGPIOMatcher(sioS5Config, sioS5Handler); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2538 | } |
| 2539 | else |
| 2540 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2541 | lg2::error("sioS5 name should be configured from json config file"); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2542 | return -1; |
| 2543 | } |
| 2544 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2545 | |
| 2546 | // Request POWER_BUTTON GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2547 | if (powerButtonConfig.type == ConfigType::GPIO) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2548 | { |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2549 | if (!requestGPIOEvents(powerButtonConfig.lineName, powerButtonHandler, |
| 2550 | powerButtonLine, powerButtonEvent)) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2551 | { |
| 2552 | return -1; |
| 2553 | } |
| 2554 | } |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2555 | else if (powerButtonConfig.type == ConfigType::DBUS) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2556 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2557 | static sdbusplus::bus::match::match powerButtonEventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2558 | power_control::dbusGPIOMatcher(powerButtonConfig, |
| 2559 | powerButtonHandler); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2560 | } |
| 2561 | |
| 2562 | // Request RESET_BUTTON GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2563 | if (resetButtonConfig.type == ConfigType::GPIO) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2564 | { |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2565 | if (!requestGPIOEvents(resetButtonConfig.lineName, resetButtonHandler, |
| 2566 | resetButtonLine, resetButtonEvent)) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2567 | { |
| 2568 | return -1; |
| 2569 | } |
| 2570 | } |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2571 | else if (resetButtonConfig.type == ConfigType::DBUS) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2572 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2573 | static sdbusplus::bus::match::match resetButtonEventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2574 | power_control::dbusGPIOMatcher(resetButtonConfig, |
| 2575 | resetButtonHandler); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | // Request NMI_BUTTON GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2579 | if (nmiButtonConfig.type == ConfigType::GPIO) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2580 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2581 | if (!nmiButtonConfig.lineName.empty()) |
| 2582 | { |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2583 | requestGPIOEvents(nmiButtonConfig.lineName, nmiButtonHandler, |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2584 | nmiButtonLine, nmiButtonEvent); |
| 2585 | } |
| 2586 | } |
| 2587 | else if (nmiButtonConfig.type == ConfigType::DBUS) |
| 2588 | { |
| 2589 | static sdbusplus::bus::match::match nmiButtonEventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2590 | power_control::dbusGPIOMatcher(nmiButtonConfig, nmiButtonHandler); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2591 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2592 | |
| 2593 | // Request ID_BUTTON GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2594 | if (idButtonConfig.type == ConfigType::GPIO) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2595 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2596 | if (!idButtonConfig.lineName.empty()) |
| 2597 | { |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2598 | requestGPIOEvents(idButtonConfig.lineName, idButtonHandler, |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2599 | idButtonLine, idButtonEvent); |
| 2600 | } |
| 2601 | } |
| 2602 | else if (idButtonConfig.type == ConfigType::DBUS) |
| 2603 | { |
| 2604 | static sdbusplus::bus::match::match idButtonEventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2605 | power_control::dbusGPIOMatcher(idButtonConfig, idButtonHandler); |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2606 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2607 | |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2608 | #ifdef USE_PLT_RST |
| 2609 | sdbusplus::bus::match::match pltRstMatch( |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2610 | *conn, |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2611 | "type='signal',interface='org.freedesktop.DBus.Properties',member='" |
| 2612 | "PropertiesChanged',arg0='xyz.openbmc_project.State.Host.Misc'", |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2613 | hostMiscHandler); |
Jason M. Bills | fb95733 | 2021-01-28 13:18:46 -0800 | [diff] [blame] | 2614 | #endif |
| 2615 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2616 | // Request POST_COMPLETE GPIO events |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2617 | if (postCompleteConfig.type == ConfigType::GPIO) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2618 | { |
Zev Weiss | 676ef2c | 2021-09-02 21:54:02 -0500 | [diff] [blame] | 2619 | if (!requestGPIOEvents(postCompleteConfig.lineName, postCompleteHandler, |
| 2620 | postCompleteLine, postCompleteEvent)) |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2621 | { |
| 2622 | return -1; |
| 2623 | } |
| 2624 | } |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2625 | else if (postCompleteConfig.type == ConfigType::DBUS) |
| 2626 | { |
| 2627 | static sdbusplus::bus::match::match postCompleteEventMonitor = |
Zev Weiss | 584aa13 | 2021-09-02 19:21:52 -0500 | [diff] [blame] | 2628 | power_control::dbusGPIOMatcher(postCompleteConfig, |
| 2629 | postCompleteHandler); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2630 | } |
Priyatharshan P | e4d7f2b | 2020-06-22 22:41:42 +0530 | [diff] [blame] | 2631 | else |
| 2632 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2633 | lg2::error( |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2634 | "postComplete name should be configured from json config file"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2635 | return -1; |
| 2636 | } |
| 2637 | |
| 2638 | // initialize NMI_OUT GPIO. |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2639 | if (!nmiOutConfig.lineName.empty()) |
| 2640 | { |
| 2641 | setGPIOOutput(nmiOutConfig.lineName, 0, nmiOutLine); |
| 2642 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2643 | |
Vijay Khemka | 0dc7d4c | 2019-10-22 12:30:17 -0700 | [diff] [blame] | 2644 | // Initialize POWER_OUT and RESET_OUT GPIO. |
| 2645 | gpiod::line line; |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2646 | if (!powerOutConfig.lineName.empty()) |
Vijay Khemka | 0dc7d4c | 2019-10-22 12:30:17 -0700 | [diff] [blame] | 2647 | { |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 2648 | if (!setGPIOOutput(powerOutConfig.lineName, !powerOutConfig.polarity, |
| 2649 | line)) |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2650 | { |
| 2651 | return -1; |
| 2652 | } |
| 2653 | } |
| 2654 | else |
| 2655 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2656 | lg2::error("powerOut name should be configured from json config file"); |
Vijay Khemka | 0dc7d4c | 2019-10-22 12:30:17 -0700 | [diff] [blame] | 2657 | return -1; |
| 2658 | } |
| 2659 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2660 | if (!resetOutConfig.lineName.empty()) |
Vijay Khemka | 0dc7d4c | 2019-10-22 12:30:17 -0700 | [diff] [blame] | 2661 | { |
Jean-Marie Verdun | 50937e7 | 2021-08-31 09:15:49 -0700 | [diff] [blame] | 2662 | if (!setGPIOOutput(resetOutConfig.lineName, !resetOutConfig.polarity, |
| 2663 | line)) |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2664 | { |
| 2665 | return -1; |
| 2666 | } |
| 2667 | } |
| 2668 | else |
| 2669 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2670 | lg2::error("ResetOut name should be configured from json config file"); |
Vijay Khemka | 0dc7d4c | 2019-10-22 12:30:17 -0700 | [diff] [blame] | 2671 | return -1; |
| 2672 | } |
Vijay Khemka | 0dc7d4c | 2019-10-22 12:30:17 -0700 | [diff] [blame] | 2673 | // Release line |
| 2674 | line.reset(); |
| 2675 | |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2676 | // Initialize the power state |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2677 | powerState = PowerState::off; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2678 | // Check power good |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2679 | |
| 2680 | if (powerOkConfig.type == ConfigType::GPIO) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2681 | { |
Jean-Marie Verdun | 61b4a5b | 2021-09-19 08:53:28 -0400 | [diff] [blame] | 2682 | if (psPowerOKLine.get_value() > 0 || |
Lei YU | a37c247 | 2021-09-26 15:57:12 +0800 | [diff] [blame] | 2683 | (sioEnabled && |
| 2684 | (sioPowerGoodLine.get_value() == sioPwrGoodConfig.polarity))) |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2685 | { |
| 2686 | powerState = PowerState::on; |
| 2687 | } |
| 2688 | } |
| 2689 | else |
| 2690 | { |
| 2691 | if (getProperty(powerOkConfig)) |
| 2692 | { |
| 2693 | powerState = PowerState::on; |
| 2694 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2695 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2696 | // Initialize the power state storage |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2697 | if (initializePowerStateStorage() < 0) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2698 | { |
| 2699 | return -1; |
| 2700 | } |
| 2701 | |
| 2702 | // Check if we need to start the Power Restore policy |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2703 | powerRestorePolicyCheck(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2704 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2705 | if (nmiOutLine) |
| 2706 | nmiSourcePropertyMonitor(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2707 | |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2708 | lg2::info("Initializing power state."); |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2709 | logStateTransition(powerState); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2710 | |
| 2711 | // Power Control Service |
| 2712 | sdbusplus::asio::object_server hostServer = |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2713 | sdbusplus::asio::object_server(conn); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2714 | |
| 2715 | // Power Control Interface |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2716 | hostIface = |
| 2717 | hostServer.add_interface("/xyz/openbmc_project/state/host" + node, |
| 2718 | "xyz.openbmc_project.State.Host"); |
Vernon Mauery | b4d03b1 | 2021-05-26 19:11:41 -0700 | [diff] [blame] | 2719 | // Interface for IPMI/Redfish initiated host state transitions |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2720 | hostIface->register_property( |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2721 | "RequestedHostTransition", |
| 2722 | std::string("xyz.openbmc_project.State.Host.Transition.Off"), |
| 2723 | [](const std::string& requested, std::string& resp) { |
| 2724 | if (requested == "xyz.openbmc_project.State.Host.Transition.Off") |
| 2725 | { |
Vernon Mauery | b4d03b1 | 2021-05-26 19:11:41 -0700 | [diff] [blame] | 2726 | // if power button is masked, ignore this |
| 2727 | if (!powerButtonMask) |
| 2728 | { |
| 2729 | sendPowerControlEvent(Event::gracefulPowerOffRequest); |
| 2730 | addRestartCause(RestartCause::command); |
| 2731 | } |
| 2732 | else |
| 2733 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2734 | lg2::info("Power Button Masked."); |
Vernon Mauery | b4d03b1 | 2021-05-26 19:11:41 -0700 | [diff] [blame] | 2735 | throw std::invalid_argument("Transition Request Masked"); |
| 2736 | return 0; |
| 2737 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2738 | } |
| 2739 | else if (requested == |
| 2740 | "xyz.openbmc_project.State.Host.Transition.On") |
| 2741 | { |
Vernon Mauery | b4d03b1 | 2021-05-26 19:11:41 -0700 | [diff] [blame] | 2742 | // if power button is masked, ignore this |
| 2743 | if (!powerButtonMask) |
| 2744 | { |
| 2745 | sendPowerControlEvent(Event::powerOnRequest); |
| 2746 | addRestartCause(RestartCause::command); |
| 2747 | } |
| 2748 | else |
| 2749 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2750 | lg2::info("Power Button Masked."); |
Vernon Mauery | b4d03b1 | 2021-05-26 19:11:41 -0700 | [diff] [blame] | 2751 | throw std::invalid_argument("Transition Request Masked"); |
| 2752 | return 0; |
| 2753 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2754 | } |
| 2755 | else if (requested == |
| 2756 | "xyz.openbmc_project.State.Host.Transition.Reboot") |
| 2757 | { |
Vernon Mauery | b4d03b1 | 2021-05-26 19:11:41 -0700 | [diff] [blame] | 2758 | // if power button is masked, ignore this |
| 2759 | if (!powerButtonMask) |
| 2760 | { |
| 2761 | sendPowerControlEvent(Event::powerCycleRequest); |
| 2762 | addRestartCause(RestartCause::command); |
| 2763 | } |
| 2764 | else |
| 2765 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2766 | lg2::info("Power Button Masked."); |
Vernon Mauery | b4d03b1 | 2021-05-26 19:11:41 -0700 | [diff] [blame] | 2767 | throw std::invalid_argument("Transition Request Masked"); |
| 2768 | return 0; |
| 2769 | } |
Jason M. Bills | e7520ba | 2020-01-31 11:19:03 -0800 | [diff] [blame] | 2770 | } |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 2771 | else if ( |
| 2772 | requested == |
| 2773 | "xyz.openbmc_project.State.Host.Transition.GracefulWarmReboot") |
Jason M. Bills | e7520ba | 2020-01-31 11:19:03 -0800 | [diff] [blame] | 2774 | { |
Vernon Mauery | b4d03b1 | 2021-05-26 19:11:41 -0700 | [diff] [blame] | 2775 | // if reset button is masked, ignore this |
| 2776 | if (!resetButtonMask) |
| 2777 | { |
| 2778 | sendPowerControlEvent(Event::gracefulPowerCycleRequest); |
| 2779 | addRestartCause(RestartCause::command); |
| 2780 | } |
| 2781 | else |
| 2782 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2783 | lg2::info("Reset Button Masked."); |
Vernon Mauery | b4d03b1 | 2021-05-26 19:11:41 -0700 | [diff] [blame] | 2784 | throw std::invalid_argument("Transition Request Masked"); |
| 2785 | return 0; |
| 2786 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2787 | } |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 2788 | else if ( |
| 2789 | requested == |
| 2790 | "xyz.openbmc_project.State.Host.Transition.ForceWarmReboot") |
Jason M. Bills | e7520ba | 2020-01-31 11:19:03 -0800 | [diff] [blame] | 2791 | { |
Vernon Mauery | b4d03b1 | 2021-05-26 19:11:41 -0700 | [diff] [blame] | 2792 | // if reset button is masked, ignore this |
| 2793 | if (!resetButtonMask) |
| 2794 | { |
| 2795 | sendPowerControlEvent(Event::resetRequest); |
| 2796 | addRestartCause(RestartCause::command); |
| 2797 | } |
| 2798 | else |
| 2799 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2800 | lg2::info("Reset Button Masked."); |
Vernon Mauery | b4d03b1 | 2021-05-26 19:11:41 -0700 | [diff] [blame] | 2801 | throw std::invalid_argument("Transition Request Masked"); |
| 2802 | return 0; |
| 2803 | } |
Jason M. Bills | e7520ba | 2020-01-31 11:19:03 -0800 | [diff] [blame] | 2804 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2805 | else |
| 2806 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2807 | lg2::error("Unrecognized host state transition request."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2808 | throw std::invalid_argument("Unrecognized Transition Request"); |
| 2809 | return 0; |
| 2810 | } |
| 2811 | resp = requested; |
| 2812 | return 1; |
| 2813 | }); |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2814 | hostIface->register_property("CurrentHostState", |
| 2815 | std::string(getHostState(powerState))); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2816 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2817 | hostIface->initialize(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2818 | |
| 2819 | // Chassis Control Service |
| 2820 | sdbusplus::asio::object_server chassisServer = |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2821 | sdbusplus::asio::object_server(conn); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2822 | |
| 2823 | // Chassis Control Interface |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2824 | chassisIface = |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2825 | chassisServer.add_interface("/xyz/openbmc_project/state/chassis" + node, |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2826 | "xyz.openbmc_project.State.Chassis"); |
| 2827 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2828 | chassisIface->register_property( |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2829 | "RequestedPowerTransition", |
| 2830 | std::string("xyz.openbmc_project.State.Chassis.Transition.Off"), |
| 2831 | [](const std::string& requested, std::string& resp) { |
| 2832 | if (requested == "xyz.openbmc_project.State.Chassis.Transition.Off") |
| 2833 | { |
Vernon Mauery | 2a26943 | 2021-07-14 10:00:21 -0700 | [diff] [blame] | 2834 | // if power button is masked, ignore this |
| 2835 | if (!powerButtonMask) |
| 2836 | { |
| 2837 | sendPowerControlEvent(Event::powerOffRequest); |
| 2838 | addRestartCause(RestartCause::command); |
| 2839 | } |
| 2840 | else |
| 2841 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2842 | lg2::info("Power Button Masked."); |
Vernon Mauery | 2a26943 | 2021-07-14 10:00:21 -0700 | [diff] [blame] | 2843 | throw std::invalid_argument("Transition Request Masked"); |
| 2844 | return 0; |
| 2845 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2846 | } |
| 2847 | else if (requested == |
| 2848 | "xyz.openbmc_project.State.Chassis.Transition.On") |
| 2849 | { |
Vernon Mauery | 2a26943 | 2021-07-14 10:00:21 -0700 | [diff] [blame] | 2850 | // if power button is masked, ignore this |
| 2851 | if (!powerButtonMask) |
| 2852 | { |
| 2853 | sendPowerControlEvent(Event::powerOnRequest); |
| 2854 | addRestartCause(RestartCause::command); |
| 2855 | } |
| 2856 | else |
| 2857 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2858 | lg2::info("Power Button Masked."); |
Vernon Mauery | 2a26943 | 2021-07-14 10:00:21 -0700 | [diff] [blame] | 2859 | throw std::invalid_argument("Transition Request Masked"); |
| 2860 | return 0; |
| 2861 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2862 | } |
| 2863 | else if (requested == |
| 2864 | "xyz.openbmc_project.State.Chassis.Transition.PowerCycle") |
| 2865 | { |
Vernon Mauery | 2a26943 | 2021-07-14 10:00:21 -0700 | [diff] [blame] | 2866 | // if power button is masked, ignore this |
| 2867 | if (!powerButtonMask) |
| 2868 | { |
| 2869 | sendPowerControlEvent(Event::powerCycleRequest); |
| 2870 | addRestartCause(RestartCause::command); |
| 2871 | } |
| 2872 | else |
| 2873 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2874 | lg2::info("Power Button Masked."); |
Vernon Mauery | 2a26943 | 2021-07-14 10:00:21 -0700 | [diff] [blame] | 2875 | throw std::invalid_argument("Transition Request Masked"); |
| 2876 | return 0; |
| 2877 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2878 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2879 | else |
| 2880 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2881 | lg2::error("Unrecognized chassis state transition request."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2882 | throw std::invalid_argument("Unrecognized Transition Request"); |
| 2883 | return 0; |
| 2884 | } |
| 2885 | resp = requested; |
| 2886 | return 1; |
| 2887 | }); |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2888 | chassisIface->register_property("CurrentPowerState", |
| 2889 | std::string(getChassisState(powerState))); |
| 2890 | chassisIface->register_property("LastStateChangeTime", getCurrentTimeMs()); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2891 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2892 | chassisIface->initialize(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2893 | |
Vijay Khemka | 04175c2 | 2020-10-09 14:28:11 -0700 | [diff] [blame] | 2894 | #ifdef CHASSIS_SYSTEM_RESET |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 2895 | // Chassis System Service |
| 2896 | sdbusplus::asio::object_server chassisSysServer = |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2897 | sdbusplus::asio::object_server(conn); |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 2898 | |
| 2899 | // Chassis System Interface |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2900 | chassisSysIface = chassisSysServer.add_interface( |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 2901 | "/xyz/openbmc_project/state/chassis_system0", |
| 2902 | "xyz.openbmc_project.State.Chassis"); |
| 2903 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2904 | chassisSysIface->register_property( |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 2905 | "RequestedPowerTransition", |
| 2906 | std::string("xyz.openbmc_project.State.Chassis.Transition.On"), |
| 2907 | [](const std::string& requested, std::string& resp) { |
| 2908 | if (requested == |
| 2909 | "xyz.openbmc_project.State.Chassis.Transition.PowerCycle") |
| 2910 | { |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2911 | systemReset(); |
| 2912 | addRestartCause(RestartCause::command); |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 2913 | } |
| 2914 | else |
| 2915 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2916 | lg2::error( |
Jason M. Bills | 41a49aa | 2021-03-03 16:03:25 -0800 | [diff] [blame] | 2917 | "Unrecognized chassis system state transition request."); |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 2918 | throw std::invalid_argument("Unrecognized Transition Request"); |
| 2919 | return 0; |
| 2920 | } |
| 2921 | resp = requested; |
| 2922 | return 1; |
| 2923 | }); |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2924 | chassisSysIface->register_property( |
| 2925 | "CurrentPowerState", std::string(getChassisState(powerState))); |
| 2926 | chassisSysIface->register_property("LastStateChangeTime", |
| 2927 | getCurrentTimeMs()); |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 2928 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2929 | chassisSysIface->initialize(); |
Vijay Khemka | 75ad0cf | 2020-04-02 15:23:51 -0700 | [diff] [blame] | 2930 | |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 2931 | if (!slotPowerConfig.lineName.empty()) |
| 2932 | { |
| 2933 | if (!setGPIOOutput(slotPowerConfig.lineName, 1, slotPowerLine)) |
| 2934 | { |
| 2935 | return -1; |
| 2936 | } |
| 2937 | |
| 2938 | slotPowerState = SlotPowerState::off; |
| 2939 | if (slotPowerLine.get_value() > 0) |
| 2940 | { |
| 2941 | slotPowerState = SlotPowerState::on; |
| 2942 | } |
| 2943 | |
| 2944 | chassisSlotIface = chassisSysServer.add_interface( |
| 2945 | "/xyz/openbmc_project/state/chassis_system" + node, |
| 2946 | "xyz.openbmc_project.State.Chassis"); |
| 2947 | chassisSlotIface->register_property( |
| 2948 | "RequestedPowerTransition", |
| 2949 | std::string("xyz.openbmc_project.State.Chassis.Transition.On"), |
| 2950 | [](const std::string& requested, std::string& resp) { |
| 2951 | if (requested == |
| 2952 | "xyz.openbmc_project.State.Chassis.Transition.On") |
| 2953 | { |
| 2954 | slotPowerOn(); |
| 2955 | } |
| 2956 | else if (requested == |
| 2957 | "xyz.openbmc_project.State.Chassis.Transition.Off") |
| 2958 | { |
| 2959 | slotPowerOff(); |
| 2960 | } |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 2961 | else if ( |
| 2962 | requested == |
| 2963 | "xyz.openbmc_project.State.Chassis.Transition.PowerCycle") |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 2964 | { |
| 2965 | slotPowerCycle(); |
| 2966 | } |
| 2967 | else |
| 2968 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 2969 | lg2::error( |
Jason M. Bills | 418ce11 | 2021-09-08 15:15:05 -0700 | [diff] [blame] | 2970 | "Unrecognized chassis system state transition request.\n"); |
Naveen Moses | 117c34e | 2021-05-26 20:10:51 +0530 | [diff] [blame] | 2971 | throw std::invalid_argument( |
| 2972 | "Unrecognized Transition Request"); |
| 2973 | return 0; |
| 2974 | } |
| 2975 | resp = requested; |
| 2976 | return 1; |
| 2977 | }); |
| 2978 | chassisSlotIface->register_property( |
| 2979 | "CurrentPowerState", std::string(getSlotState(slotPowerState))); |
| 2980 | chassisSlotIface->register_property("LastStateChangeTime", |
| 2981 | getCurrentTimeMs()); |
| 2982 | chassisSlotIface->initialize(); |
| 2983 | } |
| 2984 | #endif |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2985 | // Buttons Service |
| 2986 | sdbusplus::asio::object_server buttonsServer = |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 2987 | sdbusplus::asio::object_server(conn); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 2988 | |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2989 | if (!powerButtonConfig.lineName.empty()) |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 2990 | { |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 2991 | // Power Button Interface |
| 2992 | power_control::powerButtonIface = buttonsServer.add_interface( |
| 2993 | "/xyz/openbmc_project/chassis/buttons/power", |
| 2994 | "xyz.openbmc_project.Chassis.Buttons"); |
| 2995 | |
| 2996 | powerButtonIface->register_property( |
| 2997 | "ButtonMasked", false, [](const bool requested, bool& current) { |
| 2998 | if (requested) |
| 2999 | { |
| 3000 | if (powerButtonMask) |
| 3001 | { |
| 3002 | return 1; |
| 3003 | } |
Jean-Marie Verdun | 2c495cf | 2021-09-17 21:42:23 -0400 | [diff] [blame] | 3004 | if (!setGPIOOutput(powerOutConfig.lineName, |
| 3005 | !powerOutConfig.polarity, |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3006 | powerButtonMask)) |
| 3007 | { |
| 3008 | throw std::runtime_error("Failed to request GPIO"); |
| 3009 | return 0; |
| 3010 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 3011 | lg2::info("Power Button Masked."); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3012 | } |
| 3013 | else |
| 3014 | { |
| 3015 | if (!powerButtonMask) |
| 3016 | { |
| 3017 | return 1; |
| 3018 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 3019 | lg2::info("Power Button Un-masked"); |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3020 | powerButtonMask.reset(); |
| 3021 | } |
| 3022 | // Update the mask setting |
| 3023 | current = requested; |
| 3024 | return 1; |
| 3025 | }); |
| 3026 | |
| 3027 | // Check power button state |
| 3028 | bool powerButtonPressed; |
| 3029 | if (powerButtonConfig.type == ConfigType::GPIO) |
| 3030 | { |
| 3031 | powerButtonPressed = powerButtonLine.get_value() == 0; |
| 3032 | } |
| 3033 | else |
| 3034 | { |
| 3035 | powerButtonPressed = getProperty(powerButtonConfig) == 0; |
| 3036 | } |
| 3037 | |
| 3038 | powerButtonIface->register_property("ButtonPressed", |
| 3039 | powerButtonPressed); |
| 3040 | |
| 3041 | powerButtonIface->initialize(); |
| 3042 | } |
| 3043 | |
| 3044 | if (!resetButtonConfig.lineName.empty()) |
| 3045 | { |
| 3046 | // Reset Button Interface |
| 3047 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3048 | resetButtonIface = buttonsServer.add_interface( |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3049 | "/xyz/openbmc_project/chassis/buttons/reset", |
| 3050 | "xyz.openbmc_project.Chassis.Buttons"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3051 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3052 | resetButtonIface->register_property( |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3053 | "ButtonMasked", false, [](const bool requested, bool& current) { |
| 3054 | if (requested) |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3055 | { |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3056 | if (resetButtonMask) |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3057 | { |
| 3058 | return 1; |
| 3059 | } |
Jean-Marie Verdun | 2c495cf | 2021-09-17 21:42:23 -0400 | [diff] [blame] | 3060 | if (!setGPIOOutput(resetOutConfig.lineName, |
| 3061 | !resetOutConfig.polarity, |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3062 | resetButtonMask)) |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3063 | { |
| 3064 | throw std::runtime_error("Failed to request GPIO"); |
| 3065 | return 0; |
| 3066 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 3067 | lg2::info("Reset Button Masked."); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3068 | } |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3069 | else |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3070 | { |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3071 | if (!resetButtonMask) |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3072 | { |
| 3073 | return 1; |
| 3074 | } |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 3075 | lg2::info("Reset Button Un-masked"); |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3076 | resetButtonMask.reset(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3077 | } |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3078 | // Update the mask setting |
| 3079 | current = requested; |
| 3080 | return 1; |
| 3081 | }); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3082 | |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3083 | // Check reset button state |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3084 | bool resetButtonPressed; |
| 3085 | if (resetButtonConfig.type == ConfigType::GPIO) |
| 3086 | { |
| 3087 | resetButtonPressed = resetButtonLine.get_value() == 0; |
| 3088 | } |
| 3089 | else |
| 3090 | { |
| 3091 | resetButtonPressed = getProperty(resetButtonConfig) == 0; |
| 3092 | } |
| 3093 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3094 | resetButtonIface->register_property("ButtonPressed", |
| 3095 | resetButtonPressed); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3096 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3097 | resetButtonIface->initialize(); |
John Wang | 6c09007 | 2020-09-30 13:32:16 +0800 | [diff] [blame] | 3098 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3099 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3100 | if (nmiButtonLine) |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3101 | { |
| 3102 | // NMI Button Interface |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3103 | nmiButtonIface = buttonsServer.add_interface( |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3104 | "/xyz/openbmc_project/chassis/buttons/nmi", |
| 3105 | "xyz.openbmc_project.Chassis.Buttons"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3106 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3107 | nmiButtonIface->register_property( |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3108 | "ButtonMasked", false, [](const bool requested, bool& current) { |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3109 | if (nmiButtonMasked == requested) |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3110 | { |
| 3111 | // NMI button mask is already set as requested, so no change |
| 3112 | return 1; |
| 3113 | } |
| 3114 | if (requested) |
| 3115 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 3116 | lg2::info("NMI Button Masked."); |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3117 | nmiButtonMasked = true; |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3118 | } |
| 3119 | else |
| 3120 | { |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 3121 | lg2::info("NMI Button Un-masked."); |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3122 | nmiButtonMasked = false; |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3123 | } |
| 3124 | // Update the mask setting |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3125 | current = nmiButtonMasked; |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3126 | return 1; |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3127 | }); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3128 | |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3129 | // Check NMI button state |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3130 | bool nmiButtonPressed; |
| 3131 | if (nmiButtonConfig.type == ConfigType::GPIO) |
| 3132 | { |
| 3133 | nmiButtonPressed = nmiButtonLine.get_value() == 0; |
| 3134 | } |
| 3135 | else |
| 3136 | { |
| 3137 | nmiButtonPressed = getProperty(nmiButtonConfig) == 0; |
| 3138 | } |
| 3139 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3140 | nmiButtonIface->register_property("ButtonPressed", nmiButtonPressed); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3141 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3142 | nmiButtonIface->initialize(); |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3143 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3144 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3145 | if (nmiOutLine) |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3146 | { |
| 3147 | // NMI out Service |
| 3148 | sdbusplus::asio::object_server nmiOutServer = |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3149 | sdbusplus::asio::object_server(conn); |
Chen Yugang | 174ec66 | 2019-08-19 19:58:49 +0800 | [diff] [blame] | 3150 | |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3151 | // NMI out Interface |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3152 | nmiOutIface = nmiOutServer.add_interface( |
| 3153 | "/xyz/openbmc_project/control/host" + node + "/nmi", |
| 3154 | "xyz.openbmc_project.Control.Host.NMI"); |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3155 | nmiOutIface->register_method("NMI", nmiReset); |
| 3156 | nmiOutIface->initialize(); |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3157 | } |
Chen Yugang | 174ec66 | 2019-08-19 19:58:49 +0800 | [diff] [blame] | 3158 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3159 | if (idButtonLine) |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3160 | { |
| 3161 | // ID Button Interface |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3162 | idButtonIface = buttonsServer.add_interface( |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3163 | "/xyz/openbmc_project/chassis/buttons/id", |
| 3164 | "xyz.openbmc_project.Chassis.Buttons"); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3165 | |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3166 | // Check ID button state |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3167 | bool idButtonPressed; |
| 3168 | if (idButtonConfig.type == ConfigType::GPIO) |
| 3169 | { |
| 3170 | idButtonPressed = idButtonLine.get_value() == 0; |
| 3171 | } |
| 3172 | else |
| 3173 | { |
| 3174 | idButtonPressed = getProperty(idButtonConfig) == 0; |
| 3175 | } |
| 3176 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3177 | idButtonIface->register_property("ButtonPressed", idButtonPressed); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3178 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3179 | idButtonIface->initialize(); |
Vijay Khemka | 33a532d | 2019-11-14 16:50:35 -0800 | [diff] [blame] | 3180 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3181 | |
| 3182 | // OS State Service |
| 3183 | sdbusplus::asio::object_server osServer = |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3184 | sdbusplus::asio::object_server(conn); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3185 | |
| 3186 | // OS State Interface |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3187 | osIface = osServer.add_interface( |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3188 | "/xyz/openbmc_project/state/os", |
| 3189 | "xyz.openbmc_project.State.OperatingSystem.Status"); |
| 3190 | |
| 3191 | // Get the initial OS state based on POST complete |
| 3192 | // 0: Asserted, OS state is "Standby" (ready to boot) |
| 3193 | // 1: De-Asserted, OS state is "Inactive" |
Priyatharshan P | 7012051 | 2020-09-16 18:47:20 +0530 | [diff] [blame] | 3194 | std::string osState; |
| 3195 | if (postCompleteConfig.type == ConfigType::GPIO) |
| 3196 | { |
| 3197 | osState = postCompleteLine.get_value() > 0 ? "Inactive" : "Standby"; |
| 3198 | } |
| 3199 | else |
| 3200 | { |
| 3201 | osState = getProperty(postCompleteConfig) > 0 ? "Inactive" : "Standby"; |
| 3202 | } |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3203 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3204 | osIface->register_property("OperatingSystemState", std::string(osState)); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3205 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3206 | osIface->initialize(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3207 | |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 3208 | // Restart Cause Service |
| 3209 | sdbusplus::asio::object_server restartCauseServer = |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3210 | sdbusplus::asio::object_server(conn); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 3211 | |
| 3212 | // Restart Cause Interface |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3213 | restartCauseIface = restartCauseServer.add_interface( |
Naveen Moses | ec972d8 | 2021-07-16 21:19:23 +0530 | [diff] [blame] | 3214 | "/xyz/openbmc_project/control/host" + node + "/restart_cause", |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 3215 | "xyz.openbmc_project.Control.Host.RestartCause"); |
| 3216 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3217 | restartCauseIface->register_property( |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 3218 | "RestartCause", |
| 3219 | std::string("xyz.openbmc_project.State.Host.RestartCause.Unknown")); |
| 3220 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3221 | restartCauseIface->register_property( |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 3222 | "RequestedRestartCause", |
| 3223 | std::string("xyz.openbmc_project.State.Host.RestartCause.Unknown"), |
| 3224 | [](const std::string& requested, std::string& resp) { |
| 3225 | if (requested == |
| 3226 | "xyz.openbmc_project.State.Host.RestartCause.WatchdogTimer") |
| 3227 | { |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3228 | addRestartCause(RestartCause::watchdog); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 3229 | } |
| 3230 | else |
| 3231 | { |
| 3232 | throw std::invalid_argument( |
| 3233 | "Unrecognized RestartCause Request"); |
| 3234 | return 0; |
| 3235 | } |
| 3236 | |
Jason M. Bills | c46ebb4 | 2021-11-10 11:41:31 -0800 | [diff] [blame^] | 3237 | lg2::info("RestartCause requested: {RESTART_CAUSE}", |
| 3238 | "RESTART_CAUSE", requested); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 3239 | resp = requested; |
| 3240 | return 1; |
| 3241 | }); |
| 3242 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3243 | restartCauseIface->initialize(); |
Jason M. Bills | 7d4aaac | 2019-09-19 14:03:44 -0700 | [diff] [blame] | 3244 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3245 | currentHostStateMonitor(); |
Yong Li | 8d66021 | 2019-12-27 10:18:10 +0800 | [diff] [blame] | 3246 | |
Lei YU | 92caa4c | 2021-02-23 16:59:25 +0800 | [diff] [blame] | 3247 | io.run(); |
Ed Tanous | f61ca6f | 2019-08-15 15:09:05 -0700 | [diff] [blame] | 3248 | |
| 3249 | return 0; |
| 3250 | } |