blob: dce73f863b3845acc79f0a385d803a62d6870f1b [file] [log] [blame]
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +05301#pragma once
2
Gunnar Mills94df8c92018-09-14 14:50:03 -05003#include "i2c_occ.hpp"
Chris Caina8857c52021-01-27 11:53:05 -06004#include "occ_command.hpp"
Gunnar Mills94df8c92018-09-14 14:50:03 -05005#include "occ_device.hpp"
6#include "occ_events.hpp"
George Liuf3b75142021-06-10 11:22:50 +08007#include "utils.hpp"
Gunnar Mills94df8c92018-09-14 14:50:03 -05008
Gunnar Mills94df8c92018-09-14 14:50:03 -05009#include <org/open_power/Control/Host/server.hpp>
10#include <org/open_power/OCC/Status/server.hpp>
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053011#include <sdbusplus/bus.hpp>
12#include <sdbusplus/server/object.hpp>
Lei YU0ab90ca2017-07-13 17:02:23 +080013
George Liub5ca1012021-09-10 12:53:11 +080014#include <functional>
15
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053016namespace open_power
17{
18namespace occ
19{
20
Edward A. James636577f2017-10-06 10:53:55 -050021class Manager;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053022namespace Base = sdbusplus::org::open_power::OCC::server;
23using Interface = sdbusplus::server::object::object<Base::Status>;
24
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053025// IPMID's host control application
26namespace Control = sdbusplus::org::open_power::Control::server;
27
28// For waiting on signals
29namespace sdbusRule = sdbusplus::bus::match::rules;
30
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +053031// OCC status instance. Ex. for "occ0", the instance is 0
32using instanceID = int;
33
34// IPMI sensor ID for a given OCC instance
35using sensorID = uint8_t;
36
Alexander Filippov1d69e192019-03-21 18:12:07 +030037// Human readable sensor name for DBus tree. E.g. "CPU0_OCC"
38using sensorName = std::string;
39
40// OCC sensors definitions in the map
41using sensorDefs = std::tuple<sensorID, sensorName>;
42
Eddie Jamese7d976b2018-02-26 13:42:45 -060043// OCC sysfs name prefix
44const std::string sysfsName = "occ-hwmon";
45
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053046/** @class Status
47 * @brief Implementation of OCC Active Status
48 */
49class Status : public Interface
50{
Gunnar Mills94df8c92018-09-14 14:50:03 -050051 public:
52 Status() = delete;
53 ~Status() = default;
54 Status(const Status&) = delete;
55 Status& operator=(const Status&) = delete;
56 Status(Status&&) = default;
57 Status& operator=(Status&&) = default;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053058
Gunnar Mills94df8c92018-09-14 14:50:03 -050059 /** @brief Constructs the Status object and
60 * the underlying device object
61 *
Gunnar Mills94df8c92018-09-14 14:50:03 -050062 * @param[in] event - sd_event unique pointer reference
63 * @param[in] path - DBus object path
64 * @param[in] manager - OCC manager instance
65 * @param[in] callBack - Callback handler to invoke during
66 * property change
Tom Joseph00325232020-07-29 17:51:48 +053067 * @param[in] resetCallBack - callback handler to invoke for resetting the
68 * OCC if PLDM is the host communication
69 * protocol
Gunnar Mills94df8c92018-09-14 14:50:03 -050070 */
George Liuf3b75142021-06-10 11:22:50 +080071 Status(EventPtr& event, const char* path, const Manager& manager,
72 std::function<void(bool)> callBack = nullptr
Tom Joseph00325232020-07-29 17:51:48 +053073#ifdef PLDM
74 ,
75 std::function<void(instanceID)> resetCallBack = nullptr
76#endif
77 ) :
78
George Liuf3b75142021-06-10 11:22:50 +080079 Interface(utils::getBus(), getDbusPath(path).c_str(), true),
80 path(path), callBack(callBack), instance(getInstance(path)),
Gunnar Mills94df8c92018-09-14 14:50:03 -050081 device(event,
Lei YU0ab90ca2017-07-13 17:02:23 +080082#ifdef I2C_OCC
Eddie James774f9af2019-03-19 20:58:53 +000083 fs::path(DEV_PATH) / i2c_occ::getI2cDeviceName(path),
Lei YU0ab90ca2017-07-13 17:02:23 +080084#else
Eddie James774f9af2019-03-19 20:58:53 +000085 fs::path(DEV_PATH) /
86 fs::path(sysfsName + "." + std::to_string(instance + 1)),
Lei YU0ab90ca2017-07-13 17:02:23 +080087#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -050088 manager, *this,
89 std::bind(std::mem_fn(&Status::deviceErrorHandler), this,
90 std::placeholders::_1)),
91 hostControlSignal(
George Liuf3b75142021-06-10 11:22:50 +080092 utils::getBus(),
Gunnar Mills94df8c92018-09-14 14:50:03 -050093 sdbusRule::type::signal() + sdbusRule::member("CommandComplete") +
94 sdbusRule::path("/org/open_power/control/host0") +
95 sdbusRule::interface("org.open_power.Control.Host") +
96 sdbusRule::argN(0, Control::convertForMessage(
97 Control::Host::Command::OCCReset)),
98 std::bind(std::mem_fn(&Status::hostControlEvent), this,
Chris Caina8857c52021-01-27 11:53:05 -060099 std::placeholders::_1)),
George Liuf3b75142021-06-10 11:22:50 +0800100 occCmd(instance, (fs::path(OCC_CONTROL_ROOT) /
101 (std::string(OCC_NAME) + std::to_string(instance)))
102 .c_str())
Tom Joseph00325232020-07-29 17:51:48 +0530103#ifdef PLDM
104 ,
105 resetCallBack(resetCallBack)
106#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -0500107 {
108 // Check to see if we have OCC already bound. If so, just set it
109 if (device.bound())
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530110 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500111 this->occActive(true);
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530112 }
113
Gunnar Mills94df8c92018-09-14 14:50:03 -0500114 // Announce that we are ready
115 this->emit_object_added();
116 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530117
Gunnar Mills94df8c92018-09-14 14:50:03 -0500118 /** @brief Since we are overriding the setter-occActive but not the
119 * getter-occActive, we need to have this using in order to
120 * allow passthrough usage of the getter-occActive
121 */
122 using Base::Status::occActive;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530123
Gunnar Mills94df8c92018-09-14 14:50:03 -0500124 /** @brief SET OccActive to True or False
125 *
126 * @param[in] value - Intended value
127 *
128 * @return - Updated value of the property
129 */
130 bool occActive(bool value) override;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530131
Gunnar Mills94df8c92018-09-14 14:50:03 -0500132 /** @brief Starts OCC error detection */
133 inline void addErrorWatch()
134 {
135 return device.addErrorWatch();
136 }
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530137
Gunnar Mills94df8c92018-09-14 14:50:03 -0500138 /** @brief Stops OCC error detection */
139 inline void removeErrorWatch()
140 {
141 return device.removeErrorWatch();
142 }
Eddie Jamesdae2d942017-12-20 10:50:03 -0600143
Gunnar Mills94df8c92018-09-14 14:50:03 -0500144 /** @brief Starts to watch how many OCCs are present on the master */
145 inline void addPresenceWatchMaster()
146 {
147 return device.addPresenceWatchMaster();
148 }
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530149
Chicago Duanbb895cb2021-06-18 19:37:16 +0800150 /** @brief Gets the occ instance number */
151 unsigned int getOccInstanceID()
152 {
153 return instance;
154 }
155
156 /** @brief Is this OCC the master OCC */
157 bool isMasterOcc()
158 {
159 return device.master();
160 }
161
Chris Caina8857c52021-01-27 11:53:05 -0600162 /** @brief Read OCC state (will trigger kernel to poll the OCC) */
163 void readOccState();
164
Chris Cain78e86012021-03-04 16:15:31 -0600165#ifdef POWER10
166 /** @brief Handle additional tasks when the OCCs reach active state */
167 void occsWentActive();
168
169 /** @brief Send mode change command to the master OCC
170 * @return SUCCESS on success
171 */
172 CmdStatus sendModeChange();
173#endif // POWER10
174
Gunnar Mills94df8c92018-09-14 14:50:03 -0500175 private:
Gunnar Mills94df8c92018-09-14 14:50:03 -0500176 /** @brief OCC dbus object path */
177 std::string path;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530178
Gunnar Mills94df8c92018-09-14 14:50:03 -0500179 /** @brief Callback handler to be invoked during property change.
180 * This is a handler in Manager class
181 */
182 std::function<void(bool)> callBack;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530183
Gunnar Mills94df8c92018-09-14 14:50:03 -0500184 /** @brief OCC instance number. Ex, 0,1, etc */
Chris Caina8857c52021-01-27 11:53:05 -0600185 unsigned int instance;
186
187 /** @brief The last state read from the OCC */
188 unsigned int lastState = 0;
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +0530189
Alexander Filippov1d69e192019-03-21 18:12:07 +0300190 /** @brief OCC instance to Sensor definitions mapping */
191 static const std::map<instanceID, sensorDefs> sensorMap;
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +0530192
Gunnar Mills94df8c92018-09-14 14:50:03 -0500193 /** @brief OCC device object to do bind and unbind */
194 Device device;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530195
Gunnar Mills94df8c92018-09-14 14:50:03 -0500196 /** @brief Subscribe to host control signal
197 *
198 * Once the OCC reset is requested, BMC sends that message to host.
199 * If the host does not ack the message, then there would be a timeout
200 * and we need to catch that to log an error
201 **/
202 sdbusplus::bus::match_t hostControlSignal;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530203
Chris Caina8857c52021-01-27 11:53:05 -0600204 /** @brief Command object to send commands to the OCC */
205 OccCommand occCmd;
206
Gunnar Mills94df8c92018-09-14 14:50:03 -0500207 /** @brief Callback handler when device errors are detected
208 *
209 * @param[in] error - True if an error is reported, false otherwise
210 */
211 void deviceErrorHandler(bool error);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530212
Gunnar Mills94df8c92018-09-14 14:50:03 -0500213 /** @brief Callback function on host control signals
214 *
215 * @param[in] msg - Data associated with subscribed signal
216 */
217 void hostControlEvent(sdbusplus::message::message& msg);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530218
Gunnar Mills94df8c92018-09-14 14:50:03 -0500219 /** @brief Sends a message to host control command handler to reset OCC
220 */
221 void resetOCC();
Alexander Filippov1d69e192019-03-21 18:12:07 +0300222
223 /** @brief Determines the instance ID by specified object path.
224 * @param[in] path Estimated OCC Dbus object path
225 * @return Instance number
226 */
227 static int getInstance(const std::string& path)
228 {
229 return (path.empty() ? 0 : path.back() - '0');
230 }
231
Chris Cain78e86012021-03-04 16:15:31 -0600232#ifdef POWER10
233 /** @brief Query the current Hypervisor target
234 * @return true if the current Hypervisor target is PowerVM
235 */
236 bool isPowerVM();
237
238 /** @brief Get the requested power mode property
239 * @return Power mode
240 */
241 SysPwrMode getMode();
242
243 /** @brief Send Idle Power Saver config data to the master OCC
244 * @return SUCCESS on success
245 */
246 CmdStatus sendIpsData();
247#endif // POWER10
248
Alexander Filippov1d69e192019-03-21 18:12:07 +0300249 /** @brief Override the sensor name with name from the definition.
250 * @param[in] estimatedPath - Estimated OCC Dbus object path
251 * @return Fixed OCC DBus object path
252 */
253 static std::string getDbusPath(const std::string& estimatedPath)
254 {
255 if (!estimatedPath.empty())
256 {
257 auto it = sensorMap.find(getInstance(estimatedPath));
258 if (sensorMap.end() != it)
259 {
260 auto& name = std::get<1>(it->second);
261 if (!name.empty() && name != "None")
262 {
263 auto path = fs::path(estimatedPath);
264 path.replace_filename(name);
265 return path.string();
266 }
267 }
268 }
269
270 return estimatedPath;
271 }
Tom Joseph00325232020-07-29 17:51:48 +0530272#ifdef PLDM
273 std::function<void(instanceID)> resetCallBack = nullptr;
274#endif
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530275};
276
277} // namespace occ
278} // namespace open_power