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