blob: 886ca58d2eabc26a61ff0e25472ee636bceb4ce0 [file] [log] [blame]
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05001#include "host_pdr_handler.hpp"
2
Pavithra Barithayae8beb892020-04-14 23:24:25 -05003#include <assert.h>
George Liuc453e162022-12-21 17:16:23 +08004#include <libpldm/pldm.h>
Pavithra Barithayae8beb892020-04-14 23:24:25 -05005
Deepak Kodihalli87514cc2020-04-16 09:08:38 -05006#include <nlohmann/json.hpp>
Riya Dixit49cfb132023-03-02 04:26:53 -06007#include <phosphor-logging/lg2.hpp>
sampmisr6decfc12021-03-02 11:07:36 +05308#include <sdeventplus/clock.hpp>
9#include <sdeventplus/exception.hpp>
10#include <sdeventplus/source/io.hpp>
11#include <sdeventplus/source/time.hpp>
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050012
George Liu6492f522020-06-16 10:34:05 +080013#include <fstream>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050014
Riya Dixit49cfb132023-03-02 04:26:53 -060015PHOSPHOR_LOG2_USING;
16
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050017namespace pldm
18{
Brad Bishop5079ac42021-08-19 18:35:06 -040019using namespace pldm::responder::events;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050020using namespace pldm::utils;
21using namespace sdbusplus::bus::match::rules;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050022using Json = nlohmann::json;
23namespace fs = std::filesystem;
24constexpr auto fruJson = "host_frus.json";
25const Json emptyJson{};
26const std::vector<Json> emptyJsonList{};
27
Manojkiran Eda3ca40452021-10-04 22:51:37 +053028template <typename T>
29uint16_t extractTerminusHandle(std::vector<uint8_t>& pdr)
30{
31 T* var = nullptr;
32 if (std::is_same<T, pldm_pdr_fru_record_set>::value)
33 {
34 var = (T*)(pdr.data() + sizeof(pldm_pdr_hdr));
35 }
36 else
37 {
38 var = (T*)(pdr.data());
39 }
40 if (var != nullptr)
41 {
42 return var->terminus_handle;
43 }
44 return TERMINUS_HANDLE;
45}
46
Tom Joseph74f27c72021-05-16 07:58:53 -070047HostPDRHandler::HostPDRHandler(
48 int mctp_fd, uint8_t mctp_eid, sdeventplus::Event& event, pldm_pdr* repo,
49 const std::string& eventsJsonsDir, pldm_entity_association_tree* entityTree,
Andrew Jefferya330b2f2023-05-04 14:55:37 +093050 pldm_entity_association_tree* bmcEntityTree,
51 pldm::InstanceIdDb& instanceIdDb,
Tom Josephe5268cd2021-09-07 13:04:03 +053052 pldm::requester::Handler<pldm::requester::Request>* handler) :
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050053 mctp_fd(mctp_fd),
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060054 mctp_eid(mctp_eid), event(event), repo(repo),
55 stateSensorHandler(eventsJsonsDir), entityTree(entityTree),
Andrew Jefferya330b2f2023-05-04 14:55:37 +093056 bmcEntityTree(bmcEntityTree), instanceIdDb(instanceIdDb), handler(handler)
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050057{
58 fs::path hostFruJson(fs::path(HOST_JSONS_DIR) / fruJson);
59 if (fs::exists(hostFruJson))
60 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -050061 // Note parent entities for entities sent down by the host firmware.
62 // This will enable a merge of entity associations.
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050063 try
64 {
65 std::ifstream jsonFile(hostFruJson);
66 auto data = Json::parse(jsonFile, nullptr, false);
67 if (data.is_discarded())
68 {
Riya Dixit49cfb132023-03-02 04:26:53 -060069 error("Parsing Host FRU json file failed");
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050070 }
71 else
72 {
73 auto entities = data.value("entities", emptyJsonList);
74 for (auto& entity : entities)
75 {
76 EntityType entityType = entity.value("entity_type", 0);
77 auto parent = entity.value("parent", emptyJson);
78 pldm_entity p{};
79 p.entity_type = parent.value("entity_type", 0);
80 p.entity_instance_num = parent.value("entity_instance", 0);
81 parents.emplace(entityType, std::move(p));
82 }
83 }
84 }
85 catch (const std::exception& e)
86 {
Riya Dixit49cfb132023-03-02 04:26:53 -060087 error("Parsing Host FRU json file failed, exception = {ERR_EXCEP}",
88 "ERR_EXCEP", e.what());
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050089 }
90 }
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050091
Patrick Williams84b790c2022-07-22 19:26:56 -050092 hostOffMatch = std::make_unique<sdbusplus::bus::match_t>(
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050093 pldm::utils::DBusHandler::getBus(),
94 propertiesChanged("/xyz/openbmc_project/state/host0",
95 "xyz.openbmc_project.State.Host"),
Patrick Williams84b790c2022-07-22 19:26:56 -050096 [this, repo, entityTree, bmcEntityTree](sdbusplus::message_t& msg) {
Patrick Williams6da4f912023-05-10 07:50:53 -050097 DbusChangedProps props{};
98 std::string intf;
99 msg.read(intf, props);
100 const auto itr = props.find("CurrentHostState");
101 if (itr != props.end())
102 {
103 PropertyValue value = itr->second;
104 auto propVal = std::get<std::string>(value);
105 if (propVal == "xyz.openbmc_project.State.Host.HostState.Off")
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500106 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500107 // Delete all the remote terminus information
108 std::erase_if(tlPDRInfo, [](const auto& item) {
109 auto const& [key, value] = item;
110 return key != TERMINUS_HANDLE;
111 });
112 pldm_pdr_remove_remote_pdrs(repo);
113 pldm_entity_association_tree_destroy_root(entityTree);
114 pldm_entity_association_tree_copy_root(bmcEntityTree,
115 entityTree);
116 this->sensorMap.clear();
117 this->responseReceived = false;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500118 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500119 }
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500120 });
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500121}
122
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500123void HostPDRHandler::fetchPDR(PDRRecordHandles&& recordHandles)
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500124{
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500125 pdrRecordHandles.clear();
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500126 modifiedPDRRecordHandles.clear();
127
128 if (isHostPdrModified)
129 {
130 modifiedPDRRecordHandles = std::move(recordHandles);
131 }
132 else
133 {
134 pdrRecordHandles = std::move(recordHandles);
135 }
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500136
137 // Defer the actual fetch of PDRs from the host (by queuing the call on the
138 // main event loop). That way, we can respond to the platform event msg from
139 // the host firmware.
140 pdrFetchEvent = std::make_unique<sdeventplus::source::Defer>(
141 event, std::bind(std::mem_fn(&HostPDRHandler::_fetchPDR), this,
142 std::placeholders::_1));
143}
144
145void HostPDRHandler::_fetchPDR(sdeventplus::source::EventBase& /*source*/)
146{
Sampa Misrac0c79482021-06-02 08:01:54 -0500147 getHostPDR();
148}
149
150void HostPDRHandler::getHostPDR(uint32_t nextRecordHandle)
151{
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500152 pdrFetchEvent.reset();
153
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500154 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
155 PLDM_GET_PDR_REQ_BYTES);
156 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500157 uint32_t recordHandle{};
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500158 if (!nextRecordHandle && (!modifiedPDRRecordHandles.empty()) &&
159 isHostPdrModified)
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500160 {
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500161 recordHandle = modifiedPDRRecordHandles.front();
162 modifiedPDRRecordHandles.pop_front();
163 }
164 else if (!nextRecordHandle && (!pdrRecordHandles.empty()))
165 {
166 recordHandle = pdrRecordHandles.front();
167 pdrRecordHandles.pop_front();
Sampa Misrac0c79482021-06-02 08:01:54 -0500168 }
169 else
170 {
171 recordHandle = nextRecordHandle;
172 }
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930173 auto instanceId = instanceIdDb.next(mctp_eid);
Sampa Misrac0c79482021-06-02 08:01:54 -0500174
Patrick Williams6da4f912023-05-10 07:50:53 -0500175 auto rc = encode_get_pdr_req(instanceId, recordHandle, 0,
176 PLDM_GET_FIRSTPART, UINT16_MAX, 0, request,
177 PLDM_GET_PDR_REQ_BYTES);
Sampa Misrac0c79482021-06-02 08:01:54 -0500178 if (rc != PLDM_SUCCESS)
179 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930180 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -0600181 error("Failed to encode_get_pdr_req, rc = {RC}", "RC", rc);
Sampa Misrac0c79482021-06-02 08:01:54 -0500182 return;
183 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500184
Sampa Misrac0c79482021-06-02 08:01:54 -0500185 rc = handler->registerRequest(
186 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_GET_PDR,
187 std::move(requestMsg),
188 std::move(std::bind_front(&HostPDRHandler::processHostPDRs, this)));
189 if (rc)
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500190 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600191 error("Failed to send the GetPDR request to Host");
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500192 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500193}
194
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600195int HostPDRHandler::handleStateSensorEvent(const StateSensorEntry& entry,
196 pdr::EventState state)
197{
198 auto rc = stateSensorHandler.eventAction(entry, state);
199 if (rc != PLDM_SUCCESS)
200 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600201 error("Failed to fetch and update D-bus property, rc = {RC}", "RC", rc);
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600202 return rc;
203 }
204 return PLDM_SUCCESS;
205}
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500206bool HostPDRHandler::getParent(EntityType type, pldm_entity& parent)
207{
208 auto found = parents.find(type);
209 if (found != parents.end())
210 {
211 parent.entity_type = found->second.entity_type;
212 parent.entity_instance_num = found->second.entity_instance_num;
213 return true;
214 }
215
216 return false;
217}
218
219void HostPDRHandler::mergeEntityAssociations(const std::vector<uint8_t>& pdr)
220{
221 size_t numEntities{};
222 pldm_entity* entities = nullptr;
223 bool merged = false;
224 auto entityPdr = reinterpret_cast<pldm_pdr_entity_association*>(
225 const_cast<uint8_t*>(pdr.data()) + sizeof(pldm_pdr_hdr));
226
227 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &numEntities,
228 &entities);
229 for (size_t i = 0; i < numEntities; ++i)
230 {
231 pldm_entity parent{};
232 if (getParent(entities[i].entity_type, parent))
233 {
234 auto node = pldm_entity_association_tree_find(entityTree, &parent);
235 if (node)
236 {
George Liu64a8f0f2021-06-12 10:56:11 +0800237 pldm_entity_association_tree_add(entityTree, &entities[i],
238 0xFFFF, node,
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500239 entityPdr->association_type);
240 merged = true;
241 }
242 }
243 }
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500244
245 if (merged)
246 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500247 // Update our PDR repo with the merged entity association PDRs
Sampa Misra719ed392021-06-04 05:15:13 -0500248 pldm_entity_node* node = nullptr;
249 pldm_find_entity_ref_in_tree(entityTree, entities[0], &node);
250 if (node == nullptr)
251 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600252 error("could not find referrence of the entity in the tree");
Sampa Misra719ed392021-06-04 05:15:13 -0500253 }
254 else
255 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530256 pldm_entity_association_pdr_add_from_node(
257 node, repo, &entities, numEntities, true, TERMINUS_HANDLE);
Sampa Misra719ed392021-06-04 05:15:13 -0500258 }
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500259 }
Sampa Misra719ed392021-06-04 05:15:13 -0500260 free(entities);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500261}
262
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500263void HostPDRHandler::sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
264 uint8_t eventDataFormat)
265{
266 assert(eventDataFormat == FORMAT_IS_PDR_HANDLES);
267
268 // Extract from the PDR repo record handles of PDRs we want the host
269 // to pull up.
270 std::vector<uint8_t> eventDataOps{PLDM_RECORDS_ADDED};
271 std::vector<uint8_t> numsOfChangeEntries(1);
272 std::vector<std::vector<ChangeEntry>> changeEntries(
273 numsOfChangeEntries.size());
274 for (auto pdrType : pdrTypes)
275 {
276 const pldm_pdr_record* record{};
277 do
278 {
279 record = pldm_pdr_find_record_by_type(repo, pdrType, record,
280 nullptr, nullptr);
281 if (record && pldm_pdr_record_is_remote(record))
282 {
283 changeEntries[0].push_back(
284 pldm_pdr_get_record_handle(repo, record));
285 }
286 } while (record);
287 }
288 if (changeEntries.empty())
289 {
290 return;
291 }
292 numsOfChangeEntries[0] = changeEntries[0].size();
293
294 // Encode PLDM platform event msg to indicate a PDR repo change.
295 size_t maxSize = PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH +
296 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH +
297 changeEntries[0].size() * sizeof(uint32_t);
298 std::vector<uint8_t> eventDataVec{};
299 eventDataVec.resize(maxSize);
300 auto eventData =
301 reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>(
302 eventDataVec.data());
303 size_t actualSize{};
304 auto firstEntry = changeEntries[0].data();
305 auto rc = encode_pldm_pdr_repository_chg_event_data(
306 eventDataFormat, 1, eventDataOps.data(), numsOfChangeEntries.data(),
307 &firstEntry, eventData, &actualSize, maxSize);
308 if (rc != PLDM_SUCCESS)
309 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600310 error("Failed to encode_pldm_pdr_repository_chg_event_data, rc = {RC}",
311 "RC", rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500312 return;
313 }
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930314 auto instanceId = instanceIdDb.next(mctp_eid);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500315 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
316 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
317 actualSize);
318 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
319 rc = encode_platform_event_message_req(
ArchanaKakani6c39c7a2022-12-05 04:36:35 -0600320 instanceId, 1, TERMINUS_ID, PLDM_PDR_REPOSITORY_CHG_EVENT,
321 eventDataVec.data(), actualSize, request,
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500322 actualSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500323 if (rc != PLDM_SUCCESS)
324 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930325 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -0600326 error("Failed to encode_platform_event_message_req, rc = {RC}", "RC",
327 rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500328 return;
329 }
330
Patrick Williams6da4f912023-05-10 07:50:53 -0500331 auto platformEventMessageResponseHandler =
332 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
Tom Joseph74f27c72021-05-16 07:58:53 -0700333 if (response == nullptr || !respMsgLen)
334 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600335 error(
336 "Failed to receive response for the PDR repository changed event");
Tom Joseph74f27c72021-05-16 07:58:53 -0700337 return;
338 }
339
340 uint8_t completionCode{};
341 uint8_t status{};
342 auto responsePtr = reinterpret_cast<const struct pldm_msg*>(response);
Pavithra Barithaya54b5a562021-09-27 06:07:10 -0500343 auto rc = decode_platform_event_message_resp(responsePtr, respMsgLen,
344 &completionCode, &status);
Sampa Misrac0c79482021-06-02 08:01:54 -0500345 if (rc || completionCode)
Tom Joseph74f27c72021-05-16 07:58:53 -0700346 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600347 error(
348 "Failed to decode_platform_event_message_resp: {RC}, cc = {CC}",
349 "RC", rc, "CC", static_cast<unsigned>(completionCode));
Tom Joseph74f27c72021-05-16 07:58:53 -0700350 }
351 };
352
Sampa Misrac0c79482021-06-02 08:01:54 -0500353 rc = handler->registerRequest(
Sampa Misra626c5652021-08-11 10:28:48 -0500354 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
Tom Joseph74f27c72021-05-16 07:58:53 -0700355 std::move(requestMsg), std::move(platformEventMessageResponseHandler));
Sampa Misrac0c79482021-06-02 08:01:54 -0500356 if (rc)
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500357 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600358 error("Failed to send the PDR repository changed event request");
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500359 }
360}
361
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530362void HostPDRHandler::parseStateSensorPDRs(const PDRList& stateSensorPDRs)
Sampa Misra868c8792020-05-26 03:12:13 -0500363{
364 for (const auto& pdr : stateSensorPDRs)
365 {
366 SensorEntry sensorEntry{};
367 const auto& [terminusHandle, sensorID, sensorInfo] =
368 responder::pdr_utils::parseStateSensorPDR(pdr);
369 sensorEntry.sensorID = sensorID;
370 try
371 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530372 sensorEntry.terminusID = std::get<0>(tlPDRInfo.at(terminusHandle));
Sampa Misra868c8792020-05-26 03:12:13 -0500373 }
374 // If there is no mapping for terminusHandle assign the reserved TID
375 // value of 0xFF to indicate that.
376 catch (const std::out_of_range& e)
377 {
378 sensorEntry.terminusID = PLDM_TID_RESERVED;
379 }
380 sensorMap.emplace(sensorEntry, std::move(sensorInfo));
381 }
382}
383
Sampa Misrac0c79482021-06-02 08:01:54 -0500384void HostPDRHandler::processHostPDRs(mctp_eid_t /*eid*/,
385 const pldm_msg* response,
386 size_t respMsgLen)
387{
388 static bool merged = false;
389 static PDRList stateSensorPDRs{};
Sampa Misrac0c79482021-06-02 08:01:54 -0500390 uint32_t nextRecordHandle{};
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600391 uint8_t tlEid = 0;
392 bool tlValid = true;
393 uint32_t rh = 0;
394 uint16_t terminusHandle = 0;
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530395 uint16_t pdrTerminusHandle = 0;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600396 uint8_t tid = 0;
397
Sampa Misrac0c79482021-06-02 08:01:54 -0500398 uint8_t completionCode{};
399 uint32_t nextDataTransferHandle{};
400 uint8_t transferFlag{};
401 uint16_t respCount{};
402 uint8_t transferCRC{};
403 if (response == nullptr || !respMsgLen)
404 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600405 error("Failed to receive response for the GetPDR command");
Sampa Misrac0c79482021-06-02 08:01:54 -0500406 return;
407 }
408
409 auto rc = decode_get_pdr_resp(
410 response, respMsgLen /*- sizeof(pldm_msg_hdr)*/, &completionCode,
411 &nextRecordHandle, &nextDataTransferHandle, &transferFlag, &respCount,
412 nullptr, 0, &transferCRC);
413 std::vector<uint8_t> responsePDRMsg;
414 responsePDRMsg.resize(respMsgLen + sizeof(pldm_msg_hdr));
415 memcpy(responsePDRMsg.data(), response, respMsgLen + sizeof(pldm_msg_hdr));
Sampa Misrac0c79482021-06-02 08:01:54 -0500416 if (rc != PLDM_SUCCESS)
417 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600418 error("Failed to decode_get_pdr_resp, rc = {RC}", "RC", rc);
Sampa Misrac0c79482021-06-02 08:01:54 -0500419 return;
420 }
421 else
422 {
423 std::vector<uint8_t> pdr(respCount, 0);
424 rc = decode_get_pdr_resp(response, respMsgLen, &completionCode,
425 &nextRecordHandle, &nextDataTransferHandle,
426 &transferFlag, &respCount, pdr.data(),
427 respCount, &transferCRC);
428 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
429 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600430 error("Failed to decode_get_pdr_resp: rc = {RC}, cc = {CC}", "RC",
431 rc, "CC", static_cast<unsigned>(completionCode));
Sampa Misrac0c79482021-06-02 08:01:54 -0500432 return;
433 }
434 else
435 {
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600436 // when nextRecordHandle is 0, we need the recordHandle of the last
437 // PDR and not 0-1.
438 if (!nextRecordHandle)
439 {
440 rh = nextRecordHandle;
441 }
442 else
443 {
444 rh = nextRecordHandle - 1;
445 }
446
Sampa Misrac0c79482021-06-02 08:01:54 -0500447 auto pdrHdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600448 if (!rh)
449 {
450 rh = pdrHdr->record_handle;
451 }
452
Sampa Misrac0c79482021-06-02 08:01:54 -0500453 if (pdrHdr->type == PLDM_PDR_ENTITY_ASSOCIATION)
454 {
455 this->mergeEntityAssociations(pdr);
456 merged = true;
457 }
458 else
459 {
460 if (pdrHdr->type == PLDM_TERMINUS_LOCATOR_PDR)
461 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530462 pdrTerminusHandle =
463 extractTerminusHandle<pldm_terminus_locator_pdr>(pdr);
Sampa Misrac0c79482021-06-02 08:01:54 -0500464 auto tlpdr =
465 reinterpret_cast<const pldm_terminus_locator_pdr*>(
466 pdr.data());
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600467
468 terminusHandle = tlpdr->terminus_handle;
469 tid = tlpdr->tid;
470 auto terminus_locator_type = tlpdr->terminus_locator_type;
471 if (terminus_locator_type ==
472 PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID)
473 {
474 auto locatorValue = reinterpret_cast<
475 const pldm_terminus_locator_type_mctp_eid*>(
476 tlpdr->terminus_locator_value);
477 tlEid = static_cast<uint8_t>(locatorValue->eid);
478 }
479 if (tlpdr->validity == 0)
480 {
481 tlValid = false;
482 }
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500483 for (const auto& terminusMap : tlPDRInfo)
484 {
485 if ((terminusHandle == (terminusMap.first)) &&
486 (get<1>(terminusMap.second) == tlEid) &&
487 (get<2>(terminusMap.second) == tlpdr->validity))
488 {
489 // TL PDR already present with same validity don't
490 // add the PDR to the repo just return
491 return;
492 }
493 }
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530494 tlPDRInfo.insert_or_assign(
495 tlpdr->terminus_handle,
496 std::make_tuple(tlpdr->tid, tlEid, tlpdr->validity));
Sampa Misrac0c79482021-06-02 08:01:54 -0500497 }
498 else if (pdrHdr->type == PLDM_STATE_SENSOR_PDR)
499 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530500 pdrTerminusHandle =
501 extractTerminusHandle<pldm_state_sensor_pdr>(pdr);
Sampa Misrac0c79482021-06-02 08:01:54 -0500502 stateSensorPDRs.emplace_back(pdr);
503 }
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530504 else if (pdrHdr->type == PLDM_PDR_FRU_RECORD_SET)
505 {
506 pdrTerminusHandle =
507 extractTerminusHandle<pldm_pdr_fru_record_set>(pdr);
508 }
509 else if (pdrHdr->type == PLDM_STATE_EFFECTER_PDR)
510 {
511 pdrTerminusHandle =
512 extractTerminusHandle<pldm_state_effecter_pdr>(pdr);
513 }
514 else if (pdrHdr->type == PLDM_NUMERIC_EFFECTER_PDR)
515 {
516 pdrTerminusHandle =
517 extractTerminusHandle<pldm_numeric_effecter_value_pdr>(
518 pdr);
519 }
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600520 // if the TLPDR is invalid update the repo accordingly
521 if (!tlValid)
522 {
523 pldm_pdr_update_TL_pdr(repo, terminusHandle, tid, tlEid,
524 tlValid);
525 }
526 else
527 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530528 pldm_pdr_add(repo, pdr.data(), respCount, rh, true,
529 pdrTerminusHandle);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600530 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500531 }
532 }
533 }
534 if (!nextRecordHandle)
535 {
536 /*received last record*/
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530537 this->parseStateSensorPDRs(stateSensorPDRs);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600538 if (isHostUp())
539 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530540 this->setHostSensorState(stateSensorPDRs);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600541 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500542 stateSensorPDRs.clear();
Sampa Misrac0c79482021-06-02 08:01:54 -0500543 if (merged)
544 {
545 merged = false;
546 deferredPDRRepoChgEvent =
547 std::make_unique<sdeventplus::source::Defer>(
548 event,
549 std::bind(
550 std::mem_fn((&HostPDRHandler::_processPDRRepoChgEvent)),
551 this, std::placeholders::_1));
552 }
553 }
554 else
555 {
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500556 if (modifiedPDRRecordHandles.empty() && isHostPdrModified)
557 {
558 isHostPdrModified = false;
559 }
560 else
561 {
562 deferredFetchPDREvent =
563 std::make_unique<sdeventplus::source::Defer>(
564 event,
565 std::bind(
566 std::mem_fn((&HostPDRHandler::_processFetchPDREvent)),
567 this, nextRecordHandle, std::placeholders::_1));
568 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500569 }
570}
571
572void HostPDRHandler::_processPDRRepoChgEvent(
573 sdeventplus::source::EventBase& /*source */)
574{
575 deferredPDRRepoChgEvent.reset();
576 this->sendPDRRepositoryChgEvent(
577 std::move(std::vector<uint8_t>(1, PLDM_PDR_ENTITY_ASSOCIATION)),
578 FORMAT_IS_PDR_HANDLES);
579}
580
581void HostPDRHandler::_processFetchPDREvent(
582 uint32_t nextRecordHandle, sdeventplus::source::EventBase& /*source */)
583{
584 deferredFetchPDREvent.reset();
585 if (!this->pdrRecordHandles.empty())
586 {
587 nextRecordHandle = this->pdrRecordHandles.front();
588 this->pdrRecordHandles.pop_front();
589 }
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500590 if (isHostPdrModified && (!this->modifiedPDRRecordHandles.empty()))
591 {
592 nextRecordHandle = this->modifiedPDRRecordHandles.front();
593 this->modifiedPDRRecordHandles.pop_front();
594 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500595 this->getHostPDR(nextRecordHandle);
596}
597
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500598void HostPDRHandler::setHostFirmwareCondition()
sampmisr6decfc12021-03-02 11:07:36 +0530599{
sampmisr6decfc12021-03-02 11:07:36 +0530600 responseReceived = false;
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930601 auto instanceId = instanceIdDb.next(mctp_eid);
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500602 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
603 PLDM_GET_VERSION_REQ_BYTES);
604 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
605 auto rc = encode_get_version_req(instanceId, 0, PLDM_GET_FIRSTPART,
606 PLDM_BASE, request);
607 if (rc != PLDM_SUCCESS)
sampmisr6decfc12021-03-02 11:07:36 +0530608 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600609 error("GetPLDMVersion encode failure. PLDM error code = {RC}", "RC",
610 lg2::hex, rc);
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930611 instanceIdDb.free(mctp_eid, instanceId);
sampmisr6decfc12021-03-02 11:07:36 +0530612 return;
613 }
614
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500615 auto getPLDMVersionHandler = [this](mctp_eid_t /*eid*/,
616 const pldm_msg* response,
617 size_t respMsgLen) {
618 if (response == nullptr || !respMsgLen)
sampmisr6decfc12021-03-02 11:07:36 +0530619 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600620 error(
621 "Failed to receive response for getPLDMVersion command, Host seems to be off");
sampmisr6decfc12021-03-02 11:07:36 +0530622 return;
623 }
Riya Dixit49cfb132023-03-02 04:26:53 -0600624 info("Getting the response. PLDM RC = {RC}", "RC", lg2::hex,
625 static_cast<uint16_t>(response->payload[0]));
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500626 this->responseReceived = true;
627 getHostPDR();
sampmisr6decfc12021-03-02 11:07:36 +0530628 };
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500629 rc = handler->registerRequest(mctp_eid, instanceId, PLDM_BASE,
630 PLDM_GET_PLDM_VERSION, std::move(requestMsg),
631 std::move(getPLDMVersionHandler));
632 if (rc)
sampmisr6decfc12021-03-02 11:07:36 +0530633 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600634 error("Failed to discover Host state. Assuming Host as off");
sampmisr6decfc12021-03-02 11:07:36 +0530635 }
636}
637
638bool HostPDRHandler::isHostUp()
639{
640 return responseReceived;
641}
642
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530643void HostPDRHandler::setHostSensorState(const PDRList& stateSensorPDRs)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600644{
645 for (const auto& stateSensorPDR : stateSensorPDRs)
646 {
647 auto pdr = reinterpret_cast<const pldm_state_sensor_pdr*>(
648 stateSensorPDR.data());
649
650 if (!pdr)
651 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600652 error("Failed to get State sensor PDR");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600653 pldm::utils::reportError(
654 "xyz.openbmc_project.bmc.pldm.InternalFailure");
655 return;
656 }
657
658 uint16_t sensorId = pdr->sensor_id;
659
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530660 for (const auto& [terminusHandle, terminusInfo] : tlPDRInfo)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600661 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530662 if (terminusHandle == pdr->terminus_handle)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600663 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530664 if (std::get<2>(terminusInfo) == PLDM_TL_PDR_VALID)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600665 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530666 mctp_eid = std::get<1>(terminusInfo);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600667 }
668
669 bitfield8_t sensorRearm;
670 sensorRearm.byte = 0;
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530671 uint8_t tid = std::get<0>(terminusInfo);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600672
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930673 auto instanceId = instanceIdDb.next(mctp_eid);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600674 std::vector<uint8_t> requestMsg(
675 sizeof(pldm_msg_hdr) +
676 PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES);
677 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
678 auto rc = encode_get_state_sensor_readings_req(
679 instanceId, sensorId, sensorRearm, 0, request);
680
681 if (rc != PLDM_SUCCESS)
682 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930683 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -0600684 error(
685 "Failed to encode_get_state_sensor_readings_req, rc = {RC}",
686 "RC", rc);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600687 pldm::utils::reportError(
688 "xyz.openbmc_project.bmc.pldm.InternalFailure");
689 return;
690 }
691
Patrick Williams6da4f912023-05-10 07:50:53 -0500692 auto getStateSensorReadingRespHandler =
693 [=, this](mctp_eid_t /*eid*/, const pldm_msg* response,
694 size_t respMsgLen) {
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600695 if (response == nullptr || !respMsgLen)
696 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600697 error(
698 "Failed to receive response for getStateSensorReading command");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600699 return;
700 }
701 std::array<get_sensor_state_field, 8> stateField{};
702 uint8_t completionCode = 0;
703 uint8_t comp_sensor_count = 0;
704
705 auto rc = decode_get_state_sensor_readings_resp(
706 response, respMsgLen, &completionCode,
707 &comp_sensor_count, stateField.data());
708
709 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
710 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600711 error(
712 "Failed to decode_get_state_sensor_readings_resp, rc = {RC} cc = {CC}",
713 "RC", rc, "CC",
714 static_cast<unsigned>(completionCode));
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600715 pldm::utils::reportError(
716 "xyz.openbmc_project.bmc.pldm.InternalFailure");
717 }
718
719 uint8_t eventState;
720 uint8_t previousEventState;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600721
RIYA DIXITbb5fda42022-09-27 06:48:08 -0500722 for (uint8_t sensorOffset = 0;
723 sensorOffset < comp_sensor_count; sensorOffset++)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600724 {
RIYA DIXITbb5fda42022-09-27 06:48:08 -0500725 eventState = stateField[sensorOffset].present_state;
726 previousEventState =
727 stateField[sensorOffset].previous_state;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600728
729 emitStateSensorEventSignal(tid, sensorId, sensorOffset,
730 eventState,
731 previousEventState);
732
733 SensorEntry sensorEntry{tid, sensorId};
734
735 pldm::pdr::EntityInfo entityInfo{};
736 pldm::pdr::CompositeSensorStates
737 compositeSensorStates{};
738
739 try
740 {
741 std::tie(entityInfo, compositeSensorStates) =
742 lookupSensorInfo(sensorEntry);
743 }
744 catch (const std::out_of_range& e)
745 {
746 try
747 {
748 sensorEntry.terminusID = PLDM_TID_RESERVED;
749 std::tie(entityInfo, compositeSensorStates) =
750 lookupSensorInfo(sensorEntry);
751 }
752 catch (const std::out_of_range& e)
753 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600754 error("No mapping for the events");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600755 }
756 }
757
758 if (sensorOffset > compositeSensorStates.size())
759 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600760 error("Error Invalid data, Invalid sensor offset");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600761 return;
762 }
763
764 const auto& possibleStates =
765 compositeSensorStates[sensorOffset];
766 if (possibleStates.find(eventState) ==
767 possibleStates.end())
768 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600769 error("Error invalid_data, Invalid event state");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600770 return;
771 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500772 const auto& [containerId, entityType,
773 entityInstance] = entityInfo;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600774 pldm::responder::events::StateSensorEntry
775 stateSensorEntry{containerId, entityType,
776 entityInstance, sensorOffset};
777 handleStateSensorEvent(stateSensorEntry, eventState);
778 }
779 };
780
781 rc = handler->registerRequest(
782 mctp_eid, instanceId, PLDM_PLATFORM,
783 PLDM_GET_STATE_SENSOR_READINGS, std::move(requestMsg),
784 std::move(getStateSensorReadingRespHandler));
785
786 if (rc != PLDM_SUCCESS)
787 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600788 error(
789 "Failed to send request to get State sensor reading on Host");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600790 }
791 }
792 }
793 }
794}
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500795} // namespace pldm