blob: 90f33ae6656811653db7d4783db914775242c384 [file] [log] [blame]
Brandon Wyman24e422f2017-07-25 19:40:14 -05001/**
2 * Copyright © 2017 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brandon Wyman442035f2017-08-08 15:58:45 -050016#include <phosphor-logging/log.hpp>
17#include <phosphor-logging/elog.hpp>
18#include <xyz/openbmc_project/Sensor/Device/error.hpp>
19#include <xyz/openbmc_project/Control/Device/error.hpp>
20#include <xyz/openbmc_project/Power/Fault/error.hpp>
21#include "elog-errors.hpp"
Brandon Wyman10295542017-08-09 18:20:44 -050022#include "names_values.hpp"
Brandon Wyman24e422f2017-07-25 19:40:14 -050023#include "power_supply.hpp"
Brandon Wyman442035f2017-08-08 15:58:45 -050024#include "pmbus.hpp"
25#include "utility.hpp"
26
27using namespace phosphor::logging;
28using namespace sdbusplus::xyz::openbmc_project::Control::Device::Error;
29using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error;
30using namespace sdbusplus::xyz::openbmc_project::Power::Fault::Error;
Brandon Wyman24e422f2017-07-25 19:40:14 -050031
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050032namespace witherspoon
Brandon Wyman24e422f2017-07-25 19:40:14 -050033{
34namespace power
35{
36namespace psu
37{
38
Brandon Wyman10295542017-08-09 18:20:44 -050039constexpr auto INVENTORY_OBJ_PATH = "/xyz/openbmc_project/inventory";
40constexpr auto INVENTORY_INTERFACE = "xyz.openbmc_project.Inventory.Item";
41constexpr auto PRESENT_PROP = "Present";
Brandon Wyman431fbe42017-08-18 16:22:09 -050042constexpr auto POWER_OBJ_PATH = "/org/openbmc/control/power0";
43constexpr auto POWER_INTERFACE = "org.openbmc.control.Power";
Brandon Wyman10295542017-08-09 18:20:44 -050044
45PowerSupply::PowerSupply(const std::string& name, size_t inst,
Brandon Wyman431fbe42017-08-18 16:22:09 -050046 const std::string& objpath,
47 const std::string& invpath,
48 sdbusplus::bus::bus& bus,
49 event::Event& e,
50 std::chrono::seconds& t)
51 : Device(name, inst), monitorPath(objpath), pmbusIntf(objpath),
52 inventoryPath(invpath), bus(bus), event(e), powerOnInterval(t),
53 powerOnTimer(e, [this]()
54 {
55 this->powerOn = true;
56 })
Brandon Wyman10295542017-08-09 18:20:44 -050057{
Brandon Wyman10295542017-08-09 18:20:44 -050058 using namespace sdbusplus::bus;
59 auto present_obj_path = INVENTORY_OBJ_PATH + inventoryPath;
60 presentMatch = std::make_unique<match_t>(bus,
61 match::rules::propertiesChanged(
62 present_obj_path,
63 INVENTORY_INTERFACE),
64 [this](auto& msg)
Brandon Wyman431fbe42017-08-18 16:22:09 -050065 {
66 this->inventoryChanged(msg);
67 });
68 // Get initial presence state.
Brandon Wyman253dc9b2017-08-12 13:45:52 -050069 updatePresence();
Brandon Wyman431fbe42017-08-18 16:22:09 -050070
71 // Subscribe to power state changes
72 powerOnMatch = std::make_unique<match_t>(bus,
73 match::rules::propertiesChanged(
74 POWER_OBJ_PATH,
75 POWER_INTERFACE),
76 [this](auto& msg)
77 {
78 this->powerStateChanged(msg);
79 });
80 // Get initial power state.
81 updatePowerState();
Brandon Wyman10295542017-08-09 18:20:44 -050082}
Brandon Wyman442035f2017-08-08 15:58:45 -050083
Brandon Wyman431fbe42017-08-18 16:22:09 -050084
Brandon Wyman1db9a9e2017-07-26 18:50:22 -050085void PowerSupply::analyze()
86{
Brandon Wyman442035f2017-08-08 15:58:45 -050087 using namespace witherspoon::pmbus;
88
89 try
90 {
Brandon Wyman10295542017-08-09 18:20:44 -050091 if (present)
Brandon Wyman442035f2017-08-08 15:58:45 -050092 {
Brandon Wyman764c7972017-08-22 17:05:36 -050093 std::uint16_t statusWord = 0;
Brandon Wyman764c7972017-08-22 17:05:36 -050094
95 // Read the 2 byte STATUS_WORD value to check for faults.
96 statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug);
97
Brandon Wyman10295542017-08-09 18:20:44 -050098 //TODO: 3 consecutive reads should be performed.
99 // If 3 consecutive reads are seen, log the fault.
100 // Driver gives cached value, read once a second.
101 // increment for fault on, decrement for fault off, to deglitch.
102 // If count reaches 3, we have fault. If count reaches 0, fault is
103 // cleared.
104
Brandon Wyman603cc002017-08-28 18:17:58 -0500105 checkInputFault(statusWord);
Brandon Wyman764c7972017-08-22 17:05:36 -0500106
107 if (powerOn)
108 {
Brandon Wyman603cc002017-08-28 18:17:58 -0500109 checkPGOrUnitOffFault(statusWord);
110 checkCurrentOutOverCurrentFault(statusWord);
Brandon Wyman442035f2017-08-08 15:58:45 -0500111 }
112 }
113 }
114 catch (ReadFailure& e)
115 {
116 if (!readFailLogged)
117 {
118 commit<ReadFailure>();
119 readFailLogged = true;
Brandon Wyman442035f2017-08-08 15:58:45 -0500120 }
121 }
122
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500123 return;
124}
125
Brandon Wyman10295542017-08-09 18:20:44 -0500126void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
127{
128 std::string msgSensor;
129 std::map<std::string, sdbusplus::message::variant<uint32_t, bool>> msgData;
130 msg.read(msgSensor, msgData);
131
132 // Check if it was the Present property that changed.
133 auto valPropMap = msgData.find(PRESENT_PROP);
134 if (valPropMap != msgData.end())
135 {
136 present = sdbusplus::message::variant_ns::get<bool>(valPropMap->second);
137
138 if (present)
139 {
140 readFailLogged = false;
141 vinUVFault = false;
Brandon Wyman253dc9b2017-08-12 13:45:52 -0500142 inputFault = false;
Brandon Wymanb165c252017-08-25 18:59:54 -0500143 outputOCFault = false;
Brandon Wyman10295542017-08-09 18:20:44 -0500144 }
145 }
146
147 return;
148}
149
150void PowerSupply::updatePresence()
151{
152 // Use getProperty utility function to get presence status.
153 std::string path = INVENTORY_OBJ_PATH + inventoryPath;
154 std::string service = "xyz.openbmc_project.Inventory.Manager";
Brandon Wyman8731a302017-08-16 16:15:34 -0500155
156 try
157 {
158 util::getProperty(INVENTORY_INTERFACE, PRESENT_PROP, path,
159 service, bus, this->present);
160 }
161 catch (std::exception& e)
162 {
163 // If we happen to be trying to update presence just as it is being
164 // updated, we may encounter a runtime_error. Just catch that for
165 // now, let the inventoryChanged signal handler update presence later.
166 present = false;
167 }
168
Brandon Wyman10295542017-08-09 18:20:44 -0500169}
170
Brandon Wyman431fbe42017-08-18 16:22:09 -0500171void PowerSupply::powerStateChanged(sdbusplus::message::message& msg)
172{
173 int32_t state = 0;
174 std::string msgSensor;
175 std::map<std::string, sdbusplus::message::variant<int32_t, int32_t>>
176 msgData;
177 msg.read(msgSensor, msgData);
178
179 // Check if it was the Present property that changed.
180 auto valPropMap = msgData.find("state");
181 if (valPropMap != msgData.end())
182 {
183 state = sdbusplus::message::variant_ns::get<int32_t>(valPropMap->second);
184
185 // Power is on when state=1. Set the fault logged variables to false
186 // and start the power on timer when the state changes to 1.
187 if (state)
188 {
189 readFailLogged = false;
190 vinUVFault = false;
191 inputFault = false;
Brandon Wyman764c7972017-08-22 17:05:36 -0500192 powerOnFault = false;
Brandon Wymanb165c252017-08-25 18:59:54 -0500193 outputOCFault = false;
Brandon Wyman431fbe42017-08-18 16:22:09 -0500194 powerOnTimer.start(powerOnInterval, Timer::TimerType::oneshot);
195 }
196 else
197 {
198 powerOnTimer.stop();
199 powerOn = false;
200 }
201 }
202
203}
204
205void PowerSupply::updatePowerState()
206{
207 // When state = 1, system is powered on
208 int32_t state = 0;
209
210 try
211 {
212 auto service = util::getService(POWER_OBJ_PATH,
213 POWER_INTERFACE,
214 bus);
215
216 // Use getProperty utility function to get power state.
217 util::getProperty<int32_t>(POWER_INTERFACE,
218 "state",
219 POWER_OBJ_PATH,
220 service,
221 bus,
222 state);
223
224 if (state)
225 {
226 powerOn = true;
227 }
228 else
229 {
230 powerOn = false;
231 }
232 }
233 catch (std::exception& e)
234 {
235 log<level::INFO>("Failed to get power state. Assuming it is off.");
236 powerOn = false;
237 }
238
239}
240
Brandon Wyman603cc002017-08-28 18:17:58 -0500241void PowerSupply::checkInputFault(const uint16_t statusWord)
242{
243 using namespace witherspoon::pmbus;
244
245 std::uint8_t statusInput = 0;
246
247 if ((statusWord & status_word::VIN_UV_FAULT) && !vinUVFault)
248 {
249 vinUVFault = true;
250
251 util::NamesValues nv;
252 nv.add("STATUS_WORD", statusWord);
253
254 using metadata = xyz::openbmc_project::Power::Fault::
255 PowerSupplyUnderVoltageFault;
256
257 report<PowerSupplyUnderVoltageFault>(metadata::RAW_STATUS(
258 nv.get().c_str()));
259 }
260 else
261 {
262 if (vinUVFault)
263 {
264 vinUVFault = false;
265 log<level::INFO>("VIN_UV_FAULT cleared",
266 entry("POWERSUPPLY=%s",
267 inventoryPath.c_str()));
268 }
269 }
270
271 if ((statusWord & status_word::INPUT_FAULT_WARN) && !inputFault)
272 {
273 inputFault = true;
274
275 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug);
276
277 util::NamesValues nv;
278 nv.add("STATUS_WORD", statusWord);
279 nv.add("STATUS_INPUT", statusInput);
280
281 using metadata = xyz::openbmc_project::Power::Fault::
282 PowerSupplyInputFault;
283
284 report<PowerSupplyInputFault>(
285 metadata::RAW_STATUS(nv.get().c_str()));
286 }
287 else
288 {
289 if ((inputFault) &&
290 !(statusWord & status_word::INPUT_FAULT_WARN))
291 {
292 inputFault = false;
293 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug);
294
295 log<level::INFO>("INPUT_FAULT_WARN cleared",
296 entry("POWERSUPPLY=%s", inventoryPath.c_str()),
297 entry("STATUS_WORD=0x%04X", statusWord),
298 entry("STATUS_INPUT=0x%02X", statusInput));
299 }
300 }
301}
302
303void PowerSupply::checkPGOrUnitOffFault(const uint16_t statusWord)
304{
305 using namespace witherspoon::pmbus;
306
307 std::uint8_t statusInput = 0;
308 std::uint8_t statusVout = 0;
309 std::uint8_t statusIout = 0;
310 std::uint8_t statusMFR = 0;
311
312 // Check PG# and UNIT_IS_OFF
313 if (((statusWord & status_word::POWER_GOOD_NEGATED) ||
314 (statusWord & status_word::UNIT_IS_OFF)) &&
315 !powerOnFault)
316 {
317 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug);
318 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
319 statusVout = pmbusIntf.read(status0Vout, Type::Debug);
320 statusIout = pmbusIntf.read(STATUS_IOUT, Type::Debug);
321 statusMFR = pmbusIntf.read(STATUS_MFR, Type::Debug);
322
323 util::NamesValues nv;
324 nv.add("STATUS_WORD", statusWord);
325 nv.add("STATUS_INPUT", statusInput);
326 nv.add("STATUS_VOUT", statusVout);
327 nv.add("STATUS_IOUT", statusIout);
328 nv.add("MFR_SPECIFIC", statusMFR);
329
330 using metadata = xyz::openbmc_project::Power::Fault::
331 PowerSupplyShouldBeOn;
332
333 // A power supply is OFF (or pgood low) but should be on.
334 report<PowerSupplyShouldBeOn>(metadata::RAW_STATUS(nv.get().c_str()),
335 metadata::CALLOUT_INVENTORY_PATH(
336 inventoryPath.c_str()));
337
338 powerOnFault = true;
339 }
340
341}
342
343void PowerSupply::checkCurrentOutOverCurrentFault(const uint16_t statusWord)
344{
345 using namespace witherspoon::pmbus;
346
347 std::uint8_t statusInput = 0;
348 std::uint8_t statusVout = 0;
349 std::uint8_t statusIout = 0;
350 std::uint8_t statusMFR = 0;
351
352 // Check for an output overcurrent fault.
353 if ((statusWord & status_word::IOUT_OC_FAULT) &&
354 !outputOCFault)
355 {
356 statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug);
357 auto status0Vout = pmbusIntf.insertPageNum(STATUS_VOUT, 0);
358 statusVout = pmbusIntf.read(status0Vout, Type::Debug);
359 statusIout = pmbusIntf.read(STATUS_IOUT, Type::Debug);
360 statusMFR = pmbusIntf.read(STATUS_MFR, Type::Debug);
361
362 util::NamesValues nv;
363 nv.add("STATUS_WORD", statusWord);
364 nv.add("STATUS_INPUT", statusInput);
365 nv.add("STATUS_VOUT", statusVout);
366 nv.add("STATUS_IOUT", statusIout);
367 nv.add("MFR_SPECIFIC", statusMFR);
368
369 using metadata = xyz::openbmc_project::Power::Fault::
370 PowerSupplyOutputOvercurrent;
371
372 report<PowerSupplyOutputOvercurrent>(metadata::RAW_STATUS(
373 nv.get().c_str()),
374 metadata::CALLOUT_INVENTORY_PATH(
375 inventoryPath.c_str()));
376
377 outputOCFault = true;
378 }
379}
380
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500381void PowerSupply::clearFaults()
382{
Brandon Wyman10295542017-08-09 18:20:44 -0500383 //TODO - Clear faults at pre-poweron. openbmc/openbmc#1736
Brandon Wyman1db9a9e2017-07-26 18:50:22 -0500384 return;
385}
386
Brandon Wyman24e422f2017-07-25 19:40:14 -0500387}
388}
389}