blob: cd7f87f7d923d96884f9eac011bb0a033e309d1b [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 Wyman764c7972017-08-22 17:05:36 -050093 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
Brandon Wyman10295542017-08-09 18:20:44 -050099 //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
Brandon Wyman764c7972017-08-22 17:05:36 -0500106 if ((statusWord & status_word::VIN_UV_FAULT) && !vinUVFault)
Brandon Wyman442035f2017-08-08 15:58:45 -0500107 {
Brandon Wyman764c7972017-08-22 17:05:36 -0500108 util::NamesValues nv;
109 nv.add("STATUS_WORD", statusWord);
Brandon Wyman10295542017-08-09 18:20:44 -0500110
Brandon Wyman764c7972017-08-22 17:05:36 -0500111 using metadata = xyz::openbmc_project::Power::Fault::
112 PowerSupplyUnderVoltageFault;
Brandon Wyman10295542017-08-09 18:20:44 -0500113
Brandon Wyman764c7972017-08-22 17:05:36 -0500114 report<PowerSupplyUnderVoltageFault>(
115 metadata::RAW_STATUS(nv.get().c_str()));
Brandon Wyman10295542017-08-09 18:20:44 -0500116
Brandon Wyman764c7972017-08-22 17:05:36 -0500117 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()));
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500125 }
126
Brandon Wyman764c7972017-08-22 17:05:36 -0500127 if ((statusWord & status_word::INPUT_FAULT_WARN) && !inputFault)
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500128 {
Brandon Wyman764c7972017-08-22 17:05:36 -0500129 inputFault = true;
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500130
Brandon Wyman764c7972017-08-22 17:05:36 -0500131 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
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500150 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug);
151
Brandon Wyman764c7972017-08-22 17:05:36 -0500152 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 {
Brandon Wymanb165c252017-08-25 18:59:54 -0500162 std::uint8_t statusVout = 0;
163 std::uint8_t statusIout = 0;
164 std::uint8_t statusMFR = 0;
165
Brandon Wyman764c7972017-08-22 17:05:36 -0500166 // 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
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500181 util::NamesValues nv;
182 nv.add("STATUS_WORD", statusWord);
183 nv.add("STATUS_INPUT", statusInput);
Brandon Wyman764c7972017-08-22 17:05:36 -0500184 nv.add("STATUS_VOUT", statusVout);
185 nv.add("STATUS_IOUT", statusIout);
186 nv.add("MFR_SPECIFIC", statusMFR);
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500187
188 using metadata = xyz::openbmc_project::Power::Fault::
Brandon Wyman764c7972017-08-22 17:05:36 -0500189 PowerSupplyShouldBeOn;
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500190
Brandon Wyman764c7972017-08-22 17:05:36 -0500191 // 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()));
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500196
Brandon Wyman764c7972017-08-22 17:05:36 -0500197 powerOnFault = true;
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500198 }
199
Brandon Wymanb165c252017-08-25 18:59:54 -0500200 // 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
Brandon Wyman442035f2017-08-08 15:58:45 -0500227 }
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
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500241 return;
242}
243
Brandon Wyman10295542017-08-09 18:20:44 -0500244void 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;
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500260 inputFault = false;
Brandon Wymanb165c252017-08-25 18:59:54 -0500261 outputOCFault = false;
Brandon Wyman10295542017-08-09 18:20:44 -0500262 }
263 }
264
265 return;
266}
267
268void 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";
Brandon Wyman8731a302017-08-16 16:15:34 -0500273
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
Brandon Wyman10295542017-08-09 18:20:44 -0500287}
288
Brandon Wyman431fbe42017-08-18 16:22:09 -0500289void 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;
Brandon Wyman764c7972017-08-22 17:05:36 -0500310 powerOnFault = false;
Brandon Wymanb165c252017-08-25 18:59:54 -0500311 outputOCFault = false;
Brandon Wyman431fbe42017-08-18 16:22:09 -0500312 powerOnTimer.start(powerOnInterval, Timer::TimerType::oneshot);
313 }
314 else
315 {
316 powerOnTimer.stop();
317 powerOn = false;
318 }
319 }
320
321}
322
323void 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
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500359void PowerSupply::clearFaults()
360{
Brandon Wyman10295542017-08-09 18:20:44 -0500361 //TODO - Clear faults at pre-poweron. openbmc/openbmc#1736
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500362 return;
363}
364
Brandon Wyman24e422f2017-07-25 19:40:14 -0500365}
366}
367}