blob: 6907a935c8281656bdd0783f4276975fb249144d [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>
Lakshmi Yadlapati6213f192024-07-01 11:50:09 -050010#include <libpldm/transport/af-mctp.h>
Rashmica Gupta52328cb2023-02-15 10:38:16 +110011#include <libpldm/transport/mctp-demux.h>
Patrick Williams05e95592021-09-02 09:28:14 -050012
13#include <sdbusplus/bus/match.hpp>
Chris Cainbae4d072022-02-28 09:46:50 -060014#include <sdeventplus/event.hpp>
15#include <sdeventplus/utility/timer.hpp>
Patrick Williams05e95592021-09-02 09:28:14 -050016
Chris Cainf0295f52024-09-12 15:41:14 -050017enum pldm_msg_type
18{
19 MSG_UNDEFINED = 0,
20 MSG_SENSOR_STATUS = 1,
21 MSG_OCC_RESET = 2,
22 MSG_HRESET = 3
23};
24
Patrick Williams05e95592021-09-02 09:28:14 -050025namespace pldm
26{
27
28namespace MatchRules = sdbusplus::bus::match::rules;
Chris Cainbae4d072022-02-28 09:46:50 -060029using namespace open_power::occ;
Patrick Williams05e95592021-09-02 09:28:14 -050030
31using CompositeEffecterCount = uint8_t;
32using EffecterID = uint16_t;
33using EntityType = uint16_t;
34using EntityInstance = uint16_t;
35using EventState = uint8_t;
Eddie Jamescbad2192021-10-07 09:39:39 -050036using InstanceToEffecter = std::map<open_power::occ::instanceID, EffecterID>;
Patrick Williams05e95592021-09-02 09:28:14 -050037using PdrList = std::vector<std::vector<uint8_t>>;
38using SensorID = uint16_t;
39using SensorOffset = uint8_t;
Eddie Jamescbad2192021-10-07 09:39:39 -050040using SensorToInstance = std::map<SensorID, open_power::occ::instanceID>;
Patrick Williams05e95592021-09-02 09:28:14 -050041using TerminusID = uint8_t;
42
Patrick Williams05e95592021-09-02 09:28:14 -050043/** @brief OCC instance starts with 0 for example "occ0" */
44constexpr open_power::occ::instanceID start = 0;
45
46/** @brief Hardcoded mctpEid for HBRT */
47constexpr mctp_eid_t mctpEid = 10;
48
Rashmica Gupta52328cb2023-02-15 10:38:16 +110049/** @brief Hardcoded TID */
50constexpr TerminusID tid = mctpEid;
51
Patrick Williams05e95592021-09-02 09:28:14 -050052/** @class Interface
53 *
54 * @brief Abstracts the PLDM details related to the OCC
55 */
56class Interface
57{
58 public:
59 Interface() = delete;
Rashmica Guptadb38e912023-05-25 10:33:46 +100060 //~Interface() = default;
Patrick Williams05e95592021-09-02 09:28:14 -050061 Interface(const Interface&) = delete;
62 Interface& operator=(const Interface&) = delete;
63 Interface(Interface&&) = delete;
64 Interface& operator=(Interface&&) = delete;
65
66 /** @brief Constructs the PLDM Interface object for OCC functions
67 *
Chris Cainf0295f52024-09-12 15:41:14 -050068 * @param[in] occActiveCallBack - callBack handler to invoke when the OCC
69 * state changes.
70 * @param[in] sbeCallBack - callBack handler to invoke when the SBE
71 * state changes.
72 * @param[in] safeModeCallBack - callBack handler to invoke when the
73 * system is in safe mode.
Patrick Williams05e95592021-09-02 09:28:14 -050074 */
75 explicit Interface(
Chris Cainf0295f52024-09-12 15:41:14 -050076 std::function<bool(open_power::occ::instanceID, bool)>
77 occActiveCallBack,
Chris Cainbae4d072022-02-28 09:46:50 -060078 std::function<void(open_power::occ::instanceID, bool)> sbeCallBack,
Sheldon Bailey31a2f132022-05-20 11:31:52 -050079 std::function<void(bool)> safeModeCallBack, EventPtr& event) :
Chris Cainf0295f52024-09-12 15:41:14 -050080 occActiveCallBack(occActiveCallBack), sbeCallBack(sbeCallBack),
Patrick Williamsd7542c82024-08-16 15:20:28 -040081 safeModeCallBack(safeModeCallBack), event(event),
Patrick Williams05e95592021-09-02 09:28:14 -050082 pldmEventSignal(
83 open_power::occ::utils::getBus(),
84 MatchRules::type::signal() +
85 MatchRules::member("StateSensorEvent") +
86 MatchRules::path("/xyz/openbmc_project/pldm") +
87 MatchRules::interface("xyz.openbmc_project.PLDM.Event"),
88 std::bind(std::mem_fn(&Interface::sensorEvent), this,
89 std::placeholders::_1)),
Chris Cain157467d2022-06-24 11:25:23 -050090 hostStateSignal(
91 open_power::occ::utils::getBus(),
92 MatchRules::propertiesChanged("/xyz/openbmc_project/state/host0",
93 "xyz.openbmc_project.State.Host"),
94 std::bind(std::mem_fn(&Interface::hostStateEvent), this,
95 std::placeholders::_1)),
Chris Cainbae4d072022-02-28 09:46:50 -060096 sdpEvent(sdeventplus::Event::get_default()),
97 pldmRspTimer(
98 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>(
99 sdpEvent, std::bind(&Interface::pldmRspExpired, this)))
Rashmica Guptadb38e912023-05-25 10:33:46 +1000100 {
101 int rc = pldm_instance_db_init_default(&pldmInstanceIdDb);
102 if (rc)
103 {
104 throw std::system_category().default_error_condition(rc);
105 }
106 }
107
108 ~Interface()
109 {
110 int rc = pldm_instance_db_destroy(pldmInstanceIdDb);
111 if (rc)
112 {
113 std::cout << "pldm_instance_db_destroy failed, rc =" << rc << "\n";
114 }
115 }
Patrick Williams05e95592021-09-02 09:28:14 -0500116
Eddie Jamescbad2192021-10-07 09:39:39 -0500117 /** @brief Fetch the state sensor PDRs and populate the cache with
118 * sensorId to OCC/SBE instance mapping information and the sensor
119 * offset for the relevent state set.
Patrick Williams05e95592021-09-02 09:28:14 -0500120 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500121 * @param[in] stateSetId - the state set ID to look for
122 * @param[out] sensorInstanceMap - map of sensorID to instance
Patrick Williams05e95592021-09-02 09:28:14 -0500123 * @param[out] sensorOffset - sensor offset of interested state set ID
124 */
Eddie Jamescbad2192021-10-07 09:39:39 -0500125 void fetchSensorInfo(uint16_t stateSetId,
126 SensorToInstance& sensorInstanceMap,
127 SensorOffset& sensorOffset);
Patrick Williams05e95592021-09-02 09:28:14 -0500128
Eddie Jamescbad2192021-10-07 09:39:39 -0500129 /** @brief Fetch the OCC/SBE state effecter PDRs and populate the cache
130 * with OCC/SBE instance to EffecterID information.
Patrick Williams05e95592021-09-02 09:28:14 -0500131 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500132 * @param[in] stateSetId - the state set ID to look for
133 * @param[out] instanceToEffecterMap - map of instance to effecterID
Patrick Williams05e95592021-09-02 09:28:14 -0500134 * @param[out] count - sensor offset of interested state set ID
Eddie Jamescbad2192021-10-07 09:39:39 -0500135 * @param[out] stateIdPos - position of the stateSetID
Patrick Williams05e95592021-09-02 09:28:14 -0500136 */
Eddie James432dc482021-11-19 15:29:31 -0600137 void fetchEffecterInfo(uint16_t stateSetId,
Eddie Jamescbad2192021-10-07 09:39:39 -0500138 InstanceToEffecter& instanceToEffecterMap,
139 CompositeEffecterCount& count, uint8_t& stateIdPos);
Patrick Williams05e95592021-09-02 09:28:14 -0500140
141 /** @brief Prepare the request for SetStateEffecterStates command
142 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500143 * @param[in] effecterId - the instance effecter ID
144 * @param[in] effecterCount - compositeEffecterCount for the effecter PDR
145 * @param[in] stateIdPos - position of the stateSetID
146 * @param[in] stateSetValue - the value to set the state set to
Patrick Williams05e95592021-09-02 09:28:14 -0500147 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500148 * @return PLDM request message to be sent to host for OCC reset or SBE
149 * HRESET, empty response in the case of failure.
Patrick Williams05e95592021-09-02 09:28:14 -0500150 */
Patrick Williamsd7542c82024-08-16 15:20:28 -0400151 std::vector<uint8_t> prepareSetEffecterReq(
152 EffecterID effecterId, CompositeEffecterCount effecterCount,
153 uint8_t stateIdPos, uint8_t stateSetValue);
Patrick Williams05e95592021-09-02 09:28:14 -0500154
155 /** @brief Send the PLDM message to reset the OCC
156 *
157 * @param[in] instanceId - OCC instance to reset
158 *
159 */
160 void resetOCC(open_power::occ::instanceID occInstanceId);
161
Eddie Jamescbad2192021-10-07 09:39:39 -0500162 /** @brief Send the PLDM message to perform the HRESET
163 *
164 * @param[in] instanceID - SBE instance to HRESET
165 */
166 void sendHRESET(open_power::occ::instanceID sbeInstanceId);
167
Chris Cainbae4d072022-02-28 09:46:50 -0600168 /** @brief Check if the OCC active sensor is available
169 * On successful read, the Manager callback will be called to update
170 * the status
171 *
172 * @param[in] instance - OCC instance to check
173 */
174 void checkActiveSensor(uint8_t instance);
175
Chris Cain755af102024-02-27 16:09:51 -0600176 /** @brief Set the throttleTraces flag
177 *
178 * @param[in] throttle - Flag to indicate if traces should be throttled
179 */
180 void setTraceThrottle(const bool throttle);
181
Patrick Williams05e95592021-09-02 09:28:14 -0500182 private:
Rashmica Guptadb38e912023-05-25 10:33:46 +1000183 /** @brief PLDM instance ID database object used to get instance IDs
184 */
185 pldm_instance_db* pldmInstanceIdDb = nullptr;
186
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100187 /** @brief PLDM instance number used in PLDM requests
Chris Cain8b508bf2022-05-26 14:01:31 -0500188 */
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100189 std::optional<uint8_t> pldmInstanceID;
Chris Cain8b508bf2022-05-26 14:01:31 -0500190
Patrick Williams05e95592021-09-02 09:28:14 -0500191 /** @brief Callback handler to be invoked when the state of the OCC
192 * changes
193 */
Chris Cainf0295f52024-09-12 15:41:14 -0500194 std::function<bool(open_power::occ::instanceID, bool)> occActiveCallBack =
195 nullptr;
Patrick Williams05e95592021-09-02 09:28:14 -0500196
Eddie Jamescbad2192021-10-07 09:39:39 -0500197 /** @brief Callback handler to be invoked when the maintenance state of the
198 * SBE changes
199 */
200 std::function<void(open_power::occ::instanceID, bool)> sbeCallBack =
201 nullptr;
202
Sheldon Bailey31a2f132022-05-20 11:31:52 -0500203 /** @brief Callback handler to be invoked when the OCC is in SAFE Mode =
204 * true or when OCCs are in_service = false.
205 */
206 std::function<void(bool)> safeModeCallBack = nullptr;
207
Chris Cainbae4d072022-02-28 09:46:50 -0600208 /** @brief reference to sd_event wrapped in unique_ptr */
209 EventPtr& event;
210
211 /** @brief event source wrapped in unique_ptr */
212 EventSourcePtr eventSource;
213
Patrick Williams05e95592021-09-02 09:28:14 -0500214 /** @brief Used to subscribe to D-Bus PLDM StateSensorEvent signal and
215 * processes if the event corresponds to OCC state change.
216 */
217 sdbusplus::bus::match_t pldmEventSignal;
218
Chris Cain157467d2022-06-24 11:25:23 -0500219 /** @brief Used to subscribe for host state change signal */
220 sdbusplus::bus::match_t hostStateSignal;
221
Patrick Williams05e95592021-09-02 09:28:14 -0500222 /** @brief PLDM Sensor ID to OCC Instance mapping
223 */
Eddie Jamescbad2192021-10-07 09:39:39 -0500224 SensorToInstance sensorToOCCInstance;
Patrick Williams05e95592021-09-02 09:28:14 -0500225
Eddie Jamescbad2192021-10-07 09:39:39 -0500226 /** @brief PLDM Sensor ID to SBE Instance mapping
227 */
228 SensorToInstance sensorToSBEInstance;
229
230 /** @brief Sensor offset of OCC state set ID
Patrick Williams05e95592021-09-02 09:28:14 -0500231 * PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS in state sensor PDR.
232 */
George Liuf3a4a692021-12-28 13:59:51 +0800233 SensorOffset OCCSensorOffset = 0;
Eddie Jamescbad2192021-10-07 09:39:39 -0500234
235 /** @brief Sensor offset of the SBE state set ID
236 * PLDM_OEM_IBM_SBE_HRESET_STATE in state sensor PDR.
237 */
George Liuf3a4a692021-12-28 13:59:51 +0800238 SensorOffset SBESensorOffset = 0;
Patrick Williams05e95592021-09-02 09:28:14 -0500239
240 /** @brief OCC Instance mapping to PLDM Effecter ID
241 */
Eddie Jamescbad2192021-10-07 09:39:39 -0500242 InstanceToEffecter occInstanceToEffecter;
243
244 /** @brief SBE instance mapping to PLDM Effecter ID
245 */
246 InstanceToEffecter sbeInstanceToEffecter;
Patrick Williams05e95592021-09-02 09:28:14 -0500247
248 /** @brief compositeEffecterCount for OCC reset state effecter PDR */
Eddie Jamescbad2192021-10-07 09:39:39 -0500249 CompositeEffecterCount OCCEffecterCount = 0;
250
251 /** @brief compositeEffecterCount for SBE HRESET state effecter PDR */
252 CompositeEffecterCount SBEEffecterCount = 0;
Patrick Williams05e95592021-09-02 09:28:14 -0500253
254 /** @brief Position of Boot/Restart Cause stateSetID in OCC state
255 * effecter PDR
256 */
257 uint8_t bootRestartPosition = 0;
258
Eddie Jamescbad2192021-10-07 09:39:39 -0500259 /** @brief Position of the SBE maintenance stateSetID in the state
260 * effecter PDR
261 */
262 uint8_t sbeMaintenanceStatePosition = 0;
263
Chris Cainbae4d072022-02-28 09:46:50 -0600264 /** @brief OCC instance number for the PLDM message */
265 uint8_t pldmResponseOcc = 0;
266
267 /** @brief File descriptor for PLDM messages */
268 int pldmFd = -1;
269
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100270 /** pldm transport instance */
271 struct pldm_transport* pldmTransport = NULL;
272
Chris Cainf0295f52024-09-12 15:41:14 -0500273 static enum pldm_msg_type msgType;
274
Lakshmi Yadlapati6213f192024-07-01 11:50:09 -0500275 union TransportImpl
276 {
277 struct pldm_transport_mctp_demux* mctpDemux;
278 struct pldm_transport_af_mctp* afMctp;
279 };
280
281 TransportImpl impl;
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100282
Chris Cainbae4d072022-02-28 09:46:50 -0600283 /** @brief The response for the PLDM request msg is received flag.
284 */
285 bool pldmResponseReceived = false;
286
287 /** @brief The response for the PLDM request has timed out.
288 */
289 bool pldmResponseTimeout = false;
290
Chris Cainf0295f52024-09-12 15:41:14 -0500291 /** @brief The instance ID for the OCC/HRESET request */
292 static open_power::occ::instanceID resetInstance;
293
Chris Cainbae4d072022-02-28 09:46:50 -0600294 /** @brief timer event */
295 sdeventplus::Event sdpEvent;
296
297 /** @brief Timer that is started when PLDM command is sent
298 */
299 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> pldmRspTimer;
300
Chris Cain12d0b822022-04-22 17:29:18 -0500301 std::set<uint8_t> outstandingHResets;
302
Chris Cain755af102024-02-27 16:09:51 -0600303 /** @brief Flag to indicate that traces should be throttled.
304 Used to limit traces when there are issues getting OCC status.
305 */
306 static bool throttleTraces;
307
Chris Cainbae4d072022-02-28 09:46:50 -0600308 /** @brief Callback when PLDM response has not been received within the
309 * timeout period.
310 */
311 void pldmRspExpired();
312
313 /** @brief Close the MCTP file */
314 void pldmClose();
315
Patrick Williams05e95592021-09-02 09:28:14 -0500316 /** @brief When the OCC state changes host sends PlatformEventMessage
317 * StateSensorEvent, this function processes the D-Bus signal
318 * with the sensor event information and invokes the callback
319 * to change the OCC state.
320 *
321 * @param[in] msg - data associated with the subscribed signal
322 */
Patrick Williamsaf408082022-07-22 19:26:54 -0500323 void sensorEvent(sdbusplus::message_t& msg);
Patrick Williams05e95592021-09-02 09:28:14 -0500324
Chris Cain157467d2022-06-24 11:25:23 -0500325 /** @brief When the host state changes and if the CurrentHostState is
326 * xyz.openbmc_project.State.Host.HostState.Off then
327 * the cache of OCC sensors and effecters mapping is cleared.
328 *
329 * @param[in] msg - data associated with the subscribed signal
330 */
Patrick Williamsaf408082022-07-22 19:26:54 -0500331 void hostStateEvent(sdbusplus::message_t& msg);
Chris Cain157467d2022-06-24 11:25:23 -0500332
Chris Cainbae4d072022-02-28 09:46:50 -0600333 /** @brief Called when it is determined that the Host is not running.
334 * The cache of OCC sensors and effecters mapping is cleared.
335 */
336 void clearData();
337
Patrick Williams05e95592021-09-02 09:28:14 -0500338 /** @brief Check if the PDR cache for PLDM OCC sensors is valid
339 *
340 * @return true if cache is populated and false if the cache is not
341 * populated.
342 */
343 auto isOCCSensorCacheValid()
344 {
345 return (sensorToOCCInstance.empty() ? false : true);
346 }
347
348 /** @brief Check if the PDR cache for PLDM OCC effecters is valid
349 *
350 * @return true if cache is populated and false if the cache is not
351 * populated.
352 */
353 auto isPDREffecterCacheValid()
354 {
355 return (occInstanceToEffecter.empty() ? false : true);
356 }
Eddie Jamescbad2192021-10-07 09:39:39 -0500357
Rashmica Guptadb38e912023-05-25 10:33:46 +1000358 /** @brief Get a PLDM requester instance id
Eddie Jamescbad2192021-10-07 09:39:39 -0500359 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500360 * @return true if the id was found and false if not
361 */
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100362 bool getPldmInstanceId();
Eddie Jamescbad2192021-10-07 09:39:39 -0500363
Rashmica Guptadb38e912023-05-25 10:33:46 +1000364 /** @brief Free PLDM requester instance id */
365 void freePldmInstanceId();
366
Chris Cainbae4d072022-02-28 09:46:50 -0600367 /** @brief Encode a GetStateSensor command into a PLDM request
368 * @param[in] instance - OCC instance number
369 * @param[in] sensorId - OCC Active sensor ID number
370 *
371 * @return request - The encoded PLDM messsage to be sent
372 */
Patrick Williamsd7542c82024-08-16 15:20:28 -0400373 std::vector<uint8_t>
374 encodeGetStateSensorRequest(uint8_t instance, uint16_t sensorId);
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100375
376 /** @brief setup PLDM transport for sending and receiving PLDM messages.
377 *
378 * @return true on success, otherwise return false
379 */
380 int pldmOpen(void);
381
382 /** @brief Opens the MCTP socket for sending and receiving messages.
383 *
Lakshmi Yadlapati6213f192024-07-01 11:50:09 -0500384 * @return 0 on success, otherwise returns a negative error code
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100385 */
386 int openMctpDemuxTransport();
387
Lakshmi Yadlapati6213f192024-07-01 11:50:09 -0500388 /** @brief Opens the MCTP AF_MCTP for sending and receiving messages.
389 *
390 * @return 0 on success, otherwise returns a negative error code
391 */
392 int openAfMctpTransport();
393
Eddie Jamescbad2192021-10-07 09:39:39 -0500394 /** @brief Send the PLDM request
395 *
Chris Caind1b68262022-02-28 09:56:50 -0600396 * @param[in] request - the request data
Chris Cainbae4d072022-02-28 09:46:50 -0600397 * @param[in] rspExpected - false: no need to wait for the response
398 * true: will need to process response in callback
Eddie Jamescbad2192021-10-07 09:39:39 -0500399 */
Chris Cainbae4d072022-02-28 09:46:50 -0600400 void sendPldm(const std::vector<uint8_t>& request, const uint8_t instance,
401 const bool rspExpected = false);
402
403 /** @brief Register a callback function to handle the PLDM response */
404 void registerPldmRspCallback();
405
406 /** @brief callback for the PLDM response event
407 *
408 * @param[in] es - Populated event source
409 * @param[in] fd - Associated File descriptor
410 * @param[in] revents - Type of event
411 * @param[in] userData - User data that was passed during registration
412 *
413 * @return - 0 or positive number on success and negative
414 * errno otherwise
415 */
416 static int pldmRspCallback(sd_event_source* es, int fd, uint32_t revents,
417 void* userData);
Chris Cainf0295f52024-09-12 15:41:14 -0500418
419 /** @brief callback for a OCC / HRESET response event
420 *
421 * @param[in] es - Populated event source
422 * @param[in] fd - Associated File descriptor
423 * @param[in] revents - Type of event
424 * @param[in] userData - User data that was passed during registration
425 *
426 * @return - 0 or positive number on success and negative
427 * errno otherwise
428 */
429 static int pldmResetCallback(sd_event_source* /*es*/,
430 __attribute__((unused)) int fd,
431 uint32_t revents, void* userData);
Patrick Williams05e95592021-09-02 09:28:14 -0500432};
433
434} // namespace pldm
Chris Cainf0295f52024-09-12 15:41:14 -0500435
436template <>
437struct std::formatter<enum pldm_msg_type> : formatter<int>
438{
439 auto format(enum pldm_msg_type m, format_context& ctx) const
440 {
441 return formatter<int>::format(std::to_underlying(m), ctx);
442 }
443};