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 | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 9 | using namespace phosphor::logging; |
| 10 | |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 11 | namespace phosphor::power::manager |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 12 | { |
| 13 | |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 14 | PSUManager::PSUManager(sdbusplus::bus::bus& bus, const sdeventplus::Event& e, |
| 15 | const std::string& configfile) : |
| 16 | bus(bus) |
| 17 | { |
| 18 | // Parse out the JSON properties |
| 19 | sys_properties properties; |
| 20 | getJSONProperties(configfile, bus, properties, psus); |
| 21 | |
| 22 | using namespace sdeventplus; |
Jay Meyer | da36455 | 2020-06-26 16:14:11 -0500 | [diff] [blame] | 23 | auto interval = std::chrono::milliseconds(1000); |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 24 | timer = std::make_unique<utility::Timer<ClockId::Monotonic>>( |
| 25 | e, std::bind(&PSUManager::analyze, this), interval); |
| 26 | |
| 27 | minPSUs = {properties.minPowerSupplies}; |
| 28 | maxPSUs = {properties.maxPowerSupplies}; |
| 29 | |
| 30 | // Subscribe to power state changes |
| 31 | powerService = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus); |
| 32 | powerOnMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 33 | bus, |
| 34 | sdbusplus::bus::match::rules::propertiesChanged(POWER_OBJ_PATH, |
| 35 | POWER_IFACE), |
| 36 | [this](auto& msg) { this->powerStateChanged(msg); }); |
| 37 | |
| 38 | initialize(); |
| 39 | } |
| 40 | |
| 41 | void PSUManager::getJSONProperties( |
| 42 | const std::string& path, sdbusplus::bus::bus& bus, sys_properties& p, |
| 43 | std::vector<std::unique_ptr<PowerSupply>>& psus) |
| 44 | { |
| 45 | nlohmann::json configFileJSON = util::loadJSONFromFile(path.c_str()); |
| 46 | |
| 47 | if (configFileJSON == nullptr) |
| 48 | { |
| 49 | throw std::runtime_error("Failed to load JSON configuration file"); |
| 50 | } |
| 51 | |
| 52 | if (!configFileJSON.contains("SystemProperties")) |
| 53 | { |
| 54 | throw std::runtime_error("Missing required SystemProperties"); |
| 55 | } |
| 56 | |
| 57 | if (!configFileJSON.contains("PowerSupplies")) |
| 58 | { |
| 59 | throw std::runtime_error("Missing required PowerSupplies"); |
| 60 | } |
| 61 | |
| 62 | auto sysProps = configFileJSON["SystemProperties"]; |
| 63 | |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 64 | if (sysProps.contains("MinPowerSupplies")) |
| 65 | { |
| 66 | p.minPowerSupplies = sysProps["MinPowerSupplies"]; |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | p.minPowerSupplies = 0; |
| 71 | } |
| 72 | |
| 73 | if (sysProps.contains("MaxPowerSupplies")) |
| 74 | { |
| 75 | p.maxPowerSupplies = sysProps["MaxPowerSupplies"]; |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | p.maxPowerSupplies = 0; |
| 80 | } |
| 81 | |
| 82 | for (auto psuJSON : configFileJSON["PowerSupplies"]) |
| 83 | { |
Brandon Wyman | c63941c | 2020-01-27 16:49:33 -0600 | [diff] [blame] | 84 | if (psuJSON.contains("Inventory") && psuJSON.contains("Bus") && |
| 85 | psuJSON.contains("Address")) |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 86 | { |
| 87 | std::string invpath = psuJSON["Inventory"]; |
Brandon Wyman | c63941c | 2020-01-27 16:49:33 -0600 | [diff] [blame] | 88 | std::uint8_t i2cbus = psuJSON["Bus"]; |
| 89 | std::string i2caddr = psuJSON["Address"]; |
| 90 | auto psu = |
| 91 | std::make_unique<PowerSupply>(bus, invpath, i2cbus, i2caddr); |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 92 | psus.emplace_back(std::move(psu)); |
| 93 | } |
Brandon Wyman | c63941c | 2020-01-27 16:49:33 -0600 | [diff] [blame] | 94 | else |
| 95 | { |
| 96 | log<level::ERR>("Insufficient PowerSupply properties"); |
| 97 | } |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | if (psus.empty()) |
| 101 | { |
| 102 | throw std::runtime_error("No power supplies to monitor"); |
| 103 | } |
| 104 | } |
| 105 | |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 106 | void PSUManager::powerStateChanged(sdbusplus::message::message& msg) |
| 107 | { |
| 108 | int32_t state = 0; |
| 109 | std::string msgSensor; |
Patrick Williams | abe4941 | 2020-05-13 17:59:47 -0500 | [diff] [blame] | 110 | std::map<std::string, std::variant<int32_t>> msgData; |
Brandon Wyman | a0f33ce | 2019-10-17 18:32:29 -0500 | [diff] [blame] | 111 | msg.read(msgSensor, msgData); |
| 112 | |
| 113 | // Check if it was the Present property that changed. |
| 114 | auto valPropMap = msgData.find("state"); |
| 115 | if (valPropMap != msgData.end()) |
| 116 | { |
| 117 | state = std::get<int32_t>(valPropMap->second); |
| 118 | |
| 119 | // Power is on when state=1. Clear faults. |
| 120 | if (state) |
| 121 | { |
| 122 | powerOn = true; |
| 123 | clearFaults(); |
| 124 | } |
| 125 | else |
| 126 | { |
| 127 | powerOn = false; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 132 | void PSUManager::createError( |
| 133 | const std::string& faultName, |
| 134 | const std::map<std::string, std::string>& additionalData) |
| 135 | { |
| 136 | using namespace sdbusplus::xyz::openbmc_project; |
| 137 | constexpr auto loggingObjectPath = "/xyz/openbmc_project/logging"; |
| 138 | constexpr auto loggingCreateInterface = |
| 139 | "xyz.openbmc_project.Logging.Create"; |
| 140 | |
| 141 | try |
| 142 | { |
| 143 | auto service = |
| 144 | util::getService(loggingObjectPath, loggingCreateInterface, bus); |
| 145 | |
| 146 | if (service.empty()) |
| 147 | { |
| 148 | log<level::ERR>("Unable to get logging manager service"); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | auto method = bus.new_method_call(service.c_str(), loggingObjectPath, |
| 153 | loggingCreateInterface, "Create"); |
| 154 | |
| 155 | auto level = Logging::server::Entry::Level::Error; |
| 156 | method.append(faultName, level, additionalData); |
| 157 | |
| 158 | auto reply = bus.call(method); |
| 159 | } |
| 160 | catch (std::exception& e) |
| 161 | { |
| 162 | log<level::ERR>( |
| 163 | fmt::format( |
| 164 | "Failed creating event log for fault {} due to error {}", |
| 165 | faultName, e.what()) |
| 166 | .c_str()); |
| 167 | } |
| 168 | } |
| 169 | |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 170 | void PSUManager::analyze() |
| 171 | { |
| 172 | for (auto& psu : psus) |
| 173 | { |
| 174 | psu->analyze(); |
| 175 | } |
| 176 | |
Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 177 | if (powerOn) |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 178 | { |
Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 179 | for (auto& psu : psus) |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 180 | { |
Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 181 | std::map<std::string, std::string> additionalData; |
| 182 | additionalData["_PID"] = std::to_string(getpid()); |
| 183 | // TODO: Fault priorities #918 |
| 184 | if (!psu->isFaultLogged() && !psu->isPresent()) |
| 185 | { |
| 186 | // Create error for power supply missing. |
| 187 | additionalData["CALLOUT_INVENTORY_PATH"] = |
| 188 | psu->getInventoryPath(); |
| 189 | additionalData["CALLOUT_PRIORITY"] = "H"; |
| 190 | createError( |
| 191 | "xyz.openbmc_project.Power.PowerSupply.Error.Missing", |
| 192 | additionalData); |
| 193 | psu->setFaultLogged(); |
| 194 | } |
| 195 | else if (!psu->isFaultLogged() && psu->isFaulted()) |
| 196 | { |
| 197 | additionalData["STATUS_WORD"] = |
| 198 | std::to_string(psu->getStatusWord()); |
| 199 | // If there are faults being reported, they possibly could be |
| 200 | // related to a bug in the firmware version running on the power |
| 201 | // supply. Capture that data into the error as well. |
| 202 | additionalData["FW_VERSION"] = psu->getFWVersion(); |
| 203 | |
| 204 | if ((psu->hasInputFault() || psu->hasVINUVFault())) |
| 205 | { |
| 206 | /* The power supply location might be needed if the input |
| 207 | * fault is due to a problem with the power supply itself. |
| 208 | * Include the inventory path with a call out priority of |
| 209 | * low. |
| 210 | */ |
| 211 | additionalData["CALLOUT_INVENTORY_PATH"] = |
| 212 | psu->getInventoryPath(); |
| 213 | additionalData["CALLOUT_PRIORITY"] = "L"; |
| 214 | createError("xyz.openbmc_project.Power.PowerSupply.Error." |
| 215 | "InputFault", |
| 216 | additionalData); |
| 217 | psu->setFaultLogged(); |
| 218 | } |
| 219 | else if (psu->hasMFRFault()) |
| 220 | { |
| 221 | /* This can represent a variety of faults that result in |
| 222 | * calling out the power supply for replacement: Output |
| 223 | * OverCurrent, Output Under Voltage, and potentially other |
| 224 | * faults. |
| 225 | * |
| 226 | * Also plan on putting specific fault in AdditionalData, |
| 227 | * along with register names and register values |
| 228 | * (STATUS_WORD, STATUS_MFR, etc.).*/ |
| 229 | |
| 230 | additionalData["CALLOUT_INVENTORY_PATH"] = |
| 231 | psu->getInventoryPath(); |
| 232 | |
| 233 | createError( |
| 234 | "xyz.openbmc_project.Power.PowerSupply.Error.Fault", |
Brandon Wyman | 52e54e8 | 2020-10-08 14:44:58 -0500 | [diff] [blame] | 235 | additionalData); |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 236 | |
Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 237 | psu->setFaultLogged(); |
| 238 | } |
| 239 | else if (psu->hasCommFault()) |
| 240 | { |
| 241 | /* Attempts to communicate with the power supply have |
| 242 | * reached there limit. Create an error. */ |
| 243 | additionalData["CALLOUT_DEVICE_PATH"] = |
| 244 | psu->getDevicePath(); |
Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 245 | |
Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 246 | createError( |
| 247 | "xyz.openbmc_project.Power.PowerSupply.Error.CommFault", |
| 248 | additionalData); |
Brandon Wyman | b76ab24 | 2020-09-16 18:06:06 -0500 | [diff] [blame] | 249 | |
Brandon Wyman | 3180f4d | 2020-12-08 17:53:46 -0600 | [diff] [blame] | 250 | psu->setFaultLogged(); |
| 251 | } |
Brandon Wyman | 4176d6b | 2020-10-07 17:41:06 -0500 | [diff] [blame] | 252 | } |
Brandon Wyman | 63ea78b | 2020-09-24 16:49:09 -0500 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | } // namespace phosphor::power::manager |