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 	Cpa32U deflateWindowSize;
212 	/**< Window size */
213 	CpaDcCompLvl compLevel;
214 	/**< Compression level */
215 	CpaDcCallbackFn pCompressionCb;
216 	/**< Callback function defined for the traditional compression session
217 	 */
218 	QatUtilsAtomic pendingStatelessCbCount;
219 	/**< Keeps track of number of pending requests on stateless session */
220 	QatUtilsAtomic pendingStatefulCbCount;
221 	/**< Keeps track of number of pending requests on stateful session */
222 	Cpa64U pendingDpStatelessCbCount;
223 	/**< Keeps track of number of data plane pending requests on stateless
224 	 * session */
225 	struct mtx sessionLock;
226 	/**< Lock used to provide exclusive access for number of stateful
227 	 * in-flight
228 	 * requests update */
229 	CpaBoolean isDcDp;
230 	/**< Indicates if the data plane API is used */
231 	Cpa32U minContextSize;
232 	/**< Indicates the minimum size required to allocate the context buffer
233 	 */
234 	CpaBufferList *pContextBuffer;
235 	/**< Context buffer */
236 	Cpa32U historyBuffSize;
237 	/**< Size of the history buffer */
238 	Cpa64U cumulativeConsumedBytes;
239 	/**< Cumulative amount of consumed bytes. Used to build the footer in
240 	 * the
241 	 * stateful case */
242 	Cpa32U previousChecksum;
243 	/**< Save the previous value of the checksum. Used to process zero byte
244 	 * stateful compression or decompression requests */
245 	CpaBoolean isSopForCompressionProcessed;
246 	/**< Indicates whether a Compression Request is received in this session
247 	 */
248 	CpaBoolean isSopForDecompressionProcessed;
249 	/**< Indicates whether a Decompression Request is received in this
250 	 * session
251 	 */
252 	/**< Data integrity table */
253 	dc_integrity_crc_fw_t dataIntegrityCrcs;
254 	/**< Physical address of Data integrity buffer */
255 	CpaPhysicalAddr physDataIntegrityCrcs;
256 	/* Seed checksums structure used to calculate software calculated
257 	 * checksums.
258 	 */
259 	dc_sw_checksums_t seedSwCrc;
260 	/* Driver calculated integrity software CRC */
261 	dc_sw_checksums_t integritySwCrc;
262 	/* Flag to disable or enable CnV Error Injection mechanism */
263 	CpaBoolean cnvErrorInjection;
264 } dc_session_desc_t;
265 
266 /**
267  *****************************************************************************
268  * @ingroup Dc_DataCompression
269  *      Initialise a compression session
270  *
271  * @description
272  *      This function will initialise a compression session
273  *
274  * @param[in]       dcInstance       Instance handle derived from discovery
275  *                                   functions
276  * @param[in,out]   pSessionHandle   Pointer to a session handle
277  * @param[in,out]   pSessionData     Pointer to a user instantiated structure
278  *                                   containing session data
279  * @param[in]       pContextBuffer   Pointer to context buffer
280  *
281  * @param[in]       callbackFn       For synchronous operation this callback
282  *                                   shall be a null pointer
283  *
284  * @retval CPA_STATUS_SUCCESS        Function executed successfully
285  * @retval CPA_STATUS_FAIL           Function failed
286  * @retval CPA_STATUS_INVALID_PARAM  Invalid parameter passed in
287  * @retval CPA_STATUS_RESOURCE       Error related to system resources
288  *****************************************************************************/
289 CpaStatus dcInitSession(CpaInstanceHandle dcInstance,
290 			CpaDcSessionHandle pSessionHandle,
291 			CpaDcSessionSetupData *pSessionData,
292 			CpaBufferList *pContextBuffer,
293 			CpaDcCallbackFn callbackFn);
294 
295 /**
296  *****************************************************************************
297  * @ingroup Dc_DataCompression
298  *      Get the size of the memory required to hold the session information
299  *
300  * @description
301  *      This function will get the size of the memory required to hold the
302  *      session information
303  *
304  * @param[in]       dcInstance       Instance handle derived from discovery
305  *                                   functions
306  * @param[in]       pSessionData     Pointer to a user instantiated structure
307  *                                   containing session data
308  * @param[out]      pSessionSize     On return, this parameter will be the size
309  *                                   of the memory that will be
310  *                                   required by cpaDcInitSession() for session
311  *                                   data.
312  * @param[out]      pContextSize     On return, this parameter will be the size
313  *                                   of the memory that will be required
314  *                                   for context data.  Context data is
315  *                                   save/restore data including history and
316  *                                   any implementation specific data that is
317  *                                   required for a save/restore operation.
318  *
319  * @retval CPA_STATUS_SUCCESS        Function executed successfully
320  * @retval CPA_STATUS_FAIL           Function failed
321  * @retval CPA_STATUS_INVALID_PARAM  Invalid parameter passed in
322  *****************************************************************************/
323 CpaStatus dcGetSessionSize(CpaInstanceHandle dcInstance,
324 			   CpaDcSessionSetupData *pSessionData,
325 			   Cpa32U *pSessionSize,
326 			   Cpa32U *pContextSize);
327 
328 /**
329  *****************************************************************************
330  * @ingroup Dc_DataCompression
331  *      Set the cnvErrorInjection flag in session descriptor
332  *
333  * @description
334  *      This function enables the CnVError injection for the session
335  *      passed in. All Compression requests sent within the session
336  *      are injected with CnV errors. This error injection is for the
337  *      duration of the session. Resetting the session results in
338  *      setting being cleared. CnV error injection does not apply to
339  *      Data Plane API.
340  *
341  * @param[in]       dcInstance       Instance Handle
342  * @param[in]       pSessionHandle   Pointer to a session handle
343  *
344  * @retval CPA_STATUS_SUCCESS        Function executed successfully
345  * @retval CPA_STATUS_INVALID_PARAM  Invalid parameter passed in
346  * @retval CPA_STATUS_UNSUPPORTED    Unsupported feature
347  *****************************************************************************/
348 CpaStatus dcSetCnvError(CpaInstanceHandle dcInstance,
349 			CpaDcSessionHandle pSessionHandle);
350 
351 /**
352  *****************************************************************************
353  * @ingroup Dc_DataCompression
354  *      Check that pSessionData is valid
355  *
356  * @description
357  *      Check that all the parameters defined in the pSessionData are valid
358  *
359  * @param[in]       pSessionData     Pointer to a user instantiated structure
360  *                                   containing session data
361  *
362  * @retval CPA_STATUS_SUCCESS        Function executed successfully
363  * @retval CPA_STATUS_FAIL           Function failed to find device
364  * @retval CPA_STATUS_INVALID_PARAM  Invalid parameter passed in
365  * @retval CPA_STATUS_UNSUPPORTED    Unsupported algorithm/feature
366  *
367  *****************************************************************************/
368 CpaStatus dcCheckSessionData(const CpaDcSessionSetupData *pSessionData,
369 			     CpaInstanceHandle dcInstance);
370 
371 /**
372  *****************************************************************************
373  * @ingroup Dc_DataCompression
374  *      Get the compression command id for the given session setup data.
375  *
376  * @description
377  *      This function will get the compression command id based on parameters
378  *      passed in the given session setup data.
379  *
380  * @param[in]   pService           Pointer to the service
381  * @param[in]   pSessionData       Pointer to a user instantiated
382  *                                 structure containing session data
383  * @param[out]  pDcCmdId           Pointer to the command id
384  *
385  * @retval CPA_STATUS_SUCCESS      Function executed successfully
386  * @retval CPA_STATUS_UNSUPPORTED  Unsupported algorithm/feature
387  *
388  *****************************************************************************/
389 CpaStatus dcGetCompressCommandId(sal_compression_service_t *pService,
390 				 CpaDcSessionSetupData *pSessionData,
391 				 Cpa8U *pDcCmdId);
392 
393 /**
394  *****************************************************************************
395  * @ingroup Dc_DataCompression
396  *      Get the decompression command id for the given session setup data.
397  *
398  * @description
399  *      This function will get the decompression command id based on parameters
400  *      passed in the given session setup data.
401  *
402  * @param[in]   pService           Pointer to the service
403  * @param[in]   pSessionData       Pointer to a user instantiated
404  *                                 structure containing session data
405  * @param[out]  pDcCmdId           Pointer to the command id
406  *
407  * @retval CPA_STATUS_SUCCESS      Function executed successfully
408  * @retval CPA_STATUS_UNSUPPORTED  Unsupported algorithm/feature
409  *
410  *****************************************************************************/
411 CpaStatus dcGetDecompressCommandId(sal_compression_service_t *pService,
412 				   CpaDcSessionSetupData *pSessionData,
413 				   Cpa8U *pDcCmdId);
414 
415 /**
416  *****************************************************************************
417  * @ingroup Dc_DataCompression
418  *      Populate the translator content descriptor
419  *
420  * @description
421  *      This function will populate the translator content descriptor
422  *
423  * @param[out]  pMsg                     Pointer to the compression message
424  * @param[in]   nextSlice                Next slice
425  *
426  *****************************************************************************/
427 void dcTransContentDescPopulate(icp_qat_fw_comp_req_t *pMsg,
428 				icp_qat_fw_slice_t nextSlice);
429 
430 #endif /* DC_SESSION_H */
431