blob: 8a4e21a1db08c2db58aa2f2b0f2a9b18ce9aceaa [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"
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050010
George Liuc453e162022-12-21 17:16:23 +080011#include <libpldm/base.h>
12#include <libpldm/platform.h>
13
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050014#include <sdeventplus/event.hpp>
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050015#include <sdeventplus/source/event.hpp>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050016
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -050017#include <deque>
George Liudf9a6d32020-12-22 16:27:16 +080018#include <filesystem>
George Liu6492f522020-06-16 10:34:05 +080019#include <map>
20#include <memory>
21#include <vector>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050022
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050023namespace pldm
24{
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050025using EntityType = uint16_t;
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
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050090 */
Tom Joseph74f27c72021-05-16 07:58:53 -070091 explicit HostPDRHandler(
92 int mctp_fd, uint8_t mctp_eid, sdeventplus::Event& event,
93 pldm_pdr* repo, const std::string& eventsJsonsDir,
94 pldm_entity_association_tree* entityTree,
Brad Bishop5079ac42021-08-19 18:35:06 -040095 pldm_entity_association_tree* bmcEntityTree,
Andrew Jefferya330b2f2023-05-04 14:55:37 +093096 pldm::InstanceIdDb& instanceIdDb,
Sagar Srinivas3687e2b2023-04-10 05:08:28 -050097 pldm::requester::Handler<pldm::requester::Request>* handler,
98 pldm::responder::oem_platform::Handler* oemPlatformHandler);
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 Barithayae8beb892020-04-14 23:24:25 -0500107 /** @brief Send a PLDM event to host firmware containing a list of record
108 * handles of PDRs that the host firmware has to fetch.
109 * @param[in] pdrTypes - list of PDR types that need to be looked up in the
110 * BMC repo
111 * @param[in] eventDataFormat - format for PDRRepositoryChgEvent in DSP0248
112 */
113 void sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
114 uint8_t eventDataFormat);
115
Tom Josephb4268602020-04-17 17:20:45 +0530116 /** @brief Lookup host sensor info corresponding to requested SensorEntry
117 *
118 * @param[in] entry - TerminusID and SensorID
119 *
120 * @return SensorInfo corresponding to the input paramter SensorEntry
121 * throw std::out_of_range exception if not found
122 */
123 const pdr::SensorInfo& lookupSensorInfo(const SensorEntry& entry) const
124 {
125 return sensorMap.at(entry);
126 }
127
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600128 /** @brief Handles state sensor event
129 *
130 * @param[in] entry - state sensor entry
131 * @param[in] state - event state
132 *
133 * @return PLDM completion code
134 */
Brad Bishop5079ac42021-08-19 18:35:06 -0400135 int handleStateSensorEvent(
136 const pldm::responder::events::StateSensorEntry& entry,
137 pdr::EventState state);
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600138
Sampa Misra868c8792020-05-26 03:12:13 -0500139 /** @brief Parse state sensor PDRs and populate the sensorMap lookup data
140 * structure
141 *
142 * @param[in] stateSensorPDRs - host state sensor PDRs
Sampa Misra868c8792020-05-26 03:12:13 -0500143 *
144 */
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530145 void parseStateSensorPDRs(const PDRList& stateSensorPDRs);
Sampa Misra868c8792020-05-26 03:12:13 -0500146
Sampa Misrac0c79482021-06-02 08:01:54 -0500147 /** @brief this function sends a GetPDR request to Host firmware.
148 * And processes the PDRs based on type
149 *
150 * @param[in] - nextRecordHandle - the next record handle to ask for
151 */
152 void getHostPDR(uint32_t nextRecordHandle = 0);
153
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500154 /** @brief set the Host firmware condition when pldmd starts
sampmisr6decfc12021-03-02 11:07:36 +0530155 */
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500156 void setHostFirmwareCondition();
sampmisr6decfc12021-03-02 11:07:36 +0530157
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600158 /** @brief set HostSensorStates when pldmd starts or restarts
159 * and updates the D-Bus property
160 * @param[in] stateSensorPDRs - host state sensor PDRs
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600161 */
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530162 void setHostSensorState(const PDRList& stateSensorPDRs);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600163
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500164 /** @brief whether we received PLDM_RECORDS_MODIFIED event data operation
165 * from host
166 */
167 bool isHostPdrModified = false;
168
sampmisr6decfc12021-03-02 11:07:36 +0530169 /** @brief check whether Host is running when pldmd starts
170 */
171 bool isHostUp();
172
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530173 /** @brief map that captures various terminus information **/
174 TLPDRMap tlPDRInfo;
175
sampmisr6decfc12021-03-02 11:07:36 +0530176 private:
177 /** @brief deferred function to fetch PDR from Host, scheduled to work on
178 * the event loop. The PDR exchg with the host is async.
179 * @param[in] source - sdeventplus event source
180 */
181 void _fetchPDR(sdeventplus::source::EventBase& source);
182
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500183 /** @brief Merge host firmware's entity association PDRs into BMC's
184 * @details A merge operation involves adding a pldm_entity under the
185 * appropriate parent, and updating container ids.
186 * @param[in] pdr - entity association pdr
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500187 * @param[in] size - size of input PDR record in bytes
188 * @param[in] record_handle - record handle of the PDR
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500189 */
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500190 void
191 mergeEntityAssociations(const std::vector<uint8_t>& pdr,
192 [[maybe_unused]] const uint32_t& size,
193 [[maybe_unused]] const uint32_t& record_handle);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500194
Sampa Misrac0c79482021-06-02 08:01:54 -0500195 /** @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
George Liu682ee182020-12-25 15:24:33 +0800215 /** @brief Get FRU record table metadata by remote PLDM terminus
216 *
217 * @param[out] uint16_t - total table records
218 */
219 void getFRURecordTableMetadataByRemote(const PDRList& fruRecordSetPDRs);
220
221 /** @brief Set Location Code in the dbus objects
222 *
223 * @param[in] fruRecordSetPDRs - the Fru Record set PDR's
224 * @param[in] fruRecordData - the Fru Record Data
225 */
226
227 void setFRUDataOnDBus(
228 const PDRList& fruRecordSetPDRs,
229 const std::vector<responder::pdr_utils::FruRecordDataFormat>&
230 fruRecordData);
231
232 /** @brief Get FRU record table by remote PLDM terminus
233 *
234 * @param[in] fruRecordSetPDRs - the Fru Record set PDR's
235 * @param[in] totalTableRecords - the Number of total table records
236 * @return
237 */
238 void getFRURecordTableByRemote(const PDRList& fruRecordSetPDRs,
239 uint16_t totalTableRecords);
240
241 /** @brief Create Dbus objects by remote PLDM entity Fru PDRs
242 *
243 * @param[in] fruRecordSetPDRs - fru record set pdr
244 *
245 * @ return
246 */
247 void createDbusObjects(const PDRList& fruRecordSetPDRs);
248
249 /** @brief Get FRU Record Set Identifier from FRU Record data Format
250 * @param[in] fruRecordSetPDRs - fru record set pdr
251 * @param[in] entity - PLDM entity information
252 * @return
253 */
254 std::optional<uint16_t> getRSI(const PDRList& fruRecordSetPDRs,
255 const pldm_entity& entity);
256
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500257 /** @brief fd of MCTP communications socket */
258 int mctp_fd;
259 /** @brief MCTP EID of host firmware */
260 uint8_t mctp_eid;
261 /** @brief reference of main event loop of pldmd, primarily used to schedule
262 * work.
263 */
264 sdeventplus::Event& event;
265 /** @brief pointer to BMC's primary PDR repo, host PDRs are added here */
266 pldm_pdr* repo;
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600267
Brad Bishop5079ac42021-08-19 18:35:06 -0400268 pldm::responder::events::StateSensorHandler stateSensorHandler;
Sampa Misrac073a202021-05-08 10:56:05 -0500269 /** @brief Pointer to BMC's and Host's entity association tree */
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500270 pldm_entity_association_tree* entityTree;
Sampa Misrac073a202021-05-08 10:56:05 -0500271
272 /** @brief Pointer to BMC's entity association tree */
273 pldm_entity_association_tree* bmcEntityTree;
274
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930275 /** @brief reference to Instance ID database object, used to obtain PLDM
276 * instance IDs
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500277 */
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930278 pldm::InstanceIdDb& instanceIdDb;
Tom Joseph74f27c72021-05-16 07:58:53 -0700279
280 /** @brief PLDM request handler */
Sampa Misrac0c79482021-06-02 08:01:54 -0500281 pldm::requester::Handler<pldm::requester::Request>* handler;
Tom Joseph74f27c72021-05-16 07:58:53 -0700282
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500283 /** @brief sdeventplus event source */
284 std::unique_ptr<sdeventplus::source::Defer> pdrFetchEvent;
Sampa Misrac0c79482021-06-02 08:01:54 -0500285 std::unique_ptr<sdeventplus::source::Defer> deferredFetchPDREvent;
286 std::unique_ptr<sdeventplus::source::Defer> deferredPDRRepoChgEvent;
287
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500288 /** @brief list of PDR record handles pointing to host's PDRs */
289 PDRRecordHandles pdrRecordHandles;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500290 /** @brief maps an entity type to parent pldm_entity from the BMC's entity
291 * association tree
292 */
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500293
294 /** @brief list of PDR record handles modified pointing to host PDRs */
295 PDRRecordHandles modifiedPDRRecordHandles;
296
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500297 std::map<EntityType, pldm_entity> parents;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500298 /** @brief D-Bus property changed signal match */
Patrick Williams84b790c2022-07-22 19:26:56 -0500299 std::unique_ptr<sdbusplus::bus::match_t> hostOffMatch;
Tom Josephb4268602020-04-17 17:20:45 +0530300
301 /** @brief sensorMap is a lookup data structure that is build from the
302 * hostPDR that speeds up the lookup of <TerminusID, SensorID> in
303 * PlatformEventMessage command request.
304 */
305 HostStateSensorMap sensorMap;
sampmisr6decfc12021-03-02 11:07:36 +0530306
307 /** @brief whether response received from Host */
308 bool responseReceived;
George Liuacf2c8c2021-05-10 14:08:52 +0800309
310 /** @brief variable that captures if the first entity association PDR
311 * from host is merged into the BMC tree
312 */
313 bool mergedHostParents;
314
315 /** @brief whether timed out waiting for a response from Host */
316 bool timeOut;
317
318 /** @brief request message instance id */
319 uint8_t insId;
George Liudf9a6d32020-12-22 16:27:16 +0800320
321 /** @brief maps an object path to pldm_entity from the BMC's entity
322 * association tree
323 */
324 utils::ObjectPathMaps objPathMap;
325
326 /** @brief maps an entity name to map, maps to entity name to pldm_entity
327 */
328 utils::EntityAssociations entityAssociations;
George Liu682ee182020-12-25 15:24:33 +0800329
330 /** @brief the vector of FRU Record Data Format
331 */
332 std::vector<responder::pdr_utils::FruRecordDataFormat> fruRecordData;
333
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500334 /** @OEM platform handler */
335 pldm::responder::oem_platform::Handler* oemPlatformHandler;
336
George Liu682ee182020-12-25 15:24:33 +0800337 /** @brief Object path and entity association and is only loaded once
338 */
339 bool objPathEntityAssociation;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500340};
341
342} // namespace pldm