blob: 2e8a0a31abc17e35ce5e2dea927fdaae58c42915 [file] [log] [blame]
Deepak Kodihalli87514cc2020-04-16 09:08:38 -05001#include "config.h"
2
Pavithra Barithaya51efaf82020-04-02 02:42:27 -05003#include "host_pdr_handler.hpp"
4
George Liu6492f522020-06-16 10:34:05 +08005#include "libpldm/requester/pldm.h"
6
Pavithra Barithayae8beb892020-04-14 23:24:25 -05007#include <assert.h>
8
Deepak Kodihalli87514cc2020-04-16 09:08:38 -05009#include <nlohmann/json.hpp>
10
George Liu6492f522020-06-16 10:34:05 +080011#include <fstream>
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050012
13namespace pldm
14{
15
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050016using namespace pldm::utils;
17using namespace sdbusplus::bus::match::rules;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050018using Json = nlohmann::json;
19namespace fs = std::filesystem;
20constexpr auto fruJson = "host_frus.json";
21const Json emptyJson{};
22const std::vector<Json> emptyJsonList{};
23
24HostPDRHandler::HostPDRHandler(int mctp_fd, uint8_t mctp_eid,
25 sdeventplus::Event& event, pldm_pdr* repo,
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060026 const std::string& eventsJsonsDir,
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050027 pldm_entity_association_tree* entityTree,
28 Requester& requester) :
29 mctp_fd(mctp_fd),
Pavithra Barithaya3aec9972020-12-14 01:55:44 -060030 mctp_eid(mctp_eid), event(event), repo(repo),
31 stateSensorHandler(eventsJsonsDir), entityTree(entityTree),
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050032 requester(requester)
33{
34 fs::path hostFruJson(fs::path(HOST_JSONS_DIR) / fruJson);
35 if (fs::exists(hostFruJson))
36 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -050037 // Note parent entities for entities sent down by the host firmware.
38 // This will enable a merge of entity associations.
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050039 try
40 {
41 std::ifstream jsonFile(hostFruJson);
42 auto data = Json::parse(jsonFile, nullptr, false);
43 if (data.is_discarded())
44 {
45 std::cerr << "Parsing Host FRU json file failed" << std::endl;
46 }
47 else
48 {
49 auto entities = data.value("entities", emptyJsonList);
50 for (auto& entity : entities)
51 {
52 EntityType entityType = entity.value("entity_type", 0);
53 auto parent = entity.value("parent", emptyJson);
54 pldm_entity p{};
55 p.entity_type = parent.value("entity_type", 0);
56 p.entity_instance_num = parent.value("entity_instance", 0);
57 parents.emplace(entityType, std::move(p));
58 }
59 }
60 }
61 catch (const std::exception& e)
62 {
63 std::cerr << "Parsing Host FRU json file failed, exception = "
64 << e.what() << std::endl;
65 }
66 }
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050067
68 hostOffMatch = std::make_unique<sdbusplus::bus::match::match>(
69 pldm::utils::DBusHandler::getBus(),
70 propertiesChanged("/xyz/openbmc_project/state/host0",
71 "xyz.openbmc_project.State.Host"),
Tom Josephb4268602020-04-17 17:20:45 +053072 [this, repo](sdbusplus::message::message& msg) {
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050073 DbusChangedProps props{};
74 std::string intf;
75 msg.read(intf, props);
76 const auto itr = props.find("CurrentHostState");
77 if (itr != props.end())
78 {
79 PropertyValue value = itr->second;
80 auto propVal = std::get<std::string>(value);
81 if (propVal == "xyz.openbmc_project.State.Host.HostState.Off")
82 {
83 pldm_pdr_remove_remote_pdrs(repo);
Tom Josephb4268602020-04-17 17:20:45 +053084 this->sensorMap.clear();
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050085 }
86 }
87 });
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050088}
89
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -050090void HostPDRHandler::fetchPDR(PDRRecordHandles&& recordHandles)
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050091{
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050092 pdrRecordHandles.clear();
93 pdrRecordHandles = std::move(recordHandles);
94
95 // Defer the actual fetch of PDRs from the host (by queuing the call on the
96 // main event loop). That way, we can respond to the platform event msg from
97 // the host firmware.
98 pdrFetchEvent = std::make_unique<sdeventplus::source::Defer>(
99 event, std::bind(std::mem_fn(&HostPDRHandler::_fetchPDR), this,
100 std::placeholders::_1));
101}
102
103void HostPDRHandler::_fetchPDR(sdeventplus::source::EventBase& /*source*/)
104{
105 pdrFetchEvent.reset();
106
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500107 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
108 PLDM_GET_PDR_REQ_BYTES);
109 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500110 bool merged = false;
Sampa Misra868c8792020-05-26 03:12:13 -0500111 PDRList stateSensorPDRs{};
112 TLPDRMap tlpdrInfo{};
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500113
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500114 uint32_t nextRecordHandle{};
115 uint32_t recordHandle{};
116 bool isFormatRecHandles = false;
117 if (!pdrRecordHandles.empty())
118 {
119 recordHandle = pdrRecordHandles.front();
120 pdrRecordHandles.pop_front();
121 isFormatRecHandles = true;
122 }
123
124 do
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500125 {
126 auto instanceId = requester.getInstanceId(mctp_eid);
127
128 auto rc =
129 encode_get_pdr_req(instanceId, recordHandle, 0, PLDM_GET_FIRSTPART,
130 UINT16_MAX, 0, request, PLDM_GET_PDR_REQ_BYTES);
131 if (rc != PLDM_SUCCESS)
132 {
133 requester.markFree(mctp_eid, instanceId);
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500134 std::cerr << "Failed to encode_get_pdr_req, rc = " << rc
135 << std::endl;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500136 return;
137 }
138
139 uint8_t* responseMsg = nullptr;
140 size_t responseMsgSize{};
141 auto requesterRc =
142 pldm_send_recv(mctp_eid, mctp_fd, requestMsg.data(),
143 requestMsg.size(), &responseMsg, &responseMsgSize);
144 std::unique_ptr<uint8_t, decltype(std::free)*> responseMsgPtr{
145 responseMsg, std::free};
146 requester.markFree(mctp_eid, instanceId);
147 if (requesterRc != PLDM_REQUESTER_SUCCESS)
148 {
149 std::cerr << "Failed to send msg to fetch pdrs, rc = "
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500150 << requesterRc << std::endl;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500151 return;
152 }
153
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500154 uint8_t completionCode{};
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500155 uint32_t nextDataTransferHandle{};
156 uint8_t transferFlag{};
157 uint16_t respCount{};
158 uint8_t transferCRC{};
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500159 auto responsePtr =
160 reinterpret_cast<struct pldm_msg*>(responseMsgPtr.get());
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500161 rc = decode_get_pdr_resp(
162 responsePtr, responseMsgSize - sizeof(pldm_msg_hdr),
163 &completionCode, &nextRecordHandle, &nextDataTransferHandle,
164 &transferFlag, &respCount, nullptr, 0, &transferCRC);
165 if (rc != PLDM_SUCCESS)
166 {
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500167 std::cerr << "Failed to decode_get_pdr_resp, rc = " << rc
168 << std::endl;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500169 }
170 else
171 {
172 std::vector<uint8_t> pdr(respCount, 0);
173 rc = decode_get_pdr_resp(
174 responsePtr, responseMsgSize - sizeof(pldm_msg_hdr),
175 &completionCode, &nextRecordHandle, &nextDataTransferHandle,
176 &transferFlag, &respCount, pdr.data(), respCount, &transferCRC);
177 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
178 {
179 std::cerr << "Failed to decode_get_pdr_resp: "
180 << "rc=" << rc
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500181 << ", cc=" << static_cast<unsigned>(completionCode)
182 << std::endl;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500183 }
184 else
185 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500186 // Process the PDR host firmware sent us. The most common action
187 // is to add the PDR to the the BMC's PDR repo.
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500188 auto pdrHdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
189 if (pdrHdr->type == PLDM_PDR_ENTITY_ASSOCIATION)
190 {
191 mergeEntityAssociations(pdr);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500192 merged = true;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500193 }
194 else
195 {
Sampa Misra868c8792020-05-26 03:12:13 -0500196 if (pdrHdr->type == PLDM_TERMINUS_LOCATOR_PDR)
Tom Josephb4268602020-04-17 17:20:45 +0530197 {
Sampa Misra868c8792020-05-26 03:12:13 -0500198 auto tlpdr =
199 reinterpret_cast<const pldm_terminus_locator_pdr*>(
200 pdr.data());
201 tlpdrInfo.emplace(
202 static_cast<pdr::TerminusHandle>(
203 tlpdr->terminus_handle),
204 static_cast<pdr::TerminusID>(tlpdr->tid));
205 }
206 else if (pdrHdr->type == PLDM_STATE_SENSOR_PDR)
207 {
208 stateSensorPDRs.emplace_back(pdr);
Tom Josephb4268602020-04-17 17:20:45 +0530209 }
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500210 pldm_pdr_add(repo, pdr.data(), respCount, 0, true);
211 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500212 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500213
214 recordHandle = nextRecordHandle;
215 if (!pdrRecordHandles.empty())
216 {
217 recordHandle = pdrRecordHandles.front();
218 pdrRecordHandles.pop_front();
219 }
220 else if (isFormatRecHandles)
221 {
222 break;
223 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500224 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500225 } while (recordHandle);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500226
Sampa Misra868c8792020-05-26 03:12:13 -0500227 parseStateSensorPDRs(stateSensorPDRs, tlpdrInfo);
228
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500229 if (merged)
230 {
231 // We have merged host's entity association PDRs with our own. Send an
232 // event to the host firmware to indicate the same.
233 sendPDRRepositoryChgEvent(
234 std::move(std::vector<uint8_t>(1, PLDM_PDR_ENTITY_ASSOCIATION)),
235 FORMAT_IS_PDR_HANDLES);
236 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500237}
238
Pavithra Barithaya3aec9972020-12-14 01:55:44 -0600239int HostPDRHandler::handleStateSensorEvent(const StateSensorEntry& entry,
240 pdr::EventState state)
241{
242 auto rc = stateSensorHandler.eventAction(entry, state);
243 if (rc != PLDM_SUCCESS)
244 {
245 std::cerr << "Failed to fetch and update D-bus property, rc = " << rc
246 << std::endl;
247 return rc;
248 }
249 return PLDM_SUCCESS;
250}
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500251bool HostPDRHandler::getParent(EntityType type, pldm_entity& parent)
252{
253 auto found = parents.find(type);
254 if (found != parents.end())
255 {
256 parent.entity_type = found->second.entity_type;
257 parent.entity_instance_num = found->second.entity_instance_num;
258 return true;
259 }
260
261 return false;
262}
263
264void HostPDRHandler::mergeEntityAssociations(const std::vector<uint8_t>& pdr)
265{
266 size_t numEntities{};
267 pldm_entity* entities = nullptr;
268 bool merged = false;
269 auto entityPdr = reinterpret_cast<pldm_pdr_entity_association*>(
270 const_cast<uint8_t*>(pdr.data()) + sizeof(pldm_pdr_hdr));
271
272 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &numEntities,
273 &entities);
274 for (size_t i = 0; i < numEntities; ++i)
275 {
276 pldm_entity parent{};
277 if (getParent(entities[i].entity_type, parent))
278 {
279 auto node = pldm_entity_association_tree_find(entityTree, &parent);
280 if (node)
281 {
282 pldm_entity_association_tree_add(entityTree, &entities[i], node,
283 entityPdr->association_type);
284 merged = true;
285 }
286 }
287 }
288 free(entities);
289
290 if (merged)
291 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500292 // Update our PDR repo with the merged entity association PDRs
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500293 pldm_entity_association_pdr_add(entityTree, repo, true);
294 }
295}
296
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500297void HostPDRHandler::sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
298 uint8_t eventDataFormat)
299{
300 assert(eventDataFormat == FORMAT_IS_PDR_HANDLES);
301
302 // Extract from the PDR repo record handles of PDRs we want the host
303 // to pull up.
304 std::vector<uint8_t> eventDataOps{PLDM_RECORDS_ADDED};
305 std::vector<uint8_t> numsOfChangeEntries(1);
306 std::vector<std::vector<ChangeEntry>> changeEntries(
307 numsOfChangeEntries.size());
308 for (auto pdrType : pdrTypes)
309 {
310 const pldm_pdr_record* record{};
311 do
312 {
313 record = pldm_pdr_find_record_by_type(repo, pdrType, record,
314 nullptr, nullptr);
315 if (record && pldm_pdr_record_is_remote(record))
316 {
317 changeEntries[0].push_back(
318 pldm_pdr_get_record_handle(repo, record));
319 }
320 } while (record);
321 }
322 if (changeEntries.empty())
323 {
324 return;
325 }
326 numsOfChangeEntries[0] = changeEntries[0].size();
327
328 // Encode PLDM platform event msg to indicate a PDR repo change.
329 size_t maxSize = PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH +
330 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH +
331 changeEntries[0].size() * sizeof(uint32_t);
332 std::vector<uint8_t> eventDataVec{};
333 eventDataVec.resize(maxSize);
334 auto eventData =
335 reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>(
336 eventDataVec.data());
337 size_t actualSize{};
338 auto firstEntry = changeEntries[0].data();
339 auto rc = encode_pldm_pdr_repository_chg_event_data(
340 eventDataFormat, 1, eventDataOps.data(), numsOfChangeEntries.data(),
341 &firstEntry, eventData, &actualSize, maxSize);
342 if (rc != PLDM_SUCCESS)
343 {
344 std::cerr
345 << "Failed to encode_pldm_pdr_repository_chg_event_data, rc = "
346 << rc << std::endl;
347 return;
348 }
349 auto instanceId = requester.getInstanceId(mctp_eid);
350 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
351 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
352 actualSize);
353 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
354 rc = encode_platform_event_message_req(
355 instanceId, 1, 0, PLDM_PDR_REPOSITORY_CHG_EVENT, eventDataVec.data(),
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500356 actualSize, request,
357 actualSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500358 if (rc != PLDM_SUCCESS)
359 {
360 requester.markFree(mctp_eid, instanceId);
361 std::cerr << "Failed to encode_platform_event_message_req, rc = " << rc
362 << std::endl;
363 return;
364 }
365
366 // Send up the event to host.
367 uint8_t* responseMsg = nullptr;
368 size_t responseMsgSize{};
369 auto requesterRc =
370 pldm_send_recv(mctp_eid, mctp_fd, requestMsg.data(), requestMsg.size(),
371 &responseMsg, &responseMsgSize);
372 requester.markFree(mctp_eid, instanceId);
373 if (requesterRc != PLDM_REQUESTER_SUCCESS)
374 {
375 std::cerr << "Failed to send msg to report pdrs, rc = " << requesterRc
376 << std::endl;
377 return;
378 }
379 uint8_t completionCode{};
380 uint8_t status{};
381 auto responsePtr = reinterpret_cast<struct pldm_msg*>(responseMsg);
382 rc = decode_platform_event_message_resp(
383 responsePtr, responseMsgSize - sizeof(pldm_msg_hdr), &completionCode,
384 &status);
385 free(responseMsg);
386 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
387 {
388 std::cerr << "Failed to decode_platform_event_message_resp: "
389 << "rc=" << rc
390 << ", cc=" << static_cast<unsigned>(completionCode)
391 << std::endl;
392 }
393}
394
Sampa Misra868c8792020-05-26 03:12:13 -0500395void HostPDRHandler::parseStateSensorPDRs(const PDRList& stateSensorPDRs,
396 const TLPDRMap& tlpdrInfo)
397{
398 for (const auto& pdr : stateSensorPDRs)
399 {
400 SensorEntry sensorEntry{};
401 const auto& [terminusHandle, sensorID, sensorInfo] =
402 responder::pdr_utils::parseStateSensorPDR(pdr);
403 sensorEntry.sensorID = sensorID;
404 try
405 {
406 sensorEntry.terminusID = tlpdrInfo.at(terminusHandle);
407 }
408 // If there is no mapping for terminusHandle assign the reserved TID
409 // value of 0xFF to indicate that.
410 catch (const std::out_of_range& e)
411 {
412 sensorEntry.terminusID = PLDM_TID_RESERVED;
413 }
414 sensorMap.emplace(sensorEntry, std::move(sensorInfo));
415 }
416}
417
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500418} // namespace pldm