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