Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Deepak Kodihalli | d130e1a | 2020-06-17 05:55:32 -0500 | [diff] [blame] | 3 | #include "common/types.hpp" |
| 4 | #include "common/utils.hpp" |
Pavithra Barithaya | 3aec997 | 2020-12-14 01:55:44 -0600 | [diff] [blame] | 5 | #include "libpldmresponder/event_parser.hpp" |
Tom Joseph | b426860 | 2020-04-17 17:20:45 +0530 | [diff] [blame] | 6 | #include "libpldmresponder/pdr_utils.hpp" |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 7 | #include "pldmd/instance_id.hpp" |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 8 | #include "requester/handler.hpp" |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 9 | |
George Liu | c453e16 | 2022-12-21 17:16:23 +0800 | [diff] [blame] | 10 | #include <libpldm/base.h> |
| 11 | #include <libpldm/platform.h> |
| 12 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 13 | #include <sdeventplus/event.hpp> |
Deepak Kodihalli | 8cb6f66 | 2020-04-10 02:55:43 -0500 | [diff] [blame] | 14 | #include <sdeventplus/source/event.hpp> |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 15 | |
Deepak Kodihalli | 7246e0c | 2020-07-08 06:40:18 -0500 | [diff] [blame] | 16 | #include <deque> |
George Liu | 6492f52 | 2020-06-16 10:34:05 +0800 | [diff] [blame] | 17 | #include <map> |
| 18 | #include <memory> |
| 19 | #include <vector> |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 20 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 21 | namespace pldm |
| 22 | { |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 23 | using EntityType = uint16_t; |
Deepak Kodihalli | 8cb6f66 | 2020-04-10 02:55:43 -0500 | [diff] [blame] | 24 | // vector which would hold the PDR record handle data returned by |
| 25 | // pldmPDRRepositoryChgEvent event data |
| 26 | using ChangeEntry = uint32_t; |
Deepak Kodihalli | 7246e0c | 2020-07-08 06:40:18 -0500 | [diff] [blame] | 27 | using PDRRecordHandles = std::deque<ChangeEntry>; |
Deepak Kodihalli | 8cb6f66 | 2020-04-10 02:55:43 -0500 | [diff] [blame] | 28 | |
Tom Joseph | b426860 | 2020-04-17 17:20:45 +0530 | [diff] [blame] | 29 | /** @struct SensorEntry |
| 30 | * |
| 31 | * SensorEntry is a unique key which maps a sensorEventType request in the |
| 32 | * PlatformEventMessage command to a host sensor PDR. This struct is a key |
| 33 | * in a std::map, so implemented operator==and operator<. |
| 34 | */ |
| 35 | struct SensorEntry |
| 36 | { |
| 37 | pdr::TerminusID terminusID; |
| 38 | pdr::SensorID sensorID; |
| 39 | |
| 40 | bool operator==(const SensorEntry& e) const |
| 41 | { |
| 42 | return ((terminusID == e.terminusID) && (sensorID == e.sensorID)); |
| 43 | } |
| 44 | |
| 45 | bool operator<(const SensorEntry& e) const |
| 46 | { |
| 47 | return ((terminusID < e.terminusID) || |
| 48 | ((terminusID == e.terminusID) && (sensorID < e.sensorID))); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | using HostStateSensorMap = std::map<SensorEntry, pdr::SensorInfo>; |
Sampa Misra | 868c879 | 2020-05-26 03:12:13 -0500 | [diff] [blame] | 53 | using PDRList = std::vector<std::vector<uint8_t>>; |
Tom Joseph | b426860 | 2020-04-17 17:20:45 +0530 | [diff] [blame] | 54 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 55 | /** @class HostPDRHandler |
| 56 | * @brief This class can fetch and process PDRs from host firmware |
| 57 | * @details Provides an API to fetch PDRs from the host firmware. Upon |
| 58 | * receiving the PDRs, they are stored into the BMC's primary PDR repo. |
| 59 | * Adjustments are made to entity association PDRs received from the host, |
| 60 | * because they need to be assimilated into the BMC's entity association |
| 61 | * tree. A PLDM event containing the record handles of the updated entity |
| 62 | * association PDRs is sent to the host. |
| 63 | */ |
| 64 | class HostPDRHandler |
| 65 | { |
| 66 | public: |
| 67 | HostPDRHandler() = delete; |
| 68 | HostPDRHandler(const HostPDRHandler&) = delete; |
| 69 | HostPDRHandler(HostPDRHandler&&) = delete; |
| 70 | HostPDRHandler& operator=(const HostPDRHandler&) = delete; |
| 71 | HostPDRHandler& operator=(HostPDRHandler&&) = delete; |
| 72 | ~HostPDRHandler() = default; |
| 73 | |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 74 | using TerminusInfo = |
| 75 | std::tuple<pdr::TerminusID, pdr::EID, pdr::TerminusValidity>; |
| 76 | using TLPDRMap = std::map<pdr::TerminusHandle, TerminusInfo>; |
Sampa Misra | 868c879 | 2020-05-26 03:12:13 -0500 | [diff] [blame] | 77 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 78 | /** @brief Constructor |
| 79 | * @param[in] mctp_fd - fd of MCTP communications socket |
| 80 | * @param[in] mctp_eid - MCTP EID of host firmware |
| 81 | * @param[in] event - reference of main event loop of pldmd |
| 82 | * @param[in] repo - pointer to BMC's primary PDR repo |
Pavithra Barithaya | 3aec997 | 2020-12-14 01:55:44 -0600 | [diff] [blame] | 83 | * @param[in] eventsJsonDir - directory path which has the config JSONs |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 84 | * @param[in] entityTree - Pointer to BMC and Host entity association tree |
| 85 | * @param[in] bmcEntityTree - pointer to BMC's entity association tree |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 86 | * @param[in] instanceIdDb - reference to an InstanceIdDb object |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 87 | * @param[in] handler - PLDM request handler |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 88 | */ |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 89 | explicit HostPDRHandler( |
| 90 | int mctp_fd, uint8_t mctp_eid, sdeventplus::Event& event, |
| 91 | pldm_pdr* repo, const std::string& eventsJsonsDir, |
| 92 | pldm_entity_association_tree* entityTree, |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 93 | pldm_entity_association_tree* bmcEntityTree, |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 94 | pldm::InstanceIdDb& instanceIdDb, |
Tom Joseph | e5268cd | 2021-09-07 13:04:03 +0530 | [diff] [blame] | 95 | pldm::requester::Handler<pldm::requester::Request>* handler); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 96 | |
| 97 | /** @brief fetch PDRs from host firmware. See @class. |
| 98 | * @param[in] recordHandles - list of record handles pointing to host's |
| 99 | * PDRs that need to be fetched. |
| 100 | */ |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 101 | |
Deepak Kodihalli | 7246e0c | 2020-07-08 06:40:18 -0500 | [diff] [blame] | 102 | void fetchPDR(PDRRecordHandles&& recordHandles); |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 103 | |
Pavithra Barithaya | e8beb89 | 2020-04-14 23:24:25 -0500 | [diff] [blame] | 104 | /** @brief Send a PLDM event to host firmware containing a list of record |
| 105 | * handles of PDRs that the host firmware has to fetch. |
| 106 | * @param[in] pdrTypes - list of PDR types that need to be looked up in the |
| 107 | * BMC repo |
| 108 | * @param[in] eventDataFormat - format for PDRRepositoryChgEvent in DSP0248 |
| 109 | */ |
| 110 | void sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes, |
| 111 | uint8_t eventDataFormat); |
| 112 | |
Tom Joseph | b426860 | 2020-04-17 17:20:45 +0530 | [diff] [blame] | 113 | /** @brief Lookup host sensor info corresponding to requested SensorEntry |
| 114 | * |
| 115 | * @param[in] entry - TerminusID and SensorID |
| 116 | * |
| 117 | * @return SensorInfo corresponding to the input paramter SensorEntry |
| 118 | * throw std::out_of_range exception if not found |
| 119 | */ |
| 120 | const pdr::SensorInfo& lookupSensorInfo(const SensorEntry& entry) const |
| 121 | { |
| 122 | return sensorMap.at(entry); |
| 123 | } |
| 124 | |
Pavithra Barithaya | 3aec997 | 2020-12-14 01:55:44 -0600 | [diff] [blame] | 125 | /** @brief Handles state sensor event |
| 126 | * |
| 127 | * @param[in] entry - state sensor entry |
| 128 | * @param[in] state - event state |
| 129 | * |
| 130 | * @return PLDM completion code |
| 131 | */ |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 132 | int handleStateSensorEvent( |
| 133 | const pldm::responder::events::StateSensorEntry& entry, |
| 134 | pdr::EventState state); |
Pavithra Barithaya | 3aec997 | 2020-12-14 01:55:44 -0600 | [diff] [blame] | 135 | |
Sampa Misra | 868c879 | 2020-05-26 03:12:13 -0500 | [diff] [blame] | 136 | /** @brief Parse state sensor PDRs and populate the sensorMap lookup data |
| 137 | * structure |
| 138 | * |
| 139 | * @param[in] stateSensorPDRs - host state sensor PDRs |
Sampa Misra | 868c879 | 2020-05-26 03:12:13 -0500 | [diff] [blame] | 140 | * |
| 141 | */ |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 142 | void parseStateSensorPDRs(const PDRList& stateSensorPDRs); |
Sampa Misra | 868c879 | 2020-05-26 03:12:13 -0500 | [diff] [blame] | 143 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 144 | /** @brief this function sends a GetPDR request to Host firmware. |
| 145 | * And processes the PDRs based on type |
| 146 | * |
| 147 | * @param[in] - nextRecordHandle - the next record handle to ask for |
| 148 | */ |
| 149 | void getHostPDR(uint32_t nextRecordHandle = 0); |
| 150 | |
Sampa Misra | f9ba8c1 | 2021-08-06 00:33:47 -0500 | [diff] [blame] | 151 | /** @brief set the Host firmware condition when pldmd starts |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 152 | */ |
Sampa Misra | f9ba8c1 | 2021-08-06 00:33:47 -0500 | [diff] [blame] | 153 | void setHostFirmwareCondition(); |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 154 | |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 155 | /** @brief set HostSensorStates when pldmd starts or restarts |
| 156 | * and updates the D-Bus property |
| 157 | * @param[in] stateSensorPDRs - host state sensor PDRs |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 158 | */ |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 159 | void setHostSensorState(const PDRList& stateSensorPDRs); |
Pavithra Barithaya | 4f2538a | 2021-03-05 07:32:15 -0600 | [diff] [blame] | 160 | |
Pavithra Barithaya | ae5c97e | 2022-08-29 02:57:59 -0500 | [diff] [blame] | 161 | /** @brief whether we received PLDM_RECORDS_MODIFIED event data operation |
| 162 | * from host |
| 163 | */ |
| 164 | bool isHostPdrModified = false; |
| 165 | |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 166 | /** @brief check whether Host is running when pldmd starts |
| 167 | */ |
| 168 | bool isHostUp(); |
| 169 | |
Manojkiran Eda | 60e1fe9 | 2021-10-08 15:58:16 +0530 | [diff] [blame] | 170 | /** @brief map that captures various terminus information **/ |
| 171 | TLPDRMap tlPDRInfo; |
| 172 | |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 173 | private: |
| 174 | /** @brief deferred function to fetch PDR from Host, scheduled to work on |
| 175 | * the event loop. The PDR exchg with the host is async. |
| 176 | * @param[in] source - sdeventplus event source |
| 177 | */ |
| 178 | void _fetchPDR(sdeventplus::source::EventBase& source); |
| 179 | |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 180 | /** @brief Merge host firmware's entity association PDRs into BMC's |
| 181 | * @details A merge operation involves adding a pldm_entity under the |
| 182 | * appropriate parent, and updating container ids. |
| 183 | * @param[in] pdr - entity association pdr |
| 184 | */ |
| 185 | void mergeEntityAssociations(const std::vector<uint8_t>& pdr); |
| 186 | |
| 187 | /** @brief Find parent of input entity type, from the entity association |
| 188 | * tree |
| 189 | * @param[in] type - PLDM entity type |
| 190 | * @param[out] parent - PLDM entity information of parent |
| 191 | * @return bool - true if parent found, false otherwise |
| 192 | */ |
| 193 | bool getParent(EntityType type, pldm_entity& parent); |
| 194 | |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 195 | /** @brief process the Host's PDR and add to BMC's PDR repo |
| 196 | * @param[in] eid - MCTP id of Host |
| 197 | * @param[in] response - response from Host for GetPDR |
| 198 | * @param[in] respMsgLen - response message length |
| 199 | */ |
| 200 | void processHostPDRs(mctp_eid_t eid, const pldm_msg* response, |
| 201 | size_t respMsgLen); |
| 202 | |
| 203 | /** @brief send PDR Repo change after merging Host's PDR to BMC PDR repo |
| 204 | * @param[in] source - sdeventplus event source |
| 205 | */ |
| 206 | void _processPDRRepoChgEvent(sdeventplus::source::EventBase& source); |
| 207 | |
| 208 | /** @brief fetch the next PDR based on the record handle sent by Host |
| 209 | * @param[in] nextRecordHandle - next record handle |
| 210 | * @param[in] source - sdeventplus event source |
| 211 | */ |
| 212 | void _processFetchPDREvent(uint32_t nextRecordHandle, |
| 213 | sdeventplus::source::EventBase& source); |
| 214 | |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 215 | /** @brief fd of MCTP communications socket */ |
| 216 | int mctp_fd; |
| 217 | /** @brief MCTP EID of host firmware */ |
| 218 | uint8_t mctp_eid; |
| 219 | /** @brief reference of main event loop of pldmd, primarily used to schedule |
| 220 | * work. |
| 221 | */ |
| 222 | sdeventplus::Event& event; |
| 223 | /** @brief pointer to BMC's primary PDR repo, host PDRs are added here */ |
| 224 | pldm_pdr* repo; |
Pavithra Barithaya | 3aec997 | 2020-12-14 01:55:44 -0600 | [diff] [blame] | 225 | |
Brad Bishop | 5079ac4 | 2021-08-19 18:35:06 -0400 | [diff] [blame] | 226 | pldm::responder::events::StateSensorHandler stateSensorHandler; |
Sampa Misra | c073a20 | 2021-05-08 10:56:05 -0500 | [diff] [blame] | 227 | /** @brief Pointer to BMC's and Host's entity association tree */ |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 228 | pldm_entity_association_tree* entityTree; |
Sampa Misra | c073a20 | 2021-05-08 10:56:05 -0500 | [diff] [blame] | 229 | |
| 230 | /** @brief Pointer to BMC's entity association tree */ |
| 231 | pldm_entity_association_tree* bmcEntityTree; |
| 232 | |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 233 | /** @brief reference to Instance ID database object, used to obtain PLDM |
| 234 | * instance IDs |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 235 | */ |
Andrew Jeffery | a330b2f | 2023-05-04 14:55:37 +0930 | [diff] [blame] | 236 | pldm::InstanceIdDb& instanceIdDb; |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 237 | |
| 238 | /** @brief PLDM request handler */ |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 239 | pldm::requester::Handler<pldm::requester::Request>* handler; |
Tom Joseph | 74f27c7 | 2021-05-16 07:58:53 -0700 | [diff] [blame] | 240 | |
Deepak Kodihalli | 8cb6f66 | 2020-04-10 02:55:43 -0500 | [diff] [blame] | 241 | /** @brief sdeventplus event source */ |
| 242 | std::unique_ptr<sdeventplus::source::Defer> pdrFetchEvent; |
Sampa Misra | c0c7948 | 2021-06-02 08:01:54 -0500 | [diff] [blame] | 243 | std::unique_ptr<sdeventplus::source::Defer> deferredFetchPDREvent; |
| 244 | std::unique_ptr<sdeventplus::source::Defer> deferredPDRRepoChgEvent; |
| 245 | |
Deepak Kodihalli | 8cb6f66 | 2020-04-10 02:55:43 -0500 | [diff] [blame] | 246 | /** @brief list of PDR record handles pointing to host's PDRs */ |
| 247 | PDRRecordHandles pdrRecordHandles; |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 248 | /** @brief maps an entity type to parent pldm_entity from the BMC's entity |
| 249 | * association tree |
| 250 | */ |
Pavithra Barithaya | ae5c97e | 2022-08-29 02:57:59 -0500 | [diff] [blame] | 251 | |
| 252 | /** @brief list of PDR record handles modified pointing to host PDRs */ |
| 253 | PDRRecordHandles modifiedPDRRecordHandles; |
| 254 | |
Deepak Kodihalli | 87514cc | 2020-04-16 09:08:38 -0500 | [diff] [blame] | 255 | std::map<EntityType, pldm_entity> parents; |
Deepak Kodihalli | 6b1d1ca | 2020-04-27 07:24:51 -0500 | [diff] [blame] | 256 | /** @brief D-Bus property changed signal match */ |
Patrick Williams | 84b790c | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 257 | std::unique_ptr<sdbusplus::bus::match_t> hostOffMatch; |
Tom Joseph | b426860 | 2020-04-17 17:20:45 +0530 | [diff] [blame] | 258 | |
| 259 | /** @brief sensorMap is a lookup data structure that is build from the |
| 260 | * hostPDR that speeds up the lookup of <TerminusID, SensorID> in |
| 261 | * PlatformEventMessage command request. |
| 262 | */ |
| 263 | HostStateSensorMap sensorMap; |
sampmisr | 6decfc1 | 2021-03-02 11:07:36 +0530 | [diff] [blame] | 264 | |
| 265 | /** @brief whether response received from Host */ |
| 266 | bool responseReceived; |
Pavithra Barithaya | 51efaf8 | 2020-04-02 02:42:27 -0500 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | } // namespace pldm |