blob: 5dc5e9ea3a6e717665395a318813024e58f426aa [file] [log] [blame]
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05001#pragma once
2
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05003#include "common/types.hpp"
4#include "common/utils.hpp"
Pavithra Barithaya3aec9972020-12-14 01:55:44 -06005#include "libpldmresponder/event_parser.hpp"
Tom Josephb4268602020-04-17 17:20:45 +05306#include "libpldmresponder/pdr_utils.hpp"
Andrew Jefferya330b2f2023-05-04 14:55:37 +09307#include "pldmd/instance_id.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -07008#include "requester/handler.hpp"
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05009
George Liuc453e162022-12-21 17:16:23 +080010#include <libpldm/base.h>
11#include <libpldm/platform.h>
12
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050013#include <sdeventplus/event.hpp>
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050014#include <sdeventplus/source/event.hpp>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050015
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -050016#include <deque>
George Liudf9a6d32020-12-22 16:27:16 +080017#include <filesystem>
George Liu6492f522020-06-16 10:34:05 +080018#include <map>
19#include <memory>
20#include <vector>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050021
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050022namespace pldm
23{
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050024using EntityType = uint16_t;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050025// vector which would hold the PDR record handle data returned by
26// pldmPDRRepositoryChgEvent event data
27using ChangeEntry = uint32_t;
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -050028using PDRRecordHandles = std::deque<ChangeEntry>;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050029
Tom Josephb4268602020-04-17 17:20:45 +053030/** @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 */
36struct 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
53using HostStateSensorMap = std::map<SensorEntry, pdr::SensorInfo>;
Sampa Misra868c8792020-05-26 03:12:13 -050054using PDRList = std::vector<std::vector<uint8_t>>;
Tom Josephb4268602020-04-17 17:20:45 +053055
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050056/** @class HostPDRHandler
57 * @brief This class can fetch and process PDRs from host firmware
58 * @details Provides an API to fetch PDRs from the host firmware. Upon
59 * receiving the PDRs, they are stored into the BMC's primary PDR repo.
60 * Adjustments are made to entity association PDRs received from the host,
61 * because they need to be assimilated into the BMC's entity association
62 * tree. A PLDM event containing the record handles of the updated entity
63 * association PDRs is sent to the host.
64 */
65class HostPDRHandler
66{
67 public:
68 HostPDRHandler() = delete;
69 HostPDRHandler(const HostPDRHandler&) = delete;
70 HostPDRHandler(HostPDRHandler&&) = delete;
71 HostPDRHandler& operator=(const HostPDRHandler&) = delete;
72 HostPDRHandler& operator=(HostPDRHandler&&) = delete;
73 ~HostPDRHandler() = default;
74
Manojkiran Eda60e1fe92021-10-08 15:58:16 +053075 using TerminusInfo =
76 std::tuple<pdr::TerminusID, pdr::EID, pdr::TerminusValidity>;
77 using TLPDRMap = std::map<pdr::TerminusHandle, TerminusInfo>;
Sampa Misra868c8792020-05-26 03:12:13 -050078
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050079 /** @brief Constructor
80 * @param[in] mctp_fd - fd of MCTP communications socket
81 * @param[in] mctp_eid - MCTP EID of host firmware
82 * @param[in] event - reference of main event loop of pldmd
83 * @param[in] repo - pointer to BMC's primary PDR repo
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060084 * @param[in] eventsJsonDir - directory path which has the config JSONs
Tom Joseph74f27c72021-05-16 07:58:53 -070085 * @param[in] entityTree - Pointer to BMC and Host entity association tree
86 * @param[in] bmcEntityTree - pointer to BMC's entity association tree
Andrew Jefferya330b2f2023-05-04 14:55:37 +093087 * @param[in] instanceIdDb - reference to an InstanceIdDb object
Tom Joseph74f27c72021-05-16 07:58:53 -070088 * @param[in] handler - PLDM request handler
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050089 */
Tom Joseph74f27c72021-05-16 07:58:53 -070090 explicit HostPDRHandler(
91 int mctp_fd, uint8_t mctp_eid, sdeventplus::Event& event,
92 pldm_pdr* repo, const std::string& eventsJsonsDir,
93 pldm_entity_association_tree* entityTree,
Brad Bishop5079ac42021-08-19 18:35:06 -040094 pldm_entity_association_tree* bmcEntityTree,
Andrew Jefferya330b2f2023-05-04 14:55:37 +093095 pldm::InstanceIdDb& instanceIdDb,
Tom Josephe5268cd2021-09-07 13:04:03 +053096 pldm::requester::Handler<pldm::requester::Request>* handler);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050097
98 /** @brief fetch PDRs from host firmware. See @class.
99 * @param[in] recordHandles - list of record handles pointing to host's
100 * PDRs that need to be fetched.
101 */
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500102
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500103 void fetchPDR(PDRRecordHandles&& recordHandles);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500104
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500105 /** @brief Send a PLDM event to host firmware containing a list of record
106 * handles of PDRs that the host firmware has to fetch.
107 * @param[in] pdrTypes - list of PDR types that need to be looked up in the
108 * BMC repo
109 * @param[in] eventDataFormat - format for PDRRepositoryChgEvent in DSP0248
110 */
111 void sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
112 uint8_t eventDataFormat);
113
Tom Josephb4268602020-04-17 17:20:45 +0530114 /** @brief Lookup host sensor info corresponding to requested SensorEntry
115 *
116 * @param[in] entry - TerminusID and SensorID
117 *
118 * @return SensorInfo corresponding to the input paramter SensorEntry
119 * throw std::out_of_range exception if not found
120 */
121 const pdr::SensorInfo& lookupSensorInfo(const SensorEntry& entry) const
122 {
123 return sensorMap.at(entry);
124 }
125
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600126 /** @brief Handles state sensor event
127 *
128 * @param[in] entry - state sensor entry
129 * @param[in] state - event state
130 *
131 * @return PLDM completion code
132 */
Brad Bishop5079ac42021-08-19 18:35:06 -0400133 int handleStateSensorEvent(
134 const pldm::responder::events::StateSensorEntry& entry,
135 pdr::EventState state);
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600136
Sampa Misra868c8792020-05-26 03:12:13 -0500137 /** @brief Parse state sensor PDRs and populate the sensorMap lookup data
138 * structure
139 *
140 * @param[in] stateSensorPDRs - host state sensor PDRs
Sampa Misra868c8792020-05-26 03:12:13 -0500141 *
142 */
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530143 void parseStateSensorPDRs(const PDRList& stateSensorPDRs);
Sampa Misra868c8792020-05-26 03:12:13 -0500144
Sampa Misrac0c79482021-06-02 08:01:54 -0500145 /** @brief this function sends a GetPDR request to Host firmware.
146 * And processes the PDRs based on type
147 *
148 * @param[in] - nextRecordHandle - the next record handle to ask for
149 */
150 void getHostPDR(uint32_t nextRecordHandle = 0);
151
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500152 /** @brief set the Host firmware condition when pldmd starts
sampmisr6decfc12021-03-02 11:07:36 +0530153 */
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500154 void setHostFirmwareCondition();
sampmisr6decfc12021-03-02 11:07:36 +0530155
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600156 /** @brief set HostSensorStates when pldmd starts or restarts
157 * and updates the D-Bus property
158 * @param[in] stateSensorPDRs - host state sensor PDRs
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600159 */
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530160 void setHostSensorState(const PDRList& stateSensorPDRs);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600161
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500162 /** @brief whether we received PLDM_RECORDS_MODIFIED event data operation
163 * from host
164 */
165 bool isHostPdrModified = false;
166
sampmisr6decfc12021-03-02 11:07:36 +0530167 /** @brief check whether Host is running when pldmd starts
168 */
169 bool isHostUp();
170
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530171 /** @brief map that captures various terminus information **/
172 TLPDRMap tlPDRInfo;
173
sampmisr6decfc12021-03-02 11:07:36 +0530174 private:
175 /** @brief deferred function to fetch PDR from Host, scheduled to work on
176 * the event loop. The PDR exchg with the host is async.
177 * @param[in] source - sdeventplus event source
178 */
179 void _fetchPDR(sdeventplus::source::EventBase& source);
180
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500181 /** @brief Merge host firmware's entity association PDRs into BMC's
182 * @details A merge operation involves adding a pldm_entity under the
183 * appropriate parent, and updating container ids.
184 * @param[in] pdr - entity association pdr
185 */
186 void mergeEntityAssociations(const std::vector<uint8_t>& pdr);
187
Sampa Misrac0c79482021-06-02 08:01:54 -0500188 /** @brief process the Host's PDR and add to BMC's PDR repo
189 * @param[in] eid - MCTP id of Host
190 * @param[in] response - response from Host for GetPDR
191 * @param[in] respMsgLen - response message length
192 */
193 void processHostPDRs(mctp_eid_t eid, const pldm_msg* response,
194 size_t respMsgLen);
195
196 /** @brief send PDR Repo change after merging Host's PDR to BMC PDR repo
197 * @param[in] source - sdeventplus event source
198 */
199 void _processPDRRepoChgEvent(sdeventplus::source::EventBase& source);
200
201 /** @brief fetch the next PDR based on the record handle sent by Host
202 * @param[in] nextRecordHandle - next record handle
203 * @param[in] source - sdeventplus event source
204 */
205 void _processFetchPDREvent(uint32_t nextRecordHandle,
206 sdeventplus::source::EventBase& source);
207
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500208 /** @brief fd of MCTP communications socket */
209 int mctp_fd;
210 /** @brief MCTP EID of host firmware */
211 uint8_t mctp_eid;
212 /** @brief reference of main event loop of pldmd, primarily used to schedule
213 * work.
214 */
215 sdeventplus::Event& event;
216 /** @brief pointer to BMC's primary PDR repo, host PDRs are added here */
217 pldm_pdr* repo;
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600218
Brad Bishop5079ac42021-08-19 18:35:06 -0400219 pldm::responder::events::StateSensorHandler stateSensorHandler;
Sampa Misrac073a202021-05-08 10:56:05 -0500220 /** @brief Pointer to BMC's and Host's entity association tree */
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500221 pldm_entity_association_tree* entityTree;
Sampa Misrac073a202021-05-08 10:56:05 -0500222
223 /** @brief Pointer to BMC's entity association tree */
224 pldm_entity_association_tree* bmcEntityTree;
225
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930226 /** @brief reference to Instance ID database object, used to obtain PLDM
227 * instance IDs
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500228 */
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930229 pldm::InstanceIdDb& instanceIdDb;
Tom Joseph74f27c72021-05-16 07:58:53 -0700230
231 /** @brief PLDM request handler */
Sampa Misrac0c79482021-06-02 08:01:54 -0500232 pldm::requester::Handler<pldm::requester::Request>* handler;
Tom Joseph74f27c72021-05-16 07:58:53 -0700233
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500234 /** @brief sdeventplus event source */
235 std::unique_ptr<sdeventplus::source::Defer> pdrFetchEvent;
Sampa Misrac0c79482021-06-02 08:01:54 -0500236 std::unique_ptr<sdeventplus::source::Defer> deferredFetchPDREvent;
237 std::unique_ptr<sdeventplus::source::Defer> deferredPDRRepoChgEvent;
238
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500239 /** @brief list of PDR record handles pointing to host's PDRs */
240 PDRRecordHandles pdrRecordHandles;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500241 /** @brief maps an entity type to parent pldm_entity from the BMC's entity
242 * association tree
243 */
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500244
245 /** @brief list of PDR record handles modified pointing to host PDRs */
246 PDRRecordHandles modifiedPDRRecordHandles;
247
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500248 std::map<EntityType, pldm_entity> parents;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500249 /** @brief D-Bus property changed signal match */
Patrick Williams84b790c2022-07-22 19:26:56 -0500250 std::unique_ptr<sdbusplus::bus::match_t> hostOffMatch;
Tom Josephb4268602020-04-17 17:20:45 +0530251
252 /** @brief sensorMap is a lookup data structure that is build from the
253 * hostPDR that speeds up the lookup of <TerminusID, SensorID> in
254 * PlatformEventMessage command request.
255 */
256 HostStateSensorMap sensorMap;
sampmisr6decfc12021-03-02 11:07:36 +0530257
258 /** @brief whether response received from Host */
259 bool responseReceived;
George Liuacf2c8c2021-05-10 14:08:52 +0800260
261 /** @brief variable that captures if the first entity association PDR
262 * from host is merged into the BMC tree
263 */
264 bool mergedHostParents;
265
266 /** @brief whether timed out waiting for a response from Host */
267 bool timeOut;
268
269 /** @brief request message instance id */
270 uint8_t insId;
George Liudf9a6d32020-12-22 16:27:16 +0800271
272 /** @brief maps an object path to pldm_entity from the BMC's entity
273 * association tree
274 */
275 utils::ObjectPathMaps objPathMap;
276
277 /** @brief maps an entity name to map, maps to entity name to pldm_entity
278 */
279 utils::EntityAssociations entityAssociations;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500280};
281
282} // namespace pldm