blob: aa7588ee24150246cbf053efd2392b1f6a3076f1 [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 */
George Liu690e7802019-08-23 11:04:01 +080016#include "config.h"
17
Matt Spinlerf0f02b92018-10-25 16:12:43 -050018#include "power_supply.hpp"
19
Brandon Wyman442035f2017-08-08 15:58:45 -050020#include "elog-errors.hpp"
Matt Spinlerd734e652018-01-18 14:31:15 -060021#include "gpio.hpp"
Brandon Wyman10295542017-08-09 18:20:44 -050022#include "names_values.hpp"
Brandon Wyman442035f2017-08-08 15:58:45 -050023#include "pmbus.hpp"
Lei YUcfc040c2019-10-29 17:10:26 +080024#include "types.hpp"
Brandon Wyman442035f2017-08-08 15:58:45 -050025#include "utility.hpp"
26
Matt Spinlerf0f02b92018-10-25 16:12:43 -050027#include <org/open_power/Witherspoon/Fault/error.hpp>
Matt Spinlerf0f02b92018-10-25 16:12:43 -050028#include <phosphor-logging/log.hpp>
29#include <xyz/openbmc_project/Common/Device/error.hpp>
Matt Spinlerf0f02b92018-10-25 16:12:43 -050030
Brandon Wymand1bc4ce2019-12-13 14:20:34 -060031#include <functional>
32
Lei YUab093322019-10-09 16:43:22 +080033namespace phosphor
Brandon Wyman24e422f2017-07-25 19:40:14 -050034{
35namespace power
36{
37namespace psu
38{
39
Matt Spinler589e8722018-01-04 15:24:49 -060040using namespace phosphor::logging;
41using namespace sdbusplus::org::open_power::Witherspoon::Fault::Error;
42using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
43
Brandon Wyman10295542017-08-09 18:20:44 -050044PowerSupply::PowerSupply(const std::string& name, size_t inst,
Matt Spinlerf0f02b92018-10-25 16:12:43 -050045 const std::string& objpath, const std::string& invpath,
46 sdbusplus::bus::bus& bus, const sdeventplus::Event& e,
47 std::chrono::seconds& t, std::chrono::seconds& p) :
48 Device(name, inst),
49 monitorPath(objpath), pmbusIntf(objpath),
50 inventoryPath(INVENTORY_OBJ_PATH + invpath), bus(bus), presentInterval(p),
51 presentTimer(e, std::bind([this]() {
52 // The hwmon path may have changed.
53 pmbusIntf.findHwmonDir();
54 this->present = true;
Matt Spinler234ce0d2018-01-04 15:06:57 -060055
Matt Spinlerf0f02b92018-10-25 16:12:43 -050056 // Sync the INPUT_HISTORY data for all PSs
57 syncHistory();
Matt Spinlerd734e652018-01-18 14:31:15 -060058
Matt Spinlerf0f02b92018-10-25 16:12:43 -050059 // Update the inventory for the new device
60 updateInventory();
61 })),
62 powerOnInterval(t),
63 powerOnTimer(e, std::bind([this]() { this->powerOn = true; }))
Brandon Wyman10295542017-08-09 18:20:44 -050064{
George Liu690e7802019-08-23 11:04:01 +080065 getAccessType();
66
Brandon Wyman10295542017-08-09 18:20:44 -050067 using namespace sdbusplus::bus;
Lei YUab093322019-10-09 16:43:22 +080068 using namespace phosphor::pmbus;
Aatir Manzur817f8a72019-08-07 07:42:15 -050069 std::uint16_t statusWord = 0;
70 try
71 {
72 // Read the 2 byte STATUS_WORD value to check for faults.
73 statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug);
74 if (!((statusWord & status_word::INPUT_FAULT_WARN) ||
75 (statusWord & status_word::VIN_UV_FAULT)))
76 {
77 resolveError(inventoryPath,
78 std::string(PowerSupplyInputFault::errName));
79 }
80 }
81 catch (ReadFailure& e)
82 {
83 log<level::INFO>("Unable to read the 2 byte STATUS_WORD value to check "
84 "for power-supply input faults.");
85 }
Matt Spinlerf0f02b92018-10-25 16:12:43 -050086 presentMatch = std::make_unique<match_t>(
87 bus, match::rules::propertiesChanged(inventoryPath, INVENTORY_IFACE),
88 [this](auto& msg) { this->inventoryChanged(msg); });
Brandon Wyman431fbe42017-08-18 16:22:09 -050089 // Get initial presence state.
Brandon Wyman253dc9b2017-08-12 13:45:52 -050090 updatePresence();
Brandon Wyman431fbe42017-08-18 16:22:09 -050091
Matt Spinler234ce0d2018-01-04 15:06:57 -060092 // Write the SN, PN, etc to the inventory
93 updateInventory();
94
Brandon Wyman431fbe42017-08-18 16:22:09 -050095 // Subscribe to power state changes
Matt Spinlerf0f02b92018-10-25 16:12:43 -050096 powerOnMatch = std::make_unique<match_t>(
97 bus, match::rules::propertiesChanged(POWER_OBJ_PATH, POWER_IFACE),
98 [this](auto& msg) { this->powerStateChanged(msg); });
Brandon Wyman431fbe42017-08-18 16:22:09 -050099 // Get initial power state.
100 updatePowerState();
Brandon Wyman10295542017-08-09 18:20:44 -0500101}
Brandon Wyman442035f2017-08-08 15:58:45 -0500102
George Liu690e7802019-08-23 11:04:01 +0800103void PowerSupply::getAccessType()
104{
Lei YUab093322019-10-09 16:43:22 +0800105 using namespace phosphor::power::util;
George Liu690e7802019-08-23 11:04:01 +0800106 fruJson = loadJSONFromFile(PSU_JSON_PATH);
107 if (fruJson == nullptr)
108 {
109 log<level::ERR>("InternalFailure when parsing the JSON file");
110 return;
111 }
Lei YU40705462019-10-09 17:07:11 +0800112 inventoryPMBusAccessType = getPMBusAccessType(fruJson);
George Liu690e7802019-08-23 11:04:01 +0800113}
114
Brandon Wymana1e96342017-09-25 16:47:44 -0500115void PowerSupply::captureCmd(util::NamesValues& nv, const std::string& cmd,
Lei YUab093322019-10-09 16:43:22 +0800116 phosphor::pmbus::Type type)
Brandon Wymana1e96342017-09-25 16:47:44 -0500117{
118 if (pmbusIntf.exists(cmd, type))
119 {
120 try
121 {
122 auto val = pmbusIntf.read(cmd, type);
123 nv.add(cmd, val);
124 }
125 catch (std::exception& e)
126 {
Joseph Reynolds5b485962018-05-17 16:05:06 -0500127 log<level::INFO>("Unable to capture metadata",
128 entry("CMD=%s", cmd.c_str()));
Brandon Wymana1e96342017-09-25 16:47:44 -0500129 }
130 }
131}
Brandon Wyman431fbe42017-08-18 16:22:09 -0500132
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500133void PowerSupply::analyze()
134{
Lei YUab093322019-10-09 16:43:22 +0800135 using namespace phosphor::pmbus;
Brandon Wyman442035f2017-08-08 15:58:45 -0500136
137 try
138 {
Brandon Wyman10295542017-08-09 18:20:44 -0500139 if (present)
Brandon Wyman442035f2017-08-08 15:58:45 -0500140 {
Brandon Wyman764c7972017-08-22 17:05:36 -0500141 std::uint16_t statusWord = 0;
Brandon Wyman764c7972017-08-22 17:05:36 -0500142
143 // Read the 2 byte STATUS_WORD value to check for faults.
144 statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug);
Brandon Wymane4af9802017-11-13 15:58:33 -0600145 readFail = 0;
Brandon Wyman764c7972017-08-22 17:05:36 -0500146
Brandon Wyman603cc002017-08-28 18:17:58 -0500147 checkInputFault(statusWord);
Brandon Wyman764c7972017-08-22 17:05:36 -0500148
Brandon Wyman654eb6e2018-02-14 14:55:47 -0600149 if (powerOn && (inputFault == 0) && !faultFound)
Brandon Wyman764c7972017-08-22 17:05:36 -0500150 {
Brandon Wyman12661f12017-08-31 15:28:21 -0500151 checkFanFault(statusWord);
Brandon Wyman875b3632017-09-13 18:46:03 -0500152 checkTemperatureFault(statusWord);
Brandon Wymancfa032b2017-09-25 17:37:50 -0500153 checkOutputOvervoltageFault(statusWord);
154 checkCurrentOutOverCurrentFault(statusWord);
155 checkPGOrUnitOffFault(statusWord);
Brandon Wyman442035f2017-08-08 15:58:45 -0500156 }
Matt Spinlereb169fd2018-01-18 14:19:08 -0600157
158 updateHistory();
Brandon Wyman442035f2017-08-08 15:58:45 -0500159 }
160 }
161 catch (ReadFailure& e)
162 {
Brandon Wymane4af9802017-11-13 15:58:33 -0600163 if (readFail < FAULT_COUNT)
164 {
165 readFail++;
166 }
167
168 if (!readFailLogged && readFail >= FAULT_COUNT)
Brandon Wyman442035f2017-08-08 15:58:45 -0500169 {
170 commit<ReadFailure>();
171 readFailLogged = true;
Brandon Wyman442035f2017-08-08 15:58:45 -0500172 }
173 }
174
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500175 return;
176}
177
Brandon Wyman10295542017-08-09 18:20:44 -0500178void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
179{
180 std::string msgSensor;
181 std::map<std::string, sdbusplus::message::variant<uint32_t, bool>> msgData;
182 msg.read(msgSensor, msgData);
183
184 // Check if it was the Present property that changed.
185 auto valPropMap = msgData.find(PRESENT_PROP);
186 if (valPropMap != msgData.end())
187 {
Brandon Wyman2ef48cf2017-11-21 15:43:54 -0600188 if (sdbusplus::message::variant_ns::get<bool>(valPropMap->second))
Brandon Wyman10295542017-08-09 18:20:44 -0500189 {
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500190 clearFaults();
William A. Kennington III1a0c9172018-10-18 17:57:49 -0700191 presentTimer.restartOnce(presentInterval);
Brandon Wyman590fc282017-11-01 18:22:25 -0500192 }
193 else
194 {
Brandon Wyman2ef48cf2017-11-21 15:43:54 -0600195 present = false;
William A. Kennington III1a0c9172018-10-18 17:57:49 -0700196 presentTimer.setEnabled(false);
Matt Spinler234ce0d2018-01-04 15:06:57 -0600197
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500198 // Clear out the now outdated inventory properties
Matt Spinler234ce0d2018-01-04 15:06:57 -0600199 updateInventory();
Brandon Wyman10295542017-08-09 18:20:44 -0500200 }
201 }
202
203 return;
204}
205
206void PowerSupply::updatePresence()
207{
208 // Use getProperty utility function to get presence status.
Brandon Wyman10295542017-08-09 18:20:44 -0500209 std::string service = "xyz.openbmc_project.Inventory.Manager";
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500210 util::getProperty(INVENTORY_IFACE, PRESENT_PROP, inventoryPath, service,
211 bus, this->present);
Brandon Wyman10295542017-08-09 18:20:44 -0500212}
213
Brandon Wyman431fbe42017-08-18 16:22:09 -0500214void PowerSupply::powerStateChanged(sdbusplus::message::message& msg)
215{
216 int32_t state = 0;
217 std::string msgSensor;
William A. Kennington IIIac9d5c32018-11-12 14:58:39 -0800218 std::map<std::string, sdbusplus::message::variant<int32_t>> msgData;
Brandon Wyman431fbe42017-08-18 16:22:09 -0500219 msg.read(msgSensor, msgData);
220
221 // Check if it was the Present property that changed.
222 auto valPropMap = msgData.find("state");
223 if (valPropMap != msgData.end())
224 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500225 state =
226 sdbusplus::message::variant_ns::get<int32_t>(valPropMap->second);
Brandon Wyman431fbe42017-08-18 16:22:09 -0500227
228 // Power is on when state=1. Set the fault logged variables to false
229 // and start the power on timer when the state changes to 1.
230 if (state)
231 {
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500232 clearFaults();
William A. Kennington III1a0c9172018-10-18 17:57:49 -0700233 powerOnTimer.restartOnce(powerOnInterval);
Brandon Wyman431fbe42017-08-18 16:22:09 -0500234 }
235 else
236 {
William A. Kennington III1a0c9172018-10-18 17:57:49 -0700237 powerOnTimer.setEnabled(false);
Brandon Wyman431fbe42017-08-18 16:22:09 -0500238 powerOn = false;
239 }
240 }
Brandon Wyman431fbe42017-08-18 16:22:09 -0500241}
242
243void PowerSupply::updatePowerState()
244{
Lei YUcfc040c2019-10-29 17:10:26 +0800245 powerOn = util::isPoweredOn(bus);
Brandon Wyman431fbe42017-08-18 16:22:09 -0500246}
247
Brandon Wyman603cc002017-08-28 18:17:58 -0500248void PowerSupply::checkInputFault(const uint16_t statusWord)
249{
Lei YUab093322019-10-09 16:43:22 +0800250 using namespace phosphor::pmbus;
Brandon Wyman603cc002017-08-28 18:17:58 -0500251
Brandon Wymana3c675c2017-11-14 14:54:54 -0600252 if ((inputFault < FAULT_COUNT) &&
253 ((statusWord & status_word::INPUT_FAULT_WARN) ||
254 (statusWord & status_word::VIN_UV_FAULT)))
Brandon Wyman603cc002017-08-28 18:17:58 -0500255 {
Brandon Wymanad708242018-01-19 17:28:35 -0600256 if (inputFault == 0)
257 {
258 log<level::INFO>("INPUT or VIN_UV fault",
259 entry("STATUS_WORD=0x%04X", statusWord));
260 }
261
Brandon Wymana3c675c2017-11-14 14:54:54 -0600262 inputFault++;
Brandon Wyman603cc002017-08-28 18:17:58 -0500263 }
264 else
265 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500266 if ((inputFault > 0) && !(statusWord & status_word::INPUT_FAULT_WARN) &&
Brandon Wymand20686a2017-11-01 17:45:23 -0500267 !(statusWord & status_word::VIN_UV_FAULT))
Brandon Wyman603cc002017-08-28 18:17:58 -0500268 {
Brandon Wymana3c675c2017-11-14 14:54:54 -0600269 inputFault = 0;
Brandon Wyman3343e822017-11-03 16:54:11 -0500270 faultFound = false;
Brandon Wyman7502f6c2018-02-12 20:20:39 -0600271 // When an input fault occurs, the power supply cannot be on.
272 // However, the check for the case where the power supply should be
273 // on will stop when there is a fault found.
274 // Clear the powerOnFault when the inputFault is cleared to reset
275 // the powerOnFault de-glitching.
276 powerOnFault = 0;
Brandon Wyman69591bd2017-11-01 18:07:23 -0500277
Brandon Wyman603cc002017-08-28 18:17:58 -0500278 log<level::INFO>("INPUT_FAULT_WARN cleared",
Brandon Wymana3c675c2017-11-14 14:54:54 -0600279 entry("POWERSUPPLY=%s", inventoryPath.c_str()));
Brandon Wyman69591bd2017-11-01 18:07:23 -0500280
Brandon Wyman08b05712017-11-30 17:53:56 -0600281 resolveError(inventoryPath,
282 std::string(PowerSupplyInputFault::errName));
283
Brandon Wyman69591bd2017-11-01 18:07:23 -0500284 if (powerOn)
285 {
286 // The power supply will not be immediately powered on after
287 // the input power is restored.
288 powerOn = false;
289 // Start up the timer that will set the state to indicate we
290 // are ready for the powered on fault checks.
William A. Kennington III1a0c9172018-10-18 17:57:49 -0700291 powerOnTimer.restartOnce(powerOnInterval);
Brandon Wyman69591bd2017-11-01 18:07:23 -0500292 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500293 }
294 }
Brandon Wymana3c675c2017-11-14 14:54:54 -0600295
296 if (!faultFound && (inputFault >= FAULT_COUNT))
297 {
Brandon Wymanad708242018-01-19 17:28:35 -0600298 // If the power is on, report the fault in an error log entry.
299 if (powerOn)
300 {
301 util::NamesValues nv;
302 nv.add("STATUS_WORD", statusWord);
303 captureCmd(nv, STATUS_INPUT, Type::Debug);
Brandon Wymana3c675c2017-11-14 14:54:54 -0600304
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500305 using metadata =
306 org::open_power::Witherspoon::Fault::PowerSupplyInputFault;
Brandon Wymana3c675c2017-11-14 14:54:54 -0600307
Brandon Wymanad708242018-01-19 17:28:35 -0600308 report<PowerSupplyInputFault>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500309 metadata::RAW_STATUS(nv.get().c_str()),
310 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wymanad708242018-01-19 17:28:35 -0600311
312 faultFound = true;
313 }
Brandon Wymana3c675c2017-11-14 14:54:54 -0600314 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500315}
316
317void PowerSupply::checkPGOrUnitOffFault(const uint16_t statusWord)
318{
Lei YUab093322019-10-09 16:43:22 +0800319 using namespace phosphor::pmbus;
Brandon Wyman603cc002017-08-28 18:17:58 -0500320
Brandon Wyman593d24f2017-10-13 18:15:23 -0500321 if (powerOnFault < FAULT_COUNT)
Brandon Wyman603cc002017-08-28 18:17:58 -0500322 {
Brandon Wyman593d24f2017-10-13 18:15:23 -0500323 // Check PG# and UNIT_IS_OFF
324 if ((statusWord & status_word::POWER_GOOD_NEGATED) ||
325 (statusWord & status_word::UNIT_IS_OFF))
326 {
327 log<level::INFO>("PGOOD or UNIT_IS_OFF bit bad",
328 entry("STATUS_WORD=0x%04X", statusWord));
329 powerOnFault++;
330 }
331 else
332 {
333 if (powerOnFault > 0)
334 {
335 log<level::INFO>("PGOOD and UNIT_IS_OFF bits good");
336 powerOnFault = 0;
337 }
338 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500339
Brandon Wymane2fc7aa2017-11-13 17:37:10 -0600340 if (!faultFound && (powerOnFault >= FAULT_COUNT))
Brandon Wyman593d24f2017-10-13 18:15:23 -0500341 {
Brandon Wyman3343e822017-11-03 16:54:11 -0500342 faultFound = true;
343
Brandon Wyman593d24f2017-10-13 18:15:23 -0500344 util::NamesValues nv;
345 nv.add("STATUS_WORD", statusWord);
346 captureCmd(nv, STATUS_INPUT, Type::Debug);
347 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
348 captureCmd(nv, status0Vout, Type::Debug);
349 captureCmd(nv, STATUS_IOUT, Type::Debug);
350 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wyman603cc002017-08-28 18:17:58 -0500351
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500352 using metadata =
353 org::open_power::Witherspoon::Fault::PowerSupplyShouldBeOn;
Brandon Wyman603cc002017-08-28 18:17:58 -0500354
Brandon Wyman593d24f2017-10-13 18:15:23 -0500355 // A power supply is OFF (or pgood low) but should be on.
356 report<PowerSupplyShouldBeOn>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500357 metadata::RAW_STATUS(nv.get().c_str()),
358 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wyman593d24f2017-10-13 18:15:23 -0500359 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500360 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500361}
362
363void PowerSupply::checkCurrentOutOverCurrentFault(const uint16_t statusWord)
364{
Lei YUab093322019-10-09 16:43:22 +0800365 using namespace phosphor::pmbus;
Brandon Wyman603cc002017-08-28 18:17:58 -0500366
Brandon Wymandd61be42017-11-07 18:38:54 -0600367 if (outputOCFault < FAULT_COUNT)
Brandon Wyman603cc002017-08-28 18:17:58 -0500368 {
Brandon Wymandd61be42017-11-07 18:38:54 -0600369 // Check for an output overcurrent fault.
370 if ((statusWord & status_word::IOUT_OC_FAULT))
371 {
372 outputOCFault++;
373 }
374 else
375 {
376 if (outputOCFault > 0)
377 {
378 outputOCFault = 0;
379 }
380 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500381
Brandon Wymane2fc7aa2017-11-13 17:37:10 -0600382 if (!faultFound && (outputOCFault >= FAULT_COUNT))
Brandon Wymandd61be42017-11-07 18:38:54 -0600383 {
384 util::NamesValues nv;
385 nv.add("STATUS_WORD", statusWord);
386 captureCmd(nv, STATUS_INPUT, Type::Debug);
387 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
388 captureCmd(nv, status0Vout, Type::Debug);
389 captureCmd(nv, STATUS_IOUT, Type::Debug);
390 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wyman603cc002017-08-28 18:17:58 -0500391
Brandon Wymandd61be42017-11-07 18:38:54 -0600392 using metadata = org::open_power::Witherspoon::Fault::
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500393 PowerSupplyOutputOvercurrent;
Brandon Wyman603cc002017-08-28 18:17:58 -0500394
Brandon Wymandd61be42017-11-07 18:38:54 -0600395 report<PowerSupplyOutputOvercurrent>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500396 metadata::RAW_STATUS(nv.get().c_str()),
397 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wymandd61be42017-11-07 18:38:54 -0600398
399 faultFound = true;
400 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500401 }
402}
403
Brandon Wymanab05c072017-08-30 18:26:41 -0500404void PowerSupply::checkOutputOvervoltageFault(const uint16_t statusWord)
405{
Lei YUab093322019-10-09 16:43:22 +0800406 using namespace phosphor::pmbus;
Brandon Wymanab05c072017-08-30 18:26:41 -0500407
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600408 if (outputOVFault < FAULT_COUNT)
Brandon Wymanab05c072017-08-30 18:26:41 -0500409 {
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600410 // Check for an output overvoltage fault.
411 if (statusWord & status_word::VOUT_OV_FAULT)
412 {
413 outputOVFault++;
414 }
415 else
416 {
417 if (outputOVFault > 0)
418 {
419 outputOVFault = 0;
420 }
421 }
Brandon Wymanab05c072017-08-30 18:26:41 -0500422
Brandon Wymane2fc7aa2017-11-13 17:37:10 -0600423 if (!faultFound && (outputOVFault >= FAULT_COUNT))
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600424 {
425 util::NamesValues nv;
426 nv.add("STATUS_WORD", statusWord);
427 captureCmd(nv, STATUS_INPUT, Type::Debug);
428 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
429 captureCmd(nv, status0Vout, Type::Debug);
430 captureCmd(nv, STATUS_IOUT, Type::Debug);
431 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wymanab05c072017-08-30 18:26:41 -0500432
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600433 using metadata = org::open_power::Witherspoon::Fault::
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500434 PowerSupplyOutputOvervoltage;
Brandon Wymanab05c072017-08-30 18:26:41 -0500435
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600436 report<PowerSupplyOutputOvervoltage>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500437 metadata::RAW_STATUS(nv.get().c_str()),
438 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600439
440 faultFound = true;
441 }
Brandon Wymanab05c072017-08-30 18:26:41 -0500442 }
443}
444
Brandon Wyman12661f12017-08-31 15:28:21 -0500445void PowerSupply::checkFanFault(const uint16_t statusWord)
446{
Lei YUab093322019-10-09 16:43:22 +0800447 using namespace phosphor::pmbus;
Brandon Wyman12661f12017-08-31 15:28:21 -0500448
Brandon Wymanba255532017-11-08 17:44:10 -0600449 if (fanFault < FAULT_COUNT)
Brandon Wyman12661f12017-08-31 15:28:21 -0500450 {
Brandon Wymanba255532017-11-08 17:44:10 -0600451 // Check for a fan fault or warning condition
452 if (statusWord & status_word::FAN_FAULT)
453 {
454 fanFault++;
455 }
456 else
457 {
458 if (fanFault > 0)
459 {
460 fanFault = 0;
461 }
462 }
Brandon Wyman12661f12017-08-31 15:28:21 -0500463
Brandon Wymane2fc7aa2017-11-13 17:37:10 -0600464 if (!faultFound && (fanFault >= FAULT_COUNT))
Brandon Wymanba255532017-11-08 17:44:10 -0600465 {
466 util::NamesValues nv;
467 nv.add("STATUS_WORD", statusWord);
468 captureCmd(nv, STATUS_MFR, Type::Debug);
469 captureCmd(nv, STATUS_TEMPERATURE, Type::Debug);
470 captureCmd(nv, STATUS_FANS_1_2, Type::Debug);
Brandon Wyman12661f12017-08-31 15:28:21 -0500471
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500472 using metadata =
473 org::open_power::Witherspoon::Fault::PowerSupplyFanFault;
Brandon Wyman12661f12017-08-31 15:28:21 -0500474
Brandon Wymanba255532017-11-08 17:44:10 -0600475 report<PowerSupplyFanFault>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500476 metadata::RAW_STATUS(nv.get().c_str()),
477 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wymanba255532017-11-08 17:44:10 -0600478
479 faultFound = true;
480 }
Brandon Wyman12661f12017-08-31 15:28:21 -0500481 }
482}
483
Brandon Wyman875b3632017-09-13 18:46:03 -0500484void PowerSupply::checkTemperatureFault(const uint16_t statusWord)
485{
Lei YUab093322019-10-09 16:43:22 +0800486 using namespace phosphor::pmbus;
Brandon Wyman875b3632017-09-13 18:46:03 -0500487
488 // Due to how the PMBus core device driver sends a clear faults command
489 // the bit in STATUS_WORD will likely be cleared when we attempt to examine
490 // it for a Thermal Fault or Warning. So, check the STATUS_WORD and the
491 // STATUS_TEMPERATURE bits. If either indicates a fault, proceed with
492 // logging the over-temperature condition.
493 std::uint8_t statusTemperature = 0;
494 statusTemperature = pmbusIntf.read(STATUS_TEMPERATURE, Type::Debug);
Brandon Wyman50044ea2017-11-08 17:58:56 -0600495 if (temperatureFault < FAULT_COUNT)
Brandon Wyman875b3632017-09-13 18:46:03 -0500496 {
Brandon Wyman50044ea2017-11-08 17:58:56 -0600497 if ((statusWord & status_word::TEMPERATURE_FAULT_WARN) ||
498 (statusTemperature & status_temperature::OT_FAULT))
499 {
500 temperatureFault++;
501 }
502 else
503 {
504 if (temperatureFault > 0)
505 {
506 temperatureFault = 0;
507 }
508 }
Brandon Wyman875b3632017-09-13 18:46:03 -0500509
Brandon Wymane2fc7aa2017-11-13 17:37:10 -0600510 if (!faultFound && (temperatureFault >= FAULT_COUNT))
Brandon Wyman50044ea2017-11-08 17:58:56 -0600511 {
512 // The power supply has had an over-temperature condition.
513 // This may not result in a shutdown if experienced for a short
514 // duration.
515 // This should not occur under normal conditions.
516 // The power supply may be faulty, or the paired supply may be
517 // putting out less current.
518 // Capture command responses with potentially relevant information,
519 // and call out the power supply reporting the condition.
520 util::NamesValues nv;
521 nv.add("STATUS_WORD", statusWord);
522 captureCmd(nv, STATUS_MFR, Type::Debug);
523 captureCmd(nv, STATUS_IOUT, Type::Debug);
524 nv.add("STATUS_TEMPERATURE", statusTemperature);
525 captureCmd(nv, STATUS_FANS_1_2, Type::Debug);
Brandon Wyman875b3632017-09-13 18:46:03 -0500526
Brandon Wyman50044ea2017-11-08 17:58:56 -0600527 using metadata = org::open_power::Witherspoon::Fault::
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500528 PowerSupplyTemperatureFault;
Brandon Wyman875b3632017-09-13 18:46:03 -0500529
Brandon Wyman50044ea2017-11-08 17:58:56 -0600530 report<PowerSupplyTemperatureFault>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500531 metadata::RAW_STATUS(nv.get().c_str()),
532 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wyman50044ea2017-11-08 17:58:56 -0600533
534 faultFound = true;
535 }
Brandon Wyman875b3632017-09-13 18:46:03 -0500536 }
537}
538
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500539void PowerSupply::clearFaults()
540{
Brandon Wymane4af9802017-11-13 15:58:33 -0600541 readFail = 0;
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500542 readFailLogged = false;
Brandon Wymana3c675c2017-11-14 14:54:54 -0600543 inputFault = 0;
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500544 powerOnFault = 0;
Brandon Wymandd61be42017-11-07 18:38:54 -0600545 outputOCFault = 0;
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600546 outputOVFault = 0;
Brandon Wymanba255532017-11-08 17:44:10 -0600547 fanFault = 0;
Brandon Wyman50044ea2017-11-08 17:58:56 -0600548 temperatureFault = 0;
Brandon Wyman3343e822017-11-03 16:54:11 -0500549 faultFound = false;
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500550
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500551 return;
552}
553
Brandon Wyman43ce2082017-11-30 17:24:01 -0600554void PowerSupply::resolveError(const std::string& callout,
555 const std::string& message)
556{
Brandon Wyman01741f12017-12-01 17:22:08 -0600557 using EndpointList = std::vector<std::string>;
558
559 try
560 {
561 auto path = callout + "/fault";
562 // Get the service name from the mapper for the fault callout
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500563 auto service = util::getService(path, ASSOCIATION_IFACE, bus);
Brandon Wyman01741f12017-12-01 17:22:08 -0600564
565 // Use getProperty utility function to get log entries (endpoints)
566 EndpointList logEntries;
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500567 util::getProperty(ASSOCIATION_IFACE, ENDPOINTS_PROP, path, service, bus,
568 logEntries);
Brandon Wyman01741f12017-12-01 17:22:08 -0600569
570 // It is possible that all such entries for this callout have since
571 // been deleted.
572 if (logEntries.empty())
573 {
574 return;
575 }
576
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500577 auto logEntryService =
578 util::getService(logEntries[0], LOGGING_IFACE, bus);
Brandon Wyman01741f12017-12-01 17:22:08 -0600579 if (logEntryService.empty())
580 {
581 return;
582 }
583
584 // go through each log entry that matches this callout path
585 std::string logMessage;
586 for (const auto& logEntry : logEntries)
587 {
588 // Check to see if this logEntry has a message that matches.
Matt Spinler589e8722018-01-04 15:24:49 -0600589 util::getProperty(LOGGING_IFACE, MESSAGE_PROP, logEntry,
Brandon Wyman01741f12017-12-01 17:22:08 -0600590 logEntryService, bus, logMessage);
591
592 if (message == logMessage)
593 {
594 // Log entry matches call out and message, set Resolved to true
595 bool resolved = true;
Matt Spinler589e8722018-01-04 15:24:49 -0600596 util::setProperty(LOGGING_IFACE, RESOLVED_PROP, logEntry,
Brandon Wyman01741f12017-12-01 17:22:08 -0600597 logEntryService, bus, resolved);
598 }
Brandon Wyman01741f12017-12-01 17:22:08 -0600599 }
Brandon Wyman01741f12017-12-01 17:22:08 -0600600 }
601 catch (std::exception& e)
602 {
603 log<level::INFO>("Failed to resolve error",
604 entry("CALLOUT=%s", callout.c_str()),
Matt Spinler0d09f292018-01-22 14:51:26 -0600605 entry("ERROR=%s", message.c_str()));
Brandon Wyman01741f12017-12-01 17:22:08 -0600606 }
Brandon Wyman43ce2082017-11-30 17:24:01 -0600607}
608
Matt Spinler234ce0d2018-01-04 15:06:57 -0600609void PowerSupply::updateInventory()
610{
Lei YUab093322019-10-09 16:43:22 +0800611 using namespace phosphor::pmbus;
Matt Spinler018a7bc2018-01-04 15:36:41 -0600612 using namespace sdbusplus::message;
613
Matt Spinler018a7bc2018-01-04 15:36:41 -0600614 // Build the object map and send it to the inventory
615 using Properties = std::map<std::string, variant<std::string>>;
616 using Interfaces = std::map<std::string, Properties>;
617 using Object = std::map<object_path, Interfaces>;
618 Properties assetProps;
Matt Spinler018a7bc2018-01-04 15:36:41 -0600619 Interfaces interfaces;
620 Object object;
621
George Liu690e7802019-08-23 11:04:01 +0800622 // If any of these accesses fail, the fields will just be
623 // blank in the inventory. Leave logging ReadFailure errors
624 // to analyze() as it runs continuously and will most
625 // likely hit and threshold them first anyway. The
626 // readString() function will do the tracing of the failing
627 // path so this code doesn't need to.
628 for (const auto& fru : fruJson.at("fruConfigs"))
629 {
630 if (fru.at("interface") == ASSET_IFACE)
631 {
632 try
633 {
634 assetProps.emplace(
635 fru.at("propertyName"),
636 present ? pmbusIntf.readString(fru.at("fileName"),
637 inventoryPMBusAccessType)
638 : "");
639 }
640 catch (ReadFailure& e)
641 {
642 }
643 }
George Liu690e7802019-08-23 11:04:01 +0800644 }
Matt Spinler018a7bc2018-01-04 15:36:41 -0600645
George Liu690e7802019-08-23 11:04:01 +0800646 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
Matt Spinler018a7bc2018-01-04 15:36:41 -0600647
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500648 // For Notify(), just send the relative path of the inventory
649 // object so remove the INVENTORY_OBJ_PATH prefix
Matt Spinler018a7bc2018-01-04 15:36:41 -0600650 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
651
652 object.emplace(path, std::move(interfaces));
653
654 try
655 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500656 auto service =
657 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
Matt Spinler018a7bc2018-01-04 15:36:41 -0600658
659 if (service.empty())
660 {
661 log<level::ERR>("Unable to get inventory manager service");
662 return;
663 }
664
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500665 auto method = bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
666 INVENTORY_MGR_IFACE, "Notify");
Matt Spinler018a7bc2018-01-04 15:36:41 -0600667
668 method.append(std::move(object));
669
670 auto reply = bus.call(method);
Matt Spinler018a7bc2018-01-04 15:36:41 -0600671 }
672 catch (std::exception& e)
673 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500674 log<level::ERR>(e.what(), entry("PATH=%s", inventoryPath.c_str()));
Matt Spinler018a7bc2018-01-04 15:36:41 -0600675 }
Matt Spinler234ce0d2018-01-04 15:06:57 -0600676}
677
Matt Spinlerd734e652018-01-18 14:31:15 -0600678void PowerSupply::syncHistory()
679{
Lei YUab093322019-10-09 16:43:22 +0800680 using namespace phosphor::gpio;
Matt Spinlerd734e652018-01-18 14:31:15 -0600681
682 if (syncGPIODevPath.empty())
683 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500684 // Sync not implemented
Matt Spinlerd734e652018-01-18 14:31:15 -0600685 return;
686 }
687
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500688 GPIO gpio{syncGPIODevPath, static_cast<gpioNum_t>(syncGPIONumber),
Matt Spinlerd734e652018-01-18 14:31:15 -0600689 Direction::output};
690
691 try
692 {
693 gpio.set(Value::low);
694
695 std::this_thread::sleep_for(std::chrono::milliseconds{5});
696
697 gpio.set(Value::high);
698
699 recordManager->clear();
700 }
701 catch (std::exception& e)
702 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500703 // Do nothing. There would already be a journal entry.
Matt Spinlerd734e652018-01-18 14:31:15 -0600704 }
705}
706
Matt Spinler82384142018-01-18 14:15:03 -0600707void PowerSupply::enableHistory(const std::string& objectPath,
708 size_t numRecords,
709 const std::string& syncGPIOPath,
710 size_t syncGPIONum)
711{
712 historyObjectPath = objectPath;
713 syncGPIODevPath = syncGPIOPath;
714 syncGPIONumber = syncGPIONum;
715
716 recordManager = std::make_unique<history::RecordManager>(numRecords);
717
718 auto avgPath = historyObjectPath + '/' + history::Average::name;
719 auto maxPath = historyObjectPath + '/' + history::Maximum::name;
720
721 average = std::make_unique<history::Average>(bus, avgPath);
722
723 maximum = std::make_unique<history::Maximum>(bus, maxPath);
724}
725
Matt Spinlereb169fd2018-01-18 14:19:08 -0600726void PowerSupply::updateHistory()
727{
728 if (!recordManager)
729 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500730 // Not enabled
Matt Spinlereb169fd2018-01-18 14:19:08 -0600731 return;
732 }
733
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500734 // Read just the most recent average/max record
735 auto data =
736 pmbusIntf.readBinary(INPUT_HISTORY, pmbus::Type::HwmonDeviceDebug,
737 history::RecordManager::RAW_RECORD_SIZE);
Matt Spinlereb169fd2018-01-18 14:19:08 -0600738
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500739 // Update D-Bus only if something changed (a new record ID, or cleared out)
Matt Spinlereb169fd2018-01-18 14:19:08 -0600740 auto changed = recordManager->add(data);
741 if (changed)
742 {
743 average->values(std::move(recordManager->getAverageRecords()));
744 maximum->values(std::move(recordManager->getMaximumRecords()));
745 }
746}
747
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500748} // namespace psu
749} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800750} // namespace phosphor