xref: /freebsd/sys/dev/qat/qat_api/common/compression/dc_buffers.c (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2022 Intel Corporation */
3 /* $FreeBSD$ */
4 /**
5  *****************************************************************************
6  * @file dc_buffers.c
7  *
8  * @defgroup Dc_DataCompression DC Data Compression
9  *
10  * @ingroup Dc_DataCompression
11  *
12  * @description
13  *      Implementation of the buffer management operations for
14  *      Data Compression service.
15  *
16  *****************************************************************************/
17 
18 /*
19 *******************************************************************************
20 * Include public/global header files
21 *******************************************************************************
22 */
23 #include "cpa.h"
24 #include "cpa_dc.h"
25 #include "cpa_dc_bp.h"
26 
27 #include "sal_types_compression.h"
28 #include "icp_qat_fw_comp.h"
29 
30 #define CPA_DC_CEIL_DIV(x, y) (((x) + (y)-1) / (y))
31 #define DC_DEST_BUFF_EXTRA_DEFLATE_GEN2 (55)
32 
33 CpaStatus
34 cpaDcBufferListGetMetaSize(const CpaInstanceHandle instanceHandle,
35 			   Cpa32U numBuffers,
36 			   Cpa32U *pSizeInBytes)
37 {
38 	CpaInstanceHandle insHandle = NULL;
39 
40 	if (CPA_INSTANCE_HANDLE_SINGLE == instanceHandle) {
41 		insHandle = dcGetFirstHandle();
42 	} else {
43 		insHandle = instanceHandle;
44 	}
45 
46 	LAC_CHECK_INSTANCE_HANDLE(insHandle);
47 	LAC_CHECK_NULL_PARAM(pSizeInBytes);
48 
49 	/* Ensure this is a compression instance */
50 	SAL_CHECK_INSTANCE_TYPE(insHandle, SAL_SERVICE_TYPE_COMPRESSION);
51 
52 	if (0 == numBuffers) {
53 		QAT_UTILS_LOG("Number of buffers is 0.\n");
54 		return CPA_STATUS_INVALID_PARAM;
55 	}
56 
57 	*pSizeInBytes = (sizeof(icp_buffer_list_desc_t) +
58 			 (sizeof(icp_flat_buffer_desc_t) * (numBuffers + 1)) +
59 			 ICP_DESCRIPTOR_ALIGNMENT_BYTES);
60 
61 	return CPA_STATUS_SUCCESS;
62 }
63 
64 CpaStatus
65 cpaDcBnpBufferListGetMetaSize(const CpaInstanceHandle instanceHandle,
66 			      Cpa32U numJobs,
67 			      Cpa32U *pSizeInBytes)
68 {
69 	return CPA_STATUS_UNSUPPORTED;
70 }
71 
72 static inline CpaStatus
73 dcDeflateBoundGen2(CpaDcHuffType huffType, Cpa32U inputSize, Cpa32U *outputSize)
74 {
75 	/* Formula for GEN2 deflate:
76 	 * ceil(9 * Total input bytes / 8) + 55 bytes.
77 	 * 55 bytes is the skid pad value for GEN2 devices.
78 	 */
79 	*outputSize =
80 	    CPA_DC_CEIL_DIV(9 * inputSize, 8) + DC_DEST_BUFF_EXTRA_DEFLATE_GEN2;
81 
82 	return CPA_STATUS_SUCCESS;
83 }
84 
85 CpaStatus
86 cpaDcDeflateCompressBound(const CpaInstanceHandle dcInstance,
87 			  CpaDcHuffType huffType,
88 			  Cpa32U inputSize,
89 			  Cpa32U *outputSize)
90 {
91 	CpaInstanceHandle insHandle = NULL;
92 
93 	if (CPA_INSTANCE_HANDLE_SINGLE == dcInstance) {
94 		insHandle = dcGetFirstHandle();
95 	} else {
96 		insHandle = dcInstance;
97 	}
98 
99 	LAC_CHECK_INSTANCE_HANDLE(insHandle);
100 	LAC_CHECK_NULL_PARAM(outputSize);
101 	/* Ensure this is a compression instance */
102 	SAL_CHECK_INSTANCE_TYPE(insHandle, SAL_SERVICE_TYPE_COMPRESSION);
103 	if (!inputSize) {
104 		QAT_UTILS_LOG(
105 		    "The input size needs to be greater than zero.\n");
106 		return CPA_STATUS_INVALID_PARAM;
107 	}
108 
109 	if ((CPA_DC_HT_STATIC != huffType) &&
110 	    (CPA_DC_HT_FULL_DYNAMIC != huffType)) {
111 		QAT_UTILS_LOG("Invalid huffType value.\n");
112 		return CPA_STATUS_INVALID_PARAM;
113 	}
114 
115 	return dcDeflateBoundGen2(huffType, inputSize, outputSize);
116 }
117