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,
188 : HDC_SESSION session, 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(
206 : unsigned int phyId, void *sendRcvBuf, unsigned int inBufLen, struct drvHdcMsg **pMsgRcv)
207 : {
208 : int ret;
209 : struct drvHdcMsg *pMsgSnd = NULL;
210 :
211 : HDC_SESSION session = gRaHdc[phyId].session;
212 : if (session == NULL) {
213 : hccp_err("[send_recv][pkt]session is NULL!, phyId(%u)", phyId);
214 : return -EINVAL;
215 : }
216 :
217 : ret = RA_HDC_OPS.allocMsg(session, &pMsgSnd, 1);
218 : if (ret != 0) {
219 : hccp_err("[send_recv][pkt]HDC alloc msg err ret(%d) phyId(%u)", ret, phyId);
220 : return ret;
221 : }
222 : ret = HdcSendRecvPktSend(pMsgSnd, (char *)sendRcvBuf, inBufLen, session, pMsgRcv);
223 : if (ret != 0) {
224 : hccp_err("[send_recv][pkt]HDC pkt send err ret(%d) phyId(%u)", ret, phyId);
225 : goto msg_err;
226 : }
227 :
228 : return 0;
229 :
230 : msg_err:
231 : RA_HDC_OPS.freeMsg(pMsgSnd);
232 : return ret;
233 : }
234 :
235 : STATIC int RaHdcSendRetryMsg(unsigned int phyId, struct drvHdcMsg **pMsgRcv)
236 : {
237 : int ret;
238 : void *sendRcvBuf = NULL;
239 : unsigned int sendRcvLen;
240 : union OpIfnumData ifnumData;
241 : ifnumData.txData.phyId = phyId;
242 :
243 : pid_t hostTgid = DlDrvDeviceGetBareTgid();
244 : unsigned int dataSize = sizeof(union OpIfnumData);
245 : sendRcvLen = sizeof(struct MsgHead) + dataSize;
246 : sendRcvBuf = (void *)calloc(sendRcvLen, sizeof(char));
247 : CHK_PRT_RETURN(sendRcvBuf == NULL, hccp_err("[process][ra_hdc_msg]send_rcv_buf calloc failed. phyId(%u)",
248 : phyId), -ENOMEM);
249 : MsgHeadBuildUp(sendRcvBuf, RA_RS_GET_IFNUM, 0, dataSize, hostTgid);
250 :
251 : ret = memcpy_s(
252 : sendRcvBuf + sizeof(struct MsgHead), sendRcvLen - sizeof(struct MsgHead), &ifnumData, dataSize);
253 : if (ret) {
254 : hccp_err("[process][ra_hdc_msg]memcpy_s failed, ret(%d) phyId(%u)", ret, phyId);
255 : ret = -ESAFEFUNC;
256 : goto out;
257 : }
258 :
259 : ret = HdcSendRetryPkt(phyId, sendRcvBuf, sendRcvLen, pMsgRcv);
260 : if (ret) {
261 : hccp_err("[process][ra_hdc_msg]hdc_send_recv_pkt failed ret(%d) phyId(%u)", ret, phyId);
262 : goto out;
263 : }
264 :
265 : out:
266 : free(sendRcvBuf);
267 : sendRcvBuf = NULL;
268 : return ret;
269 : }
270 :
271 : static int RaHdcRecvRetryMsg(HDC_SESSION session, struct drvHdcMsg *pMsgRcv)
272 : {
273 : int outBufLen = sizeof(struct MsgHead) + sizeof(union OpIfnumData);
274 : char *recvBuf = NULL;
275 : int recvBufCnt = 0;
276 : int rcvBufLen = 0;
277 : int ret;
278 :
279 : ret = RA_HDC_OPS.recv(
280 : session, pMsgRcv, MAX_HDC_DATA, RA_HDC_WAIT_TIMEOUT, &recvBufCnt, RA_HDC_RETRY_SEND_TIMEOUT);
281 : if (ret) {
282 : hccp_err("[recv][ra_hdc_recv_retry_msg]HDC get retry recv msg failed(%d)", ret);
283 : return ret;
284 : }
285 :
286 : ret = RA_HDC_OPS.getMsgBuffer(pMsgRcv, 0, &recvBuf, &rcvBufLen);
287 : if (ret) {
288 : hccp_err("[recv][ra_hdc_recv_retry_msg]HDC get retry msg buffer failed(%d)", ret);
289 : return ret;
290 : }
291 :
292 : ret = MsgHeadCheck((struct MsgHead *)recvBuf, RA_RS_GET_IFNUM, 0, sizeof(union OpIfnumData));
293 : if (rcvBufLen != outBufLen || ret != 0) {
294 : hccp_err("[recv][ra_hdc_recv_retry_msg]HDC get retry recv msg failed, ret(%d), rcvBufLen:%d, outBufLen:%d",
295 : ret, rcvBufLen, outBufLen);
296 : if (rcvBufLen != outBufLen) {
297 : ret = -EPIPE;
298 : }
299 : return ret;
300 : }
301 :
302 : return 0;
303 : }
304 : #endif
305 :
306 81 : static int HdcSendRecvPktRecv(HDC_SESSION session, unsigned int phyId, struct drvHdcMsg *pMsgRcv,
307 : char **recvBuf, int *rcvBufLen)
308 : {
309 81 : struct drvHdcMsg *pRetryRcv = NULL;
310 81 : int recvBufCnt = 0;
311 : int ret;
312 :
313 81 : ret =
314 81 : RA_HDC_OPS.recv(session, pMsgRcv, MAX_HDC_DATA, RA_HDC_WAIT_TIMEOUT, &recvBufCnt, RA_HDC_RECV_SEND_TIMEOUT);
315 : #ifndef HNS_ROCE_LLT
316 : /* if timeout, start retry */
317 : if (gRaHdc[phyId].startDeinit == 0 && ret == -DRV_ERROR_WAIT_TIMEOUT) {
318 : hccp_run_info("[recv][hdc_send_recv_pkt_recv]HDC recv timeout, start retry");
319 : ret = RaHdcSendRetryMsg(phyId, &pRetryRcv);
320 : CHK_PRT_RETURN(
321 : ret != 0, hccp_err("[recv][hdc_send_recv_pkt_recv]HDC get msg by first retry failed(%d)", ret), ret);
322 :
323 : ret = RA_HDC_OPS.recv(
324 : session, pMsgRcv, MAX_HDC_DATA, RA_HDC_WAIT_TIMEOUT, &recvBufCnt, RA_HDC_RECV_SEND_TIMEOUT);
325 : if (ret) {
326 : hccp_err("[recv][hdc_send_recv_pkt_recv]HDC get msg by first retry failed(%d)", ret);
327 : RA_HDC_OPS.freeMsg(pRetryRcv);
328 : return ret;
329 : }
330 :
331 : ret = RA_HDC_OPS.getMsgBuffer(pMsgRcv, 0, (char **)recvBuf, rcvBufLen);
332 : if (ret) {
333 : hccp_err("[recv][hdc_send_recv_pkt]HDC get_msg_buffer msg err ret(%d), rcvBufLen(%d)", ret, *rcvBufLen);
334 : RA_HDC_OPS.freeMsg(pRetryRcv);
335 : return ret;
336 : }
337 :
338 : ret = RaHdcRecvRetryMsg(session, pRetryRcv);
339 : if (ret) {
340 : hccp_err("[recv][hdc_send_recv_pkt_recv]HDC recv first retry msg failed(%d)", ret);
341 : }
342 :
343 : RA_HDC_OPS.freeMsg(pRetryRcv);
344 : return ret;
345 : }
346 : #endif
347 81 : CHK_PRT_RETURN(ret != 0, hccp_err("[recv][hdc_send_recv_pkt]HDC recv msg err ret(%d)", ret), ret);
348 :
349 80 : ret = RA_HDC_OPS.getMsgBuffer(pMsgRcv, 0, (char **)recvBuf, rcvBufLen);
350 80 : CHK_PRT_RETURN(ret != 0, hccp_err("[recv][hdc_send_recv_pkt]HDC get_msg_buffer msg err ret(%d), rcvBufLen(%d)",
351 : ret, *rcvBufLen), ret);
352 :
353 79 : return 0;
354 : }
355 :
356 79 : STATIC int HdcSendRecvPktRecvCheck(int rcvBufLen, unsigned int outDataLen, struct MsgHead *recvMsgHead,
357 : struct drvHdcMsg *pMsgRcv)
358 : {
359 : unsigned int rcvBufLenTmp;
360 :
361 79 : rcvBufLenTmp = (unsigned int)rcvBufLen;
362 79 : if (outDataLen != rcvBufLenTmp) {
363 0 : if (recvMsgHead->ret == -EACCES) {
364 0 : hccp_warn("exceed the speed limit, need try again, ret:%d", recvMsgHead->ret);
365 0 : RA_HDC_OPS.freeMsg(pMsgRcv);
366 0 : return -EAGAIN;
367 0 : } else if (recvMsgHead->ret == -EPROTONOSUPPORT) {
368 0 : hccp_err("[check][hdc_send_recv_pkt_recv]unsupported opcode, ret(%d)", recvMsgHead->ret);
369 0 : RA_HDC_OPS.freeMsg(pMsgRcv);
370 0 : return -EPROTONOSUPPORT;
371 0 : } else if (recvMsgHead->ret == -EPERM) {
372 0 : hccp_err("[check][hdc_send_recv_pkt_recv]host pid is invalid, ret(%d)", recvMsgHead->ret);
373 0 : RA_HDC_OPS.freeMsg(pMsgRcv);
374 0 : return -EPERM;
375 : }
376 0 : hccp_err("[check][hdc_send_recv_pkt_recv]date len err out_data_len(%d) != rcv_buf_len(%d) ",
377 : outDataLen, rcvBufLen);
378 0 : RA_HDC_OPS.freeMsg(pMsgRcv);
379 0 : return -EPIPE;
380 : }
381 79 : return 0;
382 : }
383 :
384 234 : STATIC int HdcSendRecvPkt(unsigned int phyId, void *sendRcvBuf, unsigned int inBufLen, unsigned int outBufLen)
385 : {
386 : int ret;
387 234 : char *recvBuf = NULL;
388 234 : struct drvHdcMsg *pMsgRcv = NULL, *pMsgSnd = NULL;
389 234 : int rcvBufLen = 0;
390 :
391 234 : struct MsgHead *recvMsgHead = NULL;
392 :
393 234 : RA_PTHREAD_MUTEX_LOCK(&gRaHdc[phyId].lock);
394 234 : HDC_SESSION session = gRaHdc[phyId].session;
395 234 : if (session == NULL) {
396 147 : hccp_err("[send_recv][pkt]session is NULL!, phyId(%u)", phyId);
397 147 : RA_PTHREAD_MUTEX_UNLOCK(&gRaHdc[phyId].lock);
398 147 : return -EINVAL;
399 : }
400 :
401 : // check last recv status
402 87 : if (gRaHdc[phyId].lastRecvStatus == -DRV_ERROR_WAIT_TIMEOUT) {
403 0 : ret = -DRV_ERROR_WAIT_TIMEOUT;
404 0 : goto alloc_msg_err;
405 : }
406 :
407 87 : ret = RA_HDC_OPS.allocMsg(session, &pMsgSnd, 1);
408 87 : if (ret != 0) {
409 1 : hccp_err("[send_recv][pkt]HDC alloc msg err ret(%d) phyId(%u)", ret, phyId);
410 1 : goto alloc_msg_err;
411 : }
412 86 : ret = HdcSendRecvPktSend(pMsgSnd, (char *)sendRcvBuf, inBufLen, session, &pMsgRcv);
413 86 : if (ret) {
414 5 : hccp_err("[send_recv][pkt]HDC pkt send err ret(%d) phyId(%u)", ret, phyId);
415 5 : goto msg_err;
416 : }
417 81 : ret = HdcSendRecvPktRecv(session, phyId, pMsgRcv, &recvBuf, &rcvBufLen);
418 81 : if (ret == -DRV_ERROR_WAIT_TIMEOUT) {
419 0 : hccp_err("[send_recv][pkt]HDC broken, pkt recv err ret(%d) phyId(%u)", ret, phyId);
420 0 : gRaHdc[phyId].lastRecvStatus = ret;
421 0 : goto msg_err;
422 : }
423 81 : if (ret) {
424 2 : hccp_err("[send_recv][pkt]HDC pkt recv err ret(%d) phyId(%u)", ret, phyId);
425 2 : goto msg_err;
426 : }
427 79 : RA_PTHREAD_MUTEX_UNLOCK(&gRaHdc[phyId].lock);
428 79 : recvMsgHead = (struct MsgHead *)recvBuf;
429 79 : ret = HdcSendRecvPktRecvCheck(rcvBufLen, outBufLen, recvMsgHead, pMsgRcv);
430 79 : CHK_PRT_RETURN(ret, hccp_err("[send_recv][pkt]HDC pkt recv check ret(%d) phyId(%u)", ret, phyId), ret);
431 :
432 79 : ret = memcpy_s(sendRcvBuf, outBufLen, recvBuf, rcvBufLen);
433 79 : if (ret) {
434 0 : hccp_err("[send_recv][pkt]memcpy_s failed, ret(%d) phyId(%u)", ret, phyId);
435 0 : RA_HDC_OPS.freeMsg(pMsgRcv);
436 0 : return -ESAFEFUNC;
437 : }
438 :
439 79 : RA_HDC_OPS.freeMsg(pMsgRcv);
440 79 : return 0;
441 7 : msg_err:
442 7 : RA_HDC_OPS.freeMsg(pMsgSnd);
443 8 : alloc_msg_err:
444 8 : RA_PTHREAD_MUTEX_UNLOCK(&gRaHdc[phyId].lock);
445 8 : return ret;
446 : }
447 :
448 293 : void MsgHeadBuildUp(struct MsgHead *pSendRcvHead, unsigned int opcode, unsigned int reqId,
449 : unsigned int msgDataLen, pid_t hostTgid)
450 : {
451 293 : pSendRcvHead->opcode = opcode;
452 293 : pSendRcvHead->ret = 0;
453 293 : pSendRcvHead->asyncReqId = reqId;
454 293 : pSendRcvHead->msgDataLen = msgDataLen;
455 293 : pSendRcvHead->hostTgid = hostTgid;
456 :
457 293 : return;
458 : }
459 :
460 124 : STATIC int MsgHeadCheck(struct MsgHead *sendRcvHead, unsigned int opcode, int rsRet, unsigned int msgDataLen)
461 : {
462 : unsigned int ret;
463 :
464 : /* return rs real return value */
465 124 : if (sendRcvHead->ret < rsRet) {
466 3 : return sendRcvHead->ret;
467 : }
468 :
469 121 : ret = (sendRcvHead->opcode != opcode) || (sendRcvHead->msgDataLen != msgDataLen);
470 :
471 121 : return (ret != 0 ? -EPERM : 0);
472 : }
473 :
474 296 : int RaHdcProcessMsg(unsigned int opcode, unsigned int phyId, char *data, unsigned int dataSize)
475 : {
476 296 : pid_t hostTgid = DlDrvDeviceGetBareTgid();
477 296 : void *sendRcvBuf = NULL;
478 : unsigned int sendRcvLen;
479 : int ret;
480 :
481 296 : if (gRaHdc[phyId].restoreFlag != 0) {
482 0 : return 0;
483 : }
484 :
485 296 : sendRcvLen = sizeof(struct MsgHead) + dataSize;
486 296 : CHK_PRT_RETURN(data == NULL, hccp_err("[process][ra_hdc_msg]data is NULL. phyId(%u)", phyId), -EINVAL);
487 296 : sendRcvBuf = (void *)calloc(sendRcvLen, sizeof(char));
488 296 : CHK_PRT_RETURN(sendRcvBuf == NULL, hccp_err("[process][ra_hdc_msg]send_rcv_buf calloc failed. phyId(%u)",
489 : phyId), -ENOMEM);
490 289 : MsgHeadBuildUp(sendRcvBuf, opcode, 0, dataSize, hostTgid);
491 289 : ret = memcpy_s(sendRcvBuf + sizeof(struct MsgHead), sendRcvLen - sizeof(struct MsgHead), data, dataSize);
492 289 : if (ret) {
493 2 : hccp_err("[process][ra_hdc_msg]memcpy_s failed, ret(%d) phyId(%u)", ret, phyId);
494 2 : ret = -ESAFEFUNC;
495 2 : goto out;
496 : }
497 :
498 287 : ret = HdcSendRecvPkt(phyId, sendRcvBuf, sendRcvLen, sendRcvLen);
499 287 : if (ret) {
500 158 : hccp_err("[process][ra_hdc_msg]hdc_send_recv_pkt opcode(%u) failed ret(%d) phyId(%u)", opcode, ret, phyId);
501 158 : goto out;
502 : }
503 :
504 129 : ret = MsgHeadCheck(sendRcvBuf, opcode, 0, dataSize);
505 : // opcode RA_RS_SOCKET_RECV not to print EAGAIN to avoid log flush
506 129 : if ((ret != 0) && (ret != -EAGAIN || opcode != RA_RS_SOCKET_RECV)) {
507 : /* maybe has retry return value, record warning log */
508 7 : hccp_warn("message head check unsuccessful, ret[%d] phyId[%u]", ret, phyId);
509 : }
510 :
511 129 : if (memcpy_s(data, dataSize, sendRcvBuf + sizeof(struct MsgHead), dataSize)) {
512 0 : hccp_err("[process][ra_hdc_msg]memcpy_s failed. dest size(%d) ret(%d) phyId(%u)", dataSize, ret, phyId);
513 0 : ret = -ESAFEFUNC;
514 : }
515 129 : out:
516 289 : free(sendRcvBuf);
517 289 : sendRcvBuf = NULL;
518 289 : return ret;
519 : }
520 :
521 0 : int HdcAsyncSendPkt(struct HdcAsyncInfo *asyncInfo, unsigned int phyId, void *sendBuf, unsigned int sendLen)
522 : {
523 0 : struct drvHdcMsg *pMsgSnd = NULL;
524 0 : HDC_SESSION session = NULL;
525 0 : int ret = 0;
526 :
527 0 : RA_PTHREAD_MUTEX_LOCK(&asyncInfo->sendMutex);
528 0 : session = asyncInfo->session;
529 0 : if (session == NULL) {
530 0 : RA_PTHREAD_MUTEX_UNLOCK(&asyncInfo->sendMutex);
531 0 : hccp_err("[async][send_pkt]session is NULL!, phyId(%u)", phyId);
532 0 : return -EINVAL;
533 : }
534 :
535 0 : ret = RA_HDC_OPS.allocMsg(session, &pMsgSnd, 1);
536 0 : if (ret != 0) {
537 0 : hccp_err("[async][send_pkt]HDC alloc msg err ret(%d) phyId(%u)", ret, phyId);
538 0 : goto alloc_msg_err;
539 : }
540 :
541 0 : ret = RA_HDC_OPS.addMsgBuffer(pMsgSnd, sendBuf, (int)sendLen);
542 0 : if (ret != 0) {
543 0 : hccp_err("[async][send_pkt]HDC add msg buffer err ret(%d) phyId(%u)", ret, phyId);
544 0 : goto msg_err;
545 : }
546 :
547 0 : ret = RA_HDC_OPS.send(session, pMsgSnd, RA_HDC_WAIT_TIMEOUT, RA_HDC_RECV_SEND_TIMEOUT);
548 0 : if (ret != 0) {
549 0 : hccp_err("[async][send_pkt]HDC send err ret(%d) phyId(%u)", ret, phyId);
550 0 : goto msg_err;
551 : }
552 :
553 0 : msg_err:
554 0 : RA_HDC_OPS.freeMsg(pMsgSnd);
555 0 : alloc_msg_err:
556 0 : RA_PTHREAD_MUTEX_UNLOCK(&asyncInfo->sendMutex);
557 0 : return ret;
558 : }
559 :
560 2 : STATIC int HdcAsyncPrepareRecvPkt(struct HdcAsyncInfo *asyncInfo, unsigned int phyId, HDC_SESSION *session,
561 : struct drvHdcMsg **pMsgRcv)
562 : {
563 2 : int ret = 0;
564 :
565 2 : *session = asyncInfo->session;
566 2 : if (*session == NULL) {
567 0 : hccp_err("[async][recv_pkt]session is NULL!, phyId(%u)", phyId);
568 0 : return -EINVAL;
569 : }
570 :
571 : // check last recv status
572 2 : if (RaHdcIsBroken(asyncInfo->lastRecvStatus)) {
573 1 : return asyncInfo->lastRecvStatus;
574 : }
575 :
576 1 : ret = RA_HDC_OPS.allocMsg(*session, pMsgRcv, 1);
577 1 : if (ret != 0) {
578 0 : hccp_err("[async][recv_pkt]HDC alloc msg err ret(%d) phyId(%u)", ret, phyId);
579 0 : return ret;
580 : }
581 :
582 1 : return 0;
583 : }
584 :
585 2 : int HdcAsyncRecvPkt(struct HdcAsyncInfo *asyncInfo, unsigned int phyId, void *recvBuf, unsigned int *recvLen)
586 : {
587 2 : struct drvHdcMsg *pMsgRcv = NULL;
588 2 : HDC_SESSION session = NULL;
589 2 : int recvBufCnt = 0;
590 2 : char *rcvBuf = NULL;
591 2 : int rcvLen = 0;
592 2 : int ret = 0;
593 :
594 2 : ret = HdcAsyncPrepareRecvPkt(asyncInfo, phyId, &session, &pMsgRcv);
595 2 : if (ret != 0) {
596 1 : goto session_err;
597 : }
598 :
599 1 : ret = RA_HDC_OPS.recv(session, pMsgRcv, MAX_HDC_DATA, RA_HDC_WAIT_TIMEOUT, &recvBufCnt,
600 : RA_HDC_RECV_SEND_TIMEOUT);
601 : // occur hdc time out when async session was closed before async request done
602 1 : if (ret == -DRV_ERROR_WAIT_TIMEOUT) {
603 0 : hccp_run_warn("[async][recv_pkt]HDC recv timeout, phyId(%u)", phyId);
604 0 : goto msg_err;
605 : }
606 :
607 1 : if (ret != 0) {
608 1 : hccp_err("[async][recv_pkt]HDC recv err ret(%d) phyId(%u)", ret, phyId);
609 1 : asyncInfo->lastRecvStatus = ret;
610 1 : goto msg_err;
611 : }
612 :
613 0 : ret = RA_HDC_OPS.getMsgBuffer(pMsgRcv, 0, (char **)&rcvBuf, &rcvLen);
614 0 : if (ret != 0 || rcvLen <= 0) {
615 0 : hccp_err("[async][recv_pkt]HDC get_msg_buffer err ret(%d) phyId(%u) rcv_len(%d)", ret, phyId, rcvLen);
616 0 : goto msg_err;
617 : }
618 :
619 0 : ret = memcpy_s(recvBuf, *recvLen, rcvBuf, (unsigned int)rcvLen);
620 0 : if (ret != 0) {
621 0 : hccp_err("[async][recv_pkt]memcpy_s failed, ret(%d) phyId(%u)", ret, phyId);
622 0 : RA_HDC_OPS.freeMsg(pMsgRcv);
623 0 : return -ESAFEFUNC;
624 : }
625 :
626 0 : *recvLen = (unsigned int)rcvLen;
627 0 : RA_HDC_OPS.freeMsg(pMsgRcv);
628 0 : return 0;
629 :
630 1 : msg_err:
631 1 : RA_HDC_OPS.freeMsg(pMsgRcv);
632 2 : session_err:
633 2 : return ret;
634 : }
635 :
636 58 : int RaHdcGetInterfaceVersion(unsigned int phyId, unsigned int interfaceOpcode, unsigned int *interfaceVersion)
637 : {
638 58 : int num = sizeof(gRaInterfaceInfoList) / sizeof(gRaInterfaceInfoList[0]);
639 : int i;
640 :
641 58 : CHK_PRT_RETURN(interfaceVersion == NULL || phyId >= RA_MAX_PHY_ID_NUM,
642 : hccp_err("[get][ra_interface_version]para invalid! interface_version is NULL or phyId(%u) >= [%u]",
643 : phyId, RA_MAX_PHY_ID_NUM), -EINVAL);
644 :
645 58 : *interfaceVersion = 0;
646 2397 : for (i = 0; i < num; i++) {
647 2393 : if (gRaInterfaceInfoList[i].opcode == interfaceOpcode) {
648 54 : *interfaceVersion = gRaInterfaceInfoList[i].version;
649 54 : break;
650 : }
651 : }
652 58 : return 0;
653 : }
654 :
655 125 : STATIC int RaHdcGetOpcodeVersion(unsigned int phyId, unsigned int interfaceOpcode,
656 : unsigned int *interfaceVersion)
657 : {
658 125 : union OpGetVersionData versionInfo = {0};
659 : int ret;
660 :
661 125 : versionInfo.txData.opcode = interfaceOpcode;
662 :
663 125 : ret = RaHdcProcessMsg(RA_RS_GET_INTERFACE_VERSION, phyId, (char *)&versionInfo,
664 : sizeof(union OpGetVersionData));
665 125 : CHK_PRT_RETURN(ret, hccp_err("[get][ra_hdc_interface_version]ra hdc message process failed ret(%d) phyId(%u)",
666 : ret, phyId), ret);
667 :
668 0 : *interfaceVersion = versionInfo.rxData.version;
669 0 : return 0;
670 : }
671 :
672 1 : void RaHdcGetAllOpcodeVersion(unsigned int phyId)
673 : {
674 1 : int num = sizeof(gRaInterfaceInfoList) / sizeof(gRaInterfaceInfoList[0]);
675 : int ret;
676 : int i;
677 :
678 126 : for (i = 0; i < num; i++) {
679 125 : ret = RaHdcGetOpcodeVersion(phyId, gRaInterfaceInfoList[i].opcode,
680 : &gRaInterfaceInfoList[i].version);
681 125 : if (ret != 0) {
682 125 : hccp_warn("ra_hdc_get_opcode_version unsuccessful, ret[%d], opcode[%d]",
683 : ret, gRaInterfaceInfoList[i].opcode);
684 125 : continue;
685 : }
686 : }
687 :
688 1 : return;
689 : }
690 :
691 0 : bool RaHdcHasCapability(unsigned int phyId, unsigned int capability)
692 : {
693 0 : unsigned int roceVersion = 0;
694 0 : unsigned int netVersion = 0;
695 0 : bool hasCapability = false;
696 :
697 : // return value is ignored as this function is guaranteed to succeed
698 0 : (void)RaHdcGetInterfaceVersion(phyId, RA_RS_GET_ROCE_API_VERSION, &roceVersion);
699 0 : (void)RaHdcGetInterfaceVersion(phyId, RA_RS_GET_NET_API_VERSION, &netVersion);
700 :
701 0 : hasCapability = RaRsHasCapability(capability, roceVersion, netVersion);
702 0 : if (!hasCapability) {
703 0 : hccp_run_info("phy_id:%u capability:%u roceVersion:0x%x netVersion:0x%x hasCapability:%d RA_CAP_INVALID:%u",
704 : phyId, capability, roceVersion, netVersion, hasCapability, RA_CAP_INVALID);
705 : }
706 0 : return hasCapability;
707 : }
708 :
709 27 : STATIC int RaHdcSendPid(unsigned int phyId, struct ProcessRaSign pRaSign)
710 : {
711 27 : union OpSetPidData setPidData = {0};
712 : int ret;
713 :
714 27 : setPidData.txData.pid = pRaSign.tgid;
715 27 : setPidData.txData.phyId = phyId;
716 :
717 27 : ret = strcpy_s(setPidData.txData.pidSign, PROCESS_RA_SIGN_LENGTH, pRaSign.sign);
718 27 : CHK_PRT_RETURN(ret, hccp_err("[send][ra_hdc_pid]Invalid pid sign, ret(%d)", ret), -ESAFEFUNC);
719 :
720 27 : ret = RaHdcProcessMsg(RA_RS_SET_PID, phyId,
721 : (char *)&setPidData, sizeof(union OpSetPidData));
722 27 : CHK_PRT_RETURN(ret, hccp_err("[send][ra_hdc_pid]ra hdc message process failed ret(%d) phyId(%u)",
723 : ret, phyId), ret);
724 :
725 27 : return 0;
726 : }
727 :
728 32 : STATIC int RaHdcInitApart(unsigned int phyId, unsigned int *logicId)
729 : {
730 : int ret;
731 32 : ret = DlDrvDeviceGetIndexByPhyId(phyId, logicId);
732 32 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc_apart]get logic id failed(%d), phyId(%u)", ret, phyId), -ENODEV);
733 :
734 32 : ret = pthread_mutex_init(&gRaHdc[phyId].lock, NULL);
735 32 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc_apart]pthread_mutex_init failed, ret(%d) phyId(%u)",
736 : ret, phyId), -ESYSFUNC);
737 30 : return 0;
738 : }
739 :
740 0 : STATIC int RaHdcInitSessionConnectEx(int peerNode, int peerDevid, unsigned int phyId, HDC_SESSION *session)
741 : {
742 0 : struct halQueryDevpidInfo info = {0};
743 : pid_t devPid;
744 : int ret;
745 :
746 0 : info.hostpid = getpid();
747 0 : info.devid = (unsigned int)peerDevid;
748 0 : info.proc_type = DEVDRV_PROCESS_HCCP;
749 0 : ret = DlHalQueryDevPid(info, &devPid);
750 0 : if (ret != 0) {
751 0 : hccp_err("[init][ra_hdc]hdc dl_hal_query_dev_pid failed ret(%d) peer_devid(%d) phyId(%u)",
752 : ret, peerDevid, phyId);
753 0 : return ret;
754 : }
755 :
756 0 : return RA_HDC_OPS.sessionConnectEx(peerNode, peerDevid, devPid, gRaHdc[phyId].client, session);
757 : }
758 :
759 29 : int RaHdcInitSession(int peerNode, int peerDevid, unsigned int phyId, int hdcType, HDC_SESSION *session)
760 : {
761 29 : if (hdcType == HDC_SERVICE_TYPE_RDMA_V2) {
762 0 : return RaHdcInitSessionConnectEx(peerNode, peerDevid, phyId, session);
763 : }
764 :
765 : // default hdc type: HDC_SERVICE_TYPE_RDMA
766 29 : return RA_HDC_OPS.sessionConnect(peerNode, peerDevid, gRaHdc[phyId].client, session);
767 : }
768 :
769 56 : void RaHdcDeinitSession(HDC_SESSION *session)
770 : {
771 56 : if (*session == NULL) {
772 29 : return;
773 : }
774 :
775 27 : (void)RA_HDC_OPS.sessionClose(*session);
776 27 : *session = NULL;
777 27 : return;
778 : }
779 :
780 0 : int RaHdcSetSessionReference(HDC_SESSION *session)
781 : {
782 0 : return RA_HDC_OPS.setSessionReference(*session);
783 : }
784 :
785 33 : int RaHdcInit(struct RaInitConfig *cfg, struct ProcessRaSign pRaSign)
786 : {
787 33 : unsigned int logicId = RA_MAX_PHY_ID_NUM;
788 33 : unsigned int phyId = cfg->phyId;
789 33 : int hdcType = cfg->hdcType;
790 : int ret;
791 :
792 33 : ret = DlHalInit();
793 33 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc]dl_hal_init failed, ret = %d", ret), ret);
794 :
795 33 : ret = RaHdcInitApart(phyId, &logicId);
796 33 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc]ra_hdc_init_apart failed, ret(%d)", ret), ret);
797 :
798 30 : hccp_run_info("hdc init start! logic id is %u, phy id is %u, hdcType is %d", logicId, phyId, hdcType);
799 :
800 30 : if (gRaHdc[phyId].session == NULL) {
801 : // maxSessionNum 2U include: sync & async session
802 29 : ret = RA_HDC_OPS.clientCreate(&gRaHdc[phyId].client, 2U, hdcType, 0);
803 29 : if (ret != 0) {
804 0 : hccp_err("[init][ra_hdc]hdc client create failed, hccp not up ret(%d) phyId(%u)", ret, phyId);
805 0 : goto HDC_ERR;
806 : }
807 29 : ret = RaHdcInitSession(0, (int)logicId, phyId, hdcType, &gRaHdc[phyId].session);
808 29 : if (ret != 0) {
809 1 : hccp_err("[init][ra_hdc]hdc session_connect failed ret(%d) logic_id(%u) phyId(%u)",
810 : ret, logicId, phyId);
811 1 : goto CONN_ERR;
812 : }
813 28 : ret = RA_HDC_OPS.setSessionReference(gRaHdc[phyId].session);
814 28 : if (ret != 0) {
815 1 : hccp_err("[init][ra_hdc]hdc set_session_reference failed, ret(%d) phyId(%u)", ret, phyId);
816 1 : goto SESS_ERR;
817 : }
818 : } else {
819 1 : hccp_warn("hdc session for phyId[%u] already existed", phyId);
820 1 : return -EEXIST;
821 : }
822 :
823 27 : ret = RaHdcSendPid(phyId, pRaSign);
824 27 : if (ret) {
825 0 : hccp_err("[init][ra_hdc]set pid for phyId(%u) failed, ret(%d), hostTgid(%d)", phyId, ret, pRaSign.tgid);
826 0 : goto SESS_ERR;
827 : }
828 :
829 27 : ret = RaHdcLiteInitCqeErrInfo(phyId);
830 27 : if (ret) {
831 0 : hccp_err("[init][ra_hdc]ra_hdc_lite_init_cqe_err_info failed, ret(%d)", ret);
832 0 : goto SESS_ERR;
833 : }
834 :
835 27 : hccp_run_info("hdc init OK! phyId[%u]", phyId);
836 :
837 27 : return 0;
838 1 : SESS_ERR:
839 1 : RA_HDC_OPS.sessionClose(gRaHdc[phyId].session);
840 1 : gRaHdc[phyId].session = NULL;
841 2 : CONN_ERR:
842 2 : RA_HDC_OPS.clientDestroy(gRaHdc[phyId].client);
843 2 : gRaHdc[phyId].client = NULL;
844 2 : HDC_ERR:
845 2 : pthread_mutex_destroy(&gRaHdc[phyId].lock);
846 2 : return -EPERM;
847 : }
848 :
849 1 : int RaHdcGetTlsEnable(unsigned int phyId, bool *tlsEnable)
850 : {
851 1 : union OpGetTlsEnableData opData = {0};
852 : int ret;
853 :
854 1 : opData.txData.phyId = phyId;
855 1 : ret = RaHdcProcessMsg(RA_RS_GET_TLS_ENABLE, phyId, (char *)&opData, sizeof(union OpGetTlsEnableData));
856 1 : CHK_PRT_RETURN(ret != 0, hccp_err("[get][tls_enable]ra hdc message process failed ret(%d) phyId(%u)",
857 : ret, phyId), ret);
858 :
859 1 : *tlsEnable = opData.rxData.tlsEnable;
860 1 : return ret;
861 : }
862 :
863 26 : STATIC int RaHdcSessionClose(unsigned int phyId)
864 : {
865 26 : union OpHdcCloseData hdcCloseData = {0};
866 : int ret;
867 :
868 26 : hdcCloseData.txData.phyId = phyId;
869 :
870 26 : ret = RaHdcProcessMsg(RA_RS_HDC_SESSION_CLOSE, phyId, (char *)&hdcCloseData,
871 : sizeof(union OpHdcCloseData));
872 26 : CHK_PRT_RETURN(ret, hccp_err("[close][ra_hdc_session]ra hdc message process failed ret(%d) phyId(%u)",
873 : ret, phyId), ret);
874 :
875 25 : return 0;
876 : }
877 :
878 28 : STATIC int RaHdcClientDeinit(unsigned int phyId)
879 : {
880 : int ret;
881 :
882 28 : gRaHdc[phyId].startDeinit = 1;
883 28 : ret = RaHdcSessionClose(phyId);
884 28 : if (ret) {
885 1 : hccp_err("[deinit][ra_hdc]close hdc session failed, ret(%d) phyId(%u)", ret, phyId);
886 : }
887 28 : RaHdcDeinitSession(&gRaHdc[phyId].snapshotSession);
888 28 : RaHdcDeinitSession(&gRaHdc[phyId].session);
889 :
890 28 : ret = RA_HDC_OPS.clientDestroy(gRaHdc[phyId].client);
891 28 : if (ret) {
892 1 : hccp_err("[deinit][ra_hdc]hdc client_destroy failed, ret(%d) phyId(%u)", ret, phyId);
893 : }
894 28 : gRaHdc[phyId].client = NULL;
895 28 : gRaHdc[phyId].startDeinit = 0;
896 :
897 28 : return ret;
898 : }
899 :
900 28 : int RaHdcDeinit(struct RaInitConfig *cfg)
901 : {
902 28 : unsigned int phyId = cfg->phyId;
903 : int ret;
904 :
905 28 : hccp_run_info("hdc deinit start! phyId[%u] restore_flag[%u]", phyId, gRaHdc[phyId].restoreFlag);
906 :
907 28 : RaHdcLiteDeinitCqeErrInfo(phyId);
908 :
909 28 : ret = RaHdcClientDeinit(phyId);
910 28 : if (ret != 0) {
911 1 : hccp_err("[deinit][ra_hdc]client deinit failed! ret(%d) phyId(%u)", ret, phyId);
912 : }
913 :
914 28 : ret = pthread_mutex_destroy(&gRaHdc[phyId].lock);
915 28 : if (ret != 0) {
916 1 : hccp_err("[deinit][ra_hdc]pthread_mutex_destroy failed! ret(%d) phyId(%u)", ret, phyId);
917 1 : ret = -ESYSFUNC;
918 : }
919 :
920 28 : CHK_PRT_RETURN(ret != 0, hccp_err("[deinit][ra_hdc]hdc deinit failed! phyId(%u)", phyId), ret);
921 27 : DlHalDeinit();
922 :
923 27 : (void)memset_s(&gRaHdc[phyId], sizeof(gRaHdc[phyId]), 0, sizeof(gRaHdc[phyId]));
924 :
925 27 : hccp_run_info("hdc deinit OK! phyId[%u]", phyId);
926 27 : return 0;
927 : }
928 :
929 0 : STATIC int RaHdcGetValidCqeErrInfo(
930 : struct CqeErrInfo *outInfo, struct CqeErrInfo info0, struct CqeErrInfo info1)
931 : {
932 : int ret;
933 :
934 0 : if (info0.status != 0 && info1.status != 0) {
935 0 : if (timercmp((&info0.time), (&info1.time), <)) {
936 0 : ret = memcpy_s(outInfo, sizeof(struct CqeErrInfo), &info0, sizeof(struct CqeErrInfo));
937 : } else {
938 0 : ret = memcpy_s(outInfo, sizeof(struct CqeErrInfo), &info1, sizeof(struct CqeErrInfo));
939 : }
940 : } else {
941 0 : if (info0.status == 0) {
942 0 : ret = memcpy_s(outInfo, sizeof(struct CqeErrInfo), &info1, sizeof(struct CqeErrInfo));
943 : } else {
944 0 : ret = memcpy_s(outInfo, sizeof(struct CqeErrInfo), &info0, sizeof(struct CqeErrInfo));
945 : }
946 : }
947 :
948 0 : if (ret) {
949 0 : hccp_err("memcpy_s failed, ret(%d)", ret);
950 0 : return -ESAFEFUNC;
951 : }
952 :
953 0 : return 0;
954 : }
955 :
956 0 : int RaHdcGetCqeErrInfo(unsigned int phyId, struct CqeErrInfo *info)
957 : {
958 : int ret;
959 0 : struct CqeErrInfo opCqeInfo = { 0 };
960 : union OpGetCqeErrInfoData cqeErrInfoData;
961 :
962 0 : RaHdcLiteGetCqeErrInfo(phyId, &opCqeInfo);
963 :
964 0 : RA_PTHREAD_MUTEX_LOCK(&gRaHdc[phyId].lock);
965 0 : HDC_SESSION session = gRaHdc[phyId].session;
966 0 : if (session == NULL) {
967 0 : RA_PTHREAD_MUTEX_UNLOCK(&gRaHdc[phyId].lock);
968 0 : return 0;
969 : }
970 0 : RA_PTHREAD_MUTEX_UNLOCK(&gRaHdc[phyId].lock);
971 :
972 0 : ret = memset_s(&cqeErrInfoData, sizeof(cqeErrInfoData), 0, sizeof(cqeErrInfoData));
973 0 : CHK_PRT_RETURN(ret, hccp_err("[init]memset_s failed ret(%d)", ret), -ESAFEFUNC);
974 0 : ret = RaHdcProcessMsg(RA_RS_GET_CQE_ERR_INFO, phyId,
975 : (char *)&cqeErrInfoData, sizeof(union OpGetCqeErrInfoData));
976 0 : CHK_PRT_RETURN(ret, hccp_err("ra hdc message process failed ret(%d)", ret), ret);
977 :
978 0 : return RaHdcGetValidCqeErrInfo(info, opCqeInfo, cqeErrInfoData.rxData.info);
979 : }
980 :
981 0 : STATIC int RaHdcSessionSaveSnapshot(unsigned int phyId, enum SaveSnapshotAction action)
982 : {
983 0 : int ret = 0;
984 :
985 0 : RA_PTHREAD_MUTEX_LOCK(&gRaHdc[phyId].lock);
986 0 : if (action == SAVE_SNAPSHOT_ACTION_PRE_PROCESSING && gRaHdc[phyId].session != NULL) {
987 0 : gRaHdc[phyId].snapshotSession = gRaHdc[phyId].session;
988 0 : gRaHdc[phyId].session = NULL;
989 0 : } else if (action == SAVE_SNAPSHOT_ACTION_POST_PROCESSING && gRaHdc[phyId].session == NULL) {
990 0 : gRaHdc[phyId].session = gRaHdc[phyId].snapshotSession;
991 0 : gRaHdc[phyId].snapshotSession = NULL;
992 : } else {
993 0 : hccp_err("duplicate or incorrect order calls are not allowed, phyId[%u] action[%d]", phyId, action);
994 0 : ret = -EPERM;
995 : }
996 0 : RA_PTHREAD_MUTEX_UNLOCK(&gRaHdc[phyId].lock);
997 :
998 0 : return ret;
999 : }
1000 :
1001 0 : int RaHdcSaveSnapshot(unsigned int phyId, enum SaveSnapshotAction action)
1002 : {
1003 : int ret;
1004 :
1005 0 : ret = RaHdcAsyncSaveSnapshot(phyId, action);
1006 0 : CHK_PRT_RETURN(ret != 0, hccp_err("ra_hdc_async_save_snapshot failed ret[%d]", ret), ret);
1007 0 : ret = RaHdcSessionSaveSnapshot(phyId, action);
1008 0 : CHK_PRT_RETURN(ret != 0, hccp_err("ra_hdc_session_save_snapshot failed ret[%d]", ret), ret);
1009 :
1010 0 : return ret;
1011 : }
1012 :
1013 0 : STATIC void RaHdcSessionRestoreSnapshot(unsigned int phyId)
1014 : {
1015 0 : RA_PTHREAD_MUTEX_LOCK(&gRaHdc[phyId].lock);
1016 0 : gRaHdc[phyId].restoreFlag = 1;
1017 0 : RA_PTHREAD_MUTEX_UNLOCK(&gRaHdc[phyId].lock);
1018 0 : }
1019 :
1020 0 : int RaHdcRestoreSnapshot(unsigned int phyId)
1021 : {
1022 : int ret;
1023 :
1024 0 : ret = RaHdcAsyncRestoreSnapshot(phyId);
1025 0 : CHK_PRT_RETURN(ret != 0, hccp_err("ra_hdc_async_restore_snapshot failed ret[%d]", ret), ret);
1026 0 : RaHdcSessionRestoreSnapshot(phyId);
1027 :
1028 0 : return ret;
1029 : }
1030 :
1031 1 : int RaHdcGetSecRandom(unsigned int phyId, unsigned int *value)
1032 : {
1033 1 : union OpGetSecRandomData opData = {0};
1034 : int ret;
1035 :
1036 1 : ret = RaHdcProcessMsg(RA_RS_GET_SEC_RANDOM, phyId, (char *)&opData, sizeof(union OpGetSecRandomData));
1037 1 : CHK_PRT_RETURN(ret != 0, hccp_err("[get][sec_random]ra hdc message process failed ret(%d) phyId(%u)",
1038 : ret, phyId), ret);
1039 :
1040 1 : *value = opData.rxData.value;
1041 1 : return ret;
1042 : }
1043 :
1044 1 : int RaHdcGetHccnCfg(unsigned int phyId, enum HccnCfgKey key, char *value, unsigned int *valueLen)
1045 : {
1046 1 : union OpGetHccnCfgData opData = {0};
1047 : int ret;
1048 :
1049 1 : opData.txData.phyId = phyId;
1050 1 : opData.txData.key = key;
1051 1 : ret = RaHdcProcessMsg(RA_RS_GET_HCCN_CFG, phyId, (char *)&opData, sizeof(union OpGetHccnCfgData));
1052 1 : CHK_PRT_RETURN(ret != 0, hccp_err("[get][hccn_cfg]ra hdc message process failed ret(%d) phyId(%u)",
1053 : ret, phyId), ret);
1054 :
1055 1 : ret = memcpy_s(value, *valueLen, opData.rxData.value, opData.rxData.valueLen);
1056 1 : CHK_PRT_RETURN(ret != 0, hccp_err("[get][hccn_cfg]ra hdc message process failed ret(%d) phyId(%u)",
1057 : ret, phyId), ret);
1058 :
1059 1 : *valueLen = opData.rxData.valueLen;
1060 1 : return ret;
1061 : }
|