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 std::uint8_t statusInput = 0; 95 96 // Read the 2 byte STATUS_WORD value to check for faults. 97 statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug); 98 99 //TODO: 3 consecutive reads should be performed. 100 // If 3 consecutive reads are seen, log the fault. 101 // Driver gives cached value, read once a second. 102 // increment for fault on, decrement for fault off, to deglitch. 103 // If count reaches 3, we have fault. If count reaches 0, fault is 104 // cleared. 105 106 if ((statusWord & status_word::VIN_UV_FAULT) && !vinUVFault) 107 { 108 util::NamesValues nv; 109 nv.add("STATUS_WORD", statusWord); 110 111 using metadata = xyz::openbmc_project::Power::Fault:: 112 PowerSupplyUnderVoltageFault; 113 114 report<PowerSupplyUnderVoltageFault>( 115 metadata::RAW_STATUS(nv.get().c_str())); 116 117 vinUVFault = true; 118 } 119 else 120 { 121 vinUVFault = false; 122 log<level::INFO>("VIN_UV_FAULT cleared", 123 entry("POWERSUPPLY=%s", 124 inventoryPath.c_str())); 125 } 126 127 if ((statusWord & status_word::INPUT_FAULT_WARN) && !inputFault) 128 { 129 inputFault = true; 130 131 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug); 132 133 util::NamesValues nv; 134 nv.add("STATUS_WORD", statusWord); 135 nv.add("STATUS_INPUT", statusInput); 136 137 using metadata = xyz::openbmc_project::Power::Fault:: 138 PowerSupplyInputFault; 139 140 report<PowerSupplyInputFault>(metadata::RAW_STATUS( 141 nv.get().c_str())); 142 } 143 else 144 { 145 if ((inputFault) && 146 !(statusWord & status_word::INPUT_FAULT_WARN)) 147 { 148 inputFault = false; 149 150 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug); 151 152 log<level::INFO>("INPUT_FAULT_WARN cleared", 153 entry("POWERSUPPLY=%s", 154 inventoryPath.c_str()), 155 entry("STATUS_WORD=0x%04X", statusWord), 156 entry("STATUS_INPUT=0x%02X", statusInput)); 157 } 158 } 159 160 if (powerOn) 161 { 162 std::uint8_t statusVout = 0; 163 std::uint8_t statusIout = 0; 164 std::uint8_t statusMFR = 0; 165 166 // Check PG# and UNIT_IS_OFF 167 if (((statusWord & status_word::POWER_GOOD_NEGATED) || 168 (statusWord & status_word::UNIT_IS_OFF)) && 169 !powerOnFault) 170 { 171 std::uint8_t statusVout = 0; 172 std::uint8_t statusIout = 0; 173 std::uint8_t statusMFR = 0; 174 175 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug); 176 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0); 177 statusVout = pmbusIntf.read(status0Vout, Type::Debug); 178 statusIout = pmbusIntf.read(STATUS_IOUT, Type::Debug); 179 statusMFR = pmbusIntf.read(STATUS_MFR, Type::Debug); 180 181 util::NamesValues nv; 182 nv.add("STATUS_WORD", statusWord); 183 nv.add("STATUS_INPUT", statusInput); 184 nv.add("STATUS_VOUT", statusVout); 185 nv.add("STATUS_IOUT", statusIout); 186 nv.add("MFR_SPECIFIC", statusMFR); 187 188 using metadata = xyz::openbmc_project::Power::Fault:: 189 PowerSupplyShouldBeOn; 190 191 // A power supply is OFF (or pgood low) but should be on. 192 report<PowerSupplyShouldBeOn>( 193 metadata::RAW_STATUS(nv.get().c_str()), 194 metadata::CALLOUT_INVENTORY_PATH( 195 inventoryPath.c_str())); 196 197 powerOnFault = true; 198 } 199 200 // Check for an output overcurrent fault. 201 if ((statusWord & status_word::IOUT_OC_FAULT) && 202 !outputOCFault) 203 { 204 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug); 205 statusVout = pmbusIntf.read(STATUS_VOUT, Type::Debug); 206 statusIout = pmbusIntf.read(STATUS_IOUT, Type::Debug); 207 statusMFR = pmbusIntf.read(STATUS_MFR, Type::Debug); 208 209 util::NamesValues nv; 210 nv.add("STATUS_WORD", statusWord); 211 nv.add("STATUS_INPUT", statusInput); 212 nv.add("STATUS_VOUT", statusVout); 213 nv.add("STATUS_IOUT", statusIout); 214 nv.add("MFR_SPECIFIC", statusMFR); 215 216 using metadata = xyz::openbmc_project::Power::Fault:: 217 PowerSupplyOutputOvercurrent; 218 219 report<PowerSupplyOutputOvercurrent>( 220 metadata::RAW_STATUS(nv.get().c_str()), 221 metadata::CALLOUT_INVENTORY_PATH( 222 inventoryPath.c_str())); 223 224 outputOCFault = true; 225 } 226 227 } 228 } 229 } 230 catch (ReadFailure& e) 231 { 232 if (!readFailLogged) 233 { 234 commit<ReadFailure>(); 235 readFailLogged = true; 236 // TODO - Need to reset that to false at start of power on, or 237 // presence change. 238 } 239 } 240 241 return; 242 } 243 244 void PowerSupply::inventoryChanged(sdbusplus::message::message& msg) 245 { 246 std::string msgSensor; 247 std::map<std::string, sdbusplus::message::variant<uint32_t, bool>> msgData; 248 msg.read(msgSensor, msgData); 249 250 // Check if it was the Present property that changed. 251 auto valPropMap = msgData.find(PRESENT_PROP); 252 if (valPropMap != msgData.end()) 253 { 254 present = sdbusplus::message::variant_ns::get<bool>(valPropMap->second); 255 256 if (present) 257 { 258 readFailLogged = false; 259 vinUVFault = false; 260 inputFault = false; 261 outputOCFault = false; 262 } 263 } 264 265 return; 266 } 267 268 void PowerSupply::updatePresence() 269 { 270 // Use getProperty utility function to get presence status. 271 std::string path = INVENTORY_OBJ_PATH + inventoryPath; 272 std::string service = "xyz.openbmc_project.Inventory.Manager"; 273 274 try 275 { 276 util::getProperty(INVENTORY_INTERFACE, PRESENT_PROP, path, 277 service, bus, this->present); 278 } 279 catch (std::exception& e) 280 { 281 // If we happen to be trying to update presence just as it is being 282 // updated, we may encounter a runtime_error. Just catch that for 283 // now, let the inventoryChanged signal handler update presence later. 284 present = false; 285 } 286 287 } 288 289 void PowerSupply::powerStateChanged(sdbusplus::message::message& msg) 290 { 291 int32_t state = 0; 292 std::string msgSensor; 293 std::map<std::string, sdbusplus::message::variant<int32_t, int32_t>> 294 msgData; 295 msg.read(msgSensor, msgData); 296 297 // Check if it was the Present property that changed. 298 auto valPropMap = msgData.find("state"); 299 if (valPropMap != msgData.end()) 300 { 301 state = sdbusplus::message::variant_ns::get<int32_t>(valPropMap->second); 302 303 // Power is on when state=1. Set the fault logged variables to false 304 // and start the power on timer when the state changes to 1. 305 if (state) 306 { 307 readFailLogged = false; 308 vinUVFault = false; 309 inputFault = false; 310 powerOnFault = false; 311 outputOCFault = false; 312 powerOnTimer.start(powerOnInterval, Timer::TimerType::oneshot); 313 } 314 else 315 { 316 powerOnTimer.stop(); 317 powerOn = false; 318 } 319 } 320 321 } 322 323 void PowerSupply::updatePowerState() 324 { 325 // When state = 1, system is powered on 326 int32_t state = 0; 327 328 try 329 { 330 auto service = util::getService(POWER_OBJ_PATH, 331 POWER_INTERFACE, 332 bus); 333 334 // Use getProperty utility function to get power state. 335 util::getProperty<int32_t>(POWER_INTERFACE, 336 "state", 337 POWER_OBJ_PATH, 338 service, 339 bus, 340 state); 341 342 if (state) 343 { 344 powerOn = true; 345 } 346 else 347 { 348 powerOn = false; 349 } 350 } 351 catch (std::exception& e) 352 { 353 log<level::INFO>("Failed to get power state. Assuming it is off."); 354 powerOn = false; 355 } 356 357 } 358 359 void PowerSupply::clearFaults() 360 { 361 //TODO - Clear faults at pre-poweron. openbmc/openbmc#1736 362 return; 363 } 364 365 } 366 } 367 } 368