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,
99 : hccp_err("rs_urma_set_jfc_opt URMA_JFC_ID failed, ret:%d, errno:%d", ret, errno), -EOPENSRC);
100 :
101 0 : ret = RsUrmaSetJfcOpt(jfcAttr->jfc, URMA_JFC_CQE_BASE_ADDR,
102 0 : (void *)&jfcAttr->cqeBaseAddrVa, sizeof(uint64_t));
103 0 : CHK_PRT_RETURN(ret != 0,
104 : hccp_err("rs_urma_set_jfc_opt URMA_JFC_CQE_BASE_ADDR failed, ret:%d, errno:%d", ret, errno), -EOPENSRC);
105 :
106 0 : return 0;
107 : }
108 :
109 0 : STATIC int RsJfcResAddrMunmap(struct RsCtxJfcCb *jfcCb, struct UdmaVaInfo *vaInfo)
110 : {
111 0 : struct res_map_info_in resInfoIn = {0};
112 0 : int ret = 0;
113 :
114 0 : resInfoIn.res_id = RsGenerateMmapResId(jfcCb->jfcId, jfcCb->devCb->devAttr.ub.dieId,
115 0 : jfcCb->devCb->devAttr.ub.funcId);
116 0 : resInfoIn.target_proc_type = PROCESS_CP1;
117 0 : resInfoIn.res_type = (enum res_addr_type)vaInfo->resType;
118 0 : resInfoIn.priv_len = sizeof(struct UdmaVaInfo);
119 0 : resInfoIn.priv = (void *)vaInfo;
120 0 : ret = DlHalResAddrUnmapV2(jfcCb->devCb->rscb->logicId, &resInfoIn);
121 0 : CHK_PRT_RETURN(ret != 0, hccp_err("DlHalResAddrUnmapV2 failed, res_type:%d ret:%d, errno:%d",
122 : resInfoIn.res_type, ret, errno), ret);
123 :
124 0 : return ret;
125 : }
126 :
127 0 : STATIC int RsJfcResAddrMmap(struct RsCtxJfcCb *jfcCb, struct UdmaVaInfo *vaInfo,
128 : struct res_map_info_out *resInfoOut)
129 : {
130 0 : struct res_map_info_in resInfoIn = {0};
131 0 : int ret = 0;
132 :
133 0 : resInfoIn.res_id = RsGenerateMmapResId(jfcCb->jfcId, jfcCb->devCb->devAttr.ub.dieId,
134 0 : jfcCb->devCb->devAttr.ub.funcId);
135 0 : resInfoIn.target_proc_type = PROCESS_CP1;
136 0 : resInfoIn.res_type = (enum res_addr_type)vaInfo->resType;
137 0 : resInfoIn.priv_len = sizeof(struct UdmaVaInfo);
138 0 : resInfoIn.priv = (void *)vaInfo;
139 0 : ret = DlHalResAddrMapV2(jfcCb->devCb->rscb->logicId, &resInfoIn, resInfoOut);
140 0 : CHK_PRT_RETURN(ret != 0, hccp_err("DlHalResAddrMapV2 failed, res_type:%d ret:%d, errno:%d",
141 : resInfoIn.res_type, ret, errno), ret);
142 :
143 0 : return ret;
144 : }
145 :
146 0 : STATIC void RsMunmapJfcVa(struct RsCtxJfcCb *jfcCb)
147 : {
148 0 : struct UdmaVaInfo vaInfo = {0};
149 :
150 0 : if (jfcCb->jfcType != JFC_MODE_USER_CTL_NORMAL) {
151 0 : return;
152 : }
153 :
154 0 : vaInfo.resType = RES_ADDR_TYPE_HCCP_URMA_JFC;
155 0 : vaInfo.va = jfcCb->bufAddr;
156 0 : vaInfo.len = WQE_BB_SIZE * jfcCb->depth;
157 0 : vaInfo.pid = getpid();
158 0 : (void)RsJfcResAddrMunmap(jfcCb, &vaInfo);
159 :
160 0 : vaInfo.resType = RES_ADDR_TYPE_HCCP_URMA_DB;
161 0 : vaInfo.va = jfcCb->swdbAddr;
162 0 : vaInfo.len = sizeof(uint64_t);
163 0 : vaInfo.pid = getpid();
164 0 : (void)RsJfcResAddrMunmap(jfcCb, &vaInfo);
165 : }
166 :
167 0 : STATIC int RsMmapJfcVa(struct RsCtxJfcCb *jfcCb)
168 : {
169 0 : struct res_map_info_out resInfoOut = {0};
170 0 : struct UdmaVaInfo jfcVaInfo = {0};
171 0 : struct UdmaVaInfo dbVaInfo = {0};
172 0 : int retTmp = 0;
173 0 : int ret = 0;
174 :
175 0 : jfcVaInfo.resType = RES_ADDR_TYPE_HCCP_URMA_JFC;
176 0 : jfcVaInfo.va = jfcCb->bufAddr;
177 0 : jfcVaInfo.len = WQE_BB_SIZE * jfcCb->depth;
178 0 : jfcVaInfo.pid = getpid();
179 0 : ret = RsJfcResAddrMmap(jfcCb, &jfcVaInfo, &resInfoOut);
180 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_jfc_res_addr_mmap failed, res_type:%u ret:%d", jfcVaInfo.resType, ret),
181 : ret);
182 0 : jfcCb->bufAddr = resInfoOut.va;
183 :
184 0 : dbVaInfo.resType = RES_ADDR_TYPE_HCCP_URMA_DB;
185 0 : dbVaInfo.va = jfcCb->swdbAddr;
186 0 : dbVaInfo.len = sizeof(uint64_t);
187 0 : dbVaInfo.pid = getpid();
188 0 : ret = RsJfcResAddrMmap(jfcCb, &dbVaInfo, &resInfoOut);
189 0 : if (ret != 0) {
190 0 : hccp_err("rs_jfc_res_addr_mmap failed, res_type:%u ret:%d", dbVaInfo.resType, ret);
191 0 : goto munmap_jfc_buff_va;
192 : }
193 :
194 0 : jfcCb->swdbAddr = resInfoOut.va;
195 0 : return ret;
196 :
197 0 : munmap_jfc_buff_va:
198 0 : jfcVaInfo.va = jfcCb->bufAddr;
199 0 : retTmp = RsJfcResAddrMunmap(jfcCb, &jfcVaInfo);
200 0 : CHK_PRT_RETURN(retTmp != 0, hccp_err("rs_jfc_res_addr_munmap failed, res_type:%u ret:%d",
201 : jfcVaInfo.resType, retTmp), retTmp);
202 0 : return ret;
203 : }
204 :
205 0 : STATIC int RsGetJfcOpt(struct RsCtxJfcCb *jfcCb, urma_jfc_t *jfc)
206 : {
207 0 : uint64_t cqBuffVa = 0, dbVa = 0;
208 0 : int ret = 0;
209 :
210 0 : if (jfcCb->jfcType != JFC_MODE_USER_CTL_NORMAL) {
211 0 : return ret;
212 : }
213 :
214 0 : ret = RsUrmaGetJfcOpt(jfc, URMA_JFC_CQE_BASE_ADDR, &cqBuffVa, sizeof(uint64_t));
215 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_urma_get_jfc_opt URMA_JFC_CQE_BASE_ADDR failed, ret:%d, errno:%d", ret, errno),
216 : -EOPENSRC);
217 :
218 0 : ret = RsUrmaGetJfcOpt(jfc, URMA_JFC_DB_ADDR, &dbVa, sizeof(uint64_t));
219 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_urma_get_jfc_opt URMA_JFC_DB_ADDR failed, ret:%d, errno:%d",
220 : ret, errno), -EOPENSRC);
221 :
222 0 : jfcCb->bufAddr = cqBuffVa;
223 0 : jfcCb->swdbAddr = dbVa;
224 :
225 0 : ret = RsMmapJfcVa(jfcCb);
226 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_mmap_jfc_va failed, ret:%d", ret), ret);
227 :
228 0 : return ret;
229 : }
230 :
231 0 : int RsUbCtxJfcCreateExt(struct RsCtxJfcCb *jfcCb, urma_jfc_cfg_t *jfcCfg, urma_jfc_t **jfc)
232 : {
233 0 : struct ExtJfcAttr jfcAttr = {0};
234 0 : int ret = 0;
235 :
236 0 : ret = RsInitJfcAttr(jfcCb, jfcCfg, &jfcAttr);
237 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_init_jfc_attr failed, ret:%d", ret), ret);
238 :
239 0 : ret = RsSetJfcOpt(jfcCb, &jfcAttr);
240 0 : if (ret != 0) {
241 0 : hccp_err("rs_set_jfc_attr failed, ret:%d", ret);
242 0 : goto deinit_attr;
243 : }
244 :
245 0 : ret = RsUrmaActiveJfc(jfcAttr.jfc);
246 0 : if (ret != 0) {
247 0 : hccp_err("rs_urma_active_jfc failed, jfcId:%u, ret:%d, errno:%d", jfcAttr.jfc->jfc_id.id, ret, errno);
248 0 : ret = -EOPENSRC;
249 0 : goto deinit_attr;
250 : }
251 0 : jfcCb->jfcId = jfcAttr.jfc->jfc_id.id;
252 :
253 0 : ret = RsGetJfcOpt(jfcCb, jfcAttr.jfc);
254 0 : if (ret != 0) {
255 0 : hccp_err("rs_get_jfc_opt failed, jfcId:%u, ret:%d, errno:%d", jfcAttr.jfc->jfc_id.id, ret, errno);
256 0 : goto deactive_jfc;
257 : }
258 :
259 0 : *jfc = jfcAttr.jfc;
260 0 : return 0;
261 :
262 0 : deactive_jfc:
263 0 : (void)RsUrmaDeactiveJfc(jfcAttr.jfc);
264 0 : deinit_attr:
265 0 : (void)RsDeinitJfcAttr(jfcCb, jfcCfg, &jfcAttr);
266 0 : *jfc = NULL;
267 0 : return ret;
268 : }
269 :
270 0 : int RsUbDeleteJfcExt(struct RsUbDevCb *devCb, struct RsCtxJfcCb *jfcCb)
271 : {
272 0 : urma_jfc_t *jfc = (urma_jfc_t *)(uintptr_t)(jfcCb->jfcAddr);
273 0 : unsigned int jfcId = jfc->jfc_id.id;
274 0 : int ret = 0;
275 :
276 0 : RsMunmapJfcVa(jfcCb);
277 :
278 0 : ret = RsUrmaDeactiveJfc(jfc);
279 0 : if (ret != 0) {
280 0 : hccp_err("rs_urma_deactive_jfc failed, jfcId:%u, ret:%d, errno:%d", jfcId, ret, errno);
281 0 : ret = -EOPENSRC;
282 : }
283 :
284 0 : ret = RsUrmaFreeJfc(jfc);
285 0 : if (ret != 0) {
286 0 : hccp_err("rs_urma_free_jfc failed, jfcId:%u, ret:%d, errno:%d", jfcId, ret, errno);
287 0 : ret = -EOPENSRC;
288 : }
289 :
290 0 : if (jfcCb->jfcType != JFC_MODE_USER_CTL_NORMAL) {
291 0 : ret = RsNetFreeJfcId(devCb->urmaDev->name, jfcCb->jfcType, jfcId);
292 0 : if (ret != 0) {
293 0 : hccp_err("rs_net_free_jfc_id failed, jfcId:%u, ret:%d", jfcId, ret);
294 : }
295 : }
296 :
297 0 : return ret;
298 : }
|