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.
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.
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]
23 * This file represents the QAT implementation of checksums and encryption.
24 * Internally, QAT shares the same cryptographic instances for both of these
25 * operations, so the code has been combined here. QAT data compression uses
26 * compression instances, so that code is separated into qat_compress.c
29 #if defined(_KERNEL) && defined(HAVE_QAT)
30 #include <linux/slab.h>
31 #include <linux/vmalloc.h>
32 #include <linux/pagemap.h>
33 #include <linux/completion.h>
34 #include <sys/zfs_context.h>
35 #include <sys/zio_crypt.h>
36 #include "lac/cpa_cy_im.h"
37 #include "lac/cpa_cy_common.h"
41 * Max instances in a QAT device, each instance is a channel to submit
42 * jobs to QAT hardware, this is only for pre-allocating instances
43 * and session arrays; the actual number of instances are defined in
44 * the QAT driver's configure file.
46 #define QAT_CRYPT_MAX_INSTANCES 48
48 #define MAX_PAGE_NUM 1024
50 static Cpa32U inst_num
= 0;
51 static Cpa16U num_inst
= 0;
52 static CpaInstanceHandle cy_inst_handles
[QAT_CRYPT_MAX_INSTANCES
];
53 static boolean_t qat_cy_init_done
= B_FALSE
;
54 int zfs_qat_encrypt_disable
= 0;
55 int zfs_qat_checksum_disable
= 0;
57 typedef struct cy_callback
{
58 CpaBoolean verify_result
;
59 struct completion complete
;
63 symcallback(void *p_callback
, CpaStatus status
, const CpaCySymOp operation
,
64 void *op_data
, CpaBufferList
*buf_list_dst
, CpaBoolean verify
)
66 cy_callback_t
*cb
= p_callback
;
69 /* indicate that the function has been called */
70 cb
->verify_result
= verify
;
71 complete(&cb
->complete
);
76 qat_crypt_use_accel(size_t s_len
)
78 return (!zfs_qat_encrypt_disable
&&
80 s_len
>= QAT_MIN_BUF_SIZE
&&
81 s_len
<= QAT_MAX_BUF_SIZE
);
85 qat_checksum_use_accel(size_t s_len
)
87 return (!zfs_qat_checksum_disable
&&
89 s_len
>= QAT_MIN_BUF_SIZE
&&
90 s_len
<= QAT_MAX_BUF_SIZE
);
96 for (Cpa16U i
= 0; i
< num_inst
; i
++)
97 cpaCyStopInstance(cy_inst_handles
[i
]);
100 qat_cy_init_done
= B_FALSE
;
106 CpaStatus status
= CPA_STATUS_FAIL
;
108 if (qat_cy_init_done
)
111 status
= cpaCyGetNumInstances(&num_inst
);
112 if (status
!= CPA_STATUS_SUCCESS
)
115 /* if the user has configured no QAT encryption units just return */
119 if (num_inst
> QAT_CRYPT_MAX_INSTANCES
)
120 num_inst
= QAT_CRYPT_MAX_INSTANCES
;
122 status
= cpaCyGetInstances(num_inst
, &cy_inst_handles
[0]);
123 if (status
!= CPA_STATUS_SUCCESS
)
126 for (Cpa16U i
= 0; i
< num_inst
; i
++) {
127 status
= cpaCySetAddressTranslation(cy_inst_handles
[i
],
128 (void *)virt_to_phys
);
129 if (status
!= CPA_STATUS_SUCCESS
)
132 status
= cpaCyStartInstance(cy_inst_handles
[i
]);
133 if (status
!= CPA_STATUS_SUCCESS
)
137 qat_cy_init_done
= B_TRUE
;
148 if (!qat_cy_init_done
)
155 qat_init_crypt_session_ctx(qat_encrypt_dir_t dir
, CpaInstanceHandle inst_handle
,
156 CpaCySymSessionCtx
**cy_session_ctx
, crypto_key_t
*key
,
157 Cpa64U crypt
, Cpa32U aad_len
)
159 CpaStatus status
= CPA_STATUS_SUCCESS
;
161 Cpa32U ciper_algorithm
;
162 Cpa32U hash_algorithm
;
163 CpaCySymSessionSetupData sd
= { 0 };
165 if (zio_crypt_table
[crypt
].ci_crypt_type
== ZC_TYPE_CCM
) {
166 return (CPA_STATUS_FAIL
);
168 ciper_algorithm
= CPA_CY_SYM_CIPHER_AES_GCM
;
169 hash_algorithm
= CPA_CY_SYM_HASH_AES_GCM
;
172 sd
.cipherSetupData
.cipherAlgorithm
= ciper_algorithm
;
173 sd
.cipherSetupData
.pCipherKey
= key
->ck_data
;
174 sd
.cipherSetupData
.cipherKeyLenInBytes
= key
->ck_length
/ 8;
175 sd
.hashSetupData
.hashAlgorithm
= hash_algorithm
;
176 sd
.hashSetupData
.hashMode
= CPA_CY_SYM_HASH_MODE_AUTH
;
177 sd
.hashSetupData
.digestResultLenInBytes
= ZIO_DATA_MAC_LEN
;
178 sd
.hashSetupData
.authModeSetupData
.aadLenInBytes
= aad_len
;
179 sd
.sessionPriority
= CPA_CY_PRIORITY_NORMAL
;
180 sd
.symOperation
= CPA_CY_SYM_OP_ALGORITHM_CHAINING
;
181 sd
.digestIsAppended
= CPA_FALSE
;
182 sd
.verifyDigest
= CPA_FALSE
;
184 if (dir
== QAT_ENCRYPT
) {
185 sd
.cipherSetupData
.cipherDirection
=
186 CPA_CY_SYM_CIPHER_DIRECTION_ENCRYPT
;
188 CPA_CY_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER
;
190 ASSERT3U(dir
, ==, QAT_DECRYPT
);
191 sd
.cipherSetupData
.cipherDirection
=
192 CPA_CY_SYM_CIPHER_DIRECTION_DECRYPT
;
194 CPA_CY_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH
;
197 status
= cpaCySymSessionCtxGetSize(inst_handle
, &sd
, &ctx_size
);
198 if (status
!= CPA_STATUS_SUCCESS
)
201 status
= QAT_PHYS_CONTIG_ALLOC(cy_session_ctx
, ctx_size
);
202 if (status
!= CPA_STATUS_SUCCESS
)
205 status
= cpaCySymInitSession(inst_handle
, symcallback
, &sd
,
207 if (status
!= CPA_STATUS_SUCCESS
) {
208 QAT_PHYS_CONTIG_FREE(*cy_session_ctx
);
212 return (CPA_STATUS_SUCCESS
);
216 qat_init_checksum_session_ctx(CpaInstanceHandle inst_handle
,
217 CpaCySymSessionCtx
**cy_session_ctx
, Cpa64U cksum
)
219 CpaStatus status
= CPA_STATUS_SUCCESS
;
221 Cpa32U hash_algorithm
;
222 CpaCySymSessionSetupData sd
= { 0 };
225 * ZFS's SHA512 checksum is actually SHA512/256, which uses
226 * a different IV from standard SHA512. QAT does not support
227 * SHA512/256, so we can only support SHA256.
229 if (cksum
== ZIO_CHECKSUM_SHA256
)
230 hash_algorithm
= CPA_CY_SYM_HASH_SHA256
;
232 return (CPA_STATUS_FAIL
);
234 sd
.sessionPriority
= CPA_CY_PRIORITY_NORMAL
;
235 sd
.symOperation
= CPA_CY_SYM_OP_HASH
;
236 sd
.hashSetupData
.hashAlgorithm
= hash_algorithm
;
237 sd
.hashSetupData
.hashMode
= CPA_CY_SYM_HASH_MODE_PLAIN
;
238 sd
.hashSetupData
.digestResultLenInBytes
= sizeof (zio_cksum_t
);
239 sd
.digestIsAppended
= CPA_FALSE
;
240 sd
.verifyDigest
= CPA_FALSE
;
242 status
= cpaCySymSessionCtxGetSize(inst_handle
, &sd
, &ctx_size
);
243 if (status
!= CPA_STATUS_SUCCESS
)
246 status
= QAT_PHYS_CONTIG_ALLOC(cy_session_ctx
, ctx_size
);
247 if (status
!= CPA_STATUS_SUCCESS
)
250 status
= cpaCySymInitSession(inst_handle
, symcallback
, &sd
,
252 if (status
!= CPA_STATUS_SUCCESS
) {
253 QAT_PHYS_CONTIG_FREE(*cy_session_ctx
);
257 return (CPA_STATUS_SUCCESS
);
261 qat_init_cy_buffer_lists(CpaInstanceHandle inst_handle
, uint32_t nr_bufs
,
262 CpaBufferList
*src
, CpaBufferList
*dst
)
264 CpaStatus status
= CPA_STATUS_SUCCESS
;
265 Cpa32U meta_size
= 0;
267 status
= cpaCyBufferListGetMetaSize(inst_handle
, nr_bufs
, &meta_size
);
268 if (status
!= CPA_STATUS_SUCCESS
)
271 status
= QAT_PHYS_CONTIG_ALLOC(&src
->pPrivateMetaData
, meta_size
);
272 if (status
!= CPA_STATUS_SUCCESS
)
276 status
= QAT_PHYS_CONTIG_ALLOC(&dst
->pPrivateMetaData
,
278 if (status
!= CPA_STATUS_SUCCESS
)
282 return (CPA_STATUS_SUCCESS
);
285 QAT_PHYS_CONTIG_FREE(src
->pPrivateMetaData
);
287 QAT_PHYS_CONTIG_FREE(dst
->pPrivateMetaData
);
293 qat_crypt(qat_encrypt_dir_t dir
, uint8_t *src_buf
, uint8_t *dst_buf
,
294 uint8_t *aad_buf
, uint32_t aad_len
, uint8_t *iv_buf
, uint8_t *digest_buf
,
295 crypto_key_t
*key
, uint64_t crypt
, uint32_t enc_len
)
297 CpaStatus status
= CPA_STATUS_SUCCESS
;
299 CpaInstanceHandle cy_inst_handle
;
300 Cpa16U nr_bufs
= (enc_len
>> PAGE_SHIFT
) + 2;
301 Cpa32U bytes_left
= 0;
303 CpaCySymSessionCtx
*cy_session_ctx
= NULL
;
305 CpaCySymOpData op_data
= { 0 };
306 CpaBufferList src_buffer_list
= { 0 };
307 CpaBufferList dst_buffer_list
= { 0 };
308 CpaFlatBuffer
*flat_src_buf_array
= NULL
;
309 CpaFlatBuffer
*flat_src_buf
= NULL
;
310 CpaFlatBuffer
*flat_dst_buf_array
= NULL
;
311 CpaFlatBuffer
*flat_dst_buf
= NULL
;
312 struct page
*in_pages
[MAX_PAGE_NUM
];
313 struct page
*out_pages
[MAX_PAGE_NUM
];
314 Cpa32U in_page_num
= 0;
315 Cpa32U out_page_num
= 0;
316 Cpa32U in_page_off
= 0;
317 Cpa32U out_page_off
= 0;
319 if (dir
== QAT_ENCRYPT
) {
320 QAT_STAT_BUMP(encrypt_requests
);
321 QAT_STAT_INCR(encrypt_total_in_bytes
, enc_len
);
323 QAT_STAT_BUMP(decrypt_requests
);
324 QAT_STAT_INCR(decrypt_total_in_bytes
, enc_len
);
327 i
= (Cpa32U
)atomic_inc_32_nv(&inst_num
) % num_inst
;
328 cy_inst_handle
= cy_inst_handles
[i
];
330 status
= qat_init_crypt_session_ctx(dir
, cy_inst_handle
,
331 &cy_session_ctx
, key
, crypt
, aad_len
);
332 if (status
!= CPA_STATUS_SUCCESS
) {
333 /* don't count CCM as a failure since it's not supported */
334 if (zio_crypt_table
[crypt
].ci_crypt_type
== ZC_TYPE_GCM
)
335 QAT_STAT_BUMP(crypt_fails
);
340 * We increment nr_bufs by 2 to allow us to handle non
341 * page-aligned buffer addresses and buffers whose sizes
342 * are not divisible by PAGE_SIZE.
344 status
= qat_init_cy_buffer_lists(cy_inst_handle
, nr_bufs
,
345 &src_buffer_list
, &dst_buffer_list
);
346 if (status
!= CPA_STATUS_SUCCESS
)
349 status
= QAT_PHYS_CONTIG_ALLOC(&flat_src_buf_array
,
350 nr_bufs
* sizeof (CpaFlatBuffer
));
351 if (status
!= CPA_STATUS_SUCCESS
)
353 status
= QAT_PHYS_CONTIG_ALLOC(&flat_dst_buf_array
,
354 nr_bufs
* sizeof (CpaFlatBuffer
));
355 if (status
!= CPA_STATUS_SUCCESS
)
357 status
= QAT_PHYS_CONTIG_ALLOC(&op_data
.pDigestResult
,
359 if (status
!= CPA_STATUS_SUCCESS
)
361 status
= QAT_PHYS_CONTIG_ALLOC(&op_data
.pIv
,
363 if (status
!= CPA_STATUS_SUCCESS
)
366 status
= QAT_PHYS_CONTIG_ALLOC(&op_data
.pAdditionalAuthData
,
368 if (status
!= CPA_STATUS_SUCCESS
)
370 memcpy(op_data
.pAdditionalAuthData
, aad_buf
, aad_len
);
373 bytes_left
= enc_len
;
375 flat_src_buf
= flat_src_buf_array
;
376 while (bytes_left
> 0) {
377 in_page_off
= ((long)data
& ~PAGE_MASK
);
378 in_pages
[in_page_num
] = qat_mem_to_page(data
);
379 flat_src_buf
->pData
= kmap(in_pages
[in_page_num
]) + in_page_off
;
380 flat_src_buf
->dataLenInBytes
=
381 min((long)PAGE_SIZE
- in_page_off
, (long)bytes_left
);
382 data
+= flat_src_buf
->dataLenInBytes
;
383 bytes_left
-= flat_src_buf
->dataLenInBytes
;
387 src_buffer_list
.pBuffers
= flat_src_buf_array
;
388 src_buffer_list
.numBuffers
= in_page_num
;
390 bytes_left
= enc_len
;
392 flat_dst_buf
= flat_dst_buf_array
;
393 while (bytes_left
> 0) {
394 out_page_off
= ((long)data
& ~PAGE_MASK
);
395 out_pages
[out_page_num
] = qat_mem_to_page(data
);
396 flat_dst_buf
->pData
= kmap(out_pages
[out_page_num
]) +
398 flat_dst_buf
->dataLenInBytes
=
399 min((long)PAGE_SIZE
- out_page_off
, (long)bytes_left
);
400 data
+= flat_dst_buf
->dataLenInBytes
;
401 bytes_left
-= flat_dst_buf
->dataLenInBytes
;
405 dst_buffer_list
.pBuffers
= flat_dst_buf_array
;
406 dst_buffer_list
.numBuffers
= out_page_num
;
408 op_data
.sessionCtx
= cy_session_ctx
;
409 op_data
.packetType
= CPA_CY_SYM_PACKET_TYPE_FULL
;
410 op_data
.cryptoStartSrcOffsetInBytes
= 0;
411 op_data
.messageLenToCipherInBytes
= 0;
412 op_data
.hashStartSrcOffsetInBytes
= 0;
413 op_data
.messageLenToHashInBytes
= 0;
414 op_data
.messageLenToCipherInBytes
= enc_len
;
415 op_data
.ivLenInBytes
= ZIO_DATA_IV_LEN
;
416 memcpy(op_data
.pIv
, iv_buf
, ZIO_DATA_IV_LEN
);
417 /* if dir is QAT_DECRYPT, copy digest_buf to pDigestResult */
418 if (dir
== QAT_DECRYPT
)
419 memcpy(op_data
.pDigestResult
, digest_buf
, ZIO_DATA_MAC_LEN
);
421 cb
.verify_result
= CPA_FALSE
;
422 init_completion(&cb
.complete
);
423 status
= cpaCySymPerformOp(cy_inst_handle
, &cb
, &op_data
,
424 &src_buffer_list
, &dst_buffer_list
, NULL
);
425 if (status
!= CPA_STATUS_SUCCESS
)
428 /* we now wait until the completion of the operation. */
429 wait_for_completion(&cb
.complete
);
431 if (cb
.verify_result
== CPA_FALSE
) {
432 status
= CPA_STATUS_FAIL
;
436 if (dir
== QAT_ENCRYPT
) {
437 /* if dir is QAT_ENCRYPT, save pDigestResult to digest_buf */
438 memcpy(digest_buf
, op_data
.pDigestResult
, ZIO_DATA_MAC_LEN
);
439 QAT_STAT_INCR(encrypt_total_out_bytes
, enc_len
);
441 QAT_STAT_INCR(decrypt_total_out_bytes
, enc_len
);
445 if (status
!= CPA_STATUS_SUCCESS
)
446 QAT_STAT_BUMP(crypt_fails
);
448 for (i
= 0; i
< in_page_num
; i
++)
450 for (i
= 0; i
< out_page_num
; i
++)
451 kunmap(out_pages
[i
]);
453 cpaCySymRemoveSession(cy_inst_handle
, cy_session_ctx
);
455 QAT_PHYS_CONTIG_FREE(op_data
.pAdditionalAuthData
);
456 QAT_PHYS_CONTIG_FREE(op_data
.pIv
);
457 QAT_PHYS_CONTIG_FREE(op_data
.pDigestResult
);
458 QAT_PHYS_CONTIG_FREE(src_buffer_list
.pPrivateMetaData
);
459 QAT_PHYS_CONTIG_FREE(dst_buffer_list
.pPrivateMetaData
);
460 QAT_PHYS_CONTIG_FREE(cy_session_ctx
);
461 QAT_PHYS_CONTIG_FREE(flat_src_buf_array
);
462 QAT_PHYS_CONTIG_FREE(flat_dst_buf_array
);
468 qat_checksum(uint64_t cksum
, uint8_t *buf
, uint64_t size
, zio_cksum_t
*zcp
)
472 CpaInstanceHandle cy_inst_handle
;
473 Cpa16U nr_bufs
= (size
>> PAGE_SHIFT
) + 2;
474 Cpa32U bytes_left
= 0;
476 CpaCySymSessionCtx
*cy_session_ctx
= NULL
;
478 Cpa8U
*digest_buffer
= NULL
;
479 CpaCySymOpData op_data
= { 0 };
480 CpaBufferList src_buffer_list
= { 0 };
481 CpaFlatBuffer
*flat_src_buf_array
= NULL
;
482 CpaFlatBuffer
*flat_src_buf
= NULL
;
483 struct page
*in_pages
[MAX_PAGE_NUM
];
487 QAT_STAT_BUMP(cksum_requests
);
488 QAT_STAT_INCR(cksum_total_in_bytes
, size
);
490 i
= (Cpa32U
)atomic_inc_32_nv(&inst_num
) % num_inst
;
491 cy_inst_handle
= cy_inst_handles
[i
];
493 status
= qat_init_checksum_session_ctx(cy_inst_handle
,
494 &cy_session_ctx
, cksum
);
495 if (status
!= CPA_STATUS_SUCCESS
) {
496 /* don't count unsupported checksums as a failure */
497 if (cksum
== ZIO_CHECKSUM_SHA256
||
498 cksum
== ZIO_CHECKSUM_SHA512
)
499 QAT_STAT_BUMP(cksum_fails
);
504 * We increment nr_bufs by 2 to allow us to handle non
505 * page-aligned buffer addresses and buffers whose sizes
506 * are not divisible by PAGE_SIZE.
508 status
= qat_init_cy_buffer_lists(cy_inst_handle
, nr_bufs
,
509 &src_buffer_list
, &src_buffer_list
);
510 if (status
!= CPA_STATUS_SUCCESS
)
513 status
= QAT_PHYS_CONTIG_ALLOC(&flat_src_buf_array
,
514 nr_bufs
* sizeof (CpaFlatBuffer
));
515 if (status
!= CPA_STATUS_SUCCESS
)
517 status
= QAT_PHYS_CONTIG_ALLOC(&digest_buffer
,
518 sizeof (zio_cksum_t
));
519 if (status
!= CPA_STATUS_SUCCESS
)
524 flat_src_buf
= flat_src_buf_array
;
525 while (bytes_left
> 0) {
526 page_off
= ((long)data
& ~PAGE_MASK
);
527 in_pages
[page_num
] = qat_mem_to_page(data
);
528 flat_src_buf
->pData
= kmap(in_pages
[page_num
]) + page_off
;
529 flat_src_buf
->dataLenInBytes
=
530 min((long)PAGE_SIZE
- page_off
, (long)bytes_left
);
531 data
+= flat_src_buf
->dataLenInBytes
;
532 bytes_left
-= flat_src_buf
->dataLenInBytes
;
536 src_buffer_list
.pBuffers
= flat_src_buf_array
;
537 src_buffer_list
.numBuffers
= page_num
;
539 op_data
.sessionCtx
= cy_session_ctx
;
540 op_data
.packetType
= CPA_CY_SYM_PACKET_TYPE_FULL
;
541 op_data
.hashStartSrcOffsetInBytes
= 0;
542 op_data
.messageLenToHashInBytes
= size
;
543 op_data
.pDigestResult
= digest_buffer
;
545 cb
.verify_result
= CPA_FALSE
;
546 init_completion(&cb
.complete
);
547 status
= cpaCySymPerformOp(cy_inst_handle
, &cb
, &op_data
,
548 &src_buffer_list
, &src_buffer_list
, NULL
);
549 if (status
!= CPA_STATUS_SUCCESS
)
552 /* we now wait until the completion of the operation. */
553 wait_for_completion(&cb
.complete
);
555 if (cb
.verify_result
== CPA_FALSE
) {
556 status
= CPA_STATUS_FAIL
;
560 memcpy(zcp
, digest_buffer
, sizeof (zio_cksum_t
));
563 if (status
!= CPA_STATUS_SUCCESS
)
564 QAT_STAT_BUMP(cksum_fails
);
566 for (i
= 0; i
< page_num
; i
++)
569 cpaCySymRemoveSession(cy_inst_handle
, cy_session_ctx
);
570 QAT_PHYS_CONTIG_FREE(digest_buffer
);
571 QAT_PHYS_CONTIG_FREE(src_buffer_list
.pPrivateMetaData
);
572 QAT_PHYS_CONTIG_FREE(cy_session_ctx
);
573 QAT_PHYS_CONTIG_FREE(flat_src_buf_array
);
579 param_set_qat_encrypt(const char *val
, zfs_kernel_param_t
*kp
)
582 int *pvalue
= kp
->arg
;
583 ret
= param_set_int(val
, kp
);
587 * zfs_qat_encrypt_disable = 0: enable qat encrypt
588 * try to initialize qat instance if it has not been done
590 if (*pvalue
== 0 && !qat_cy_init_done
) {
593 zfs_qat_encrypt_disable
= 1;
601 param_set_qat_checksum(const char *val
, zfs_kernel_param_t
*kp
)
604 int *pvalue
= kp
->arg
;
605 ret
= param_set_int(val
, kp
);
609 * set_checksum_param_ops = 0: enable qat checksum
610 * try to initialize qat instance if it has not been done
612 if (*pvalue
== 0 && !qat_cy_init_done
) {
615 zfs_qat_checksum_disable
= 1;
622 module_param_call(zfs_qat_encrypt_disable
, param_set_qat_encrypt
,
623 param_get_int
, &zfs_qat_encrypt_disable
, 0644);
624 MODULE_PARM_DESC(zfs_qat_encrypt_disable
, "Enable/Disable QAT encryption");
626 module_param_call(zfs_qat_checksum_disable
, param_set_qat_checksum
,
627 param_get_int
, &zfs_qat_checksum_disable
, 0644);
628 MODULE_PARM_DESC(zfs_qat_checksum_disable
, "Enable/Disable QAT checksumming");