Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 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 | */ |
Brandon Wyman | 442035f | 2017-08-08 15:58:45 -0500 | [diff] [blame] | 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" |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 22 | #include "names_values.hpp" |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 23 | #include "power_supply.hpp" |
Brandon Wyman | 442035f | 2017-08-08 15:58:45 -0500 | [diff] [blame] | 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; |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 31 | |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 32 | namespace witherspoon |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 33 | { |
| 34 | namespace power |
| 35 | { |
| 36 | namespace psu |
| 37 | { |
| 38 | |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 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 | |
| 43 | PowerSupply::PowerSupply(const std::string& name, size_t inst, |
| 44 | const std::string& objpath, const std::string& invpath, |
| 45 | sdbusplus::bus::bus& bus) |
| 46 | : Device(name, inst), monitorPath(objpath), inventoryPath(invpath), |
| 47 | bus(bus), pmbusIntf(objpath) |
| 48 | { |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 49 | using namespace sdbusplus::bus; |
| 50 | auto present_obj_path = INVENTORY_OBJ_PATH + inventoryPath; |
| 51 | presentMatch = std::make_unique<match_t>(bus, |
| 52 | match::rules::propertiesChanged( |
| 53 | present_obj_path, |
| 54 | INVENTORY_INTERFACE), |
| 55 | [this](auto& msg) |
| 56 | { |
| 57 | this->inventoryChanged(msg); |
| 58 | }); |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 59 | |
| 60 | updatePresence(); |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 61 | } |
Brandon Wyman | 442035f | 2017-08-08 15:58:45 -0500 | [diff] [blame] | 62 | |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 63 | void PowerSupply::analyze() |
| 64 | { |
Brandon Wyman | 442035f | 2017-08-08 15:58:45 -0500 | [diff] [blame] | 65 | using namespace witherspoon::pmbus; |
| 66 | |
| 67 | try |
| 68 | { |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 69 | if (present) |
Brandon Wyman | 442035f | 2017-08-08 15:58:45 -0500 | [diff] [blame] | 70 | { |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 71 | auto curUVFault = pmbusIntf.readBit(VIN_UV_FAULT, Type::Hwmon); |
| 72 | //TODO: 3 consecutive reads should be performed. |
| 73 | // If 3 consecutive reads are seen, log the fault. |
| 74 | // Driver gives cached value, read once a second. |
| 75 | // increment for fault on, decrement for fault off, to deglitch. |
| 76 | // If count reaches 3, we have fault. If count reaches 0, fault is |
| 77 | // cleared. |
| 78 | |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 79 | auto curInputFault = pmbusIntf.readBit(INPUT_FAULT_WARN, |
| 80 | Type::Hwmon); |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 81 | |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 82 | if (curUVFault != vinUVFault) |
Brandon Wyman | 442035f | 2017-08-08 15:58:45 -0500 | [diff] [blame] | 83 | { |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 84 | vinUVFault = curUVFault; |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 85 | |
| 86 | if (curUVFault) |
| 87 | { |
| 88 | std::uint16_t statusWord = 0; |
| 89 | statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug); |
| 90 | |
| 91 | util::NamesValues nv; |
| 92 | nv.add("STATUS_WORD", statusWord); |
| 93 | |
| 94 | using metadata = xyz::openbmc_project::Power::Fault:: |
| 95 | PowerSupplyUnderVoltageFault; |
| 96 | |
| 97 | report<PowerSupplyUnderVoltageFault>( |
| 98 | metadata::RAW_STATUS(nv.get().c_str())); |
| 99 | |
| 100 | vinUVFault = true; |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | log<level::INFO>("VIN_UV_FAULT cleared", |
| 105 | entry("POWERSUPPLY=%s", |
| 106 | inventoryPath.c_str())); |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 107 | } |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 108 | |
| 109 | } |
| 110 | |
| 111 | if (curInputFault != inputFault) |
| 112 | { |
| 113 | if (curInputFault) |
| 114 | { |
| 115 | std::uint16_t statusWord = 0; |
| 116 | std::uint8_t statusInput = 0; |
| 117 | |
| 118 | statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug); |
| 119 | statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug); |
| 120 | |
| 121 | util::NamesValues nv; |
| 122 | nv.add("STATUS_WORD", statusWord); |
| 123 | nv.add("STATUS_INPUT", statusInput); |
| 124 | |
| 125 | using metadata = xyz::openbmc_project::Power::Fault:: |
| 126 | PowerSupplyInputFault; |
| 127 | |
| 128 | report<PowerSupplyInputFault>( |
| 129 | metadata::RAW_STATUS(nv.get().c_str())); |
| 130 | |
| 131 | inputFault = true; |
| 132 | } |
| 133 | else |
| 134 | { |
| 135 | inputFault = false; |
| 136 | } |
| 137 | |
Brandon Wyman | 442035f | 2017-08-08 15:58:45 -0500 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } |
| 141 | catch (ReadFailure& e) |
| 142 | { |
| 143 | if (!readFailLogged) |
| 144 | { |
| 145 | commit<ReadFailure>(); |
| 146 | readFailLogged = true; |
| 147 | // TODO - Need to reset that to false at start of power on, or |
| 148 | // presence change. |
| 149 | } |
| 150 | } |
| 151 | |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 155 | void PowerSupply::inventoryChanged(sdbusplus::message::message& msg) |
| 156 | { |
| 157 | std::string msgSensor; |
| 158 | std::map<std::string, sdbusplus::message::variant<uint32_t, bool>> msgData; |
| 159 | msg.read(msgSensor, msgData); |
| 160 | |
| 161 | // Check if it was the Present property that changed. |
| 162 | auto valPropMap = msgData.find(PRESENT_PROP); |
| 163 | if (valPropMap != msgData.end()) |
| 164 | { |
| 165 | present = sdbusplus::message::variant_ns::get<bool>(valPropMap->second); |
| 166 | |
| 167 | if (present) |
| 168 | { |
| 169 | readFailLogged = false; |
| 170 | vinUVFault = false; |
Brandon Wyman | 253dc9b | 2017-08-12 13:45:52 -0500 | [diff] [blame] | 171 | inputFault = false; |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | void PowerSupply::updatePresence() |
| 179 | { |
| 180 | // Use getProperty utility function to get presence status. |
| 181 | std::string path = INVENTORY_OBJ_PATH + inventoryPath; |
| 182 | std::string service = "xyz.openbmc_project.Inventory.Manager"; |
Brandon Wyman | 8731a30 | 2017-08-16 16:15:34 -0500 | [diff] [blame^] | 183 | |
| 184 | try |
| 185 | { |
| 186 | util::getProperty(INVENTORY_INTERFACE, PRESENT_PROP, path, |
| 187 | service, bus, this->present); |
| 188 | } |
| 189 | catch (std::exception& e) |
| 190 | { |
| 191 | // If we happen to be trying to update presence just as it is being |
| 192 | // updated, we may encounter a runtime_error. Just catch that for |
| 193 | // now, let the inventoryChanged signal handler update presence later. |
| 194 | present = false; |
| 195 | } |
| 196 | |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 197 | } |
| 198 | |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 199 | void PowerSupply::clearFaults() |
| 200 | { |
Brandon Wyman | 1029554 | 2017-08-09 18:20:44 -0500 | [diff] [blame] | 201 | //TODO - Clear faults at pre-poweron. openbmc/openbmc#1736 |
Brandon Wyman | 1db9a9e | 2017-07-26 18:50:22 -0500 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | |
Brandon Wyman | 24e422f | 2017-07-25 19:40:14 -0500 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | } |