blob: 4629f5a27b1aa8abda299a11bfa4d73417c3314c [file] [log] [blame]
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05001#pragma once
2
George Liu6492f522020-06-16 10:34:05 +08003#include "libpldm/base.h"
4#include "libpldm/platform.h"
5
Deepak Kodihallid130e1a2020-06-17 05:55:32 -05006#include "common/types.hpp"
7#include "common/utils.hpp"
Pavithra Barithaya3aec9972020-12-14 01:55:44 -06008#include "libpldmresponder/event_parser.hpp"
Tom Josephb4268602020-04-17 17:20:45 +05309#include "libpldmresponder/pdr_utils.hpp"
Deepak Kodihalli1521f6d2020-06-16 08:51:02 -050010#include "pldmd/dbus_impl_requester.hpp"
Tom Joseph74f27c72021-05-16 07:58:53 -070011#include "requester/handler.hpp"
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050012
13#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 Liu6492f522020-06-16 10:34:05 +080017#include <map>
18#include <memory>
19#include <vector>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050020
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050021namespace pldm
22{
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
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -060053/* @struct TerminusLocatorInfo
54 * Contains validity, eid, terminus_id and terminus handle
55 * of a terminus locator PDR.
56 */
57struct TlInfo
58{
59 uint8_t valid;
60 uint8_t eid;
61 uint8_t tid;
62 uint16_t terminusHandle;
63};
64
Tom Josephb4268602020-04-17 17:20:45 +053065using HostStateSensorMap = std::map<SensorEntry, pdr::SensorInfo>;
Sampa Misra868c8792020-05-26 03:12:13 -050066using PDRList = std::vector<std::vector<uint8_t>>;
Tom Josephb4268602020-04-17 17:20:45 +053067
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050068/** @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 */
77class 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 Misra868c8792020-05-26 03:12:13 -050087 using TLPDRMap = std::map<pdr::TerminusHandle, pdr::TerminusID>;
88
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050089 /** @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 Barithaya3aec9972020-12-14 01:55:44 -060094 * @param[in] eventsJsonDir - directory path which has the config JSONs
Tom Joseph74f27c72021-05-16 07:58:53 -070095 * @param[in] entityTree - Pointer to BMC and Host entity association tree
96 * @param[in] bmcEntityTree - pointer to BMC's entity association tree
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050097 * @param[in] requester - reference to Requester object
Tom Joseph74f27c72021-05-16 07:58:53 -070098 * @param[in] handler - PLDM request handler
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050099 */
Tom Joseph74f27c72021-05-16 07:58:53 -0700100 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 Bishop5079ac42021-08-19 18:35:06 -0400104 pldm_entity_association_tree* bmcEntityTree,
105 pldm::dbus_api::Requester& requester,
Sampa Misrac0c79482021-06-02 08:01:54 -0500106 pldm::requester::Handler<pldm::requester::Request>* handler,
Tom Joseph74f27c72021-05-16 07:58:53 -0700107 bool verbose = false);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500108
109 /** @brief fetch PDRs from host firmware. See @class.
110 * @param[in] recordHandles - list of record handles pointing to host's
111 * PDRs that need to be fetched.
112 */
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500113
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500114 void fetchPDR(PDRRecordHandles&& recordHandles);
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500115
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500116 /** @brief Send a PLDM event to host firmware containing a list of record
117 * handles of PDRs that the host firmware has to fetch.
118 * @param[in] pdrTypes - list of PDR types that need to be looked up in the
119 * BMC repo
120 * @param[in] eventDataFormat - format for PDRRepositoryChgEvent in DSP0248
121 */
122 void sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
123 uint8_t eventDataFormat);
124
Tom Josephb4268602020-04-17 17:20:45 +0530125 /** @brief Lookup host sensor info corresponding to requested SensorEntry
126 *
127 * @param[in] entry - TerminusID and SensorID
128 *
129 * @return SensorInfo corresponding to the input paramter SensorEntry
130 * throw std::out_of_range exception if not found
131 */
132 const pdr::SensorInfo& lookupSensorInfo(const SensorEntry& entry) const
133 {
134 return sensorMap.at(entry);
135 }
136
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600137 /** @brief Handles state sensor event
138 *
139 * @param[in] entry - state sensor entry
140 * @param[in] state - event state
141 *
142 * @return PLDM completion code
143 */
Brad Bishop5079ac42021-08-19 18:35:06 -0400144 int handleStateSensorEvent(
145 const pldm::responder::events::StateSensorEntry& entry,
146 pdr::EventState state);
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600147
Sampa Misra868c8792020-05-26 03:12:13 -0500148 /** @brief Parse state sensor PDRs and populate the sensorMap lookup data
149 * structure
150 *
151 * @param[in] stateSensorPDRs - host state sensor PDRs
152 * @param[in] tlpdrInfo - terminus locator PDRs info
153 *
154 */
155 void parseStateSensorPDRs(const PDRList& stateSensorPDRs,
156 const TLPDRMap& tlpdrInfo);
157
Sampa Misrac0c79482021-06-02 08:01:54 -0500158 /** @brief this function sends a GetPDR request to Host firmware.
159 * And processes the PDRs based on type
160 *
161 * @param[in] - nextRecordHandle - the next record handle to ask for
162 */
163 void getHostPDR(uint32_t nextRecordHandle = 0);
164
sampmisr6decfc12021-03-02 11:07:36 +0530165 /** @brief set the Host state when pldmd starts
166 */
167 void setHostState();
168
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600169 /** @brief set HostSensorStates when pldmd starts or restarts
170 * and updates the D-Bus property
171 * @param[in] stateSensorPDRs - host state sensor PDRs
172 * @param[in] tlinfo - vector of struct TlInfo
173 */
174 void setHostSensorState(const PDRList& stateSensorPDRs,
175 const std::vector<TlInfo>& tlinfo);
176
sampmisr6decfc12021-03-02 11:07:36 +0530177 /** @brief check whether Host is running when pldmd starts
178 */
179 bool isHostUp();
180
181 private:
182 /** @brief deferred function to fetch PDR from Host, scheduled to work on
183 * the event loop. The PDR exchg with the host is async.
184 * @param[in] source - sdeventplus event source
185 */
186 void _fetchPDR(sdeventplus::source::EventBase& source);
187
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500188 /** @brief Merge host firmware's entity association PDRs into BMC's
189 * @details A merge operation involves adding a pldm_entity under the
190 * appropriate parent, and updating container ids.
191 * @param[in] pdr - entity association pdr
192 */
193 void mergeEntityAssociations(const std::vector<uint8_t>& pdr);
194
195 /** @brief Find parent of input entity type, from the entity association
196 * tree
197 * @param[in] type - PLDM entity type
198 * @param[out] parent - PLDM entity information of parent
199 * @return bool - true if parent found, false otherwise
200 */
201 bool getParent(EntityType type, pldm_entity& parent);
202
Sampa Misrac0c79482021-06-02 08:01:54 -0500203 /** @brief process the Host's PDR and add to BMC's PDR repo
204 * @param[in] eid - MCTP id of Host
205 * @param[in] response - response from Host for GetPDR
206 * @param[in] respMsgLen - response message length
207 */
208 void processHostPDRs(mctp_eid_t eid, const pldm_msg* response,
209 size_t respMsgLen);
210
211 /** @brief send PDR Repo change after merging Host's PDR to BMC PDR repo
212 * @param[in] source - sdeventplus event source
213 */
214 void _processPDRRepoChgEvent(sdeventplus::source::EventBase& source);
215
216 /** @brief fetch the next PDR based on the record handle sent by Host
217 * @param[in] nextRecordHandle - next record handle
218 * @param[in] source - sdeventplus event source
219 */
220 void _processFetchPDREvent(uint32_t nextRecordHandle,
221 sdeventplus::source::EventBase& source);
222
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500223 /** @brief fd of MCTP communications socket */
224 int mctp_fd;
225 /** @brief MCTP EID of host firmware */
226 uint8_t mctp_eid;
227 /** @brief reference of main event loop of pldmd, primarily used to schedule
228 * work.
229 */
230 sdeventplus::Event& event;
231 /** @brief pointer to BMC's primary PDR repo, host PDRs are added here */
232 pldm_pdr* repo;
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600233
Brad Bishop5079ac42021-08-19 18:35:06 -0400234 pldm::responder::events::StateSensorHandler stateSensorHandler;
Sampa Misrac073a202021-05-08 10:56:05 -0500235 /** @brief Pointer to BMC's and Host's entity association tree */
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500236 pldm_entity_association_tree* entityTree;
Sampa Misrac073a202021-05-08 10:56:05 -0500237
238 /** @brief Pointer to BMC's entity association tree */
239 pldm_entity_association_tree* bmcEntityTree;
240
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500241 /** @brief reference to Requester object, primarily used to access API to
242 * obtain PLDM instance id.
243 */
Brad Bishop5079ac42021-08-19 18:35:06 -0400244 pldm::dbus_api::Requester& requester;
Tom Joseph74f27c72021-05-16 07:58:53 -0700245
246 /** @brief PLDM request handler */
Sampa Misrac0c79482021-06-02 08:01:54 -0500247 pldm::requester::Handler<pldm::requester::Request>* handler;
Tom Joseph74f27c72021-05-16 07:58:53 -0700248
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500249 /** @brief sdeventplus event source */
250 std::unique_ptr<sdeventplus::source::Defer> pdrFetchEvent;
Sampa Misrac0c79482021-06-02 08:01:54 -0500251 std::unique_ptr<sdeventplus::source::Defer> deferredFetchPDREvent;
252 std::unique_ptr<sdeventplus::source::Defer> deferredPDRRepoChgEvent;
253
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500254 /** @brief list of PDR record handles pointing to host's PDRs */
255 PDRRecordHandles pdrRecordHandles;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500256 /** @brief maps an entity type to parent pldm_entity from the BMC's entity
257 * association tree
258 */
259 std::map<EntityType, pldm_entity> parents;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500260 /** @brief D-Bus property changed signal match */
261 std::unique_ptr<sdbusplus::bus::match::match> hostOffMatch;
Tom Josephb4268602020-04-17 17:20:45 +0530262
263 /** @brief sensorMap is a lookup data structure that is build from the
264 * hostPDR that speeds up the lookup of <TerminusID, SensorID> in
265 * PlatformEventMessage command request.
266 */
267 HostStateSensorMap sensorMap;
Sridevi Rameshae28bc72020-12-10 07:21:16 -0600268 bool verbose;
sampmisr6decfc12021-03-02 11:07:36 +0530269
270 /** @brief whether response received from Host */
271 bool responseReceived;
272 /** @brief whether timed out waiting for a response from Host */
273 bool timeOut;
274 /** @brief request message instance id */
275 uint8_t insId;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500276};
277
278} // namespace pldm