blob: 1913f0bb05ddb42a38e0120315e2cccad3a8ca0d [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 {
Andrew Jeffery1fd3c512023-07-03 13:02:03 +0930256 int rc = pldm_entity_association_pdr_add_from_node_check(
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530257 node, repo, &entities, numEntities, true, TERMINUS_HANDLE);
Andrew Jeffery1fd3c512023-07-03 13:02:03 +0930258 if (rc)
259 {
260 error(
261 "Failed to add entity association PDR from node: {LIBPLDM_ERROR}",
262 "LIBPLDM_ERROR", rc);
263 }
Sampa Misra719ed392021-06-04 05:15:13 -0500264 }
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500265 }
Sampa Misra719ed392021-06-04 05:15:13 -0500266 free(entities);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500267}
268
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500269void HostPDRHandler::sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
270 uint8_t eventDataFormat)
271{
272 assert(eventDataFormat == FORMAT_IS_PDR_HANDLES);
273
274 // Extract from the PDR repo record handles of PDRs we want the host
275 // to pull up.
276 std::vector<uint8_t> eventDataOps{PLDM_RECORDS_ADDED};
277 std::vector<uint8_t> numsOfChangeEntries(1);
278 std::vector<std::vector<ChangeEntry>> changeEntries(
279 numsOfChangeEntries.size());
280 for (auto pdrType : pdrTypes)
281 {
282 const pldm_pdr_record* record{};
283 do
284 {
285 record = pldm_pdr_find_record_by_type(repo, pdrType, record,
286 nullptr, nullptr);
287 if (record && pldm_pdr_record_is_remote(record))
288 {
289 changeEntries[0].push_back(
290 pldm_pdr_get_record_handle(repo, record));
291 }
292 } while (record);
293 }
294 if (changeEntries.empty())
295 {
296 return;
297 }
298 numsOfChangeEntries[0] = changeEntries[0].size();
299
300 // Encode PLDM platform event msg to indicate a PDR repo change.
301 size_t maxSize = PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH +
302 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH +
303 changeEntries[0].size() * sizeof(uint32_t);
304 std::vector<uint8_t> eventDataVec{};
305 eventDataVec.resize(maxSize);
306 auto eventData =
307 reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>(
308 eventDataVec.data());
309 size_t actualSize{};
310 auto firstEntry = changeEntries[0].data();
311 auto rc = encode_pldm_pdr_repository_chg_event_data(
312 eventDataFormat, 1, eventDataOps.data(), numsOfChangeEntries.data(),
313 &firstEntry, eventData, &actualSize, maxSize);
314 if (rc != PLDM_SUCCESS)
315 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600316 error("Failed to encode_pldm_pdr_repository_chg_event_data, rc = {RC}",
317 "RC", rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500318 return;
319 }
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930320 auto instanceId = instanceIdDb.next(mctp_eid);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500321 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
322 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
323 actualSize);
324 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
325 rc = encode_platform_event_message_req(
ArchanaKakani6c39c7a2022-12-05 04:36:35 -0600326 instanceId, 1, TERMINUS_ID, PLDM_PDR_REPOSITORY_CHG_EVENT,
327 eventDataVec.data(), actualSize, request,
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500328 actualSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500329 if (rc != PLDM_SUCCESS)
330 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930331 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -0600332 error("Failed to encode_platform_event_message_req, rc = {RC}", "RC",
333 rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500334 return;
335 }
336
Patrick Williams6da4f912023-05-10 07:50:53 -0500337 auto platformEventMessageResponseHandler =
338 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
Tom Joseph74f27c72021-05-16 07:58:53 -0700339 if (response == nullptr || !respMsgLen)
340 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600341 error(
342 "Failed to receive response for the PDR repository changed event");
Tom Joseph74f27c72021-05-16 07:58:53 -0700343 return;
344 }
345
346 uint8_t completionCode{};
347 uint8_t status{};
348 auto responsePtr = reinterpret_cast<const struct pldm_msg*>(response);
Pavithra Barithaya54b5a562021-09-27 06:07:10 -0500349 auto rc = decode_platform_event_message_resp(responsePtr, respMsgLen,
350 &completionCode, &status);
Sampa Misrac0c79482021-06-02 08:01:54 -0500351 if (rc || completionCode)
Tom Joseph74f27c72021-05-16 07:58:53 -0700352 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600353 error(
354 "Failed to decode_platform_event_message_resp: {RC}, cc = {CC}",
355 "RC", rc, "CC", static_cast<unsigned>(completionCode));
Tom Joseph74f27c72021-05-16 07:58:53 -0700356 }
357 };
358
Sampa Misrac0c79482021-06-02 08:01:54 -0500359 rc = handler->registerRequest(
Sampa Misra626c5652021-08-11 10:28:48 -0500360 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
Tom Joseph74f27c72021-05-16 07:58:53 -0700361 std::move(requestMsg), std::move(platformEventMessageResponseHandler));
Sampa Misrac0c79482021-06-02 08:01:54 -0500362 if (rc)
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500363 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600364 error("Failed to send the PDR repository changed event request");
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500365 }
366}
367
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530368void HostPDRHandler::parseStateSensorPDRs(const PDRList& stateSensorPDRs)
Sampa Misra868c8792020-05-26 03:12:13 -0500369{
370 for (const auto& pdr : stateSensorPDRs)
371 {
372 SensorEntry sensorEntry{};
373 const auto& [terminusHandle, sensorID, sensorInfo] =
374 responder::pdr_utils::parseStateSensorPDR(pdr);
375 sensorEntry.sensorID = sensorID;
376 try
377 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530378 sensorEntry.terminusID = std::get<0>(tlPDRInfo.at(terminusHandle));
Sampa Misra868c8792020-05-26 03:12:13 -0500379 }
380 // If there is no mapping for terminusHandle assign the reserved TID
381 // value of 0xFF to indicate that.
382 catch (const std::out_of_range& e)
383 {
384 sensorEntry.terminusID = PLDM_TID_RESERVED;
385 }
386 sensorMap.emplace(sensorEntry, std::move(sensorInfo));
387 }
388}
389
Sampa Misrac0c79482021-06-02 08:01:54 -0500390void HostPDRHandler::processHostPDRs(mctp_eid_t /*eid*/,
391 const pldm_msg* response,
392 size_t respMsgLen)
393{
394 static bool merged = false;
395 static PDRList stateSensorPDRs{};
Sampa Misrac0c79482021-06-02 08:01:54 -0500396 uint32_t nextRecordHandle{};
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600397 uint8_t tlEid = 0;
398 bool tlValid = true;
399 uint32_t rh = 0;
400 uint16_t terminusHandle = 0;
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530401 uint16_t pdrTerminusHandle = 0;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600402 uint8_t tid = 0;
403
Sampa Misrac0c79482021-06-02 08:01:54 -0500404 uint8_t completionCode{};
405 uint32_t nextDataTransferHandle{};
406 uint8_t transferFlag{};
407 uint16_t respCount{};
408 uint8_t transferCRC{};
409 if (response == nullptr || !respMsgLen)
410 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600411 error("Failed to receive response for the GetPDR command");
Sampa Misrac0c79482021-06-02 08:01:54 -0500412 return;
413 }
414
415 auto rc = decode_get_pdr_resp(
416 response, respMsgLen /*- sizeof(pldm_msg_hdr)*/, &completionCode,
417 &nextRecordHandle, &nextDataTransferHandle, &transferFlag, &respCount,
418 nullptr, 0, &transferCRC);
419 std::vector<uint8_t> responsePDRMsg;
420 responsePDRMsg.resize(respMsgLen + sizeof(pldm_msg_hdr));
421 memcpy(responsePDRMsg.data(), response, respMsgLen + sizeof(pldm_msg_hdr));
Sampa Misrac0c79482021-06-02 08:01:54 -0500422 if (rc != PLDM_SUCCESS)
423 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600424 error("Failed to decode_get_pdr_resp, rc = {RC}", "RC", rc);
Sampa Misrac0c79482021-06-02 08:01:54 -0500425 return;
426 }
427 else
428 {
429 std::vector<uint8_t> pdr(respCount, 0);
430 rc = decode_get_pdr_resp(response, respMsgLen, &completionCode,
431 &nextRecordHandle, &nextDataTransferHandle,
432 &transferFlag, &respCount, pdr.data(),
433 respCount, &transferCRC);
434 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
435 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600436 error("Failed to decode_get_pdr_resp: rc = {RC}, cc = {CC}", "RC",
437 rc, "CC", static_cast<unsigned>(completionCode));
Sampa Misrac0c79482021-06-02 08:01:54 -0500438 return;
439 }
440 else
441 {
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600442 // when nextRecordHandle is 0, we need the recordHandle of the last
443 // PDR and not 0-1.
444 if (!nextRecordHandle)
445 {
446 rh = nextRecordHandle;
447 }
448 else
449 {
450 rh = nextRecordHandle - 1;
451 }
452
Sampa Misrac0c79482021-06-02 08:01:54 -0500453 auto pdrHdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600454 if (!rh)
455 {
456 rh = pdrHdr->record_handle;
457 }
458
Sampa Misrac0c79482021-06-02 08:01:54 -0500459 if (pdrHdr->type == PLDM_PDR_ENTITY_ASSOCIATION)
460 {
461 this->mergeEntityAssociations(pdr);
462 merged = true;
463 }
464 else
465 {
466 if (pdrHdr->type == PLDM_TERMINUS_LOCATOR_PDR)
467 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530468 pdrTerminusHandle =
469 extractTerminusHandle<pldm_terminus_locator_pdr>(pdr);
Sampa Misrac0c79482021-06-02 08:01:54 -0500470 auto tlpdr =
471 reinterpret_cast<const pldm_terminus_locator_pdr*>(
472 pdr.data());
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600473
474 terminusHandle = tlpdr->terminus_handle;
475 tid = tlpdr->tid;
476 auto terminus_locator_type = tlpdr->terminus_locator_type;
477 if (terminus_locator_type ==
478 PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID)
479 {
480 auto locatorValue = reinterpret_cast<
481 const pldm_terminus_locator_type_mctp_eid*>(
482 tlpdr->terminus_locator_value);
483 tlEid = static_cast<uint8_t>(locatorValue->eid);
484 }
485 if (tlpdr->validity == 0)
486 {
487 tlValid = false;
488 }
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500489 for (const auto& terminusMap : tlPDRInfo)
490 {
491 if ((terminusHandle == (terminusMap.first)) &&
492 (get<1>(terminusMap.second) == tlEid) &&
493 (get<2>(terminusMap.second) == tlpdr->validity))
494 {
495 // TL PDR already present with same validity don't
496 // add the PDR to the repo just return
497 return;
498 }
499 }
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530500 tlPDRInfo.insert_or_assign(
501 tlpdr->terminus_handle,
502 std::make_tuple(tlpdr->tid, tlEid, tlpdr->validity));
Sampa Misrac0c79482021-06-02 08:01:54 -0500503 }
504 else if (pdrHdr->type == PLDM_STATE_SENSOR_PDR)
505 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530506 pdrTerminusHandle =
507 extractTerminusHandle<pldm_state_sensor_pdr>(pdr);
Sampa Misrac0c79482021-06-02 08:01:54 -0500508 stateSensorPDRs.emplace_back(pdr);
509 }
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530510 else if (pdrHdr->type == PLDM_PDR_FRU_RECORD_SET)
511 {
512 pdrTerminusHandle =
513 extractTerminusHandle<pldm_pdr_fru_record_set>(pdr);
514 }
515 else if (pdrHdr->type == PLDM_STATE_EFFECTER_PDR)
516 {
517 pdrTerminusHandle =
518 extractTerminusHandle<pldm_state_effecter_pdr>(pdr);
519 }
520 else if (pdrHdr->type == PLDM_NUMERIC_EFFECTER_PDR)
521 {
522 pdrTerminusHandle =
523 extractTerminusHandle<pldm_numeric_effecter_value_pdr>(
524 pdr);
525 }
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600526 // if the TLPDR is invalid update the repo accordingly
527 if (!tlValid)
528 {
529 pldm_pdr_update_TL_pdr(repo, terminusHandle, tid, tlEid,
530 tlValid);
531 }
532 else
533 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530534 pldm_pdr_add(repo, pdr.data(), respCount, rh, true,
535 pdrTerminusHandle);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600536 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500537 }
538 }
539 }
540 if (!nextRecordHandle)
541 {
542 /*received last record*/
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530543 this->parseStateSensorPDRs(stateSensorPDRs);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600544 if (isHostUp())
545 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530546 this->setHostSensorState(stateSensorPDRs);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600547 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500548 stateSensorPDRs.clear();
Sampa Misrac0c79482021-06-02 08:01:54 -0500549 if (merged)
550 {
551 merged = false;
552 deferredPDRRepoChgEvent =
553 std::make_unique<sdeventplus::source::Defer>(
554 event,
555 std::bind(
556 std::mem_fn((&HostPDRHandler::_processPDRRepoChgEvent)),
557 this, std::placeholders::_1));
558 }
559 }
560 else
561 {
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500562 if (modifiedPDRRecordHandles.empty() && isHostPdrModified)
563 {
564 isHostPdrModified = false;
565 }
566 else
567 {
568 deferredFetchPDREvent =
569 std::make_unique<sdeventplus::source::Defer>(
570 event,
571 std::bind(
572 std::mem_fn((&HostPDRHandler::_processFetchPDREvent)),
573 this, nextRecordHandle, std::placeholders::_1));
574 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500575 }
576}
577
578void HostPDRHandler::_processPDRRepoChgEvent(
579 sdeventplus::source::EventBase& /*source */)
580{
581 deferredPDRRepoChgEvent.reset();
582 this->sendPDRRepositoryChgEvent(
583 std::move(std::vector<uint8_t>(1, PLDM_PDR_ENTITY_ASSOCIATION)),
584 FORMAT_IS_PDR_HANDLES);
585}
586
587void HostPDRHandler::_processFetchPDREvent(
588 uint32_t nextRecordHandle, sdeventplus::source::EventBase& /*source */)
589{
590 deferredFetchPDREvent.reset();
591 if (!this->pdrRecordHandles.empty())
592 {
593 nextRecordHandle = this->pdrRecordHandles.front();
594 this->pdrRecordHandles.pop_front();
595 }
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500596 if (isHostPdrModified && (!this->modifiedPDRRecordHandles.empty()))
597 {
598 nextRecordHandle = this->modifiedPDRRecordHandles.front();
599 this->modifiedPDRRecordHandles.pop_front();
600 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500601 this->getHostPDR(nextRecordHandle);
602}
603
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500604void HostPDRHandler::setHostFirmwareCondition()
sampmisr6decfc12021-03-02 11:07:36 +0530605{
sampmisr6decfc12021-03-02 11:07:36 +0530606 responseReceived = false;
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930607 auto instanceId = instanceIdDb.next(mctp_eid);
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500608 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
609 PLDM_GET_VERSION_REQ_BYTES);
610 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
611 auto rc = encode_get_version_req(instanceId, 0, PLDM_GET_FIRSTPART,
612 PLDM_BASE, request);
613 if (rc != PLDM_SUCCESS)
sampmisr6decfc12021-03-02 11:07:36 +0530614 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600615 error("GetPLDMVersion encode failure. PLDM error code = {RC}", "RC",
616 lg2::hex, rc);
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930617 instanceIdDb.free(mctp_eid, instanceId);
sampmisr6decfc12021-03-02 11:07:36 +0530618 return;
619 }
620
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500621 auto getPLDMVersionHandler = [this](mctp_eid_t /*eid*/,
622 const pldm_msg* response,
623 size_t respMsgLen) {
624 if (response == nullptr || !respMsgLen)
sampmisr6decfc12021-03-02 11:07:36 +0530625 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600626 error(
627 "Failed to receive response for getPLDMVersion command, Host seems to be off");
sampmisr6decfc12021-03-02 11:07:36 +0530628 return;
629 }
Riya Dixit49cfb132023-03-02 04:26:53 -0600630 info("Getting the response. PLDM RC = {RC}", "RC", lg2::hex,
631 static_cast<uint16_t>(response->payload[0]));
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500632 this->responseReceived = true;
633 getHostPDR();
sampmisr6decfc12021-03-02 11:07:36 +0530634 };
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500635 rc = handler->registerRequest(mctp_eid, instanceId, PLDM_BASE,
636 PLDM_GET_PLDM_VERSION, std::move(requestMsg),
637 std::move(getPLDMVersionHandler));
638 if (rc)
sampmisr6decfc12021-03-02 11:07:36 +0530639 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600640 error("Failed to discover Host state. Assuming Host as off");
sampmisr6decfc12021-03-02 11:07:36 +0530641 }
642}
643
644bool HostPDRHandler::isHostUp()
645{
646 return responseReceived;
647}
648
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530649void HostPDRHandler::setHostSensorState(const PDRList& stateSensorPDRs)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600650{
651 for (const auto& stateSensorPDR : stateSensorPDRs)
652 {
653 auto pdr = reinterpret_cast<const pldm_state_sensor_pdr*>(
654 stateSensorPDR.data());
655
656 if (!pdr)
657 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600658 error("Failed to get State sensor PDR");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600659 pldm::utils::reportError(
660 "xyz.openbmc_project.bmc.pldm.InternalFailure");
661 return;
662 }
663
664 uint16_t sensorId = pdr->sensor_id;
665
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530666 for (const auto& [terminusHandle, terminusInfo] : tlPDRInfo)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600667 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530668 if (terminusHandle == pdr->terminus_handle)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600669 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530670 if (std::get<2>(terminusInfo) == PLDM_TL_PDR_VALID)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600671 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530672 mctp_eid = std::get<1>(terminusInfo);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600673 }
674
675 bitfield8_t sensorRearm;
676 sensorRearm.byte = 0;
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530677 uint8_t tid = std::get<0>(terminusInfo);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600678
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930679 auto instanceId = instanceIdDb.next(mctp_eid);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600680 std::vector<uint8_t> requestMsg(
681 sizeof(pldm_msg_hdr) +
682 PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES);
683 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
684 auto rc = encode_get_state_sensor_readings_req(
685 instanceId, sensorId, sensorRearm, 0, request);
686
687 if (rc != PLDM_SUCCESS)
688 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930689 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -0600690 error(
691 "Failed to encode_get_state_sensor_readings_req, rc = {RC}",
692 "RC", rc);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600693 pldm::utils::reportError(
694 "xyz.openbmc_project.bmc.pldm.InternalFailure");
695 return;
696 }
697
Patrick Williams6da4f912023-05-10 07:50:53 -0500698 auto getStateSensorReadingRespHandler =
699 [=, this](mctp_eid_t /*eid*/, const pldm_msg* response,
700 size_t respMsgLen) {
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600701 if (response == nullptr || !respMsgLen)
702 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600703 error(
704 "Failed to receive response for getStateSensorReading command");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600705 return;
706 }
707 std::array<get_sensor_state_field, 8> stateField{};
708 uint8_t completionCode = 0;
709 uint8_t comp_sensor_count = 0;
710
711 auto rc = decode_get_state_sensor_readings_resp(
712 response, respMsgLen, &completionCode,
713 &comp_sensor_count, stateField.data());
714
715 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
716 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600717 error(
718 "Failed to decode_get_state_sensor_readings_resp, rc = {RC} cc = {CC}",
719 "RC", rc, "CC",
720 static_cast<unsigned>(completionCode));
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600721 pldm::utils::reportError(
722 "xyz.openbmc_project.bmc.pldm.InternalFailure");
723 }
724
725 uint8_t eventState;
726 uint8_t previousEventState;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600727
RIYA DIXITbb5fda42022-09-27 06:48:08 -0500728 for (uint8_t sensorOffset = 0;
729 sensorOffset < comp_sensor_count; sensorOffset++)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600730 {
RIYA DIXITbb5fda42022-09-27 06:48:08 -0500731 eventState = stateField[sensorOffset].present_state;
732 previousEventState =
733 stateField[sensorOffset].previous_state;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600734
735 emitStateSensorEventSignal(tid, sensorId, sensorOffset,
736 eventState,
737 previousEventState);
738
739 SensorEntry sensorEntry{tid, sensorId};
740
741 pldm::pdr::EntityInfo entityInfo{};
742 pldm::pdr::CompositeSensorStates
743 compositeSensorStates{};
744
745 try
746 {
747 std::tie(entityInfo, compositeSensorStates) =
748 lookupSensorInfo(sensorEntry);
749 }
750 catch (const std::out_of_range& e)
751 {
752 try
753 {
754 sensorEntry.terminusID = PLDM_TID_RESERVED;
755 std::tie(entityInfo, compositeSensorStates) =
756 lookupSensorInfo(sensorEntry);
757 }
758 catch (const std::out_of_range& e)
759 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600760 error("No mapping for the events");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600761 }
762 }
763
764 if (sensorOffset > compositeSensorStates.size())
765 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600766 error("Error Invalid data, Invalid sensor offset");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600767 return;
768 }
769
770 const auto& possibleStates =
771 compositeSensorStates[sensorOffset];
772 if (possibleStates.find(eventState) ==
773 possibleStates.end())
774 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600775 error("Error invalid_data, Invalid event state");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600776 return;
777 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500778 const auto& [containerId, entityType,
779 entityInstance] = entityInfo;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600780 pldm::responder::events::StateSensorEntry
781 stateSensorEntry{containerId, entityType,
782 entityInstance, sensorOffset};
783 handleStateSensorEvent(stateSensorEntry, eventState);
784 }
785 };
786
787 rc = handler->registerRequest(
788 mctp_eid, instanceId, PLDM_PLATFORM,
789 PLDM_GET_STATE_SENSOR_READINGS, std::move(requestMsg),
790 std::move(getStateSensorReadingRespHandler));
791
792 if (rc != PLDM_SUCCESS)
793 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600794 error(
795 "Failed to send request to get State sensor reading on Host");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600796 }
797 }
798 }
799 }
800}
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500801} // namespace pldm