blob: 8c97693ea07d6a70291043465465f308fd638af6 [file] [log] [blame]
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05001#pragma once
2
Andrew Jeffery2abbce72023-10-18 10:17:35 +10303#include "common/instance_id.hpp"
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05004#include "common/types.hpp"
5#include "common/utils.hpp"
Pavithra Barithaya3aec9972020-12-14 01:55:44 -06006#include "libpldmresponder/event_parser.hpp"
Sagar Srinivas3687e2b2023-04-10 05:08:28 -05007#include "libpldmresponder/oem_handler.hpp"
Tom Josephb4268602020-04-17 17:20:45 +05308#include "libpldmresponder/pdr_utils.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -07009#include "requester/handler.hpp"
Kamalkumar Patel4e69d252024-05-10 08:48:03 -050010#include "utils.hpp"
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050011
George Liuc453e162022-12-21 17:16:23 +080012#include <libpldm/base.h>
13#include <libpldm/platform.h>
14
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050015#include <sdeventplus/event.hpp>
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050016#include <sdeventplus/source/event.hpp>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050017
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -050018#include <deque>
George Liudf9a6d32020-12-22 16:27:16 +080019#include <filesystem>
George Liu6492f522020-06-16 10:34:05 +080020#include <map>
21#include <memory>
22#include <vector>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050023
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050024namespace pldm
25{
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050026// vector which would hold the PDR record handle data returned by
27// pldmPDRRepositoryChgEvent event data
28using ChangeEntry = uint32_t;
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -050029using PDRRecordHandles = std::deque<ChangeEntry>;
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050030
Tom Josephb4268602020-04-17 17:20:45 +053031/** @struct SensorEntry
32 *
33 * SensorEntry is a unique key which maps a sensorEventType request in the
34 * PlatformEventMessage command to a host sensor PDR. This struct is a key
35 * in a std::map, so implemented operator==and operator<.
36 */
37struct SensorEntry
38{
39 pdr::TerminusID terminusID;
40 pdr::SensorID sensorID;
41
42 bool operator==(const SensorEntry& e) const
43 {
44 return ((terminusID == e.terminusID) && (sensorID == e.sensorID));
45 }
46
47 bool operator<(const SensorEntry& e) const
48 {
49 return ((terminusID < e.terminusID) ||
50 ((terminusID == e.terminusID) && (sensorID < e.sensorID)));
51 }
52};
53
54using HostStateSensorMap = std::map<SensorEntry, pdr::SensorInfo>;
Sampa Misra868c8792020-05-26 03:12:13 -050055using PDRList = std::vector<std::vector<uint8_t>>;
Tom Josephb4268602020-04-17 17:20:45 +053056
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050057/** @class HostPDRHandler
58 * @brief This class can fetch and process PDRs from host firmware
59 * @details Provides an API to fetch PDRs from the host firmware. Upon
60 * receiving the PDRs, they are stored into the BMC's primary PDR repo.
61 * Adjustments are made to entity association PDRs received from the host,
62 * because they need to be assimilated into the BMC's entity association
63 * tree. A PLDM event containing the record handles of the updated entity
64 * association PDRs is sent to the host.
65 */
66class HostPDRHandler
67{
68 public:
69 HostPDRHandler() = delete;
70 HostPDRHandler(const HostPDRHandler&) = delete;
71 HostPDRHandler(HostPDRHandler&&) = delete;
72 HostPDRHandler& operator=(const HostPDRHandler&) = delete;
73 HostPDRHandler& operator=(HostPDRHandler&&) = delete;
74 ~HostPDRHandler() = default;
75
Manojkiran Eda60e1fe92021-10-08 15:58:16 +053076 using TerminusInfo =
77 std::tuple<pdr::TerminusID, pdr::EID, pdr::TerminusValidity>;
78 using TLPDRMap = std::map<pdr::TerminusHandle, TerminusInfo>;
Sampa Misra868c8792020-05-26 03:12:13 -050079
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050080 /** @brief Constructor
81 * @param[in] mctp_fd - fd of MCTP communications socket
82 * @param[in] mctp_eid - MCTP EID of host firmware
83 * @param[in] event - reference of main event loop of pldmd
84 * @param[in] repo - pointer to BMC's primary PDR repo
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060085 * @param[in] eventsJsonDir - directory path which has the config JSONs
Tom Joseph74f27c72021-05-16 07:58:53 -070086 * @param[in] entityTree - Pointer to BMC and Host entity association tree
87 * @param[in] bmcEntityTree - pointer to BMC's entity association tree
Andrew Jefferya330b2f2023-05-04 14:55:37 +093088 * @param[in] instanceIdDb - reference to an InstanceIdDb object
Tom Joseph74f27c72021-05-16 07:58:53 -070089 * @param[in] handler - PLDM request handler
Kamalkumar Pateleb43d6c2024-05-01 06:11:31 -050090 * @param[in] oemUtilsHandler - pointer oem utils handler
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050091 */
Tom Joseph74f27c72021-05-16 07:58:53 -070092 explicit HostPDRHandler(
93 int mctp_fd, uint8_t mctp_eid, sdeventplus::Event& event,
94 pldm_pdr* repo, const std::string& eventsJsonsDir,
95 pldm_entity_association_tree* entityTree,
Brad Bishop5079ac42021-08-19 18:35:06 -040096 pldm_entity_association_tree* bmcEntityTree,
Andrew Jefferya330b2f2023-05-04 14:55:37 +093097 pldm::InstanceIdDb& instanceIdDb,
George Liua881c172021-06-21 18:28:11 +080098 pldm::requester::Handler<pldm::requester::Request>* handler);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050099
100 /** @brief fetch PDRs from host firmware. See @class.
101 * @param[in] recordHandles - list of record handles pointing to host's
102 * PDRs that need to be fetched.
103 */
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500104
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500105 void fetchPDR(PDRRecordHandles&& recordHandles);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500106
Pavithra Barithaya273bee22025-08-22 15:38:21 +0530107 /** @brief delete PDRs from remote pldm endpoint.
108 * @param[in] recordHandles - list of record handles pointing to remote
109 * PDRs that need to be deleted.
110 */
111 void deletePDRFromRepo(PDRRecordHandles&& recordHandles);
112
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500113 /** @brief Send a PLDM event to host firmware containing a list of record
114 * handles of PDRs that the host firmware has to fetch.
115 * @param[in] pdrTypes - list of PDR types that need to be looked up in the
116 * BMC repo
117 * @param[in] eventDataFormat - format for PDRRepositoryChgEvent in DSP0248
118 */
119 void sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
120 uint8_t eventDataFormat);
121
Tom Josephb4268602020-04-17 17:20:45 +0530122 /** @brief Lookup host sensor info corresponding to requested SensorEntry
123 *
124 * @param[in] entry - TerminusID and SensorID
125 *
Manojkiran Eda2576aec2024-06-17 12:05:17 +0530126 * @return SensorInfo corresponding to the input parameter SensorEntry
Tom Josephb4268602020-04-17 17:20:45 +0530127 * throw std::out_of_range exception if not found
128 */
129 const pdr::SensorInfo& lookupSensorInfo(const SensorEntry& entry) const
130 {
131 return sensorMap.at(entry);
132 }
133
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600134 /** @brief Handles state sensor event
135 *
136 * @param[in] entry - state sensor entry
137 * @param[in] state - event state
138 *
139 * @return PLDM completion code
140 */
Brad Bishop5079ac42021-08-19 18:35:06 -0400141 int handleStateSensorEvent(
142 const pldm::responder::events::StateSensorEntry& entry,
143 pdr::EventState state);
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600144
Sampa Misra868c8792020-05-26 03:12:13 -0500145 /** @brief Parse state sensor PDRs and populate the sensorMap lookup data
146 * structure
147 *
148 * @param[in] stateSensorPDRs - host state sensor PDRs
Sampa Misra868c8792020-05-26 03:12:13 -0500149 *
150 */
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530151 void parseStateSensorPDRs(const PDRList& stateSensorPDRs);
Sampa Misra868c8792020-05-26 03:12:13 -0500152
Sampa Misrac0c79482021-06-02 08:01:54 -0500153 /** @brief this function sends a GetPDR request to Host firmware.
154 * And processes the PDRs based on type
155 *
156 * @param[in] - nextRecordHandle - the next record handle to ask for
157 */
158 void getHostPDR(uint32_t nextRecordHandle = 0);
159
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500160 /** @brief set the Host firmware condition when pldmd starts
sampmisr6decfc12021-03-02 11:07:36 +0530161 */
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500162 void setHostFirmwareCondition();
sampmisr6decfc12021-03-02 11:07:36 +0530163
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600164 /** @brief set HostSensorStates when pldmd starts or restarts
165 * and updates the D-Bus property
166 * @param[in] stateSensorPDRs - host state sensor PDRs
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600167 */
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530168 void setHostSensorState(const PDRList& stateSensorPDRs);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600169
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500170 /** @brief whether we received PLDM_RECORDS_MODIFIED event data operation
171 * from host
172 */
173 bool isHostPdrModified = false;
174
sampmisr6decfc12021-03-02 11:07:36 +0530175 /** @brief check whether Host is running when pldmd starts
176 */
177 bool isHostUp();
178
George Liua881c172021-06-21 18:28:11 +0800179 /* @brief Method to set the oem platform handler in host pdr handler class
180 *
181 * @param[in] handler - oem platform handler
182 */
Patrick Williams366507c2025-02-03 14:28:01 -0500183 inline void setOemPlatformHandler(
184 pldm::responder::oem_platform::Handler* handler)
George Liua881c172021-06-21 18:28:11 +0800185 {
186 oemPlatformHandler = handler;
187 }
188
Kamalkumar Pateleb43d6c2024-05-01 06:11:31 -0500189 /* @brief Method to set the oem utils handler in host pdr handler class
190 *
191 * @param[in] handler - oem utils handler
192 */
193 inline void setOemUtilsHandler(pldm::responder::oem_utils::Handler* handler)
194 {
195 oemUtilsHandler = handler;
196 }
197
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530198 /** @brief map that captures various terminus information **/
199 TLPDRMap tlPDRInfo;
200
sampmisr6decfc12021-03-02 11:07:36 +0530201 private:
202 /** @brief deferred function to fetch PDR from Host, scheduled to work on
203 * the event loop. The PDR exchg with the host is async.
204 * @param[in] source - sdeventplus event source
205 */
206 void _fetchPDR(sdeventplus::source::EventBase& source);
207
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500208 /** @brief Merge host firmware's entity association PDRs into BMC's
209 * @details A merge operation involves adding a pldm_entity under the
210 * appropriate parent, and updating container ids.
211 * @param[in] pdr - entity association pdr
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500212 * @param[in] size - size of input PDR record in bytes
213 * @param[in] record_handle - record handle of the PDR
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500214 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400215 void mergeEntityAssociations(
216 const std::vector<uint8_t>& pdr, [[maybe_unused]] const uint32_t& size,
217 [[maybe_unused]] const uint32_t& record_handle);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500218
Sampa Misrac0c79482021-06-02 08:01:54 -0500219 /** @brief process the Host's PDR and add to BMC's PDR repo
220 * @param[in] eid - MCTP id of Host
221 * @param[in] response - response from Host for GetPDR
222 * @param[in] respMsgLen - response message length
223 */
224 void processHostPDRs(mctp_eid_t eid, const pldm_msg* response,
225 size_t respMsgLen);
226
227 /** @brief send PDR Repo change after merging Host's PDR to BMC PDR repo
228 * @param[in] source - sdeventplus event source
229 */
230 void _processPDRRepoChgEvent(sdeventplus::source::EventBase& source);
231
232 /** @brief fetch the next PDR based on the record handle sent by Host
233 * @param[in] nextRecordHandle - next record handle
234 * @param[in] source - sdeventplus event source
235 */
236 void _processFetchPDREvent(uint32_t nextRecordHandle,
237 sdeventplus::source::EventBase& source);
238
George Liu682ee182020-12-25 15:24:33 +0800239 /** @brief Get FRU record table metadata by remote PLDM terminus
240 *
241 * @param[out] uint16_t - total table records
242 */
243 void getFRURecordTableMetadataByRemote(const PDRList& fruRecordSetPDRs);
244
245 /** @brief Set Location Code in the dbus objects
246 *
247 * @param[in] fruRecordSetPDRs - the Fru Record set PDR's
248 * @param[in] fruRecordData - the Fru Record Data
249 */
250
251 void setFRUDataOnDBus(
252 const PDRList& fruRecordSetPDRs,
253 const std::vector<responder::pdr_utils::FruRecordDataFormat>&
254 fruRecordData);
255
256 /** @brief Get FRU record table by remote PLDM terminus
257 *
258 * @param[in] fruRecordSetPDRs - the Fru Record set PDR's
259 * @param[in] totalTableRecords - the Number of total table records
260 * @return
261 */
262 void getFRURecordTableByRemote(const PDRList& fruRecordSetPDRs,
263 uint16_t totalTableRecords);
264
265 /** @brief Create Dbus objects by remote PLDM entity Fru PDRs
266 *
267 * @param[in] fruRecordSetPDRs - fru record set pdr
268 *
269 * @ return
270 */
271 void createDbusObjects(const PDRList& fruRecordSetPDRs);
272
Archana Kakanif9355372025-02-04 03:13:24 -0600273 /** @brief set the FRU presence based on the remote PLDM terminus off signal
274 */
275 void setPresenceFrus();
276
Archana Kakanic3664472025-02-04 05:36:37 -0600277 /** @brief Set the Present dbus Property
278 * @param[in] path - object path
279 * @return
280 */
281 void setPresentPropertyStatus(const std::string& path);
282
Archana Kakanif9355372025-02-04 03:13:24 -0600283 /** @brief Set the availabilty dbus Property
284 * @param[in] path - object path
285 */
286 void setAvailabilityState(const std::string& path);
287
George Liu682ee182020-12-25 15:24:33 +0800288 /** @brief Get FRU Record Set Identifier from FRU Record data Format
289 * @param[in] fruRecordSetPDRs - fru record set pdr
290 * @param[in] entity - PLDM entity information
291 * @return
292 */
293 std::optional<uint16_t> getRSI(const PDRList& fruRecordSetPDRs,
294 const pldm_entity& entity);
295
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500296 /** @brief MCTP EID of host firmware */
297 uint8_t mctp_eid;
298 /** @brief reference of main event loop of pldmd, primarily used to schedule
299 * work.
300 */
301 sdeventplus::Event& event;
302 /** @brief pointer to BMC's primary PDR repo, host PDRs are added here */
303 pldm_pdr* repo;
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600304
Brad Bishop5079ac42021-08-19 18:35:06 -0400305 pldm::responder::events::StateSensorHandler stateSensorHandler;
Sampa Misrac073a202021-05-08 10:56:05 -0500306 /** @brief Pointer to BMC's and Host's entity association tree */
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500307 pldm_entity_association_tree* entityTree;
Sampa Misrac073a202021-05-08 10:56:05 -0500308
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930309 /** @brief reference to Instance ID database object, used to obtain PLDM
310 * instance IDs
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500311 */
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930312 pldm::InstanceIdDb& instanceIdDb;
Tom Joseph74f27c72021-05-16 07:58:53 -0700313
314 /** @brief PLDM request handler */
Sampa Misrac0c79482021-06-02 08:01:54 -0500315 pldm::requester::Handler<pldm::requester::Request>* handler;
Tom Joseph74f27c72021-05-16 07:58:53 -0700316
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500317 /** @brief sdeventplus event source */
318 std::unique_ptr<sdeventplus::source::Defer> pdrFetchEvent;
Sampa Misrac0c79482021-06-02 08:01:54 -0500319 std::unique_ptr<sdeventplus::source::Defer> deferredFetchPDREvent;
320 std::unique_ptr<sdeventplus::source::Defer> deferredPDRRepoChgEvent;
321
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500322 /** @brief list of PDR record handles pointing to host's PDRs */
323 PDRRecordHandles pdrRecordHandles;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500324 /** @brief maps an entity type to parent pldm_entity from the BMC's entity
325 * association tree
326 */
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500327
328 /** @brief list of PDR record handles modified pointing to host PDRs */
329 PDRRecordHandles modifiedPDRRecordHandles;
330
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500331 /** @brief D-Bus property changed signal match */
Patrick Williams84b790c2022-07-22 19:26:56 -0500332 std::unique_ptr<sdbusplus::bus::match_t> hostOffMatch;
Tom Josephb4268602020-04-17 17:20:45 +0530333
334 /** @brief sensorMap is a lookup data structure that is build from the
335 * hostPDR that speeds up the lookup of <TerminusID, SensorID> in
336 * PlatformEventMessage command request.
337 */
338 HostStateSensorMap sensorMap;
sampmisr6decfc12021-03-02 11:07:36 +0530339
340 /** @brief whether response received from Host */
341 bool responseReceived;
George Liuacf2c8c2021-05-10 14:08:52 +0800342
343 /** @brief variable that captures if the first entity association PDR
344 * from host is merged into the BMC tree
345 */
346 bool mergedHostParents;
347
George Liudf9a6d32020-12-22 16:27:16 +0800348 /** @brief maps an object path to pldm_entity from the BMC's entity
349 * association tree
350 */
Kamalkumar Patel7d427f12024-05-16 03:44:00 -0500351 pldm::utils::ObjectPathMaps objPathMap;
George Liudf9a6d32020-12-22 16:27:16 +0800352
353 /** @brief maps an entity name to map, maps to entity name to pldm_entity
354 */
Kamalkumar Patel7d427f12024-05-16 03:44:00 -0500355 pldm::utils::EntityAssociations entityAssociations;
George Liu682ee182020-12-25 15:24:33 +0800356
357 /** @brief the vector of FRU Record Data Format
358 */
359 std::vector<responder::pdr_utils::FruRecordDataFormat> fruRecordData;
360
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500361 /** @OEM platform handler */
George Liua881c172021-06-21 18:28:11 +0800362 pldm::responder::oem_platform::Handler* oemPlatformHandler = nullptr;
Kamalkumar Patel516122e2024-05-07 04:39:32 -0500363
364 /** @brief entityID and entity name is only loaded once
365 */
Kamalkumar Patel7d427f12024-05-16 03:44:00 -0500366 pldm::utils::EntityMaps entityMaps;
Kamalkumar Pateleb43d6c2024-05-01 06:11:31 -0500367
368 /** @OEM Utils handler */
369 pldm::responder::oem_utils::Handler* oemUtilsHandler;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500370};
371
372} // namespace pldm