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 "user_log.h"
12 : #include "ra_hdc.h"
13 : #include <stdlib.h>
14 : #include <string.h>
15 : #include <arpa/inet.h>
16 : #include <unistd.h>
17 : #include <errno.h>
18 : #include <sys/time.h>
19 : #include "securec.h"
20 : #include "dl_hal_function.h"
21 : #include "ra.h"
22 : #include "ra_comm.h"
23 : #include "ra_hdc_lite.h"
24 : #include "ra_rs_err.h"
25 : #include "ra_rs_comm.h"
26 : #include "ra_hdc_async.h"
27 :
28 : struct HdcInfo gRaHdc[RA_MAX_PHY_ID_NUM] = {0};
29 :
30 : struct HdcOps gRaHdcOpsHost = {
31 : .getCapacity = DlDrvHdcGetCapacity,
32 : .clientCreate = DlDrvHdcClientCreate,
33 : .clientDestroy = DlDrvHdcClientDestroy,
34 : .sessionConnect = DlDrvHdcSessionConnect,
35 : .sessionConnectEx = DlHalHdcSessionConnectEx,
36 : .serverCreate = DlDrvHdcServerCreate,
37 : .serverDestroy = DlDrvHdcServerDestroy,
38 : .sessionAccept = DlDrvHdcSessionAccept,
39 : .sessionClose = DlDrvHdcSessionClose,
40 : .freeMsg = DlDrvHdcFreeMsg,
41 : .reuseMsg = DlDrvHdcReuseMsg,
42 : .addMsgBuffer = DlDrvHdcAddMsgBuffer,
43 : .getMsgBuffer = DlDrvHdcGetMsgBuffer,
44 : .recv = DlHalHdcRecv,
45 : .send = DlHalHdcSend,
46 : .allocMsg = DlDrvHdcAllocMsg,
47 : .setSessionReference = DlDrvHdcSetSessionReference,
48 : };
49 :
50 : #define RA_HDC_OPS gRaHdcOpsHost
51 :
52 : struct OpcodeInterfaceInfo gRaInterfaceInfoList[] = {
53 : // outer opcode version: 1.0
54 : {RA_RS_SOCKET_CONN, 0},
55 : {RA_RS_SOCKET_CLOSE, 0},
56 : {RA_RS_SOCKET_ABORT, 0},
57 : {RA_RS_SOCKET_LISTEN_START, 0},
58 : {RA_RS_SOCKET_LISTEN_STOP, 0},
59 : {RA_RS_GET_SOCKET, 0},
60 : {RA_RS_SOCKET_SEND, 0},
61 : {RA_RS_SOCKET_RECV, 0},
62 : {RA_RS_QP_CREATE, 0},
63 : {RA_RS_QP_CREATE_WITH_ATTRS, 0},
64 : {RA_RS_AI_QP_CREATE, 0},
65 : {RA_RS_AI_QP_CREATE_WITH_ATTRS, 0},
66 : {RA_RS_TYPICAL_QP_CREATE, 0},
67 : {RA_RS_QP_DESTROY, 0},
68 : {RA_RS_QP_DESTROY_WITHOUT_CQ, 0},
69 : {RA_RS_QP_CONNECT, 0},
70 : {RA_RS_TYPICAL_QP_MODIFY, 0},
71 : {RA_RS_QP_STATUS, 0},
72 : {RA_RS_QP_INFO, 0},
73 : {RA_RS_QP_BATCH_MODIFY, 0},
74 : {RA_RS_MR_REG, 0},
75 : {RA_RS_MR_DEREG, 0},
76 : {RA_RS_TYPICAL_MR_REG_V1, 0},
77 : {RA_RS_TYPICAL_MR_REG, 0},
78 : {RA_RS_REMAP_MR, 0},
79 : {RA_RS_TYPICAL_MR_DEREG, 0},
80 : {RA_RS_SEND_WR, 0},
81 : {RA_RS_GET_NOTIFY_BA, 0},
82 : {RA_RS_INIT, 0},
83 : {RA_RS_DEINIT, 0},
84 : {RA_RS_SOCKET_INIT, 0},
85 : {RA_RS_SOCKET_DEINIT, 0},
86 : {RA_RS_RDEV_INIT, 0},
87 : {RA_RS_RDEV_INIT_WITH_BACKUP, 0},
88 : {RA_RS_RDEV_GET_PORT_STATUS, 0},
89 : {RA_RS_RDEV_DEINIT, 0},
90 : {RA_RS_WLIST_ADD, 0},
91 : {RA_RS_WLIST_ADD_V2, 0},
92 : {RA_RS_WLIST_DEL, 0},
93 : {RA_RS_WLIST_DEL_V2, 0},
94 : {RA_RS_ACCEPT_CREDIT_ADD, 0},
95 : {RA_RS_GET_IFADDRS, 0},
96 : {RA_RS_GET_IFADDRS_V2, 0},
97 : {RA_RS_GET_INTERFACE_VERSION, 0},
98 : {RA_RS_SEND_WRLIST, 0},
99 : {RA_RS_SEND_WRLIST_V2, 0},
100 : {RA_RS_SEND_WRLIST_EXT, 0},
101 : {RA_RS_SEND_WRLIST_EXT_V2, 0},
102 : {RA_RS_SEND_NORMAL_WRLIST, 0},
103 : {RA_RS_SET_TSQP_DEPTH, 0},
104 : {RA_RS_GET_TSQP_DEPTH, 0},
105 : {RA_RS_SET_QP_ATTR_QOS, 0},
106 : {RA_RS_SET_QP_ATTR_TIMEOUT, 0},
107 : {RA_RS_SET_QP_ATTR_RETRY_CNT, 0},
108 : {RA_RS_GET_CQE_ERR_INFO, 0},
109 : {RA_RS_GET_CQE_ERR_INFO_NUM, 0},
110 : {RA_RS_GET_CQE_ERR_INFO_LIST, 0},
111 : {RA_RS_GET_LITE_SUPPORT, 0},
112 : {RA_RS_GET_LITE_RDEV_CAP, 0},
113 : {RA_RS_GET_LITE_QP_CQ_ATTR, 0},
114 : {RA_RS_GET_LITE_QP_ATTR, 0},
115 : {RA_RS_GET_LITE_CONNECTED_INFO, 0},
116 : {RA_RS_GET_LITE_MEM_ATTR, 0},
117 : {RA_RS_PING_INIT, 0},
118 : {RA_RS_PING_ADD, 0},
119 : {RA_RS_PING_START, 0},
120 : {RA_RS_PING_GET_RESULTS, 0},
121 : {RA_RS_PING_STOP, 0},
122 : {RA_RS_PING_DEL, 0},
123 : {RA_RS_PING_DEINIT, 0},
124 : {RA_RS_GET_VNIC_IP_INFOS_V1, 0},
125 : {RA_RS_GET_VNIC_IP_INFOS, 0},
126 : {RA_RS_TLV_INIT_V1, 0},
127 : {RA_RS_TLV_INIT, 0},
128 : {RA_RS_TLV_DEINIT, 0},
129 : {RA_RS_TLV_REQUEST, 0},
130 : {RA_RS_TLV_REQUEST_V2, 0},
131 : {RA_RS_GET_TLS_ENABLE, 0},
132 : {RA_RS_GET_SEC_RANDOM, 0},
133 : {RA_RS_GET_HCCN_CFG, 0},
134 : {RA_RS_GET_ROCE_API_VERSION, 0},
135 : {RA_RS_TYPICAL_CQ_CREATE, 0},
136 : {RA_RS_GET_LITE_CQ_ATTR, 0},
137 : {RA_RS_QP_CREATE_WITH_CQ_WITH_ATTRS, 0},
138 : {RA_RS_TYPICAL_CQ_DESTROY, 0},
139 :
140 : // outer opcode version: 2.0
141 : {RA_RS_GET_DEV_EID_INFO_NUM, 0},
142 : {RA_RS_GET_DEV_EID_INFO_LIST, 0},
143 : {RA_RS_CTX_INIT, 0},
144 : {RA_RS_CTX_GET_ASYNC_EVENTS, 0},
145 : {RA_RS_CTX_DEINIT, 0},
146 : {RA_RS_GET_EID_BY_IP, 0},
147 : {RA_RS_GET_TP_INFO_LIST, 0},
148 : {RA_RS_GET_TP_ATTR, 0},
149 : {RA_RS_SET_TP_ATTR, 0},
150 : {RA_RS_CTX_TOKEN_ID_ALLOC, 0},
151 : {RA_RS_CTX_TOKEN_ID_FREE, 0},
152 : {RA_RS_LMEM_REG, 0},
153 : {RA_RS_LMEM_UNREG, 0},
154 : {RA_RS_RMEM_IMPORT, 0},
155 : {RA_RS_RMEM_UNIMPORT, 0},
156 : {RA_RS_CTX_CHAN_CREATE, 0},
157 : {RA_RS_CTX_CHAN_DESTROY, 0},
158 : {RA_RS_CTX_CQ_CREATE, 0},
159 : {RA_RS_CTX_CQ_DESTROY, 0},
160 : {RA_RS_CTX_QP_CREATE, 0},
161 : {RA_RS_CTX_QUERY_QP_BATCH, 0},
162 : {RA_RS_CTX_QP_DESTROY, 0},
163 : {RA_RS_CTX_QP_DESTROY_BATCH, 0},
164 : {RA_RS_CTX_QP_IMPORT, 0},
165 : {RA_RS_CTX_QP_UNIMPORT, 0},
166 : {RA_RS_CTX_QP_BIND, 0},
167 : {RA_RS_CTX_QP_UNBIND, 0},
168 : {RA_RS_CTX_BATCH_SEND_WR, 0},
169 : {RA_RS_CTX_UPDATE_CI, 0},
170 : {RA_RS_CTX_GET_AUX_INFO, 0},
171 : {RA_RS_CTX_GET_CR_ERR_INFO_LIST, 0},
172 : {RA_RS_CTX_GET_UB_CONTEXT, 0},
173 : {RA_RS_GET_NET_API_VERSION, 0},
174 :
175 : // inner opcode version
176 : {RA_RS_HDC_SESSION_CLOSE, 0},
177 : {RA_RS_GET_VNIC_IP, 0},
178 : {RA_RS_NOTIFY_CFG_SET, 0},
179 : {RA_RS_NOTIFY_CFG_GET, 0},
180 : {RA_RS_SET_PID, 0},
181 : {RA_RS_ASYNC_HDC_SESSION_CONNECT, 0},
182 : {RA_RS_ASYNC_HDC_SESSION_CLOSE, 0},
183 : };
184 :
185 : STATIC int MsgHeadCheck(struct MsgHead *sendRcvHead, unsigned int opcode, int rsRet, unsigned int msgDataLen);
186 :
187 86 : static int HdcSendRecvPktSend(struct drvHdcMsg *pMsgSnd, char *sendRcvBuf, unsigned int inBufLen, HDC_SESSION session,
188 : struct drvHdcMsg **pMsgRcv)
189 : {
190 : int ret;
191 86 : ret = RA_HDC_OPS.addMsgBuffer(pMsgSnd, sendRcvBuf, inBufLen);
192 86 : CHK_PRT_RETURN(ret != 0, hccp_err("[send][hdc_send_recv_pkt]HDC add msg buffer err ret(%d)", ret), ret);
193 :
194 83 : ret = RA_HDC_OPS.send(session, pMsgSnd, RA_HDC_WAIT_TIMEOUT, RA_HDC_RECV_SEND_TIMEOUT);
195 83 : CHK_PRT_RETURN(ret != 0, hccp_err("[send][hdc_send_recv_pkt]HDC send err ret(%d)", ret), ret);
196 :
197 82 : ret = RA_HDC_OPS.reuseMsg(pMsgSnd);
198 82 : CHK_PRT_RETURN(ret != 0, hccp_err("[send][hdc_send_recv_pkt]HDC reuser msg err ret(%d)", ret), ret);
199 :
200 81 : *pMsgRcv = pMsgSnd;
201 81 : return 0;
202 : }
203 :
204 : #ifndef HNS_ROCE_LLT
205 : STATIC int HdcSendRetryPkt(unsigned int phyId, void *sendRcvBuf, unsigned int inBufLen, struct drvHdcMsg **pMsgRcv)
206 : {
207 : int ret;
208 : struct drvHdcMsg *pMsgSnd = NULL;
209 :
210 : HDC_SESSION session = gRaHdc[phyId].session;
211 : if (session == NULL) {
212 : hccp_err("[send_recv][pkt]session is NULL!, phyId(%u)", phyId);
213 : return -EINVAL;
214 : }
215 :
216 : ret = RA_HDC_OPS.allocMsg(session, &pMsgSnd, 1);
217 : if (ret != 0) {
218 : hccp_err("[send_recv][pkt]HDC alloc msg err ret(%d) phyId(%u)", ret, phyId);
219 : return ret;
220 : }
221 : ret = HdcSendRecvPktSend(pMsgSnd, (char *)sendRcvBuf, inBufLen, session, pMsgRcv);
222 : if (ret != 0) {
223 : hccp_err("[send_recv][pkt]HDC pkt send err ret(%d) phyId(%u)", ret, phyId);
224 : goto msg_err;
225 : }
226 :
227 : return 0;
228 :
229 : msg_err:
230 : RA_HDC_OPS.freeMsg(pMsgSnd);
231 : return ret;
232 : }
233 :
234 : STATIC int RaHdcSendRetryMsg(unsigned int phyId, struct drvHdcMsg **pMsgRcv)
235 : {
236 : int ret;
237 : void *sendRcvBuf = NULL;
238 : unsigned int sendRcvLen;
239 : union OpIfnumData ifnumData;
240 : ifnumData.txData.phyId = phyId;
241 :
242 : pid_t hostTgid = DlDrvDeviceGetBareTgid();
243 : unsigned int dataSize = sizeof(union OpIfnumData);
244 : sendRcvLen = sizeof(struct MsgHead) + dataSize;
245 : sendRcvBuf = (void *)calloc(sendRcvLen, sizeof(char));
246 : CHK_PRT_RETURN(sendRcvBuf == NULL, hccp_err("[process][ra_hdc_msg]send_rcv_buf calloc failed. phyId(%u)", phyId),
247 : -ENOMEM);
248 : MsgHeadBuildUp(sendRcvBuf, RA_RS_GET_IFNUM, 0, dataSize, hostTgid);
249 :
250 : ret = memcpy_s(sendRcvBuf + sizeof(struct MsgHead), sendRcvLen - sizeof(struct MsgHead), &ifnumData, dataSize);
251 : if (ret) {
252 : hccp_err("[process][ra_hdc_msg]memcpy_s failed, ret(%d) phyId(%u)", ret, phyId);
253 : ret = -ESAFEFUNC;
254 : goto out;
255 : }
256 :
257 : ret = HdcSendRetryPkt(phyId, sendRcvBuf, sendRcvLen, pMsgRcv);
258 : if (ret) {
259 : hccp_err("[process][ra_hdc_msg]hdc_send_recv_pkt failed ret(%d) phyId(%u)", ret, phyId);
260 : goto out;
261 : }
262 :
263 : out:
264 : free(sendRcvBuf);
265 : sendRcvBuf = NULL;
266 : return ret;
267 : }
268 :
269 : static int RaHdcRecvRetryMsg(HDC_SESSION session, struct drvHdcMsg *pMsgRcv)
270 : {
271 : int outBufLen = sizeof(struct MsgHead) + sizeof(union OpIfnumData);
272 : char *recvBuf = NULL;
273 : int recvBufCnt = 0;
274 : int rcvBufLen = 0;
275 : int ret;
276 :
277 : ret = RA_HDC_OPS.recv(session, pMsgRcv, MAX_HDC_DATA, RA_HDC_WAIT_TIMEOUT, &recvBufCnt, RA_HDC_RETRY_SEND_TIMEOUT);
278 : if (ret) {
279 : hccp_err("[recv][ra_hdc_recv_retry_msg]HDC get retry recv msg failed(%d)", ret);
280 : return ret;
281 : }
282 :
283 : ret = RA_HDC_OPS.getMsgBuffer(pMsgRcv, 0, &recvBuf, &rcvBufLen);
284 : if (ret) {
285 : hccp_err("[recv][ra_hdc_recv_retry_msg]HDC get retry msg buffer failed(%d)", ret);
286 : return ret;
287 : }
288 :
289 : ret = MsgHeadCheck((struct MsgHead *)recvBuf, RA_RS_GET_IFNUM, 0, sizeof(union OpIfnumData));
290 : if (rcvBufLen != outBufLen || ret != 0) {
291 : hccp_err("[recv][ra_hdc_recv_retry_msg]HDC get retry recv msg failed, ret(%d), rcvBufLen:%d, outBufLen:%d", ret,
292 : rcvBufLen, outBufLen);
293 : if (rcvBufLen != outBufLen) {
294 : ret = -EPIPE;
295 : }
296 : return ret;
297 : }
298 :
299 : return 0;
300 : }
301 : #endif
302 :
303 81 : static int HdcSendRecvPktRecv(HDC_SESSION session, unsigned int phyId, struct drvHdcMsg *pMsgRcv, char **recvBuf,
304 : int *rcvBufLen)
305 : {
306 81 : struct drvHdcMsg *pRetryRcv = NULL;
307 81 : int recvBufCnt = 0;
308 : int ret;
309 :
310 81 : ret = RA_HDC_OPS.recv(session, pMsgRcv, MAX_HDC_DATA, RA_HDC_WAIT_TIMEOUT, &recvBufCnt, RA_HDC_RECV_SEND_TIMEOUT);
311 : #ifndef HNS_ROCE_LLT
312 : /* if timeout, start retry */
313 : if (gRaHdc[phyId].startDeinit == 0 && ret == -DRV_ERROR_WAIT_TIMEOUT) {
314 : hccp_run_info("[recv][hdc_send_recv_pkt_recv]HDC recv timeout, start retry");
315 : ret = RaHdcSendRetryMsg(phyId, &pRetryRcv);
316 : CHK_PRT_RETURN(ret != 0, hccp_err("[recv][hdc_send_recv_pkt_recv]HDC get msg by first retry failed(%d)", ret),
317 : ret);
318 :
319 : ret = RA_HDC_OPS.recv(session, pMsgRcv, MAX_HDC_DATA, RA_HDC_WAIT_TIMEOUT, &recvBufCnt,
320 : RA_HDC_RECV_SEND_TIMEOUT);
321 : if (ret) {
322 : hccp_err("[recv][hdc_send_recv_pkt_recv]HDC get msg by first retry failed(%d)", ret);
323 : RA_HDC_OPS.freeMsg(pRetryRcv);
324 : return ret;
325 : }
326 :
327 : ret = RA_HDC_OPS.getMsgBuffer(pMsgRcv, 0, (char **)recvBuf, rcvBufLen);
328 : if (ret) {
329 : hccp_err("[recv][hdc_send_recv_pkt]HDC get_msg_buffer msg err ret(%d), rcvBufLen(%d)", ret, *rcvBufLen);
330 : RA_HDC_OPS.freeMsg(pRetryRcv);
331 : return ret;
332 : }
333 :
334 : ret = RaHdcRecvRetryMsg(session, pRetryRcv);
335 : if (ret) {
336 : hccp_err("[recv][hdc_send_recv_pkt_recv]HDC recv first retry msg failed(%d)", ret);
337 : }
338 :
339 : RA_HDC_OPS.freeMsg(pRetryRcv);
340 : return ret;
341 : }
342 : #endif
343 81 : CHK_PRT_RETURN(ret != 0, hccp_err("[recv][hdc_send_recv_pkt]HDC recv msg err ret(%d)", ret), ret);
344 :
345 80 : ret = RA_HDC_OPS.getMsgBuffer(pMsgRcv, 0, (char **)recvBuf, rcvBufLen);
346 80 : CHK_PRT_RETURN(ret != 0,
347 : hccp_err("[recv][hdc_send_recv_pkt]HDC get_msg_buffer msg err ret(%d), rcvBufLen(%d)", ret, *rcvBufLen), ret);
348 :
349 79 : return 0;
350 : }
351 :
352 79 : STATIC int HdcSendRecvPktRecvCheck(int rcvBufLen, unsigned int outDataLen, struct MsgHead *recvMsgHead,
353 : struct drvHdcMsg *pMsgRcv)
354 : {
355 : unsigned int rcvBufLenTmp;
356 :
357 79 : rcvBufLenTmp = (unsigned int)rcvBufLen;
358 79 : if (outDataLen != rcvBufLenTmp) {
359 0 : if (recvMsgHead->ret == -EACCES) {
360 0 : hccp_warn("exceed the speed limit, need try again, ret:%d", recvMsgHead->ret);
361 0 : RA_HDC_OPS.freeMsg(pMsgRcv);
362 0 : return -EAGAIN;
363 0 : } else if (recvMsgHead->ret == -EPROTONOSUPPORT) {
364 0 : hccp_err("[check][hdc_send_recv_pkt_recv]unsupported opcode, ret(%d)", recvMsgHead->ret);
365 0 : RA_HDC_OPS.freeMsg(pMsgRcv);
366 0 : return -EPROTONOSUPPORT;
367 0 : } else if (recvMsgHead->ret == -EPERM) {
368 0 : hccp_err("[check][hdc_send_recv_pkt_recv]host pid is invalid, ret(%d)", recvMsgHead->ret);
369 0 : RA_HDC_OPS.freeMsg(pMsgRcv);
370 0 : return -EPERM;
371 : }
372 0 : hccp_err("[check][hdc_send_recv_pkt_recv]date len err out_data_len(%d) != rcv_buf_len(%d) ", outDataLen,
373 : rcvBufLen);
374 0 : RA_HDC_OPS.freeMsg(pMsgRcv);
375 0 : return -EPIPE;
376 : }
377 79 : return 0;
378 : }
379 :
380 234 : STATIC int HdcSendRecvPkt(unsigned int phyId, void *sendRcvBuf, unsigned int inBufLen, unsigned int outBufLen)
381 : {
382 : int ret;
383 234 : char *recvBuf = NULL;
384 234 : struct drvHdcMsg *pMsgRcv = NULL, *pMsgSnd = NULL;
385 234 : int rcvBufLen = 0;
386 :
387 234 : struct MsgHead *recvMsgHead = NULL;
388 :
389 234 : RA_PTHREAD_MUTEX_LOCK(&gRaHdc[phyId].lock);
390 234 : HDC_SESSION session = gRaHdc[phyId].session;
391 234 : if (session == NULL) {
392 147 : hccp_err("[send_recv][pkt]session is NULL!, phyId(%u)", phyId);
393 147 : RA_PTHREAD_MUTEX_UNLOCK(&gRaHdc[phyId].lock);
394 147 : return -EINVAL;
395 : }
396 :
397 : // check last recv status
398 87 : if (gRaHdc[phyId].lastRecvStatus == -DRV_ERROR_WAIT_TIMEOUT) {
399 0 : ret = -DRV_ERROR_WAIT_TIMEOUT;
400 0 : goto alloc_msg_err;
401 : }
402 :
403 87 : ret = RA_HDC_OPS.allocMsg(session, &pMsgSnd, 1);
404 87 : if (ret != 0) {
405 1 : hccp_err("[send_recv][pkt]HDC alloc msg err ret(%d) phyId(%u)", ret, phyId);
406 1 : goto alloc_msg_err;
407 : }
408 86 : ret = HdcSendRecvPktSend(pMsgSnd, (char *)sendRcvBuf, inBufLen, session, &pMsgRcv);
409 86 : if (ret) {
410 5 : hccp_err("[send_recv][pkt]HDC pkt send err ret(%d) phyId(%u)", ret, phyId);
411 5 : goto msg_err;
412 : }
413 81 : ret = HdcSendRecvPktRecv(session, phyId, pMsgRcv, &recvBuf, &rcvBufLen);
414 81 : if (ret == -DRV_ERROR_WAIT_TIMEOUT) {
415 0 : hccp_err("[send_recv][pkt]HDC broken, pkt recv err ret(%d) phyId(%u)", ret, phyId);
416 0 : gRaHdc[phyId].lastRecvStatus = ret;
417 0 : goto msg_err;
418 : }
419 81 : if (ret) {
420 2 : hccp_err("[send_recv][pkt]HDC pkt recv err ret(%d) phyId(%u)", ret, phyId);
421 2 : goto msg_err;
422 : }
423 79 : RA_PTHREAD_MUTEX_UNLOCK(&gRaHdc[phyId].lock);
424 79 : recvMsgHead = (struct MsgHead *)recvBuf;
425 79 : ret = HdcSendRecvPktRecvCheck(rcvBufLen, outBufLen, recvMsgHead, pMsgRcv);
426 79 : CHK_PRT_RETURN(ret, hccp_err("[send_recv][pkt]HDC pkt recv check ret(%d) phyId(%u)", ret, phyId), ret);
427 :
428 79 : ret = memcpy_s(sendRcvBuf, outBufLen, recvBuf, rcvBufLen);
429 79 : if (ret) {
430 0 : hccp_err("[send_recv][pkt]memcpy_s failed, ret(%d) phyId(%u)", ret, phyId);
431 0 : RA_HDC_OPS.freeMsg(pMsgRcv);
432 0 : return -ESAFEFUNC;
433 : }
434 :
435 79 : RA_HDC_OPS.freeMsg(pMsgRcv);
436 79 : return 0;
437 7 : msg_err:
438 7 : RA_HDC_OPS.freeMsg(pMsgSnd);
439 8 : alloc_msg_err:
440 8 : RA_PTHREAD_MUTEX_UNLOCK(&gRaHdc[phyId].lock);
441 8 : return ret;
442 : }
443 :
444 293 : void MsgHeadBuildUp(struct MsgHead *pSendRcvHead, unsigned int opcode, unsigned int reqId, unsigned int msgDataLen,
445 : pid_t hostTgid)
446 : {
447 293 : pSendRcvHead->opcode = opcode;
448 293 : pSendRcvHead->ret = 0;
449 293 : pSendRcvHead->asyncReqId = reqId;
450 293 : pSendRcvHead->msgDataLen = msgDataLen;
451 293 : pSendRcvHead->hostTgid = hostTgid;
452 :
453 293 : return;
454 : }
455 :
456 124 : STATIC int MsgHeadCheck(struct MsgHead *sendRcvHead, unsigned int opcode, int rsRet, unsigned int msgDataLen)
457 : {
458 : unsigned int ret;
459 :
460 : /* return rs real return value */
461 124 : if (sendRcvHead->ret < rsRet) {
462 3 : return sendRcvHead->ret;
463 : }
464 :
465 121 : ret = (sendRcvHead->opcode != opcode) || (sendRcvHead->msgDataLen != msgDataLen);
466 :
467 121 : return (ret != 0 ? -EPERM : 0);
468 : }
469 :
470 296 : int RaHdcProcessMsg(unsigned int opcode, unsigned int phyId, char *data, unsigned int dataSize)
471 : {
472 296 : pid_t hostTgid = DlDrvDeviceGetBareTgid();
473 296 : void *sendRcvBuf = NULL;
474 : unsigned int sendRcvLen;
475 : int ret;
476 :
477 296 : if (gRaHdc[phyId].restoreFlag != 0) {
478 0 : return 0;
479 : }
480 :
481 296 : sendRcvLen = sizeof(struct MsgHead) + dataSize;
482 296 : CHK_PRT_RETURN(data == NULL, hccp_err("[process][ra_hdc_msg]data is NULL. phyId(%u)", phyId), -EINVAL);
483 296 : sendRcvBuf = (void *)calloc(sendRcvLen, sizeof(char));
484 296 : CHK_PRT_RETURN(sendRcvBuf == NULL, hccp_err("[process][ra_hdc_msg]send_rcv_buf calloc failed. phyId(%u)", phyId),
485 : -ENOMEM);
486 289 : MsgHeadBuildUp(sendRcvBuf, opcode, 0, dataSize, hostTgid);
487 289 : ret = memcpy_s(sendRcvBuf + sizeof(struct MsgHead), sendRcvLen - sizeof(struct MsgHead), data, dataSize);
488 289 : if (ret) {
489 2 : hccp_err("[process][ra_hdc_msg]memcpy_s failed, ret(%d) phyId(%u)", ret, phyId);
490 2 : ret = -ESAFEFUNC;
491 2 : goto out;
492 : }
493 :
494 287 : ret = HdcSendRecvPkt(phyId, sendRcvBuf, sendRcvLen, sendRcvLen);
495 287 : if (ret) {
496 158 : hccp_err("[process][ra_hdc_msg]hdc_send_recv_pkt opcode(%u) failed ret(%d) phyId(%u)", opcode, ret, phyId);
497 158 : goto out;
498 : }
499 :
500 129 : ret = MsgHeadCheck(sendRcvBuf, opcode, 0, dataSize);
501 : // opcode RA_RS_SOCKET_RECV not to print EAGAIN to avoid log flush
502 129 : if ((ret != 0) && (ret != -EAGAIN || opcode != RA_RS_SOCKET_RECV)) {
503 : /* maybe has retry return value, record warning log */
504 7 : hccp_warn("message head check unsuccessful, ret[%d] phyId[%u]", ret, phyId);
505 : }
506 :
507 129 : if (memcpy_s(data, dataSize, sendRcvBuf + sizeof(struct MsgHead), dataSize)) {
508 0 : hccp_err("[process][ra_hdc_msg]memcpy_s failed. dest size(%d) ret(%d) phyId(%u)", dataSize, ret, phyId);
509 0 : ret = -ESAFEFUNC;
510 : }
511 129 : out:
512 289 : free(sendRcvBuf);
513 289 : sendRcvBuf = NULL;
514 289 : return ret;
515 : }
516 :
517 0 : int HdcAsyncSendPkt(struct HdcAsyncInfo *asyncInfo, unsigned int phyId, void *sendBuf, unsigned int sendLen)
518 : {
519 0 : struct drvHdcMsg *pMsgSnd = NULL;
520 0 : HDC_SESSION session = NULL;
521 0 : int ret = 0;
522 :
523 0 : RA_PTHREAD_MUTEX_LOCK(&asyncInfo->sendMutex);
524 0 : session = asyncInfo->session;
525 0 : if (session == NULL) {
526 0 : RA_PTHREAD_MUTEX_UNLOCK(&asyncInfo->sendMutex);
527 0 : hccp_err("[async][send_pkt]session is NULL!, phyId(%u)", phyId);
528 0 : return -EINVAL;
529 : }
530 :
531 0 : ret = RA_HDC_OPS.allocMsg(session, &pMsgSnd, 1);
532 0 : if (ret != 0) {
533 0 : hccp_err("[async][send_pkt]HDC alloc msg err ret(%d) phyId(%u)", ret, phyId);
534 0 : goto alloc_msg_err;
535 : }
536 :
537 0 : ret = RA_HDC_OPS.addMsgBuffer(pMsgSnd, sendBuf, (int)sendLen);
538 0 : if (ret != 0) {
539 0 : hccp_err("[async][send_pkt]HDC add msg buffer err ret(%d) phyId(%u)", ret, phyId);
540 0 : goto msg_err;
541 : }
542 :
543 0 : ret = RA_HDC_OPS.send(session, pMsgSnd, RA_HDC_WAIT_TIMEOUT, RA_HDC_RECV_SEND_TIMEOUT);
544 0 : if (ret != 0) {
545 0 : hccp_err("[async][send_pkt]HDC send err ret(%d) phyId(%u)", ret, phyId);
546 0 : goto msg_err;
547 : }
548 :
549 0 : msg_err:
550 0 : RA_HDC_OPS.freeMsg(pMsgSnd);
551 0 : alloc_msg_err:
552 0 : RA_PTHREAD_MUTEX_UNLOCK(&asyncInfo->sendMutex);
553 0 : return ret;
554 : }
555 :
556 2 : STATIC int HdcAsyncPrepareRecvPkt(struct HdcAsyncInfo *asyncInfo, unsigned int phyId, HDC_SESSION *session,
557 : struct drvHdcMsg **pMsgRcv)
558 : {
559 2 : int ret = 0;
560 :
561 2 : *session = asyncInfo->session;
562 2 : if (*session == NULL) {
563 0 : hccp_err("[async][recv_pkt]session is NULL!, phyId(%u)", phyId);
564 0 : return -EINVAL;
565 : }
566 :
567 : // check last recv status
568 2 : if (RaHdcIsBroken(asyncInfo->lastRecvStatus)) {
569 1 : return asyncInfo->lastRecvStatus;
570 : }
571 :
572 1 : ret = RA_HDC_OPS.allocMsg(*session, pMsgRcv, 1);
573 1 : if (ret != 0) {
574 0 : hccp_err("[async][recv_pkt]HDC alloc msg err ret(%d) phyId(%u)", ret, phyId);
575 0 : return ret;
576 : }
577 :
578 1 : return 0;
579 : }
580 :
581 2 : int HdcAsyncRecvPkt(struct HdcAsyncInfo *asyncInfo, unsigned int phyId, void *recvBuf, unsigned int *recvLen)
582 : {
583 2 : struct drvHdcMsg *pMsgRcv = NULL;
584 2 : HDC_SESSION session = NULL;
585 2 : int recvBufCnt = 0;
586 2 : char *rcvBuf = NULL;
587 2 : int rcvLen = 0;
588 2 : int ret = 0;
589 :
590 2 : ret = HdcAsyncPrepareRecvPkt(asyncInfo, phyId, &session, &pMsgRcv);
591 2 : if (ret != 0) {
592 1 : goto session_err;
593 : }
594 :
595 1 : ret = RA_HDC_OPS.recv(session, pMsgRcv, MAX_HDC_DATA, RA_HDC_WAIT_TIMEOUT, &recvBufCnt, RA_HDC_RECV_SEND_TIMEOUT);
596 : // occur hdc time out when async session was closed before async request done
597 1 : if (ret == -DRV_ERROR_WAIT_TIMEOUT) {
598 0 : hccp_run_warn("[async][recv_pkt]HDC recv timeout, phyId(%u)", phyId);
599 0 : goto msg_err;
600 : }
601 :
602 1 : if (ret != 0) {
603 1 : hccp_err("[async][recv_pkt]HDC recv err ret(%d) phyId(%u)", ret, phyId);
604 1 : asyncInfo->lastRecvStatus = ret;
605 1 : goto msg_err;
606 : }
607 :
608 0 : ret = RA_HDC_OPS.getMsgBuffer(pMsgRcv, 0, (char **)&rcvBuf, &rcvLen);
609 0 : if (ret != 0 || rcvLen <= 0) {
610 0 : hccp_err("[async][recv_pkt]HDC get_msg_buffer err ret(%d) phyId(%u) rcv_len(%d)", ret, phyId, rcvLen);
611 0 : goto msg_err;
612 : }
613 :
614 0 : ret = memcpy_s(recvBuf, *recvLen, rcvBuf, (unsigned int)rcvLen);
615 0 : if (ret != 0) {
616 0 : hccp_err("[async][recv_pkt]memcpy_s failed, ret(%d) phyId(%u)", ret, phyId);
617 0 : RA_HDC_OPS.freeMsg(pMsgRcv);
618 0 : return -ESAFEFUNC;
619 : }
620 :
621 0 : *recvLen = (unsigned int)rcvLen;
622 0 : RA_HDC_OPS.freeMsg(pMsgRcv);
623 0 : return 0;
624 :
625 1 : msg_err:
626 1 : RA_HDC_OPS.freeMsg(pMsgRcv);
627 2 : session_err:
628 2 : return ret;
629 : }
630 :
631 58 : int RaHdcGetInterfaceVersion(unsigned int phyId, unsigned int interfaceOpcode, unsigned int *interfaceVersion)
632 : {
633 58 : int num = sizeof(gRaInterfaceInfoList) / sizeof(gRaInterfaceInfoList[0]);
634 : int i;
635 :
636 58 : CHK_PRT_RETURN(interfaceVersion == NULL || phyId >= RA_MAX_PHY_ID_NUM,
637 : hccp_err("[get][ra_interface_version]para invalid! interface_version is NULL or phyId(%u) >= [%u]", phyId,
638 : RA_MAX_PHY_ID_NUM),
639 : -EINVAL);
640 :
641 58 : *interfaceVersion = 0;
642 2397 : for (i = 0; i < num; i++) {
643 2393 : if (gRaInterfaceInfoList[i].opcode == interfaceOpcode) {
644 54 : *interfaceVersion = gRaInterfaceInfoList[i].version;
645 54 : break;
646 : }
647 : }
648 58 : return 0;
649 : }
650 :
651 125 : STATIC int RaHdcGetOpcodeVersion(unsigned int phyId, unsigned int interfaceOpcode, unsigned int *interfaceVersion)
652 : {
653 125 : union OpGetVersionData versionInfo = {0};
654 : int ret;
655 :
656 125 : versionInfo.txData.opcode = interfaceOpcode;
657 :
658 125 : ret = RaHdcProcessMsg(RA_RS_GET_INTERFACE_VERSION, phyId, (char *)&versionInfo, sizeof(union OpGetVersionData));
659 125 : CHK_PRT_RETURN(ret,
660 : hccp_err("[get][ra_hdc_interface_version]ra hdc message process failed ret(%d) phyId(%u)", ret, phyId), ret);
661 :
662 0 : *interfaceVersion = versionInfo.rxData.version;
663 0 : return 0;
664 : }
665 :
666 1 : void RaHdcGetAllOpcodeVersion(unsigned int phyId)
667 : {
668 1 : int num = sizeof(gRaInterfaceInfoList) / sizeof(gRaInterfaceInfoList[0]);
669 : int ret;
670 : int i;
671 :
672 126 : for (i = 0; i < num; i++) {
673 125 : ret = RaHdcGetOpcodeVersion(phyId, gRaInterfaceInfoList[i].opcode, &gRaInterfaceInfoList[i].version);
674 125 : if (ret != 0) {
675 125 : hccp_warn("ra_hdc_get_opcode_version unsuccessful, ret[%d], opcode[%d]", ret,
676 : gRaInterfaceInfoList[i].opcode);
677 125 : continue;
678 : }
679 : }
680 :
681 1 : return;
682 : }
683 :
684 0 : bool RaHdcHasCapability(unsigned int phyId, unsigned int capability)
685 : {
686 0 : unsigned int roceVersion = 0;
687 0 : unsigned int netVersion = 0;
688 0 : bool hasCapability = false;
689 :
690 : // return value is ignored as this function is guaranteed to succeed
691 0 : (void)RaHdcGetInterfaceVersion(phyId, RA_RS_GET_ROCE_API_VERSION, &roceVersion);
692 0 : (void)RaHdcGetInterfaceVersion(phyId, RA_RS_GET_NET_API_VERSION, &netVersion);
693 :
694 0 : hasCapability = RaRsHasCapability(capability, roceVersion, netVersion);
695 0 : if (!hasCapability) {
696 0 : hccp_run_info("phy_id:%u capability:%u roceVersion:0x%x netVersion:0x%x hasCapability:%d RA_CAP_INVALID:%u",
697 : phyId, capability, roceVersion, netVersion, hasCapability, RA_CAP_INVALID);
698 : }
699 0 : return hasCapability;
700 : }
701 :
702 27 : STATIC int RaHdcSendPid(unsigned int phyId, struct ProcessRaSign pRaSign)
703 : {
704 27 : union OpSetPidData setPidData = {0};
705 : int ret;
706 :
707 27 : setPidData.txData.pid = pRaSign.tgid;
708 27 : setPidData.txData.phyId = phyId;
709 :
710 27 : ret = strcpy_s(setPidData.txData.pidSign, PROCESS_RA_SIGN_LENGTH, pRaSign.sign);
711 27 : CHK_PRT_RETURN(ret, hccp_err("[send][ra_hdc_pid]Invalid pid sign, ret(%d)", ret), -ESAFEFUNC);
712 :
713 27 : ret = RaHdcProcessMsg(RA_RS_SET_PID, phyId, (char *)&setPidData, sizeof(union OpSetPidData));
714 27 : CHK_PRT_RETURN(ret, hccp_err("[send][ra_hdc_pid]ra hdc message process failed ret(%d) phyId(%u)", ret, phyId), ret);
715 :
716 27 : return 0;
717 : }
718 :
719 32 : STATIC int RaHdcInitApart(unsigned int phyId, unsigned int *logicId)
720 : {
721 : int ret;
722 32 : ret = DlDrvDeviceGetIndexByPhyId(phyId, logicId);
723 32 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc_apart]get logic id failed(%d), phyId(%u)", ret, phyId), -ENODEV);
724 :
725 32 : ret = pthread_mutex_init(&gRaHdc[phyId].lock, NULL);
726 32 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc_apart]pthread_mutex_init failed, ret(%d) phyId(%u)", ret, phyId),
727 : -ESYSFUNC);
728 30 : return 0;
729 : }
730 :
731 0 : STATIC int RaHdcInitSessionConnectEx(int peerNode, int peerDevid, unsigned int phyId, HDC_SESSION *session)
732 : {
733 0 : struct halQueryDevpidInfo info = {0};
734 : pid_t devPid;
735 : int ret;
736 :
737 0 : info.hostpid = getpid();
738 0 : info.devid = (unsigned int)peerDevid;
739 0 : info.proc_type = DEVDRV_PROCESS_HCCP;
740 0 : ret = DlHalQueryDevPid(info, &devPid);
741 0 : if (ret != 0) {
742 0 : hccp_err("[init][ra_hdc]hdc dl_hal_query_dev_pid failed ret(%d) peer_devid(%d) phyId(%u)", ret, peerDevid,
743 : phyId);
744 0 : return ret;
745 : }
746 :
747 0 : return RA_HDC_OPS.sessionConnectEx(peerNode, peerDevid, devPid, gRaHdc[phyId].client, session);
748 : }
749 :
750 29 : int RaHdcInitSession(int peerNode, int peerDevid, unsigned int phyId, int hdcType, HDC_SESSION *session)
751 : {
752 29 : if (hdcType == HDC_SERVICE_TYPE_RDMA_V2) {
753 0 : return RaHdcInitSessionConnectEx(peerNode, peerDevid, phyId, session);
754 : }
755 :
756 : // default hdc type: HDC_SERVICE_TYPE_RDMA
757 29 : return RA_HDC_OPS.sessionConnect(peerNode, peerDevid, gRaHdc[phyId].client, session);
758 : }
759 :
760 56 : void RaHdcDeinitSession(HDC_SESSION *session)
761 : {
762 56 : if (*session == NULL) {
763 29 : return;
764 : }
765 :
766 27 : (void)RA_HDC_OPS.sessionClose(*session);
767 27 : *session = NULL;
768 27 : return;
769 : }
770 :
771 0 : int RaHdcSetSessionReference(HDC_SESSION *session)
772 : {
773 0 : return RA_HDC_OPS.setSessionReference(*session);
774 : }
775 :
776 33 : int RaHdcInit(struct RaInitConfig *cfg, struct ProcessRaSign pRaSign)
777 : {
778 33 : unsigned int logicId = RA_MAX_PHY_ID_NUM;
779 33 : unsigned int phyId = cfg->phyId;
780 33 : int hdcType = cfg->hdcType;
781 : int ret;
782 :
783 33 : ret = DlHalInit();
784 33 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc]dl_hal_init failed, ret = %d", ret), ret);
785 :
786 33 : ret = RaHdcInitApart(phyId, &logicId);
787 33 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc]ra_hdc_init_apart failed, ret(%d)", ret), ret);
788 :
789 30 : hccp_run_info("hdc init start! logic id is %u, phy id is %u, hdcType is %d", logicId, phyId, hdcType);
790 :
791 30 : if (gRaHdc[phyId].session == NULL) {
792 : // maxSessionNum 2U include: sync & async session
793 29 : ret = RA_HDC_OPS.clientCreate(&gRaHdc[phyId].client, 2U, hdcType, 0);
794 29 : if (ret != 0) {
795 0 : hccp_err("[init][ra_hdc]hdc client create failed, hccp not up ret(%d) phyId(%u)", ret, phyId);
796 0 : goto HDC_ERR;
797 : }
798 29 : ret = RaHdcInitSession(0, (int)logicId, phyId, hdcType, &gRaHdc[phyId].session);
799 29 : if (ret != 0) {
800 1 : hccp_err("[init][ra_hdc]hdc session_connect failed ret(%d) logic_id(%u) phyId(%u)", ret, logicId, phyId);
801 1 : goto CONN_ERR;
802 : }
803 28 : ret = RA_HDC_OPS.setSessionReference(gRaHdc[phyId].session);
804 28 : if (ret != 0) {
805 1 : hccp_err("[init][ra_hdc]hdc set_session_reference failed, ret(%d) phyId(%u)", ret, phyId);
806 1 : goto SESS_ERR;
807 : }
808 : } else {
809 1 : hccp_warn("hdc session for phyId[%u] already existed", phyId);
810 1 : return -EEXIST;
811 : }
812 :
813 27 : ret = RaHdcSendPid(phyId, pRaSign);
814 27 : if (ret) {
815 0 : hccp_err("[init][ra_hdc]set pid for phyId(%u) failed, ret(%d), hostTgid(%d)", phyId, ret, pRaSign.tgid);
816 0 : goto SESS_ERR;
817 : }
818 :
819 27 : ret = RaHdcLiteInitCqeErrInfo(phyId);
820 27 : if (ret) {
821 0 : hccp_err("[init][ra_hdc]ra_hdc_lite_init_cqe_err_info failed, ret(%d)", ret);
822 0 : goto SESS_ERR;
823 : }
824 :
825 27 : hccp_run_info("hdc init OK! phyId[%u]", phyId);
826 :
827 27 : return 0;
828 1 : SESS_ERR:
829 1 : RA_HDC_OPS.sessionClose(gRaHdc[phyId].session);
830 1 : gRaHdc[phyId].session = NULL;
831 2 : CONN_ERR:
832 2 : RA_HDC_OPS.clientDestroy(gRaHdc[phyId].client);
833 2 : gRaHdc[phyId].client = NULL;
834 2 : HDC_ERR:
835 2 : pthread_mutex_destroy(&gRaHdc[phyId].lock);
836 2 : return -EPERM;
837 : }
838 :
839 1 : int RaHdcGetTlsEnable(unsigned int phyId, bool *tlsEnable)
840 : {
841 1 : union OpGetTlsEnableData opData = {0};
842 : int ret;
843 :
844 1 : opData.txData.phyId = phyId;
845 1 : ret = RaHdcProcessMsg(RA_RS_GET_TLS_ENABLE, phyId, (char *)&opData, sizeof(union OpGetTlsEnableData));
846 1 : CHK_PRT_RETURN(ret != 0, hccp_err("[get][tls_enable]ra hdc message process failed ret(%d) phyId(%u)", ret, phyId),
847 : ret);
848 :
849 1 : *tlsEnable = opData.rxData.tlsEnable;
850 1 : return ret;
851 : }
852 :
853 26 : STATIC int RaHdcSessionClose(unsigned int phyId)
854 : {
855 26 : union OpHdcCloseData hdcCloseData = {0};
856 : int ret;
857 :
858 26 : hdcCloseData.txData.phyId = phyId;
859 :
860 26 : ret = RaHdcProcessMsg(RA_RS_HDC_SESSION_CLOSE, phyId, (char *)&hdcCloseData, sizeof(union OpHdcCloseData));
861 26 : CHK_PRT_RETURN(ret, hccp_err("[close][ra_hdc_session]ra hdc message process failed ret(%d) phyId(%u)", ret, phyId),
862 : ret);
863 :
864 25 : return 0;
865 : }
866 :
867 28 : STATIC int RaHdcClientDeinit(unsigned int phyId)
868 : {
869 : int ret;
870 :
871 28 : gRaHdc[phyId].startDeinit = 1;
872 28 : ret = RaHdcSessionClose(phyId);
873 28 : if (ret) {
874 1 : hccp_err("[deinit][ra_hdc]close hdc session failed, ret(%d) phyId(%u)", ret, phyId);
875 : }
876 28 : RaHdcDeinitSession(&gRaHdc[phyId].snapshotSession);
877 28 : RaHdcDeinitSession(&gRaHdc[phyId].session);
878 :
879 28 : ret = RA_HDC_OPS.clientDestroy(gRaHdc[phyId].client);
880 28 : if (ret) {
881 1 : hccp_err("[deinit][ra_hdc]hdc client_destroy failed, ret(%d) phyId(%u)", ret, phyId);
882 : }
883 28 : gRaHdc[phyId].client = NULL;
884 28 : gRaHdc[phyId].startDeinit = 0;
885 :
886 28 : return ret;
887 : }
888 :
889 28 : int RaHdcDeinit(struct RaInitConfig *cfg)
890 : {
891 28 : unsigned int phyId = cfg->phyId;
892 : int ret;
893 :
894 28 : hccp_run_info("hdc deinit start! phyId[%u] restore_flag[%u]", phyId, gRaHdc[phyId].restoreFlag);
895 :
896 28 : RaHdcLiteDeinitCqeErrInfo(phyId);
897 :
898 28 : ret = RaHdcClientDeinit(phyId);
899 28 : if (ret != 0) {
900 1 : hccp_err("[deinit][ra_hdc]client deinit failed! ret(%d) phyId(%u)", ret, phyId);
901 : }
902 :
903 28 : ret = pthread_mutex_destroy(&gRaHdc[phyId].lock);
904 28 : if (ret != 0) {
905 1 : hccp_err("[deinit][ra_hdc]pthread_mutex_destroy failed! ret(%d) phyId(%u)", ret, phyId);
906 1 : ret = -ESYSFUNC;
907 : }
908 :
909 28 : CHK_PRT_RETURN(ret != 0, hccp_err("[deinit][ra_hdc]hdc deinit failed! phyId(%u)", phyId), ret);
910 27 : DlHalDeinit();
911 :
912 27 : (void)memset_s(&gRaHdc[phyId], sizeof(gRaHdc[phyId]), 0, sizeof(gRaHdc[phyId]));
913 :
914 27 : hccp_run_info("hdc deinit OK! phyId[%u]", phyId);
915 27 : return 0;
916 : }
917 :
918 0 : STATIC int RaHdcGetValidCqeErrInfo(struct CqeErrInfo *outInfo, struct CqeErrInfo info0, struct CqeErrInfo info1)
919 : {
920 : int ret;
921 :
922 0 : if (info0.status != 0 && info1.status != 0) {
923 0 : if (timercmp((&info0.time), (&info1.time), <)) {
924 0 : ret = memcpy_s(outInfo, sizeof(struct CqeErrInfo), &info0, sizeof(struct CqeErrInfo));
925 : } else {
926 0 : ret = memcpy_s(outInfo, sizeof(struct CqeErrInfo), &info1, sizeof(struct CqeErrInfo));
927 : }
928 : } else {
929 0 : if (info0.status == 0) {
930 0 : ret = memcpy_s(outInfo, sizeof(struct CqeErrInfo), &info1, sizeof(struct CqeErrInfo));
931 : } else {
932 0 : ret = memcpy_s(outInfo, sizeof(struct CqeErrInfo), &info0, sizeof(struct CqeErrInfo));
933 : }
934 : }
935 :
936 0 : if (ret) {
937 0 : hccp_err("memcpy_s failed, ret(%d)", ret);
938 0 : return -ESAFEFUNC;
939 : }
940 :
941 0 : return 0;
942 : }
943 :
944 0 : int RaHdcGetCqeErrInfo(unsigned int phyId, struct CqeErrInfo *info)
945 : {
946 : int ret;
947 0 : struct CqeErrInfo opCqeInfo = {0};
948 : union OpGetCqeErrInfoData cqeErrInfoData;
949 :
950 0 : RaHdcLiteGetCqeErrInfo(phyId, &opCqeInfo);
951 :
952 0 : RA_PTHREAD_MUTEX_LOCK(&gRaHdc[phyId].lock);
953 0 : HDC_SESSION session = gRaHdc[phyId].session;
954 0 : if (session == NULL) {
955 0 : RA_PTHREAD_MUTEX_UNLOCK(&gRaHdc[phyId].lock);
956 0 : return 0;
957 : }
958 0 : RA_PTHREAD_MUTEX_UNLOCK(&gRaHdc[phyId].lock);
959 :
960 0 : ret = memset_s(&cqeErrInfoData, sizeof(cqeErrInfoData), 0, sizeof(cqeErrInfoData));
961 0 : CHK_PRT_RETURN(ret, hccp_err("[init]memset_s failed ret(%d)", ret), -ESAFEFUNC);
962 0 : ret = RaHdcProcessMsg(RA_RS_GET_CQE_ERR_INFO, phyId, (char *)&cqeErrInfoData, sizeof(union OpGetCqeErrInfoData));
963 0 : CHK_PRT_RETURN(ret, hccp_err("ra hdc message process failed ret(%d)", ret), ret);
964 :
965 0 : return RaHdcGetValidCqeErrInfo(info, opCqeInfo, cqeErrInfoData.rxData.info);
966 : }
967 :
968 0 : STATIC int RaHdcSessionSaveSnapshot(unsigned int phyId, enum SaveSnapshotAction action)
969 : {
970 0 : int ret = 0;
971 :
972 0 : RA_PTHREAD_MUTEX_LOCK(&gRaHdc[phyId].lock);
973 0 : if (action == SAVE_SNAPSHOT_ACTION_PRE_PROCESSING && gRaHdc[phyId].session != NULL) {
974 0 : gRaHdc[phyId].snapshotSession = gRaHdc[phyId].session;
975 0 : gRaHdc[phyId].session = NULL;
976 0 : } else if (action == SAVE_SNAPSHOT_ACTION_POST_PROCESSING && gRaHdc[phyId].session == NULL) {
977 0 : gRaHdc[phyId].session = gRaHdc[phyId].snapshotSession;
978 0 : gRaHdc[phyId].snapshotSession = NULL;
979 : } else {
980 0 : hccp_err("duplicate or incorrect order calls are not allowed, phyId[%u] action[%d]", phyId, action);
981 0 : ret = -EPERM;
982 : }
983 0 : RA_PTHREAD_MUTEX_UNLOCK(&gRaHdc[phyId].lock);
984 :
985 0 : return ret;
986 : }
987 :
988 0 : int RaHdcSaveSnapshot(unsigned int phyId, enum SaveSnapshotAction action)
989 : {
990 : int ret;
991 :
992 0 : ret = RaHdcAsyncSaveSnapshot(phyId, action);
993 0 : CHK_PRT_RETURN(ret != 0, hccp_err("ra_hdc_async_save_snapshot failed ret[%d]", ret), ret);
994 0 : ret = RaHdcSessionSaveSnapshot(phyId, action);
995 0 : CHK_PRT_RETURN(ret != 0, hccp_err("ra_hdc_session_save_snapshot failed ret[%d]", ret), ret);
996 :
997 0 : return ret;
998 : }
999 :
1000 0 : STATIC void RaHdcSessionRestoreSnapshot(unsigned int phyId)
1001 : {
1002 0 : RA_PTHREAD_MUTEX_LOCK(&gRaHdc[phyId].lock);
1003 0 : gRaHdc[phyId].restoreFlag = 1;
1004 0 : RA_PTHREAD_MUTEX_UNLOCK(&gRaHdc[phyId].lock);
1005 0 : }
1006 :
1007 0 : int RaHdcRestoreSnapshot(unsigned int phyId)
1008 : {
1009 : int ret;
1010 :
1011 0 : ret = RaHdcAsyncRestoreSnapshot(phyId);
1012 0 : CHK_PRT_RETURN(ret != 0, hccp_err("ra_hdc_async_restore_snapshot failed ret[%d]", ret), ret);
1013 0 : RaHdcSessionRestoreSnapshot(phyId);
1014 :
1015 0 : return ret;
1016 : }
1017 :
1018 1 : int RaHdcGetSecRandom(unsigned int phyId, unsigned int *value)
1019 : {
1020 1 : union OpGetSecRandomData opData = {0};
1021 : int ret;
1022 :
1023 1 : ret = RaHdcProcessMsg(RA_RS_GET_SEC_RANDOM, phyId, (char *)&opData, sizeof(union OpGetSecRandomData));
1024 1 : CHK_PRT_RETURN(ret != 0, hccp_err("[get][sec_random]ra hdc message process failed ret(%d) phyId(%u)", ret, phyId),
1025 : ret);
1026 :
1027 1 : *value = opData.rxData.value;
1028 1 : return ret;
1029 : }
1030 :
1031 1 : int RaHdcGetHccnCfg(unsigned int phyId, enum HccnCfgKey key, char *value, unsigned int *valueLen)
1032 : {
1033 1 : union OpGetHccnCfgData opData = {0};
1034 : int ret;
1035 :
1036 1 : opData.txData.phyId = phyId;
1037 1 : opData.txData.key = key;
1038 1 : ret = RaHdcProcessMsg(RA_RS_GET_HCCN_CFG, phyId, (char *)&opData, sizeof(union OpGetHccnCfgData));
1039 1 : CHK_PRT_RETURN(ret != 0, hccp_err("[get][hccn_cfg]ra hdc message process failed ret(%d) phyId(%u)", ret, phyId),
1040 : ret);
1041 :
1042 1 : ret = memcpy_s(value, *valueLen, opData.rxData.value, opData.rxData.valueLen);
1043 1 : CHK_PRT_RETURN(ret != 0, hccp_err("[get][hccn_cfg]ra hdc message process failed ret(%d) phyId(%u)", ret, phyId),
1044 : ret);
1045 :
1046 1 : *valueLen = opData.rxData.valueLen;
1047 1 : return ret;
1048 : }
|