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