blob: 17b0b88d4df51552607b0098c0731431ba0a3111 [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
Pavithra Barithayae8beb892020-04-14 23:24:25 -05005#include <assert.h>
6
Deepak Kodihalli87514cc2020-04-16 09:08:38 -05007#include <fstream>
8#include <nlohmann/json.hpp>
9
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050010#include "libpldm/requester/pldm.h"
11
12namespace pldm
13{
14
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050015using namespace pldm::utils;
16using namespace sdbusplus::bus::match::rules;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050017using Json = nlohmann::json;
18namespace fs = std::filesystem;
19constexpr auto fruJson = "host_frus.json";
20const Json emptyJson{};
21const std::vector<Json> emptyJsonList{};
22
23HostPDRHandler::HostPDRHandler(int mctp_fd, uint8_t mctp_eid,
24 sdeventplus::Event& event, pldm_pdr* repo,
25 pldm_entity_association_tree* entityTree,
26 Requester& requester) :
27 mctp_fd(mctp_fd),
28 mctp_eid(mctp_eid), event(event), repo(repo), entityTree(entityTree),
29 requester(requester)
30{
31 fs::path hostFruJson(fs::path(HOST_JSONS_DIR) / fruJson);
32 if (fs::exists(hostFruJson))
33 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -050034 // Note parent entities for entities sent down by the host firmware.
35 // This will enable a merge of entity associations.
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050036 try
37 {
38 std::ifstream jsonFile(hostFruJson);
39 auto data = Json::parse(jsonFile, nullptr, false);
40 if (data.is_discarded())
41 {
42 std::cerr << "Parsing Host FRU json file failed" << std::endl;
43 }
44 else
45 {
46 auto entities = data.value("entities", emptyJsonList);
47 for (auto& entity : entities)
48 {
49 EntityType entityType = entity.value("entity_type", 0);
50 auto parent = entity.value("parent", emptyJson);
51 pldm_entity p{};
52 p.entity_type = parent.value("entity_type", 0);
53 p.entity_instance_num = parent.value("entity_instance", 0);
54 parents.emplace(entityType, std::move(p));
55 }
56 }
57 }
58 catch (const std::exception& e)
59 {
60 std::cerr << "Parsing Host FRU json file failed, exception = "
61 << e.what() << std::endl;
62 }
63 }
Deepak Kodihalli6b1d1ca2020-04-27 07:24:51 -050064
65 hostOffMatch = std::make_unique<sdbusplus::bus::match::match>(
66 pldm::utils::DBusHandler::getBus(),
67 propertiesChanged("/xyz/openbmc_project/state/host0",
68 "xyz.openbmc_project.State.Host"),
69 [repo](sdbusplus::message::message& msg) {
70 DbusChangedProps props{};
71 std::string intf;
72 msg.read(intf, props);
73 const auto itr = props.find("CurrentHostState");
74 if (itr != props.end())
75 {
76 PropertyValue value = itr->second;
77 auto propVal = std::get<std::string>(value);
78 if (propVal == "xyz.openbmc_project.State.Host.HostState.Off")
79 {
80 pldm_pdr_remove_remote_pdrs(repo);
81 }
82 }
83 });
Deepak Kodihalli87514cc2020-04-16 09:08:38 -050084}
85
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050086void HostPDRHandler::fetchPDR(std::vector<uint32_t>&& recordHandles)
Pavithra Barithaya51efaf82020-04-02 02:42:27 -050087{
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -050088 pdrRecordHandles.clear();
89 pdrRecordHandles = std::move(recordHandles);
90
91 // Defer the actual fetch of PDRs from the host (by queuing the call on the
92 // main event loop). That way, we can respond to the platform event msg from
93 // the host firmware.
94 pdrFetchEvent = std::make_unique<sdeventplus::source::Defer>(
95 event, std::bind(std::mem_fn(&HostPDRHandler::_fetchPDR), this,
96 std::placeholders::_1));
97}
98
99void HostPDRHandler::_fetchPDR(sdeventplus::source::EventBase& /*source*/)
100{
101 pdrFetchEvent.reset();
102
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500103 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
104 PLDM_GET_PDR_REQ_BYTES);
105 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500106 bool merged = false;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500107
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500108 for (auto recordHandle : pdrRecordHandles)
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500109 {
110 auto instanceId = requester.getInstanceId(mctp_eid);
111
112 auto rc =
113 encode_get_pdr_req(instanceId, recordHandle, 0, PLDM_GET_FIRSTPART,
114 UINT16_MAX, 0, request, PLDM_GET_PDR_REQ_BYTES);
115 if (rc != PLDM_SUCCESS)
116 {
117 requester.markFree(mctp_eid, instanceId);
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500118 std::cerr << "Failed to encode_get_pdr_req, rc = " << rc
119 << std::endl;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500120 return;
121 }
122
123 uint8_t* responseMsg = nullptr;
124 size_t responseMsgSize{};
125 auto requesterRc =
126 pldm_send_recv(mctp_eid, mctp_fd, requestMsg.data(),
127 requestMsg.size(), &responseMsg, &responseMsgSize);
128 std::unique_ptr<uint8_t, decltype(std::free)*> responseMsgPtr{
129 responseMsg, std::free};
130 requester.markFree(mctp_eid, instanceId);
131 if (requesterRc != PLDM_REQUESTER_SUCCESS)
132 {
133 std::cerr << "Failed to send msg to fetch pdrs, rc = "
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500134 << requesterRc << std::endl;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500135 return;
136 }
137
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500138 uint8_t completionCode{};
139 uint32_t nextRecordHandle{};
140 uint32_t nextDataTransferHandle{};
141 uint8_t transferFlag{};
142 uint16_t respCount{};
143 uint8_t transferCRC{};
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500144 auto responsePtr =
145 reinterpret_cast<struct pldm_msg*>(responseMsgPtr.get());
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500146 rc = decode_get_pdr_resp(
147 responsePtr, responseMsgSize - sizeof(pldm_msg_hdr),
148 &completionCode, &nextRecordHandle, &nextDataTransferHandle,
149 &transferFlag, &respCount, nullptr, 0, &transferCRC);
150 if (rc != PLDM_SUCCESS)
151 {
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500152 std::cerr << "Failed to decode_get_pdr_resp, rc = " << rc
153 << std::endl;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500154 }
155 else
156 {
157 std::vector<uint8_t> pdr(respCount, 0);
158 rc = decode_get_pdr_resp(
159 responsePtr, responseMsgSize - sizeof(pldm_msg_hdr),
160 &completionCode, &nextRecordHandle, &nextDataTransferHandle,
161 &transferFlag, &respCount, pdr.data(), respCount, &transferCRC);
162 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
163 {
164 std::cerr << "Failed to decode_get_pdr_resp: "
165 << "rc=" << rc
Deepak Kodihalli8cb6f662020-04-10 02:55:43 -0500166 << ", cc=" << static_cast<unsigned>(completionCode)
167 << std::endl;
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500168 }
169 else
170 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500171 // Process the PDR host firmware sent us. The most common action
172 // is to add the PDR to the the BMC's PDR repo.
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500173 auto pdrHdr = reinterpret_cast<pldm_pdr_hdr*>(pdr.data());
174 if (pdrHdr->type == PLDM_PDR_ENTITY_ASSOCIATION)
175 {
176 mergeEntityAssociations(pdr);
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500177 merged = true;
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500178 }
179 else
180 {
181 pldm_pdr_add(repo, pdr.data(), respCount, 0, true);
182 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500183 }
184 }
185 }
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500186
187 if (merged)
188 {
189 // We have merged host's entity association PDRs with our own. Send an
190 // event to the host firmware to indicate the same.
191 sendPDRRepositoryChgEvent(
192 std::move(std::vector<uint8_t>(1, PLDM_PDR_ENTITY_ASSOCIATION)),
193 FORMAT_IS_PDR_HANDLES);
194 }
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500195}
196
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500197bool HostPDRHandler::getParent(EntityType type, pldm_entity& parent)
198{
199 auto found = parents.find(type);
200 if (found != parents.end())
201 {
202 parent.entity_type = found->second.entity_type;
203 parent.entity_instance_num = found->second.entity_instance_num;
204 return true;
205 }
206
207 return false;
208}
209
210void HostPDRHandler::mergeEntityAssociations(const std::vector<uint8_t>& pdr)
211{
212 size_t numEntities{};
213 pldm_entity* entities = nullptr;
214 bool merged = false;
215 auto entityPdr = reinterpret_cast<pldm_pdr_entity_association*>(
216 const_cast<uint8_t*>(pdr.data()) + sizeof(pldm_pdr_hdr));
217
218 pldm_entity_association_pdr_extract(pdr.data(), pdr.size(), &numEntities,
219 &entities);
220 for (size_t i = 0; i < numEntities; ++i)
221 {
222 pldm_entity parent{};
223 if (getParent(entities[i].entity_type, parent))
224 {
225 auto node = pldm_entity_association_tree_find(entityTree, &parent);
226 if (node)
227 {
228 pldm_entity_association_tree_add(entityTree, &entities[i], node,
229 entityPdr->association_type);
230 merged = true;
231 }
232 }
233 }
234 free(entities);
235
236 if (merged)
237 {
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500238 // Update our PDR repo with the merged entity association PDRs
Deepak Kodihalli87514cc2020-04-16 09:08:38 -0500239 pldm_entity_association_pdr_add(entityTree, repo, true);
240 }
241}
242
Pavithra Barithayae8beb892020-04-14 23:24:25 -0500243void HostPDRHandler::sendPDRRepositoryChgEvent(std::vector<uint8_t>&& pdrTypes,
244 uint8_t eventDataFormat)
245{
246 assert(eventDataFormat == FORMAT_IS_PDR_HANDLES);
247
248 // Extract from the PDR repo record handles of PDRs we want the host
249 // to pull up.
250 std::vector<uint8_t> eventDataOps{PLDM_RECORDS_ADDED};
251 std::vector<uint8_t> numsOfChangeEntries(1);
252 std::vector<std::vector<ChangeEntry>> changeEntries(
253 numsOfChangeEntries.size());
254 for (auto pdrType : pdrTypes)
255 {
256 const pldm_pdr_record* record{};
257 do
258 {
259 record = pldm_pdr_find_record_by_type(repo, pdrType, record,
260 nullptr, nullptr);
261 if (record && pldm_pdr_record_is_remote(record))
262 {
263 changeEntries[0].push_back(
264 pldm_pdr_get_record_handle(repo, record));
265 }
266 } while (record);
267 }
268 if (changeEntries.empty())
269 {
270 return;
271 }
272 numsOfChangeEntries[0] = changeEntries[0].size();
273
274 // Encode PLDM platform event msg to indicate a PDR repo change.
275 size_t maxSize = PLDM_PDR_REPOSITORY_CHG_EVENT_MIN_LENGTH +
276 PLDM_PDR_REPOSITORY_CHANGE_RECORD_MIN_LENGTH +
277 changeEntries[0].size() * sizeof(uint32_t);
278 std::vector<uint8_t> eventDataVec{};
279 eventDataVec.resize(maxSize);
280 auto eventData =
281 reinterpret_cast<struct pldm_pdr_repository_chg_event_data*>(
282 eventDataVec.data());
283 size_t actualSize{};
284 auto firstEntry = changeEntries[0].data();
285 auto rc = encode_pldm_pdr_repository_chg_event_data(
286 eventDataFormat, 1, eventDataOps.data(), numsOfChangeEntries.data(),
287 &firstEntry, eventData, &actualSize, maxSize);
288 if (rc != PLDM_SUCCESS)
289 {
290 std::cerr
291 << "Failed to encode_pldm_pdr_repository_chg_event_data, rc = "
292 << rc << std::endl;
293 return;
294 }
295 auto instanceId = requester.getInstanceId(mctp_eid);
296 std::vector<uint8_t> requestMsg(sizeof(pldm_msg_hdr) +
297 PLDM_PLATFORM_EVENT_MESSAGE_MIN_REQ_BYTES +
298 actualSize);
299 auto request = reinterpret_cast<pldm_msg*>(requestMsg.data());
300 rc = encode_platform_event_message_req(
301 instanceId, 1, 0, PLDM_PDR_REPOSITORY_CHG_EVENT, eventDataVec.data(),
302 actualSize, request);
303 if (rc != PLDM_SUCCESS)
304 {
305 requester.markFree(mctp_eid, instanceId);
306 std::cerr << "Failed to encode_platform_event_message_req, rc = " << rc
307 << std::endl;
308 return;
309 }
310
311 // Send up the event to host.
312 uint8_t* responseMsg = nullptr;
313 size_t responseMsgSize{};
314 auto requesterRc =
315 pldm_send_recv(mctp_eid, mctp_fd, requestMsg.data(), requestMsg.size(),
316 &responseMsg, &responseMsgSize);
317 requester.markFree(mctp_eid, instanceId);
318 if (requesterRc != PLDM_REQUESTER_SUCCESS)
319 {
320 std::cerr << "Failed to send msg to report pdrs, rc = " << requesterRc
321 << std::endl;
322 return;
323 }
324 uint8_t completionCode{};
325 uint8_t status{};
326 auto responsePtr = reinterpret_cast<struct pldm_msg*>(responseMsg);
327 rc = decode_platform_event_message_resp(
328 responsePtr, responseMsgSize - sizeof(pldm_msg_hdr), &completionCode,
329 &status);
330 free(responseMsg);
331 if (rc != PLDM_SUCCESS || completionCode != PLDM_SUCCESS)
332 {
333 std::cerr << "Failed to decode_platform_event_message_resp: "
334 << "rc=" << rc
335 << ", cc=" << static_cast<unsigned>(completionCode)
336 << std::endl;
337 }
338}
339
Pavithra Barithaya51efaf82020-04-02 02:42:27 -0500340} // namespace pldm