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