| 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 |  | 
| Brandon Wyman | ecbecbc | 2021-08-31 22:53:21 +0000 | [diff] [blame] | 11 | #include <regex> | 
|  | 12 |  | 
| Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 13 | using namespace phosphor::logging; | 
|  | 14 |  | 
| Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 15 | namespace phosphor::power::manager | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 16 | { | 
| Adriana Kobylak | c9b0573 | 2022-03-19 15:15:10 +0000 | [diff] [blame] | 17 | constexpr auto managerBusName = "xyz.openbmc_project.Power.PSUMonitor"; | 
|  | 18 | constexpr auto objectManagerObjPath = | 
|  | 19 | "/xyz/openbmc_project/power/power_supplies"; | 
|  | 20 | constexpr auto powerSystemsInputsObjPath = | 
|  | 21 | "/xyz/openbmc_project/power/power_supplies/chassis0/psus"; | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 22 |  | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 23 | constexpr auto IBMCFFPSInterface = | 
|  | 24 | "xyz.openbmc_project.Configuration.IBMCFFPSConnector"; | 
|  | 25 | constexpr auto i2cBusProp = "I2CBus"; | 
|  | 26 | constexpr auto i2cAddressProp = "I2CAddress"; | 
|  | 27 | constexpr auto psuNameProp = "Name"; | 
| B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 28 | constexpr auto presLineName = "NamedPresenceGpio"; | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 29 |  | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 30 | constexpr auto supportedConfIntf = | 
|  | 31 | "xyz.openbmc_project.Configuration.SupportedConfiguration"; | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 32 |  | 
| Brandon Wyman | 18a24d9 | 2022-04-19 22:48:34 +0000 | [diff] [blame^] | 33 | constexpr auto INPUT_HISTORY_SYNC_DELAY = 1100; | 
|  | 34 |  | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 35 | PSUManager::PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e) : | 
| Adriana Kobylak | c9b0573 | 2022-03-19 15:15:10 +0000 | [diff] [blame] | 36 | bus(bus), powerSystemInputs(bus, powerSystemsInputsObjPath), | 
| Brandon Wyman | c332442 | 2022-03-24 20:30:57 +0000 | [diff] [blame] | 37 | objectManager(bus, objectManagerObjPath), | 
|  | 38 | historyManager(bus, "/org/open_power/sensors") | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 39 | { | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 40 | // Subscribe to InterfacesAdded before doing a property read, otherwise | 
|  | 41 | // the interface could be created after the read attempt but before the | 
|  | 42 | // match is created. | 
|  | 43 | entityManagerIfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>( | 
|  | 44 | bus, | 
|  | 45 | sdbusplus::bus::match::rules::interfacesAdded() + | 
|  | 46 | sdbusplus::bus::match::rules::sender( | 
|  | 47 | "xyz.openbmc_project.EntityManager"), | 
|  | 48 | std::bind(&PSUManager::entityManagerIfaceAdded, this, | 
|  | 49 | std::placeholders::_1)); | 
|  | 50 | getPSUConfiguration(); | 
|  | 51 | getSystemProperties(); | 
|  | 52 |  | 
| Adriana Kobylak | c9b0573 | 2022-03-19 15:15:10 +0000 | [diff] [blame] | 53 | // Request the bus name before the analyze() function, which is the one that | 
|  | 54 | // determines the brownout condition and sets the status d-bus property. | 
|  | 55 | bus.request_name(managerBusName); | 
|  | 56 |  | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 57 | using namespace sdeventplus; | 
|  | 58 | auto interval = std::chrono::milliseconds(1000); | 
|  | 59 | timer = std::make_unique<utility::Timer<ClockId::Monotonic>>( | 
|  | 60 | e, std::bind(&PSUManager::analyze, this), interval); | 
|  | 61 |  | 
| Adriana Kobylak | a4d38fa | 2021-10-05 19:57:47 +0000 | [diff] [blame] | 62 | validationTimer = std::make_unique<utility::Timer<ClockId::Monotonic>>( | 
|  | 63 | e, std::bind(&PSUManager::validateConfig, this)); | 
|  | 64 |  | 
| Adriana Kobylak | c0a0758 | 2021-10-13 15:52:25 +0000 | [diff] [blame] | 65 | try | 
|  | 66 | { | 
|  | 67 | powerConfigGPIO = createGPIO("power-config-full-load"); | 
|  | 68 | } | 
|  | 69 | catch (const std::exception& e) | 
|  | 70 | { | 
|  | 71 | // Ignore error, GPIO may not be implemented in this system. | 
|  | 72 | powerConfigGPIO = nullptr; | 
|  | 73 | } | 
|  | 74 |  | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 75 | // Subscribe to power state changes | 
|  | 76 | powerService = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus); | 
|  | 77 | powerOnMatch = std::make_unique<sdbusplus::bus::match_t>( | 
|  | 78 | bus, | 
|  | 79 | sdbusplus::bus::match::rules::propertiesChanged(POWER_OBJ_PATH, | 
|  | 80 | POWER_IFACE), | 
|  | 81 | [this](auto& msg) { this->powerStateChanged(msg); }); | 
|  | 82 |  | 
|  | 83 | initialize(); | 
|  | 84 | } | 
|  | 85 |  | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 86 | void PSUManager::getPSUConfiguration() | 
|  | 87 | { | 
|  | 88 | using namespace phosphor::power::util; | 
|  | 89 | auto depth = 0; | 
|  | 90 | auto objects = getSubTree(bus, "/", IBMCFFPSInterface, depth); | 
|  | 91 |  | 
|  | 92 | psus.clear(); | 
|  | 93 |  | 
|  | 94 | // I should get a map of objects back. | 
|  | 95 | // Each object will have a path, a service, and an interface. | 
|  | 96 | // The interface should match the one passed into this function. | 
|  | 97 | for (const auto& [path, services] : objects) | 
|  | 98 | { | 
|  | 99 | auto service = services.begin()->first; | 
|  | 100 |  | 
|  | 101 | if (path.empty() || service.empty()) | 
|  | 102 | { | 
|  | 103 | continue; | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | // For each object in the array of objects, I want to get properties | 
|  | 107 | // from the service, path, and interface. | 
|  | 108 | auto properties = | 
|  | 109 | getAllProperties(bus, path, IBMCFFPSInterface, service); | 
|  | 110 |  | 
|  | 111 | getPSUProperties(properties); | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | if (psus.empty()) | 
|  | 115 | { | 
|  | 116 | // Interface or properties not found. Let the Interfaces Added callback | 
|  | 117 | // process the information once the interfaces are added to D-Bus. | 
|  | 118 | log<level::INFO>(fmt::format("No power supplies to monitor").c_str()); | 
|  | 119 | } | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | void PSUManager::getPSUProperties(util::DbusPropertyMap& properties) | 
|  | 123 | { | 
|  | 124 | // From passed in properties, I want to get: I2CBus, I2CAddress, | 
|  | 125 | // and Name. Create a power supply object, using Name to build the inventory | 
|  | 126 | // path. | 
|  | 127 | const auto basePSUInvPath = | 
|  | 128 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply"; | 
|  | 129 | uint64_t* i2cbus = nullptr; | 
|  | 130 | uint64_t* i2caddr = nullptr; | 
|  | 131 | std::string* psuname = nullptr; | 
| B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 132 | std::string* preslineptr = nullptr; | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 133 |  | 
|  | 134 | for (const auto& property : properties) | 
|  | 135 | { | 
|  | 136 | try | 
|  | 137 | { | 
|  | 138 | if (property.first == i2cBusProp) | 
|  | 139 | { | 
|  | 140 | i2cbus = std::get_if<uint64_t>(&properties[i2cBusProp]); | 
|  | 141 | } | 
|  | 142 | else if (property.first == i2cAddressProp) | 
|  | 143 | { | 
|  | 144 | i2caddr = std::get_if<uint64_t>(&properties[i2cAddressProp]); | 
|  | 145 | } | 
|  | 146 | else if (property.first == psuNameProp) | 
|  | 147 | { | 
|  | 148 | psuname = std::get_if<std::string>(&properties[psuNameProp]); | 
|  | 149 | } | 
| B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 150 | else if (property.first == presLineName) | 
|  | 151 | { | 
|  | 152 | preslineptr = | 
|  | 153 | std::get_if<std::string>(&properties[presLineName]); | 
|  | 154 | } | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 155 | } | 
| Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 156 | catch (const std::exception& e) | 
| Adriana Kobylak | 0c9a33d | 2021-09-13 18:05:09 +0000 | [diff] [blame] | 157 | {} | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 158 | } | 
|  | 159 |  | 
|  | 160 | if ((i2cbus) && (i2caddr) && (psuname) && (!psuname->empty())) | 
|  | 161 | { | 
|  | 162 | std::string invpath = basePSUInvPath; | 
|  | 163 | invpath.push_back(psuname->back()); | 
| B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 164 | std::string presline = ""; | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 165 |  | 
|  | 166 | log<level::DEBUG>(fmt::format("Inventory Path: {}", invpath).c_str()); | 
|  | 167 |  | 
| B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 168 | if (nullptr != preslineptr) | 
|  | 169 | { | 
|  | 170 | presline = *preslineptr; | 
|  | 171 | } | 
|  | 172 |  | 
| Brandon Wyman | ecbecbc | 2021-08-31 22:53:21 +0000 | [diff] [blame] | 173 | auto invMatch = | 
|  | 174 | std::find_if(psus.begin(), psus.end(), [&invpath](auto& psu) { | 
|  | 175 | return psu->getInventoryPath() == invpath; | 
|  | 176 | }); | 
|  | 177 | if (invMatch != psus.end()) | 
|  | 178 | { | 
|  | 179 | // This power supply has the same inventory path as the one with | 
|  | 180 | // information just added to D-Bus. | 
|  | 181 | // Changes to GPIO line name unlikely, so skip checking. | 
|  | 182 | // Changes to the I2C bus and address unlikely, as that would | 
|  | 183 | // require corresponding device tree updates. | 
|  | 184 | // Return out to avoid duplicate object creation. | 
|  | 185 | return; | 
|  | 186 | } | 
|  | 187 |  | 
| Brandon Wyman | c332442 | 2022-03-24 20:30:57 +0000 | [diff] [blame] | 188 | constexpr auto driver = "ibm-cffps"; | 
| B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 189 | log<level::DEBUG>( | 
| Brandon Wyman | c332442 | 2022-03-24 20:30:57 +0000 | [diff] [blame] | 190 | fmt::format( | 
|  | 191 | "make PowerSupply bus: {} addr: {} driver: {} presline: {}", | 
|  | 192 | *i2cbus, *i2caddr, driver, presline) | 
| B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 193 | .c_str()); | 
|  | 194 | auto psu = std::make_unique<PowerSupply>(bus, invpath, *i2cbus, | 
| Brandon Wyman | c332442 | 2022-03-24 20:30:57 +0000 | [diff] [blame] | 195 | *i2caddr, driver, presline); | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 196 | psus.emplace_back(std::move(psu)); | 
| Adriana Kobylak | 9ba3823 | 2021-11-16 20:27:45 +0000 | [diff] [blame] | 197 |  | 
|  | 198 | // Subscribe to power supply presence changes | 
|  | 199 | auto presenceMatch = std::make_unique<sdbusplus::bus::match_t>( | 
|  | 200 | bus, | 
|  | 201 | sdbusplus::bus::match::rules::propertiesChanged(invpath, | 
|  | 202 | INVENTORY_IFACE), | 
|  | 203 | [this](auto& msg) { this->presenceChanged(msg); }); | 
|  | 204 | presenceMatches.emplace_back(std::move(presenceMatch)); | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 205 | } | 
|  | 206 |  | 
|  | 207 | if (psus.empty()) | 
|  | 208 | { | 
|  | 209 | log<level::INFO>(fmt::format("No power supplies to monitor").c_str()); | 
|  | 210 | } | 
|  | 211 | } | 
|  | 212 |  | 
| Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 213 | void PSUManager::populateSysProperties(const util::DbusPropertyMap& properties) | 
|  | 214 | { | 
|  | 215 | try | 
|  | 216 | { | 
| Adriana Kobylak | 9ea66a6 | 2021-03-24 17:54:14 +0000 | [diff] [blame] | 217 | auto propIt = properties.find("SupportedType"); | 
|  | 218 | if (propIt == properties.end()) | 
|  | 219 | { | 
|  | 220 | return; | 
|  | 221 | } | 
|  | 222 | const std::string* type = std::get_if<std::string>(&(propIt->second)); | 
|  | 223 | if ((type == nullptr) || (*type != "PowerSupply")) | 
|  | 224 | { | 
|  | 225 | return; | 
|  | 226 | } | 
|  | 227 |  | 
| Adriana Kobylak | 9ea66a6 | 2021-03-24 17:54:14 +0000 | [diff] [blame] | 228 | propIt = properties.find("SupportedModel"); | 
|  | 229 | if (propIt == properties.end()) | 
|  | 230 | { | 
|  | 231 | return; | 
|  | 232 | } | 
| Adriana Kobylak | d3a70d9 | 2021-06-04 16:24:45 +0000 | [diff] [blame] | 233 | const std::string* model = std::get_if<std::string>(&(propIt->second)); | 
|  | 234 | if (model == nullptr) | 
| Adriana Kobylak | 9ea66a6 | 2021-03-24 17:54:14 +0000 | [diff] [blame] | 235 | { | 
|  | 236 | return; | 
|  | 237 | } | 
| Adriana Kobylak | 9ea66a6 | 2021-03-24 17:54:14 +0000 | [diff] [blame] | 238 |  | 
| Adriana Kobylak | d3a70d9 | 2021-06-04 16:24:45 +0000 | [diff] [blame] | 239 | sys_properties sys; | 
| Adriana Kobylak | 9ea66a6 | 2021-03-24 17:54:14 +0000 | [diff] [blame] | 240 | propIt = properties.find("RedundantCount"); | 
| Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 241 | if (propIt != properties.end()) | 
|  | 242 | { | 
|  | 243 | const uint64_t* count = std::get_if<uint64_t>(&(propIt->second)); | 
|  | 244 | if (count != nullptr) | 
|  | 245 | { | 
| Adriana Kobylak | d3a70d9 | 2021-06-04 16:24:45 +0000 | [diff] [blame] | 246 | sys.powerSupplyCount = *count; | 
| Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 247 | } | 
|  | 248 | } | 
| Adriana Kobylak | 9ea66a6 | 2021-03-24 17:54:14 +0000 | [diff] [blame] | 249 | propIt = properties.find("InputVoltage"); | 
|  | 250 | if (propIt != properties.end()) | 
|  | 251 | { | 
| Adriana Kobylak | d3a70d9 | 2021-06-04 16:24:45 +0000 | [diff] [blame] | 252 | const std::vector<uint64_t>* voltage = | 
|  | 253 | std::get_if<std::vector<uint64_t>>(&(propIt->second)); | 
| Adriana Kobylak | 9ea66a6 | 2021-03-24 17:54:14 +0000 | [diff] [blame] | 254 | if (voltage != nullptr) | 
|  | 255 | { | 
|  | 256 | sys.inputVoltage = *voltage; | 
|  | 257 | } | 
|  | 258 | } | 
|  | 259 |  | 
| Adriana Kobylak | 886574c | 2021-11-01 18:22:28 +0000 | [diff] [blame] | 260 | // The PowerConfigFullLoad is an optional property, default it to false | 
|  | 261 | // since that's the default value of the power-config-full-load GPIO. | 
|  | 262 | sys.powerConfigFullLoad = false; | 
|  | 263 | propIt = properties.find("PowerConfigFullLoad"); | 
|  | 264 | if (propIt != properties.end()) | 
|  | 265 | { | 
|  | 266 | const bool* fullLoad = std::get_if<bool>(&(propIt->second)); | 
|  | 267 | if (fullLoad != nullptr) | 
|  | 268 | { | 
|  | 269 | sys.powerConfigFullLoad = *fullLoad; | 
|  | 270 | } | 
|  | 271 | } | 
|  | 272 |  | 
| Adriana Kobylak | d3a70d9 | 2021-06-04 16:24:45 +0000 | [diff] [blame] | 273 | supportedConfigs.emplace(*model, sys); | 
| Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 274 | } | 
| Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 275 | catch (const std::exception& e) | 
| Adriana Kobylak | 0c9a33d | 2021-09-13 18:05:09 +0000 | [diff] [blame] | 276 | {} | 
| Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 277 | } | 
|  | 278 |  | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 279 | void PSUManager::getSystemProperties() | 
|  | 280 | { | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 281 |  | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 282 | try | 
|  | 283 | { | 
|  | 284 | util::DbusSubtree subtree = | 
|  | 285 | util::getSubTree(bus, INVENTORY_OBJ_PATH, supportedConfIntf, 0); | 
| Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 286 | if (subtree.empty()) | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 287 | { | 
|  | 288 | throw std::runtime_error("Supported Configuration Not Found"); | 
|  | 289 | } | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 290 |  | 
| Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 291 | for (const auto& [objPath, services] : subtree) | 
|  | 292 | { | 
|  | 293 | std::string service = services.begin()->first; | 
|  | 294 | if (objPath.empty() || service.empty()) | 
|  | 295 | { | 
|  | 296 | continue; | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 297 | } | 
| Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 298 | auto properties = util::getAllProperties( | 
|  | 299 | bus, objPath, supportedConfIntf, service); | 
|  | 300 | populateSysProperties(properties); | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 301 | } | 
|  | 302 | } | 
| Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 303 | catch (const std::exception& e) | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 304 | { | 
|  | 305 | // Interface or property not found. Let the Interfaces Added callback | 
|  | 306 | // process the information once the interfaces are added to D-Bus. | 
|  | 307 | } | 
|  | 308 | } | 
|  | 309 |  | 
| Brandon Wyman | 3e42913 | 2021-03-18 18:03:14 -0500 | [diff] [blame] | 310 | void PSUManager::entityManagerIfaceAdded(sdbusplus::message::message& msg) | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 311 | { | 
|  | 312 | try | 
|  | 313 | { | 
|  | 314 | sdbusplus::message::object_path objPath; | 
| Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 315 | std::map<std::string, std::map<std::string, util::DbusVariant>> | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 316 | interfaces; | 
|  | 317 | msg.read(objPath, interfaces); | 
|  | 318 |  | 
|  | 319 | auto itIntf = interfaces.find(supportedConfIntf); | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 320 | if (itIntf != interfaces.cend()) | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 321 | { | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 322 | populateSysProperties(itIntf->second); | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 323 | } | 
|  | 324 |  | 
| Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 325 | itIntf = interfaces.find(IBMCFFPSInterface); | 
|  | 326 | if (itIntf != interfaces.cend()) | 
|  | 327 | { | 
|  | 328 | log<level::INFO>( | 
|  | 329 | fmt::format("InterfacesAdded for: {}", IBMCFFPSInterface) | 
|  | 330 | .c_str()); | 
|  | 331 | getPSUProperties(itIntf->second); | 
|  | 332 | } | 
| Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 333 |  | 
|  | 334 | // Call to validate the psu configuration if the power is on and both | 
|  | 335 | // the IBMCFFPSConnector and SupportedConfiguration interfaces have been | 
|  | 336 | // processed | 
|  | 337 | if (powerOn && !psus.empty() && !supportedConfigs.empty()) | 
|  | 338 | { | 
| Adriana Kobylak | a4d38fa | 2021-10-05 19:57:47 +0000 | [diff] [blame] | 339 | validationTimer->restartOnce(validationTimeout); | 
| Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 340 | } | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 341 | } | 
| Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 342 | catch (const std::exception& e) | 
| Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 343 | { | 
|  | 344 | // Ignore, the property may be of a different type than expected. | 
|  | 345 | } | 
|  | 346 | } | 
|  | 347 |  | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 348 | void PSUManager::powerStateChanged(sdbusplus::message::message& msg) | 
|  | 349 | { | 
|  | 350 | int32_t state = 0; | 
|  | 351 | std::string msgSensor; | 
| Patrick Williams | abe4941 | 2020-05-13 17:59:47 -0500 | [diff] [blame] | 352 | std::map<std::string, std::variant<int32_t>> msgData; | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 353 | msg.read(msgSensor, msgData); | 
|  | 354 |  | 
|  | 355 | // Check if it was the Present property that changed. | 
|  | 356 | auto valPropMap = msgData.find("state"); | 
|  | 357 | if (valPropMap != msgData.end()) | 
|  | 358 | { | 
|  | 359 | state = std::get<int32_t>(valPropMap->second); | 
|  | 360 |  | 
|  | 361 | // Power is on when state=1. Clear faults. | 
|  | 362 | if (state) | 
|  | 363 | { | 
|  | 364 | powerOn = true; | 
| Adriana Kobylak | a4d38fa | 2021-10-05 19:57:47 +0000 | [diff] [blame] | 365 | validationTimer->restartOnce(validationTimeout); | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 366 | clearFaults(); | 
| Adriana Kobylak | c0a0758 | 2021-10-13 15:52:25 +0000 | [diff] [blame] | 367 | setPowerConfigGPIO(); | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 368 | } | 
|  | 369 | else | 
|  | 370 | { | 
|  | 371 | powerOn = false; | 
| Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 372 | runValidateConfig = true; | 
| Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 373 | } | 
|  | 374 | } | 
|  | 375 | } | 
|  | 376 |  | 
| Adriana Kobylak | 9ba3823 | 2021-11-16 20:27:45 +0000 | [diff] [blame] | 377 | void PSUManager::presenceChanged(sdbusplus::message::message& msg) | 
|  | 378 | { | 
|  | 379 | std::string msgSensor; | 
|  | 380 | std::map<std::string, std::variant<uint32_t, bool>> msgData; | 
|  | 381 | msg.read(msgSensor, msgData); | 
|  | 382 |  | 
|  | 383 | // Check if it was the Present property that changed. | 
|  | 384 | auto valPropMap = msgData.find(PRESENT_PROP); | 
|  | 385 | if (valPropMap != msgData.end()) | 
|  | 386 | { | 
|  | 387 | if (std::get<bool>(valPropMap->second)) | 
|  | 388 | { | 
|  | 389 | // A PSU became present, force the PSU validation to run. | 
|  | 390 | runValidateConfig = true; | 
|  | 391 | validationTimer->restartOnce(validationTimeout); | 
|  | 392 | } | 
|  | 393 | } | 
|  | 394 | } | 
|  | 395 |  | 
| Brandon Wyman | 10fc6e8 | 2022-02-08 20:51:22 +0000 | [diff] [blame] | 396 | void PSUManager::setPowerSupplyError(const std::string& psuErrorString) | 
|  | 397 | { | 
|  | 398 | using namespace sdbusplus::xyz::openbmc_project; | 
|  | 399 | constexpr auto service = "org.openbmc.control.Power"; | 
|  | 400 | constexpr auto objPath = "/org/openbmc/control/power0"; | 
|  | 401 | constexpr auto interface = "org.openbmc.control.Power"; | 
|  | 402 | constexpr auto method = "setPowerSupplyError"; | 
|  | 403 |  | 
|  | 404 | try | 
|  | 405 | { | 
|  | 406 | // Call D-Bus method to inform pseq of PSU error | 
|  | 407 | auto methodMsg = | 
|  | 408 | bus.new_method_call(service, objPath, interface, method); | 
|  | 409 | methodMsg.append(psuErrorString); | 
|  | 410 | auto callReply = bus.call(methodMsg); | 
|  | 411 | } | 
|  | 412 | catch (const std::exception& e) | 
|  | 413 | { | 
|  | 414 | log<level::INFO>( | 
|  | 415 | fmt::format("Failed calling setPowerSupplyError due to error {}", | 
|  | 416 | e.what()) | 
|  | 417 | .c_str()); | 
|  | 418 | } | 
|  | 419 | } | 
|  | 420 |  | 
| Brandon Wyman | 8b66288 | 2021-10-08 17:31:51 +0000 | [diff] [blame] | 421 | void PSUManager::createError(const std::string& faultName, | 
|  | 422 | std::map<std::string, std::string>& additionalData) | 
| Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 423 | { | 
|  | 424 | using namespace sdbusplus::xyz::openbmc_project; | 
|  | 425 | constexpr auto loggingObjectPath = "/xyz/openbmc_project/logging"; | 
|  | 426 | constexpr auto loggingCreateInterface = | 
|  | 427 | "xyz.openbmc_project.Logging.Create"; | 
|  | 428 |  | 
|  | 429 | try | 
|  | 430 | { | 
| Brandon Wyman | 8b66288 | 2021-10-08 17:31:51 +0000 | [diff] [blame] | 431 | additionalData["_PID"] = std::to_string(getpid()); | 
|  | 432 |  | 
| Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 433 | auto service = | 
|  | 434 | util::getService(loggingObjectPath, loggingCreateInterface, bus); | 
|  | 435 |  | 
|  | 436 | if (service.empty()) | 
|  | 437 | { | 
|  | 438 | log<level::ERR>("Unable to get logging manager service"); | 
|  | 439 | return; | 
|  | 440 | } | 
|  | 441 |  | 
|  | 442 | auto method = bus.new_method_call(service.c_str(), loggingObjectPath, | 
|  | 443 | loggingCreateInterface, "Create"); | 
|  | 444 |  | 
|  | 445 | auto level = Logging::server::Entry::Level::Error; | 
|  | 446 | method.append(faultName, level, additionalData); | 
|  | 447 |  | 
|  | 448 | auto reply = bus.call(method); | 
| Brandon Wyman | 10fc6e8 | 2022-02-08 20:51:22 +0000 | [diff] [blame] | 449 | setPowerSupplyError(faultName); | 
| Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 450 | } | 
| Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 451 | catch (const std::exception& e) | 
| Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 452 | { | 
|  | 453 | log<level::ERR>( | 
|  | 454 | fmt::format( | 
|  | 455 | "Failed creating event log for fault {} due to error {}", | 
|  | 456 | faultName, e.what()) | 
|  | 457 | .c_str()); | 
|  | 458 | } | 
|  | 459 | } | 
|  | 460 |  | 
| Brandon Wyman | 18a24d9 | 2022-04-19 22:48:34 +0000 | [diff] [blame^] | 461 | void PSUManager::syncHistory() | 
|  | 462 | { | 
|  | 463 | log<level::INFO>("Synchronize INPUT_HISTORY"); | 
|  | 464 |  | 
|  | 465 | if (!syncHistoryGPIO) | 
|  | 466 | { | 
|  | 467 | syncHistoryGPIO = createGPIO(INPUT_HISTORY_SYNC_GPIO); | 
|  | 468 | } | 
|  | 469 | if (syncHistoryGPIO) | 
|  | 470 | { | 
|  | 471 | const std::chrono::milliseconds delay{INPUT_HISTORY_SYNC_DELAY}; | 
|  | 472 | syncHistoryGPIO->toggleLowHigh(delay); | 
|  | 473 | for (auto& psu : psus) | 
|  | 474 | { | 
|  | 475 | psu->clearSyncHistoryRequired(); | 
|  | 476 | } | 
|  | 477 | } | 
|  | 478 |  | 
|  | 479 | log<level::INFO>("Synchronize INPUT_HISTORY completed"); | 
|  | 480 | } | 
|  | 481 |  | 
| Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 482 | void PSUManager::analyze() | 
|  | 483 | { | 
| Brandon Wyman | 18a24d9 | 2022-04-19 22:48:34 +0000 | [diff] [blame^] | 484 | auto syncHistoryRequired = | 
|  | 485 | std::any_of(psus.begin(), psus.end(), [](const auto& psu) { | 
|  | 486 | return psu->isSyncHistoryRequired(); | 
|  | 487 | }); | 
|  | 488 | if (syncHistoryRequired) | 
|  | 489 | { | 
|  | 490 | syncHistory(); | 
|  | 491 | } | 
|  | 492 |  | 
| Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 493 | for (auto& psu : psus) | 
|  | 494 | { | 
|  | 495 | psu->analyze(); | 
|  | 496 | } | 
|  | 497 |  | 
| Adriana Kobylak | e5b1e08 | 2022-03-02 15:37:32 +0000 | [diff] [blame] | 498 | std::map<std::string, std::string> additionalData; | 
|  | 499 |  | 
|  | 500 | auto notPresentCount = decltype(psus.size())( | 
|  | 501 | std::count_if(psus.begin(), psus.end(), | 
|  | 502 | [](const auto& psu) { return !psu->isPresent(); })); | 
|  | 503 |  | 
|  | 504 | auto hasVINUVFaultCount = decltype(psus.size())( | 
|  | 505 | std::count_if(psus.begin(), psus.end(), | 
|  | 506 | [](const auto& psu) { return psu->hasVINUVFault(); })); | 
|  | 507 |  | 
|  | 508 | // The PSU D-Bus objects may not be available yet, so ignore if all | 
|  | 509 | // PSUs are not present or the number of PSUs is still 0. | 
|  | 510 | if ((psus.size() == (notPresentCount + hasVINUVFaultCount)) && | 
|  | 511 | (psus.size() != notPresentCount) && (psus.size() != 0)) | 
|  | 512 | { | 
|  | 513 | // Brownout: All PSUs report an AC failure: At least one PSU reports | 
|  | 514 | // AC loss VIN fault and the rest either report AC loss VIN fault as | 
|  | 515 | // well or are not present. | 
|  | 516 | additionalData["NOT_PRESENT_COUNT"] = std::to_string(notPresentCount); | 
|  | 517 | additionalData["VIN_FAULT_COUNT"] = std::to_string(hasVINUVFaultCount); | 
|  | 518 | setBrownout(additionalData); | 
|  | 519 | } | 
|  | 520 | else | 
|  | 521 | { | 
|  | 522 | // Brownout condition is not present or has been cleared | 
|  | 523 | clearBrownout(); | 
|  | 524 | } | 
|  | 525 |  | 
| Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 526 | if (powerOn) | 
| Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 527 | { | 
| Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 528 | for (auto& psu : psus) | 
| Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 529 | { | 
| Brandon Wyman | ec0b8dc | 2021-10-08 21:49:43 +0000 | [diff] [blame] | 530 | additionalData.clear(); | 
| Brandon Wyman | 39ea02b | 2021-11-23 23:22:23 +0000 | [diff] [blame] | 531 |  | 
| Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 532 | if (!psu->isFaultLogged() && !psu->isPresent()) | 
|  | 533 | { | 
| Brandon Wyman | da369c7 | 2021-10-08 18:43:30 +0000 | [diff] [blame] | 534 | std::map<std::string, std::string> requiredPSUsData; | 
|  | 535 | auto requiredPSUsPresent = hasRequiredPSUs(requiredPSUsData); | 
| Adriana Kobylak | f2ba146 | 2021-06-24 15:16:17 +0000 | [diff] [blame] | 536 | if (!requiredPSUsPresent) | 
|  | 537 | { | 
| Brandon Wyman | da369c7 | 2021-10-08 18:43:30 +0000 | [diff] [blame] | 538 | additionalData.merge(requiredPSUsData); | 
| Adriana Kobylak | f2ba146 | 2021-06-24 15:16:17 +0000 | [diff] [blame] | 539 | // Create error for power supply missing. | 
|  | 540 | additionalData["CALLOUT_INVENTORY_PATH"] = | 
|  | 541 | psu->getInventoryPath(); | 
|  | 542 | additionalData["CALLOUT_PRIORITY"] = "H"; | 
|  | 543 | createError( | 
|  | 544 | "xyz.openbmc_project.Power.PowerSupply.Error.Missing", | 
|  | 545 | additionalData); | 
|  | 546 | } | 
| Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 547 | psu->setFaultLogged(); | 
|  | 548 | } | 
|  | 549 | else if (!psu->isFaultLogged() && psu->isFaulted()) | 
|  | 550 | { | 
| Brandon Wyman | 786b6f4 | 2021-10-12 20:21:41 +0000 | [diff] [blame] | 551 | // Add STATUS_WORD and STATUS_MFR last response, in padded | 
|  | 552 | // hexadecimal format. | 
| Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 553 | additionalData["STATUS_WORD"] = | 
| Brandon Wyman | 786b6f4 | 2021-10-12 20:21:41 +0000 | [diff] [blame] | 554 | fmt::format("{:#04x}", psu->getStatusWord()); | 
| Jay Meyer | 10d9405 | 2020-11-30 14:41:21 -0600 | [diff] [blame] | 555 | additionalData["STATUS_MFR"] = | 
| Brandon Wyman | 786b6f4 | 2021-10-12 20:21:41 +0000 | [diff] [blame] | 556 | fmt::format("{:#02x}", psu->getMFRFault()); | 
| Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 557 | // If there are faults being reported, they possibly could be | 
|  | 558 | // related to a bug in the firmware version running on the power | 
|  | 559 | // supply. Capture that data into the error as well. | 
|  | 560 | additionalData["FW_VERSION"] = psu->getFWVersion(); | 
|  | 561 |  | 
| Brandon Wyman | b85b9dd | 2021-10-19 21:25:17 +0000 | [diff] [blame] | 562 | if (psu->hasCommFault()) | 
|  | 563 | { | 
| Brandon Wyman | 85c7bf4 | 2021-10-19 22:28:48 +0000 | [diff] [blame] | 564 | additionalData["STATUS_CML"] = | 
|  | 565 | fmt::format("{:#02x}", psu->getStatusCML()); | 
| Brandon Wyman | b85b9dd | 2021-10-19 21:25:17 +0000 | [diff] [blame] | 566 | /* Attempts to communicate with the power supply have | 
|  | 567 | * reached there limit. Create an error. */ | 
|  | 568 | additionalData["CALLOUT_DEVICE_PATH"] = | 
|  | 569 | psu->getDevicePath(); | 
|  | 570 |  | 
|  | 571 | createError( | 
|  | 572 | "xyz.openbmc_project.Power.PowerSupply.Error.CommFault", | 
|  | 573 | additionalData); | 
|  | 574 |  | 
|  | 575 | psu->setFaultLogged(); | 
|  | 576 | } | 
|  | 577 | else if ((psu->hasInputFault() || psu->hasVINUVFault())) | 
| Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 578 | { | 
| Brandon Wyman | f07bc79 | 2021-10-12 19:00:35 +0000 | [diff] [blame] | 579 | // Include STATUS_INPUT for input faults. | 
|  | 580 | additionalData["STATUS_INPUT"] = | 
|  | 581 | fmt::format("{:#02x}", psu->getStatusInput()); | 
|  | 582 |  | 
| Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 583 | /* The power supply location might be needed if the input | 
|  | 584 | * fault is due to a problem with the power supply itself. | 
|  | 585 | * Include the inventory path with a call out priority of | 
|  | 586 | * low. | 
|  | 587 | */ | 
|  | 588 | additionalData["CALLOUT_INVENTORY_PATH"] = | 
|  | 589 | psu->getInventoryPath(); | 
|  | 590 | additionalData["CALLOUT_PRIORITY"] = "L"; | 
|  | 591 | createError("xyz.openbmc_project.Power.PowerSupply.Error." | 
|  | 592 | "InputFault", | 
|  | 593 | additionalData); | 
|  | 594 | psu->setFaultLogged(); | 
|  | 595 | } | 
| Brandon Wyman | 39ea02b | 2021-11-23 23:22:23 +0000 | [diff] [blame] | 596 | else if (psu->hasPSKillFault()) | 
|  | 597 | { | 
|  | 598 | createError( | 
|  | 599 | "xyz.openbmc_project.Power.PowerSupply.Error.PSKillFault", | 
|  | 600 | additionalData); | 
|  | 601 | psu->setFaultLogged(); | 
|  | 602 | } | 
| Brandon Wyman | 6710ba2 | 2021-10-27 17:39:31 +0000 | [diff] [blame] | 603 | else if (psu->hasVoutOVFault()) | 
|  | 604 | { | 
|  | 605 | // Include STATUS_VOUT for Vout faults. | 
|  | 606 | additionalData["STATUS_VOUT"] = | 
|  | 607 | fmt::format("{:#02x}", psu->getStatusVout()); | 
|  | 608 |  | 
|  | 609 | additionalData["CALLOUT_INVENTORY_PATH"] = | 
|  | 610 | psu->getInventoryPath(); | 
|  | 611 |  | 
|  | 612 | createError( | 
|  | 613 | "xyz.openbmc_project.Power.PowerSupply.Error.Fault", | 
|  | 614 | additionalData); | 
|  | 615 |  | 
|  | 616 | psu->setFaultLogged(); | 
|  | 617 | } | 
| Brandon Wyman | b10b3be | 2021-11-09 22:12:15 +0000 | [diff] [blame] | 618 | else if (psu->hasIoutOCFault()) | 
|  | 619 | { | 
|  | 620 | // Include STATUS_IOUT for Iout faults. | 
|  | 621 | additionalData["STATUS_IOUT"] = | 
|  | 622 | fmt::format("{:#02x}", psu->getStatusIout()); | 
|  | 623 |  | 
|  | 624 | createError( | 
|  | 625 | "xyz.openbmc_project.Power.PowerSupply.Error.IoutOCFault", | 
|  | 626 | additionalData); | 
|  | 627 |  | 
|  | 628 | psu->setFaultLogged(); | 
|  | 629 | } | 
| Brandon Wyman | 39ea02b | 2021-11-23 23:22:23 +0000 | [diff] [blame] | 630 | else if (psu->hasVoutUVFault() || psu->hasPS12VcsFault() || | 
|  | 631 | psu->hasPSCS12VFault()) | 
| Brandon Wyman | 2cf4694 | 2021-10-28 19:09:16 +0000 | [diff] [blame] | 632 | { | 
|  | 633 | // Include STATUS_VOUT for Vout faults. | 
|  | 634 | additionalData["STATUS_VOUT"] = | 
|  | 635 | fmt::format("{:#02x}", psu->getStatusVout()); | 
|  | 636 |  | 
|  | 637 | additionalData["CALLOUT_INVENTORY_PATH"] = | 
|  | 638 | psu->getInventoryPath(); | 
|  | 639 |  | 
|  | 640 | createError( | 
|  | 641 | "xyz.openbmc_project.Power.PowerSupply.Error.Fault", | 
|  | 642 | additionalData); | 
|  | 643 |  | 
|  | 644 | psu->setFaultLogged(); | 
|  | 645 | } | 
| Brandon Wyman | 7ee4d7e | 2021-11-19 20:48:23 +0000 | [diff] [blame] | 646 | // A fan fault should have priority over a temperature fault, | 
|  | 647 | // since a failed fan may lead to a temperature problem. | 
|  | 648 | else if (psu->hasFanFault()) | 
|  | 649 | { | 
|  | 650 | // Include STATUS_TEMPERATURE and STATUS_FANS_1_2 | 
|  | 651 | additionalData["STATUS_TEMPERATURE"] = | 
|  | 652 | fmt::format("{:#02x}", psu->getStatusTemperature()); | 
|  | 653 | additionalData["STATUS_FANS_1_2"] = | 
|  | 654 | fmt::format("{:#02x}", psu->getStatusFans12()); | 
|  | 655 |  | 
|  | 656 | additionalData["CALLOUT_INVENTORY_PATH"] = | 
|  | 657 | psu->getInventoryPath(); | 
|  | 658 |  | 
|  | 659 | createError( | 
|  | 660 | "xyz.openbmc_project.Power.PowerSupply.Error.FanFault", | 
|  | 661 | additionalData); | 
|  | 662 |  | 
|  | 663 | psu->setFaultLogged(); | 
|  | 664 | } | 
| Brandon Wyman | 96893a4 | 2021-11-05 19:56:57 +0000 | [diff] [blame] | 665 | else if (psu->hasTempFault()) | 
|  | 666 | { | 
|  | 667 | // Include STATUS_TEMPERATURE for temperature faults. | 
|  | 668 | additionalData["STATUS_TEMPERATURE"] = | 
|  | 669 | fmt::format("{:#02x}", psu->getStatusTemperature()); | 
|  | 670 |  | 
|  | 671 | additionalData["CALLOUT_INVENTORY_PATH"] = | 
|  | 672 | psu->getInventoryPath(); | 
|  | 673 |  | 
|  | 674 | createError( | 
|  | 675 | "xyz.openbmc_project.Power.PowerSupply.Error.Fault", | 
|  | 676 | additionalData); | 
|  | 677 |  | 
|  | 678 | psu->setFaultLogged(); | 
|  | 679 | } | 
| Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 680 | else if (psu->hasMFRFault()) | 
|  | 681 | { | 
|  | 682 | /* This can represent a variety of faults that result in | 
|  | 683 | * calling out the power supply for replacement: Output | 
|  | 684 | * OverCurrent, Output Under Voltage, and potentially other | 
|  | 685 | * faults. | 
|  | 686 | * | 
|  | 687 | * Also plan on putting specific fault in AdditionalData, | 
|  | 688 | * along with register names and register values | 
|  | 689 | * (STATUS_WORD, STATUS_MFR, etc.).*/ | 
|  | 690 |  | 
|  | 691 | additionalData["CALLOUT_INVENTORY_PATH"] = | 
|  | 692 | psu->getInventoryPath(); | 
|  | 693 |  | 
|  | 694 | createError( | 
|  | 695 | "xyz.openbmc_project.Power.PowerSupply.Error.Fault", | 
| Brandon Wyman | 52e54e8 | 2020-10-08 14:44:58 -0500 | [diff] [blame] | 696 | additionalData); | 
| Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 697 |  | 
| Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 698 | psu->setFaultLogged(); | 
|  | 699 | } | 
| Brandon Wyman | 2916ea5 | 2021-11-06 03:31:18 +0000 | [diff] [blame] | 700 | else if (psu->hasPgoodFault()) | 
|  | 701 | { | 
|  | 702 | /* POWER_GOOD# is not low, or OFF is on */ | 
|  | 703 | additionalData["CALLOUT_INVENTORY_PATH"] = | 
|  | 704 | psu->getInventoryPath(); | 
|  | 705 |  | 
|  | 706 | createError( | 
|  | 707 | "xyz.openbmc_project.Power.PowerSupply.Error.Fault", | 
|  | 708 | additionalData); | 
|  | 709 |  | 
|  | 710 | psu->setFaultLogged(); | 
|  | 711 | } | 
| Brandon Wyman | 4176d6b | 2020-10-07 17:41:06 -0500 | [diff] [blame] | 712 | } | 
| Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 713 | } | 
|  | 714 | } | 
|  | 715 | } | 
|  | 716 |  | 
| Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 717 | void PSUManager::validateConfig() | 
|  | 718 | { | 
| Adriana Kobylak | b23e443 | 2022-04-01 14:22:47 +0000 | [diff] [blame] | 719 | if (!runValidateConfig || supportedConfigs.empty() || psus.empty()) | 
| Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 720 | { | 
|  | 721 | return; | 
|  | 722 | } | 
|  | 723 |  | 
| Adriana Kobylak | 4d9aaf9 | 2021-06-30 15:27:42 +0000 | [diff] [blame] | 724 | std::map<std::string, std::string> additionalData; | 
|  | 725 | auto supported = hasRequiredPSUs(additionalData); | 
|  | 726 | if (supported) | 
|  | 727 | { | 
|  | 728 | runValidateConfig = false; | 
|  | 729 | return; | 
|  | 730 | } | 
|  | 731 |  | 
|  | 732 | // Validation failed, create an error log. | 
|  | 733 | // Return without setting the runValidateConfig flag to false because | 
|  | 734 | // it may be that an additional supported configuration interface is | 
|  | 735 | // added and we need to validate it to see if it matches this system. | 
|  | 736 | createError("xyz.openbmc_project.Power.PowerSupply.Error.NotSupported", | 
|  | 737 | additionalData); | 
|  | 738 | } | 
|  | 739 |  | 
|  | 740 | bool PSUManager::hasRequiredPSUs( | 
|  | 741 | std::map<std::string, std::string>& additionalData) | 
|  | 742 | { | 
| Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 743 | std::string model{}; | 
| Adriana Kobylak | 523704d | 2021-09-21 15:55:41 +0000 | [diff] [blame] | 744 | if (!validateModelName(model, additionalData)) | 
| Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 745 | { | 
| Adriana Kobylak | 523704d | 2021-09-21 15:55:41 +0000 | [diff] [blame] | 746 | return false; | 
| Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 747 | } | 
| Adriana Kobylak | 70e7f93 | 2021-06-10 18:53:56 +0000 | [diff] [blame] | 748 |  | 
| Adriana Kobylak | 4d9aaf9 | 2021-06-30 15:27:42 +0000 | [diff] [blame] | 749 | auto presentCount = | 
|  | 750 | std::count_if(psus.begin(), psus.end(), | 
|  | 751 | [](const auto& psu) { return psu->isPresent(); }); | 
|  | 752 |  | 
| Adriana Kobylak | 70e7f93 | 2021-06-10 18:53:56 +0000 | [diff] [blame] | 753 | // Validate the supported configurations. A system may support more than one | 
| Adriana Kobylak | 4175ffb | 2021-08-02 14:51:05 +0000 | [diff] [blame] | 754 | // power supply model configuration. Since all configurations need to be | 
|  | 755 | // checked, the additional data would contain only the information of the | 
|  | 756 | // last configuration that did not match. | 
|  | 757 | std::map<std::string, std::string> tmpAdditionalData; | 
| Adriana Kobylak | 70e7f93 | 2021-06-10 18:53:56 +0000 | [diff] [blame] | 758 | for (const auto& config : supportedConfigs) | 
|  | 759 | { | 
| Adriana Kobylak | 4d9aaf9 | 2021-06-30 15:27:42 +0000 | [diff] [blame] | 760 | if (config.first != model) | 
| Adriana Kobylak | 70e7f93 | 2021-06-10 18:53:56 +0000 | [diff] [blame] | 761 | { | 
|  | 762 | continue; | 
|  | 763 | } | 
|  | 764 | if (presentCount != config.second.powerSupplyCount) | 
|  | 765 | { | 
| Adriana Kobylak | 4175ffb | 2021-08-02 14:51:05 +0000 | [diff] [blame] | 766 | tmpAdditionalData.clear(); | 
|  | 767 | tmpAdditionalData["EXPECTED_COUNT"] = | 
| Adriana Kobylak | 70e7f93 | 2021-06-10 18:53:56 +0000 | [diff] [blame] | 768 | std::to_string(config.second.powerSupplyCount); | 
| Adriana Kobylak | 4175ffb | 2021-08-02 14:51:05 +0000 | [diff] [blame] | 769 | tmpAdditionalData["ACTUAL_COUNT"] = std::to_string(presentCount); | 
| Adriana Kobylak | 70e7f93 | 2021-06-10 18:53:56 +0000 | [diff] [blame] | 770 | continue; | 
|  | 771 | } | 
| Adriana Kobylak | 4175ffb | 2021-08-02 14:51:05 +0000 | [diff] [blame] | 772 |  | 
|  | 773 | bool voltageValidated = true; | 
|  | 774 | for (const auto& psu : psus) | 
|  | 775 | { | 
|  | 776 | if (!psu->isPresent()) | 
|  | 777 | { | 
|  | 778 | // Only present PSUs report a valid input voltage | 
|  | 779 | continue; | 
|  | 780 | } | 
|  | 781 |  | 
|  | 782 | double actualInputVoltage; | 
|  | 783 | int inputVoltage; | 
|  | 784 | psu->getInputVoltage(actualInputVoltage, inputVoltage); | 
|  | 785 |  | 
|  | 786 | if (std::find(config.second.inputVoltage.begin(), | 
|  | 787 | config.second.inputVoltage.end(), | 
|  | 788 | inputVoltage) == config.second.inputVoltage.end()) | 
|  | 789 | { | 
|  | 790 | tmpAdditionalData.clear(); | 
|  | 791 | tmpAdditionalData["ACTUAL_VOLTAGE"] = | 
|  | 792 | std::to_string(actualInputVoltage); | 
|  | 793 | for (const auto& voltage : config.second.inputVoltage) | 
|  | 794 | { | 
|  | 795 | tmpAdditionalData["EXPECTED_VOLTAGE"] += | 
|  | 796 | std::to_string(voltage) + " "; | 
|  | 797 | } | 
|  | 798 | tmpAdditionalData["CALLOUT_INVENTORY_PATH"] = | 
|  | 799 | psu->getInventoryPath(); | 
|  | 800 |  | 
|  | 801 | voltageValidated = false; | 
|  | 802 | break; | 
|  | 803 | } | 
|  | 804 | } | 
|  | 805 | if (!voltageValidated) | 
|  | 806 | { | 
|  | 807 | continue; | 
|  | 808 | } | 
|  | 809 |  | 
| Adriana Kobylak | 4d9aaf9 | 2021-06-30 15:27:42 +0000 | [diff] [blame] | 810 | return true; | 
| Adriana Kobylak | 70e7f93 | 2021-06-10 18:53:56 +0000 | [diff] [blame] | 811 | } | 
| Adriana Kobylak | 70e7f93 | 2021-06-10 18:53:56 +0000 | [diff] [blame] | 812 |  | 
| Adriana Kobylak | 4175ffb | 2021-08-02 14:51:05 +0000 | [diff] [blame] | 813 | additionalData.insert(tmpAdditionalData.begin(), tmpAdditionalData.end()); | 
| Adriana Kobylak | 4d9aaf9 | 2021-06-30 15:27:42 +0000 | [diff] [blame] | 814 | return false; | 
| Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 815 | } | 
|  | 816 |  | 
| Adriana Kobylak | 523704d | 2021-09-21 15:55:41 +0000 | [diff] [blame] | 817 | bool PSUManager::validateModelName( | 
|  | 818 | std::string& model, std::map<std::string, std::string>& additionalData) | 
|  | 819 | { | 
|  | 820 | // Check that all PSUs have the same model name. Initialize the model | 
|  | 821 | // 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] | 822 | // against the rest of the PSUs and get its inventory path to use as callout | 
|  | 823 | // if needed. | 
| Adriana Kobylak | 523704d | 2021-09-21 15:55:41 +0000 | [diff] [blame] | 824 | model.clear(); | 
| Adriana Kobylak | b70eae9 | 2022-01-20 22:09:56 +0000 | [diff] [blame] | 825 | std::string modelInventoryPath{}; | 
| Adriana Kobylak | 523704d | 2021-09-21 15:55:41 +0000 | [diff] [blame] | 826 | for (const auto& psu : psus) | 
|  | 827 | { | 
|  | 828 | auto psuModel = psu->getModelName(); | 
|  | 829 | if (psuModel.empty()) | 
|  | 830 | { | 
|  | 831 | continue; | 
|  | 832 | } | 
|  | 833 | if (model.empty()) | 
|  | 834 | { | 
|  | 835 | model = psuModel; | 
| Adriana Kobylak | b70eae9 | 2022-01-20 22:09:56 +0000 | [diff] [blame] | 836 | modelInventoryPath = psu->getInventoryPath(); | 
| Adriana Kobylak | 523704d | 2021-09-21 15:55:41 +0000 | [diff] [blame] | 837 | continue; | 
|  | 838 | } | 
|  | 839 | if (psuModel != model) | 
|  | 840 | { | 
| Adriana Kobylak | b70eae9 | 2022-01-20 22:09:56 +0000 | [diff] [blame] | 841 | if (supportedConfigs.find(model) != supportedConfigs.end()) | 
|  | 842 | { | 
|  | 843 | // The base model is supported, callout the mismatched PSU. The | 
|  | 844 | // mismatched PSU may or may not be supported. | 
|  | 845 | additionalData["EXPECTED_MODEL"] = model; | 
|  | 846 | additionalData["ACTUAL_MODEL"] = psuModel; | 
|  | 847 | additionalData["CALLOUT_INVENTORY_PATH"] = | 
|  | 848 | psu->getInventoryPath(); | 
|  | 849 | } | 
|  | 850 | else if (supportedConfigs.find(psuModel) != supportedConfigs.end()) | 
|  | 851 | { | 
|  | 852 | // The base model is not supported, but the mismatched PSU is, | 
|  | 853 | // callout the base PSU. | 
|  | 854 | additionalData["EXPECTED_MODEL"] = psuModel; | 
|  | 855 | additionalData["ACTUAL_MODEL"] = model; | 
|  | 856 | additionalData["CALLOUT_INVENTORY_PATH"] = modelInventoryPath; | 
|  | 857 | } | 
|  | 858 | else | 
|  | 859 | { | 
|  | 860 | // The base model and the mismatched PSU are not supported or | 
|  | 861 | // could not be found in the supported configuration, callout | 
|  | 862 | // the mismatched PSU. | 
|  | 863 | additionalData["EXPECTED_MODEL"] = model; | 
|  | 864 | additionalData["ACTUAL_MODEL"] = psuModel; | 
|  | 865 | additionalData["CALLOUT_INVENTORY_PATH"] = | 
|  | 866 | psu->getInventoryPath(); | 
|  | 867 | } | 
| Adriana Kobylak | 523704d | 2021-09-21 15:55:41 +0000 | [diff] [blame] | 868 | model.clear(); | 
|  | 869 | return false; | 
|  | 870 | } | 
|  | 871 | } | 
|  | 872 | return true; | 
|  | 873 | } | 
|  | 874 |  | 
| Adriana Kobylak | c0a0758 | 2021-10-13 15:52:25 +0000 | [diff] [blame] | 875 | void PSUManager::setPowerConfigGPIO() | 
|  | 876 | { | 
|  | 877 | if (!powerConfigGPIO) | 
|  | 878 | { | 
|  | 879 | return; | 
|  | 880 | } | 
|  | 881 |  | 
|  | 882 | std::string model{}; | 
|  | 883 | std::map<std::string, std::string> additionalData; | 
|  | 884 | if (!validateModelName(model, additionalData)) | 
|  | 885 | { | 
|  | 886 | return; | 
|  | 887 | } | 
|  | 888 |  | 
|  | 889 | auto config = supportedConfigs.find(model); | 
|  | 890 | if (config != supportedConfigs.end()) | 
|  | 891 | { | 
|  | 892 | // The power-config-full-load is an open drain GPIO. Set it to low (0) | 
|  | 893 | // if the supported configuration indicates that this system model | 
|  | 894 | // expects the maximum number of power supplies (full load set to true). | 
|  | 895 | // Else, set it to high (1), this is the default. | 
|  | 896 | auto powerConfigValue = | 
|  | 897 | (config->second.powerConfigFullLoad == true ? 0 : 1); | 
|  | 898 | auto flags = gpiod::line_request::FLAG_OPEN_DRAIN; | 
|  | 899 | powerConfigGPIO->write(powerConfigValue, flags); | 
|  | 900 | } | 
|  | 901 | } | 
|  | 902 |  | 
| Adriana Kobylak | e5b1e08 | 2022-03-02 15:37:32 +0000 | [diff] [blame] | 903 | void PSUManager::setBrownout(std::map<std::string, std::string>& additionalData) | 
|  | 904 | { | 
|  | 905 | powerSystemInputs.status(sdbusplus::xyz::openbmc_project::State::Decorator:: | 
|  | 906 | server::PowerSystemInputs::Status::Fault); | 
|  | 907 | if (!brownoutLogged) | 
|  | 908 | { | 
|  | 909 | if (powerOn) | 
|  | 910 | { | 
|  | 911 | createError( | 
|  | 912 | "xyz.openbmc_project.State.Shutdown.Power.Error.Blackout", | 
|  | 913 | additionalData); | 
|  | 914 | brownoutLogged = true; | 
|  | 915 | } | 
|  | 916 | } | 
|  | 917 | } | 
|  | 918 |  | 
|  | 919 | void PSUManager::clearBrownout() | 
|  | 920 | { | 
|  | 921 | powerSystemInputs.status(sdbusplus::xyz::openbmc_project::State::Decorator:: | 
|  | 922 | server::PowerSystemInputs::Status::Good); | 
|  | 923 | brownoutLogged = false; | 
|  | 924 | } | 
|  | 925 |  | 
| Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 926 | } // namespace phosphor::power::manager |