Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "occ_status.hpp" |
| 4 | #include "utils.hpp" |
| 5 | |
| 6 | #include <libpldm/pldm.h> |
| 7 | |
| 8 | #include <sdbusplus/bus/match.hpp> |
| 9 | |
| 10 | namespace pldm |
| 11 | { |
| 12 | |
| 13 | namespace MatchRules = sdbusplus::bus::match::rules; |
| 14 | |
| 15 | using CompositeEffecterCount = uint8_t; |
| 16 | using EffecterID = uint16_t; |
| 17 | using EntityType = uint16_t; |
| 18 | using EntityInstance = uint16_t; |
| 19 | using EventState = uint8_t; |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 20 | using InstanceToEffecter = std::map<open_power::occ::instanceID, EffecterID>; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 21 | using PdrList = std::vector<std::vector<uint8_t>>; |
| 22 | using SensorID = uint16_t; |
| 23 | using SensorOffset = uint8_t; |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 24 | using SensorToInstance = std::map<SensorID, open_power::occ::instanceID>; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 25 | using TerminusID = uint8_t; |
| 26 | |
| 27 | /** @brief Hardcoded TID */ |
| 28 | constexpr TerminusID tid = 0; |
| 29 | |
| 30 | /** @brief OCC instance starts with 0 for example "occ0" */ |
| 31 | constexpr open_power::occ::instanceID start = 0; |
| 32 | |
| 33 | /** @brief Hardcoded mctpEid for HBRT */ |
| 34 | constexpr mctp_eid_t mctpEid = 10; |
| 35 | |
| 36 | /** @class Interface |
| 37 | * |
| 38 | * @brief Abstracts the PLDM details related to the OCC |
| 39 | */ |
| 40 | class Interface |
| 41 | { |
| 42 | public: |
| 43 | Interface() = delete; |
| 44 | ~Interface() = default; |
| 45 | Interface(const Interface&) = delete; |
| 46 | Interface& operator=(const Interface&) = delete; |
| 47 | Interface(Interface&&) = delete; |
| 48 | Interface& operator=(Interface&&) = delete; |
| 49 | |
| 50 | /** @brief Constructs the PLDM Interface object for OCC functions |
| 51 | * |
| 52 | * @param[in] callBack - callBack handler to invoke when the OCC state |
| 53 | * changes. |
| 54 | */ |
| 55 | explicit Interface( |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 56 | std::function<bool(open_power::occ::instanceID, bool)> callBack, |
| 57 | std::function<void(open_power::occ::instanceID, bool)> sbeCallBack) : |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 58 | callBack(callBack), |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 59 | sbeCallBack(sbeCallBack), |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 60 | pldmEventSignal( |
| 61 | open_power::occ::utils::getBus(), |
| 62 | MatchRules::type::signal() + |
| 63 | MatchRules::member("StateSensorEvent") + |
| 64 | MatchRules::path("/xyz/openbmc_project/pldm") + |
| 65 | MatchRules::interface("xyz.openbmc_project.PLDM.Event"), |
| 66 | std::bind(std::mem_fn(&Interface::sensorEvent), this, |
| 67 | std::placeholders::_1)), |
| 68 | hostStateSignal( |
| 69 | open_power::occ::utils::getBus(), |
| 70 | MatchRules::propertiesChanged("/xyz/openbmc_project/state/host0", |
| 71 | "xyz.openbmc_project.State.Host"), |
| 72 | std::bind(std::mem_fn(&Interface::hostStateEvent), this, |
| 73 | std::placeholders::_1)) |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 74 | {} |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 75 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 76 | /** @brief Fetch the state sensor PDRs and populate the cache with |
| 77 | * sensorId to OCC/SBE instance mapping information and the sensor |
| 78 | * offset for the relevent state set. |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 79 | * |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 80 | * @param[in] stateSetId - the state set ID to look for |
| 81 | * @param[out] sensorInstanceMap - map of sensorID to instance |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 82 | * @param[out] sensorOffset - sensor offset of interested state set ID |
| 83 | */ |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 84 | void fetchSensorInfo(uint16_t stateSetId, |
| 85 | SensorToInstance& sensorInstanceMap, |
| 86 | SensorOffset& sensorOffset); |
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 | /** @brief Fetch the OCC/SBE state effecter PDRs and populate the cache |
| 89 | * with OCC/SBE instance to EffecterID information. |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 90 | * |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 91 | * @param[in] entityId - the entity ID to query |
| 92 | * @param[in] stateSetId - the state set ID to look for |
| 93 | * @param[out] instanceToEffecterMap - map of instance to effecterID |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 94 | * @param[out] count - sensor offset of interested state set ID |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 95 | * @param[out] stateIdPos - position of the stateSetID |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 96 | */ |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 97 | void fetchEffecterInfo(uint16_t entityId, uint16_t stateSetId, |
| 98 | InstanceToEffecter& instanceToEffecterMap, |
| 99 | CompositeEffecterCount& count, uint8_t& stateIdPos); |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 100 | |
| 101 | /** @brief Prepare the request for SetStateEffecterStates command |
| 102 | * |
| 103 | * @param[in] instanceId - PLDM instanceID |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 104 | * @param[in] effecterId - the instance effecter ID |
| 105 | * @param[in] effecterCount - compositeEffecterCount for the effecter PDR |
| 106 | * @param[in] stateIdPos - position of the stateSetID |
| 107 | * @param[in] stateSetValue - the value to set the state set to |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 108 | * |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 109 | * @return PLDM request message to be sent to host for OCC reset or SBE |
| 110 | * HRESET, empty response in the case of failure. |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 111 | */ |
| 112 | std::vector<uint8_t> |
| 113 | prepareSetEffecterReq(uint8_t instanceId, EffecterID effecterId, |
| 114 | CompositeEffecterCount effecterCount, |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 115 | uint8_t stateIdPos, uint8_t stateSetValue); |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 116 | |
| 117 | /** @brief Send the PLDM message to reset the OCC |
| 118 | * |
| 119 | * @param[in] instanceId - OCC instance to reset |
| 120 | * |
| 121 | */ |
| 122 | void resetOCC(open_power::occ::instanceID occInstanceId); |
| 123 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 124 | /** @brief Send the PLDM message to perform the HRESET |
| 125 | * |
| 126 | * @param[in] instanceID - SBE instance to HRESET |
| 127 | */ |
| 128 | void sendHRESET(open_power::occ::instanceID sbeInstanceId); |
| 129 | |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 130 | private: |
| 131 | /** @brief Callback handler to be invoked when the state of the OCC |
| 132 | * changes |
| 133 | */ |
| 134 | std::function<bool(open_power::occ::instanceID, bool)> callBack = nullptr; |
| 135 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 136 | /** @brief Callback handler to be invoked when the maintenance state of the |
| 137 | * SBE changes |
| 138 | */ |
| 139 | std::function<void(open_power::occ::instanceID, bool)> sbeCallBack = |
| 140 | nullptr; |
| 141 | |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 142 | /** @brief Used to subscribe to D-Bus PLDM StateSensorEvent signal and |
| 143 | * processes if the event corresponds to OCC state change. |
| 144 | */ |
| 145 | sdbusplus::bus::match_t pldmEventSignal; |
| 146 | |
| 147 | /** @brief Used to subscribe for host state change signal */ |
| 148 | sdbusplus::bus::match_t hostStateSignal; |
| 149 | |
| 150 | /** @brief PLDM Sensor ID to OCC Instance mapping |
| 151 | */ |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 152 | SensorToInstance sensorToOCCInstance; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 153 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 154 | /** @brief PLDM Sensor ID to SBE Instance mapping |
| 155 | */ |
| 156 | SensorToInstance sensorToSBEInstance; |
| 157 | |
| 158 | /** @brief Sensor offset of OCC state set ID |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 159 | * PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS in state sensor PDR. |
| 160 | */ |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 161 | SensorOffset OCCSensorOffset; |
| 162 | |
| 163 | /** @brief Sensor offset of the SBE state set ID |
| 164 | * PLDM_OEM_IBM_SBE_HRESET_STATE in state sensor PDR. |
| 165 | */ |
| 166 | SensorOffset SBESensorOffset; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 167 | |
| 168 | /** @brief OCC Instance mapping to PLDM Effecter ID |
| 169 | */ |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 170 | InstanceToEffecter occInstanceToEffecter; |
| 171 | |
| 172 | /** @brief SBE instance mapping to PLDM Effecter ID |
| 173 | */ |
| 174 | InstanceToEffecter sbeInstanceToEffecter; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 175 | |
| 176 | /** @brief compositeEffecterCount for OCC reset state effecter PDR */ |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 177 | CompositeEffecterCount OCCEffecterCount = 0; |
| 178 | |
| 179 | /** @brief compositeEffecterCount for SBE HRESET state effecter PDR */ |
| 180 | CompositeEffecterCount SBEEffecterCount = 0; |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 181 | |
| 182 | /** @brief Position of Boot/Restart Cause stateSetID in OCC state |
| 183 | * effecter PDR |
| 184 | */ |
| 185 | uint8_t bootRestartPosition = 0; |
| 186 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 187 | /** @brief Position of the SBE maintenance stateSetID in the state |
| 188 | * effecter PDR |
| 189 | */ |
| 190 | uint8_t sbeMaintenanceStatePosition = 0; |
| 191 | |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 192 | /** @brief When the OCC state changes host sends PlatformEventMessage |
| 193 | * StateSensorEvent, this function processes the D-Bus signal |
| 194 | * with the sensor event information and invokes the callback |
| 195 | * to change the OCC state. |
| 196 | * |
| 197 | * @param[in] msg - data associated with the subscribed signal |
| 198 | */ |
| 199 | void sensorEvent(sdbusplus::message::message& msg); |
| 200 | |
| 201 | /** @brief When the host state changes and if the CurrentHostState is |
| 202 | * xyz.openbmc_project.State.Host.HostState.Off then |
| 203 | * the cache of OCC sensors and effecters mapping is cleared. |
| 204 | * |
| 205 | * @param[in] msg - data associated with the subscribed signal |
| 206 | */ |
| 207 | void hostStateEvent(sdbusplus::message::message& msg); |
| 208 | |
| 209 | /** @brief Check if the PDR cache for PLDM OCC sensors is valid |
| 210 | * |
| 211 | * @return true if cache is populated and false if the cache is not |
| 212 | * populated. |
| 213 | */ |
| 214 | auto isOCCSensorCacheValid() |
| 215 | { |
| 216 | return (sensorToOCCInstance.empty() ? false : true); |
| 217 | } |
| 218 | |
| 219 | /** @brief Check if the PDR cache for PLDM OCC effecters is valid |
| 220 | * |
| 221 | * @return true if cache is populated and false if the cache is not |
| 222 | * populated. |
| 223 | */ |
| 224 | auto isPDREffecterCacheValid() |
| 225 | { |
| 226 | return (occInstanceToEffecter.empty() ? false : true); |
| 227 | } |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 228 | |
| 229 | /** @brief Query PLDM for the MCTP requester instance id |
| 230 | * |
| 231 | * @param[out] - the instance id |
| 232 | * |
| 233 | * @return true if the id was found and false if not |
| 234 | */ |
| 235 | bool getMctpInstanceId(uint8_t& instanceId); |
| 236 | |
| 237 | /** @brief Send the PLDM request |
| 238 | * |
| 239 | * @param[in] - the request data |
| 240 | */ |
| 241 | void sendPldm(const std::vector<uint8_t>& request); |
Patrick Williams | 05e9559 | 2021-09-02 09:28:14 -0500 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | } // namespace pldm |