xref: /freebsd/sys/contrib/openzfs/include/sys/qat.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 #ifndef	_SYS_QAT_H
24 #define	_SYS_QAT_H
25 
26 typedef enum qat_compress_dir {
27 	QAT_DECOMPRESS = 0,
28 	QAT_COMPRESS = 1,
29 } qat_compress_dir_t;
30 
31 typedef enum qat_encrypt_dir {
32 	QAT_DECRYPT = 0,
33 	QAT_ENCRYPT = 1,
34 } qat_encrypt_dir_t;
35 
36 
37 #if defined(_KERNEL) && defined(HAVE_QAT)
38 #include <sys/zio.h>
39 #include <sys/crypto/api.h>
40 #include "cpa.h"
41 #include "dc/cpa_dc.h"
42 #include "lac/cpa_cy_sym.h"
43 
44 /*
45  * The minimal and maximal buffer size which are not restricted
46  * in the QAT hardware, but with the input buffer size between 4KB
47  * and 128KB the hardware can provide the optimal performance.
48  */
49 #define	QAT_MIN_BUF_SIZE	(4*1024)
50 #define	QAT_MAX_BUF_SIZE	(128*1024)
51 
52 /*
53  * Used for QAT kstat.
54  */
55 typedef struct qat_stats {
56 	/*
57 	 * Number of jobs submitted to QAT compression engine.
58 	 */
59 	kstat_named_t comp_requests;
60 	/*
61 	 * Total bytes sent to QAT compression engine.
62 	 */
63 	kstat_named_t comp_total_in_bytes;
64 	/*
65 	 * Total bytes output from QAT compression engine.
66 	 */
67 	kstat_named_t comp_total_out_bytes;
68 	/*
69 	 * Number of jobs submitted to QAT de-compression engine.
70 	 */
71 	kstat_named_t decomp_requests;
72 	/*
73 	 * Total bytes sent to QAT de-compression engine.
74 	 */
75 	kstat_named_t decomp_total_in_bytes;
76 	/*
77 	 * Total bytes output from QAT de-compression engine.
78 	 */
79 	kstat_named_t decomp_total_out_bytes;
80 	/*
81 	 * Number of fails in the QAT compression / decompression engine.
82 	 * Note: when a QAT error happens, it doesn't necessarily indicate a
83 	 * critical hardware issue. Sometimes it is because the output buffer
84 	 * is not big enough. The compression job will be transferred to the
85 	 * gzip software implementation so the functionality of ZFS is not
86 	 * impacted.
87 	 */
88 	kstat_named_t dc_fails;
89 
90 	/*
91 	 * Number of jobs submitted to QAT encryption engine.
92 	 */
93 	kstat_named_t encrypt_requests;
94 	/*
95 	 * Total bytes sent to QAT encryption engine.
96 	 */
97 	kstat_named_t encrypt_total_in_bytes;
98 	/*
99 	 * Total bytes output from QAT encryption engine.
100 	 */
101 	kstat_named_t encrypt_total_out_bytes;
102 	/*
103 	 * Number of jobs submitted to QAT decryption engine.
104 	 */
105 	kstat_named_t decrypt_requests;
106 	/*
107 	 * Total bytes sent to QAT decryption engine.
108 	 */
109 	kstat_named_t decrypt_total_in_bytes;
110 	/*
111 	 * Total bytes output from QAT decryption engine.
112 	 */
113 	kstat_named_t decrypt_total_out_bytes;
114 	/*
115 	 * Number of fails in the QAT encryption / decryption engine.
116 	 * Note: when a QAT error happens, it doesn't necessarily indicate a
117 	 * critical hardware issue. The encryption job will be transferred
118 	 * to the software implementation so the functionality of ZFS is
119 	 * not impacted.
120 	 */
121 	kstat_named_t crypt_fails;
122 
123 	/*
124 	 * Number of jobs submitted to QAT checksum engine.
125 	 */
126 	kstat_named_t cksum_requests;
127 	/*
128 	 * Total bytes sent to QAT checksum engine.
129 	 */
130 	kstat_named_t cksum_total_in_bytes;
131 	/*
132 	 * Number of fails in the QAT checksum engine.
133 	 * Note: when a QAT error happens, it doesn't necessarily indicate a
134 	 * critical hardware issue. The checksum job will be transferred to the
135 	 * software implementation so the functionality of ZFS is not impacted.
136 	 */
137 	kstat_named_t cksum_fails;
138 } qat_stats_t;
139 
140 #define	QAT_STAT_INCR(stat, val) \
141 	atomic_add_64(&qat_stats.stat.value.ui64, (val))
142 #define	QAT_STAT_BUMP(stat) \
143 	QAT_STAT_INCR(stat, 1)
144 
145 extern qat_stats_t qat_stats;
146 extern int zfs_qat_compress_disable;
147 extern int zfs_qat_checksum_disable;
148 extern int zfs_qat_encrypt_disable;
149 
150 /* inlined for performance */
151 static inline struct page *
qat_mem_to_page(void * addr)152 qat_mem_to_page(void *addr)
153 {
154 	if (!is_vmalloc_addr(addr))
155 		return (virt_to_page(addr));
156 
157 	return (vmalloc_to_page(addr));
158 }
159 
160 CpaStatus qat_mem_alloc_contig(void **pp_mem_addr, Cpa32U size_bytes);
161 void qat_mem_free_contig(void **pp_mem_addr);
162 #define	QAT_PHYS_CONTIG_ALLOC(pp_mem_addr, size_bytes)	\
163 	qat_mem_alloc_contig((void *)(pp_mem_addr), (size_bytes))
164 #define	QAT_PHYS_CONTIG_FREE(p_mem_addr)	\
165 	qat_mem_free_contig((void *)&(p_mem_addr))
166 
167 extern int qat_dc_init(void);
168 extern void qat_dc_fini(void);
169 extern int qat_cy_init(void);
170 extern void qat_cy_fini(void);
171 extern int qat_init(void);
172 extern void qat_fini(void);
173 
174 /* fake CpaStatus used to indicate data was not compressible */
175 #define	CPA_STATUS_INCOMPRESSIBLE		(-127)
176 
177 extern boolean_t qat_dc_use_accel(size_t s_len);
178 extern boolean_t qat_crypt_use_accel(size_t s_len);
179 extern boolean_t qat_checksum_use_accel(size_t s_len);
180 extern int qat_compress(qat_compress_dir_t dir, char *src, int src_len,
181     char *dst, int dst_len, size_t *c_len);
182 extern int qat_crypt(qat_encrypt_dir_t dir, uint8_t *src_buf, uint8_t *dst_buf,
183     uint8_t *aad_buf, uint32_t aad_len, uint8_t *iv_buf, uint8_t *digest_buf,
184     crypto_key_t *key, uint64_t crypt, uint32_t enc_len);
185 extern int qat_checksum(uint64_t cksum, uint8_t *buf, uint64_t size,
186     zio_cksum_t *zcp);
187 #else
188 #define	CPA_STATUS_SUCCESS			0
189 #define	CPA_STATUS_INCOMPRESSIBLE		(-127)
190 #define	qat_init()
191 #define	qat_fini()
192 #define	qat_dc_use_accel(s_len)			((void) sizeof (s_len), 0)
193 #define	qat_crypt_use_accel(s_len)		((void) sizeof (s_len), 0)
194 #define	qat_checksum_use_accel(s_len)		((void) sizeof (s_len), 0)
195 #define	qat_compress(dir, s, sl, d, dl, cl)			\
196 	((void) sizeof (dir), (void) sizeof (s), (void) sizeof (sl), \
197 	    (void) sizeof (d), (void) sizeof (dl), (void) sizeof (cl), 0)
198 #define	qat_crypt(dir, s, d, a, al, i, db, k, c, el)		\
199 	((void) sizeof (dir), (void) sizeof (s), (void) sizeof (d), \
200 	    (void) sizeof (a),  (void) sizeof (al), (void) sizeof (i), \
201 	    (void) sizeof (db), (void) sizeof (k), (void) sizeof (c), \
202 	    (void) sizeof (el), 0)
203 #define	qat_checksum(c, buf, s, z)				\
204 	((void) sizeof (c), (void) sizeof (buf), (void) sizeof (s), \
205 	    (void) sizeof (z), 0)
206 #endif
207 
208 #endif /* _SYS_QAT_H */
209