1 #pragma once
2 
3 #include "config.h"
4 
5 #include "occ_errors.hpp"
6 #include "occ_events.hpp"
7 #include "occ_ffdc.hpp"
8 #include "occ_presence.hpp"
9 #include "powermode.hpp"
10 
11 #include <org/open_power/OCC/Device/error.hpp>
12 
13 #include <filesystem>
14 #include <fstream>
15 #include <regex>
16 
17 namespace open_power
18 {
19 namespace occ
20 {
21 
22 class Manager;
23 class Status;
24 namespace fs = std::filesystem;
25 using namespace sdbusplus::org::open_power::OCC::Device::Error;
26 
27 /** @class Device
28  *  @brief Binds and unbinds the OCC driver upon request
29  */
30 class Device
31 {
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;
39 
40     /** @brief Constructs the Device object
41      *
42      *  @param[in] event    - Unique ptr reference to sd_event
43      *  @param[in] path     - Path to the OCC instance
44      *  @param[in] manager  - OCC manager instance
45      *  @param[in] status   - Status instance
46      *  @param[in] instance - OCC instance number
47      */
Device(EventPtr & event,const fs::path & path,Manager & manager,Status & status,std::unique_ptr<powermode::PowerMode> & powerModeRef,unsigned int instance=0)48     Device(EventPtr& event, const fs::path& path, Manager& manager,
49            Status& status,
50 #ifdef POWER10
51            std::unique_ptr<powermode::PowerMode>& powerModeRef,
52 #endif
53            unsigned int instance = 0) :
54         devPath(path),
55         instance(instance), statusObject(status), managerObject(manager),
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                     ),
70         ffdc(event, path / "ffdc", instance),
71         presence(event, path / "occs_present", manager,
72                  std::bind(std::mem_fn(&Device::errorCallback), this,
73                            std::placeholders::_1)),
74         throttleProcTemp(
75             event, path / "occ_dvfs_overtemp",
76             std::bind(std::mem_fn(&Device::throttleProcTempCallback), this,
77                       std::placeholders::_1)),
78         throttleProcPower(
79             event, path / "occ_dvfs_power",
80             std::bind(std::mem_fn(&Device::throttleProcPowerCallback), this,
81                       std::placeholders::_1)),
82         throttleMemTemp(event, path / "occ_mem_throttle",
83                         std::bind(std::mem_fn(&Device::throttleMemTempCallback),
84                                   this, std::placeholders::_1))
85 #ifdef POWER10
86         ,
87         pmode(powerModeRef)
88 #endif
89     {
90         // Nothing to do here
91     }
92 
93     /** @brief Sets the device active or inactive
94      *
95      * @param[in] active - Indicates whether or not to set the device active
96      */
97     void setActive(bool active);
98 
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      */
addErrorWatch(bool poll=true)106     inline void addErrorWatch(bool poll = true)
107     {
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 
119 #ifdef POWER10
120         if (master())
121         {
122             pmode->addIpsWatch(poll);
123         }
124 #endif
125 
126         throttleProcPower.addWatch(poll);
127         throttleMemTemp.addWatch(poll);
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 
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 
147         error.addWatch(poll);
148     }
149 
150     /** @brief stops monitoring for errors */
removeErrorWatch()151     inline void removeErrorWatch()
152     {
153         // we can always safely remove watch even if we don't add it
154         presence.removeWatch();
155         ffdc.removeWatch();
156         error.removeWatch();
157         timeout.removeWatch();
158         throttleMemTemp.removeWatch();
159         throttleProcPower.removeWatch();
160         throttleProcTemp.removeWatch();
161 #ifdef POWER10
162         if (master())
163         {
164             pmode->removeIpsWatch();
165         }
166 #endif
167     }
168 
169     /** @brief Starts to watch how many OCCs are present on the master */
addPresenceWatchMaster()170     inline void addPresenceWatchMaster()
171     {
172         if (master())
173         {
174             presence.addWatch();
175         }
176     }
177 
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 
185     /** @brief Returns true if the device is active */
186     bool active() const;
187 
188     /** @brief Returns true if device represents the master OCC */
189     bool master() const;
190 
191   private:
192     /** @brief This directory contains the error files */
193     const fs::path devPath;
194 
195     /** @brief OCC instance ID */
196     const unsigned int instance;
197 
198     /**  Store the associated Status instance */
199     Status& statusObject;
200 
201     /** Store the parent Manager instance */
202     Manager& managerObject;
203 
204     /** Abstraction of error monitoring */
205     Error error;
206 
207     /** Abstraction of SBE timeout monitoring */
208     Error timeout;
209 
210     /** SBE FFDC monitoring */
211     FFDC ffdc;
212 
213     /** Abstraction of OCC presence monitoring */
214     Presence presence;
215 
216     /** Error instances for watching for throttling events */
217     Error throttleProcTemp;
218     Error throttleProcPower;
219     Error throttleMemTemp;
220 
221 #ifdef POWER10
222     /** @brief OCC PowerMode object */
223     std::unique_ptr<powermode::PowerMode>& pmode;
224 #endif
225 
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 
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      */
write(const fs::path & fileName,const std::string & data)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     }
247 
248     /** @brief callback for OCC error monitoring
249      *
250      * @param[in] error - Errno stored in the error file, 0 if no error
251      */
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);
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      */
266     void timeoutCallback(int error);
267 #endif
268 
269     /** @brief callback for the proc temp throttle event
270      *
271      *  @param[in] error - True if an error is reported, false otherwise
272      */
273     void throttleProcTempCallback(int error);
274 
275     /** @brief callback for the proc power throttle event
276      *
277      *  @param[in] error - True if an error is reported, false otherwise
278      */
279     void throttleProcPowerCallback(int error);
280 
281     /** @brief callback for the proc temp throttle event
282      *
283      *  @param[in] error - True if an error is reported, false otherwise
284      */
285     void throttleMemTempCallback(int error);
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      */
294     fs::path getFilenameByRegex(fs::path basePath,
295                                 const std::regex& expr) const;
296 };
297 
298 } // namespace occ
299 } // namespace open_power
300