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))dlsym(gRdmaLiteApiHandle,
66 : "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,
74 : u32 memIdx))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 *
78 : (*)(struct rdma_lite_context * liteCtx, struct rdma_lite_cq_attr * liteCqAttr))
79 : dlsym(gRdmaLiteApiHandle, "rdma_lite_create_cq");
80 : DL_API_RET_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteCreateCq, "rdma_lite_create_cq", gRdmaLiteApiLock);
81 :
82 : gRdmaLiteOps.raRdmaLiteDestroyCq = (int (*)(struct rdma_lite_cq *liteCq))dlsym(gRdmaLiteApiHandle,
83 : "rdma_lite_destroy_cq");
84 : DL_API_RET_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteDestroyCq, "rdma_lite_destroy_cq", gRdmaLiteApiLock);
85 :
86 : gRdmaLiteOps.raRdmaLiteCreateQp = (struct rdma_lite_qp *
87 : (*)(struct rdma_lite_context * liteCtx, struct rdma_lite_qp_attr * liteQpAttr))
88 : dlsym(gRdmaLiteApiHandle, "rdma_lite_create_qp");
89 : DL_API_RET_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteCreateQp, "rdma_lite_create_qp", gRdmaLiteApiLock);
90 :
91 : gRdmaLiteOps.raRdmaLiteDestroyQp = (int (*)(struct rdma_lite_qp *liteQp))dlsym(gRdmaLiteApiHandle,
92 : "rdma_lite_destroy_qp");
93 : DL_API_RET_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteDestroyQp, "rdma_lite_destroy_qp", gRdmaLiteApiLock);
94 :
95 : gRdmaLiteOps.raRdmaLiteSetQpSl = (int (*)(struct rdma_lite_qp *liteQp, int sl))dlsym(gRdmaLiteApiHandle,
96 : "rdma_lite_set_qp_sl");
97 : DL_API_RET_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteSetQpSl, "rdma_lite_set_qp_sl", gRdmaLiteApiLock);
98 :
99 : gRdmaLiteOps.raRdmaLiteCleanQp = (int (*)(struct rdma_lite_qp *liteQp))dlsym(gRdmaLiteApiHandle,
100 : "rdma_lite_clean_qp");
101 : DL_API_PTR_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteCleanQp, "rdma_lite_clean_qp");
102 :
103 : gRdmaLiteOps.raRdmaLiteRestoreSnapshot = (int (*)(struct rdma_lite_context *liteCtx))dlsym(gRdmaLiteApiHandle,
104 : "rdma_lite_restore_snapshot");
105 : DL_API_PTR_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteRestoreSnapshot, "rdma_lite_restore_snapshot");
106 :
107 : gRdmaLiteOps.raRdmaLiteGetApiVersion = (unsigned int (*)(void))dlsym(gRdmaLiteApiHandle,
108 : "rdma_lite_get_api_version");
109 : DL_API_PTR_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLiteGetApiVersion, "rdma_lite_get_api_version");
110 :
111 : return 0;
112 : }
113 :
114 : static int RaRdmaLiteDataPlaneApiInit(void)
115 : {
116 : gRdmaLiteOps.raRdmaLitePostSend = (int (*)(struct rdma_lite_qp *liteQp, struct rdma_lite_send_wr *wr,
117 : struct rdma_lite_send_wr **badWr, struct rdma_lite_post_send_attr *attr,
118 : struct rdma_lite_post_send_resp *resp))dlsym(gRdmaLiteApiHandle, "rdma_lite_post_send");
119 : DL_API_RET_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLitePostSend, "rdma_lite_post_send", gRdmaLiteApiLock);
120 :
121 : gRdmaLiteOps.raRdmaLitePostRecv = (int (*)(struct rdma_lite_qp *liteQp, struct rdma_lite_recv_wr *wr,
122 : struct rdma_lite_recv_wr **badWr))dlsym(gRdmaLiteApiHandle, "rdma_lite_post_recv");
123 : DL_API_PTR_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLitePostRecv, "rdma_lite_post_recv");
124 :
125 : gRdmaLiteOps.raRdmaLitePollCq = (int (*)(struct rdma_lite_cq *liteCq, int numEntries,
126 : struct rdma_lite_wc *liteWc))dlsym(gRdmaLiteApiHandle, "rdma_lite_poll_cq");
127 : DL_API_RET_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLitePollCq, "rdma_lite_poll_cq", gRdmaLiteApiLock);
128 :
129 : gRdmaLiteOps.raRdmaLitePollCqV2 = (int (*)(struct rdma_lite_cq *liteCq, int numEntries,
130 : struct rdma_lite_wc_v2 *liteWc))dlsym(gRdmaLiteApiHandle, "rdma_lite_poll_cq_v2");
131 : DL_API_PTR_IS_NULL_CHECK(gRdmaLiteOps.raRdmaLitePollCqV2, "rdma_lite_poll_cq_v2");
132 : return 0;
133 : }
134 : #endif
135 :
136 7 : DL_ATTRI_VISI_DEF int RaHdcRdmaLiteApiInit(void)
137 : {
138 : #ifndef HNS_ROCE_LLT
139 : int ret;
140 :
141 : pthread_mutex_lock(&gRdmaLiteApiLock);
142 : if (gRdmaLiteApiHandle != NULL) {
143 : gRdmaLiteApiRefcnt++;
144 : pthread_mutex_unlock(&gRdmaLiteApiLock);
145 : return 0;
146 : }
147 :
148 : ret = RaHdcOpenRdmaLiteSo();
149 : if (ret) {
150 : pthread_mutex_unlock(&gRdmaLiteApiLock);
151 : hccp_err("HccpDlopen[libascend_rdma_lite.so]"
152 : "failed! ret=[%d][%s]. Please check rdma lite driver has been installed.",
153 : ret, dlerror());
154 : return ret;
155 : }
156 :
157 : ret = RaRdmaLiteControlPlaneApiInit();
158 : if (ret != 0) {
159 : return ret;
160 : }
161 :
162 : ret = RaRdmaLiteDataPlaneApiInit();
163 : if (ret != 0) {
164 : return ret;
165 : }
166 :
167 : gRdmaLiteApiRefcnt++;
168 : pthread_mutex_unlock(&gRdmaLiteApiLock);
169 : #endif
170 7 : return 0;
171 : }
172 :
173 7 : DL_ATTRI_VISI_DEF void RaHdcRdmaLiteApiDeinit(void)
174 : {
175 7 : pthread_mutex_lock(&gRdmaLiteApiLock);
176 7 : if (gRdmaLiteApiHandle != NULL) {
177 0 : gRdmaLiteApiRefcnt--;
178 0 : if (gRdmaLiteApiRefcnt > 0) {
179 0 : pthread_mutex_unlock(&gRdmaLiteApiLock);
180 0 : return;
181 : }
182 0 : (void)dlclose(gRdmaLiteApiHandle);
183 0 : gRdmaLiteApiHandle = NULL;
184 : }
185 7 : pthread_mutex_unlock(&gRdmaLiteApiLock);
186 :
187 7 : return;
188 : }
189 :
190 6 : struct rdma_lite_context *RaRdmaLiteAllocCtx(u8 phyId, struct dev_cap_info *cap)
191 : {
192 6 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteAllocCtx == NULL) {
193 : #ifndef HNS_ROCE_LLT
194 : hccp_err("ra_rdma_lite_alloc_ctx is null");
195 : return NULL;
196 : #endif
197 : }
198 6 : return gRdmaLiteOps.raRdmaLiteAllocCtx(phyId, cap);
199 : }
200 :
201 6 : void RaRdmaLiteFreeCtx(struct rdma_lite_context *liteCtx)
202 : {
203 6 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteFreeCtx == NULL) {
204 : #ifndef HNS_ROCE_LLT
205 : hccp_err("ra_rdma_lite_free_ctx is null");
206 : return;
207 : #endif
208 : }
209 6 : return gRdmaLiteOps.raRdmaLiteFreeCtx(liteCtx);
210 : }
211 :
212 6 : struct rdma_lite_cq *RaRdmaLiteCreateCq(struct rdma_lite_context *liteCtx, struct rdma_lite_cq_attr *liteCqAttr)
213 : {
214 6 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteCreateCq == NULL) {
215 : #ifndef HNS_ROCE_LLT
216 : hccp_err("ra_rdma_lite_create_cq is null");
217 : return NULL;
218 : #endif
219 : }
220 6 : return gRdmaLiteOps.raRdmaLiteCreateCq(liteCtx, liteCqAttr);
221 : }
222 :
223 6 : int RaRdmaLiteDestroyCq(struct rdma_lite_cq *liteCq)
224 : {
225 6 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteDestroyCq == NULL) {
226 : #ifndef HNS_ROCE_LLT
227 : hccp_err("ra_rdma_lite_destroy_cq is null");
228 : return -EINVAL;
229 : #endif
230 : }
231 6 : return gRdmaLiteOps.raRdmaLiteDestroyCq(liteCq);
232 : }
233 :
234 0 : int RaRdmaLitePollCq(struct rdma_lite_cq *liteCq, int numEntries, struct rdma_lite_wc *liteWc)
235 : {
236 0 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLitePollCq == NULL) {
237 : #ifndef HNS_ROCE_LLT
238 : hccp_err("ra_rdma_lite_poll_cq is null");
239 : return -EINVAL;
240 : #endif
241 : }
242 0 : return gRdmaLiteOps.raRdmaLitePollCq(liteCq, numEntries, liteWc);
243 : }
244 :
245 1 : int RaRdmaLitePollCqV2(struct rdma_lite_cq *liteCq, int numEntries, struct rdma_lite_wc_v2 *liteWc)
246 : {
247 1 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLitePollCqV2 == NULL) {
248 : #ifndef HNS_ROCE_LLT
249 : hccp_err("ra_rdma_lite_poll_cq_v2 is null");
250 : return -EINVAL;
251 : #endif
252 : }
253 1 : return gRdmaLiteOps.raRdmaLitePollCqV2(liteCq, numEntries, liteWc);
254 : }
255 :
256 3 : struct rdma_lite_qp *RaRdmaLiteCreateQp(struct rdma_lite_context *liteCtx, struct rdma_lite_qp_attr *liteQpAttr)
257 : {
258 3 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteCreateQp == NULL) {
259 : #ifndef HNS_ROCE_LLT
260 : hccp_err("ra_rdma_lite_create_qp is null");
261 : return NULL;
262 : #endif
263 : }
264 3 : return gRdmaLiteOps.raRdmaLiteCreateQp(liteCtx, liteQpAttr);
265 : }
266 :
267 3 : int RaRdmaLiteDestroyQp(struct rdma_lite_qp *liteQp)
268 : {
269 3 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteDestroyQp == NULL) {
270 : #ifndef HNS_ROCE_LLT
271 : hccp_err("ra_rdma_lite_destroy_qp is null");
272 : return -EINVAL;
273 : #endif
274 : }
275 3 : return gRdmaLiteOps.raRdmaLiteDestroyQp(liteQp);
276 : }
277 :
278 2 : int RaRdmaLitePostSend(struct rdma_lite_qp *liteQp, struct rdma_lite_send_wr *wr, struct rdma_lite_send_wr **badWr,
279 : struct rdma_lite_post_send_attr *attr, struct rdma_lite_post_send_resp *resp)
280 : {
281 2 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLitePostSend == NULL) {
282 : #ifndef HNS_ROCE_LLT
283 : hccp_err("ra_rdma_lite_post_send is null");
284 : return -EINVAL;
285 : #endif
286 : }
287 2 : return gRdmaLiteOps.raRdmaLitePostSend(liteQp, wr, badWr, attr, resp);
288 : }
289 :
290 1 : int RaRdmaLitePostRecv(struct rdma_lite_qp *liteQp, struct rdma_lite_recv_wr *wr, struct rdma_lite_recv_wr **badWr)
291 : {
292 1 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLitePostRecv == NULL) {
293 : #ifndef HNS_ROCE_LLT
294 : hccp_err("ra_rdma_lite_post_recv is null");
295 : return -EINVAL;
296 : #endif
297 : }
298 1 : return gRdmaLiteOps.raRdmaLitePostRecv(liteQp, wr, badWr);
299 : }
300 :
301 1 : int RaRdmaLiteSetQpSl(struct rdma_lite_qp *liteQp, unsigned char sl)
302 : {
303 1 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteSetQpSl == NULL) {
304 : #ifndef HNS_ROCE_LLT
305 : hccp_err("ra_rdma_lite_set_qp_sl is null");
306 : return -EINVAL;
307 : #endif
308 : }
309 1 : return gRdmaLiteOps.raRdmaLiteSetQpSl(liteQp, sl);
310 : }
311 :
312 0 : int RaRdmaLiteCleanQp(struct rdma_lite_qp *liteQp)
313 : {
314 0 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteCleanQp == NULL) {
315 : #ifndef HNS_ROCE_LLT
316 : hccp_err("ra_rdma_lite_clean_qp is null");
317 : return -EINVAL;
318 : #endif
319 : }
320 0 : return gRdmaLiteOps.raRdmaLiteCleanQp(liteQp);
321 : }
322 :
323 0 : int RaRdmaLiteInitMemPool(struct rdma_lite_context *liteCtx, struct rdma_lite_mem_attr *liteMemAttr)
324 : {
325 0 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteInitMemPool == NULL) {
326 : #ifndef HNS_ROCE_LLT
327 : hccp_err("ra_rdma_lite_init_mem_pool is null");
328 : return -EINVAL;
329 : #endif
330 : }
331 0 : return gRdmaLiteOps.raRdmaLiteInitMemPool(liteCtx, liteMemAttr);
332 : }
333 :
334 0 : int RaRdmaLiteDeinitMemPool(struct rdma_lite_context *liteCtx, u32 memIdx)
335 : {
336 0 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteDeinitMemPool == NULL) {
337 : #ifndef HNS_ROCE_LLT
338 : hccp_err("ra_rdma_lite_deinit_mem_pool is null");
339 : return -EINVAL;
340 : #endif
341 : }
342 0 : return gRdmaLiteOps.raRdmaLiteDeinitMemPool(liteCtx, memIdx);
343 : }
344 :
345 0 : int RaRdmaLiteRestoreSnapshot(struct rdma_lite_context *liteCtx)
346 : {
347 0 : if (gRdmaLiteApiHandle == NULL || gRdmaLiteOps.raRdmaLiteRestoreSnapshot == NULL) {
348 : #ifndef HNS_ROCE_LLT
349 : hccp_err("driver package may not support ra_rdma_lite_restore_snapshot interface, please change new one");
350 : return -ENOTSUPP;
351 : #endif
352 : }
353 0 : return gRdmaLiteOps.raRdmaLiteRestoreSnapshot(liteCtx);
354 : }
355 :
356 0 : unsigned int RaRdmaLiteGetApiVersion(void)
357 : {
358 0 : if (gRdmaLiteApiHandle != NULL && gRdmaLiteOps.raRdmaLiteGetApiVersion != NULL) {
359 0 : return gRdmaLiteOps.raRdmaLiteGetApiVersion();
360 : }
361 :
362 0 : return 0;
363 : }
|