Jim Wright | 10eb00f | 2021-07-21 12:10:38 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2021 IBM 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 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 17 | #include "power_control.hpp" |
| 18 | |
| 19 | #include "types.hpp" |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 20 | #include "ucd90320_monitor.hpp" |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 21 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 22 | #include <fmt/format.h> |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 25 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 26 | #include <phosphor-logging/elog-errors.hpp> |
| 27 | #include <phosphor-logging/elog.hpp> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 28 | #include <phosphor-logging/log.hpp> |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 29 | #include <xyz/openbmc_project/Common/error.hpp> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 30 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 31 | #include <exception> |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 32 | #include <string> |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 33 | |
| 34 | using namespace phosphor::logging; |
| 35 | |
| 36 | namespace phosphor::power::sequencer |
Jim Wright | 10eb00f | 2021-07-21 12:10:38 -0500 | [diff] [blame] | 37 | { |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 38 | |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 39 | const std::string interfaceName = "xyz.openbmc_project.Configuration.UCD90320"; |
| 40 | const std::string addressPropertyName = "Address"; |
| 41 | const std::string busPropertyName = "Bus"; |
| 42 | const std::string namePropertyName = "Name"; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 43 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 44 | PowerControl::PowerControl(sdbusplus::bus::bus& bus, |
| 45 | const sdeventplus::Event& event) : |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 46 | PowerObject{bus, POWER_OBJ_PATH, true}, |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame^] | 47 | bus{bus}, match{bus, |
| 48 | sdbusplus::bus::match::rules::interfacesAdded() + |
| 49 | sdbusplus::bus::match::rules::sender( |
| 50 | "xyz.openbmc_project.EntityManager"), |
| 51 | std::bind(&PowerControl::interfacesAddedHandler, this, |
| 52 | std::placeholders::_1)}, |
| 53 | timer{event, std::bind(&PowerControl::pollPgood, this), pollInterval} |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 54 | { |
| 55 | // Obtain dbus service name |
| 56 | bus.request_name(POWER_IFACE); |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 57 | |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 58 | setUpDevice(); |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 59 | setUpGpio(); |
Jim Wright | 10eb00f | 2021-07-21 12:10:38 -0500 | [diff] [blame] | 60 | } |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 61 | |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 62 | void PowerControl::getDeviceProperties(util::DbusPropertyMap& properties) |
| 63 | { |
| 64 | uint64_t* i2cBus = nullptr; |
| 65 | uint64_t* i2cAddress = nullptr; |
| 66 | std::string* name = nullptr; |
| 67 | |
| 68 | for (const auto& property : properties) |
| 69 | { |
| 70 | try |
| 71 | { |
| 72 | if (property.first == busPropertyName) |
| 73 | { |
| 74 | i2cBus = std::get_if<uint64_t>(&properties[busPropertyName]); |
| 75 | } |
| 76 | else if (property.first == addressPropertyName) |
| 77 | { |
| 78 | i2cAddress = |
| 79 | std::get_if<uint64_t>(&properties[addressPropertyName]); |
| 80 | } |
| 81 | else if (property.first == namePropertyName) |
| 82 | { |
| 83 | name = std::get_if<std::string>(&properties[namePropertyName]); |
| 84 | } |
| 85 | } |
| 86 | catch (std::exception& e) |
| 87 | {} |
| 88 | } |
| 89 | |
| 90 | if (i2cBus && i2cAddress && name && !name->empty()) |
| 91 | { |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 92 | log<level::DEBUG>( |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 93 | fmt::format( |
| 94 | "Found power sequencer device properties, name: {}, bus: {} addr: {:#02x} ", |
| 95 | *name, *i2cBus, *i2cAddress) |
| 96 | .c_str()); |
| 97 | // Create device object |
Jim Wright | 7945dd2 | 2021-04-06 16:55:15 -0500 | [diff] [blame] | 98 | device = std::make_unique<UCD90320Monitor>(bus, *i2cBus, *i2cAddress); |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame^] | 99 | deviceFound = true; |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 103 | int PowerControl::getPgood() const |
| 104 | { |
| 105 | return pgood; |
| 106 | } |
| 107 | |
| 108 | int PowerControl::getPgoodTimeout() const |
| 109 | { |
| 110 | return timeout.count(); |
| 111 | } |
| 112 | |
| 113 | int PowerControl::getState() const |
| 114 | { |
| 115 | return state; |
| 116 | } |
| 117 | |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 118 | void PowerControl::interfacesAddedHandler(sdbusplus::message::message& msg) |
| 119 | { |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame^] | 120 | // Only continue if message is valid and device has not already been found |
| 121 | if (!msg || deviceFound) |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 122 | { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | try |
| 127 | { |
| 128 | // Read the dbus message |
| 129 | sdbusplus::message::object_path objPath; |
| 130 | std::map<std::string, std::map<std::string, util::DbusVariant>> |
| 131 | interfaces; |
| 132 | msg.read(objPath, interfaces); |
| 133 | |
| 134 | // Find the device interface, if present |
| 135 | auto itIntf = interfaces.find(interfaceName); |
| 136 | if (itIntf != interfaces.cend()) |
| 137 | { |
| 138 | log<level::INFO>( |
| 139 | fmt::format("InterfacesAdded for: {}", interfaceName).c_str()); |
| 140 | getDeviceProperties(itIntf->second); |
| 141 | } |
| 142 | } |
| 143 | catch (const std::exception&) |
| 144 | { |
| 145 | // Error trying to read interfacesAdded message. |
| 146 | } |
| 147 | } |
| 148 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 149 | void PowerControl::pollPgood() |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 150 | { |
| 151 | if (inStateTransition) |
| 152 | { |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 153 | // In transition between power on and off, check for timeout |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 154 | const auto now = std::chrono::steady_clock::now(); |
| 155 | if (now > pgoodTimeoutTime) |
| 156 | { |
| 157 | log<level::ERR>("ERROR PowerControl: Pgood poll timeout"); |
| 158 | inStateTransition = false; |
Jim Wright | 4e25df5 | 2021-11-17 09:38:00 -0600 | [diff] [blame] | 159 | |
| 160 | try |
| 161 | { |
| 162 | auto method = bus.new_method_call( |
| 163 | "xyz.openbmc_project.Logging", |
| 164 | "/xyz/openbmc_project/logging", |
| 165 | "xyz.openbmc_project.Logging.Create", "Create"); |
| 166 | |
| 167 | std::map<std::string, std::string> additionalData; |
| 168 | // Add PID to AdditionalData |
| 169 | additionalData.emplace("_PID", std::to_string(getpid())); |
| 170 | |
| 171 | method.append( |
| 172 | state ? "xyz.openbmc_project.Power.Error.PowerOnTimeout" |
| 173 | : "xyz.openbmc_project.Power.Error.PowerOffTimeout", |
| 174 | sdbusplus::xyz::openbmc_project::Logging::server::Entry:: |
| 175 | Level::Critical, |
| 176 | additionalData); |
| 177 | bus.call_noreply(method); |
| 178 | } |
| 179 | catch (const std::exception& e) |
| 180 | { |
| 181 | log<level::ERR>( |
| 182 | fmt::format( |
| 183 | "Unable to log timeout error, state: {}, error {}", |
| 184 | state, e.what()) |
| 185 | .c_str()); |
| 186 | } |
| 187 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 188 | return; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | int pgoodState = pgoodLine.get_value(); |
| 193 | if (pgoodState != pgood) |
| 194 | { |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 195 | // Power good has changed since last read |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 196 | pgood = pgoodState; |
| 197 | if (pgoodState == 0) |
| 198 | { |
| 199 | emitPowerLostSignal(); |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | emitPowerGoodSignal(); |
| 204 | } |
| 205 | emitPropertyChangedSignal("pgood"); |
| 206 | } |
| 207 | if (pgoodState == state) |
| 208 | { |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 209 | // Power good matches requested state |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 210 | inStateTransition = false; |
| 211 | } |
Jim Wright | 9d7d95c | 2021-11-16 11:32:23 -0600 | [diff] [blame] | 212 | else if (!inStateTransition && (pgoodState == 0)) |
| 213 | { |
| 214 | // Not in power off state, not changing state, and power good is off |
| 215 | // Power good has failed, call for chassis hard power off |
| 216 | log<level::ERR>("Chassis pgood failure"); |
| 217 | |
| 218 | auto method = |
| 219 | bus.new_method_call(util::SYSTEMD_SERVICE, util::SYSTEMD_ROOT, |
| 220 | util::SYSTEMD_INTERFACE, "StartUnit"); |
| 221 | method.append(util::POWEROFF_TARGET); |
| 222 | method.append("replace"); |
| 223 | bus.call_noreply(method); |
| 224 | } |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 225 | } |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 226 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 227 | void PowerControl::setPgoodTimeout(int t) |
| 228 | { |
| 229 | if (timeout.count() != t) |
| 230 | { |
| 231 | timeout = std::chrono::seconds(t); |
| 232 | emitPropertyChangedSignal("pgood_timeout"); |
| 233 | } |
| 234 | } |
| 235 | |
Jim Wright | ccea2d2 | 2021-12-10 14:10:46 -0600 | [diff] [blame^] | 236 | void PowerControl::setPowerSupplyError(const std::string& error) |
| 237 | { |
| 238 | powerSupplyError = error; |
| 239 | } |
| 240 | |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 241 | void PowerControl::setState(int s) |
| 242 | { |
| 243 | if (state == s) |
| 244 | { |
| 245 | log<level::INFO>( |
| 246 | fmt::format("Power already at requested state: {}", state).c_str()); |
| 247 | return; |
| 248 | } |
Jim Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 249 | if (s == 0) |
| 250 | { |
| 251 | // Wait for two seconds when powering down. This is to allow host and |
| 252 | // other BMC applications time to complete power off processing |
| 253 | std::this_thread::sleep_for(std::chrono::seconds(2)); |
| 254 | } |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 255 | |
| 256 | log<level::INFO>(fmt::format("setState: {}", s).c_str()); |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 257 | powerControlLine.request( |
| 258 | {"phosphor-power-control", gpiod::line_request::DIRECTION_OUTPUT, 0}); |
| 259 | powerControlLine.set_value(s); |
| 260 | powerControlLine.release(); |
| 261 | |
| 262 | pgoodTimeoutTime = std::chrono::steady_clock::now() + timeout; |
| 263 | inStateTransition = true; |
Jim Wright | 22318a3 | 2021-08-27 15:56:09 -0500 | [diff] [blame] | 264 | state = s; |
| 265 | emitPropertyChangedSignal("state"); |
| 266 | } |
| 267 | |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 268 | void PowerControl::setUpDevice() |
| 269 | { |
| 270 | try |
| 271 | { |
| 272 | auto objects = util::getSubTree(bus, "/", interfaceName, 0); |
| 273 | |
| 274 | // Search for matching interface in returned objects |
| 275 | for (const auto& [path, services] : objects) |
| 276 | { |
| 277 | auto service = services.begin()->first; |
| 278 | |
| 279 | if (path.empty() || service.empty()) |
| 280 | { |
| 281 | continue; |
| 282 | } |
| 283 | |
| 284 | // Get the properties for the device interface |
| 285 | auto properties = |
| 286 | util::getAllProperties(bus, path, interfaceName, service); |
| 287 | |
| 288 | getDeviceProperties(properties); |
| 289 | } |
| 290 | } |
| 291 | catch (const std::exception& e) |
| 292 | { |
| 293 | // Interface or property not found. Let the Interfaces Added callback |
| 294 | // process the information once the interfaces are added to D-Bus. |
| 295 | } |
| 296 | } |
| 297 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 298 | void PowerControl::setUpGpio() |
| 299 | { |
Jim Wright | 2d99bf7 | 2021-11-19 11:18:12 -0600 | [diff] [blame] | 300 | const std::string powerControlLineName = "power-chassis-control"; |
| 301 | const std::string pgoodLineName = "power-chassis-good"; |
| 302 | |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 303 | pgoodLine = gpiod::find_line(pgoodLineName); |
| 304 | if (!pgoodLine) |
| 305 | { |
Jim Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 306 | std::string errorString{"GPIO line name not found: " + pgoodLineName}; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 307 | log<level::ERR>(errorString.c_str()); |
| 308 | report< |
| 309 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>(); |
| 310 | throw std::runtime_error(errorString); |
| 311 | } |
| 312 | powerControlLine = gpiod::find_line(powerControlLineName); |
| 313 | if (!powerControlLine) |
| 314 | { |
Jim Wright | 209690b | 2021-11-11 14:46:42 -0600 | [diff] [blame] | 315 | std::string errorString{"GPIO line name not found: " + |
| 316 | powerControlLineName}; |
Jim Wright | 7a5dd99 | 2021-08-31 16:56:52 -0500 | [diff] [blame] | 317 | log<level::ERR>(errorString.c_str()); |
| 318 | report< |
| 319 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure>(); |
| 320 | throw std::runtime_error(errorString); |
| 321 | } |
| 322 | |
| 323 | pgoodLine.request( |
| 324 | {"phosphor-power-control", gpiod::line_request::DIRECTION_INPUT, 0}); |
| 325 | int pgoodState = pgoodLine.get_value(); |
| 326 | pgood = pgoodState; |
| 327 | state = pgoodState; |
| 328 | log<level::INFO>(fmt::format("Pgood state: {}", pgoodState).c_str()); |
| 329 | } |
| 330 | |
Jim Wright | 539b608 | 2021-08-02 14:50:23 -0500 | [diff] [blame] | 331 | } // namespace phosphor::power::sequencer |