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