blob: 62827ed19bd755839e8f9f728b5a4f9810309c34 [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 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050016#include "power_supply.hpp"
17
Brandon Wyman442035f2017-08-08 15:58:45 -050018#include "elog-errors.hpp"
Matt Spinlerd734e652018-01-18 14:31:15 -060019#include "gpio.hpp"
Brandon Wyman10295542017-08-09 18:20:44 -050020#include "names_values.hpp"
Brandon Wyman442035f2017-08-08 15:58:45 -050021#include "pmbus.hpp"
22#include "utility.hpp"
23
Matt Spinlerf0f02b92018-10-25 16:12:43 -050024#include <functional>
25#include <org/open_power/Witherspoon/Fault/error.hpp>
26#include <phosphor-logging/elog.hpp>
27#include <phosphor-logging/log.hpp>
28#include <xyz/openbmc_project/Common/Device/error.hpp>
29#include <xyz/openbmc_project/Software/Version/server.hpp>
30
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050031namespace witherspoon
Brandon Wyman24e422f2017-07-25 19:40:14 -050032{
33namespace power
34{
35namespace psu
36{
37
Matt Spinler589e8722018-01-04 15:24:49 -060038using namespace phosphor::logging;
39using namespace sdbusplus::org::open_power::Witherspoon::Fault::Error;
40using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
Matt Spinler018a7bc2018-01-04 15:36:41 -060041namespace version = sdbusplus::xyz::openbmc_project::Software::server;
Matt Spinler589e8722018-01-04 15:24:49 -060042
43constexpr auto ASSOCIATION_IFACE = "org.openbmc.Association";
44constexpr auto LOGGING_IFACE = "xyz.openbmc_project.Logging.Entry";
45constexpr auto INVENTORY_IFACE = "xyz.openbmc_project.Inventory.Item";
46constexpr auto POWER_IFACE = "org.openbmc.control.Power";
Matt Spinler018a7bc2018-01-04 15:36:41 -060047constexpr auto INVENTORY_MGR_IFACE = "xyz.openbmc_project.Inventory.Manager";
48constexpr auto ASSET_IFACE = "xyz.openbmc_project.Inventory.Decorator.Asset";
49constexpr auto VERSION_IFACE = "xyz.openbmc_project.Software.Version";
Matt Spinler589e8722018-01-04 15:24:49 -060050
51constexpr auto ENDPOINTS_PROP = "endpoints";
52constexpr auto MESSAGE_PROP = "Message";
53constexpr auto RESOLVED_PROP = "Resolved";
Brandon Wyman10295542017-08-09 18:20:44 -050054constexpr auto PRESENT_PROP = "Present";
Matt Spinler018a7bc2018-01-04 15:36:41 -060055constexpr auto SN_PROP = "SerialNumber";
56constexpr auto PN_PROP = "PartNumber";
57constexpr auto MODEL_PROP = "Model";
58constexpr auto VERSION_PROP = "Version";
59constexpr auto VERSION_PURPOSE_PROP = "Purpose";
Matt Spinler589e8722018-01-04 15:24:49 -060060
61constexpr auto INVENTORY_OBJ_PATH = "/xyz/openbmc_project/inventory";
Brandon Wyman431fbe42017-08-18 16:22:09 -050062constexpr auto POWER_OBJ_PATH = "/org/openbmc/control/power0";
Brandon Wyman10295542017-08-09 18:20:44 -050063
Matt Spinler018a7bc2018-01-04 15:36:41 -060064constexpr auto SERIAL_NUMBER = "serial_number";
65constexpr auto PART_NUMBER = "part_number";
66constexpr auto FW_VERSION = "fw_version";
67constexpr auto CCIN = "ccin";
Matt Spinlereb169fd2018-01-18 14:19:08 -060068constexpr auto INPUT_HISTORY = "input_history";
Matt Spinler018a7bc2018-01-04 15:36:41 -060069
Brandon Wyman10295542017-08-09 18:20:44 -050070PowerSupply::PowerSupply(const std::string& name, size_t inst,
Matt Spinlerf0f02b92018-10-25 16:12:43 -050071 const std::string& objpath, const std::string& invpath,
72 sdbusplus::bus::bus& bus, const sdeventplus::Event& e,
73 std::chrono::seconds& t, std::chrono::seconds& p) :
74 Device(name, inst),
75 monitorPath(objpath), pmbusIntf(objpath),
76 inventoryPath(INVENTORY_OBJ_PATH + invpath), bus(bus), presentInterval(p),
77 presentTimer(e, std::bind([this]() {
78 // The hwmon path may have changed.
79 pmbusIntf.findHwmonDir();
80 this->present = true;
Matt Spinler234ce0d2018-01-04 15:06:57 -060081
Matt Spinlerf0f02b92018-10-25 16:12:43 -050082 // Sync the INPUT_HISTORY data for all PSs
83 syncHistory();
Matt Spinlerd734e652018-01-18 14:31:15 -060084
Matt Spinlerf0f02b92018-10-25 16:12:43 -050085 // Update the inventory for the new device
86 updateInventory();
87 })),
88 powerOnInterval(t),
89 powerOnTimer(e, std::bind([this]() { this->powerOn = true; }))
Brandon Wyman10295542017-08-09 18:20:44 -050090{
Brandon Wyman10295542017-08-09 18:20:44 -050091 using namespace sdbusplus::bus;
Matt Spinlerf0f02b92018-10-25 16:12:43 -050092 presentMatch = std::make_unique<match_t>(
93 bus, match::rules::propertiesChanged(inventoryPath, INVENTORY_IFACE),
94 [this](auto& msg) { this->inventoryChanged(msg); });
Brandon Wyman431fbe42017-08-18 16:22:09 -050095 // Get initial presence state.
Brandon Wyman253dc9b2017-08-12 13:45:52 -050096 updatePresence();
Brandon Wyman431fbe42017-08-18 16:22:09 -050097
Matt Spinler234ce0d2018-01-04 15:06:57 -060098 // Write the SN, PN, etc to the inventory
99 updateInventory();
100
Brandon Wyman431fbe42017-08-18 16:22:09 -0500101 // Subscribe to power state changes
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500102 powerOnMatch = std::make_unique<match_t>(
103 bus, match::rules::propertiesChanged(POWER_OBJ_PATH, POWER_IFACE),
104 [this](auto& msg) { this->powerStateChanged(msg); });
Brandon Wyman431fbe42017-08-18 16:22:09 -0500105 // Get initial power state.
106 updatePowerState();
Brandon Wyman10295542017-08-09 18:20:44 -0500107}
Brandon Wyman442035f2017-08-08 15:58:45 -0500108
Brandon Wymana1e96342017-09-25 16:47:44 -0500109void PowerSupply::captureCmd(util::NamesValues& nv, const std::string& cmd,
110 witherspoon::pmbus::Type type)
111{
112 if (pmbusIntf.exists(cmd, type))
113 {
114 try
115 {
116 auto val = pmbusIntf.read(cmd, type);
117 nv.add(cmd, val);
118 }
119 catch (std::exception& e)
120 {
Joseph Reynolds5b485962018-05-17 16:05:06 -0500121 log<level::INFO>("Unable to capture metadata",
122 entry("CMD=%s", cmd.c_str()));
Brandon Wymana1e96342017-09-25 16:47:44 -0500123 }
124 }
125}
Brandon Wyman431fbe42017-08-18 16:22:09 -0500126
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500127void PowerSupply::analyze()
128{
Brandon Wyman442035f2017-08-08 15:58:45 -0500129 using namespace witherspoon::pmbus;
130
131 try
132 {
Brandon Wyman10295542017-08-09 18:20:44 -0500133 if (present)
Brandon Wyman442035f2017-08-08 15:58:45 -0500134 {
Brandon Wyman764c7972017-08-22 17:05:36 -0500135 std::uint16_t statusWord = 0;
Brandon Wyman764c7972017-08-22 17:05:36 -0500136
137 // Read the 2 byte STATUS_WORD value to check for faults.
138 statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug);
Brandon Wymane4af9802017-11-13 15:58:33 -0600139 readFail = 0;
Brandon Wyman764c7972017-08-22 17:05:36 -0500140
Brandon Wyman603cc002017-08-28 18:17:58 -0500141 checkInputFault(statusWord);
Brandon Wyman764c7972017-08-22 17:05:36 -0500142
Brandon Wyman654eb6e2018-02-14 14:55:47 -0600143 if (powerOn && (inputFault == 0) && !faultFound)
Brandon Wyman764c7972017-08-22 17:05:36 -0500144 {
Brandon Wyman12661f12017-08-31 15:28:21 -0500145 checkFanFault(statusWord);
Brandon Wyman875b3632017-09-13 18:46:03 -0500146 checkTemperatureFault(statusWord);
Brandon Wymancfa032b2017-09-25 17:37:50 -0500147 checkOutputOvervoltageFault(statusWord);
148 checkCurrentOutOverCurrentFault(statusWord);
149 checkPGOrUnitOffFault(statusWord);
Brandon Wyman442035f2017-08-08 15:58:45 -0500150 }
Matt Spinlereb169fd2018-01-18 14:19:08 -0600151
152 updateHistory();
Brandon Wyman442035f2017-08-08 15:58:45 -0500153 }
154 }
155 catch (ReadFailure& e)
156 {
Brandon Wymane4af9802017-11-13 15:58:33 -0600157 if (readFail < FAULT_COUNT)
158 {
159 readFail++;
160 }
161
162 if (!readFailLogged && readFail >= FAULT_COUNT)
Brandon Wyman442035f2017-08-08 15:58:45 -0500163 {
164 commit<ReadFailure>();
165 readFailLogged = true;
Brandon Wyman442035f2017-08-08 15:58:45 -0500166 }
167 }
168
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500169 return;
170}
171
Brandon Wyman10295542017-08-09 18:20:44 -0500172void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
173{
174 std::string msgSensor;
175 std::map<std::string, sdbusplus::message::variant<uint32_t, bool>> msgData;
176 msg.read(msgSensor, msgData);
177
178 // Check if it was the Present property that changed.
179 auto valPropMap = msgData.find(PRESENT_PROP);
180 if (valPropMap != msgData.end())
181 {
Brandon Wyman2ef48cf2017-11-21 15:43:54 -0600182 if (sdbusplus::message::variant_ns::get<bool>(valPropMap->second))
Brandon Wyman10295542017-08-09 18:20:44 -0500183 {
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500184 clearFaults();
William A. Kennington III1a0c9172018-10-18 17:57:49 -0700185 presentTimer.restartOnce(presentInterval);
Brandon Wyman590fc282017-11-01 18:22:25 -0500186 }
187 else
188 {
Brandon Wyman2ef48cf2017-11-21 15:43:54 -0600189 present = false;
William A. Kennington III1a0c9172018-10-18 17:57:49 -0700190 presentTimer.setEnabled(false);
Matt Spinler234ce0d2018-01-04 15:06:57 -0600191
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500192 // Clear out the now outdated inventory properties
Matt Spinler234ce0d2018-01-04 15:06:57 -0600193 updateInventory();
Brandon Wyman10295542017-08-09 18:20:44 -0500194 }
195 }
196
197 return;
198}
199
200void PowerSupply::updatePresence()
201{
202 // Use getProperty utility function to get presence status.
Brandon Wyman10295542017-08-09 18:20:44 -0500203 std::string service = "xyz.openbmc_project.Inventory.Manager";
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500204 util::getProperty(INVENTORY_IFACE, PRESENT_PROP, inventoryPath, service,
205 bus, this->present);
Brandon Wyman10295542017-08-09 18:20:44 -0500206}
207
Brandon Wyman431fbe42017-08-18 16:22:09 -0500208void PowerSupply::powerStateChanged(sdbusplus::message::message& msg)
209{
210 int32_t state = 0;
211 std::string msgSensor;
William A. Kennington IIIac9d5c32018-11-12 14:58:39 -0800212 std::map<std::string, sdbusplus::message::variant<int32_t>> msgData;
Brandon Wyman431fbe42017-08-18 16:22:09 -0500213 msg.read(msgSensor, msgData);
214
215 // Check if it was the Present property that changed.
216 auto valPropMap = msgData.find("state");
217 if (valPropMap != msgData.end())
218 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500219 state =
220 sdbusplus::message::variant_ns::get<int32_t>(valPropMap->second);
Brandon Wyman431fbe42017-08-18 16:22:09 -0500221
222 // Power is on when state=1. Set the fault logged variables to false
223 // and start the power on timer when the state changes to 1.
224 if (state)
225 {
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500226 clearFaults();
William A. Kennington III1a0c9172018-10-18 17:57:49 -0700227 powerOnTimer.restartOnce(powerOnInterval);
Brandon Wyman431fbe42017-08-18 16:22:09 -0500228 }
229 else
230 {
William A. Kennington III1a0c9172018-10-18 17:57:49 -0700231 powerOnTimer.setEnabled(false);
Brandon Wyman431fbe42017-08-18 16:22:09 -0500232 powerOn = false;
233 }
234 }
Brandon Wyman431fbe42017-08-18 16:22:09 -0500235}
236
237void PowerSupply::updatePowerState()
238{
239 // When state = 1, system is powered on
240 int32_t state = 0;
241
242 try
243 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500244 auto service = util::getService(POWER_OBJ_PATH, POWER_IFACE, bus);
Brandon Wyman431fbe42017-08-18 16:22:09 -0500245
246 // Use getProperty utility function to get power state.
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500247 util::getProperty<int32_t>(POWER_IFACE, "state", POWER_OBJ_PATH,
248 service, bus, state);
Brandon Wyman431fbe42017-08-18 16:22:09 -0500249
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 }
Brandon Wyman431fbe42017-08-18 16:22:09 -0500264}
265
Brandon Wyman603cc002017-08-28 18:17:58 -0500266void PowerSupply::checkInputFault(const uint16_t statusWord)
267{
268 using namespace witherspoon::pmbus;
269
Brandon Wymana3c675c2017-11-14 14:54:54 -0600270 if ((inputFault < FAULT_COUNT) &&
271 ((statusWord & status_word::INPUT_FAULT_WARN) ||
272 (statusWord & status_word::VIN_UV_FAULT)))
Brandon Wyman603cc002017-08-28 18:17:58 -0500273 {
Brandon Wymanad708242018-01-19 17:28:35 -0600274 if (inputFault == 0)
275 {
276 log<level::INFO>("INPUT or VIN_UV fault",
277 entry("STATUS_WORD=0x%04X", statusWord));
278 }
279
Brandon Wymana3c675c2017-11-14 14:54:54 -0600280 inputFault++;
Brandon Wyman603cc002017-08-28 18:17:58 -0500281 }
282 else
283 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500284 if ((inputFault > 0) && !(statusWord & status_word::INPUT_FAULT_WARN) &&
Brandon Wymand20686a2017-11-01 17:45:23 -0500285 !(statusWord & status_word::VIN_UV_FAULT))
Brandon Wyman603cc002017-08-28 18:17:58 -0500286 {
Brandon Wymana3c675c2017-11-14 14:54:54 -0600287 inputFault = 0;
Brandon Wyman3343e822017-11-03 16:54:11 -0500288 faultFound = false;
Brandon Wyman7502f6c2018-02-12 20:20:39 -0600289 // When an input fault occurs, the power supply cannot be on.
290 // However, the check for the case where the power supply should be
291 // on will stop when there is a fault found.
292 // Clear the powerOnFault when the inputFault is cleared to reset
293 // the powerOnFault de-glitching.
294 powerOnFault = 0;
Brandon Wyman69591bd2017-11-01 18:07:23 -0500295
Brandon Wyman603cc002017-08-28 18:17:58 -0500296 log<level::INFO>("INPUT_FAULT_WARN cleared",
Brandon Wymana3c675c2017-11-14 14:54:54 -0600297 entry("POWERSUPPLY=%s", inventoryPath.c_str()));
Brandon Wyman69591bd2017-11-01 18:07:23 -0500298
Brandon Wyman08b05712017-11-30 17:53:56 -0600299 resolveError(inventoryPath,
300 std::string(PowerSupplyInputFault::errName));
301
Brandon Wyman69591bd2017-11-01 18:07:23 -0500302 if (powerOn)
303 {
304 // The power supply will not be immediately powered on after
305 // the input power is restored.
306 powerOn = false;
307 // Start up the timer that will set the state to indicate we
308 // are ready for the powered on fault checks.
William A. Kennington III1a0c9172018-10-18 17:57:49 -0700309 powerOnTimer.restartOnce(powerOnInterval);
Brandon Wyman69591bd2017-11-01 18:07:23 -0500310 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500311 }
312 }
Brandon Wymana3c675c2017-11-14 14:54:54 -0600313
314 if (!faultFound && (inputFault >= FAULT_COUNT))
315 {
Brandon Wymanad708242018-01-19 17:28:35 -0600316 // If the power is on, report the fault in an error log entry.
317 if (powerOn)
318 {
319 util::NamesValues nv;
320 nv.add("STATUS_WORD", statusWord);
321 captureCmd(nv, STATUS_INPUT, Type::Debug);
Brandon Wymana3c675c2017-11-14 14:54:54 -0600322
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500323 using metadata =
324 org::open_power::Witherspoon::Fault::PowerSupplyInputFault;
Brandon Wymana3c675c2017-11-14 14:54:54 -0600325
Brandon Wymanad708242018-01-19 17:28:35 -0600326 report<PowerSupplyInputFault>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500327 metadata::RAW_STATUS(nv.get().c_str()),
328 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wymanad708242018-01-19 17:28:35 -0600329
330 faultFound = true;
331 }
Brandon Wymana3c675c2017-11-14 14:54:54 -0600332 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500333}
334
335void PowerSupply::checkPGOrUnitOffFault(const uint16_t statusWord)
336{
337 using namespace witherspoon::pmbus;
338
Brandon Wyman593d24f2017-10-13 18:15:23 -0500339 if (powerOnFault < FAULT_COUNT)
Brandon Wyman603cc002017-08-28 18:17:58 -0500340 {
Brandon Wyman593d24f2017-10-13 18:15:23 -0500341 // Check PG# and UNIT_IS_OFF
342 if ((statusWord & status_word::POWER_GOOD_NEGATED) ||
343 (statusWord & status_word::UNIT_IS_OFF))
344 {
345 log<level::INFO>("PGOOD or UNIT_IS_OFF bit bad",
346 entry("STATUS_WORD=0x%04X", statusWord));
347 powerOnFault++;
348 }
349 else
350 {
351 if (powerOnFault > 0)
352 {
353 log<level::INFO>("PGOOD and UNIT_IS_OFF bits good");
354 powerOnFault = 0;
355 }
356 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500357
Brandon Wymane2fc7aa2017-11-13 17:37:10 -0600358 if (!faultFound && (powerOnFault >= FAULT_COUNT))
Brandon Wyman593d24f2017-10-13 18:15:23 -0500359 {
Brandon Wyman3343e822017-11-03 16:54:11 -0500360 faultFound = true;
361
Brandon Wyman593d24f2017-10-13 18:15:23 -0500362 util::NamesValues nv;
363 nv.add("STATUS_WORD", statusWord);
364 captureCmd(nv, STATUS_INPUT, Type::Debug);
365 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
366 captureCmd(nv, status0Vout, Type::Debug);
367 captureCmd(nv, STATUS_IOUT, Type::Debug);
368 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wyman603cc002017-08-28 18:17:58 -0500369
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500370 using metadata =
371 org::open_power::Witherspoon::Fault::PowerSupplyShouldBeOn;
Brandon Wyman603cc002017-08-28 18:17:58 -0500372
Brandon Wyman593d24f2017-10-13 18:15:23 -0500373 // A power supply is OFF (or pgood low) but should be on.
374 report<PowerSupplyShouldBeOn>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500375 metadata::RAW_STATUS(nv.get().c_str()),
376 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wyman593d24f2017-10-13 18:15:23 -0500377 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500378 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500379}
380
381void PowerSupply::checkCurrentOutOverCurrentFault(const uint16_t statusWord)
382{
383 using namespace witherspoon::pmbus;
384
Brandon Wymandd61be42017-11-07 18:38:54 -0600385 if (outputOCFault < FAULT_COUNT)
Brandon Wyman603cc002017-08-28 18:17:58 -0500386 {
Brandon Wymandd61be42017-11-07 18:38:54 -0600387 // Check for an output overcurrent fault.
388 if ((statusWord & status_word::IOUT_OC_FAULT))
389 {
390 outputOCFault++;
391 }
392 else
393 {
394 if (outputOCFault > 0)
395 {
396 outputOCFault = 0;
397 }
398 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500399
Brandon Wymane2fc7aa2017-11-13 17:37:10 -0600400 if (!faultFound && (outputOCFault >= FAULT_COUNT))
Brandon Wymandd61be42017-11-07 18:38:54 -0600401 {
402 util::NamesValues nv;
403 nv.add("STATUS_WORD", statusWord);
404 captureCmd(nv, STATUS_INPUT, Type::Debug);
405 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
406 captureCmd(nv, status0Vout, Type::Debug);
407 captureCmd(nv, STATUS_IOUT, Type::Debug);
408 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wyman603cc002017-08-28 18:17:58 -0500409
Brandon Wymandd61be42017-11-07 18:38:54 -0600410 using metadata = org::open_power::Witherspoon::Fault::
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500411 PowerSupplyOutputOvercurrent;
Brandon Wyman603cc002017-08-28 18:17:58 -0500412
Brandon Wymandd61be42017-11-07 18:38:54 -0600413 report<PowerSupplyOutputOvercurrent>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500414 metadata::RAW_STATUS(nv.get().c_str()),
415 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wymandd61be42017-11-07 18:38:54 -0600416
417 faultFound = true;
418 }
Brandon Wyman603cc002017-08-28 18:17:58 -0500419 }
420}
421
Brandon Wymanab05c072017-08-30 18:26:41 -0500422void PowerSupply::checkOutputOvervoltageFault(const uint16_t statusWord)
423{
424 using namespace witherspoon::pmbus;
425
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600426 if (outputOVFault < FAULT_COUNT)
Brandon Wymanab05c072017-08-30 18:26:41 -0500427 {
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600428 // Check for an output overvoltage fault.
429 if (statusWord & status_word::VOUT_OV_FAULT)
430 {
431 outputOVFault++;
432 }
433 else
434 {
435 if (outputOVFault > 0)
436 {
437 outputOVFault = 0;
438 }
439 }
Brandon Wymanab05c072017-08-30 18:26:41 -0500440
Brandon Wymane2fc7aa2017-11-13 17:37:10 -0600441 if (!faultFound && (outputOVFault >= FAULT_COUNT))
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600442 {
443 util::NamesValues nv;
444 nv.add("STATUS_WORD", statusWord);
445 captureCmd(nv, STATUS_INPUT, Type::Debug);
446 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
447 captureCmd(nv, status0Vout, Type::Debug);
448 captureCmd(nv, STATUS_IOUT, Type::Debug);
449 captureCmd(nv, STATUS_MFR, Type::Debug);
Brandon Wymanab05c072017-08-30 18:26:41 -0500450
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600451 using metadata = org::open_power::Witherspoon::Fault::
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500452 PowerSupplyOutputOvervoltage;
Brandon Wymanab05c072017-08-30 18:26:41 -0500453
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600454 report<PowerSupplyOutputOvervoltage>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500455 metadata::RAW_STATUS(nv.get().c_str()),
456 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600457
458 faultFound = true;
459 }
Brandon Wymanab05c072017-08-30 18:26:41 -0500460 }
461}
462
Brandon Wyman12661f12017-08-31 15:28:21 -0500463void PowerSupply::checkFanFault(const uint16_t statusWord)
464{
465 using namespace witherspoon::pmbus;
466
Brandon Wymanba255532017-11-08 17:44:10 -0600467 if (fanFault < FAULT_COUNT)
Brandon Wyman12661f12017-08-31 15:28:21 -0500468 {
Brandon Wymanba255532017-11-08 17:44:10 -0600469 // Check for a fan fault or warning condition
470 if (statusWord & status_word::FAN_FAULT)
471 {
472 fanFault++;
473 }
474 else
475 {
476 if (fanFault > 0)
477 {
478 fanFault = 0;
479 }
480 }
Brandon Wyman12661f12017-08-31 15:28:21 -0500481
Brandon Wymane2fc7aa2017-11-13 17:37:10 -0600482 if (!faultFound && (fanFault >= FAULT_COUNT))
Brandon Wymanba255532017-11-08 17:44:10 -0600483 {
484 util::NamesValues nv;
485 nv.add("STATUS_WORD", statusWord);
486 captureCmd(nv, STATUS_MFR, Type::Debug);
487 captureCmd(nv, STATUS_TEMPERATURE, Type::Debug);
488 captureCmd(nv, STATUS_FANS_1_2, Type::Debug);
Brandon Wyman12661f12017-08-31 15:28:21 -0500489
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500490 using metadata =
491 org::open_power::Witherspoon::Fault::PowerSupplyFanFault;
Brandon Wyman12661f12017-08-31 15:28:21 -0500492
Brandon Wymanba255532017-11-08 17:44:10 -0600493 report<PowerSupplyFanFault>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500494 metadata::RAW_STATUS(nv.get().c_str()),
495 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wymanba255532017-11-08 17:44:10 -0600496
497 faultFound = true;
498 }
Brandon Wyman12661f12017-08-31 15:28:21 -0500499 }
500}
501
Brandon Wyman875b3632017-09-13 18:46:03 -0500502void PowerSupply::checkTemperatureFault(const uint16_t statusWord)
503{
504 using namespace witherspoon::pmbus;
505
506 // Due to how the PMBus core device driver sends a clear faults command
507 // the bit in STATUS_WORD will likely be cleared when we attempt to examine
508 // it for a Thermal Fault or Warning. So, check the STATUS_WORD and the
509 // STATUS_TEMPERATURE bits. If either indicates a fault, proceed with
510 // logging the over-temperature condition.
511 std::uint8_t statusTemperature = 0;
512 statusTemperature = pmbusIntf.read(STATUS_TEMPERATURE, Type::Debug);
Brandon Wyman50044ea2017-11-08 17:58:56 -0600513 if (temperatureFault < FAULT_COUNT)
Brandon Wyman875b3632017-09-13 18:46:03 -0500514 {
Brandon Wyman50044ea2017-11-08 17:58:56 -0600515 if ((statusWord & status_word::TEMPERATURE_FAULT_WARN) ||
516 (statusTemperature & status_temperature::OT_FAULT))
517 {
518 temperatureFault++;
519 }
520 else
521 {
522 if (temperatureFault > 0)
523 {
524 temperatureFault = 0;
525 }
526 }
Brandon Wyman875b3632017-09-13 18:46:03 -0500527
Brandon Wymane2fc7aa2017-11-13 17:37:10 -0600528 if (!faultFound && (temperatureFault >= FAULT_COUNT))
Brandon Wyman50044ea2017-11-08 17:58:56 -0600529 {
530 // The power supply has had an over-temperature condition.
531 // This may not result in a shutdown if experienced for a short
532 // duration.
533 // This should not occur under normal conditions.
534 // The power supply may be faulty, or the paired supply may be
535 // putting out less current.
536 // Capture command responses with potentially relevant information,
537 // and call out the power supply reporting the condition.
538 util::NamesValues nv;
539 nv.add("STATUS_WORD", statusWord);
540 captureCmd(nv, STATUS_MFR, Type::Debug);
541 captureCmd(nv, STATUS_IOUT, Type::Debug);
542 nv.add("STATUS_TEMPERATURE", statusTemperature);
543 captureCmd(nv, STATUS_FANS_1_2, Type::Debug);
Brandon Wyman875b3632017-09-13 18:46:03 -0500544
Brandon Wyman50044ea2017-11-08 17:58:56 -0600545 using metadata = org::open_power::Witherspoon::Fault::
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500546 PowerSupplyTemperatureFault;
Brandon Wyman875b3632017-09-13 18:46:03 -0500547
Brandon Wyman50044ea2017-11-08 17:58:56 -0600548 report<PowerSupplyTemperatureFault>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500549 metadata::RAW_STATUS(nv.get().c_str()),
550 metadata::CALLOUT_INVENTORY_PATH(inventoryPath.c_str()));
Brandon Wyman50044ea2017-11-08 17:58:56 -0600551
552 faultFound = true;
553 }
Brandon Wyman875b3632017-09-13 18:46:03 -0500554 }
555}
556
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500557void PowerSupply::clearFaults()
558{
Brandon Wymane4af9802017-11-13 15:58:33 -0600559 readFail = 0;
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500560 readFailLogged = false;
Brandon Wymana3c675c2017-11-14 14:54:54 -0600561 inputFault = 0;
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500562 powerOnFault = 0;
Brandon Wymandd61be42017-11-07 18:38:54 -0600563 outputOCFault = 0;
Brandon Wyman2ab319b2017-11-08 17:34:59 -0600564 outputOVFault = 0;
Brandon Wymanba255532017-11-08 17:44:10 -0600565 fanFault = 0;
Brandon Wyman50044ea2017-11-08 17:58:56 -0600566 temperatureFault = 0;
Brandon Wyman3343e822017-11-03 16:54:11 -0500567 faultFound = false;
Brandon Wyman6ccce0b2017-10-26 15:13:10 -0500568
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500569 return;
570}
571
Brandon Wyman43ce2082017-11-30 17:24:01 -0600572void PowerSupply::resolveError(const std::string& callout,
573 const std::string& message)
574{
Brandon Wyman01741f12017-12-01 17:22:08 -0600575 using EndpointList = std::vector<std::string>;
576
577 try
578 {
579 auto path = callout + "/fault";
580 // Get the service name from the mapper for the fault callout
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500581 auto service = util::getService(path, ASSOCIATION_IFACE, bus);
Brandon Wyman01741f12017-12-01 17:22:08 -0600582
583 // Use getProperty utility function to get log entries (endpoints)
584 EndpointList logEntries;
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500585 util::getProperty(ASSOCIATION_IFACE, ENDPOINTS_PROP, path, service, bus,
586 logEntries);
Brandon Wyman01741f12017-12-01 17:22:08 -0600587
588 // It is possible that all such entries for this callout have since
589 // been deleted.
590 if (logEntries.empty())
591 {
592 return;
593 }
594
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500595 auto logEntryService =
596 util::getService(logEntries[0], LOGGING_IFACE, bus);
Brandon Wyman01741f12017-12-01 17:22:08 -0600597 if (logEntryService.empty())
598 {
599 return;
600 }
601
602 // go through each log entry that matches this callout path
603 std::string logMessage;
604 for (const auto& logEntry : logEntries)
605 {
606 // Check to see if this logEntry has a message that matches.
Matt Spinler589e8722018-01-04 15:24:49 -0600607 util::getProperty(LOGGING_IFACE, MESSAGE_PROP, logEntry,
Brandon Wyman01741f12017-12-01 17:22:08 -0600608 logEntryService, bus, logMessage);
609
610 if (message == logMessage)
611 {
612 // Log entry matches call out and message, set Resolved to true
613 bool resolved = true;
Matt Spinler589e8722018-01-04 15:24:49 -0600614 util::setProperty(LOGGING_IFACE, RESOLVED_PROP, logEntry,
Brandon Wyman01741f12017-12-01 17:22:08 -0600615 logEntryService, bus, resolved);
616 }
Brandon Wyman01741f12017-12-01 17:22:08 -0600617 }
Brandon Wyman01741f12017-12-01 17:22:08 -0600618 }
619 catch (std::exception& e)
620 {
621 log<level::INFO>("Failed to resolve error",
622 entry("CALLOUT=%s", callout.c_str()),
Matt Spinler0d09f292018-01-22 14:51:26 -0600623 entry("ERROR=%s", message.c_str()));
Brandon Wyman01741f12017-12-01 17:22:08 -0600624 }
Brandon Wyman43ce2082017-11-30 17:24:01 -0600625}
626
Matt Spinler234ce0d2018-01-04 15:06:57 -0600627void PowerSupply::updateInventory()
628{
Matt Spinler018a7bc2018-01-04 15:36:41 -0600629 using namespace witherspoon::pmbus;
630 using namespace sdbusplus::message;
631
632 // If any of these accesses fail, the fields will just be
633 // blank in the inventory. Leave logging ReadFailure errors
634 // to analyze() as it runs continuously and will most
635 // likely hit and threshold them first anyway. The
636 // readString() function will do the tracing of the failing
637 // path so this code doesn't need to.
638 std::string pn;
639 std::string sn;
640 std::string ccin;
641 std::string version;
642
643 if (present)
644 {
645 try
646 {
647 sn = pmbusIntf.readString(SERIAL_NUMBER, Type::HwmonDeviceDebug);
648 }
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500649 catch (ReadFailure& e)
650 {
651 }
Matt Spinler018a7bc2018-01-04 15:36:41 -0600652
653 try
654 {
655 pn = pmbusIntf.readString(PART_NUMBER, Type::HwmonDeviceDebug);
656 }
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500657 catch (ReadFailure& e)
658 {
659 }
Matt Spinler018a7bc2018-01-04 15:36:41 -0600660
661 try
662 {
663 ccin = pmbusIntf.readString(CCIN, Type::HwmonDeviceDebug);
664 }
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500665 catch (ReadFailure& e)
666 {
667 }
Matt Spinler018a7bc2018-01-04 15:36:41 -0600668
669 try
670 {
671 version = pmbusIntf.readString(FW_VERSION, Type::HwmonDeviceDebug);
672 }
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500673 catch (ReadFailure& e)
674 {
675 }
Matt Spinler018a7bc2018-01-04 15:36:41 -0600676 }
677
678 // Build the object map and send it to the inventory
679 using Properties = std::map<std::string, variant<std::string>>;
680 using Interfaces = std::map<std::string, Properties>;
681 using Object = std::map<object_path, Interfaces>;
682 Properties assetProps;
683 Properties versionProps;
684 Interfaces interfaces;
685 Object object;
686
687 assetProps.emplace(SN_PROP, sn);
688 assetProps.emplace(PN_PROP, pn);
689 assetProps.emplace(MODEL_PROP, ccin);
690 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
691
692 versionProps.emplace(VERSION_PROP, version);
693 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
694
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500695 // For Notify(), just send the relative path of the inventory
696 // object so remove the INVENTORY_OBJ_PATH prefix
Matt Spinler018a7bc2018-01-04 15:36:41 -0600697 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
698
699 object.emplace(path, std::move(interfaces));
700
701 try
702 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500703 auto service =
704 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
Matt Spinler018a7bc2018-01-04 15:36:41 -0600705
706 if (service.empty())
707 {
708 log<level::ERR>("Unable to get inventory manager service");
709 return;
710 }
711
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500712 auto method = bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
713 INVENTORY_MGR_IFACE, "Notify");
Matt Spinler018a7bc2018-01-04 15:36:41 -0600714
715 method.append(std::move(object));
716
717 auto reply = bus.call(method);
Matt Spinler018a7bc2018-01-04 15:36:41 -0600718
719 // TODO: openbmc/openbmc#2756
720 // Calling Notify() with an enumerated property crashes inventory
721 // manager, so let it default to Unknown and now set it to the
722 // right value.
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500723 auto purpose =
724 version::convertForMessage(version::Version::VersionPurpose::Other);
Matt Spinler018a7bc2018-01-04 15:36:41 -0600725
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500726 util::setProperty(VERSION_IFACE, VERSION_PURPOSE_PROP, inventoryPath,
727 service, bus, purpose);
Matt Spinler018a7bc2018-01-04 15:36:41 -0600728 }
729 catch (std::exception& e)
730 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500731 log<level::ERR>(e.what(), entry("PATH=%s", inventoryPath.c_str()));
Matt Spinler018a7bc2018-01-04 15:36:41 -0600732 }
Matt Spinler234ce0d2018-01-04 15:06:57 -0600733}
734
Matt Spinlerd734e652018-01-18 14:31:15 -0600735void PowerSupply::syncHistory()
736{
737 using namespace witherspoon::gpio;
738
739 if (syncGPIODevPath.empty())
740 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500741 // Sync not implemented
Matt Spinlerd734e652018-01-18 14:31:15 -0600742 return;
743 }
744
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500745 GPIO gpio{syncGPIODevPath, static_cast<gpioNum_t>(syncGPIONumber),
Matt Spinlerd734e652018-01-18 14:31:15 -0600746 Direction::output};
747
748 try
749 {
750 gpio.set(Value::low);
751
752 std::this_thread::sleep_for(std::chrono::milliseconds{5});
753
754 gpio.set(Value::high);
755
756 recordManager->clear();
757 }
758 catch (std::exception& e)
759 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500760 // Do nothing. There would already be a journal entry.
Matt Spinlerd734e652018-01-18 14:31:15 -0600761 }
762}
763
Matt Spinler82384142018-01-18 14:15:03 -0600764void PowerSupply::enableHistory(const std::string& objectPath,
765 size_t numRecords,
766 const std::string& syncGPIOPath,
767 size_t syncGPIONum)
768{
769 historyObjectPath = objectPath;
770 syncGPIODevPath = syncGPIOPath;
771 syncGPIONumber = syncGPIONum;
772
773 recordManager = std::make_unique<history::RecordManager>(numRecords);
774
775 auto avgPath = historyObjectPath + '/' + history::Average::name;
776 auto maxPath = historyObjectPath + '/' + history::Maximum::name;
777
778 average = std::make_unique<history::Average>(bus, avgPath);
779
780 maximum = std::make_unique<history::Maximum>(bus, maxPath);
781}
782
Matt Spinlereb169fd2018-01-18 14:19:08 -0600783void PowerSupply::updateHistory()
784{
785 if (!recordManager)
786 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500787 // Not enabled
Matt Spinlereb169fd2018-01-18 14:19:08 -0600788 return;
789 }
790
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500791 // Read just the most recent average/max record
792 auto data =
793 pmbusIntf.readBinary(INPUT_HISTORY, pmbus::Type::HwmonDeviceDebug,
794 history::RecordManager::RAW_RECORD_SIZE);
Matt Spinlereb169fd2018-01-18 14:19:08 -0600795
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500796 // Update D-Bus only if something changed (a new record ID, or cleared out)
Matt Spinlereb169fd2018-01-18 14:19:08 -0600797 auto changed = recordManager->add(data);
798 if (changed)
799 {
800 average->values(std::move(recordManager->getAverageRecords()));
801 maximum->values(std::move(recordManager->getMaximumRecords()));
802 }
803}
804
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500805} // namespace psu
806} // namespace power
807} // namespace witherspoon