blob: 775885864c357b58e717143996e0ba5eb4d1b41d [file] [log] [blame]
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05001#include "host_pdr_handler.hpp"
2
Andrew Jeffery4668f5c2024-01-15 14:59:17 +10303#include <libpldm/fru.h>
George Liu682ee182020-12-25 15:24:33 +08004#ifdef OEM_IBM
Andrew Jeffery21f128d2024-01-15 15:34:26 +10305#include <libpldm/oem/ibm/fru.h>
Sagar Srinivas3687e2b2023-04-10 05:08:28 -05006#endif
Kamalkumar Patel14107a12024-06-19 08:50:01 -05007#include "dbus/custom_dbus.hpp"
George Liu682ee182020-12-25 15:24:33 +08008
Pavithra Barithayae8beb892020-04-14 23:24:25 -05009#include <assert.h>
10
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050011#include <nlohmann/json.hpp>
Riya Dixit49cfb132023-03-02 04:26:53 -060012#include <phosphor-logging/lg2.hpp>
sampmisr6decfc12021-03-02 11:07:36 +053013#include <sdeventplus/clock.hpp>
14#include <sdeventplus/exception.hpp>
15#include <sdeventplus/source/io.hpp>
16#include <sdeventplus/source/time.hpp>
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050017
George Liu6492f522020-06-16 10:34:05 +080018#include <fstream>
George Liu96af8cb2021-07-31 15:23:45 +080019#include <type_traits>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050020
Riya Dixit49cfb132023-03-02 04:26:53 -060021PHOSPHOR_LOG2_USING;
22
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050023namespace pldm
24{
Brad Bishop5079ac42021-08-19 18:35:06 -040025using namespace pldm::responder::events;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050026using namespace pldm::utils;
27using namespace sdbusplus::bus::match::rules;
Sagar Srinivas3687e2b2023-04-10 05:08:28 -050028using namespace pldm::responder::pdr_utils;
Kamalkumar Patel516122e2024-05-07 04:39:32 -050029using namespace pldm::hostbmc::utils;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050030using Json = nlohmann::json;
31namespace fs = std::filesystem;
George Liu682ee182020-12-25 15:24:33 +080032using namespace pldm::dbus;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050033constexpr auto fruJson = "host_frus.json";
34const Json emptyJson{};
35const std::vector<Json> emptyJsonList{};
36
Manojkiran Eda3ca40452021-10-04 22:51:37 +053037template <typename T>
38uint16_t extractTerminusHandle(std::vector<uint8_t>& pdr)
39{
40 T* var = nullptr;
41 if (std::is_same<T, pldm_pdr_fru_record_set>::value)
42 {
43 var = (T*)(pdr.data() + sizeof(pldm_pdr_hdr));
44 }
45 else
46 {
47 var = (T*)(pdr.data());
48 }
49 if (var != nullptr)
50 {
51 return var->terminus_handle;
52 }
53 return TERMINUS_HANDLE;
54}
55
George Liu96af8cb2021-07-31 15:23:45 +080056template <typename T>
57void updateContainerId(pldm_entity_association_tree* entityTree,
58 std::vector<uint8_t>& pdr)
59{
60 T* t = nullptr;
61 if (entityTree == nullptr)
62 {
63 return;
64 }
65 if (std::is_same<T, pldm_pdr_fru_record_set>::value)
66 {
67 t = (T*)(pdr.data() + sizeof(pldm_pdr_hdr));
68 }
69 else
70 {
71 t = (T*)(pdr.data());
72 }
73 if (t == nullptr)
74 {
75 return;
76 }
77
78 pldm_entity entity{t->entity_type, t->entity_instance, t->container_id};
79 auto node = pldm_entity_association_tree_find_with_locality(entityTree,
80 &entity, true);
81 if (node)
82 {
83 pldm_entity e = pldm_entity_extract(node);
84 t->container_id = e.entity_container_id;
85 }
86}
87
Tom Joseph74f27c72021-05-16 07:58:53 -070088HostPDRHandler::HostPDRHandler(
89 int mctp_fd, uint8_t mctp_eid, sdeventplus::Event& event, pldm_pdr* repo,
90 const std::string& eventsJsonsDir, pldm_entity_association_tree* entityTree,
Andrew Jefferya330b2f2023-05-04 14:55:37 +093091 pldm_entity_association_tree* bmcEntityTree,
92 pldm::InstanceIdDb& instanceIdDb,
George Liua881c172021-06-21 18:28:11 +080093 pldm::requester::Handler<pldm::requester::Request>* handler) :
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050094 mctp_fd(mctp_fd),
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060095 mctp_eid(mctp_eid), event(event), repo(repo),
96 stateSensorHandler(eventsJsonsDir), entityTree(entityTree),
Sagar Srinivas3687e2b2023-04-10 05:08:28 -050097 bmcEntityTree(bmcEntityTree), instanceIdDb(instanceIdDb), handler(handler),
Kamalkumar Pateleb43d6c2024-05-01 06:11:31 -050098 entityMaps(parseEntityMap(ENTITY_MAP_JSON)), oemUtilsHandler(nullptr)
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050099{
George Liuacf2c8c2021-05-10 14:08:52 +0800100 mergedHostParents = false;
Patrick Williams84b790c2022-07-22 19:26:56 -0500101 hostOffMatch = std::make_unique<sdbusplus::bus::match_t>(
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500102 pldm::utils::DBusHandler::getBus(),
103 propertiesChanged("/xyz/openbmc_project/state/host0",
104 "xyz.openbmc_project.State.Host"),
Patrick Williams84b790c2022-07-22 19:26:56 -0500105 [this, repo, entityTree, bmcEntityTree](sdbusplus::message_t& msg) {
Patrick Williams6da4f912023-05-10 07:50:53 -0500106 DbusChangedProps props{};
107 std::string intf;
108 msg.read(intf, props);
109 const auto itr = props.find("CurrentHostState");
110 if (itr != props.end())
111 {
112 PropertyValue value = itr->second;
113 auto propVal = std::get<std::string>(value);
114 if (propVal == "xyz.openbmc_project.State.Host.HostState.Off")
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500115 {
Patrick Williams6da4f912023-05-10 07:50:53 -0500116 // Delete all the remote terminus information
117 std::erase_if(tlPDRInfo, [](const auto& item) {
118 auto const& [key, value] = item;
119 return key != TERMINUS_HANDLE;
120 });
121 pldm_pdr_remove_remote_pdrs(repo);
122 pldm_entity_association_tree_destroy_root(entityTree);
123 pldm_entity_association_tree_copy_root(bmcEntityTree,
124 entityTree);
125 this->sensorMap.clear();
126 this->responseReceived = false;
George Liuacf2c8c2021-05-10 14:08:52 +0800127 this->mergedHostParents = false;
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -0500128 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500129 }
Patrick Williamsa6756622023-10-20 11:19:15 -0500130 });
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500131}
132
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500133void HostPDRHandler::fetchPDR(PDRRecordHandles&& recordHandles)
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500134{
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500135 pdrRecordHandles.clear();
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500136 modifiedPDRRecordHandles.clear();
137
138 if (isHostPdrModified)
139 {
140 modifiedPDRRecordHandles = std::move(recordHandles);
141 }
142 else
143 {
144 pdrRecordHandles = std::move(recordHandles);
145 }
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500146
147 // Defer the actual fetch of PDRs from the host (by queuing the call on the
148 // main event loop). That way, we can respond to the platform event msg from
149 // the host firmware.
150 pdrFetchEvent = std::make_unique<sdeventplus::source::Defer>(
151 event, std::bind(std::mem_fn(&HostPDRHandler::_fetchPDR), this,
152 std::placeholders::_1));
153}
154
155void HostPDRHandler::_fetchPDR(sdeventplus::source::EventBase& /*source*/)
156{
Sampa Misrac0c79482021-06-02 08:01:54 -0500157 getHostPDR();
158}
159
160void HostPDRHandler::getHostPDR(uint32_t nextRecordHandle)
161{
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500162 pdrFetchEvent.reset();
163
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500164 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
165 PLDM_GET_PDR_REQ_BYTES);
166 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500167 uint32_t recordHandle{};
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500168 if (!nextRecordHandle && (!modifiedPDRRecordHandles.empty()) &&
169 isHostPdrModified)
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500170 {
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500171 recordHandle = modifiedPDRRecordHandles.front();
172 modifiedPDRRecordHandles.pop_front();
173 }
174 else if (!nextRecordHandle && (!pdrRecordHandles.empty()))
175 {
176 recordHandle = pdrRecordHandles.front();
177 pdrRecordHandles.pop_front();
Sampa Misrac0c79482021-06-02 08:01:54 -0500178 }
179 else
180 {
181 recordHandle = nextRecordHandle;
182 }
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930183 auto instanceId = instanceIdDb.next(mctp_eid);
Sampa Misrac0c79482021-06-02 08:01:54 -0500184
Patrick Williams6da4f912023-05-10 07:50:53 -0500185 auto rc = encode_get_pdr_req(instanceId, recordHandle, 0,
186 PLDM_GET_FIRSTPART, UINT16_MAX, 0, request,
187 PLDM_GET_PDR_REQ_BYTES);
Sampa Misrac0c79482021-06-02 08:01:54 -0500188 if (rc != PLDM_SUCCESS)
189 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930190 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500191 error("Failed to encode get pdr request, response code '{RC}'", "RC",
192 rc);
Sampa Misrac0c79482021-06-02 08:01:54 -0500193 return;
194 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500195
Sampa Misrac0c79482021-06-02 08:01:54 -0500196 rc = handler->registerRequest(
197 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_GET_PDR,
198 std::move(requestMsg),
199 std::move(std::bind_front(&HostPDRHandler::processHostPDRs, this)));
200 if (rc)
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500201 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500202 error(
203 "Failed to send the getPDR request to remote terminus, response code '{RC}'",
204 "RC", rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500205 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500206}
207
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600208int HostPDRHandler::handleStateSensorEvent(const StateSensorEntry& entry,
209 pdr::EventState state)
210{
211 auto rc = stateSensorHandler.eventAction(entry, state);
212 if (rc != PLDM_SUCCESS)
213 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500214 error("Failed to fetch and update D-bus property, response code '{RC}'",
215 "RC", rc);
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600216 return rc;
217 }
218 return PLDM_SUCCESS;
219}
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500220
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500221void HostPDRHandler::mergeEntityAssociations(
222 const std::vector<uint8_t>& pdr, [[maybe_unused]] const uint32_t& size,
223 [[maybe_unused]] const uint32_t& record_handle)
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500224{
225 size_t numEntities{};
226 pldm_entity* entities = nullptr;
227 bool merged = false;
228 auto entityPdr = reinterpret_cast<pldm_pdr_entity_association*>(
229 const_cast<uint8_t*>(pdr.data()) + sizeof(pldm_pdr_hdr));
230
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500231 if (oemPlatformHandler &&
232 oemPlatformHandler->checkRecordHandleInRange(record_handle))
233 {
234 // Adding the remote range PDRs to the repo before merging it
235 uint32_t handle = record_handle;
236 pldm_pdr_add_check(repo, pdr.data(), size, true, 0xFFFF, &handle);
237 }
238
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500239 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &numEntities,
240 &entities);
George Liuacf2c8c2021-05-10 14:08:52 +0800241 if (numEntities > 0)
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500242 {
George Liuacf2c8c2021-05-10 14:08:52 +0800243 pldm_entity_node* pNode = nullptr;
244 if (!mergedHostParents)
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500245 {
George Liuacf2c8c2021-05-10 14:08:52 +0800246 pNode = pldm_entity_association_tree_find_with_locality(
247 entityTree, &entities[0], false);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500248 }
George Liuacf2c8c2021-05-10 14:08:52 +0800249 else
250 {
251 pNode = pldm_entity_association_tree_find_with_locality(
252 entityTree, &entities[0], true);
253 }
254 if (!pNode)
255 {
256 return;
257 }
258
George Liudf9a6d32020-12-22 16:27:16 +0800259 Entities entityAssoc;
260 entityAssoc.push_back(pNode);
George Liuacf2c8c2021-05-10 14:08:52 +0800261 for (size_t i = 1; i < numEntities; ++i)
262 {
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500263 bool isUpdateContainerId = true;
264 if (oemPlatformHandler)
265 {
266 isUpdateContainerId =
Sagar Srinivas5db6e872023-12-01 10:03:30 -0600267 checkIfLogicalBitSet(entities[i].entity_container_id);
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500268 }
George Liudf9a6d32020-12-22 16:27:16 +0800269 auto node = pldm_entity_association_tree_add_entity(
George Liuacf2c8c2021-05-10 14:08:52 +0800270 entityTree, &entities[i], entities[i].entity_instance_num,
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500271 pNode, entityPdr->association_type, true, isUpdateContainerId,
272 0xFFFF);
273 if (!node)
274 {
275 continue;
276 }
George Liuacf2c8c2021-05-10 14:08:52 +0800277 merged = true;
George Liudf9a6d32020-12-22 16:27:16 +0800278 entityAssoc.push_back(node);
George Liuacf2c8c2021-05-10 14:08:52 +0800279 }
280
281 mergedHostParents = true;
George Liudf9a6d32020-12-22 16:27:16 +0800282 if (merged)
283 {
284 entityAssociations.push_back(entityAssoc);
285 }
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500286 }
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500287
288 if (merged)
289 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500290 // Update our PDR repo with the merged entity association PDRs
Sampa Misra719ed392021-06-04 05:15:13 -0500291 pldm_entity_node* node = nullptr;
292 pldm_find_entity_ref_in_tree(entityTree, entities[0], &node);
293 if (node == nullptr)
294 {
Manojkiran Eda2576aec2024-06-17 12:05:17 +0530295 error("Failed to find reference of the entity in the tree");
Sampa Misra719ed392021-06-04 05:15:13 -0500296 }
297 else
298 {
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500299 int rc = 0;
300 if (oemPlatformHandler)
301 {
302 auto record = oemPlatformHandler->fetchLastBMCRecord(repo);
303
304 uint32_t record_handle = pldm_pdr_get_record_handle(repo,
305 record);
306
307 rc =
308 pldm_entity_association_pdr_add_from_node_with_record_handle(
309 node, repo, &entities, numEntities, true,
310 TERMINUS_HANDLE, (record_handle + 1));
311 }
312 else
313 {
314 rc = pldm_entity_association_pdr_add_from_node_check(
315 node, repo, &entities, numEntities, true, TERMINUS_HANDLE);
316 }
317
Andrew Jeffery1fd3c512023-07-03 13:02:03 +0930318 if (rc)
319 {
320 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500321 "Failed to add entity association PDR from node, response code '{RC}'",
322 "RC", rc);
Andrew Jeffery1fd3c512023-07-03 13:02:03 +0930323 }
Sampa Misra719ed392021-06-04 05:15:13 -0500324 }
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500325 }
Sampa Misra719ed392021-06-04 05:15:13 -0500326 free(entities);
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500327}
328
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500329void HostPDRHandler::sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
330 uint8_t eventDataFormat)
331{
332 assert(eventDataFormat == FORMAT_IS_PDR_HANDLES);
333
334 // Extract from the PDR repo record handles of PDRs we want the host
335 // to pull up.
336 std::vector<uint8_t> eventDataOps{PLDM_RECORDS_ADDED};
337 std::vector<uint8_t> numsOfChangeEntries(1);
338 std::vector<std::vector<ChangeEntry>> changeEntries(
339 numsOfChangeEntries.size());
340 for (auto pdrType : pdrTypes)
341 {
342 const pldm_pdr_record* record{};
343 do
344 {
345 record = pldm_pdr_find_record_by_type(repo, pdrType, record,
346 nullptr, nullptr);
347 if (record && pldm_pdr_record_is_remote(record))
348 {
349 changeEntries[0].push_back(
350 pldm_pdr_get_record_handle(repo, record));
351 }
352 } while (record);
353 }
354 if (changeEntries.empty())
355 {
356 return;
357 }
358 numsOfChangeEntries[0] = changeEntries[0].size();
359
360 // Encode PLDM platform event msg to indicate a PDR repo change.
361 size_t maxSize = PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH +
362 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH +
363 changeEntries[0].size() * sizeof(uint32_t);
364 std::vector<uint8_t> eventDataVec{};
365 eventDataVec.resize(maxSize);
366 auto eventData =
367 reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>(
368 eventDataVec.data());
369 size_t actualSize{};
370 auto firstEntry = changeEntries[0].data();
371 auto rc = encode_pldm_pdr_repository_chg_event_data(
372 eventDataFormat, 1, eventDataOps.data(), numsOfChangeEntries.data(),
373 &firstEntry, eventData, &actualSize, maxSize);
374 if (rc != PLDM_SUCCESS)
375 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500376 error(
377 "Failed to encode pldm pdr repository change event data, response code '{RC}'",
378 "RC", rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500379 return;
380 }
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930381 auto instanceId = instanceIdDb.next(mctp_eid);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500382 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
383 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
384 actualSize);
385 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
386 rc = encode_platform_event_message_req(
ArchanaKakani6c39c7a2022-12-05 04:36:35 -0600387 instanceId, 1, TERMINUS_ID, PLDM_PDR_REPOSITORY_CHG_EVENT,
388 eventDataVec.data(), actualSize, request,
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500389 actualSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500390 if (rc != PLDM_SUCCESS)
391 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930392 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500393 error(
394 "Failed to encode platform event message request, response code '{RC}'",
395 "RC", rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500396 return;
397 }
398
Patrick Williams6da4f912023-05-10 07:50:53 -0500399 auto platformEventMessageResponseHandler =
400 [](mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
Tom Joseph74f27c72021-05-16 07:58:53 -0700401 if (response == nullptr || !respMsgLen)
402 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600403 error(
404 "Failed to receive response for the PDR repository changed event");
Tom Joseph74f27c72021-05-16 07:58:53 -0700405 return;
406 }
407
408 uint8_t completionCode{};
409 uint8_t status{};
410 auto responsePtr = reinterpret_cast<const struct pldm_msg*>(response);
Pavithra Barithaya54b5a562021-09-27 06:07:10 -0500411 auto rc = decode_platform_event_message_resp(responsePtr, respMsgLen,
412 &completionCode, &status);
Sampa Misrac0c79482021-06-02 08:01:54 -0500413 if (rc || completionCode)
Tom Joseph74f27c72021-05-16 07:58:53 -0700414 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600415 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500416 "Failed to decode platform event message response, response code '{RC}' and completion code '{CC}'",
417 "RC", rc, "CC", completionCode);
Tom Joseph74f27c72021-05-16 07:58:53 -0700418 }
419 };
420
Sampa Misrac0c79482021-06-02 08:01:54 -0500421 rc = handler->registerRequest(
Sampa Misra626c5652021-08-11 10:28:48 -0500422 mctp_eid, instanceId, PLDM_PLATFORM, PLDM_PLATFORM_EVENT_MESSAGE,
Tom Joseph74f27c72021-05-16 07:58:53 -0700423 std::move(requestMsg), std::move(platformEventMessageResponseHandler));
Sampa Misrac0c79482021-06-02 08:01:54 -0500424 if (rc)
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500425 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500426 error(
427 "Failed to send the PDR repository changed event request, response code '{RC}'",
428 "RC", rc);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500429 }
430}
431
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530432void HostPDRHandler::parseStateSensorPDRs(const PDRList& stateSensorPDRs)
Sampa Misra868c8792020-05-26 03:12:13 -0500433{
434 for (const auto& pdr : stateSensorPDRs)
435 {
436 SensorEntry sensorEntry{};
437 const auto& [terminusHandle, sensorID, sensorInfo] =
438 responder::pdr_utils::parseStateSensorPDR(pdr);
439 sensorEntry.sensorID = sensorID;
440 try
441 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530442 sensorEntry.terminusID = std::get<0>(tlPDRInfo.at(terminusHandle));
Sampa Misra868c8792020-05-26 03:12:13 -0500443 }
444 // If there is no mapping for terminusHandle assign the reserved TID
445 // value of 0xFF to indicate that.
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500446 catch (const std::out_of_range&)
Sampa Misra868c8792020-05-26 03:12:13 -0500447 {
448 sensorEntry.terminusID = PLDM_TID_RESERVED;
449 }
450 sensorMap.emplace(sensorEntry, std::move(sensorInfo));
451 }
452}
453
Sampa Misrac0c79482021-06-02 08:01:54 -0500454void HostPDRHandler::processHostPDRs(mctp_eid_t /*eid*/,
455 const pldm_msg* response,
456 size_t respMsgLen)
457{
458 static bool merged = false;
459 static PDRList stateSensorPDRs{};
George Liu682ee182020-12-25 15:24:33 +0800460 static PDRList fruRecordSetPDRs{};
Sampa Misrac0c79482021-06-02 08:01:54 -0500461 uint32_t nextRecordHandle{};
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600462 uint8_t tlEid = 0;
463 bool tlValid = true;
464 uint32_t rh = 0;
465 uint16_t terminusHandle = 0;
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530466 uint16_t pdrTerminusHandle = 0;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600467 uint8_t tid = 0;
468
Sampa Misrac0c79482021-06-02 08:01:54 -0500469 uint8_t completionCode{};
470 uint32_t nextDataTransferHandle{};
471 uint8_t transferFlag{};
472 uint16_t respCount{};
473 uint8_t transferCRC{};
474 if (response == nullptr || !respMsgLen)
475 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600476 error("Failed to receive response for the GetPDR command");
Riya Dixit478e71d2024-06-21 14:10:14 -0500477 pldm::utils::reportError(
478 "xyz.openbmc_project.PLDM.Error.GetPDR.PDRExchangeFailure");
Sampa Misrac0c79482021-06-02 08:01:54 -0500479 return;
480 }
481
482 auto rc = decode_get_pdr_resp(
483 response, respMsgLen /*- sizeof(pldm_msg_hdr)*/, &completionCode,
484 &nextRecordHandle, &nextDataTransferHandle, &transferFlag, &respCount,
485 nullptr, 0, &transferCRC);
486 std::vector<uint8_t> responsePDRMsg;
487 responsePDRMsg.resize(respMsgLen + sizeof(pldm_msg_hdr));
488 memcpy(responsePDRMsg.data(), response, respMsgLen + sizeof(pldm_msg_hdr));
Sampa Misrac0c79482021-06-02 08:01:54 -0500489 if (rc != PLDM_SUCCESS)
490 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500491 error(
492 "Failed to decode getPDR response for next record handle '{NEXT_RECORD_HANDLE}', response code '{RC}'",
493 "NEXT_RECORD_HANDLE", nextRecordHandle, "RC", rc);
Sampa Misrac0c79482021-06-02 08:01:54 -0500494 return;
495 }
496 else
497 {
498 std::vector<uint8_t> pdr(respCount, 0);
499 rc = decode_get_pdr_resp(response, respMsgLen, &completionCode,
500 &nextRecordHandle, &nextDataTransferHandle,
501 &transferFlag, &respCount, pdr.data(),
502 respCount, &transferCRC);
503 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
504 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500505 error(
506 "Failed to decode getPDR response for next record handle '{NEXT_RECORD_HANDLE}', next data transfer handle '{DATA_TRANSFER_HANDLE}' and transfer flag '{FLAG}', response code '{RC}' and completion code '{CC}'",
507 "NEXT_RECORD_HANDLE", nextRecordHandle, "DATA_TRANSFER_HANDLE",
508 nextDataTransferHandle, "FLAG", transferFlag, "RC", rc, "CC",
509 completionCode);
Sampa Misrac0c79482021-06-02 08:01:54 -0500510 return;
511 }
512 else
513 {
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600514 // when nextRecordHandle is 0, we need the recordHandle of the last
515 // PDR and not 0-1.
516 if (!nextRecordHandle)
517 {
518 rh = nextRecordHandle;
519 }
520 else
521 {
522 rh = nextRecordHandle - 1;
523 }
524
Sampa Misrac0c79482021-06-02 08:01:54 -0500525 auto pdrHdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600526 if (!rh)
527 {
528 rh = pdrHdr->record_handle;
529 }
530
Sampa Misrac0c79482021-06-02 08:01:54 -0500531 if (pdrHdr->type == PLDM_PDR_ENTITY_ASSOCIATION)
532 {
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500533 this->mergeEntityAssociations(pdr, respCount, rh);
Sampa Misrac0c79482021-06-02 08:01:54 -0500534 merged = true;
535 }
536 else
537 {
538 if (pdrHdr->type == PLDM_TERMINUS_LOCATOR_PDR)
539 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530540 pdrTerminusHandle =
541 extractTerminusHandle<pldm_terminus_locator_pdr>(pdr);
Sampa Misrac0c79482021-06-02 08:01:54 -0500542 auto tlpdr =
543 reinterpret_cast<const pldm_terminus_locator_pdr*>(
544 pdr.data());
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600545
546 terminusHandle = tlpdr->terminus_handle;
547 tid = tlpdr->tid;
548 auto terminus_locator_type = tlpdr->terminus_locator_type;
549 if (terminus_locator_type ==
550 PLDM_TERMINUS_LOCATOR_TYPE_MCTP_EID)
551 {
552 auto locatorValue = reinterpret_cast<
553 const pldm_terminus_locator_type_mctp_eid*>(
554 tlpdr->terminus_locator_value);
555 tlEid = static_cast<uint8_t>(locatorValue->eid);
556 }
557 if (tlpdr->validity == 0)
558 {
559 tlValid = false;
560 }
Pavithra Barithaya52aad392022-08-02 04:18:52 -0500561 for (const auto& terminusMap : tlPDRInfo)
562 {
563 if ((terminusHandle == (terminusMap.first)) &&
564 (get<1>(terminusMap.second) == tlEid) &&
565 (get<2>(terminusMap.second) == tlpdr->validity))
566 {
567 // TL PDR already present with same validity don't
568 // add the PDR to the repo just return
569 return;
570 }
571 }
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530572 tlPDRInfo.insert_or_assign(
573 tlpdr->terminus_handle,
574 std::make_tuple(tlpdr->tid, tlEid, tlpdr->validity));
Sampa Misrac0c79482021-06-02 08:01:54 -0500575 }
576 else if (pdrHdr->type == PLDM_STATE_SENSOR_PDR)
577 {
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530578 pdrTerminusHandle =
579 extractTerminusHandle<pldm_state_sensor_pdr>(pdr);
George Liu96af8cb2021-07-31 15:23:45 +0800580 updateContainerId<pldm_state_sensor_pdr>(entityTree, pdr);
Sampa Misrac0c79482021-06-02 08:01:54 -0500581 stateSensorPDRs.emplace_back(pdr);
582 }
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530583 else if (pdrHdr->type == PLDM_PDR_FRU_RECORD_SET)
584 {
585 pdrTerminusHandle =
586 extractTerminusHandle<pldm_pdr_fru_record_set>(pdr);
George Liu96af8cb2021-07-31 15:23:45 +0800587 updateContainerId<pldm_pdr_fru_record_set>(entityTree, pdr);
George Liu682ee182020-12-25 15:24:33 +0800588 fruRecordSetPDRs.emplace_back(pdr);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530589 }
590 else if (pdrHdr->type == PLDM_STATE_EFFECTER_PDR)
591 {
592 pdrTerminusHandle =
593 extractTerminusHandle<pldm_state_effecter_pdr>(pdr);
George Liu96af8cb2021-07-31 15:23:45 +0800594 updateContainerId<pldm_state_effecter_pdr>(entityTree, pdr);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530595 }
596 else if (pdrHdr->type == PLDM_NUMERIC_EFFECTER_PDR)
597 {
598 pdrTerminusHandle =
599 extractTerminusHandle<pldm_numeric_effecter_value_pdr>(
600 pdr);
George Liu96af8cb2021-07-31 15:23:45 +0800601 updateContainerId<pldm_numeric_effecter_value_pdr>(
602 entityTree, pdr);
Manojkiran Eda3ca40452021-10-04 22:51:37 +0530603 }
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600604 // if the TLPDR is invalid update the repo accordingly
605 if (!tlValid)
606 {
607 pldm_pdr_update_TL_pdr(repo, terminusHandle, tid, tlEid,
608 tlValid);
Sagar Srinivas3687e2b2023-04-10 05:08:28 -0500609
610 if (!isHostUp())
611 {
612 // The terminus PDR becomes invalid when the terminus
613 // itself is down. We don't need to do PDR exchange in
614 // that case, so setting the next record handle to 0.
615 nextRecordHandle = 0;
616 }
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600617 }
618 else
619 {
Andrew Jeffery64f37fe2023-07-03 15:41:13 +0930620 rc = pldm_pdr_add_check(repo, pdr.data(), respCount, true,
621 pdrTerminusHandle, &rh);
622 if (rc)
623 {
624 // pldm_pdr_add() assert()ed on failure to add a PDR.
625 throw std::runtime_error("Failed to add PDR");
626 }
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600627 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500628 }
629 }
630 }
631 if (!nextRecordHandle)
632 {
Kamalkumar Patel516122e2024-05-07 04:39:32 -0500633 updateEntityAssociation(entityAssociations, entityTree, objPathMap,
Kamalkumar Patel15ce5a12024-05-07 11:45:11 -0500634 entityMaps, oemPlatformHandler);
Kamalkumar Pateleb43d6c2024-05-01 06:11:31 -0500635 if (oemUtilsHandler)
636 {
637 oemUtilsHandler->setCoreCount(entityAssociations, entityMaps);
638 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500639 /*received last record*/
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530640 this->parseStateSensorPDRs(stateSensorPDRs);
George Liu682ee182020-12-25 15:24:33 +0800641 this->createDbusObjects(fruRecordSetPDRs);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600642 if (isHostUp())
643 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530644 this->setHostSensorState(stateSensorPDRs);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600645 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500646 stateSensorPDRs.clear();
George Liu682ee182020-12-25 15:24:33 +0800647 fruRecordSetPDRs.clear();
George Liudf9a6d32020-12-22 16:27:16 +0800648 entityAssociations.clear();
649
Sampa Misrac0c79482021-06-02 08:01:54 -0500650 if (merged)
651 {
652 merged = false;
653 deferredPDRRepoChgEvent =
654 std::make_unique<sdeventplus::source::Defer>(
655 event,
656 std::bind(
657 std::mem_fn((&HostPDRHandler::_processPDRRepoChgEvent)),
658 this, std::placeholders::_1));
659 }
660 }
661 else
662 {
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500663 if (modifiedPDRRecordHandles.empty() && isHostPdrModified)
664 {
665 isHostPdrModified = false;
666 }
667 else
668 {
669 deferredFetchPDREvent =
670 std::make_unique<sdeventplus::source::Defer>(
671 event,
672 std::bind(
673 std::mem_fn((&HostPDRHandler::_processFetchPDREvent)),
674 this, nextRecordHandle, std::placeholders::_1));
675 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500676 }
677}
678
679void HostPDRHandler::_processPDRRepoChgEvent(
680 sdeventplus::source::EventBase& /*source */)
681{
682 deferredPDRRepoChgEvent.reset();
683 this->sendPDRRepositoryChgEvent(
684 std::move(std::vector<uint8_t>(1, PLDM_PDR_ENTITY_ASSOCIATION)),
685 FORMAT_IS_PDR_HANDLES);
686}
687
688void HostPDRHandler::_processFetchPDREvent(
689 uint32_t nextRecordHandle, sdeventplus::source::EventBase& /*source */)
690{
691 deferredFetchPDREvent.reset();
692 if (!this->pdrRecordHandles.empty())
693 {
694 nextRecordHandle = this->pdrRecordHandles.front();
695 this->pdrRecordHandles.pop_front();
696 }
Pavithra Barithayaae5c97e2022-08-29 02:57:59 -0500697 if (isHostPdrModified && (!this->modifiedPDRRecordHandles.empty()))
698 {
699 nextRecordHandle = this->modifiedPDRRecordHandles.front();
700 this->modifiedPDRRecordHandles.pop_front();
701 }
Sampa Misrac0c79482021-06-02 08:01:54 -0500702 this->getHostPDR(nextRecordHandle);
703}
704
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500705void HostPDRHandler::setHostFirmwareCondition()
sampmisr6decfc12021-03-02 11:07:36 +0530706{
sampmisr6decfc12021-03-02 11:07:36 +0530707 responseReceived = false;
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930708 auto instanceId = instanceIdDb.next(mctp_eid);
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500709 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
710 PLDM_GET_VERSION_REQ_BYTES);
711 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
712 auto rc = encode_get_version_req(instanceId, 0, PLDM_GET_FIRSTPART,
713 PLDM_BASE, request);
714 if (rc != PLDM_SUCCESS)
sampmisr6decfc12021-03-02 11:07:36 +0530715 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500716 error("Failed to encode GetPLDMVersion, response code {RC}", "RC",
Riya Dixit49cfb132023-03-02 04:26:53 -0600717 lg2::hex, rc);
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930718 instanceIdDb.free(mctp_eid, instanceId);
sampmisr6decfc12021-03-02 11:07:36 +0530719 return;
720 }
721
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500722 auto getPLDMVersionHandler = [this](mctp_eid_t /*eid*/,
723 const pldm_msg* response,
724 size_t respMsgLen) {
725 if (response == nullptr || !respMsgLen)
sampmisr6decfc12021-03-02 11:07:36 +0530726 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600727 error(
728 "Failed to receive response for getPLDMVersion command, Host seems to be off");
sampmisr6decfc12021-03-02 11:07:36 +0530729 return;
730 }
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500731 info("Getting the response code '{RC}'", "RC", lg2::hex,
Riya Dixit1e5c81e2024-05-03 07:54:00 -0500732 response->payload[0]);
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500733 this->responseReceived = true;
sampmisr6decfc12021-03-02 11:07:36 +0530734 };
Sampa Misraf9ba8c12021-08-06 00:33:47 -0500735 rc = handler->registerRequest(mctp_eid, instanceId, PLDM_BASE,
736 PLDM_GET_PLDM_VERSION, std::move(requestMsg),
737 std::move(getPLDMVersionHandler));
738 if (rc)
sampmisr6decfc12021-03-02 11:07:36 +0530739 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500740 error(
741 "Failed to discover remote terminus state. Assuming remote terminus as off");
sampmisr6decfc12021-03-02 11:07:36 +0530742 }
743}
744
745bool HostPDRHandler::isHostUp()
746{
747 return responseReceived;
748}
749
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530750void HostPDRHandler::setHostSensorState(const PDRList& stateSensorPDRs)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600751{
752 for (const auto& stateSensorPDR : stateSensorPDRs)
753 {
754 auto pdr = reinterpret_cast<const pldm_state_sensor_pdr*>(
755 stateSensorPDR.data());
756
757 if (!pdr)
758 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500759 error("Failed to get state sensor PDR");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600760 pldm::utils::reportError(
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530761 "xyz.openbmc_project.bmc.pldm.InternalFailure");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600762 return;
763 }
764
765 uint16_t sensorId = pdr->sensor_id;
766
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530767 for (const auto& [terminusHandle, terminusInfo] : tlPDRInfo)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600768 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530769 if (terminusHandle == pdr->terminus_handle)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600770 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530771 if (std::get<2>(terminusInfo) == PLDM_TL_PDR_VALID)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600772 {
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530773 mctp_eid = std::get<1>(terminusInfo);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600774 }
775
776 bitfield8_t sensorRearm;
777 sensorRearm.byte = 0;
Manojkiran Eda60e1fe92021-10-08 15:58:16 +0530778 uint8_t tid = std::get<0>(terminusInfo);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600779
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930780 auto instanceId = instanceIdDb.next(mctp_eid);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600781 std::vector<uint8_t> requestMsg(
782 sizeof(pldm_msg_hdr) +
783 PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES);
784 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
785 auto rc = encode_get_state_sensor_readings_req(
786 instanceId, sensorId, sensorRearm, 0, request);
787
788 if (rc != PLDM_SUCCESS)
789 {
Andrew Jefferya330b2f2023-05-04 14:55:37 +0930790 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixit49cfb132023-03-02 04:26:53 -0600791 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500792 "Failed to encode get state sensor readings request for sensorID '{SENSOR_ID}' and instanceID '{INSTANCE}', response code '{RC}'",
793 "SENSOR_ID", sensorId, "INSTANCE", instanceId, "RC",
794 rc);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600795 pldm::utils::reportError(
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530796 "xyz.openbmc_project.bmc.pldm.InternalFailure");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600797 return;
798 }
799
Patrick Williams6da4f912023-05-10 07:50:53 -0500800 auto getStateSensorReadingRespHandler =
801 [=, this](mctp_eid_t /*eid*/, const pldm_msg* response,
802 size_t respMsgLen) {
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600803 if (response == nullptr || !respMsgLen)
804 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600805 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500806 "Failed to receive response for get state sensor reading command for sensorID '{SENSOR_ID}' and instanceID '{INSTANCE}'",
807 "SENSOR_ID", sensorId, "INSTANCE", instanceId);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600808 return;
809 }
810 std::array<get_sensor_state_field, 8> stateField{};
811 uint8_t completionCode = 0;
812 uint8_t comp_sensor_count = 0;
813
814 auto rc = decode_get_state_sensor_readings_resp(
815 response, respMsgLen, &completionCode,
816 &comp_sensor_count, stateField.data());
817
818 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
819 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600820 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500821 "Failed to decode get state sensor readings response for sensorID '{SENSOR_ID}' and instanceID '{INSTANCE}', response code'{RC}' and completion code '{CC}'",
822 "SENSOR_ID", sensorId, "INSTANCE", instanceId, "RC",
823 rc, "CC", completionCode);
Manojkiran Eda92fb0b52024-04-17 10:48:17 +0530824 pldm::utils::reportError(
825 "xyz.openbmc_project.bmc.pldm.InternalFailure");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600826 }
827
828 uint8_t eventState;
829 uint8_t previousEventState;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600830
RIYA DIXITbb5fda42022-09-27 06:48:08 -0500831 for (uint8_t sensorOffset = 0;
832 sensorOffset < comp_sensor_count; sensorOffset++)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600833 {
RIYA DIXITbb5fda42022-09-27 06:48:08 -0500834 eventState = stateField[sensorOffset].present_state;
835 previousEventState =
836 stateField[sensorOffset].previous_state;
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600837
838 emitStateSensorEventSignal(tid, sensorId, sensorOffset,
839 eventState,
840 previousEventState);
841
842 SensorEntry sensorEntry{tid, sensorId};
843
844 pldm::pdr::EntityInfo entityInfo{};
845 pldm::pdr::CompositeSensorStates
846 compositeSensorStates{};
Sagar Srinivase3607a32024-02-16 03:50:53 -0600847 std::vector<pldm::pdr::StateSetId> stateSetIds{};
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600848
849 try
850 {
Sagar Srinivase3607a32024-02-16 03:50:53 -0600851 std::tie(entityInfo, compositeSensorStates,
852 stateSetIds) =
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600853 lookupSensorInfo(sensorEntry);
854 }
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500855 catch (const std::out_of_range&)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600856 {
857 try
858 {
859 sensorEntry.terminusID = PLDM_TID_RESERVED;
Sagar Srinivase3607a32024-02-16 03:50:53 -0600860 std::tie(entityInfo, compositeSensorStates,
861 stateSetIds) =
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600862 lookupSensorInfo(sensorEntry);
863 }
Kamalkumar Patel58cbcaf2023-10-06 03:48:25 -0500864 catch (const std::out_of_range&)
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600865 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600866 error("No mapping for the events");
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600867 }
868 }
869
Manojkiran Eda07a07e22024-04-24 19:15:10 +0530870 if ((compositeSensorStates.size() > 1) &&
871 (sensorOffset > (compositeSensorStates.size() - 1)))
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600872 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500873 error(
874 "Error Invalid data, Invalid sensor offset '{SENSOR_OFFSET}'",
875 "SENSOR_OFFSET", sensorOffset);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600876 return;
877 }
878
879 const auto& possibleStates =
880 compositeSensorStates[sensorOffset];
881 if (possibleStates.find(eventState) ==
882 possibleStates.end())
883 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500884 error(
885 "Error invalid_data, Invalid event state '{STATE}'",
886 "STATE", eventState);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600887 return;
888 }
Patrick Williams6da4f912023-05-10 07:50:53 -0500889 const auto& [containerId, entityType,
890 entityInstance] = entityInfo;
Sagar Srinivase3607a32024-02-16 03:50:53 -0600891 auto stateSetId = stateSetIds[sensorOffset];
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600892 pldm::responder::events::StateSensorEntry
Manojkiran Edafa084702024-05-27 10:20:30 +0530893 stateSensorEntry{containerId, entityType,
Sagar Srinivase3607a32024-02-16 03:50:53 -0600894 entityInstance, sensorOffset,
Manojkiran Edafa084702024-05-27 10:20:30 +0530895 stateSetId, false};
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600896 handleStateSensorEvent(stateSensorEntry, eventState);
897 }
898 };
899
900 rc = handler->registerRequest(
901 mctp_eid, instanceId, PLDM_PLATFORM,
902 PLDM_GET_STATE_SENSOR_READINGS, std::move(requestMsg),
903 std::move(getStateSensorReadingRespHandler));
904
905 if (rc != PLDM_SUCCESS)
906 {
Riya Dixit49cfb132023-03-02 04:26:53 -0600907 error(
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500908 "Failed to send request to get state sensor reading on remote terminus for sensorID '{SENSOR_ID}' and instanceID '{INSTANCE}', response code '{RC}'",
909 "SENSOR_ID", sensorId, "INSTANCE", instanceId, "RC",
910 rc);
Pavithra Barithaya4f2538a2021-03-05 07:32:15 -0600911 }
912 }
913 }
914 }
915}
George Liu682ee182020-12-25 15:24:33 +0800916
917void HostPDRHandler::getFRURecordTableMetadataByRemote(
918 const PDRList& fruRecordSetPDRs)
919{
920 auto instanceId = instanceIdDb.next(mctp_eid);
921 std::vector<uint8_t> requestMsg(
922 sizeof(pldm_msg_hdr) + PLDM_GET_FRU_RECORD_TABLE_METADATA_REQ_BYTES);
923
924 // GetFruRecordTableMetadata
925 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
926 auto rc = encode_get_fru_record_table_metadata_req(
927 instanceId, request, requestMsg.size() - sizeof(pldm_msg_hdr));
928 if (rc != PLDM_SUCCESS)
929 {
930 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500931 error(
932 "Failed to encode get fru record table metadata request, response code '{RC}'",
George Liu682ee182020-12-25 15:24:33 +0800933 "RC", lg2::hex, rc);
934 return;
935 }
936
937 auto getFruRecordTableMetadataResponseHandler =
938 [this, fruRecordSetPDRs](mctp_eid_t /*eid*/, const pldm_msg* response,
939 size_t respMsgLen) {
940 if (response == nullptr || !respMsgLen)
941 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500942 error(
943 "Failed to receive response for the get fru record table metadata");
George Liu682ee182020-12-25 15:24:33 +0800944 return;
945 }
946
947 uint8_t cc = 0;
948 uint8_t fru_data_major_version, fru_data_minor_version;
949 uint32_t fru_table_maximum_size, fru_table_length;
950 uint16_t total_record_set_identifiers;
951 uint16_t total;
952 uint32_t checksum;
953
954 auto rc = decode_get_fru_record_table_metadata_resp(
955 response, respMsgLen, &cc, &fru_data_major_version,
956 &fru_data_minor_version, &fru_table_maximum_size, &fru_table_length,
957 &total_record_set_identifiers, &total, &checksum);
958
959 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
960 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500961 error(
962 "Failed to decode get fru record table metadata response, response code '{RC}' and completion code '{CC}'",
George Liu682ee182020-12-25 15:24:33 +0800963 "RC", lg2::hex, rc, "CC", cc);
964 return;
965 }
966
967 // pass total to getFRURecordTableByRemote
968 this->getFRURecordTableByRemote(fruRecordSetPDRs, total);
969 };
970
971 rc = handler->registerRequest(
972 mctp_eid, instanceId, PLDM_FRU, PLDM_GET_FRU_RECORD_TABLE_METADATA,
973 std::move(requestMsg),
974 std::move(getFruRecordTableMetadataResponseHandler));
975 if (rc != PLDM_SUCCESS)
976 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500977 error(
978 "Failed to send the the set state effecter states request, response code '{RC}'",
979 "RC", rc);
George Liu682ee182020-12-25 15:24:33 +0800980 }
981
982 return;
983}
984
985void HostPDRHandler::getFRURecordTableByRemote(const PDRList& fruRecordSetPDRs,
986 uint16_t totalTableRecords)
987{
988 fruRecordData.clear();
989
990 if (!totalTableRecords)
991 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -0500992 error("Failed to get fru record table");
George Liu682ee182020-12-25 15:24:33 +0800993 return;
994 }
995
996 auto instanceId = instanceIdDb.next(mctp_eid);
997 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
998 PLDM_GET_FRU_RECORD_TABLE_REQ_BYTES);
999
1000 // send the getFruRecordTable command
1001 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
1002 auto rc = encode_get_fru_record_table_req(
1003 instanceId, 0, PLDM_GET_FIRSTPART, request,
1004 requestMsg.size() - sizeof(pldm_msg_hdr));
1005 if (rc != PLDM_SUCCESS)
1006 {
1007 instanceIdDb.free(mctp_eid, instanceId);
Riya Dixitd6e10ad2024-03-28 19:44:16 -05001008 error(
1009 "Failed to encode get fru record table request, response code '{RC}'",
1010 "RC", lg2::hex, rc);
George Liu682ee182020-12-25 15:24:33 +08001011 return;
1012 }
1013
1014 auto getFruRecordTableResponseHandler =
1015 [totalTableRecords, this, fruRecordSetPDRs](
1016 mctp_eid_t /*eid*/, const pldm_msg* response, size_t respMsgLen) {
1017 if (response == nullptr || !respMsgLen)
1018 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -05001019 error("Failed to receive response for the get fru record table");
George Liu682ee182020-12-25 15:24:33 +08001020 return;
1021 }
1022
1023 uint8_t cc = 0;
1024 uint32_t next_data_transfer_handle = 0;
1025 uint8_t transfer_flag = 0;
1026 size_t fru_record_table_length = 0;
1027 std::vector<uint8_t> fru_record_table_data(respMsgLen -
1028 sizeof(pldm_msg_hdr));
1029 auto responsePtr = reinterpret_cast<const struct pldm_msg*>(response);
1030 auto rc = decode_get_fru_record_table_resp(
Manojkiran Eda33a9cd02024-01-22 11:06:59 +05301031 responsePtr, respMsgLen, &cc, &next_data_transfer_handle,
1032 &transfer_flag, fru_record_table_data.data(),
1033 &fru_record_table_length);
George Liu682ee182020-12-25 15:24:33 +08001034
1035 if (rc != PLDM_SUCCESS || cc != PLDM_SUCCESS)
1036 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -05001037 error(
1038 "Failed to decode get fru record table resp, response code '{RC}' and completion code '{CC}'",
George Liu682ee182020-12-25 15:24:33 +08001039 "RC", lg2::hex, rc, "CC", cc);
1040 return;
1041 }
1042
1043 fruRecordData = responder::pdr_utils::parseFruRecordTable(
1044 fru_record_table_data.data(), fru_record_table_length);
1045
1046 if (totalTableRecords != fruRecordData.size())
1047 {
1048 fruRecordData.clear();
1049
Manojkiran Eda2576aec2024-06-17 12:05:17 +05301050 error("Failed to parse fru record data format.");
George Liu682ee182020-12-25 15:24:33 +08001051 return;
1052 }
1053
1054 this->setFRUDataOnDBus(fruRecordSetPDRs, fruRecordData);
1055 };
1056
1057 rc = handler->registerRequest(
1058 mctp_eid, instanceId, PLDM_FRU, PLDM_GET_FRU_RECORD_TABLE,
1059 std::move(requestMsg), std::move(getFruRecordTableResponseHandler));
1060 if (rc != PLDM_SUCCESS)
1061 {
Riya Dixitd6e10ad2024-03-28 19:44:16 -05001062 error("Failed to send the the set state effecter states request");
George Liu682ee182020-12-25 15:24:33 +08001063 }
1064}
1065
1066std::optional<uint16_t> HostPDRHandler::getRSI(const PDRList& fruRecordSetPDRs,
1067 const pldm_entity& entity)
1068{
1069 for (const auto& pdr : fruRecordSetPDRs)
1070 {
1071 auto fruPdr = reinterpret_cast<const pldm_pdr_fru_record_set*>(
1072 const_cast<uint8_t*>(pdr.data()) + sizeof(pldm_pdr_hdr));
1073
1074 if (fruPdr->entity_type == entity.entity_type &&
Sagar Srinivasebf8bb52023-10-06 01:15:55 -05001075 fruPdr->entity_instance == entity.entity_instance_num &&
1076 fruPdr->container_id == entity.entity_container_id)
George Liu682ee182020-12-25 15:24:33 +08001077 {
1078 return fruPdr->fru_rsi;
1079 }
1080 }
1081
1082 return std::nullopt;
1083}
1084
1085void HostPDRHandler::setFRUDataOnDBus(
Patrick Williamsd310f822023-10-07 19:01:19 -05001086 [[maybe_unused]] const PDRList& fruRecordSetPDRs,
1087 [[maybe_unused]] const std::vector<
1088 responder::pdr_utils::FruRecordDataFormat>& fruRecordData)
George Liu682ee182020-12-25 15:24:33 +08001089{
Patrick Williamsd310f822023-10-07 19:01:19 -05001090#ifdef OEM_IBM
George Liu682ee182020-12-25 15:24:33 +08001091 for (const auto& entity : objPathMap)
1092 {
1093 pldm_entity node = pldm_entity_extract(entity.second);
1094 auto fruRSI = getRSI(fruRecordSetPDRs, node);
1095
1096 for (const auto& data : fruRecordData)
1097 {
1098 if (!fruRSI || *fruRSI != data.fruRSI)
1099 {
1100 continue;
1101 }
1102
1103 if (data.fruRecType == PLDM_FRU_RECORD_TYPE_OEM)
1104 {
1105 for (const auto& tlv : data.fruTLV)
1106 {
1107 if (tlv.fruFieldType ==
1108 PLDM_OEM_FRU_FIELD_TYPE_LOCATION_CODE)
1109 {
1110 CustomDBus::getCustomDBus().setLocationCode(
1111 entity.first,
1112 std::string(reinterpret_cast<const char*>(
1113 tlv.fruFieldValue.data()),
1114 tlv.fruFieldLen));
1115 }
1116 }
1117 }
1118 }
1119 }
Patrick Williamsd310f822023-10-07 19:01:19 -05001120#endif
George Liu682ee182020-12-25 15:24:33 +08001121}
1122void HostPDRHandler::createDbusObjects(const PDRList& fruRecordSetPDRs)
1123{
1124 // TODO: Creating and Refreshing dbus hosted by remote PLDM entity Fru PDRs
Kamalkumar Patel56da5742024-05-23 04:53:07 -05001125 for (const auto& entity : objPathMap)
1126 {
1127 pldm_entity node = pldm_entity_extract(entity.second);
1128 switch (node.entity_type)
1129 {
1130 case PLDM_ENTITY_PROC | 0x8000:
1131 CustomDBus::getCustomDBus().implementCpuCoreInterface(
1132 entity.first);
1133 break;
Archana Kakanibf1fd272024-06-05 13:25:53 -05001134 case PLDM_ENTITY_SLOT:
1135 CustomDBus::getCustomDBus().implementPCIeSlotInterface(
1136 entity.first);
1137 break;
Archana Kakani733b39d2024-06-05 21:05:20 -05001138 case PLDM_ENTITY_CARD:
1139 CustomDBus::getCustomDBus().implementPCIeDeviceInterface(
1140 entity.first);
1141 break;
1142 default:
1143 break;
Kamalkumar Patel56da5742024-05-23 04:53:07 -05001144 }
1145 }
George Liu682ee182020-12-25 15:24:33 +08001146 getFRURecordTableMetadataByRemote(fruRecordSetPDRs);
1147}
1148
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05001149} // namespace pldm