xref: /openbmc/openpower-occ-control/occ_status.hpp (revision bae4d07e6b877f920acca7b09d9334a76c0c9eaf)
1 #pragma once
2 #include "config.h"
3 
4 #include "i2c_occ.hpp"
5 #include "occ_command.hpp"
6 #include "occ_device.hpp"
7 #include "occ_events.hpp"
8 #include "powercap.hpp"
9 #include "powermode.hpp"
10 #include "utils.hpp"
11 
12 #include <org/open_power/Control/Host/server.hpp>
13 #include <org/open_power/OCC/Status/server.hpp>
14 #include <sdbusplus/bus.hpp>
15 #include <sdbusplus/server/object.hpp>
16 #ifdef POWER10
17 #include <sdeventplus/event.hpp>
18 #include <sdeventplus/utility/timer.hpp>
19 #endif
20 
21 #include <functional>
22 
23 namespace open_power
24 {
25 namespace occ
26 {
27 
28 class Manager;
29 namespace Base = sdbusplus::org::open_power::OCC::server;
30 using Interface = sdbusplus::server::object::object<Base::Status>;
31 
32 // IPMID's host control application
33 namespace Control = sdbusplus::org::open_power::Control::server;
34 
35 // For waiting on signals
36 namespace sdbusRule = sdbusplus::bus::match::rules;
37 
38 // OCC status instance. Ex. for "occ0", the instance is 0
39 using instanceID = unsigned int;
40 
41 // IPMI sensor ID for a given OCC instance
42 using sensorID = uint8_t;
43 
44 // Human readable sensor name for DBus tree. E.g. "CPU0_OCC"
45 using sensorName = std::string;
46 
47 // OCC sensors definitions in the map
48 using sensorDefs = std::tuple<sensorID, sensorName>;
49 
50 // OCC sysfs name prefix
51 const std::string sysfsName = "occ-hwmon";
52 
53 /** @class Status
54  *  @brief Implementation of OCC Active Status
55  */
56 class Status : public Interface
57 {
58   public:
59     Status() = delete;
60     ~Status() = default;
61     Status(const Status&) = delete;
62     Status& operator=(const Status&) = delete;
63     Status(Status&&) = default;
64     Status& operator=(Status&&) = default;
65 
66     /** @brief Constructs the Status object and
67      *         the underlying device object
68      *
69      *  @param[in] event    - sd_event unique pointer reference
70      *  @param[in] path     - DBus object path
71      *  @param[in] manager  - OCC manager instance
72      *  @param[in] callBack - Callback handler to invoke during
73      *                        property change
74      *  @param[in] resetCallBack - callback handler to invoke for resetting the
75      *                             OCC if PLDM is the host communication
76      *                             protocol
77      */
78     Status(EventPtr& event, const char* path, Manager& managerRef,
79 #ifdef POWER10
80            std::unique_ptr<powermode::PowerMode>& powerModeRef,
81 #endif
82            std::function<void(instanceID, bool)> callBack = nullptr
83 #ifdef PLDM
84            ,
85            std::function<void(instanceID)> resetCallBack = nullptr
86 #endif
87            ) :
88 
89         Interface(utils::getBus(), getDbusPath(path).c_str(),
90                   Interface::action::defer_emit),
91         path(path), managerCallBack(callBack), instance(getInstance(path)),
92         manager(managerRef),
93 #ifdef POWER10
94         pmode(powerModeRef),
95 #endif
96         device(event,
97 #ifdef I2C_OCC
98                fs::path(DEV_PATH) / i2c_occ::getI2cDeviceName(path),
99 #else
100                fs::path(DEV_PATH) /
101                    fs::path(sysfsName + "." + std::to_string(instance + 1)),
102 #endif
103                managerRef, *this, instance),
104         hostControlSignal(
105             utils::getBus(),
106             sdbusRule::type::signal() + sdbusRule::member("CommandComplete") +
107                 sdbusRule::path("/org/open_power/control/host0") +
108                 sdbusRule::interface("org.open_power.Control.Host") +
109                 sdbusRule::argN(0, Control::convertForMessage(
110                                        Control::Host::Command::OCCReset)),
111             std::bind(std::mem_fn(&Status::hostControlEvent), this,
112                       std::placeholders::_1)),
113         occCmd(instance, (fs::path(OCC_CONTROL_ROOT) /
114                           (std::string(OCC_NAME) + std::to_string(instance)))
115                              .c_str())
116 #ifdef POWER10
117         ,
118         sdpEvent(sdeventplus::Event::get_default()),
119         safeStateDelayTimer(
120             sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>(
121                 sdpEvent, std::bind(&Status::safeStateDelayExpired, this))),
122         occReadStateFailTimer(
123             sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>(
124                 sdpEvent, std::bind(&Status::occReadStateNow, this)))
125 #endif
126 
127 #ifdef PLDM
128         ,
129         resetCallBack(resetCallBack)
130 #endif
131     {
132         // Announce that we are ready
133         this->emit_object_added();
134     }
135 
136     /** @brief Since we are overriding the setter-occActive but not the
137      *         getter-occActive, we need to have this using in order to
138      *         allow passthrough usage of the getter-occActive
139      */
140     using Base::Status::occActive;
141 
142     /** @brief SET OccActive to True or False
143      *
144      *  @param[in] value - Intended value
145      *
146      *  @return          - Updated value of the property
147      */
148     bool occActive(bool value) override;
149 
150     /** @brief Starts OCC error detection */
151     inline void addErrorWatch()
152     {
153         return device.addErrorWatch();
154     }
155 
156     /** @brief Stops OCC error detection */
157     inline void removeErrorWatch()
158     {
159         return device.removeErrorWatch();
160     }
161 
162     /** @brief Starts to watch how many OCCs are present on the master */
163     inline void addPresenceWatchMaster()
164     {
165         return device.addPresenceWatchMaster();
166     }
167 
168     /** @brief Gets the occ instance number */
169     unsigned int getOccInstanceID()
170     {
171         return instance;
172     }
173 
174     /** @brief Is this OCC the master OCC */
175     bool isMasterOcc()
176     {
177         return device.master();
178     }
179 
180     /** @brief Read OCC state (will trigger kernel to poll the OCC) */
181     void readOccState();
182 
183     /** @brief Called when device errors are detected */
184     void deviceError();
185 
186 #ifdef POWER10
187     /** @brief Handle additional tasks when the OCCs reach active state */
188     void occsWentActive();
189 
190     /** @brief Send Ambient & Altitude data to OCC
191      *
192      *  @param[in] ambient - temperature to send (0xFF will force read
193      *                       of current temperature and altitude)
194      *  @param[in] altitude - altitude to send (0xFFFF = unavailable)
195      *
196      *  @return SUCCESS on success
197      */
198     CmdStatus sendAmbient(const uint8_t ambient = 0xFF,
199                           const uint16_t altitude = 0xFFFF);
200 #endif // POWER10
201 
202     /** @brief Return the HWMON path for this OCC
203      *
204      *  @return path or empty path if not found
205      */
206     fs::path getHwmonPath();
207 
208   private:
209     /** @brief OCC dbus object path */
210     std::string path;
211 
212     /** @brief Callback handler to be invoked during property change.
213      *         This is a handler in Manager class
214      */
215     std::function<void(instanceID, bool)> managerCallBack;
216 
217     /** @brief OCC instance number. Ex, 0,1, etc */
218     unsigned int instance;
219 
220     /** @brief The last state read from the OCC */
221     unsigned int lastState = 0;
222 
223     /** @brief Number of retry attempts to open file and update state. */
224     const unsigned int occReadRetries = 1;
225 
226     /** @brief Current number of retries attempted towards occReadRetries. */
227     size_t currentOccReadRetriesCount = 0;
228 
229     /** @brief The Trigger to indicate OCC State is valid or not. */
230     bool stateValid = false;
231 
232     /** @brief OCC instance to Sensor definitions mapping */
233     static const std::map<instanceID, sensorDefs> sensorMap;
234 
235     /** @brief OCC manager object */
236     const Manager& manager;
237 
238 #ifdef POWER10
239     /** @brief OCC PowerMode object */
240     std::unique_ptr<powermode::PowerMode>& pmode;
241 #endif
242 
243     /** @brief OCC device object to do bind and unbind */
244     Device device;
245 
246     /** @brief Subscribe to host control signal
247      *
248      *  Once the OCC reset is requested, BMC sends that message to host.
249      *  If the host does not ack the message, then there would be a timeout
250      *  and we need to catch that to log an error
251      **/
252     sdbusplus::bus::match_t hostControlSignal;
253 
254     /** @brief Command object to send commands to the OCC */
255     OccCommand occCmd;
256 
257 #ifdef POWER10
258     /** @brief timer event */
259     sdeventplus::Event sdpEvent;
260 #endif
261 
262     /** @brief hwmon path for this OCC */
263     fs::path hwmonPath;
264 
265     /** @brief Callback function on host control signals
266      *
267      *  @param[in]  msg - Data associated with subscribed signal
268      */
269     void hostControlEvent(sdbusplus::message::message& msg);
270 
271     /** @brief Sends a message to host control command handler to reset OCC
272      */
273     void resetOCC();
274 
275     /** @brief Determines the instance ID by specified object path.
276      *  @param[in]  path  Estimated OCC Dbus object path
277      *  @return  Instance number
278      */
279     static int getInstance(const std::string& path)
280     {
281         return (path.empty() ? 0 : path.back() - '0');
282     }
283 
284 #ifdef POWER10
285     /**
286      * @brief Timer that is started when OCC is detected to be in safe mode
287      */
288     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>
289         safeStateDelayTimer;
290 
291     /** @brief Callback for timer that is started when OCC was detected to be in
292      * safe mode. Called to verify and then disable and reset the OCCs.
293      */
294     void safeStateDelayExpired();
295 
296     /**
297      * @brief Timer that is started when OCC read Valid state failed.
298      */
299     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>
300         occReadStateFailTimer;
301 
302 #endif // POWER10
303     /** @brief Callback for timer that is started when OCC state
304      * was not able to be read. Called to attempt another read when needed.
305      */
306     void occReadStateNow();
307 
308     /** @brief Override the sensor name with name from the definition.
309      *  @param[in]  estimatedPath - Estimated OCC Dbus object path
310      *  @return  Fixed OCC DBus object path
311      */
312     static std::string getDbusPath(const std::string& estimatedPath)
313     {
314         if (!estimatedPath.empty())
315         {
316             auto it = sensorMap.find(getInstance(estimatedPath));
317             if (sensorMap.end() != it)
318             {
319                 auto& name = std::get<1>(it->second);
320                 if (!name.empty() && name != "None")
321                 {
322                     auto objectPath = fs::path(estimatedPath);
323                     objectPath.replace_filename(name);
324                     return objectPath.string();
325                 }
326             }
327         }
328 
329         return estimatedPath;
330     }
331 #ifdef PLDM
332     std::function<void(instanceID)> resetCallBack = nullptr;
333 #endif
334 };
335 
336 } // namespace occ
337 } // namespace open_power
338