Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 1 | #include "psu_manager.hpp" |
| 2 | |
| 3 | #include "utility.hpp" |
| 4 | |
Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 5 | #include <fmt/format.h> |
| 6 | #include <sys/types.h> |
| 7 | #include <unistd.h> |
| 8 | |
Brandon Wyman | ecbecbc | 2021-08-31 22:53:21 +0000 | [diff] [blame] | 9 | #include <regex> |
| 10 | |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 11 | using namespace phosphor::logging; |
| 12 | |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 13 | namespace phosphor::power::manager |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 14 | { |
| 15 | |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 16 | constexpr auto IBMCFFPSInterface = |
| 17 | "xyz.openbmc_project.Configuration.IBMCFFPSConnector"; |
| 18 | constexpr auto i2cBusProp = "I2CBus"; |
| 19 | constexpr auto i2cAddressProp = "I2CAddress"; |
| 20 | constexpr auto psuNameProp = "Name"; |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 21 | constexpr auto presLineName = "NamedPresenceGpio"; |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 22 | |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 23 | constexpr auto supportedConfIntf = |
| 24 | "xyz.openbmc_project.Configuration.SupportedConfiguration"; |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 25 | |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 26 | PSUManager::PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e) : |
| 27 | bus(bus) |
| 28 | { |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 29 | // Subscribe to InterfacesAdded before doing a property read, otherwise |
| 30 | // the interface could be created after the read attempt but before the |
| 31 | // match is created. |
| 32 | entityManagerIfacesAddedMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 33 | bus, |
| 34 | sdbusplus::bus::match::rules::interfacesAdded() + |
| 35 | sdbusplus::bus::match::rules::sender( |
| 36 | "xyz.openbmc_project.EntityManager"), |
| 37 | std::bind(&PSUManager::entityManagerIfaceAdded, this, |
| 38 | std::placeholders::_1)); |
| 39 | getPSUConfiguration(); |
| 40 | getSystemProperties(); |
| 41 | |
| 42 | using namespace sdeventplus; |
| 43 | auto interval = std::chrono::milliseconds(1000); |
| 44 | timer = std::make_unique<utility::Timer<ClockId::Monotonic>>( |
| 45 | e, std::bind(&PSUManager::analyze, this), interval); |
| 46 | |
Adriana Kobylak | a4d38fa | 2021-10-05 19:57:47 +0000 | [diff] [blame] | 47 | validationTimer = std::make_unique<utility::Timer<ClockId::Monotonic>>( |
| 48 | e, std::bind(&PSUManager::validateConfig, this)); |
| 49 | |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 50 | // Subscribe to power state changes |
| 51 | powerService = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus); |
| 52 | powerOnMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 53 | bus, |
| 54 | sdbusplus::bus::match::rules::propertiesChanged(POWER_OBJ_PATH, |
| 55 | POWER_IFACE), |
| 56 | [this](auto& msg) { this->powerStateChanged(msg); }); |
| 57 | |
| 58 | initialize(); |
| 59 | } |
| 60 | |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 61 | void PSUManager::getPSUConfiguration() |
| 62 | { |
| 63 | using namespace phosphor::power::util; |
| 64 | auto depth = 0; |
| 65 | auto objects = getSubTree(bus, "/", IBMCFFPSInterface, depth); |
| 66 | |
| 67 | psus.clear(); |
| 68 | |
| 69 | // I should get a map of objects back. |
| 70 | // Each object will have a path, a service, and an interface. |
| 71 | // The interface should match the one passed into this function. |
| 72 | for (const auto& [path, services] : objects) |
| 73 | { |
| 74 | auto service = services.begin()->first; |
| 75 | |
| 76 | if (path.empty() || service.empty()) |
| 77 | { |
| 78 | continue; |
| 79 | } |
| 80 | |
| 81 | // For each object in the array of objects, I want to get properties |
| 82 | // from the service, path, and interface. |
| 83 | auto properties = |
| 84 | getAllProperties(bus, path, IBMCFFPSInterface, service); |
| 85 | |
| 86 | getPSUProperties(properties); |
| 87 | } |
| 88 | |
| 89 | if (psus.empty()) |
| 90 | { |
| 91 | // Interface or properties not found. Let the Interfaces Added callback |
| 92 | // process the information once the interfaces are added to D-Bus. |
| 93 | log<level::INFO>(fmt::format("No power supplies to monitor").c_str()); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void PSUManager::getPSUProperties(util::DbusPropertyMap& properties) |
| 98 | { |
| 99 | // From passed in properties, I want to get: I2CBus, I2CAddress, |
| 100 | // and Name. Create a power supply object, using Name to build the inventory |
| 101 | // path. |
| 102 | const auto basePSUInvPath = |
| 103 | "/xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply"; |
| 104 | uint64_t* i2cbus = nullptr; |
| 105 | uint64_t* i2caddr = nullptr; |
| 106 | std::string* psuname = nullptr; |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 107 | std::string* preslineptr = nullptr; |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 108 | |
| 109 | for (const auto& property : properties) |
| 110 | { |
| 111 | try |
| 112 | { |
| 113 | if (property.first == i2cBusProp) |
| 114 | { |
| 115 | i2cbus = std::get_if<uint64_t>(&properties[i2cBusProp]); |
| 116 | } |
| 117 | else if (property.first == i2cAddressProp) |
| 118 | { |
| 119 | i2caddr = std::get_if<uint64_t>(&properties[i2cAddressProp]); |
| 120 | } |
| 121 | else if (property.first == psuNameProp) |
| 122 | { |
| 123 | psuname = std::get_if<std::string>(&properties[psuNameProp]); |
| 124 | } |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 125 | else if (property.first == presLineName) |
| 126 | { |
| 127 | preslineptr = |
| 128 | std::get_if<std::string>(&properties[presLineName]); |
| 129 | } |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 130 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 131 | catch (const std::exception& e) |
Adriana Kobylak | 0c9a33d | 2021-09-13 18:05:09 +0000 | [diff] [blame] | 132 | {} |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | if ((i2cbus) && (i2caddr) && (psuname) && (!psuname->empty())) |
| 136 | { |
| 137 | std::string invpath = basePSUInvPath; |
| 138 | invpath.push_back(psuname->back()); |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 139 | std::string presline = ""; |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 140 | |
| 141 | log<level::DEBUG>(fmt::format("Inventory Path: {}", invpath).c_str()); |
| 142 | |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 143 | if (nullptr != preslineptr) |
| 144 | { |
| 145 | presline = *preslineptr; |
| 146 | } |
| 147 | |
Brandon Wyman | ecbecbc | 2021-08-31 22:53:21 +0000 | [diff] [blame] | 148 | auto invMatch = |
| 149 | std::find_if(psus.begin(), psus.end(), [&invpath](auto& psu) { |
| 150 | return psu->getInventoryPath() == invpath; |
| 151 | }); |
| 152 | if (invMatch != psus.end()) |
| 153 | { |
| 154 | // This power supply has the same inventory path as the one with |
| 155 | // information just added to D-Bus. |
| 156 | // Changes to GPIO line name unlikely, so skip checking. |
| 157 | // Changes to the I2C bus and address unlikely, as that would |
| 158 | // require corresponding device tree updates. |
| 159 | // Return out to avoid duplicate object creation. |
| 160 | return; |
| 161 | } |
| 162 | |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 163 | log<level::DEBUG>( |
| 164 | fmt::format("make PowerSupply bus: {} addr: {} presline: {}", |
| 165 | *i2cbus, *i2caddr, presline) |
| 166 | .c_str()); |
| 167 | auto psu = std::make_unique<PowerSupply>(bus, invpath, *i2cbus, |
| 168 | *i2caddr, presline); |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 169 | psus.emplace_back(std::move(psu)); |
| 170 | } |
| 171 | |
| 172 | if (psus.empty()) |
| 173 | { |
| 174 | log<level::INFO>(fmt::format("No power supplies to monitor").c_str()); |
| 175 | } |
| 176 | } |
| 177 | |
Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 178 | void PSUManager::populateSysProperties(const util::DbusPropertyMap& properties) |
| 179 | { |
| 180 | try |
| 181 | { |
Adriana Kobylak | 9ea66a6 | 2021-03-24 17:54:14 +0000 | [diff] [blame] | 182 | auto propIt = properties.find("SupportedType"); |
| 183 | if (propIt == properties.end()) |
| 184 | { |
| 185 | return; |
| 186 | } |
| 187 | const std::string* type = std::get_if<std::string>(&(propIt->second)); |
| 188 | if ((type == nullptr) || (*type != "PowerSupply")) |
| 189 | { |
| 190 | return; |
| 191 | } |
| 192 | |
Adriana Kobylak | 9ea66a6 | 2021-03-24 17:54:14 +0000 | [diff] [blame] | 193 | propIt = properties.find("SupportedModel"); |
| 194 | if (propIt == properties.end()) |
| 195 | { |
| 196 | return; |
| 197 | } |
Adriana Kobylak | d3a70d9 | 2021-06-04 16:24:45 +0000 | [diff] [blame] | 198 | const std::string* model = std::get_if<std::string>(&(propIt->second)); |
| 199 | if (model == nullptr) |
Adriana Kobylak | 9ea66a6 | 2021-03-24 17:54:14 +0000 | [diff] [blame] | 200 | { |
| 201 | return; |
| 202 | } |
Adriana Kobylak | 9ea66a6 | 2021-03-24 17:54:14 +0000 | [diff] [blame] | 203 | |
Adriana Kobylak | d3a70d9 | 2021-06-04 16:24:45 +0000 | [diff] [blame] | 204 | sys_properties sys; |
Adriana Kobylak | 9ea66a6 | 2021-03-24 17:54:14 +0000 | [diff] [blame] | 205 | propIt = properties.find("RedundantCount"); |
Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 206 | if (propIt != properties.end()) |
| 207 | { |
| 208 | const uint64_t* count = std::get_if<uint64_t>(&(propIt->second)); |
| 209 | if (count != nullptr) |
| 210 | { |
Adriana Kobylak | d3a70d9 | 2021-06-04 16:24:45 +0000 | [diff] [blame] | 211 | sys.powerSupplyCount = *count; |
Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
Adriana Kobylak | 9ea66a6 | 2021-03-24 17:54:14 +0000 | [diff] [blame] | 214 | propIt = properties.find("InputVoltage"); |
| 215 | if (propIt != properties.end()) |
| 216 | { |
Adriana Kobylak | d3a70d9 | 2021-06-04 16:24:45 +0000 | [diff] [blame] | 217 | const std::vector<uint64_t>* voltage = |
| 218 | std::get_if<std::vector<uint64_t>>(&(propIt->second)); |
Adriana Kobylak | 9ea66a6 | 2021-03-24 17:54:14 +0000 | [diff] [blame] | 219 | if (voltage != nullptr) |
| 220 | { |
| 221 | sys.inputVoltage = *voltage; |
| 222 | } |
| 223 | } |
| 224 | |
Adriana Kobylak | d3a70d9 | 2021-06-04 16:24:45 +0000 | [diff] [blame] | 225 | supportedConfigs.emplace(*model, sys); |
Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 226 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 227 | catch (const std::exception& e) |
Adriana Kobylak | 0c9a33d | 2021-09-13 18:05:09 +0000 | [diff] [blame] | 228 | {} |
Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 231 | void PSUManager::getSystemProperties() |
| 232 | { |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 233 | |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 234 | try |
| 235 | { |
| 236 | util::DbusSubtree subtree = |
| 237 | util::getSubTree(bus, INVENTORY_OBJ_PATH, supportedConfIntf, 0); |
Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 238 | if (subtree.empty()) |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 239 | { |
| 240 | throw std::runtime_error("Supported Configuration Not Found"); |
| 241 | } |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 242 | |
Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 243 | for (const auto& [objPath, services] : subtree) |
| 244 | { |
| 245 | std::string service = services.begin()->first; |
| 246 | if (objPath.empty() || service.empty()) |
| 247 | { |
| 248 | continue; |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 249 | } |
Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 250 | auto properties = util::getAllProperties( |
| 251 | bus, objPath, supportedConfIntf, service); |
| 252 | populateSysProperties(properties); |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 253 | } |
| 254 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 255 | catch (const std::exception& e) |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 256 | { |
| 257 | // Interface or property not found. Let the Interfaces Added callback |
| 258 | // process the information once the interfaces are added to D-Bus. |
| 259 | } |
| 260 | } |
| 261 | |
Brandon Wyman | 3e42913 | 2021-03-18 18:03:14 -0500 | [diff] [blame] | 262 | void PSUManager::entityManagerIfaceAdded(sdbusplus::message::message& msg) |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 263 | { |
| 264 | try |
| 265 | { |
| 266 | sdbusplus::message::object_path objPath; |
Adriana Kobylak | e1074d8 | 2021-03-16 20:46:44 +0000 | [diff] [blame] | 267 | std::map<std::string, std::map<std::string, util::DbusVariant>> |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 268 | interfaces; |
| 269 | msg.read(objPath, interfaces); |
| 270 | |
| 271 | auto itIntf = interfaces.find(supportedConfIntf); |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 272 | if (itIntf != interfaces.cend()) |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 273 | { |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 274 | populateSysProperties(itIntf->second); |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 275 | } |
| 276 | |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 277 | itIntf = interfaces.find(IBMCFFPSInterface); |
| 278 | if (itIntf != interfaces.cend()) |
| 279 | { |
| 280 | log<level::INFO>( |
| 281 | fmt::format("InterfacesAdded for: {}", IBMCFFPSInterface) |
| 282 | .c_str()); |
| 283 | getPSUProperties(itIntf->second); |
| 284 | } |
Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 285 | |
| 286 | // Call to validate the psu configuration if the power is on and both |
| 287 | // the IBMCFFPSConnector and SupportedConfiguration interfaces have been |
| 288 | // processed |
| 289 | if (powerOn && !psus.empty() && !supportedConfigs.empty()) |
| 290 | { |
Adriana Kobylak | a4d38fa | 2021-10-05 19:57:47 +0000 | [diff] [blame] | 291 | validationTimer->restartOnce(validationTimeout); |
Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 292 | } |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 293 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 294 | catch (const std::exception& e) |
Adriana Kobylak | 9bab9e1 | 2021-02-24 15:32:03 -0600 | [diff] [blame] | 295 | { |
| 296 | // Ignore, the property may be of a different type than expected. |
| 297 | } |
| 298 | } |
| 299 | |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 300 | void PSUManager::powerStateChanged(sdbusplus::message::message& msg) |
| 301 | { |
| 302 | int32_t state = 0; |
| 303 | std::string msgSensor; |
Patrick Williams | abe4941 | 2020-05-13 17:59:47 -0500 | [diff] [blame] | 304 | std::map<std::string, std::variant<int32_t>> msgData; |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 305 | msg.read(msgSensor, msgData); |
| 306 | |
| 307 | // Check if it was the Present property that changed. |
| 308 | auto valPropMap = msgData.find("state"); |
| 309 | if (valPropMap != msgData.end()) |
| 310 | { |
| 311 | state = std::get<int32_t>(valPropMap->second); |
| 312 | |
| 313 | // Power is on when state=1. Clear faults. |
| 314 | if (state) |
| 315 | { |
| 316 | powerOn = true; |
Adriana Kobylak | a4d38fa | 2021-10-05 19:57:47 +0000 | [diff] [blame] | 317 | validationTimer->restartOnce(validationTimeout); |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 318 | clearFaults(); |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | powerOn = false; |
Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 323 | runValidateConfig = true; |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
Brandon Wyman | 8b66288 | 2021-10-08 17:31:51 +0000 | [diff] [blame] | 328 | void PSUManager::createError(const std::string& faultName, |
| 329 | std::map<std::string, std::string>& additionalData) |
Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 330 | { |
| 331 | using namespace sdbusplus::xyz::openbmc_project; |
| 332 | constexpr auto loggingObjectPath = "/xyz/openbmc_project/logging"; |
| 333 | constexpr auto loggingCreateInterface = |
| 334 | "xyz.openbmc_project.Logging.Create"; |
| 335 | |
| 336 | try |
| 337 | { |
Brandon Wyman | 8b66288 | 2021-10-08 17:31:51 +0000 | [diff] [blame] | 338 | additionalData["_PID"] = std::to_string(getpid()); |
| 339 | |
Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 340 | auto service = |
| 341 | util::getService(loggingObjectPath, loggingCreateInterface, bus); |
| 342 | |
| 343 | if (service.empty()) |
| 344 | { |
| 345 | log<level::ERR>("Unable to get logging manager service"); |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | auto method = bus.new_method_call(service.c_str(), loggingObjectPath, |
| 350 | loggingCreateInterface, "Create"); |
| 351 | |
| 352 | auto level = Logging::server::Entry::Level::Error; |
| 353 | method.append(faultName, level, additionalData); |
| 354 | |
| 355 | auto reply = bus.call(method); |
| 356 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 357 | catch (const std::exception& e) |
Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 358 | { |
| 359 | log<level::ERR>( |
| 360 | fmt::format( |
| 361 | "Failed creating event log for fault {} due to error {}", |
| 362 | faultName, e.what()) |
| 363 | .c_str()); |
| 364 | } |
| 365 | } |
| 366 | |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 367 | void PSUManager::analyze() |
| 368 | { |
| 369 | for (auto& psu : psus) |
| 370 | { |
| 371 | psu->analyze(); |
| 372 | } |
| 373 | |
Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 374 | if (powerOn) |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 375 | { |
Adriana Kobylak | f2ba146 | 2021-06-24 15:16:17 +0000 | [diff] [blame] | 376 | std::map<std::string, std::string> additionalData; |
Adriana Kobylak | f2ba146 | 2021-06-24 15:16:17 +0000 | [diff] [blame] | 377 | |
Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 378 | for (auto& psu : psus) |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 379 | { |
Brandon Wyman | ec0b8dc | 2021-10-08 21:49:43 +0000 | [diff] [blame] | 380 | additionalData.clear(); |
Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 381 | // TODO: Fault priorities #918 |
| 382 | if (!psu->isFaultLogged() && !psu->isPresent()) |
| 383 | { |
Brandon Wyman | da369c7 | 2021-10-08 18:43:30 +0000 | [diff] [blame] | 384 | std::map<std::string, std::string> requiredPSUsData; |
| 385 | auto requiredPSUsPresent = hasRequiredPSUs(requiredPSUsData); |
Adriana Kobylak | f2ba146 | 2021-06-24 15:16:17 +0000 | [diff] [blame] | 386 | if (!requiredPSUsPresent) |
| 387 | { |
Brandon Wyman | da369c7 | 2021-10-08 18:43:30 +0000 | [diff] [blame] | 388 | additionalData.merge(requiredPSUsData); |
Adriana Kobylak | f2ba146 | 2021-06-24 15:16:17 +0000 | [diff] [blame] | 389 | // Create error for power supply missing. |
| 390 | additionalData["CALLOUT_INVENTORY_PATH"] = |
| 391 | psu->getInventoryPath(); |
| 392 | additionalData["CALLOUT_PRIORITY"] = "H"; |
| 393 | createError( |
| 394 | "xyz.openbmc_project.Power.PowerSupply.Error.Missing", |
| 395 | additionalData); |
| 396 | } |
Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 397 | psu->setFaultLogged(); |
| 398 | } |
| 399 | else if (!psu->isFaultLogged() && psu->isFaulted()) |
| 400 | { |
Brandon Wyman | 786b6f4 | 2021-10-12 20:21:41 +0000 | [diff] [blame] | 401 | // Add STATUS_WORD and STATUS_MFR last response, in padded |
| 402 | // hexadecimal format. |
Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 403 | additionalData["STATUS_WORD"] = |
Brandon Wyman | 786b6f4 | 2021-10-12 20:21:41 +0000 | [diff] [blame] | 404 | fmt::format("{:#04x}", psu->getStatusWord()); |
Jay Meyer | 10d9405 | 2020-11-30 14:41:21 -0600 | [diff] [blame] | 405 | additionalData["STATUS_MFR"] = |
Brandon Wyman | 786b6f4 | 2021-10-12 20:21:41 +0000 | [diff] [blame] | 406 | fmt::format("{:#02x}", psu->getMFRFault()); |
Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 407 | // If there are faults being reported, they possibly could be |
| 408 | // related to a bug in the firmware version running on the power |
| 409 | // supply. Capture that data into the error as well. |
| 410 | additionalData["FW_VERSION"] = psu->getFWVersion(); |
| 411 | |
Brandon Wyman | b85b9dd | 2021-10-19 21:25:17 +0000 | [diff] [blame] | 412 | if (psu->hasCommFault()) |
| 413 | { |
Brandon Wyman | 85c7bf4 | 2021-10-19 22:28:48 +0000 | [diff] [blame^] | 414 | additionalData["STATUS_CML"] = |
| 415 | fmt::format("{:#02x}", psu->getStatusCML()); |
Brandon Wyman | b85b9dd | 2021-10-19 21:25:17 +0000 | [diff] [blame] | 416 | /* Attempts to communicate with the power supply have |
| 417 | * reached there limit. Create an error. */ |
| 418 | additionalData["CALLOUT_DEVICE_PATH"] = |
| 419 | psu->getDevicePath(); |
| 420 | |
| 421 | createError( |
| 422 | "xyz.openbmc_project.Power.PowerSupply.Error.CommFault", |
| 423 | additionalData); |
| 424 | |
| 425 | psu->setFaultLogged(); |
| 426 | } |
| 427 | else if ((psu->hasInputFault() || psu->hasVINUVFault())) |
Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 428 | { |
Brandon Wyman | f07bc79 | 2021-10-12 19:00:35 +0000 | [diff] [blame] | 429 | // Include STATUS_INPUT for input faults. |
| 430 | additionalData["STATUS_INPUT"] = |
| 431 | fmt::format("{:#02x}", psu->getStatusInput()); |
| 432 | |
Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 433 | /* The power supply location might be needed if the input |
| 434 | * fault is due to a problem with the power supply itself. |
| 435 | * Include the inventory path with a call out priority of |
| 436 | * low. |
| 437 | */ |
| 438 | additionalData["CALLOUT_INVENTORY_PATH"] = |
| 439 | psu->getInventoryPath(); |
| 440 | additionalData["CALLOUT_PRIORITY"] = "L"; |
| 441 | createError("xyz.openbmc_project.Power.PowerSupply.Error." |
| 442 | "InputFault", |
| 443 | additionalData); |
| 444 | psu->setFaultLogged(); |
| 445 | } |
| 446 | else if (psu->hasMFRFault()) |
| 447 | { |
| 448 | /* This can represent a variety of faults that result in |
| 449 | * calling out the power supply for replacement: Output |
| 450 | * OverCurrent, Output Under Voltage, and potentially other |
| 451 | * faults. |
| 452 | * |
| 453 | * Also plan on putting specific fault in AdditionalData, |
| 454 | * along with register names and register values |
| 455 | * (STATUS_WORD, STATUS_MFR, etc.).*/ |
| 456 | |
| 457 | additionalData["CALLOUT_INVENTORY_PATH"] = |
| 458 | psu->getInventoryPath(); |
| 459 | |
| 460 | createError( |
| 461 | "xyz.openbmc_project.Power.PowerSupply.Error.Fault", |
Brandon Wyman | 52e54e8 | 2020-10-08 14:44:58 -0500 | [diff] [blame] | 462 | additionalData); |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 463 | |
Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 464 | psu->setFaultLogged(); |
| 465 | } |
Brandon Wyman | 4176d6b | 2020-10-07 17:41:06 -0500 | [diff] [blame] | 466 | } |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 471 | void PSUManager::validateConfig() |
| 472 | { |
Adriana Kobylak | 70e7f93 | 2021-06-10 18:53:56 +0000 | [diff] [blame] | 473 | if (!runValidateConfig || supportedConfigs.empty()) |
Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 474 | { |
| 475 | return; |
| 476 | } |
| 477 | |
Adriana Kobylak | 4d9aaf9 | 2021-06-30 15:27:42 +0000 | [diff] [blame] | 478 | std::map<std::string, std::string> additionalData; |
| 479 | auto supported = hasRequiredPSUs(additionalData); |
| 480 | if (supported) |
| 481 | { |
| 482 | runValidateConfig = false; |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | // Validation failed, create an error log. |
| 487 | // Return without setting the runValidateConfig flag to false because |
| 488 | // it may be that an additional supported configuration interface is |
| 489 | // added and we need to validate it to see if it matches this system. |
| 490 | createError("xyz.openbmc_project.Power.PowerSupply.Error.NotSupported", |
| 491 | additionalData); |
| 492 | } |
| 493 | |
| 494 | bool PSUManager::hasRequiredPSUs( |
| 495 | std::map<std::string, std::string>& additionalData) |
| 496 | { |
Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 497 | std::string model{}; |
Adriana Kobylak | 523704d | 2021-09-21 15:55:41 +0000 | [diff] [blame] | 498 | if (!validateModelName(model, additionalData)) |
Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 499 | { |
Adriana Kobylak | 523704d | 2021-09-21 15:55:41 +0000 | [diff] [blame] | 500 | return false; |
Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 501 | } |
Adriana Kobylak | 70e7f93 | 2021-06-10 18:53:56 +0000 | [diff] [blame] | 502 | |
Adriana Kobylak | 4d9aaf9 | 2021-06-30 15:27:42 +0000 | [diff] [blame] | 503 | auto presentCount = |
| 504 | std::count_if(psus.begin(), psus.end(), |
| 505 | [](const auto& psu) { return psu->isPresent(); }); |
| 506 | |
Adriana Kobylak | 70e7f93 | 2021-06-10 18:53:56 +0000 | [diff] [blame] | 507 | // Validate the supported configurations. A system may support more than one |
Adriana Kobylak | 4175ffb | 2021-08-02 14:51:05 +0000 | [diff] [blame] | 508 | // power supply model configuration. Since all configurations need to be |
| 509 | // checked, the additional data would contain only the information of the |
| 510 | // last configuration that did not match. |
| 511 | std::map<std::string, std::string> tmpAdditionalData; |
Adriana Kobylak | 70e7f93 | 2021-06-10 18:53:56 +0000 | [diff] [blame] | 512 | for (const auto& config : supportedConfigs) |
| 513 | { |
Adriana Kobylak | 4d9aaf9 | 2021-06-30 15:27:42 +0000 | [diff] [blame] | 514 | if (config.first != model) |
Adriana Kobylak | 70e7f93 | 2021-06-10 18:53:56 +0000 | [diff] [blame] | 515 | { |
| 516 | continue; |
| 517 | } |
| 518 | if (presentCount != config.second.powerSupplyCount) |
| 519 | { |
Adriana Kobylak | 4175ffb | 2021-08-02 14:51:05 +0000 | [diff] [blame] | 520 | tmpAdditionalData.clear(); |
| 521 | tmpAdditionalData["EXPECTED_COUNT"] = |
Adriana Kobylak | 70e7f93 | 2021-06-10 18:53:56 +0000 | [diff] [blame] | 522 | std::to_string(config.second.powerSupplyCount); |
Adriana Kobylak | 4175ffb | 2021-08-02 14:51:05 +0000 | [diff] [blame] | 523 | tmpAdditionalData["ACTUAL_COUNT"] = std::to_string(presentCount); |
Adriana Kobylak | 70e7f93 | 2021-06-10 18:53:56 +0000 | [diff] [blame] | 524 | continue; |
| 525 | } |
Adriana Kobylak | 4175ffb | 2021-08-02 14:51:05 +0000 | [diff] [blame] | 526 | |
| 527 | bool voltageValidated = true; |
| 528 | for (const auto& psu : psus) |
| 529 | { |
| 530 | if (!psu->isPresent()) |
| 531 | { |
| 532 | // Only present PSUs report a valid input voltage |
| 533 | continue; |
| 534 | } |
| 535 | |
| 536 | double actualInputVoltage; |
| 537 | int inputVoltage; |
| 538 | psu->getInputVoltage(actualInputVoltage, inputVoltage); |
| 539 | |
| 540 | if (std::find(config.second.inputVoltage.begin(), |
| 541 | config.second.inputVoltage.end(), |
| 542 | inputVoltage) == config.second.inputVoltage.end()) |
| 543 | { |
| 544 | tmpAdditionalData.clear(); |
| 545 | tmpAdditionalData["ACTUAL_VOLTAGE"] = |
| 546 | std::to_string(actualInputVoltage); |
| 547 | for (const auto& voltage : config.second.inputVoltage) |
| 548 | { |
| 549 | tmpAdditionalData["EXPECTED_VOLTAGE"] += |
| 550 | std::to_string(voltage) + " "; |
| 551 | } |
| 552 | tmpAdditionalData["CALLOUT_INVENTORY_PATH"] = |
| 553 | psu->getInventoryPath(); |
| 554 | |
| 555 | voltageValidated = false; |
| 556 | break; |
| 557 | } |
| 558 | } |
| 559 | if (!voltageValidated) |
| 560 | { |
| 561 | continue; |
| 562 | } |
| 563 | |
Adriana Kobylak | 4d9aaf9 | 2021-06-30 15:27:42 +0000 | [diff] [blame] | 564 | return true; |
Adriana Kobylak | 70e7f93 | 2021-06-10 18:53:56 +0000 | [diff] [blame] | 565 | } |
Adriana Kobylak | 70e7f93 | 2021-06-10 18:53:56 +0000 | [diff] [blame] | 566 | |
Adriana Kobylak | 4175ffb | 2021-08-02 14:51:05 +0000 | [diff] [blame] | 567 | additionalData.insert(tmpAdditionalData.begin(), tmpAdditionalData.end()); |
Adriana Kobylak | 4d9aaf9 | 2021-06-30 15:27:42 +0000 | [diff] [blame] | 568 | return false; |
Adriana Kobylak | 8f16fb5 | 2021-03-31 15:50:15 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Adriana Kobylak | 523704d | 2021-09-21 15:55:41 +0000 | [diff] [blame] | 571 | bool PSUManager::validateModelName( |
| 572 | std::string& model, std::map<std::string, std::string>& additionalData) |
| 573 | { |
| 574 | // Check that all PSUs have the same model name. Initialize the model |
| 575 | // variable with the first PSU name found, then use it as a base to compare |
| 576 | // against the rest of the PSUs. |
| 577 | model.clear(); |
| 578 | for (const auto& psu : psus) |
| 579 | { |
| 580 | auto psuModel = psu->getModelName(); |
| 581 | if (psuModel.empty()) |
| 582 | { |
| 583 | continue; |
| 584 | } |
| 585 | if (model.empty()) |
| 586 | { |
| 587 | model = psuModel; |
| 588 | continue; |
| 589 | } |
| 590 | if (psuModel != model) |
| 591 | { |
| 592 | additionalData["EXPECTED_MODEL"] = model; |
| 593 | additionalData["ACTUAL_MODEL"] = psuModel; |
| 594 | additionalData["CALLOUT_INVENTORY_PATH"] = psu->getInventoryPath(); |
| 595 | model.clear(); |
| 596 | return false; |
| 597 | } |
| 598 | } |
| 599 | return true; |
| 600 | } |
| 601 | |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 602 | } // namespace phosphor::power::manager |