1 #include "config.h" 2 3 #include "psu_manager.hpp" 4 5 #include "utility.hpp" 6 7 #include <fmt/format.h> 8 #include <sys/types.h> 9 #include <unistd.h> 10 11 #include <algorithm> 12 #include <regex> 13 #include <set> 14 15 using namespace phosphor::logging; 16 17 namespace phosphor::power::manager 18 { 19 constexpr auto managerBusName = "xyz.openbmc_project.Power.PSUMonitor"; 20 constexpr auto objectManagerObjPath = 21 "/xyz/openbmc_project/power/power_supplies"; 22 constexpr auto powerSystemsInputsObjPath = 23 "/xyz/openbmc_project/power/power_supplies/chassis0/psus"; 24 25 constexpr auto IBMCFFPSInterface = 26 "xyz.openbmc_project.Configuration.IBMCFFPSConnector"; 27 constexpr auto i2cBusProp = "I2CBus"; 28 constexpr auto i2cAddressProp = "I2CAddress"; 29 constexpr auto psuNameProp = "Name"; 30 constexpr auto presLineName = "NamedPresenceGpio"; 31 32 constexpr auto supportedConfIntf = 33 "xyz.openbmc_project.Configuration.SupportedConfiguration"; 34 35 constexpr auto INPUT_HISTORY_SYNC_DELAY = 5; 36 37 PSUManager::PSUManager(sdbusplus::bus_t& bus, const sdeventplus::Event& e) : 38 bus(bus), powerSystemInputs(bus, powerSystemsInputsObjPath), 39 objectManager(bus, objectManagerObjPath), 40 historyManager(bus, "/org/open_power/sensors") 41 { 42 // Subscribe to InterfacesAdded before doing a property read, otherwise 43 // the interface could be created after the read attempt but before the 44 // match is created. 45 entityManagerIfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>( 46 bus, 47 sdbusplus::bus::match::rules::interfacesAdded() + 48 sdbusplus::bus::match::rules::sender( 49 "xyz.openbmc_project.EntityManager"), 50 std::bind(&PSUManager::entityManagerIfaceAdded, this, 51 std::placeholders::_1)); 52 getPSUConfiguration(); 53 getSystemProperties(); 54 55 // Request the bus name before the analyze() function, which is the one that 56 // determines the brownout condition and sets the status d-bus property. 57 bus.request_name(managerBusName); 58 59 using namespace sdeventplus; 60 auto interval = std::chrono::milliseconds(1000); 61 timer = std::make_unique<utility::Timer<ClockId::Monotonic>>( 62 e, std::bind(&PSUManager::analyze, this), interval); 63 64 validationTimer = std::make_unique<utility::Timer<ClockId::Monotonic>>( 65 e, std::bind(&PSUManager::validateConfig, this)); 66 67 try 68 { 69 powerConfigGPIO = createGPIO("power-config-full-load"); 70 } 71 catch (const std::exception& e) 72 { 73 // Ignore error, GPIO may not be implemented in this system. 74 powerConfigGPIO = nullptr; 75 } 76 77 // Subscribe to power state changes 78 powerService = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus); 79 powerOnMatch = std::make_unique<sdbusplus::bus::match_t>( 80 bus, 81 sdbusplus::bus::match::rules::propertiesChanged(POWER_OBJ_PATH, 82 POWER_IFACE), 83 [this](auto& msg) { this->powerStateChanged(msg); }); 84 85 initialize(); 86 } 87 88 void PSUManager::initialize() 89 { 90 try 91 { 92 // pgood is the latest read of the chassis pgood 93 int pgood = 0; 94 util::getProperty<int>(POWER_IFACE, "pgood", POWER_OBJ_PATH, 95 powerService, bus, pgood); 96 97 // state is the latest requested power on / off transition 98 auto method = bus.new_method_call(POWER_IFACE, POWER_OBJ_PATH, 99 POWER_IFACE, "getPowerState"); 100 auto reply = bus.call(method); 101 int state = 0; 102 reply.read(state); 103 104 if (state) 105 { 106 // Monitor PSUs anytime state is on 107 powerOn = true; 108 // In the power fault window if pgood is off 109 powerFaultOccurring = !pgood; 110 validationTimer->restartOnce(validationTimeout); 111 } 112 else 113 { 114 // Power is off 115 powerOn = false; 116 powerFaultOccurring = false; 117 runValidateConfig = true; 118 } 119 } 120 catch (const std::exception& e) 121 { 122 log<level::INFO>( 123 fmt::format( 124 "Failed to get power state, assuming it is off, error {}", 125 e.what()) 126 .c_str()); 127 powerOn = false; 128 powerFaultOccurring = false; 129 runValidateConfig = true; 130 } 131 132 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY); 133 clearFaults(); 134 updateMissingPSUs(); 135 setPowerConfigGPIO(); 136 137 log<level::INFO>( 138 fmt::format("initialize: power on: {}, power fault occurring: {}", 139 powerOn, powerFaultOccurring) 140 .c_str()); 141 } 142 143 void PSUManager::getPSUConfiguration() 144 { 145 using namespace phosphor::power::util; 146 auto depth = 0; 147 auto objects = getSubTree(bus, "/", IBMCFFPSInterface, depth); 148 149 psus.clear(); 150 151 // I should get a map of objects back. 152 // Each object will have a path, a service, and an interface. 153 // The interface should match the one passed into this function. 154 for (const auto& [path, services] : objects) 155 { 156 auto service = services.begin()->first; 157 158 if (path.empty() || service.empty()) 159 { 160 continue; 161 } 162 163 // For each object in the array of objects, I want to get properties 164 // from the service, path, and interface. 165 auto properties = 166 getAllProperties(bus, path, IBMCFFPSInterface, service); 167 168 getPSUProperties(properties); 169 } 170 171 if (psus.empty()) 172 { 173 // Interface or properties not found. Let the Interfaces Added callback 174 // process the information once the interfaces are added to D-Bus. 175 log<level::INFO>(fmt::format("No power supplies to monitor").c_str()); 176 } 177 } 178 179 void PSUManager::getPSUProperties(util::DbusPropertyMap& properties) 180 { 181 // From passed in properties, I want to get: I2CBus, I2CAddress, 182 // and Name. Create a power supply object, using Name to build the inventory 183 // path. 184 const auto basePSUInvPath = 185 "/xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply"; 186 uint64_t* i2cbus = nullptr; 187 uint64_t* i2caddr = nullptr; 188 std::string* psuname = nullptr; 189 std::string* preslineptr = nullptr; 190 191 for (const auto& property : properties) 192 { 193 try 194 { 195 if (property.first == i2cBusProp) 196 { 197 i2cbus = std::get_if<uint64_t>(&properties[i2cBusProp]); 198 } 199 else if (property.first == i2cAddressProp) 200 { 201 i2caddr = std::get_if<uint64_t>(&properties[i2cAddressProp]); 202 } 203 else if (property.first == psuNameProp) 204 { 205 psuname = std::get_if<std::string>(&properties[psuNameProp]); 206 } 207 else if (property.first == presLineName) 208 { 209 preslineptr = 210 std::get_if<std::string>(&properties[presLineName]); 211 } 212 } 213 catch (const std::exception& e) 214 {} 215 } 216 217 if ((i2cbus) && (i2caddr) && (psuname) && (!psuname->empty())) 218 { 219 std::string invpath = basePSUInvPath; 220 invpath.push_back(psuname->back()); 221 std::string presline = ""; 222 223 log<level::DEBUG>(fmt::format("Inventory Path: {}", invpath).c_str()); 224 225 if (nullptr != preslineptr) 226 { 227 presline = *preslineptr; 228 } 229 230 auto invMatch = 231 std::find_if(psus.begin(), psus.end(), [&invpath](auto& psu) { 232 return psu->getInventoryPath() == invpath; 233 }); 234 if (invMatch != psus.end()) 235 { 236 // This power supply has the same inventory path as the one with 237 // information just added to D-Bus. 238 // Changes to GPIO line name unlikely, so skip checking. 239 // Changes to the I2C bus and address unlikely, as that would 240 // require corresponding device tree updates. 241 // Return out to avoid duplicate object creation. 242 return; 243 } 244 245 constexpr auto driver = "ibm-cffps"; 246 log<level::DEBUG>( 247 fmt::format( 248 "make PowerSupply bus: {} addr: {} driver: {} presline: {}", 249 *i2cbus, *i2caddr, driver, presline) 250 .c_str()); 251 auto psu = std::make_unique<PowerSupply>(bus, invpath, *i2cbus, 252 *i2caddr, driver, presline); 253 psus.emplace_back(std::move(psu)); 254 255 // Subscribe to power supply presence changes 256 auto presenceMatch = std::make_unique<sdbusplus::bus::match_t>( 257 bus, 258 sdbusplus::bus::match::rules::propertiesChanged(invpath, 259 INVENTORY_IFACE), 260 [this](auto& msg) { this->presenceChanged(msg); }); 261 presenceMatches.emplace_back(std::move(presenceMatch)); 262 } 263 264 if (psus.empty()) 265 { 266 log<level::INFO>(fmt::format("No power supplies to monitor").c_str()); 267 } 268 } 269 270 void PSUManager::populateSysProperties(const util::DbusPropertyMap& properties) 271 { 272 try 273 { 274 auto propIt = properties.find("SupportedType"); 275 if (propIt == properties.end()) 276 { 277 return; 278 } 279 const std::string* type = std::get_if<std::string>(&(propIt->second)); 280 if ((type == nullptr) || (*type != "PowerSupply")) 281 { 282 return; 283 } 284 285 propIt = properties.find("SupportedModel"); 286 if (propIt == properties.end()) 287 { 288 return; 289 } 290 const std::string* model = std::get_if<std::string>(&(propIt->second)); 291 if (model == nullptr) 292 { 293 return; 294 } 295 296 sys_properties sys; 297 propIt = properties.find("RedundantCount"); 298 if (propIt != properties.end()) 299 { 300 const uint64_t* count = std::get_if<uint64_t>(&(propIt->second)); 301 if (count != nullptr) 302 { 303 sys.powerSupplyCount = *count; 304 } 305 } 306 propIt = properties.find("InputVoltage"); 307 if (propIt != properties.end()) 308 { 309 const std::vector<uint64_t>* voltage = 310 std::get_if<std::vector<uint64_t>>(&(propIt->second)); 311 if (voltage != nullptr) 312 { 313 sys.inputVoltage = *voltage; 314 } 315 } 316 317 // The PowerConfigFullLoad is an optional property, default it to false 318 // since that's the default value of the power-config-full-load GPIO. 319 sys.powerConfigFullLoad = false; 320 propIt = properties.find("PowerConfigFullLoad"); 321 if (propIt != properties.end()) 322 { 323 const bool* fullLoad = std::get_if<bool>(&(propIt->second)); 324 if (fullLoad != nullptr) 325 { 326 sys.powerConfigFullLoad = *fullLoad; 327 } 328 } 329 330 supportedConfigs.emplace(*model, sys); 331 } 332 catch (const std::exception& e) 333 {} 334 } 335 336 void PSUManager::getSystemProperties() 337 { 338 339 try 340 { 341 util::DbusSubtree subtree = 342 util::getSubTree(bus, INVENTORY_OBJ_PATH, supportedConfIntf, 0); 343 if (subtree.empty()) 344 { 345 throw std::runtime_error("Supported Configuration Not Found"); 346 } 347 348 for (const auto& [objPath, services] : subtree) 349 { 350 std::string service = services.begin()->first; 351 if (objPath.empty() || service.empty()) 352 { 353 continue; 354 } 355 auto properties = util::getAllProperties( 356 bus, objPath, supportedConfIntf, service); 357 populateSysProperties(properties); 358 } 359 } 360 catch (const std::exception& e) 361 { 362 // Interface or property not found. Let the Interfaces Added callback 363 // process the information once the interfaces are added to D-Bus. 364 } 365 } 366 367 void PSUManager::entityManagerIfaceAdded(sdbusplus::message_t& msg) 368 { 369 try 370 { 371 sdbusplus::message::object_path objPath; 372 std::map<std::string, std::map<std::string, util::DbusVariant>> 373 interfaces; 374 msg.read(objPath, interfaces); 375 376 auto itIntf = interfaces.find(supportedConfIntf); 377 if (itIntf != interfaces.cend()) 378 { 379 populateSysProperties(itIntf->second); 380 updateMissingPSUs(); 381 } 382 383 itIntf = interfaces.find(IBMCFFPSInterface); 384 if (itIntf != interfaces.cend()) 385 { 386 log<level::INFO>( 387 fmt::format("InterfacesAdded for: {}", IBMCFFPSInterface) 388 .c_str()); 389 getPSUProperties(itIntf->second); 390 updateMissingPSUs(); 391 } 392 393 // Call to validate the psu configuration if the power is on and both 394 // the IBMCFFPSConnector and SupportedConfiguration interfaces have been 395 // processed 396 if (powerOn && !psus.empty() && !supportedConfigs.empty()) 397 { 398 validationTimer->restartOnce(validationTimeout); 399 } 400 } 401 catch (const std::exception& e) 402 { 403 // Ignore, the property may be of a different type than expected. 404 } 405 } 406 407 void PSUManager::powerStateChanged(sdbusplus::message_t& msg) 408 { 409 std::string msgSensor; 410 std::map<std::string, std::variant<int>> msgData; 411 msg.read(msgSensor, msgData); 412 413 // Check if it was the state property that changed. 414 auto valPropMap = msgData.find("state"); 415 if (valPropMap != msgData.end()) 416 { 417 int state = std::get<int>(valPropMap->second); 418 if (state) 419 { 420 // Power on requested 421 powerOn = true; 422 powerFaultOccurring = false; 423 validationTimer->restartOnce(validationTimeout); 424 clearFaults(); 425 syncHistory(); 426 setPowerConfigGPIO(); 427 } 428 else 429 { 430 // Power off requested 431 powerOn = false; 432 powerFaultOccurring = false; 433 runValidateConfig = true; 434 } 435 } 436 437 // Check if it was the pgood property that changed. 438 valPropMap = msgData.find("pgood"); 439 if (valPropMap != msgData.end()) 440 { 441 int pgood = std::get<int>(valPropMap->second); 442 if (!pgood) 443 { 444 // Chassis power good has turned off 445 if (powerOn) 446 { 447 // pgood is off but state is on, in power fault window 448 powerFaultOccurring = true; 449 } 450 } 451 } 452 log<level::INFO>( 453 fmt::format( 454 "powerStateChanged: power on: {}, power fault occurring: {}", 455 powerOn, powerFaultOccurring) 456 .c_str()); 457 } 458 459 void PSUManager::presenceChanged(sdbusplus::message_t& msg) 460 { 461 std::string msgSensor; 462 std::map<std::string, std::variant<uint32_t, bool>> msgData; 463 msg.read(msgSensor, msgData); 464 465 // Check if it was the Present property that changed. 466 auto valPropMap = msgData.find(PRESENT_PROP); 467 if (valPropMap != msgData.end()) 468 { 469 if (std::get<bool>(valPropMap->second)) 470 { 471 // A PSU became present, force the PSU validation to run. 472 runValidateConfig = true; 473 validationTimer->restartOnce(validationTimeout); 474 } 475 } 476 } 477 478 void PSUManager::setPowerSupplyError(const std::string& psuErrorString) 479 { 480 using namespace sdbusplus::xyz::openbmc_project; 481 constexpr auto service = "org.openbmc.control.Power"; 482 constexpr auto objPath = "/org/openbmc/control/power0"; 483 constexpr auto interface = "org.openbmc.control.Power"; 484 constexpr auto method = "setPowerSupplyError"; 485 486 try 487 { 488 // Call D-Bus method to inform pseq of PSU error 489 auto methodMsg = 490 bus.new_method_call(service, objPath, interface, method); 491 methodMsg.append(psuErrorString); 492 auto callReply = bus.call(methodMsg); 493 } 494 catch (const std::exception& e) 495 { 496 log<level::INFO>( 497 fmt::format("Failed calling setPowerSupplyError due to error {}", 498 e.what()) 499 .c_str()); 500 } 501 } 502 503 void PSUManager::createError(const std::string& faultName, 504 std::map<std::string, std::string>& additionalData) 505 { 506 using namespace sdbusplus::xyz::openbmc_project; 507 constexpr auto loggingObjectPath = "/xyz/openbmc_project/logging"; 508 constexpr auto loggingCreateInterface = 509 "xyz.openbmc_project.Logging.Create"; 510 511 try 512 { 513 additionalData["_PID"] = std::to_string(getpid()); 514 515 auto service = 516 util::getService(loggingObjectPath, loggingCreateInterface, bus); 517 518 if (service.empty()) 519 { 520 log<level::ERR>("Unable to get logging manager service"); 521 return; 522 } 523 524 auto method = bus.new_method_call(service.c_str(), loggingObjectPath, 525 loggingCreateInterface, "Create"); 526 527 auto level = Logging::server::Entry::Level::Error; 528 method.append(faultName, level, additionalData); 529 530 auto reply = bus.call(method); 531 setPowerSupplyError(faultName); 532 } 533 catch (const std::exception& e) 534 { 535 log<level::ERR>( 536 fmt::format( 537 "Failed creating event log for fault {} due to error {}", 538 faultName, e.what()) 539 .c_str()); 540 } 541 } 542 543 void PSUManager::syncHistory() 544 { 545 log<level::INFO>("Synchronize INPUT_HISTORY"); 546 547 if (!syncHistoryGPIO) 548 { 549 syncHistoryGPIO = createGPIO(INPUT_HISTORY_SYNC_GPIO); 550 } 551 if (syncHistoryGPIO) 552 { 553 const std::chrono::milliseconds delay{INPUT_HISTORY_SYNC_DELAY}; 554 syncHistoryGPIO->toggleLowHigh(delay); 555 for (auto& psu : psus) 556 { 557 psu->clearSyncHistoryRequired(); 558 } 559 } 560 561 log<level::INFO>("Synchronize INPUT_HISTORY completed"); 562 } 563 564 void PSUManager::analyze() 565 { 566 auto syncHistoryRequired = 567 std::any_of(psus.begin(), psus.end(), [](const auto& psu) { 568 return psu->isSyncHistoryRequired(); 569 }); 570 if (syncHistoryRequired) 571 { 572 syncHistory(); 573 } 574 575 for (auto& psu : psus) 576 { 577 psu->analyze(); 578 } 579 580 std::map<std::string, std::string> additionalData; 581 582 auto notPresentCount = decltype(psus.size())( 583 std::count_if(psus.begin(), psus.end(), 584 [](const auto& psu) { return !psu->isPresent(); })); 585 586 auto hasVINUVFaultCount = decltype(psus.size())( 587 std::count_if(psus.begin(), psus.end(), [](const auto& psu) { 588 return (psu->isPresent() && psu->hasVINUVFault()); 589 })); 590 591 // The PSU D-Bus objects may not be available yet, so ignore if all 592 // PSUs are not present or the number of PSUs is still 0. 593 if ((psus.size() == (notPresentCount + hasVINUVFaultCount)) && 594 (psus.size() != notPresentCount) && (psus.size() != 0)) 595 { 596 // Brownout: All PSUs report an AC failure: At least one PSU reports 597 // AC loss VIN fault and the rest either report AC loss VIN fault as 598 // well or are not present. 599 additionalData["NOT_PRESENT_COUNT"] = std::to_string(notPresentCount); 600 additionalData["VIN_FAULT_COUNT"] = std::to_string(hasVINUVFaultCount); 601 setBrownout(additionalData); 602 } 603 else 604 { 605 // Brownout condition is not present or has been cleared 606 clearBrownout(); 607 } 608 609 if (powerOn) 610 { 611 for (auto& psu : psus) 612 { 613 additionalData.clear(); 614 615 if (!psu->isFaultLogged() && !psu->isPresent()) 616 { 617 std::map<std::string, std::string> requiredPSUsData; 618 auto requiredPSUsPresent = hasRequiredPSUs(requiredPSUsData); 619 if (!requiredPSUsPresent && isRequiredPSU(*psu)) 620 { 621 additionalData.merge(requiredPSUsData); 622 // Create error for power supply missing. 623 additionalData["CALLOUT_INVENTORY_PATH"] = 624 psu->getInventoryPath(); 625 additionalData["CALLOUT_PRIORITY"] = "H"; 626 createError( 627 "xyz.openbmc_project.Power.PowerSupply.Error.Missing", 628 additionalData); 629 } 630 psu->setFaultLogged(); 631 } 632 else if (!psu->isFaultLogged() && psu->isFaulted()) 633 { 634 // Add STATUS_WORD and STATUS_MFR last response, in padded 635 // hexadecimal format. 636 additionalData["STATUS_WORD"] = 637 fmt::format("{:#04x}", psu->getStatusWord()); 638 additionalData["STATUS_MFR"] = 639 fmt::format("{:#02x}", psu->getMFRFault()); 640 // If there are faults being reported, they possibly could be 641 // related to a bug in the firmware version running on the power 642 // supply. Capture that data into the error as well. 643 additionalData["FW_VERSION"] = psu->getFWVersion(); 644 645 if (psu->hasCommFault()) 646 { 647 additionalData["STATUS_CML"] = 648 fmt::format("{:#02x}", psu->getStatusCML()); 649 /* Attempts to communicate with the power supply have 650 * reached there limit. Create an error. */ 651 additionalData["CALLOUT_DEVICE_PATH"] = 652 psu->getDevicePath(); 653 654 createError( 655 "xyz.openbmc_project.Power.PowerSupply.Error.CommFault", 656 additionalData); 657 658 psu->setFaultLogged(); 659 } 660 else if ((psu->hasInputFault() || psu->hasVINUVFault())) 661 { 662 // Include STATUS_INPUT for input faults. 663 additionalData["STATUS_INPUT"] = 664 fmt::format("{:#02x}", psu->getStatusInput()); 665 666 /* The power supply location might be needed if the input 667 * fault is due to a problem with the power supply itself. 668 * Include the inventory path with a call out priority of 669 * low. 670 */ 671 additionalData["CALLOUT_INVENTORY_PATH"] = 672 psu->getInventoryPath(); 673 additionalData["CALLOUT_PRIORITY"] = "L"; 674 createError("xyz.openbmc_project.Power.PowerSupply.Error." 675 "InputFault", 676 additionalData); 677 psu->setFaultLogged(); 678 } 679 else if (psu->hasPSKillFault()) 680 { 681 createError( 682 "xyz.openbmc_project.Power.PowerSupply.Error.PSKillFault", 683 additionalData); 684 psu->setFaultLogged(); 685 } 686 else if (psu->hasVoutOVFault()) 687 { 688 // Include STATUS_VOUT for Vout faults. 689 additionalData["STATUS_VOUT"] = 690 fmt::format("{:#02x}", psu->getStatusVout()); 691 692 additionalData["CALLOUT_INVENTORY_PATH"] = 693 psu->getInventoryPath(); 694 695 createError( 696 "xyz.openbmc_project.Power.PowerSupply.Error.Fault", 697 additionalData); 698 699 psu->setFaultLogged(); 700 } 701 else if (psu->hasIoutOCFault()) 702 { 703 // Include STATUS_IOUT for Iout faults. 704 additionalData["STATUS_IOUT"] = 705 fmt::format("{:#02x}", psu->getStatusIout()); 706 707 createError( 708 "xyz.openbmc_project.Power.PowerSupply.Error.IoutOCFault", 709 additionalData); 710 711 psu->setFaultLogged(); 712 } 713 else if (psu->hasVoutUVFault() || psu->hasPS12VcsFault() || 714 psu->hasPSCS12VFault()) 715 { 716 // Include STATUS_VOUT for Vout faults. 717 additionalData["STATUS_VOUT"] = 718 fmt::format("{:#02x}", psu->getStatusVout()); 719 720 additionalData["CALLOUT_INVENTORY_PATH"] = 721 psu->getInventoryPath(); 722 723 createError( 724 "xyz.openbmc_project.Power.PowerSupply.Error.Fault", 725 additionalData); 726 727 psu->setFaultLogged(); 728 } 729 // A fan fault should have priority over a temperature fault, 730 // since a failed fan may lead to a temperature problem. 731 // Only process if not in power fault window. 732 else if (psu->hasFanFault() && !powerFaultOccurring) 733 { 734 // Include STATUS_TEMPERATURE and STATUS_FANS_1_2 735 additionalData["STATUS_TEMPERATURE"] = 736 fmt::format("{:#02x}", psu->getStatusTemperature()); 737 additionalData["STATUS_FANS_1_2"] = 738 fmt::format("{:#02x}", psu->getStatusFans12()); 739 740 additionalData["CALLOUT_INVENTORY_PATH"] = 741 psu->getInventoryPath(); 742 743 createError( 744 "xyz.openbmc_project.Power.PowerSupply.Error.FanFault", 745 additionalData); 746 747 psu->setFaultLogged(); 748 } 749 else if (psu->hasTempFault()) 750 { 751 // Include STATUS_TEMPERATURE for temperature faults. 752 additionalData["STATUS_TEMPERATURE"] = 753 fmt::format("{:#02x}", psu->getStatusTemperature()); 754 755 additionalData["CALLOUT_INVENTORY_PATH"] = 756 psu->getInventoryPath(); 757 758 createError( 759 "xyz.openbmc_project.Power.PowerSupply.Error.Fault", 760 additionalData); 761 762 psu->setFaultLogged(); 763 } 764 else if (psu->hasMFRFault()) 765 { 766 /* This can represent a variety of faults that result in 767 * calling out the power supply for replacement: Output 768 * OverCurrent, Output Under Voltage, and potentially other 769 * faults. 770 * 771 * Also plan on putting specific fault in AdditionalData, 772 * along with register names and register values 773 * (STATUS_WORD, STATUS_MFR, etc.).*/ 774 775 additionalData["CALLOUT_INVENTORY_PATH"] = 776 psu->getInventoryPath(); 777 778 createError( 779 "xyz.openbmc_project.Power.PowerSupply.Error.Fault", 780 additionalData); 781 782 psu->setFaultLogged(); 783 } 784 // Only process if not in power fault window. 785 else if (psu->hasPgoodFault() && !powerFaultOccurring) 786 { 787 /* POWER_GOOD# is not low, or OFF is on */ 788 additionalData["CALLOUT_INVENTORY_PATH"] = 789 psu->getInventoryPath(); 790 791 createError( 792 "xyz.openbmc_project.Power.PowerSupply.Error.Fault", 793 additionalData); 794 795 psu->setFaultLogged(); 796 } 797 } 798 } 799 } 800 } 801 802 void PSUManager::updateMissingPSUs() 803 { 804 if (supportedConfigs.empty() || psus.empty()) 805 { 806 return; 807 } 808 809 // Power supplies default to missing. If the power supply is present, 810 // the PowerSupply object will update the inventory Present property to 811 // true. If we have less than the required number of power supplies, and 812 // this power supply is missing, update the inventory Present property 813 // to false to indicate required power supply is missing. Avoid 814 // indicating power supply missing if not required. 815 816 auto presentCount = 817 std::count_if(psus.begin(), psus.end(), 818 [](const auto& psu) { return psu->isPresent(); }); 819 820 for (const auto& config : supportedConfigs) 821 { 822 for (const auto& psu : psus) 823 { 824 auto psuModel = psu->getModelName(); 825 auto psuShortName = psu->getShortName(); 826 auto psuInventoryPath = psu->getInventoryPath(); 827 auto relativeInvPath = 828 psuInventoryPath.substr(strlen(INVENTORY_OBJ_PATH)); 829 auto psuPresent = psu->isPresent(); 830 auto presProperty = false; 831 auto propReadFail = false; 832 833 try 834 { 835 presProperty = getPresence(bus, psuInventoryPath); 836 propReadFail = false; 837 } 838 catch (const sdbusplus::exception_t& e) 839 { 840 propReadFail = true; 841 // Relying on property change or interface added to retry. 842 // Log an informational trace to the journal. 843 log<level::INFO>( 844 fmt::format("D-Bus property {} access failure exception", 845 psuInventoryPath) 846 .c_str()); 847 } 848 849 if (psuModel.empty()) 850 { 851 if (!propReadFail && (presProperty != psuPresent)) 852 { 853 // We already have this property, and it is not false 854 // set Present to false 855 setPresence(bus, relativeInvPath, psuPresent, psuShortName); 856 } 857 continue; 858 } 859 860 if (config.first != psuModel) 861 { 862 continue; 863 } 864 865 if ((presentCount < config.second.powerSupplyCount) && !psuPresent) 866 { 867 setPresence(bus, relativeInvPath, psuPresent, psuShortName); 868 } 869 } 870 } 871 } 872 873 void PSUManager::validateConfig() 874 { 875 if (!runValidateConfig || supportedConfigs.empty() || psus.empty()) 876 { 877 return; 878 } 879 880 for (const auto& psu : psus) 881 { 882 if ((psu->hasInputFault() || psu->hasVINUVFault())) 883 { 884 // Do not try to validate if input voltage fault present. 885 validationTimer->restartOnce(validationTimeout); 886 return; 887 } 888 } 889 890 std::map<std::string, std::string> additionalData; 891 auto supported = hasRequiredPSUs(additionalData); 892 if (supported) 893 { 894 runValidateConfig = false; 895 return; 896 } 897 898 // Validation failed, create an error log. 899 // Return without setting the runValidateConfig flag to false because 900 // it may be that an additional supported configuration interface is 901 // added and we need to validate it to see if it matches this system. 902 createError("xyz.openbmc_project.Power.PowerSupply.Error.NotSupported", 903 additionalData); 904 } 905 906 bool PSUManager::hasRequiredPSUs( 907 std::map<std::string, std::string>& additionalData) 908 { 909 std::string model{}; 910 if (!validateModelName(model, additionalData)) 911 { 912 return false; 913 } 914 915 auto presentCount = 916 std::count_if(psus.begin(), psus.end(), 917 [](const auto& psu) { return psu->isPresent(); }); 918 919 // Validate the supported configurations. A system may support more than one 920 // power supply model configuration. Since all configurations need to be 921 // checked, the additional data would contain only the information of the 922 // last configuration that did not match. 923 std::map<std::string, std::string> tmpAdditionalData; 924 for (const auto& config : supportedConfigs) 925 { 926 if (config.first != model) 927 { 928 continue; 929 } 930 931 // Number of power supplies present should equal or exceed the expected 932 // count 933 if (presentCount < config.second.powerSupplyCount) 934 { 935 tmpAdditionalData.clear(); 936 tmpAdditionalData["EXPECTED_COUNT"] = 937 std::to_string(config.second.powerSupplyCount); 938 tmpAdditionalData["ACTUAL_COUNT"] = std::to_string(presentCount); 939 continue; 940 } 941 942 bool voltageValidated = true; 943 for (const auto& psu : psus) 944 { 945 if (!psu->isPresent()) 946 { 947 // Only present PSUs report a valid input voltage 948 continue; 949 } 950 951 double actualInputVoltage; 952 int inputVoltage; 953 psu->getInputVoltage(actualInputVoltage, inputVoltage); 954 955 if (std::find(config.second.inputVoltage.begin(), 956 config.second.inputVoltage.end(), 957 inputVoltage) == config.second.inputVoltage.end()) 958 { 959 tmpAdditionalData.clear(); 960 tmpAdditionalData["ACTUAL_VOLTAGE"] = 961 std::to_string(actualInputVoltage); 962 for (const auto& voltage : config.second.inputVoltage) 963 { 964 tmpAdditionalData["EXPECTED_VOLTAGE"] += 965 std::to_string(voltage) + " "; 966 } 967 tmpAdditionalData["CALLOUT_INVENTORY_PATH"] = 968 psu->getInventoryPath(); 969 970 voltageValidated = false; 971 break; 972 } 973 } 974 if (!voltageValidated) 975 { 976 continue; 977 } 978 979 return true; 980 } 981 982 additionalData.insert(tmpAdditionalData.begin(), tmpAdditionalData.end()); 983 return false; 984 } 985 986 unsigned int PSUManager::getRequiredPSUCount() 987 { 988 unsigned int requiredCount{0}; 989 990 // Verify we have the supported configuration and PSU information 991 if (!supportedConfigs.empty() && !psus.empty()) 992 { 993 // Find PSU models. They should all be the same. 994 std::set<std::string> models{}; 995 std::for_each(psus.begin(), psus.end(), [&models](const auto& psu) { 996 if (!psu->getModelName().empty()) 997 { 998 models.insert(psu->getModelName()); 999 } 1000 }); 1001 1002 // If exactly one model was found, find corresponding configuration 1003 if (models.size() == 1) 1004 { 1005 const std::string& model = *(models.begin()); 1006 auto it = supportedConfigs.find(model); 1007 if (it != supportedConfigs.end()) 1008 { 1009 requiredCount = it->second.powerSupplyCount; 1010 } 1011 } 1012 } 1013 1014 return requiredCount; 1015 } 1016 1017 bool PSUManager::isRequiredPSU(const PowerSupply& psu) 1018 { 1019 // Get required number of PSUs; if not found, we don't know if PSU required 1020 unsigned int requiredCount = getRequiredPSUCount(); 1021 if (requiredCount == 0) 1022 { 1023 return false; 1024 } 1025 1026 // If total PSU count <= the required count, all PSUs are required 1027 if (psus.size() <= requiredCount) 1028 { 1029 return true; 1030 } 1031 1032 // We don't currently get information from EntityManager about which PSUs 1033 // are required, so we have to do some guesswork. First check if this PSU 1034 // is present. If so, assume it is required. 1035 if (psu.isPresent()) 1036 { 1037 return true; 1038 } 1039 1040 // This PSU is not present. Count the number of other PSUs that are 1041 // present. If enough other PSUs are present, assume the specified PSU is 1042 // not required. 1043 unsigned int psuCount = 1044 std::count_if(psus.begin(), psus.end(), 1045 [](const auto& psu) { return psu->isPresent(); }); 1046 if (psuCount >= requiredCount) 1047 { 1048 return false; 1049 } 1050 1051 // Check if this PSU was previously present. If so, assume it is required. 1052 // We know it was previously present if it has a non-empty model name. 1053 if (!psu.getModelName().empty()) 1054 { 1055 return true; 1056 } 1057 1058 // This PSU was never present. Count the number of other PSUs that were 1059 // previously present. If including those PSUs is enough, assume the 1060 // specified PSU is not required. 1061 psuCount += std::count_if(psus.begin(), psus.end(), [](const auto& psu) { 1062 return (!psu->isPresent() && !psu->getModelName().empty()); 1063 }); 1064 if (psuCount >= requiredCount) 1065 { 1066 return false; 1067 } 1068 1069 // We still haven't found enough PSUs. Sort the inventory paths of PSUs 1070 // that were never present. PSU inventory paths typically end with the PSU 1071 // number (0, 1, 2, ...). Assume that lower-numbered PSUs are required. 1072 std::vector<std::string> sortedPaths; 1073 std::for_each(psus.begin(), psus.end(), [&sortedPaths](const auto& psu) { 1074 if (!psu->isPresent() && psu->getModelName().empty()) 1075 { 1076 sortedPaths.push_back(psu->getInventoryPath()); 1077 } 1078 }); 1079 std::sort(sortedPaths.begin(), sortedPaths.end()); 1080 1081 // Check if specified PSU is close enough to start of list to be required 1082 for (const auto& path : sortedPaths) 1083 { 1084 if (path == psu.getInventoryPath()) 1085 { 1086 return true; 1087 } 1088 if (++psuCount >= requiredCount) 1089 { 1090 break; 1091 } 1092 } 1093 1094 // PSU was not close to start of sorted list; assume not required 1095 return false; 1096 } 1097 1098 bool PSUManager::validateModelName( 1099 std::string& model, std::map<std::string, std::string>& additionalData) 1100 { 1101 // Check that all PSUs have the same model name. Initialize the model 1102 // variable with the first PSU name found, then use it as a base to compare 1103 // against the rest of the PSUs and get its inventory path to use as callout 1104 // if needed. 1105 model.clear(); 1106 std::string modelInventoryPath{}; 1107 for (const auto& psu : psus) 1108 { 1109 auto psuModel = psu->getModelName(); 1110 if (psuModel.empty()) 1111 { 1112 continue; 1113 } 1114 if (model.empty()) 1115 { 1116 model = psuModel; 1117 modelInventoryPath = psu->getInventoryPath(); 1118 continue; 1119 } 1120 if (psuModel != model) 1121 { 1122 if (supportedConfigs.find(model) != supportedConfigs.end()) 1123 { 1124 // The base model is supported, callout the mismatched PSU. The 1125 // mismatched PSU may or may not be supported. 1126 additionalData["EXPECTED_MODEL"] = model; 1127 additionalData["ACTUAL_MODEL"] = psuModel; 1128 additionalData["CALLOUT_INVENTORY_PATH"] = 1129 psu->getInventoryPath(); 1130 } 1131 else if (supportedConfigs.find(psuModel) != supportedConfigs.end()) 1132 { 1133 // The base model is not supported, but the mismatched PSU is, 1134 // callout the base PSU. 1135 additionalData["EXPECTED_MODEL"] = psuModel; 1136 additionalData["ACTUAL_MODEL"] = model; 1137 additionalData["CALLOUT_INVENTORY_PATH"] = modelInventoryPath; 1138 } 1139 else 1140 { 1141 // The base model and the mismatched PSU are not supported or 1142 // could not be found in the supported configuration, callout 1143 // the mismatched PSU. 1144 additionalData["EXPECTED_MODEL"] = model; 1145 additionalData["ACTUAL_MODEL"] = psuModel; 1146 additionalData["CALLOUT_INVENTORY_PATH"] = 1147 psu->getInventoryPath(); 1148 } 1149 model.clear(); 1150 return false; 1151 } 1152 } 1153 return true; 1154 } 1155 1156 void PSUManager::setPowerConfigGPIO() 1157 { 1158 if (!powerConfigGPIO) 1159 { 1160 return; 1161 } 1162 1163 std::string model{}; 1164 std::map<std::string, std::string> additionalData; 1165 if (!validateModelName(model, additionalData)) 1166 { 1167 return; 1168 } 1169 1170 auto config = supportedConfigs.find(model); 1171 if (config != supportedConfigs.end()) 1172 { 1173 // The power-config-full-load is an open drain GPIO. Set it to low (0) 1174 // if the supported configuration indicates that this system model 1175 // expects the maximum number of power supplies (full load set to true). 1176 // Else, set it to high (1), this is the default. 1177 auto powerConfigValue = 1178 (config->second.powerConfigFullLoad == true ? 0 : 1); 1179 auto flags = gpiod::line_request::FLAG_OPEN_DRAIN; 1180 powerConfigGPIO->write(powerConfigValue, flags); 1181 } 1182 } 1183 1184 void PSUManager::setBrownout(std::map<std::string, std::string>& additionalData) 1185 { 1186 powerSystemInputs.status(sdbusplus::xyz::openbmc_project::State::Decorator:: 1187 server::PowerSystemInputs::Status::Fault); 1188 if (!brownoutLogged) 1189 { 1190 if (powerOn) 1191 { 1192 createError( 1193 "xyz.openbmc_project.State.Shutdown.Power.Error.Blackout", 1194 additionalData); 1195 brownoutLogged = true; 1196 } 1197 } 1198 } 1199 1200 void PSUManager::clearBrownout() 1201 { 1202 powerSystemInputs.status(sdbusplus::xyz::openbmc_project::State::Decorator:: 1203 server::PowerSystemInputs::Status::Good); 1204 brownoutLogged = false; 1205 } 1206 1207 } // namespace phosphor::power::manager 1208