blob: 384ad2051606405a0c1cc269869a801eb46c8b60 [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 Williamsd7542c82024-08-16 15:20:28 -040066 callBack(callBack), sbeCallBack(sbeCallBack),
67 safeModeCallBack(safeModeCallBack), event(event),
Patrick Williams05e95592021-09-02 09:28:14 -050068 pldmEventSignal(
69 open_power::occ::utils::getBus(),
70 MatchRules::type::signal() +
71 MatchRules::member("StateSensorEvent") +
72 MatchRules::path("/xyz/openbmc_project/pldm") +
73 MatchRules::interface("xyz.openbmc_project.PLDM.Event"),
74 std::bind(std::mem_fn(&Interface::sensorEvent), this,
75 std::placeholders::_1)),
Chris Cain157467d2022-06-24 11:25:23 -050076 hostStateSignal(
77 open_power::occ::utils::getBus(),
78 MatchRules::propertiesChanged("/xyz/openbmc_project/state/host0",
79 "xyz.openbmc_project.State.Host"),
80 std::bind(std::mem_fn(&Interface::hostStateEvent), this,
81 std::placeholders::_1)),
Chris Cainbae4d072022-02-28 09:46:50 -060082 sdpEvent(sdeventplus::Event::get_default()),
83 pldmRspTimer(
84 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>(
85 sdpEvent, std::bind(&Interface::pldmRspExpired, this)))
Rashmica Guptadb38e912023-05-25 10:33:46 +100086 {
87 int rc = pldm_instance_db_init_default(&pldmInstanceIdDb);
88 if (rc)
89 {
90 throw std::system_category().default_error_condition(rc);
91 }
92 }
93
94 ~Interface()
95 {
96 int rc = pldm_instance_db_destroy(pldmInstanceIdDb);
97 if (rc)
98 {
99 std::cout << "pldm_instance_db_destroy failed, rc =" << rc << "\n";
100 }
101 }
Patrick Williams05e95592021-09-02 09:28:14 -0500102
Eddie Jamescbad2192021-10-07 09:39:39 -0500103 /** @brief Fetch the state sensor PDRs and populate the cache with
104 * sensorId to OCC/SBE instance mapping information and the sensor
105 * offset for the relevent state set.
Patrick Williams05e95592021-09-02 09:28:14 -0500106 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500107 * @param[in] stateSetId - the state set ID to look for
108 * @param[out] sensorInstanceMap - map of sensorID to instance
Patrick Williams05e95592021-09-02 09:28:14 -0500109 * @param[out] sensorOffset - sensor offset of interested state set ID
110 */
Eddie Jamescbad2192021-10-07 09:39:39 -0500111 void fetchSensorInfo(uint16_t stateSetId,
112 SensorToInstance& sensorInstanceMap,
113 SensorOffset& sensorOffset);
Patrick Williams05e95592021-09-02 09:28:14 -0500114
Eddie Jamescbad2192021-10-07 09:39:39 -0500115 /** @brief Fetch the OCC/SBE state effecter PDRs and populate the cache
116 * with OCC/SBE instance to EffecterID information.
Patrick Williams05e95592021-09-02 09:28:14 -0500117 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500118 * @param[in] stateSetId - the state set ID to look for
119 * @param[out] instanceToEffecterMap - map of instance to effecterID
Patrick Williams05e95592021-09-02 09:28:14 -0500120 * @param[out] count - sensor offset of interested state set ID
Eddie Jamescbad2192021-10-07 09:39:39 -0500121 * @param[out] stateIdPos - position of the stateSetID
Patrick Williams05e95592021-09-02 09:28:14 -0500122 */
Eddie James432dc482021-11-19 15:29:31 -0600123 void fetchEffecterInfo(uint16_t stateSetId,
Eddie Jamescbad2192021-10-07 09:39:39 -0500124 InstanceToEffecter& instanceToEffecterMap,
125 CompositeEffecterCount& count, uint8_t& stateIdPos);
Patrick Williams05e95592021-09-02 09:28:14 -0500126
127 /** @brief Prepare the request for SetStateEffecterStates command
128 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500129 * @param[in] effecterId - the instance effecter ID
130 * @param[in] effecterCount - compositeEffecterCount for the effecter PDR
131 * @param[in] stateIdPos - position of the stateSetID
132 * @param[in] stateSetValue - the value to set the state set to
Patrick Williams05e95592021-09-02 09:28:14 -0500133 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500134 * @return PLDM request message to be sent to host for OCC reset or SBE
135 * HRESET, empty response in the case of failure.
Patrick Williams05e95592021-09-02 09:28:14 -0500136 */
Patrick Williamsd7542c82024-08-16 15:20:28 -0400137 std::vector<uint8_t> prepareSetEffecterReq(
138 EffecterID effecterId, CompositeEffecterCount effecterCount,
139 uint8_t stateIdPos, uint8_t stateSetValue);
Patrick Williams05e95592021-09-02 09:28:14 -0500140
141 /** @brief Send the PLDM message to reset the OCC
142 *
143 * @param[in] instanceId - OCC instance to reset
144 *
145 */
146 void resetOCC(open_power::occ::instanceID occInstanceId);
147
Eddie Jamescbad2192021-10-07 09:39:39 -0500148 /** @brief Send the PLDM message to perform the HRESET
149 *
150 * @param[in] instanceID - SBE instance to HRESET
151 */
152 void sendHRESET(open_power::occ::instanceID sbeInstanceId);
153
Chris Cainbae4d072022-02-28 09:46:50 -0600154 /** @brief Check if the OCC active sensor is available
155 * On successful read, the Manager callback will be called to update
156 * the status
157 *
158 * @param[in] instance - OCC instance to check
159 */
160 void checkActiveSensor(uint8_t instance);
161
Chris Cain755af102024-02-27 16:09:51 -0600162 /** @brief Set the throttleTraces flag
163 *
164 * @param[in] throttle - Flag to indicate if traces should be throttled
165 */
166 void setTraceThrottle(const bool throttle);
167
Patrick Williams05e95592021-09-02 09:28:14 -0500168 private:
Rashmica Guptadb38e912023-05-25 10:33:46 +1000169 /** @brief PLDM instance ID database object used to get instance IDs
170 */
171 pldm_instance_db* pldmInstanceIdDb = nullptr;
172
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100173 /** @brief PLDM instance number used in PLDM requests
Chris Cain8b508bf2022-05-26 14:01:31 -0500174 */
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100175 std::optional<uint8_t> pldmInstanceID;
Chris Cain8b508bf2022-05-26 14:01:31 -0500176
Patrick Williams05e95592021-09-02 09:28:14 -0500177 /** @brief Callback handler to be invoked when the state of the OCC
178 * changes
179 */
180 std::function<bool(open_power::occ::instanceID, bool)> callBack = nullptr;
181
Eddie Jamescbad2192021-10-07 09:39:39 -0500182 /** @brief Callback handler to be invoked when the maintenance state of the
183 * SBE changes
184 */
185 std::function<void(open_power::occ::instanceID, bool)> sbeCallBack =
186 nullptr;
187
Sheldon Bailey31a2f132022-05-20 11:31:52 -0500188 /** @brief Callback handler to be invoked when the OCC is in SAFE Mode =
189 * true or when OCCs are in_service = false.
190 */
191 std::function<void(bool)> safeModeCallBack = nullptr;
192
Chris Cainbae4d072022-02-28 09:46:50 -0600193 /** @brief reference to sd_event wrapped in unique_ptr */
194 EventPtr& event;
195
196 /** @brief event source wrapped in unique_ptr */
197 EventSourcePtr eventSource;
198
Patrick Williams05e95592021-09-02 09:28:14 -0500199 /** @brief Used to subscribe to D-Bus PLDM StateSensorEvent signal and
200 * processes if the event corresponds to OCC state change.
201 */
202 sdbusplus::bus::match_t pldmEventSignal;
203
Chris Cain157467d2022-06-24 11:25:23 -0500204 /** @brief Used to subscribe for host state change signal */
205 sdbusplus::bus::match_t hostStateSignal;
206
Patrick Williams05e95592021-09-02 09:28:14 -0500207 /** @brief PLDM Sensor ID to OCC Instance mapping
208 */
Eddie Jamescbad2192021-10-07 09:39:39 -0500209 SensorToInstance sensorToOCCInstance;
Patrick Williams05e95592021-09-02 09:28:14 -0500210
Eddie Jamescbad2192021-10-07 09:39:39 -0500211 /** @brief PLDM Sensor ID to SBE Instance mapping
212 */
213 SensorToInstance sensorToSBEInstance;
214
215 /** @brief Sensor offset of OCC state set ID
Patrick Williams05e95592021-09-02 09:28:14 -0500216 * PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS in state sensor PDR.
217 */
George Liuf3a4a692021-12-28 13:59:51 +0800218 SensorOffset OCCSensorOffset = 0;
Eddie Jamescbad2192021-10-07 09:39:39 -0500219
220 /** @brief Sensor offset of the SBE state set ID
221 * PLDM_OEM_IBM_SBE_HRESET_STATE in state sensor PDR.
222 */
George Liuf3a4a692021-12-28 13:59:51 +0800223 SensorOffset SBESensorOffset = 0;
Patrick Williams05e95592021-09-02 09:28:14 -0500224
225 /** @brief OCC Instance mapping to PLDM Effecter ID
226 */
Eddie Jamescbad2192021-10-07 09:39:39 -0500227 InstanceToEffecter occInstanceToEffecter;
228
229 /** @brief SBE instance mapping to PLDM Effecter ID
230 */
231 InstanceToEffecter sbeInstanceToEffecter;
Patrick Williams05e95592021-09-02 09:28:14 -0500232
233 /** @brief compositeEffecterCount for OCC reset state effecter PDR */
Eddie Jamescbad2192021-10-07 09:39:39 -0500234 CompositeEffecterCount OCCEffecterCount = 0;
235
236 /** @brief compositeEffecterCount for SBE HRESET state effecter PDR */
237 CompositeEffecterCount SBEEffecterCount = 0;
Patrick Williams05e95592021-09-02 09:28:14 -0500238
239 /** @brief Position of Boot/Restart Cause stateSetID in OCC state
240 * effecter PDR
241 */
242 uint8_t bootRestartPosition = 0;
243
Eddie Jamescbad2192021-10-07 09:39:39 -0500244 /** @brief Position of the SBE maintenance stateSetID in the state
245 * effecter PDR
246 */
247 uint8_t sbeMaintenanceStatePosition = 0;
248
Chris Cainbae4d072022-02-28 09:46:50 -0600249 /** @brief OCC instance number for the PLDM message */
250 uint8_t pldmResponseOcc = 0;
251
252 /** @brief File descriptor for PLDM messages */
253 int pldmFd = -1;
254
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100255 /** pldm transport instance */
256 struct pldm_transport* pldmTransport = NULL;
257
258 struct pldm_transport_mctp_demux* mctpDemux;
259
Chris Cainbae4d072022-02-28 09:46:50 -0600260 /** @brief The response for the PLDM request msg is received flag.
261 */
262 bool pldmResponseReceived = false;
263
264 /** @brief The response for the PLDM request has timed out.
265 */
266 bool pldmResponseTimeout = false;
267
268 /** @brief timer event */
269 sdeventplus::Event sdpEvent;
270
271 /** @brief Timer that is started when PLDM command is sent
272 */
273 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> pldmRspTimer;
274
Chris Cain12d0b822022-04-22 17:29:18 -0500275 std::set<uint8_t> outstandingHResets;
276
Chris Cain755af102024-02-27 16:09:51 -0600277 /** @brief Flag to indicate that traces should be throttled.
278 Used to limit traces when there are issues getting OCC status.
279 */
280 static bool throttleTraces;
281
Chris Cainbae4d072022-02-28 09:46:50 -0600282 /** @brief Callback when PLDM response has not been received within the
283 * timeout period.
284 */
285 void pldmRspExpired();
286
287 /** @brief Close the MCTP file */
288 void pldmClose();
289
Patrick Williams05e95592021-09-02 09:28:14 -0500290 /** @brief When the OCC state changes host sends PlatformEventMessage
291 * StateSensorEvent, this function processes the D-Bus signal
292 * with the sensor event information and invokes the callback
293 * to change the OCC state.
294 *
295 * @param[in] msg - data associated with the subscribed signal
296 */
Patrick Williamsaf408082022-07-22 19:26:54 -0500297 void sensorEvent(sdbusplus::message_t& msg);
Patrick Williams05e95592021-09-02 09:28:14 -0500298
Chris Cain157467d2022-06-24 11:25:23 -0500299 /** @brief When the host state changes and if the CurrentHostState is
300 * xyz.openbmc_project.State.Host.HostState.Off then
301 * the cache of OCC sensors and effecters mapping is cleared.
302 *
303 * @param[in] msg - data associated with the subscribed signal
304 */
Patrick Williamsaf408082022-07-22 19:26:54 -0500305 void hostStateEvent(sdbusplus::message_t& msg);
Chris Cain157467d2022-06-24 11:25:23 -0500306
Chris Cainbae4d072022-02-28 09:46:50 -0600307 /** @brief Called when it is determined that the Host is not running.
308 * The cache of OCC sensors and effecters mapping is cleared.
309 */
310 void clearData();
311
Patrick Williams05e95592021-09-02 09:28:14 -0500312 /** @brief Check if the PDR cache for PLDM OCC sensors is valid
313 *
314 * @return true if cache is populated and false if the cache is not
315 * populated.
316 */
317 auto isOCCSensorCacheValid()
318 {
319 return (sensorToOCCInstance.empty() ? false : true);
320 }
321
322 /** @brief Check if the PDR cache for PLDM OCC effecters is valid
323 *
324 * @return true if cache is populated and false if the cache is not
325 * populated.
326 */
327 auto isPDREffecterCacheValid()
328 {
329 return (occInstanceToEffecter.empty() ? false : true);
330 }
Eddie Jamescbad2192021-10-07 09:39:39 -0500331
Rashmica Guptadb38e912023-05-25 10:33:46 +1000332 /** @brief Get a PLDM requester instance id
Eddie Jamescbad2192021-10-07 09:39:39 -0500333 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500334 * @return true if the id was found and false if not
335 */
Rashmica Guptaaeba51c2023-02-17 12:30:46 +1100336 bool getPldmInstanceId();
Eddie Jamescbad2192021-10-07 09:39:39 -0500337
Rashmica Guptadb38e912023-05-25 10:33:46 +1000338 /** @brief Free PLDM requester instance id */
339 void freePldmInstanceId();
340
Chris Cainbae4d072022-02-28 09:46:50 -0600341 /** @brief Encode a GetStateSensor command into a PLDM request
342 * @param[in] instance - OCC instance number
343 * @param[in] sensorId - OCC Active sensor ID number
344 *
345 * @return request - The encoded PLDM messsage to be sent
346 */
Patrick Williamsd7542c82024-08-16 15:20:28 -0400347 std::vector<uint8_t>
348 encodeGetStateSensorRequest(uint8_t instance, uint16_t sensorId);
Rashmica Gupta52328cb2023-02-15 10:38:16 +1100349
350 /** @brief setup PLDM transport for sending and receiving PLDM messages.
351 *
352 * @return true on success, otherwise return false
353 */
354 int pldmOpen(void);
355
356 /** @brief Opens the MCTP socket for sending and receiving messages.
357 *
358 * @return true on success, otherwise returns a negative error code
359 */
360 int openMctpDemuxTransport();
361
Eddie Jamescbad2192021-10-07 09:39:39 -0500362 /** @brief Send the PLDM request
363 *
Chris Caind1b68262022-02-28 09:56:50 -0600364 * @param[in] request - the request data
Chris Cainbae4d072022-02-28 09:46:50 -0600365 * @param[in] rspExpected - false: no need to wait for the response
366 * true: will need to process response in callback
Eddie Jamescbad2192021-10-07 09:39:39 -0500367 */
Chris Cainbae4d072022-02-28 09:46:50 -0600368 void sendPldm(const std::vector<uint8_t>& request, const uint8_t instance,
369 const bool rspExpected = false);
370
371 /** @brief Register a callback function to handle the PLDM response */
372 void registerPldmRspCallback();
373
374 /** @brief callback for the PLDM response event
375 *
376 * @param[in] es - Populated event source
377 * @param[in] fd - Associated File descriptor
378 * @param[in] revents - Type of event
379 * @param[in] userData - User data that was passed during registration
380 *
381 * @return - 0 or positive number on success and negative
382 * errno otherwise
383 */
384 static int pldmRspCallback(sd_event_source* es, int fd, uint32_t revents,
385 void* userData);
Patrick Williams05e95592021-09-02 09:28:14 -0500386};
387
388} // namespace pldm