Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include <dlfcn.h>
12 : #include <pthread.h>
13 : #include "ra.h"
14 : #include "ra_rs_err.h"
15 : #include "ra_rdma_lite.h"
16 :
17 : static pthread_mutex_t gRdmaLiteApiLock = PTHREAD_MUTEX_INITIALIZER;
18 : void *gRdmaLiteApiHandle = NULL;
19 : int gRdmaLiteApiRefcnt = 0;
20 : #ifndef HNS_ROCE_LLT
21 : struct RaRdmaLiteOps gRdmaLiteOps;
22 : #else
23 : struct RaRdmaLiteOps gRdmaLiteOps = {
24 : .raRdmaLiteAllocCtx = rdma_lite_alloc_context,
25 : .raRdmaLiteFreeCtx = rdma_lite_free_context,
26 : .raRdmaLiteInitMemPool = rdma_lite_init_mem_pool,
27 : .raRdmaLiteDeinitMemPool = rdma_lite_deinit_mem_pool,
28 : .raRdmaLiteCreateCq = rdma_lite_create_cq,
29 : .raRdmaLiteDestroyCq = rdma_lite_destroy_cq,
30 : .raRdmaLitePollCq = rdma_lite_poll_cq,
31 : .raRdmaLitePollCqV2 = rdma_lite_poll_cq_v2,
32 : .raRdmaLiteCreateQp = rdma_lite_create_qp,
33 : .raRdmaLiteDestroyQp = rdma_lite_destroy_qp,
34 : .raRdmaLitePostSend = rdma_lite_post_send,
35 : .raRdmaLitePostRecv = rdma_lite_post_recv,
36 : .raRdmaLiteSetQpSl = rdma_lite_set_qp_sl,
37 : .raRdmaLiteCleanQp = rdma_lite_clean_qp,
38 : .raRdmaLiteRestoreSnapshot = rdma_lite_restore_snapshot,
39 : };
40 : #endif
41 :
42 0 : STATIC int RaHdcOpenRdmaLiteSo(void)
43 : {
44 : #ifndef HNS_ROCE_LLT
45 : if (gRdmaLiteApiHandle == NULL) {
46 : gRdmaLiteApiHandle = dlopen("libascend_rdma_lite.so", RTLD_NOW);
47 : if (gRdmaLiteApiHandle != NULL) {
48 : return 0;
49 : }
50 : return -EINVAL;
51 : } else {
52 : hccp_run_info("rdma lite api dlopen again!");
53 : }
54 : #endif
55 0 : return 0;
56 : }
57 :
58 : #ifndef HNS_ROCE_LLT
59 : static int RaRdmaLiteControlPlaneApiInit(void)
60 : {
61 : gRdmaLiteOps.raRdmaLiteAllocCtx = (struct rdma_lite_context* (*)(u8 phyId, struct dev_cap_info *cap))
62 : dlsym(gRdmaLiteApiHandle, "rdma_lite_alloc_context");
63 : DL_API_RET_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteAllocCtx, "rdma_lite_alloc_context", gRdmaLiteApiLock);
64 :
65 : gRdmaLiteOps.raRdmaLiteFreeCtx = (void (*)(struct rdma_lite_context *liteCtx))
66 : dlsym(gRdmaLiteApiHandle, "rdma_lite_free_context");
67 : DL_API_RET_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteFreeCtx, "rdma_lite_free_context", gRdmaLiteApiLock);
68 :
69 : gRdmaLiteOps.raRdmaLiteInitMemPool = (int (*)(struct rdma_lite_context *liteCtx,
70 : struct rdma_lite_mem_attr * liteMemAttr)) dlsym(gRdmaLiteApiHandle, "rdma_lite_init_mem_pool");
71 : DL_API_PTR_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteInitMemPool, "rdma_lite_init_mem_pool");
72 :
73 : gRdmaLiteOps.raRdmaLiteDeinitMemPool = (int (*)(struct rdma_lite_context *liteCtx, u32 memIdx))
74 : dlsym(gRdmaLiteApiHandle, "rdma_lite_deinit_mem_pool");
75 : DL_API_PTR_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteDeinitMemPool, "rdma_lite_deinit_mem_pool");
76 :
77 : gRdmaLiteOps.raRdmaLiteCreateCq = (struct rdma_lite_cq* (*)(struct rdma_lite_context * liteCtx,
78 : struct rdma_lite_cq_attr * liteCqAttr)) dlsym(gRdmaLiteApiHandle, "rdma_lite_create_cq");
79 : DL_API_RET_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteCreateCq, "rdma_lite_create_cq", gRdmaLiteApiLock);
80 :
81 : gRdmaLiteOps.raRdmaLiteDestroyCq = (int (*)(struct rdma_lite_cq * liteCq))
82 : dlsym(gRdmaLiteApiHandle, "rdma_lite_destroy_cq");
83 : DL_API_RET_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteDestroyCq, "rdma_lite_destroy_cq", gRdmaLiteApiLock);
84 :
85 : gRdmaLiteOps.raRdmaLiteCreateQp = (struct rdma_lite_qp* (*)(struct rdma_lite_context * liteCtx,
86 : struct rdma_lite_qp_attr * liteQpAttr)) dlsym(gRdmaLiteApiHandle, "rdma_lite_create_qp");
87 : DL_API_RET_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteCreateQp, "rdma_lite_create_qp", gRdmaLiteApiLock);
88 :
89 : gRdmaLiteOps.raRdmaLiteDestroyQp = (int (*)(struct rdma_lite_qp * liteQp))
90 : dlsym(gRdmaLiteApiHandle, "rdma_lite_destroy_qp");
91 : DL_API_RET_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteDestroyQp, "rdma_lite_destroy_qp", gRdmaLiteApiLock);
92 :
93 : gRdmaLiteOps.raRdmaLiteSetQpSl = (int (*)(struct rdma_lite_qp * liteQp, int sl))
94 : dlsym(gRdmaLiteApiHandle, "rdma_lite_set_qp_sl");
95 : DL_API_RET_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteSetQpSl, "rdma_lite_set_qp_sl", gRdmaLiteApiLock);
96 :
97 : gRdmaLiteOps.raRdmaLiteCleanQp = (int (*)(struct rdma_lite_qp *liteQp))
98 : dlsym(gRdmaLiteApiHandle, "rdma_lite_clean_qp");
99 : DL_API_PTR_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteCleanQp, "rdma_lite_clean_qp");
100 :
101 : gRdmaLiteOps.raRdmaLiteRestoreSnapshot = (int (*)(struct rdma_lite_context *liteCtx))
102 : dlsym(gRdmaLiteApiHandle, "rdma_lite_restore_snapshot");
103 : DL_API_PTR_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteRestoreSnapshot, "rdma_lite_restore_snapshot");
104 :
105 : gRdmaLiteOps.raRdmaLiteGetApiVersion = (unsigned int (*)(void))
106 : dlsym(gRdmaLiteApiHandle, "rdma_lite_get_api_version");
107 : DL_API_PTR_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteGetApiVersion, "rdma_lite_get_api_version");
108 :
109 : return 0;
110 : }
111 :
112 : static int RaRdmaLiteDataPlaneApiInit(void)
113 : {
114 : gRdmaLiteOps.raRdmaLitePostSend = (int (*)(struct rdma_lite_qp * liteQp, struct rdma_lite_send_wr * wr,
115 : struct rdma_lite_send_wr * *badWr, struct rdma_lite_post_send_attr * attr,
116 : struct rdma_lite_post_send_resp * resp)) dlsym(gRdmaLiteApiHandle, "rdma_lite_post_send");
117 : DL_API_RET_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLitePostSend, "rdma_lite_post_send", gRdmaLiteApiLock);
118 :
119 : gRdmaLiteOps.raRdmaLitePostRecv = (int (*)(struct rdma_lite_qp * liteQp, struct rdma_lite_recv_wr * wr,
120 : struct rdma_lite_recv_wr * *badWr)) dlsym(gRdmaLiteApiHandle, "rdma_lite_post_recv");
121 : DL_API_PTR_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLitePostRecv, "rdma_lite_post_recv");
122 :
123 : gRdmaLiteOps.raRdmaLitePollCq = (int (*)(struct rdma_lite_cq * liteCq, int numEntries,
124 : struct rdma_lite_wc *liteWc)) dlsym(gRdmaLiteApiHandle, "rdma_lite_poll_cq");
125 : DL_API_RET_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLitePollCq, "rdma_lite_poll_cq", gRdmaLiteApiLock);
126 :
127 : gRdmaLiteOps.raRdmaLitePollCqV2 = (int (*)(struct rdma_lite_cq * liteCq, int numEntries,
128 : struct rdma_lite_wc_v2 *liteWc)) dlsym(gRdmaLiteApiHandle, "rdma_lite_poll_cq_v2");
129 : DL_API_PTR_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLitePollCqV2, "rdma_lite_poll_cq_v2");
130 : return 0;
131 : }
132 : #endif
133 :
134 7 : DL_ATTRI_VISI_DEF int RaHdcRdmaLiteApiInit(void)
135 : {
136 : #ifndef HNS_ROCE_LLT
137 : int ret;
138 :
139 : pthread_mutex_lock(&gRdmaLiteApiLock);
140 : if (gRdmaLiteApiHandle != NULL) {
141 : gRdmaLiteApiRefcnt++;
142 : pthread_mutex_unlock(&gRdmaLiteApiLock);
143 : return 0;
144 : }
145 :
146 : ret = RaHdcOpenRdmaLiteSo();
147 : if (ret) {
148 : pthread_mutex_unlock(&gRdmaLiteApiLock);
149 : hccp_err("HccpDlopen[libascend_rdma_lite.so]"\
150 : "failed! ret=[%d][%s]. Please check rdma lite driver has been installed.", ret, dlerror());
151 : return ret;
152 : }
153 :
154 : ret = RaRdmaLiteControlPlaneApiInit();
155 : if (ret != 0) {
156 : return ret;
157 : }
158 :
159 : ret = RaRdmaLiteDataPlaneApiInit();
160 : if (ret != 0) {
161 : return ret;
162 : }
163 :
164 : gRdmaLiteApiRefcnt++;
165 : pthread_mutex_unlock(&gRdmaLiteApiLock);
166 : #endif
167 7 : return 0;
168 : }
169 :
170 7 : DL_ATTRI_VISI_DEF void RaHdcRdmaLiteApiDeinit(void)
171 : {
172 7 : pthread_mutex_lock(&gRdmaLiteApiLock);
173 7 : if (gRdmaLiteApiHandle != NULL) {
174 0 : gRdmaLiteApiRefcnt--;
175 0 : if (gRdmaLiteApiRefcnt > 0) {
176 0 : pthread_mutex_unlock(&gRdmaLiteApiLock);
177 0 : return;
178 : }
179 0 : (void)dlclose(gRdmaLiteApiHandle);
180 0 : gRdmaLiteApiHandle = NULL;
181 : }
182 7 : pthread_mutex_unlock(&gRdmaLiteApiLock);
183 :
184 7 : return;
185 : }
186 :
187 6 : struct rdma_lite_context *RaRdmaLiteAllocCtx(u8 phyId, struct dev_cap_info *cap)
188 : {
189 6 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteAllocCtx == NULL) {
190 : #ifndef HNS_ROCE_LLT
191 : hccp_err("ra_rdma_lite_alloc_ctx is null");
192 : return NULL;
193 : #endif
194 : }
195 6 : return gRdmaLiteOps.raRdmaLiteAllocCtx(phyId, cap);
196 : }
197 :
198 6 : void RaRdmaLiteFreeCtx(struct rdma_lite_context *liteCtx)
199 : {
200 6 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteFreeCtx == NULL) {
201 : #ifndef HNS_ROCE_LLT
202 : hccp_err("ra_rdma_lite_free_ctx is null");
203 : return;
204 : #endif
205 : }
206 6 : return gRdmaLiteOps.raRdmaLiteFreeCtx(liteCtx);
207 : }
208 :
209 6 : struct rdma_lite_cq *RaRdmaLiteCreateCq(struct rdma_lite_context *liteCtx, struct rdma_lite_cq_attr *liteCqAttr)
210 : {
211 6 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteCreateCq == NULL) {
212 : #ifndef HNS_ROCE_LLT
213 : hccp_err("ra_rdma_lite_create_cq is null");
214 : return NULL;
215 : #endif
216 : }
217 6 : return gRdmaLiteOps.raRdmaLiteCreateCq(liteCtx, liteCqAttr);
218 : }
219 :
220 6 : int RaRdmaLiteDestroyCq(struct rdma_lite_cq *liteCq)
221 : {
222 6 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteDestroyCq == NULL) {
223 : #ifndef HNS_ROCE_LLT
224 : hccp_err("ra_rdma_lite_destroy_cq is null");
225 : return -EINVAL;
226 : #endif
227 : }
228 6 : return gRdmaLiteOps.raRdmaLiteDestroyCq(liteCq);
229 : }
230 :
231 0 : int RaRdmaLitePollCq(struct rdma_lite_cq *liteCq, int numEntries, struct rdma_lite_wc *liteWc)
232 : {
233 0 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLitePollCq == NULL) {
234 : #ifndef HNS_ROCE_LLT
235 : hccp_err("ra_rdma_lite_poll_cq is null");
236 : return -EINVAL;
237 : #endif
238 : }
239 0 : return gRdmaLiteOps.raRdmaLitePollCq(liteCq, numEntries, liteWc);
240 : }
241 :
242 1 : int RaRdmaLitePollCqV2(struct rdma_lite_cq *liteCq, int numEntries, struct rdma_lite_wc_v2 *liteWc)
243 : {
244 1 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLitePollCqV2 == NULL) {
245 : #ifndef HNS_ROCE_LLT
246 : hccp_err("ra_rdma_lite_poll_cq_v2 is null");
247 : return -EINVAL;
248 : #endif
249 : }
250 1 : return gRdmaLiteOps.raRdmaLitePollCqV2(liteCq, numEntries, liteWc);
251 : }
252 :
253 3 : struct rdma_lite_qp *RaRdmaLiteCreateQp(struct rdma_lite_context *liteCtx, struct rdma_lite_qp_attr *liteQpAttr)
254 : {
255 3 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteCreateQp == NULL) {
256 : #ifndef HNS_ROCE_LLT
257 : hccp_err("ra_rdma_lite_create_qp is null");
258 : return NULL;
259 : #endif
260 : }
261 3 : return gRdmaLiteOps.raRdmaLiteCreateQp(liteCtx, liteQpAttr);
262 : }
263 :
264 3 : int RaRdmaLiteDestroyQp(struct rdma_lite_qp *liteQp)
265 : {
266 3 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteDestroyQp == NULL) {
267 : #ifndef HNS_ROCE_LLT
268 : hccp_err("ra_rdma_lite_destroy_qp is null");
269 : return -EINVAL;
270 : #endif
271 : }
272 3 : return gRdmaLiteOps.raRdmaLiteDestroyQp(liteQp);
273 : }
274 :
275 2 : int RaRdmaLitePostSend(struct rdma_lite_qp *liteQp, struct rdma_lite_send_wr *wr,
276 : struct rdma_lite_send_wr **badWr, struct rdma_lite_post_send_attr *attr, struct rdma_lite_post_send_resp *resp)
277 : {
278 2 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLitePostSend == NULL) {
279 : #ifndef HNS_ROCE_LLT
280 : hccp_err("ra_rdma_lite_post_send is null");
281 : return -EINVAL;
282 : #endif
283 : }
284 2 : return gRdmaLiteOps.raRdmaLitePostSend(liteQp, wr, badWr, attr, resp);
285 : }
286 :
287 1 : int RaRdmaLitePostRecv(struct rdma_lite_qp *liteQp, struct rdma_lite_recv_wr *wr,
288 : struct rdma_lite_recv_wr **badWr)
289 : {
290 1 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLitePostRecv == NULL) {
291 : #ifndef HNS_ROCE_LLT
292 : hccp_err("ra_rdma_lite_post_recv is null");
293 : return -EINVAL;
294 : #endif
295 : }
296 1 : return gRdmaLiteOps.raRdmaLitePostRecv(liteQp, wr, badWr);
297 : }
298 :
299 1 : int RaRdmaLiteSetQpSl(struct rdma_lite_qp *liteQp, unsigned char sl)
300 : {
301 1 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteSetQpSl == NULL) {
302 : #ifndef HNS_ROCE_LLT
303 : hccp_err("ra_rdma_lite_set_qp_sl is null");
304 : return -EINVAL;
305 : #endif
306 : }
307 1 : return gRdmaLiteOps.raRdmaLiteSetQpSl(liteQp, sl);
308 : }
309 :
310 0 : int RaRdmaLiteCleanQp(struct rdma_lite_qp *liteQp)
311 : {
312 0 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteCleanQp == NULL) {
313 : #ifndef HNS_ROCE_LLT
314 : hccp_err("ra_rdma_lite_clean_qp is null");
315 : return -EINVAL;
316 : #endif
317 : }
318 0 : return gRdmaLiteOps.raRdmaLiteCleanQp(liteQp);
319 : }
320 :
321 0 : int RaRdmaLiteInitMemPool(struct rdma_lite_context *liteCtx, struct rdma_lite_mem_attr *liteMemAttr)
322 : {
323 0 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteInitMemPool == NULL) {
324 : #ifndef HNS_ROCE_LLT
325 : hccp_err("ra_rdma_lite_init_mem_pool is null");
326 : return -EINVAL;
327 : #endif
328 : }
329 0 : return gRdmaLiteOps.raRdmaLiteInitMemPool(liteCtx, liteMemAttr);
330 : }
331 :
332 0 : int RaRdmaLiteDeinitMemPool(struct rdma_lite_context *liteCtx, u32 memIdx)
333 : {
334 0 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteDeinitMemPool == NULL) {
335 : #ifndef HNS_ROCE_LLT
336 : hccp_err("ra_rdma_lite_deinit_mem_pool is null");
337 : return -EINVAL;
338 : #endif
339 : }
340 0 : return gRdmaLiteOps.raRdmaLiteDeinitMemPool(liteCtx, memIdx);
341 : }
342 :
343 0 : int RaRdmaLiteRestoreSnapshot(struct rdma_lite_context *liteCtx)
344 : {
345 0 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteRestoreSnapshot == NULL) {
346 : #ifndef HNS_ROCE_LLT
347 : hccp_err("driver package may not support ra_rdma_lite_restore_snapshot interface, please change new one");
348 : return -ENOTSUPP;
349 : #endif
350 : }
351 0 : return gRdmaLiteOps.raRdmaLiteRestoreSnapshot(liteCtx);
352 : }
353 :
354 0 : unsigned int RaRdmaLiteGetApiVersion(void)
355 : {
356 0 : if (gRdmaLiteApiHandle != NULL && gRdmaLiteOps.raRdmaLiteGetApiVersion != NULL) {
357 0 : return gRdmaLiteOps.raRdmaLiteGetApiVersion();
358 : }
359 :
360 0 : return 0;
361 : }
|