blob: 7a93066e24eb7fc5cb7fe3d35685647de7739207 [file] [log] [blame]
Tom Joseph815f9f52020-07-27 12:12:13 +05301#pragma once
2
3#include "occ_status.hpp"
George Liuf3b75142021-06-10 11:22:50 +08004#include "utils.hpp"
Tom Joseph815f9f52020-07-27 12:12:13 +05305
Tom Joseph00325232020-07-29 17:51:48 +05306#include <libpldm/pldm.h>
7
Tom Joseph815f9f52020-07-27 12:12:13 +05308#include <sdbusplus/bus/match.hpp>
9
10namespace pldm
11{
12
13namespace MatchRules = sdbusplus::bus::match::rules;
14
Tom Joseph00325232020-07-29 17:51:48 +053015using CompositeEffecterCount = uint8_t;
16using EffecterID = uint16_t;
Tom Joseph815f9f52020-07-27 12:12:13 +053017using EntityType = uint16_t;
18using EntityInstance = uint16_t;
19using EventState = uint8_t;
Tom Joseph00325232020-07-29 17:51:48 +053020using OccInstanceToEffecter = std::map<open_power::occ::instanceID, EffecterID>;
Tom Joseph815f9f52020-07-27 12:12:13 +053021using PdrList = std::vector<std::vector<uint8_t>>;
22using SensorID = uint16_t;
23using SensorOffset = uint8_t;
24using SensorToOCCInstance = std::map<SensorID, open_power::occ::instanceID>;
25using TerminusID = uint8_t;
26
Tom Joseph00325232020-07-29 17:51:48 +053027/** @brief Hardcoded TID */
28constexpr TerminusID tid = 0;
29
Tom Joseph815f9f52020-07-27 12:12:13 +053030/** @brief OCC instance starts with 0 for example "occ0" */
31constexpr open_power::occ::instanceID start = 0;
32
Tom Joseph00325232020-07-29 17:51:48 +053033/** @brief Hardcoded mctpEid for HBRT */
34constexpr mctp_eid_t mctpEid = 10;
Tom Joseph815f9f52020-07-27 12:12:13 +053035
36/** @class Interface
37 *
38 * @brief Abstracts the PLDM details related to the OCC
39 */
40class Interface
41{
42 public:
43 Interface() = delete;
44 ~Interface() = default;
45 Interface(const Interface&) = delete;
46 Interface& operator=(const Interface&) = delete;
47 Interface(Interface&&) = delete;
48 Interface& operator=(Interface&&) = delete;
49
50 /** @brief Constructs the PLDM Interface object for OCC functions
51 *
Tom Joseph815f9f52020-07-27 12:12:13 +053052 * @param[in] callBack - callBack handler to invoke when the OCC state
53 * changes.
54 */
55 explicit Interface(
Tom Joseph815f9f52020-07-27 12:12:13 +053056 std::function<bool(open_power::occ::instanceID, bool)> callBack) :
Tom Joseph815f9f52020-07-27 12:12:13 +053057 callBack(callBack),
58 pldmEventSignal(
George Liuf3b75142021-06-10 11:22:50 +080059 utils::getBus(),
Tom Joseph815f9f52020-07-27 12:12:13 +053060 MatchRules::type::signal() +
61 MatchRules::member("StateSensorEvent") +
62 MatchRules::path("/xyz/openbmc_project/pldm") +
63 MatchRules::interface("xyz.openbmc_project.PLDM.Event"),
64 std::bind(std::mem_fn(&Interface::sensorEvent), this,
65 std::placeholders::_1)),
66 hostStateSignal(
George Liuf3b75142021-06-10 11:22:50 +080067 utils::getBus(),
Tom Joseph815f9f52020-07-27 12:12:13 +053068 MatchRules::propertiesChanged("/xyz/openbmc_project/state/host0",
69 "xyz.openbmc_project.State.Host"),
70 std::bind(std::mem_fn(&Interface::hostStateEvent), this,
71 std::placeholders::_1))
72 {
73 }
74
75 /** @brief Fetch the OCC state sensor PDRs and populate the cache with
76 * sensorId to OCC instance mapping information and the sensor
77 * offset for Operational Running Status.
78 *
79 * @param[in] pdrs - OCC state sensor PDRs
80 * @param[out] sensorInstanceMap - map of sensorID to OCC instance
81 * @param[out] sensorOffset - sensor offset of interested state set ID
82 */
83 void fetchOCCSensorInfo(const PdrList& pdrs,
84 SensorToOCCInstance& sensorInstanceMap,
85 SensorOffset& sensorOffset);
86
Tom Joseph00325232020-07-29 17:51:48 +053087 /** @brief Fetch the OCC state effecter PDRs and populate the cache with
88 * OCC instance to EffecterID information.
89 *
90 * @param[in] pdrs - OCC state effecter PDRs
91 * @param[out] instanceToEffecterMap - map of OCC instance to effecterID
92 * @param[out] count - sensor offset of interested state set ID
93 * @param[out] bootRestartPos - position of Boot/Restart Cause stateSetID
94 */
95 void fetchOCCEffecterInfo(const PdrList& pdrs,
96 OccInstanceToEffecter& instanceToEffecterMap,
97 CompositeEffecterCount& count,
98 uint8_t& bootRestartPos);
99
100 /** @brief Prepare the request for SetStateEffecterStates command
101 *
102 * @param[in] instanceId - PLDM instanceID
103 * @param[in] instanceToEffecterMap - map of OCC instance to effecterID
104 * @param[in] count - compositeEffecterCount for OCC reset effecter PDR
105 * @param[in] bootRestartPos - position of Boot/Restart Cause stateSetID
106 *
107 * @return PLDM request message to be sent to host for OCC reset, empty
108 * response in the case of failure.
109 */
110 std::vector<uint8_t>
111 prepareSetEffecterReq(uint8_t instanceId, EffecterID effecterId,
112 CompositeEffecterCount effecterCount,
113 uint8_t bootRestartPos);
114
115 /** @brief Send the PLDM message to reset the OCC
116 *
117 * @param[in] instanceId - OCC instance to reset
118 *
119 */
120 void resetOCC(open_power::occ::instanceID occInstanceId);
121
Tom Joseph815f9f52020-07-27 12:12:13 +0530122 private:
Tom Joseph815f9f52020-07-27 12:12:13 +0530123 /** @brief Callback handler to be invoked when the state of the OCC
124 * changes
125 */
126 std::function<bool(open_power::occ::instanceID, bool)> callBack = nullptr;
127
128 /** @brief Used to subscribe to D-Bus PLDM StateSensorEvent signal and
129 * processes if the event corresponds to OCC state change.
130 */
131 sdbusplus::bus::match_t pldmEventSignal;
132
133 /** @brief Used to subscribe for host state change signal */
134 sdbusplus::bus::match_t hostStateSignal;
135
136 /** @brief PLDM Sensor ID to OCC Instance mapping
137 */
138 SensorToOCCInstance sensorToOCCInstance;
139
140 /** @brief Sensor offset of state set ID
141 * PLDM_STATE_SET_OPERATIONAL_RUNNING_STATUS in state sensor PDR.
142 */
143 SensorOffset sensorOffset;
144
Tom Joseph00325232020-07-29 17:51:48 +0530145 /** @brief OCC Instance mapping to PLDM Effecter ID
146 */
147 OccInstanceToEffecter occInstanceToEffecter;
148
149 /** @brief compositeEffecterCount for OCC reset state effecter PDR */
150 CompositeEffecterCount effecterCount = 0;
151
152 /** @brief Position of Boot/Restart Cause stateSetID in OCC state
153 * effecter PDR
154 */
155 uint8_t bootRestartPosition = 0;
156
Tom Joseph815f9f52020-07-27 12:12:13 +0530157 /** @brief When the OCC state changes host sends PlatformEventMessage
158 * StateSensorEvent, this function processes the D-Bus signal
159 * with the sensor event information and invokes the callback
160 * to change the OCC state.
161 *
162 * @param[in] msg - data associated with the subscribed signal
163 */
164 void sensorEvent(sdbusplus::message::message& msg);
165
166 /** @brief When the host state changes and if the CurrentHostState is
167 * xyz.openbmc_project.State.Host.HostState.Off then
168 * the cache of OCC sensors and effecters mapping is cleared.
169 *
170 * @param[in] msg - data associated with the subscribed signal
171 */
172 void hostStateEvent(sdbusplus::message::message& msg);
173
174 /** @brief Check if the PDR cache for PLDM OCC sensors is valid
175 *
176 * @return true if cache is populated and false if the cache is not
177 * populated.
178 */
179 auto isOCCSensorCacheValid()
180 {
181 return (sensorToOCCInstance.empty() ? false : true);
182 }
Tom Joseph00325232020-07-29 17:51:48 +0530183
184 /** @brief Check if the PDR cache for PLDM OCC effecters is valid
185 *
186 * @return true if cache is populated and false if the cache is not
187 * populated.
188 */
189 auto isPDREffecterCacheValid()
190 {
191 return (occInstanceToEffecter.empty() ? false : true);
192 }
Tom Joseph815f9f52020-07-27 12:12:13 +0530193};
194
195} // namespace pldm