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 */ 16 #include <phosphor-logging/log.hpp> 17 #include <phosphor-logging/elog.hpp> 18 #include <org/open_power/Witherspoon/Fault/error.hpp> 19 #include <xyz/openbmc_project/Common/Device/error.hpp> 20 #include "elog-errors.hpp" 21 #include "names_values.hpp" 22 #include "power_supply.hpp" 23 #include "pmbus.hpp" 24 #include "utility.hpp" 25 26 using namespace phosphor::logging; 27 using namespace sdbusplus::org::open_power::Witherspoon::Fault::Error; 28 using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error; 29 30 namespace witherspoon 31 { 32 namespace power 33 { 34 namespace psu 35 { 36 37 constexpr auto INVENTORY_OBJ_PATH = "/xyz/openbmc_project/inventory"; 38 constexpr auto INVENTORY_INTERFACE = "xyz.openbmc_project.Inventory.Item"; 39 constexpr auto PRESENT_PROP = "Present"; 40 constexpr auto POWER_OBJ_PATH = "/org/openbmc/control/power0"; 41 constexpr auto POWER_INTERFACE = "org.openbmc.control.Power"; 42 43 PowerSupply::PowerSupply(const std::string& name, size_t inst, 44 const std::string& objpath, 45 const std::string& invpath, 46 sdbusplus::bus::bus& bus, 47 event::Event& e, 48 std::chrono::seconds& t) 49 : Device(name, inst), monitorPath(objpath), pmbusIntf(objpath), 50 inventoryPath(invpath), bus(bus), event(e), powerOnInterval(t), 51 powerOnTimer(e, [this]() 52 { 53 this->powerOn = true; 54 }) 55 { 56 using namespace sdbusplus::bus; 57 auto present_obj_path = INVENTORY_OBJ_PATH + inventoryPath; 58 presentMatch = std::make_unique<match_t>(bus, 59 match::rules::propertiesChanged( 60 present_obj_path, 61 INVENTORY_INTERFACE), 62 [this](auto& msg) 63 { 64 this->inventoryChanged(msg); 65 }); 66 // Get initial presence state. 67 updatePresence(); 68 69 // Subscribe to power state changes 70 powerOnMatch = std::make_unique<match_t>(bus, 71 match::rules::propertiesChanged( 72 POWER_OBJ_PATH, 73 POWER_INTERFACE), 74 [this](auto& msg) 75 { 76 this->powerStateChanged(msg); 77 }); 78 // Get initial power state. 79 updatePowerState(); 80 } 81 82 void PowerSupply::captureCmd(util::NamesValues& nv, const std::string& cmd, 83 witherspoon::pmbus::Type type) 84 { 85 if (pmbusIntf.exists(cmd, type)) 86 { 87 try 88 { 89 auto val = pmbusIntf.read(cmd, type); 90 nv.add(cmd, val); 91 } 92 catch (std::exception& e) 93 { 94 log<level::INFO>("Unable to capture metadata", entry("CMD=%s", 95 cmd)); 96 } 97 } 98 } 99 100 void PowerSupply::analyze() 101 { 102 using namespace witherspoon::pmbus; 103 104 try 105 { 106 if (present) 107 { 108 std::uint16_t statusWord = 0; 109 110 // Read the 2 byte STATUS_WORD value to check for faults. 111 statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug); 112 113 //TODO: 3 consecutive reads should be performed. 114 // If 3 consecutive reads are seen, log the fault. 115 // Driver gives cached value, read once a second. 116 // increment for fault on, decrement for fault off, to deglitch. 117 // If count reaches 3, we have fault. If count reaches 0, fault is 118 // cleared. 119 120 checkInputFault(statusWord); 121 122 if (powerOn) 123 { 124 checkFanFault(statusWord); 125 checkTemperatureFault(statusWord); 126 checkOutputOvervoltageFault(statusWord); 127 checkCurrentOutOverCurrentFault(statusWord); 128 checkPGOrUnitOffFault(statusWord); 129 } 130 } 131 } 132 catch (ReadFailure& e) 133 { 134 if (!readFailLogged) 135 { 136 commit<ReadFailure>(); 137 readFailLogged = true; 138 } 139 } 140 141 return; 142 } 143 144 void PowerSupply::inventoryChanged(sdbusplus::message::message& msg) 145 { 146 std::string msgSensor; 147 std::map<std::string, sdbusplus::message::variant<uint32_t, bool>> msgData; 148 msg.read(msgSensor, msgData); 149 150 // Check if it was the Present property that changed. 151 auto valPropMap = msgData.find(PRESENT_PROP); 152 if (valPropMap != msgData.end()) 153 { 154 present = sdbusplus::message::variant_ns::get<bool>(valPropMap->second); 155 156 if (present) 157 { 158 clearFaults(); 159 } 160 } 161 162 return; 163 } 164 165 void PowerSupply::updatePresence() 166 { 167 // Use getProperty utility function to get presence status. 168 std::string path = INVENTORY_OBJ_PATH + inventoryPath; 169 std::string service = "xyz.openbmc_project.Inventory.Manager"; 170 171 util::getProperty(INVENTORY_INTERFACE, PRESENT_PROP, path,service, bus, 172 this->present); 173 } 174 175 void PowerSupply::powerStateChanged(sdbusplus::message::message& msg) 176 { 177 int32_t state = 0; 178 std::string msgSensor; 179 std::map<std::string, sdbusplus::message::variant<int32_t, int32_t>> 180 msgData; 181 msg.read(msgSensor, msgData); 182 183 // Check if it was the Present property that changed. 184 auto valPropMap = msgData.find("state"); 185 if (valPropMap != msgData.end()) 186 { 187 state = sdbusplus::message::variant_ns::get<int32_t>(valPropMap->second); 188 189 // Power is on when state=1. Set the fault logged variables to false 190 // and start the power on timer when the state changes to 1. 191 if (state) 192 { 193 clearFaults(); 194 powerOnTimer.start(powerOnInterval, Timer::TimerType::oneshot); 195 } 196 else 197 { 198 powerOnTimer.stop(); 199 powerOn = false; 200 } 201 } 202 203 } 204 205 void PowerSupply::updatePowerState() 206 { 207 // When state = 1, system is powered on 208 int32_t state = 0; 209 210 try 211 { 212 auto service = util::getService(POWER_OBJ_PATH, 213 POWER_INTERFACE, 214 bus); 215 216 // Use getProperty utility function to get power state. 217 util::getProperty<int32_t>(POWER_INTERFACE, 218 "state", 219 POWER_OBJ_PATH, 220 service, 221 bus, 222 state); 223 224 if (state) 225 { 226 powerOn = true; 227 } 228 else 229 { 230 powerOn = false; 231 } 232 } 233 catch (std::exception& e) 234 { 235 log<level::INFO>("Failed to get power state. Assuming it is off."); 236 powerOn = false; 237 } 238 239 } 240 241 void PowerSupply::checkInputFault(const uint16_t statusWord) 242 { 243 using namespace witherspoon::pmbus; 244 245 std::uint8_t statusInput = 0; 246 247 if ((statusWord & status_word::VIN_UV_FAULT) && !vinUVFault) 248 { 249 vinUVFault = true; 250 251 util::NamesValues nv; 252 nv.add("STATUS_WORD", statusWord); 253 254 using metadata = org::open_power::Witherspoon::Fault:: 255 PowerSupplyUnderVoltageFault; 256 257 report<PowerSupplyUnderVoltageFault>(metadata::RAW_STATUS( 258 nv.get().c_str())); 259 } 260 else 261 { 262 if (vinUVFault) 263 { 264 vinUVFault = false; 265 log<level::INFO>("VIN_UV_FAULT cleared", 266 entry("POWERSUPPLY=%s", 267 inventoryPath.c_str())); 268 } 269 } 270 271 if ((statusWord & status_word::INPUT_FAULT_WARN) && !inputFault) 272 { 273 inputFault = true; 274 275 util::NamesValues nv; 276 nv.add("STATUS_WORD", statusWord); 277 captureCmd(nv, STATUS_INPUT, Type::Debug); 278 279 using metadata = org::open_power::Witherspoon::Fault:: 280 PowerSupplyInputFault; 281 282 report<PowerSupplyInputFault>( 283 metadata::RAW_STATUS(nv.get().c_str())); 284 } 285 else 286 { 287 if ((inputFault) && 288 !(statusWord & status_word::INPUT_FAULT_WARN)) 289 { 290 inputFault = false; 291 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug); 292 293 log<level::INFO>("INPUT_FAULT_WARN cleared", 294 entry("POWERSUPPLY=%s", inventoryPath.c_str()), 295 entry("STATUS_WORD=0x%04X", statusWord), 296 entry("STATUS_INPUT=0x%02X", statusInput)); 297 } 298 } 299 } 300 301 void PowerSupply::checkPGOrUnitOffFault(const uint16_t statusWord) 302 { 303 using namespace witherspoon::pmbus; 304 305 if (powerOnFault < FAULT_COUNT) 306 { 307 // Check PG# and UNIT_IS_OFF 308 if ((statusWord & status_word::POWER_GOOD_NEGATED) || 309 (statusWord & status_word::UNIT_IS_OFF)) 310 { 311 log<level::INFO>("PGOOD or UNIT_IS_OFF bit bad", 312 entry("STATUS_WORD=0x%04X", statusWord)); 313 powerOnFault++; 314 } 315 else 316 { 317 if (powerOnFault > 0) 318 { 319 log<level::INFO>("PGOOD and UNIT_IS_OFF bits good"); 320 powerOnFault = 0; 321 } 322 } 323 324 if (powerOnFault >= FAULT_COUNT) 325 { 326 util::NamesValues nv; 327 nv.add("STATUS_WORD", statusWord); 328 captureCmd(nv, STATUS_INPUT, Type::Debug); 329 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0); 330 captureCmd(nv, status0Vout, Type::Debug); 331 captureCmd(nv, STATUS_IOUT, Type::Debug); 332 captureCmd(nv, STATUS_MFR, Type::Debug); 333 334 using metadata = org::open_power::Witherspoon::Fault:: 335 PowerSupplyShouldBeOn; 336 337 // A power supply is OFF (or pgood low) but should be on. 338 report<PowerSupplyShouldBeOn>( 339 metadata::RAW_STATUS(nv.get().c_str()), 340 metadata::CALLOUT_INVENTORY_PATH( 341 inventoryPath.c_str())); 342 } 343 } 344 345 } 346 347 void PowerSupply::checkCurrentOutOverCurrentFault(const uint16_t statusWord) 348 { 349 using namespace witherspoon::pmbus; 350 351 // Check for an output overcurrent fault. 352 if ((statusWord & status_word::IOUT_OC_FAULT) && 353 !outputOCFault) 354 { 355 util::NamesValues nv; 356 nv.add("STATUS_WORD", statusWord); 357 captureCmd(nv, STATUS_INPUT, Type::Debug); 358 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0); 359 captureCmd(nv, status0Vout, Type::Debug); 360 captureCmd(nv, STATUS_IOUT, Type::Debug); 361 captureCmd(nv, STATUS_MFR, Type::Debug); 362 363 using metadata = org::open_power::Witherspoon::Fault:: 364 PowerSupplyOutputOvercurrent; 365 366 report<PowerSupplyOutputOvercurrent>(metadata::RAW_STATUS( 367 nv.get().c_str()), 368 metadata::CALLOUT_INVENTORY_PATH( 369 inventoryPath.c_str())); 370 371 outputOCFault = true; 372 } 373 } 374 375 void PowerSupply::checkOutputOvervoltageFault(const uint16_t statusWord) 376 { 377 using namespace witherspoon::pmbus; 378 379 // Check for an output overvoltage fault. 380 if ((statusWord & status_word::VOUT_OV_FAULT) && 381 !outputOVFault) 382 { 383 util::NamesValues nv; 384 nv.add("STATUS_WORD", statusWord); 385 captureCmd(nv, STATUS_INPUT, Type::Debug); 386 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0); 387 captureCmd(nv, status0Vout, Type::Debug); 388 captureCmd(nv, STATUS_IOUT, Type::Debug); 389 captureCmd(nv, STATUS_MFR, Type::Debug); 390 391 using metadata = org::open_power::Witherspoon::Fault:: 392 PowerSupplyOutputOvervoltage; 393 394 report<PowerSupplyOutputOvervoltage>(metadata::RAW_STATUS( 395 nv.get().c_str()), 396 metadata::CALLOUT_INVENTORY_PATH( 397 inventoryPath.c_str())); 398 399 outputOVFault = true; 400 } 401 } 402 403 void PowerSupply::checkFanFault(const uint16_t statusWord) 404 { 405 using namespace witherspoon::pmbus; 406 407 // Check for a fan fault or warning condition 408 if ((statusWord & status_word::FAN_FAULT) && 409 !fanFault) 410 { 411 util::NamesValues nv; 412 nv.add("STATUS_WORD", statusWord); 413 captureCmd(nv, STATUS_MFR, Type::Debug); 414 captureCmd(nv, STATUS_TEMPERATURE, Type::Debug); 415 captureCmd(nv, STATUS_FANS_1_2, Type::Debug); 416 417 using metadata = org::open_power::Witherspoon::Fault:: 418 PowerSupplyFanFault; 419 420 report<PowerSupplyFanFault>( 421 metadata::RAW_STATUS(nv.get().c_str()), 422 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str())); 423 424 fanFault = true; 425 } 426 } 427 428 void PowerSupply::checkTemperatureFault(const uint16_t statusWord) 429 { 430 using namespace witherspoon::pmbus; 431 432 // Due to how the PMBus core device driver sends a clear faults command 433 // the bit in STATUS_WORD will likely be cleared when we attempt to examine 434 // it for a Thermal Fault or Warning. So, check the STATUS_WORD and the 435 // STATUS_TEMPERATURE bits. If either indicates a fault, proceed with 436 // logging the over-temperature condition. 437 std::uint8_t statusTemperature = 0; 438 statusTemperature = pmbusIntf.read(STATUS_TEMPERATURE, Type::Debug); 439 if (((statusWord & status_word::TEMPERATURE_FAULT_WARN) || 440 (statusTemperature & status_temperature::OT_FAULT)) && 441 !temperatureFault) 442 { 443 // The power supply has had an over-temperature condition. 444 // This may not result in a shutdown if experienced for a short 445 // duration. 446 // This should not occur under normal conditions. 447 // The power supply may be faulty, or the paired supply may be putting 448 // out less current. 449 // Capture command responses with potentially relevant information, 450 // and call out the power supply reporting the condition. 451 util::NamesValues nv; 452 nv.add("STATUS_WORD", statusWord); 453 captureCmd(nv, STATUS_MFR, Type::Debug); 454 captureCmd(nv, STATUS_IOUT, Type::Debug); 455 nv.add("STATUS_TEMPERATURE", statusTemperature); 456 captureCmd(nv, STATUS_FANS_1_2, Type::Debug); 457 458 using metadata = org::open_power::Witherspoon::Fault:: 459 PowerSupplyTemperatureFault; 460 461 report<PowerSupplyTemperatureFault>( 462 metadata::RAW_STATUS(nv.get().c_str()), 463 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str())); 464 465 temperatureFault = true; 466 } 467 } 468 469 void PowerSupply::clearFaults() 470 { 471 readFailLogged = false; 472 vinUVFault = false; 473 inputFault = false; 474 powerOnFault = 0; 475 outputOCFault = false; 476 outputOVFault = false; 477 fanFault = false; 478 temperatureFault = false; 479 480 return; 481 } 482 483 } 484 } 485 } 486