Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2017 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 | */ |
Brandon Wyman | 442035f | 2017-08-08 15:58:45 -0500 | [diff] [blame] | 16 | #include <phosphor-logging/log.hpp> |
| 17 | #include <phosphor-logging/elog.hpp> |
| 18 | #include <xyz/openbmc_project/Sensor/Device/error.hpp> |
| 19 | #include <xyz/openbmc_project/Control/Device/error.hpp> |
| 20 | #include <xyz/openbmc_project/Power/Fault/error.hpp> |
| 21 | #include "elog-errors.hpp" |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 22 | #include "names_values.hpp" |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 23 | #include "power_supply.hpp" |
Brandon Wyman | 442035f | 2017-08-08 15:58:45 -0500 | [diff] [blame] | 24 | #include "pmbus.hpp" |
| 25 | #include "utility.hpp" |
| 26 | |
| 27 | using namespace phosphor::logging; |
| 28 | using namespace sdbusplus::xyz::openbmc_project::Control::Device::Error; |
| 29 | using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error; |
| 30 | using namespace sdbusplus::xyz::openbmc_project::Power::Fault::Error; |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 31 | |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 32 | namespace witherspoon |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 33 | { |
| 34 | namespace power |
| 35 | { |
| 36 | namespace psu |
| 37 | { |
| 38 | |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 39 | constexpr auto INVENTORY_OBJ_PATH = "/xyz/openbmc_project/inventory"; |
| 40 | constexpr auto INVENTORY_INTERFACE = "xyz.openbmc_project.Inventory.Item"; |
| 41 | constexpr auto PRESENT_PROP = "Present"; |
Brandon Wyman | 431fbe4 | 2017-08-18 16:22:09 -0500 | [diff] [blame] | 42 | constexpr auto POWER_OBJ_PATH = "/org/openbmc/control/power0"; |
| 43 | constexpr auto POWER_INTERFACE = "org.openbmc.control.Power"; |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 44 | |
| 45 | PowerSupply::PowerSupply(const std::string& name, size_t inst, |
Brandon Wyman | 431fbe4 | 2017-08-18 16:22:09 -0500 | [diff] [blame] | 46 | const std::string& objpath, |
| 47 | const std::string& invpath, |
| 48 | sdbusplus::bus::bus& bus, |
| 49 | event::Event& e, |
| 50 | std::chrono::seconds& t) |
| 51 | : Device(name, inst), monitorPath(objpath), pmbusIntf(objpath), |
| 52 | inventoryPath(invpath), bus(bus), event(e), powerOnInterval(t), |
| 53 | powerOnTimer(e, [this]() |
| 54 | { |
| 55 | this->powerOn = true; |
| 56 | }) |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 57 | { |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 58 | using namespace sdbusplus::bus; |
| 59 | auto present_obj_path = INVENTORY_OBJ_PATH + inventoryPath; |
| 60 | presentMatch = std::make_unique<match_t>(bus, |
| 61 | match::rules::propertiesChanged( |
| 62 | present_obj_path, |
| 63 | INVENTORY_INTERFACE), |
| 64 | [this](auto& msg) |
Brandon Wyman | 431fbe4 | 2017-08-18 16:22:09 -0500 | [diff] [blame] | 65 | { |
| 66 | this->inventoryChanged(msg); |
| 67 | }); |
| 68 | // Get initial presence state. |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 69 | updatePresence(); |
Brandon Wyman | 431fbe4 | 2017-08-18 16:22:09 -0500 | [diff] [blame] | 70 | |
| 71 | // Subscribe to power state changes |
| 72 | powerOnMatch = std::make_unique<match_t>(bus, |
| 73 | match::rules::propertiesChanged( |
| 74 | POWER_OBJ_PATH, |
| 75 | POWER_INTERFACE), |
| 76 | [this](auto& msg) |
| 77 | { |
| 78 | this->powerStateChanged(msg); |
| 79 | }); |
| 80 | // Get initial power state. |
| 81 | updatePowerState(); |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 82 | } |
Brandon Wyman | 442035f | 2017-08-08 15:58:45 -0500 | [diff] [blame] | 83 | |
Brandon Wyman | 431fbe4 | 2017-08-18 16:22:09 -0500 | [diff] [blame] | 84 | |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 85 | void PowerSupply::analyze() |
| 86 | { |
Brandon Wyman | 442035f | 2017-08-08 15:58:45 -0500 | [diff] [blame] | 87 | using namespace witherspoon::pmbus; |
| 88 | |
| 89 | try |
| 90 | { |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 91 | if (present) |
Brandon Wyman | 442035f | 2017-08-08 15:58:45 -0500 | [diff] [blame] | 92 | { |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 93 | std::uint16_t statusWord = 0; |
| 94 | std::uint8_t statusInput = 0; |
| 95 | |
| 96 | // Read the 2 byte STATUS_WORD value to check for faults. |
| 97 | statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug); |
| 98 | |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 99 | //TODO: 3 consecutive reads should be performed. |
| 100 | // If 3 consecutive reads are seen, log the fault. |
| 101 | // Driver gives cached value, read once a second. |
| 102 | // increment for fault on, decrement for fault off, to deglitch. |
| 103 | // If count reaches 3, we have fault. If count reaches 0, fault is |
| 104 | // cleared. |
| 105 | |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 106 | if ((statusWord & status_word::VIN_UV_FAULT) && !vinUVFault) |
Brandon Wyman | 442035f | 2017-08-08 15:58:45 -0500 | [diff] [blame] | 107 | { |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 108 | util::NamesValues nv; |
| 109 | nv.add("STATUS_WORD", statusWord); |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 110 | |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 111 | using metadata = xyz::openbmc_project::Power::Fault:: |
| 112 | PowerSupplyUnderVoltageFault; |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 113 | |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 114 | report<PowerSupplyUnderVoltageFault>( |
| 115 | metadata::RAW_STATUS(nv.get().c_str())); |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 116 | |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 117 | vinUVFault = true; |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | vinUVFault = false; |
| 122 | log<level::INFO>("VIN_UV_FAULT cleared", |
| 123 | entry("POWERSUPPLY=%s", |
| 124 | inventoryPath.c_str())); |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 127 | if ((statusWord & status_word::INPUT_FAULT_WARN) && !inputFault) |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 128 | { |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 129 | inputFault = true; |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 130 | |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 131 | statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug); |
| 132 | |
| 133 | util::NamesValues nv; |
| 134 | nv.add("STATUS_WORD", statusWord); |
| 135 | nv.add("STATUS_INPUT", statusInput); |
| 136 | |
| 137 | using metadata = xyz::openbmc_project::Power::Fault:: |
| 138 | PowerSupplyInputFault; |
| 139 | |
| 140 | report<PowerSupplyInputFault>(metadata::RAW_STATUS( |
| 141 | nv.get().c_str())); |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | if ((inputFault) && |
| 146 | !(statusWord & status_word::INPUT_FAULT_WARN)) |
| 147 | { |
| 148 | inputFault = false; |
| 149 | |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 150 | statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug); |
| 151 | |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 152 | log<level::INFO>("INPUT_FAULT_WARN cleared", |
| 153 | entry("POWERSUPPLY=%s", |
| 154 | inventoryPath.c_str()), |
| 155 | entry("STATUS_WORD=0x%04X", statusWord), |
| 156 | entry("STATUS_INPUT=0x%02X", statusInput)); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (powerOn) |
| 161 | { |
Brandon Wyman | b165c25 | 2017-08-25 18:59:54 -0500 | [diff] [blame^] | 162 | std::uint8_t statusVout = 0; |
| 163 | std::uint8_t statusIout = 0; |
| 164 | std::uint8_t statusMFR = 0; |
| 165 | |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 166 | // Check PG# and UNIT_IS_OFF |
| 167 | if (((statusWord & status_word::POWER_GOOD_NEGATED) || |
| 168 | (statusWord & status_word::UNIT_IS_OFF)) && |
| 169 | !powerOnFault) |
| 170 | { |
| 171 | std::uint8_t statusVout = 0; |
| 172 | std::uint8_t statusIout = 0; |
| 173 | std::uint8_t statusMFR = 0; |
| 174 | |
| 175 | statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug); |
| 176 | auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0); |
| 177 | statusVout = pmbusIntf.read(status0Vout, Type::Debug); |
| 178 | statusIout = pmbusIntf.read(STATUS_IOUT, Type::Debug); |
| 179 | statusMFR = pmbusIntf.read(STATUS_MFR, Type::Debug); |
| 180 | |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 181 | util::NamesValues nv; |
| 182 | nv.add("STATUS_WORD", statusWord); |
| 183 | nv.add("STATUS_INPUT", statusInput); |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 184 | nv.add("STATUS_VOUT", statusVout); |
| 185 | nv.add("STATUS_IOUT", statusIout); |
| 186 | nv.add("MFR_SPECIFIC", statusMFR); |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 187 | |
| 188 | using metadata = xyz::openbmc_project::Power::Fault:: |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 189 | PowerSupplyShouldBeOn; |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 190 | |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 191 | // A power supply is OFF (or pgood low) but should be on. |
| 192 | report<PowerSupplyShouldBeOn>( |
| 193 | metadata::RAW_STATUS(nv.get().c_str()), |
| 194 | metadata::CALLOUT_INVENTORY_PATH( |
| 195 | inventoryPath.c_str())); |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 196 | |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 197 | powerOnFault = true; |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 198 | } |
| 199 | |
Brandon Wyman | b165c25 | 2017-08-25 18:59:54 -0500 | [diff] [blame^] | 200 | // Check for an output overcurrent fault. |
| 201 | if ((statusWord & status_word::IOUT_OC_FAULT) && |
| 202 | !outputOCFault) |
| 203 | { |
| 204 | statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug); |
| 205 | statusVout = pmbusIntf.read(STATUS_VOUT, Type::Debug); |
| 206 | statusIout = pmbusIntf.read(STATUS_IOUT, Type::Debug); |
| 207 | statusMFR = pmbusIntf.read(STATUS_MFR, Type::Debug); |
| 208 | |
| 209 | util::NamesValues nv; |
| 210 | nv.add("STATUS_WORD", statusWord); |
| 211 | nv.add("STATUS_INPUT", statusInput); |
| 212 | nv.add("STATUS_VOUT", statusVout); |
| 213 | nv.add("STATUS_IOUT", statusIout); |
| 214 | nv.add("MFR_SPECIFIC", statusMFR); |
| 215 | |
| 216 | using metadata = xyz::openbmc_project::Power::Fault:: |
| 217 | PowerSupplyOutputOvercurrent; |
| 218 | |
| 219 | report<PowerSupplyOutputOvercurrent>( |
| 220 | metadata::RAW_STATUS(nv.get().c_str()), |
| 221 | metadata::CALLOUT_INVENTORY_PATH( |
| 222 | inventoryPath.c_str())); |
| 223 | |
| 224 | outputOCFault = true; |
| 225 | } |
| 226 | |
Brandon Wyman | 442035f | 2017-08-08 15:58:45 -0500 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | } |
| 230 | catch (ReadFailure& e) |
| 231 | { |
| 232 | if (!readFailLogged) |
| 233 | { |
| 234 | commit<ReadFailure>(); |
| 235 | readFailLogged = true; |
| 236 | // TODO - Need to reset that to false at start of power on, or |
| 237 | // presence change. |
| 238 | } |
| 239 | } |
| 240 | |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 241 | return; |
| 242 | } |
| 243 | |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 244 | void PowerSupply::inventoryChanged(sdbusplus::message::message& msg) |
| 245 | { |
| 246 | std::string msgSensor; |
| 247 | std::map<std::string, sdbusplus::message::variant<uint32_t, bool>> msgData; |
| 248 | msg.read(msgSensor, msgData); |
| 249 | |
| 250 | // Check if it was the Present property that changed. |
| 251 | auto valPropMap = msgData.find(PRESENT_PROP); |
| 252 | if (valPropMap != msgData.end()) |
| 253 | { |
| 254 | present = sdbusplus::message::variant_ns::get<bool>(valPropMap->second); |
| 255 | |
| 256 | if (present) |
| 257 | { |
| 258 | readFailLogged = false; |
| 259 | vinUVFault = false; |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 260 | inputFault = false; |
Brandon Wyman | b165c25 | 2017-08-25 18:59:54 -0500 | [diff] [blame^] | 261 | outputOCFault = false; |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | void PowerSupply::updatePresence() |
| 269 | { |
| 270 | // Use getProperty utility function to get presence status. |
| 271 | std::string path = INVENTORY_OBJ_PATH + inventoryPath; |
| 272 | std::string service = "xyz.openbmc_project.Inventory.Manager"; |
Brandon Wyman | 8731a30 | 2017-08-16 16:15:34 -0500 | [diff] [blame] | 273 | |
| 274 | try |
| 275 | { |
| 276 | util::getProperty(INVENTORY_INTERFACE, PRESENT_PROP, path, |
| 277 | service, bus, this->present); |
| 278 | } |
| 279 | catch (std::exception& e) |
| 280 | { |
| 281 | // If we happen to be trying to update presence just as it is being |
| 282 | // updated, we may encounter a runtime_error. Just catch that for |
| 283 | // now, let the inventoryChanged signal handler update presence later. |
| 284 | present = false; |
| 285 | } |
| 286 | |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 287 | } |
| 288 | |
Brandon Wyman | 431fbe4 | 2017-08-18 16:22:09 -0500 | [diff] [blame] | 289 | void PowerSupply::powerStateChanged(sdbusplus::message::message& msg) |
| 290 | { |
| 291 | int32_t state = 0; |
| 292 | std::string msgSensor; |
| 293 | std::map<std::string, sdbusplus::message::variant<int32_t, int32_t>> |
| 294 | msgData; |
| 295 | msg.read(msgSensor, msgData); |
| 296 | |
| 297 | // Check if it was the Present property that changed. |
| 298 | auto valPropMap = msgData.find("state"); |
| 299 | if (valPropMap != msgData.end()) |
| 300 | { |
| 301 | state = sdbusplus::message::variant_ns::get<int32_t>(valPropMap->second); |
| 302 | |
| 303 | // Power is on when state=1. Set the fault logged variables to false |
| 304 | // and start the power on timer when the state changes to 1. |
| 305 | if (state) |
| 306 | { |
| 307 | readFailLogged = false; |
| 308 | vinUVFault = false; |
| 309 | inputFault = false; |
Brandon Wyman | 764c797 | 2017-08-22 17:05:36 -0500 | [diff] [blame] | 310 | powerOnFault = false; |
Brandon Wyman | b165c25 | 2017-08-25 18:59:54 -0500 | [diff] [blame^] | 311 | outputOCFault = false; |
Brandon Wyman | 431fbe4 | 2017-08-18 16:22:09 -0500 | [diff] [blame] | 312 | powerOnTimer.start(powerOnInterval, Timer::TimerType::oneshot); |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | powerOnTimer.stop(); |
| 317 | powerOn = false; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | } |
| 322 | |
| 323 | void PowerSupply::updatePowerState() |
| 324 | { |
| 325 | // When state = 1, system is powered on |
| 326 | int32_t state = 0; |
| 327 | |
| 328 | try |
| 329 | { |
| 330 | auto service = util::getService(POWER_OBJ_PATH, |
| 331 | POWER_INTERFACE, |
| 332 | bus); |
| 333 | |
| 334 | // Use getProperty utility function to get power state. |
| 335 | util::getProperty<int32_t>(POWER_INTERFACE, |
| 336 | "state", |
| 337 | POWER_OBJ_PATH, |
| 338 | service, |
| 339 | bus, |
| 340 | state); |
| 341 | |
| 342 | if (state) |
| 343 | { |
| 344 | powerOn = true; |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | powerOn = false; |
| 349 | } |
| 350 | } |
| 351 | catch (std::exception& e) |
| 352 | { |
| 353 | log<level::INFO>("Failed to get power state. Assuming it is off."); |
| 354 | powerOn = false; |
| 355 | } |
| 356 | |
| 357 | } |
| 358 | |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 359 | void PowerSupply::clearFaults() |
| 360 | { |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 361 | //TODO - Clear faults at pre-poweron. openbmc/openbmc#1736 |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 362 | return; |
| 363 | } |
| 364 | |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | } |