1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2022 Intel Corporation */
3 /* $FreeBSD$ */
4 
5 /**
6  *****************************************************************************
7  * @file lac_session.h
8  *
9  * @defgroup LacSym_Session Session
10  *
11  * @ingroup LacSym
12  *
13  * Definition of symmetric session descriptor structure
14  *
15  * @lld_start
16  *
17  * @lld_overview
18  * A session is required for each symmetric operation. The session descriptor
19  * holds information about the session from when the session is initialised to
20  * when the session is removed. The session descriptor is used in the
21  * subsequent perform operations in the paths for both sending the request and
22  * receiving the response. The session descriptor and any other state
23  * information required for processing responses from the QAT are stored in an
24  * internal cookie. A pointer to this cookie is stored in the opaque data
25  * field of the QAT request.
26  *
27  * The user allocates the memory for the session using the size returned from
28  * \ref cpaCySymSessionCtxGetSize(). Internally this memory is re-aligned on a
29  * 64 byte boundary for use by the QAT engine. The aligned pointer is saved in
30  * the first bytes (size of void *) of the session memory. This address
31  * is then dereferenced in subsequent performs to get access to the session
32  * descriptor.
33  *
34  * <b>LAC Session Init</b>\n The session descriptor is re-aligned and
35  * populated. This includes populating the content descriptor which contains
36  * the hardware setup for the QAT engine. The content descriptor is a read
37  * only structure after session init and a pointer to it is sent to the QAT
38  * for each perform operation.
39  *
40  * <b>LAC Perform </b>\n
41  * The address for the session descriptor is got by dereferencing the first
42  * bytes of the session memory (size of void *). For each successful
43  * request put on the ring, the pendingCbCount for the session is incremented.
44  *
45  * <b>LAC Callback </b>\n
46  * For each successful response the pendingCbCount for the session is
47  * decremented. See \ref LacSymCb_ProcessCallbackInternal()
48  *
49  * <b>LAC Session Remove </b>\n
50  * The address for the session descriptor is got by dereferencing the first
51  * bytes of the session memory (size of void *).
52  * The pendingCbCount for the session is checked to see if it is 0. If it is
53  * non 0 then there are requests in flight. An error is returned to the user.
54  *
55  * <b>Concurrency</b>\n
56  * A reference count is used to prevent the descriptor being removed
57  * while there are requests in flight.
58  *
59  * <b>Reference Count</b>\n
60  * - The perform funcion increments the reference count for the session.
61  * - The callback function decrements the reference count for the session.
62  * - The Remove function checks the reference count to ensure that it is 0.
63  *
64  * @lld_dependencies
65  * - \ref LacMem "Memory" - Inline memory functions
66  * - QatUtils: logging, locking & virt to phys translations.
67  *
68  * @lld_initialisation
69  *
70  * @lld_module_algorithms
71  *
72  * @lld_process_context
73  *
74  * @lld_end
75  *
76  *****************************************************************************/
77 
78 /***************************************************************************/
79 
80 #ifndef LAC_SYM_SESSION_H
81 #define LAC_SYM_SESSION_H
82 
83 /*
84  * Common alignment attributes to ensure
85  * hashStatePrefixBuffer is 64-byte aligned
86  */
87 #define ALIGN_START(x)
88 #define ALIGN_END(x) __attribute__((__aligned__(x)))
89 /*
90 ******************************************************************************
91 * Include public/global header files
92 ******************************************************************************
93 */
94 
95 #include "cpa.h"
96 #include "icp_accel_devices.h"
97 #include "lac_list.h"
98 #include "lac_sal_types.h"
99 #include "sal_qat_cmn_msg.h"
100 #include "lac_sym_cipher_defs.h"
101 #include "lac_sym.h"
102 #include "lac_sym_hash_defs.h"
103 #include "lac_sym_qat_hash.h"
104 
105 /*
106 *******************************************************************************
107 * Include private header files
108 *******************************************************************************
109 */
110 /**
111 *****************************************************************************
112 * @ingroup LacSym
113 *      Spc state
114 *
115 * @description
116 *      This enum is used to indicate the Spc state.
117 *
118 *****************************************************************************/
119 typedef enum lac_single_pass_state_e {
120 	NON_SPC,    /* Algorithms other than CHACHA-POLY and AES-GCM */
121 	LIKELY_SPC, /* AES-GCM - Likely to handle it as single pass  */
122 	SPC	    /* CHACHA-POLY and AES-GCM */
123 } lac_single_pass_state_t;
124 
125 /**
126 *******************************************************************************
127 * @ingroup LacSym_Session
128 *      Symmetric session descriptor
129 * @description
130 *      This structure stores information about a session
131 *      Note: struct types lac_session_d1_s and lac_session_d2_s are subsets of
132 *      this structure. Elements in all three should retain the same order
133 *      Only this structure is used in the session init call. The other two are
134 *      for determining the size of memory to allocate.
135 *      The comments section of each of the other two structures below show
136 *      the conditions that determine which session context memory size to use.
137 *****************************************************************************/
138 typedef struct lac_session_desc_s {
139 	Cpa8U contentDescriptor[LAC_SYM_QAT_CONTENT_DESC_MAX_SIZE];
140 	/**< QAT Content Descriptor for this session.
141 	 * NOTE: Field must be correctly aligned in memory for access by QAT
142 	 * engine
143 	 */
144 	Cpa8U contentDescriptorOptimised[LAC_SYM_OPTIMISED_CD_SIZE];
145 	/**< QAT Optimised Content Descriptor for this session.
146 	 * NOTE: Field must be correctly aligned in memory for access by QAT
147 	 * engine
148 	 */
149 	CpaCySymOp symOperation;
150 	/**< type of command to be performed */
151 	sal_qat_content_desc_info_t contentDescInfo;
152 	/**< info on the content descriptor */
153 	sal_qat_content_desc_info_t contentDescOptimisedInfo;
154 	/**< info on the optimised content descriptor */
155 	icp_qat_fw_la_cmd_id_t laCmdId;
156 	/**<Command Id for the QAT FW */
157 	lac_sym_qat_hash_state_buffer_info_t hashStateBufferInfo;
158 	/**< info on the hash state prefix buffer */
159 	CpaCySymHashAlgorithm hashAlgorithm;
160 	/**< hash algorithm */
161 	Cpa32U authKeyLenInBytes;
162 	/**< Authentication key length in bytes */
163 	CpaCySymHashMode hashMode;
164 	/**< Mode of the hash operation. plain, auth or nested */
165 	Cpa32U hashResultSize;
166 	/**< size of the digest produced/compared in bytes */
167 	CpaCySymCipherAlgorithm cipherAlgorithm;
168 	/**< Cipher algorithm and mode */
169 	Cpa32U cipherKeyLenInBytes;
170 	/**< Cipher key length in bytes */
171 	CpaCySymCipherDirection cipherDirection;
172 	/**< This parameter determines if the cipher operation is an encrypt or
173 	 * a decrypt operation. */
174 	CpaCySymPacketType partialState;
175 	/**< state of the partial packet. This can be written to by the perform
176 	 * because the SpinLock pPartialInFlightSpinlock guarantees that the
177 	 * state is accessible in only one place at a time. */
178 	icp_qat_la_bulk_req_hdr_t reqCacheHdr;
179 	icp_qat_fw_la_key_gen_common_t reqCacheMid;
180 	icp_qat_la_bulk_req_ftr_t reqCacheFtr;
181 	/**< Cache as much as possible of the bulk request in a pre built
182 	 * request (header, mid & footer). */
183 	CpaCySymCbFunc pSymCb;
184 	/**< symmetric function callback pointer */
185 	union {
186 		QatUtilsAtomic pendingCbCount;
187 		/**< Keeps track of number of pending requests.  */
188 		QatUtilsAtomic pendingDpCbCount;
189 		/**< Keeps track of number of pending DP requests (not thread
190 		 * safe)*/
191 	} u;
192 	struct lac_sym_bulk_cookie_s *pRequestQueueHead;
193 	/**< A fifo list of queued QAT requests. Head points to first queue
194 	 * entry */
195 	struct lac_sym_bulk_cookie_s *pRequestQueueTail;
196 	/**< A fifo list of queued QAT requests. Tail points to last queue entry
197 	 */
198 	struct mtx requestQueueLock;
199 	/**< A lock to protect accesses to the above request queue  */
200 	CpaInstanceHandle pInstance;
201 	/**< Pointer to Crypto instance running this session. */
202 	CpaBoolean isAuthEncryptOp : 1;
203 	/**< if the algorithm chaining operation is auth encrypt */
204 	CpaBoolean nonBlockingOpsInProgress : 1;
205 	/**< Flag is set if a non blocking operation is in progress for a
206 	 * session.
207 	 * If set to false, new requests will be queued until the condition is
208 	 * cleared.
209 	 * ASSUMPTION: Only one blocking condition per session can exist at any
210 	 * time
211 	 */
212 	CpaBoolean internalSession : 1;
213 	/**< Flag which is set if the session was set up internally for DRBG */
214 	CpaBoolean isDPSession : 1;
215 	/**< Flag which is set if the session was set up for Data Plane */
216 	CpaBoolean digestVerify : 1;
217 	/**< Session digest verify for data plane and for CCM/GCM for trad
218 	 * api. For other cases on trad api this flag is set in each performOp
219 	 */
220 	CpaBoolean digestIsAppended : 1;
221 	/**< Flag indicating whether the digest is appended immediately
222 	 * following
223 	 * the region over which the digest is computed */
224 	CpaBoolean isCipher : 1;
225 	/**< Flag indicating whether symOperation includes a cipher operation */
226 	CpaBoolean isAuth : 1;
227 	/**< Flag indicating whether symOperation includes an auth operation */
228 	CpaBoolean useSymConstantsTable : 1;
229 	/**< Flag indicating whether the SymConstantsTable can be used or not */
230 	CpaBoolean useOptimisedContentDesc : 1;
231 	/**< Flag indicating whether to use the optimised CD or not */
232 	CpaBoolean isPartialSupported : 1;
233 	/**< Flag indicating whether symOperation support partial packet */
234 	CpaBoolean useStatefulSha3ContentDesc : 1;
235 	/**< Flag indicating whether to use the stateful SHA3 CD or not */
236 	icp_qat_la_bulk_req_hdr_t shramReqCacheHdr;
237 	icp_qat_fw_la_key_gen_common_t shramReqCacheMid;
238 	icp_qat_la_bulk_req_ftr_t shramReqCacheFtr;
239 	/**< Alternative pre-built request (header, mid & footer)
240 	 * for use with symConstantsTable. */
241 	lac_single_pass_state_t singlePassState;
242 	/**< Flag indicating whether symOperation support single pass */
243 	icp_qat_fw_serv_specif_flags laCmdFlags;
244 	/**< Common request - Service specific flags type  */
245 	icp_qat_fw_comn_flags cmnRequestFlags;
246 	/**< Common request flags type  */
247 	icp_qat_fw_ext_serv_specif_flags laExtCmdFlags;
248 	/**< Common request - Service specific flags type  */
249 	icp_qat_la_bulk_req_hdr_t reqSpcCacheHdr;
250 	icp_qat_la_bulk_req_ftr_t reqSpcCacheFtr;
251 	/**< request (header & footer)for use with Single Pass. */
252 	icp_qat_hw_auth_mode_t qatHashMode;
253 	/**< Hash Mode for the qat slices. Not to be confused with QA-API
254 	 * hashMode
255 	 */
256 	Cpa32U cipherSliceType;
257 	/**< Cipher slice type to be used, set at init session time */
258 	Cpa8U cipherAesXtsKey1Forward[LAC_CIPHER_AES_XTS_KEY_MAX_LENGTH];
259 	/**< Cached AES XTS Forward key
260 	 * For CPM2.0 AES XTS key convertion need to be done in SW.
261 	 * Because use can update session direction at any time,
262 	 * also forward key needs to be cached
263 	 */
264 	Cpa8U cipherAesXtsKey1Reverse[LAC_CIPHER_AES_XTS_KEY_MAX_LENGTH];
265 	/**< AES XTS Reverse key
266 	 * For CPM2.0 AES XTS key convertion need to be done in SW.
267 	 * Reverse key always will be calcilated at session setup time and
268 	 * cached to be used when needed */
269 	Cpa8U cipherAesXtsKey2[LAC_CIPHER_AES_XTS_KEY_MAX_LENGTH];
270 	/**< For AES XTS session need to store Key2 value in order to generate
271 	 * tweak
272 	 */
273 	void *writeRingMsgFunc;
274 	/**< function which will be called to write ring message */
275 	Cpa32U aadLenInBytes;
276 	/**< For CCM,GCM and Snow3G cases, this parameter holds the AAD size,
277 	 * otherwise it is set to zero */
278 	ALIGN_START(64)
279 	Cpa8U hashStatePrefixBuffer[LAC_MAX_AAD_SIZE_BYTES] ALIGN_END(64);
280 	/**< hash state prefix buffer used for hash operations - AAD only
281 	 * NOTE: Field must be correctly aligned in memory for access by QAT
282 	 * engine
283 	 */
284 	Cpa8U hashStatePrefixBufferExt[LAC_MAX_HASH_STATE_BUFFER_SIZE_BYTES -
285 				       LAC_MAX_AAD_SIZE_BYTES];
286 	/**< hash state prefix buffer used for hash operations - Remainder of
287 	 * array.
288 	 * NOTE: Field must be correctly aligned in memory for access by QAT
289 	 * engine
290 	 */
291 	Cpa8U cipherPartialOpState[LAC_CIPHER_STATE_SIZE_MAX];
292 	/**< Buffer to hold the cipher state for the session (for partial ops).
293 	 * NOTE: Field must be correctly aligned in memory for access by QAT
294 	 * engine
295 	 */
296 	Cpa8U cipherARC4InitialState[LAC_CIPHER_ARC4_STATE_LEN_BYTES];
297 	/**< Buffer to hold the initial ARC4 cipher state for the session, which
298 	 * is derived from the user-supplied base key during session
299 	 * registration.
300 	 * NOTE: Field must be correctly aligned in memory for access by QAT
301 	 * engine
302 	 */
303 	CpaPhysicalAddr cipherARC4InitialStatePhysAddr;
304 	/**< The physical address of the ARC4 initial state, set at init
305 	** session time .
306 	*/
307 } lac_session_desc_t;
308 
309 /**
310 *******************************************************************************
311 * @ingroup LacSym_Session
312 *      Symmetric session descriptor - d1
313 * @description
314 *      This structure stores information about a specific session which
315 *       assumes the following:
316 *      - cipher algorithm is not ARC4 or Snow3G
317 *      - partials not used
318 *      - not AuthEncrypt operation
319 *      - hash mode not Auth or Nested
320 *      - no hashStatePrefixBuffer required
321 *      It is therefore a subset of the standard symmetric session descriptor,
322 *       with a smaller memory footprint
323 *****************************************************************************/
324 typedef struct lac_session_desc_d1_s {
325 	Cpa8U contentDescriptor[LAC_SYM_QAT_CONTENT_DESC_MAX_SIZE];
326 	/**< QAT Content Descriptor for this session.
327 	 * NOTE: Field must be correctly aligned in memory for access by QAT
328 	 * engine
329 	 */
330 	Cpa8U contentDescriptorOptimised[LAC_SYM_OPTIMISED_CD_SIZE];
331 	/**< QAT Optimised Content Descriptor for this session.
332 	 * NOTE: Field must be correctly aligned in memory for access by QAT
333 	 * engine
334 	 */
335 	CpaCySymOp symOperation;
336 	/**< type of command to be performed */
337 	sal_qat_content_desc_info_t contentDescInfo;
338 	/**< info on the content descriptor */
339 	sal_qat_content_desc_info_t contentDescOptimisedInfo;
340 	/**< info on the optimised content descriptor */
341 	icp_qat_fw_la_cmd_id_t laCmdId;
342 	/**<Command Id for the QAT FW */
343 	lac_sym_qat_hash_state_buffer_info_t hashStateBufferInfo;
344 	/**< info on the hash state prefix buffer */
345 	CpaCySymHashAlgorithm hashAlgorithm;
346 	/**< hash algorithm */
347 	Cpa32U authKeyLenInBytes;
348 	/**< Authentication key length in bytes */
349 	CpaCySymHashMode hashMode;
350 	/**< Mode of the hash operation. plain, auth or nested */
351 	Cpa32U hashResultSize;
352 	/**< size of the digest produced/compared in bytes */
353 	CpaCySymCipherAlgorithm cipherAlgorithm;
354 	/**< Cipher algorithm and mode */
355 	Cpa32U cipherKeyLenInBytes;
356 	/**< Cipher key length in bytes */
357 	CpaCySymCipherDirection cipherDirection;
358 	/**< This parameter determines if the cipher operation is an encrypt or
359 	 * a decrypt operation. */
360 	CpaCySymPacketType partialState;
361 	/**< state of the partial packet. This can be written to by the perform
362 	 * because the SpinLock pPartialInFlightSpinlock guarantees that the
363 	 * state is accessible in only one place at a time. */
364 	icp_qat_la_bulk_req_hdr_t reqCacheHdr;
365 	icp_qat_fw_la_key_gen_common_t reqCacheMid;
366 	icp_qat_la_bulk_req_ftr_t reqCacheFtr;
367 	/**< Cache as much as possible of the bulk request in a pre built
368 	 * request (header, mid & footer). */
369 	CpaCySymCbFunc pSymCb;
370 	/**< symmetric function callback pointer */
371 	union {
372 		QatUtilsAtomic pendingCbCount;
373 		/**< Keeps track of number of pending requests.  */
374 		Cpa64U pendingDpCbCount;
375 		/**< Keeps track of number of pending DP requests (not thread
376 		 * safe)*/
377 	} u;
378 	struct lac_sym_bulk_cookie_s *pRequestQueueHead;
379 	/**< A fifo list of queued QAT requests. Head points to first queue
380 	 * entry */
381 	struct lac_sym_bulk_cookie_s *pRequestQueueTail;
382 	/**< A fifo list of queued QAT requests. Tail points to last queue entry
383 	 */
384 	struct mtx requestQueueLock;
385 	/**< A lock to protect accesses to the above request queue  */
386 	CpaInstanceHandle pInstance;
387 	/**< Pointer to Crypto instance running this session. */
388 	CpaBoolean isAuthEncryptOp : 1;
389 	/**< if the algorithm chaining operation is auth encrypt */
390 	CpaBoolean nonBlockingOpsInProgress : 1;
391 	/**< Flag is set if a non blocking operation is in progress for a
392 	 * session.
393 	 * If set to false, new requests will be queued until the condition is
394 	 * cleared.
395 	 * ASSUMPTION: Only one blocking condition per session can exist at any
396 	 * time
397 	 */
398 	CpaBoolean internalSession : 1;
399 	/**< Flag which is set if the session was set up internally for DRBG */
400 	CpaBoolean isDPSession : 1;
401 	/**< Flag which is set if the session was set up for Data Plane */
402 	CpaBoolean digestVerify : 1;
403 	/**< Session digest verify for data plane and for CCM/GCM for trad
404 	 * api. For other cases on trad api this flag is set in each performOp
405 	 */
406 	CpaBoolean digestIsAppended : 1;
407 	/**< Flag indicating whether the digest is appended immediately
408 	 * following
409 	 * the region over which the digest is computed */
410 	CpaBoolean isCipher : 1;
411 	/**< Flag indicating whether symOperation includes a cipher operation */
412 	CpaBoolean isAuth : 1;
413 	/**< Flag indicating whether symOperation includes an auth operation */
414 	CpaBoolean useSymConstantsTable : 1;
415 	/**< Flag indicating whether the SymConstantsTable can be used or not */
416 	CpaBoolean useOptimisedContentDesc : 1;
417 	/**< Flag indicating whether to use the optimised CD or not */
418 	CpaBoolean isPartialSupported : 1;
419 	/**< Flag indicating whether symOperation support partial packet */
420 	CpaBoolean useStatefulSha3ContentDesc : 1;
421 	/**< Flag indicating whether to use the stateful SHA3 CD or not */
422 	icp_qat_la_bulk_req_hdr_t shramReqCacheHdr;
423 	icp_qat_fw_la_key_gen_common_t shramReqCacheMid;
424 	icp_qat_la_bulk_req_ftr_t shramReqCacheFtr;
425 	/**< Alternative pre-built request (header, mid & footer)
426 	 * for use with symConstantsTable. */
427 	lac_single_pass_state_t singlePassState;
428 	/**< Flag indicating whether symOperation support single pass */
429 	icp_qat_fw_serv_specif_flags laCmdFlags;
430 	/**< Common request - Service specific flags type  */
431 	icp_qat_fw_comn_flags cmnRequestFlags;
432 	/**< Common request flags type  */
433 	icp_qat_fw_ext_serv_specif_flags laExtCmdFlags;
434 	/**< Common request - Service specific flags type  */
435 	icp_qat_la_bulk_req_hdr_t reqSpcCacheHdr;
436 	icp_qat_la_bulk_req_ftr_t reqSpcCacheFtr;
437 	/**< request (header & footer)for use with Single Pass. */
438 	icp_qat_hw_auth_mode_t qatHashMode;
439 	/**< Hash Mode for the qat slices. Not to be confused with QA-API
440 	 * hashMode
441 	 */
442 	Cpa32U cipherSliceType;
443 	/**< Cipher slice type to be used, set at init session time */
444 	Cpa8U cipherAesXtsKey1Forward[LAC_CIPHER_AES_XTS_KEY_MAX_LENGTH];
445 	/**< Cached AES XTS Forward key
446 	 * For CPM2.0 AES XTS key convertion need to be done in SW.
447 	 * Because use can update session direction at any time,
448 	 * also forward key needs to be cached
449 	 */
450 	Cpa8U cipherAesXtsKey1Reverse[LAC_CIPHER_AES_XTS_KEY_MAX_LENGTH];
451 	/**< AES XTS Reverse key
452 	 * For CPM2.0 AES XTS key convertion need to be done in SW.
453 	 * Reverse key always will be calcilated at session setup time and
454 	 * cached to be used when needed */
455 	Cpa8U cipherAesXtsKey2[LAC_CIPHER_AES_XTS_KEY_MAX_LENGTH];
456 	/**< For AES XTS session need to store Key2 value in order to generate
457 	 * tweak
458 	 */
459 	void *writeRingMsgFunc;
460 	/**< function which will be called to write ring message */
461 } lac_session_desc_d1_t;
462 
463 /**
464 *******************************************************************************
465 * @ingroup LacSym_Session
466 *      Symmetric session descriptor - d2
467 * @description
468 *      This structure stores information about a specific session which
469 *       assumes the following:
470 *      - authEncrypt only
471 *      - partials not used
472 *      - hasStatePrefixBuffer just contains AAD
473 *      It is therefore a subset of the standard symmetric session descriptor,
474 *       with a smaller memory footprint
475 *****************************************************************************/
476 typedef struct lac_session_desc_d2_s {
477 	Cpa8U contentDescriptor[LAC_SYM_QAT_CONTENT_DESC_MAX_SIZE];
478 	/**< QAT Content Descriptor for this session.
479 	 * NOTE: Field must be correctly aligned in memory for access by QAT
480 	 * engine
481 	 */
482 	Cpa8U contentDescriptorOptimised[LAC_SYM_OPTIMISED_CD_SIZE];
483 	/**< QAT Optimised Content Descriptor for this session.
484 	 * NOTE: Field must be correctly aligned in memory for access by QAT
485 	 * engine
486 	 */
487 	CpaCySymOp symOperation;
488 	/**< type of command to be performed */
489 	sal_qat_content_desc_info_t contentDescInfo;
490 	/**< info on the content descriptor */
491 	sal_qat_content_desc_info_t contentDescOptimisedInfo;
492 	/**< info on the optimised content descriptor */
493 	icp_qat_fw_la_cmd_id_t laCmdId;
494 	/**<Command Id for the QAT FW */
495 	lac_sym_qat_hash_state_buffer_info_t hashStateBufferInfo;
496 	/**< info on the hash state prefix buffer */
497 	CpaCySymHashAlgorithm hashAlgorithm;
498 	/**< hash algorithm */
499 	Cpa32U authKeyLenInBytes;
500 	/**< Authentication key length in bytes */
501 	CpaCySymHashMode hashMode;
502 	/**< Mode of the hash operation. plain, auth or nested */
503 	Cpa32U hashResultSize;
504 	/**< size of the digest produced/compared in bytes */
505 	CpaCySymCipherAlgorithm cipherAlgorithm;
506 	/**< Cipher algorithm and mode */
507 	Cpa32U cipherKeyLenInBytes;
508 	/**< Cipher key length in bytes */
509 	CpaCySymCipherDirection cipherDirection;
510 	/**< This parameter determines if the cipher operation is an encrypt or
511 	 * a decrypt operation. */
512 	CpaCySymPacketType partialState;
513 	/**< state of the partial packet. This can be written to by the perform
514 	 * because the SpinLock pPartialInFlightSpinlock guarantees that the
515 	 * state is accessible in only one place at a time. */
516 	icp_qat_la_bulk_req_hdr_t reqCacheHdr;
517 	icp_qat_fw_la_key_gen_common_t reqCacheMid;
518 	icp_qat_la_bulk_req_ftr_t reqCacheFtr;
519 	/**< Cache as much as possible of the bulk request in a pre built
520 	 * request (header. mid & footer). */
521 	CpaCySymCbFunc pSymCb;
522 	/**< symmetric function callback pointer */
523 	union {
524 		QatUtilsAtomic pendingCbCount;
525 		/**< Keeps track of number of pending requests.  */
526 		Cpa64U pendingDpCbCount;
527 		/**< Keeps track of number of pending DP requests (not thread
528 		 * safe)*/
529 	} u;
530 	struct lac_sym_bulk_cookie_s *pRequestQueueHead;
531 	/**< A fifo list of queued QAT requests. Head points to first queue
532 	 * entry */
533 	struct lac_sym_bulk_cookie_s *pRequestQueueTail;
534 	/**< A fifo list of queued QAT requests. Tail points to last queue entry
535 	 */
536 	struct mtx requestQueueLock;
537 	/**< A lock to protect accesses to the above request queue  */
538 	CpaInstanceHandle pInstance;
539 	/**< Pointer to Crypto instance running this session. */
540 	CpaBoolean isAuthEncryptOp : 1;
541 	/**< if the algorithm chaining operation is auth encrypt */
542 	CpaBoolean nonBlockingOpsInProgress : 1;
543 	/**< Flag is set if a non blocking operation is in progress for a
544 	 * session.
545 	 * If set to false, new requests will be queued until the condition is
546 	 * cleared.
547 	 * ASSUMPTION: Only one blocking condition per session can exist at any
548 	 * time
549 	 */
550 	CpaBoolean internalSession : 1;
551 	/**< Flag which is set if the session was set up internally for DRBG */
552 	CpaBoolean isDPSession : 1;
553 	/**< Flag which is set if the session was set up for Data Plane */
554 	CpaBoolean digestVerify : 1;
555 	/**< Session digest verify for data plane and for CCM/GCM for trad
556 	 * api. For other cases on trad api this flag is set in each performOp
557 	 */
558 	CpaBoolean digestIsAppended : 1;
559 	/**< Flag indicating whether the digest is appended immediately
560 	 * following
561 	 * the region over which the digest is computed */
562 	CpaBoolean isCipher : 1;
563 	/**< Flag indicating whether symOperation includes a cipher operation */
564 	CpaBoolean isAuth : 1;
565 	/**< Flag indicating whether symOperation includes an auth operation */
566 	CpaBoolean useSymConstantsTable : 1;
567 	/**< Flag indicating whether the SymConstantsTable can be used or not */
568 	CpaBoolean useOptimisedContentDesc : 1;
569 	/**< Flag indicating whether to use the optimised CD or not */
570 	CpaBoolean isPartialSupported : 1;
571 	/**< Flag indicating whether symOperation support partial packet */
572 	CpaBoolean useStatefulSha3ContentDesc : 1;
573 	/**< Flag indicating whether to use the stateful SHA3 CD or not */
574 	icp_qat_la_bulk_req_hdr_t shramReqCacheHdr;
575 	icp_qat_fw_la_key_gen_common_t shramReqCacheMid;
576 	icp_qat_la_bulk_req_ftr_t shramReqCacheFtr;
577 	/**< Alternative pre-built request (header. mid & footer)
578 	 * for use with symConstantsTable. */
579 	lac_single_pass_state_t singlePassState;
580 	/**< Flag indicating whether symOperation support single pass */
581 	icp_qat_fw_serv_specif_flags laCmdFlags;
582 	/**< Common request - Service specific flags type  */
583 	icp_qat_fw_comn_flags cmnRequestFlags;
584 	/**< Common request flags type  */
585 	icp_qat_fw_ext_serv_specif_flags laExtCmdFlags;
586 	/**< Common request - Service specific flags type  */
587 	icp_qat_la_bulk_req_hdr_t reqSpcCacheHdr;
588 	icp_qat_la_bulk_req_ftr_t reqSpcCacheFtr;
589 	/**< request (header & footer)for use with Single Pass. */
590 	icp_qat_hw_auth_mode_t qatHashMode;
591 	/**< Hash Mode for the qat slices. Not to be confused with QA-API
592 	 * hashMode
593 	 */
594 	Cpa32U cipherSliceType;
595 	/**< Cipher slice type to be used, set at init session time */
596 	Cpa8U cipherAesXtsKey1Forward[LAC_CIPHER_AES_XTS_KEY_MAX_LENGTH];
597 	/**< Cached AES XTS Forward key
598 	 * For CPM2.0 AES XTS key convertion need to be done in SW.
599 	 * Because use can update session direction at any time,
600 	 * also forward key needs to be cached
601 	 */
602 	Cpa8U cipherAesXtsKey1Reverse[LAC_CIPHER_AES_XTS_KEY_MAX_LENGTH];
603 	/**< AES XTS Reverse key
604 	 * For CPM2.0 AES XTS key convertion need to be done in SW.
605 	 * Reverse key always will be calcilated at session setup time and
606 	 * cached to be used when needed */
607 	Cpa8U cipherAesXtsKey2[LAC_CIPHER_AES_XTS_KEY_MAX_LENGTH];
608 	/**< For AES XTS session need to store Key2 value in order to generate
609 	 * tweak
610 	 */
611 	void *writeRingMsgFunc;
612 	/**< function which will be called to write ring message */
613 	Cpa32U aadLenInBytes;
614 	/**< For CCM,GCM and Snow3G cases, this parameter holds the AAD size,
615 	 * otherwise it is set to zero */
616 	ALIGN_START(64)
617 	Cpa8U hashStatePrefixBuffer[LAC_MAX_AAD_SIZE_BYTES] ALIGN_END(64);
618 	/**< hash state prefix buffer used for hash operations - AAD only
619 	 * NOTE: Field must be correctly aligned in memory for access by QAT
620 	 * engine
621 	 */
622 } lac_session_desc_d2_t;
623 
624 #define LAC_SYM_SESSION_SIZE                                                   \
625 	(sizeof(lac_session_desc_t) + LAC_64BYTE_ALIGNMENT +                   \
626 	 sizeof(LAC_ARCH_UINT))
627 /**< @ingroup LacSym_Session
628  * Size of the memory that the client has to allocate for a session. Extra
629  * memory is needed to internally re-align the data. The pointer to the algined
630  * data is stored at the start of the user allocated memory hence the extra
631  * space for an LAC_ARCH_UINT */
632 
633 #define LAC_SYM_SESSION_D1_SIZE                                                \
634 	(sizeof(lac_session_desc_d1_t) + LAC_64BYTE_ALIGNMENT +                \
635 	 sizeof(LAC_ARCH_UINT))
636 /**< @ingroup LacSym_Session
637 **  Size of the memory that the client has to allocate for a session where :
638 *     - cipher algorithm not ARC4 or Snow3G, no Partials, nonAuthEncrypt.
639 * Extra memory is needed to internally re-align the data. The pointer to the
640 * aligned data is stored at the start of the user allocated memory hence the
641 * extra space for an LAC_ARCH_UINT */
642 
643 #define LAC_SYM_SESSION_D2_SIZE                                                \
644 	(sizeof(lac_session_desc_d2_t) + LAC_64BYTE_ALIGNMENT +                \
645 	 sizeof(LAC_ARCH_UINT))
646 /**< @ingroup LacSym_Session
647 **  Size of the memory that the client has to allocate for a session where :
648 *     - authEncrypt, no Partials - so hashStatePrefixBuffer is only AAD
649 * Extra memory is needed to internally re-align the data. The pointer to the
650 * aligned data is stored at the start of the user allocated memory hence the
651 * extra space for an LAC_ARCH_UINT */
652 
653 #define LAC_SYM_SESSION_DESC_FROM_CTX_GET(pSession)                            \
654 	(lac_session_desc_t *)(*(LAC_ARCH_UINT *)pSession)
655 /**< @ingroup LacSym_Session
656  * Retrieve the session descriptor pointer from the session context structure
657  * that the user allocates. The pointer to the internally realigned address
658  * is stored at the start of the session context that the user allocates */
659 
660 /**
661 *******************************************************************************
662 * @ingroup LacSym_Session
663 *      This function initializes a session
664 *
665 * @description
666 *      This function is called from the LAC session register API functions.
667 *      It validates all input parameters. If an invalid parameter is passed,
668 *      an error is returned to the calling function. If all parameters are valid
669 *      a symmetric session is initialized
670 *
671 * @param[in] instanceHandle_in    Instance Handle
672 * @param[in] pSymCb               callback function
673 * @param[in] pSessionSetupData    pointer to the strucutre containing the setup
674 *data
675 * @param[in] isDpSession          CPA_TRUE for a data plane session
676 * @param[out] pSessionCtx         Pointer to session context
677 *
678 *
679 * @retval CPA_STATUS_SUCCESS        Function executed successfully.
680 * @retval CPA_STATUS_FAIL           Function failed.
681 * @retval CPA_STATUS_INVALID_PARAM  Invalid parameter passed in.
682 * @retval CPA_STATUS_RESOURCE       Error related to system resources.
683 *
684 */
685 
686 CpaStatus LacSym_InitSession(const CpaInstanceHandle instanceHandle_in,
687 			     const CpaCySymCbFunc pSymCb,
688 			     const CpaCySymSessionSetupData *pSessionSetupData,
689 			     const CpaBoolean isDpSession,
690 			     CpaCySymSessionCtx pSessionCtx);
691 
692 #endif /* LAC_SYM_SESSION_H */
693