xref: /openbmc/openpower-occ-control/occ_status.hpp (revision 5d66a0aadab248f243bbbcfc6a732ff2a7749079)
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 = 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(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(), true),
90         path(path), managerCallBack(callBack), instance(getInstance(path)),
91         manager(managerRef),
92 #ifdef POWER10
93         pmode(powerModeRef),
94 #endif
95         device(event,
96 #ifdef I2C_OCC
97                fs::path(DEV_PATH) / i2c_occ::getI2cDeviceName(path),
98 #else
99                fs::path(DEV_PATH) /
100                    fs::path(sysfsName + "." + std::to_string(instance + 1)),
101 #endif
102                managerRef, *this, instance),
103         hostControlSignal(
104             utils::getBus(),
105             sdbusRule::type::signal() + sdbusRule::member("CommandComplete") +
106                 sdbusRule::path("/org/open_power/control/host0") +
107                 sdbusRule::interface("org.open_power.Control.Host") +
108                 sdbusRule::argN(0, Control::convertForMessage(
109                                        Control::Host::Command::OCCReset)),
110             std::bind(std::mem_fn(&Status::hostControlEvent), this,
111                       std::placeholders::_1)),
112         occCmd(instance, (fs::path(OCC_CONTROL_ROOT) /
113                           (std::string(OCC_NAME) + std::to_string(instance)))
114                              .c_str())
115 #ifdef POWER10
116         ,
117         sdpEvent(sdeventplus::Event::get_default()),
118         safeStateDelayTimer(
119             sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>(
120                 sdpEvent, std::bind(&Status::safeStateDelayExpired, this)))
121 #endif
122 #ifdef PLDM
123         ,
124         resetCallBack(resetCallBack)
125 #endif
126     {
127         // Check to see if we have OCC already bound.  If so, just set it
128         if (device.bound())
129         {
130             this->occActive(true);
131         }
132 
133         // Announce that we are ready
134         this->emit_object_added();
135     }
136 
137     /** @brief Since we are overriding the setter-occActive but not the
138      *         getter-occActive, we need to have this using in order to
139      *         allow passthrough usage of the getter-occActive
140      */
141     using Base::Status::occActive;
142 
143     /** @brief SET OccActive to True or False
144      *
145      *  @param[in] value - Intended value
146      *
147      *  @return          - Updated value of the property
148      */
149     bool occActive(bool value) override;
150 
151     /** @brief Starts OCC error detection */
152     inline void addErrorWatch()
153     {
154         return device.addErrorWatch();
155     }
156 
157     /** @brief Stops OCC error detection */
158     inline void removeErrorWatch()
159     {
160         return device.removeErrorWatch();
161     }
162 
163     /** @brief Starts to watch how many OCCs are present on the master */
164     inline void addPresenceWatchMaster()
165     {
166         return device.addPresenceWatchMaster();
167     }
168 
169     /** @brief Gets the occ instance number */
170     unsigned int getOccInstanceID()
171     {
172         return instance;
173     }
174 
175     /** @brief Is this OCC the master OCC */
176     bool isMasterOcc()
177     {
178         return device.master();
179     }
180 
181     /** @brief Read OCC state (will trigger kernel to poll the OCC) */
182     void readOccState();
183 
184     /** @brief Called when device errors are detected */
185     void deviceError();
186 
187 #ifdef POWER10
188     /** @brief Handle additional tasks when the OCCs reach active state */
189     void occsWentActive();
190 
191     /** @brief Send Ambient & Altitude data to OCC
192      *
193      *  @param[in] ambient - temperature to send (0xFF will force read
194      *                       of current temperature and altitude)
195      *  @param[in] altitude - altitude to send (0xFFFF = unavailable)
196      *
197      *  @return SUCCESS on success
198      */
199     CmdStatus sendAmbient(const uint8_t ambient = 0xFF,
200                           const uint16_t altitude = 0xFFFF);
201 #endif // POWER10
202 
203     /** @brief Return the HWMON path for this OCC
204      *
205      *  @return path or empty path if not found
206      */
207     fs::path getHwmonPath() const;
208 
209   private:
210     /** @brief OCC dbus object path */
211     std::string path;
212 
213     /** @brief Callback handler to be invoked during property change.
214      *         This is a handler in Manager class
215      */
216     std::function<void(bool)> managerCallBack;
217 
218     /** @brief OCC instance number. Ex, 0,1, etc */
219     unsigned int instance;
220 
221     /** @brief The last state read from the OCC */
222     unsigned int lastState = 0;
223 
224     /** @brief OCC instance to Sensor definitions mapping */
225     static const std::map<instanceID, sensorDefs> sensorMap;
226 
227     /** @brief OCC manager object */
228     const Manager& manager;
229 
230     /** @brief Power cap monitor and occ notification object */
231     std::unique_ptr<powercap::PowerCap> pcap;
232 
233 #ifdef POWER10
234     /** @brief OCC PowerMode object */
235     std::unique_ptr<powermode::PowerMode>& pmode;
236 #endif
237 
238     /** @brief OCC device object to do bind and unbind */
239     Device device;
240 
241     /** @brief Subscribe to host control signal
242      *
243      *  Once the OCC reset is requested, BMC sends that message to host.
244      *  If the host does not ack the message, then there would be a timeout
245      *  and we need to catch that to log an error
246      **/
247     sdbusplus::bus::match_t hostControlSignal;
248 
249     /** @brief Command object to send commands to the OCC */
250     OccCommand occCmd;
251 
252 #ifdef POWER10
253     /** @brief timer event */
254     sdeventplus::Event sdpEvent;
255 #endif
256 
257     /** @brief Callback function on host control signals
258      *
259      *  @param[in]  msg - Data associated with subscribed signal
260      */
261     void hostControlEvent(sdbusplus::message::message& msg);
262 
263     /** @brief Sends a message to host control command handler to reset OCC
264      */
265     void resetOCC();
266 
267     /** @brief Determines the instance ID by specified object path.
268      *  @param[in]  path  Estimated OCC Dbus object path
269      *  @return  Instance number
270      */
271     static int getInstance(const std::string& path)
272     {
273         return (path.empty() ? 0 : path.back() - '0');
274     }
275 
276 #ifdef POWER10
277     /**
278      * @brief Timer that is started when OCC is detected to be in safe mode
279      */
280     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>
281         safeStateDelayTimer;
282 
283     /** @brief Callback for timer that is started when OCC was detected to be in
284      * safe mode. Called to verify and then disable and reset the OCCs.
285      */
286     void safeStateDelayExpired();
287 #endif // POWER10
288 
289     /** @brief Override the sensor name with name from the definition.
290      *  @param[in]  estimatedPath - Estimated OCC Dbus object path
291      *  @return  Fixed OCC DBus object path
292      */
293     static std::string getDbusPath(const std::string& estimatedPath)
294     {
295         if (!estimatedPath.empty())
296         {
297             auto it = sensorMap.find(getInstance(estimatedPath));
298             if (sensorMap.end() != it)
299             {
300                 auto& name = std::get<1>(it->second);
301                 if (!name.empty() && name != "None")
302                 {
303                     auto objectPath = fs::path(estimatedPath);
304                     objectPath.replace_filename(name);
305                     return objectPath.string();
306                 }
307             }
308         }
309 
310         return estimatedPath;
311     }
312 #ifdef PLDM
313     std::function<void(instanceID)> resetCallBack = nullptr;
314 #endif
315 };
316 
317 } // namespace occ
318 } // namespace open_power
319