blob: b61d7e531172846e38e5e5ce122f7cb8e919280e [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
Brandon Wymanb49986b2017-10-27 18:00:36 -0500122 if (powerOn && !vinUVFault && !inputFault)
Brandon Wyman764c7972017-08-22 17:05:36 -0500123 {
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 {
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500158 clearFaults();
Brandon Wyman10295542017-08-09 18:20:44 -0500159 }
160 }
161
162 return;
163}
164
165void PowerSupply::updatePresence()
166{
167 // Use getProperty utility function to get presence status.
168 std::string path = INVENTORY_OBJ_PATH + inventoryPath;
169 std::string service = "xyz.openbmc_project.Inventory.Manager";
Brandon Wyman8731a302017-08-16 16:15:34 -0500170
Brandon Wyman800e01b2017-10-02 17:30:37 -0500171 util::getProperty(INVENTORY_INTERFACE, PRESENT_PROP, path,service, bus,
172 this->present);
Brandon Wyman10295542017-08-09 18:20:44 -0500173}
174
Brandon Wyman431fbe42017-08-18 16:22:09 -0500175void PowerSupply::powerStateChanged(sdbusplus::message::message& msg)
176{
177 int32_t state = 0;
178 std::string msgSensor;
179 std::map<std::string, sdbusplus::message::variant<int32_t, int32_t>>
180 msgData;
181 msg.read(msgSensor, msgData);
182
183 // Check if it was the Present property that changed.
184 auto valPropMap = msgData.find("state");
185 if (valPropMap != msgData.end())
186 {
187 state = sdbusplus::message::variant_ns::get<int32_t>(valPropMap->second);
188
189 // Power is on when state=1. Set the fault logged variables to false
190 // and start the power on timer when the state changes to 1.
191 if (state)
192 {
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500193 clearFaults();
Brandon Wyman431fbe42017-08-18 16:22:09 -0500194 powerOnTimer.start(powerOnInterval, Timer::TimerType::oneshot);
195 }
196 else
197 {
198 powerOnTimer.stop();
199 powerOn = false;
200 }
201 }
202
203}
204
205void PowerSupply::updatePowerState()
206{
207 // When state = 1, system is powered on
208 int32_t state = 0;
209
210 try
211 {
212 auto service = util::getService(POWER_OBJ_PATH,
213 POWER_INTERFACE,
214 bus);
215
216 // Use getProperty utility function to get power state.
217 util::getProperty<int32_t>(POWER_INTERFACE,
218 "state",
219 POWER_OBJ_PATH,
220 service,
221 bus,
222 state);
223
224 if (state)
225 {
226 powerOn = true;
227 }
228 else
229 {
230 powerOn = false;
231 }
232 }
233 catch (std::exception& e)
234 {
235 log<level::INFO>("Failed to get power state. Assuming it is off.");
236 powerOn = false;
237 }
238
239}
240
Brandon Wyman603cc002017-08-28 18:17:58 -0500241void PowerSupply::checkInputFault(const uint16_t statusWord)
242{
243 using namespace witherspoon::pmbus;
244
245 std::uint8_t statusInput = 0;
246
247 if ((statusWord & status_word::VIN_UV_FAULT) && !vinUVFault)
248 {
249 vinUVFault = true;
250
251 util::NamesValues nv;
252 nv.add("STATUS_WORD", statusWord);
253
Brandon Wymane0eb45c2017-10-06 12:58:42 -0500254 using metadata = org::open_power::Witherspoon::Fault::
Brandon Wyman603cc002017-08-28 18:17:58 -0500255 PowerSupplyUnderVoltageFault;
256
Brandon Wymanea358c92017-10-27 17:34:07 -0500257 report<PowerSupplyUnderVoltageFault>(
258 metadata::RAW_STATUS(nv.get().c_str()),
259 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wyman603cc002017-08-28 18:17:58 -0500260 }
261 else
262 {
263 if (vinUVFault)
264 {
265 vinUVFault = false;
266 log<level::INFO>("VIN_UV_FAULT cleared",
267 entry("POWERSUPPLY=%s",
268 inventoryPath.c_str()));
269 }
270 }
271
272 if ((statusWord & status_word::INPUT_FAULT_WARN) && !inputFault)
273 {
274 inputFault = true;
275
Brandon Wyman603cc002017-08-28 18:17:58 -0500276 util::NamesValues nv;
277 nv.add("STATUS_WORD", statusWord);
Brandon Wymana1e96342017-09-25 16:47:44 -0500278 captureCmd(nv, STATUS_INPUT, Type::Debug);
Brandon Wyman603cc002017-08-28 18:17:58 -0500279
Brandon Wymane0eb45c2017-10-06 12:58:42 -0500280 using metadata = org::open_power::Witherspoon::Fault::
Brandon Wyman603cc002017-08-28 18:17:58 -0500281 PowerSupplyInputFault;
282
283 report<PowerSupplyInputFault>(
Brandon Wymanea358c92017-10-27 17:34:07 -0500284 metadata::RAW_STATUS(nv.get().c_str()),
285 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wyman603cc002017-08-28 18:17:58 -0500286 }
287 else
288 {
289 if ((inputFault) &&
290 !(statusWord & status_word::INPUT_FAULT_WARN))
291 {
292 inputFault = false;
293 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug);
294
295 log<level::INFO>("INPUT_FAULT_WARN cleared",
296 entry("POWERSUPPLY=%s", inventoryPath.c_str()),
297 entry("STATUS_WORD=0x%04X", statusWord),
298 entry("STATUS_INPUT=0x%02X", statusInput));
299 }
300 }
301}
302
303void PowerSupply::checkPGOrUnitOffFault(const uint16_t statusWord)
304{
305 using namespace witherspoon::pmbus;
306
Brandon Wyman593d24f2017-10-13 18:15:23 -0500307 if (powerOnFault < FAULT_COUNT)
Brandon Wyman603cc002017-08-28 18:17:58 -0500308 {
Brandon Wyman593d24f2017-10-13 18:15:23 -0500309 // Check PG# and UNIT_IS_OFF
310 if ((statusWord & status_word::POWER_GOOD_NEGATED) ||
311 (statusWord & status_word::UNIT_IS_OFF))
312 {
313 log<level::INFO>("PGOOD or UNIT_IS_OFF bit bad",
314 entry("STATUS_WORD=0x%04X", statusWord));
315 powerOnFault++;
316 }
317 else
318 {
319 if (powerOnFault > 0)
320 {
321 log<level::INFO>("PGOOD and UNIT_IS_OFF bits good");
322 powerOnFault = 0;
323 }
324 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500325
Brandon Wyman593d24f2017-10-13 18:15:23 -0500326 if (powerOnFault >= FAULT_COUNT)
327 {
328 util::NamesValues nv;
329 nv.add("STATUS_WORD", statusWord);
330 captureCmd(nv, STATUS_INPUT, Type::Debug);
331 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
332 captureCmd(nv, status0Vout, Type::Debug);
333 captureCmd(nv, STATUS_IOUT, Type::Debug);
334 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wyman603cc002017-08-28 18:17:58 -0500335
Brandon Wyman593d24f2017-10-13 18:15:23 -0500336 using metadata = org::open_power::Witherspoon::Fault::
337 PowerSupplyShouldBeOn;
Brandon Wyman603cc002017-08-28 18:17:58 -0500338
Brandon Wyman593d24f2017-10-13 18:15:23 -0500339 // A power supply is OFF (or pgood low) but should be on.
340 report<PowerSupplyShouldBeOn>(
341 metadata::RAW_STATUS(nv.get().c_str()),
342 metadata::CALLOUT_INVENTORY_PATH(
343 inventoryPath.c_str()));
344 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500345 }
346
347}
348
349void PowerSupply::checkCurrentOutOverCurrentFault(const uint16_t statusWord)
350{
351 using namespace witherspoon::pmbus;
352
Brandon Wyman603cc002017-08-28 18:17:58 -0500353 // Check for an output overcurrent fault.
354 if ((statusWord & status_word::IOUT_OC_FAULT) &&
355 !outputOCFault)
356 {
Brandon Wyman603cc002017-08-28 18:17:58 -0500357 util::NamesValues nv;
358 nv.add("STATUS_WORD", statusWord);
Brandon Wymana1e96342017-09-25 16:47:44 -0500359 captureCmd(nv, STATUS_INPUT, Type::Debug);
360 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
361 captureCmd(nv, status0Vout, Type::Debug);
362 captureCmd(nv, STATUS_IOUT, Type::Debug);
363 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wyman603cc002017-08-28 18:17:58 -0500364
Brandon Wymane0eb45c2017-10-06 12:58:42 -0500365 using metadata = org::open_power::Witherspoon::Fault::
Brandon Wyman603cc002017-08-28 18:17:58 -0500366 PowerSupplyOutputOvercurrent;
367
368 report<PowerSupplyOutputOvercurrent>(metadata::RAW_STATUS(
369 nv.get().c_str()),
370 metadata::CALLOUT_INVENTORY_PATH(
371 inventoryPath.c_str()));
372
373 outputOCFault = true;
374 }
375}
376
Brandon Wymanab05c072017-08-30 18:26:41 -0500377void PowerSupply::checkOutputOvervoltageFault(const uint16_t statusWord)
378{
379 using namespace witherspoon::pmbus;
380
Brandon Wymanab05c072017-08-30 18:26:41 -0500381 // Check for an output overvoltage fault.
382 if ((statusWord & status_word::VOUT_OV_FAULT) &&
383 !outputOVFault)
384 {
Brandon Wymanab05c072017-08-30 18:26:41 -0500385 util::NamesValues nv;
386 nv.add("STATUS_WORD", statusWord);
Brandon Wymana1e96342017-09-25 16:47:44 -0500387 captureCmd(nv, STATUS_INPUT, Type::Debug);
388 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
389 captureCmd(nv, status0Vout, Type::Debug);
390 captureCmd(nv, STATUS_IOUT, Type::Debug);
391 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wymanab05c072017-08-30 18:26:41 -0500392
Brandon Wymane0eb45c2017-10-06 12:58:42 -0500393 using metadata = org::open_power::Witherspoon::Fault::
Brandon Wymanab05c072017-08-30 18:26:41 -0500394 PowerSupplyOutputOvervoltage;
395
396 report<PowerSupplyOutputOvervoltage>(metadata::RAW_STATUS(
397 nv.get().c_str()),
398 metadata::CALLOUT_INVENTORY_PATH(
399 inventoryPath.c_str()));
400
401 outputOVFault = true;
402 }
403}
404
Brandon Wyman12661f12017-08-31 15:28:21 -0500405void PowerSupply::checkFanFault(const uint16_t statusWord)
406{
407 using namespace witherspoon::pmbus;
408
Brandon Wyman875b3632017-09-13 18:46:03 -0500409 // Check for a fan fault or warning condition
Brandon Wyman12661f12017-08-31 15:28:21 -0500410 if ((statusWord & status_word::FAN_FAULT) &&
411 !fanFault)
412 {
Brandon Wyman12661f12017-08-31 15:28:21 -0500413 util::NamesValues nv;
414 nv.add("STATUS_WORD", statusWord);
Brandon Wymana1e96342017-09-25 16:47:44 -0500415 captureCmd(nv, STATUS_MFR, Type::Debug);
416 captureCmd(nv, STATUS_TEMPERATURE, Type::Debug);
417 captureCmd(nv, STATUS_FANS_1_2, Type::Debug);
Brandon Wyman12661f12017-08-31 15:28:21 -0500418
Brandon Wymane0eb45c2017-10-06 12:58:42 -0500419 using metadata = org::open_power::Witherspoon::Fault::
Brandon Wyman12661f12017-08-31 15:28:21 -0500420 PowerSupplyFanFault;
421
422 report<PowerSupplyFanFault>(
423 metadata::RAW_STATUS(nv.get().c_str()),
424 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
425
426 fanFault = true;
427 }
428}
429
Brandon Wyman875b3632017-09-13 18:46:03 -0500430void PowerSupply::checkTemperatureFault(const uint16_t statusWord)
431{
432 using namespace witherspoon::pmbus;
433
434 // Due to how the PMBus core device driver sends a clear faults command
435 // the bit in STATUS_WORD will likely be cleared when we attempt to examine
436 // it for a Thermal Fault or Warning. So, check the STATUS_WORD and the
437 // STATUS_TEMPERATURE bits. If either indicates a fault, proceed with
438 // logging the over-temperature condition.
439 std::uint8_t statusTemperature = 0;
440 statusTemperature = pmbusIntf.read(STATUS_TEMPERATURE, Type::Debug);
441 if (((statusWord & status_word::TEMPERATURE_FAULT_WARN) ||
442 (statusTemperature & status_temperature::OT_FAULT)) &&
443 !temperatureFault)
444 {
445 // The power supply has had an over-temperature condition.
446 // This may not result in a shutdown if experienced for a short
447 // duration.
448 // This should not occur under normal conditions.
449 // The power supply may be faulty, or the paired supply may be putting
450 // out less current.
451 // Capture command responses with potentially relevant information,
452 // and call out the power supply reporting the condition.
Brandon Wyman875b3632017-09-13 18:46:03 -0500453 util::NamesValues nv;
454 nv.add("STATUS_WORD", statusWord);
Brandon Wymana1e96342017-09-25 16:47:44 -0500455 captureCmd(nv, STATUS_MFR, Type::Debug);
456 captureCmd(nv, STATUS_IOUT, Type::Debug);
Brandon Wyman875b3632017-09-13 18:46:03 -0500457 nv.add("STATUS_TEMPERATURE", statusTemperature);
Brandon Wymana1e96342017-09-25 16:47:44 -0500458 captureCmd(nv, STATUS_FANS_1_2, Type::Debug);
Brandon Wyman875b3632017-09-13 18:46:03 -0500459
Brandon Wymane0eb45c2017-10-06 12:58:42 -0500460 using metadata = org::open_power::Witherspoon::Fault::
Brandon Wyman875b3632017-09-13 18:46:03 -0500461 PowerSupplyTemperatureFault;
462
463 report<PowerSupplyTemperatureFault>(
464 metadata::RAW_STATUS(nv.get().c_str()),
465 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
466
467 temperatureFault = true;
468 }
469}
470
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500471void PowerSupply::clearFaults()
472{
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500473 readFailLogged = false;
474 vinUVFault = false;
475 inputFault = false;
476 powerOnFault = 0;
477 outputOCFault = false;
478 outputOVFault = false;
479 fanFault = false;
480 temperatureFault = false;
481
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500482 return;
483}
484
Brandon Wyman24e422f2017-07-25 19:40:14 -0500485}
486}
487}