blob: 151075390fefd95af061ace2eb2d5875116f1e39 [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";
Brandon Wyman431fbe42017-08-18 16:22:09 -050042constexpr auto POWER_OBJ_PATH = "/org/openbmc/control/power0";
43constexpr auto POWER_INTERFACE = "org.openbmc.control.Power";
Brandon Wyman10295542017-08-09 18:20:44 -050044
45PowerSupply::PowerSupply(const std::string& name, size_t inst,
Brandon Wyman431fbe42017-08-18 16:22:09 -050046 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 })
Brandon Wyman10295542017-08-09 18:20:44 -050057{
Brandon Wyman10295542017-08-09 18:20:44 -050058 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)
Brandon Wyman431fbe42017-08-18 16:22:09 -050065 {
66 this->inventoryChanged(msg);
67 });
68 // Get initial presence state.
Brandon Wyman253dc9b2017-08-12 13:45:52 -050069 updatePresence();
Brandon Wyman431fbe42017-08-18 16:22:09 -050070
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();
Brandon Wyman10295542017-08-09 18:20:44 -050082}
Brandon Wyman442035f2017-08-08 15:58:45 -050083
Brandon Wyman431fbe42017-08-18 16:22:09 -050084
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050085void PowerSupply::analyze()
86{
Brandon Wyman442035f2017-08-08 15:58:45 -050087 using namespace witherspoon::pmbus;
88
89 try
90 {
Brandon Wyman10295542017-08-09 18:20:44 -050091 if (present)
Brandon Wyman442035f2017-08-08 15:58:45 -050092 {
Brandon Wyman10295542017-08-09 18:20:44 -050093 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
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500101 auto curInputFault = pmbusIntf.readBit(INPUT_FAULT_WARN,
102 Type::Hwmon);
Brandon Wyman10295542017-08-09 18:20:44 -0500103
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500104 if (curUVFault != vinUVFault)
Brandon Wyman442035f2017-08-08 15:58:45 -0500105 {
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500106 vinUVFault = curUVFault;
Brandon Wyman10295542017-08-09 18:20:44 -0500107
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()));
Brandon Wyman10295542017-08-09 18:20:44 -0500129 }
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500130
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
Brandon Wyman442035f2017-08-08 15:58:45 -0500160 }
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
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500174 return;
175}
176
Brandon Wyman10295542017-08-09 18:20:44 -0500177void 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;
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500193 inputFault = false;
Brandon Wyman10295542017-08-09 18:20:44 -0500194 }
195 }
196
197 return;
198}
199
200void 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";
Brandon Wyman8731a302017-08-16 16:15:34 -0500205
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
Brandon Wyman10295542017-08-09 18:20:44 -0500219}
220
Brandon Wyman431fbe42017-08-18 16:22:09 -0500221void 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
253void 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
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500289void PowerSupply::clearFaults()
290{
Brandon Wyman10295542017-08-09 18:20:44 -0500291 //TODO - Clear faults at pre-poweron. openbmc/openbmc#1736
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500292 return;
293}
294
Brandon Wyman24e422f2017-07-25 19:40:14 -0500295}
296}
297}