blob: 40bc78b6bdc1e1a6d9242de89b8613063880c25e [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)),
73 hostStateSignal(
74 open_power::occ::utils::getBus(),
75 MatchRules::propertiesChanged("/xyz/openbmc_project/state/host0",
76 "xyz.openbmc_project.State.Host"),
77 std::bind(std::mem_fn(&Interface::hostStateEvent), this,
Chris Cainbae4d072022-02-28 09:46:50 -060078 std::placeholders::_1)),
79 sdpEvent(sdeventplus::Event::get_default()),
80 pldmRspTimer(
81 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>(
82 sdpEvent, std::bind(&Interface::pldmRspExpired, this)))
George Liub5ca1012021-09-10 12:53:11 +080083 {}
Patrick Williams05e95592021-09-02 09:28:14 -050084
Eddie Jamescbad2192021-10-07 09:39:39 -050085 /** @brief Fetch the state sensor PDRs and populate the cache with
86 * sensorId to OCC/SBE instance mapping information and the sensor
87 * offset for the relevent state set.
Patrick Williams05e95592021-09-02 09:28:14 -050088 *
Eddie Jamescbad2192021-10-07 09:39:39 -050089 * @param[in] stateSetId - the state set ID to look for
90 * @param[out] sensorInstanceMap - map of sensorID to instance
Patrick Williams05e95592021-09-02 09:28:14 -050091 * @param[out] sensorOffset - sensor offset of interested state set ID
92 */
Eddie Jamescbad2192021-10-07 09:39:39 -050093 void fetchSensorInfo(uint16_t stateSetId,
94 SensorToInstance& sensorInstanceMap,
95 SensorOffset& sensorOffset);
Patrick Williams05e95592021-09-02 09:28:14 -050096
Eddie Jamescbad2192021-10-07 09:39:39 -050097 /** @brief Fetch the OCC/SBE state effecter PDRs and populate the cache
98 * with OCC/SBE instance to EffecterID information.
Patrick Williams05e95592021-09-02 09:28:14 -050099 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500100 * @param[in] stateSetId - the state set ID to look for
101 * @param[out] instanceToEffecterMap - map of instance to effecterID
Patrick Williams05e95592021-09-02 09:28:14 -0500102 * @param[out] count - sensor offset of interested state set ID
Eddie Jamescbad2192021-10-07 09:39:39 -0500103 * @param[out] stateIdPos - position of the stateSetID
Patrick Williams05e95592021-09-02 09:28:14 -0500104 */
Eddie James432dc482021-11-19 15:29:31 -0600105 void fetchEffecterInfo(uint16_t stateSetId,
Eddie Jamescbad2192021-10-07 09:39:39 -0500106 InstanceToEffecter& instanceToEffecterMap,
107 CompositeEffecterCount& count, uint8_t& stateIdPos);
Patrick Williams05e95592021-09-02 09:28:14 -0500108
109 /** @brief Prepare the request for SetStateEffecterStates command
110 *
111 * @param[in] instanceId - PLDM instanceID
Eddie Jamescbad2192021-10-07 09:39:39 -0500112 * @param[in] effecterId - the instance effecter ID
113 * @param[in] effecterCount - compositeEffecterCount for the effecter PDR
114 * @param[in] stateIdPos - position of the stateSetID
115 * @param[in] stateSetValue - the value to set the state set to
Patrick Williams05e95592021-09-02 09:28:14 -0500116 *
Eddie Jamescbad2192021-10-07 09:39:39 -0500117 * @return PLDM request message to be sent to host for OCC reset or SBE
118 * HRESET, empty response in the case of failure.
Patrick Williams05e95592021-09-02 09:28:14 -0500119 */
120 std::vector<uint8_t>
121 prepareSetEffecterReq(uint8_t instanceId, EffecterID effecterId,
122 CompositeEffecterCount effecterCount,
Eddie Jamescbad2192021-10-07 09:39:39 -0500123 uint8_t stateIdPos, uint8_t stateSetValue);
Patrick Williams05e95592021-09-02 09:28:14 -0500124
125 /** @brief Send the PLDM message to reset the OCC
126 *
127 * @param[in] instanceId - OCC instance to reset
128 *
129 */
130 void resetOCC(open_power::occ::instanceID occInstanceId);
131
Eddie Jamescbad2192021-10-07 09:39:39 -0500132 /** @brief Send the PLDM message to perform the HRESET
133 *
134 * @param[in] instanceID - SBE instance to HRESET
135 */
136 void sendHRESET(open_power::occ::instanceID sbeInstanceId);
137
Chris Cainbae4d072022-02-28 09:46:50 -0600138 /** @brief Check if the OCC active sensor is available
139 * On successful read, the Manager callback will be called to update
140 * the status
141 *
142 * @param[in] instance - OCC instance to check
143 */
144 void checkActiveSensor(uint8_t instance);
145
Patrick Williams05e95592021-09-02 09:28:14 -0500146 private:
147 /** @brief Callback handler to be invoked when the state of the OCC
148 * changes
149 */
150 std::function<bool(open_power::occ::instanceID, bool)> callBack = nullptr;
151
Eddie Jamescbad2192021-10-07 09:39:39 -0500152 /** @brief Callback handler to be invoked when the maintenance state of the
153 * SBE changes
154 */
155 std::function<void(open_power::occ::instanceID, bool)> sbeCallBack =
156 nullptr;
157
Sheldon Bailey31a2f132022-05-20 11:31:52 -0500158 /** @brief Callback handler to be invoked when the OCC is in SAFE Mode =
159 * true or when OCCs are in_service = false.
160 */
161 std::function<void(bool)> safeModeCallBack = nullptr;
162
Chris Cainbae4d072022-02-28 09:46:50 -0600163 /** @brief reference to sd_event wrapped in unique_ptr */
164 EventPtr& event;
165
166 /** @brief event source wrapped in unique_ptr */
167 EventSourcePtr eventSource;
168
Patrick Williams05e95592021-09-02 09:28:14 -0500169 /** @brief Used to subscribe to D-Bus PLDM StateSensorEvent signal and
170 * processes if the event corresponds to OCC state change.
171 */
172 sdbusplus::bus::match_t pldmEventSignal;
173
174 /** @brief Used to subscribe for host state change signal */
175 sdbusplus::bus::match_t hostStateSignal;
176
177 /** @brief PLDM Sensor ID to OCC Instance mapping
178 */
Eddie Jamescbad2192021-10-07 09:39:39 -0500179 SensorToInstance sensorToOCCInstance;
Patrick Williams05e95592021-09-02 09:28:14 -0500180
Eddie Jamescbad2192021-10-07 09:39:39 -0500181 /** @brief PLDM Sensor ID to SBE Instance mapping
182 */
183 SensorToInstance sensorToSBEInstance;
184
185 /** @brief Sensor offset of OCC state set ID
Patrick Williams05e95592021-09-02 09:28:14 -0500186 * PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS in state sensor PDR.
187 */
George Liuf3a4a692021-12-28 13:59:51 +0800188 SensorOffset OCCSensorOffset = 0;
Eddie Jamescbad2192021-10-07 09:39:39 -0500189
190 /** @brief Sensor offset of the SBE state set ID
191 * PLDM_OEM_IBM_SBE_HRESET_STATE in state sensor PDR.
192 */
George Liuf3a4a692021-12-28 13:59:51 +0800193 SensorOffset SBESensorOffset = 0;
Patrick Williams05e95592021-09-02 09:28:14 -0500194
195 /** @brief OCC Instance mapping to PLDM Effecter ID
196 */
Eddie Jamescbad2192021-10-07 09:39:39 -0500197 InstanceToEffecter occInstanceToEffecter;
198
199 /** @brief SBE instance mapping to PLDM Effecter ID
200 */
201 InstanceToEffecter sbeInstanceToEffecter;
Patrick Williams05e95592021-09-02 09:28:14 -0500202
203 /** @brief compositeEffecterCount for OCC reset state effecter PDR */
Eddie Jamescbad2192021-10-07 09:39:39 -0500204 CompositeEffecterCount OCCEffecterCount = 0;
205
206 /** @brief compositeEffecterCount for SBE HRESET state effecter PDR */
207 CompositeEffecterCount SBEEffecterCount = 0;
Patrick Williams05e95592021-09-02 09:28:14 -0500208
209 /** @brief Position of Boot/Restart Cause stateSetID in OCC state
210 * effecter PDR
211 */
212 uint8_t bootRestartPosition = 0;
213
Eddie Jamescbad2192021-10-07 09:39:39 -0500214 /** @brief Position of the SBE maintenance stateSetID in the state
215 * effecter PDR
216 */
217 uint8_t sbeMaintenanceStatePosition = 0;
218
Chris Cainbae4d072022-02-28 09:46:50 -0600219 /** @brief OCC instance number for the PLDM message */
220 uint8_t pldmResponseOcc = 0;
221
222 /** @brief File descriptor for PLDM messages */
223 int pldmFd = -1;
224
225 /** @brief MCTP instance number used in PLDM requests
226 */
227 uint8_t mctpInstance{};
228
229 /** @brief The response for the PLDM request msg is received flag.
230 */
231 bool pldmResponseReceived = false;
232
233 /** @brief The response for the PLDM request has timed out.
234 */
235 bool pldmResponseTimeout = false;
236
237 /** @brief timer event */
238 sdeventplus::Event sdpEvent;
239
240 /** @brief Timer that is started when PLDM command is sent
241 */
242 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> pldmRspTimer;
243
Chris Cain12d0b822022-04-22 17:29:18 -0500244 std::set<uint8_t> outstandingHResets;
245
Chris Cainbae4d072022-02-28 09:46:50 -0600246 /** @brief Callback when PLDM response has not been received within the
247 * timeout period.
248 */
249 void pldmRspExpired();
250
251 /** @brief Close the MCTP file */
252 void pldmClose();
253
Patrick Williams05e95592021-09-02 09:28:14 -0500254 /** @brief When the OCC state changes host sends PlatformEventMessage
255 * StateSensorEvent, this function processes the D-Bus signal
256 * with the sensor event information and invokes the callback
257 * to change the OCC state.
258 *
259 * @param[in] msg - data associated with the subscribed signal
260 */
261 void sensorEvent(sdbusplus::message::message& msg);
262
263 /** @brief When the host state changes and if the CurrentHostState is
264 * xyz.openbmc_project.State.Host.HostState.Off then
265 * the cache of OCC sensors and effecters mapping is cleared.
266 *
267 * @param[in] msg - data associated with the subscribed signal
268 */
269 void hostStateEvent(sdbusplus::message::message& msg);
270
Chris Cainbae4d072022-02-28 09:46:50 -0600271 /** @brief Called when it is determined that the Host is not running.
272 * The cache of OCC sensors and effecters mapping is cleared.
273 */
274 void clearData();
275
Patrick Williams05e95592021-09-02 09:28:14 -0500276 /** @brief Check if the PDR cache for PLDM OCC sensors is valid
277 *
278 * @return true if cache is populated and false if the cache is not
279 * populated.
280 */
281 auto isOCCSensorCacheValid()
282 {
283 return (sensorToOCCInstance.empty() ? false : true);
284 }
285
286 /** @brief Check if the PDR cache for PLDM OCC effecters is valid
287 *
288 * @return true if cache is populated and false if the cache is not
289 * populated.
290 */
291 auto isPDREffecterCacheValid()
292 {
293 return (occInstanceToEffecter.empty() ? false : true);
294 }
Eddie Jamescbad2192021-10-07 09:39:39 -0500295
296 /** @brief Query PLDM for the MCTP requester instance id
297 *
298 * @param[out] - the instance id
299 *
300 * @return true if the id was found and false if not
301 */
302 bool getMctpInstanceId(uint8_t& instanceId);
303
Chris Cainbae4d072022-02-28 09:46:50 -0600304 /** @brief Encode a GetStateSensor command into a PLDM request
305 * @param[in] instance - OCC instance number
306 * @param[in] sensorId - OCC Active sensor ID number
307 *
308 * @return request - The encoded PLDM messsage to be sent
309 */
310 std::vector<uint8_t> encodeGetStateSensorRequest(uint8_t instance,
311 uint16_t sensorId);
Eddie Jamescbad2192021-10-07 09:39:39 -0500312 /** @brief Send the PLDM request
313 *
Chris Caind1b68262022-02-28 09:56:50 -0600314 * @param[in] request - the request data
Chris Cainbae4d072022-02-28 09:46:50 -0600315 * @param[in] rspExpected - false: no need to wait for the response
316 * true: will need to process response in callback
Eddie Jamescbad2192021-10-07 09:39:39 -0500317 */
Chris Cainbae4d072022-02-28 09:46:50 -0600318 void sendPldm(const std::vector<uint8_t>& request, const uint8_t instance,
319 const bool rspExpected = false);
320
321 /** @brief Register a callback function to handle the PLDM response */
322 void registerPldmRspCallback();
323
324 /** @brief callback for the PLDM response event
325 *
326 * @param[in] es - Populated event source
327 * @param[in] fd - Associated File descriptor
328 * @param[in] revents - Type of event
329 * @param[in] userData - User data that was passed during registration
330 *
331 * @return - 0 or positive number on success and negative
332 * errno otherwise
333 */
334 static int pldmRspCallback(sd_event_source* es, int fd, uint32_t revents,
335 void* userData);
Patrick Williams05e95592021-09-02 09:28:14 -0500336};
337
338} // namespace pldm