blob: cedebb0f662beb75460b6fdc83eced465752baeb [file] [log] [blame]
Deepak Kodihalli87514cc2020-04-16 09:08:38 -05001#include "config.h"
2
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05003#include "host_pdr_handler.hpp"
4
Pavithra Barithayae8beb892020-04-14 23:24:25 -05005#include <assert.h>
George Liuc453e162022-12-21 17:16:23 +08006#include <libpldm/pldm.h>
Pavithra Barithayae8beb892020-04-14 23:24:25 -05007
Deepak Kodihalli87514cc2020-04-16 09:08:38 -05008#include <nlohmann/json.hpp>
Riya Dixit49cfb132023-03-02 04:26:53 -06009#include <phosphor-logging/lg2.hpp>
sampmisr6decfc12021-03-02 11:07:36 +053010#include <sdeventplus/clock.hpp>
11#include <sdeventplus/exception.hpp>
12#include <sdeventplus/source/io.hpp>
13#include <sdeventplus/source/time.hpp>
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050014
George Liu6492f522020-06-16 10:34:05 +080015#include <fstream>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050016
Riya Dixit49cfb132023-03-02 04:26:53 -060017PHOSPHOR_LOG2_USING;
18
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050019namespace pldm
20{
Brad Bishop5079ac42021-08-19 18:35:06 -040021using namespace pldm::dbus_api;
22using namespace pldm::responder::events;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050023using namespace pldm::utils;
24using namespace sdbusplus::bus::match::rules;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050025using Json = nlohmann::json;
26namespace fs = std::filesystem;
27constexpr auto fruJson = "host_frus.json";
28const Json emptyJson{};
29const std::vector<Json> emptyJsonList{};
30
Manojkiran Eda3ca40452021-10-04 22:51:37 +053031template <typename T>
32uint16_t extractTerminusHandle(std::vector<uint8_t>& pdr)
33{
34 T* var = nullptr;
35 if (std::is_same<T, pldm_pdr_fru_record_set>::value)
36 {
37 var = (T*)(pdr.data() + sizeof(pldm_pdr_hdr));
38 }
39 else
40 {
41 var = (T*)(pdr.data());
42 }
43 if (var != nullptr)
44 {
45 return var->terminus_handle;
46 }
47 return TERMINUS_HANDLE;
48}
49
Tom Joseph74f27c72021-05-16 07:58:53 -070050HostPDRHandler::HostPDRHandler(
51 int mctp_fd, uint8_t mctp_eid, sdeventplus::Event& event, pldm_pdr* repo,
52 const std::string& eventsJsonsDir, pldm_entity_association_tree* entityTree,
53 pldm_entity_association_tree* bmcEntityTree, Requester& requester,
Tom Josephe5268cd2021-09-07 13:04:03 +053054 pldm::requester::Handler<pldm::requester::Request>* handler) :
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050055 mctp_fd(mctp_fd),
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060056 mctp_eid(mctp_eid), event(event), repo(repo),
57 stateSensorHandler(eventsJsonsDir), entityTree(entityTree),
Tom Josephe5268cd2021-09-07 13:04:03 +053058 bmcEntityTree(bmcEntityTree), requester(requester), handler(handler)
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050059{
60 fs::path hostFruJson(fs::path(HOST_JSONS_DIR) / fruJson);
61 if (fs::exists(hostFruJson))
62 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -050063 // Note parent entities for entities sent down by the host firmware.
64 // This will enable a merge of entity associations.
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050065 try
66 {
67 std::ifstream jsonFile(hostFruJson);
68 auto data = Json::parse(jsonFile, nullptr, false);
69 if (data.is_discarded())
70 {
Riya Dixit49cfb132023-03-02 04:26:53 -060071 error("Parsing Host FRU json file failed");
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050072 }
73 else
74 {
75 auto entities = data.value("entities", emptyJsonList);
76 for (auto& entity : entities)
77 {
78 EntityType entityType = entity.value("entity_type", 0);
79 auto parent = entity.value("parent", emptyJson);
80 pldm_entity p{};
81 p.entity_type = parent.value("entity_type", 0);
82 p.entity_instance_num = parent.value("entity_instance", 0);
83 parents.emplace(entityType, std::move(p));
84 }
85 }
86 }
87 catch (const std::exception& e)
88 {
Riya Dixit49cfb132023-03-02 04:26:53 -060089 error("Parsing Host FRU json file failed, exception = {ERR_EXCEP}",
90 "ERR_EXCEP", e.what());
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050091 }
92 }
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050093
Patrick Williams84b790c2022-07-22 19:26:56 -050094 hostOffMatch = std::make_unique<sdbusplus::bus::match_t>(
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050095 pldm::utils::DBusHandler::getBus(),
96 propertiesChanged("/xyz/openbmc_project/state/host0",
97 "xyz.openbmc_project.State.Host"),
Patrick Williams84b790c2022-07-22 19:26:56 -050098 [this, repo, entityTree, bmcEntityTree](sdbusplus::message_t& msg) {
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050099 DbusChangedProps props{};
100 std::string intf;
101 msg.read(intf, props);
102 const auto itr = props.find("CurrentHostState");
103 if (itr != props.end())
104 {
105 PropertyValue value = itr->second;
106 auto propVal = std::get<std::string>(value);
107 if (propVal == "xyz.openbmc_project.State.Host.HostState.Off")
108 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530109 // Delete all the remote terminus information
Manojkiran Eda8fee19a2021-11-15 12:07:44 +0530110 std::erase_if(tlPDRInfo, [](const auto& item) {
111 auto const& [key, value] = item;
112 return key != TERMINUS_HANDLE;
113 });
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500114 pldm_pdr_remove_remote_pdrs(repo);
Sampa Misrac073a202021-05-08 10:56:05 -0500115 pldm_entity_association_tree_destroy_root(entityTree);
116 pldm_entity_association_tree_copy_root(bmcEntityTree,
117 entityTree);
Tom Josephb4268602020-04-17 17:20:45 +0530118 this->sensorMap.clear();
Pavithra Barithaya23025b92021-09-28 05:22:52 -0500119 this->responseReceived = false;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500120 }
121 }
122 });
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500123}
124
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500125void HostPDRHandler::fetchPDR(PDRRecordHandles&& recordHandles)
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500126{
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500127 pdrRecordHandles.clear();
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500128 modifiedPDRRecordHandles.clear();
129
130 if (isHostPdrModified)
131 {
132 modifiedPDRRecordHandles = std::move(recordHandles);
133 }
134 else
135 {
136 pdrRecordHandles = std::move(recordHandles);
137 }
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500138
139 // Defer the actual fetch of PDRs from the host (by queuing the call on the
140 // main event loop). That way, we can respond to the platform event msg from
141 // the host firmware.
142 pdrFetchEvent = std::make_unique<sdeventplus::source::Defer>(
143 event, std::bind(std::mem_fn(&HostPDRHandler::_fetchPDR), this,
144 std::placeholders::_1));
145}
146
147void HostPDRHandler::_fetchPDR(sdeventplus::source::EventBase& /*source*/)
148{
Sampa Misrac0c79482021-06-02 08:01:54 -0500149 getHostPDR();
150}
151
152void HostPDRHandler::getHostPDR(uint32_t nextRecordHandle)
153{
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500154 pdrFetchEvent.reset();
155
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500156 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
157 PLDM_GET_PDR_REQ_BYTES);
158 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500159 uint32_t recordHandle{};
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500160 if (!nextRecordHandle && (!modifiedPDRRecordHandles.empty()) &&
161 isHostPdrModified)
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500162 {
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500163 recordHandle = modifiedPDRRecordHandles.front();
164 modifiedPDRRecordHandles.pop_front();
165 }
166 else if (!nextRecordHandle && (!pdrRecordHandles.empty()))
167 {
168 recordHandle = pdrRecordHandles.front();
169 pdrRecordHandles.pop_front();
Sampa Misrac0c79482021-06-02 08:01:54 -0500170 }
171 else
172 {
173 recordHandle = nextRecordHandle;
174 }
175 auto instanceId = requester.getInstanceId(mctp_eid);
176
177 auto rc =
178 encode_get_pdr_req(instanceId, recordHandle, 0, PLDM_GET_FIRSTPART,
179 UINT16_MAX, 0, request, PLDM_GET_PDR_REQ_BYTES);
180 if (rc != PLDM_SUCCESS)
181 {
182 requester.markFree(mctp_eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -0600183 error("Failed to encode_get_pdr_req, rc = {RC}", "RC", rc);
Sampa Misrac0c79482021-06-02 08:01:54 -0500184 return;
185 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500186
Sampa Misrac0c79482021-06-02 08:01:54 -0500187 rc = handler->registerRequest(
188 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_GET_PDR,
189 std::move(requestMsg),
190 std::move(std::bind_front(&HostPDRHandler::processHostPDRs, this)));
191 if (rc)
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500192 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600193 error("Failed to send the GetPDR request to Host");
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500194 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500195}
196
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600197int HostPDRHandler::handleStateSensorEvent(const StateSensorEntry& entry,
198 pdr::EventState state)
199{
200 auto rc = stateSensorHandler.eventAction(entry, state);
201 if (rc != PLDM_SUCCESS)
202 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600203 error("Failed to fetch and update D-bus property, rc = {RC}", "RC", rc);
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600204 return rc;
205 }
206 return PLDM_SUCCESS;
207}
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500208bool HostPDRHandler::getParent(EntityType type, pldm_entity& parent)
209{
210 auto found = parents.find(type);
211 if (found != parents.end())
212 {
213 parent.entity_type = found->second.entity_type;
214 parent.entity_instance_num = found->second.entity_instance_num;
215 return true;
216 }
217
218 return false;
219}
220
221void HostPDRHandler::mergeEntityAssociations(const std::vector<uint8_t>& pdr)
222{
223 size_t numEntities{};
224 pldm_entity* entities = nullptr;
225 bool merged = false;
226 auto entityPdr = reinterpret_cast<pldm_pdr_entity_association*>(
227 const_cast<uint8_t*>(pdr.data()) + sizeof(pldm_pdr_hdr));
228
229 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &numEntities,
230 &entities);
231 for (size_t i = 0; i < numEntities; ++i)
232 {
233 pldm_entity parent{};
234 if (getParent(entities[i].entity_type, parent))
235 {
236 auto node = pldm_entity_association_tree_find(entityTree, &parent);
237 if (node)
238 {
George Liu64a8f0f2021-06-12 10:56:11 +0800239 pldm_entity_association_tree_add(entityTree, &entities[i],
240 0xFFFF, node,
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500241 entityPdr->association_type);
242 merged = true;
243 }
244 }
245 }
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500246
247 if (merged)
248 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500249 // Update our PDR repo with the merged entity association PDRs
Sampa Misra719ed392021-06-04 05:15:13 -0500250 pldm_entity_node* node = nullptr;
251 pldm_find_entity_ref_in_tree(entityTree, entities[0], &node);
252 if (node == nullptr)
253 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600254 error("could not find referrence of the entity in the tree");
Sampa Misra719ed392021-06-04 05:15:13 -0500255 }
256 else
257 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530258 pldm_entity_association_pdr_add_from_node(
259 node, repo, &entities, numEntities, true, TERMINUS_HANDLE);
Sampa Misra719ed392021-06-04 05:15:13 -0500260 }
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500261 }
Sampa Misra719ed392021-06-04 05:15:13 -0500262 free(entities);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500263}
264
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500265void HostPDRHandler::sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
266 uint8_t eventDataFormat)
267{
268 assert(eventDataFormat == FORMAT_IS_PDR_HANDLES);
269
270 // Extract from the PDR repo record handles of PDRs we want the host
271 // to pull up.
272 std::vector<uint8_t> eventDataOps{PLDM_RECORDS_ADDED};
273 std::vector<uint8_t> numsOfChangeEntries(1);
274 std::vector<std::vector<ChangeEntry>> changeEntries(
275 numsOfChangeEntries.size());
276 for (auto pdrType : pdrTypes)
277 {
278 const pldm_pdr_record* record{};
279 do
280 {
281 record = pldm_pdr_find_record_by_type(repo, pdrType, record,
282 nullptr, nullptr);
283 if (record && pldm_pdr_record_is_remote(record))
284 {
285 changeEntries[0].push_back(
286 pldm_pdr_get_record_handle(repo, record));
287 }
288 } while (record);
289 }
290 if (changeEntries.empty())
291 {
292 return;
293 }
294 numsOfChangeEntries[0] = changeEntries[0].size();
295
296 // Encode PLDM platform event msg to indicate a PDR repo change.
297 size_t maxSize = PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH +
298 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH +
299 changeEntries[0].size() * sizeof(uint32_t);
300 std::vector<uint8_t> eventDataVec{};
301 eventDataVec.resize(maxSize);
302 auto eventData =
303 reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>(
304 eventDataVec.data());
305 size_t actualSize{};
306 auto firstEntry = changeEntries[0].data();
307 auto rc = encode_pldm_pdr_repository_chg_event_data(
308 eventDataFormat, 1, eventDataOps.data(), numsOfChangeEntries.data(),
309 &firstEntry, eventData, &actualSize, maxSize);
310 if (rc != PLDM_SUCCESS)
311 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600312 error("Failed to encode_pldm_pdr_repository_chg_event_data, rc = {RC}",
313 "RC", rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500314 return;
315 }
316 auto instanceId = requester.getInstanceId(mctp_eid);
317 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
318 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
319 actualSize);
320 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
321 rc = encode_platform_event_message_req(
ArchanaKakani6c39c7a2022-12-05 04:36:35 -0600322 instanceId, 1, TERMINUS_ID, PLDM_PDR_REPOSITORY_CHG_EVENT,
323 eventDataVec.data(), actualSize, request,
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500324 actualSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500325 if (rc != PLDM_SUCCESS)
326 {
327 requester.markFree(mctp_eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -0600328 error("Failed to encode_platform_event_message_req, rc = {RC}", "RC",
329 rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500330 return;
331 }
332
Tom Joseph74f27c72021-05-16 07:58:53 -0700333 auto platformEventMessageResponseHandler = [](mctp_eid_t /*eid*/,
334 const pldm_msg* response,
335 size_t respMsgLen) {
336 if (response == nullptr || !respMsgLen)
337 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600338 error(
339 "Failed to receive response for the PDR repository changed event");
Tom Joseph74f27c72021-05-16 07:58:53 -0700340 return;
341 }
342
343 uint8_t completionCode{};
344 uint8_t status{};
345 auto responsePtr = reinterpret_cast<const struct pldm_msg*>(response);
Pavithra Barithaya54b5a562021-09-27 06:07:10 -0500346 auto rc = decode_platform_event_message_resp(responsePtr, respMsgLen,
347 &completionCode, &status);
Sampa Misrac0c79482021-06-02 08:01:54 -0500348 if (rc || completionCode)
Tom Joseph74f27c72021-05-16 07:58:53 -0700349 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600350 error(
351 "Failed to decode_platform_event_message_resp: {RC}, cc = {CC}",
352 "RC", rc, "CC", static_cast<unsigned>(completionCode));
Tom Joseph74f27c72021-05-16 07:58:53 -0700353 }
354 };
355
Sampa Misrac0c79482021-06-02 08:01:54 -0500356 rc = handler->registerRequest(
Sampa Misra626c5652021-08-11 10:28:48 -0500357 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
Tom Joseph74f27c72021-05-16 07:58:53 -0700358 std::move(requestMsg), std::move(platformEventMessageResponseHandler));
Sampa Misrac0c79482021-06-02 08:01:54 -0500359 if (rc)
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500360 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600361 error("Failed to send the PDR repository changed event request");
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500362 }
363}
364
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530365void HostPDRHandler::parseStateSensorPDRs(const PDRList& stateSensorPDRs)
Sampa Misra868c8792020-05-26 03:12:13 -0500366{
367 for (const auto& pdr : stateSensorPDRs)
368 {
369 SensorEntry sensorEntry{};
370 const auto& [terminusHandle, sensorID, sensorInfo] =
371 responder::pdr_utils::parseStateSensorPDR(pdr);
372 sensorEntry.sensorID = sensorID;
373 try
374 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530375 sensorEntry.terminusID = std::get<0>(tlPDRInfo.at(terminusHandle));
Sampa Misra868c8792020-05-26 03:12:13 -0500376 }
377 // If there is no mapping for terminusHandle assign the reserved TID
378 // value of 0xFF to indicate that.
379 catch (const std::out_of_range& e)
380 {
381 sensorEntry.terminusID = PLDM_TID_RESERVED;
382 }
383 sensorMap.emplace(sensorEntry, std::move(sensorInfo));
384 }
385}
386
Sampa Misrac0c79482021-06-02 08:01:54 -0500387void HostPDRHandler::processHostPDRs(mctp_eid_t /*eid*/,
388 const pldm_msg* response,
389 size_t respMsgLen)
390{
391 static bool merged = false;
392 static PDRList stateSensorPDRs{};
Sampa Misrac0c79482021-06-02 08:01:54 -0500393 uint32_t nextRecordHandle{};
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600394 uint8_t tlEid = 0;
395 bool tlValid = true;
396 uint32_t rh = 0;
397 uint16_t terminusHandle = 0;
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530398 uint16_t pdrTerminusHandle = 0;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600399 uint8_t tid = 0;
400
Sampa Misrac0c79482021-06-02 08:01:54 -0500401 uint8_t completionCode{};
402 uint32_t nextDataTransferHandle{};
403 uint8_t transferFlag{};
404 uint16_t respCount{};
405 uint8_t transferCRC{};
406 if (response == nullptr || !respMsgLen)
407 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600408 error("Failed to receive response for the GetPDR command");
Sampa Misrac0c79482021-06-02 08:01:54 -0500409 return;
410 }
411
412 auto rc = decode_get_pdr_resp(
413 response, respMsgLen /*- sizeof(pldm_msg_hdr)*/, &completionCode,
414 &nextRecordHandle, &nextDataTransferHandle, &transferFlag, &respCount,
415 nullptr, 0, &transferCRC);
416 std::vector<uint8_t> responsePDRMsg;
417 responsePDRMsg.resize(respMsgLen + sizeof(pldm_msg_hdr));
418 memcpy(responsePDRMsg.data(), response, respMsgLen + sizeof(pldm_msg_hdr));
Sampa Misrac0c79482021-06-02 08:01:54 -0500419 if (rc != PLDM_SUCCESS)
420 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600421 error("Failed to decode_get_pdr_resp, rc = {RC}", "RC", rc);
Sampa Misrac0c79482021-06-02 08:01:54 -0500422 return;
423 }
424 else
425 {
426 std::vector<uint8_t> pdr(respCount, 0);
427 rc = decode_get_pdr_resp(response, respMsgLen, &completionCode,
428 &nextRecordHandle, &nextDataTransferHandle,
429 &transferFlag, &respCount, pdr.data(),
430 respCount, &transferCRC);
431 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
432 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600433 error("Failed to decode_get_pdr_resp: rc = {RC}, cc = {CC}", "RC",
434 rc, "CC", static_cast<unsigned>(completionCode));
Sampa Misrac0c79482021-06-02 08:01:54 -0500435 return;
436 }
437 else
438 {
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600439 // when nextRecordHandle is 0, we need the recordHandle of the last
440 // PDR and not 0-1.
441 if (!nextRecordHandle)
442 {
443 rh = nextRecordHandle;
444 }
445 else
446 {
447 rh = nextRecordHandle - 1;
448 }
449
Sampa Misrac0c79482021-06-02 08:01:54 -0500450 auto pdrHdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600451 if (!rh)
452 {
453 rh = pdrHdr->record_handle;
454 }
455
Sampa Misrac0c79482021-06-02 08:01:54 -0500456 if (pdrHdr->type == PLDM_PDR_ENTITY_ASSOCIATION)
457 {
458 this->mergeEntityAssociations(pdr);
459 merged = true;
460 }
461 else
462 {
463 if (pdrHdr->type == PLDM_TERMINUS_LOCATOR_PDR)
464 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530465 pdrTerminusHandle =
466 extractTerminusHandle<pldm_terminus_locator_pdr>(pdr);
Sampa Misrac0c79482021-06-02 08:01:54 -0500467 auto tlpdr =
468 reinterpret_cast<const pldm_terminus_locator_pdr*>(
469 pdr.data());
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600470
471 terminusHandle = tlpdr->terminus_handle;
472 tid = tlpdr->tid;
473 auto terminus_locator_type = tlpdr->terminus_locator_type;
474 if (terminus_locator_type ==
475 PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID)
476 {
477 auto locatorValue = reinterpret_cast<
478 const pldm_terminus_locator_type_mctp_eid*>(
479 tlpdr->terminus_locator_value);
480 tlEid = static_cast<uint8_t>(locatorValue->eid);
481 }
482 if (tlpdr->validity == 0)
483 {
484 tlValid = false;
485 }
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500486 for (const auto& terminusMap : tlPDRInfo)
487 {
488 if ((terminusHandle == (terminusMap.first)) &&
489 (get<1>(terminusMap.second) == tlEid) &&
490 (get<2>(terminusMap.second) == tlpdr->validity))
491 {
492 // TL PDR already present with same validity don't
493 // add the PDR to the repo just return
494 return;
495 }
496 }
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530497 tlPDRInfo.insert_or_assign(
498 tlpdr->terminus_handle,
499 std::make_tuple(tlpdr->tid, tlEid, tlpdr->validity));
Sampa Misrac0c79482021-06-02 08:01:54 -0500500 }
501 else if (pdrHdr->type == PLDM_STATE_SENSOR_PDR)
502 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530503 pdrTerminusHandle =
504 extractTerminusHandle<pldm_state_sensor_pdr>(pdr);
Sampa Misrac0c79482021-06-02 08:01:54 -0500505 stateSensorPDRs.emplace_back(pdr);
506 }
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530507 else if (pdrHdr->type == PLDM_PDR_FRU_RECORD_SET)
508 {
509 pdrTerminusHandle =
510 extractTerminusHandle<pldm_pdr_fru_record_set>(pdr);
511 }
512 else if (pdrHdr->type == PLDM_STATE_EFFECTER_PDR)
513 {
514 pdrTerminusHandle =
515 extractTerminusHandle<pldm_state_effecter_pdr>(pdr);
516 }
517 else if (pdrHdr->type == PLDM_NUMERIC_EFFECTER_PDR)
518 {
519 pdrTerminusHandle =
520 extractTerminusHandle<pldm_numeric_effecter_value_pdr>(
521 pdr);
522 }
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600523 // if the TLPDR is invalid update the repo accordingly
524 if (!tlValid)
525 {
526 pldm_pdr_update_TL_pdr(repo, terminusHandle, tid, tlEid,
527 tlValid);
528 }
529 else
530 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530531 pldm_pdr_add(repo, pdr.data(), respCount, rh, true,
532 pdrTerminusHandle);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600533 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500534 }
535 }
536 }
537 if (!nextRecordHandle)
538 {
539 /*received last record*/
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530540 this->parseStateSensorPDRs(stateSensorPDRs);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600541 if (isHostUp())
542 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530543 this->setHostSensorState(stateSensorPDRs);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600544 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500545 stateSensorPDRs.clear();
Sampa Misrac0c79482021-06-02 08:01:54 -0500546 if (merged)
547 {
548 merged = false;
549 deferredPDRRepoChgEvent =
550 std::make_unique<sdeventplus::source::Defer>(
551 event,
552 std::bind(
553 std::mem_fn((&HostPDRHandler::_processPDRRepoChgEvent)),
554 this, std::placeholders::_1));
555 }
556 }
557 else
558 {
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500559 if (modifiedPDRRecordHandles.empty() && isHostPdrModified)
560 {
561 isHostPdrModified = false;
562 }
563 else
564 {
565 deferredFetchPDREvent =
566 std::make_unique<sdeventplus::source::Defer>(
567 event,
568 std::bind(
569 std::mem_fn((&HostPDRHandler::_processFetchPDREvent)),
570 this, nextRecordHandle, std::placeholders::_1));
571 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500572 }
573}
574
575void HostPDRHandler::_processPDRRepoChgEvent(
576 sdeventplus::source::EventBase& /*source */)
577{
578 deferredPDRRepoChgEvent.reset();
579 this->sendPDRRepositoryChgEvent(
580 std::move(std::vector<uint8_t>(1, PLDM_PDR_ENTITY_ASSOCIATION)),
581 FORMAT_IS_PDR_HANDLES);
582}
583
584void HostPDRHandler::_processFetchPDREvent(
585 uint32_t nextRecordHandle, sdeventplus::source::EventBase& /*source */)
586{
587 deferredFetchPDREvent.reset();
588 if (!this->pdrRecordHandles.empty())
589 {
590 nextRecordHandle = this->pdrRecordHandles.front();
591 this->pdrRecordHandles.pop_front();
592 }
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500593 if (isHostPdrModified && (!this->modifiedPDRRecordHandles.empty()))
594 {
595 nextRecordHandle = this->modifiedPDRRecordHandles.front();
596 this->modifiedPDRRecordHandles.pop_front();
597 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500598 this->getHostPDR(nextRecordHandle);
599}
600
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500601void HostPDRHandler::setHostFirmwareCondition()
sampmisr6decfc12021-03-02 11:07:36 +0530602{
sampmisr6decfc12021-03-02 11:07:36 +0530603 responseReceived = false;
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500604 auto instanceId = requester.getInstanceId(mctp_eid);
605 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
606 PLDM_GET_VERSION_REQ_BYTES);
607 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
608 auto rc = encode_get_version_req(instanceId, 0, PLDM_GET_FIRSTPART,
609 PLDM_BASE, request);
610 if (rc != PLDM_SUCCESS)
sampmisr6decfc12021-03-02 11:07:36 +0530611 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600612 error("GetPLDMVersion encode failure. PLDM error code = {RC}", "RC",
613 lg2::hex, rc);
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500614 requester.markFree(mctp_eid, instanceId);
sampmisr6decfc12021-03-02 11:07:36 +0530615 return;
616 }
617
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500618 auto getPLDMVersionHandler = [this](mctp_eid_t /*eid*/,
619 const pldm_msg* response,
620 size_t respMsgLen) {
621 if (response == nullptr || !respMsgLen)
sampmisr6decfc12021-03-02 11:07:36 +0530622 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600623 error(
624 "Failed to receive response for getPLDMVersion command, Host seems to be off");
sampmisr6decfc12021-03-02 11:07:36 +0530625 return;
626 }
Riya Dixit49cfb132023-03-02 04:26:53 -0600627 info("Getting the response. PLDM RC = {RC}", "RC", lg2::hex,
628 static_cast<uint16_t>(response->payload[0]));
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500629 this->responseReceived = true;
630 getHostPDR();
sampmisr6decfc12021-03-02 11:07:36 +0530631 };
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500632 rc = handler->registerRequest(mctp_eid, instanceId, PLDM_BASE,
633 PLDM_GET_PLDM_VERSION, std::move(requestMsg),
634 std::move(getPLDMVersionHandler));
635 if (rc)
sampmisr6decfc12021-03-02 11:07:36 +0530636 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600637 error("Failed to discover Host state. Assuming Host as off");
sampmisr6decfc12021-03-02 11:07:36 +0530638 }
639}
640
641bool HostPDRHandler::isHostUp()
642{
643 return responseReceived;
644}
645
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530646void HostPDRHandler::setHostSensorState(const PDRList& stateSensorPDRs)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600647{
648 for (const auto& stateSensorPDR : stateSensorPDRs)
649 {
650 auto pdr = reinterpret_cast<const pldm_state_sensor_pdr*>(
651 stateSensorPDR.data());
652
653 if (!pdr)
654 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600655 error("Failed to get State sensor PDR");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600656 pldm::utils::reportError(
657 "xyz.openbmc_project.bmc.pldm.InternalFailure");
658 return;
659 }
660
661 uint16_t sensorId = pdr->sensor_id;
662
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530663 for (const auto& [terminusHandle, terminusInfo] : tlPDRInfo)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600664 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530665 if (terminusHandle == pdr->terminus_handle)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600666 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530667 if (std::get<2>(terminusInfo) == PLDM_TL_PDR_VALID)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600668 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530669 mctp_eid = std::get<1>(terminusInfo);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600670 }
671
672 bitfield8_t sensorRearm;
673 sensorRearm.byte = 0;
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530674 uint8_t tid = std::get<0>(terminusInfo);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600675
676 auto instanceId = requester.getInstanceId(mctp_eid);
677 std::vector<uint8_t> requestMsg(
678 sizeof(pldm_msg_hdr) +
679 PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES);
680 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
681 auto rc = encode_get_state_sensor_readings_req(
682 instanceId, sensorId, sensorRearm, 0, request);
683
684 if (rc != PLDM_SUCCESS)
685 {
686 requester.markFree(mctp_eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -0600687 error(
688 "Failed to encode_get_state_sensor_readings_req, rc = {RC}",
689 "RC", rc);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600690 pldm::utils::reportError(
691 "xyz.openbmc_project.bmc.pldm.InternalFailure");
692 return;
693 }
694
695 auto getStateSensorReadingRespHandler = [=, this](
696 mctp_eid_t /*eid*/,
697 const pldm_msg*
698 response,
699 size_t respMsgLen) {
700 if (response == nullptr || !respMsgLen)
701 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600702 error(
703 "Failed to receive response for getStateSensorReading command");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600704 return;
705 }
706 std::array<get_sensor_state_field, 8> stateField{};
707 uint8_t completionCode = 0;
708 uint8_t comp_sensor_count = 0;
709
710 auto rc = decode_get_state_sensor_readings_resp(
711 response, respMsgLen, &completionCode,
712 &comp_sensor_count, stateField.data());
713
714 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
715 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600716 error(
717 "Failed to decode_get_state_sensor_readings_resp, rc = {RC} cc = {CC}",
718 "RC", rc, "CC",
719 static_cast<unsigned>(completionCode));
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600720 pldm::utils::reportError(
721 "xyz.openbmc_project.bmc.pldm.InternalFailure");
722 }
723
724 uint8_t eventState;
725 uint8_t previousEventState;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600726
RIYA DIXITbb5fda42022-09-27 06:48:08 -0500727 for (uint8_t sensorOffset = 0;
728 sensorOffset < comp_sensor_count; sensorOffset++)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600729 {
RIYA DIXITbb5fda42022-09-27 06:48:08 -0500730 eventState = stateField[sensorOffset].present_state;
731 previousEventState =
732 stateField[sensorOffset].previous_state;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600733
734 emitStateSensorEventSignal(tid, sensorId, sensorOffset,
735 eventState,
736 previousEventState);
737
738 SensorEntry sensorEntry{tid, sensorId};
739
740 pldm::pdr::EntityInfo entityInfo{};
741 pldm::pdr::CompositeSensorStates
742 compositeSensorStates{};
743
744 try
745 {
746 std::tie(entityInfo, compositeSensorStates) =
747 lookupSensorInfo(sensorEntry);
748 }
749 catch (const std::out_of_range& e)
750 {
751 try
752 {
753 sensorEntry.terminusID = PLDM_TID_RESERVED;
754 std::tie(entityInfo, compositeSensorStates) =
755 lookupSensorInfo(sensorEntry);
756 }
757 catch (const std::out_of_range& e)
758 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600759 error("No mapping for the events");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600760 }
761 }
762
763 if (sensorOffset > compositeSensorStates.size())
764 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600765 error("Error Invalid data, Invalid sensor offset");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600766 return;
767 }
768
769 const auto& possibleStates =
770 compositeSensorStates[sensorOffset];
771 if (possibleStates.find(eventState) ==
772 possibleStates.end())
773 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600774 error("Error invalid_data, Invalid event state");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600775 return;
776 }
777 const auto& [containerId, entityType, entityInstance] =
778 entityInfo;
779 pldm::responder::events::StateSensorEntry
780 stateSensorEntry{containerId, entityType,
781 entityInstance, sensorOffset};
782 handleStateSensorEvent(stateSensorEntry, eventState);
783 }
784 };
785
786 rc = handler->registerRequest(
787 mctp_eid, instanceId, PLDM_PLATFORM,
788 PLDM_GET_STATE_SENSOR_READINGS, std::move(requestMsg),
789 std::move(getStateSensorReadingRespHandler));
790
791 if (rc != PLDM_SUCCESS)
792 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600793 error(
794 "Failed to send request to get State sensor reading on Host");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600795 }
796 }
797 }
798 }
799}
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500800} // namespace pldm