blob: 89c170ee2e0c69df111d218f615f71be01f4ab78 [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 {
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930534 rc = pldm_pdr_add_check(repo, pdr.data(), respCount, true,
535 pdrTerminusHandle, &rh);
536 if (rc)
537 {
538 // pldm_pdr_add() assert()ed on failure to add a PDR.
539 throw std::runtime_error("Failed to add PDR");
540 }
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600541 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500542 }
543 }
544 }
545 if (!nextRecordHandle)
546 {
547 /*received last record*/
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530548 this->parseStateSensorPDRs(stateSensorPDRs);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600549 if (isHostUp())
550 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530551 this->setHostSensorState(stateSensorPDRs);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600552 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500553 stateSensorPDRs.clear();
Sampa Misrac0c79482021-06-02 08:01:54 -0500554 if (merged)
555 {
556 merged = false;
557 deferredPDRRepoChgEvent =
558 std::make_unique<sdeventplus::source::Defer>(
559 event,
560 std::bind(
561 std::mem_fn((&HostPDRHandler::_processPDRRepoChgEvent)),
562 this, std::placeholders::_1));
563 }
564 }
565 else
566 {
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500567 if (modifiedPDRRecordHandles.empty() && isHostPdrModified)
568 {
569 isHostPdrModified = false;
570 }
571 else
572 {
573 deferredFetchPDREvent =
574 std::make_unique<sdeventplus::source::Defer>(
575 event,
576 std::bind(
577 std::mem_fn((&HostPDRHandler::_processFetchPDREvent)),
578 this, nextRecordHandle, std::placeholders::_1));
579 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500580 }
581}
582
583void HostPDRHandler::_processPDRRepoChgEvent(
584 sdeventplus::source::EventBase& /*source */)
585{
586 deferredPDRRepoChgEvent.reset();
587 this->sendPDRRepositoryChgEvent(
588 std::move(std::vector<uint8_t>(1, PLDM_PDR_ENTITY_ASSOCIATION)),
589 FORMAT_IS_PDR_HANDLES);
590}
591
592void HostPDRHandler::_processFetchPDREvent(
593 uint32_t nextRecordHandle, sdeventplus::source::EventBase& /*source */)
594{
595 deferredFetchPDREvent.reset();
596 if (!this->pdrRecordHandles.empty())
597 {
598 nextRecordHandle = this->pdrRecordHandles.front();
599 this->pdrRecordHandles.pop_front();
600 }
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500601 if (isHostPdrModified && (!this->modifiedPDRRecordHandles.empty()))
602 {
603 nextRecordHandle = this->modifiedPDRRecordHandles.front();
604 this->modifiedPDRRecordHandles.pop_front();
605 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500606 this->getHostPDR(nextRecordHandle);
607}
608
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500609void HostPDRHandler::setHostFirmwareCondition()
sampmisr6decfc12021-03-02 11:07:36 +0530610{
sampmisr6decfc12021-03-02 11:07:36 +0530611 responseReceived = false;
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930612 auto instanceId = instanceIdDb.next(mctp_eid);
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500613 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
614 PLDM_GET_VERSION_REQ_BYTES);
615 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
616 auto rc = encode_get_version_req(instanceId, 0, PLDM_GET_FIRSTPART,
617 PLDM_BASE, request);
618 if (rc != PLDM_SUCCESS)
sampmisr6decfc12021-03-02 11:07:36 +0530619 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600620 error("GetPLDMVersion encode failure. PLDM error code = {RC}", "RC",
621 lg2::hex, rc);
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930622 instanceIdDb.free(mctp_eid, instanceId);
sampmisr6decfc12021-03-02 11:07:36 +0530623 return;
624 }
625
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500626 auto getPLDMVersionHandler = [this](mctp_eid_t /*eid*/,
627 const pldm_msg* response,
628 size_t respMsgLen) {
629 if (response == nullptr || !respMsgLen)
sampmisr6decfc12021-03-02 11:07:36 +0530630 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600631 error(
632 "Failed to receive response for getPLDMVersion command, Host seems to be off");
sampmisr6decfc12021-03-02 11:07:36 +0530633 return;
634 }
Riya Dixit49cfb132023-03-02 04:26:53 -0600635 info("Getting the response. PLDM RC = {RC}", "RC", lg2::hex,
636 static_cast<uint16_t>(response->payload[0]));
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500637 this->responseReceived = true;
638 getHostPDR();
sampmisr6decfc12021-03-02 11:07:36 +0530639 };
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500640 rc = handler->registerRequest(mctp_eid, instanceId, PLDM_BASE,
641 PLDM_GET_PLDM_VERSION, std::move(requestMsg),
642 std::move(getPLDMVersionHandler));
643 if (rc)
sampmisr6decfc12021-03-02 11:07:36 +0530644 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600645 error("Failed to discover Host state. Assuming Host as off");
sampmisr6decfc12021-03-02 11:07:36 +0530646 }
647}
648
649bool HostPDRHandler::isHostUp()
650{
651 return responseReceived;
652}
653
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530654void HostPDRHandler::setHostSensorState(const PDRList& stateSensorPDRs)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600655{
656 for (const auto& stateSensorPDR : stateSensorPDRs)
657 {
658 auto pdr = reinterpret_cast<const pldm_state_sensor_pdr*>(
659 stateSensorPDR.data());
660
661 if (!pdr)
662 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600663 error("Failed to get State sensor PDR");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600664 pldm::utils::reportError(
665 "xyz.openbmc_project.bmc.pldm.InternalFailure");
666 return;
667 }
668
669 uint16_t sensorId = pdr->sensor_id;
670
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530671 for (const auto& [terminusHandle, terminusInfo] : tlPDRInfo)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600672 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530673 if (terminusHandle == pdr->terminus_handle)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600674 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530675 if (std::get<2>(terminusInfo) == PLDM_TL_PDR_VALID)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600676 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530677 mctp_eid = std::get<1>(terminusInfo);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600678 }
679
680 bitfield8_t sensorRearm;
681 sensorRearm.byte = 0;
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530682 uint8_t tid = std::get<0>(terminusInfo);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600683
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930684 auto instanceId = instanceIdDb.next(mctp_eid);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600685 std::vector<uint8_t> requestMsg(
686 sizeof(pldm_msg_hdr) +
687 PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES);
688 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
689 auto rc = encode_get_state_sensor_readings_req(
690 instanceId, sensorId, sensorRearm, 0, request);
691
692 if (rc != PLDM_SUCCESS)
693 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930694 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -0600695 error(
696 "Failed to encode_get_state_sensor_readings_req, rc = {RC}",
697 "RC", rc);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600698 pldm::utils::reportError(
699 "xyz.openbmc_project.bmc.pldm.InternalFailure");
700 return;
701 }
702
Patrick Williams6da4f912023-05-10 07:50:53 -0500703 auto getStateSensorReadingRespHandler =
704 [=, this](mctp_eid_t /*eid*/, const pldm_msg* response,
705 size_t respMsgLen) {
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600706 if (response == nullptr || !respMsgLen)
707 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600708 error(
709 "Failed to receive response for getStateSensorReading command");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600710 return;
711 }
712 std::array<get_sensor_state_field, 8> stateField{};
713 uint8_t completionCode = 0;
714 uint8_t comp_sensor_count = 0;
715
716 auto rc = decode_get_state_sensor_readings_resp(
717 response, respMsgLen, &completionCode,
718 &comp_sensor_count, stateField.data());
719
720 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
721 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600722 error(
723 "Failed to decode_get_state_sensor_readings_resp, rc = {RC} cc = {CC}",
724 "RC", rc, "CC",
725 static_cast<unsigned>(completionCode));
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600726 pldm::utils::reportError(
727 "xyz.openbmc_project.bmc.pldm.InternalFailure");
728 }
729
730 uint8_t eventState;
731 uint8_t previousEventState;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600732
RIYA DIXITbb5fda42022-09-27 06:48:08 -0500733 for (uint8_t sensorOffset = 0;
734 sensorOffset < comp_sensor_count; sensorOffset++)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600735 {
RIYA DIXITbb5fda42022-09-27 06:48:08 -0500736 eventState = stateField[sensorOffset].present_state;
737 previousEventState =
738 stateField[sensorOffset].previous_state;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600739
740 emitStateSensorEventSignal(tid, sensorId, sensorOffset,
741 eventState,
742 previousEventState);
743
744 SensorEntry sensorEntry{tid, sensorId};
745
746 pldm::pdr::EntityInfo entityInfo{};
747 pldm::pdr::CompositeSensorStates
748 compositeSensorStates{};
749
750 try
751 {
752 std::tie(entityInfo, compositeSensorStates) =
753 lookupSensorInfo(sensorEntry);
754 }
755 catch (const std::out_of_range& e)
756 {
757 try
758 {
759 sensorEntry.terminusID = PLDM_TID_RESERVED;
760 std::tie(entityInfo, compositeSensorStates) =
761 lookupSensorInfo(sensorEntry);
762 }
763 catch (const std::out_of_range& e)
764 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600765 error("No mapping for the events");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600766 }
767 }
768
769 if (sensorOffset > compositeSensorStates.size())
770 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600771 error("Error Invalid data, Invalid sensor offset");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600772 return;
773 }
774
775 const auto& possibleStates =
776 compositeSensorStates[sensorOffset];
777 if (possibleStates.find(eventState) ==
778 possibleStates.end())
779 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600780 error("Error invalid_data, Invalid event state");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600781 return;
782 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500783 const auto& [containerId, entityType,
784 entityInstance] = entityInfo;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600785 pldm::responder::events::StateSensorEntry
786 stateSensorEntry{containerId, entityType,
787 entityInstance, sensorOffset};
788 handleStateSensorEvent(stateSensorEntry, eventState);
789 }
790 };
791
792 rc = handler->registerRequest(
793 mctp_eid, instanceId, PLDM_PLATFORM,
794 PLDM_GET_STATE_SENSOR_READINGS, std::move(requestMsg),
795 std::move(getStateSensorReadingRespHandler));
796
797 if (rc != PLDM_SUCCESS)
798 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600799 error(
800 "Failed to send request to get State sensor reading on Host");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600801 }
802 }
803 }
804 }
805}
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500806} // namespace pldm