1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2022 Intel Corporation */
3 /**
4 ***************************************************************************
5 * @file lac_sync.h
6 *
7 * @defgroup LacSync LAC synchronous
8 *
9 * @ingroup LacCommon
10 *
11 * Function prototypes and defines for synchronous support
12 *
13 ***************************************************************************/
14
15 #ifndef LAC_SYNC_H
16 #define LAC_SYNC_H
17
18 #include "cpa.h"
19 #include "qat_utils.h"
20 #include "lac_mem.h"
21
22 /**
23 *****************************************************************************
24 * @ingroup LacSync
25 *
26 * @description
27 * LAC cookie for synchronous support
28 *
29 *****************************************************************************/
30 typedef struct lac_sync_op_data_s {
31 struct sema *sid;
32 /**< Semaphore to signal */
33 CpaStatus status;
34 /**< Output - Status of the QAT response */
35 CpaBoolean opResult;
36 /**< Output - Verification of the operation/protocol status */
37 CpaBoolean complete;
38 /**< Output - Operation is complete */
39 CpaBoolean canceled;
40 /**< Output - Operation canceled */
41 } lac_sync_op_data_t;
42
43 #define LAC_PKE_SYNC_CALLBACK_TIMEOUT (5000)
44 /**< @ingroup LacSync
45 * Timeout waiting for an async callbacks in msecs.
46 * This is derived from the max latency of a PKE request + 1 sec
47 */
48
49 #define LAC_SYM_DRBG_POLL_AND_WAIT_TIME_MS (10)
50 /**< @ingroup LacSyn
51 * Default interval DRBG polling in msecs */
52
53 #define LAC_SYM_SYNC_CALLBACK_TIMEOUT (300)
54 /**< @ingroup LacSyn
55 * Timeout for wait for symmetric response in msecs
56 */
57
58 #define LAC_INIT_MSG_CALLBACK_TIMEOUT (1922)
59 /**< @ingroup LacSyn
60 * Timeout for wait for init messages response in msecs
61 */
62
63 #define DC_SYNC_CALLBACK_TIMEOUT (2000)
64 /**< @ingroup LacSyn
65 * Timeout for wait for compression response in msecs */
66
67 #define LAC_SYN_INITIAL_SEM_VALUE (0)
68 /**< @ingroup LacSyn
69 * Initial value of the sync waiting semaphore */
70
71 /**
72 *******************************************************************************
73 * @ingroup LacSync
74 * This function allocates a sync op data cookie
75 * and creates and initialises the QAT Utils semaphore
76 *
77 * @param[in] ppSyncCallbackCookie Pointer to synch op data
78 *
79 * @retval CPA_STATUS_RESOURCE Failed to allocate the memory for the cookie.
80 * @retval CPA_STATUS_SUCCESS Success
81 *
82 ******************************************************************************/
83 static __inline CpaStatus
LacSync_CreateSyncCookie(lac_sync_op_data_t ** ppSyncCallbackCookie)84 LacSync_CreateSyncCookie(lac_sync_op_data_t **ppSyncCallbackCookie)
85 {
86 CpaStatus status = CPA_STATUS_SUCCESS;
87
88 *ppSyncCallbackCookie =
89 malloc(sizeof(lac_sync_op_data_t), M_QAT, M_WAITOK);
90
91 if (CPA_STATUS_SUCCESS == status) {
92 status = LAC_INIT_SEMAPHORE((*ppSyncCallbackCookie)->sid,
93 LAC_SYN_INITIAL_SEM_VALUE);
94 (*ppSyncCallbackCookie)->complete = CPA_FALSE;
95 (*ppSyncCallbackCookie)->canceled = CPA_FALSE;
96 }
97
98 if (CPA_STATUS_SUCCESS != status) {
99 LAC_OS_FREE(*ppSyncCallbackCookie);
100 }
101
102 return status;
103 }
104
105 /**
106 *******************************************************************************
107 * @ingroup LacSync
108 * This macro frees a sync op data cookie and destroys the QAT Utils
109 *semaphore
110 *
111 * @param[in] ppSyncCallbackCookie Pointer to sync op data
112 *
113 * @return void
114 ******************************************************************************/
115 static __inline CpaStatus
LacSync_DestroySyncCookie(lac_sync_op_data_t ** ppSyncCallbackCookie)116 LacSync_DestroySyncCookie(lac_sync_op_data_t **ppSyncCallbackCookie)
117 {
118 CpaStatus status = CPA_STATUS_SUCCESS;
119
120 /*
121 * If the operation has not completed, cancel it instead of destroying
122 * the
123 * cookie. Otherwise, the callback might panic. In this case, the cookie
124 * will leak, but it's better than a panic.
125 */
126 if (!(*ppSyncCallbackCookie)->complete) {
127 QAT_UTILS_LOG(
128 "Attempting to destroy an incomplete sync cookie\n");
129 (*ppSyncCallbackCookie)->canceled = CPA_TRUE;
130 return CPA_STATUS_FAIL;
131 }
132
133 status = LAC_DESTROY_SEMAPHORE((*ppSyncCallbackCookie)->sid);
134 LAC_OS_FREE(*ppSyncCallbackCookie);
135 return status;
136 }
137
138 /**
139 *****************************************************************************
140 * @ingroup LacSync
141 * Function which will wait for a sync callback on a given cookie.
142 *
143 * @param[in] pSyncCallbackCookie Pointer to sync op data
144 * @param[in] timeOut Time to wait for callback (msec)
145 * @param[out] pStatus Status returned by the callback
146 * @param[out] pOpStatus Operation status returned by callback.
147 *
148 * @retval CPA_STATUS_SUCCESS Success
149 * @retval CPA_STATUS_SUCCESS Fail waiting for a callback
150 *
151 *****************************************************************************/
152 static __inline CpaStatus
LacSync_WaitForCallback(lac_sync_op_data_t * pSyncCallbackCookie,Cpa32S timeOut,CpaStatus * pStatus,CpaBoolean * pOpStatus)153 LacSync_WaitForCallback(lac_sync_op_data_t *pSyncCallbackCookie,
154 Cpa32S timeOut,
155 CpaStatus *pStatus,
156 CpaBoolean *pOpStatus)
157 {
158 CpaStatus status = CPA_STATUS_SUCCESS;
159
160 status = LAC_WAIT_SEMAPHORE(pSyncCallbackCookie->sid, timeOut);
161
162 if (CPA_STATUS_SUCCESS == status) {
163 *pStatus = pSyncCallbackCookie->status;
164 if (NULL != pOpStatus) {
165 *pOpStatus = pSyncCallbackCookie->opResult;
166 }
167 pSyncCallbackCookie->complete = CPA_TRUE;
168 }
169
170 return status;
171 }
172
173 /**
174 *****************************************************************************
175 * @ingroup LacSync
176 * Function which will check for a sync callback on a given cookie.
177 * Returns whether the callback has happened or not, no timeout.
178 *
179 * @param[in] pSyncCallbackCookie Pointer to sync op data
180 * @param[in] timeOut Time to wait for callback (msec)
181 * @param[out] pStatus Status returned by the callback
182 * @param[out] pOpStatus Operation status returned by callback.
183 *
184 * @retval CPA_STATUS_SUCCESS Success
185 * @retval CPA_STATUS_FAIL Fail waiting for a callback
186 *
187 *****************************************************************************/
188 static __inline CpaStatus
LacSync_CheckForCallback(lac_sync_op_data_t * pSyncCallbackCookie,CpaStatus * pStatus,CpaBoolean * pOpStatus)189 LacSync_CheckForCallback(lac_sync_op_data_t *pSyncCallbackCookie,
190 CpaStatus *pStatus,
191 CpaBoolean *pOpStatus)
192 {
193 CpaStatus status = CPA_STATUS_SUCCESS;
194
195 status = LAC_CHECK_SEMAPHORE(pSyncCallbackCookie->sid);
196
197 if (CPA_STATUS_SUCCESS == status) {
198 *pStatus = pSyncCallbackCookie->status;
199 if (NULL != pOpStatus) {
200 *pOpStatus = pSyncCallbackCookie->opResult;
201 }
202 pSyncCallbackCookie->complete = CPA_TRUE;
203 }
204
205 return status;
206 }
207
208 /**
209 *****************************************************************************
210 * @ingroup LacSync
211 * Function which will mark a sync cookie as complete.
212 * If it's known that the callback will not happen it's necessary
213 * to call this, else the cookie can't be destroyed.
214 *
215 * @param[in] pSyncCallbackCookie Pointer to sync op data
216 *
217 * @retval CPA_STATUS_SUCCESS Success
218 * @retval CPA_STATUS_FAIL Failed to mark as complete
219 *
220 *****************************************************************************/
221 static __inline CpaStatus
LacSync_SetSyncCookieComplete(lac_sync_op_data_t * pSyncCallbackCookie)222 LacSync_SetSyncCookieComplete(lac_sync_op_data_t *pSyncCallbackCookie)
223 {
224 CpaStatus status = CPA_STATUS_FAIL;
225
226 if (NULL != pSyncCallbackCookie) {
227 pSyncCallbackCookie->complete = CPA_TRUE;
228 status = CPA_STATUS_SUCCESS;
229 }
230 return status;
231 }
232 /**
233 *****************************************************************************
234 * @ingroup LacSync
235 * Generic verify callback function.
236 * @description
237 * This function is used when the API is called in synchronous mode.
238 * It's assumed the callbackTag holds a lac_sync_op_data_t type
239 * and when the callback is received, this callback shall set the
240 * status element of that cookie structure and kick the sid.
241 * This function may be used directly as a callback function.
242 *
243 * @param[in] callbackTag Callback Tag
244 * @param[in] status Status of callback
245 * @param[out] pOpdata Pointer to the Op Data
246 * @param[out] opResult Boolean to indicate the result of the operation
247 *
248 * @return void
249 *****************************************************************************/
250 void LacSync_GenVerifyCb(void *callbackTag,
251 CpaStatus status,
252 void *pOpdata,
253 CpaBoolean opResult);
254
255 /**
256 *****************************************************************************
257 * @ingroup LacSync
258 * Generic flatbuffer callback function.
259 * @description
260 * This function is used when the API is called in synchronous mode.
261 * It's assumed the callbackTag holds a lac_sync_op_data_t type
262 * and when the callback is received, this callback shall set the
263 * status element of that cookie structure and kick the sid.
264 * This function may be used directly as a callback function.
265 *
266 * @param[in] callbackTag Callback Tag
267 * @param[in] status Status of callback
268 * @param[in] pOpdata Pointer to the Op Data
269 * @param[out] pOut Pointer to the flat buffer
270 *
271 * @return void
272 *****************************************************************************/
273 void LacSync_GenFlatBufCb(void *callbackTag,
274 CpaStatus status,
275 void *pOpdata,
276 CpaFlatBuffer *pOut);
277
278 /**
279 *****************************************************************************
280 * @ingroup LacSync
281 * Generic flatbuffer verify callback function.
282 * @description
283 * This function is used when the API is called in synchronous mode.
284 * It's assumed the callbackTag holds a lac_sync_op_data_t type
285 * and when the callback is received, this callback shall set the
286 * status and opResult element of that cookie structure and
287 * kick the sid.
288 * This function may be used directly as a callback function.
289 *
290 * @param[in] callbackTag Callback Tag
291 * @param[in] status Status of callback
292 * @param[in] pOpdata Pointer to the Op Data
293 * @param[out] opResult Boolean to indicate the result of the operation
294 * @param[out] pOut Pointer to the flat buffer
295 *
296 * @return void
297 *****************************************************************************/
298 void LacSync_GenFlatBufVerifyCb(void *callbackTag,
299 CpaStatus status,
300 void *pOpdata,
301 CpaBoolean opResult,
302 CpaFlatBuffer *pOut);
303
304 /**
305 *****************************************************************************
306 * @ingroup LacSync
307 * Generic dual flatbuffer verify callback function.
308 * @description
309 * This function is used when the API is called in synchronous mode.
310 * It's assumed the callbackTag holds a lac_sync_op_data_t type
311 * and when the callback is received, this callback shall set the
312 * status and opResult element of that cookie structure and
313 * kick the sid.
314 * This function may be used directly as a callback function.
315 *
316 * @param[in] callbackTag Callback Tag
317 * @param[in] status Status of callback
318 * @param[in] pOpdata Pointer to the Op Data
319 * @param[out] opResult Boolean to indicate the result of the operation
320 * @param[out] pOut0 Pointer to the flat buffer
321 * @param[out] pOut1 Pointer to the flat buffer
322 *
323 * @return void
324 *****************************************************************************/
325 void LacSync_GenDualFlatBufVerifyCb(void *callbackTag,
326 CpaStatus status,
327 void *pOpdata,
328 CpaBoolean opResult,
329 CpaFlatBuffer *pOut0,
330 CpaFlatBuffer *pOut1);
331
332 /**
333 *****************************************************************************
334 * @ingroup LacSync
335 * Generic wake up function.
336 * @description
337 * This function is used when the API is called in synchronous
338 * mode.
339 * It's assumed the callbackTag holds a lac_sync_op_data_t type
340 * and when the callback is received, this callback shall set
341 * the status element of that cookie structure and kick the
342 * sid.
343 * This function maybe called from an async callback.
344 *
345 * @param[in] callbackTag Callback Tag
346 * @param[in] status Status of callback
347 *
348 * @return void
349 *****************************************************************************/
350 void LacSync_GenWakeupSyncCaller(void *callbackTag, CpaStatus status);
351
352 /**
353 *****************************************************************************
354 * @ingroup LacSync
355 * Generic wake up verify function.
356 * @description
357 * This function is used when the API is called in synchronous
358 * mode.
359 * It's assumed the callbackTag holds a lac_sync_op_data_t type
360 * and when the callback is received, this callback shall set
361 * the status element and the opResult of that cookie structure
362 * and kick the sid.
363 * This function maybe called from an async callback.
364 *
365 * @param[in] callbackTag Callback Tag
366 * @param[in] status Status of callback
367 * @param[out] opResult Boolean to indicate the result of the operation
368 *
369 * @return void
370 *****************************************************************************/
371 void LacSync_GenVerifyWakeupSyncCaller(void *callbackTag,
372 CpaStatus status,
373 CpaBoolean opResult);
374
375 #endif /*LAC_SYNC_H*/
376