blob: f8594d6609e65a22258d1992de438a34a4f00f97 [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,
26 pldm_entity_association_tree* entityTree,
27 Requester& requester) :
28 mctp_fd(mctp_fd),
29 mctp_eid(mctp_eid), event(event), repo(repo), entityTree(entityTree),
30 requester(requester)
31{
32 fs::path hostFruJson(fs::path(HOST_JSONS_DIR) / fruJson);
33 if (fs::exists(hostFruJson))
34 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -050035 // Note parent entities for entities sent down by the host firmware.
36 // This will enable a merge of entity associations.
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050037 try
38 {
39 std::ifstream jsonFile(hostFruJson);
40 auto data = Json::parse(jsonFile, nullptr, false);
41 if (data.is_discarded())
42 {
43 std::cerr << "Parsing Host FRU json file failed" << std::endl;
44 }
45 else
46 {
47 auto entities = data.value("entities", emptyJsonList);
48 for (auto& entity : entities)
49 {
50 EntityType entityType = entity.value("entity_type", 0);
51 auto parent = entity.value("parent", emptyJson);
52 pldm_entity p{};
53 p.entity_type = parent.value("entity_type", 0);
54 p.entity_instance_num = parent.value("entity_instance", 0);
55 parents.emplace(entityType, std::move(p));
56 }
57 }
58 }
59 catch (const std::exception& e)
60 {
61 std::cerr << "Parsing Host FRU json file failed, exception = "
62 << e.what() << std::endl;
63 }
64 }
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050065
66 hostOffMatch = std::make_unique<sdbusplus::bus::match::match>(
67 pldm::utils::DBusHandler::getBus(),
68 propertiesChanged("/xyz/openbmc_project/state/host0",
69 "xyz.openbmc_project.State.Host"),
Tom Josephb4268602020-04-17 17:20:45 +053070 [this, repo](sdbusplus::message::message& msg) {
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050071 DbusChangedProps props{};
72 std::string intf;
73 msg.read(intf, props);
74 const auto itr = props.find("CurrentHostState");
75 if (itr != props.end())
76 {
77 PropertyValue value = itr->second;
78 auto propVal = std::get<std::string>(value);
79 if (propVal == "xyz.openbmc_project.State.Host.HostState.Off")
80 {
81 pldm_pdr_remove_remote_pdrs(repo);
Tom Josephb4268602020-04-17 17:20:45 +053082 this->sensorMap.clear();
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050083 }
84 }
85 });
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050086}
87
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -050088void HostPDRHandler::fetchPDR(PDRRecordHandles&& recordHandles)
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050089{
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050090 pdrRecordHandles.clear();
91 pdrRecordHandles = std::move(recordHandles);
92
93 // Defer the actual fetch of PDRs from the host (by queuing the call on the
94 // main event loop). That way, we can respond to the platform event msg from
95 // the host firmware.
96 pdrFetchEvent = std::make_unique<sdeventplus::source::Defer>(
97 event, std::bind(std::mem_fn(&HostPDRHandler::_fetchPDR), this,
98 std::placeholders::_1));
99}
100
101void HostPDRHandler::_fetchPDR(sdeventplus::source::EventBase& /*source*/)
102{
103 pdrFetchEvent.reset();
104
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500105 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
106 PLDM_GET_PDR_REQ_BYTES);
107 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500108 bool merged = false;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500109
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500110 uint32_t nextRecordHandle{};
111 uint32_t recordHandle{};
112 bool isFormatRecHandles = false;
113 if (!pdrRecordHandles.empty())
114 {
115 recordHandle = pdrRecordHandles.front();
116 pdrRecordHandles.pop_front();
117 isFormatRecHandles = true;
118 }
119
120 do
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500121 {
122 auto instanceId = requester.getInstanceId(mctp_eid);
123
124 auto rc =
125 encode_get_pdr_req(instanceId, recordHandle, 0, PLDM_GET_FIRSTPART,
126 UINT16_MAX, 0, request, PLDM_GET_PDR_REQ_BYTES);
127 if (rc != PLDM_SUCCESS)
128 {
129 requester.markFree(mctp_eid, instanceId);
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500130 std::cerr << "Failed to encode_get_pdr_req, rc = " << rc
131 << std::endl;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500132 return;
133 }
134
135 uint8_t* responseMsg = nullptr;
136 size_t responseMsgSize{};
137 auto requesterRc =
138 pldm_send_recv(mctp_eid, mctp_fd, requestMsg.data(),
139 requestMsg.size(), &responseMsg, &responseMsgSize);
140 std::unique_ptr<uint8_t, decltype(std::free)*> responseMsgPtr{
141 responseMsg, std::free};
142 requester.markFree(mctp_eid, instanceId);
143 if (requesterRc != PLDM_REQUESTER_SUCCESS)
144 {
145 std::cerr << "Failed to send msg to fetch pdrs, rc = "
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500146 << requesterRc << std::endl;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500147 return;
148 }
149
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500150 uint8_t completionCode{};
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500151 uint32_t nextDataTransferHandle{};
152 uint8_t transferFlag{};
153 uint16_t respCount{};
154 uint8_t transferCRC{};
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500155 auto responsePtr =
156 reinterpret_cast<struct pldm_msg*>(responseMsgPtr.get());
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500157 rc = decode_get_pdr_resp(
158 responsePtr, responseMsgSize - sizeof(pldm_msg_hdr),
159 &completionCode, &nextRecordHandle, &nextDataTransferHandle,
160 &transferFlag, &respCount, nullptr, 0, &transferCRC);
161 if (rc != PLDM_SUCCESS)
162 {
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500163 std::cerr << "Failed to decode_get_pdr_resp, rc = " << rc
164 << std::endl;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500165 }
166 else
167 {
168 std::vector<uint8_t> pdr(respCount, 0);
169 rc = decode_get_pdr_resp(
170 responsePtr, responseMsgSize - sizeof(pldm_msg_hdr),
171 &completionCode, &nextRecordHandle, &nextDataTransferHandle,
172 &transferFlag, &respCount, pdr.data(), respCount, &transferCRC);
173 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
174 {
175 std::cerr << "Failed to decode_get_pdr_resp: "
176 << "rc=" << rc
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500177 << ", cc=" << static_cast<unsigned>(completionCode)
178 << std::endl;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500179 }
180 else
181 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500182 // Process the PDR host firmware sent us. The most common action
183 // is to add the PDR to the the BMC's PDR repo.
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500184 auto pdrHdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
185 if (pdrHdr->type == PLDM_PDR_ENTITY_ASSOCIATION)
186 {
187 mergeEntityAssociations(pdr);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500188 merged = true;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500189 }
190 else
191 {
Tom Josephb4268602020-04-17 17:20:45 +0530192 if (pdrHdr->type == PLDM_STATE_SENSOR_PDR)
193 {
194 const auto& [terminusHandle, sensorID, sensorInfo] =
195 responder::pdr_utils::parseStateSensorPDR(pdr);
196 SensorEntry sensorEntry{};
197 // TODO: Lookup Terminus ID from the Terminus Locator
198 // PDR with terminusHandle
199 sensorEntry.terminusID = 0;
200 sensorEntry.sensorID = sensorID;
201 sensorMap.emplace(sensorEntry, std::move(sensorInfo));
202 }
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500203 pldm_pdr_add(repo, pdr.data(), respCount, 0, true);
204 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500205 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500206
207 recordHandle = nextRecordHandle;
208 if (!pdrRecordHandles.empty())
209 {
210 recordHandle = pdrRecordHandles.front();
211 pdrRecordHandles.pop_front();
212 }
213 else if (isFormatRecHandles)
214 {
215 break;
216 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500217 }
Deepak Kodihalli7246e0c2020-07-08 06:40:18 -0500218 } while (recordHandle);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500219
220 if (merged)
221 {
222 // We have merged host's entity association PDRs with our own. Send an
223 // event to the host firmware to indicate the same.
224 sendPDRRepositoryChgEvent(
225 std::move(std::vector<uint8_t>(1, PLDM_PDR_ENTITY_ASSOCIATION)),
226 FORMAT_IS_PDR_HANDLES);
227 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500228}
229
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500230bool HostPDRHandler::getParent(EntityType type, pldm_entity& parent)
231{
232 auto found = parents.find(type);
233 if (found != parents.end())
234 {
235 parent.entity_type = found->second.entity_type;
236 parent.entity_instance_num = found->second.entity_instance_num;
237 return true;
238 }
239
240 return false;
241}
242
243void HostPDRHandler::mergeEntityAssociations(const std::vector<uint8_t>& pdr)
244{
245 size_t numEntities{};
246 pldm_entity* entities = nullptr;
247 bool merged = false;
248 auto entityPdr = reinterpret_cast<pldm_pdr_entity_association*>(
249 const_cast<uint8_t*>(pdr.data()) + sizeof(pldm_pdr_hdr));
250
251 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &numEntities,
252 &entities);
253 for (size_t i = 0; i < numEntities; ++i)
254 {
255 pldm_entity parent{};
256 if (getParent(entities[i].entity_type, parent))
257 {
258 auto node = pldm_entity_association_tree_find(entityTree, &parent);
259 if (node)
260 {
261 pldm_entity_association_tree_add(entityTree, &entities[i], node,
262 entityPdr->association_type);
263 merged = true;
264 }
265 }
266 }
267 free(entities);
268
269 if (merged)
270 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500271 // Update our PDR repo with the merged entity association PDRs
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500272 pldm_entity_association_pdr_add(entityTree, repo, true);
273 }
274}
275
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500276void HostPDRHandler::sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
277 uint8_t eventDataFormat)
278{
279 assert(eventDataFormat == FORMAT_IS_PDR_HANDLES);
280
281 // Extract from the PDR repo record handles of PDRs we want the host
282 // to pull up.
283 std::vector<uint8_t> eventDataOps{PLDM_RECORDS_ADDED};
284 std::vector<uint8_t> numsOfChangeEntries(1);
285 std::vector<std::vector<ChangeEntry>> changeEntries(
286 numsOfChangeEntries.size());
287 for (auto pdrType : pdrTypes)
288 {
289 const pldm_pdr_record* record{};
290 do
291 {
292 record = pldm_pdr_find_record_by_type(repo, pdrType, record,
293 nullptr, nullptr);
294 if (record && pldm_pdr_record_is_remote(record))
295 {
296 changeEntries[0].push_back(
297 pldm_pdr_get_record_handle(repo, record));
298 }
299 } while (record);
300 }
301 if (changeEntries.empty())
302 {
303 return;
304 }
305 numsOfChangeEntries[0] = changeEntries[0].size();
306
307 // Encode PLDM platform event msg to indicate a PDR repo change.
308 size_t maxSize = PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH +
309 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH +
310 changeEntries[0].size() * sizeof(uint32_t);
311 std::vector<uint8_t> eventDataVec{};
312 eventDataVec.resize(maxSize);
313 auto eventData =
314 reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>(
315 eventDataVec.data());
316 size_t actualSize{};
317 auto firstEntry = changeEntries[0].data();
318 auto rc = encode_pldm_pdr_repository_chg_event_data(
319 eventDataFormat, 1, eventDataOps.data(), numsOfChangeEntries.data(),
320 &firstEntry, eventData, &actualSize, maxSize);
321 if (rc != PLDM_SUCCESS)
322 {
323 std::cerr
324 << "Failed to encode_pldm_pdr_repository_chg_event_data, rc = "
325 << rc << std::endl;
326 return;
327 }
328 auto instanceId = requester.getInstanceId(mctp_eid);
329 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
330 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
331 actualSize);
332 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
333 rc = encode_platform_event_message_req(
334 instanceId, 1, 0, PLDM_PDR_REPOSITORY_CHG_EVENT, eventDataVec.data(),
Christian Geddes3bdb3c22020-05-01 14:55:39 -0500335 actualSize, request,
336 actualSize + PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500337 if (rc != PLDM_SUCCESS)
338 {
339 requester.markFree(mctp_eid, instanceId);
340 std::cerr << "Failed to encode_platform_event_message_req, rc = " << rc
341 << std::endl;
342 return;
343 }
344
345 // Send up the event to host.
346 uint8_t* responseMsg = nullptr;
347 size_t responseMsgSize{};
348 auto requesterRc =
349 pldm_send_recv(mctp_eid, mctp_fd, requestMsg.data(), requestMsg.size(),
350 &responseMsg, &responseMsgSize);
351 requester.markFree(mctp_eid, instanceId);
352 if (requesterRc != PLDM_REQUESTER_SUCCESS)
353 {
354 std::cerr << "Failed to send msg to report pdrs, rc = " << requesterRc
355 << std::endl;
356 return;
357 }
358 uint8_t completionCode{};
359 uint8_t status{};
360 auto responsePtr = reinterpret_cast<struct pldm_msg*>(responseMsg);
361 rc = decode_platform_event_message_resp(
362 responsePtr, responseMsgSize - sizeof(pldm_msg_hdr), &completionCode,
363 &status);
364 free(responseMsg);
365 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
366 {
367 std::cerr << "Failed to decode_platform_event_message_resp: "
368 << "rc=" << rc
369 << ", cc=" << static_cast<unsigned>(completionCode)
370 << std::endl;
371 }
372}
373
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500374} // namespace pldm