blob: 98c3faa40979de515d9becb7a622f4e070e627bf [file] [log] [blame]
Matt Spinlerf02daec2017-08-14 14:00:46 -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 Spinler45a054a2017-08-22 15:07:07 -050016#include "config.h"
Matt Spinlerf0f02b92018-10-25 16:12:43 -050017
Matt Spinlerf02daec2017-08-14 14:00:46 -050018#include "pgood_monitor.hpp"
Matt Spinlerf0f02b92018-10-25 16:12:43 -050019
20#include "elog-errors.hpp"
Matt Spinlerf02daec2017-08-14 14:00:46 -050021#include "utility.hpp"
22
Matt Spinlerf0f02b92018-10-25 16:12:43 -050023#include <org/open_power/Witherspoon/Fault/error.hpp>
24#include <phosphor-logging/log.hpp>
25
Matt Spinlerf02daec2017-08-14 14:00:46 -050026namespace witherspoon
27{
28namespace power
29{
30
31constexpr auto POWER_OBJ_PATH = "/org/openbmc/control/power0";
32constexpr auto POWER_INTERFACE = "org.openbmc.control.Power";
33
34using namespace phosphor::logging;
Brandon Wymane0eb45c2017-10-06 12:58:42 -050035using namespace sdbusplus::org::open_power::Witherspoon::Fault::Error;
Matt Spinlerf02daec2017-08-14 14:00:46 -050036
37bool PGOODMonitor::pgoodPending()
38{
39 bool pending = false;
40 int32_t state = 0;
41 int32_t pgood = 0;
42
Matt Spinlerf0f02b92018-10-25 16:12:43 -050043 auto service = util::getService(POWER_OBJ_PATH, POWER_INTERFACE, bus);
Matt Spinlerf02daec2017-08-14 14:00:46 -050044
Matt Spinlerf0f02b92018-10-25 16:12:43 -050045 util::getProperty<int32_t>(POWER_INTERFACE, "pgood", POWER_OBJ_PATH,
46 service, bus, pgood);
Matt Spinlerf02daec2017-08-14 14:00:46 -050047
Matt Spinlerf0f02b92018-10-25 16:12:43 -050048 // When state = 1, system was switched on
49 util::getProperty<int32_t>(POWER_INTERFACE, "state", POWER_OBJ_PATH,
50 service, bus, state);
Matt Spinlerf02daec2017-08-14 14:00:46 -050051
Matt Spinlerf0f02b92018-10-25 16:12:43 -050052 // On but no PGOOD
Matt Spinlerf02daec2017-08-14 14:00:46 -050053 if (state && !pgood)
54 {
55 pending = true;
56 }
57
58 return pending;
59}
60
Matt Spinlerf02daec2017-08-14 14:00:46 -050061void PGOODMonitor::analyze()
62{
Matt Spinlerf0f02b92018-10-25 16:12:43 -050063 // Timer callback.
64 // The timer expired before it was stopped.
65 // If PGOOD is still pending (it should be),
66 // then there is a real failure.
Matt Spinlerf02daec2017-08-14 14:00:46 -050067
68 if (pgoodPending())
69 {
Matt Spinler45a054a2017-08-22 15:07:07 -050070#ifdef UCD90160_DEVICE_ACCESS
Matt Spinlerb2d72512017-08-22 09:07:01 -050071 device->onFailure();
Matt Spinler45a054a2017-08-22 15:07:07 -050072#endif
Matt Spinlerf02daec2017-08-14 14:00:46 -050073 report<PowerOnFailure>();
74 }
75
Matt Spinlerf0f02b92018-10-25 16:12:43 -050076 // The pgood-wait service (with a longer timeout)
77 // will handle powering off the system.
William A. Kennington III1a0c9172018-10-18 17:57:49 -070078 timer.get_event().exit(EXIT_SUCCESS);
Matt Spinlerf02daec2017-08-14 14:00:46 -050079}
80
81void PGOODMonitor::propertyChanged()
82{
Matt Spinlerf0f02b92018-10-25 16:12:43 -050083 // Multiple properties could have changed here.
84 // Keep things simple and just recheck the important ones.
Matt Spinlerf02daec2017-08-14 14:00:46 -050085 if (!pgoodPending())
86 {
Matt Spinlerf0f02b92018-10-25 16:12:43 -050087 // PGOOD is on, or system is off, so we are done.
William A. Kennington III1a0c9172018-10-18 17:57:49 -070088 timer.get_event().exit(EXIT_SUCCESS);
Matt Spinlerf02daec2017-08-14 14:00:46 -050089 }
90}
91
92void PGOODMonitor::startListening()
93{
94 match = std::make_unique<sdbusplus::bus::match_t>(
Matt Spinlerf0f02b92018-10-25 16:12:43 -050095 bus,
96 sdbusplus::bus::match::rules::propertiesChanged(POWER_OBJ_PATH,
97 POWER_INTERFACE),
98 [this](auto& msg) { this->propertyChanged(); });
Matt Spinlerf02daec2017-08-14 14:00:46 -050099}
100
101int PGOODMonitor::run()
102{
103 try
104 {
105 startListening();
106
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500107 // If PGOOD came up before we got here, we're done.
108 // Otherwise if PGOOD doesn't get asserted before
109 // the timer expires, it's a failure.
Matt Spinlerf02daec2017-08-14 14:00:46 -0500110 if (!pgoodPending())
111 {
112 return EXIT_SUCCESS;
113 }
114
William A. Kennington III1a0c9172018-10-18 17:57:49 -0700115 return timer.get_event().loop();
Matt Spinlerf02daec2017-08-14 14:00:46 -0500116 }
117 catch (std::exception& e)
118 {
119 log<level::ERR>(e.what());
120 log<level::ERR>("Unexpected failure prevented PGOOD checking");
121 }
122
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500123 // Letting the service fail won't help anything, so don't do it.
Matt Spinlerf02daec2017-08-14 14:00:46 -0500124 return EXIT_SUCCESS;
125}
126
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500127} // namespace power
128} // namespace witherspoon