blob: 66d2d9460006f48226098dc72e8feac12d8ab4b1 [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
Jayanth Othayothf9886b92024-12-07 01:26:08 -060044#ifdef __clang__
45#pragma clang diagnostic push
46#pragma clang diagnostic ignored "-Wpessimizing-move"
47#endif
Brandon Wyman10295542017-08-09 18:20:44 -050048PowerSupply::PowerSupply(const std::string& name, size_t inst,
Matt Spinlerf0f02b92018-10-25 16:12:43 -050049 const std::string& objpath, const std::string& invpath,
Patrick Williams7354ce62022-07-22 19:26:56 -050050 sdbusplus::bus_t& bus, const sdeventplus::Event& e,
Matt Spinlerf0f02b92018-10-25 16:12:43 -050051 std::chrono::seconds& t, std::chrono::seconds& p) :
Patrick Williamsf5402192024-08-16 15:20:53 -040052 Device(name, inst), monitorPath(objpath), pmbusIntf(objpath),
Matt Spinlerf0f02b92018-10-25 16:12:43 -050053 inventoryPath(INVENTORY_OBJ_PATH + invpath), bus(bus), presentInterval(p),
54 presentTimer(e, std::bind([this]() {
Patrick Williamsf5402192024-08-16 15:20:53 -040055 // The hwmon path may have changed.
56 pmbusIntf.findHwmonDir();
57 this->present = true;
Matt Spinler234ce0d2018-01-04 15:06:57 -060058
Patrick Williamsf5402192024-08-16 15:20:53 -040059 // Sync the INPUT_HISTORY data for all PSs
60 syncHistory();
Matt Spinlerd734e652018-01-18 14:31:15 -060061
Patrick Williamsf5402192024-08-16 15:20:53 -040062 // Update the inventory for the new device
63 updateInventory();
64 })),
Matt Spinlerf0f02b92018-10-25 16:12:43 -050065 powerOnInterval(t),
66 powerOnTimer(e, std::bind([this]() { this->powerOn = true; }))
Brandon Wyman10295542017-08-09 18:20:44 -050067{
George Liu690e7802019-08-23 11:04:01 +080068 getAccessType();
69
Brandon Wyman10295542017-08-09 18:20:44 -050070 using namespace sdbusplus::bus;
Lei YUab093322019-10-09 16:43:22 +080071 using namespace phosphor::pmbus;
Aatir Manzur817f8a72019-08-07 07:42:15 -050072 std::uint16_t statusWord = 0;
73 try
74 {
75 // Read the 2 byte STATUS_WORD value to check for faults.
76 statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug);
77 if (!((statusWord & status_word::INPUT_FAULT_WARN) ||
78 (statusWord & status_word::VIN_UV_FAULT)))
79 {
80 resolveError(inventoryPath,
81 std::string(PowerSupplyInputFault::errName));
82 }
83 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -050084 catch (const ReadFailure& e)
Aatir Manzur817f8a72019-08-07 07:42:15 -050085 {
86 log<level::INFO>("Unable to read the 2 byte STATUS_WORD value to check "
87 "for power-supply input faults.");
88 }
Matt Spinlerf0f02b92018-10-25 16:12:43 -050089 presentMatch = std::make_unique<match_t>(
90 bus, match::rules::propertiesChanged(inventoryPath, INVENTORY_IFACE),
91 [this](auto& msg) { this->inventoryChanged(msg); });
Brandon Wyman431fbe42017-08-18 16:22:09 -050092 // Get initial presence state.
Brandon Wyman253dc9b2017-08-12 13:45:52 -050093 updatePresence();
Brandon Wyman431fbe42017-08-18 16:22:09 -050094
Matt Spinler234ce0d2018-01-04 15:06:57 -060095 // Write the SN, PN, etc to the inventory
96 updateInventory();
97
Brandon Wyman431fbe42017-08-18 16:22:09 -050098 // Subscribe to power state changes
Matt Spinlerf0f02b92018-10-25 16:12:43 -050099 powerOnMatch = std::make_unique<match_t>(
100 bus, match::rules::propertiesChanged(POWER_OBJ_PATH, POWER_IFACE),
101 [this](auto& msg) { this->powerStateChanged(msg); });
Brandon Wyman431fbe42017-08-18 16:22:09 -0500102 // Get initial power state.
103 updatePowerState();
Brandon Wyman10295542017-08-09 18:20:44 -0500104}
Jayanth Othayothf9886b92024-12-07 01:26:08 -0600105#ifdef __clang__
106#pragma clang diagnostic pop
107#endif
Brandon Wyman442035f2017-08-08 15:58:45 -0500108
George Liu690e7802019-08-23 11:04:01 +0800109void PowerSupply::getAccessType()
110{
Lei YUab093322019-10-09 16:43:22 +0800111 using namespace phosphor::power::util;
George Liu690e7802019-08-23 11:04:01 +0800112 fruJson = loadJSONFromFile(PSU_JSON_PATH);
113 if (fruJson == nullptr)
114 {
115 log<level::ERR>("InternalFailure when parsing the JSON file");
116 return;
117 }
Lei YU40705462019-10-09 17:07:11 +0800118 inventoryPMBusAccessType = getPMBusAccessType(fruJson);
George Liu690e7802019-08-23 11:04:01 +0800119}
120
Brandon Wymana1e96342017-09-25 16:47:44 -0500121void PowerSupply::captureCmd(util::NamesValues& nv, const std::string& cmd,
Lei YUab093322019-10-09 16:43:22 +0800122 phosphor::pmbus::Type type)
Brandon Wymana1e96342017-09-25 16:47:44 -0500123{
124 if (pmbusIntf.exists(cmd, type))
125 {
126 try
127 {
128 auto val = pmbusIntf.read(cmd, type);
129 nv.add(cmd, val);
130 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500131 catch (const std::exception& e)
Brandon Wymana1e96342017-09-25 16:47:44 -0500132 {
Joseph Reynolds5b485962018-05-17 16:05:06 -0500133 log<level::INFO>("Unable to capture metadata",
134 entry("CMD=%s", cmd.c_str()));
Brandon Wymana1e96342017-09-25 16:47:44 -0500135 }
136 }
137}
Brandon Wyman431fbe42017-08-18 16:22:09 -0500138
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500139void PowerSupply::analyze()
140{
Lei YUab093322019-10-09 16:43:22 +0800141 using namespace phosphor::pmbus;
Brandon Wyman442035f2017-08-08 15:58:45 -0500142
143 try
144 {
Brandon Wyman10295542017-08-09 18:20:44 -0500145 if (present)
Brandon Wyman442035f2017-08-08 15:58:45 -0500146 {
Brandon Wyman764c7972017-08-22 17:05:36 -0500147 std::uint16_t statusWord = 0;
Brandon Wyman764c7972017-08-22 17:05:36 -0500148
149 // Read the 2 byte STATUS_WORD value to check for faults.
150 statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug);
Brandon Wymane4af9802017-11-13 15:58:33 -0600151 readFail = 0;
Brandon Wyman764c7972017-08-22 17:05:36 -0500152
Brandon Wyman603cc002017-08-28 18:17:58 -0500153 checkInputFault(statusWord);
Brandon Wyman764c7972017-08-22 17:05:36 -0500154
Brandon Wyman654eb6e2018-02-14 14:55:47 -0600155 if (powerOn && (inputFault == 0) && !faultFound)
Brandon Wyman764c7972017-08-22 17:05:36 -0500156 {
Brandon Wyman12661f12017-08-31 15:28:21 -0500157 checkFanFault(statusWord);
Brandon Wyman875b3632017-09-13 18:46:03 -0500158 checkTemperatureFault(statusWord);
Brandon Wymancfa032b2017-09-25 17:37:50 -0500159 checkOutputOvervoltageFault(statusWord);
160 checkCurrentOutOverCurrentFault(statusWord);
161 checkPGOrUnitOffFault(statusWord);
Brandon Wyman442035f2017-08-08 15:58:45 -0500162 }
Matt Spinlereb169fd2018-01-18 14:19:08 -0600163
164 updateHistory();
Brandon Wyman442035f2017-08-08 15:58:45 -0500165 }
166 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500167 catch (const ReadFailure& e)
Brandon Wyman442035f2017-08-08 15:58:45 -0500168 {
Brandon Wymane4af9802017-11-13 15:58:33 -0600169 if (readFail < FAULT_COUNT)
170 {
171 readFail++;
172 }
173
174 if (!readFailLogged && readFail >= FAULT_COUNT)
Brandon Wyman442035f2017-08-08 15:58:45 -0500175 {
176 commit<ReadFailure>();
177 readFailLogged = true;
Brandon Wyman442035f2017-08-08 15:58:45 -0500178 }
179 }
180
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500181 return;
182}
183
Patrick Williams7354ce62022-07-22 19:26:56 -0500184void PowerSupply::inventoryChanged(sdbusplus::message_t& msg)
Brandon Wyman10295542017-08-09 18:20:44 -0500185{
186 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500187 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wyman10295542017-08-09 18:20:44 -0500188 msg.read(msgSensor, msgData);
189
190 // Check if it was the Present property that changed.
191 auto valPropMap = msgData.find(PRESENT_PROP);
192 if (valPropMap != msgData.end())
193 {
Patrick Williams365d61c2020-05-13 12:23:08 -0500194 if (std::get<bool>(valPropMap->second))
Brandon Wyman10295542017-08-09 18:20:44 -0500195 {
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500196 clearFaults();
William A. Kennington III1a0c9172018-10-18 17:57:49 -0700197 presentTimer.restartOnce(presentInterval);
Brandon Wyman590fc282017-11-01 18:22:25 -0500198 }
199 else
200 {
Brandon Wyman2ef48cf2017-11-21 15:43:54 -0600201 present = false;
William A. Kennington III1a0c9172018-10-18 17:57:49 -0700202 presentTimer.setEnabled(false);
Matt Spinler234ce0d2018-01-04 15:06:57 -0600203
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500204 // Clear out the now outdated inventory properties
Matt Spinler234ce0d2018-01-04 15:06:57 -0600205 updateInventory();
Brandon Wyman10295542017-08-09 18:20:44 -0500206 }
207 }
208
209 return;
210}
211
212void PowerSupply::updatePresence()
213{
214 // Use getProperty utility function to get presence status.
Brandon Wyman10295542017-08-09 18:20:44 -0500215 std::string service = "xyz.openbmc_project.Inventory.Manager";
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500216 util::getProperty(INVENTORY_IFACE, PRESENT_PROP, inventoryPath, service,
217 bus, this->present);
Brandon Wyman10295542017-08-09 18:20:44 -0500218}
219
Patrick Williams7354ce62022-07-22 19:26:56 -0500220void PowerSupply::powerStateChanged(sdbusplus::message_t& msg)
Brandon Wyman431fbe42017-08-18 16:22:09 -0500221{
222 int32_t state = 0;
223 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500224 std::map<std::string, std::variant<int32_t>> msgData;
Brandon Wyman431fbe42017-08-18 16:22:09 -0500225 msg.read(msgSensor, msgData);
226
227 // Check if it was the Present property that changed.
228 auto valPropMap = msgData.find("state");
229 if (valPropMap != msgData.end())
230 {
Patrick Williams365d61c2020-05-13 12:23:08 -0500231 state = std::get<int32_t>(valPropMap->second);
Brandon Wyman431fbe42017-08-18 16:22:09 -0500232
233 // Power is on when state=1. Set the fault logged variables to false
234 // and start the power on timer when the state changes to 1.
235 if (state)
236 {
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500237 clearFaults();
William A. Kennington III1a0c9172018-10-18 17:57:49 -0700238 powerOnTimer.restartOnce(powerOnInterval);
Brandon Wyman431fbe42017-08-18 16:22:09 -0500239 }
240 else
241 {
William A. Kennington III1a0c9172018-10-18 17:57:49 -0700242 powerOnTimer.setEnabled(false);
Brandon Wyman431fbe42017-08-18 16:22:09 -0500243 powerOn = false;
244 }
245 }
Brandon Wyman431fbe42017-08-18 16:22:09 -0500246}
247
248void PowerSupply::updatePowerState()
249{
Lei YUcfc040c2019-10-29 17:10:26 +0800250 powerOn = util::isPoweredOn(bus);
Brandon Wyman431fbe42017-08-18 16:22:09 -0500251}
252
Brandon Wyman603cc002017-08-28 18:17:58 -0500253void PowerSupply::checkInputFault(const uint16_t statusWord)
254{
Lei YUab093322019-10-09 16:43:22 +0800255 using namespace phosphor::pmbus;
Brandon Wyman603cc002017-08-28 18:17:58 -0500256
Brandon Wymana3c675c2017-11-14 14:54:54 -0600257 if ((inputFault < FAULT_COUNT) &&
258 ((statusWord & status_word::INPUT_FAULT_WARN) ||
259 (statusWord & status_word::VIN_UV_FAULT)))
Brandon Wyman603cc002017-08-28 18:17:58 -0500260 {
Brandon Wymanad708242018-01-19 17:28:35 -0600261 if (inputFault == 0)
262 {
263 log<level::INFO>("INPUT or VIN_UV fault",
264 entry("STATUS_WORD=0x%04X", statusWord));
265 }
266
Brandon Wymana3c675c2017-11-14 14:54:54 -0600267 inputFault++;
Brandon Wyman603cc002017-08-28 18:17:58 -0500268 }
269 else
270 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500271 if ((inputFault > 0) && !(statusWord & status_word::INPUT_FAULT_WARN) &&
Brandon Wymand20686a2017-11-01 17:45:23 -0500272 !(statusWord & status_word::VIN_UV_FAULT))
Brandon Wyman603cc002017-08-28 18:17:58 -0500273 {
Brandon Wymana3c675c2017-11-14 14:54:54 -0600274 inputFault = 0;
Brandon Wyman3343e822017-11-03 16:54:11 -0500275 faultFound = false;
Brandon Wyman7502f6c2018-02-12 20:20:39 -0600276 // When an input fault occurs, the power supply cannot be on.
277 // However, the check for the case where the power supply should be
278 // on will stop when there is a fault found.
279 // Clear the powerOnFault when the inputFault is cleared to reset
280 // the powerOnFault de-glitching.
281 powerOnFault = 0;
Brandon Wyman69591bd2017-11-01 18:07:23 -0500282
Brandon Wyman603cc002017-08-28 18:17:58 -0500283 log<level::INFO>("INPUT_FAULT_WARN cleared",
Brandon Wymana3c675c2017-11-14 14:54:54 -0600284 entry("POWERSUPPLY=%s", inventoryPath.c_str()));
Brandon Wyman69591bd2017-11-01 18:07:23 -0500285
Brandon Wyman08b05712017-11-30 17:53:56 -0600286 resolveError(inventoryPath,
287 std::string(PowerSupplyInputFault::errName));
288
Brandon Wyman69591bd2017-11-01 18:07:23 -0500289 if (powerOn)
290 {
291 // The power supply will not be immediately powered on after
292 // the input power is restored.
293 powerOn = false;
294 // Start up the timer that will set the state to indicate we
295 // are ready for the powered on fault checks.
William A. Kennington III1a0c9172018-10-18 17:57:49 -0700296 powerOnTimer.restartOnce(powerOnInterval);
Brandon Wyman69591bd2017-11-01 18:07:23 -0500297 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500298 }
299 }
Brandon Wymana3c675c2017-11-14 14:54:54 -0600300
301 if (!faultFound && (inputFault >= FAULT_COUNT))
302 {
Brandon Wymanad708242018-01-19 17:28:35 -0600303 // If the power is on, report the fault in an error log entry.
304 if (powerOn)
305 {
306 util::NamesValues nv;
307 nv.add("STATUS_WORD", statusWord);
308 captureCmd(nv, STATUS_INPUT, Type::Debug);
Brandon Wymana3c675c2017-11-14 14:54:54 -0600309
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500310 using metadata =
311 org::open_power::Witherspoon::Fault::PowerSupplyInputFault;
Brandon Wymana3c675c2017-11-14 14:54:54 -0600312
Brandon Wymanad708242018-01-19 17:28:35 -0600313 report<PowerSupplyInputFault>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500314 metadata::RAW_STATUS(nv.get().c_str()),
315 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wymanad708242018-01-19 17:28:35 -0600316
317 faultFound = true;
318 }
Brandon Wymana3c675c2017-11-14 14:54:54 -0600319 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500320}
321
322void PowerSupply::checkPGOrUnitOffFault(const uint16_t statusWord)
323{
Lei YUab093322019-10-09 16:43:22 +0800324 using namespace phosphor::pmbus;
Brandon Wyman603cc002017-08-28 18:17:58 -0500325
Brandon Wyman593d24f2017-10-13 18:15:23 -0500326 if (powerOnFault < FAULT_COUNT)
Brandon Wyman603cc002017-08-28 18:17:58 -0500327 {
Brandon Wyman593d24f2017-10-13 18:15:23 -0500328 // Check PG# and UNIT_IS_OFF
329 if ((statusWord & status_word::POWER_GOOD_NEGATED) ||
330 (statusWord & status_word::UNIT_IS_OFF))
331 {
332 log<level::INFO>("PGOOD or UNIT_IS_OFF bit bad",
333 entry("STATUS_WORD=0x%04X", statusWord));
334 powerOnFault++;
335 }
336 else
337 {
338 if (powerOnFault > 0)
339 {
340 log<level::INFO>("PGOOD and UNIT_IS_OFF bits good");
341 powerOnFault = 0;
342 }
343 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500344
Brandon Wymane2fc7aa2017-11-13 17:37:10 -0600345 if (!faultFound && (powerOnFault >= FAULT_COUNT))
Brandon Wyman593d24f2017-10-13 18:15:23 -0500346 {
Brandon Wyman3343e822017-11-03 16:54:11 -0500347 faultFound = true;
348
Brandon Wyman593d24f2017-10-13 18:15:23 -0500349 util::NamesValues nv;
350 nv.add("STATUS_WORD", statusWord);
351 captureCmd(nv, STATUS_INPUT, Type::Debug);
352 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
353 captureCmd(nv, status0Vout, Type::Debug);
354 captureCmd(nv, STATUS_IOUT, Type::Debug);
355 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wyman603cc002017-08-28 18:17:58 -0500356
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500357 using metadata =
358 org::open_power::Witherspoon::Fault::PowerSupplyShouldBeOn;
Brandon Wyman603cc002017-08-28 18:17:58 -0500359
Brandon Wyman593d24f2017-10-13 18:15:23 -0500360 // A power supply is OFF (or pgood low) but should be on.
361 report<PowerSupplyShouldBeOn>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500362 metadata::RAW_STATUS(nv.get().c_str()),
363 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wyman593d24f2017-10-13 18:15:23 -0500364 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500365 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500366}
367
368void PowerSupply::checkCurrentOutOverCurrentFault(const uint16_t statusWord)
369{
Lei YUab093322019-10-09 16:43:22 +0800370 using namespace phosphor::pmbus;
Brandon Wyman603cc002017-08-28 18:17:58 -0500371
Brandon Wymandd61be42017-11-07 18:38:54 -0600372 if (outputOCFault < FAULT_COUNT)
Brandon Wyman603cc002017-08-28 18:17:58 -0500373 {
Brandon Wymandd61be42017-11-07 18:38:54 -0600374 // Check for an output overcurrent fault.
375 if ((statusWord & status_word::IOUT_OC_FAULT))
376 {
377 outputOCFault++;
378 }
379 else
380 {
381 if (outputOCFault > 0)
382 {
383 outputOCFault = 0;
384 }
385 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500386
Brandon Wymane2fc7aa2017-11-13 17:37:10 -0600387 if (!faultFound && (outputOCFault >= FAULT_COUNT))
Brandon Wymandd61be42017-11-07 18:38:54 -0600388 {
389 util::NamesValues nv;
390 nv.add("STATUS_WORD", statusWord);
391 captureCmd(nv, STATUS_INPUT, Type::Debug);
392 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
393 captureCmd(nv, status0Vout, Type::Debug);
394 captureCmd(nv, STATUS_IOUT, Type::Debug);
395 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wyman603cc002017-08-28 18:17:58 -0500396
Brandon Wymandd61be42017-11-07 18:38:54 -0600397 using metadata = org::open_power::Witherspoon::Fault::
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500398 PowerSupplyOutputOvercurrent;
Brandon Wyman603cc002017-08-28 18:17:58 -0500399
Brandon Wymandd61be42017-11-07 18:38:54 -0600400 report<PowerSupplyOutputOvercurrent>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500401 metadata::RAW_STATUS(nv.get().c_str()),
402 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wymandd61be42017-11-07 18:38:54 -0600403
404 faultFound = true;
405 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500406 }
407}
408
Brandon Wymanab05c072017-08-30 18:26:41 -0500409void PowerSupply::checkOutputOvervoltageFault(const uint16_t statusWord)
410{
Lei YUab093322019-10-09 16:43:22 +0800411 using namespace phosphor::pmbus;
Brandon Wymanab05c072017-08-30 18:26:41 -0500412
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600413 if (outputOVFault < FAULT_COUNT)
Brandon Wymanab05c072017-08-30 18:26:41 -0500414 {
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600415 // Check for an output overvoltage fault.
416 if (statusWord & status_word::VOUT_OV_FAULT)
417 {
418 outputOVFault++;
419 }
420 else
421 {
422 if (outputOVFault > 0)
423 {
424 outputOVFault = 0;
425 }
426 }
Brandon Wymanab05c072017-08-30 18:26:41 -0500427
Brandon Wymane2fc7aa2017-11-13 17:37:10 -0600428 if (!faultFound && (outputOVFault >= FAULT_COUNT))
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600429 {
430 util::NamesValues nv;
431 nv.add("STATUS_WORD", statusWord);
432 captureCmd(nv, STATUS_INPUT, Type::Debug);
433 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
434 captureCmd(nv, status0Vout, Type::Debug);
435 captureCmd(nv, STATUS_IOUT, Type::Debug);
436 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wymanab05c072017-08-30 18:26:41 -0500437
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600438 using metadata = org::open_power::Witherspoon::Fault::
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500439 PowerSupplyOutputOvervoltage;
Brandon Wymanab05c072017-08-30 18:26:41 -0500440
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600441 report<PowerSupplyOutputOvervoltage>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500442 metadata::RAW_STATUS(nv.get().c_str()),
443 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600444
445 faultFound = true;
446 }
Brandon Wymanab05c072017-08-30 18:26:41 -0500447 }
448}
449
Brandon Wyman12661f12017-08-31 15:28:21 -0500450void PowerSupply::checkFanFault(const uint16_t statusWord)
451{
Lei YUab093322019-10-09 16:43:22 +0800452 using namespace phosphor::pmbus;
Brandon Wyman12661f12017-08-31 15:28:21 -0500453
Brandon Wymanba255532017-11-08 17:44:10 -0600454 if (fanFault < FAULT_COUNT)
Brandon Wyman12661f12017-08-31 15:28:21 -0500455 {
Brandon Wymanba255532017-11-08 17:44:10 -0600456 // Check for a fan fault or warning condition
457 if (statusWord & status_word::FAN_FAULT)
458 {
459 fanFault++;
460 }
461 else
462 {
463 if (fanFault > 0)
464 {
465 fanFault = 0;
466 }
467 }
Brandon Wyman12661f12017-08-31 15:28:21 -0500468
Brandon Wymane2fc7aa2017-11-13 17:37:10 -0600469 if (!faultFound && (fanFault >= FAULT_COUNT))
Brandon Wymanba255532017-11-08 17:44:10 -0600470 {
471 util::NamesValues nv;
472 nv.add("STATUS_WORD", statusWord);
473 captureCmd(nv, STATUS_MFR, Type::Debug);
474 captureCmd(nv, STATUS_TEMPERATURE, Type::Debug);
475 captureCmd(nv, STATUS_FANS_1_2, Type::Debug);
Brandon Wyman12661f12017-08-31 15:28:21 -0500476
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500477 using metadata =
478 org::open_power::Witherspoon::Fault::PowerSupplyFanFault;
Brandon Wyman12661f12017-08-31 15:28:21 -0500479
Brandon Wymanba255532017-11-08 17:44:10 -0600480 report<PowerSupplyFanFault>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500481 metadata::RAW_STATUS(nv.get().c_str()),
482 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wymanba255532017-11-08 17:44:10 -0600483
484 faultFound = true;
485 }
Brandon Wyman12661f12017-08-31 15:28:21 -0500486 }
487}
488
Brandon Wyman875b3632017-09-13 18:46:03 -0500489void PowerSupply::checkTemperatureFault(const uint16_t statusWord)
490{
Lei YUab093322019-10-09 16:43:22 +0800491 using namespace phosphor::pmbus;
Brandon Wyman875b3632017-09-13 18:46:03 -0500492
493 // Due to how the PMBus core device driver sends a clear faults command
494 // the bit in STATUS_WORD will likely be cleared when we attempt to examine
495 // it for a Thermal Fault or Warning. So, check the STATUS_WORD and the
496 // STATUS_TEMPERATURE bits. If either indicates a fault, proceed with
497 // logging the over-temperature condition.
498 std::uint8_t statusTemperature = 0;
499 statusTemperature = pmbusIntf.read(STATUS_TEMPERATURE, Type::Debug);
Brandon Wyman50044ea2017-11-08 17:58:56 -0600500 if (temperatureFault < FAULT_COUNT)
Brandon Wyman875b3632017-09-13 18:46:03 -0500501 {
Brandon Wyman50044ea2017-11-08 17:58:56 -0600502 if ((statusWord & status_word::TEMPERATURE_FAULT_WARN) ||
503 (statusTemperature & status_temperature::OT_FAULT))
504 {
505 temperatureFault++;
506 }
507 else
508 {
509 if (temperatureFault > 0)
510 {
511 temperatureFault = 0;
512 }
513 }
Brandon Wyman875b3632017-09-13 18:46:03 -0500514
Brandon Wymane2fc7aa2017-11-13 17:37:10 -0600515 if (!faultFound && (temperatureFault >= FAULT_COUNT))
Brandon Wyman50044ea2017-11-08 17:58:56 -0600516 {
517 // The power supply has had an over-temperature condition.
518 // This may not result in a shutdown if experienced for a short
519 // duration.
520 // This should not occur under normal conditions.
521 // The power supply may be faulty, or the paired supply may be
522 // putting out less current.
523 // Capture command responses with potentially relevant information,
524 // and call out the power supply reporting the condition.
525 util::NamesValues nv;
526 nv.add("STATUS_WORD", statusWord);
527 captureCmd(nv, STATUS_MFR, Type::Debug);
528 captureCmd(nv, STATUS_IOUT, Type::Debug);
529 nv.add("STATUS_TEMPERATURE", statusTemperature);
530 captureCmd(nv, STATUS_FANS_1_2, Type::Debug);
Brandon Wyman875b3632017-09-13 18:46:03 -0500531
Brandon Wyman50044ea2017-11-08 17:58:56 -0600532 using metadata = org::open_power::Witherspoon::Fault::
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500533 PowerSupplyTemperatureFault;
Brandon Wyman875b3632017-09-13 18:46:03 -0500534
Brandon Wyman50044ea2017-11-08 17:58:56 -0600535 report<PowerSupplyTemperatureFault>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500536 metadata::RAW_STATUS(nv.get().c_str()),
537 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wyman50044ea2017-11-08 17:58:56 -0600538
539 faultFound = true;
540 }
Brandon Wyman875b3632017-09-13 18:46:03 -0500541 }
542}
543
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500544void PowerSupply::clearFaults()
545{
Brandon Wymane4af9802017-11-13 15:58:33 -0600546 readFail = 0;
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500547 readFailLogged = false;
Brandon Wymana3c675c2017-11-14 14:54:54 -0600548 inputFault = 0;
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500549 powerOnFault = 0;
Brandon Wymandd61be42017-11-07 18:38:54 -0600550 outputOCFault = 0;
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600551 outputOVFault = 0;
Brandon Wymanba255532017-11-08 17:44:10 -0600552 fanFault = 0;
Brandon Wyman50044ea2017-11-08 17:58:56 -0600553 temperatureFault = 0;
Brandon Wyman3343e822017-11-03 16:54:11 -0500554 faultFound = false;
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500555
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500556 return;
557}
558
Brandon Wyman43ce2082017-11-30 17:24:01 -0600559void PowerSupply::resolveError(const std::string& callout,
560 const std::string& message)
561{
Brandon Wyman01741f12017-12-01 17:22:08 -0600562 using EndpointList = std::vector<std::string>;
563
564 try
565 {
566 auto path = callout + "/fault";
567 // Get the service name from the mapper for the fault callout
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500568 auto service = util::getService(path, ASSOCIATION_IFACE, bus);
Brandon Wyman01741f12017-12-01 17:22:08 -0600569
570 // Use getProperty utility function to get log entries (endpoints)
571 EndpointList logEntries;
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500572 util::getProperty(ASSOCIATION_IFACE, ENDPOINTS_PROP, path, service, bus,
573 logEntries);
Brandon Wyman01741f12017-12-01 17:22:08 -0600574
575 // It is possible that all such entries for this callout have since
576 // been deleted.
577 if (logEntries.empty())
578 {
579 return;
580 }
581
Patrick Williamsf5402192024-08-16 15:20:53 -0400582 auto logEntryService =
583 util::getService(logEntries[0], LOGGING_IFACE, bus);
Brandon Wyman01741f12017-12-01 17:22:08 -0600584 if (logEntryService.empty())
585 {
586 return;
587 }
588
589 // go through each log entry that matches this callout path
590 std::string logMessage;
591 for (const auto& logEntry : logEntries)
592 {
593 // Check to see if this logEntry has a message that matches.
Matt Spinler589e8722018-01-04 15:24:49 -0600594 util::getProperty(LOGGING_IFACE, MESSAGE_PROP, logEntry,
Brandon Wyman01741f12017-12-01 17:22:08 -0600595 logEntryService, bus, logMessage);
596
597 if (message == logMessage)
598 {
599 // Log entry matches call out and message, set Resolved to true
600 bool resolved = true;
Matt Spinler589e8722018-01-04 15:24:49 -0600601 util::setProperty(LOGGING_IFACE, RESOLVED_PROP, logEntry,
Brandon Wyman01741f12017-12-01 17:22:08 -0600602 logEntryService, bus, resolved);
603 }
Brandon Wyman01741f12017-12-01 17:22:08 -0600604 }
Brandon Wyman01741f12017-12-01 17:22:08 -0600605 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500606 catch (const std::exception& e)
Brandon Wyman01741f12017-12-01 17:22:08 -0600607 {
608 log<level::INFO>("Failed to resolve error",
609 entry("CALLOUT=%s", callout.c_str()),
Matt Spinler0d09f292018-01-22 14:51:26 -0600610 entry("ERROR=%s", message.c_str()));
Brandon Wyman01741f12017-12-01 17:22:08 -0600611 }
Brandon Wyman43ce2082017-11-30 17:24:01 -0600612}
613
Matt Spinler234ce0d2018-01-04 15:06:57 -0600614void PowerSupply::updateInventory()
615{
Lei YUab093322019-10-09 16:43:22 +0800616 using namespace phosphor::pmbus;
Matt Spinler018a7bc2018-01-04 15:36:41 -0600617 using namespace sdbusplus::message;
618
Matt Spinler018a7bc2018-01-04 15:36:41 -0600619 // Build the object map and send it to the inventory
George Liu070c1bc2020-10-12 11:28:01 +0800620 using Properties = std::map<std::string, std::variant<std::string, bool>>;
Matt Spinler018a7bc2018-01-04 15:36:41 -0600621 using Interfaces = std::map<std::string, Properties>;
622 using Object = std::map<object_path, Interfaces>;
623 Properties assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800624 Properties operProps;
Matt Spinler018a7bc2018-01-04 15:36:41 -0600625 Interfaces interfaces;
626 Object object;
627
George Liu690e7802019-08-23 11:04:01 +0800628 // If any of these accesses fail, the fields will just be
629 // blank in the inventory. Leave logging ReadFailure errors
630 // to analyze() as it runs continuously and will most
631 // likely hit and threshold them first anyway. The
632 // readString() function will do the tracing of the failing
633 // path so this code doesn't need to.
634 for (const auto& fru : fruJson.at("fruConfigs"))
635 {
636 if (fru.at("interface") == ASSET_IFACE)
637 {
638 try
639 {
640 assetProps.emplace(
641 fru.at("propertyName"),
642 present ? pmbusIntf.readString(fru.at("fileName"),
643 inventoryPMBusAccessType)
644 : "");
645 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500646 catch (const ReadFailure& e)
Adriana Kobylak0c9a33d2021-09-13 18:05:09 +0000647 {}
George Liu690e7802019-08-23 11:04:01 +0800648 }
George Liu690e7802019-08-23 11:04:01 +0800649 }
Matt Spinler018a7bc2018-01-04 15:36:41 -0600650
George Liu070c1bc2020-10-12 11:28:01 +0800651 operProps.emplace(FUNCTIONAL_PROP, present);
George Liu690e7802019-08-23 11:04:01 +0800652 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
George Liu070c1bc2020-10-12 11:28:01 +0800653 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
Matt Spinler018a7bc2018-01-04 15:36:41 -0600654
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500655 // For Notify(), just send the relative path of the inventory
656 // object so remove the INVENTORY_OBJ_PATH prefix
Matt Spinler018a7bc2018-01-04 15:36:41 -0600657 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
658
659 object.emplace(path, std::move(interfaces));
660
661 try
662 {
Patrick Williamsf5402192024-08-16 15:20:53 -0400663 auto service =
664 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
Matt Spinler018a7bc2018-01-04 15:36:41 -0600665
666 if (service.empty())
667 {
668 log<level::ERR>("Unable to get inventory manager service");
669 return;
670 }
671
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500672 auto method = bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
673 INVENTORY_MGR_IFACE, "Notify");
Matt Spinler018a7bc2018-01-04 15:36:41 -0600674
675 method.append(std::move(object));
676
677 auto reply = bus.call(method);
Matt Spinler018a7bc2018-01-04 15:36:41 -0600678 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500679 catch (const std::exception& e)
Matt Spinler018a7bc2018-01-04 15:36:41 -0600680 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500681 log<level::ERR>(e.what(), entry("PATH=%s", inventoryPath.c_str()));
Matt Spinler018a7bc2018-01-04 15:36:41 -0600682 }
Matt Spinler234ce0d2018-01-04 15:06:57 -0600683}
684
Matt Spinlerd734e652018-01-18 14:31:15 -0600685void PowerSupply::syncHistory()
686{
Lei YUab093322019-10-09 16:43:22 +0800687 using namespace phosphor::gpio;
Matt Spinlerd734e652018-01-18 14:31:15 -0600688
689 if (syncGPIODevPath.empty())
690 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500691 // Sync not implemented
Matt Spinlerd734e652018-01-18 14:31:15 -0600692 return;
693 }
694
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500695 GPIO gpio{syncGPIODevPath, static_cast<gpioNum_t>(syncGPIONumber),
Matt Spinlerd734e652018-01-18 14:31:15 -0600696 Direction::output};
697
698 try
699 {
700 gpio.set(Value::low);
701
702 std::this_thread::sleep_for(std::chrono::milliseconds{5});
703
704 gpio.set(Value::high);
705
706 recordManager->clear();
707 }
Patrick Williamsc1d4de52021-10-06 12:45:57 -0500708 catch (const std::exception& e)
Matt Spinlerd734e652018-01-18 14:31:15 -0600709 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500710 // Do nothing. There would already be a journal entry.
Matt Spinlerd734e652018-01-18 14:31:15 -0600711 }
712}
713
Patrick Williamsf5402192024-08-16 15:20:53 -0400714void PowerSupply::enableHistory(
715 const std::string& objectPath, size_t numRecords,
716 const std::string& syncGPIOPath, size_t syncGPIONum)
Matt Spinler82384142018-01-18 14:15:03 -0600717{
718 historyObjectPath = objectPath;
719 syncGPIODevPath = syncGPIOPath;
720 syncGPIONumber = syncGPIONum;
721
722 recordManager = std::make_unique<history::RecordManager>(numRecords);
723
724 auto avgPath = historyObjectPath + '/' + history::Average::name;
725 auto maxPath = historyObjectPath + '/' + history::Maximum::name;
726
727 average = std::make_unique<history::Average>(bus, avgPath);
728
729 maximum = std::make_unique<history::Maximum>(bus, maxPath);
730}
731
Matt Spinlereb169fd2018-01-18 14:19:08 -0600732void PowerSupply::updateHistory()
733{
734 if (!recordManager)
735 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500736 // Not enabled
Matt Spinlereb169fd2018-01-18 14:19:08 -0600737 return;
738 }
739
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500740 // Read just the most recent average/max record
Patrick Williamsf5402192024-08-16 15:20:53 -0400741 auto data =
742 pmbusIntf.readBinary(INPUT_HISTORY, pmbus::Type::HwmonDeviceDebug,
743 history::RecordManager::RAW_RECORD_SIZE);
Matt Spinlereb169fd2018-01-18 14:19:08 -0600744
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500745 // Update D-Bus only if something changed (a new record ID, or cleared out)
Matt Spinlereb169fd2018-01-18 14:19:08 -0600746 auto changed = recordManager->add(data);
747 if (changed)
748 {
Patrick Williams41a6b9f2023-05-31 19:54:59 -0500749 average->values(recordManager->getAverageRecords());
750 maximum->values(recordManager->getMaximumRecords());
Matt Spinlereb169fd2018-01-18 14:19:08 -0600751 }
752}
753
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500754} // namespace psu
755} // namespace power
Lei YUab093322019-10-09 16:43:22 +0800756} // namespace phosphor