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