1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2022 Intel Corporation */
3 /* $FreeBSD$ */
4 /**
5  *****************************************************************************
6  * @file dc_session.h
7  *
8  * @ingroup Dc_DataCompression
9  *
10  * @description
11  *      Definition of the Data Compression session parameters.
12  *
13  *****************************************************************************/
14 #ifndef DC_SESSION_H
15 #define DC_SESSION_H
16 
17 #include "cpa_dc_dp.h"
18 #include "icp_qat_fw_comp.h"
19 #include "sal_qat_cmn_msg.h"
20 #include "sal_types_compression.h"
21 
22 /* Maximum number of intermediate buffers SGLs for devices
23  * with a maximum of 6 compression slices */
24 #define DC_QAT_MAX_NUM_INTER_BUFFERS_6COMP_SLICES (12)
25 
26 /* Maximum number of intermediate buffers SGLs for devices
27  * with a maximum of 10 max compression slices */
28 #define DC_QAT_MAX_NUM_INTER_BUFFERS_10COMP_SLICES (20)
29 
30 /* Maximum number of intermediate buffers SGLs for devices
31  * with a maximum of 24 max compression slices and 32 MEs */
32 #define DC_QAT_MAX_NUM_INTER_BUFFERS_24COMP_SLICES (64)
33 
34 /* Maximum size of the state registers 64 bytes */
35 #define DC_QAT_STATE_REGISTERS_MAX_SIZE (64)
36 
37 /* Size of the history window.
38  * Base 2 logarithm of maximum window size minus 8 */
39 #define DC_4K_WINDOW_SIZE (4)
40 #define DC_8K_WINDOW_SIZE (5)
41 #define DC_16K_WINDOW_SIZE (6)
42 #define DC_32K_WINDOW_SIZE (7)
43 
44 /* Context size */
45 #define DC_DEFLATE_MAX_CONTEXT_SIZE (49152)
46 #define DC_INFLATE_CONTEXT_SIZE (36864)
47 
48 #define DC_DEFLATE_EH_MAX_CONTEXT_SIZE (65536)
49 #define DC_DEFLATE_EH_MIN_CONTEXT_SIZE (49152)
50 #define DC_INFLATE_EH_CONTEXT_SIZE (34032)
51 
52 /* Retrieve the session descriptor pointer from the session context structure
53  * that the user allocates. The pointer to the internally realigned address
54  * is stored at the start of the session context that the user allocates */
55 #define DC_SESSION_DESC_FROM_CTX_GET(pSession)                                 \
56 	(dc_session_desc_t *)(*(LAC_ARCH_UINT *)pSession)
57 
58 /* Maximum size for the compression part of the content descriptor */
59 #define DC_QAT_COMP_CONTENT_DESC_SIZE sizeof(icp_qat_fw_comp_cd_hdr_t)
60 
61 /* Maximum size for the translator part of the content descriptor */
62 #define DC_QAT_TRANS_CONTENT_DESC_SIZE                                         \
63 	(sizeof(icp_qat_fw_xlt_cd_hdr_t) + DC_QAT_MAX_TRANS_SETUP_BLK_SZ)
64 
65 /* Maximum size of the decompression content descriptor */
66 #define DC_QAT_CONTENT_DESC_DECOMP_MAX_SIZE                                    \
67 	LAC_ALIGN_POW2_ROUNDUP(DC_QAT_COMP_CONTENT_DESC_SIZE,                  \
68 			       (1 << LAC_64BYTE_ALIGNMENT_SHIFT))
69 
70 /* Maximum size of the compression content descriptor */
71 #define DC_QAT_CONTENT_DESC_COMP_MAX_SIZE                                      \
72 	LAC_ALIGN_POW2_ROUNDUP(DC_QAT_COMP_CONTENT_DESC_SIZE +                 \
73 				   DC_QAT_TRANS_CONTENT_DESC_SIZE,             \
74 			       (1 << LAC_64BYTE_ALIGNMENT_SHIFT))
75 
76 /* Direction of the request */
77 typedef enum dc_request_dir_e {
78 	DC_COMPRESSION_REQUEST = 1,
79 	DC_DECOMPRESSION_REQUEST
80 } dc_request_dir_t;
81 
82 /* Type of the compression request */
83 typedef enum dc_request_type_e {
84 	DC_REQUEST_FIRST = 1,
85 	DC_REQUEST_SUBSEQUENT
86 } dc_request_type_t;
87 
88 typedef enum dc_block_type_e {
89 	DC_CLEARTEXT_TYPE = 0,
90 	DC_STATIC_TYPE,
91 	DC_DYNAMIC_TYPE
92 } dc_block_type_t;
93 
94 /* Internal data structure supporting end to end data integrity checks. */
95 typedef struct dc_integrity_crc_fw_s {
96 	Cpa32U crc32;
97 	/* CRC32 checksum returned for compressed data */
98 	Cpa32U adler32;
99 	/* ADLER32 checksum returned for compressed data */
100 
101 	union {
102 		struct {
103 			Cpa32U oCrc32Cpr;
104 			/* CRC32 checksum returned for data output by
105 			 * compression accelerator */
106 			Cpa32U iCrc32Cpr;
107 			/* CRC32 checksum returned for input data to compression
108 			 * accelerator
109 			 */
110 			Cpa32U oCrc32Xlt;
111 			/* CRC32 checksum returned for data output by translator
112 			 * accelerator
113 			 */
114 			Cpa32U iCrc32Xlt;
115 			/* CRC32 checksum returned for input data to translator
116 			 * accelerator
117 			 */
118 			Cpa32U xorFlags;
119 			/* Initialise transactor pCRC controls in state register
120 			 */
121 			Cpa32U crcPoly;
122 			/* CRC32 polynomial used by hardware */
123 			Cpa32U xorOut;
124 			/* CRC32 from XOR stage (Input CRC is xor'ed with value
125 			 * in the state) */
126 			Cpa32U deflateBlockType;
127 			/* Bit 1 - Bit 0
128 			 *   0        0 -> RAW DATA + Deflate header.
129 			 *                 This will not produced any CRC check
130 			 *                 because the output will not come
131 			 *                 from the slices. It will be a simple
132 			 *                 copy from input to output buffer
133 			 * list. 0        1 -> Static deflate block type 1 0 ->
134 			 * Dynamic deflate block type 1        1 -> Invalid type
135 			 */
136 		};
137 
138 		struct {
139 			Cpa64U iCrc64Cpr;
140 			/* CRC64 checksum returned for input data to compression
141 			 * accelerator
142 			 */
143 			Cpa64U oCrc64Cpr;
144 			/* CRC64 checksum returned for data output by
145 			 * compression accelerator */
146 			Cpa64U iCrc64Xlt;
147 			/* CRC64 checksum returned for input data to translator
148 			 * accelerator
149 			 */
150 			Cpa64U oCrc64Xlt;
151 			/* CRC64 checksum returned for data output by translator
152 			 * accelerator
153 			 */
154 			Cpa64U crc64Poly;
155 			/* CRC64 polynomial used by hardware */
156 			Cpa64U xor64Out;
157 			/* CRC64 from XOR stage (Input CRC is xor'ed with value
158 			 * in the state) */
159 		};
160 	};
161 } dc_integrity_crc_fw_t;
162 
163 typedef struct dc_sw_checksums_s {
164 	union {
165 		struct {
166 			Cpa32U swCrc32I;
167 			Cpa32U swCrc32O;
168 		};
169 
170 		struct {
171 			Cpa64U swCrc64I;
172 			Cpa64U swCrc64O;
173 		};
174 	};
175 } dc_sw_checksums_t;
176 
177 /* Session descriptor structure for compression */
178 typedef struct dc_session_desc_s {
179 	Cpa8U stateRegistersComp[DC_QAT_STATE_REGISTERS_MAX_SIZE];
180 	/**< State registers for compression */
181 	Cpa8U stateRegistersDecomp[DC_QAT_STATE_REGISTERS_MAX_SIZE];
182 	/**< State registers for decompression */
183 	icp_qat_fw_comp_req_t reqCacheComp;
184 	/**< Cache as much as possible of the compression request in a pre-built
185 	 * request */
186 	icp_qat_fw_comp_req_t reqCacheDecomp;
187 	/**< Cache as much as possible of the decompression request in a
188 	 * pre-built
189 	 * request */
190 	dc_request_type_t requestType;
191 	/**< Type of the compression request. As stateful mode do not support
192 	 * more
193 	 * than one in-flight request there is no need to use spinlocks */
194 	dc_request_type_t previousRequestType;
195 	/**< Type of the previous compression request. Used in cases where there
196 	 * the
197 	 * stateful operation needs to be resubmitted */
198 	CpaDcHuffType huffType;
199 	/**< Huffman tree type */
200 	CpaDcCompType compType;
201 	/**< Compression type */
202 	CpaDcChecksum checksumType;
203 	/**< Type of checksum */
204 	CpaDcAutoSelectBest autoSelectBestHuffmanTree;
205 	/**< Indicates if the implementation selects the best Huffman encoding
206 	 */
207 	CpaDcSessionDir sessDirection;
208 	/**< Session direction */
209 	CpaDcSessionState sessState;
210 	/**< Session state */
211 	CpaDcCompLvl compLevel;
212 	/**< Compression level */
213 	CpaDcCallbackFn pCompressionCb;
214 	/**< Callback function defined for the traditional compression session
215 	 */
216 	QatUtilsAtomic pendingStatelessCbCount;
217 	/**< Keeps track of number of pending requests on stateless session */
218 	QatUtilsAtomic pendingStatefulCbCount;
219 	/**< Keeps track of number of pending requests on stateful session */
220 	Cpa64U pendingDpStatelessCbCount;
221 	/**< Keeps track of number of data plane pending requests on stateless
222 	 * session */
223 	struct mtx sessionLock;
224 	/**< Lock used to provide exclusive access for number of stateful
225 	 * in-flight
226 	 * requests update */
227 	CpaBoolean isDcDp;
228 	/**< Indicates if the data plane API is used */
229 	Cpa32U minContextSize;
230 	/**< Indicates the minimum size required to allocate the context buffer
231 	 */
232 	CpaBufferList *pContextBuffer;
233 	/**< Context buffer */
234 	Cpa32U historyBuffSize;
235 	/**< Size of the history buffer */
236 	Cpa64U cumulativeConsumedBytes;
237 	/**< Cumulative amount of consumed bytes. Used to build the footer in
238 	 * the
239 	 * stateful case */
240 	Cpa32U previousChecksum;
241 	/**< Save the previous value of the checksum. Used to process zero byte
242 	 * stateful compression or decompression requests */
243 	CpaBoolean isSopForCompressionProcessed;
244 	/**< Indicates whether a Compression Request is received in this session
245 	 */
246 	CpaBoolean isSopForDecompressionProcessed;
247 	/**< Indicates whether a Decompression Request is received in this
248 	 * session
249 	 */
250 	/**< Data integrity table */
251 	dc_integrity_crc_fw_t dataIntegrityCrcs;
252 	/**< Physical address of Data integrity buffer */
253 	CpaPhysicalAddr physDataIntegrityCrcs;
254 	/* Seed checksums structure used to calculate software calculated
255 	 * checksums.
256 	 */
257 	dc_sw_checksums_t seedSwCrc;
258 	/* Driver calculated integrity software CRC */
259 	dc_sw_checksums_t integritySwCrc;
260 	/* Flag to disable or enable CnV Error Injection mechanism */
261 	CpaBoolean cnvErrorInjection;
262 } dc_session_desc_t;
263 
264 /**
265  *****************************************************************************
266  * @ingroup Dc_DataCompression
267  *      Initialise a compression session
268  *
269  * @description
270  *      This function will initialise a compression session
271  *
272  * @param[in]       dcInstance       Instance handle derived from discovery
273  *                                   functions
274  * @param[in,out]   pSessionHandle   Pointer to a session handle
275  * @param[in,out]   pSessionData     Pointer to a user instantiated structure
276  *                                   containing session data
277  * @param[in]       pContextBuffer   Pointer to context buffer
278  *
279  * @param[in]       callbackFn       For synchronous operation this callback
280  *                                   shall be a null pointer
281  *
282  * @retval CPA_STATUS_SUCCESS        Function executed successfully
283  * @retval CPA_STATUS_FAIL           Function failed
284  * @retval CPA_STATUS_INVALID_PARAM  Invalid parameter passed in
285  * @retval CPA_STATUS_RESOURCE       Error related to system resources
286  *****************************************************************************/
287 CpaStatus dcInitSession(CpaInstanceHandle dcInstance,
288 			CpaDcSessionHandle pSessionHandle,
289 			CpaDcSessionSetupData *pSessionData,
290 			CpaBufferList *pContextBuffer,
291 			CpaDcCallbackFn callbackFn);
292 
293 /**
294  *****************************************************************************
295  * @ingroup Dc_DataCompression
296  *      Get the size of the memory required to hold the session information
297  *
298  * @description
299  *      This function will get the size of the memory required to hold the
300  *      session information
301  *
302  * @param[in]       dcInstance       Instance handle derived from discovery
303  *                                   functions
304  * @param[in]       pSessionData     Pointer to a user instantiated structure
305  *                                   containing session data
306  * @param[out]      pSessionSize     On return, this parameter will be the size
307  *                                   of the memory that will be
308  *                                   required by cpaDcInitSession() for session
309  *                                   data.
310  * @param[out]      pContextSize     On return, this parameter will be the size
311  *                                   of the memory that will be required
312  *                                   for context data.  Context data is
313  *                                   save/restore data including history and
314  *                                   any implementation specific data that is
315  *                                   required for a save/restore operation.
316  *
317  * @retval CPA_STATUS_SUCCESS        Function executed successfully
318  * @retval CPA_STATUS_FAIL           Function failed
319  * @retval CPA_STATUS_INVALID_PARAM  Invalid parameter passed in
320  *****************************************************************************/
321 CpaStatus dcGetSessionSize(CpaInstanceHandle dcInstance,
322 			   CpaDcSessionSetupData *pSessionData,
323 			   Cpa32U *pSessionSize,
324 			   Cpa32U *pContextSize);
325 
326 /**
327  *****************************************************************************
328  * @ingroup Dc_DataCompression
329  *      Set the cnvErrorInjection flag in session descriptor
330  *
331  * @description
332  *      This function enables the CnVError injection for the session
333  *      passed in. All Compression requests sent within the session
334  *      are injected with CnV errors. This error injection is for the
335  *      duration of the session. Resetting the session results in
336  *      setting being cleared. CnV error injection does not apply to
337  *      Data Plane API.
338  *
339  * @param[in]       dcInstance       Instance Handle
340  * @param[in]       pSessionHandle   Pointer to a session handle
341  *
342  * @retval CPA_STATUS_SUCCESS        Function executed successfully
343  * @retval CPA_STATUS_INVALID_PARAM  Invalid parameter passed in
344  * @retval CPA_STATUS_UNSUPPORTED    Unsupported feature
345  *****************************************************************************/
346 CpaStatus dcSetCnvError(CpaInstanceHandle dcInstance,
347 			CpaDcSessionHandle pSessionHandle);
348 
349 /**
350  *****************************************************************************
351  * @ingroup Dc_DataCompression
352  *      Check that pSessionData is valid
353  *
354  * @description
355  *      Check that all the parameters defined in the pSessionData are valid
356  *
357  * @param[in]       pSessionData     Pointer to a user instantiated structure
358  *                                   containing session data
359  *
360  * @retval CPA_STATUS_SUCCESS        Function executed successfully
361  * @retval CPA_STATUS_FAIL           Function failed to find device
362  * @retval CPA_STATUS_INVALID_PARAM  Invalid parameter passed in
363  * @retval CPA_STATUS_UNSUPPORTED    Unsupported algorithm/feature
364  *
365  *****************************************************************************/
366 CpaStatus dcCheckSessionData(const CpaDcSessionSetupData *pSessionData,
367 			     CpaInstanceHandle dcInstance);
368 
369 /**
370  *****************************************************************************
371  * @ingroup Dc_DataCompression
372  *      Get the compression command id for the given session setup data.
373  *
374  * @description
375  *      This function will get the compression command id based on parameters
376  *      passed in the given session setup data.
377  *
378  * @param[in]   pService           Pointer to the service
379  * @param[in]   pSessionData       Pointer to a user instantiated
380  *                                 structure containing session data
381  * @param[out]  pDcCmdId           Pointer to the command id
382  *
383  * @retval CPA_STATUS_SUCCESS      Function executed successfully
384  * @retval CPA_STATUS_UNSUPPORTED  Unsupported algorithm/feature
385  *
386  *****************************************************************************/
387 CpaStatus dcGetCompressCommandId(sal_compression_service_t *pService,
388 				 CpaDcSessionSetupData *pSessionData,
389 				 Cpa8U *pDcCmdId);
390 
391 /**
392  *****************************************************************************
393  * @ingroup Dc_DataCompression
394  *      Get the decompression command id for the given session setup data.
395  *
396  * @description
397  *      This function will get the decompression command id based on parameters
398  *      passed in the given session setup data.
399  *
400  * @param[in]   pService           Pointer to the service
401  * @param[in]   pSessionData       Pointer to a user instantiated
402  *                                 structure containing session data
403  * @param[out]  pDcCmdId           Pointer to the command id
404  *
405  * @retval CPA_STATUS_SUCCESS      Function executed successfully
406  * @retval CPA_STATUS_UNSUPPORTED  Unsupported algorithm/feature
407  *
408  *****************************************************************************/
409 CpaStatus dcGetDecompressCommandId(sal_compression_service_t *pService,
410 				   CpaDcSessionSetupData *pSessionData,
411 				   Cpa8U *pDcCmdId);
412 
413 /**
414  *****************************************************************************
415  * @ingroup Dc_DataCompression
416  *      Populate the translator content descriptor
417  *
418  * @description
419  *      This function will populate the translator content descriptor
420  *
421  * @param[out]  pMsg                     Pointer to the compression message
422  * @param[in]   nextSlice                Next slice
423  *
424  *****************************************************************************/
425 void dcTransContentDescPopulate(icp_qat_fw_comp_req_t *pMsg,
426 				icp_qat_fw_slice_t nextSlice);
427 
428 #endif /* DC_SESSION_H */
429