blob: f8611f7d21000006aa8d7fce0d8277739040fe00 [file] [log] [blame]
Gilbert Chen6c7fed42022-02-22 15:40:17 +00001#pragma once
2
3#include "config.h"
4
5#include "libpldm/platform.h"
6#include "libpldm/pldm.h"
7
8#include "requester/handler.hpp"
9#include "requester/mctp_endpoint_discovery.hpp"
10#include "terminus.hpp"
11
12#include <limits>
13#include <map>
14#include <memory>
15#include <optional>
16#include <queue>
17#include <utility>
18#include <vector>
19
20namespace pldm
21{
22
23enum class SupportedTransportLayer
24{
25 MCTP
26};
27
28namespace platform_mc
29{
30
31/** @brief Size of TID Pool in pldmd */
32constexpr size_t tidPoolSize = std::numeric_limits<pldm_tid_t>::max() + 1;
33/** @brief Type definition for Requester request handler */
34using RequesterHandler = requester::Handler<requester::Request>;
35/** @brief Type definition for Terminus handler mapper */
36using TerminiMapper = std::map<pldm_tid_t, std::shared_ptr<Terminus>>;
37
38class Manager;
39/**
40 * @brief TerminusManager
41 *
42 * TerminusManager class to discover and initialize PLDM terminus.
43 */
44class TerminusManager
45{
46 public:
47 TerminusManager() = delete;
48 TerminusManager(const TerminusManager&) = delete;
49 TerminusManager(TerminusManager&&) = delete;
50 TerminusManager& operator=(const TerminusManager&) = delete;
51 TerminusManager& operator=(TerminusManager&&) = delete;
52 virtual ~TerminusManager() = default;
53
Thu Nguyen51d66b52024-08-06 09:15:55 +000054 explicit TerminusManager(
55 sdeventplus::Event& /* event */, RequesterHandler& handler,
56 pldm::InstanceIdDb& instanceIdDb, TerminiMapper& termini,
57 Manager* manager, mctp_eid_t localEid) :
Patrick Williams16c2a0a2024-08-16 15:20:59 -040058 handler(handler), instanceIdDb(instanceIdDb), termini(termini),
Thu Nguyen51d66b52024-08-06 09:15:55 +000059 tidPool(tidPoolSize, false), manager(manager), localEid(localEid)
Gilbert Chen6c7fed42022-02-22 15:40:17 +000060 {
61 // DSP0240 v1.1.0 table-8, special value: 0,0xFF = reserved
62 tidPool[0] = true;
63 tidPool[PLDM_TID_RESERVED] = true;
64 }
65
66 /** @brief start a coroutine to discover terminus
67 *
68 * @param[in] mctpInfos - list information of the MCTP endpoints
69 */
70 void discoverMctpTerminus(const MctpInfos& mctpInfos);
71
72 /** @brief remove MCTP endpoints
73 *
74 * @param[in] mctpInfos - list information of the MCTP endpoints
75 */
76 void removeMctpTerminus(const MctpInfos& mctpInfos);
77
78 /** @brief Send request PLDM message to tid. The function will return when
79 * received the response message from terminus. The function will
80 * auto get the instanceID from libmctp and update to request
81 * message.
82 *
83 * @param[in] tid - Destination TID
84 * @param[in] request - request PLDM message
85 * @param[out] responseMsg - response PLDM message
86 * @param[out] responseLen - length of response PLDM message
87 * @return coroutine return_value - PLDM completion code
88 */
89 exec::task<int> sendRecvPldmMsg(pldm_tid_t tid, Request& request,
90 const pldm_msg** responseMsg,
91 size_t* responseLen);
92
93 /** @brief Send request PLDM message to eid. The function will
94 * return when received the response message from terminus.
95 *
96 * @param[in] eid - Destination EID
97 * @param[in] request - request PLDM message
98 * @param[out] responseMsg - response PLDM message
99 * @param[out] responseLen - length of response PLDM message
100 * @return coroutine return_value - PLDM completion code
101 */
Patrick Williams16c2a0a2024-08-16 15:20:59 -0400102 virtual exec::task<int> sendRecvPldmMsgOverMctp(
103 mctp_eid_t eid, Request& request, const pldm_msg** responseMsg,
104 size_t* responseLen);
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000105
106 /** @brief member functions to map/unmap tid
107 */
108 std::optional<MctpInfo> toMctpInfo(const pldm_tid_t& tid);
109
110 /** @brief Member functions to response the TID of specific MCTP interface
111 *
112 * @param[in] mctpInfos - list information of the MCTP endpoints
113 *
114 * @return tid - Terminus tid
115 */
116 std::optional<pldm_tid_t> toTid(const MctpInfo& mctpInfo) const;
117
118 /** @brief Member functions to find the TID for MCTP interface. Response the
119 * Terminus TID when mctpInfo is already in the data base. Response
120 * new tid from pool when mctpInfo is new.
121 *
122 * @param[in] mctpInfos - list information of the MCTP endpoints
123 *
124 * @return tid - Terminus tid
125 */
126 std::optional<pldm_tid_t> mapTid(const MctpInfo& mctpInfo);
127
128 /** @brief Member functions to store the mctp info and tid to terminus info
129 * list.
130 *
131 * @param[in] mctpInfos - list information of the MCTP endpoints
132 * @param[in] tid - Destination TID
133 *
134 * @return tid - Terminus tid
135 */
Patrick Williams366507c2025-02-03 14:28:01 -0500136 std::optional<pldm_tid_t> storeTerminusInfo(const MctpInfo& mctpInfo,
137 pldm_tid_t tid);
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000138
139 /** @brief Member functions to remove the TID from the transportLayer and
140 * mctpInfo table
141 *
142 * @param[in] tid - Destination TID
143 *
144 * @return true/false - True when tid in the table otherwise return false
145 */
146 bool unmapTid(const pldm_tid_t& tid);
147
Thu Nguyen51d66b52024-08-06 09:15:55 +0000148 /** @brief getter of local EID
149 *
150 * @return uint8_t - local EID
151 */
152 mctp_eid_t getLocalEid()
153 {
154 return localEid;
155 }
156
Chau Ly75e00422024-03-19 12:33:08 +0000157 /** @brief Helper function to invoke registered handlers for
158 * updating the availability status of the MCTP endpoint
159 *
160 * @param[in] mctpInfo - information of the target endpoint
161 * @param[in] availability - new availability status
162 */
163 void updateMctpEndpointAvailability(const MctpInfo& mctpInfo,
164 Availability availability);
165
Chau Ly8fa40db2024-04-02 09:32:01 +0000166 /** @brief Construct MCTP Endpoint object path base on the MCTP endpoint
167 * info
168 *
169 * @param[in] mctpInfo - information of the target endpoint
170 */
171 std::string constructEndpointObjPath(const MctpInfo& mctpInfo);
172
Thu Nguyen38e12aa2025-01-21 22:47:56 +0000173 /** @brief Member functions to get the MCTP eid of active MCTP medium
174 * interface of one terminus by terminus name
175 *
176 * @param[in] terminusName - terminus name
177 *
178 * @return option mctp_eid_t - the mctp eid or std::nullopt
179 */
180 std::optional<mctp_eid_t> getActiveEidByName(
181 const std::string& terminusName);
182
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000183 private:
184 /** @brief Find the terminus object pointer in termini list.
185 *
186 * @param[in] mctpInfos - list information of the MCTP endpoints
187 */
Manojkiran Edaef5c4eb2024-07-25 22:36:41 +0530188 TerminiMapper::iterator findTerminusPtr(const MctpInfo& mctpInfo);
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000189
190 /** @brief The coroutine task execute by discoverMctpTerminus()
191 *
192 * @return coroutine return_value - PLDM completion code
193 */
194 exec::task<int> discoverMctpTerminusTask();
195
196 /** @brief Initialize terminus and then instantiate terminus object to keeps
197 * the data fetched from terminus
198 *
199 * @param[in] mctpInfo - information of the MCTP endpoints
200 * @return coroutine return_value - PLDM completion code
201 */
202 exec::task<int> initMctpTerminus(const MctpInfo& mctpInfo);
203
204 /** @brief Send getTID PLDM command to destination EID and then return the
205 * value of tid in reference parameter.
206 *
207 * @param[in] eid - Destination EID
208 * @param[out] tid - Terminus TID
209 * @return coroutine return_value - PLDM completion code
210 */
211 exec::task<int> getTidOverMctp(mctp_eid_t eid, pldm_tid_t* tid);
212
213 /** @brief Send setTID command to destination EID.
214 *
215 * @param[in] eid - Destination EID
216 * @param[in] tid - Destination TID
217 * @return coroutine return_value - PLDM completion code
218 */
219 exec::task<int> setTidOverMctp(mctp_eid_t eid, pldm_tid_t tid);
220
221 /** @brief Send getPLDMTypes command to destination TID and then return the
222 * value of supportedTypes in reference parameter.
223 *
224 * @param[in] tid - Destination TID
225 * @param[out] supportedTypes - Supported Types returned from terminus
226 * @return coroutine return_value - PLDM completion code
227 */
228 exec::task<int> getPLDMTypes(pldm_tid_t tid, uint64_t& supportedTypes);
229
Thu Nguyen6e615622024-06-22 02:10:34 +0000230 /** @brief Send getPLDMVersion command to destination TID and then return
231 * the version of the PLDM supported type.
232 *
233 * @param[in] tid - Destination TID
234 * @param[in] type - PLDM Type
235 * @param[out] version - PLDM Type version
236 * @return coroutine return_value - PLDM completion code
237 */
238 exec::task<int> getPLDMVersion(pldm_tid_t tid, uint8_t type,
239 ver32_t* version);
240
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000241 /** @brief Send getPLDMCommands command to destination TID and then return
242 * the value of supportedCommands in reference parameter.
243 *
244 * @param[in] tid - Destination TID
245 * @param[in] type - PLDM Type
Thu Nguyen6e615622024-06-22 02:10:34 +0000246 * @param[in] version - PLDM Type version
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000247 * @param[in] supportedCmds - Supported commands returned from terminus
248 * for specific type
249 * @return coroutine return_value - PLDM completion code
250 */
251 exec::task<int> getPLDMCommands(pldm_tid_t tid, uint8_t type,
Thu Nguyen6e615622024-06-22 02:10:34 +0000252 ver32_t version,
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000253 bitfield8_t* supportedCmds);
254
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000255 /** @brief Reference to a Handler object that manages the request/response
256 * logic.
257 */
258 RequesterHandler& handler;
259
260 /** @brief Reference to the instanceID data base from libpldm */
261 pldm::InstanceIdDb& instanceIdDb;
262
263 /** @brief Managed termini list */
264 TerminiMapper& termini;
265
266 /** @brief tables for maintaining assigned TID */
267 std::vector<bool> tidPool;
268
269 /** @brief Store the supported transport layers of specific TID */
270 std::map<pldm_tid_t, SupportedTransportLayer> transportLayerTable;
271
272 /** @brief Store the supported MCTP interface info of specific TID */
273 std::map<pldm_tid_t, MctpInfo> mctpInfoTable;
274
275 /** @brief A queue of MctpInfos to be discovered **/
276 std::queue<MctpInfos> queuedMctpInfos{};
277
278 /** @brief coroutine handle of discoverTerminusTask */
279 std::optional<std::pair<exec::async_scope, std::optional<int>>>
280 discoverMctpTerminusTaskHandle{};
281
282 /** @brief A Manager interface for calling the hook functions **/
283 Manager* manager;
Thu Nguyen51d66b52024-08-06 09:15:55 +0000284
285 /** @brief local EID */
286 mctp_eid_t localEid;
Chau Ly75e00422024-03-19 12:33:08 +0000287
288 /** @brief MCTP Endpoint available status mapping */
289 std::map<MctpInfo, Availability> mctpInfoAvailTable;
Gilbert Chen6c7fed42022-02-22 15:40:17 +0000290};
291} // namespace platform_mc
292} // namespace pldm