blob: 415e0b7d47f085ac18ee9c415069deb47682dc81 [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
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +05309#include <functional>
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
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053015namespace open_power
16{
17namespace occ
18{
19
Edward A. James636577f2017-10-06 10:53:55 -050020class Manager;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053021namespace Base = sdbusplus::org::open_power::OCC::server;
22using Interface = sdbusplus::server::object::object<Base::Status>;
23
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053024// IPMID's host control application
25namespace Control = sdbusplus::org::open_power::Control::server;
26
27// For waiting on signals
28namespace sdbusRule = sdbusplus::bus::match::rules;
29
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +053030// OCC status instance. Ex. for "occ0", the instance is 0
31using instanceID = int;
32
33// IPMI sensor ID for a given OCC instance
34using sensorID = uint8_t;
35
Alexander Filippov1d69e192019-03-21 18:12:07 +030036// Human readable sensor name for DBus tree. E.g. "CPU0_OCC"
37using sensorName = std::string;
38
39// OCC sensors definitions in the map
40using sensorDefs = std::tuple<sensorID, sensorName>;
41
Eddie Jamese7d976b2018-02-26 13:42:45 -060042// OCC sysfs name prefix
43const std::string sysfsName = "occ-hwmon";
44
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053045/** @class Status
46 * @brief Implementation of OCC Active Status
47 */
48class Status : public Interface
49{
Gunnar Mills94df8c92018-09-14 14:50:03 -050050 public:
51 Status() = delete;
52 ~Status() = default;
53 Status(const Status&) = delete;
54 Status& operator=(const Status&) = delete;
55 Status(Status&&) = default;
56 Status& operator=(Status&&) = default;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053057
Gunnar Mills94df8c92018-09-14 14:50:03 -050058 /** @brief Constructs the Status object and
59 * the underlying device object
60 *
Gunnar Mills94df8c92018-09-14 14:50:03 -050061 * @param[in] event - sd_event unique pointer reference
62 * @param[in] path - DBus object path
63 * @param[in] manager - OCC manager instance
64 * @param[in] callBack - Callback handler to invoke during
65 * property change
Tom Joseph00325232020-07-29 17:51:48 +053066 * @param[in] resetCallBack - callback handler to invoke for resetting the
67 * OCC if PLDM is the host communication
68 * protocol
Gunnar Mills94df8c92018-09-14 14:50:03 -050069 */
George Liuf3b75142021-06-10 11:22:50 +080070 Status(EventPtr& event, const char* path, const Manager& manager,
71 std::function<void(bool)> callBack = nullptr
Tom Joseph00325232020-07-29 17:51:48 +053072#ifdef PLDM
73 ,
74 std::function<void(instanceID)> resetCallBack = nullptr
75#endif
76 ) :
77
George Liuf3b75142021-06-10 11:22:50 +080078 Interface(utils::getBus(), getDbusPath(path).c_str(), true),
79 path(path), callBack(callBack), instance(getInstance(path)),
Gunnar Mills94df8c92018-09-14 14:50:03 -050080 device(event,
Lei YU0ab90ca2017-07-13 17:02:23 +080081#ifdef I2C_OCC
Eddie James774f9af2019-03-19 20:58:53 +000082 fs::path(DEV_PATH) / i2c_occ::getI2cDeviceName(path),
Lei YU0ab90ca2017-07-13 17:02:23 +080083#else
Eddie James774f9af2019-03-19 20:58:53 +000084 fs::path(DEV_PATH) /
85 fs::path(sysfsName + "." + std::to_string(instance + 1)),
Lei YU0ab90ca2017-07-13 17:02:23 +080086#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -050087 manager, *this,
88 std::bind(std::mem_fn(&Status::deviceErrorHandler), this,
89 std::placeholders::_1)),
90 hostControlSignal(
George Liuf3b75142021-06-10 11:22:50 +080091 utils::getBus(),
Gunnar Mills94df8c92018-09-14 14:50:03 -050092 sdbusRule::type::signal() + sdbusRule::member("CommandComplete") +
93 sdbusRule::path("/org/open_power/control/host0") +
94 sdbusRule::interface("org.open_power.Control.Host") +
95 sdbusRule::argN(0, Control::convertForMessage(
96 Control::Host::Command::OCCReset)),
97 std::bind(std::mem_fn(&Status::hostControlEvent), this,
Chris Caina8857c52021-01-27 11:53:05 -060098 std::placeholders::_1)),
George Liuf3b75142021-06-10 11:22:50 +080099 occCmd(instance, (fs::path(OCC_CONTROL_ROOT) /
100 (std::string(OCC_NAME) + std::to_string(instance)))
101 .c_str())
Tom Joseph00325232020-07-29 17:51:48 +0530102#ifdef PLDM
103 ,
104 resetCallBack(resetCallBack)
105#endif
Gunnar Mills94df8c92018-09-14 14:50:03 -0500106 {
107 // Check to see if we have OCC already bound. If so, just set it
108 if (device.bound())
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530109 {
Gunnar Mills94df8c92018-09-14 14:50:03 -0500110 this->occActive(true);
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530111 }
112
Gunnar Mills94df8c92018-09-14 14:50:03 -0500113 // Announce that we are ready
114 this->emit_object_added();
115 }
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530116
Gunnar Mills94df8c92018-09-14 14:50:03 -0500117 /** @brief Since we are overriding the setter-occActive but not the
118 * getter-occActive, we need to have this using in order to
119 * allow passthrough usage of the getter-occActive
120 */
121 using Base::Status::occActive;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530122
Gunnar Mills94df8c92018-09-14 14:50:03 -0500123 /** @brief SET OccActive to True or False
124 *
125 * @param[in] value - Intended value
126 *
127 * @return - Updated value of the property
128 */
129 bool occActive(bool value) override;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530130
Gunnar Mills94df8c92018-09-14 14:50:03 -0500131 /** @brief Starts OCC error detection */
132 inline void addErrorWatch()
133 {
134 return device.addErrorWatch();
135 }
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530136
Gunnar Mills94df8c92018-09-14 14:50:03 -0500137 /** @brief Stops OCC error detection */
138 inline void removeErrorWatch()
139 {
140 return device.removeErrorWatch();
141 }
Eddie Jamesdae2d942017-12-20 10:50:03 -0600142
Gunnar Mills94df8c92018-09-14 14:50:03 -0500143 /** @brief Starts to watch how many OCCs are present on the master */
144 inline void addPresenceWatchMaster()
145 {
146 return device.addPresenceWatchMaster();
147 }
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530148
Chris Caina8857c52021-01-27 11:53:05 -0600149 /** @brief Read OCC state (will trigger kernel to poll the OCC) */
150 void readOccState();
151
Chris Cain78e86012021-03-04 16:15:31 -0600152#ifdef POWER10
153 /** @brief Handle additional tasks when the OCCs reach active state */
154 void occsWentActive();
155
156 /** @brief Send mode change command to the master OCC
157 * @return SUCCESS on success
158 */
159 CmdStatus sendModeChange();
160#endif // POWER10
161
Gunnar Mills94df8c92018-09-14 14:50:03 -0500162 private:
Gunnar Mills94df8c92018-09-14 14:50:03 -0500163 /** @brief OCC dbus object path */
164 std::string path;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530165
Gunnar Mills94df8c92018-09-14 14:50:03 -0500166 /** @brief Callback handler to be invoked during property change.
167 * This is a handler in Manager class
168 */
169 std::function<void(bool)> callBack;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530170
Gunnar Mills94df8c92018-09-14 14:50:03 -0500171 /** @brief OCC instance number. Ex, 0,1, etc */
Chris Caina8857c52021-01-27 11:53:05 -0600172 unsigned int instance;
173
174 /** @brief The last state read from the OCC */
175 unsigned int lastState = 0;
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +0530176
Alexander Filippov1d69e192019-03-21 18:12:07 +0300177 /** @brief OCC instance to Sensor definitions mapping */
178 static const std::map<instanceID, sensorDefs> sensorMap;
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +0530179
Gunnar Mills94df8c92018-09-14 14:50:03 -0500180 /** @brief OCC device object to do bind and unbind */
181 Device device;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530182
Gunnar Mills94df8c92018-09-14 14:50:03 -0500183 /** @brief Subscribe to host control signal
184 *
185 * Once the OCC reset is requested, BMC sends that message to host.
186 * If the host does not ack the message, then there would be a timeout
187 * and we need to catch that to log an error
188 **/
189 sdbusplus::bus::match_t hostControlSignal;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530190
Chris Caina8857c52021-01-27 11:53:05 -0600191 /** @brief Command object to send commands to the OCC */
192 OccCommand occCmd;
193
Gunnar Mills94df8c92018-09-14 14:50:03 -0500194 /** @brief Callback handler when device errors are detected
195 *
196 * @param[in] error - True if an error is reported, false otherwise
197 */
198 void deviceErrorHandler(bool error);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530199
Gunnar Mills94df8c92018-09-14 14:50:03 -0500200 /** @brief Callback function on host control signals
201 *
202 * @param[in] msg - Data associated with subscribed signal
203 */
204 void hostControlEvent(sdbusplus::message::message& msg);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530205
Gunnar Mills94df8c92018-09-14 14:50:03 -0500206 /** @brief Sends a message to host control command handler to reset OCC
207 */
208 void resetOCC();
Alexander Filippov1d69e192019-03-21 18:12:07 +0300209
210 /** @brief Determines the instance ID by specified object path.
211 * @param[in] path Estimated OCC Dbus object path
212 * @return Instance number
213 */
214 static int getInstance(const std::string& path)
215 {
216 return (path.empty() ? 0 : path.back() - '0');
217 }
218
Chris Cain78e86012021-03-04 16:15:31 -0600219#ifdef POWER10
220 /** @brief Query the current Hypervisor target
221 * @return true if the current Hypervisor target is PowerVM
222 */
223 bool isPowerVM();
224
225 /** @brief Get the requested power mode property
226 * @return Power mode
227 */
228 SysPwrMode getMode();
229
230 /** @brief Send Idle Power Saver config data to the master OCC
231 * @return SUCCESS on success
232 */
233 CmdStatus sendIpsData();
234#endif // POWER10
235
Alexander Filippov1d69e192019-03-21 18:12:07 +0300236 /** @brief Override the sensor name with name from the definition.
237 * @param[in] estimatedPath - Estimated OCC Dbus object path
238 * @return Fixed OCC DBus object path
239 */
240 static std::string getDbusPath(const std::string& estimatedPath)
241 {
242 if (!estimatedPath.empty())
243 {
244 auto it = sensorMap.find(getInstance(estimatedPath));
245 if (sensorMap.end() != it)
246 {
247 auto& name = std::get<1>(it->second);
248 if (!name.empty() && name != "None")
249 {
250 auto path = fs::path(estimatedPath);
251 path.replace_filename(name);
252 return path.string();
253 }
254 }
255 }
256
257 return estimatedPath;
258 }
Tom Joseph00325232020-07-29 17:51:48 +0530259#ifdef PLDM
260 std::function<void(instanceID)> resetCallBack = nullptr;
261#endif
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530262};
263
264} // namespace occ
265} // namespace open_power