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