blob: f67a36b530272eb6395f8c8e97620fb3c1d6c2fe [file] [log] [blame]
Patrick Williams05e95592021-09-02 09:28:14 -05001#pragma once
Chris Cainbae4d072022-02-28 09:46:50 -06002#include "occ_events.hpp"
Patrick Williams05e95592021-09-02 09:28:14 -05003#include "occ_status.hpp"
4#include "utils.hpp"
5
6#include <libpldm/pldm.h>
7
8#include <sdbusplus/bus/match.hpp>
Chris Cainbae4d072022-02-28 09:46:50 -06009#include <sdeventplus/event.hpp>
10#include <sdeventplus/utility/timer.hpp>
Patrick Williams05e95592021-09-02 09:28:14 -050011
12namespace pldm
13{
14
15namespace MatchRules = sdbusplus::bus::match::rules;
Chris Cainbae4d072022-02-28 09:46:50 -060016using namespace open_power::occ;
Patrick Williams05e95592021-09-02 09:28:14 -050017
18using CompositeEffecterCount = uint8_t;
19using EffecterID = uint16_t;
20using EntityType = uint16_t;
21using EntityInstance = uint16_t;
22using EventState = uint8_t;
Eddie Jamescbad2192021-10-07 09:39:39 -050023using InstanceToEffecter = std::map<open_power::occ::instanceID, EffecterID>;
Patrick Williams05e95592021-09-02 09:28:14 -050024using PdrList = std::vector<std::vector<uint8_t>>;
25using SensorID = uint16_t;
26using SensorOffset = uint8_t;
Eddie Jamescbad2192021-10-07 09:39:39 -050027using SensorToInstance = std::map<SensorID, open_power::occ::instanceID>;
Patrick Williams05e95592021-09-02 09:28:14 -050028using TerminusID = uint8_t;
29
30/** @brief Hardcoded TID */
31constexpr TerminusID tid = 0;
32
33/** @brief OCC instance starts with 0 for example "occ0" */
34constexpr open_power::occ::instanceID start = 0;
35
36/** @brief Hardcoded mctpEid for HBRT */
37constexpr mctp_eid_t mctpEid = 10;
38
39/** @class Interface
40 *
41 * @brief Abstracts the PLDM details related to the OCC
42 */
43class Interface
44{
45 public:
46 Interface() = delete;
47 ~Interface() = default;
48 Interface(const Interface&) = delete;
49 Interface& operator=(const Interface&) = delete;
50 Interface(Interface&&) = delete;
51 Interface& operator=(Interface&&) = delete;
52
53 /** @brief Constructs the PLDM Interface object for OCC functions
54 *
55 * @param[in] callBack - callBack handler to invoke when the OCC state
56 * changes.
57 */
58 explicit Interface(
Eddie Jamescbad2192021-10-07 09:39:39 -050059 std::function<bool(open_power::occ::instanceID, bool)> callBack,
Chris Cainbae4d072022-02-28 09:46:50 -060060 std::function<void(open_power::occ::instanceID, bool)> sbeCallBack,
Sheldon Bailey31a2f132022-05-20 11:31:52 -050061 std::function<void(bool)> safeModeCallBack, EventPtr& event) :
Patrick Williams05e95592021-09-02 09:28:14 -050062 callBack(callBack),
Sheldon Bailey31a2f132022-05-20 11:31:52 -050063 sbeCallBack(sbeCallBack), safeModeCallBack(safeModeCallBack),
64 event(event),
Patrick Williams05e95592021-09-02 09:28:14 -050065 pldmEventSignal(
66 open_power::occ::utils::getBus(),
67 MatchRules::type::signal() +
68 MatchRules::member("StateSensorEvent") +
69 MatchRules::path("/xyz/openbmc_project/pldm") +
70 MatchRules::interface("xyz.openbmc_project.PLDM.Event"),
71 std::bind(std::mem_fn(&Interface::sensorEvent), this,
72 std::placeholders::_1)),
Chris Cainbae4d072022-02-28 09:46:50 -060073 sdpEvent(sdeventplus::Event::get_default()),
74 pldmRspTimer(
75 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>(
76 sdpEvent, std::bind(&Interface::pldmRspExpired, this)))
George Liub5ca1012021-09-10 12:53:11 +080077 {}
Patrick Williams05e95592021-09-02 09:28:14 -050078
Eddie Jamescbad2192021-10-07 09:39:39 -050079 /** @brief Fetch the state sensor PDRs and populate the cache with
80 * sensorId to OCC/SBE instance mapping information and the sensor
81 * offset for the relevent state set.
Patrick Williams05e95592021-09-02 09:28:14 -050082 *
Eddie Jamescbad2192021-10-07 09:39:39 -050083 * @param[in] stateSetId - the state set ID to look for
84 * @param[out] sensorInstanceMap - map of sensorID to instance
Patrick Williams05e95592021-09-02 09:28:14 -050085 * @param[out] sensorOffset - sensor offset of interested state set ID
86 */
Eddie Jamescbad2192021-10-07 09:39:39 -050087 void fetchSensorInfo(uint16_t stateSetId,
88 SensorToInstance& sensorInstanceMap,
89 SensorOffset& sensorOffset);
Patrick Williams05e95592021-09-02 09:28:14 -050090
Eddie Jamescbad2192021-10-07 09:39:39 -050091 /** @brief Fetch the OCC/SBE state effecter PDRs and populate the cache
92 * with OCC/SBE instance to EffecterID information.
Patrick Williams05e95592021-09-02 09:28:14 -050093 *
Eddie Jamescbad2192021-10-07 09:39:39 -050094 * @param[in] stateSetId - the state set ID to look for
95 * @param[out] instanceToEffecterMap - map of instance to effecterID
Patrick Williams05e95592021-09-02 09:28:14 -050096 * @param[out] count - sensor offset of interested state set ID
Eddie Jamescbad2192021-10-07 09:39:39 -050097 * @param[out] stateIdPos - position of the stateSetID
Patrick Williams05e95592021-09-02 09:28:14 -050098 */
Eddie James432dc482021-11-19 15:29:31 -060099 void fetchEffecterInfo(uint16_t stateSetId,
Eddie Jamescbad2192021-10-07 09:39:39 -0500100 InstanceToEffecter& instanceToEffecterMap,
101 CompositeEffecterCount& count, uint8_t& stateIdPos);
Patrick Williams05e95592021-09-02 09:28:14 -0500102
103 /** @brief Prepare the request for SetStateEffecterStates command
104 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500105 * @param[in] effecterId - the instance effecter ID
106 * @param[in] effecterCount - compositeEffecterCount for the effecter PDR
107 * @param[in] stateIdPos - position of the stateSetID
108 * @param[in] stateSetValue - the value to set the state set to
Patrick Williams05e95592021-09-02 09:28:14 -0500109 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500110 * @return PLDM request message to be sent to host for OCC reset or SBE
111 * HRESET, empty response in the case of failure.
Patrick Williams05e95592021-09-02 09:28:14 -0500112 */
113 std::vector<uint8_t>
Chris Cain8b508bf2022-05-26 14:01:31 -0500114 prepareSetEffecterReq(EffecterID effecterId,
Patrick Williams05e95592021-09-02 09:28:14 -0500115 CompositeEffecterCount effecterCount,
Eddie Jamescbad2192021-10-07 09:39:39 -0500116 uint8_t stateIdPos, uint8_t stateSetValue);
Patrick Williams05e95592021-09-02 09:28:14 -0500117
118 /** @brief Send the PLDM message to reset the OCC
119 *
120 * @param[in] instanceId - OCC instance to reset
121 *
122 */
123 void resetOCC(open_power::occ::instanceID occInstanceId);
124
Eddie Jamescbad2192021-10-07 09:39:39 -0500125 /** @brief Send the PLDM message to perform the HRESET
126 *
127 * @param[in] instanceID - SBE instance to HRESET
128 */
129 void sendHRESET(open_power::occ::instanceID sbeInstanceId);
130
Chris Cainbae4d072022-02-28 09:46:50 -0600131 /** @brief Check if the OCC active sensor is available
132 * On successful read, the Manager callback will be called to update
133 * the status
134 *
135 * @param[in] instance - OCC instance to check
136 */
137 void checkActiveSensor(uint8_t instance);
138
Patrick Williams05e95592021-09-02 09:28:14 -0500139 private:
Chris Cain8b508bf2022-05-26 14:01:31 -0500140 /** @brief MCTP instance number used in PLDM requests
141 */
142 std::optional<uint8_t> mctpInstance;
143
Patrick Williams05e95592021-09-02 09:28:14 -0500144 /** @brief Callback handler to be invoked when the state of the OCC
145 * changes
146 */
147 std::function<bool(open_power::occ::instanceID, bool)> callBack = nullptr;
148
Eddie Jamescbad2192021-10-07 09:39:39 -0500149 /** @brief Callback handler to be invoked when the maintenance state of the
150 * SBE changes
151 */
152 std::function<void(open_power::occ::instanceID, bool)> sbeCallBack =
153 nullptr;
154
Sheldon Bailey31a2f132022-05-20 11:31:52 -0500155 /** @brief Callback handler to be invoked when the OCC is in SAFE Mode =
156 * true or when OCCs are in_service = false.
157 */
158 std::function<void(bool)> safeModeCallBack = nullptr;
159
Chris Cainbae4d072022-02-28 09:46:50 -0600160 /** @brief reference to sd_event wrapped in unique_ptr */
161 EventPtr& event;
162
163 /** @brief event source wrapped in unique_ptr */
164 EventSourcePtr eventSource;
165
Patrick Williams05e95592021-09-02 09:28:14 -0500166 /** @brief Used to subscribe to D-Bus PLDM StateSensorEvent signal and
167 * processes if the event corresponds to OCC state change.
168 */
169 sdbusplus::bus::match_t pldmEventSignal;
170
Patrick Williams05e95592021-09-02 09:28:14 -0500171 /** @brief PLDM Sensor ID to OCC Instance mapping
172 */
Eddie Jamescbad2192021-10-07 09:39:39 -0500173 SensorToInstance sensorToOCCInstance;
Patrick Williams05e95592021-09-02 09:28:14 -0500174
Eddie Jamescbad2192021-10-07 09:39:39 -0500175 /** @brief PLDM Sensor ID to SBE Instance mapping
176 */
177 SensorToInstance sensorToSBEInstance;
178
179 /** @brief Sensor offset of OCC state set ID
Patrick Williams05e95592021-09-02 09:28:14 -0500180 * PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS in state sensor PDR.
181 */
George Liuf3a4a692021-12-28 13:59:51 +0800182 SensorOffset OCCSensorOffset = 0;
Eddie Jamescbad2192021-10-07 09:39:39 -0500183
184 /** @brief Sensor offset of the SBE state set ID
185 * PLDM_OEM_IBM_SBE_HRESET_STATE in state sensor PDR.
186 */
George Liuf3a4a692021-12-28 13:59:51 +0800187 SensorOffset SBESensorOffset = 0;
Patrick Williams05e95592021-09-02 09:28:14 -0500188
189 /** @brief OCC Instance mapping to PLDM Effecter ID
190 */
Eddie Jamescbad2192021-10-07 09:39:39 -0500191 InstanceToEffecter occInstanceToEffecter;
192
193 /** @brief SBE instance mapping to PLDM Effecter ID
194 */
195 InstanceToEffecter sbeInstanceToEffecter;
Patrick Williams05e95592021-09-02 09:28:14 -0500196
197 /** @brief compositeEffecterCount for OCC reset state effecter PDR */
Eddie Jamescbad2192021-10-07 09:39:39 -0500198 CompositeEffecterCount OCCEffecterCount = 0;
199
200 /** @brief compositeEffecterCount for SBE HRESET state effecter PDR */
201 CompositeEffecterCount SBEEffecterCount = 0;
Patrick Williams05e95592021-09-02 09:28:14 -0500202
203 /** @brief Position of Boot/Restart Cause stateSetID in OCC state
204 * effecter PDR
205 */
206 uint8_t bootRestartPosition = 0;
207
Eddie Jamescbad2192021-10-07 09:39:39 -0500208 /** @brief Position of the SBE maintenance stateSetID in the state
209 * effecter PDR
210 */
211 uint8_t sbeMaintenanceStatePosition = 0;
212
Chris Cainbae4d072022-02-28 09:46:50 -0600213 /** @brief OCC instance number for the PLDM message */
214 uint8_t pldmResponseOcc = 0;
215
216 /** @brief File descriptor for PLDM messages */
217 int pldmFd = -1;
218
Chris Cainbae4d072022-02-28 09:46:50 -0600219 /** @brief The response for the PLDM request msg is received flag.
220 */
221 bool pldmResponseReceived = false;
222
223 /** @brief The response for the PLDM request has timed out.
224 */
225 bool pldmResponseTimeout = false;
226
227 /** @brief timer event */
228 sdeventplus::Event sdpEvent;
229
230 /** @brief Timer that is started when PLDM command is sent
231 */
232 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> pldmRspTimer;
233
Chris Cain12d0b822022-04-22 17:29:18 -0500234 std::set<uint8_t> outstandingHResets;
235
Chris Cainbae4d072022-02-28 09:46:50 -0600236 /** @brief Callback when PLDM response has not been received within the
237 * timeout period.
238 */
239 void pldmRspExpired();
240
241 /** @brief Close the MCTP file */
242 void pldmClose();
243
Patrick Williams05e95592021-09-02 09:28:14 -0500244 /** @brief When the OCC state changes host sends PlatformEventMessage
245 * StateSensorEvent, this function processes the D-Bus signal
246 * with the sensor event information and invokes the callback
247 * to change the OCC state.
248 *
249 * @param[in] msg - data associated with the subscribed signal
250 */
251 void sensorEvent(sdbusplus::message::message& msg);
252
Chris Cainbae4d072022-02-28 09:46:50 -0600253 /** @brief Called when it is determined that the Host is not running.
254 * The cache of OCC sensors and effecters mapping is cleared.
255 */
256 void clearData();
257
Patrick Williams05e95592021-09-02 09:28:14 -0500258 /** @brief Check if the PDR cache for PLDM OCC sensors is valid
259 *
260 * @return true if cache is populated and false if the cache is not
261 * populated.
262 */
263 auto isOCCSensorCacheValid()
264 {
265 return (sensorToOCCInstance.empty() ? false : true);
266 }
267
268 /** @brief Check if the PDR cache for PLDM OCC effecters is valid
269 *
270 * @return true if cache is populated and false if the cache is not
271 * populated.
272 */
273 auto isPDREffecterCacheValid()
274 {
275 return (occInstanceToEffecter.empty() ? false : true);
276 }
Eddie Jamescbad2192021-10-07 09:39:39 -0500277
278 /** @brief Query PLDM for the MCTP requester instance id
279 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500280 * @return true if the id was found and false if not
281 */
Chris Cain8b508bf2022-05-26 14:01:31 -0500282 bool getMctpInstanceId();
Eddie Jamescbad2192021-10-07 09:39:39 -0500283
Chris Cainbae4d072022-02-28 09:46:50 -0600284 /** @brief Encode a GetStateSensor command into a PLDM request
285 * @param[in] instance - OCC instance number
286 * @param[in] sensorId - OCC Active sensor ID number
287 *
288 * @return request - The encoded PLDM messsage to be sent
289 */
290 std::vector<uint8_t> encodeGetStateSensorRequest(uint8_t instance,
291 uint16_t sensorId);
Eddie Jamescbad2192021-10-07 09:39:39 -0500292 /** @brief Send the PLDM request
293 *
Chris Caind1b68262022-02-28 09:56:50 -0600294 * @param[in] request - the request data
Chris Cainbae4d072022-02-28 09:46:50 -0600295 * @param[in] rspExpected - false: no need to wait for the response
296 * true: will need to process response in callback
Eddie Jamescbad2192021-10-07 09:39:39 -0500297 */
Chris Cainbae4d072022-02-28 09:46:50 -0600298 void sendPldm(const std::vector<uint8_t>& request, const uint8_t instance,
299 const bool rspExpected = false);
300
301 /** @brief Register a callback function to handle the PLDM response */
302 void registerPldmRspCallback();
303
304 /** @brief callback for the PLDM response event
305 *
306 * @param[in] es - Populated event source
307 * @param[in] fd - Associated File descriptor
308 * @param[in] revents - Type of event
309 * @param[in] userData - User data that was passed during registration
310 *
311 * @return - 0 or positive number on success and negative
312 * errno otherwise
313 */
314 static int pldmRspCallback(sd_event_source* es, int fd, uint32_t revents,
315 void* userData);
Patrick Williams05e95592021-09-02 09:28:14 -0500316};
317
318} // namespace pldm