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