Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 3 | #include "i2c_occ.hpp" |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 4 | #include "occ_command.hpp" |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 5 | #include "occ_device.hpp" |
| 6 | #include "occ_events.hpp" |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 7 | #include "utils.hpp" |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 8 | |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 9 | #include <functional> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 10 | #include <org/open_power/Control/Host/server.hpp> |
| 11 | #include <org/open_power/OCC/Status/server.hpp> |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 12 | #include <sdbusplus/bus.hpp> |
| 13 | #include <sdbusplus/server/object.hpp> |
Lei YU | 0ab90ca | 2017-07-13 17:02:23 +0800 | [diff] [blame] | 14 | |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 15 | namespace open_power |
| 16 | { |
| 17 | namespace occ |
| 18 | { |
| 19 | |
Edward A. James | 636577f | 2017-10-06 10:53:55 -0500 | [diff] [blame] | 20 | class Manager; |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 21 | namespace Base = sdbusplus::org::open_power::OCC::server; |
| 22 | using Interface = sdbusplus::server::object::object<Base::Status>; |
| 23 | |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 24 | // IPMID's host control application |
| 25 | namespace Control = sdbusplus::org::open_power::Control::server; |
| 26 | |
| 27 | // For waiting on signals |
| 28 | namespace sdbusRule = sdbusplus::bus::match::rules; |
| 29 | |
Vishwanatha Subbanna | 6add0b8 | 2017-07-21 19:02:37 +0530 | [diff] [blame] | 30 | // OCC status instance. Ex. for "occ0", the instance is 0 |
| 31 | using instanceID = int; |
| 32 | |
| 33 | // IPMI sensor ID for a given OCC instance |
| 34 | using sensorID = uint8_t; |
| 35 | |
Alexander Filippov | 1d69e19 | 2019-03-21 18:12:07 +0300 | [diff] [blame] | 36 | // Human readable sensor name for DBus tree. E.g. "CPU0_OCC" |
| 37 | using sensorName = std::string; |
| 38 | |
| 39 | // OCC sensors definitions in the map |
| 40 | using sensorDefs = std::tuple<sensorID, sensorName>; |
| 41 | |
Eddie James | e7d976b | 2018-02-26 13:42:45 -0600 | [diff] [blame] | 42 | // OCC sysfs name prefix |
| 43 | const std::string sysfsName = "occ-hwmon"; |
| 44 | |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 45 | /** @class Status |
| 46 | * @brief Implementation of OCC Active Status |
| 47 | */ |
| 48 | class Status : public Interface |
| 49 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 50 | 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 Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 57 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 58 | /** @brief Constructs the Status object and |
| 59 | * the underlying device object |
| 60 | * |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 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 Joseph | 0032523 | 2020-07-29 17:51:48 +0530 | [diff] [blame] | 66 | * @param[in] resetCallBack - callback handler to invoke for resetting the |
| 67 | * OCC if PLDM is the host communication |
| 68 | * protocol |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 69 | */ |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 70 | Status(EventPtr& event, const char* path, const Manager& manager, |
| 71 | std::function<void(bool)> callBack = nullptr |
Tom Joseph | 0032523 | 2020-07-29 17:51:48 +0530 | [diff] [blame] | 72 | #ifdef PLDM |
| 73 | , |
| 74 | std::function<void(instanceID)> resetCallBack = nullptr |
| 75 | #endif |
| 76 | ) : |
| 77 | |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 78 | Interface(utils::getBus(), getDbusPath(path).c_str(), true), |
| 79 | path(path), callBack(callBack), instance(getInstance(path)), |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 80 | device(event, |
Lei YU | 0ab90ca | 2017-07-13 17:02:23 +0800 | [diff] [blame] | 81 | #ifdef I2C_OCC |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 82 | fs::path(DEV_PATH) / i2c_occ::getI2cDeviceName(path), |
Lei YU | 0ab90ca | 2017-07-13 17:02:23 +0800 | [diff] [blame] | 83 | #else |
Eddie James | 774f9af | 2019-03-19 20:58:53 +0000 | [diff] [blame] | 84 | fs::path(DEV_PATH) / |
| 85 | fs::path(sysfsName + "." + std::to_string(instance + 1)), |
Lei YU | 0ab90ca | 2017-07-13 17:02:23 +0800 | [diff] [blame] | 86 | #endif |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 87 | manager, *this, |
| 88 | std::bind(std::mem_fn(&Status::deviceErrorHandler), this, |
| 89 | std::placeholders::_1)), |
| 90 | hostControlSignal( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 91 | utils::getBus(), |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 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 Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 98 | std::placeholders::_1)), |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 99 | occCmd(instance, (fs::path(OCC_CONTROL_ROOT) / |
| 100 | (std::string(OCC_NAME) + std::to_string(instance))) |
| 101 | .c_str()) |
Tom Joseph | 0032523 | 2020-07-29 17:51:48 +0530 | [diff] [blame] | 102 | #ifdef PLDM |
| 103 | , |
| 104 | resetCallBack(resetCallBack) |
| 105 | #endif |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 106 | { |
| 107 | // Check to see if we have OCC already bound. If so, just set it |
| 108 | if (device.bound()) |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 109 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 110 | this->occActive(true); |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 111 | } |
| 112 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 113 | // Announce that we are ready |
| 114 | this->emit_object_added(); |
| 115 | } |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 116 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 117 | /** @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 Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 122 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 123 | /** @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 Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 130 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 131 | /** @brief Starts OCC error detection */ |
| 132 | inline void addErrorWatch() |
| 133 | { |
| 134 | return device.addErrorWatch(); |
| 135 | } |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 136 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 137 | /** @brief Stops OCC error detection */ |
| 138 | inline void removeErrorWatch() |
| 139 | { |
| 140 | return device.removeErrorWatch(); |
| 141 | } |
Eddie James | dae2d94 | 2017-12-20 10:50:03 -0600 | [diff] [blame] | 142 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 143 | /** @brief Starts to watch how many OCCs are present on the master */ |
| 144 | inline void addPresenceWatchMaster() |
| 145 | { |
| 146 | return device.addPresenceWatchMaster(); |
| 147 | } |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 148 | |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 149 | /** @brief Gets the occ instance number */ |
| 150 | unsigned int getOccInstanceID() |
| 151 | { |
| 152 | return instance; |
| 153 | } |
| 154 | |
| 155 | /** @brief Is this OCC the master OCC */ |
| 156 | bool isMasterOcc() |
| 157 | { |
| 158 | return device.master(); |
| 159 | } |
| 160 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 161 | /** @brief Read OCC state (will trigger kernel to poll the OCC) */ |
| 162 | void readOccState(); |
| 163 | |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 164 | #ifdef POWER10 |
| 165 | /** @brief Handle additional tasks when the OCCs reach active state */ |
| 166 | void occsWentActive(); |
| 167 | |
| 168 | /** @brief Send mode change command to the master OCC |
| 169 | * @return SUCCESS on success |
| 170 | */ |
| 171 | CmdStatus sendModeChange(); |
| 172 | #endif // POWER10 |
| 173 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 174 | private: |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 175 | /** @brief OCC dbus object path */ |
| 176 | std::string path; |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 177 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 178 | /** @brief Callback handler to be invoked during property change. |
| 179 | * This is a handler in Manager class |
| 180 | */ |
| 181 | std::function<void(bool)> callBack; |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 182 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 183 | /** @brief OCC instance number. Ex, 0,1, etc */ |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 184 | unsigned int instance; |
| 185 | |
| 186 | /** @brief The last state read from the OCC */ |
| 187 | unsigned int lastState = 0; |
Vishwanatha Subbanna | 6add0b8 | 2017-07-21 19:02:37 +0530 | [diff] [blame] | 188 | |
Alexander Filippov | 1d69e19 | 2019-03-21 18:12:07 +0300 | [diff] [blame] | 189 | /** @brief OCC instance to Sensor definitions mapping */ |
| 190 | static const std::map<instanceID, sensorDefs> sensorMap; |
Vishwanatha Subbanna | 6add0b8 | 2017-07-21 19:02:37 +0530 | [diff] [blame] | 191 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 192 | /** @brief OCC device object to do bind and unbind */ |
| 193 | Device device; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 194 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 195 | /** @brief Subscribe to host control signal |
| 196 | * |
| 197 | * Once the OCC reset is requested, BMC sends that message to host. |
| 198 | * If the host does not ack the message, then there would be a timeout |
| 199 | * and we need to catch that to log an error |
| 200 | **/ |
| 201 | sdbusplus::bus::match_t hostControlSignal; |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 202 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 203 | /** @brief Command object to send commands to the OCC */ |
| 204 | OccCommand occCmd; |
| 205 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 206 | /** @brief Callback handler when device errors are detected |
| 207 | * |
| 208 | * @param[in] error - True if an error is reported, false otherwise |
| 209 | */ |
| 210 | void deviceErrorHandler(bool error); |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 211 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 212 | /** @brief Callback function on host control signals |
| 213 | * |
| 214 | * @param[in] msg - Data associated with subscribed signal |
| 215 | */ |
| 216 | void hostControlEvent(sdbusplus::message::message& msg); |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 217 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 218 | /** @brief Sends a message to host control command handler to reset OCC |
| 219 | */ |
| 220 | void resetOCC(); |
Alexander Filippov | 1d69e19 | 2019-03-21 18:12:07 +0300 | [diff] [blame] | 221 | |
| 222 | /** @brief Determines the instance ID by specified object path. |
| 223 | * @param[in] path Estimated OCC Dbus object path |
| 224 | * @return Instance number |
| 225 | */ |
| 226 | static int getInstance(const std::string& path) |
| 227 | { |
| 228 | return (path.empty() ? 0 : path.back() - '0'); |
| 229 | } |
| 230 | |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 231 | #ifdef POWER10 |
| 232 | /** @brief Query the current Hypervisor target |
| 233 | * @return true if the current Hypervisor target is PowerVM |
| 234 | */ |
| 235 | bool isPowerVM(); |
| 236 | |
| 237 | /** @brief Get the requested power mode property |
| 238 | * @return Power mode |
| 239 | */ |
| 240 | SysPwrMode getMode(); |
| 241 | |
| 242 | /** @brief Send Idle Power Saver config data to the master OCC |
| 243 | * @return SUCCESS on success |
| 244 | */ |
| 245 | CmdStatus sendIpsData(); |
| 246 | #endif // POWER10 |
| 247 | |
Alexander Filippov | 1d69e19 | 2019-03-21 18:12:07 +0300 | [diff] [blame] | 248 | /** @brief Override the sensor name with name from the definition. |
| 249 | * @param[in] estimatedPath - Estimated OCC Dbus object path |
| 250 | * @return Fixed OCC DBus object path |
| 251 | */ |
| 252 | static std::string getDbusPath(const std::string& estimatedPath) |
| 253 | { |
| 254 | if (!estimatedPath.empty()) |
| 255 | { |
| 256 | auto it = sensorMap.find(getInstance(estimatedPath)); |
| 257 | if (sensorMap.end() != it) |
| 258 | { |
| 259 | auto& name = std::get<1>(it->second); |
| 260 | if (!name.empty() && name != "None") |
| 261 | { |
| 262 | auto path = fs::path(estimatedPath); |
| 263 | path.replace_filename(name); |
| 264 | return path.string(); |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return estimatedPath; |
| 270 | } |
Tom Joseph | 0032523 | 2020-07-29 17:51:48 +0530 | [diff] [blame] | 271 | #ifdef PLDM |
| 272 | std::function<void(instanceID)> resetCallBack = nullptr; |
| 273 | #endif |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | } // namespace occ |
| 277 | } // namespace open_power |