1 #include "config.h" 2 3 #include "power_supply.hpp" 4 5 #include "types.hpp" 6 #include "util.hpp" 7 8 #include <fmt/format.h> 9 10 #include <xyz/openbmc_project/Common/Device/error.hpp> 11 12 #include <chrono> // sleep_for() 13 #include <cstdint> // uint8_t... 14 #include <fstream> 15 #include <thread> // sleep_for() 16 17 namespace phosphor::power::psu 18 { 19 // Amount of time in milliseconds to delay between power supply going from 20 // missing to present before running the bind command(s). 21 constexpr auto bindDelay = 1000; 22 23 using namespace phosphor::logging; 24 using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error; 25 26 PowerSupply::PowerSupply(sdbusplus::bus::bus& bus, const std::string& invpath, 27 std::uint8_t i2cbus, std::uint16_t i2caddr, 28 const std::string& gpioLineName) : 29 bus(bus), 30 inventoryPath(invpath), bindPath("/sys/bus/i2c/drivers/ibm-cffps") 31 { 32 if (inventoryPath.empty()) 33 { 34 throw std::invalid_argument{"Invalid empty inventoryPath"}; 35 } 36 37 if (gpioLineName.empty()) 38 { 39 throw std::invalid_argument{"Invalid empty gpioLineName"}; 40 } 41 42 log<level::DEBUG>(fmt::format("gpioLineName: {}", gpioLineName).c_str()); 43 presenceGPIO = createGPIO(gpioLineName); 44 45 std::ostringstream ss; 46 ss << std::hex << std::setw(4) << std::setfill('0') << i2caddr; 47 std::string addrStr = ss.str(); 48 std::string busStr = std::to_string(i2cbus); 49 bindDevice = busStr; 50 bindDevice.append("-"); 51 bindDevice.append(addrStr); 52 53 pmbusIntf = phosphor::pmbus::createPMBus(i2cbus, addrStr); 54 55 // Get the current state of the Present property. 56 try 57 { 58 updatePresenceGPIO(); 59 } 60 catch (...) 61 { 62 // If the above attempt to use the GPIO failed, it likely means that the 63 // GPIOs are in use by the kernel, meaning it is using gpio-keys. 64 // So, I should rely on phosphor-gpio-presence to update D-Bus, and 65 // work that way for power supply presence. 66 presenceGPIO = nullptr; 67 // Setup the functions to call when the D-Bus inventory path for the 68 // Present property changes. 69 presentMatch = std::make_unique<sdbusplus::bus::match_t>( 70 bus, 71 sdbusplus::bus::match::rules::propertiesChanged(inventoryPath, 72 INVENTORY_IFACE), 73 [this](auto& msg) { this->inventoryChanged(msg); }); 74 75 presentAddedMatch = std::make_unique<sdbusplus::bus::match_t>( 76 bus, 77 sdbusplus::bus::match::rules::interfacesAdded() + 78 sdbusplus::bus::match::rules::argNpath(0, inventoryPath), 79 [this](auto& msg) { this->inventoryAdded(msg); }); 80 81 updatePresence(); 82 updateInventory(); 83 } 84 } 85 86 void PowerSupply::bindOrUnbindDriver(bool present) 87 { 88 auto action = (present) ? "bind" : "unbind"; 89 auto path = bindPath / action; 90 91 if (present) 92 { 93 log<level::INFO>( 94 fmt::format("Binding device driver. path: {} device: {}", 95 path.string(), bindDevice) 96 .c_str()); 97 } 98 else 99 { 100 log<level::INFO>( 101 fmt::format("Unbinding device driver. path: {} device: {}", 102 path.string(), bindDevice) 103 .c_str()); 104 } 105 106 std::ofstream file; 107 108 file.exceptions(std::ofstream::failbit | std::ofstream::badbit | 109 std::ofstream::eofbit); 110 111 try 112 { 113 file.open(path); 114 file << bindDevice; 115 file.close(); 116 } 117 catch (const std::exception& e) 118 { 119 auto err = errno; 120 121 log<level::ERR>( 122 fmt::format("Failed binding or unbinding device. errno={}", err) 123 .c_str()); 124 } 125 } 126 127 void PowerSupply::updatePresence() 128 { 129 try 130 { 131 present = getPresence(bus, inventoryPath); 132 } 133 catch (const sdbusplus::exception::exception& e) 134 { 135 // Relying on property change or interface added to retry. 136 // Log an informational trace to the journal. 137 log<level::INFO>( 138 fmt::format("D-Bus property {} access failure exception", 139 inventoryPath) 140 .c_str()); 141 } 142 } 143 144 void PowerSupply::updatePresenceGPIO() 145 { 146 bool presentOld = present; 147 148 try 149 { 150 if (presenceGPIO->read() > 0) 151 { 152 present = true; 153 } 154 else 155 { 156 present = false; 157 } 158 } 159 catch (const std::exception& e) 160 { 161 log<level::ERR>( 162 fmt::format("presenceGPIO read fail: {}", e.what()).c_str()); 163 throw; 164 } 165 166 if (presentOld != present) 167 { 168 log<level::DEBUG>( 169 fmt::format("presentOld: {} present: {}", presentOld, present) 170 .c_str()); 171 if (present) 172 { 173 std::this_thread::sleep_for(std::chrono::milliseconds(bindDelay)); 174 bindOrUnbindDriver(present); 175 pmbusIntf->findHwmonDir(); 176 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY); 177 clearFaults(); 178 } 179 else 180 { 181 bindOrUnbindDriver(present); 182 } 183 184 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH)); 185 auto const lastSlashPos = invpath.find_last_of('/'); 186 std::string prettyName = invpath.substr(lastSlashPos + 1); 187 setPresence(bus, invpath, present, prettyName); 188 updateInventory(); 189 } 190 } 191 192 void PowerSupply::analyze() 193 { 194 using namespace phosphor::pmbus; 195 196 if (presenceGPIO) 197 { 198 updatePresenceGPIO(); 199 } 200 201 if ((present) && (readFail < LOG_LIMIT)) 202 { 203 try 204 { 205 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug); 206 // Read worked, reset the fail count. 207 readFail = 0; 208 209 if (statusWord) 210 { 211 statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug); 212 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug); 213 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug); 214 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0); 215 statusVout = pmbusIntf->read(status0Vout, Type::Debug); 216 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug); 217 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug); 218 statusTemperature = 219 pmbusIntf->read(STATUS_TEMPERATURE, Type::Debug); 220 if (statusWord & status_word::CML_FAULT) 221 { 222 if (!cmlFault) 223 { 224 log<level::ERR>( 225 fmt::format("CML fault: STATUS_WORD = {:#04x}, " 226 "STATUS_CML = {:#02x}", 227 statusWord, statusCML) 228 .c_str()); 229 } 230 231 cmlFault = true; 232 } 233 234 if (statusWord & status_word::INPUT_FAULT_WARN) 235 { 236 if (!inputFault) 237 { 238 log<level::ERR>( 239 fmt::format("INPUT fault: STATUS_WORD = {:#04x}, " 240 "STATUS_MFR_SPECIFIC = {:#02x}, " 241 "STATUS_INPUT = {:#02x}", 242 statusWord, statusMFR, statusInput) 243 .c_str()); 244 } 245 246 inputFault = true; 247 } 248 249 if (statusWord & status_word::VOUT_OV_FAULT) 250 { 251 if (!voutOVFault) 252 { 253 log<level::ERR>( 254 fmt::format( 255 "VOUT_OV_FAULT fault: STATUS_WORD = {:#04x}, " 256 "STATUS_MFR_SPECIFIC = {:#02x}, " 257 "STATUS_VOUT = {:#02x}", 258 statusWord, statusMFR, statusVout) 259 .c_str()); 260 } 261 262 voutOVFault = true; 263 } 264 265 if (statusWord & status_word::IOUT_OC_FAULT) 266 { 267 if (!ioutOCFault) 268 { 269 log<level::ERR>( 270 fmt::format("IOUT fault: STATUS_WORD = {:#04x}, " 271 "STATUS_MFR_SPECIFIC = {:#02x}, " 272 "STATUS_IOUT = {:#02x}", 273 statusWord, statusMFR, statusIout) 274 .c_str()); 275 } 276 277 ioutOCFault = true; 278 } 279 280 if ((statusWord & status_word::VOUT_FAULT) && 281 !(statusWord & status_word::VOUT_OV_FAULT)) 282 { 283 if (!voutUVFault) 284 { 285 log<level::ERR>( 286 fmt::format( 287 "VOUT_UV_FAULT fault: STATUS_WORD = {:#04x}, " 288 "STATUS_MFR_SPECIFIC = {:#02x}, " 289 "STATUS_VOUT = {:#02x}", 290 statusWord, statusMFR, statusVout) 291 .c_str()); 292 } 293 294 voutUVFault = true; 295 } 296 297 if (statusWord & status_word::FAN_FAULT) 298 { 299 if (!fanFault) 300 { 301 log<level::ERR>( 302 fmt::format("FANS fault/warning: " 303 "STATUS_WORD = {:#04x}, " 304 "STATUS_MFR_SPECIFIC = {:#02x}, " 305 "STATUS_FANS_1_2 = {:#02x}", 306 statusWord, statusMFR, statusFans12) 307 .c_str()); 308 } 309 310 fanFault = true; 311 } 312 313 if (statusWord & status_word::TEMPERATURE_FAULT_WARN) 314 { 315 if (!tempFault) 316 { 317 log<level::ERR>( 318 fmt::format("TEMPERATURE fault/warning: " 319 "STATUS_WORD = {:#04x}, " 320 "STATUS_MFR_SPECIFIC = {:#02x}, " 321 "STATUS_TEMPERATURE = {:#02x}", 322 statusWord, statusMFR, 323 statusTemperature) 324 .c_str()); 325 } 326 327 tempFault = true; 328 } 329 330 if ((statusWord & status_word::POWER_GOOD_NEGATED) || 331 (statusWord & status_word::UNIT_IS_OFF)) 332 { 333 if (!pgoodFault) 334 { 335 log<level::ERR>( 336 fmt::format("PGOOD fault: " 337 "STATUS_WORD = {:#04x}, " 338 "STATUS_MFR_SPECIFIC = {:#02x}", 339 statusWord, statusMFR) 340 .c_str()); 341 } 342 343 pgoodFault = true; 344 } 345 346 if (statusWord & status_word::MFR_SPECIFIC_FAULT) 347 { 348 if (!mfrFault) 349 { 350 log<level::ERR>( 351 fmt::format("MFR fault: " 352 "STATUS_WORD = {:#04x} " 353 "STATUS_MFR_SPECIFIC = {:#02x}", 354 statusWord, statusMFR) 355 .c_str()); 356 } 357 358 mfrFault = true; 359 } 360 361 if (statusWord & status_word::VIN_UV_FAULT) 362 { 363 if (!vinUVFault) 364 { 365 log<level::ERR>( 366 fmt::format("VIN_UV fault: STATUS_WORD = {:#04x}, " 367 "STATUS_MFR_SPECIFIC = {:#02x}, " 368 "STATUS_INPUT = {:#02x}", 369 statusWord, statusMFR, statusInput) 370 .c_str()); 371 } 372 373 vinUVFault = true; 374 } 375 } 376 else 377 { 378 cmlFault = false; 379 inputFault = false; 380 mfrFault = false; 381 vinUVFault = false; 382 voutOVFault = false; 383 ioutOCFault = false; 384 voutUVFault = false; 385 fanFault = false; 386 tempFault = false; 387 pgoodFault = false; 388 } 389 } 390 catch (const ReadFailure& e) 391 { 392 readFail++; 393 phosphor::logging::commit<ReadFailure>(); 394 } 395 } 396 } 397 398 void PowerSupply::onOffConfig(uint8_t data) 399 { 400 using namespace phosphor::pmbus; 401 402 if (present) 403 { 404 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data)); 405 try 406 { 407 std::vector<uint8_t> configData{data}; 408 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData, 409 Type::HwmonDeviceDebug); 410 } 411 catch (...) 412 { 413 // The underlying code in writeBinary will log a message to the 414 // journal if the write fails. If the ON_OFF_CONFIG is not setup 415 // as desired, later fault detection and analysis code should 416 // catch any of the fall out. We should not need to terminate 417 // the application if this write fails. 418 } 419 } 420 } 421 422 void PowerSupply::clearFaults() 423 { 424 faultLogged = false; 425 // The PMBus device driver does not allow for writing CLEAR_FAULTS 426 // directly. However, the pmbus hwmon device driver code will send a 427 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so 428 // reading in1_input should result in clearing the fault bits in 429 // STATUS_BYTE/STATUS_WORD. 430 // I do not care what the return value is. 431 if (present) 432 { 433 inputFault = false; 434 mfrFault = false; 435 statusMFR = 0; 436 vinUVFault = false; 437 cmlFault = false; 438 voutOVFault = false; 439 ioutOCFault = false; 440 voutUVFault = false; 441 fanFault = false; 442 tempFault = false; 443 pgoodFault = false; 444 readFail = 0; 445 446 try 447 { 448 static_cast<void>( 449 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon)); 450 } 451 catch (const ReadFailure& e) 452 { 453 // Since I do not care what the return value is, I really do not 454 // care much if it gets a ReadFailure either. However, this 455 // should not prevent the application from continuing to run, so 456 // catching the read failure. 457 } 458 } 459 } 460 461 void PowerSupply::inventoryChanged(sdbusplus::message::message& msg) 462 { 463 std::string msgSensor; 464 std::map<std::string, std::variant<uint32_t, bool>> msgData; 465 msg.read(msgSensor, msgData); 466 467 // Check if it was the Present property that changed. 468 auto valPropMap = msgData.find(PRESENT_PROP); 469 if (valPropMap != msgData.end()) 470 { 471 if (std::get<bool>(valPropMap->second)) 472 { 473 present = true; 474 // TODO: Immediately trying to read or write the "files" causes 475 // read or write failures. 476 using namespace std::chrono_literals; 477 std::this_thread::sleep_for(20ms); 478 pmbusIntf->findHwmonDir(); 479 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY); 480 clearFaults(); 481 updateInventory(); 482 } 483 else 484 { 485 present = false; 486 487 // Clear out the now outdated inventory properties 488 updateInventory(); 489 } 490 } 491 } 492 493 void PowerSupply::inventoryAdded(sdbusplus::message::message& msg) 494 { 495 sdbusplus::message::object_path path; 496 msg.read(path); 497 // Make sure the signal is for the PSU inventory path 498 if (path == inventoryPath) 499 { 500 std::map<std::string, std::map<std::string, std::variant<bool>>> 501 interfaces; 502 // Get map of interfaces and their properties 503 msg.read(interfaces); 504 505 auto properties = interfaces.find(INVENTORY_IFACE); 506 if (properties != interfaces.end()) 507 { 508 auto property = properties->second.find(PRESENT_PROP); 509 if (property != properties->second.end()) 510 { 511 present = std::get<bool>(property->second); 512 513 log<level::INFO>(fmt::format("Power Supply {} Present {}", 514 inventoryPath, present) 515 .c_str()); 516 517 updateInventory(); 518 } 519 } 520 } 521 } 522 523 void PowerSupply::updateInventory() 524 { 525 using namespace phosphor::pmbus; 526 527 #if IBM_VPD 528 std::string ccin; 529 std::string pn; 530 std::string fn; 531 std::string header; 532 std::string sn; 533 using PropertyMap = 534 std::map<std::string, 535 std::variant<std::string, std::vector<uint8_t>, bool>>; 536 PropertyMap assetProps; 537 PropertyMap operProps; 538 PropertyMap versionProps; 539 PropertyMap ipzvpdDINFProps; 540 PropertyMap ipzvpdVINIProps; 541 using InterfaceMap = std::map<std::string, PropertyMap>; 542 InterfaceMap interfaces; 543 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>; 544 ObjectMap object; 545 #endif 546 log<level::DEBUG>( 547 fmt::format("updateInventory() inventoryPath: {}", inventoryPath) 548 .c_str()); 549 550 if (present) 551 { 552 // TODO: non-IBM inventory updates? 553 554 #if IBM_VPD 555 try 556 { 557 ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug); 558 assetProps.emplace(MODEL_PROP, ccin); 559 modelName = ccin; 560 } 561 catch (const ReadFailure& e) 562 { 563 // Ignore the read failure, let pmbus code indicate failure, 564 // path... 565 // TODO - ibm918 566 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md 567 // The BMC must log errors if any of the VPD cannot be properly 568 // parsed or fails ECC checks. 569 } 570 571 try 572 { 573 pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug); 574 assetProps.emplace(PN_PROP, pn); 575 } 576 catch (const ReadFailure& e) 577 { 578 // Ignore the read failure, let pmbus code indicate failure, 579 // path... 580 } 581 582 try 583 { 584 fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug); 585 } 586 catch (const ReadFailure& e) 587 { 588 // Ignore the read failure, let pmbus code indicate failure, 589 // path... 590 } 591 592 try 593 { 594 header = 595 pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug); 596 sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug); 597 assetProps.emplace(SN_PROP, sn); 598 } 599 catch (const ReadFailure& e) 600 { 601 // Ignore the read failure, let pmbus code indicate failure, 602 // path... 603 } 604 605 try 606 { 607 fwVersion = 608 pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug); 609 versionProps.emplace(VERSION_PROP, fwVersion); 610 } 611 catch (const ReadFailure& e) 612 { 613 // Ignore the read failure, let pmbus code indicate failure, 614 // path... 615 } 616 617 ipzvpdVINIProps.emplace("CC", 618 std::vector<uint8_t>(ccin.begin(), ccin.end())); 619 ipzvpdVINIProps.emplace("PN", 620 std::vector<uint8_t>(pn.begin(), pn.end())); 621 ipzvpdVINIProps.emplace("FN", 622 std::vector<uint8_t>(fn.begin(), fn.end())); 623 std::string header_sn = header + sn + '\0'; 624 ipzvpdVINIProps.emplace( 625 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end())); 626 std::string description = "IBM PS"; 627 ipzvpdVINIProps.emplace( 628 "DR", std::vector<uint8_t>(description.begin(), description.end())); 629 630 // Update the Resource Identifier (RI) keyword 631 // 2 byte FRC: 0x0003 632 // 2 byte RID: 0x1000, 0x1001... 633 std::uint8_t num = std::stoul( 634 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0); 635 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num}; 636 ipzvpdDINFProps.emplace("RI", ri); 637 638 // Fill in the FRU Label (FL) keyword. 639 std::string fl = "E"; 640 fl.push_back(inventoryPath.back()); 641 fl.resize(FL_KW_SIZE, ' '); 642 ipzvpdDINFProps.emplace("FL", 643 std::vector<uint8_t>(fl.begin(), fl.end())); 644 645 interfaces.emplace(ASSET_IFACE, std::move(assetProps)); 646 interfaces.emplace(VERSION_IFACE, std::move(versionProps)); 647 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps)); 648 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps)); 649 650 // Update the Functional 651 operProps.emplace(FUNCTIONAL_PROP, present); 652 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps)); 653 654 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH)); 655 object.emplace(path, std::move(interfaces)); 656 657 try 658 { 659 auto service = 660 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus); 661 662 if (service.empty()) 663 { 664 log<level::ERR>("Unable to get inventory manager service"); 665 return; 666 } 667 668 auto method = 669 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH, 670 INVENTORY_MGR_IFACE, "Notify"); 671 672 method.append(std::move(object)); 673 674 auto reply = bus.call(method); 675 } 676 catch (const std::exception& e) 677 { 678 log<level::ERR>( 679 std::string(e.what() + std::string(" PATH=") + inventoryPath) 680 .c_str()); 681 } 682 #endif 683 } 684 } 685 686 void PowerSupply::getInputVoltage(double& actualInputVoltage, 687 int& inputVoltage) const 688 { 689 using namespace phosphor::pmbus; 690 691 actualInputVoltage = in_input::VIN_VOLTAGE_0; 692 inputVoltage = in_input::VIN_VOLTAGE_0; 693 694 if (present) 695 { 696 try 697 { 698 // Read input voltage in millivolts 699 auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon); 700 701 // Convert to volts 702 actualInputVoltage = std::stod(inputVoltageStr) / 1000; 703 704 // Calculate the voltage based on voltage thresholds 705 if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN) 706 { 707 inputVoltage = in_input::VIN_VOLTAGE_0; 708 } 709 else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD) 710 { 711 inputVoltage = in_input::VIN_VOLTAGE_110; 712 } 713 else 714 { 715 inputVoltage = in_input::VIN_VOLTAGE_220; 716 } 717 } 718 catch (const std::exception& e) 719 { 720 log<level::ERR>( 721 fmt::format("READ_VIN read error: {}", e.what()).c_str()); 722 } 723 } 724 } 725 726 } // namespace phosphor::power::psu 727