blob: d9e870c1c8bfc0a7e40cf9a5201b8ac2410abf1f [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;
Brandon Wyman764c7972017-08-22 17:05:36 -050094
95 // Read the 2 byte STATUS_WORD value to check for faults.
96 statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug);
97
Brandon Wyman10295542017-08-09 18:20:44 -050098 //TODO: 3 consecutive reads should be performed.
99 // If 3 consecutive reads are seen, log the fault.
100 // Driver gives cached value, read once a second.
101 // increment for fault on, decrement for fault off, to deglitch.
102 // If count reaches 3, we have fault. If count reaches 0, fault is
103 // cleared.
104
Brandon Wyman603cc002017-08-28 18:17:58 -0500105 checkInputFault(statusWord);
Brandon Wyman764c7972017-08-22 17:05:36 -0500106
107 if (powerOn)
108 {
Brandon Wyman603cc002017-08-28 18:17:58 -0500109 checkPGOrUnitOffFault(statusWord);
110 checkCurrentOutOverCurrentFault(statusWord);
Brandon Wymanab05c072017-08-30 18:26:41 -0500111 checkOutputOvervoltageFault(statusWord);
Brandon Wyman12661f12017-08-31 15:28:21 -0500112 checkFanFault(statusWord);
Brandon Wyman442035f2017-08-08 15:58:45 -0500113 }
114 }
115 }
116 catch (ReadFailure& e)
117 {
118 if (!readFailLogged)
119 {
120 commit<ReadFailure>();
121 readFailLogged = true;
Brandon Wyman442035f2017-08-08 15:58:45 -0500122 }
123 }
124
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500125 return;
126}
127
Brandon Wyman10295542017-08-09 18:20:44 -0500128void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
129{
130 std::string msgSensor;
131 std::map<std::string, sdbusplus::message::variant<uint32_t, bool>> msgData;
132 msg.read(msgSensor, msgData);
133
134 // Check if it was the Present property that changed.
135 auto valPropMap = msgData.find(PRESENT_PROP);
136 if (valPropMap != msgData.end())
137 {
138 present = sdbusplus::message::variant_ns::get<bool>(valPropMap->second);
139
140 if (present)
141 {
142 readFailLogged = false;
143 vinUVFault = false;
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500144 inputFault = false;
Brandon Wymanb165c252017-08-25 18:59:54 -0500145 outputOCFault = false;
Brandon Wymanab05c072017-08-30 18:26:41 -0500146 outputOVFault = false;
Brandon Wyman12661f12017-08-31 15:28:21 -0500147 fanFault = false;
Brandon Wyman10295542017-08-09 18:20:44 -0500148 }
149 }
150
151 return;
152}
153
154void PowerSupply::updatePresence()
155{
156 // Use getProperty utility function to get presence status.
157 std::string path = INVENTORY_OBJ_PATH + inventoryPath;
158 std::string service = "xyz.openbmc_project.Inventory.Manager";
Brandon Wyman8731a302017-08-16 16:15:34 -0500159
160 try
161 {
162 util::getProperty(INVENTORY_INTERFACE, PRESENT_PROP, path,
163 service, bus, this->present);
164 }
165 catch (std::exception& e)
166 {
167 // If we happen to be trying to update presence just as it is being
168 // updated, we may encounter a runtime_error. Just catch that for
169 // now, let the inventoryChanged signal handler update presence later.
170 present = false;
171 }
172
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 {
193 readFailLogged = false;
194 vinUVFault = false;
195 inputFault = false;
Brandon Wyman764c7972017-08-22 17:05:36 -0500196 powerOnFault = false;
Brandon Wymanb165c252017-08-25 18:59:54 -0500197 outputOCFault = false;
Brandon Wymanab05c072017-08-30 18:26:41 -0500198 outputOVFault = false;
Brandon Wyman12661f12017-08-31 15:28:21 -0500199 fanFault = false;
Brandon Wyman431fbe42017-08-18 16:22:09 -0500200 powerOnTimer.start(powerOnInterval, Timer::TimerType::oneshot);
201 }
202 else
203 {
204 powerOnTimer.stop();
205 powerOn = false;
206 }
207 }
208
209}
210
211void PowerSupply::updatePowerState()
212{
213 // When state = 1, system is powered on
214 int32_t state = 0;
215
216 try
217 {
218 auto service = util::getService(POWER_OBJ_PATH,
219 POWER_INTERFACE,
220 bus);
221
222 // Use getProperty utility function to get power state.
223 util::getProperty<int32_t>(POWER_INTERFACE,
224 "state",
225 POWER_OBJ_PATH,
226 service,
227 bus,
228 state);
229
230 if (state)
231 {
232 powerOn = true;
233 }
234 else
235 {
236 powerOn = false;
237 }
238 }
239 catch (std::exception& e)
240 {
241 log<level::INFO>("Failed to get power state. Assuming it is off.");
242 powerOn = false;
243 }
244
245}
246
Brandon Wyman603cc002017-08-28 18:17:58 -0500247void PowerSupply::checkInputFault(const uint16_t statusWord)
248{
249 using namespace witherspoon::pmbus;
250
251 std::uint8_t statusInput = 0;
252
253 if ((statusWord & status_word::VIN_UV_FAULT) && !vinUVFault)
254 {
255 vinUVFault = true;
256
257 util::NamesValues nv;
258 nv.add("STATUS_WORD", statusWord);
259
260 using metadata = xyz::openbmc_project::Power::Fault::
261 PowerSupplyUnderVoltageFault;
262
263 report<PowerSupplyUnderVoltageFault>(metadata::RAW_STATUS(
264 nv.get().c_str()));
265 }
266 else
267 {
268 if (vinUVFault)
269 {
270 vinUVFault = false;
271 log<level::INFO>("VIN_UV_FAULT cleared",
272 entry("POWERSUPPLY=%s",
273 inventoryPath.c_str()));
274 }
275 }
276
277 if ((statusWord & status_word::INPUT_FAULT_WARN) && !inputFault)
278 {
279 inputFault = true;
280
281 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug);
282
283 util::NamesValues nv;
284 nv.add("STATUS_WORD", statusWord);
285 nv.add("STATUS_INPUT", statusInput);
286
287 using metadata = xyz::openbmc_project::Power::Fault::
288 PowerSupplyInputFault;
289
290 report<PowerSupplyInputFault>(
291 metadata::RAW_STATUS(nv.get().c_str()));
292 }
293 else
294 {
295 if ((inputFault) &&
296 !(statusWord & status_word::INPUT_FAULT_WARN))
297 {
298 inputFault = false;
299 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug);
300
301 log<level::INFO>("INPUT_FAULT_WARN cleared",
302 entry("POWERSUPPLY=%s", inventoryPath.c_str()),
303 entry("STATUS_WORD=0x%04X", statusWord),
304 entry("STATUS_INPUT=0x%02X", statusInput));
305 }
306 }
307}
308
309void PowerSupply::checkPGOrUnitOffFault(const uint16_t statusWord)
310{
311 using namespace witherspoon::pmbus;
312
313 std::uint8_t statusInput = 0;
314 std::uint8_t statusVout = 0;
315 std::uint8_t statusIout = 0;
316 std::uint8_t statusMFR = 0;
317
318 // Check PG# and UNIT_IS_OFF
319 if (((statusWord & status_word::POWER_GOOD_NEGATED) ||
320 (statusWord & status_word::UNIT_IS_OFF)) &&
321 !powerOnFault)
322 {
323 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug);
324 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
325 statusVout = pmbusIntf.read(status0Vout, Type::Debug);
326 statusIout = pmbusIntf.read(STATUS_IOUT, Type::Debug);
327 statusMFR = pmbusIntf.read(STATUS_MFR, Type::Debug);
328
329 util::NamesValues nv;
330 nv.add("STATUS_WORD", statusWord);
331 nv.add("STATUS_INPUT", statusInput);
332 nv.add("STATUS_VOUT", statusVout);
333 nv.add("STATUS_IOUT", statusIout);
334 nv.add("MFR_SPECIFIC", statusMFR);
335
336 using metadata = xyz::openbmc_project::Power::Fault::
337 PowerSupplyShouldBeOn;
338
339 // A power supply is OFF (or pgood low) but should be on.
340 report<PowerSupplyShouldBeOn>(metadata::RAW_STATUS(nv.get().c_str()),
341 metadata::CALLOUT_INVENTORY_PATH(
342 inventoryPath.c_str()));
343
344 powerOnFault = true;
345 }
346
347}
348
349void PowerSupply::checkCurrentOutOverCurrentFault(const uint16_t statusWord)
350{
351 using namespace witherspoon::pmbus;
352
353 std::uint8_t statusInput = 0;
354 std::uint8_t statusVout = 0;
355 std::uint8_t statusIout = 0;
356 std::uint8_t statusMFR = 0;
357
358 // Check for an output overcurrent fault.
359 if ((statusWord & status_word::IOUT_OC_FAULT) &&
360 !outputOCFault)
361 {
362 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug);
363 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
364 statusVout = pmbusIntf.read(status0Vout, Type::Debug);
365 statusIout = pmbusIntf.read(STATUS_IOUT, Type::Debug);
366 statusMFR = pmbusIntf.read(STATUS_MFR, Type::Debug);
367
368 util::NamesValues nv;
369 nv.add("STATUS_WORD", statusWord);
370 nv.add("STATUS_INPUT", statusInput);
371 nv.add("STATUS_VOUT", statusVout);
372 nv.add("STATUS_IOUT", statusIout);
373 nv.add("MFR_SPECIFIC", statusMFR);
374
375 using metadata = xyz::openbmc_project::Power::Fault::
376 PowerSupplyOutputOvercurrent;
377
378 report<PowerSupplyOutputOvercurrent>(metadata::RAW_STATUS(
379 nv.get().c_str()),
380 metadata::CALLOUT_INVENTORY_PATH(
381 inventoryPath.c_str()));
382
383 outputOCFault = true;
384 }
385}
386
Brandon Wymanab05c072017-08-30 18:26:41 -0500387void PowerSupply::checkOutputOvervoltageFault(const uint16_t statusWord)
388{
389 using namespace witherspoon::pmbus;
390
391 std::uint8_t statusInput = 0;
392 std::uint8_t statusVout = 0;
393 std::uint8_t statusIout = 0;
394 std::uint8_t statusMFR = 0;
395
396 // Check for an output overvoltage fault.
397 if ((statusWord & status_word::VOUT_OV_FAULT) &&
398 !outputOVFault)
399 {
400 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug);
401 statusVout = pmbusIntf.read(STATUS_VOUT, Type::Debug);
402 statusIout = pmbusIntf.read(STATUS_IOUT, Type::Debug);
403 statusMFR = pmbusIntf.read(STATUS_MFR, Type::Debug);
404
405 util::NamesValues nv;
406 nv.add("STATUS_WORD", statusWord);
407 nv.add("STATUS_INPUT", statusInput);
408 nv.add("STATUS_VOUT", statusVout);
409 nv.add("STATUS_IOUT", statusIout);
410 nv.add("MFR_SPECIFIC", statusMFR);
411
412 using metadata = xyz::openbmc_project::Power::Fault::
413 PowerSupplyOutputOvervoltage;
414
415 report<PowerSupplyOutputOvervoltage>(metadata::RAW_STATUS(
416 nv.get().c_str()),
417 metadata::CALLOUT_INVENTORY_PATH(
418 inventoryPath.c_str()));
419
420 outputOVFault = true;
421 }
422}
423
Brandon Wyman12661f12017-08-31 15:28:21 -0500424void PowerSupply::checkFanFault(const uint16_t statusWord)
425{
426 using namespace witherspoon::pmbus;
427
428 std::uint8_t statusMFR = 0;
429 std::uint8_t statusTemperature = 0;
430 std::uint8_t statusFans12 = 0;
431
432 // Check for an output overcurrent fault.
433 if ((statusWord & status_word::FAN_FAULT) &&
434 !fanFault)
435 {
436 statusMFR = pmbusIntf.read(STATUS_MFR, Type::Debug);
437 statusTemperature = pmbusIntf.read(STATUS_TEMPERATURE, Type::Debug);
438 statusFans12 = pmbusIntf.read(STATUS_FANS_1_2, Type::Debug);
439
440 util::NamesValues nv;
441 nv.add("STATUS_WORD", statusWord);
442 nv.add("MFR_SPECIFIC", statusMFR);
443 nv.add("STATUS_TEMPERATURE", statusTemperature);
444 nv.add("STATUS_FANS_1_2", statusFans12);
445
446 using metadata = xyz::openbmc_project::Power::Fault::
447 PowerSupplyFanFault;
448
449 report<PowerSupplyFanFault>(
450 metadata::RAW_STATUS(nv.get().c_str()),
451 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
452
453 fanFault = true;
454 }
455}
456
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500457void PowerSupply::clearFaults()
458{
Brandon Wyman10295542017-08-09 18:20:44 -0500459 //TODO - Clear faults at pre-poweron. openbmc/openbmc#1736
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500460 return;
461}
462
Brandon Wyman24e422f2017-07-25 19:40:14 -0500463}
464}
465}