Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 3 | #include "config.h" |
Lei YU | 0ab90ca | 2017-07-13 17:02:23 +0800 | [diff] [blame] | 4 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 5 | #include "occ_errors.hpp" |
| 6 | #include "occ_events.hpp" |
Eddie James | 2f9f9bb | 2021-09-20 14:26:31 -0500 | [diff] [blame] | 7 | #include "occ_ffdc.hpp" |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 8 | #include "occ_presence.hpp" |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame] | 9 | #include "powermode.hpp" |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 10 | |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 11 | #include <org/open_power/OCC/Device/error.hpp> |
| 12 | |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 13 | #include <filesystem> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 14 | #include <fstream> |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 15 | #include <regex> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 16 | |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 17 | namespace open_power |
| 18 | { |
| 19 | namespace occ |
| 20 | { |
| 21 | |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 22 | class Manager; |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 23 | class Status; |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 24 | namespace fs = std::filesystem; |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 25 | using namespace sdbusplus::org::open_power::OCC::Device::Error; |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 26 | |
| 27 | /** @class Device |
| 28 | * @brief Binds and unbinds the OCC driver upon request |
| 29 | */ |
| 30 | class Device |
| 31 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 32 | public: |
| 33 | Device() = delete; |
| 34 | ~Device() = default; |
| 35 | Device(const Device&) = delete; |
| 36 | Device& operator=(const Device&) = delete; |
| 37 | Device(Device&&) = default; |
| 38 | Device& operator=(Device&&) = default; |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 39 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 40 | /** @brief Constructs the Device object |
| 41 | * |
| 42 | * @param[in] event - Unique ptr reference to sd_event |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 43 | * @param[in] path - Path to the OCC instance |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 44 | * @param[in] manager - OCC manager instance |
Eddie James | 2f9f9bb | 2021-09-20 14:26:31 -0500 | [diff] [blame] | 45 | * @param[in] status - Status instance |
| 46 | * @param[in] instance - OCC instance number |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 47 | */ |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 48 | Device(EventPtr& event, const fs::path& path, Manager& manager, |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame] | 49 | Status& status, |
| 50 | #ifdef POWER10 |
| 51 | std::unique_ptr<powermode::PowerMode>& powerModeRef, |
| 52 | #endif |
| 53 | unsigned int instance = 0) : |
Patrick Williams | d7542c8 | 2024-08-16 15:20:28 -0400 | [diff] [blame^] | 54 | devPath(path), instance(instance), statusObject(status), |
| 55 | managerObject(manager), |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 56 | error(event, path / "occ_error", |
| 57 | std::bind(std::mem_fn(&Device::errorCallback), this, |
| 58 | std::placeholders::_1)), |
| 59 | timeout(event, |
| 60 | path / |
| 61 | fs::path("../../sbefifo" + std::to_string(instance + 1)) / |
| 62 | "timeout", |
| 63 | #ifdef PLDM |
| 64 | std::bind(std::mem_fn(&Device::timeoutCallback), this, |
| 65 | std::placeholders::_1) |
| 66 | #else |
| 67 | nullptr |
| 68 | #endif |
| 69 | ), |
Eddie James | 2f9f9bb | 2021-09-20 14:26:31 -0500 | [diff] [blame] | 70 | ffdc(event, path / "ffdc", instance), |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 71 | presence(event, path / "occs_present", manager, |
| 72 | std::bind(std::mem_fn(&Device::errorCallback), this, |
| 73 | std::placeholders::_1)), |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 74 | throttleProcTemp( |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 75 | event, path / "occ_dvfs_overtemp", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 76 | std::bind(std::mem_fn(&Device::throttleProcTempCallback), this, |
| 77 | std::placeholders::_1)), |
| 78 | throttleProcPower( |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 79 | event, path / "occ_dvfs_power", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 80 | std::bind(std::mem_fn(&Device::throttleProcPowerCallback), this, |
| 81 | std::placeholders::_1)), |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 82 | throttleMemTemp(event, path / "occ_mem_throttle", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 83 | std::bind(std::mem_fn(&Device::throttleMemTempCallback), |
| 84 | this, std::placeholders::_1)) |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame] | 85 | #ifdef POWER10 |
| 86 | , |
| 87 | pmode(powerModeRef) |
| 88 | #endif |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 89 | { |
| 90 | // Nothing to do here |
| 91 | } |
| 92 | |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 93 | /** @brief Sets the device active or inactive |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 94 | * |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 95 | * @param[in] active - Indicates whether or not to set the device active |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 96 | */ |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 97 | void setActive(bool active); |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 98 | |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 99 | /** @brief Starts to monitor for errors |
| 100 | * |
| 101 | * @param[in] poll - Indicates whether or not the error file should |
| 102 | * actually be polled for changes. Disabling polling is |
| 103 | * necessary for error files that don't support the poll |
| 104 | * file operation. |
| 105 | */ |
| 106 | inline void addErrorWatch(bool poll = true) |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 107 | { |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 108 | try |
| 109 | { |
| 110 | throttleProcTemp.addWatch(poll); |
| 111 | } |
| 112 | catch (const OpenFailure& e) |
| 113 | { |
| 114 | // try the old kernel version |
| 115 | throttleProcTemp.setFile(devPath / "occ_dvfs_ot"); |
| 116 | throttleProcTemp.addWatch(poll); |
| 117 | } |
| 118 | |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame] | 119 | #ifdef POWER10 |
| 120 | if (master()) |
| 121 | { |
| 122 | pmode->addIpsWatch(poll); |
| 123 | } |
| 124 | #endif |
| 125 | |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 126 | throttleProcPower.addWatch(poll); |
| 127 | throttleMemTemp.addWatch(poll); |
Eddie James | 2f9f9bb | 2021-09-20 14:26:31 -0500 | [diff] [blame] | 128 | |
| 129 | try |
| 130 | { |
| 131 | ffdc.addWatch(poll); |
| 132 | } |
| 133 | catch (const OpenFailure& e) |
| 134 | { |
| 135 | // nothing to do if there is no FFDC file |
| 136 | } |
| 137 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 138 | try |
| 139 | { |
| 140 | timeout.addWatch(poll); |
| 141 | } |
| 142 | catch (const std::exception& e) |
| 143 | { |
| 144 | // nothing to do if there is no SBE timeout file |
| 145 | } |
| 146 | |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 147 | error.addWatch(poll); |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** @brief stops monitoring for errors */ |
| 151 | inline void removeErrorWatch() |
| 152 | { |
| 153 | // we can always safely remove watch even if we don't add it |
| 154 | presence.removeWatch(); |
Eddie James | 2f9f9bb | 2021-09-20 14:26:31 -0500 | [diff] [blame] | 155 | ffdc.removeWatch(); |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 156 | error.removeWatch(); |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 157 | timeout.removeWatch(); |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 158 | throttleMemTemp.removeWatch(); |
| 159 | throttleProcPower.removeWatch(); |
| 160 | throttleProcTemp.removeWatch(); |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame] | 161 | #ifdef POWER10 |
| 162 | if (master()) |
| 163 | { |
| 164 | pmode->removeIpsWatch(); |
| 165 | } |
| 166 | #endif |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** @brief Starts to watch how many OCCs are present on the master */ |
| 170 | inline void addPresenceWatchMaster() |
| 171 | { |
| 172 | if (master()) |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 173 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 174 | presence.addWatch(); |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 175 | } |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 176 | } |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 177 | |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 178 | /** @brief helper function to get the last part of the path |
| 179 | * |
| 180 | * @param[in] path - Path to parse |
| 181 | * @return - Last directory name in the path |
| 182 | */ |
| 183 | static std::string getPathBack(const fs::path& path); |
| 184 | |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 185 | /** @brief Returns true if the device is active */ |
| 186 | bool active() const; |
| 187 | |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 188 | /** @brief Returns true if device represents the master OCC */ |
| 189 | bool master() const; |
| 190 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 191 | private: |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 192 | /** @brief This directory contains the error files */ |
| 193 | const fs::path devPath; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 194 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 195 | /** @brief OCC instance ID */ |
| 196 | const unsigned int instance; |
| 197 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 198 | /** Store the associated Status instance */ |
| 199 | Status& statusObject; |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 200 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 201 | /** Store the parent Manager instance */ |
| 202 | Manager& managerObject; |
| 203 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 204 | /** Abstraction of error monitoring */ |
| 205 | Error error; |
Eddie James | dae2d94 | 2017-12-20 10:50:03 -0600 | [diff] [blame] | 206 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 207 | /** Abstraction of SBE timeout monitoring */ |
| 208 | Error timeout; |
| 209 | |
Eddie James | 2f9f9bb | 2021-09-20 14:26:31 -0500 | [diff] [blame] | 210 | /** SBE FFDC monitoring */ |
| 211 | FFDC ffdc; |
| 212 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 213 | /** Abstraction of OCC presence monitoring */ |
| 214 | Presence presence; |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 215 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 216 | /** Error instances for watching for throttling events */ |
| 217 | Error throttleProcTemp; |
| 218 | Error throttleProcPower; |
| 219 | Error throttleMemTemp; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 220 | |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame] | 221 | #ifdef POWER10 |
| 222 | /** @brief OCC PowerMode object */ |
| 223 | std::unique_ptr<powermode::PowerMode>& pmode; |
| 224 | #endif |
| 225 | |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 226 | /** @brief file reader to read a binary string ("1" or "0") |
| 227 | * |
| 228 | * @param[in] fileName - Name of file to be read |
| 229 | * @return - The value returned by reading the file |
| 230 | */ |
| 231 | bool readBinary(const std::string& fileName) const; |
| 232 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 233 | /** @brief file writer to achieve bind and unbind |
| 234 | * |
| 235 | * @param[in] filename - Name of file to be written |
| 236 | * @param[in] data - Data to be written to |
| 237 | * @return - None |
| 238 | */ |
| 239 | void write(const fs::path& fileName, const std::string& data) |
| 240 | { |
| 241 | // If there is an error, move the exception all the way up |
| 242 | std::ofstream file(fileName, std::ios::out); |
| 243 | file << data; |
| 244 | file.close(); |
| 245 | return; |
| 246 | } |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 247 | |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 248 | /** @brief callback for OCC error monitoring |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 249 | * |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 250 | * @param[in] error - Errno stored in the error file, 0 if no error |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 251 | */ |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 252 | void errorCallback(int error); |
| 253 | |
| 254 | /** @brief callback for OCC presence monitoring |
| 255 | * |
| 256 | * @param[in] occsPresent - The number of OCCs indicated in the poll |
| 257 | * response |
| 258 | */ |
| 259 | void presenceCallback(int occsPresent); |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 260 | |
| 261 | #ifdef PLDM |
| 262 | /** @brief callback for SBE timeout monitoring |
| 263 | * |
| 264 | * @param[in] error - True if an error is reported, false otherwise |
| 265 | */ |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 266 | void timeoutCallback(int error); |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 267 | #endif |
| 268 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 269 | /** @brief callback for the proc temp throttle event |
| 270 | * |
| 271 | * @param[in] error - True if an error is reported, false otherwise |
| 272 | */ |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 273 | void throttleProcTempCallback(int error); |
Eddie James | 482e31f | 2017-09-14 13:17:17 -0500 | [diff] [blame] | 274 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 275 | /** @brief callback for the proc power throttle event |
| 276 | * |
| 277 | * @param[in] error - True if an error is reported, false otherwise |
| 278 | */ |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 279 | void throttleProcPowerCallback(int error); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 280 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 281 | /** @brief callback for the proc temp throttle event |
| 282 | * |
| 283 | * @param[in] error - True if an error is reported, false otherwise |
| 284 | */ |
Eddie James | 9789e71 | 2022-05-25 15:43:40 -0500 | [diff] [blame] | 285 | void throttleMemTempCallback(int error); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 286 | |
| 287 | /** @brief Get the pathname for a file based on a regular expression |
| 288 | * |
| 289 | * @param[in] basePath - The path where the files will be checked |
| 290 | * @param[in] expr - Regular expression describing the target file |
| 291 | * |
| 292 | * @return path to the file or empty path if not found |
| 293 | */ |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 294 | fs::path getFilenameByRegex(fs::path basePath, |
| 295 | const std::regex& expr) const; |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 296 | }; |
| 297 | |
| 298 | } // namespace occ |
| 299 | } // namespace open_power |