blob: 1cbcdb3511578907e7408864611bae7aa021325e [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"
George Liuf3b75142021-06-10 11:22:50 +08008#include "utils.hpp"
Gunnar Mills94df8c92018-09-14 14:50:03 -05009
Gunnar Mills94df8c92018-09-14 14:50:03 -050010#include <org/open_power/Control/Host/server.hpp>
11#include <org/open_power/OCC/Status/server.hpp>
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053012#include <sdbusplus/bus.hpp>
13#include <sdbusplus/server/object.hpp>
Chris Caina7b74dc2021-11-10 17:03:43 -060014#ifdef POWER10
15#include <sdeventplus/event.hpp>
16#include <sdeventplus/utility/timer.hpp>
17#endif
Lei YU0ab90ca2017-07-13 17:02:23 +080018
George Liub5ca1012021-09-10 12:53:11 +080019#include <functional>
20
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053021namespace open_power
22{
23namespace occ
24{
25
Edward A. James636577f2017-10-06 10:53:55 -050026class Manager;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053027namespace Base = sdbusplus::org::open_power::OCC::server;
28using Interface = sdbusplus::server::object::object<Base::Status>;
29
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053030// IPMID's host control application
31namespace Control = sdbusplus::org::open_power::Control::server;
32
33// For waiting on signals
34namespace sdbusRule = sdbusplus::bus::match::rules;
35
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +053036// OCC status instance. Ex. for "occ0", the instance is 0
37using instanceID = int;
38
39// IPMI sensor ID for a given OCC instance
40using sensorID = uint8_t;
41
Alexander Filippov1d69e192019-03-21 18:12:07 +030042// Human readable sensor name for DBus tree. E.g. "CPU0_OCC"
43using sensorName = std::string;
44
45// OCC sensors definitions in the map
46using sensorDefs = std::tuple<sensorID, sensorName>;
47
Eddie Jamese7d976b2018-02-26 13:42:45 -060048// OCC sysfs name prefix
49const std::string sysfsName = "occ-hwmon";
50
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053051/** @class Status
52 * @brief Implementation of OCC Active Status
53 */
54class Status : public Interface
55{
Gunnar Mills94df8c92018-09-14 14:50:03 -050056 public:
57 Status() = delete;
58 ~Status() = default;
59 Status(const Status&) = delete;
60 Status& operator=(const Status&) = delete;
61 Status(Status&&) = default;
62 Status& operator=(Status&&) = default;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053063
Gunnar Mills94df8c92018-09-14 14:50:03 -050064 /** @brief Constructs the Status object and
65 * the underlying device object
66 *
Gunnar Mills94df8c92018-09-14 14:50:03 -050067 * @param[in] event - sd_event unique pointer reference
68 * @param[in] path - DBus object path
69 * @param[in] manager - OCC manager instance
70 * @param[in] callBack - Callback handler to invoke during
71 * property change
Tom Joseph00325232020-07-29 17:51:48 +053072 * @param[in] resetCallBack - callback handler to invoke for resetting the
73 * OCC if PLDM is the host communication
74 * protocol
Gunnar Mills94df8c92018-09-14 14:50:03 -050075 */
Chris Cain17257672021-10-22 13:41:03 -050076 Status(EventPtr& event, const char* path, Manager& managerRef,
George Liuf3b75142021-06-10 11:22:50 +080077 std::function<void(bool)> callBack = nullptr
Tom Joseph00325232020-07-29 17:51:48 +053078#ifdef PLDM
79 ,
80 std::function<void(instanceID)> resetCallBack = nullptr
81#endif
82 ) :
83
George Liuf3b75142021-06-10 11:22:50 +080084 Interface(utils::getBus(), getDbusPath(path).c_str(), true),
85 path(path), callBack(callBack), instance(getInstance(path)),
Chris Cain17257672021-10-22 13:41:03 -050086 manager(managerRef),
Gunnar Mills94df8c92018-09-14 14:50:03 -050087 device(event,
Lei YU0ab90ca2017-07-13 17:02:23 +080088#ifdef I2C_OCC
Eddie James774f9af2019-03-19 20:58:53 +000089 fs::path(DEV_PATH) / i2c_occ::getI2cDeviceName(path),
Lei YU0ab90ca2017-07-13 17:02:23 +080090#else
Eddie James774f9af2019-03-19 20:58:53 +000091 fs::path(DEV_PATH) /
92 fs::path(sysfsName + "." + std::to_string(instance + 1)),
Lei YU0ab90ca2017-07-13 17:02:23 +080093#endif
Chris Cain17257672021-10-22 13:41:03 -050094 managerRef, *this, instance),
Gunnar Mills94df8c92018-09-14 14:50:03 -050095 hostControlSignal(
George Liuf3b75142021-06-10 11:22:50 +080096 utils::getBus(),
Gunnar Mills94df8c92018-09-14 14:50:03 -050097 sdbusRule::type::signal() + sdbusRule::member("CommandComplete") +
98 sdbusRule::path("/org/open_power/control/host0") +
99 sdbusRule::interface("org.open_power.Control.Host") +
100 sdbusRule::argN(0, Control::convertForMessage(
101 Control::Host::Command::OCCReset)),
102 std::bind(std::mem_fn(&Status::hostControlEvent), this,
Chris Caina8857c52021-01-27 11:53:05 -0600103 std::placeholders::_1)),
George Liuf3b75142021-06-10 11:22:50 +0800104 occCmd(instance, (fs::path(OCC_CONTROL_ROOT) /
105 (std::string(OCC_NAME) + std::to_string(instance)))
106 .c_str())
Chris Caina7b74dc2021-11-10 17:03:43 -0600107#ifdef POWER10
108 ,
109 sdpEvent(sdeventplus::Event::get_default()),
110 safeStateDelayTimer(
111 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>(
112 sdpEvent, std::bind(&Status::safeStateDelayExpired, this)))
113#endif
Tom Joseph00325232020-07-29 17:51:48 +0530114#ifdef PLDM
115 ,
116 resetCallBack(resetCallBack)
117#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -0500118 {
119 // Check to see if we have OCC already bound. If so, just set it
120 if (device.bound())
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530121 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500122 this->occActive(true);
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530123 }
124
Gunnar Mills94df8c92018-09-14 14:50:03 -0500125 // Announce that we are ready
126 this->emit_object_added();
127 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530128
Gunnar Mills94df8c92018-09-14 14:50:03 -0500129 /** @brief Since we are overriding the setter-occActive but not the
130 * getter-occActive, we need to have this using in order to
131 * allow passthrough usage of the getter-occActive
132 */
133 using Base::Status::occActive;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530134
Gunnar Mills94df8c92018-09-14 14:50:03 -0500135 /** @brief SET OccActive to True or False
136 *
137 * @param[in] value - Intended value
138 *
139 * @return - Updated value of the property
140 */
141 bool occActive(bool value) override;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530142
Gunnar Mills94df8c92018-09-14 14:50:03 -0500143 /** @brief Starts OCC error detection */
144 inline void addErrorWatch()
145 {
146 return device.addErrorWatch();
147 }
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530148
Gunnar Mills94df8c92018-09-14 14:50:03 -0500149 /** @brief Stops OCC error detection */
150 inline void removeErrorWatch()
151 {
152 return device.removeErrorWatch();
153 }
Eddie Jamesdae2d942017-12-20 10:50:03 -0600154
Gunnar Mills94df8c92018-09-14 14:50:03 -0500155 /** @brief Starts to watch how many OCCs are present on the master */
156 inline void addPresenceWatchMaster()
157 {
158 return device.addPresenceWatchMaster();
159 }
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530160
Chicago Duanbb895cb2021-06-18 19:37:16 +0800161 /** @brief Gets the occ instance number */
162 unsigned int getOccInstanceID()
163 {
164 return instance;
165 }
166
167 /** @brief Is this OCC the master OCC */
168 bool isMasterOcc()
169 {
170 return device.master();
171 }
172
Chris Caina8857c52021-01-27 11:53:05 -0600173 /** @brief Read OCC state (will trigger kernel to poll the OCC) */
174 void readOccState();
175
Eddie Jamescbad2192021-10-07 09:39:39 -0500176 /** @brief Called when device errors are detected */
177 void deviceError();
178
Chris Cain78e86012021-03-04 16:15:31 -0600179#ifdef POWER10
180 /** @brief Handle additional tasks when the OCCs reach active state */
181 void occsWentActive();
182
183 /** @brief Send mode change command to the master OCC
184 * @return SUCCESS on success
185 */
186 CmdStatus sendModeChange();
Chris Cain1d51da22021-09-21 14:13:41 -0500187
188 /** @brief Send Idle Power Saver config data to the master OCC
189 * @return SUCCESS on success
190 */
191 CmdStatus sendIpsData();
Chris Cain17257672021-10-22 13:41:03 -0500192
193 /** @brief Send Ambient & Altitude data to OCC
194 *
195 * @param[in] ambient - temperature to send (0xFF will force read
196 * of current temperature and altitude)
197 * @param[in] altitude - altitude to send (0xFFFF = unavailable)
198 *
199 * @return SUCCESS on success
200 */
201 CmdStatus sendAmbient(const uint8_t ambient = 0xFF,
202 const uint16_t altitude = 0xFFFF);
Chris Cain78e86012021-03-04 16:15:31 -0600203#endif // POWER10
204
Gunnar Mills94df8c92018-09-14 14:50:03 -0500205 private:
Gunnar Mills94df8c92018-09-14 14:50:03 -0500206 /** @brief OCC dbus object path */
207 std::string path;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530208
Gunnar Mills94df8c92018-09-14 14:50:03 -0500209 /** @brief Callback handler to be invoked during property change.
210 * This is a handler in Manager class
211 */
212 std::function<void(bool)> callBack;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530213
Gunnar Mills94df8c92018-09-14 14:50:03 -0500214 /** @brief OCC instance number. Ex, 0,1, etc */
Chris Caina8857c52021-01-27 11:53:05 -0600215 unsigned int instance;
216
217 /** @brief The last state read from the OCC */
218 unsigned int lastState = 0;
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +0530219
Alexander Filippov1d69e192019-03-21 18:12:07 +0300220 /** @brief OCC instance to Sensor definitions mapping */
221 static const std::map<instanceID, sensorDefs> sensorMap;
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +0530222
Chris Cain17257672021-10-22 13:41:03 -0500223 /** @brief OCC manager object */
224 const Manager& manager;
225
Gunnar Mills94df8c92018-09-14 14:50:03 -0500226 /** @brief OCC device object to do bind and unbind */
227 Device device;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530228
Gunnar Mills94df8c92018-09-14 14:50:03 -0500229 /** @brief Subscribe to host control signal
230 *
231 * Once the OCC reset is requested, BMC sends that message to host.
232 * If the host does not ack the message, then there would be a timeout
233 * and we need to catch that to log an error
234 **/
235 sdbusplus::bus::match_t hostControlSignal;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530236
Chris Caina8857c52021-01-27 11:53:05 -0600237 /** @brief Command object to send commands to the OCC */
238 OccCommand occCmd;
239
Chris Caina7b74dc2021-11-10 17:03:43 -0600240#ifdef POWER10
241 /** @brief timer event */
242 sdeventplus::Event sdpEvent;
243#endif
244
Gunnar Mills94df8c92018-09-14 14:50:03 -0500245 /** @brief Callback function on host control signals
246 *
247 * @param[in] msg - Data associated with subscribed signal
248 */
249 void hostControlEvent(sdbusplus::message::message& msg);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530250
Gunnar Mills94df8c92018-09-14 14:50:03 -0500251 /** @brief Sends a message to host control command handler to reset OCC
252 */
253 void resetOCC();
Alexander Filippov1d69e192019-03-21 18:12:07 +0300254
255 /** @brief Determines the instance ID by specified object path.
256 * @param[in] path Estimated OCC Dbus object path
257 * @return Instance number
258 */
259 static int getInstance(const std::string& path)
260 {
261 return (path.empty() ? 0 : path.back() - '0');
262 }
263
Chris Cain78e86012021-03-04 16:15:31 -0600264#ifdef POWER10
265 /** @brief Query the current Hypervisor target
266 * @return true if the current Hypervisor target is PowerVM
267 */
268 bool isPowerVM();
269
270 /** @brief Get the requested power mode property
271 * @return Power mode
272 */
273 SysPwrMode getMode();
274
Chris Cain1d51da22021-09-21 14:13:41 -0500275 /** @brief Get the Idle Power Saver properties
276 * @return true if IPS is enabled
Chris Cain78e86012021-03-04 16:15:31 -0600277 */
Chris Cain1d51da22021-09-21 14:13:41 -0500278 bool getIPSParms(uint8_t& enterUtil, uint16_t& enterTime, uint8_t& exitUtil,
279 uint16_t& exitTime);
Chris Caina7b74dc2021-11-10 17:03:43 -0600280
281 /**
282 * @brief Timer that is started when OCC is detected to be in safe mode
283 */
284 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>
285 safeStateDelayTimer;
286
287 /** @brief Callback for timer that is started when OCC was detected to be in
288 * safe mode. Called to verify and then disable and reset the OCCs.
289 */
290 void safeStateDelayExpired();
Chris Cain78e86012021-03-04 16:15:31 -0600291#endif // POWER10
292
Alexander Filippov1d69e192019-03-21 18:12:07 +0300293 /** @brief Override the sensor name with name from the definition.
294 * @param[in] estimatedPath - Estimated OCC Dbus object path
295 * @return Fixed OCC DBus object path
296 */
297 static std::string getDbusPath(const std::string& estimatedPath)
298 {
299 if (!estimatedPath.empty())
300 {
301 auto it = sensorMap.find(getInstance(estimatedPath));
302 if (sensorMap.end() != it)
303 {
304 auto& name = std::get<1>(it->second);
305 if (!name.empty() && name != "None")
306 {
307 auto path = fs::path(estimatedPath);
308 path.replace_filename(name);
309 return path.string();
310 }
311 }
312 }
313
314 return estimatedPath;
315 }
Tom Joseph00325232020-07-29 17:51:48 +0530316#ifdef PLDM
317 std::function<void(instanceID)> resetCallBack = nullptr;
318#endif
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530319};
320
321} // namespace occ
322} // namespace open_power