1ac19bd68SDeepak Kodihalli #pragma once 2ac19bd68SDeepak Kodihalli 32abbce76SAndrew Jeffery #include "common/instance_id.hpp" 4d130e1a3SDeepak Kodihalli #include "common/types.hpp" 5d130e1a3SDeepak Kodihalli #include "common/utils.hpp" 63aec997cSPavithra Barithaya #include "libpldmresponder/event_parser.hpp" 73687e2b6SSagar Srinivas #include "libpldmresponder/oem_handler.hpp" 8ac19bd68SDeepak Kodihalli #include "libpldmresponder/pdr_utils.hpp" 974f27c73STom Joseph #include "requester/handler.hpp" 10*4e69d252SKamalkumar Patel #include "utils.hpp" 11ac19bd68SDeepak Kodihalli 12c453e164SGeorge Liu #include <libpldm/base.h> 13c453e164SGeorge Liu #include <libpldm/platform.h> 14c453e164SGeorge Liu 15ac19bd68SDeepak Kodihalli #include <sdeventplus/event.hpp> 16ac19bd68SDeepak Kodihalli #include <sdeventplus/source/event.hpp> 17ac19bd68SDeepak Kodihalli 187246e0cdSDeepak Kodihalli #include <deque> 19df9a6d34SGeorge Liu #include <filesystem> 20ac19bd68SDeepak Kodihalli #include <map> 21ac19bd68SDeepak Kodihalli #include <memory> 22ac19bd68SDeepak Kodihalli #include <vector> 23ac19bd68SDeepak Kodihalli 24ac19bd68SDeepak Kodihalli namespace pldm 25ac19bd68SDeepak Kodihalli { 26ac19bd68SDeepak Kodihalli using EntityType = uint16_t; 27ac19bd68SDeepak Kodihalli // vector which would hold the PDR record handle data returned by 28ac19bd68SDeepak Kodihalli // pldmPDRRepositoryChgEvent event data 29ac19bd68SDeepak Kodihalli using ChangeEntry = uint32_t; 307246e0cdSDeepak Kodihalli using PDRRecordHandles = std::deque<ChangeEntry>; 31ac19bd68SDeepak Kodihalli 32ac19bd68SDeepak Kodihalli /** @struct SensorEntry 33ac19bd68SDeepak Kodihalli * 34ac19bd68SDeepak Kodihalli * SensorEntry is a unique key which maps a sensorEventType request in the 35ac19bd68SDeepak Kodihalli * PlatformEventMessage command to a host sensor PDR. This struct is a key 36ac19bd68SDeepak Kodihalli * in a std::map, so implemented operator==and operator<. 37ac19bd68SDeepak Kodihalli */ 38ac19bd68SDeepak Kodihalli struct SensorEntry 39ac19bd68SDeepak Kodihalli { 40ac19bd68SDeepak Kodihalli pdr::TerminusID terminusID; 41ac19bd68SDeepak Kodihalli pdr::SensorID sensorID; 42ac19bd68SDeepak Kodihalli 43ac19bd68SDeepak Kodihalli bool operator==(const SensorEntry& e) const 44ac19bd68SDeepak Kodihalli { 45ac19bd68SDeepak Kodihalli return ((terminusID == e.terminusID) && (sensorID == e.sensorID)); 46ac19bd68SDeepak Kodihalli } 47ac19bd68SDeepak Kodihalli 48ac19bd68SDeepak Kodihalli bool operator<(const SensorEntry& e) const 49ac19bd68SDeepak Kodihalli { 50ac19bd68SDeepak Kodihalli return ((terminusID < e.terminusID) || 51ac19bd68SDeepak Kodihalli ((terminusID == e.terminusID) && (sensorID < e.sensorID))); 52ac19bd68SDeepak Kodihalli } 53ac19bd68SDeepak Kodihalli }; 54ac19bd68SDeepak Kodihalli 55ac19bd68SDeepak Kodihalli using HostStateSensorMap = std::map<SensorEntry, pdr::SensorInfo>; 56868c879aSSampa Misra using PDRList = std::vector<std::vector<uint8_t>>; 57ac19bd68SDeepak Kodihalli 58ac19bd68SDeepak Kodihalli /** @class HostPDRHandler 59ac19bd68SDeepak Kodihalli * @brief This class can fetch and process PDRs from host firmware 60ac19bd68SDeepak Kodihalli * @details Provides an API to fetch PDRs from the host firmware. Upon 61ac19bd68SDeepak Kodihalli * receiving the PDRs, they are stored into the BMC's primary PDR repo. 62ac19bd68SDeepak Kodihalli * Adjustments are made to entity association PDRs received from the host, 63ac19bd68SDeepak Kodihalli * because they need to be assimilated into the BMC's entity association 64ac19bd68SDeepak Kodihalli * tree. A PLDM event containing the record handles of the updated entity 65ac19bd68SDeepak Kodihalli * association PDRs is sent to the host. 66ac19bd68SDeepak Kodihalli */ 67ac19bd68SDeepak Kodihalli class HostPDRHandler 68ac19bd68SDeepak Kodihalli { 69ac19bd68SDeepak Kodihalli public: 70ac19bd68SDeepak Kodihalli HostPDRHandler() = delete; 71ac19bd68SDeepak Kodihalli HostPDRHandler(const HostPDRHandler&) = delete; 72ac19bd68SDeepak Kodihalli HostPDRHandler(HostPDRHandler&&) = delete; 73ac19bd68SDeepak Kodihalli HostPDRHandler& operator=(const HostPDRHandler&) = delete; 74ac19bd68SDeepak Kodihalli HostPDRHandler& operator=(HostPDRHandler&&) = delete; 75ac19bd68SDeepak Kodihalli ~HostPDRHandler() = default; 76ac19bd68SDeepak Kodihalli 7760e1fe91SManojkiran Eda using TerminusInfo = 7860e1fe91SManojkiran Eda std::tuple<pdr::TerminusID, pdr::EID, pdr::TerminusValidity>; 7960e1fe91SManojkiran Eda using TLPDRMap = std::map<pdr::TerminusHandle, TerminusInfo>; 80868c879aSSampa Misra 81ac19bd68SDeepak Kodihalli /** @brief Constructor 82ac19bd68SDeepak Kodihalli * @param[in] mctp_fd - fd of MCTP communications socket 83ac19bd68SDeepak Kodihalli * @param[in] mctp_eid - MCTP EID of host firmware 84ac19bd68SDeepak Kodihalli * @param[in] event - reference of main event loop of pldmd 85ac19bd68SDeepak Kodihalli * @param[in] repo - pointer to BMC's primary PDR repo 863aec997cSPavithra Barithaya * @param[in] eventsJsonDir - directory path which has the config JSONs 8774f27c73STom Joseph * @param[in] entityTree - Pointer to BMC and Host entity association tree 8874f27c73STom Joseph * @param[in] bmcEntityTree - pointer to BMC's entity association tree 89a330b2f0SAndrew Jeffery * @param[in] instanceIdDb - reference to an InstanceIdDb object 9074f27c73STom Joseph * @param[in] handler - PLDM request handler 91ac19bd68SDeepak Kodihalli */ 9274f27c73STom Joseph explicit HostPDRHandler( 9374f27c73STom Joseph int mctp_fd, uint8_t mctp_eid, sdeventplus::Event& event, 9474f27c73STom Joseph pldm_pdr* repo, const std::string& eventsJsonsDir, 95ac19bd68SDeepak Kodihalli pldm_entity_association_tree* entityTree, 965079ac4aSBrad Bishop pldm_entity_association_tree* bmcEntityTree, 97a330b2f0SAndrew Jeffery pldm::InstanceIdDb& instanceIdDb, 983687e2b6SSagar Srinivas pldm::requester::Handler<pldm::requester::Request>* handler, 993687e2b6SSagar Srinivas pldm::responder::oem_platform::Handler* oemPlatformHandler); 100ac19bd68SDeepak Kodihalli 101ac19bd68SDeepak Kodihalli /** @brief fetch PDRs from host firmware. See @class. 102ac19bd68SDeepak Kodihalli * @param[in] recordHandles - list of record handles pointing to host's 103ac19bd68SDeepak Kodihalli * PDRs that need to be fetched. 104ac19bd68SDeepak Kodihalli */ 105ac19bd68SDeepak Kodihalli 1067246e0cdSDeepak Kodihalli void fetchPDR(PDRRecordHandles&& recordHandles); 107ac19bd68SDeepak Kodihalli 108ac19bd68SDeepak Kodihalli /** @brief Send a PLDM event to host firmware containing a list of record 109ac19bd68SDeepak Kodihalli * handles of PDRs that the host firmware has to fetch. 110ac19bd68SDeepak Kodihalli * @param[in] pdrTypes - list of PDR types that need to be looked up in the 111ac19bd68SDeepak Kodihalli * BMC repo 112ac19bd68SDeepak Kodihalli * @param[in] eventDataFormat - format for PDRRepositoryChgEvent in DSP0248 113ac19bd68SDeepak Kodihalli */ 114ac19bd68SDeepak Kodihalli void sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes, 115ac19bd68SDeepak Kodihalli uint8_t eventDataFormat); 116ac19bd68SDeepak Kodihalli 117ac19bd68SDeepak Kodihalli /** @brief Lookup host sensor info corresponding to requested SensorEntry 118ac19bd68SDeepak Kodihalli * 119ac19bd68SDeepak Kodihalli * @param[in] entry - TerminusID and SensorID 120ac19bd68SDeepak Kodihalli * 121ac19bd68SDeepak Kodihalli * @return SensorInfo corresponding to the input paramter SensorEntry 122ac19bd68SDeepak Kodihalli * throw std::out_of_range exception if not found 123ac19bd68SDeepak Kodihalli */ 124ac19bd68SDeepak Kodihalli const pdr::SensorInfo& lookupSensorInfo(const SensorEntry& entry) const 125ac19bd68SDeepak Kodihalli { 126ac19bd68SDeepak Kodihalli return sensorMap.at(entry); 127ac19bd68SDeepak Kodihalli } 128ac19bd68SDeepak Kodihalli 1293aec997cSPavithra Barithaya /** @brief Handles state sensor event 1303aec997cSPavithra Barithaya * 1313aec997cSPavithra Barithaya * @param[in] entry - state sensor entry 1323aec997cSPavithra Barithaya * @param[in] state - event state 1333aec997cSPavithra Barithaya * 1343aec997cSPavithra Barithaya * @return PLDM completion code 1353aec997cSPavithra Barithaya */ 1365079ac4aSBrad Bishop int handleStateSensorEvent( 1375079ac4aSBrad Bishop const pldm::responder::events::StateSensorEntry& entry, 1383aec997cSPavithra Barithaya pdr::EventState state); 1393aec997cSPavithra Barithaya 140868c879aSSampa Misra /** @brief Parse state sensor PDRs and populate the sensorMap lookup data 141868c879aSSampa Misra * structure 142868c879aSSampa Misra * 143868c879aSSampa Misra * @param[in] stateSensorPDRs - host state sensor PDRs 144868c879aSSampa Misra * 145868c879aSSampa Misra */ 14660e1fe91SManojkiran Eda void parseStateSensorPDRs(const PDRList& stateSensorPDRs); 147868c879aSSampa Misra 148c0c79481SSampa Misra /** @brief this function sends a GetPDR request to Host firmware. 149c0c79481SSampa Misra * And processes the PDRs based on type 150c0c79481SSampa Misra * 151c0c79481SSampa Misra * @param[in] - nextRecordHandle - the next record handle to ask for 152c0c79481SSampa Misra */ 153c0c79481SSampa Misra void getHostPDR(uint32_t nextRecordHandle = 0); 154c0c79481SSampa Misra 155f9ba8c19SSampa Misra /** @brief set the Host firmware condition when pldmd starts 1566decfc18Ssampmisr */ 157f9ba8c19SSampa Misra void setHostFirmwareCondition(); 1586decfc18Ssampmisr 1594f2538a6SPavithra Barithaya /** @brief set HostSensorStates when pldmd starts or restarts 1604f2538a6SPavithra Barithaya * and updates the D-Bus property 1614f2538a6SPavithra Barithaya * @param[in] stateSensorPDRs - host state sensor PDRs 1624f2538a6SPavithra Barithaya */ 16360e1fe91SManojkiran Eda void setHostSensorState(const PDRList& stateSensorPDRs); 1644f2538a6SPavithra Barithaya 165ae5c97ebSPavithra Barithaya /** @brief whether we received PLDM_RECORDS_MODIFIED event data operation 166ae5c97ebSPavithra Barithaya * from host 167ae5c97ebSPavithra Barithaya */ 168ae5c97ebSPavithra Barithaya bool isHostPdrModified = false; 169ae5c97ebSPavithra Barithaya 1706decfc18Ssampmisr /** @brief check whether Host is running when pldmd starts 1716decfc18Ssampmisr */ 1726decfc18Ssampmisr bool isHostUp(); 1736decfc18Ssampmisr 17460e1fe91SManojkiran Eda /** @brief map that captures various terminus information **/ 17560e1fe91SManojkiran Eda TLPDRMap tlPDRInfo; 17660e1fe91SManojkiran Eda 1776decfc18Ssampmisr private: 1786decfc18Ssampmisr /** @brief deferred function to fetch PDR from Host, scheduled to work on 1796decfc18Ssampmisr * the event loop. The PDR exchg with the host is async. 1806decfc18Ssampmisr * @param[in] source - sdeventplus event source 1816decfc18Ssampmisr */ 1826decfc18Ssampmisr void _fetchPDR(sdeventplus::source::EventBase& source); 1836decfc18Ssampmisr 184ac19bd68SDeepak Kodihalli /** @brief Merge host firmware's entity association PDRs into BMC's 185ac19bd68SDeepak Kodihalli * @details A merge operation involves adding a pldm_entity under the 186ac19bd68SDeepak Kodihalli * appropriate parent, and updating container ids. 187ac19bd68SDeepak Kodihalli * @param[in] pdr - entity association pdr 1883687e2b6SSagar Srinivas * @param[in] size - size of input PDR record in bytes 1893687e2b6SSagar Srinivas * @param[in] record_handle - record handle of the PDR 190ac19bd68SDeepak Kodihalli */ 1913687e2b6SSagar Srinivas void 1923687e2b6SSagar Srinivas mergeEntityAssociations(const std::vector<uint8_t>& pdr, 1933687e2b6SSagar Srinivas [[maybe_unused]] const uint32_t& size, 1943687e2b6SSagar Srinivas [[maybe_unused]] const uint32_t& record_handle); 195ac19bd68SDeepak Kodihalli 196c0c79481SSampa Misra /** @brief process the Host's PDR and add to BMC's PDR repo 197c0c79481SSampa Misra * @param[in] eid - MCTP id of Host 198c0c79481SSampa Misra * @param[in] response - response from Host for GetPDR 199c0c79481SSampa Misra * @param[in] respMsgLen - response message length 200c0c79481SSampa Misra */ 201c0c79481SSampa Misra void processHostPDRs(mctp_eid_t eid, const pldm_msg* response, 202c0c79481SSampa Misra size_t respMsgLen); 203c0c79481SSampa Misra 204c0c79481SSampa Misra /** @brief send PDR Repo change after merging Host's PDR to BMC PDR repo 205c0c79481SSampa Misra * @param[in] source - sdeventplus event source 206c0c79481SSampa Misra */ 207c0c79481SSampa Misra void _processPDRRepoChgEvent(sdeventplus::source::EventBase& source); 208c0c79481SSampa Misra 209c0c79481SSampa Misra /** @brief fetch the next PDR based on the record handle sent by Host 210c0c79481SSampa Misra * @param[in] nextRecordHandle - next record handle 211c0c79481SSampa Misra * @param[in] source - sdeventplus event source 212c0c79481SSampa Misra */ 213c0c79481SSampa Misra void _processFetchPDREvent(uint32_t nextRecordHandle, 214c0c79481SSampa Misra sdeventplus::source::EventBase& source); 215c0c79481SSampa Misra 216682ee18cSGeorge Liu /** @brief Get FRU record table metadata by remote PLDM terminus 217682ee18cSGeorge Liu * 218682ee18cSGeorge Liu * @param[out] uint16_t - total table records 219682ee18cSGeorge Liu */ 220682ee18cSGeorge Liu void getFRURecordTableMetadataByRemote(const PDRList& fruRecordSetPDRs); 221682ee18cSGeorge Liu 222682ee18cSGeorge Liu /** @brief Set Location Code in the dbus objects 223682ee18cSGeorge Liu * 224682ee18cSGeorge Liu * @param[in] fruRecordSetPDRs - the Fru Record set PDR's 225682ee18cSGeorge Liu * @param[in] fruRecordData - the Fru Record Data 226682ee18cSGeorge Liu */ 227682ee18cSGeorge Liu 228682ee18cSGeorge Liu void setFRUDataOnDBus( 229682ee18cSGeorge Liu const PDRList& fruRecordSetPDRs, 230682ee18cSGeorge Liu const std::vector<responder::pdr_utils::FruRecordDataFormat>& 231682ee18cSGeorge Liu fruRecordData); 232682ee18cSGeorge Liu 233682ee18cSGeorge Liu /** @brief Get FRU record table by remote PLDM terminus 234682ee18cSGeorge Liu * 235682ee18cSGeorge Liu * @param[in] fruRecordSetPDRs - the Fru Record set PDR's 236682ee18cSGeorge Liu * @param[in] totalTableRecords - the Number of total table records 237682ee18cSGeorge Liu * @return 238682ee18cSGeorge Liu */ 239682ee18cSGeorge Liu void getFRURecordTableByRemote(const PDRList& fruRecordSetPDRs, 240682ee18cSGeorge Liu uint16_t totalTableRecords); 241682ee18cSGeorge Liu 242682ee18cSGeorge Liu /** @brief Create Dbus objects by remote PLDM entity Fru PDRs 243682ee18cSGeorge Liu * 244682ee18cSGeorge Liu * @param[in] fruRecordSetPDRs - fru record set pdr 245682ee18cSGeorge Liu * 246682ee18cSGeorge Liu * @ return 247682ee18cSGeorge Liu */ 248682ee18cSGeorge Liu void createDbusObjects(const PDRList& fruRecordSetPDRs); 249682ee18cSGeorge Liu 250682ee18cSGeorge Liu /** @brief Get FRU Record Set Identifier from FRU Record data Format 251682ee18cSGeorge Liu * @param[in] fruRecordSetPDRs - fru record set pdr 252682ee18cSGeorge Liu * @param[in] entity - PLDM entity information 253682ee18cSGeorge Liu * @return 254682ee18cSGeorge Liu */ 255682ee18cSGeorge Liu std::optional<uint16_t> getRSI(const PDRList& fruRecordSetPDRs, 256682ee18cSGeorge Liu const pldm_entity& entity); 257682ee18cSGeorge Liu 258ac19bd68SDeepak Kodihalli /** @brief fd of MCTP communications socket */ 259ac19bd68SDeepak Kodihalli int mctp_fd; 260ac19bd68SDeepak Kodihalli /** @brief MCTP EID of host firmware */ 261ac19bd68SDeepak Kodihalli uint8_t mctp_eid; 262ac19bd68SDeepak Kodihalli /** @brief reference of main event loop of pldmd, primarily used to schedule 263ac19bd68SDeepak Kodihalli * work. 264ac19bd68SDeepak Kodihalli */ 265ac19bd68SDeepak Kodihalli sdeventplus::Event& event; 266ac19bd68SDeepak Kodihalli /** @brief pointer to BMC's primary PDR repo, host PDRs are added here */ 267ac19bd68SDeepak Kodihalli pldm_pdr* repo; 2683aec997cSPavithra Barithaya 2695079ac4aSBrad Bishop pldm::responder::events::StateSensorHandler stateSensorHandler; 270c073a20eSSampa Misra /** @brief Pointer to BMC's and Host's entity association tree */ 271ac19bd68SDeepak Kodihalli pldm_entity_association_tree* entityTree; 272c073a20eSSampa Misra 273c073a20eSSampa Misra /** @brief Pointer to BMC's entity association tree */ 274c073a20eSSampa Misra pldm_entity_association_tree* bmcEntityTree; 275c073a20eSSampa Misra 276a330b2f0SAndrew Jeffery /** @brief reference to Instance ID database object, used to obtain PLDM 277a330b2f0SAndrew Jeffery * instance IDs 278ac19bd68SDeepak Kodihalli */ 279a330b2f0SAndrew Jeffery pldm::InstanceIdDb& instanceIdDb; 28074f27c73STom Joseph 28174f27c73STom Joseph /** @brief PLDM request handler */ 282c0c79481SSampa Misra pldm::requester::Handler<pldm::requester::Request>* handler; 28374f27c73STom Joseph 284ac19bd68SDeepak Kodihalli /** @brief sdeventplus event source */ 285ac19bd68SDeepak Kodihalli std::unique_ptr<sdeventplus::source::Defer> pdrFetchEvent; 286c0c79481SSampa Misra std::unique_ptr<sdeventplus::source::Defer> deferredFetchPDREvent; 287c0c79481SSampa Misra std::unique_ptr<sdeventplus::source::Defer> deferredPDRRepoChgEvent; 288c0c79481SSampa Misra 289ac19bd68SDeepak Kodihalli /** @brief list of PDR record handles pointing to host's PDRs */ 290ac19bd68SDeepak Kodihalli PDRRecordHandles pdrRecordHandles; 291ac19bd68SDeepak Kodihalli /** @brief maps an entity type to parent pldm_entity from the BMC's entity 292ac19bd68SDeepak Kodihalli * association tree 293ac19bd68SDeepak Kodihalli */ 294ae5c97ebSPavithra Barithaya 295ae5c97ebSPavithra Barithaya /** @brief list of PDR record handles modified pointing to host PDRs */ 296ae5c97ebSPavithra Barithaya PDRRecordHandles modifiedPDRRecordHandles; 297ae5c97ebSPavithra Barithaya 298ac19bd68SDeepak Kodihalli /** @brief D-Bus property changed signal match */ 29984b790cbSPatrick Williams std::unique_ptr<sdbusplus::bus::match_t> hostOffMatch; 300ac19bd68SDeepak Kodihalli 301ac19bd68SDeepak Kodihalli /** @brief sensorMap is a lookup data structure that is build from the 302ac19bd68SDeepak Kodihalli * hostPDR that speeds up the lookup of <TerminusID, SensorID> in 303ac19bd68SDeepak Kodihalli * PlatformEventMessage command request. 304ac19bd68SDeepak Kodihalli */ 305ac19bd68SDeepak Kodihalli HostStateSensorMap sensorMap; 3066decfc18Ssampmisr 3076decfc18Ssampmisr /** @brief whether response received from Host */ 3086decfc18Ssampmisr bool responseReceived; 309acf2c8c3SGeorge Liu 310acf2c8c3SGeorge Liu /** @brief variable that captures if the first entity association PDR 311acf2c8c3SGeorge Liu * from host is merged into the BMC tree 312acf2c8c3SGeorge Liu */ 313acf2c8c3SGeorge Liu bool mergedHostParents; 314acf2c8c3SGeorge Liu 315df9a6d34SGeorge Liu /** @brief maps an object path to pldm_entity from the BMC's entity 316df9a6d34SGeorge Liu * association tree 317df9a6d34SGeorge Liu */ 318*4e69d252SKamalkumar Patel ObjectPathMaps objPathMap; 319df9a6d34SGeorge Liu 320df9a6d34SGeorge Liu /** @brief maps an entity name to map, maps to entity name to pldm_entity 321df9a6d34SGeorge Liu */ 322*4e69d252SKamalkumar Patel EntityAssociations entityAssociations; 323682ee18cSGeorge Liu 324682ee18cSGeorge Liu /** @brief the vector of FRU Record Data Format 325682ee18cSGeorge Liu */ 326682ee18cSGeorge Liu std::vector<responder::pdr_utils::FruRecordDataFormat> fruRecordData; 327682ee18cSGeorge Liu 3283687e2b6SSagar Srinivas /** @OEM platform handler */ 3293687e2b6SSagar Srinivas pldm::responder::oem_platform::Handler* oemPlatformHandler; 330ac19bd68SDeepak Kodihalli }; 331ac19bd68SDeepak Kodihalli 332ac19bd68SDeepak Kodihalli } // namespace pldm 333