blob: 5856ee9a2f7b6e19c7c1617652a4eaaa23f77ead [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>
Lei YU0ab90ca2017-07-13 17:02:23 +080014
George Liub5ca1012021-09-10 12:53:11 +080015#include <functional>
16
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053017namespace open_power
18{
19namespace occ
20{
21
Edward A. James636577f2017-10-06 10:53:55 -050022class Manager;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053023namespace Base = sdbusplus::org::open_power::OCC::server;
24using Interface = sdbusplus::server::object::object<Base::Status>;
25
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053026// IPMID's host control application
27namespace Control = sdbusplus::org::open_power::Control::server;
28
29// For waiting on signals
30namespace sdbusRule = sdbusplus::bus::match::rules;
31
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +053032// OCC status instance. Ex. for "occ0", the instance is 0
33using instanceID = int;
34
35// IPMI sensor ID for a given OCC instance
36using sensorID = uint8_t;
37
Alexander Filippov1d69e192019-03-21 18:12:07 +030038// Human readable sensor name for DBus tree. E.g. "CPU0_OCC"
39using sensorName = std::string;
40
41// OCC sensors definitions in the map
42using sensorDefs = std::tuple<sensorID, sensorName>;
43
Eddie Jamese7d976b2018-02-26 13:42:45 -060044// OCC sysfs name prefix
45const std::string sysfsName = "occ-hwmon";
46
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053047/** @class Status
48 * @brief Implementation of OCC Active Status
49 */
50class Status : public Interface
51{
Gunnar Mills94df8c92018-09-14 14:50:03 -050052 public:
53 Status() = delete;
54 ~Status() = default;
55 Status(const Status&) = delete;
56 Status& operator=(const Status&) = delete;
57 Status(Status&&) = default;
58 Status& operator=(Status&&) = default;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053059
Gunnar Mills94df8c92018-09-14 14:50:03 -050060 /** @brief Constructs the Status object and
61 * the underlying device object
62 *
Gunnar Mills94df8c92018-09-14 14:50:03 -050063 * @param[in] event - sd_event unique pointer reference
64 * @param[in] path - DBus object path
65 * @param[in] manager - OCC manager instance
66 * @param[in] callBack - Callback handler to invoke during
67 * property change
Tom Joseph00325232020-07-29 17:51:48 +053068 * @param[in] resetCallBack - callback handler to invoke for resetting the
69 * OCC if PLDM is the host communication
70 * protocol
Gunnar Mills94df8c92018-09-14 14:50:03 -050071 */
George Liuf3b75142021-06-10 11:22:50 +080072 Status(EventPtr& event, const char* path, const Manager& manager,
73 std::function<void(bool)> callBack = nullptr
Tom Joseph00325232020-07-29 17:51:48 +053074#ifdef PLDM
75 ,
76 std::function<void(instanceID)> resetCallBack = nullptr
77#endif
78 ) :
79
George Liuf3b75142021-06-10 11:22:50 +080080 Interface(utils::getBus(), getDbusPath(path).c_str(), true),
81 path(path), callBack(callBack), instance(getInstance(path)),
Gunnar Mills94df8c92018-09-14 14:50:03 -050082 device(event,
Lei YU0ab90ca2017-07-13 17:02:23 +080083#ifdef I2C_OCC
Eddie James774f9af2019-03-19 20:58:53 +000084 fs::path(DEV_PATH) / i2c_occ::getI2cDeviceName(path),
Lei YU0ab90ca2017-07-13 17:02:23 +080085#else
Eddie James774f9af2019-03-19 20:58:53 +000086 fs::path(DEV_PATH) /
87 fs::path(sysfsName + "." + std::to_string(instance + 1)),
Lei YU0ab90ca2017-07-13 17:02:23 +080088#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -050089 manager, *this,
90 std::bind(std::mem_fn(&Status::deviceErrorHandler), this,
91 std::placeholders::_1)),
92 hostControlSignal(
George Liuf3b75142021-06-10 11:22:50 +080093 utils::getBus(),
Gunnar Mills94df8c92018-09-14 14:50:03 -050094 sdbusRule::type::signal() + sdbusRule::member("CommandComplete") +
95 sdbusRule::path("/org/open_power/control/host0") +
96 sdbusRule::interface("org.open_power.Control.Host") +
97 sdbusRule::argN(0, Control::convertForMessage(
98 Control::Host::Command::OCCReset)),
99 std::bind(std::mem_fn(&Status::hostControlEvent), this,
Chris Caina8857c52021-01-27 11:53:05 -0600100 std::placeholders::_1)),
George Liuf3b75142021-06-10 11:22:50 +0800101 occCmd(instance, (fs::path(OCC_CONTROL_ROOT) /
102 (std::string(OCC_NAME) + std::to_string(instance)))
103 .c_str())
Tom Joseph00325232020-07-29 17:51:48 +0530104#ifdef PLDM
105 ,
106 resetCallBack(resetCallBack)
107#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -0500108 {
109 // Check to see if we have OCC already bound. If so, just set it
110 if (device.bound())
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530111 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500112 this->occActive(true);
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530113 }
114
Gunnar Mills94df8c92018-09-14 14:50:03 -0500115 // Announce that we are ready
116 this->emit_object_added();
117 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530118
Gunnar Mills94df8c92018-09-14 14:50:03 -0500119 /** @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;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530124
Gunnar Mills94df8c92018-09-14 14:50:03 -0500125 /** @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;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530132
Gunnar Mills94df8c92018-09-14 14:50:03 -0500133 /** @brief Starts OCC error detection */
134 inline void addErrorWatch()
135 {
136 return device.addErrorWatch();
137 }
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530138
Gunnar Mills94df8c92018-09-14 14:50:03 -0500139 /** @brief Stops OCC error detection */
140 inline void removeErrorWatch()
141 {
142 return device.removeErrorWatch();
143 }
Eddie Jamesdae2d942017-12-20 10:50:03 -0600144
Gunnar Mills94df8c92018-09-14 14:50:03 -0500145 /** @brief Starts to watch how many OCCs are present on the master */
146 inline void addPresenceWatchMaster()
147 {
148 return device.addPresenceWatchMaster();
149 }
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530150
Chicago Duanbb895cb2021-06-18 19:37:16 +0800151 /** @brief Gets the occ instance number */
152 unsigned int getOccInstanceID()
153 {
154 return instance;
155 }
156
157 /** @brief Is this OCC the master OCC */
158 bool isMasterOcc()
159 {
160 return device.master();
161 }
162
Chris Caina8857c52021-01-27 11:53:05 -0600163 /** @brief Read OCC state (will trigger kernel to poll the OCC) */
164 void readOccState();
165
Chris Cain78e86012021-03-04 16:15:31 -0600166#ifdef POWER10
167 /** @brief Handle additional tasks when the OCCs reach active state */
168 void occsWentActive();
169
170 /** @brief Send mode change command to the master OCC
171 * @return SUCCESS on success
172 */
173 CmdStatus sendModeChange();
174#endif // POWER10
175
Gunnar Mills94df8c92018-09-14 14:50:03 -0500176 private:
Gunnar Mills94df8c92018-09-14 14:50:03 -0500177 /** @brief OCC dbus object path */
178 std::string path;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530179
Gunnar Mills94df8c92018-09-14 14:50:03 -0500180 /** @brief Callback handler to be invoked during property change.
181 * This is a handler in Manager class
182 */
183 std::function<void(bool)> callBack;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530184
Gunnar Mills94df8c92018-09-14 14:50:03 -0500185 /** @brief OCC instance number. Ex, 0,1, etc */
Chris Caina8857c52021-01-27 11:53:05 -0600186 unsigned int instance;
187
188 /** @brief The last state read from the OCC */
189 unsigned int lastState = 0;
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +0530190
Alexander Filippov1d69e192019-03-21 18:12:07 +0300191 /** @brief OCC instance to Sensor definitions mapping */
192 static const std::map<instanceID, sensorDefs> sensorMap;
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +0530193
Gunnar Mills94df8c92018-09-14 14:50:03 -0500194 /** @brief OCC device object to do bind and unbind */
195 Device device;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530196
Gunnar Mills94df8c92018-09-14 14:50:03 -0500197 /** @brief Subscribe to host control signal
198 *
199 * Once the OCC reset is requested, BMC sends that message to host.
200 * If the host does not ack the message, then there would be a timeout
201 * and we need to catch that to log an error
202 **/
203 sdbusplus::bus::match_t hostControlSignal;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530204
Chris Caina8857c52021-01-27 11:53:05 -0600205 /** @brief Command object to send commands to the OCC */
206 OccCommand occCmd;
207
Gunnar Mills94df8c92018-09-14 14:50:03 -0500208 /** @brief Callback handler when device errors are detected
209 *
210 * @param[in] error - True if an error is reported, false otherwise
211 */
212 void deviceErrorHandler(bool error);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530213
Gunnar Mills94df8c92018-09-14 14:50:03 -0500214 /** @brief Callback function on host control signals
215 *
216 * @param[in] msg - Data associated with subscribed signal
217 */
218 void hostControlEvent(sdbusplus::message::message& msg);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530219
Gunnar Mills94df8c92018-09-14 14:50:03 -0500220 /** @brief Sends a message to host control command handler to reset OCC
221 */
222 void resetOCC();
Alexander Filippov1d69e192019-03-21 18:12:07 +0300223
224 /** @brief Determines the instance ID by specified object path.
225 * @param[in] path Estimated OCC Dbus object path
226 * @return Instance number
227 */
228 static int getInstance(const std::string& path)
229 {
230 return (path.empty() ? 0 : path.back() - '0');
231 }
232
Chris Cain78e86012021-03-04 16:15:31 -0600233#ifdef POWER10
234 /** @brief Query the current Hypervisor target
235 * @return true if the current Hypervisor target is PowerVM
236 */
237 bool isPowerVM();
238
239 /** @brief Get the requested power mode property
240 * @return Power mode
241 */
242 SysPwrMode getMode();
243
244 /** @brief Send Idle Power Saver config data to the master OCC
245 * @return SUCCESS on success
246 */
247 CmdStatus sendIpsData();
248#endif // POWER10
249
Alexander Filippov1d69e192019-03-21 18:12:07 +0300250 /** @brief Override the sensor name with name from the definition.
251 * @param[in] estimatedPath - Estimated OCC Dbus object path
252 * @return Fixed OCC DBus object path
253 */
254 static std::string getDbusPath(const std::string& estimatedPath)
255 {
256 if (!estimatedPath.empty())
257 {
258 auto it = sensorMap.find(getInstance(estimatedPath));
259 if (sensorMap.end() != it)
260 {
261 auto& name = std::get<1>(it->second);
262 if (!name.empty() && name != "None")
263 {
264 auto path = fs::path(estimatedPath);
265 path.replace_filename(name);
266 return path.string();
267 }
268 }
269 }
270
271 return estimatedPath;
272 }
Tom Joseph00325232020-07-29 17:51:48 +0530273#ifdef PLDM
274 std::function<void(instanceID)> resetCallBack = nullptr;
275#endif
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530276};
277
278} // namespace occ
279} // namespace open_power