1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2017 Intel Corporation
11 #include <sys/queue.h>
14 #include <rte_common.h>
16 #include <rte_debug.h>
17 #include <rte_memory.h>
18 #include <rte_tailq.h>
19 #include <rte_malloc.h>
20 #include <rte_launch.h>
22 #include <rte_per_lcore.h>
23 #include <rte_lcore.h>
24 #include <rte_branch_prediction.h>
25 #include <rte_mempool.h>
27 #include <rte_string_fns.h>
28 #include <rte_spinlock.h>
29 #include <rte_hexdump.h>
30 #include <rte_crypto_sym.h>
31 #include <rte_byteorder.h>
33 #include <rte_bus_pci.h>
35 #include <openssl/evp.h>
39 #include "qat_crypto.h"
40 #include "adf_transport_access_macros.h"
43 /* bpi is only used for partial blocks of DES and AES
44 * so AES block len can be assumed as max len for iv, src and dst
46 #define BPI_MAX_ENCR_IV_LEN ICP_QAT_HW_AES_BLK_SZ
49 qat_is_cipher_alg_supported(enum rte_crypto_cipher_algorithm algo,
50 struct qat_pmd_private *internals) {
52 const struct rte_cryptodev_capabilities *capability;
54 while ((capability = &(internals->qat_dev_capabilities[i++]))->op !=
55 RTE_CRYPTO_OP_TYPE_UNDEFINED) {
56 if (capability->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
59 if (capability->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
62 if (capability->sym.cipher.algo == algo)
69 qat_is_auth_alg_supported(enum rte_crypto_auth_algorithm algo,
70 struct qat_pmd_private *internals) {
72 const struct rte_cryptodev_capabilities *capability;
74 while ((capability = &(internals->qat_dev_capabilities[i++]))->op !=
75 RTE_CRYPTO_OP_TYPE_UNDEFINED) {
76 if (capability->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
79 if (capability->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
82 if (capability->sym.auth.algo == algo)
88 /** Encrypt a single partial block
89 * Depends on openssl libcrypto
90 * Uses ECB+XOR to do CFB encryption, same result, more performant
93 bpi_cipher_encrypt(uint8_t *src, uint8_t *dst,
94 uint8_t *iv, int ivlen, int srclen,
97 EVP_CIPHER_CTX *ctx = (EVP_CIPHER_CTX *)bpi_ctx;
99 uint8_t encrypted_iv[BPI_MAX_ENCR_IV_LEN];
100 uint8_t *encr = encrypted_iv;
102 /* ECB method: encrypt the IV, then XOR this with plaintext */
103 if (EVP_EncryptUpdate(ctx, encrypted_iv, &encrypted_ivlen, iv, ivlen)
105 goto cipher_encrypt_err;
107 for (; srclen != 0; --srclen, ++dst, ++src, ++encr)
113 PMD_DRV_LOG(ERR, "libcrypto ECB cipher encrypt failed");
117 /** Decrypt a single partial block
118 * Depends on openssl libcrypto
119 * Uses ECB+XOR to do CFB encryption, same result, more performant
122 bpi_cipher_decrypt(uint8_t *src, uint8_t *dst,
123 uint8_t *iv, int ivlen, int srclen,
126 EVP_CIPHER_CTX *ctx = (EVP_CIPHER_CTX *)bpi_ctx;
128 uint8_t encrypted_iv[BPI_MAX_ENCR_IV_LEN];
129 uint8_t *encr = encrypted_iv;
131 /* ECB method: encrypt (not decrypt!) the IV, then XOR with plaintext */
132 if (EVP_EncryptUpdate(ctx, encrypted_iv, &encrypted_ivlen, iv, ivlen)
134 goto cipher_decrypt_err;
136 for (; srclen != 0; --srclen, ++dst, ++src, ++encr)
142 PMD_DRV_LOG(ERR, "libcrypto ECB cipher decrypt for BPI IV failed");
146 /** Creates a context in either AES or DES in ECB mode
147 * Depends on openssl libcrypto
150 bpi_cipher_ctx_init(enum rte_crypto_cipher_algorithm cryptodev_algo,
151 enum rte_crypto_cipher_operation direction __rte_unused,
152 uint8_t *key, void **ctx)
154 const EVP_CIPHER *algo = NULL;
156 *ctx = EVP_CIPHER_CTX_new();
163 if (cryptodev_algo == RTE_CRYPTO_CIPHER_DES_DOCSISBPI)
164 algo = EVP_des_ecb();
166 algo = EVP_aes_128_ecb();
168 /* IV will be ECB encrypted whether direction is encrypt or decrypt*/
169 if (EVP_EncryptInit_ex(*ctx, algo, NULL, key, 0) != 1) {
178 EVP_CIPHER_CTX_free(*ctx);
182 /** Frees a context previously created
183 * Depends on openssl libcrypto
186 bpi_cipher_ctx_free(void *bpi_ctx)
189 EVP_CIPHER_CTX_free((EVP_CIPHER_CTX *)bpi_ctx);
192 static inline uint32_t
193 adf_modulo(uint32_t data, uint32_t shift);
196 qat_write_hw_desc_entry(struct rte_crypto_op *op, uint8_t *out_msg,
197 struct qat_crypto_op_cookie *qat_op_cookie, struct qat_qp *qp);
200 qat_crypto_sym_clear_session(struct rte_cryptodev *dev,
201 struct rte_cryptodev_sym_session *sess)
203 PMD_INIT_FUNC_TRACE();
204 uint8_t index = dev->driver_id;
205 void *sess_priv = get_session_private_data(sess, index);
206 struct qat_session *s = (struct qat_session *)sess_priv;
210 bpi_cipher_ctx_free(s->bpi_ctx);
211 memset(s, 0, qat_crypto_sym_get_session_private_size(dev));
212 struct rte_mempool *sess_mp = rte_mempool_from_obj(sess_priv);
213 set_session_private_data(sess, index, NULL);
214 rte_mempool_put(sess_mp, sess_priv);
219 qat_get_cmd_id(const struct rte_crypto_sym_xform *xform)
222 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER && xform->next == NULL)
223 return ICP_QAT_FW_LA_CMD_CIPHER;
225 /* Authentication Only */
226 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH && xform->next == NULL)
227 return ICP_QAT_FW_LA_CMD_AUTH;
230 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
231 /* AES-GCM and AES-CCM works with different direction
232 * GCM first encrypts and generate hash where AES-CCM
233 * first generate hash and encrypts. Similar relation
234 * applies to decryption.
236 if (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
237 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM)
238 return ICP_QAT_FW_LA_CMD_CIPHER_HASH;
240 return ICP_QAT_FW_LA_CMD_HASH_CIPHER;
242 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM)
243 return ICP_QAT_FW_LA_CMD_HASH_CIPHER;
245 return ICP_QAT_FW_LA_CMD_CIPHER_HASH;
248 if (xform->next == NULL)
251 /* Cipher then Authenticate */
252 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
253 xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
254 return ICP_QAT_FW_LA_CMD_CIPHER_HASH;
256 /* Authenticate then Cipher */
257 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
258 xform->next->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
259 return ICP_QAT_FW_LA_CMD_HASH_CIPHER;
264 static struct rte_crypto_auth_xform *
265 qat_get_auth_xform(struct rte_crypto_sym_xform *xform)
268 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH)
277 static struct rte_crypto_cipher_xform *
278 qat_get_cipher_xform(struct rte_crypto_sym_xform *xform)
281 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER)
282 return &xform->cipher;
291 qat_crypto_sym_configure_session_cipher(struct rte_cryptodev *dev,
292 struct rte_crypto_sym_xform *xform,
293 struct qat_session *session)
295 struct qat_pmd_private *internals = dev->data->dev_private;
296 struct rte_crypto_cipher_xform *cipher_xform = NULL;
299 /* Get cipher xform from crypto xform chain */
300 cipher_xform = qat_get_cipher_xform(xform);
302 session->cipher_iv.offset = cipher_xform->iv.offset;
303 session->cipher_iv.length = cipher_xform->iv.length;
305 switch (cipher_xform->algo) {
306 case RTE_CRYPTO_CIPHER_AES_CBC:
307 if (qat_alg_validate_aes_key(cipher_xform->key.length,
308 &session->qat_cipher_alg) != 0) {
309 PMD_DRV_LOG(ERR, "Invalid AES cipher key size");
313 session->qat_mode = ICP_QAT_HW_CIPHER_CBC_MODE;
315 case RTE_CRYPTO_CIPHER_AES_CTR:
316 if (qat_alg_validate_aes_key(cipher_xform->key.length,
317 &session->qat_cipher_alg) != 0) {
318 PMD_DRV_LOG(ERR, "Invalid AES cipher key size");
322 session->qat_mode = ICP_QAT_HW_CIPHER_CTR_MODE;
324 case RTE_CRYPTO_CIPHER_SNOW3G_UEA2:
325 if (qat_alg_validate_snow3g_key(cipher_xform->key.length,
326 &session->qat_cipher_alg) != 0) {
327 PMD_DRV_LOG(ERR, "Invalid SNOW 3G cipher key size");
331 session->qat_mode = ICP_QAT_HW_CIPHER_ECB_MODE;
333 case RTE_CRYPTO_CIPHER_NULL:
334 session->qat_mode = ICP_QAT_HW_CIPHER_ECB_MODE;
336 case RTE_CRYPTO_CIPHER_KASUMI_F8:
337 if (qat_alg_validate_kasumi_key(cipher_xform->key.length,
338 &session->qat_cipher_alg) != 0) {
339 PMD_DRV_LOG(ERR, "Invalid KASUMI cipher key size");
343 session->qat_mode = ICP_QAT_HW_CIPHER_F8_MODE;
345 case RTE_CRYPTO_CIPHER_3DES_CBC:
346 if (qat_alg_validate_3des_key(cipher_xform->key.length,
347 &session->qat_cipher_alg) != 0) {
348 PMD_DRV_LOG(ERR, "Invalid 3DES cipher key size");
352 session->qat_mode = ICP_QAT_HW_CIPHER_CBC_MODE;
354 case RTE_CRYPTO_CIPHER_DES_CBC:
355 if (qat_alg_validate_des_key(cipher_xform->key.length,
356 &session->qat_cipher_alg) != 0) {
357 PMD_DRV_LOG(ERR, "Invalid DES cipher key size");
361 session->qat_mode = ICP_QAT_HW_CIPHER_CBC_MODE;
363 case RTE_CRYPTO_CIPHER_3DES_CTR:
364 if (qat_alg_validate_3des_key(cipher_xform->key.length,
365 &session->qat_cipher_alg) != 0) {
366 PMD_DRV_LOG(ERR, "Invalid 3DES cipher key size");
370 session->qat_mode = ICP_QAT_HW_CIPHER_CTR_MODE;
372 case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
373 ret = bpi_cipher_ctx_init(
376 cipher_xform->key.data,
379 PMD_DRV_LOG(ERR, "failed to create DES BPI ctx");
382 if (qat_alg_validate_des_key(cipher_xform->key.length,
383 &session->qat_cipher_alg) != 0) {
384 PMD_DRV_LOG(ERR, "Invalid DES cipher key size");
388 session->qat_mode = ICP_QAT_HW_CIPHER_CBC_MODE;
390 case RTE_CRYPTO_CIPHER_AES_DOCSISBPI:
391 ret = bpi_cipher_ctx_init(
394 cipher_xform->key.data,
397 PMD_DRV_LOG(ERR, "failed to create AES BPI ctx");
400 if (qat_alg_validate_aes_docsisbpi_key(cipher_xform->key.length,
401 &session->qat_cipher_alg) != 0) {
402 PMD_DRV_LOG(ERR, "Invalid AES DOCSISBPI key size");
406 session->qat_mode = ICP_QAT_HW_CIPHER_CBC_MODE;
408 case RTE_CRYPTO_CIPHER_ZUC_EEA3:
409 if (!qat_is_cipher_alg_supported(
410 cipher_xform->algo, internals)) {
411 PMD_DRV_LOG(ERR, "%s not supported on this device",
412 rte_crypto_cipher_algorithm_strings
413 [cipher_xform->algo]);
417 if (qat_alg_validate_zuc_key(cipher_xform->key.length,
418 &session->qat_cipher_alg) != 0) {
419 PMD_DRV_LOG(ERR, "Invalid ZUC cipher key size");
423 session->qat_mode = ICP_QAT_HW_CIPHER_ECB_MODE;
425 case RTE_CRYPTO_CIPHER_3DES_ECB:
426 case RTE_CRYPTO_CIPHER_AES_ECB:
427 case RTE_CRYPTO_CIPHER_AES_F8:
428 case RTE_CRYPTO_CIPHER_AES_XTS:
429 case RTE_CRYPTO_CIPHER_ARC4:
430 PMD_DRV_LOG(ERR, "Crypto QAT PMD: Unsupported Cipher alg %u",
435 PMD_DRV_LOG(ERR, "Crypto: Undefined Cipher specified %u\n",
441 if (cipher_xform->op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
442 session->qat_dir = ICP_QAT_HW_CIPHER_ENCRYPT;
444 session->qat_dir = ICP_QAT_HW_CIPHER_DECRYPT;
446 if (qat_alg_aead_session_create_content_desc_cipher(session,
447 cipher_xform->key.data,
448 cipher_xform->key.length)) {
456 if (session->bpi_ctx) {
457 bpi_cipher_ctx_free(session->bpi_ctx);
458 session->bpi_ctx = NULL;
464 qat_crypto_sym_configure_session(struct rte_cryptodev *dev,
465 struct rte_crypto_sym_xform *xform,
466 struct rte_cryptodev_sym_session *sess,
467 struct rte_mempool *mempool)
469 void *sess_private_data;
472 if (rte_mempool_get(mempool, &sess_private_data)) {
474 "Couldn't get object from session mempool");
478 ret = qat_crypto_set_session_parameters(dev, xform, sess_private_data);
480 PMD_DRV_LOG(ERR, "Crypto QAT PMD: failed to configure "
481 "session parameters");
483 /* Return session to mempool */
484 rte_mempool_put(mempool, sess_private_data);
488 set_session_private_data(sess, dev->driver_id,
495 qat_crypto_set_session_parameters(struct rte_cryptodev *dev,
496 struct rte_crypto_sym_xform *xform, void *session_private)
498 struct qat_session *session = session_private;
502 PMD_INIT_FUNC_TRACE();
504 /* Set context descriptor physical address */
505 session->cd_paddr = rte_mempool_virt2iova(session) +
506 offsetof(struct qat_session, cd);
508 session->min_qat_dev_gen = QAT_GEN1;
510 /* Get requested QAT command id */
511 qat_cmd_id = qat_get_cmd_id(xform);
512 if (qat_cmd_id < 0 || qat_cmd_id >= ICP_QAT_FW_LA_CMD_DELIMITER) {
513 PMD_DRV_LOG(ERR, "Unsupported xform chain requested");
516 session->qat_cmd = (enum icp_qat_fw_la_cmd_id)qat_cmd_id;
517 switch (session->qat_cmd) {
518 case ICP_QAT_FW_LA_CMD_CIPHER:
519 ret = qat_crypto_sym_configure_session_cipher(dev, xform, session);
523 case ICP_QAT_FW_LA_CMD_AUTH:
524 ret = qat_crypto_sym_configure_session_auth(dev, xform, session);
528 case ICP_QAT_FW_LA_CMD_CIPHER_HASH:
529 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
530 ret = qat_crypto_sym_configure_session_aead(xform,
535 ret = qat_crypto_sym_configure_session_cipher(dev,
539 ret = qat_crypto_sym_configure_session_auth(dev,
545 case ICP_QAT_FW_LA_CMD_HASH_CIPHER:
546 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
547 ret = qat_crypto_sym_configure_session_aead(xform,
552 ret = qat_crypto_sym_configure_session_auth(dev,
556 ret = qat_crypto_sym_configure_session_cipher(dev,
562 case ICP_QAT_FW_LA_CMD_TRNG_GET_RANDOM:
563 case ICP_QAT_FW_LA_CMD_TRNG_TEST:
564 case ICP_QAT_FW_LA_CMD_SSL3_KEY_DERIVE:
565 case ICP_QAT_FW_LA_CMD_TLS_V1_1_KEY_DERIVE:
566 case ICP_QAT_FW_LA_CMD_TLS_V1_2_KEY_DERIVE:
567 case ICP_QAT_FW_LA_CMD_MGF1:
568 case ICP_QAT_FW_LA_CMD_AUTH_PRE_COMP:
569 case ICP_QAT_FW_LA_CMD_CIPHER_PRE_COMP:
570 case ICP_QAT_FW_LA_CMD_DELIMITER:
571 PMD_DRV_LOG(ERR, "Unsupported Service %u",
575 PMD_DRV_LOG(ERR, "Unsupported Service %u",
584 qat_crypto_sym_configure_session_auth(struct rte_cryptodev *dev,
585 struct rte_crypto_sym_xform *xform,
586 struct qat_session *session)
588 struct rte_crypto_auth_xform *auth_xform = NULL;
589 struct qat_pmd_private *internals = dev->data->dev_private;
590 auth_xform = qat_get_auth_xform(xform);
591 uint8_t *key_data = auth_xform->key.data;
592 uint8_t key_length = auth_xform->key.length;
594 switch (auth_xform->algo) {
595 case RTE_CRYPTO_AUTH_SHA1_HMAC:
596 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_SHA1;
598 case RTE_CRYPTO_AUTH_SHA224_HMAC:
599 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_SHA224;
601 case RTE_CRYPTO_AUTH_SHA256_HMAC:
602 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_SHA256;
604 case RTE_CRYPTO_AUTH_SHA384_HMAC:
605 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_SHA384;
607 case RTE_CRYPTO_AUTH_SHA512_HMAC:
608 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_SHA512;
610 case RTE_CRYPTO_AUTH_AES_XCBC_MAC:
611 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_AES_XCBC_MAC;
613 case RTE_CRYPTO_AUTH_AES_GMAC:
614 if (qat_alg_validate_aes_key(auth_xform->key.length,
615 &session->qat_cipher_alg) != 0) {
616 PMD_DRV_LOG(ERR, "Invalid AES key size");
619 session->qat_mode = ICP_QAT_HW_CIPHER_CTR_MODE;
620 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_GALOIS_128;
623 case RTE_CRYPTO_AUTH_SNOW3G_UIA2:
624 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_SNOW_3G_UIA2;
626 case RTE_CRYPTO_AUTH_MD5_HMAC:
627 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_MD5;
629 case RTE_CRYPTO_AUTH_NULL:
630 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_NULL;
632 case RTE_CRYPTO_AUTH_KASUMI_F9:
633 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_KASUMI_F9;
635 case RTE_CRYPTO_AUTH_ZUC_EIA3:
636 if (!qat_is_auth_alg_supported(auth_xform->algo, internals)) {
637 PMD_DRV_LOG(ERR, "%s not supported on this device",
638 rte_crypto_auth_algorithm_strings
642 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_ZUC_3G_128_EIA3;
644 case RTE_CRYPTO_AUTH_SHA1:
645 case RTE_CRYPTO_AUTH_SHA256:
646 case RTE_CRYPTO_AUTH_SHA512:
647 case RTE_CRYPTO_AUTH_SHA224:
648 case RTE_CRYPTO_AUTH_SHA384:
649 case RTE_CRYPTO_AUTH_MD5:
650 case RTE_CRYPTO_AUTH_AES_CMAC:
651 case RTE_CRYPTO_AUTH_AES_CBC_MAC:
652 PMD_DRV_LOG(ERR, "Crypto: Unsupported hash alg %u",
656 PMD_DRV_LOG(ERR, "Crypto: Undefined Hash algo %u specified",
661 session->auth_iv.offset = auth_xform->iv.offset;
662 session->auth_iv.length = auth_xform->iv.length;
664 if (auth_xform->algo == RTE_CRYPTO_AUTH_AES_GMAC) {
665 if (auth_xform->op == RTE_CRYPTO_AUTH_OP_GENERATE) {
666 session->qat_cmd = ICP_QAT_FW_LA_CMD_CIPHER_HASH;
667 session->qat_dir = ICP_QAT_HW_CIPHER_ENCRYPT;
669 * It needs to create cipher desc content first,
670 * then authentication
672 if (qat_alg_aead_session_create_content_desc_cipher(session,
673 auth_xform->key.data,
674 auth_xform->key.length))
677 if (qat_alg_aead_session_create_content_desc_auth(session,
681 auth_xform->digest_length,
685 session->qat_cmd = ICP_QAT_FW_LA_CMD_HASH_CIPHER;
686 session->qat_dir = ICP_QAT_HW_CIPHER_DECRYPT;
688 * It needs to create authentication desc content first,
691 if (qat_alg_aead_session_create_content_desc_auth(session,
695 auth_xform->digest_length,
699 if (qat_alg_aead_session_create_content_desc_cipher(session,
700 auth_xform->key.data,
701 auth_xform->key.length))
704 /* Restore to authentication only only */
705 session->qat_cmd = ICP_QAT_FW_LA_CMD_AUTH;
707 if (qat_alg_aead_session_create_content_desc_auth(session,
711 auth_xform->digest_length,
716 session->digest_length = auth_xform->digest_length;
721 qat_crypto_sym_configure_session_aead(struct rte_crypto_sym_xform *xform,
722 struct qat_session *session)
724 struct rte_crypto_aead_xform *aead_xform = &xform->aead;
725 enum rte_crypto_auth_operation crypto_operation;
728 * Store AEAD IV parameters as cipher IV,
729 * to avoid unnecessary memory usage
731 session->cipher_iv.offset = xform->aead.iv.offset;
732 session->cipher_iv.length = xform->aead.iv.length;
734 switch (aead_xform->algo) {
735 case RTE_CRYPTO_AEAD_AES_GCM:
736 if (qat_alg_validate_aes_key(aead_xform->key.length,
737 &session->qat_cipher_alg) != 0) {
738 PMD_DRV_LOG(ERR, "Invalid AES key size");
741 session->qat_mode = ICP_QAT_HW_CIPHER_CTR_MODE;
742 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_GALOIS_128;
744 case RTE_CRYPTO_AEAD_AES_CCM:
745 if (qat_alg_validate_aes_key(aead_xform->key.length,
746 &session->qat_cipher_alg) != 0) {
747 PMD_DRV_LOG(ERR, "Invalid AES key size");
750 session->qat_mode = ICP_QAT_HW_CIPHER_CTR_MODE;
751 session->qat_hash_alg = ICP_QAT_HW_AUTH_ALGO_AES_CBC_MAC;
754 PMD_DRV_LOG(ERR, "Crypto: Undefined AEAD specified %u\n",
759 if ((aead_xform->op == RTE_CRYPTO_AEAD_OP_ENCRYPT &&
760 aead_xform->algo == RTE_CRYPTO_AEAD_AES_GCM) ||
761 (aead_xform->op == RTE_CRYPTO_AEAD_OP_DECRYPT &&
762 aead_xform->algo == RTE_CRYPTO_AEAD_AES_CCM)) {
763 session->qat_dir = ICP_QAT_HW_CIPHER_ENCRYPT;
765 * It needs to create cipher desc content first,
766 * then authentication
769 crypto_operation = aead_xform->algo == RTE_CRYPTO_AEAD_AES_GCM ?
770 RTE_CRYPTO_AUTH_OP_GENERATE : RTE_CRYPTO_AUTH_OP_VERIFY;
772 if (qat_alg_aead_session_create_content_desc_cipher(session,
773 aead_xform->key.data,
774 aead_xform->key.length))
777 if (qat_alg_aead_session_create_content_desc_auth(session,
778 aead_xform->key.data,
779 aead_xform->key.length,
780 aead_xform->aad_length,
781 aead_xform->digest_length,
785 session->qat_dir = ICP_QAT_HW_CIPHER_DECRYPT;
787 * It needs to create authentication desc content first,
791 crypto_operation = aead_xform->algo == RTE_CRYPTO_AEAD_AES_GCM ?
792 RTE_CRYPTO_AUTH_OP_VERIFY : RTE_CRYPTO_AUTH_OP_GENERATE;
794 if (qat_alg_aead_session_create_content_desc_auth(session,
795 aead_xform->key.data,
796 aead_xform->key.length,
797 aead_xform->aad_length,
798 aead_xform->digest_length,
802 if (qat_alg_aead_session_create_content_desc_cipher(session,
803 aead_xform->key.data,
804 aead_xform->key.length))
808 session->digest_length = aead_xform->digest_length;
812 unsigned qat_crypto_sym_get_session_private_size(
813 struct rte_cryptodev *dev __rte_unused)
815 return RTE_ALIGN_CEIL(sizeof(struct qat_session), 8);
818 static inline uint32_t
819 qat_bpicipher_preprocess(struct qat_session *ctx,
820 struct rte_crypto_op *op)
822 int block_len = qat_cipher_get_block_size(ctx->qat_cipher_alg);
823 struct rte_crypto_sym_op *sym_op = op->sym;
824 uint8_t last_block_len = block_len > 0 ?
825 sym_op->cipher.data.length % block_len : 0;
827 if (last_block_len &&
828 ctx->qat_dir == ICP_QAT_HW_CIPHER_DECRYPT) {
830 /* Decrypt last block */
831 uint8_t *last_block, *dst, *iv;
832 uint32_t last_block_offset = sym_op->cipher.data.offset +
833 sym_op->cipher.data.length - last_block_len;
834 last_block = (uint8_t *) rte_pktmbuf_mtod_offset(sym_op->m_src,
835 uint8_t *, last_block_offset);
837 if (unlikely(sym_op->m_dst != NULL))
838 /* out-of-place operation (OOP) */
839 dst = (uint8_t *) rte_pktmbuf_mtod_offset(sym_op->m_dst,
840 uint8_t *, last_block_offset);
844 if (last_block_len < sym_op->cipher.data.length)
845 /* use previous block ciphertext as IV */
846 iv = last_block - block_len;
848 /* runt block, i.e. less than one full block */
849 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
850 ctx->cipher_iv.offset);
852 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_TX
853 rte_hexdump(stdout, "BPI: src before pre-process:", last_block,
855 if (sym_op->m_dst != NULL)
856 rte_hexdump(stdout, "BPI: dst before pre-process:", dst,
859 bpi_cipher_decrypt(last_block, dst, iv, block_len,
860 last_block_len, ctx->bpi_ctx);
861 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_TX
862 rte_hexdump(stdout, "BPI: src after pre-process:", last_block,
864 if (sym_op->m_dst != NULL)
865 rte_hexdump(stdout, "BPI: dst after pre-process:", dst,
870 return sym_op->cipher.data.length - last_block_len;
873 static inline uint32_t
874 qat_bpicipher_postprocess(struct qat_session *ctx,
875 struct rte_crypto_op *op)
877 int block_len = qat_cipher_get_block_size(ctx->qat_cipher_alg);
878 struct rte_crypto_sym_op *sym_op = op->sym;
879 uint8_t last_block_len = block_len > 0 ?
880 sym_op->cipher.data.length % block_len : 0;
882 if (last_block_len > 0 &&
883 ctx->qat_dir == ICP_QAT_HW_CIPHER_ENCRYPT) {
885 /* Encrypt last block */
886 uint8_t *last_block, *dst, *iv;
887 uint32_t last_block_offset;
889 last_block_offset = sym_op->cipher.data.offset +
890 sym_op->cipher.data.length - last_block_len;
891 last_block = (uint8_t *) rte_pktmbuf_mtod_offset(sym_op->m_src,
892 uint8_t *, last_block_offset);
894 if (unlikely(sym_op->m_dst != NULL))
895 /* out-of-place operation (OOP) */
896 dst = (uint8_t *) rte_pktmbuf_mtod_offset(sym_op->m_dst,
897 uint8_t *, last_block_offset);
901 if (last_block_len < sym_op->cipher.data.length)
902 /* use previous block ciphertext as IV */
903 iv = dst - block_len;
905 /* runt block, i.e. less than one full block */
906 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
907 ctx->cipher_iv.offset);
909 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_RX
910 rte_hexdump(stdout, "BPI: src before post-process:", last_block,
912 if (sym_op->m_dst != NULL)
913 rte_hexdump(stdout, "BPI: dst before post-process:",
914 dst, last_block_len);
916 bpi_cipher_encrypt(last_block, dst, iv, block_len,
917 last_block_len, ctx->bpi_ctx);
918 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_RX
919 rte_hexdump(stdout, "BPI: src after post-process:", last_block,
921 if (sym_op->m_dst != NULL)
922 rte_hexdump(stdout, "BPI: dst after post-process:", dst,
926 return sym_op->cipher.data.length - last_block_len;
930 txq_write_tail(struct qat_qp *qp, struct qat_queue *q) {
931 WRITE_CSR_RING_TAIL(qp->mmap_bar_addr, q->hw_bundle_number,
932 q->hw_queue_number, q->tail);
933 q->nb_pending_requests = 0;
934 q->csr_tail = q->tail;
938 qat_pmd_enqueue_op_burst(void *qp, struct rte_crypto_op **ops,
941 register struct qat_queue *queue;
942 struct qat_qp *tmp_qp = (struct qat_qp *)qp;
943 register uint32_t nb_ops_sent = 0;
944 register struct rte_crypto_op **cur_op = ops;
946 uint16_t nb_ops_possible = nb_ops;
947 register uint8_t *base_addr;
948 register uint32_t tail;
951 if (unlikely(nb_ops == 0))
954 /* read params used a lot in main loop into registers */
955 queue = &(tmp_qp->tx_q);
956 base_addr = (uint8_t *)queue->base_addr;
959 /* Find how many can actually fit on the ring */
960 tmp_qp->inflights16 += nb_ops;
961 overflow = tmp_qp->inflights16 - queue->max_inflights;
963 tmp_qp->inflights16 -= overflow;
964 nb_ops_possible = nb_ops - overflow;
965 if (nb_ops_possible == 0)
969 while (nb_ops_sent != nb_ops_possible) {
970 ret = qat_write_hw_desc_entry(*cur_op, base_addr + tail,
971 tmp_qp->op_cookies[tail / queue->msg_size], tmp_qp);
973 tmp_qp->stats.enqueue_err_count++;
975 * This message cannot be enqueued,
976 * decrease number of ops that wasn't sent
978 tmp_qp->inflights16 -= nb_ops_possible - nb_ops_sent;
979 if (nb_ops_sent == 0)
984 tail = adf_modulo(tail + queue->msg_size, queue->modulo);
990 tmp_qp->stats.enqueued_count += nb_ops_sent;
991 queue->nb_pending_requests += nb_ops_sent;
992 if (tmp_qp->inflights16 < QAT_CSR_TAIL_FORCE_WRITE_THRESH ||
993 queue->nb_pending_requests > QAT_CSR_TAIL_WRITE_THRESH) {
994 txq_write_tail(tmp_qp, queue);
1000 void rxq_free_desc(struct qat_qp *qp, struct qat_queue *q)
1002 uint32_t old_head, new_head;
1005 old_head = q->csr_head;
1007 max_head = qp->nb_descriptors * q->msg_size;
1009 /* write out free descriptors */
1010 void *cur_desc = (uint8_t *)q->base_addr + old_head;
1012 if (new_head < old_head) {
1013 memset(cur_desc, ADF_RING_EMPTY_SIG_BYTE, max_head - old_head);
1014 memset(q->base_addr, ADF_RING_EMPTY_SIG_BYTE, new_head);
1016 memset(cur_desc, ADF_RING_EMPTY_SIG_BYTE, new_head - old_head);
1018 q->nb_processed_responses = 0;
1019 q->csr_head = new_head;
1021 /* write current head to CSR */
1022 WRITE_CSR_RING_HEAD(qp->mmap_bar_addr, q->hw_bundle_number,
1023 q->hw_queue_number, new_head);
1027 qat_pmd_dequeue_op_burst(void *qp, struct rte_crypto_op **ops,
1030 struct qat_queue *rx_queue, *tx_queue;
1031 struct qat_qp *tmp_qp = (struct qat_qp *)qp;
1032 uint32_t msg_counter = 0;
1033 struct rte_crypto_op *rx_op;
1034 struct icp_qat_fw_comn_resp *resp_msg;
1037 rx_queue = &(tmp_qp->rx_q);
1038 tx_queue = &(tmp_qp->tx_q);
1039 head = rx_queue->head;
1040 resp_msg = (struct icp_qat_fw_comn_resp *)
1041 ((uint8_t *)rx_queue->base_addr + head);
1043 while (*(uint32_t *)resp_msg != ADF_RING_EMPTY_SIG &&
1044 msg_counter != nb_ops) {
1045 rx_op = (struct rte_crypto_op *)(uintptr_t)
1046 (resp_msg->opaque_data);
1048 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_RX
1049 rte_hexdump(stdout, "qat_response:", (uint8_t *)resp_msg,
1050 sizeof(struct icp_qat_fw_comn_resp));
1052 if (ICP_QAT_FW_COMN_STATUS_FLAG_OK !=
1053 ICP_QAT_FW_COMN_RESP_CRYPTO_STAT_GET(
1054 resp_msg->comn_hdr.comn_status)) {
1055 rx_op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1057 struct qat_session *sess = (struct qat_session *)
1058 get_session_private_data(
1059 rx_op->sym->session,
1060 cryptodev_qat_driver_id);
1063 qat_bpicipher_postprocess(sess, rx_op);
1064 rx_op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1067 head = adf_modulo(head + rx_queue->msg_size, rx_queue->modulo);
1068 resp_msg = (struct icp_qat_fw_comn_resp *)
1069 ((uint8_t *)rx_queue->base_addr + head);
1074 if (msg_counter > 0) {
1075 rx_queue->head = head;
1076 tmp_qp->stats.dequeued_count += msg_counter;
1077 rx_queue->nb_processed_responses += msg_counter;
1078 tmp_qp->inflights16 -= msg_counter;
1080 if (rx_queue->nb_processed_responses > QAT_CSR_HEAD_WRITE_THRESH)
1081 rxq_free_desc(tmp_qp, rx_queue);
1083 /* also check if tail needs to be advanced */
1084 if (tmp_qp->inflights16 <= QAT_CSR_TAIL_FORCE_WRITE_THRESH &&
1085 tx_queue->tail != tx_queue->csr_tail) {
1086 txq_write_tail(tmp_qp, tx_queue);
1092 qat_sgl_fill_array(struct rte_mbuf *buf, uint64_t buff_start,
1093 struct qat_alg_buf_list *list, uint32_t data_len)
1097 uint32_t buf_len = rte_pktmbuf_iova(buf) -
1098 buff_start + rte_pktmbuf_data_len(buf);
1100 list->bufers[0].addr = buff_start;
1101 list->bufers[0].resrvd = 0;
1102 list->bufers[0].len = buf_len;
1104 if (data_len <= buf_len) {
1105 list->num_bufs = nr;
1106 list->bufers[0].len = data_len;
1112 if (unlikely(nr == QAT_SGL_MAX_NUMBER)) {
1113 PMD_DRV_LOG(ERR, "QAT PMD exceeded size of QAT SGL"
1115 QAT_SGL_MAX_NUMBER);
1119 list->bufers[nr].len = rte_pktmbuf_data_len(buf);
1120 list->bufers[nr].resrvd = 0;
1121 list->bufers[nr].addr = rte_pktmbuf_iova(buf);
1123 buf_len += list->bufers[nr].len;
1126 if (buf_len > data_len) {
1127 list->bufers[nr].len -=
1133 list->num_bufs = nr;
1139 set_cipher_iv(uint16_t iv_length, uint16_t iv_offset,
1140 struct icp_qat_fw_la_cipher_req_params *cipher_param,
1141 struct rte_crypto_op *op,
1142 struct icp_qat_fw_la_bulk_req *qat_req)
1144 /* copy IV into request if it fits */
1145 if (iv_length <= sizeof(cipher_param->u.cipher_IV_array)) {
1146 rte_memcpy(cipher_param->u.cipher_IV_array,
1147 rte_crypto_op_ctod_offset(op, uint8_t *,
1151 ICP_QAT_FW_LA_CIPH_IV_FLD_FLAG_SET(
1152 qat_req->comn_hdr.serv_specif_flags,
1153 ICP_QAT_FW_CIPH_IV_64BIT_PTR);
1154 cipher_param->u.s.cipher_IV_ptr =
1155 rte_crypto_op_ctophys_offset(op,
1160 /** Set IV for CCM is special case, 0th byte is set to q-1
1161 * where q is padding of nonce in 16 byte block
1164 set_cipher_iv_ccm(uint16_t iv_length, uint16_t iv_offset,
1165 struct icp_qat_fw_la_cipher_req_params *cipher_param,
1166 struct rte_crypto_op *op, uint8_t q, uint8_t aad_len_field_sz)
1168 rte_memcpy(((uint8_t *)cipher_param->u.cipher_IV_array) +
1169 ICP_QAT_HW_CCM_NONCE_OFFSET,
1170 rte_crypto_op_ctod_offset(op, uint8_t *,
1171 iv_offset) + ICP_QAT_HW_CCM_NONCE_OFFSET,
1173 *(uint8_t *)&cipher_param->u.cipher_IV_array[0] =
1174 q - ICP_QAT_HW_CCM_NONCE_OFFSET;
1176 if (aad_len_field_sz)
1177 rte_memcpy(&op->sym->aead.aad.data[ICP_QAT_HW_CCM_NONCE_OFFSET],
1178 rte_crypto_op_ctod_offset(op, uint8_t *,
1179 iv_offset) + ICP_QAT_HW_CCM_NONCE_OFFSET,
1184 qat_write_hw_desc_entry(struct rte_crypto_op *op, uint8_t *out_msg,
1185 struct qat_crypto_op_cookie *qat_op_cookie, struct qat_qp *qp)
1188 struct qat_session *ctx;
1189 struct icp_qat_fw_la_cipher_req_params *cipher_param;
1190 struct icp_qat_fw_la_auth_req_params *auth_param;
1191 register struct icp_qat_fw_la_bulk_req *qat_req;
1192 uint8_t do_auth = 0, do_cipher = 0, do_aead = 0;
1193 uint32_t cipher_len = 0, cipher_ofs = 0;
1194 uint32_t auth_len = 0, auth_ofs = 0;
1195 uint32_t min_ofs = 0;
1196 uint64_t src_buf_start = 0, dst_buf_start = 0;
1199 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_TX
1200 if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC)) {
1201 PMD_DRV_LOG(ERR, "QAT PMD only supports symmetric crypto "
1202 "operation requests, op (%p) is not a "
1203 "symmetric operation.", op);
1207 if (unlikely(op->sess_type == RTE_CRYPTO_OP_SESSIONLESS)) {
1208 PMD_DRV_LOG(ERR, "QAT PMD only supports session oriented"
1209 " requests, op (%p) is sessionless.", op);
1213 ctx = (struct qat_session *)get_session_private_data(
1214 op->sym->session, cryptodev_qat_driver_id);
1216 if (unlikely(ctx == NULL)) {
1217 PMD_DRV_LOG(ERR, "Session was not created for this device");
1221 if (unlikely(ctx->min_qat_dev_gen > qp->qat_dev_gen)) {
1222 PMD_DRV_LOG(ERR, "Session alg not supported on this device gen");
1223 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
1229 qat_req = (struct icp_qat_fw_la_bulk_req *)out_msg;
1230 rte_mov128((uint8_t *)qat_req, (const uint8_t *)&(ctx->fw_req));
1231 qat_req->comn_mid.opaque_data = (uint64_t)(uintptr_t)op;
1232 cipher_param = (void *)&qat_req->serv_specif_rqpars;
1233 auth_param = (void *)((uint8_t *)cipher_param + sizeof(*cipher_param));
1235 if (ctx->qat_cmd == ICP_QAT_FW_LA_CMD_HASH_CIPHER ||
1236 ctx->qat_cmd == ICP_QAT_FW_LA_CMD_CIPHER_HASH) {
1237 /* AES-GCM or AES-CCM */
1238 if (ctx->qat_hash_alg == ICP_QAT_HW_AUTH_ALGO_GALOIS_128 ||
1239 ctx->qat_hash_alg == ICP_QAT_HW_AUTH_ALGO_GALOIS_64 ||
1240 (ctx->qat_cipher_alg == ICP_QAT_HW_CIPHER_ALGO_AES128
1241 && ctx->qat_mode == ICP_QAT_HW_CIPHER_CTR_MODE
1242 && ctx->qat_hash_alg ==
1243 ICP_QAT_HW_AUTH_ALGO_AES_CBC_MAC)) {
1249 } else if (ctx->qat_cmd == ICP_QAT_FW_LA_CMD_AUTH) {
1252 } else if (ctx->qat_cmd == ICP_QAT_FW_LA_CMD_CIPHER) {
1259 if (ctx->qat_cipher_alg ==
1260 ICP_QAT_HW_CIPHER_ALGO_SNOW_3G_UEA2 ||
1261 ctx->qat_cipher_alg == ICP_QAT_HW_CIPHER_ALGO_KASUMI ||
1262 ctx->qat_cipher_alg ==
1263 ICP_QAT_HW_CIPHER_ALGO_ZUC_3G_128_EEA3) {
1266 (cipher_param->cipher_length % BYTE_LENGTH != 0)
1267 || (cipher_param->cipher_offset
1268 % BYTE_LENGTH != 0))) {
1270 "SNOW3G/KASUMI/ZUC in QAT PMD only supports byte aligned values");
1271 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
1274 cipher_len = op->sym->cipher.data.length >> 3;
1275 cipher_ofs = op->sym->cipher.data.offset >> 3;
1277 } else if (ctx->bpi_ctx) {
1278 /* DOCSIS - only send complete blocks to device
1279 * Process any partial block using CFB mode.
1280 * Even if 0 complete blocks, still send this to device
1281 * to get into rx queue for post-process and dequeuing
1283 cipher_len = qat_bpicipher_preprocess(ctx, op);
1284 cipher_ofs = op->sym->cipher.data.offset;
1286 cipher_len = op->sym->cipher.data.length;
1287 cipher_ofs = op->sym->cipher.data.offset;
1290 set_cipher_iv(ctx->cipher_iv.length, ctx->cipher_iv.offset,
1291 cipher_param, op, qat_req);
1292 min_ofs = cipher_ofs;
1297 if (ctx->qat_hash_alg == ICP_QAT_HW_AUTH_ALGO_SNOW_3G_UIA2 ||
1298 ctx->qat_hash_alg == ICP_QAT_HW_AUTH_ALGO_KASUMI_F9 ||
1299 ctx->qat_hash_alg ==
1300 ICP_QAT_HW_AUTH_ALGO_ZUC_3G_128_EIA3) {
1301 if (unlikely((auth_param->auth_off % BYTE_LENGTH != 0)
1302 || (auth_param->auth_len % BYTE_LENGTH != 0))) {
1304 "For SNOW3G/KASUMI/ZUC, QAT PMD only supports byte aligned values");
1305 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
1308 auth_ofs = op->sym->auth.data.offset >> 3;
1309 auth_len = op->sym->auth.data.length >> 3;
1311 auth_param->u1.aad_adr =
1312 rte_crypto_op_ctophys_offset(op,
1313 ctx->auth_iv.offset);
1315 } else if (ctx->qat_hash_alg ==
1316 ICP_QAT_HW_AUTH_ALGO_GALOIS_128 ||
1317 ctx->qat_hash_alg ==
1318 ICP_QAT_HW_AUTH_ALGO_GALOIS_64) {
1320 set_cipher_iv(ctx->auth_iv.length,
1321 ctx->auth_iv.offset,
1322 cipher_param, op, qat_req);
1323 auth_ofs = op->sym->auth.data.offset;
1324 auth_len = op->sym->auth.data.length;
1326 auth_param->u1.aad_adr = 0;
1327 auth_param->u2.aad_sz = 0;
1330 * If len(iv)==12B fw computes J0
1332 if (ctx->auth_iv.length == 12) {
1333 ICP_QAT_FW_LA_GCM_IV_LEN_FLAG_SET(
1334 qat_req->comn_hdr.serv_specif_flags,
1335 ICP_QAT_FW_LA_GCM_IV_LEN_12_OCTETS);
1339 auth_ofs = op->sym->auth.data.offset;
1340 auth_len = op->sym->auth.data.length;
1345 if (likely(ctx->qat_hash_alg != ICP_QAT_HW_AUTH_ALGO_NULL))
1346 auth_param->auth_res_addr =
1347 op->sym->auth.digest.phys_addr;
1353 * This address may used for setting AAD physical pointer
1354 * into IV offset from op
1356 rte_iova_t aad_phys_addr_aead = op->sym->aead.aad.phys_addr;
1357 if (ctx->qat_hash_alg ==
1358 ICP_QAT_HW_AUTH_ALGO_GALOIS_128 ||
1359 ctx->qat_hash_alg ==
1360 ICP_QAT_HW_AUTH_ALGO_GALOIS_64) {
1362 * If len(iv)==12B fw computes J0
1364 if (ctx->cipher_iv.length == 12) {
1365 ICP_QAT_FW_LA_GCM_IV_LEN_FLAG_SET(
1366 qat_req->comn_hdr.serv_specif_flags,
1367 ICP_QAT_FW_LA_GCM_IV_LEN_12_OCTETS);
1370 set_cipher_iv(ctx->cipher_iv.length,
1371 ctx->cipher_iv.offset,
1372 cipher_param, op, qat_req);
1374 } else if (ctx->qat_hash_alg ==
1375 ICP_QAT_HW_AUTH_ALGO_AES_CBC_MAC) {
1377 /* In case of AES-CCM this may point to user selected memory
1378 * or iv offset in cypto_op
1380 uint8_t *aad_data = op->sym->aead.aad.data;
1381 /* This is true AAD length, it not includes 18 bytes of
1384 uint8_t aad_ccm_real_len = 0;
1386 uint8_t aad_len_field_sz = 0;
1387 uint32_t msg_len_be =
1388 rte_bswap32(op->sym->aead.data.length);
1390 if (ctx->aad_len > ICP_QAT_HW_CCM_AAD_DATA_OFFSET) {
1391 aad_len_field_sz = ICP_QAT_HW_CCM_AAD_LEN_INFO;
1392 aad_ccm_real_len = ctx->aad_len -
1393 ICP_QAT_HW_CCM_AAD_B0_LEN -
1394 ICP_QAT_HW_CCM_AAD_LEN_INFO;
1397 * aad_len not greater than 18, so no actual aad data,
1398 * then use IV after op for B0 block
1400 aad_data = rte_crypto_op_ctod_offset(op, uint8_t *,
1401 ctx->cipher_iv.offset);
1402 aad_phys_addr_aead =
1403 rte_crypto_op_ctophys_offset(op,
1404 ctx->cipher_iv.offset);
1407 uint8_t q = ICP_QAT_HW_CCM_NQ_CONST - ctx->cipher_iv.length;
1409 aad_data[0] = ICP_QAT_HW_CCM_BUILD_B0_FLAGS(aad_len_field_sz,
1410 ctx->digest_length, q);
1412 if (q > ICP_QAT_HW_CCM_MSG_LEN_MAX_FIELD_SIZE) {
1413 memcpy(aad_data + ctx->cipher_iv.length +
1414 ICP_QAT_HW_CCM_NONCE_OFFSET
1415 + (q - ICP_QAT_HW_CCM_MSG_LEN_MAX_FIELD_SIZE),
1416 (uint8_t *)&msg_len_be,
1417 ICP_QAT_HW_CCM_MSG_LEN_MAX_FIELD_SIZE);
1419 memcpy(aad_data + ctx->cipher_iv.length +
1420 ICP_QAT_HW_CCM_NONCE_OFFSET,
1421 (uint8_t *)&msg_len_be
1422 + (ICP_QAT_HW_CCM_MSG_LEN_MAX_FIELD_SIZE
1426 if (aad_len_field_sz > 0) {
1427 *(uint16_t *)&aad_data[ICP_QAT_HW_CCM_AAD_B0_LEN]
1428 = rte_bswap16(aad_ccm_real_len);
1430 if ((aad_ccm_real_len + aad_len_field_sz)
1431 % ICP_QAT_HW_CCM_AAD_B0_LEN) {
1432 uint8_t pad_len = 0;
1433 uint8_t pad_idx = 0;
1435 pad_len = ICP_QAT_HW_CCM_AAD_B0_LEN -
1436 ((aad_ccm_real_len + aad_len_field_sz) %
1437 ICP_QAT_HW_CCM_AAD_B0_LEN);
1438 pad_idx = ICP_QAT_HW_CCM_AAD_B0_LEN +
1439 aad_ccm_real_len + aad_len_field_sz;
1440 memset(&aad_data[pad_idx],
1446 set_cipher_iv_ccm(ctx->cipher_iv.length,
1447 ctx->cipher_iv.offset,
1448 cipher_param, op, q,
1453 cipher_len = op->sym->aead.data.length;
1454 cipher_ofs = op->sym->aead.data.offset;
1455 auth_len = op->sym->aead.data.length;
1456 auth_ofs = op->sym->aead.data.offset;
1458 auth_param->u1.aad_adr = aad_phys_addr_aead;
1459 auth_param->auth_res_addr = op->sym->aead.digest.phys_addr;
1460 min_ofs = op->sym->aead.data.offset;
1463 if (op->sym->m_src->next || (op->sym->m_dst && op->sym->m_dst->next))
1466 /* adjust for chain case */
1467 if (do_cipher && do_auth)
1468 min_ofs = cipher_ofs < auth_ofs ? cipher_ofs : auth_ofs;
1470 if (unlikely(min_ofs >= rte_pktmbuf_data_len(op->sym->m_src) && do_sgl))
1473 if (unlikely(op->sym->m_dst != NULL)) {
1474 /* Out-of-place operation (OOP)
1475 * Don't align DMA start. DMA the minimum data-set
1476 * so as not to overwrite data in dest buffer
1479 rte_pktmbuf_iova_offset(op->sym->m_src, min_ofs);
1481 rte_pktmbuf_iova_offset(op->sym->m_dst, min_ofs);
1484 /* In-place operation
1485 * Start DMA at nearest aligned address below min_ofs
1488 rte_pktmbuf_iova_offset(op->sym->m_src, min_ofs)
1489 & QAT_64_BTYE_ALIGN_MASK;
1491 if (unlikely((rte_pktmbuf_iova(op->sym->m_src) -
1492 rte_pktmbuf_headroom(op->sym->m_src))
1494 /* alignment has pushed addr ahead of start of mbuf
1495 * so revert and take the performance hit
1498 rte_pktmbuf_iova_offset(op->sym->m_src,
1501 dst_buf_start = src_buf_start;
1504 if (do_cipher || do_aead) {
1505 cipher_param->cipher_offset =
1506 (uint32_t)rte_pktmbuf_iova_offset(
1507 op->sym->m_src, cipher_ofs) - src_buf_start;
1508 cipher_param->cipher_length = cipher_len;
1510 cipher_param->cipher_offset = 0;
1511 cipher_param->cipher_length = 0;
1514 if (do_auth || do_aead) {
1515 auth_param->auth_off = (uint32_t)rte_pktmbuf_iova_offset(
1516 op->sym->m_src, auth_ofs) - src_buf_start;
1517 auth_param->auth_len = auth_len;
1519 auth_param->auth_off = 0;
1520 auth_param->auth_len = 0;
1523 qat_req->comn_mid.dst_length =
1524 qat_req->comn_mid.src_length =
1525 (cipher_param->cipher_offset + cipher_param->cipher_length)
1526 > (auth_param->auth_off + auth_param->auth_len) ?
1527 (cipher_param->cipher_offset + cipher_param->cipher_length)
1528 : (auth_param->auth_off + auth_param->auth_len);
1532 ICP_QAT_FW_COMN_PTR_TYPE_SET(qat_req->comn_hdr.comn_req_flags,
1533 QAT_COMN_PTR_TYPE_SGL);
1534 ret = qat_sgl_fill_array(op->sym->m_src, src_buf_start,
1535 &qat_op_cookie->qat_sgl_list_src,
1536 qat_req->comn_mid.src_length);
1538 PMD_DRV_LOG(ERR, "QAT PMD Cannot fill sgl array");
1542 if (likely(op->sym->m_dst == NULL))
1543 qat_req->comn_mid.dest_data_addr =
1544 qat_req->comn_mid.src_data_addr =
1545 qat_op_cookie->qat_sgl_src_phys_addr;
1547 ret = qat_sgl_fill_array(op->sym->m_dst,
1549 &qat_op_cookie->qat_sgl_list_dst,
1550 qat_req->comn_mid.dst_length);
1553 PMD_DRV_LOG(ERR, "QAT PMD Cannot "
1558 qat_req->comn_mid.src_data_addr =
1559 qat_op_cookie->qat_sgl_src_phys_addr;
1560 qat_req->comn_mid.dest_data_addr =
1561 qat_op_cookie->qat_sgl_dst_phys_addr;
1564 qat_req->comn_mid.src_data_addr = src_buf_start;
1565 qat_req->comn_mid.dest_data_addr = dst_buf_start;
1568 #ifdef RTE_LIBRTE_PMD_QAT_DEBUG_TX
1569 rte_hexdump(stdout, "qat_req:", qat_req,
1570 sizeof(struct icp_qat_fw_la_bulk_req));
1571 rte_hexdump(stdout, "src_data:",
1572 rte_pktmbuf_mtod(op->sym->m_src, uint8_t*),
1573 rte_pktmbuf_data_len(op->sym->m_src));
1575 uint8_t *cipher_iv_ptr = rte_crypto_op_ctod_offset(op,
1577 ctx->cipher_iv.offset);
1578 rte_hexdump(stdout, "cipher iv:", cipher_iv_ptr,
1579 ctx->cipher_iv.length);
1583 if (ctx->auth_iv.length) {
1584 uint8_t *auth_iv_ptr = rte_crypto_op_ctod_offset(op,
1586 ctx->auth_iv.offset);
1587 rte_hexdump(stdout, "auth iv:", auth_iv_ptr,
1588 ctx->auth_iv.length);
1590 rte_hexdump(stdout, "digest:", op->sym->auth.digest.data,
1591 ctx->digest_length);
1595 rte_hexdump(stdout, "digest:", op->sym->aead.digest.data,
1596 ctx->digest_length);
1597 rte_hexdump(stdout, "aad:", op->sym->aead.aad.data,
1604 static inline uint32_t adf_modulo(uint32_t data, uint32_t shift)
1606 uint32_t div = data >> shift;
1607 uint32_t mult = div << shift;
1612 int qat_dev_config(__rte_unused struct rte_cryptodev *dev,
1613 __rte_unused struct rte_cryptodev_config *config)
1615 PMD_INIT_FUNC_TRACE();
1619 int qat_dev_start(__rte_unused struct rte_cryptodev *dev)
1621 PMD_INIT_FUNC_TRACE();
1625 void qat_dev_stop(__rte_unused struct rte_cryptodev *dev)
1627 PMD_INIT_FUNC_TRACE();
1630 int qat_dev_close(struct rte_cryptodev *dev)
1634 PMD_INIT_FUNC_TRACE();
1636 for (i = 0; i < dev->data->nb_queue_pairs; i++) {
1637 ret = qat_crypto_sym_qp_release(dev, i);
1645 void qat_dev_info_get(struct rte_cryptodev *dev,
1646 struct rte_cryptodev_info *info)
1648 struct qat_pmd_private *internals = dev->data->dev_private;
1650 PMD_INIT_FUNC_TRACE();
1652 info->max_nb_queue_pairs =
1653 ADF_NUM_SYM_QPS_PER_BUNDLE *
1654 ADF_NUM_BUNDLES_PER_DEV;
1655 info->feature_flags = dev->feature_flags;
1656 info->capabilities = internals->qat_dev_capabilities;
1657 info->sym.max_nb_sessions = internals->max_nb_sessions;
1658 info->driver_id = cryptodev_qat_driver_id;
1659 info->pci_dev = RTE_DEV_TO_PCI(dev->device);
1663 void qat_crypto_sym_stats_get(struct rte_cryptodev *dev,
1664 struct rte_cryptodev_stats *stats)
1667 struct qat_qp **qp = (struct qat_qp **)(dev->data->queue_pairs);
1669 PMD_INIT_FUNC_TRACE();
1670 if (stats == NULL) {
1671 PMD_DRV_LOG(ERR, "invalid stats ptr NULL");
1674 for (i = 0; i < dev->data->nb_queue_pairs; i++) {
1675 if (qp[i] == NULL) {
1676 PMD_DRV_LOG(DEBUG, "Uninitialised queue pair");
1680 stats->enqueued_count += qp[i]->stats.enqueued_count;
1681 stats->dequeued_count += qp[i]->stats.dequeued_count;
1682 stats->enqueue_err_count += qp[i]->stats.enqueue_err_count;
1683 stats->dequeue_err_count += qp[i]->stats.dequeue_err_count;
1687 void qat_crypto_sym_stats_reset(struct rte_cryptodev *dev)
1690 struct qat_qp **qp = (struct qat_qp **)(dev->data->queue_pairs);
1692 PMD_INIT_FUNC_TRACE();
1693 for (i = 0; i < dev->data->nb_queue_pairs; i++)
1694 memset(&(qp[i]->stats), 0, sizeof(qp[i]->stats));
1695 PMD_DRV_LOG(DEBUG, "QAT crypto: stats cleared");