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 : #ifndef RA_HDC_H
12 : #define RA_HDC_H
13 :
14 : #include <pthread.h>
15 : #include <sys/types.h>
16 : #include "ascend_hal.h"
17 : #include "hccp.h"
18 : #include "hccp_common.h"
19 : #include "ra.h"
20 : #include "ra_async.h"
21 : #include "ra_rs_comm.h"
22 :
23 : struct SocketHdcInfo {
24 : unsigned int phyId;
25 : int fd;
26 : void *socketHandle;
27 : uint32_t rsv; // consistent with socket_peer_info
28 : };
29 :
30 : struct HdcInfo {
31 : HDC_CLIENT client;
32 : HDC_SESSION session;
33 : HDC_SESSION snapshotSession;
34 : int startDeinit;
35 : int lastRecvStatus;
36 : pthread_mutex_t lock;
37 : unsigned int restoreFlag;
38 : };
39 :
40 : struct HdcAsyncInfo {
41 : pid_t hostTgid;
42 : HDC_SESSION session;
43 : HDC_SESSION snapshotSession;
44 : pthread_t tid;
45 : unsigned int connectStatus; // hdc session connect status
46 : unsigned int threadStatus; // recv thread status
47 :
48 : int lastRecvStatus;
49 : pthread_mutex_t sendMutex;
50 : pthread_mutex_t recvMutex;
51 :
52 : unsigned int reqId;
53 : pthread_mutex_t reqMutex;
54 : struct RaListHead reqList;
55 : pthread_mutex_t rspMutex;
56 : struct RaListHead rspList;
57 : unsigned int restoreFlag;
58 : };
59 :
60 : struct MsgHead {
61 : unsigned int opcode;
62 : int ret;
63 : union {
64 : unsigned int rsvd;
65 : unsigned int asyncReqId;
66 : };
67 : unsigned int msgDataLen;
68 : pid_t hostTgid;
69 : char pidSign[PROCESS_RA_SIGN_LENGTH];
70 : };
71 :
72 : enum RaHdcRecvMode {
73 : RA_HDC_WAIT_FOREVER,
74 : RA_HDC_NOWAIT,
75 : RA_HDC_WAIT_TIMEOUT,
76 : };
77 :
78 : #define RA_RSVD_NUM_2 2
79 : #define RA_RSVD_NUM_3 3
80 : #define RA_RSVD_NUM_4 4
81 : #define RA_RSVD_NUM_5 5
82 : #define RA_RSVD_NUM_6 6
83 : #define RA_RSVD_NUM_8 8
84 : #define RA_RSVD_NUM_16 16
85 : #define RA_RSVD_NUM_33 33
86 : #define RA_RSVD_NUM_50 50
87 : #define RA_RSVD_NUM_53 53
88 : #define RA_RSVD_NUM_61 61
89 : #define RA_RSVD_NUM_62 62
90 : #define RA_RSVD_NUM_63 63
91 : #define RA_RSVD_NUM_64 64
92 : #define RA_RSVD_NUM_801 801
93 : #define MAX_HDC_MSG_DATA (4096 - 16)
94 :
95 : union OpSetPidData {
96 : struct {
97 : unsigned int phyId;
98 : pid_t pid;
99 : unsigned int rsvd[RA_RSVD_NUM_2];
100 : char pidSign[PROCESS_RA_SIGN_LENGTH];
101 : char resv[PROCESS_RA_RESV_LENGTH];
102 : } txData;
103 :
104 : struct {
105 : unsigned int rsvd[RA_RSVD_NUM_4];
106 : } rxData;
107 : };
108 :
109 : union OpGetCqeErrInfoData {
110 : struct {
111 : } txData;
112 :
113 : struct {
114 : struct CqeErrInfo info;
115 : } rxData;
116 : };
117 :
118 : union OpGetTlsEnableData {
119 : struct {
120 : unsigned int phyId;
121 : unsigned int rsvd[RA_RSVD_NUM_4];
122 : } txData;
123 : struct {
124 : bool tlsEnable;
125 : unsigned int rsvd[RA_RSVD_NUM_4];
126 : } rxData;
127 : };
128 :
129 : union OpGetSecRandomData {
130 : struct {
131 : unsigned int rsvd;
132 : } txData;
133 : struct {
134 : unsigned int value;
135 : } rxData;
136 : };
137 :
138 : union OpGetHccnCfgData {
139 : struct {
140 : unsigned int phyId;
141 : enum HccnCfgKey key;
142 : unsigned int rsvd[RA_RSVD_NUM_4];
143 : } txData;
144 : struct {
145 : char value[HCCN_CFG_MSG_DATA_LEN];
146 : unsigned int valueLen;
147 : unsigned int rsvd[RA_RSVD_NUM_4];
148 : } rxData;
149 : };
150 :
151 : union OpHdcCloseData {
152 : struct {
153 : unsigned int phyId;
154 : char resv[PROCESS_RA_RESV_LENGTH];
155 : } txData;
156 :
157 : struct {
158 : unsigned int rsvd[RA_RSVD_NUM_4];
159 : } rxData;
160 : };
161 :
162 : union OpIfnumData {
163 : struct {
164 : unsigned int phyId;
165 : unsigned int num; /* resv bit 31 for is_all */
166 : } txData;
167 :
168 : struct {
169 : unsigned int num;
170 : } rxData;
171 : };
172 :
173 : union OpGetVersionData {
174 : struct {
175 : unsigned int opcode;
176 : } txData;
177 :
178 : struct {
179 : unsigned int version;
180 : } rxData;
181 : };
182 :
183 : struct HdcOps {
184 : hdcError_t (*getCapacity)(struct drvHdcCapacity *capacity);
185 : hdcError_t (*clientCreate)(HDC_CLIENT *client, int maxSessionNum, int serviceType, int flag);
186 : hdcError_t (*clientDestroy)(HDC_CLIENT client);
187 : hdcError_t (*sessionConnect)(int peerNode, int peerLogicid, HDC_CLIENT client, HDC_SESSION *session);
188 : hdcError_t (*sessionConnectEx)(int peerNode, int peerDevid, int peerPid, HDC_CLIENT client, HDC_SESSION *pSession);
189 : hdcError_t (*serverCreate)(int chipId, int serviceType, HDC_SERVER *server);
190 : hdcError_t (*serverDestroy)(HDC_SERVER server);
191 : hdcError_t (*sessionAccept)(HDC_SERVER server, HDC_SESSION *session);
192 : hdcError_t (*sessionClose)(HDC_SESSION session);
193 : hdcError_t (*allocMsg)(HDC_SESSION session, struct drvHdcMsg **ppMsg, int count);
194 : hdcError_t (*freeMsg)(struct drvHdcMsg *msg);
195 : hdcError_t (*reuseMsg)(struct drvHdcMsg *msg);
196 : hdcError_t (*addMsgBuffer)(struct drvHdcMsg *msg, char *pBuf, int len);
197 : hdcError_t (*getMsgBuffer)(struct drvHdcMsg *msg, int index, char **pBuf, int *pLen);
198 : hdcError_t (*recv)(HDC_SESSION session, struct drvHdcMsg *msg, int bufLen, unsigned long long flag,
199 : int *recvBufCount, unsigned int timeout);
200 : hdcError_t (*send)(HDC_SESSION session, struct drvHdcMsg *msg, unsigned long long flag, unsigned int timeout);
201 : hdcError_t (*setSessionReference)(HDC_SESSION session);
202 : };
203 :
204 : #define RA_PTHREAD_MUTEX_LOCK(mutex) \
205 : do { \
206 : int ret_lock = pthread_mutex_lock(mutex); \
207 : if (ret_lock) { \
208 : hccp_warn("pthread_mutex_lock unsuccessful, ret[%d]", ret_lock); \
209 : } \
210 : } while (0)
211 :
212 : #define RA_PTHREAD_MUTEX_UNLOCK(mutex) \
213 : do { \
214 : int ret_ulock = pthread_mutex_unlock(mutex); \
215 : if (ret_ulock) { \
216 : hccp_warn("pthread_mutex_unlock unsuccessful, ret[%d]", ret_ulock); \
217 : } \
218 : } while (0)
219 :
220 9 : static inline bool RaHdcIsBroken(int lastRecvStatus)
221 : {
222 9 : return lastRecvStatus == -DRV_ERROR_SOCKET_CLOSE;
223 : }
224 :
225 : int RaHdcInit(struct RaInitConfig *cfg, struct ProcessRaSign pRaSign);
226 : int RaHdcGetTlsEnable(unsigned int phyId, bool *tlsEnable);
227 : int RaHdcDeinit(struct RaInitConfig *cfg);
228 : int RaHdcGetInterfaceVersion(unsigned int phyId, unsigned int interfaceOpcode, unsigned int *interfaceVersion);
229 : void RaHdcGetAllOpcodeVersion(unsigned int phyId);
230 : bool RaHdcHasCapability(unsigned int phyId, unsigned int capability);
231 : int RaHdcGetCqeErrInfo(unsigned int phyId, struct CqeErrInfo *info);
232 : int RaHdcProcessMsg(unsigned int opcode, unsigned int phyId, char *data, unsigned int dataSize);
233 : int RaHdcInitSession(int peerNode, int peerDevid, unsigned int phyId, int hdcType, HDC_SESSION *session);
234 : void RaHdcDeinitSession(HDC_SESSION *session);
235 : int RaHdcSetSessionReference(HDC_SESSION *session);
236 : void MsgHeadBuildUp(struct MsgHead *pSendRcvHead, unsigned int opcode, unsigned int reqId, unsigned int msgDataLen,
237 : pid_t hostTgid);
238 : int HdcAsyncSendPkt(struct HdcAsyncInfo *asyncInfo, unsigned int phyId, void *sendBuf, unsigned int sendLen);
239 : int HdcAsyncRecvPkt(struct HdcAsyncInfo *asyncInfo, unsigned int phyId, void *recvBuf, unsigned int *recvLen);
240 : int RaHdcSaveSnapshot(unsigned int phyId, enum SaveSnapshotAction action);
241 : int RaHdcRestoreSnapshot(unsigned int phyId);
242 : int RaHdcGetSecRandom(unsigned int phyId, unsigned int *value);
243 : int RaHdcGetHccnCfg(unsigned int phyId, enum HccnCfgKey key, char *value, unsigned int *valueLen);
244 : #endif // RA_HDC_H
|