blob: 670065bdbba5e770050b55cf50276492bcbf2698 [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 Wymana1e96342017-09-25 16:47:44 -050084void PowerSupply::captureCmd(util::NamesValues& nv, const std::string& cmd,
85 witherspoon::pmbus::Type type)
86{
87 if (pmbusIntf.exists(cmd, type))
88 {
89 try
90 {
91 auto val = pmbusIntf.read(cmd, type);
92 nv.add(cmd, val);
93 }
94 catch (std::exception& e)
95 {
96 log<level::INFO>("Unable to capture metadata", entry("CMD=%s",
97 cmd));
98 }
99 }
100}
Brandon Wyman431fbe42017-08-18 16:22:09 -0500101
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500102void PowerSupply::analyze()
103{
Brandon Wyman442035f2017-08-08 15:58:45 -0500104 using namespace witherspoon::pmbus;
105
106 try
107 {
Brandon Wyman10295542017-08-09 18:20:44 -0500108 if (present)
Brandon Wyman442035f2017-08-08 15:58:45 -0500109 {
Brandon Wyman764c7972017-08-22 17:05:36 -0500110 std::uint16_t statusWord = 0;
Brandon Wyman764c7972017-08-22 17:05:36 -0500111
112 // Read the 2 byte STATUS_WORD value to check for faults.
113 statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug);
114
Brandon Wyman10295542017-08-09 18:20:44 -0500115 //TODO: 3 consecutive reads should be performed.
116 // If 3 consecutive reads are seen, log the fault.
117 // Driver gives cached value, read once a second.
118 // increment for fault on, decrement for fault off, to deglitch.
119 // If count reaches 3, we have fault. If count reaches 0, fault is
120 // cleared.
121
Brandon Wyman603cc002017-08-28 18:17:58 -0500122 checkInputFault(statusWord);
Brandon Wyman764c7972017-08-22 17:05:36 -0500123
124 if (powerOn)
125 {
Brandon Wyman603cc002017-08-28 18:17:58 -0500126 checkPGOrUnitOffFault(statusWord);
127 checkCurrentOutOverCurrentFault(statusWord);
Brandon Wymanab05c072017-08-30 18:26:41 -0500128 checkOutputOvervoltageFault(statusWord);
Brandon Wyman12661f12017-08-31 15:28:21 -0500129 checkFanFault(statusWord);
Brandon Wyman875b3632017-09-13 18:46:03 -0500130 checkTemperatureFault(statusWord);
Brandon Wyman442035f2017-08-08 15:58:45 -0500131 }
132 }
133 }
134 catch (ReadFailure& e)
135 {
136 if (!readFailLogged)
137 {
138 commit<ReadFailure>();
139 readFailLogged = true;
Brandon Wyman442035f2017-08-08 15:58:45 -0500140 }
141 }
142
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500143 return;
144}
145
Brandon Wyman10295542017-08-09 18:20:44 -0500146void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
147{
148 std::string msgSensor;
149 std::map<std::string, sdbusplus::message::variant<uint32_t, bool>> msgData;
150 msg.read(msgSensor, msgData);
151
152 // Check if it was the Present property that changed.
153 auto valPropMap = msgData.find(PRESENT_PROP);
154 if (valPropMap != msgData.end())
155 {
156 present = sdbusplus::message::variant_ns::get<bool>(valPropMap->second);
157
158 if (present)
159 {
160 readFailLogged = false;
161 vinUVFault = false;
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500162 inputFault = false;
Brandon Wymanb165c252017-08-25 18:59:54 -0500163 outputOCFault = false;
Brandon Wymanab05c072017-08-30 18:26:41 -0500164 outputOVFault = false;
Brandon Wyman12661f12017-08-31 15:28:21 -0500165 fanFault = false;
Brandon Wyman875b3632017-09-13 18:46:03 -0500166 temperatureFault = false;
Brandon Wyman10295542017-08-09 18:20:44 -0500167 }
168 }
169
170 return;
171}
172
173void PowerSupply::updatePresence()
174{
175 // Use getProperty utility function to get presence status.
176 std::string path = INVENTORY_OBJ_PATH + inventoryPath;
177 std::string service = "xyz.openbmc_project.Inventory.Manager";
Brandon Wyman8731a302017-08-16 16:15:34 -0500178
179 try
180 {
181 util::getProperty(INVENTORY_INTERFACE, PRESENT_PROP, path,
182 service, bus, this->present);
183 }
184 catch (std::exception& e)
185 {
186 // If we happen to be trying to update presence just as it is being
187 // updated, we may encounter a runtime_error. Just catch that for
188 // now, let the inventoryChanged signal handler update presence later.
189 present = false;
190 }
191
Brandon Wyman10295542017-08-09 18:20:44 -0500192}
193
Brandon Wyman431fbe42017-08-18 16:22:09 -0500194void PowerSupply::powerStateChanged(sdbusplus::message::message& msg)
195{
196 int32_t state = 0;
197 std::string msgSensor;
198 std::map<std::string, sdbusplus::message::variant<int32_t, int32_t>>
199 msgData;
200 msg.read(msgSensor, msgData);
201
202 // Check if it was the Present property that changed.
203 auto valPropMap = msgData.find("state");
204 if (valPropMap != msgData.end())
205 {
206 state = sdbusplus::message::variant_ns::get<int32_t>(valPropMap->second);
207
208 // Power is on when state=1. Set the fault logged variables to false
209 // and start the power on timer when the state changes to 1.
210 if (state)
211 {
212 readFailLogged = false;
213 vinUVFault = false;
214 inputFault = false;
Brandon Wyman764c7972017-08-22 17:05:36 -0500215 powerOnFault = false;
Brandon Wymanb165c252017-08-25 18:59:54 -0500216 outputOCFault = false;
Brandon Wymanab05c072017-08-30 18:26:41 -0500217 outputOVFault = false;
Brandon Wyman12661f12017-08-31 15:28:21 -0500218 fanFault = false;
Brandon Wyman875b3632017-09-13 18:46:03 -0500219 temperatureFault = false;
Brandon Wyman431fbe42017-08-18 16:22:09 -0500220 powerOnTimer.start(powerOnInterval, Timer::TimerType::oneshot);
221 }
222 else
223 {
224 powerOnTimer.stop();
225 powerOn = false;
226 }
227 }
228
229}
230
231void PowerSupply::updatePowerState()
232{
233 // When state = 1, system is powered on
234 int32_t state = 0;
235
236 try
237 {
238 auto service = util::getService(POWER_OBJ_PATH,
239 POWER_INTERFACE,
240 bus);
241
242 // Use getProperty utility function to get power state.
243 util::getProperty<int32_t>(POWER_INTERFACE,
244 "state",
245 POWER_OBJ_PATH,
246 service,
247 bus,
248 state);
249
250 if (state)
251 {
252 powerOn = true;
253 }
254 else
255 {
256 powerOn = false;
257 }
258 }
259 catch (std::exception& e)
260 {
261 log<level::INFO>("Failed to get power state. Assuming it is off.");
262 powerOn = false;
263 }
264
265}
266
Brandon Wyman603cc002017-08-28 18:17:58 -0500267void PowerSupply::checkInputFault(const uint16_t statusWord)
268{
269 using namespace witherspoon::pmbus;
270
271 std::uint8_t statusInput = 0;
272
273 if ((statusWord & status_word::VIN_UV_FAULT) && !vinUVFault)
274 {
275 vinUVFault = true;
276
277 util::NamesValues nv;
278 nv.add("STATUS_WORD", statusWord);
279
280 using metadata = xyz::openbmc_project::Power::Fault::
281 PowerSupplyUnderVoltageFault;
282
283 report<PowerSupplyUnderVoltageFault>(metadata::RAW_STATUS(
284 nv.get().c_str()));
285 }
286 else
287 {
288 if (vinUVFault)
289 {
290 vinUVFault = false;
291 log<level::INFO>("VIN_UV_FAULT cleared",
292 entry("POWERSUPPLY=%s",
293 inventoryPath.c_str()));
294 }
295 }
296
297 if ((statusWord & status_word::INPUT_FAULT_WARN) && !inputFault)
298 {
299 inputFault = true;
300
Brandon Wyman603cc002017-08-28 18:17:58 -0500301 util::NamesValues nv;
302 nv.add("STATUS_WORD", statusWord);
Brandon Wymana1e96342017-09-25 16:47:44 -0500303 captureCmd(nv, STATUS_INPUT, Type::Debug);
Brandon Wyman603cc002017-08-28 18:17:58 -0500304
305 using metadata = xyz::openbmc_project::Power::Fault::
306 PowerSupplyInputFault;
307
308 report<PowerSupplyInputFault>(
309 metadata::RAW_STATUS(nv.get().c_str()));
310 }
311 else
312 {
313 if ((inputFault) &&
314 !(statusWord & status_word::INPUT_FAULT_WARN))
315 {
316 inputFault = false;
317 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug);
318
319 log<level::INFO>("INPUT_FAULT_WARN cleared",
320 entry("POWERSUPPLY=%s", inventoryPath.c_str()),
321 entry("STATUS_WORD=0x%04X", statusWord),
322 entry("STATUS_INPUT=0x%02X", statusInput));
323 }
324 }
325}
326
327void PowerSupply::checkPGOrUnitOffFault(const uint16_t statusWord)
328{
329 using namespace witherspoon::pmbus;
330
Brandon Wyman603cc002017-08-28 18:17:58 -0500331 // Check PG# and UNIT_IS_OFF
332 if (((statusWord & status_word::POWER_GOOD_NEGATED) ||
333 (statusWord & status_word::UNIT_IS_OFF)) &&
334 !powerOnFault)
335 {
Brandon Wyman603cc002017-08-28 18:17:58 -0500336 util::NamesValues nv;
337 nv.add("STATUS_WORD", statusWord);
Brandon Wymana1e96342017-09-25 16:47:44 -0500338 captureCmd(nv, STATUS_INPUT, Type::Debug);
339 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
340 captureCmd(nv, status0Vout, Type::Debug);
341 captureCmd(nv, STATUS_IOUT, Type::Debug);
342 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wyman603cc002017-08-28 18:17:58 -0500343
344 using metadata = xyz::openbmc_project::Power::Fault::
345 PowerSupplyShouldBeOn;
346
347 // A power supply is OFF (or pgood low) but should be on.
348 report<PowerSupplyShouldBeOn>(metadata::RAW_STATUS(nv.get().c_str()),
349 metadata::CALLOUT_INVENTORY_PATH(
350 inventoryPath.c_str()));
351
352 powerOnFault = true;
353 }
354
355}
356
357void PowerSupply::checkCurrentOutOverCurrentFault(const uint16_t statusWord)
358{
359 using namespace witherspoon::pmbus;
360
Brandon Wyman603cc002017-08-28 18:17:58 -0500361 // Check for an output overcurrent fault.
362 if ((statusWord & status_word::IOUT_OC_FAULT) &&
363 !outputOCFault)
364 {
Brandon Wyman603cc002017-08-28 18:17:58 -0500365 util::NamesValues nv;
366 nv.add("STATUS_WORD", statusWord);
Brandon Wymana1e96342017-09-25 16:47:44 -0500367 captureCmd(nv, STATUS_INPUT, Type::Debug);
368 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
369 captureCmd(nv, status0Vout, Type::Debug);
370 captureCmd(nv, STATUS_IOUT, Type::Debug);
371 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wyman603cc002017-08-28 18:17:58 -0500372
373 using metadata = xyz::openbmc_project::Power::Fault::
374 PowerSupplyOutputOvercurrent;
375
376 report<PowerSupplyOutputOvercurrent>(metadata::RAW_STATUS(
377 nv.get().c_str()),
378 metadata::CALLOUT_INVENTORY_PATH(
379 inventoryPath.c_str()));
380
381 outputOCFault = true;
382 }
383}
384
Brandon Wymanab05c072017-08-30 18:26:41 -0500385void PowerSupply::checkOutputOvervoltageFault(const uint16_t statusWord)
386{
387 using namespace witherspoon::pmbus;
388
Brandon Wymanab05c072017-08-30 18:26:41 -0500389 // Check for an output overvoltage fault.
390 if ((statusWord & status_word::VOUT_OV_FAULT) &&
391 !outputOVFault)
392 {
Brandon Wymanab05c072017-08-30 18:26:41 -0500393 util::NamesValues nv;
394 nv.add("STATUS_WORD", statusWord);
Brandon Wymana1e96342017-09-25 16:47:44 -0500395 captureCmd(nv, STATUS_INPUT, Type::Debug);
396 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
397 captureCmd(nv, status0Vout, Type::Debug);
398 captureCmd(nv, STATUS_IOUT, Type::Debug);
399 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wymanab05c072017-08-30 18:26:41 -0500400
401 using metadata = xyz::openbmc_project::Power::Fault::
402 PowerSupplyOutputOvervoltage;
403
404 report<PowerSupplyOutputOvervoltage>(metadata::RAW_STATUS(
405 nv.get().c_str()),
406 metadata::CALLOUT_INVENTORY_PATH(
407 inventoryPath.c_str()));
408
409 outputOVFault = true;
410 }
411}
412
Brandon Wyman12661f12017-08-31 15:28:21 -0500413void PowerSupply::checkFanFault(const uint16_t statusWord)
414{
415 using namespace witherspoon::pmbus;
416
Brandon Wyman875b3632017-09-13 18:46:03 -0500417 // Check for a fan fault or warning condition
Brandon Wyman12661f12017-08-31 15:28:21 -0500418 if ((statusWord & status_word::FAN_FAULT) &&
419 !fanFault)
420 {
Brandon Wyman12661f12017-08-31 15:28:21 -0500421 util::NamesValues nv;
422 nv.add("STATUS_WORD", statusWord);
Brandon Wymana1e96342017-09-25 16:47:44 -0500423 captureCmd(nv, STATUS_MFR, Type::Debug);
424 captureCmd(nv, STATUS_TEMPERATURE, Type::Debug);
425 captureCmd(nv, STATUS_FANS_1_2, Type::Debug);
Brandon Wyman12661f12017-08-31 15:28:21 -0500426
427 using metadata = xyz::openbmc_project::Power::Fault::
428 PowerSupplyFanFault;
429
430 report<PowerSupplyFanFault>(
431 metadata::RAW_STATUS(nv.get().c_str()),
432 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
433
434 fanFault = true;
435 }
436}
437
Brandon Wyman875b3632017-09-13 18:46:03 -0500438void PowerSupply::checkTemperatureFault(const uint16_t statusWord)
439{
440 using namespace witherspoon::pmbus;
441
442 // Due to how the PMBus core device driver sends a clear faults command
443 // the bit in STATUS_WORD will likely be cleared when we attempt to examine
444 // it for a Thermal Fault or Warning. So, check the STATUS_WORD and the
445 // STATUS_TEMPERATURE bits. If either indicates a fault, proceed with
446 // logging the over-temperature condition.
447 std::uint8_t statusTemperature = 0;
448 statusTemperature = pmbusIntf.read(STATUS_TEMPERATURE, Type::Debug);
449 if (((statusWord & status_word::TEMPERATURE_FAULT_WARN) ||
450 (statusTemperature & status_temperature::OT_FAULT)) &&
451 !temperatureFault)
452 {
453 // The power supply has had an over-temperature condition.
454 // This may not result in a shutdown if experienced for a short
455 // duration.
456 // This should not occur under normal conditions.
457 // The power supply may be faulty, or the paired supply may be putting
458 // out less current.
459 // Capture command responses with potentially relevant information,
460 // and call out the power supply reporting the condition.
Brandon Wyman875b3632017-09-13 18:46:03 -0500461 util::NamesValues nv;
462 nv.add("STATUS_WORD", statusWord);
Brandon Wymana1e96342017-09-25 16:47:44 -0500463 captureCmd(nv, STATUS_MFR, Type::Debug);
464 captureCmd(nv, STATUS_IOUT, Type::Debug);
Brandon Wyman875b3632017-09-13 18:46:03 -0500465 nv.add("STATUS_TEMPERATURE", statusTemperature);
Brandon Wymana1e96342017-09-25 16:47:44 -0500466 captureCmd(nv, STATUS_FANS_1_2, Type::Debug);
Brandon Wyman875b3632017-09-13 18:46:03 -0500467
468 using metadata = xyz::openbmc_project::Power::Fault::
469 PowerSupplyTemperatureFault;
470
471 report<PowerSupplyTemperatureFault>(
472 metadata::RAW_STATUS(nv.get().c_str()),
473 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
474
475 temperatureFault = true;
476 }
477}
478
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500479void PowerSupply::clearFaults()
480{
Brandon Wyman10295542017-08-09 18:20:44 -0500481 //TODO - Clear faults at pre-poweron. openbmc/openbmc#1736
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500482 return;
483}
484
Brandon Wyman24e422f2017-07-25 19:40:14 -0500485}
486}
487}