blob: 03a6bf08ce4517f1c6e18d34cf4c82fa4ba064b8 [file] [log] [blame]
Shawn McCarney472101c2024-04-17 16:31:09 -05001/**
2 * Copyright © 2024 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 */
16#pragma once
17
18#include "power_sequencer_device.hpp"
19#include "rail.hpp"
20#include "services.hpp"
21
Shawn McCarney31234452025-10-28 12:32:05 -050022#include <cstdint>
Shawn McCarney472101c2024-04-17 16:31:09 -050023#include <map>
24#include <memory>
25#include <string>
26#include <utility>
27#include <vector>
28
29namespace phosphor::power::sequencer
30{
31
32/**
33 * @class StandardDevice
34 *
35 * PowerSequencerDevice sub-class that implements the standard pgood fault
36 * detection algorithm.
37 *
38 * When adding support for a new power sequencer device type, create a sub-class
39 * of StandardDevice if possible. This will ensure that pgood fault detection
40 * works consistently across device types.
41 */
42class StandardDevice : public PowerSequencerDevice
43{
44 public:
45 // Specify which compiler-generated methods we want
46 StandardDevice() = delete;
47 StandardDevice(const StandardDevice&) = delete;
48 StandardDevice(StandardDevice&&) = delete;
49 StandardDevice& operator=(const StandardDevice&) = delete;
50 StandardDevice& operator=(StandardDevice&&) = delete;
51 virtual ~StandardDevice() = default;
52
53 /**
54 * Constructor.
55 *
56 * @param name device name
Shawn McCarney31234452025-10-28 12:32:05 -050057 * @param bus I2C bus for the device
58 * @param address I2C address for the device
Shawn McCarney472101c2024-04-17 16:31:09 -050059 * @param rails voltage rails that are enabled and monitored by this device
60 */
Shawn McCarney31234452025-10-28 12:32:05 -050061 explicit StandardDevice(const std::string& name, uint8_t bus,
62 uint16_t address,
Shawn McCarney472101c2024-04-17 16:31:09 -050063 std::vector<std::unique_ptr<Rail>> rails) :
Shawn McCarney31234452025-10-28 12:32:05 -050064 name{name}, bus{bus}, address{address}, rails{std::move(rails)}
Shawn McCarney472101c2024-04-17 16:31:09 -050065 {}
66
67 /** @copydoc PowerSequencerDevice::getName() */
68 virtual const std::string& getName() const override
69 {
70 return name;
71 }
72
Shawn McCarney31234452025-10-28 12:32:05 -050073 /** @copydoc PowerSequencerDevice::getBus() */
74 virtual uint8_t getBus() const override
75 {
76 return bus;
77 }
78
79 /** @copydoc PowerSequencerDevice::getAddress() */
80 virtual uint16_t getAddress() const override
81 {
82 return address;
83 }
84
Shawn McCarney472101c2024-04-17 16:31:09 -050085 /** @copydoc PowerSequencerDevice::getRails() */
86 virtual const std::vector<std::unique_ptr<Rail>>& getRails() const override
87 {
88 return rails;
89 }
90
91 /** @copydoc PowerSequencerDevice::findPgoodFault()
92 *
93 * Calls prepareForPgoodFaultDetection() before starting detection. If a
94 * pgood fault is detected, calls storePgoodFaultDebugData().
95 */
96 virtual std::string findPgoodFault(
97 Services& services, const std::string& powerSupplyError,
98 std::map<std::string, std::string>& additionalData) override;
99
100 protected:
101 /**
102 * Prepare for pgood fault detection.
103 *
104 * Perform any actions that are necessary to prepare for fault detection.
105 * For example, cache information that is slow to obtain and is used
106 * multiple times during detection.
107 *
108 * Default implementation does nothing. Override in sub-classes if needed.
109 *
110 * @param services System services like hardware presence and the journal
111 */
Patrick Williams92261f82025-02-01 08:22:34 -0500112 virtual void prepareForPgoodFaultDetection(
113 [[maybe_unused]] Services& services)
Shawn McCarney472101c2024-04-17 16:31:09 -0500114 {}
115
116 /**
117 * Returns the GPIO values that can be read from the device, if possible.
118 *
119 * If the device does not support reading GPIO values or an error occurs, an
120 * empty vector is returned.
121 *
Shawn McCarneyfc3f31f2024-04-23 17:02:44 -0500122 * @param services System services like hardware presence and the journal
Shawn McCarney472101c2024-04-17 16:31:09 -0500123 * @return GPIO values, or empty vector if values could not be read
124 */
Shawn McCarneyfc3f31f2024-04-23 17:02:44 -0500125 virtual std::vector<int> getGPIOValuesIfPossible(Services& services);
Shawn McCarney472101c2024-04-17 16:31:09 -0500126
127 /**
Shawn McCarney16275832024-06-27 10:14:11 -0500128 * Checks whether a pgood fault has occurred on one of the rails being
129 * monitored by this device.
130 *
131 * If a pgood fault was found in a rail, a pointer to the Rail object is
132 * returned.
133 *
134 * Throws an exception if an error occurs while trying to obtain the status
135 * of the rails.
136 *
137 * @param services System services like hardware presence and the journal
138 * @param gpioValues GPIO values obtained from the device (if any)
139 * @param additionalData Additional data to include in the error log if
140 * a pgood fault was found
141 * @return pointer to Rail object where fault was found, or nullptr if no
142 * Rail found
143 */
144 virtual Rail* findRailWithPgoodFault(
145 Services& services, const std::vector<int>& gpioValues,
146 std::map<std::string, std::string>& additionalData);
147
148 /**
Shawn McCarney472101c2024-04-17 16:31:09 -0500149 * Store pgood fault debug data in the specified additional data map.
150 *
Shawn McCarneyfe78c172024-05-02 14:01:46 -0500151 * The default implementation stores the device name and then calls
152 * storeGPIOValues().
Shawn McCarney472101c2024-04-17 16:31:09 -0500153 *
Shawn McCarneyfe78c172024-05-02 14:01:46 -0500154 * Sub-classes should override if needed to store device-specific data.
Shawn McCarney472101c2024-04-17 16:31:09 -0500155 *
156 * This method should NOT throw exceptions. If debug data cannot be
157 * obtained, the error should be caught and ignored so that pgood error
158 * handling can continue.
159 *
160 * @param services System services like hardware presence and the journal
161 * @param gpioValues GPIO values obtained from the device (if any)
162 * @param additionalData Additional data to include in an error log
163 */
164 virtual void storePgoodFaultDebugData(
165 Services& services, const std::vector<int>& gpioValues,
166 std::map<std::string, std::string>& additionalData);
167
168 /**
Shawn McCarneyfe78c172024-05-02 14:01:46 -0500169 * Store GPIO values in the specified additional data map.
170 *
171 * The default implementation stores the values as a simple list of
172 * integers.
173 *
174 * Sub-classes should override if more advanced formatting is needed. For
175 * example, GPIOs could be stored individually with a name and value, or
176 * related GPIOs could be formatted as a group.
177 *
178 * @param services System services like hardware presence and the journal
179 * @param values GPIO values obtained from the device (if any)
180 * @param additionalData Additional data to include in an error log
181 */
Patrick Williams92261f82025-02-01 08:22:34 -0500182 virtual void storeGPIOValues(
183 Services& services, const std::vector<int>& values,
184 std::map<std::string, std::string>& additionalData);
Shawn McCarneyfe78c172024-05-02 14:01:46 -0500185
186 /**
Shawn McCarney472101c2024-04-17 16:31:09 -0500187 * Device name.
188 */
189 std::string name{};
190
191 /**
Shawn McCarney31234452025-10-28 12:32:05 -0500192 * I2C bus for the device.
193 */
194 uint8_t bus;
195
196 /**
197 * I2C address for the device.
198 */
199 uint16_t address;
200
201 /**
Shawn McCarney472101c2024-04-17 16:31:09 -0500202 * Voltage rails that are enabled and monitored by this device.
203 */
204 std::vector<std::unique_ptr<Rail>> rails{};
205};
206
207} // namespace phosphor::power::sequencer