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 auto curUVFault = pmbusIntf.readBit(VIN_UV_FAULT, Type::Hwmon); 94 //TODO: 3 consecutive reads should be performed. 95 // If 3 consecutive reads are seen, log the fault. 96 // Driver gives cached value, read once a second. 97 // increment for fault on, decrement for fault off, to deglitch. 98 // If count reaches 3, we have fault. If count reaches 0, fault is 99 // cleared. 100 101 auto curInputFault = pmbusIntf.readBit(INPUT_FAULT_WARN, 102 Type::Hwmon); 103 104 if (curUVFault != vinUVFault) 105 { 106 vinUVFault = curUVFault; 107 108 if (curUVFault) 109 { 110 std::uint16_t statusWord = 0; 111 statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug); 112 113 util::NamesValues nv; 114 nv.add("STATUS_WORD", statusWord); 115 116 using metadata = xyz::openbmc_project::Power::Fault:: 117 PowerSupplyUnderVoltageFault; 118 119 report<PowerSupplyUnderVoltageFault>( 120 metadata::RAW_STATUS(nv.get().c_str())); 121 122 vinUVFault = true; 123 } 124 else 125 { 126 log<level::INFO>("VIN_UV_FAULT cleared", 127 entry("POWERSUPPLY=%s", 128 inventoryPath.c_str())); 129 } 130 131 } 132 133 if (curInputFault != inputFault) 134 { 135 if (curInputFault) 136 { 137 std::uint16_t statusWord = 0; 138 std::uint8_t statusInput = 0; 139 140 statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug); 141 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug); 142 143 util::NamesValues nv; 144 nv.add("STATUS_WORD", statusWord); 145 nv.add("STATUS_INPUT", statusInput); 146 147 using metadata = xyz::openbmc_project::Power::Fault:: 148 PowerSupplyInputFault; 149 150 report<PowerSupplyInputFault>( 151 metadata::RAW_STATUS(nv.get().c_str())); 152 153 inputFault = true; 154 } 155 else 156 { 157 inputFault = false; 158 } 159 160 } 161 } 162 } 163 catch (ReadFailure& e) 164 { 165 if (!readFailLogged) 166 { 167 commit<ReadFailure>(); 168 readFailLogged = true; 169 // TODO - Need to reset that to false at start of power on, or 170 // presence change. 171 } 172 } 173 174 return; 175 } 176 177 void PowerSupply::inventoryChanged(sdbusplus::message::message& msg) 178 { 179 std::string msgSensor; 180 std::map<std::string, sdbusplus::message::variant<uint32_t, bool>> msgData; 181 msg.read(msgSensor, msgData); 182 183 // Check if it was the Present property that changed. 184 auto valPropMap = msgData.find(PRESENT_PROP); 185 if (valPropMap != msgData.end()) 186 { 187 present = sdbusplus::message::variant_ns::get<bool>(valPropMap->second); 188 189 if (present) 190 { 191 readFailLogged = false; 192 vinUVFault = false; 193 inputFault = false; 194 } 195 } 196 197 return; 198 } 199 200 void PowerSupply::updatePresence() 201 { 202 // Use getProperty utility function to get presence status. 203 std::string path = INVENTORY_OBJ_PATH + inventoryPath; 204 std::string service = "xyz.openbmc_project.Inventory.Manager"; 205 206 try 207 { 208 util::getProperty(INVENTORY_INTERFACE, PRESENT_PROP, path, 209 service, bus, this->present); 210 } 211 catch (std::exception& e) 212 { 213 // If we happen to be trying to update presence just as it is being 214 // updated, we may encounter a runtime_error. Just catch that for 215 // now, let the inventoryChanged signal handler update presence later. 216 present = false; 217 } 218 219 } 220 221 void PowerSupply::powerStateChanged(sdbusplus::message::message& msg) 222 { 223 int32_t state = 0; 224 std::string msgSensor; 225 std::map<std::string, sdbusplus::message::variant<int32_t, int32_t>> 226 msgData; 227 msg.read(msgSensor, msgData); 228 229 // Check if it was the Present property that changed. 230 auto valPropMap = msgData.find("state"); 231 if (valPropMap != msgData.end()) 232 { 233 state = sdbusplus::message::variant_ns::get<int32_t>(valPropMap->second); 234 235 // Power is on when state=1. Set the fault logged variables to false 236 // and start the power on timer when the state changes to 1. 237 if (state) 238 { 239 readFailLogged = false; 240 vinUVFault = false; 241 inputFault = false; 242 powerOnTimer.start(powerOnInterval, Timer::TimerType::oneshot); 243 } 244 else 245 { 246 powerOnTimer.stop(); 247 powerOn = false; 248 } 249 } 250 251 } 252 253 void PowerSupply::updatePowerState() 254 { 255 // When state = 1, system is powered on 256 int32_t state = 0; 257 258 try 259 { 260 auto service = util::getService(POWER_OBJ_PATH, 261 POWER_INTERFACE, 262 bus); 263 264 // Use getProperty utility function to get power state. 265 util::getProperty<int32_t>(POWER_INTERFACE, 266 "state", 267 POWER_OBJ_PATH, 268 service, 269 bus, 270 state); 271 272 if (state) 273 { 274 powerOn = true; 275 } 276 else 277 { 278 powerOn = false; 279 } 280 } 281 catch (std::exception& e) 282 { 283 log<level::INFO>("Failed to get power state. Assuming it is off."); 284 powerOn = false; 285 } 286 287 } 288 289 void PowerSupply::clearFaults() 290 { 291 //TODO - Clear faults at pre-poweron. openbmc/openbmc#1736 292 return; 293 } 294 295 } 296 } 297 } 298