blob: abdc3edbedc3196a2456c0fcee82b16206993b4c [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"
7
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +05308#include <functional>
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
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053014namespace open_power
15{
16namespace occ
17{
18
Edward A. James636577f2017-10-06 10:53:55 -050019class Manager;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053020namespace Base = sdbusplus::org::open_power::OCC::server;
21using Interface = sdbusplus::server::object::object<Base::Status>;
22
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +053023// IPMID's host control application
24namespace Control = sdbusplus::org::open_power::Control::server;
25
26// For waiting on signals
27namespace sdbusRule = sdbusplus::bus::match::rules;
28
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +053029// OCC status instance. Ex. for "occ0", the instance is 0
30using instanceID = int;
31
32// IPMI sensor ID for a given OCC instance
33using sensorID = uint8_t;
34
Alexander Filippov1d69e192019-03-21 18:12:07 +030035// Human readable sensor name for DBus tree. E.g. "CPU0_OCC"
36using sensorName = std::string;
37
38// OCC sensors definitions in the map
39using sensorDefs = std::tuple<sensorID, sensorName>;
40
Eddie Jamese7d976b2018-02-26 13:42:45 -060041// OCC sysfs name prefix
42const std::string sysfsName = "occ-hwmon";
43
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053044/** @class Status
45 * @brief Implementation of OCC Active Status
46 */
47class Status : public Interface
48{
Gunnar Mills94df8c92018-09-14 14:50:03 -050049 public:
50 Status() = delete;
51 ~Status() = default;
52 Status(const Status&) = delete;
53 Status& operator=(const Status&) = delete;
54 Status(Status&&) = default;
55 Status& operator=(Status&&) = default;
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +053056
Gunnar Mills94df8c92018-09-14 14:50:03 -050057 /** @brief Constructs the Status object and
58 * the underlying device object
59 *
60 * @param[in] bus - DBus bus to attach to
61 * @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 */
70 Status(sdbusplus::bus::bus& bus, EventPtr& event, const char* path,
Tom Joseph00325232020-07-29 17:51:48 +053071 const Manager& manager, std::function<void(bool)> callBack = nullptr
72#ifdef PLDM
73 ,
74 std::function<void(instanceID)> resetCallBack = nullptr
75#endif
76 ) :
77
Alexander Filippov1d69e192019-03-21 18:12:07 +030078 Interface(bus, getDbusPath(path).c_str(), true),
79 bus(bus), 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(
91 bus,
92 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)),
99 occCmd(instance, bus,
100 (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
Chris Caina8857c52021-01-27 11:53:05 -0600150 /** @brief Read OCC state (will trigger kernel to poll the OCC) */
151 void readOccState();
152
Gunnar Mills94df8c92018-09-14 14:50:03 -0500153 private:
154 /** @brief sdbus handle */
155 sdbusplus::bus::bus& bus;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530156
Gunnar Mills94df8c92018-09-14 14:50:03 -0500157 /** @brief OCC dbus object path */
158 std::string path;
Vishwanatha Subbanna32e84e92017-06-28 19:17:28 +0530159
Gunnar Mills94df8c92018-09-14 14:50:03 -0500160 /** @brief Callback handler to be invoked during property change.
161 * This is a handler in Manager class
162 */
163 std::function<void(bool)> callBack;
Vishwanatha Subbanna2dc9b1a2017-08-18 18:29:41 +0530164
Gunnar Mills94df8c92018-09-14 14:50:03 -0500165 /** @brief OCC instance number. Ex, 0,1, etc */
Chris Caina8857c52021-01-27 11:53:05 -0600166 unsigned int instance;
167
168 /** @brief The last state read from the OCC */
169 unsigned int lastState = 0;
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +0530170
Alexander Filippov1d69e192019-03-21 18:12:07 +0300171 /** @brief OCC instance to Sensor definitions mapping */
172 static const std::map<instanceID, sensorDefs> sensorMap;
Vishwanatha Subbanna6add0b82017-07-21 19:02:37 +0530173
Gunnar Mills94df8c92018-09-14 14:50:03 -0500174 /** @brief OCC device object to do bind and unbind */
175 Device device;
Vishwanatha Subbannaee4d83d2017-06-29 18:35:00 +0530176
Gunnar Mills94df8c92018-09-14 14:50:03 -0500177 /** @brief Subscribe to host control signal
178 *
179 * Once the OCC reset is requested, BMC sends that message to host.
180 * If the host does not ack the message, then there would be a timeout
181 * and we need to catch that to log an error
182 **/
183 sdbusplus::bus::match_t hostControlSignal;
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530184
Chris Caina8857c52021-01-27 11:53:05 -0600185 /** @brief Command object to send commands to the OCC */
186 OccCommand occCmd;
187
Gunnar Mills94df8c92018-09-14 14:50:03 -0500188 /** @brief Callback handler when device errors are detected
189 *
190 * @param[in] error - True if an error is reported, false otherwise
191 */
192 void deviceErrorHandler(bool error);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530193
Gunnar Mills94df8c92018-09-14 14:50:03 -0500194 /** @brief Callback function on host control signals
195 *
196 * @param[in] msg - Data associated with subscribed signal
197 */
198 void hostControlEvent(sdbusplus::message::message& msg);
Vishwanatha Subbanna30e329a2017-07-24 23:13:14 +0530199
Gunnar Mills94df8c92018-09-14 14:50:03 -0500200 /** @brief Sends a message to host control command handler to reset OCC
201 */
202 void resetOCC();
Alexander Filippov1d69e192019-03-21 18:12:07 +0300203
204 /** @brief Determines the instance ID by specified object path.
205 * @param[in] path Estimated OCC Dbus object path
206 * @return Instance number
207 */
208 static int getInstance(const std::string& path)
209 {
210 return (path.empty() ? 0 : path.back() - '0');
211 }
212
213 /** @brief Override the sensor name with name from the definition.
214 * @param[in] estimatedPath - Estimated OCC Dbus object path
215 * @return Fixed OCC DBus object path
216 */
217 static std::string getDbusPath(const std::string& estimatedPath)
218 {
219 if (!estimatedPath.empty())
220 {
221 auto it = sensorMap.find(getInstance(estimatedPath));
222 if (sensorMap.end() != it)
223 {
224 auto& name = std::get<1>(it->second);
225 if (!name.empty() && name != "None")
226 {
227 auto path = fs::path(estimatedPath);
228 path.replace_filename(name);
229 return path.string();
230 }
231 }
232 }
233
234 return estimatedPath;
235 }
Tom Joseph00325232020-07-29 17:51:48 +0530236#ifdef PLDM
237 std::function<void(instanceID)> resetCallBack = nullptr;
238#endif
Vishwanatha Subbanna307d80b2017-06-28 15:56:09 +0530239};
240
241} // namespace occ
242} // namespace open_power