1 /** 2 * Copyright © 2017 IBM Corporation 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #include <phosphor-logging/log.hpp> 17 #include <phosphor-logging/elog.hpp> 18 #include <xyz/openbmc_project/Sensor/Device/error.hpp> 19 #include <xyz/openbmc_project/Control/Device/error.hpp> 20 #include <xyz/openbmc_project/Power/Fault/error.hpp> 21 #include "elog-errors.hpp" 22 #include "names_values.hpp" 23 #include "power_supply.hpp" 24 #include "pmbus.hpp" 25 #include "utility.hpp" 26 27 using namespace phosphor::logging; 28 using namespace sdbusplus::xyz::openbmc_project::Control::Device::Error; 29 using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error; 30 using namespace sdbusplus::xyz::openbmc_project::Power::Fault::Error; 31 32 namespace witherspoon 33 { 34 namespace power 35 { 36 namespace psu 37 { 38 39 constexpr auto INVENTORY_OBJ_PATH = "/xyz/openbmc_project/inventory"; 40 constexpr auto INVENTORY_INTERFACE = "xyz.openbmc_project.Inventory.Item"; 41 constexpr auto PRESENT_PROP = "Present"; 42 constexpr auto POWER_OBJ_PATH = "/org/openbmc/control/power0"; 43 constexpr auto POWER_INTERFACE = "org.openbmc.control.Power"; 44 45 PowerSupply::PowerSupply(const std::string& name, size_t inst, 46 const std::string& objpath, 47 const std::string& invpath, 48 sdbusplus::bus::bus& bus, 49 event::Event& e, 50 std::chrono::seconds& t) 51 : Device(name, inst), monitorPath(objpath), pmbusIntf(objpath), 52 inventoryPath(invpath), bus(bus), event(e), powerOnInterval(t), 53 powerOnTimer(e, [this]() 54 { 55 this->powerOn = true; 56 }) 57 { 58 using namespace sdbusplus::bus; 59 auto present_obj_path = INVENTORY_OBJ_PATH + inventoryPath; 60 presentMatch = std::make_unique<match_t>(bus, 61 match::rules::propertiesChanged( 62 present_obj_path, 63 INVENTORY_INTERFACE), 64 [this](auto& msg) 65 { 66 this->inventoryChanged(msg); 67 }); 68 // Get initial presence state. 69 updatePresence(); 70 71 // Subscribe to power state changes 72 powerOnMatch = std::make_unique<match_t>(bus, 73 match::rules::propertiesChanged( 74 POWER_OBJ_PATH, 75 POWER_INTERFACE), 76 [this](auto& msg) 77 { 78 this->powerStateChanged(msg); 79 }); 80 // Get initial power state. 81 updatePowerState(); 82 } 83 84 85 void PowerSupply::analyze() 86 { 87 using namespace witherspoon::pmbus; 88 89 try 90 { 91 if (present) 92 { 93 std::uint16_t statusWord = 0; 94 95 // Read the 2 byte STATUS_WORD value to check for faults. 96 statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug); 97 98 //TODO: 3 consecutive reads should be performed. 99 // If 3 consecutive reads are seen, log the fault. 100 // Driver gives cached value, read once a second. 101 // increment for fault on, decrement for fault off, to deglitch. 102 // If count reaches 3, we have fault. If count reaches 0, fault is 103 // cleared. 104 105 checkInputFault(statusWord); 106 107 if (powerOn) 108 { 109 checkPGOrUnitOffFault(statusWord); 110 checkCurrentOutOverCurrentFault(statusWord); 111 } 112 } 113 } 114 catch (ReadFailure& e) 115 { 116 if (!readFailLogged) 117 { 118 commit<ReadFailure>(); 119 readFailLogged = true; 120 } 121 } 122 123 return; 124 } 125 126 void PowerSupply::inventoryChanged(sdbusplus::message::message& msg) 127 { 128 std::string msgSensor; 129 std::map<std::string, sdbusplus::message::variant<uint32_t, bool>> msgData; 130 msg.read(msgSensor, msgData); 131 132 // Check if it was the Present property that changed. 133 auto valPropMap = msgData.find(PRESENT_PROP); 134 if (valPropMap != msgData.end()) 135 { 136 present = sdbusplus::message::variant_ns::get<bool>(valPropMap->second); 137 138 if (present) 139 { 140 readFailLogged = false; 141 vinUVFault = false; 142 inputFault = false; 143 outputOCFault = false; 144 } 145 } 146 147 return; 148 } 149 150 void PowerSupply::updatePresence() 151 { 152 // Use getProperty utility function to get presence status. 153 std::string path = INVENTORY_OBJ_PATH + inventoryPath; 154 std::string service = "xyz.openbmc_project.Inventory.Manager"; 155 156 try 157 { 158 util::getProperty(INVENTORY_INTERFACE, PRESENT_PROP, path, 159 service, bus, this->present); 160 } 161 catch (std::exception& e) 162 { 163 // If we happen to be trying to update presence just as it is being 164 // updated, we may encounter a runtime_error. Just catch that for 165 // now, let the inventoryChanged signal handler update presence later. 166 present = false; 167 } 168 169 } 170 171 void PowerSupply::powerStateChanged(sdbusplus::message::message& msg) 172 { 173 int32_t state = 0; 174 std::string msgSensor; 175 std::map<std::string, sdbusplus::message::variant<int32_t, int32_t>> 176 msgData; 177 msg.read(msgSensor, msgData); 178 179 // Check if it was the Present property that changed. 180 auto valPropMap = msgData.find("state"); 181 if (valPropMap != msgData.end()) 182 { 183 state = sdbusplus::message::variant_ns::get<int32_t>(valPropMap->second); 184 185 // Power is on when state=1. Set the fault logged variables to false 186 // and start the power on timer when the state changes to 1. 187 if (state) 188 { 189 readFailLogged = false; 190 vinUVFault = false; 191 inputFault = false; 192 powerOnFault = false; 193 outputOCFault = false; 194 powerOnTimer.start(powerOnInterval, Timer::TimerType::oneshot); 195 } 196 else 197 { 198 powerOnTimer.stop(); 199 powerOn = false; 200 } 201 } 202 203 } 204 205 void PowerSupply::updatePowerState() 206 { 207 // When state = 1, system is powered on 208 int32_t state = 0; 209 210 try 211 { 212 auto service = util::getService(POWER_OBJ_PATH, 213 POWER_INTERFACE, 214 bus); 215 216 // Use getProperty utility function to get power state. 217 util::getProperty<int32_t>(POWER_INTERFACE, 218 "state", 219 POWER_OBJ_PATH, 220 service, 221 bus, 222 state); 223 224 if (state) 225 { 226 powerOn = true; 227 } 228 else 229 { 230 powerOn = false; 231 } 232 } 233 catch (std::exception& e) 234 { 235 log<level::INFO>("Failed to get power state. Assuming it is off."); 236 powerOn = false; 237 } 238 239 } 240 241 void PowerSupply::checkInputFault(const uint16_t statusWord) 242 { 243 using namespace witherspoon::pmbus; 244 245 std::uint8_t statusInput = 0; 246 247 if ((statusWord & status_word::VIN_UV_FAULT) && !vinUVFault) 248 { 249 vinUVFault = true; 250 251 util::NamesValues nv; 252 nv.add("STATUS_WORD", statusWord); 253 254 using metadata = xyz::openbmc_project::Power::Fault:: 255 PowerSupplyUnderVoltageFault; 256 257 report<PowerSupplyUnderVoltageFault>(metadata::RAW_STATUS( 258 nv.get().c_str())); 259 } 260 else 261 { 262 if (vinUVFault) 263 { 264 vinUVFault = false; 265 log<level::INFO>("VIN_UV_FAULT cleared", 266 entry("POWERSUPPLY=%s", 267 inventoryPath.c_str())); 268 } 269 } 270 271 if ((statusWord & status_word::INPUT_FAULT_WARN) && !inputFault) 272 { 273 inputFault = true; 274 275 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug); 276 277 util::NamesValues nv; 278 nv.add("STATUS_WORD", statusWord); 279 nv.add("STATUS_INPUT", statusInput); 280 281 using metadata = xyz::openbmc_project::Power::Fault:: 282 PowerSupplyInputFault; 283 284 report<PowerSupplyInputFault>( 285 metadata::RAW_STATUS(nv.get().c_str())); 286 } 287 else 288 { 289 if ((inputFault) && 290 !(statusWord & status_word::INPUT_FAULT_WARN)) 291 { 292 inputFault = false; 293 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug); 294 295 log<level::INFO>("INPUT_FAULT_WARN cleared", 296 entry("POWERSUPPLY=%s", inventoryPath.c_str()), 297 entry("STATUS_WORD=0x%04X", statusWord), 298 entry("STATUS_INPUT=0x%02X", statusInput)); 299 } 300 } 301 } 302 303 void PowerSupply::checkPGOrUnitOffFault(const uint16_t statusWord) 304 { 305 using namespace witherspoon::pmbus; 306 307 std::uint8_t statusInput = 0; 308 std::uint8_t statusVout = 0; 309 std::uint8_t statusIout = 0; 310 std::uint8_t statusMFR = 0; 311 312 // Check PG# and UNIT_IS_OFF 313 if (((statusWord & status_word::POWER_GOOD_NEGATED) || 314 (statusWord & status_word::UNIT_IS_OFF)) && 315 !powerOnFault) 316 { 317 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug); 318 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0); 319 statusVout = pmbusIntf.read(status0Vout, Type::Debug); 320 statusIout = pmbusIntf.read(STATUS_IOUT, Type::Debug); 321 statusMFR = pmbusIntf.read(STATUS_MFR, Type::Debug); 322 323 util::NamesValues nv; 324 nv.add("STATUS_WORD", statusWord); 325 nv.add("STATUS_INPUT", statusInput); 326 nv.add("STATUS_VOUT", statusVout); 327 nv.add("STATUS_IOUT", statusIout); 328 nv.add("MFR_SPECIFIC", statusMFR); 329 330 using metadata = xyz::openbmc_project::Power::Fault:: 331 PowerSupplyShouldBeOn; 332 333 // A power supply is OFF (or pgood low) but should be on. 334 report<PowerSupplyShouldBeOn>(metadata::RAW_STATUS(nv.get().c_str()), 335 metadata::CALLOUT_INVENTORY_PATH( 336 inventoryPath.c_str())); 337 338 powerOnFault = true; 339 } 340 341 } 342 343 void PowerSupply::checkCurrentOutOverCurrentFault(const uint16_t statusWord) 344 { 345 using namespace witherspoon::pmbus; 346 347 std::uint8_t statusInput = 0; 348 std::uint8_t statusVout = 0; 349 std::uint8_t statusIout = 0; 350 std::uint8_t statusMFR = 0; 351 352 // Check for an output overcurrent fault. 353 if ((statusWord & status_word::IOUT_OC_FAULT) && 354 !outputOCFault) 355 { 356 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug); 357 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0); 358 statusVout = pmbusIntf.read(status0Vout, Type::Debug); 359 statusIout = pmbusIntf.read(STATUS_IOUT, Type::Debug); 360 statusMFR = pmbusIntf.read(STATUS_MFR, Type::Debug); 361 362 util::NamesValues nv; 363 nv.add("STATUS_WORD", statusWord); 364 nv.add("STATUS_INPUT", statusInput); 365 nv.add("STATUS_VOUT", statusVout); 366 nv.add("STATUS_IOUT", statusIout); 367 nv.add("MFR_SPECIFIC", statusMFR); 368 369 using metadata = xyz::openbmc_project::Power::Fault:: 370 PowerSupplyOutputOvercurrent; 371 372 report<PowerSupplyOutputOvercurrent>(metadata::RAW_STATUS( 373 nv.get().c_str()), 374 metadata::CALLOUT_INVENTORY_PATH( 375 inventoryPath.c_str())); 376 377 outputOCFault = true; 378 } 379 } 380 381 void PowerSupply::clearFaults() 382 { 383 //TODO - Clear faults at pre-poweron. openbmc/openbmc#1736 384 return; 385 } 386 387 } 388 } 389 } 390