blob: b1013da581701bb076ab619906c11006110865f6 [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>
Brandon Wymane0eb45c2017-10-06 12:58:42 -050018#include <org/open_power/Witherspoon/Fault/error.hpp>
Matt Spinlerceacf942017-10-05 13:55:02 -050019#include <xyz/openbmc_project/Common/Device/error.hpp>
Brandon Wyman442035f2017-08-08 15:58:45 -050020#include "elog-errors.hpp"
Brandon Wyman10295542017-08-09 18:20:44 -050021#include "names_values.hpp"
Brandon Wyman24e422f2017-07-25 19:40:14 -050022#include "power_supply.hpp"
Brandon Wyman442035f2017-08-08 15:58:45 -050023#include "pmbus.hpp"
24#include "utility.hpp"
25
26using namespace phosphor::logging;
Brandon Wymane0eb45c2017-10-06 12:58:42 -050027using namespace sdbusplus::org::open_power::Witherspoon::Fault::Error;
Matt Spinlerceacf942017-10-05 13:55:02 -050028using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
Brandon Wyman24e422f2017-07-25 19:40:14 -050029
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050030namespace witherspoon
Brandon Wyman24e422f2017-07-25 19:40:14 -050031{
32namespace power
33{
34namespace psu
35{
36
Brandon Wyman10295542017-08-09 18:20:44 -050037constexpr auto INVENTORY_OBJ_PATH = "/xyz/openbmc_project/inventory";
38constexpr auto INVENTORY_INTERFACE = "xyz.openbmc_project.Inventory.Item";
39constexpr auto PRESENT_PROP = "Present";
Brandon Wyman431fbe42017-08-18 16:22:09 -050040constexpr auto POWER_OBJ_PATH = "/org/openbmc/control/power0";
41constexpr auto POWER_INTERFACE = "org.openbmc.control.Power";
Brandon Wyman10295542017-08-09 18:20:44 -050042
43PowerSupply::PowerSupply(const std::string& name, size_t inst,
Brandon Wyman431fbe42017-08-18 16:22:09 -050044 const std::string& objpath,
45 const std::string& invpath,
46 sdbusplus::bus::bus& bus,
47 event::Event& e,
48 std::chrono::seconds& t)
49 : Device(name, inst), monitorPath(objpath), pmbusIntf(objpath),
50 inventoryPath(invpath), bus(bus), event(e), powerOnInterval(t),
51 powerOnTimer(e, [this]()
52 {
53 this->powerOn = true;
54 })
Brandon Wyman10295542017-08-09 18:20:44 -050055{
Brandon Wyman10295542017-08-09 18:20:44 -050056 using namespace sdbusplus::bus;
57 auto present_obj_path = INVENTORY_OBJ_PATH + inventoryPath;
58 presentMatch = std::make_unique<match_t>(bus,
59 match::rules::propertiesChanged(
60 present_obj_path,
61 INVENTORY_INTERFACE),
62 [this](auto& msg)
Brandon Wyman431fbe42017-08-18 16:22:09 -050063 {
64 this->inventoryChanged(msg);
65 });
66 // Get initial presence state.
Brandon Wyman253dc9b2017-08-12 13:45:52 -050067 updatePresence();
Brandon Wyman431fbe42017-08-18 16:22:09 -050068
69 // Subscribe to power state changes
70 powerOnMatch = std::make_unique<match_t>(bus,
71 match::rules::propertiesChanged(
72 POWER_OBJ_PATH,
73 POWER_INTERFACE),
74 [this](auto& msg)
75 {
76 this->powerStateChanged(msg);
77 });
78 // Get initial power state.
79 updatePowerState();
Brandon Wyman10295542017-08-09 18:20:44 -050080}
Brandon Wyman442035f2017-08-08 15:58:45 -050081
Brandon Wymana1e96342017-09-25 16:47:44 -050082void PowerSupply::captureCmd(util::NamesValues& nv, const std::string& cmd,
83 witherspoon::pmbus::Type type)
84{
85 if (pmbusIntf.exists(cmd, type))
86 {
87 try
88 {
89 auto val = pmbusIntf.read(cmd, type);
90 nv.add(cmd, val);
91 }
92 catch (std::exception& e)
93 {
94 log<level::INFO>("Unable to capture metadata", entry("CMD=%s",
95 cmd));
96 }
97 }
98}
Brandon Wyman431fbe42017-08-18 16:22:09 -050099
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500100void PowerSupply::analyze()
101{
Brandon Wyman442035f2017-08-08 15:58:45 -0500102 using namespace witherspoon::pmbus;
103
104 try
105 {
Brandon Wyman10295542017-08-09 18:20:44 -0500106 if (present)
Brandon Wyman442035f2017-08-08 15:58:45 -0500107 {
Brandon Wyman764c7972017-08-22 17:05:36 -0500108 std::uint16_t statusWord = 0;
Brandon Wyman764c7972017-08-22 17:05:36 -0500109
110 // Read the 2 byte STATUS_WORD value to check for faults.
111 statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug);
112
Brandon Wyman10295542017-08-09 18:20:44 -0500113 //TODO: 3 consecutive reads should be performed.
114 // If 3 consecutive reads are seen, log the fault.
115 // Driver gives cached value, read once a second.
116 // increment for fault on, decrement for fault off, to deglitch.
117 // If count reaches 3, we have fault. If count reaches 0, fault is
118 // cleared.
119
Brandon Wyman603cc002017-08-28 18:17:58 -0500120 checkInputFault(statusWord);
Brandon Wyman764c7972017-08-22 17:05:36 -0500121
122 if (powerOn)
123 {
Brandon Wyman12661f12017-08-31 15:28:21 -0500124 checkFanFault(statusWord);
Brandon Wyman875b3632017-09-13 18:46:03 -0500125 checkTemperatureFault(statusWord);
Brandon Wymancfa032b2017-09-25 17:37:50 -0500126 checkOutputOvervoltageFault(statusWord);
127 checkCurrentOutOverCurrentFault(statusWord);
128 checkPGOrUnitOffFault(statusWord);
Brandon Wyman442035f2017-08-08 15:58:45 -0500129 }
130 }
131 }
132 catch (ReadFailure& e)
133 {
134 if (!readFailLogged)
135 {
136 commit<ReadFailure>();
137 readFailLogged = true;
Brandon Wyman442035f2017-08-08 15:58:45 -0500138 }
139 }
140
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500141 return;
142}
143
Brandon Wyman10295542017-08-09 18:20:44 -0500144void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
145{
146 std::string msgSensor;
147 std::map<std::string, sdbusplus::message::variant<uint32_t, bool>> msgData;
148 msg.read(msgSensor, msgData);
149
150 // Check if it was the Present property that changed.
151 auto valPropMap = msgData.find(PRESENT_PROP);
152 if (valPropMap != msgData.end())
153 {
154 present = sdbusplus::message::variant_ns::get<bool>(valPropMap->second);
155
156 if (present)
157 {
158 readFailLogged = false;
159 vinUVFault = false;
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500160 inputFault = false;
Brandon Wymanb165c252017-08-25 18:59:54 -0500161 outputOCFault = false;
Brandon Wymanab05c072017-08-30 18:26:41 -0500162 outputOVFault = false;
Brandon Wyman12661f12017-08-31 15:28:21 -0500163 fanFault = false;
Brandon Wyman875b3632017-09-13 18:46:03 -0500164 temperatureFault = false;
Brandon Wyman10295542017-08-09 18:20:44 -0500165 }
166 }
167
168 return;
169}
170
171void PowerSupply::updatePresence()
172{
173 // Use getProperty utility function to get presence status.
174 std::string path = INVENTORY_OBJ_PATH + inventoryPath;
175 std::string service = "xyz.openbmc_project.Inventory.Manager";
Brandon Wyman8731a302017-08-16 16:15:34 -0500176
177 try
178 {
179 util::getProperty(INVENTORY_INTERFACE, PRESENT_PROP, path,
180 service, bus, this->present);
181 }
182 catch (std::exception& e)
183 {
184 // If we happen to be trying to update presence just as it is being
185 // updated, we may encounter a runtime_error. Just catch that for
186 // now, let the inventoryChanged signal handler update presence later.
187 present = false;
188 }
189
Brandon Wyman10295542017-08-09 18:20:44 -0500190}
191
Brandon Wyman431fbe42017-08-18 16:22:09 -0500192void PowerSupply::powerStateChanged(sdbusplus::message::message& msg)
193{
194 int32_t state = 0;
195 std::string msgSensor;
196 std::map<std::string, sdbusplus::message::variant<int32_t, int32_t>>
197 msgData;
198 msg.read(msgSensor, msgData);
199
200 // Check if it was the Present property that changed.
201 auto valPropMap = msgData.find("state");
202 if (valPropMap != msgData.end())
203 {
204 state = sdbusplus::message::variant_ns::get<int32_t>(valPropMap->second);
205
206 // Power is on when state=1. Set the fault logged variables to false
207 // and start the power on timer when the state changes to 1.
208 if (state)
209 {
210 readFailLogged = false;
211 vinUVFault = false;
212 inputFault = false;
Brandon Wyman764c7972017-08-22 17:05:36 -0500213 powerOnFault = false;
Brandon Wymanb165c252017-08-25 18:59:54 -0500214 outputOCFault = false;
Brandon Wymanab05c072017-08-30 18:26:41 -0500215 outputOVFault = false;
Brandon Wyman12661f12017-08-31 15:28:21 -0500216 fanFault = false;
Brandon Wyman875b3632017-09-13 18:46:03 -0500217 temperatureFault = false;
Brandon Wyman431fbe42017-08-18 16:22:09 -0500218 powerOnTimer.start(powerOnInterval, Timer::TimerType::oneshot);
219 }
220 else
221 {
222 powerOnTimer.stop();
223 powerOn = false;
224 }
225 }
226
227}
228
229void PowerSupply::updatePowerState()
230{
231 // When state = 1, system is powered on
232 int32_t state = 0;
233
234 try
235 {
236 auto service = util::getService(POWER_OBJ_PATH,
237 POWER_INTERFACE,
238 bus);
239
240 // Use getProperty utility function to get power state.
241 util::getProperty<int32_t>(POWER_INTERFACE,
242 "state",
243 POWER_OBJ_PATH,
244 service,
245 bus,
246 state);
247
248 if (state)
249 {
250 powerOn = true;
251 }
252 else
253 {
254 powerOn = false;
255 }
256 }
257 catch (std::exception& e)
258 {
259 log<level::INFO>("Failed to get power state. Assuming it is off.");
260 powerOn = false;
261 }
262
263}
264
Brandon Wyman603cc002017-08-28 18:17:58 -0500265void PowerSupply::checkInputFault(const uint16_t statusWord)
266{
267 using namespace witherspoon::pmbus;
268
269 std::uint8_t statusInput = 0;
270
271 if ((statusWord & status_word::VIN_UV_FAULT) && !vinUVFault)
272 {
273 vinUVFault = true;
274
275 util::NamesValues nv;
276 nv.add("STATUS_WORD", statusWord);
277
Brandon Wymane0eb45c2017-10-06 12:58:42 -0500278 using metadata = org::open_power::Witherspoon::Fault::
Brandon Wyman603cc002017-08-28 18:17:58 -0500279 PowerSupplyUnderVoltageFault;
280
281 report<PowerSupplyUnderVoltageFault>(metadata::RAW_STATUS(
282 nv.get().c_str()));
283 }
284 else
285 {
286 if (vinUVFault)
287 {
288 vinUVFault = false;
289 log<level::INFO>("VIN_UV_FAULT cleared",
290 entry("POWERSUPPLY=%s",
291 inventoryPath.c_str()));
292 }
293 }
294
295 if ((statusWord & status_word::INPUT_FAULT_WARN) && !inputFault)
296 {
297 inputFault = true;
298
Brandon Wyman603cc002017-08-28 18:17:58 -0500299 util::NamesValues nv;
300 nv.add("STATUS_WORD", statusWord);
Brandon Wymana1e96342017-09-25 16:47:44 -0500301 captureCmd(nv, STATUS_INPUT, Type::Debug);
Brandon Wyman603cc002017-08-28 18:17:58 -0500302
Brandon Wymane0eb45c2017-10-06 12:58:42 -0500303 using metadata = org::open_power::Witherspoon::Fault::
Brandon Wyman603cc002017-08-28 18:17:58 -0500304 PowerSupplyInputFault;
305
306 report<PowerSupplyInputFault>(
307 metadata::RAW_STATUS(nv.get().c_str()));
308 }
309 else
310 {
311 if ((inputFault) &&
312 !(statusWord & status_word::INPUT_FAULT_WARN))
313 {
314 inputFault = false;
315 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug);
316
317 log<level::INFO>("INPUT_FAULT_WARN cleared",
318 entry("POWERSUPPLY=%s", inventoryPath.c_str()),
319 entry("STATUS_WORD=0x%04X", statusWord),
320 entry("STATUS_INPUT=0x%02X", statusInput));
321 }
322 }
323}
324
325void PowerSupply::checkPGOrUnitOffFault(const uint16_t statusWord)
326{
327 using namespace witherspoon::pmbus;
328
Brandon Wyman603cc002017-08-28 18:17:58 -0500329 // Check PG# and UNIT_IS_OFF
330 if (((statusWord & status_word::POWER_GOOD_NEGATED) ||
331 (statusWord & status_word::UNIT_IS_OFF)) &&
332 !powerOnFault)
333 {
Brandon Wyman603cc002017-08-28 18:17:58 -0500334 util::NamesValues nv;
335 nv.add("STATUS_WORD", statusWord);
Brandon Wymana1e96342017-09-25 16:47:44 -0500336 captureCmd(nv, STATUS_INPUT, Type::Debug);
337 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
338 captureCmd(nv, status0Vout, Type::Debug);
339 captureCmd(nv, STATUS_IOUT, Type::Debug);
340 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wyman603cc002017-08-28 18:17:58 -0500341
Brandon Wymane0eb45c2017-10-06 12:58:42 -0500342 using metadata = org::open_power::Witherspoon::Fault::
Brandon Wyman603cc002017-08-28 18:17:58 -0500343 PowerSupplyShouldBeOn;
344
345 // A power supply is OFF (or pgood low) but should be on.
346 report<PowerSupplyShouldBeOn>(metadata::RAW_STATUS(nv.get().c_str()),
347 metadata::CALLOUT_INVENTORY_PATH(
348 inventoryPath.c_str()));
349
350 powerOnFault = true;
351 }
352
353}
354
355void PowerSupply::checkCurrentOutOverCurrentFault(const uint16_t statusWord)
356{
357 using namespace witherspoon::pmbus;
358
Brandon Wyman603cc002017-08-28 18:17:58 -0500359 // Check for an output overcurrent fault.
360 if ((statusWord & status_word::IOUT_OC_FAULT) &&
361 !outputOCFault)
362 {
Brandon Wyman603cc002017-08-28 18:17:58 -0500363 util::NamesValues nv;
364 nv.add("STATUS_WORD", statusWord);
Brandon Wymana1e96342017-09-25 16:47:44 -0500365 captureCmd(nv, STATUS_INPUT, Type::Debug);
366 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
367 captureCmd(nv, status0Vout, Type::Debug);
368 captureCmd(nv, STATUS_IOUT, Type::Debug);
369 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wyman603cc002017-08-28 18:17:58 -0500370
Brandon Wymane0eb45c2017-10-06 12:58:42 -0500371 using metadata = org::open_power::Witherspoon::Fault::
Brandon Wyman603cc002017-08-28 18:17:58 -0500372 PowerSupplyOutputOvercurrent;
373
374 report<PowerSupplyOutputOvercurrent>(metadata::RAW_STATUS(
375 nv.get().c_str()),
376 metadata::CALLOUT_INVENTORY_PATH(
377 inventoryPath.c_str()));
378
379 outputOCFault = true;
380 }
381}
382
Brandon Wymanab05c072017-08-30 18:26:41 -0500383void PowerSupply::checkOutputOvervoltageFault(const uint16_t statusWord)
384{
385 using namespace witherspoon::pmbus;
386
Brandon Wymanab05c072017-08-30 18:26:41 -0500387 // Check for an output overvoltage fault.
388 if ((statusWord & status_word::VOUT_OV_FAULT) &&
389 !outputOVFault)
390 {
Brandon Wymanab05c072017-08-30 18:26:41 -0500391 util::NamesValues nv;
392 nv.add("STATUS_WORD", statusWord);
Brandon Wymana1e96342017-09-25 16:47:44 -0500393 captureCmd(nv, STATUS_INPUT, Type::Debug);
394 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
395 captureCmd(nv, status0Vout, Type::Debug);
396 captureCmd(nv, STATUS_IOUT, Type::Debug);
397 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wymanab05c072017-08-30 18:26:41 -0500398
Brandon Wymane0eb45c2017-10-06 12:58:42 -0500399 using metadata = org::open_power::Witherspoon::Fault::
Brandon Wymanab05c072017-08-30 18:26:41 -0500400 PowerSupplyOutputOvervoltage;
401
402 report<PowerSupplyOutputOvervoltage>(metadata::RAW_STATUS(
403 nv.get().c_str()),
404 metadata::CALLOUT_INVENTORY_PATH(
405 inventoryPath.c_str()));
406
407 outputOVFault = true;
408 }
409}
410
Brandon Wyman12661f12017-08-31 15:28:21 -0500411void PowerSupply::checkFanFault(const uint16_t statusWord)
412{
413 using namespace witherspoon::pmbus;
414
Brandon Wyman875b3632017-09-13 18:46:03 -0500415 // Check for a fan fault or warning condition
Brandon Wyman12661f12017-08-31 15:28:21 -0500416 if ((statusWord & status_word::FAN_FAULT) &&
417 !fanFault)
418 {
Brandon Wyman12661f12017-08-31 15:28:21 -0500419 util::NamesValues nv;
420 nv.add("STATUS_WORD", statusWord);
Brandon Wymana1e96342017-09-25 16:47:44 -0500421 captureCmd(nv, STATUS_MFR, Type::Debug);
422 captureCmd(nv, STATUS_TEMPERATURE, Type::Debug);
423 captureCmd(nv, STATUS_FANS_1_2, Type::Debug);
Brandon Wyman12661f12017-08-31 15:28:21 -0500424
Brandon Wymane0eb45c2017-10-06 12:58:42 -0500425 using metadata = org::open_power::Witherspoon::Fault::
Brandon Wyman12661f12017-08-31 15:28:21 -0500426 PowerSupplyFanFault;
427
428 report<PowerSupplyFanFault>(
429 metadata::RAW_STATUS(nv.get().c_str()),
430 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
431
432 fanFault = true;
433 }
434}
435
Brandon Wyman875b3632017-09-13 18:46:03 -0500436void PowerSupply::checkTemperatureFault(const uint16_t statusWord)
437{
438 using namespace witherspoon::pmbus;
439
440 // Due to how the PMBus core device driver sends a clear faults command
441 // the bit in STATUS_WORD will likely be cleared when we attempt to examine
442 // it for a Thermal Fault or Warning. So, check the STATUS_WORD and the
443 // STATUS_TEMPERATURE bits. If either indicates a fault, proceed with
444 // logging the over-temperature condition.
445 std::uint8_t statusTemperature = 0;
446 statusTemperature = pmbusIntf.read(STATUS_TEMPERATURE, Type::Debug);
447 if (((statusWord & status_word::TEMPERATURE_FAULT_WARN) ||
448 (statusTemperature & status_temperature::OT_FAULT)) &&
449 !temperatureFault)
450 {
451 // The power supply has had an over-temperature condition.
452 // This may not result in a shutdown if experienced for a short
453 // duration.
454 // This should not occur under normal conditions.
455 // The power supply may be faulty, or the paired supply may be putting
456 // out less current.
457 // Capture command responses with potentially relevant information,
458 // and call out the power supply reporting the condition.
Brandon Wyman875b3632017-09-13 18:46:03 -0500459 util::NamesValues nv;
460 nv.add("STATUS_WORD", statusWord);
Brandon Wymana1e96342017-09-25 16:47:44 -0500461 captureCmd(nv, STATUS_MFR, Type::Debug);
462 captureCmd(nv, STATUS_IOUT, Type::Debug);
Brandon Wyman875b3632017-09-13 18:46:03 -0500463 nv.add("STATUS_TEMPERATURE", statusTemperature);
Brandon Wymana1e96342017-09-25 16:47:44 -0500464 captureCmd(nv, STATUS_FANS_1_2, Type::Debug);
Brandon Wyman875b3632017-09-13 18:46:03 -0500465
Brandon Wymane0eb45c2017-10-06 12:58:42 -0500466 using metadata = org::open_power::Witherspoon::Fault::
Brandon Wyman875b3632017-09-13 18:46:03 -0500467 PowerSupplyTemperatureFault;
468
469 report<PowerSupplyTemperatureFault>(
470 metadata::RAW_STATUS(nv.get().c_str()),
471 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
472
473 temperatureFault = true;
474 }
475}
476
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500477void PowerSupply::clearFaults()
478{
Brandon Wyman10295542017-08-09 18:20:44 -0500479 //TODO - Clear faults at pre-poweron. openbmc/openbmc#1736
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500480 return;
481}
482
Brandon Wyman24e422f2017-07-25 19:40:14 -0500483}
484}
485}