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 <unistd.h>
12 : #include <stdlib.h>
13 : #include <sys/types.h>
14 : #include <netinet/in.h>
15 : #include <arpa/inet.h>
16 : #include <dlfcn.h>
17 : #include "urma_opcode.h"
18 : #include "user_log.h"
19 : #include "dl_hal_function.h"
20 : #include "dl_urma_function.h"
21 : #include "dl_net_function.h"
22 : #include "dl_ccu_function.h"
23 : #include "ra_rs_err.h"
24 : #include "rs_inner.h"
25 : #include "rs_ctx_inner.h"
26 : #include "rs_ub_jfc.h"
27 :
28 : struct ExtJfcAttr {
29 : urma_jfc_t *jfc;
30 : unsigned int jfcId;
31 : unsigned long long cqeBaseAddrVa;
32 : };
33 :
34 0 : STATIC int RsInitJfcAttr(struct RsCtxJfcCb *jfcCb, urma_jfc_cfg_t *jfcCfg, struct ExtJfcAttr *jfcAttr)
35 : {
36 0 : unsigned int dieId = 0;
37 0 : int ret = 0;
38 :
39 0 : ret = RsUrmaAllocJfc(jfcCb->devCb->urmaCtx, jfcCfg, &jfcAttr->jfc);
40 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_urma_alloc_jfc failed, ret:%d errno:%d", ret, errno), -EOPENSRC);
41 :
42 0 : if (jfcCb->jfcType == JFC_MODE_USER_CTL_NORMAL) {
43 0 : return 0;
44 : }
45 :
46 0 : if (jfcCb->jfcType == JFC_MODE_CCU_POLL) {
47 0 : if (!jfcCb->ccuExCfg.valid || jfcCb->ccuExCfg.cqeFlag == 0) {
48 0 : dieId = jfcCb->devCb->devAttr.ub.dieId;
49 : } else {
50 0 : dieId = (jfcCb->devCb->devAttr.ub.dieId == 0) ? 1 : 0;
51 : }
52 0 : ret = RsCcuGetCqeBaseAddr(dieId, &jfcAttr->cqeBaseAddrVa);
53 0 : if (ret != 0 || jfcAttr->cqeBaseAddrVa == 0) {
54 0 : hccp_err("rs_ccu_get_cqe_base_addr failed, ret:%d, dieId:%u", ret, dieId);
55 0 : ret = -EOPENSRC;
56 0 : goto free_jfc;
57 : }
58 : } else {
59 0 : ret = RsNetGetCqeBaseAddr(jfcCb->devCb->devAttr.ub.dieId, &jfcAttr->cqeBaseAddrVa);
60 0 : if (ret != 0 || jfcAttr->cqeBaseAddrVa == 0) {
61 0 : hccp_err("rs_net_get_cqe_base_addr failed, ret:%d, dieId:%u", ret, jfcCb->devCb->devAttr.ub.dieId);
62 0 : ret = -EOPENSRC;
63 0 : goto free_jfc;
64 : }
65 : }
66 :
67 0 : ret = RsNetAllocJfcId(jfcCb->devCb->urmaDev->name, jfcCb->jfcType, &jfcAttr->jfcId);
68 0 : if (ret != 0) {
69 0 : hccp_err("rs_net_alloc_jfc_id failed, ret:%d", ret);
70 0 : goto free_jfc;
71 : }
72 :
73 0 : return 0;
74 :
75 0 : free_jfc:
76 0 : (void)RsUrmaFreeJfc(jfcAttr->jfc);
77 0 : return ret;
78 : }
79 :
80 0 : STATIC void RsDeinitJfcAttr(struct RsCtxJfcCb *jfcCb, urma_jfc_cfg_t *jfcCfg, struct ExtJfcAttr *jfcAttr)
81 : {
82 0 : (void)RsUrmaFreeJfc(jfcAttr->jfc);
83 0 : if (jfcCb->jfcType == JFC_MODE_USER_CTL_NORMAL) {
84 0 : return;
85 : }
86 0 : (void)RsNetFreeJfcId(jfcCb->devCb->urmaDev->name, jfcCb->jfcType, jfcAttr->jfcId);
87 : }
88 :
89 0 : STATIC int RsSetJfcOpt(struct RsCtxJfcCb *jfcCb, struct ExtJfcAttr *jfcAttr)
90 : {
91 0 : int ret = 0;
92 :
93 0 : if (jfcCb->jfcType == JFC_MODE_USER_CTL_NORMAL) {
94 0 : return ret;
95 : }
96 :
97 0 : ret = RsUrmaSetJfcOpt(jfcAttr->jfc, URMA_JFC_ID, (void *)&jfcAttr->jfcId, sizeof(uint32_t));
98 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_urma_set_jfc_opt URMA_JFC_ID failed, ret:%d, errno:%d", ret, errno),
99 : -EOPENSRC);
100 :
101 0 : ret = RsUrmaSetJfcOpt(jfcAttr->jfc, URMA_JFC_CQE_BASE_ADDR, (void *)&jfcAttr->cqeBaseAddrVa, sizeof(uint64_t));
102 0 : CHK_PRT_RETURN(ret != 0,
103 : hccp_err("rs_urma_set_jfc_opt URMA_JFC_CQE_BASE_ADDR failed, ret:%d, errno:%d", ret, errno), -EOPENSRC);
104 :
105 0 : return 0;
106 : }
107 :
108 0 : STATIC int RsJfcResAddrMunmap(struct RsCtxJfcCb *jfcCb, struct UdmaVaInfo *vaInfo)
109 : {
110 0 : struct res_map_info_in resInfoIn = {0};
111 0 : int ret = 0;
112 :
113 0 : resInfoIn.res_id = RsGenerateMmapResId(jfcCb->jfcId, jfcCb->devCb->devAttr.ub.dieId,
114 0 : jfcCb->devCb->devAttr.ub.funcId);
115 0 : resInfoIn.target_proc_type = PROCESS_CP1;
116 0 : resInfoIn.res_type = (enum res_addr_type)vaInfo->resType;
117 0 : resInfoIn.priv_len = sizeof(struct UdmaVaInfo);
118 0 : resInfoIn.priv = (void *)vaInfo;
119 0 : ret = DlHalResAddrUnmapV2(jfcCb->devCb->rscb->logicId, &resInfoIn);
120 0 : CHK_PRT_RETURN(ret != 0,
121 : hccp_err("DlHalResAddrUnmapV2 failed, res_type:%d ret:%d, errno:%d", resInfoIn.res_type, ret, errno), ret);
122 :
123 0 : return ret;
124 : }
125 :
126 0 : STATIC int RsJfcResAddrMmap(struct RsCtxJfcCb *jfcCb, struct UdmaVaInfo *vaInfo, struct res_map_info_out *resInfoOut)
127 : {
128 0 : struct res_map_info_in resInfoIn = {0};
129 0 : int ret = 0;
130 :
131 0 : resInfoIn.res_id = RsGenerateMmapResId(jfcCb->jfcId, jfcCb->devCb->devAttr.ub.dieId,
132 0 : jfcCb->devCb->devAttr.ub.funcId);
133 0 : resInfoIn.target_proc_type = PROCESS_CP1;
134 0 : resInfoIn.res_type = (enum res_addr_type)vaInfo->resType;
135 0 : resInfoIn.priv_len = sizeof(struct UdmaVaInfo);
136 0 : resInfoIn.priv = (void *)vaInfo;
137 0 : ret = DlHalResAddrMapV2(jfcCb->devCb->rscb->logicId, &resInfoIn, resInfoOut);
138 0 : CHK_PRT_RETURN(ret != 0,
139 : hccp_err("DlHalResAddrMapV2 failed, res_type:%d ret:%d, errno:%d", resInfoIn.res_type, ret, errno), ret);
140 :
141 0 : return ret;
142 : }
143 :
144 0 : STATIC void RsMunmapJfcVa(struct RsCtxJfcCb *jfcCb)
145 : {
146 0 : struct UdmaVaInfo vaInfo = {0};
147 :
148 0 : if (jfcCb->jfcType != JFC_MODE_USER_CTL_NORMAL) {
149 0 : return;
150 : }
151 :
152 0 : vaInfo.resType = RES_ADDR_TYPE_HCCP_URMA_JFC;
153 0 : vaInfo.va = jfcCb->bufAddr;
154 0 : vaInfo.len = WQE_BB_SIZE * jfcCb->depth;
155 0 : vaInfo.pid = getpid();
156 0 : (void)RsJfcResAddrMunmap(jfcCb, &vaInfo);
157 :
158 0 : vaInfo.resType = RES_ADDR_TYPE_HCCP_URMA_DB;
159 0 : vaInfo.va = jfcCb->swdbAddr;
160 0 : vaInfo.len = sizeof(uint64_t);
161 0 : vaInfo.pid = getpid();
162 0 : (void)RsJfcResAddrMunmap(jfcCb, &vaInfo);
163 : }
164 :
165 0 : STATIC int RsMmapJfcVa(struct RsCtxJfcCb *jfcCb)
166 : {
167 0 : struct res_map_info_out resInfoOut = {0};
168 0 : struct UdmaVaInfo jfcVaInfo = {0};
169 0 : struct UdmaVaInfo dbVaInfo = {0};
170 0 : int retTmp = 0;
171 0 : int ret = 0;
172 :
173 0 : jfcVaInfo.resType = RES_ADDR_TYPE_HCCP_URMA_JFC;
174 0 : jfcVaInfo.va = jfcCb->bufAddr;
175 0 : jfcVaInfo.len = WQE_BB_SIZE * jfcCb->depth;
176 0 : jfcVaInfo.pid = getpid();
177 0 : ret = RsJfcResAddrMmap(jfcCb, &jfcVaInfo, &resInfoOut);
178 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_jfc_res_addr_mmap failed, res_type:%u ret:%d", jfcVaInfo.resType, ret), ret);
179 0 : jfcCb->bufAddr = resInfoOut.va;
180 :
181 0 : dbVaInfo.resType = RES_ADDR_TYPE_HCCP_URMA_DB;
182 0 : dbVaInfo.va = jfcCb->swdbAddr;
183 0 : dbVaInfo.len = sizeof(uint64_t);
184 0 : dbVaInfo.pid = getpid();
185 0 : ret = RsJfcResAddrMmap(jfcCb, &dbVaInfo, &resInfoOut);
186 0 : if (ret != 0) {
187 0 : hccp_err("rs_jfc_res_addr_mmap failed, res_type:%u ret:%d", dbVaInfo.resType, ret);
188 0 : goto munmap_jfc_buff_va;
189 : }
190 :
191 0 : jfcCb->swdbAddr = resInfoOut.va;
192 0 : return ret;
193 :
194 0 : munmap_jfc_buff_va:
195 0 : jfcVaInfo.va = jfcCb->bufAddr;
196 0 : retTmp = RsJfcResAddrMunmap(jfcCb, &jfcVaInfo);
197 0 : CHK_PRT_RETURN(retTmp != 0,
198 : hccp_err("rs_jfc_res_addr_munmap failed, res_type:%u ret:%d", jfcVaInfo.resType, retTmp), retTmp);
199 0 : return ret;
200 : }
201 :
202 0 : STATIC int RsGetJfcOpt(struct RsCtxJfcCb *jfcCb, urma_jfc_t *jfc)
203 : {
204 0 : uint64_t cqBuffVa = 0, dbVa = 0;
205 0 : int ret = 0;
206 :
207 0 : if (jfcCb->jfcType != JFC_MODE_USER_CTL_NORMAL) {
208 0 : return ret;
209 : }
210 :
211 0 : ret = RsUrmaGetJfcOpt(jfc, URMA_JFC_CQE_BASE_ADDR, &cqBuffVa, sizeof(uint64_t));
212 0 : CHK_PRT_RETURN(ret != 0,
213 : hccp_err("rs_urma_get_jfc_opt URMA_JFC_CQE_BASE_ADDR failed, ret:%d, errno:%d", ret, errno), -EOPENSRC);
214 :
215 0 : ret = RsUrmaGetJfcOpt(jfc, URMA_JFC_DB_ADDR, &dbVa, sizeof(uint64_t));
216 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_urma_get_jfc_opt URMA_JFC_DB_ADDR failed, ret:%d, errno:%d", ret, errno),
217 : -EOPENSRC);
218 :
219 0 : jfcCb->bufAddr = cqBuffVa;
220 0 : jfcCb->swdbAddr = dbVa;
221 :
222 0 : ret = RsMmapJfcVa(jfcCb);
223 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_mmap_jfc_va failed, ret:%d", ret), ret);
224 :
225 0 : return ret;
226 : }
227 :
228 0 : int RsUbCtxJfcCreateExt(struct RsCtxJfcCb *jfcCb, urma_jfc_cfg_t *jfcCfg, urma_jfc_t **jfc)
229 : {
230 0 : struct ExtJfcAttr jfcAttr = {0};
231 0 : int ret = 0;
232 :
233 0 : ret = RsInitJfcAttr(jfcCb, jfcCfg, &jfcAttr);
234 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_init_jfc_attr failed, ret:%d", ret), ret);
235 :
236 0 : ret = RsSetJfcOpt(jfcCb, &jfcAttr);
237 0 : if (ret != 0) {
238 0 : hccp_err("rs_set_jfc_attr failed, ret:%d", ret);
239 0 : goto deinit_attr;
240 : }
241 :
242 0 : ret = RsUrmaActiveJfc(jfcAttr.jfc);
243 0 : if (ret != 0) {
244 0 : hccp_err("rs_urma_active_jfc failed, jfcId:%u, ret:%d, errno:%d", jfcAttr.jfc->jfc_id.id, ret, errno);
245 0 : ret = -EOPENSRC;
246 0 : goto deinit_attr;
247 : }
248 0 : jfcCb->jfcId = jfcAttr.jfc->jfc_id.id;
249 :
250 0 : ret = RsGetJfcOpt(jfcCb, jfcAttr.jfc);
251 0 : if (ret != 0) {
252 0 : hccp_err("rs_get_jfc_opt failed, jfcId:%u, ret:%d, errno:%d", jfcAttr.jfc->jfc_id.id, ret, errno);
253 0 : goto deactive_jfc;
254 : }
255 :
256 0 : *jfc = jfcAttr.jfc;
257 0 : return 0;
258 :
259 0 : deactive_jfc:
260 0 : (void)RsUrmaDeactiveJfc(jfcAttr.jfc);
261 0 : deinit_attr:
262 0 : (void)RsDeinitJfcAttr(jfcCb, jfcCfg, &jfcAttr);
263 0 : *jfc = NULL;
264 0 : return ret;
265 : }
266 :
267 0 : int RsUbDeleteJfcExt(struct RsUbDevCb *devCb, struct RsCtxJfcCb *jfcCb)
268 : {
269 0 : urma_jfc_t *jfc = (urma_jfc_t *)(uintptr_t)(jfcCb->jfcAddr);
270 0 : unsigned int jfcId = jfc->jfc_id.id;
271 0 : int ret = 0;
272 :
273 0 : RsMunmapJfcVa(jfcCb);
274 :
275 0 : ret = RsUrmaDeactiveJfc(jfc);
276 0 : if (ret != 0) {
277 0 : hccp_err("rs_urma_deactive_jfc failed, jfcId:%u, ret:%d, errno:%d", jfcId, ret, errno);
278 0 : ret = -EOPENSRC;
279 : }
280 :
281 0 : ret = RsUrmaFreeJfc(jfc);
282 0 : if (ret != 0) {
283 0 : hccp_err("rs_urma_free_jfc failed, jfcId:%u, ret:%d, errno:%d", jfcId, ret, errno);
284 0 : ret = -EOPENSRC;
285 : }
286 :
287 0 : if (jfcCb->jfcType != JFC_MODE_USER_CTL_NORMAL) {
288 0 : ret = RsNetFreeJfcId(devCb->urmaDev->name, jfcCb->jfcType, jfcId);
289 0 : if (ret != 0) {
290 0 : hccp_err("rs_net_free_jfc_id failed, jfcId:%u, ret:%d", jfcId, ret);
291 : }
292 : }
293 :
294 0 : return ret;
295 : }
|