Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 1 | #pragma once |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame^] | 2 | #include "occ_events.hpp" |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 3 | #include "occ_status.hpp" |
| 4 | #include "utils.hpp" |
| 5 | |
| 6 | #include <libpldm/pldm.h> |
| 7 | |
| 8 | #include <sdbusplus/bus/match.hpp> |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame^] | 9 | #include <sdeventplus/event.hpp> |
| 10 | #include <sdeventplus/utility/timer.hpp> |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 11 | |
| 12 | namespace pldm |
| 13 | { |
| 14 | |
| 15 | namespace MatchRules = sdbusplus::bus::match::rules; |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame^] | 16 | using namespace open_power::occ; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 17 | |
| 18 | using CompositeEffecterCount = uint8_t; |
| 19 | using EffecterID = uint16_t; |
| 20 | using EntityType = uint16_t; |
| 21 | using EntityInstance = uint16_t; |
| 22 | using EventState = uint8_t; |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 23 | using InstanceToEffecter = std::map<open_power::occ::instanceID, EffecterID>; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 24 | using PdrList = std::vector<std::vector<uint8_t>>; |
| 25 | using SensorID = uint16_t; |
| 26 | using SensorOffset = uint8_t; |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 27 | using SensorToInstance = std::map<SensorID, open_power::occ::instanceID>; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 28 | using TerminusID = uint8_t; |
| 29 | |
| 30 | /** @brief Hardcoded TID */ |
| 31 | constexpr TerminusID tid = 0; |
| 32 | |
| 33 | /** @brief OCC instance starts with 0 for example "occ0" */ |
| 34 | constexpr open_power::occ::instanceID start = 0; |
| 35 | |
| 36 | /** @brief Hardcoded mctpEid for HBRT */ |
| 37 | constexpr mctp_eid_t mctpEid = 10; |
| 38 | |
| 39 | /** @class Interface |
| 40 | * |
| 41 | * @brief Abstracts the PLDM details related to the OCC |
| 42 | */ |
| 43 | class Interface |
| 44 | { |
| 45 | public: |
| 46 | Interface() = delete; |
| 47 | ~Interface() = default; |
| 48 | Interface(const Interface&) = delete; |
| 49 | Interface& operator=(const Interface&) = delete; |
| 50 | Interface(Interface&&) = delete; |
| 51 | Interface& operator=(Interface&&) = delete; |
| 52 | |
| 53 | /** @brief Constructs the PLDM Interface object for OCC functions |
| 54 | * |
| 55 | * @param[in] callBack - callBack handler to invoke when the OCC state |
| 56 | * changes. |
| 57 | */ |
| 58 | explicit Interface( |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 59 | std::function<bool(open_power::occ::instanceID, bool)> callBack, |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame^] | 60 | std::function<void(open_power::occ::instanceID, bool)> sbeCallBack, |
| 61 | EventPtr& event) : |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 62 | callBack(callBack), |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame^] | 63 | sbeCallBack(sbeCallBack), event(event), |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 64 | pldmEventSignal( |
| 65 | open_power::occ::utils::getBus(), |
| 66 | MatchRules::type::signal() + |
| 67 | MatchRules::member("StateSensorEvent") + |
| 68 | MatchRules::path("/xyz/openbmc_project/pldm") + |
| 69 | MatchRules::interface("xyz.openbmc_project.PLDM.Event"), |
| 70 | std::bind(std::mem_fn(&Interface::sensorEvent), this, |
| 71 | std::placeholders::_1)), |
| 72 | hostStateSignal( |
| 73 | open_power::occ::utils::getBus(), |
| 74 | MatchRules::propertiesChanged("/xyz/openbmc_project/state/host0", |
| 75 | "xyz.openbmc_project.State.Host"), |
| 76 | std::bind(std::mem_fn(&Interface::hostStateEvent), this, |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame^] | 77 | std::placeholders::_1)), |
| 78 | sdpEvent(sdeventplus::Event::get_default()), |
| 79 | pldmRspTimer( |
| 80 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>( |
| 81 | sdpEvent, std::bind(&Interface::pldmRspExpired, this))) |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 82 | {} |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 83 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 84 | /** @brief Fetch the state sensor PDRs and populate the cache with |
| 85 | * sensorId to OCC/SBE instance mapping information and the sensor |
| 86 | * offset for the relevent state set. |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 87 | * |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 88 | * @param[in] stateSetId - the state set ID to look for |
| 89 | * @param[out] sensorInstanceMap - map of sensorID to instance |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 90 | * @param[out] sensorOffset - sensor offset of interested state set ID |
| 91 | */ |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 92 | void fetchSensorInfo(uint16_t stateSetId, |
| 93 | SensorToInstance& sensorInstanceMap, |
| 94 | SensorOffset& sensorOffset); |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 95 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 96 | /** @brief Fetch the OCC/SBE state effecter PDRs and populate the cache |
| 97 | * with OCC/SBE instance to EffecterID information. |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 98 | * |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 99 | * @param[in] stateSetId - the state set ID to look for |
| 100 | * @param[out] instanceToEffecterMap - map of instance to effecterID |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 101 | * @param[out] count - sensor offset of interested state set ID |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 102 | * @param[out] stateIdPos - position of the stateSetID |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 103 | */ |
Eddie James | 432dc48 | 2021-11-19 15:29:31 -0600 | [diff] [blame] | 104 | void fetchEffecterInfo(uint16_t stateSetId, |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 105 | InstanceToEffecter& instanceToEffecterMap, |
| 106 | CompositeEffecterCount& count, uint8_t& stateIdPos); |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 107 | |
| 108 | /** @brief Prepare the request for SetStateEffecterStates command |
| 109 | * |
| 110 | * @param[in] instanceId - PLDM instanceID |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 111 | * @param[in] effecterId - the instance effecter ID |
| 112 | * @param[in] effecterCount - compositeEffecterCount for the effecter PDR |
| 113 | * @param[in] stateIdPos - position of the stateSetID |
| 114 | * @param[in] stateSetValue - the value to set the state set to |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 115 | * |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 116 | * @return PLDM request message to be sent to host for OCC reset or SBE |
| 117 | * HRESET, empty response in the case of failure. |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 118 | */ |
| 119 | std::vector<uint8_t> |
| 120 | prepareSetEffecterReq(uint8_t instanceId, EffecterID effecterId, |
| 121 | CompositeEffecterCount effecterCount, |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 122 | uint8_t stateIdPos, uint8_t stateSetValue); |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 123 | |
| 124 | /** @brief Send the PLDM message to reset the OCC |
| 125 | * |
| 126 | * @param[in] instanceId - OCC instance to reset |
| 127 | * |
| 128 | */ |
| 129 | void resetOCC(open_power::occ::instanceID occInstanceId); |
| 130 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 131 | /** @brief Send the PLDM message to perform the HRESET |
| 132 | * |
| 133 | * @param[in] instanceID - SBE instance to HRESET |
| 134 | */ |
| 135 | void sendHRESET(open_power::occ::instanceID sbeInstanceId); |
| 136 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame^] | 137 | /** @brief Check if the OCC active sensor is available |
| 138 | * On successful read, the Manager callback will be called to update |
| 139 | * the status |
| 140 | * |
| 141 | * @param[in] instance - OCC instance to check |
| 142 | */ |
| 143 | void checkActiveSensor(uint8_t instance); |
| 144 | |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 145 | private: |
| 146 | /** @brief Callback handler to be invoked when the state of the OCC |
| 147 | * changes |
| 148 | */ |
| 149 | std::function<bool(open_power::occ::instanceID, bool)> callBack = nullptr; |
| 150 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 151 | /** @brief Callback handler to be invoked when the maintenance state of the |
| 152 | * SBE changes |
| 153 | */ |
| 154 | std::function<void(open_power::occ::instanceID, bool)> sbeCallBack = |
| 155 | nullptr; |
| 156 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame^] | 157 | /** @brief reference to sd_event wrapped in unique_ptr */ |
| 158 | EventPtr& event; |
| 159 | |
| 160 | /** @brief event source wrapped in unique_ptr */ |
| 161 | EventSourcePtr eventSource; |
| 162 | |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 163 | /** @brief Used to subscribe to D-Bus PLDM StateSensorEvent signal and |
| 164 | * processes if the event corresponds to OCC state change. |
| 165 | */ |
| 166 | sdbusplus::bus::match_t pldmEventSignal; |
| 167 | |
| 168 | /** @brief Used to subscribe for host state change signal */ |
| 169 | sdbusplus::bus::match_t hostStateSignal; |
| 170 | |
| 171 | /** @brief PLDM Sensor ID to OCC Instance mapping |
| 172 | */ |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 173 | SensorToInstance sensorToOCCInstance; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 174 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 175 | /** @brief PLDM Sensor ID to SBE Instance mapping |
| 176 | */ |
| 177 | SensorToInstance sensorToSBEInstance; |
| 178 | |
| 179 | /** @brief Sensor offset of OCC state set ID |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 180 | * PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS in state sensor PDR. |
| 181 | */ |
George Liu | f3a4a69 | 2021-12-28 13:59:51 +0800 | [diff] [blame] | 182 | SensorOffset OCCSensorOffset = 0; |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 183 | |
| 184 | /** @brief Sensor offset of the SBE state set ID |
| 185 | * PLDM_OEM_IBM_SBE_HRESET_STATE in state sensor PDR. |
| 186 | */ |
George Liu | f3a4a69 | 2021-12-28 13:59:51 +0800 | [diff] [blame] | 187 | SensorOffset SBESensorOffset = 0; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 188 | |
| 189 | /** @brief OCC Instance mapping to PLDM Effecter ID |
| 190 | */ |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 191 | InstanceToEffecter occInstanceToEffecter; |
| 192 | |
| 193 | /** @brief SBE instance mapping to PLDM Effecter ID |
| 194 | */ |
| 195 | InstanceToEffecter sbeInstanceToEffecter; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 196 | |
| 197 | /** @brief compositeEffecterCount for OCC reset state effecter PDR */ |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 198 | CompositeEffecterCount OCCEffecterCount = 0; |
| 199 | |
| 200 | /** @brief compositeEffecterCount for SBE HRESET state effecter PDR */ |
| 201 | CompositeEffecterCount SBEEffecterCount = 0; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 202 | |
| 203 | /** @brief Position of Boot/Restart Cause stateSetID in OCC state |
| 204 | * effecter PDR |
| 205 | */ |
| 206 | uint8_t bootRestartPosition = 0; |
| 207 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 208 | /** @brief Position of the SBE maintenance stateSetID in the state |
| 209 | * effecter PDR |
| 210 | */ |
| 211 | uint8_t sbeMaintenanceStatePosition = 0; |
| 212 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame^] | 213 | /** @brief OCC instance number for the PLDM message */ |
| 214 | uint8_t pldmResponseOcc = 0; |
| 215 | |
| 216 | /** @brief File descriptor for PLDM messages */ |
| 217 | int pldmFd = -1; |
| 218 | |
| 219 | /** @brief MCTP instance number used in PLDM requests |
| 220 | */ |
| 221 | uint8_t mctpInstance{}; |
| 222 | |
| 223 | /** @brief The response for the PLDM request msg is received flag. |
| 224 | */ |
| 225 | bool pldmResponseReceived = false; |
| 226 | |
| 227 | /** @brief The response for the PLDM request has timed out. |
| 228 | */ |
| 229 | bool pldmResponseTimeout = false; |
| 230 | |
| 231 | /** @brief timer event */ |
| 232 | sdeventplus::Event sdpEvent; |
| 233 | |
| 234 | /** @brief Timer that is started when PLDM command is sent |
| 235 | */ |
| 236 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> pldmRspTimer; |
| 237 | |
| 238 | /** @brief Callback when PLDM response has not been received within the |
| 239 | * timeout period. |
| 240 | */ |
| 241 | void pldmRspExpired(); |
| 242 | |
| 243 | /** @brief Close the MCTP file */ |
| 244 | void pldmClose(); |
| 245 | |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 246 | /** @brief When the OCC state changes host sends PlatformEventMessage |
| 247 | * StateSensorEvent, this function processes the D-Bus signal |
| 248 | * with the sensor event information and invokes the callback |
| 249 | * to change the OCC state. |
| 250 | * |
| 251 | * @param[in] msg - data associated with the subscribed signal |
| 252 | */ |
| 253 | void sensorEvent(sdbusplus::message::message& msg); |
| 254 | |
| 255 | /** @brief When the host state changes and if the CurrentHostState is |
| 256 | * xyz.openbmc_project.State.Host.HostState.Off then |
| 257 | * the cache of OCC sensors and effecters mapping is cleared. |
| 258 | * |
| 259 | * @param[in] msg - data associated with the subscribed signal |
| 260 | */ |
| 261 | void hostStateEvent(sdbusplus::message::message& msg); |
| 262 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame^] | 263 | /** @brief Called when it is determined that the Host is not running. |
| 264 | * The cache of OCC sensors and effecters mapping is cleared. |
| 265 | */ |
| 266 | void clearData(); |
| 267 | |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 268 | /** @brief Check if the PDR cache for PLDM OCC sensors is valid |
| 269 | * |
| 270 | * @return true if cache is populated and false if the cache is not |
| 271 | * populated. |
| 272 | */ |
| 273 | auto isOCCSensorCacheValid() |
| 274 | { |
| 275 | return (sensorToOCCInstance.empty() ? false : true); |
| 276 | } |
| 277 | |
| 278 | /** @brief Check if the PDR cache for PLDM OCC effecters is valid |
| 279 | * |
| 280 | * @return true if cache is populated and false if the cache is not |
| 281 | * populated. |
| 282 | */ |
| 283 | auto isPDREffecterCacheValid() |
| 284 | { |
| 285 | return (occInstanceToEffecter.empty() ? false : true); |
| 286 | } |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 287 | |
| 288 | /** @brief Query PLDM for the MCTP requester instance id |
| 289 | * |
| 290 | * @param[out] - the instance id |
| 291 | * |
| 292 | * @return true if the id was found and false if not |
| 293 | */ |
| 294 | bool getMctpInstanceId(uint8_t& instanceId); |
| 295 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame^] | 296 | /** @brief Encode a GetStateSensor command into a PLDM request |
| 297 | * @param[in] instance - OCC instance number |
| 298 | * @param[in] sensorId - OCC Active sensor ID number |
| 299 | * |
| 300 | * @return request - The encoded PLDM messsage to be sent |
| 301 | */ |
| 302 | std::vector<uint8_t> encodeGetStateSensorRequest(uint8_t instance, |
| 303 | uint16_t sensorId); |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 304 | /** @brief Send the PLDM request |
| 305 | * |
Chris Cain | d1b6826 | 2022-02-28 09:56:50 -0600 | [diff] [blame] | 306 | * @param[in] request - the request data |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame^] | 307 | * @param[in] rspExpected - false: no need to wait for the response |
| 308 | * true: will need to process response in callback |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 309 | */ |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame^] | 310 | void sendPldm(const std::vector<uint8_t>& request, const uint8_t instance, |
| 311 | const bool rspExpected = false); |
| 312 | |
| 313 | /** @brief Register a callback function to handle the PLDM response */ |
| 314 | void registerPldmRspCallback(); |
| 315 | |
| 316 | /** @brief callback for the PLDM response event |
| 317 | * |
| 318 | * @param[in] es - Populated event source |
| 319 | * @param[in] fd - Associated File descriptor |
| 320 | * @param[in] revents - Type of event |
| 321 | * @param[in] userData - User data that was passed during registration |
| 322 | * |
| 323 | * @return - 0 or positive number on success and negative |
| 324 | * errno otherwise |
| 325 | */ |
| 326 | static int pldmRspCallback(sd_event_source* es, int fd, uint32_t revents, |
| 327 | void* userData); |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 328 | }; |
| 329 | |
| 330 | } // namespace pldm |