blob: 3703a02ce9eba94ae989599b756f7a46c75f0e77 [file] [log] [blame]
Patrick Williams05e95592021-09-02 09:28:14 -05001#include "pldm.hpp"
2
Rashmica Guptadb38e912023-05-25 10:33:46 +10003#include "libpldm/instance-id.h"
4
Patrick Williams05e95592021-09-02 09:28:14 -05005#include "file.hpp"
6
Patrick Williams05e95592021-09-02 09:28:14 -05007#include <libpldm/entity.h>
Andrew Jeffery97476a12024-06-19 21:07:29 +09308#include <libpldm/oem/ibm/state_set.h>
Patrick Williams05e95592021-09-02 09:28:14 -05009#include <libpldm/platform.h>
10#include <libpldm/state_set.h>
Rashmica Gupta52328cb2023-02-15 10:38:16 +110011#include <libpldm/transport.h>
Lakshmi Yadlapati6213f192024-07-01 11:50:09 -050012#include <libpldm/transport/af-mctp.h>
Rashmica Gupta52328cb2023-02-15 10:38:16 +110013#include <libpldm/transport/mctp-demux.h>
14#include <poll.h>
Patrick Williams05e95592021-09-02 09:28:14 -050015
16#include <phosphor-logging/log.hpp>
Chris Cainbae4d072022-02-28 09:46:50 -060017#include <sdbusplus/bus.hpp>
18#include <sdeventplus/clock.hpp>
19#include <sdeventplus/exception.hpp>
20#include <sdeventplus/source/io.hpp>
21#include <sdeventplus/source/time.hpp>
22
23#include <algorithm>
Patrick Williams48002492024-02-13 21:43:32 -060024#include <format>
Patrick Williams05e95592021-09-02 09:28:14 -050025
26namespace pldm
27{
28
Patrick Williams05e95592021-09-02 09:28:14 -050029using namespace phosphor::logging;
30
Chris Cainbae4d072022-02-28 09:46:50 -060031using namespace sdeventplus;
32using namespace sdeventplus::source;
33constexpr auto clockId = sdeventplus::ClockId::RealTime;
34using Clock = sdeventplus::Clock<clockId>;
35using Timer = Time<clockId>;
Chris Cain755af102024-02-27 16:09:51 -060036bool Interface::throttleTraces = false;
Chris Cainbae4d072022-02-28 09:46:50 -060037
Eddie Jamescbad2192021-10-07 09:39:39 -050038void Interface::fetchSensorInfo(uint16_t stateSetId,
39 SensorToInstance& sensorInstanceMap,
40 SensorOffset& sensorOffset)
Patrick Williams05e95592021-09-02 09:28:14 -050041{
Eddie Jamescbad2192021-10-07 09:39:39 -050042 PdrList pdrs{};
Chris Cainbae4d072022-02-28 09:46:50 -060043 static bool tracedError = false;
Eddie Jamescbad2192021-10-07 09:39:39 -050044
45 auto& bus = open_power::occ::utils::getBus();
46 try
47 {
48 auto method = bus.new_method_call(
49 "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm",
50 "xyz.openbmc_project.PLDM.PDR", "FindStateSensorPDR");
Chris Cainbae4d072022-02-28 09:46:50 -060051 method.append(tid, static_cast<uint16_t>(PLDM_ENTITY_PROC), stateSetId);
Eddie Jamescbad2192021-10-07 09:39:39 -050052
53 auto responseMsg = bus.call(method);
54 responseMsg.read(pdrs);
55 }
Patrick Williamsaf408082022-07-22 19:26:54 -050056 catch (const sdbusplus::exception_t& e)
Eddie Jamescbad2192021-10-07 09:39:39 -050057 {
Chris Cainbae4d072022-02-28 09:46:50 -060058 if (!tracedError)
59 {
60 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -060061 std::format(
Chris Cainbae4d072022-02-28 09:46:50 -060062 "fetchSensorInfo: Failed to find stateSetID:{} PDR: {}",
63 stateSetId, e.what())
64 .c_str());
65 tracedError = true;
66 }
Eddie Jamescbad2192021-10-07 09:39:39 -050067 }
68
69 if (pdrs.empty())
70 {
Chris Cainbae4d072022-02-28 09:46:50 -060071 if (!tracedError)
72 {
73 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -060074 std::format(
Chris Cainbae4d072022-02-28 09:46:50 -060075 "fetchSensorInfo: state sensor PDRs ({}) not present",
76 stateSetId)
77 .c_str());
78 tracedError = true;
79 }
Eddie Jamescbad2192021-10-07 09:39:39 -050080 return;
81 }
82
Chris Cainbae4d072022-02-28 09:46:50 -060083 // Found PDR
84 if (tracedError)
85 {
86 log<level::INFO>(
Patrick Williams48002492024-02-13 21:43:32 -060087 std::format("fetchSensorInfo: found {} PDRs", pdrs.size()).c_str());
Chris Cainbae4d072022-02-28 09:46:50 -060088 tracedError = false;
89 }
90
Patrick Williams05e95592021-09-02 09:28:14 -050091 bool offsetFound = false;
George Liuf3a4a692021-12-28 13:59:51 +080092 auto stateSensorPDR =
Patrick Williams05e95592021-09-02 09:28:14 -050093 reinterpret_cast<const pldm_state_sensor_pdr*>(pdrs.front().data());
George Liuf3a4a692021-12-28 13:59:51 +080094 auto possibleStatesPtr = stateSensorPDR->possible_states;
95 for (auto offset = 0; offset < stateSensorPDR->composite_sensor_count;
96 offset++)
Patrick Williams05e95592021-09-02 09:28:14 -050097 {
98 auto possibleStates =
99 reinterpret_cast<const state_sensor_possible_states*>(
100 possibleStatesPtr);
101
Eddie Jamescbad2192021-10-07 09:39:39 -0500102 if (possibleStates->state_set_id == stateSetId)
Patrick Williams05e95592021-09-02 09:28:14 -0500103 {
104 sensorOffset = offset;
105 offsetFound = true;
106 break;
107 }
108 possibleStatesPtr += sizeof(possibleStates->state_set_id) +
109 sizeof(possibleStates->possible_states_size) +
110 possibleStates->possible_states_size;
111 }
112
113 if (!offsetFound)
114 {
Eddie Jamescbad2192021-10-07 09:39:39 -0500115 log<level::ERR>("pldm: state sensor PDR not found");
Patrick Williams05e95592021-09-02 09:28:14 -0500116 return;
117 }
118
119 // To order SensorID based on the EntityInstance.
120 // Note that when a proc is on a DCM, the PDRs for these sensors
121 // could have the same instance IDs but different container IDs.
122 std::map<uint32_t, SensorID> entityInstMap{};
123 for (auto& pdr : pdrs)
124 {
125 auto pdrPtr =
126 reinterpret_cast<const pldm_state_sensor_pdr*>(pdr.data());
Chris Cain72d01aa2022-06-14 16:28:03 -0500127 uint32_t key = pdrPtr->sensor_id;
Patrick Williams05e95592021-09-02 09:28:14 -0500128 entityInstMap.emplace(key, static_cast<SensorID>(pdrPtr->sensor_id));
129 }
130
131 open_power::occ::instanceID count = start;
Patrick Williamsa49c9872023-05-10 07:50:35 -0500132 for (const auto& pair : entityInstMap)
Patrick Williams05e95592021-09-02 09:28:14 -0500133 {
134 sensorInstanceMap.emplace(pair.second, count);
135 count++;
136 }
137}
138
Patrick Williamsaf408082022-07-22 19:26:54 -0500139void Interface::sensorEvent(sdbusplus::message_t& msg)
Patrick Williams05e95592021-09-02 09:28:14 -0500140{
141 if (!isOCCSensorCacheValid())
142 {
Eddie Jamescbad2192021-10-07 09:39:39 -0500143 fetchSensorInfo(PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS,
144 sensorToOCCInstance, OCCSensorOffset);
145 }
Patrick Williams05e95592021-09-02 09:28:14 -0500146
Eddie Jamescbad2192021-10-07 09:39:39 -0500147 if (sensorToSBEInstance.empty())
148 {
149 fetchSensorInfo(PLDM_OEM_IBM_SBE_HRESET_STATE, sensorToSBEInstance,
150 SBESensorOffset);
Patrick Williams05e95592021-09-02 09:28:14 -0500151 }
152
Chris Cain72d01aa2022-06-14 16:28:03 -0500153 TerminusID sensorTid{};
Patrick Williams05e95592021-09-02 09:28:14 -0500154 SensorID sensorId{};
155 SensorOffset msgSensorOffset{};
156 EventState eventState{};
157 EventState previousEventState{};
158
Chris Cain72d01aa2022-06-14 16:28:03 -0500159 msg.read(sensorTid, sensorId, msgSensorOffset, eventState,
160 previousEventState);
Patrick Williams05e95592021-09-02 09:28:14 -0500161
Eddie Jamescbad2192021-10-07 09:39:39 -0500162 if (msgSensorOffset == OCCSensorOffset)
Patrick Williams05e95592021-09-02 09:28:14 -0500163 {
Eddie Jamescbad2192021-10-07 09:39:39 -0500164 auto sensorEntry = sensorToOCCInstance.find(sensorId);
Patrick Williams05e95592021-09-02 09:28:14 -0500165
Eddie James432dc482021-11-19 15:29:31 -0600166 if (sensorEntry != sensorToOCCInstance.end())
Eddie Jamescbad2192021-10-07 09:39:39 -0500167 {
Chris Cain8b508bf2022-05-26 14:01:31 -0500168 const uint8_t instance = sensorEntry->second;
Chris Cain755af102024-02-27 16:09:51 -0600169 bool validEvent = true;
Chris Cain7b00cde2023-03-14 15:47:12 -0500170 bool isRunning = false;
Eddie James432dc482021-11-19 15:29:31 -0600171 if (eventState ==
172 static_cast<EventState>(
173 PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_IN_SERVICE))
174 {
175 log<level::INFO>(
Patrick Williams48002492024-02-13 21:43:32 -0600176 std::format("PLDM: OCC{} is RUNNING", instance).c_str());
Chris Cain7b00cde2023-03-14 15:47:12 -0500177 isRunning = true;
Eddie James432dc482021-11-19 15:29:31 -0600178 }
179 else if (eventState ==
180 static_cast<EventState>(
181 PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_STOPPED))
182 {
Chris Cain8b508bf2022-05-26 14:01:31 -0500183 log<level::INFO>(
Patrick Williams48002492024-02-13 21:43:32 -0600184 std::format("PLDM: OCC{} has now STOPPED", instance)
Chris Cain8b508bf2022-05-26 14:01:31 -0500185 .c_str());
Eddie James432dc482021-11-19 15:29:31 -0600186 }
Chris Cainbae4d072022-02-28 09:46:50 -0600187 else if (eventState ==
188 static_cast<EventState>(
189 PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_DORMANT))
190 {
Chris Cain755af102024-02-27 16:09:51 -0600191 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600192 std::format(
Chris Cainbae4d072022-02-28 09:46:50 -0600193 "PLDM: OCC{} has now STOPPED and system is in SAFE MODE",
Chris Cain8b508bf2022-05-26 14:01:31 -0500194 instance)
Chris Cainbae4d072022-02-28 09:46:50 -0600195 .c_str());
Sheldon Bailey31a2f132022-05-20 11:31:52 -0500196
197 // Setting safe mode true
198 safeModeCallBack(true);
Chris Cainbae4d072022-02-28 09:46:50 -0600199 }
200 else
201 {
Chris Cain755af102024-02-27 16:09:51 -0600202 log<level::WARNING>(
Patrick Williams48002492024-02-13 21:43:32 -0600203 std::format("PLDM: Unexpected PLDM state {} for OCC{}",
Chris Cain8b508bf2022-05-26 14:01:31 -0500204 eventState, instance)
Chris Cainbae4d072022-02-28 09:46:50 -0600205 .c_str());
Chris Cain755af102024-02-27 16:09:51 -0600206 validEvent = false;
Chris Cainbae4d072022-02-28 09:46:50 -0600207 }
Chris Cain755af102024-02-27 16:09:51 -0600208 if (validEvent)
209 {
210 if ((pldmFd > 0) && (instance == pldmResponseOcc))
211 {
212 // Waiting for a response for this OCC, can stop waiting
213 pldmClose();
214 }
215 callBack(instance, isRunning);
216 }
Eddie Jamescbad2192021-10-07 09:39:39 -0500217 return;
218 }
Eddie Jamescbad2192021-10-07 09:39:39 -0500219 }
Eddie James432dc482021-11-19 15:29:31 -0600220
221 if (msgSensorOffset == SBESensorOffset)
Eddie Jamescbad2192021-10-07 09:39:39 -0500222 {
223 auto sensorEntry = sensorToSBEInstance.find(sensorId);
224
Eddie James432dc482021-11-19 15:29:31 -0600225 if (sensorEntry != sensorToSBEInstance.end())
Eddie Jamescbad2192021-10-07 09:39:39 -0500226 {
Chris Cain12d0b822022-04-22 17:29:18 -0500227 const uint8_t instance = sensorEntry->second;
228 auto match = std::find(outstandingHResets.begin(),
229 outstandingHResets.end(), instance);
230 if (match != outstandingHResets.end())
Eddie James432dc482021-11-19 15:29:31 -0600231 {
Chris Cain12d0b822022-04-22 17:29:18 -0500232 outstandingHResets.erase(match);
233 if (eventState == static_cast<EventState>(SBE_HRESET_NOT_READY))
234 {
235 log<level::INFO>(
Patrick Williams48002492024-02-13 21:43:32 -0600236 std::format("pldm: HRESET is NOT READY (OCC{})",
Chris Cain12d0b822022-04-22 17:29:18 -0500237 instance)
238 .c_str());
239 }
240 else if (eventState ==
241 static_cast<EventState>(SBE_HRESET_READY))
242 {
243 sbeCallBack(instance, true);
244 }
245 else if (eventState ==
246 static_cast<EventState>(SBE_HRESET_FAILED))
247 {
248 sbeCallBack(instance, false);
249 }
Eddie James432dc482021-11-19 15:29:31 -0600250 }
Chris Cain12d0b822022-04-22 17:29:18 -0500251 // else request was not from us
Eddie Jamescbad2192021-10-07 09:39:39 -0500252 }
253 }
Patrick Williams05e95592021-09-02 09:28:14 -0500254}
255
Patrick Williamsaf408082022-07-22 19:26:54 -0500256void Interface::hostStateEvent(sdbusplus::message_t& msg)
Chris Cain157467d2022-06-24 11:25:23 -0500257{
258 std::map<std::string, std::variant<std::string>> properties{};
259 std::string interface;
260 msg.read(interface, properties);
261 const auto stateEntry = properties.find("CurrentHostState");
262 if (stateEntry != properties.end())
263 {
264 auto stateEntryValue = stateEntry->second;
265 auto propVal = std::get<std::string>(stateEntryValue);
266 if (propVal == "xyz.openbmc_project.State.Host.HostState.Off")
267 {
268 clearData();
269 }
270 }
271}
272
Chris Cainbae4d072022-02-28 09:46:50 -0600273void Interface::clearData()
274{
Chris Cain72d01aa2022-06-14 16:28:03 -0500275 if (!sensorToOCCInstance.empty())
276 {
277 log<level::INFO>(
Patrick Williams48002492024-02-13 21:43:32 -0600278 std::format("clearData: Clearing sensorToOCCInstance ({} entries)",
Chris Cain72d01aa2022-06-14 16:28:03 -0500279 sensorToOCCInstance.size())
280 .c_str());
281 for (auto entry : sensorToOCCInstance)
282 {
283 log<level::INFO>(
Patrick Williams48002492024-02-13 21:43:32 -0600284 std::format("clearData: OCC{} / sensorID: 0x{:04X}",
Chris Cain72d01aa2022-06-14 16:28:03 -0500285 entry.second, entry.first)
286 .c_str());
Chris Cain082a6ca2023-03-21 10:27:26 -0500287 callBack(entry.second, false);
Chris Cain72d01aa2022-06-14 16:28:03 -0500288 }
289 sensorToOCCInstance.clear();
290 }
291 if (!occInstanceToEffecter.empty())
292 {
293 log<level::DEBUG>(
Patrick Williams48002492024-02-13 21:43:32 -0600294 std::format(
Chris Cain72d01aa2022-06-14 16:28:03 -0500295 "clearData: Clearing occInstanceToEffecter ({} entries)",
296 occInstanceToEffecter.size())
297 .c_str());
298 occInstanceToEffecter.clear();
299 }
300 if (!sensorToSBEInstance.empty())
301 {
302 log<level::DEBUG>(
Patrick Williams48002492024-02-13 21:43:32 -0600303 std::format("clearData: Clearing sensorToSBEInstance ({} entries)",
Chris Cain72d01aa2022-06-14 16:28:03 -0500304 sensorToSBEInstance.size())
305 .c_str());
306 sensorToSBEInstance.clear();
307 }
308 if (!sbeInstanceToEffecter.empty())
309 {
310 log<level::DEBUG>(
Patrick Williams48002492024-02-13 21:43:32 -0600311 std::format(
Chris Cain72d01aa2022-06-14 16:28:03 -0500312 "clearData: Clearing sbeInstanceToEffecter ({} entries)",
313 sbeInstanceToEffecter.size())
314 .c_str());
315 sbeInstanceToEffecter.clear();
316 }
Chris Cainbae4d072022-02-28 09:46:50 -0600317}
318
Patrick Williamsd7542c82024-08-16 15:20:28 -0400319void Interface::fetchEffecterInfo(
320 uint16_t stateSetId, InstanceToEffecter& instanceToEffecterMap,
321 CompositeEffecterCount& effecterCount, uint8_t& stateIdPos)
Patrick Williams05e95592021-09-02 09:28:14 -0500322{
Eddie Jamescbad2192021-10-07 09:39:39 -0500323 PdrList pdrs{};
324
325 auto& bus = open_power::occ::utils::getBus();
326 try
327 {
328 auto method = bus.new_method_call(
329 "xyz.openbmc_project.PLDM", "/xyz/openbmc_project/pldm",
330 "xyz.openbmc_project.PLDM.PDR", "FindStateEffecterPDR");
Chris Cainbae4d072022-02-28 09:46:50 -0600331 method.append(tid, static_cast<uint16_t>(PLDM_ENTITY_PROC), stateSetId);
Eddie Jamescbad2192021-10-07 09:39:39 -0500332
333 auto responseMsg = bus.call(method);
334 responseMsg.read(pdrs);
335 }
Patrick Williamsaf408082022-07-22 19:26:54 -0500336 catch (const sdbusplus::exception_t& e)
Eddie Jamescbad2192021-10-07 09:39:39 -0500337 {
338 log<level::ERR>("pldm: Failed to fetch the state effecter PDRs",
339 entry("ERROR=%s", e.what()));
340 }
341
342 if (!pdrs.size())
343 {
344 log<level::ERR>("pldm: state effecter PDRs not present");
345 return;
346 }
347
Patrick Williams05e95592021-09-02 09:28:14 -0500348 bool offsetFound = false;
George Liuf3a4a692021-12-28 13:59:51 +0800349 auto stateEffecterPDR =
Patrick Williams05e95592021-09-02 09:28:14 -0500350 reinterpret_cast<const pldm_state_effecter_pdr*>(pdrs.front().data());
George Liuf3a4a692021-12-28 13:59:51 +0800351 auto possibleStatesPtr = stateEffecterPDR->possible_states;
352 for (auto offset = 0; offset < stateEffecterPDR->composite_effecter_count;
353 offset++)
Patrick Williams05e95592021-09-02 09:28:14 -0500354 {
355 auto possibleStates =
356 reinterpret_cast<const state_effecter_possible_states*>(
357 possibleStatesPtr);
358
Eddie Jamescbad2192021-10-07 09:39:39 -0500359 if (possibleStates->state_set_id == stateSetId)
Patrick Williams05e95592021-09-02 09:28:14 -0500360 {
Eddie Jamescbad2192021-10-07 09:39:39 -0500361 stateIdPos = offset;
George Liuf3a4a692021-12-28 13:59:51 +0800362 effecterCount = stateEffecterPDR->composite_effecter_count;
Patrick Williams05e95592021-09-02 09:28:14 -0500363 offsetFound = true;
364 break;
365 }
366 possibleStatesPtr += sizeof(possibleStates->state_set_id) +
367 sizeof(possibleStates->possible_states_size) +
368 possibleStates->possible_states_size;
369 }
370
371 if (!offsetFound)
372 {
373 return;
374 }
375
Chris Cain0f516522022-02-07 14:48:28 -0600376 std::map<uint32_t, EffecterID> entityInstMap{};
Patrick Williams05e95592021-09-02 09:28:14 -0500377 for (auto& pdr : pdrs)
378 {
379 auto pdrPtr =
380 reinterpret_cast<const pldm_state_effecter_pdr*>(pdr.data());
Chris Cain72d01aa2022-06-14 16:28:03 -0500381 uint32_t key = pdrPtr->effecter_id;
Patrick Williams05e95592021-09-02 09:28:14 -0500382 entityInstMap.emplace(key, static_cast<SensorID>(pdrPtr->effecter_id));
383 }
384
385 open_power::occ::instanceID position = start;
Patrick Williamsa49c9872023-05-10 07:50:35 -0500386 for (const auto& pair : entityInstMap)
Patrick Williams05e95592021-09-02 09:28:14 -0500387 {
Eddie Jamescbad2192021-10-07 09:39:39 -0500388 instanceToEffecterMap.emplace(position, pair.second);
Patrick Williams05e95592021-09-02 09:28:14 -0500389 position++;
390 }
391}
392
Patrick Williamsd7542c82024-08-16 15:20:28 -0400393std::vector<uint8_t> Interface::prepareSetEffecterReq(
394 EffecterID effecterId, CompositeEffecterCount effecterCount,
395 uint8_t stateIdPos, uint8_t stateSetValue)
Patrick Williams05e95592021-09-02 09:28:14 -0500396{
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100397 if (!getPldmInstanceId())
Chris Cain8b508bf2022-05-26 14:01:31 -0500398 {
399 return std::vector<uint8_t>();
400 }
401
Patrick Williams05e95592021-09-02 09:28:14 -0500402 std::vector<uint8_t> request(
403 sizeof(pldm_msg_hdr) + sizeof(effecterId) + sizeof(effecterCount) +
404 (effecterCount * sizeof(set_effecter_state_field)));
405 auto requestMsg = reinterpret_cast<pldm_msg*>(request.data());
406 std::vector<set_effecter_state_field> stateField;
407
408 for (uint8_t effecterPos = 0; effecterPos < effecterCount; effecterPos++)
409 {
Eddie Jamescbad2192021-10-07 09:39:39 -0500410 if (effecterPos == stateIdPos)
Patrick Williams05e95592021-09-02 09:28:14 -0500411 {
Eddie Jamescbad2192021-10-07 09:39:39 -0500412 stateField.emplace_back(
413 set_effecter_state_field{PLDM_REQUEST_SET, stateSetValue});
Patrick Williams05e95592021-09-02 09:28:14 -0500414 }
415 else
416 {
417 stateField.emplace_back(
418 set_effecter_state_field{PLDM_NO_CHANGE, 0});
419 }
420 }
421 auto rc = encode_set_state_effecter_states_req(
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100422 pldmInstanceID.value(), effecterId, effecterCount, stateField.data(),
Chris Cain8b508bf2022-05-26 14:01:31 -0500423 requestMsg);
Patrick Williams05e95592021-09-02 09:28:14 -0500424 if (rc != PLDM_SUCCESS)
425 {
426 log<level::ERR>("encode set effecter states request returned error ",
427 entry("RC=%d", rc));
428 request.clear();
429 }
430 return request;
431}
432
433void Interface::resetOCC(open_power::occ::instanceID occInstanceId)
434{
Chris Cainbae4d072022-02-28 09:46:50 -0600435 if (open_power::occ::utils::isHostRunning())
Patrick Williams05e95592021-09-02 09:28:14 -0500436 {
Chris Cainbae4d072022-02-28 09:46:50 -0600437 if (!isPDREffecterCacheValid())
438 {
439 fetchEffecterInfo(PLDM_STATE_SET_BOOT_RESTART_CAUSE,
440 occInstanceToEffecter, OCCEffecterCount,
441 bootRestartPosition);
442 }
Patrick Williams05e95592021-09-02 09:28:14 -0500443
Chris Cainbae4d072022-02-28 09:46:50 -0600444 // Find the matching effecter for the OCC instance
445 auto effecterEntry = occInstanceToEffecter.find(occInstanceId);
446 if (effecterEntry == occInstanceToEffecter.end())
447 {
448 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600449 std::format(
Chris Cainbae4d072022-02-28 09:46:50 -0600450 "pldm: Failed to find a matching effecter for OCC instance {}",
451 occInstanceId)
452 .c_str());
453
454 return;
455 }
456
Chris Cainbae4d072022-02-28 09:46:50 -0600457 // Prepare the SetStateEffecterStates request to reset the OCC
458 auto request = prepareSetEffecterReq(
Chris Cain8b508bf2022-05-26 14:01:31 -0500459 effecterEntry->second, OCCEffecterCount, bootRestartPosition,
460 PLDM_STATE_SET_BOOT_RESTART_CAUSE_WARM_RESET);
Chris Cainbae4d072022-02-28 09:46:50 -0600461
462 if (request.empty())
463 {
464 log<level::ERR>(
465 "pldm: SetStateEffecterStates OCC reset request empty");
466 return;
467 }
468
469 // Send request to reset the OCCs/PM Complex (ignore response)
470 sendPldm(request, occInstanceId, false);
471 }
472 else
Patrick Williams05e95592021-09-02 09:28:14 -0500473 {
474 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600475 std::format("resetOCC: HOST is not running (OCC{})", occInstanceId)
Chris Cain0f516522022-02-07 14:48:28 -0600476 .c_str());
Chris Cainbae4d072022-02-28 09:46:50 -0600477 clearData();
Patrick Williams05e95592021-09-02 09:28:14 -0500478 }
Eddie Jamescbad2192021-10-07 09:39:39 -0500479}
480
481void Interface::sendHRESET(open_power::occ::instanceID sbeInstanceId)
482{
Chris Cainbae4d072022-02-28 09:46:50 -0600483 if (open_power::occ::utils::isHostRunning())
Eddie Jamescbad2192021-10-07 09:39:39 -0500484 {
Chris Cainbae4d072022-02-28 09:46:50 -0600485 if (sbeInstanceToEffecter.empty())
486 {
487 fetchEffecterInfo(PLDM_OEM_IBM_SBE_MAINTENANCE_STATE,
488 sbeInstanceToEffecter, SBEEffecterCount,
489 sbeMaintenanceStatePosition);
490 }
Eddie Jamescbad2192021-10-07 09:39:39 -0500491
Chris Cainbae4d072022-02-28 09:46:50 -0600492 auto effecterEntry = sbeInstanceToEffecter.find(sbeInstanceId);
493 if (effecterEntry == sbeInstanceToEffecter.end())
494 {
495 log<level::ERR>(
496 "pldm: Failed to find a matching effecter for SBE instance",
497 entry("SBE=%d", sbeInstanceId));
498 return;
499 }
500
Chris Cainbae4d072022-02-28 09:46:50 -0600501 // Prepare the SetStateEffecterStates request to HRESET the SBE
502 auto request = prepareSetEffecterReq(
Chris Cain8b508bf2022-05-26 14:01:31 -0500503 effecterEntry->second, SBEEffecterCount,
Chris Cainbae4d072022-02-28 09:46:50 -0600504 sbeMaintenanceStatePosition, SBE_RETRY_REQUIRED);
505
506 if (request.empty())
507 {
508 log<level::ERR>(
509 "pldm: SetStateEffecterStates HRESET request empty");
510 return;
511 }
512
513 // Send request to issue HRESET of SBE (ignore response)
514 sendPldm(request, sbeInstanceId, false);
Chris Cain12d0b822022-04-22 17:29:18 -0500515 outstandingHResets.insert(sbeInstanceId);
Chris Cainbae4d072022-02-28 09:46:50 -0600516 }
517 else
Eddie Jamescbad2192021-10-07 09:39:39 -0500518 {
Patrick Williams48002492024-02-13 21:43:32 -0600519 log<level::ERR>(std::format("sendHRESET: HOST is not running (OCC{})",
Chris Cainbae4d072022-02-28 09:46:50 -0600520 sbeInstanceId)
521 .c_str());
522 clearData();
Eddie Jamescbad2192021-10-07 09:39:39 -0500523 }
Eddie Jamescbad2192021-10-07 09:39:39 -0500524}
525
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100526bool Interface::getPldmInstanceId()
Eddie Jamescbad2192021-10-07 09:39:39 -0500527{
Rashmica Guptadb38e912023-05-25 10:33:46 +1000528 pldm_instance_id_t id;
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100529 if (!pldmInstanceID)
Patrick Williams05e95592021-09-02 09:28:14 -0500530 {
Chris Cain8b508bf2022-05-26 14:01:31 -0500531 // Request new instance ID
Rashmica Guptadb38e912023-05-25 10:33:46 +1000532 int rc = pldm_instance_id_alloc(pldmInstanceIdDb, tid, &id);
533 if (rc == -EAGAIN)
Chris Cain8b508bf2022-05-26 14:01:31 -0500534 {
Rashmica Guptadb38e912023-05-25 10:33:46 +1000535 std::this_thread::sleep_for(std::chrono::milliseconds(100));
536 rc = pldm_instance_id_alloc(pldmInstanceIdDb, tid, &id);
Chris Cain8b508bf2022-05-26 14:01:31 -0500537 }
Rashmica Guptadb38e912023-05-25 10:33:46 +1000538
539 if (rc)
Chris Cain8b508bf2022-05-26 14:01:31 -0500540 {
541 log<level::ERR>(
Rashmica Guptadb38e912023-05-25 10:33:46 +1000542 std::format(
543 "getPldmInstanceId: Failed to alloc ID for TID {}. RC{}",
544 tid, rc)
Chris Cain8b508bf2022-05-26 14:01:31 -0500545 .c_str());
546 return false;
547 }
Rashmica Guptadb38e912023-05-25 10:33:46 +1000548 pldmInstanceID.emplace(id);
549 if (!throttleTraces)
550 {
551 log<level::INFO>(
552 std::format("got id {} and set PldmInstanceId to {}", id,
553 pldmInstanceID.value())
554 .c_str());
555 }
Patrick Williams05e95592021-09-02 09:28:14 -0500556 }
Eddie Jamescbad2192021-10-07 09:39:39 -0500557 return true;
558}
Patrick Williams05e95592021-09-02 09:28:14 -0500559
Rashmica Guptadb38e912023-05-25 10:33:46 +1000560void Interface::freePldmInstanceId()
561{
562 if (pldmInstanceID)
563 {
564 int rc = pldm_instance_id_free(pldmInstanceIdDb, tid,
565 pldmInstanceID.value());
566 if (rc)
567 {
568 log<level::ERR>(
569 std::format(
570 "freePldmInstanceId: Failed to free ID {} for TID {}. RC{}",
571 pldmInstanceID.value(), tid, rc)
572 .c_str());
573 return;
574 }
575 if (!throttleTraces)
576 {
577 log<level::INFO>(
578 std::format("Freed PLDM instance ID {}", pldmInstanceID.value())
579 .c_str());
580 }
581 pldmInstanceID = std::nullopt;
582 }
583}
584
Lakshmi Yadlapati6213f192024-07-01 11:50:09 -0500585[[maybe_unused]] int Interface::openMctpDemuxTransport()
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100586{
Lakshmi Yadlapati6213f192024-07-01 11:50:09 -0500587 impl.mctpDemux = nullptr;
588 int rc = pldm_transport_mctp_demux_init(&impl.mctpDemux);
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100589 if (rc)
590 {
591 log<level::ERR>(
592 std::format(
593 "openMctpDemuxTransport: Failed to init MCTP demux transport, errno={}/{}",
594 rc, strerror(rc))
595 .c_str());
596 return -1;
597 }
598
Lakshmi Yadlapati6213f192024-07-01 11:50:09 -0500599 if (pldm_transport_mctp_demux_map_tid(impl.mctpDemux, mctpEid, mctpEid))
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100600 {
601 log<level::ERR>(
602 std::format(
603 "openMctpDemuxTransport: Failed to setup tid to eid mapping, errno={}/{}",
604 errno, strerror(errno))
605 .c_str());
606 pldmClose();
607 return -1;
608 }
Lakshmi Yadlapati6213f192024-07-01 11:50:09 -0500609 pldmTransport = pldm_transport_mctp_demux_core(impl.mctpDemux);
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100610
611 struct pollfd pollfd;
612 if (pldm_transport_mctp_demux_init_pollfd(pldmTransport, &pollfd))
613 {
614 log<level::ERR>(
615 std::format(
616 "openMctpDemuxTransport: Failed to get pollfd , errno={}/{}",
617 errno, strerror(errno))
618 .c_str());
619 pldmClose();
620 return -1;
621 }
622 pldmFd = pollfd.fd;
623 if (!throttleTraces)
624 {
625 log<level::INFO>(
626 std::format("openMctpDemuxTransport: pldmFd has fd={}", pldmFd)
627 .c_str());
628 }
629 return 0;
630}
631
Lakshmi Yadlapati6213f192024-07-01 11:50:09 -0500632[[maybe_unused]] int Interface::openAfMctpTransport()
633{
634 impl.afMctp = nullptr;
635 int rc = pldm_transport_af_mctp_init(&impl.afMctp);
636 if (rc)
637 {
638 log<level::ERR>(
639 std::format(
640 "openAfMctpTransport: Failed to init af MCTP transport, errno={}/{}",
641 rc, strerror(rc))
642 .c_str());
643 return -1;
644 }
645
646 if (pldm_transport_af_mctp_map_tid(impl.afMctp, mctpEid, mctpEid))
647 {
648 log<level::ERR>(
649 std::format(
650 "openAfMctpTransport: Failed to setup tid to eid mapping, errno={}/{}",
651 errno, strerror(errno))
652 .c_str());
653 pldmClose();
654 return -1;
655 }
656 pldmTransport = pldm_transport_af_mctp_core(impl.afMctp);
657
658 struct pollfd pollfd;
659 if (pldm_transport_af_mctp_init_pollfd(pldmTransport, &pollfd))
660 {
661 log<level::ERR>(
662 std::format(
663 "openAfMctpTransport: Failed to get pollfd , errno={}/{}",
664 errno, strerror(errno))
665 .c_str());
666 pldmClose();
667 return -1;
668 }
669 pldmFd = pollfd.fd;
670 if (!throttleTraces)
671 {
672 log<level::INFO>(
673 std::format("openAfMctpTransport: pldmFd has fd={}", pldmFd)
674 .c_str());
675 }
676 return 0;
677}
678
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100679int Interface::pldmOpen()
680{
681 if (pldmTransport)
682 {
683 log<level::ERR>(
684 std::format("pldmOpen: pldmTransport already setup!, errno={}/{}",
685 errno, strerror(errno))
686 .c_str());
687 return -1;
688 }
Lakshmi Yadlapati6213f192024-07-01 11:50:09 -0500689#if defined(PLDM_TRANSPORT_WITH_MCTP_DEMUX)
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100690 return openMctpDemuxTransport();
Lakshmi Yadlapati6213f192024-07-01 11:50:09 -0500691#elif defined(PLDM_TRANSPORT_WITH_AF_MCTP)
692 return openAfMctpTransport();
693#else
694 return -1;
695#endif
696
697 return 0;
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100698}
699
Chris Cainbae4d072022-02-28 09:46:50 -0600700void Interface::sendPldm(const std::vector<uint8_t>& request,
701 const uint8_t instance, const bool rspExpected)
Eddie Jamescbad2192021-10-07 09:39:39 -0500702{
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100703 if (!pldmInstanceID)
Chris Cain8b508bf2022-05-26 14:01:31 -0500704 {
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100705 log<level::ERR>("sendPldm: No PLDM Instance ID found!");
Chris Cain8b508bf2022-05-26 14:01:31 -0500706 return;
707 }
708
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100709 auto rc = pldmOpen();
710 if (rc)
Patrick Williams05e95592021-09-02 09:28:14 -0500711 {
Eddie James88811ad2024-08-27 13:39:05 -0500712 if (!throttleTraces)
713 {
714 log<level::ERR>(
715 std::format("sendPldm: pldmOpen failed rc={}", rc).c_str());
716 }
Rashmica Guptadb38e912023-05-25 10:33:46 +1000717 freePldmInstanceId();
Patrick Williams05e95592021-09-02 09:28:14 -0500718 return;
719 }
Eddie Jamescbad2192021-10-07 09:39:39 -0500720
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100721 pldm_tid_t pldmTID = static_cast<pldm_tid_t>(mctpEid);
Patrick Williams05e95592021-09-02 09:28:14 -0500722 // Send the PLDM request message to HBRT
Chris Cainbae4d072022-02-28 09:46:50 -0600723 if (rspExpected)
Patrick Williams05e95592021-09-02 09:28:14 -0500724 {
Chris Cainbae4d072022-02-28 09:46:50 -0600725 // Register callback when response is available
726 registerPldmRspCallback();
727
728 // Send PLDM request
Chris Cain755af102024-02-27 16:09:51 -0600729 if (!throttleTraces)
730 {
731 log<level::INFO>(
732 std::format(
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100733 "sendPldm: calling pldm_transport_send_msg(OCC{}, instance:{}, {} bytes)",
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100734 instance, pldmInstanceID.value(), request.size())
Chris Cain755af102024-02-27 16:09:51 -0600735 .c_str());
736 }
Chris Cainbae4d072022-02-28 09:46:50 -0600737 pldmResponseReceived = false;
738 pldmResponseTimeout = false;
739 pldmResponseOcc = instance;
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100740 auto pldmRc = pldm_transport_send_msg(pldmTransport, pldmTID,
741 request.data(), request.size());
Chris Cainbae4d072022-02-28 09:46:50 -0600742 auto sendErrno = errno;
743 if (pldmRc != PLDM_REQUESTER_SUCCESS)
744 {
745 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600746 std::format(
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100747 "sendPldm: pldm_transport_send_msg failed with rc={} and errno={}/{}",
Chris Cain5161a022023-08-15 10:07:12 -0500748 static_cast<
749 std::underlying_type_t<pldm_requester_error_codes>>(
750 pldmRc),
751 sendErrno, strerror(sendErrno))
Chris Cainbae4d072022-02-28 09:46:50 -0600752 .c_str());
753 pldmClose();
754 return;
755 }
756
757 // start timer waiting for the response
758 using namespace std::literals::chrono_literals;
Chris Cainbd551de2022-04-26 13:41:16 -0500759 pldmRspTimer.restartOnce(8s);
Chris Cainbae4d072022-02-28 09:46:50 -0600760
761 // Wait for response/timeout
762 }
763 else // not expecting the response
764 {
Chris Cain755af102024-02-27 16:09:51 -0600765 if (!throttleTraces)
766 {
767 log<level::INFO>(
768 std::format(
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100769 "sendPldm: calling pldm_transport_send_msg(mctpID:{}, fd:{}, {} bytes) for OCC{}",
Chris Cain755af102024-02-27 16:09:51 -0600770 mctpEid, pldmFd, request.size(), instance)
771 .c_str());
772 }
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100773 auto rc = pldm_transport_send_msg(pldmTransport, pldmTID,
774 request.data(), request.size());
Chris Cainbae4d072022-02-28 09:46:50 -0600775 auto sendErrno = errno;
Chris Caind1b68262022-02-28 09:56:50 -0600776 if (rc)
777 {
778 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600779 std::format(
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100780 "sendPldm: pldm_transport_send_msg(mctpID:{}, fd:{}, {} bytes) failed with rc={} and errno={}/{}",
Chris Cain5161a022023-08-15 10:07:12 -0500781 mctpEid, pldmFd, request.size(),
782 static_cast<
783 std::underlying_type_t<pldm_requester_error_codes>>(rc),
784 sendErrno, strerror(sendErrno))
Chris Caind1b68262022-02-28 09:56:50 -0600785 .c_str());
786 }
Chris Cainbae4d072022-02-28 09:46:50 -0600787 pldmClose();
788 }
789}
Patrick Williams05e95592021-09-02 09:28:14 -0500790
Chris Cainbae4d072022-02-28 09:46:50 -0600791// Attaches the FD to event loop and registers the callback handler
792void Interface::registerPldmRspCallback()
793{
794 decltype(eventSource.get()) sourcePtr = nullptr;
795 auto rc = sd_event_add_io(event.get(), &sourcePtr, pldmFd, EPOLLIN,
796 pldmRspCallback, this);
797 if (rc < 0)
798 {
799 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600800 std::format(
Chris Cainbae4d072022-02-28 09:46:50 -0600801 "registerPldmRspCallback: sd_event_add_io: Error({})={} : fd={}",
802 rc, strerror(-rc), pldmFd)
803 .c_str());
Chris Caind1b68262022-02-28 09:56:50 -0600804 }
805 else
Patrick Williams05e95592021-09-02 09:28:14 -0500806 {
Chris Cainbae4d072022-02-28 09:46:50 -0600807 // puts sourcePtr in the event source.
808 eventSource.reset(sourcePtr);
809 }
810}
811
812// Add a timer to the event loop, default 30s.
813void Interface::pldmRspExpired()
814{
815 if (!pldmResponseReceived)
816 {
Chris Cain755af102024-02-27 16:09:51 -0600817 if (!throttleTraces)
818 {
819 log<level::WARNING>(
820 std::format(
821 "pldmRspExpired: timerCallback - timeout waiting for pldm response for OCC{}",
822 pldmResponseOcc)
823 .c_str());
824 }
Chris Cainbae4d072022-02-28 09:46:50 -0600825 pldmResponseTimeout = true;
826 if (pldmFd)
827 {
828 pldmClose();
829 }
830 }
831 return;
832};
833
834void Interface::pldmClose()
835{
Rashmica Guptadb38e912023-05-25 10:33:46 +1000836 freePldmInstanceId();
Chris Cainbae4d072022-02-28 09:46:50 -0600837 if (pldmRspTimer.isEnabled())
838 {
839 // stop PLDM response timer
840 pldmRspTimer.setEnabled(false);
841 }
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100842
Lakshmi Yadlapati6213f192024-07-01 11:50:09 -0500843#if defined(PLDM_TRANSPORT_WITH_MCTP_DEMUX)
844 pldm_transport_mctp_demux_destroy(impl.mctpDemux);
845 impl.mctpDemux = NULL;
846#elif defined(PLDM_TRANSPORT_WITH_AF_MCTP)
847 pldm_transport_af_mctp_destroy(impl.afMctp);
848 impl.afMctp = NULL;
849#endif
Chris Cainbae4d072022-02-28 09:46:50 -0600850 pldmFd = -1;
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100851 pldmTransport = NULL;
Chris Cainbae4d072022-02-28 09:46:50 -0600852 eventSource.reset();
853}
854
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100855int Interface::pldmRspCallback(sd_event_source* /*es*/,
856 __attribute__((unused)) int fd, uint32_t revents,
857 void* userData)
Chris Cainbae4d072022-02-28 09:46:50 -0600858{
859 if (!(revents & EPOLLIN))
860 {
861 log<level::INFO>(
Patrick Williams48002492024-02-13 21:43:32 -0600862 std::format("pldmRspCallback - revents={:08X}", revents).c_str());
Chris Cainbae4d072022-02-28 09:46:50 -0600863 return -1;
864 }
865
866 auto pldmIface = static_cast<Interface*>(userData);
867
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100868 if (!pldmIface->pldmInstanceID)
Chris Cain8b508bf2022-05-26 14:01:31 -0500869 {
870 log<level::ERR>(
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100871 "pldmRspCallback: No outstanding PLDM Instance ID found");
Chris Cain8b508bf2022-05-26 14:01:31 -0500872 return -1;
873 }
874
Chris Cainbae4d072022-02-28 09:46:50 -0600875 uint8_t* responseMsg = nullptr;
876 size_t responseMsgSize{};
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100877 pldm_tid_t pldmTID = static_cast<pldm_tid_t>(mctpEid);
Chris Cainbae4d072022-02-28 09:46:50 -0600878
Chris Cain755af102024-02-27 16:09:51 -0600879 if (!throttleTraces)
880 {
881 log<level::INFO>(
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100882 std::format(
883 "pldmRspCallback: calling pldm_transport_recv_msg() instance:{}",
884 pldmIface->pldmInstanceID.value())
Chris Cain755af102024-02-27 16:09:51 -0600885 .c_str());
886 }
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100887 auto rc = pldm_transport_recv_msg(pldmIface->pldmTransport, &pldmTID,
888 (void**)&responseMsg, &responseMsgSize);
Chris Cainbae4d072022-02-28 09:46:50 -0600889 int lastErrno = errno;
890 if (rc)
891 {
Chris Cain755af102024-02-27 16:09:51 -0600892 if (!throttleTraces)
893 {
894 log<level::ERR>(
895 std::format(
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100896 "pldmRspCallback: pldm_transport_recv_msg failed with rc={}, errno={}/{}",
Chris Cain755af102024-02-27 16:09:51 -0600897 static_cast<
898 std::underlying_type_t<pldm_requester_error_codes>>(rc),
899 lastErrno, strerror(lastErrno))
900 .c_str());
901 }
Chris Cainbae4d072022-02-28 09:46:50 -0600902 return -1;
903 }
Chris Cain8b508bf2022-05-26 14:01:31 -0500904
905 // We got the response for the PLDM request msg that was sent
Chris Cain755af102024-02-27 16:09:51 -0600906 if (!throttleTraces)
907 {
908 log<level::INFO>(
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100909 std::format(
910 "pldmRspCallback: pldm_transport_recv_msg() rsp was {} bytes",
911 responseMsgSize)
Chris Cain755af102024-02-27 16:09:51 -0600912 .c_str());
913 }
Chris Cainbae4d072022-02-28 09:46:50 -0600914
915 if (pldmIface->pldmRspTimer.isEnabled())
916 {
917 // stop PLDM response timer
918 pldmIface->pldmRspTimer.setEnabled(false);
919 }
920
Chris Cain8b508bf2022-05-26 14:01:31 -0500921 // instance ID should be freed
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100922 pldmIface->pldmInstanceID = std::nullopt;
Chris Cain8b508bf2022-05-26 14:01:31 -0500923
Chris Cainbae4d072022-02-28 09:46:50 -0600924 // Set pointer to autodelete
Patrick Williamsd7542c82024-08-16 15:20:28 -0400925 std::unique_ptr<uint8_t, decltype(std::free)*> responseMsgPtr{
926 responseMsg, std::free};
Chris Cainbae4d072022-02-28 09:46:50 -0600927
Chris Cainbae4d072022-02-28 09:46:50 -0600928 auto response = reinterpret_cast<pldm_msg*>(responseMsgPtr.get());
929 if (response->payload[0] != PLDM_SUCCESS)
930 {
931 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600932 std::format("pldmRspCallback: payload[0] was not success: {}",
Chris Cainbae4d072022-02-28 09:46:50 -0600933 response->payload[0])
934 .c_str());
935 pldmIface->pldmClose();
936 return -1;
937 }
938
939 // Decode the response
940 uint8_t compCode = 0, sensorCount = 1;
941 get_sensor_state_field field[6];
942 responseMsgSize -= sizeof(pldm_msg_hdr);
943 auto msgRc = decode_get_state_sensor_readings_resp(
944 response, responseMsgSize, &compCode, &sensorCount, field);
945 if ((msgRc != PLDM_SUCCESS) || (compCode != PLDM_SUCCESS))
946 {
947 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600948 std::format(
Chris Cainbae4d072022-02-28 09:46:50 -0600949 "pldmRspCallback: decode_get_state_sensor_readings failed with rc={} and compCode={}",
950 msgRc, compCode)
951 .c_str());
952 pldmIface->pldmClose();
953 return -1;
954 }
955
956 pldmIface->pldmClose();
957
958 const uint8_t instance = pldmIface->pldmResponseOcc;
959 const uint8_t occSensorState = field[0].present_state;
960 pldmIface->pldmResponseReceived = true;
961
962 if (occSensorState == PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_IN_SERVICE)
963 {
964 log<level::INFO>(
Patrick Williams48002492024-02-13 21:43:32 -0600965 std::format("pldmRspCallback: OCC{} is RUNNING", instance).c_str());
Chris Cainbae4d072022-02-28 09:46:50 -0600966 pldmIface->callBack(instance, true);
967 }
Chris Cain733b2012022-05-04 11:54:06 -0500968 else if (occSensorState ==
969 PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_DORMANT)
970 {
Chris Cain755af102024-02-27 16:09:51 -0600971 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -0600972 std::format(
Chris Cain733b2012022-05-04 11:54:06 -0500973 "pldmRspCallback: OCC{} has now STOPPED and system is in SAFE MODE",
974 instance)
975 .c_str());
Sheldon Bailey31a2f132022-05-20 11:31:52 -0500976
977 // Setting safe mode true
978 pldmIface->safeModeCallBack(true);
979
Chris Cain733b2012022-05-04 11:54:06 -0500980 pldmIface->callBack(instance, false);
981 }
Chris Cain755af102024-02-27 16:09:51 -0600982 else if (occSensorState ==
983 PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS_STOPPED)
Chris Cainbae4d072022-02-28 09:46:50 -0600984 {
985 log<level::INFO>(
Chris Cain755af102024-02-27 16:09:51 -0600986 std::format("pldmRspCallback: OCC{} is not running", instance)
Chris Cainbae4d072022-02-28 09:46:50 -0600987 .c_str());
Chris Cain755af102024-02-27 16:09:51 -0600988 pldmIface->callBack(instance, false);
989 }
990 else
991 {
992 const size_t rspLength = responseMsgSize + sizeof(pldm_msg_hdr);
993 std::vector<std::uint8_t> pldmResponse(rspLength);
994 memcpy(&pldmResponse[0], reinterpret_cast<std::uint8_t*>(response),
995 rspLength);
996 if (!throttleTraces)
Chris Cainc9dc4412023-03-06 15:56:34 -0600997 {
Chris Cain755af102024-02-27 16:09:51 -0600998 log<level::WARNING>(
Patrick Williams48002492024-02-13 21:43:32 -0600999 std::format(
Chris Cain755af102024-02-27 16:09:51 -06001000 "pldmRspCallback: Unexpected State: {} - PLDM response ({} bytes) for OCC{}:",
1001 occSensorState, rspLength, instance)
Chris Cainc9dc4412023-03-06 15:56:34 -06001002 .c_str());
1003 dump_hex(pldmResponse);
1004 }
Chris Cainbae4d072022-02-28 09:46:50 -06001005 }
1006
1007 return 0;
1008};
1009
Patrick Williamsd7542c82024-08-16 15:20:28 -04001010std::vector<uint8_t>
1011 Interface::encodeGetStateSensorRequest(uint8_t instance, uint16_t sensorId)
Chris Cainbae4d072022-02-28 09:46:50 -06001012{
Rashmica Guptaaeba51c2023-02-17 12:30:46 +11001013 if (!getPldmInstanceId())
Chris Cain8b508bf2022-05-26 14:01:31 -05001014 {
1015 log<level::ERR>(
Rashmica Guptaaeba51c2023-02-17 12:30:46 +11001016 "encodeGetStateSensorRequest: failed to getPldmInstanceId");
Chris Cain8b508bf2022-05-26 14:01:31 -05001017 return std::vector<uint8_t>();
1018 }
1019
Chris Cainbae4d072022-02-28 09:46:50 -06001020 bitfield8_t sRearm = {0};
Patrick Williamsd7542c82024-08-16 15:20:28 -04001021 const size_t msgSize =
1022 sizeof(pldm_msg_hdr) + PLDM_GET_STATE_SENSOR_READINGS_REQ_BYTES;
Chris Cainbae4d072022-02-28 09:46:50 -06001023 std::vector<uint8_t> request(msgSize);
Chris Cain8b508bf2022-05-26 14:01:31 -05001024
Chris Cainbae4d072022-02-28 09:46:50 -06001025 auto msg = reinterpret_cast<pldm_msg*>(request.data());
Rashmica Guptaaeba51c2023-02-17 12:30:46 +11001026 auto msgRc = encode_get_state_sensor_readings_req(pldmInstanceID.value(),
Chris Cain8b508bf2022-05-26 14:01:31 -05001027 sensorId, sRearm, 0, msg);
Chris Cainbae4d072022-02-28 09:46:50 -06001028 if (msgRc != PLDM_SUCCESS)
1029 {
1030 log<level::ERR>(
Patrick Williams48002492024-02-13 21:43:32 -06001031 std::format(
Chris Cainbae4d072022-02-28 09:46:50 -06001032 "encodeGetStateSensorRequest: Failed to encode sensorId:0x{:08X} for OCC{} (rc={})",
1033 sensorId, instance, msgRc)
1034 .c_str());
1035 }
1036 return request;
1037}
1038
1039// Initiate query of the specified OCC Active Sensor
1040void Interface::checkActiveSensor(uint8_t instance)
1041{
1042 static bool tracedOnce = false;
1043 if (pldmFd > 0)
1044 {
Chris Cain755af102024-02-27 16:09:51 -06001045 if (!throttleTraces && !tracedOnce)
Chris Caind1b68262022-02-28 09:56:50 -06001046 {
Chris Cain755af102024-02-27 16:09:51 -06001047 log<level::WARNING>(
Patrick Williams48002492024-02-13 21:43:32 -06001048 std::format(
Chris Cainbae4d072022-02-28 09:46:50 -06001049 "checkActiveSensor: already waiting on OCC{} (fd={})",
1050 pldmResponseOcc, pldmFd)
Chris Caind1b68262022-02-28 09:56:50 -06001051 .c_str());
Chris Cainbae4d072022-02-28 09:46:50 -06001052 tracedOnce = true;
Chris Caind1b68262022-02-28 09:56:50 -06001053 }
Chris Cainbae4d072022-02-28 09:46:50 -06001054 return;
1055 }
1056 tracedOnce = false;
1057
1058 if (!isOCCSensorCacheValid())
1059 {
1060 fetchSensorInfo(PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS,
1061 sensorToOCCInstance, OCCSensorOffset);
1062 }
1063
1064 // look up sensor id (key) based on instance
1065 auto entry = std::find_if(
1066 sensorToOCCInstance.begin(), sensorToOCCInstance.end(),
1067 [instance](const auto& entry) { return instance == entry.second; });
1068 if (entry != sensorToOCCInstance.end())
1069 {
1070 // Query the OCC Active Sensor state for this instance
Chris Cain755af102024-02-27 16:09:51 -06001071 if (!throttleTraces)
1072 {
1073 log<level::INFO>(
1074 std::format("checkActiveSensor: OCC{} / sensorID: 0x{:04X}",
1075 instance, entry->first)
1076 .c_str());
1077 }
Chris Cainbae4d072022-02-28 09:46:50 -06001078
Chris Cainbae4d072022-02-28 09:46:50 -06001079 // Encode GetStateSensorReadings PLDM message
1080 auto request = encodeGetStateSensorRequest(instance, entry->first);
1081 if (request.empty())
1082 {
1083 return;
1084 }
1085
1086 // Send request to PLDM and setup callback for response
1087 sendPldm(request, instance, true);
1088 }
1089 else
1090 {
Chris Cain755af102024-02-27 16:09:51 -06001091 if (!throttleTraces)
1092 {
1093 log<level::ERR>(
1094 std::format(
1095 "checkActiveSensor: Unable to find PLDM sensor for OCC{}",
1096 instance)
1097 .c_str());
1098 log<level::INFO>(
1099 "checkActiveSensor: fetching STATE_SET_OPERATIONAL_RUNNING_STATUS");
1100 }
Chris Cain8cf74962022-06-29 08:45:16 -05001101 fetchSensorInfo(PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS,
1102 sensorToOCCInstance, OCCSensorOffset);
Patrick Williams05e95592021-09-02 09:28:14 -05001103 }
Patrick Williams05e95592021-09-02 09:28:14 -05001104}
1105
Chris Cain755af102024-02-27 16:09:51 -06001106void Interface::setTraceThrottle(const bool throttle)
1107{
1108 if (throttle != throttleTraces)
1109 {
1110 if (throttle)
1111 {
1112 log<level::WARNING>("PLDM traces being throttled");
1113 }
1114 else
1115 {
1116 log<level::INFO>("PLDM traces no longer being throttled");
1117 }
1118 throttleTraces = throttle;
1119 }
1120}
1121
Patrick Williams05e95592021-09-02 09:28:14 -05001122} // namespace pldm