blob: 38eddf61835d3b2aa9ce48fdc48330b3e46600e8 [file] [log] [blame]
Brandon Wyman24e422f2017-07-25 19:40:14 -05001/**
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 Wyman442035f2017-08-08 15:58:45 -050016#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 Wyman10295542017-08-09 18:20:44 -050022#include "names_values.hpp"
Brandon Wyman24e422f2017-07-25 19:40:14 -050023#include "power_supply.hpp"
Brandon Wyman442035f2017-08-08 15:58:45 -050024#include "pmbus.hpp"
25#include "utility.hpp"
26
27using namespace phosphor::logging;
28using namespace sdbusplus::xyz::openbmc_project::Control::Device::Error;
29using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
30using namespace sdbusplus::xyz::openbmc_project::Power::Fault::Error;
Brandon Wyman24e422f2017-07-25 19:40:14 -050031
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050032namespace witherspoon
Brandon Wyman24e422f2017-07-25 19:40:14 -050033{
34namespace power
35{
36namespace psu
37{
38
Brandon Wyman10295542017-08-09 18:20:44 -050039constexpr auto INVENTORY_OBJ_PATH = "/xyz/openbmc_project/inventory";
40constexpr auto INVENTORY_INTERFACE = "xyz.openbmc_project.Inventory.Item";
41constexpr auto PRESENT_PROP = "Present";
42
43PowerSupply::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 Wyman10295542017-08-09 18:20:44 -050049 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 Wyman253dc9b2017-08-12 13:45:52 -050059
60 updatePresence();
Brandon Wyman10295542017-08-09 18:20:44 -050061}
Brandon Wyman442035f2017-08-08 15:58:45 -050062
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050063void PowerSupply::analyze()
64{
Brandon Wyman442035f2017-08-08 15:58:45 -050065 using namespace witherspoon::pmbus;
66
67 try
68 {
Brandon Wyman10295542017-08-09 18:20:44 -050069 if (present)
Brandon Wyman442035f2017-08-08 15:58:45 -050070 {
Brandon Wyman10295542017-08-09 18:20:44 -050071 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 Wyman253dc9b2017-08-12 13:45:52 -050079 auto curInputFault = pmbusIntf.readBit(INPUT_FAULT_WARN,
80 Type::Hwmon);
Brandon Wyman10295542017-08-09 18:20:44 -050081
Brandon Wyman253dc9b2017-08-12 13:45:52 -050082 if (curUVFault != vinUVFault)
Brandon Wyman442035f2017-08-08 15:58:45 -050083 {
Brandon Wyman253dc9b2017-08-12 13:45:52 -050084 vinUVFault = curUVFault;
Brandon Wyman10295542017-08-09 18:20:44 -050085
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 Wyman10295542017-08-09 18:20:44 -0500107 }
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500108
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 Wyman442035f2017-08-08 15:58:45 -0500138 }
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 Wyman1db9a9e2017-07-26 18:50:22 -0500152 return;
153}
154
Brandon Wyman10295542017-08-09 18:20:44 -0500155void 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 Wyman253dc9b2017-08-12 13:45:52 -0500171 inputFault = false;
Brandon Wyman10295542017-08-09 18:20:44 -0500172 }
173 }
174
175 return;
176}
177
178void 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 Wyman8731a302017-08-16 16:15:34 -0500183
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 Wyman10295542017-08-09 18:20:44 -0500197}
198
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500199void PowerSupply::clearFaults()
200{
Brandon Wyman10295542017-08-09 18:20:44 -0500201 //TODO - Clear faults at pre-poweron. openbmc/openbmc#1736
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500202 return;
203}
204
Brandon Wyman24e422f2017-07-25 19:40:14 -0500205}
206}
207}