blob: e02aaea47812b9beb52ac0804110964101e4183b [file] [log] [blame]
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +05301#pragma once
George Liubddcf852021-09-08 08:46:22 +08002#include "config.h"
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +05303
Gunnar Mills94df8c92018-09-14 14:50:03 -05004#include "i2c_occ.hpp"
Chris Caina8857c52021-01-27 11:53:05 -06005#include "occ_command.hpp"
Gunnar Mills94df8c92018-09-14 14:50:03 -05006#include "occ_device.hpp"
7#include "occ_events.hpp"
Chris Cain5d66a0a2022-02-09 08:52:10 -06008#include "powercap.hpp"
Chris Cain36f9cde2021-11-22 11:18:21 -06009#include "powermode.hpp"
George Liuf3b75142021-06-10 11:22:50 +080010#include "utils.hpp"
Gunnar Mills94df8c92018-09-14 14:50:03 -050011
Gunnar Mills94df8c92018-09-14 14:50:03 -050012#include <org/open_power/Control/Host/server.hpp>
13#include <org/open_power/OCC/Status/server.hpp>
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053014#include <sdbusplus/bus.hpp>
15#include <sdbusplus/server/object.hpp>
Chris Caina7b74dc2021-11-10 17:03:43 -060016#ifdef POWER10
17#include <sdeventplus/event.hpp>
18#include <sdeventplus/utility/timer.hpp>
19#endif
Lei YU0ab90ca2017-07-13 17:02:23 +080020
George Liub5ca1012021-09-10 12:53:11 +080021#include <functional>
22
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053023namespace open_power
24{
25namespace occ
26{
27
Edward A. James636577f2017-10-06 10:53:55 -050028class Manager;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053029namespace Base = sdbusplus::org::open_power::OCC::server;
30using Interface = sdbusplus::server::object::object<Base::Status>;
31
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053032// IPMID's host control application
33namespace Control = sdbusplus::org::open_power::Control::server;
34
35// For waiting on signals
36namespace sdbusRule = sdbusplus::bus::match::rules;
37
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +053038// OCC status instance. Ex. for "occ0", the instance is 0
39using instanceID = int;
40
41// IPMI sensor ID for a given OCC instance
42using sensorID = uint8_t;
43
Alexander Filippov1d69e192019-03-21 18:12:07 +030044// Human readable sensor name for DBus tree. E.g. "CPU0_OCC"
45using sensorName = std::string;
46
47// OCC sensors definitions in the map
48using sensorDefs = std::tuple<sensorID, sensorName>;
49
Eddie Jamese7d976b2018-02-26 13:42:45 -060050// OCC sysfs name prefix
51const std::string sysfsName = "occ-hwmon";
52
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053053/** @class Status
54 * @brief Implementation of OCC Active Status
55 */
56class Status : public Interface
57{
Gunnar Mills94df8c92018-09-14 14:50:03 -050058 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;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053065
Gunnar Mills94df8c92018-09-14 14:50:03 -050066 /** @brief Constructs the Status object and
67 * the underlying device object
68 *
Gunnar Mills94df8c92018-09-14 14:50:03 -050069 * @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
Tom Joseph00325232020-07-29 17:51:48 +053074 * @param[in] resetCallBack - callback handler to invoke for resetting the
75 * OCC if PLDM is the host communication
76 * protocol
Gunnar Mills94df8c92018-09-14 14:50:03 -050077 */
Chris Cain17257672021-10-22 13:41:03 -050078 Status(EventPtr& event, const char* path, Manager& managerRef,
Chris Cain36f9cde2021-11-22 11:18:21 -060079#ifdef POWER10
Chris Cain5d66a0a2022-02-09 08:52:10 -060080 std::unique_ptr<powermode::PowerMode>& powerModeRef,
Chris Cain36f9cde2021-11-22 11:18:21 -060081#endif
Sheldon Bailey373af752022-02-21 15:14:00 -060082 std::function<void(instanceID, bool)> callBack = nullptr
Tom Joseph00325232020-07-29 17:51:48 +053083#ifdef PLDM
84 ,
85 std::function<void(instanceID)> resetCallBack = nullptr
86#endif
87 ) :
88
George Liuf3b75142021-06-10 11:22:50 +080089 Interface(utils::getBus(), getDbusPath(path).c_str(), true),
Chris Cain1be43372021-12-09 19:29:37 -060090 path(path), managerCallBack(callBack), instance(getInstance(path)),
Chris Cain17257672021-10-22 13:41:03 -050091 manager(managerRef),
Chris Cain36f9cde2021-11-22 11:18:21 -060092#ifdef POWER10
93 pmode(powerModeRef),
94#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -050095 device(event,
Lei YU0ab90ca2017-07-13 17:02:23 +080096#ifdef I2C_OCC
Eddie James774f9af2019-03-19 20:58:53 +000097 fs::path(DEV_PATH) / i2c_occ::getI2cDeviceName(path),
Lei YU0ab90ca2017-07-13 17:02:23 +080098#else
Eddie James774f9af2019-03-19 20:58:53 +000099 fs::path(DEV_PATH) /
100 fs::path(sysfsName + "." + std::to_string(instance + 1)),
Lei YU0ab90ca2017-07-13 17:02:23 +0800101#endif
Chris Cain17257672021-10-22 13:41:03 -0500102 managerRef, *this, instance),
Gunnar Mills94df8c92018-09-14 14:50:03 -0500103 hostControlSignal(
George Liuf3b75142021-06-10 11:22:50 +0800104 utils::getBus(),
Gunnar Mills94df8c92018-09-14 14:50:03 -0500105 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,
Chris Caina8857c52021-01-27 11:53:05 -0600111 std::placeholders::_1)),
George Liuf3b75142021-06-10 11:22:50 +0800112 occCmd(instance, (fs::path(OCC_CONTROL_ROOT) /
113 (std::string(OCC_NAME) + std::to_string(instance)))
114 .c_str())
Chris Caina7b74dc2021-11-10 17:03:43 -0600115#ifdef POWER10
116 ,
117 sdpEvent(sdeventplus::Event::get_default()),
118 safeStateDelayTimer(
119 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>(
Sheldon Bailey373af752022-02-21 15:14:00 -0600120 sdpEvent, std::bind(&Status::safeStateDelayExpired, this))),
121 occReadStateFailTimer(
122 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>(
123 sdpEvent, std::bind(&Status::occReadStateNow, this)))
Chris Caina7b74dc2021-11-10 17:03:43 -0600124#endif
Sheldon Bailey373af752022-02-21 15:14:00 -0600125
Tom Joseph00325232020-07-29 17:51:48 +0530126#ifdef PLDM
127 ,
128 resetCallBack(resetCallBack)
129#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -0500130 {
131 // Check to see if we have OCC already bound. If so, just set it
132 if (device.bound())
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530133 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500134 this->occActive(true);
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530135 }
136
Gunnar Mills94df8c92018-09-14 14:50:03 -0500137 // Announce that we are ready
138 this->emit_object_added();
139 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530140
Gunnar Mills94df8c92018-09-14 14:50:03 -0500141 /** @brief Since we are overriding the setter-occActive but not the
142 * getter-occActive, we need to have this using in order to
143 * allow passthrough usage of the getter-occActive
144 */
145 using Base::Status::occActive;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530146
Gunnar Mills94df8c92018-09-14 14:50:03 -0500147 /** @brief SET OccActive to True or False
148 *
149 * @param[in] value - Intended value
150 *
151 * @return - Updated value of the property
152 */
153 bool occActive(bool value) override;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530154
Gunnar Mills94df8c92018-09-14 14:50:03 -0500155 /** @brief Starts OCC error detection */
156 inline void addErrorWatch()
157 {
158 return device.addErrorWatch();
159 }
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530160
Gunnar Mills94df8c92018-09-14 14:50:03 -0500161 /** @brief Stops OCC error detection */
162 inline void removeErrorWatch()
163 {
164 return device.removeErrorWatch();
165 }
Eddie Jamesdae2d942017-12-20 10:50:03 -0600166
Gunnar Mills94df8c92018-09-14 14:50:03 -0500167 /** @brief Starts to watch how many OCCs are present on the master */
168 inline void addPresenceWatchMaster()
169 {
170 return device.addPresenceWatchMaster();
171 }
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530172
Chicago Duanbb895cb2021-06-18 19:37:16 +0800173 /** @brief Gets the occ instance number */
174 unsigned int getOccInstanceID()
175 {
176 return instance;
177 }
178
179 /** @brief Is this OCC the master OCC */
180 bool isMasterOcc()
181 {
182 return device.master();
183 }
184
Chris Caina8857c52021-01-27 11:53:05 -0600185 /** @brief Read OCC state (will trigger kernel to poll the OCC) */
186 void readOccState();
187
Eddie Jamescbad2192021-10-07 09:39:39 -0500188 /** @brief Called when device errors are detected */
189 void deviceError();
190
Chris Cain78e86012021-03-04 16:15:31 -0600191#ifdef POWER10
192 /** @brief Handle additional tasks when the OCCs reach active state */
193 void occsWentActive();
194
Chris Cain17257672021-10-22 13:41:03 -0500195 /** @brief Send Ambient & Altitude data to OCC
196 *
197 * @param[in] ambient - temperature to send (0xFF will force read
198 * of current temperature and altitude)
199 * @param[in] altitude - altitude to send (0xFFFF = unavailable)
200 *
201 * @return SUCCESS on success
202 */
203 CmdStatus sendAmbient(const uint8_t ambient = 0xFF,
204 const uint16_t altitude = 0xFFFF);
Chris Cain78e86012021-03-04 16:15:31 -0600205#endif // POWER10
206
Chris Cain5d66a0a2022-02-09 08:52:10 -0600207 /** @brief Return the HWMON path for this OCC
208 *
209 * @return path or empty path if not found
210 */
Chris Caine2d0a432022-03-28 11:08:49 -0500211 fs::path getHwmonPath();
Chris Cain5d66a0a2022-02-09 08:52:10 -0600212
Gunnar Mills94df8c92018-09-14 14:50:03 -0500213 private:
Gunnar Mills94df8c92018-09-14 14:50:03 -0500214 /** @brief OCC dbus object path */
215 std::string path;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530216
Gunnar Mills94df8c92018-09-14 14:50:03 -0500217 /** @brief Callback handler to be invoked during property change.
218 * This is a handler in Manager class
219 */
Sheldon Bailey373af752022-02-21 15:14:00 -0600220 std::function<void(instanceID, bool)> managerCallBack;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530221
Gunnar Mills94df8c92018-09-14 14:50:03 -0500222 /** @brief OCC instance number. Ex, 0,1, etc */
Chris Caina8857c52021-01-27 11:53:05 -0600223 unsigned int instance;
224
225 /** @brief The last state read from the OCC */
226 unsigned int lastState = 0;
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +0530227
Sheldon Bailey373af752022-02-21 15:14:00 -0600228 /** @brief Number of retry attempts to open file and update state. */
229 const unsigned int occReadRetries = 1;
230
231 /** @brief Current number of retries attempted towards occReadRetries. */
232 size_t currentOccReadRetriesCount = 0;
233
234 /** @brief The Trigger to indicate OCC State is valid or not. */
235 bool stateValid = false;
236
Alexander Filippov1d69e192019-03-21 18:12:07 +0300237 /** @brief OCC instance to Sensor definitions mapping */
238 static const std::map<instanceID, sensorDefs> sensorMap;
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +0530239
Chris Cain17257672021-10-22 13:41:03 -0500240 /** @brief OCC manager object */
241 const Manager& manager;
242
Chris Cain36f9cde2021-11-22 11:18:21 -0600243#ifdef POWER10
244 /** @brief OCC PowerMode object */
Chris Cain5d66a0a2022-02-09 08:52:10 -0600245 std::unique_ptr<powermode::PowerMode>& pmode;
Chris Cain36f9cde2021-11-22 11:18:21 -0600246#endif
247
Gunnar Mills94df8c92018-09-14 14:50:03 -0500248 /** @brief OCC device object to do bind and unbind */
249 Device device;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530250
Gunnar Mills94df8c92018-09-14 14:50:03 -0500251 /** @brief Subscribe to host control signal
252 *
253 * Once the OCC reset is requested, BMC sends that message to host.
254 * If the host does not ack the message, then there would be a timeout
255 * and we need to catch that to log an error
256 **/
257 sdbusplus::bus::match_t hostControlSignal;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530258
Chris Caina8857c52021-01-27 11:53:05 -0600259 /** @brief Command object to send commands to the OCC */
260 OccCommand occCmd;
261
Chris Caina7b74dc2021-11-10 17:03:43 -0600262#ifdef POWER10
263 /** @brief timer event */
264 sdeventplus::Event sdpEvent;
265#endif
266
Chris Caine2d0a432022-03-28 11:08:49 -0500267 /** @brief hwmon path for this OCC */
268 fs::path hwmonPath;
269
Gunnar Mills94df8c92018-09-14 14:50:03 -0500270 /** @brief Callback function on host control signals
271 *
272 * @param[in] msg - Data associated with subscribed signal
273 */
274 void hostControlEvent(sdbusplus::message::message& msg);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530275
Gunnar Mills94df8c92018-09-14 14:50:03 -0500276 /** @brief Sends a message to host control command handler to reset OCC
277 */
278 void resetOCC();
Alexander Filippov1d69e192019-03-21 18:12:07 +0300279
280 /** @brief Determines the instance ID by specified object path.
281 * @param[in] path Estimated OCC Dbus object path
282 * @return Instance number
283 */
284 static int getInstance(const std::string& path)
285 {
286 return (path.empty() ? 0 : path.back() - '0');
287 }
288
Chris Cain78e86012021-03-04 16:15:31 -0600289#ifdef POWER10
Chris Caina7b74dc2021-11-10 17:03:43 -0600290 /**
291 * @brief Timer that is started when OCC is detected to be in safe mode
292 */
293 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>
294 safeStateDelayTimer;
295
296 /** @brief Callback for timer that is started when OCC was detected to be in
297 * safe mode. Called to verify and then disable and reset the OCCs.
298 */
299 void safeStateDelayExpired();
Sheldon Bailey373af752022-02-21 15:14:00 -0600300
301 /**
302 * @brief Timer that is started when OCC read Valid state failed.
303 */
304 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>
305 occReadStateFailTimer;
306
Chris Cain78e86012021-03-04 16:15:31 -0600307#endif // POWER10
Sheldon Bailey373af752022-02-21 15:14:00 -0600308 /** @brief Callback for timer that is started when OCC state
309 * was not able to be read. Called to attempt another read when needed.
310 */
311 void occReadStateNow();
Chris Cain78e86012021-03-04 16:15:31 -0600312
Alexander Filippov1d69e192019-03-21 18:12:07 +0300313 /** @brief Override the sensor name with name from the definition.
314 * @param[in] estimatedPath - Estimated OCC Dbus object path
315 * @return Fixed OCC DBus object path
316 */
317 static std::string getDbusPath(const std::string& estimatedPath)
318 {
319 if (!estimatedPath.empty())
320 {
321 auto it = sensorMap.find(getInstance(estimatedPath));
322 if (sensorMap.end() != it)
323 {
324 auto& name = std::get<1>(it->second);
325 if (!name.empty() && name != "None")
326 {
George Liuf3a4a692021-12-28 13:59:51 +0800327 auto objectPath = fs::path(estimatedPath);
328 objectPath.replace_filename(name);
329 return objectPath.string();
Alexander Filippov1d69e192019-03-21 18:12:07 +0300330 }
331 }
332 }
333
334 return estimatedPath;
335 }
Tom Joseph00325232020-07-29 17:51:48 +0530336#ifdef PLDM
337 std::function<void(instanceID)> resetCallBack = nullptr;
338#endif
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530339};
340
341} // namespace occ
342} // namespace open_power