blob: 1f1bad18cfd875f1b4bd285554fd128adbcf31fa [file] [log] [blame]
Patrick Williams05e95592021-09-02 09:28:14 -05001#pragma once
Rashmica Guptadb38e912023-05-25 10:33:46 +10002
Chris Cainbae4d072022-02-28 09:46:50 -06003#include "occ_events.hpp"
Patrick Williams05e95592021-09-02 09:28:14 -05004#include "occ_status.hpp"
5#include "utils.hpp"
6
Rashmica Guptadb38e912023-05-25 10:33:46 +10007#include <libpldm/instance-id.h>
Patrick Williams05e95592021-09-02 09:28:14 -05008#include <libpldm/pldm.h>
Rashmica Gupta52328cb2023-02-15 10:38:16 +11009#include <libpldm/transport.h>
10#include <libpldm/transport/mctp-demux.h>
Patrick Williams05e95592021-09-02 09:28:14 -050011
12#include <sdbusplus/bus/match.hpp>
Chris Cainbae4d072022-02-28 09:46:50 -060013#include <sdeventplus/event.hpp>
14#include <sdeventplus/utility/timer.hpp>
Patrick Williams05e95592021-09-02 09:28:14 -050015
16namespace pldm
17{
18
19namespace MatchRules = sdbusplus::bus::match::rules;
Chris Cainbae4d072022-02-28 09:46:50 -060020using namespace open_power::occ;
Patrick Williams05e95592021-09-02 09:28:14 -050021
22using CompositeEffecterCount = uint8_t;
23using EffecterID = uint16_t;
24using EntityType = uint16_t;
25using EntityInstance = uint16_t;
26using EventState = uint8_t;
Eddie Jamescbad2192021-10-07 09:39:39 -050027using InstanceToEffecter = std::map<open_power::occ::instanceID, EffecterID>;
Patrick Williams05e95592021-09-02 09:28:14 -050028using PdrList = std::vector<std::vector<uint8_t>>;
29using SensorID = uint16_t;
30using SensorOffset = uint8_t;
Eddie Jamescbad2192021-10-07 09:39:39 -050031using SensorToInstance = std::map<SensorID, open_power::occ::instanceID>;
Patrick Williams05e95592021-09-02 09:28:14 -050032using TerminusID = uint8_t;
33
Patrick Williams05e95592021-09-02 09:28:14 -050034/** @brief OCC instance starts with 0 for example "occ0" */
35constexpr open_power::occ::instanceID start = 0;
36
37/** @brief Hardcoded mctpEid for HBRT */
38constexpr mctp_eid_t mctpEid = 10;
39
Rashmica Gupta52328cb2023-02-15 10:38:16 +110040/** @brief Hardcoded TID */
41constexpr TerminusID tid = mctpEid;
42
Patrick Williams05e95592021-09-02 09:28:14 -050043/** @class Interface
44 *
45 * @brief Abstracts the PLDM details related to the OCC
46 */
47class Interface
48{
49 public:
50 Interface() = delete;
Rashmica Guptadb38e912023-05-25 10:33:46 +100051 //~Interface() = default;
Patrick Williams05e95592021-09-02 09:28:14 -050052 Interface(const Interface&) = delete;
53 Interface& operator=(const Interface&) = delete;
54 Interface(Interface&&) = delete;
55 Interface& operator=(Interface&&) = delete;
56
57 /** @brief Constructs the PLDM Interface object for OCC functions
58 *
59 * @param[in] callBack - callBack handler to invoke when the OCC state
60 * changes.
61 */
62 explicit Interface(
Eddie Jamescbad2192021-10-07 09:39:39 -050063 std::function<bool(open_power::occ::instanceID, bool)> callBack,
Chris Cainbae4d072022-02-28 09:46:50 -060064 std::function<void(open_power::occ::instanceID, bool)> sbeCallBack,
Sheldon Bailey31a2f132022-05-20 11:31:52 -050065 std::function<void(bool)> safeModeCallBack, EventPtr& event) :
Patrick Williams05e95592021-09-02 09:28:14 -050066 callBack(callBack),
Sheldon Bailey31a2f132022-05-20 11:31:52 -050067 sbeCallBack(sbeCallBack), safeModeCallBack(safeModeCallBack),
68 event(event),
Patrick Williams05e95592021-09-02 09:28:14 -050069 pldmEventSignal(
70 open_power::occ::utils::getBus(),
71 MatchRules::type::signal() +
72 MatchRules::member("StateSensorEvent") +
73 MatchRules::path("/xyz/openbmc_project/pldm") +
74 MatchRules::interface("xyz.openbmc_project.PLDM.Event"),
75 std::bind(std::mem_fn(&Interface::sensorEvent), this,
76 std::placeholders::_1)),
Chris Cain157467d2022-06-24 11:25:23 -050077 hostStateSignal(
78 open_power::occ::utils::getBus(),
79 MatchRules::propertiesChanged("/xyz/openbmc_project/state/host0",
80 "xyz.openbmc_project.State.Host"),
81 std::bind(std::mem_fn(&Interface::hostStateEvent), this,
82 std::placeholders::_1)),
Chris Cainbae4d072022-02-28 09:46:50 -060083 sdpEvent(sdeventplus::Event::get_default()),
84 pldmRspTimer(
85 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>(
86 sdpEvent, std::bind(&Interface::pldmRspExpired, this)))
Rashmica Guptadb38e912023-05-25 10:33:46 +100087 {
88 int rc = pldm_instance_db_init_default(&pldmInstanceIdDb);
89 if (rc)
90 {
91 throw std::system_category().default_error_condition(rc);
92 }
93 }
94
95 ~Interface()
96 {
97 int rc = pldm_instance_db_destroy(pldmInstanceIdDb);
98 if (rc)
99 {
100 std::cout << "pldm_instance_db_destroy failed, rc =" << rc << "\n";
101 }
102 }
Patrick Williams05e95592021-09-02 09:28:14 -0500103
Eddie Jamescbad2192021-10-07 09:39:39 -0500104 /** @brief Fetch the state sensor PDRs and populate the cache with
105 * sensorId to OCC/SBE instance mapping information and the sensor
106 * offset for the relevent state set.
Patrick Williams05e95592021-09-02 09:28:14 -0500107 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500108 * @param[in] stateSetId - the state set ID to look for
109 * @param[out] sensorInstanceMap - map of sensorID to instance
Patrick Williams05e95592021-09-02 09:28:14 -0500110 * @param[out] sensorOffset - sensor offset of interested state set ID
111 */
Eddie Jamescbad2192021-10-07 09:39:39 -0500112 void fetchSensorInfo(uint16_t stateSetId,
113 SensorToInstance& sensorInstanceMap,
114 SensorOffset& sensorOffset);
Patrick Williams05e95592021-09-02 09:28:14 -0500115
Eddie Jamescbad2192021-10-07 09:39:39 -0500116 /** @brief Fetch the OCC/SBE state effecter PDRs and populate the cache
117 * with OCC/SBE instance to EffecterID information.
Patrick Williams05e95592021-09-02 09:28:14 -0500118 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500119 * @param[in] stateSetId - the state set ID to look for
120 * @param[out] instanceToEffecterMap - map of instance to effecterID
Patrick Williams05e95592021-09-02 09:28:14 -0500121 * @param[out] count - sensor offset of interested state set ID
Eddie Jamescbad2192021-10-07 09:39:39 -0500122 * @param[out] stateIdPos - position of the stateSetID
Patrick Williams05e95592021-09-02 09:28:14 -0500123 */
Eddie James432dc482021-11-19 15:29:31 -0600124 void fetchEffecterInfo(uint16_t stateSetId,
Eddie Jamescbad2192021-10-07 09:39:39 -0500125 InstanceToEffecter& instanceToEffecterMap,
126 CompositeEffecterCount& count, uint8_t& stateIdPos);
Patrick Williams05e95592021-09-02 09:28:14 -0500127
128 /** @brief Prepare the request for SetStateEffecterStates command
129 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500130 * @param[in] effecterId - the instance effecter ID
131 * @param[in] effecterCount - compositeEffecterCount for the effecter PDR
132 * @param[in] stateIdPos - position of the stateSetID
133 * @param[in] stateSetValue - the value to set the state set to
Patrick Williams05e95592021-09-02 09:28:14 -0500134 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500135 * @return PLDM request message to be sent to host for OCC reset or SBE
136 * HRESET, empty response in the case of failure.
Patrick Williams05e95592021-09-02 09:28:14 -0500137 */
138 std::vector<uint8_t>
Chris Cain8b508bf2022-05-26 14:01:31 -0500139 prepareSetEffecterReq(EffecterID effecterId,
Patrick Williams05e95592021-09-02 09:28:14 -0500140 CompositeEffecterCount effecterCount,
Eddie Jamescbad2192021-10-07 09:39:39 -0500141 uint8_t stateIdPos, uint8_t stateSetValue);
Patrick Williams05e95592021-09-02 09:28:14 -0500142
143 /** @brief Send the PLDM message to reset the OCC
144 *
145 * @param[in] instanceId - OCC instance to reset
146 *
147 */
148 void resetOCC(open_power::occ::instanceID occInstanceId);
149
Eddie Jamescbad2192021-10-07 09:39:39 -0500150 /** @brief Send the PLDM message to perform the HRESET
151 *
152 * @param[in] instanceID - SBE instance to HRESET
153 */
154 void sendHRESET(open_power::occ::instanceID sbeInstanceId);
155
Chris Cainbae4d072022-02-28 09:46:50 -0600156 /** @brief Check if the OCC active sensor is available
157 * On successful read, the Manager callback will be called to update
158 * the status
159 *
160 * @param[in] instance - OCC instance to check
161 */
162 void checkActiveSensor(uint8_t instance);
163
Chris Cain755af102024-02-27 16:09:51 -0600164 /** @brief Set the throttleTraces flag
165 *
166 * @param[in] throttle - Flag to indicate if traces should be throttled
167 */
168 void setTraceThrottle(const bool throttle);
169
Patrick Williams05e95592021-09-02 09:28:14 -0500170 private:
Rashmica Guptadb38e912023-05-25 10:33:46 +1000171 /** @brief PLDM instance ID database object used to get instance IDs
172 */
173 pldm_instance_db* pldmInstanceIdDb = nullptr;
174
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100175 /** @brief PLDM instance number used in PLDM requests
Chris Cain8b508bf2022-05-26 14:01:31 -0500176 */
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100177 std::optional<uint8_t> pldmInstanceID;
Chris Cain8b508bf2022-05-26 14:01:31 -0500178
Patrick Williams05e95592021-09-02 09:28:14 -0500179 /** @brief Callback handler to be invoked when the state of the OCC
180 * changes
181 */
182 std::function<bool(open_power::occ::instanceID, bool)> callBack = nullptr;
183
Eddie Jamescbad2192021-10-07 09:39:39 -0500184 /** @brief Callback handler to be invoked when the maintenance state of the
185 * SBE changes
186 */
187 std::function<void(open_power::occ::instanceID, bool)> sbeCallBack =
188 nullptr;
189
Sheldon Bailey31a2f132022-05-20 11:31:52 -0500190 /** @brief Callback handler to be invoked when the OCC is in SAFE Mode =
191 * true or when OCCs are in_service = false.
192 */
193 std::function<void(bool)> safeModeCallBack = nullptr;
194
Chris Cainbae4d072022-02-28 09:46:50 -0600195 /** @brief reference to sd_event wrapped in unique_ptr */
196 EventPtr& event;
197
198 /** @brief event source wrapped in unique_ptr */
199 EventSourcePtr eventSource;
200
Patrick Williams05e95592021-09-02 09:28:14 -0500201 /** @brief Used to subscribe to D-Bus PLDM StateSensorEvent signal and
202 * processes if the event corresponds to OCC state change.
203 */
204 sdbusplus::bus::match_t pldmEventSignal;
205
Chris Cain157467d2022-06-24 11:25:23 -0500206 /** @brief Used to subscribe for host state change signal */
207 sdbusplus::bus::match_t hostStateSignal;
208
Patrick Williams05e95592021-09-02 09:28:14 -0500209 /** @brief PLDM Sensor ID to OCC Instance mapping
210 */
Eddie Jamescbad2192021-10-07 09:39:39 -0500211 SensorToInstance sensorToOCCInstance;
Patrick Williams05e95592021-09-02 09:28:14 -0500212
Eddie Jamescbad2192021-10-07 09:39:39 -0500213 /** @brief PLDM Sensor ID to SBE Instance mapping
214 */
215 SensorToInstance sensorToSBEInstance;
216
217 /** @brief Sensor offset of OCC state set ID
Patrick Williams05e95592021-09-02 09:28:14 -0500218 * PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS in state sensor PDR.
219 */
George Liuf3a4a692021-12-28 13:59:51 +0800220 SensorOffset OCCSensorOffset = 0;
Eddie Jamescbad2192021-10-07 09:39:39 -0500221
222 /** @brief Sensor offset of the SBE state set ID
223 * PLDM_OEM_IBM_SBE_HRESET_STATE in state sensor PDR.
224 */
George Liuf3a4a692021-12-28 13:59:51 +0800225 SensorOffset SBESensorOffset = 0;
Patrick Williams05e95592021-09-02 09:28:14 -0500226
227 /** @brief OCC Instance mapping to PLDM Effecter ID
228 */
Eddie Jamescbad2192021-10-07 09:39:39 -0500229 InstanceToEffecter occInstanceToEffecter;
230
231 /** @brief SBE instance mapping to PLDM Effecter ID
232 */
233 InstanceToEffecter sbeInstanceToEffecter;
Patrick Williams05e95592021-09-02 09:28:14 -0500234
235 /** @brief compositeEffecterCount for OCC reset state effecter PDR */
Eddie Jamescbad2192021-10-07 09:39:39 -0500236 CompositeEffecterCount OCCEffecterCount = 0;
237
238 /** @brief compositeEffecterCount for SBE HRESET state effecter PDR */
239 CompositeEffecterCount SBEEffecterCount = 0;
Patrick Williams05e95592021-09-02 09:28:14 -0500240
241 /** @brief Position of Boot/Restart Cause stateSetID in OCC state
242 * effecter PDR
243 */
244 uint8_t bootRestartPosition = 0;
245
Eddie Jamescbad2192021-10-07 09:39:39 -0500246 /** @brief Position of the SBE maintenance stateSetID in the state
247 * effecter PDR
248 */
249 uint8_t sbeMaintenanceStatePosition = 0;
250
Chris Cainbae4d072022-02-28 09:46:50 -0600251 /** @brief OCC instance number for the PLDM message */
252 uint8_t pldmResponseOcc = 0;
253
254 /** @brief File descriptor for PLDM messages */
255 int pldmFd = -1;
256
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100257 /** pldm transport instance */
258 struct pldm_transport* pldmTransport = NULL;
259
260 struct pldm_transport_mctp_demux* mctpDemux;
261
Chris Cainbae4d072022-02-28 09:46:50 -0600262 /** @brief The response for the PLDM request msg is received flag.
263 */
264 bool pldmResponseReceived = false;
265
266 /** @brief The response for the PLDM request has timed out.
267 */
268 bool pldmResponseTimeout = false;
269
270 /** @brief timer event */
271 sdeventplus::Event sdpEvent;
272
273 /** @brief Timer that is started when PLDM command is sent
274 */
275 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> pldmRspTimer;
276
Chris Cain12d0b822022-04-22 17:29:18 -0500277 std::set<uint8_t> outstandingHResets;
278
Chris Cain755af102024-02-27 16:09:51 -0600279 /** @brief Flag to indicate that traces should be throttled.
280 Used to limit traces when there are issues getting OCC status.
281 */
282 static bool throttleTraces;
283
Chris Cainbae4d072022-02-28 09:46:50 -0600284 /** @brief Callback when PLDM response has not been received within the
285 * timeout period.
286 */
287 void pldmRspExpired();
288
289 /** @brief Close the MCTP file */
290 void pldmClose();
291
Patrick Williams05e95592021-09-02 09:28:14 -0500292 /** @brief When the OCC state changes host sends PlatformEventMessage
293 * StateSensorEvent, this function processes the D-Bus signal
294 * with the sensor event information and invokes the callback
295 * to change the OCC state.
296 *
297 * @param[in] msg - data associated with the subscribed signal
298 */
Patrick Williamsaf408082022-07-22 19:26:54 -0500299 void sensorEvent(sdbusplus::message_t& msg);
Patrick Williams05e95592021-09-02 09:28:14 -0500300
Chris Cain157467d2022-06-24 11:25:23 -0500301 /** @brief When the host state changes and if the CurrentHostState is
302 * xyz.openbmc_project.State.Host.HostState.Off then
303 * the cache of OCC sensors and effecters mapping is cleared.
304 *
305 * @param[in] msg - data associated with the subscribed signal
306 */
Patrick Williamsaf408082022-07-22 19:26:54 -0500307 void hostStateEvent(sdbusplus::message_t& msg);
Chris Cain157467d2022-06-24 11:25:23 -0500308
Chris Cainbae4d072022-02-28 09:46:50 -0600309 /** @brief Called when it is determined that the Host is not running.
310 * The cache of OCC sensors and effecters mapping is cleared.
311 */
312 void clearData();
313
Patrick Williams05e95592021-09-02 09:28:14 -0500314 /** @brief Check if the PDR cache for PLDM OCC sensors is valid
315 *
316 * @return true if cache is populated and false if the cache is not
317 * populated.
318 */
319 auto isOCCSensorCacheValid()
320 {
321 return (sensorToOCCInstance.empty() ? false : true);
322 }
323
324 /** @brief Check if the PDR cache for PLDM OCC effecters is valid
325 *
326 * @return true if cache is populated and false if the cache is not
327 * populated.
328 */
329 auto isPDREffecterCacheValid()
330 {
331 return (occInstanceToEffecter.empty() ? false : true);
332 }
Eddie Jamescbad2192021-10-07 09:39:39 -0500333
Rashmica Guptadb38e912023-05-25 10:33:46 +1000334 /** @brief Get a PLDM requester instance id
Eddie Jamescbad2192021-10-07 09:39:39 -0500335 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500336 * @return true if the id was found and false if not
337 */
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100338 bool getPldmInstanceId();
Eddie Jamescbad2192021-10-07 09:39:39 -0500339
Rashmica Guptadb38e912023-05-25 10:33:46 +1000340 /** @brief Free PLDM requester instance id */
341 void freePldmInstanceId();
342
Chris Cainbae4d072022-02-28 09:46:50 -0600343 /** @brief Encode a GetStateSensor command into a PLDM request
344 * @param[in] instance - OCC instance number
345 * @param[in] sensorId - OCC Active sensor ID number
346 *
347 * @return request - The encoded PLDM messsage to be sent
348 */
349 std::vector<uint8_t> encodeGetStateSensorRequest(uint8_t instance,
350 uint16_t sensorId);
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100351
352 /** @brief setup PLDM transport for sending and receiving PLDM messages.
353 *
354 * @return true on success, otherwise return false
355 */
356 int pldmOpen(void);
357
358 /** @brief Opens the MCTP socket for sending and receiving messages.
359 *
360 * @return true on success, otherwise returns a negative error code
361 */
362 int openMctpDemuxTransport();
363
Eddie Jamescbad2192021-10-07 09:39:39 -0500364 /** @brief Send the PLDM request
365 *
Chris Caind1b68262022-02-28 09:56:50 -0600366 * @param[in] request - the request data
Chris Cainbae4d072022-02-28 09:46:50 -0600367 * @param[in] rspExpected - false: no need to wait for the response
368 * true: will need to process response in callback
Eddie Jamescbad2192021-10-07 09:39:39 -0500369 */
Chris Cainbae4d072022-02-28 09:46:50 -0600370 void sendPldm(const std::vector<uint8_t>& request, const uint8_t instance,
371 const bool rspExpected = false);
372
373 /** @brief Register a callback function to handle the PLDM response */
374 void registerPldmRspCallback();
375
376 /** @brief callback for the PLDM response event
377 *
378 * @param[in] es - Populated event source
379 * @param[in] fd - Associated File descriptor
380 * @param[in] revents - Type of event
381 * @param[in] userData - User data that was passed during registration
382 *
383 * @return - 0 or positive number on success and negative
384 * errno otherwise
385 */
386 static int pldmRspCallback(sd_event_source* es, int fd, uint32_t revents,
387 void* userData);
Patrick Williams05e95592021-09-02 09:28:14 -0500388};
389
390} // namespace pldm