Shawn McCarney | 472101c | 2024-04-17 16:31:09 -0500 | [diff] [blame] | 1 | /** |
| 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 | |
| 22 | #include <map> |
| 23 | #include <memory> |
| 24 | #include <string> |
| 25 | #include <utility> |
| 26 | #include <vector> |
| 27 | |
| 28 | namespace phosphor::power::sequencer |
| 29 | { |
| 30 | |
| 31 | /** |
| 32 | * @class StandardDevice |
| 33 | * |
| 34 | * PowerSequencerDevice sub-class that implements the standard pgood fault |
| 35 | * detection algorithm. |
| 36 | * |
| 37 | * When adding support for a new power sequencer device type, create a sub-class |
| 38 | * of StandardDevice if possible. This will ensure that pgood fault detection |
| 39 | * works consistently across device types. |
| 40 | */ |
| 41 | class StandardDevice : public PowerSequencerDevice |
| 42 | { |
| 43 | public: |
| 44 | // Specify which compiler-generated methods we want |
| 45 | StandardDevice() = delete; |
| 46 | StandardDevice(const StandardDevice&) = delete; |
| 47 | StandardDevice(StandardDevice&&) = delete; |
| 48 | StandardDevice& operator=(const StandardDevice&) = delete; |
| 49 | StandardDevice& operator=(StandardDevice&&) = delete; |
| 50 | virtual ~StandardDevice() = default; |
| 51 | |
| 52 | /** |
| 53 | * Constructor. |
| 54 | * |
| 55 | * @param name device name |
| 56 | * @param rails voltage rails that are enabled and monitored by this device |
| 57 | */ |
| 58 | explicit StandardDevice(const std::string& name, |
| 59 | std::vector<std::unique_ptr<Rail>> rails) : |
| 60 | name{name}, |
| 61 | rails{std::move(rails)} |
| 62 | {} |
| 63 | |
| 64 | /** @copydoc PowerSequencerDevice::getName() */ |
| 65 | virtual const std::string& getName() const override |
| 66 | { |
| 67 | return name; |
| 68 | } |
| 69 | |
| 70 | /** @copydoc PowerSequencerDevice::getRails() */ |
| 71 | virtual const std::vector<std::unique_ptr<Rail>>& getRails() const override |
| 72 | { |
| 73 | return rails; |
| 74 | } |
| 75 | |
| 76 | /** @copydoc PowerSequencerDevice::findPgoodFault() |
| 77 | * |
| 78 | * Calls prepareForPgoodFaultDetection() before starting detection. If a |
| 79 | * pgood fault is detected, calls storePgoodFaultDebugData(). |
| 80 | */ |
| 81 | virtual std::string findPgoodFault( |
| 82 | Services& services, const std::string& powerSupplyError, |
| 83 | std::map<std::string, std::string>& additionalData) override; |
| 84 | |
| 85 | protected: |
| 86 | /** |
| 87 | * Prepare for pgood fault detection. |
| 88 | * |
| 89 | * Perform any actions that are necessary to prepare for fault detection. |
| 90 | * For example, cache information that is slow to obtain and is used |
| 91 | * multiple times during detection. |
| 92 | * |
| 93 | * Default implementation does nothing. Override in sub-classes if needed. |
| 94 | * |
| 95 | * @param services System services like hardware presence and the journal |
| 96 | */ |
| 97 | virtual void |
| 98 | prepareForPgoodFaultDetection([[maybe_unused]] Services& services) |
| 99 | {} |
| 100 | |
| 101 | /** |
| 102 | * Returns the GPIO values that can be read from the device, if possible. |
| 103 | * |
| 104 | * If the device does not support reading GPIO values or an error occurs, an |
| 105 | * empty vector is returned. |
| 106 | * |
Shawn McCarney | fc3f31f | 2024-04-23 17:02:44 -0500 | [diff] [blame] | 107 | * @param services System services like hardware presence and the journal |
Shawn McCarney | 472101c | 2024-04-17 16:31:09 -0500 | [diff] [blame] | 108 | * @return GPIO values, or empty vector if values could not be read |
| 109 | */ |
Shawn McCarney | fc3f31f | 2024-04-23 17:02:44 -0500 | [diff] [blame] | 110 | virtual std::vector<int> getGPIOValuesIfPossible(Services& services); |
Shawn McCarney | 472101c | 2024-04-17 16:31:09 -0500 | [diff] [blame] | 111 | |
| 112 | /** |
| 113 | * Store pgood fault debug data in the specified additional data map. |
| 114 | * |
Shawn McCarney | fe78c17 | 2024-05-02 14:01:46 -0500 | [diff] [blame^] | 115 | * The default implementation stores the device name and then calls |
| 116 | * storeGPIOValues(). |
Shawn McCarney | 472101c | 2024-04-17 16:31:09 -0500 | [diff] [blame] | 117 | * |
Shawn McCarney | fe78c17 | 2024-05-02 14:01:46 -0500 | [diff] [blame^] | 118 | * Sub-classes should override if needed to store device-specific data. |
Shawn McCarney | 472101c | 2024-04-17 16:31:09 -0500 | [diff] [blame] | 119 | * |
| 120 | * This method should NOT throw exceptions. If debug data cannot be |
| 121 | * obtained, the error should be caught and ignored so that pgood error |
| 122 | * handling can continue. |
| 123 | * |
| 124 | * @param services System services like hardware presence and the journal |
| 125 | * @param gpioValues GPIO values obtained from the device (if any) |
| 126 | * @param additionalData Additional data to include in an error log |
| 127 | */ |
| 128 | virtual void storePgoodFaultDebugData( |
| 129 | Services& services, const std::vector<int>& gpioValues, |
| 130 | std::map<std::string, std::string>& additionalData); |
| 131 | |
| 132 | /** |
Shawn McCarney | fe78c17 | 2024-05-02 14:01:46 -0500 | [diff] [blame^] | 133 | * Store GPIO values in the specified additional data map. |
| 134 | * |
| 135 | * The default implementation stores the values as a simple list of |
| 136 | * integers. |
| 137 | * |
| 138 | * Sub-classes should override if more advanced formatting is needed. For |
| 139 | * example, GPIOs could be stored individually with a name and value, or |
| 140 | * related GPIOs could be formatted as a group. |
| 141 | * |
| 142 | * @param services System services like hardware presence and the journal |
| 143 | * @param values GPIO values obtained from the device (if any) |
| 144 | * @param additionalData Additional data to include in an error log |
| 145 | */ |
| 146 | virtual void |
| 147 | storeGPIOValues(Services& services, const std::vector<int>& values, |
| 148 | std::map<std::string, std::string>& additionalData); |
| 149 | |
| 150 | /** |
Shawn McCarney | 472101c | 2024-04-17 16:31:09 -0500 | [diff] [blame] | 151 | * Device name. |
| 152 | */ |
| 153 | std::string name{}; |
| 154 | |
| 155 | /** |
| 156 | * Voltage rails that are enabled and monitored by this device. |
| 157 | */ |
| 158 | std::vector<std::unique_ptr<Rail>> rails{}; |
| 159 | }; |
| 160 | |
| 161 | } // namespace phosphor::power::sequencer |