18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only) 28c2ecf20Sopenharmony_ci/* Copyright(c) 2014 - 2020 Intel Corporation */ 38c2ecf20Sopenharmony_ci#include <linux/module.h> 48c2ecf20Sopenharmony_ci#include <linux/slab.h> 58c2ecf20Sopenharmony_ci#include <linux/crypto.h> 68c2ecf20Sopenharmony_ci#include <crypto/internal/aead.h> 78c2ecf20Sopenharmony_ci#include <crypto/internal/skcipher.h> 88c2ecf20Sopenharmony_ci#include <crypto/aes.h> 98c2ecf20Sopenharmony_ci#include <crypto/sha.h> 108c2ecf20Sopenharmony_ci#include <crypto/hash.h> 118c2ecf20Sopenharmony_ci#include <crypto/hmac.h> 128c2ecf20Sopenharmony_ci#include <crypto/algapi.h> 138c2ecf20Sopenharmony_ci#include <crypto/authenc.h> 148c2ecf20Sopenharmony_ci#include <crypto/xts.h> 158c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h> 168c2ecf20Sopenharmony_ci#include "adf_accel_devices.h" 178c2ecf20Sopenharmony_ci#include "adf_transport.h" 188c2ecf20Sopenharmony_ci#include "adf_common_drv.h" 198c2ecf20Sopenharmony_ci#include "qat_crypto.h" 208c2ecf20Sopenharmony_ci#include "icp_qat_hw.h" 218c2ecf20Sopenharmony_ci#include "icp_qat_fw.h" 228c2ecf20Sopenharmony_ci#include "icp_qat_fw_la.h" 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define QAT_AES_HW_CONFIG_ENC(alg, mode) \ 258c2ecf20Sopenharmony_ci ICP_QAT_HW_CIPHER_CONFIG_BUILD(mode, alg, \ 268c2ecf20Sopenharmony_ci ICP_QAT_HW_CIPHER_NO_CONVERT, \ 278c2ecf20Sopenharmony_ci ICP_QAT_HW_CIPHER_ENCRYPT) 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci#define QAT_AES_HW_CONFIG_DEC(alg, mode) \ 308c2ecf20Sopenharmony_ci ICP_QAT_HW_CIPHER_CONFIG_BUILD(mode, alg, \ 318c2ecf20Sopenharmony_ci ICP_QAT_HW_CIPHER_KEY_CONVERT, \ 328c2ecf20Sopenharmony_ci ICP_QAT_HW_CIPHER_DECRYPT) 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(algs_lock); 358c2ecf20Sopenharmony_cistatic unsigned int active_devs; 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci/* Common content descriptor */ 388c2ecf20Sopenharmony_cistruct qat_alg_cd { 398c2ecf20Sopenharmony_ci union { 408c2ecf20Sopenharmony_ci struct qat_enc { /* Encrypt content desc */ 418c2ecf20Sopenharmony_ci struct icp_qat_hw_cipher_algo_blk cipher; 428c2ecf20Sopenharmony_ci struct icp_qat_hw_auth_algo_blk hash; 438c2ecf20Sopenharmony_ci } qat_enc_cd; 448c2ecf20Sopenharmony_ci struct qat_dec { /* Decrypt content desc */ 458c2ecf20Sopenharmony_ci struct icp_qat_hw_auth_algo_blk hash; 468c2ecf20Sopenharmony_ci struct icp_qat_hw_cipher_algo_blk cipher; 478c2ecf20Sopenharmony_ci } qat_dec_cd; 488c2ecf20Sopenharmony_ci }; 498c2ecf20Sopenharmony_ci} __aligned(64); 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cistruct qat_alg_aead_ctx { 528c2ecf20Sopenharmony_ci struct qat_alg_cd *enc_cd; 538c2ecf20Sopenharmony_ci struct qat_alg_cd *dec_cd; 548c2ecf20Sopenharmony_ci dma_addr_t enc_cd_paddr; 558c2ecf20Sopenharmony_ci dma_addr_t dec_cd_paddr; 568c2ecf20Sopenharmony_ci struct icp_qat_fw_la_bulk_req enc_fw_req; 578c2ecf20Sopenharmony_ci struct icp_qat_fw_la_bulk_req dec_fw_req; 588c2ecf20Sopenharmony_ci struct crypto_shash *hash_tfm; 598c2ecf20Sopenharmony_ci enum icp_qat_hw_auth_algo qat_hash_alg; 608c2ecf20Sopenharmony_ci struct qat_crypto_instance *inst; 618c2ecf20Sopenharmony_ci union { 628c2ecf20Sopenharmony_ci struct sha1_state sha1; 638c2ecf20Sopenharmony_ci struct sha256_state sha256; 648c2ecf20Sopenharmony_ci struct sha512_state sha512; 658c2ecf20Sopenharmony_ci }; 668c2ecf20Sopenharmony_ci char ipad[SHA512_BLOCK_SIZE]; /* sufficient for SHA-1/SHA-256 as well */ 678c2ecf20Sopenharmony_ci char opad[SHA512_BLOCK_SIZE]; 688c2ecf20Sopenharmony_ci}; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_cistruct qat_alg_skcipher_ctx { 718c2ecf20Sopenharmony_ci struct icp_qat_hw_cipher_algo_blk *enc_cd; 728c2ecf20Sopenharmony_ci struct icp_qat_hw_cipher_algo_blk *dec_cd; 738c2ecf20Sopenharmony_ci dma_addr_t enc_cd_paddr; 748c2ecf20Sopenharmony_ci dma_addr_t dec_cd_paddr; 758c2ecf20Sopenharmony_ci struct icp_qat_fw_la_bulk_req enc_fw_req; 768c2ecf20Sopenharmony_ci struct icp_qat_fw_la_bulk_req dec_fw_req; 778c2ecf20Sopenharmony_ci struct qat_crypto_instance *inst; 788c2ecf20Sopenharmony_ci struct crypto_skcipher *ftfm; 798c2ecf20Sopenharmony_ci bool fallback; 808c2ecf20Sopenharmony_ci}; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_cistatic int qat_get_inter_state_size(enum icp_qat_hw_auth_algo qat_hash_alg) 838c2ecf20Sopenharmony_ci{ 848c2ecf20Sopenharmony_ci switch (qat_hash_alg) { 858c2ecf20Sopenharmony_ci case ICP_QAT_HW_AUTH_ALGO_SHA1: 868c2ecf20Sopenharmony_ci return ICP_QAT_HW_SHA1_STATE1_SZ; 878c2ecf20Sopenharmony_ci case ICP_QAT_HW_AUTH_ALGO_SHA256: 888c2ecf20Sopenharmony_ci return ICP_QAT_HW_SHA256_STATE1_SZ; 898c2ecf20Sopenharmony_ci case ICP_QAT_HW_AUTH_ALGO_SHA512: 908c2ecf20Sopenharmony_ci return ICP_QAT_HW_SHA512_STATE1_SZ; 918c2ecf20Sopenharmony_ci default: 928c2ecf20Sopenharmony_ci return -EFAULT; 938c2ecf20Sopenharmony_ci }; 948c2ecf20Sopenharmony_ci return -EFAULT; 958c2ecf20Sopenharmony_ci} 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_cistatic int qat_alg_do_precomputes(struct icp_qat_hw_auth_algo_blk *hash, 988c2ecf20Sopenharmony_ci struct qat_alg_aead_ctx *ctx, 998c2ecf20Sopenharmony_ci const u8 *auth_key, 1008c2ecf20Sopenharmony_ci unsigned int auth_keylen) 1018c2ecf20Sopenharmony_ci{ 1028c2ecf20Sopenharmony_ci SHASH_DESC_ON_STACK(shash, ctx->hash_tfm); 1038c2ecf20Sopenharmony_ci int block_size = crypto_shash_blocksize(ctx->hash_tfm); 1048c2ecf20Sopenharmony_ci int digest_size = crypto_shash_digestsize(ctx->hash_tfm); 1058c2ecf20Sopenharmony_ci __be32 *hash_state_out; 1068c2ecf20Sopenharmony_ci __be64 *hash512_state_out; 1078c2ecf20Sopenharmony_ci int i, offset; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci memset(ctx->ipad, 0, block_size); 1108c2ecf20Sopenharmony_ci memset(ctx->opad, 0, block_size); 1118c2ecf20Sopenharmony_ci shash->tfm = ctx->hash_tfm; 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci if (auth_keylen > block_size) { 1148c2ecf20Sopenharmony_ci int ret = crypto_shash_digest(shash, auth_key, 1158c2ecf20Sopenharmony_ci auth_keylen, ctx->ipad); 1168c2ecf20Sopenharmony_ci if (ret) 1178c2ecf20Sopenharmony_ci return ret; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci memcpy(ctx->opad, ctx->ipad, digest_size); 1208c2ecf20Sopenharmony_ci } else { 1218c2ecf20Sopenharmony_ci memcpy(ctx->ipad, auth_key, auth_keylen); 1228c2ecf20Sopenharmony_ci memcpy(ctx->opad, auth_key, auth_keylen); 1238c2ecf20Sopenharmony_ci } 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci for (i = 0; i < block_size; i++) { 1268c2ecf20Sopenharmony_ci char *ipad_ptr = ctx->ipad + i; 1278c2ecf20Sopenharmony_ci char *opad_ptr = ctx->opad + i; 1288c2ecf20Sopenharmony_ci *ipad_ptr ^= HMAC_IPAD_VALUE; 1298c2ecf20Sopenharmony_ci *opad_ptr ^= HMAC_OPAD_VALUE; 1308c2ecf20Sopenharmony_ci } 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci if (crypto_shash_init(shash)) 1338c2ecf20Sopenharmony_ci return -EFAULT; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci if (crypto_shash_update(shash, ctx->ipad, block_size)) 1368c2ecf20Sopenharmony_ci return -EFAULT; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci hash_state_out = (__be32 *)hash->sha.state1; 1398c2ecf20Sopenharmony_ci hash512_state_out = (__be64 *)hash_state_out; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci switch (ctx->qat_hash_alg) { 1428c2ecf20Sopenharmony_ci case ICP_QAT_HW_AUTH_ALGO_SHA1: 1438c2ecf20Sopenharmony_ci if (crypto_shash_export(shash, &ctx->sha1)) 1448c2ecf20Sopenharmony_ci return -EFAULT; 1458c2ecf20Sopenharmony_ci for (i = 0; i < digest_size >> 2; i++, hash_state_out++) 1468c2ecf20Sopenharmony_ci *hash_state_out = cpu_to_be32(ctx->sha1.state[i]); 1478c2ecf20Sopenharmony_ci break; 1488c2ecf20Sopenharmony_ci case ICP_QAT_HW_AUTH_ALGO_SHA256: 1498c2ecf20Sopenharmony_ci if (crypto_shash_export(shash, &ctx->sha256)) 1508c2ecf20Sopenharmony_ci return -EFAULT; 1518c2ecf20Sopenharmony_ci for (i = 0; i < digest_size >> 2; i++, hash_state_out++) 1528c2ecf20Sopenharmony_ci *hash_state_out = cpu_to_be32(ctx->sha256.state[i]); 1538c2ecf20Sopenharmony_ci break; 1548c2ecf20Sopenharmony_ci case ICP_QAT_HW_AUTH_ALGO_SHA512: 1558c2ecf20Sopenharmony_ci if (crypto_shash_export(shash, &ctx->sha512)) 1568c2ecf20Sopenharmony_ci return -EFAULT; 1578c2ecf20Sopenharmony_ci for (i = 0; i < digest_size >> 3; i++, hash512_state_out++) 1588c2ecf20Sopenharmony_ci *hash512_state_out = cpu_to_be64(ctx->sha512.state[i]); 1598c2ecf20Sopenharmony_ci break; 1608c2ecf20Sopenharmony_ci default: 1618c2ecf20Sopenharmony_ci return -EFAULT; 1628c2ecf20Sopenharmony_ci } 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci if (crypto_shash_init(shash)) 1658c2ecf20Sopenharmony_ci return -EFAULT; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci if (crypto_shash_update(shash, ctx->opad, block_size)) 1688c2ecf20Sopenharmony_ci return -EFAULT; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci offset = round_up(qat_get_inter_state_size(ctx->qat_hash_alg), 8); 1718c2ecf20Sopenharmony_ci if (offset < 0) 1728c2ecf20Sopenharmony_ci return -EFAULT; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci hash_state_out = (__be32 *)(hash->sha.state1 + offset); 1758c2ecf20Sopenharmony_ci hash512_state_out = (__be64 *)hash_state_out; 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci switch (ctx->qat_hash_alg) { 1788c2ecf20Sopenharmony_ci case ICP_QAT_HW_AUTH_ALGO_SHA1: 1798c2ecf20Sopenharmony_ci if (crypto_shash_export(shash, &ctx->sha1)) 1808c2ecf20Sopenharmony_ci return -EFAULT; 1818c2ecf20Sopenharmony_ci for (i = 0; i < digest_size >> 2; i++, hash_state_out++) 1828c2ecf20Sopenharmony_ci *hash_state_out = cpu_to_be32(ctx->sha1.state[i]); 1838c2ecf20Sopenharmony_ci break; 1848c2ecf20Sopenharmony_ci case ICP_QAT_HW_AUTH_ALGO_SHA256: 1858c2ecf20Sopenharmony_ci if (crypto_shash_export(shash, &ctx->sha256)) 1868c2ecf20Sopenharmony_ci return -EFAULT; 1878c2ecf20Sopenharmony_ci for (i = 0; i < digest_size >> 2; i++, hash_state_out++) 1888c2ecf20Sopenharmony_ci *hash_state_out = cpu_to_be32(ctx->sha256.state[i]); 1898c2ecf20Sopenharmony_ci break; 1908c2ecf20Sopenharmony_ci case ICP_QAT_HW_AUTH_ALGO_SHA512: 1918c2ecf20Sopenharmony_ci if (crypto_shash_export(shash, &ctx->sha512)) 1928c2ecf20Sopenharmony_ci return -EFAULT; 1938c2ecf20Sopenharmony_ci for (i = 0; i < digest_size >> 3; i++, hash512_state_out++) 1948c2ecf20Sopenharmony_ci *hash512_state_out = cpu_to_be64(ctx->sha512.state[i]); 1958c2ecf20Sopenharmony_ci break; 1968c2ecf20Sopenharmony_ci default: 1978c2ecf20Sopenharmony_ci return -EFAULT; 1988c2ecf20Sopenharmony_ci } 1998c2ecf20Sopenharmony_ci memzero_explicit(ctx->ipad, block_size); 2008c2ecf20Sopenharmony_ci memzero_explicit(ctx->opad, block_size); 2018c2ecf20Sopenharmony_ci return 0; 2028c2ecf20Sopenharmony_ci} 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_cistatic void qat_alg_init_hdr_iv_updt(struct icp_qat_fw_comn_req_hdr *header) 2058c2ecf20Sopenharmony_ci{ 2068c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_CIPH_IV_FLD_FLAG_SET(header->serv_specif_flags, 2078c2ecf20Sopenharmony_ci ICP_QAT_FW_CIPH_IV_64BIT_PTR); 2088c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_UPDATE_STATE_SET(header->serv_specif_flags, 2098c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_UPDATE_STATE); 2108c2ecf20Sopenharmony_ci} 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_cistatic void qat_alg_init_hdr_no_iv_updt(struct icp_qat_fw_comn_req_hdr *header) 2138c2ecf20Sopenharmony_ci{ 2148c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_CIPH_IV_FLD_FLAG_SET(header->serv_specif_flags, 2158c2ecf20Sopenharmony_ci ICP_QAT_FW_CIPH_IV_16BYTE_DATA); 2168c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_UPDATE_STATE_SET(header->serv_specif_flags, 2178c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_NO_UPDATE_STATE); 2188c2ecf20Sopenharmony_ci} 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_cistatic void qat_alg_init_common_hdr(struct icp_qat_fw_comn_req_hdr *header, 2218c2ecf20Sopenharmony_ci int aead) 2228c2ecf20Sopenharmony_ci{ 2238c2ecf20Sopenharmony_ci header->hdr_flags = 2248c2ecf20Sopenharmony_ci ICP_QAT_FW_COMN_HDR_FLAGS_BUILD(ICP_QAT_FW_COMN_REQ_FLAG_SET); 2258c2ecf20Sopenharmony_ci header->service_type = ICP_QAT_FW_COMN_REQ_CPM_FW_LA; 2268c2ecf20Sopenharmony_ci header->comn_req_flags = 2278c2ecf20Sopenharmony_ci ICP_QAT_FW_COMN_FLAGS_BUILD(QAT_COMN_CD_FLD_TYPE_64BIT_ADR, 2288c2ecf20Sopenharmony_ci QAT_COMN_PTR_TYPE_SGL); 2298c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_PARTIAL_SET(header->serv_specif_flags, 2308c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_PARTIAL_NONE); 2318c2ecf20Sopenharmony_ci if (aead) 2328c2ecf20Sopenharmony_ci qat_alg_init_hdr_no_iv_updt(header); 2338c2ecf20Sopenharmony_ci else 2348c2ecf20Sopenharmony_ci qat_alg_init_hdr_iv_updt(header); 2358c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_PROTO_SET(header->serv_specif_flags, 2368c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_NO_PROTO); 2378c2ecf20Sopenharmony_ci} 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_cistatic int qat_alg_aead_init_enc_session(struct crypto_aead *aead_tfm, 2408c2ecf20Sopenharmony_ci int alg, 2418c2ecf20Sopenharmony_ci struct crypto_authenc_keys *keys, 2428c2ecf20Sopenharmony_ci int mode) 2438c2ecf20Sopenharmony_ci{ 2448c2ecf20Sopenharmony_ci struct qat_alg_aead_ctx *ctx = crypto_aead_ctx(aead_tfm); 2458c2ecf20Sopenharmony_ci unsigned int digestsize = crypto_aead_authsize(aead_tfm); 2468c2ecf20Sopenharmony_ci struct qat_enc *enc_ctx = &ctx->enc_cd->qat_enc_cd; 2478c2ecf20Sopenharmony_ci struct icp_qat_hw_cipher_algo_blk *cipher = &enc_ctx->cipher; 2488c2ecf20Sopenharmony_ci struct icp_qat_hw_auth_algo_blk *hash = 2498c2ecf20Sopenharmony_ci (struct icp_qat_hw_auth_algo_blk *)((char *)enc_ctx + 2508c2ecf20Sopenharmony_ci sizeof(struct icp_qat_hw_auth_setup) + keys->enckeylen); 2518c2ecf20Sopenharmony_ci struct icp_qat_fw_la_bulk_req *req_tmpl = &ctx->enc_fw_req; 2528c2ecf20Sopenharmony_ci struct icp_qat_fw_comn_req_hdr_cd_pars *cd_pars = &req_tmpl->cd_pars; 2538c2ecf20Sopenharmony_ci struct icp_qat_fw_comn_req_hdr *header = &req_tmpl->comn_hdr; 2548c2ecf20Sopenharmony_ci void *ptr = &req_tmpl->cd_ctrl; 2558c2ecf20Sopenharmony_ci struct icp_qat_fw_cipher_cd_ctrl_hdr *cipher_cd_ctrl = ptr; 2568c2ecf20Sopenharmony_ci struct icp_qat_fw_auth_cd_ctrl_hdr *hash_cd_ctrl = ptr; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci /* CD setup */ 2598c2ecf20Sopenharmony_ci cipher->aes.cipher_config.val = QAT_AES_HW_CONFIG_ENC(alg, mode); 2608c2ecf20Sopenharmony_ci memcpy(cipher->aes.key, keys->enckey, keys->enckeylen); 2618c2ecf20Sopenharmony_ci hash->sha.inner_setup.auth_config.config = 2628c2ecf20Sopenharmony_ci ICP_QAT_HW_AUTH_CONFIG_BUILD(ICP_QAT_HW_AUTH_MODE1, 2638c2ecf20Sopenharmony_ci ctx->qat_hash_alg, digestsize); 2648c2ecf20Sopenharmony_ci hash->sha.inner_setup.auth_counter.counter = 2658c2ecf20Sopenharmony_ci cpu_to_be32(crypto_shash_blocksize(ctx->hash_tfm)); 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_ci if (qat_alg_do_precomputes(hash, ctx, keys->authkey, keys->authkeylen)) 2688c2ecf20Sopenharmony_ci return -EFAULT; 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci /* Request setup */ 2718c2ecf20Sopenharmony_ci qat_alg_init_common_hdr(header, 1); 2728c2ecf20Sopenharmony_ci header->service_cmd_id = ICP_QAT_FW_LA_CMD_CIPHER_HASH; 2738c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_DIGEST_IN_BUFFER_SET(header->serv_specif_flags, 2748c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_DIGEST_IN_BUFFER); 2758c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_RET_AUTH_SET(header->serv_specif_flags, 2768c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_RET_AUTH_RES); 2778c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_CMP_AUTH_SET(header->serv_specif_flags, 2788c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_NO_CMP_AUTH_RES); 2798c2ecf20Sopenharmony_ci cd_pars->u.s.content_desc_addr = ctx->enc_cd_paddr; 2808c2ecf20Sopenharmony_ci cd_pars->u.s.content_desc_params_sz = sizeof(struct qat_alg_cd) >> 3; 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci /* Cipher CD config setup */ 2838c2ecf20Sopenharmony_ci cipher_cd_ctrl->cipher_key_sz = keys->enckeylen >> 3; 2848c2ecf20Sopenharmony_ci cipher_cd_ctrl->cipher_state_sz = AES_BLOCK_SIZE >> 3; 2858c2ecf20Sopenharmony_ci cipher_cd_ctrl->cipher_cfg_offset = 0; 2868c2ecf20Sopenharmony_ci ICP_QAT_FW_COMN_CURR_ID_SET(cipher_cd_ctrl, ICP_QAT_FW_SLICE_CIPHER); 2878c2ecf20Sopenharmony_ci ICP_QAT_FW_COMN_NEXT_ID_SET(cipher_cd_ctrl, ICP_QAT_FW_SLICE_AUTH); 2888c2ecf20Sopenharmony_ci /* Auth CD config setup */ 2898c2ecf20Sopenharmony_ci hash_cd_ctrl->hash_cfg_offset = ((char *)hash - (char *)cipher) >> 3; 2908c2ecf20Sopenharmony_ci hash_cd_ctrl->hash_flags = ICP_QAT_FW_AUTH_HDR_FLAG_NO_NESTED; 2918c2ecf20Sopenharmony_ci hash_cd_ctrl->inner_res_sz = digestsize; 2928c2ecf20Sopenharmony_ci hash_cd_ctrl->final_sz = digestsize; 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci switch (ctx->qat_hash_alg) { 2958c2ecf20Sopenharmony_ci case ICP_QAT_HW_AUTH_ALGO_SHA1: 2968c2ecf20Sopenharmony_ci hash_cd_ctrl->inner_state1_sz = 2978c2ecf20Sopenharmony_ci round_up(ICP_QAT_HW_SHA1_STATE1_SZ, 8); 2988c2ecf20Sopenharmony_ci hash_cd_ctrl->inner_state2_sz = 2998c2ecf20Sopenharmony_ci round_up(ICP_QAT_HW_SHA1_STATE2_SZ, 8); 3008c2ecf20Sopenharmony_ci break; 3018c2ecf20Sopenharmony_ci case ICP_QAT_HW_AUTH_ALGO_SHA256: 3028c2ecf20Sopenharmony_ci hash_cd_ctrl->inner_state1_sz = ICP_QAT_HW_SHA256_STATE1_SZ; 3038c2ecf20Sopenharmony_ci hash_cd_ctrl->inner_state2_sz = ICP_QAT_HW_SHA256_STATE2_SZ; 3048c2ecf20Sopenharmony_ci break; 3058c2ecf20Sopenharmony_ci case ICP_QAT_HW_AUTH_ALGO_SHA512: 3068c2ecf20Sopenharmony_ci hash_cd_ctrl->inner_state1_sz = ICP_QAT_HW_SHA512_STATE1_SZ; 3078c2ecf20Sopenharmony_ci hash_cd_ctrl->inner_state2_sz = ICP_QAT_HW_SHA512_STATE2_SZ; 3088c2ecf20Sopenharmony_ci break; 3098c2ecf20Sopenharmony_ci default: 3108c2ecf20Sopenharmony_ci break; 3118c2ecf20Sopenharmony_ci } 3128c2ecf20Sopenharmony_ci hash_cd_ctrl->inner_state2_offset = hash_cd_ctrl->hash_cfg_offset + 3138c2ecf20Sopenharmony_ci ((sizeof(struct icp_qat_hw_auth_setup) + 3148c2ecf20Sopenharmony_ci round_up(hash_cd_ctrl->inner_state1_sz, 8)) >> 3); 3158c2ecf20Sopenharmony_ci ICP_QAT_FW_COMN_CURR_ID_SET(hash_cd_ctrl, ICP_QAT_FW_SLICE_AUTH); 3168c2ecf20Sopenharmony_ci ICP_QAT_FW_COMN_NEXT_ID_SET(hash_cd_ctrl, ICP_QAT_FW_SLICE_DRAM_WR); 3178c2ecf20Sopenharmony_ci return 0; 3188c2ecf20Sopenharmony_ci} 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_cistatic int qat_alg_aead_init_dec_session(struct crypto_aead *aead_tfm, 3218c2ecf20Sopenharmony_ci int alg, 3228c2ecf20Sopenharmony_ci struct crypto_authenc_keys *keys, 3238c2ecf20Sopenharmony_ci int mode) 3248c2ecf20Sopenharmony_ci{ 3258c2ecf20Sopenharmony_ci struct qat_alg_aead_ctx *ctx = crypto_aead_ctx(aead_tfm); 3268c2ecf20Sopenharmony_ci unsigned int digestsize = crypto_aead_authsize(aead_tfm); 3278c2ecf20Sopenharmony_ci struct qat_dec *dec_ctx = &ctx->dec_cd->qat_dec_cd; 3288c2ecf20Sopenharmony_ci struct icp_qat_hw_auth_algo_blk *hash = &dec_ctx->hash; 3298c2ecf20Sopenharmony_ci struct icp_qat_hw_cipher_algo_blk *cipher = 3308c2ecf20Sopenharmony_ci (struct icp_qat_hw_cipher_algo_blk *)((char *)dec_ctx + 3318c2ecf20Sopenharmony_ci sizeof(struct icp_qat_hw_auth_setup) + 3328c2ecf20Sopenharmony_ci roundup(crypto_shash_digestsize(ctx->hash_tfm), 8) * 2); 3338c2ecf20Sopenharmony_ci struct icp_qat_fw_la_bulk_req *req_tmpl = &ctx->dec_fw_req; 3348c2ecf20Sopenharmony_ci struct icp_qat_fw_comn_req_hdr_cd_pars *cd_pars = &req_tmpl->cd_pars; 3358c2ecf20Sopenharmony_ci struct icp_qat_fw_comn_req_hdr *header = &req_tmpl->comn_hdr; 3368c2ecf20Sopenharmony_ci void *ptr = &req_tmpl->cd_ctrl; 3378c2ecf20Sopenharmony_ci struct icp_qat_fw_cipher_cd_ctrl_hdr *cipher_cd_ctrl = ptr; 3388c2ecf20Sopenharmony_ci struct icp_qat_fw_auth_cd_ctrl_hdr *hash_cd_ctrl = ptr; 3398c2ecf20Sopenharmony_ci struct icp_qat_fw_la_auth_req_params *auth_param = 3408c2ecf20Sopenharmony_ci (struct icp_qat_fw_la_auth_req_params *) 3418c2ecf20Sopenharmony_ci ((char *)&req_tmpl->serv_specif_rqpars + 3428c2ecf20Sopenharmony_ci sizeof(struct icp_qat_fw_la_cipher_req_params)); 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_ci /* CD setup */ 3458c2ecf20Sopenharmony_ci cipher->aes.cipher_config.val = QAT_AES_HW_CONFIG_DEC(alg, mode); 3468c2ecf20Sopenharmony_ci memcpy(cipher->aes.key, keys->enckey, keys->enckeylen); 3478c2ecf20Sopenharmony_ci hash->sha.inner_setup.auth_config.config = 3488c2ecf20Sopenharmony_ci ICP_QAT_HW_AUTH_CONFIG_BUILD(ICP_QAT_HW_AUTH_MODE1, 3498c2ecf20Sopenharmony_ci ctx->qat_hash_alg, 3508c2ecf20Sopenharmony_ci digestsize); 3518c2ecf20Sopenharmony_ci hash->sha.inner_setup.auth_counter.counter = 3528c2ecf20Sopenharmony_ci cpu_to_be32(crypto_shash_blocksize(ctx->hash_tfm)); 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ci if (qat_alg_do_precomputes(hash, ctx, keys->authkey, keys->authkeylen)) 3558c2ecf20Sopenharmony_ci return -EFAULT; 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_ci /* Request setup */ 3588c2ecf20Sopenharmony_ci qat_alg_init_common_hdr(header, 1); 3598c2ecf20Sopenharmony_ci header->service_cmd_id = ICP_QAT_FW_LA_CMD_HASH_CIPHER; 3608c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_DIGEST_IN_BUFFER_SET(header->serv_specif_flags, 3618c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_DIGEST_IN_BUFFER); 3628c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_RET_AUTH_SET(header->serv_specif_flags, 3638c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_NO_RET_AUTH_RES); 3648c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_CMP_AUTH_SET(header->serv_specif_flags, 3658c2ecf20Sopenharmony_ci ICP_QAT_FW_LA_CMP_AUTH_RES); 3668c2ecf20Sopenharmony_ci cd_pars->u.s.content_desc_addr = ctx->dec_cd_paddr; 3678c2ecf20Sopenharmony_ci cd_pars->u.s.content_desc_params_sz = sizeof(struct qat_alg_cd) >> 3; 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci /* Cipher CD config setup */ 3708c2ecf20Sopenharmony_ci cipher_cd_ctrl->cipher_key_sz = keys->enckeylen >> 3; 3718c2ecf20Sopenharmony_ci cipher_cd_ctrl->cipher_state_sz = AES_BLOCK_SIZE >> 3; 3728c2ecf20Sopenharmony_ci cipher_cd_ctrl->cipher_cfg_offset = 3738c2ecf20Sopenharmony_ci (sizeof(struct icp_qat_hw_auth_setup) + 3748c2ecf20Sopenharmony_ci roundup(crypto_shash_digestsize(ctx->hash_tfm), 8) * 2) >> 3; 3758c2ecf20Sopenharmony_ci ICP_QAT_FW_COMN_CURR_ID_SET(cipher_cd_ctrl, ICP_QAT_FW_SLICE_CIPHER); 3768c2ecf20Sopenharmony_ci ICP_QAT_FW_COMN_NEXT_ID_SET(cipher_cd_ctrl, ICP_QAT_FW_SLICE_DRAM_WR); 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci /* Auth CD config setup */ 3798c2ecf20Sopenharmony_ci hash_cd_ctrl->hash_cfg_offset = 0; 3808c2ecf20Sopenharmony_ci hash_cd_ctrl->hash_flags = ICP_QAT_FW_AUTH_HDR_FLAG_NO_NESTED; 3818c2ecf20Sopenharmony_ci hash_cd_ctrl->inner_res_sz = digestsize; 3828c2ecf20Sopenharmony_ci hash_cd_ctrl->final_sz = digestsize; 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci switch (ctx->qat_hash_alg) { 3858c2ecf20Sopenharmony_ci case ICP_QAT_HW_AUTH_ALGO_SHA1: 3868c2ecf20Sopenharmony_ci hash_cd_ctrl->inner_state1_sz = 3878c2ecf20Sopenharmony_ci round_up(ICP_QAT_HW_SHA1_STATE1_SZ, 8); 3888c2ecf20Sopenharmony_ci hash_cd_ctrl->inner_state2_sz = 3898c2ecf20Sopenharmony_ci round_up(ICP_QAT_HW_SHA1_STATE2_SZ, 8); 3908c2ecf20Sopenharmony_ci break; 3918c2ecf20Sopenharmony_ci case ICP_QAT_HW_AUTH_ALGO_SHA256: 3928c2ecf20Sopenharmony_ci hash_cd_ctrl->inner_state1_sz = ICP_QAT_HW_SHA256_STATE1_SZ; 3938c2ecf20Sopenharmony_ci hash_cd_ctrl->inner_state2_sz = ICP_QAT_HW_SHA256_STATE2_SZ; 3948c2ecf20Sopenharmony_ci break; 3958c2ecf20Sopenharmony_ci case ICP_QAT_HW_AUTH_ALGO_SHA512: 3968c2ecf20Sopenharmony_ci hash_cd_ctrl->inner_state1_sz = ICP_QAT_HW_SHA512_STATE1_SZ; 3978c2ecf20Sopenharmony_ci hash_cd_ctrl->inner_state2_sz = ICP_QAT_HW_SHA512_STATE2_SZ; 3988c2ecf20Sopenharmony_ci break; 3998c2ecf20Sopenharmony_ci default: 4008c2ecf20Sopenharmony_ci break; 4018c2ecf20Sopenharmony_ci } 4028c2ecf20Sopenharmony_ci 4038c2ecf20Sopenharmony_ci hash_cd_ctrl->inner_state2_offset = hash_cd_ctrl->hash_cfg_offset + 4048c2ecf20Sopenharmony_ci ((sizeof(struct icp_qat_hw_auth_setup) + 4058c2ecf20Sopenharmony_ci round_up(hash_cd_ctrl->inner_state1_sz, 8)) >> 3); 4068c2ecf20Sopenharmony_ci auth_param->auth_res_sz = digestsize; 4078c2ecf20Sopenharmony_ci ICP_QAT_FW_COMN_CURR_ID_SET(hash_cd_ctrl, ICP_QAT_FW_SLICE_AUTH); 4088c2ecf20Sopenharmony_ci ICP_QAT_FW_COMN_NEXT_ID_SET(hash_cd_ctrl, ICP_QAT_FW_SLICE_CIPHER); 4098c2ecf20Sopenharmony_ci return 0; 4108c2ecf20Sopenharmony_ci} 4118c2ecf20Sopenharmony_ci 4128c2ecf20Sopenharmony_cistatic void qat_alg_skcipher_init_com(struct qat_alg_skcipher_ctx *ctx, 4138c2ecf20Sopenharmony_ci struct icp_qat_fw_la_bulk_req *req, 4148c2ecf20Sopenharmony_ci struct icp_qat_hw_cipher_algo_blk *cd, 4158c2ecf20Sopenharmony_ci const u8 *key, unsigned int keylen) 4168c2ecf20Sopenharmony_ci{ 4178c2ecf20Sopenharmony_ci struct icp_qat_fw_comn_req_hdr_cd_pars *cd_pars = &req->cd_pars; 4188c2ecf20Sopenharmony_ci struct icp_qat_fw_comn_req_hdr *header = &req->comn_hdr; 4198c2ecf20Sopenharmony_ci struct icp_qat_fw_cipher_cd_ctrl_hdr *cd_ctrl = (void *)&req->cd_ctrl; 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci memcpy(cd->aes.key, key, keylen); 4228c2ecf20Sopenharmony_ci qat_alg_init_common_hdr(header, 0); 4238c2ecf20Sopenharmony_ci header->service_cmd_id = ICP_QAT_FW_LA_CMD_CIPHER; 4248c2ecf20Sopenharmony_ci cd_pars->u.s.content_desc_params_sz = 4258c2ecf20Sopenharmony_ci sizeof(struct icp_qat_hw_cipher_algo_blk) >> 3; 4268c2ecf20Sopenharmony_ci /* Cipher CD config setup */ 4278c2ecf20Sopenharmony_ci cd_ctrl->cipher_key_sz = keylen >> 3; 4288c2ecf20Sopenharmony_ci cd_ctrl->cipher_state_sz = AES_BLOCK_SIZE >> 3; 4298c2ecf20Sopenharmony_ci cd_ctrl->cipher_cfg_offset = 0; 4308c2ecf20Sopenharmony_ci ICP_QAT_FW_COMN_CURR_ID_SET(cd_ctrl, ICP_QAT_FW_SLICE_CIPHER); 4318c2ecf20Sopenharmony_ci ICP_QAT_FW_COMN_NEXT_ID_SET(cd_ctrl, ICP_QAT_FW_SLICE_DRAM_WR); 4328c2ecf20Sopenharmony_ci} 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_cistatic void qat_alg_skcipher_init_enc(struct qat_alg_skcipher_ctx *ctx, 4358c2ecf20Sopenharmony_ci int alg, const u8 *key, 4368c2ecf20Sopenharmony_ci unsigned int keylen, int mode) 4378c2ecf20Sopenharmony_ci{ 4388c2ecf20Sopenharmony_ci struct icp_qat_hw_cipher_algo_blk *enc_cd = ctx->enc_cd; 4398c2ecf20Sopenharmony_ci struct icp_qat_fw_la_bulk_req *req = &ctx->enc_fw_req; 4408c2ecf20Sopenharmony_ci struct icp_qat_fw_comn_req_hdr_cd_pars *cd_pars = &req->cd_pars; 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci qat_alg_skcipher_init_com(ctx, req, enc_cd, key, keylen); 4438c2ecf20Sopenharmony_ci cd_pars->u.s.content_desc_addr = ctx->enc_cd_paddr; 4448c2ecf20Sopenharmony_ci enc_cd->aes.cipher_config.val = QAT_AES_HW_CONFIG_ENC(alg, mode); 4458c2ecf20Sopenharmony_ci} 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_cistatic void qat_alg_skcipher_init_dec(struct qat_alg_skcipher_ctx *ctx, 4488c2ecf20Sopenharmony_ci int alg, const u8 *key, 4498c2ecf20Sopenharmony_ci unsigned int keylen, int mode) 4508c2ecf20Sopenharmony_ci{ 4518c2ecf20Sopenharmony_ci struct icp_qat_hw_cipher_algo_blk *dec_cd = ctx->dec_cd; 4528c2ecf20Sopenharmony_ci struct icp_qat_fw_la_bulk_req *req = &ctx->dec_fw_req; 4538c2ecf20Sopenharmony_ci struct icp_qat_fw_comn_req_hdr_cd_pars *cd_pars = &req->cd_pars; 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci qat_alg_skcipher_init_com(ctx, req, dec_cd, key, keylen); 4568c2ecf20Sopenharmony_ci cd_pars->u.s.content_desc_addr = ctx->dec_cd_paddr; 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ci if (mode != ICP_QAT_HW_CIPHER_CTR_MODE) 4598c2ecf20Sopenharmony_ci dec_cd->aes.cipher_config.val = 4608c2ecf20Sopenharmony_ci QAT_AES_HW_CONFIG_DEC(alg, mode); 4618c2ecf20Sopenharmony_ci else 4628c2ecf20Sopenharmony_ci dec_cd->aes.cipher_config.val = 4638c2ecf20Sopenharmony_ci QAT_AES_HW_CONFIG_ENC(alg, mode); 4648c2ecf20Sopenharmony_ci} 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_cistatic int qat_alg_validate_key(int key_len, int *alg, int mode) 4678c2ecf20Sopenharmony_ci{ 4688c2ecf20Sopenharmony_ci if (mode != ICP_QAT_HW_CIPHER_XTS_MODE) { 4698c2ecf20Sopenharmony_ci switch (key_len) { 4708c2ecf20Sopenharmony_ci case AES_KEYSIZE_128: 4718c2ecf20Sopenharmony_ci *alg = ICP_QAT_HW_CIPHER_ALGO_AES128; 4728c2ecf20Sopenharmony_ci break; 4738c2ecf20Sopenharmony_ci case AES_KEYSIZE_192: 4748c2ecf20Sopenharmony_ci *alg = ICP_QAT_HW_CIPHER_ALGO_AES192; 4758c2ecf20Sopenharmony_ci break; 4768c2ecf20Sopenharmony_ci case AES_KEYSIZE_256: 4778c2ecf20Sopenharmony_ci *alg = ICP_QAT_HW_CIPHER_ALGO_AES256; 4788c2ecf20Sopenharmony_ci break; 4798c2ecf20Sopenharmony_ci default: 4808c2ecf20Sopenharmony_ci return -EINVAL; 4818c2ecf20Sopenharmony_ci } 4828c2ecf20Sopenharmony_ci } else { 4838c2ecf20Sopenharmony_ci switch (key_len) { 4848c2ecf20Sopenharmony_ci case AES_KEYSIZE_128 << 1: 4858c2ecf20Sopenharmony_ci *alg = ICP_QAT_HW_CIPHER_ALGO_AES128; 4868c2ecf20Sopenharmony_ci break; 4878c2ecf20Sopenharmony_ci case AES_KEYSIZE_256 << 1: 4888c2ecf20Sopenharmony_ci *alg = ICP_QAT_HW_CIPHER_ALGO_AES256; 4898c2ecf20Sopenharmony_ci break; 4908c2ecf20Sopenharmony_ci default: 4918c2ecf20Sopenharmony_ci return -EINVAL; 4928c2ecf20Sopenharmony_ci } 4938c2ecf20Sopenharmony_ci } 4948c2ecf20Sopenharmony_ci return 0; 4958c2ecf20Sopenharmony_ci} 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_cistatic int qat_alg_aead_init_sessions(struct crypto_aead *tfm, const u8 *key, 4988c2ecf20Sopenharmony_ci unsigned int keylen, int mode) 4998c2ecf20Sopenharmony_ci{ 5008c2ecf20Sopenharmony_ci struct crypto_authenc_keys keys; 5018c2ecf20Sopenharmony_ci int alg; 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_ci if (crypto_authenc_extractkeys(&keys, key, keylen)) 5048c2ecf20Sopenharmony_ci goto bad_key; 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci if (qat_alg_validate_key(keys.enckeylen, &alg, mode)) 5078c2ecf20Sopenharmony_ci goto bad_key; 5088c2ecf20Sopenharmony_ci 5098c2ecf20Sopenharmony_ci if (qat_alg_aead_init_enc_session(tfm, alg, &keys, mode)) 5108c2ecf20Sopenharmony_ci goto error; 5118c2ecf20Sopenharmony_ci 5128c2ecf20Sopenharmony_ci if (qat_alg_aead_init_dec_session(tfm, alg, &keys, mode)) 5138c2ecf20Sopenharmony_ci goto error; 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci memzero_explicit(&keys, sizeof(keys)); 5168c2ecf20Sopenharmony_ci return 0; 5178c2ecf20Sopenharmony_cibad_key: 5188c2ecf20Sopenharmony_ci memzero_explicit(&keys, sizeof(keys)); 5198c2ecf20Sopenharmony_ci return -EINVAL; 5208c2ecf20Sopenharmony_cierror: 5218c2ecf20Sopenharmony_ci memzero_explicit(&keys, sizeof(keys)); 5228c2ecf20Sopenharmony_ci return -EFAULT; 5238c2ecf20Sopenharmony_ci} 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_cistatic int qat_alg_skcipher_init_sessions(struct qat_alg_skcipher_ctx *ctx, 5268c2ecf20Sopenharmony_ci const u8 *key, 5278c2ecf20Sopenharmony_ci unsigned int keylen, 5288c2ecf20Sopenharmony_ci int mode) 5298c2ecf20Sopenharmony_ci{ 5308c2ecf20Sopenharmony_ci int alg; 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ci if (qat_alg_validate_key(keylen, &alg, mode)) 5338c2ecf20Sopenharmony_ci return -EINVAL; 5348c2ecf20Sopenharmony_ci 5358c2ecf20Sopenharmony_ci qat_alg_skcipher_init_enc(ctx, alg, key, keylen, mode); 5368c2ecf20Sopenharmony_ci qat_alg_skcipher_init_dec(ctx, alg, key, keylen, mode); 5378c2ecf20Sopenharmony_ci return 0; 5388c2ecf20Sopenharmony_ci} 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_cistatic int qat_alg_aead_rekey(struct crypto_aead *tfm, const u8 *key, 5418c2ecf20Sopenharmony_ci unsigned int keylen) 5428c2ecf20Sopenharmony_ci{ 5438c2ecf20Sopenharmony_ci struct qat_alg_aead_ctx *ctx = crypto_aead_ctx(tfm); 5448c2ecf20Sopenharmony_ci 5458c2ecf20Sopenharmony_ci memset(ctx->enc_cd, 0, sizeof(*ctx->enc_cd)); 5468c2ecf20Sopenharmony_ci memset(ctx->dec_cd, 0, sizeof(*ctx->dec_cd)); 5478c2ecf20Sopenharmony_ci memset(&ctx->enc_fw_req, 0, sizeof(ctx->enc_fw_req)); 5488c2ecf20Sopenharmony_ci memset(&ctx->dec_fw_req, 0, sizeof(ctx->dec_fw_req)); 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ci return qat_alg_aead_init_sessions(tfm, key, keylen, 5518c2ecf20Sopenharmony_ci ICP_QAT_HW_CIPHER_CBC_MODE); 5528c2ecf20Sopenharmony_ci} 5538c2ecf20Sopenharmony_ci 5548c2ecf20Sopenharmony_cistatic int qat_alg_aead_newkey(struct crypto_aead *tfm, const u8 *key, 5558c2ecf20Sopenharmony_ci unsigned int keylen) 5568c2ecf20Sopenharmony_ci{ 5578c2ecf20Sopenharmony_ci struct qat_alg_aead_ctx *ctx = crypto_aead_ctx(tfm); 5588c2ecf20Sopenharmony_ci struct qat_crypto_instance *inst = NULL; 5598c2ecf20Sopenharmony_ci int node = get_current_node(); 5608c2ecf20Sopenharmony_ci struct device *dev; 5618c2ecf20Sopenharmony_ci int ret; 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_ci inst = qat_crypto_get_instance_node(node); 5648c2ecf20Sopenharmony_ci if (!inst) 5658c2ecf20Sopenharmony_ci return -EINVAL; 5668c2ecf20Sopenharmony_ci dev = &GET_DEV(inst->accel_dev); 5678c2ecf20Sopenharmony_ci ctx->inst = inst; 5688c2ecf20Sopenharmony_ci ctx->enc_cd = dma_alloc_coherent(dev, sizeof(*ctx->enc_cd), 5698c2ecf20Sopenharmony_ci &ctx->enc_cd_paddr, 5708c2ecf20Sopenharmony_ci GFP_ATOMIC); 5718c2ecf20Sopenharmony_ci if (!ctx->enc_cd) { 5728c2ecf20Sopenharmony_ci ret = -ENOMEM; 5738c2ecf20Sopenharmony_ci goto out_free_inst; 5748c2ecf20Sopenharmony_ci } 5758c2ecf20Sopenharmony_ci ctx->dec_cd = dma_alloc_coherent(dev, sizeof(*ctx->dec_cd), 5768c2ecf20Sopenharmony_ci &ctx->dec_cd_paddr, 5778c2ecf20Sopenharmony_ci GFP_ATOMIC); 5788c2ecf20Sopenharmony_ci if (!ctx->dec_cd) { 5798c2ecf20Sopenharmony_ci ret = -ENOMEM; 5808c2ecf20Sopenharmony_ci goto out_free_enc; 5818c2ecf20Sopenharmony_ci } 5828c2ecf20Sopenharmony_ci 5838c2ecf20Sopenharmony_ci ret = qat_alg_aead_init_sessions(tfm, key, keylen, 5848c2ecf20Sopenharmony_ci ICP_QAT_HW_CIPHER_CBC_MODE); 5858c2ecf20Sopenharmony_ci if (ret) 5868c2ecf20Sopenharmony_ci goto out_free_all; 5878c2ecf20Sopenharmony_ci 5888c2ecf20Sopenharmony_ci return 0; 5898c2ecf20Sopenharmony_ci 5908c2ecf20Sopenharmony_ciout_free_all: 5918c2ecf20Sopenharmony_ci memset(ctx->dec_cd, 0, sizeof(struct qat_alg_cd)); 5928c2ecf20Sopenharmony_ci dma_free_coherent(dev, sizeof(struct qat_alg_cd), 5938c2ecf20Sopenharmony_ci ctx->dec_cd, ctx->dec_cd_paddr); 5948c2ecf20Sopenharmony_ci ctx->dec_cd = NULL; 5958c2ecf20Sopenharmony_ciout_free_enc: 5968c2ecf20Sopenharmony_ci memset(ctx->enc_cd, 0, sizeof(struct qat_alg_cd)); 5978c2ecf20Sopenharmony_ci dma_free_coherent(dev, sizeof(struct qat_alg_cd), 5988c2ecf20Sopenharmony_ci ctx->enc_cd, ctx->enc_cd_paddr); 5998c2ecf20Sopenharmony_ci ctx->enc_cd = NULL; 6008c2ecf20Sopenharmony_ciout_free_inst: 6018c2ecf20Sopenharmony_ci ctx->inst = NULL; 6028c2ecf20Sopenharmony_ci qat_crypto_put_instance(inst); 6038c2ecf20Sopenharmony_ci return ret; 6048c2ecf20Sopenharmony_ci} 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_cistatic int qat_alg_aead_setkey(struct crypto_aead *tfm, const u8 *key, 6078c2ecf20Sopenharmony_ci unsigned int keylen) 6088c2ecf20Sopenharmony_ci{ 6098c2ecf20Sopenharmony_ci struct qat_alg_aead_ctx *ctx = crypto_aead_ctx(tfm); 6108c2ecf20Sopenharmony_ci 6118c2ecf20Sopenharmony_ci if (ctx->enc_cd) 6128c2ecf20Sopenharmony_ci return qat_alg_aead_rekey(tfm, key, keylen); 6138c2ecf20Sopenharmony_ci else 6148c2ecf20Sopenharmony_ci return qat_alg_aead_newkey(tfm, key, keylen); 6158c2ecf20Sopenharmony_ci} 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_cistatic void qat_alg_free_bufl(struct qat_crypto_instance *inst, 6188c2ecf20Sopenharmony_ci struct qat_crypto_request *qat_req) 6198c2ecf20Sopenharmony_ci{ 6208c2ecf20Sopenharmony_ci struct device *dev = &GET_DEV(inst->accel_dev); 6218c2ecf20Sopenharmony_ci struct qat_alg_buf_list *bl = qat_req->buf.bl; 6228c2ecf20Sopenharmony_ci struct qat_alg_buf_list *blout = qat_req->buf.blout; 6238c2ecf20Sopenharmony_ci dma_addr_t blp = qat_req->buf.blp; 6248c2ecf20Sopenharmony_ci dma_addr_t blpout = qat_req->buf.bloutp; 6258c2ecf20Sopenharmony_ci size_t sz = qat_req->buf.sz; 6268c2ecf20Sopenharmony_ci size_t sz_out = qat_req->buf.sz_out; 6278c2ecf20Sopenharmony_ci int bl_dma_dir; 6288c2ecf20Sopenharmony_ci int i; 6298c2ecf20Sopenharmony_ci 6308c2ecf20Sopenharmony_ci bl_dma_dir = blp != blpout ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL; 6318c2ecf20Sopenharmony_ci 6328c2ecf20Sopenharmony_ci for (i = 0; i < bl->num_bufs; i++) 6338c2ecf20Sopenharmony_ci dma_unmap_single(dev, bl->bufers[i].addr, 6348c2ecf20Sopenharmony_ci bl->bufers[i].len, bl_dma_dir); 6358c2ecf20Sopenharmony_ci 6368c2ecf20Sopenharmony_ci dma_unmap_single(dev, blp, sz, DMA_TO_DEVICE); 6378c2ecf20Sopenharmony_ci 6388c2ecf20Sopenharmony_ci if (!qat_req->buf.sgl_src_valid) 6398c2ecf20Sopenharmony_ci kfree(bl); 6408c2ecf20Sopenharmony_ci 6418c2ecf20Sopenharmony_ci if (blp != blpout) { 6428c2ecf20Sopenharmony_ci /* If out of place operation dma unmap only data */ 6438c2ecf20Sopenharmony_ci int bufless = blout->num_bufs - blout->num_mapped_bufs; 6448c2ecf20Sopenharmony_ci 6458c2ecf20Sopenharmony_ci for (i = bufless; i < blout->num_bufs; i++) { 6468c2ecf20Sopenharmony_ci dma_unmap_single(dev, blout->bufers[i].addr, 6478c2ecf20Sopenharmony_ci blout->bufers[i].len, 6488c2ecf20Sopenharmony_ci DMA_FROM_DEVICE); 6498c2ecf20Sopenharmony_ci } 6508c2ecf20Sopenharmony_ci dma_unmap_single(dev, blpout, sz_out, DMA_TO_DEVICE); 6518c2ecf20Sopenharmony_ci 6528c2ecf20Sopenharmony_ci if (!qat_req->buf.sgl_dst_valid) 6538c2ecf20Sopenharmony_ci kfree(blout); 6548c2ecf20Sopenharmony_ci } 6558c2ecf20Sopenharmony_ci} 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_cistatic int qat_alg_sgl_to_bufl(struct qat_crypto_instance *inst, 6588c2ecf20Sopenharmony_ci struct scatterlist *sgl, 6598c2ecf20Sopenharmony_ci struct scatterlist *sglout, 6608c2ecf20Sopenharmony_ci struct qat_crypto_request *qat_req) 6618c2ecf20Sopenharmony_ci{ 6628c2ecf20Sopenharmony_ci struct device *dev = &GET_DEV(inst->accel_dev); 6638c2ecf20Sopenharmony_ci int i, sg_nctr = 0; 6648c2ecf20Sopenharmony_ci int n = sg_nents(sgl); 6658c2ecf20Sopenharmony_ci struct qat_alg_buf_list *bufl; 6668c2ecf20Sopenharmony_ci struct qat_alg_buf_list *buflout = NULL; 6678c2ecf20Sopenharmony_ci dma_addr_t blp = DMA_MAPPING_ERROR; 6688c2ecf20Sopenharmony_ci dma_addr_t bloutp = DMA_MAPPING_ERROR; 6698c2ecf20Sopenharmony_ci struct scatterlist *sg; 6708c2ecf20Sopenharmony_ci size_t sz_out, sz = struct_size(bufl, bufers, n); 6718c2ecf20Sopenharmony_ci int node = dev_to_node(&GET_DEV(inst->accel_dev)); 6728c2ecf20Sopenharmony_ci int bufl_dma_dir; 6738c2ecf20Sopenharmony_ci 6748c2ecf20Sopenharmony_ci if (unlikely(!n)) 6758c2ecf20Sopenharmony_ci return -EINVAL; 6768c2ecf20Sopenharmony_ci 6778c2ecf20Sopenharmony_ci qat_req->buf.sgl_src_valid = false; 6788c2ecf20Sopenharmony_ci qat_req->buf.sgl_dst_valid = false; 6798c2ecf20Sopenharmony_ci 6808c2ecf20Sopenharmony_ci if (n > QAT_MAX_BUFF_DESC) { 6818c2ecf20Sopenharmony_ci bufl = kzalloc_node(sz, GFP_ATOMIC, node); 6828c2ecf20Sopenharmony_ci if (unlikely(!bufl)) 6838c2ecf20Sopenharmony_ci return -ENOMEM; 6848c2ecf20Sopenharmony_ci } else { 6858c2ecf20Sopenharmony_ci bufl = &qat_req->buf.sgl_src.sgl_hdr; 6868c2ecf20Sopenharmony_ci memset(bufl, 0, sizeof(struct qat_alg_buf_list)); 6878c2ecf20Sopenharmony_ci qat_req->buf.sgl_src_valid = true; 6888c2ecf20Sopenharmony_ci } 6898c2ecf20Sopenharmony_ci 6908c2ecf20Sopenharmony_ci bufl_dma_dir = sgl != sglout ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL; 6918c2ecf20Sopenharmony_ci 6928c2ecf20Sopenharmony_ci for_each_sg(sgl, sg, n, i) 6938c2ecf20Sopenharmony_ci bufl->bufers[i].addr = DMA_MAPPING_ERROR; 6948c2ecf20Sopenharmony_ci 6958c2ecf20Sopenharmony_ci for_each_sg(sgl, sg, n, i) { 6968c2ecf20Sopenharmony_ci int y = sg_nctr; 6978c2ecf20Sopenharmony_ci 6988c2ecf20Sopenharmony_ci if (!sg->length) 6998c2ecf20Sopenharmony_ci continue; 7008c2ecf20Sopenharmony_ci 7018c2ecf20Sopenharmony_ci bufl->bufers[y].addr = dma_map_single(dev, sg_virt(sg), 7028c2ecf20Sopenharmony_ci sg->length, 7038c2ecf20Sopenharmony_ci bufl_dma_dir); 7048c2ecf20Sopenharmony_ci bufl->bufers[y].len = sg->length; 7058c2ecf20Sopenharmony_ci if (unlikely(dma_mapping_error(dev, bufl->bufers[y].addr))) 7068c2ecf20Sopenharmony_ci goto err_in; 7078c2ecf20Sopenharmony_ci sg_nctr++; 7088c2ecf20Sopenharmony_ci } 7098c2ecf20Sopenharmony_ci bufl->num_bufs = sg_nctr; 7108c2ecf20Sopenharmony_ci blp = dma_map_single(dev, bufl, sz, DMA_TO_DEVICE); 7118c2ecf20Sopenharmony_ci if (unlikely(dma_mapping_error(dev, blp))) 7128c2ecf20Sopenharmony_ci goto err_in; 7138c2ecf20Sopenharmony_ci qat_req->buf.bl = bufl; 7148c2ecf20Sopenharmony_ci qat_req->buf.blp = blp; 7158c2ecf20Sopenharmony_ci qat_req->buf.sz = sz; 7168c2ecf20Sopenharmony_ci /* Handle out of place operation */ 7178c2ecf20Sopenharmony_ci if (sgl != sglout) { 7188c2ecf20Sopenharmony_ci struct qat_alg_buf *bufers; 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ci n = sg_nents(sglout); 7218c2ecf20Sopenharmony_ci sz_out = struct_size(buflout, bufers, n); 7228c2ecf20Sopenharmony_ci sg_nctr = 0; 7238c2ecf20Sopenharmony_ci 7248c2ecf20Sopenharmony_ci if (n > QAT_MAX_BUFF_DESC) { 7258c2ecf20Sopenharmony_ci buflout = kzalloc_node(sz_out, GFP_ATOMIC, node); 7268c2ecf20Sopenharmony_ci if (unlikely(!buflout)) 7278c2ecf20Sopenharmony_ci goto err_in; 7288c2ecf20Sopenharmony_ci } else { 7298c2ecf20Sopenharmony_ci buflout = &qat_req->buf.sgl_dst.sgl_hdr; 7308c2ecf20Sopenharmony_ci memset(buflout, 0, sizeof(struct qat_alg_buf_list)); 7318c2ecf20Sopenharmony_ci qat_req->buf.sgl_dst_valid = true; 7328c2ecf20Sopenharmony_ci } 7338c2ecf20Sopenharmony_ci 7348c2ecf20Sopenharmony_ci bufers = buflout->bufers; 7358c2ecf20Sopenharmony_ci for_each_sg(sglout, sg, n, i) 7368c2ecf20Sopenharmony_ci bufers[i].addr = DMA_MAPPING_ERROR; 7378c2ecf20Sopenharmony_ci 7388c2ecf20Sopenharmony_ci for_each_sg(sglout, sg, n, i) { 7398c2ecf20Sopenharmony_ci int y = sg_nctr; 7408c2ecf20Sopenharmony_ci 7418c2ecf20Sopenharmony_ci if (!sg->length) 7428c2ecf20Sopenharmony_ci continue; 7438c2ecf20Sopenharmony_ci 7448c2ecf20Sopenharmony_ci bufers[y].addr = dma_map_single(dev, sg_virt(sg), 7458c2ecf20Sopenharmony_ci sg->length, 7468c2ecf20Sopenharmony_ci DMA_FROM_DEVICE); 7478c2ecf20Sopenharmony_ci if (unlikely(dma_mapping_error(dev, bufers[y].addr))) 7488c2ecf20Sopenharmony_ci goto err_out; 7498c2ecf20Sopenharmony_ci bufers[y].len = sg->length; 7508c2ecf20Sopenharmony_ci sg_nctr++; 7518c2ecf20Sopenharmony_ci } 7528c2ecf20Sopenharmony_ci buflout->num_bufs = sg_nctr; 7538c2ecf20Sopenharmony_ci buflout->num_mapped_bufs = sg_nctr; 7548c2ecf20Sopenharmony_ci bloutp = dma_map_single(dev, buflout, sz_out, DMA_TO_DEVICE); 7558c2ecf20Sopenharmony_ci if (unlikely(dma_mapping_error(dev, bloutp))) 7568c2ecf20Sopenharmony_ci goto err_out; 7578c2ecf20Sopenharmony_ci qat_req->buf.blout = buflout; 7588c2ecf20Sopenharmony_ci qat_req->buf.bloutp = bloutp; 7598c2ecf20Sopenharmony_ci qat_req->buf.sz_out = sz_out; 7608c2ecf20Sopenharmony_ci } else { 7618c2ecf20Sopenharmony_ci /* Otherwise set the src and dst to the same address */ 7628c2ecf20Sopenharmony_ci qat_req->buf.bloutp = qat_req->buf.blp; 7638c2ecf20Sopenharmony_ci qat_req->buf.sz_out = 0; 7648c2ecf20Sopenharmony_ci } 7658c2ecf20Sopenharmony_ci return 0; 7668c2ecf20Sopenharmony_ci 7678c2ecf20Sopenharmony_cierr_out: 7688c2ecf20Sopenharmony_ci if (!dma_mapping_error(dev, bloutp)) 7698c2ecf20Sopenharmony_ci dma_unmap_single(dev, bloutp, sz_out, DMA_TO_DEVICE); 7708c2ecf20Sopenharmony_ci 7718c2ecf20Sopenharmony_ci n = sg_nents(sglout); 7728c2ecf20Sopenharmony_ci for (i = 0; i < n; i++) 7738c2ecf20Sopenharmony_ci if (!dma_mapping_error(dev, buflout->bufers[i].addr)) 7748c2ecf20Sopenharmony_ci dma_unmap_single(dev, buflout->bufers[i].addr, 7758c2ecf20Sopenharmony_ci buflout->bufers[i].len, 7768c2ecf20Sopenharmony_ci DMA_FROM_DEVICE); 7778c2ecf20Sopenharmony_ci 7788c2ecf20Sopenharmony_ci if (!qat_req->buf.sgl_dst_valid) 7798c2ecf20Sopenharmony_ci kfree(buflout); 7808c2ecf20Sopenharmony_ci 7818c2ecf20Sopenharmony_cierr_in: 7828c2ecf20Sopenharmony_ci if (!dma_mapping_error(dev, blp)) 7838c2ecf20Sopenharmony_ci dma_unmap_single(dev, blp, sz, DMA_TO_DEVICE); 7848c2ecf20Sopenharmony_ci 7858c2ecf20Sopenharmony_ci n = sg_nents(sgl); 7868c2ecf20Sopenharmony_ci for (i = 0; i < n; i++) 7878c2ecf20Sopenharmony_ci if (!dma_mapping_error(dev, bufl->bufers[i].addr)) 7888c2ecf20Sopenharmony_ci dma_unmap_single(dev, bufl->bufers[i].addr, 7898c2ecf20Sopenharmony_ci bufl->bufers[i].len, 7908c2ecf20Sopenharmony_ci bufl_dma_dir); 7918c2ecf20Sopenharmony_ci 7928c2ecf20Sopenharmony_ci if (!qat_req->buf.sgl_src_valid) 7938c2ecf20Sopenharmony_ci kfree(bufl); 7948c2ecf20Sopenharmony_ci 7958c2ecf20Sopenharmony_ci dev_err(dev, "Failed to map buf for dma\n"); 7968c2ecf20Sopenharmony_ci return -ENOMEM; 7978c2ecf20Sopenharmony_ci} 7988c2ecf20Sopenharmony_ci 7998c2ecf20Sopenharmony_cistatic void qat_aead_alg_callback(struct icp_qat_fw_la_resp *qat_resp, 8008c2ecf20Sopenharmony_ci struct qat_crypto_request *qat_req) 8018c2ecf20Sopenharmony_ci{ 8028c2ecf20Sopenharmony_ci struct qat_alg_aead_ctx *ctx = qat_req->aead_ctx; 8038c2ecf20Sopenharmony_ci struct qat_crypto_instance *inst = ctx->inst; 8048c2ecf20Sopenharmony_ci struct aead_request *areq = qat_req->aead_req; 8058c2ecf20Sopenharmony_ci u8 stat_filed = qat_resp->comn_resp.comn_status; 8068c2ecf20Sopenharmony_ci int res = 0, qat_res = ICP_QAT_FW_COMN_RESP_CRYPTO_STAT_GET(stat_filed); 8078c2ecf20Sopenharmony_ci 8088c2ecf20Sopenharmony_ci qat_alg_free_bufl(inst, qat_req); 8098c2ecf20Sopenharmony_ci if (unlikely(qat_res != ICP_QAT_FW_COMN_STATUS_FLAG_OK)) 8108c2ecf20Sopenharmony_ci res = -EBADMSG; 8118c2ecf20Sopenharmony_ci areq->base.complete(&areq->base, res); 8128c2ecf20Sopenharmony_ci} 8138c2ecf20Sopenharmony_ci 8148c2ecf20Sopenharmony_cistatic void qat_skcipher_alg_callback(struct icp_qat_fw_la_resp *qat_resp, 8158c2ecf20Sopenharmony_ci struct qat_crypto_request *qat_req) 8168c2ecf20Sopenharmony_ci{ 8178c2ecf20Sopenharmony_ci struct qat_alg_skcipher_ctx *ctx = qat_req->skcipher_ctx; 8188c2ecf20Sopenharmony_ci struct qat_crypto_instance *inst = ctx->inst; 8198c2ecf20Sopenharmony_ci struct skcipher_request *sreq = qat_req->skcipher_req; 8208c2ecf20Sopenharmony_ci u8 stat_filed = qat_resp->comn_resp.comn_status; 8218c2ecf20Sopenharmony_ci struct device *dev = &GET_DEV(ctx->inst->accel_dev); 8228c2ecf20Sopenharmony_ci int res = 0, qat_res = ICP_QAT_FW_COMN_RESP_CRYPTO_STAT_GET(stat_filed); 8238c2ecf20Sopenharmony_ci 8248c2ecf20Sopenharmony_ci qat_alg_free_bufl(inst, qat_req); 8258c2ecf20Sopenharmony_ci if (unlikely(qat_res != ICP_QAT_FW_COMN_STATUS_FLAG_OK)) 8268c2ecf20Sopenharmony_ci res = -EINVAL; 8278c2ecf20Sopenharmony_ci 8288c2ecf20Sopenharmony_ci memcpy(sreq->iv, qat_req->iv, AES_BLOCK_SIZE); 8298c2ecf20Sopenharmony_ci dma_free_coherent(dev, AES_BLOCK_SIZE, qat_req->iv, 8308c2ecf20Sopenharmony_ci qat_req->iv_paddr); 8318c2ecf20Sopenharmony_ci 8328c2ecf20Sopenharmony_ci sreq->base.complete(&sreq->base, res); 8338c2ecf20Sopenharmony_ci} 8348c2ecf20Sopenharmony_ci 8358c2ecf20Sopenharmony_civoid qat_alg_callback(void *resp) 8368c2ecf20Sopenharmony_ci{ 8378c2ecf20Sopenharmony_ci struct icp_qat_fw_la_resp *qat_resp = resp; 8388c2ecf20Sopenharmony_ci struct qat_crypto_request *qat_req = 8398c2ecf20Sopenharmony_ci (void *)(__force long)qat_resp->opaque_data; 8408c2ecf20Sopenharmony_ci 8418c2ecf20Sopenharmony_ci qat_req->cb(qat_resp, qat_req); 8428c2ecf20Sopenharmony_ci} 8438c2ecf20Sopenharmony_ci 8448c2ecf20Sopenharmony_cistatic int qat_alg_aead_dec(struct aead_request *areq) 8458c2ecf20Sopenharmony_ci{ 8468c2ecf20Sopenharmony_ci struct crypto_aead *aead_tfm = crypto_aead_reqtfm(areq); 8478c2ecf20Sopenharmony_ci struct crypto_tfm *tfm = crypto_aead_tfm(aead_tfm); 8488c2ecf20Sopenharmony_ci struct qat_alg_aead_ctx *ctx = crypto_tfm_ctx(tfm); 8498c2ecf20Sopenharmony_ci struct qat_crypto_request *qat_req = aead_request_ctx(areq); 8508c2ecf20Sopenharmony_ci struct icp_qat_fw_la_cipher_req_params *cipher_param; 8518c2ecf20Sopenharmony_ci struct icp_qat_fw_la_auth_req_params *auth_param; 8528c2ecf20Sopenharmony_ci struct icp_qat_fw_la_bulk_req *msg; 8538c2ecf20Sopenharmony_ci int digst_size = crypto_aead_authsize(aead_tfm); 8548c2ecf20Sopenharmony_ci int ret, ctr = 0; 8558c2ecf20Sopenharmony_ci u32 cipher_len; 8568c2ecf20Sopenharmony_ci 8578c2ecf20Sopenharmony_ci cipher_len = areq->cryptlen - digst_size; 8588c2ecf20Sopenharmony_ci if (cipher_len % AES_BLOCK_SIZE != 0) 8598c2ecf20Sopenharmony_ci return -EINVAL; 8608c2ecf20Sopenharmony_ci 8618c2ecf20Sopenharmony_ci ret = qat_alg_sgl_to_bufl(ctx->inst, areq->src, areq->dst, qat_req); 8628c2ecf20Sopenharmony_ci if (unlikely(ret)) 8638c2ecf20Sopenharmony_ci return ret; 8648c2ecf20Sopenharmony_ci 8658c2ecf20Sopenharmony_ci msg = &qat_req->req; 8668c2ecf20Sopenharmony_ci *msg = ctx->dec_fw_req; 8678c2ecf20Sopenharmony_ci qat_req->aead_ctx = ctx; 8688c2ecf20Sopenharmony_ci qat_req->aead_req = areq; 8698c2ecf20Sopenharmony_ci qat_req->cb = qat_aead_alg_callback; 8708c2ecf20Sopenharmony_ci qat_req->req.comn_mid.opaque_data = (u64)(__force long)qat_req; 8718c2ecf20Sopenharmony_ci qat_req->req.comn_mid.src_data_addr = qat_req->buf.blp; 8728c2ecf20Sopenharmony_ci qat_req->req.comn_mid.dest_data_addr = qat_req->buf.bloutp; 8738c2ecf20Sopenharmony_ci cipher_param = (void *)&qat_req->req.serv_specif_rqpars; 8748c2ecf20Sopenharmony_ci cipher_param->cipher_length = cipher_len; 8758c2ecf20Sopenharmony_ci cipher_param->cipher_offset = areq->assoclen; 8768c2ecf20Sopenharmony_ci memcpy(cipher_param->u.cipher_IV_array, areq->iv, AES_BLOCK_SIZE); 8778c2ecf20Sopenharmony_ci auth_param = (void *)((u8 *)cipher_param + sizeof(*cipher_param)); 8788c2ecf20Sopenharmony_ci auth_param->auth_off = 0; 8798c2ecf20Sopenharmony_ci auth_param->auth_len = areq->assoclen + cipher_param->cipher_length; 8808c2ecf20Sopenharmony_ci do { 8818c2ecf20Sopenharmony_ci ret = adf_send_message(ctx->inst->sym_tx, (u32 *)msg); 8828c2ecf20Sopenharmony_ci } while (ret == -EAGAIN && ctr++ < 10); 8838c2ecf20Sopenharmony_ci 8848c2ecf20Sopenharmony_ci if (ret == -EAGAIN) { 8858c2ecf20Sopenharmony_ci qat_alg_free_bufl(ctx->inst, qat_req); 8868c2ecf20Sopenharmony_ci return -EBUSY; 8878c2ecf20Sopenharmony_ci } 8888c2ecf20Sopenharmony_ci return -EINPROGRESS; 8898c2ecf20Sopenharmony_ci} 8908c2ecf20Sopenharmony_ci 8918c2ecf20Sopenharmony_cistatic int qat_alg_aead_enc(struct aead_request *areq) 8928c2ecf20Sopenharmony_ci{ 8938c2ecf20Sopenharmony_ci struct crypto_aead *aead_tfm = crypto_aead_reqtfm(areq); 8948c2ecf20Sopenharmony_ci struct crypto_tfm *tfm = crypto_aead_tfm(aead_tfm); 8958c2ecf20Sopenharmony_ci struct qat_alg_aead_ctx *ctx = crypto_tfm_ctx(tfm); 8968c2ecf20Sopenharmony_ci struct qat_crypto_request *qat_req = aead_request_ctx(areq); 8978c2ecf20Sopenharmony_ci struct icp_qat_fw_la_cipher_req_params *cipher_param; 8988c2ecf20Sopenharmony_ci struct icp_qat_fw_la_auth_req_params *auth_param; 8998c2ecf20Sopenharmony_ci struct icp_qat_fw_la_bulk_req *msg; 9008c2ecf20Sopenharmony_ci u8 *iv = areq->iv; 9018c2ecf20Sopenharmony_ci int ret, ctr = 0; 9028c2ecf20Sopenharmony_ci 9038c2ecf20Sopenharmony_ci if (areq->cryptlen % AES_BLOCK_SIZE != 0) 9048c2ecf20Sopenharmony_ci return -EINVAL; 9058c2ecf20Sopenharmony_ci 9068c2ecf20Sopenharmony_ci ret = qat_alg_sgl_to_bufl(ctx->inst, areq->src, areq->dst, qat_req); 9078c2ecf20Sopenharmony_ci if (unlikely(ret)) 9088c2ecf20Sopenharmony_ci return ret; 9098c2ecf20Sopenharmony_ci 9108c2ecf20Sopenharmony_ci msg = &qat_req->req; 9118c2ecf20Sopenharmony_ci *msg = ctx->enc_fw_req; 9128c2ecf20Sopenharmony_ci qat_req->aead_ctx = ctx; 9138c2ecf20Sopenharmony_ci qat_req->aead_req = areq; 9148c2ecf20Sopenharmony_ci qat_req->cb = qat_aead_alg_callback; 9158c2ecf20Sopenharmony_ci qat_req->req.comn_mid.opaque_data = (u64)(__force long)qat_req; 9168c2ecf20Sopenharmony_ci qat_req->req.comn_mid.src_data_addr = qat_req->buf.blp; 9178c2ecf20Sopenharmony_ci qat_req->req.comn_mid.dest_data_addr = qat_req->buf.bloutp; 9188c2ecf20Sopenharmony_ci cipher_param = (void *)&qat_req->req.serv_specif_rqpars; 9198c2ecf20Sopenharmony_ci auth_param = (void *)((u8 *)cipher_param + sizeof(*cipher_param)); 9208c2ecf20Sopenharmony_ci 9218c2ecf20Sopenharmony_ci memcpy(cipher_param->u.cipher_IV_array, iv, AES_BLOCK_SIZE); 9228c2ecf20Sopenharmony_ci cipher_param->cipher_length = areq->cryptlen; 9238c2ecf20Sopenharmony_ci cipher_param->cipher_offset = areq->assoclen; 9248c2ecf20Sopenharmony_ci 9258c2ecf20Sopenharmony_ci auth_param->auth_off = 0; 9268c2ecf20Sopenharmony_ci auth_param->auth_len = areq->assoclen + areq->cryptlen; 9278c2ecf20Sopenharmony_ci 9288c2ecf20Sopenharmony_ci do { 9298c2ecf20Sopenharmony_ci ret = adf_send_message(ctx->inst->sym_tx, (u32 *)msg); 9308c2ecf20Sopenharmony_ci } while (ret == -EAGAIN && ctr++ < 10); 9318c2ecf20Sopenharmony_ci 9328c2ecf20Sopenharmony_ci if (ret == -EAGAIN) { 9338c2ecf20Sopenharmony_ci qat_alg_free_bufl(ctx->inst, qat_req); 9348c2ecf20Sopenharmony_ci return -EBUSY; 9358c2ecf20Sopenharmony_ci } 9368c2ecf20Sopenharmony_ci return -EINPROGRESS; 9378c2ecf20Sopenharmony_ci} 9388c2ecf20Sopenharmony_ci 9398c2ecf20Sopenharmony_cistatic int qat_alg_skcipher_rekey(struct qat_alg_skcipher_ctx *ctx, 9408c2ecf20Sopenharmony_ci const u8 *key, unsigned int keylen, 9418c2ecf20Sopenharmony_ci int mode) 9428c2ecf20Sopenharmony_ci{ 9438c2ecf20Sopenharmony_ci memset(ctx->enc_cd, 0, sizeof(*ctx->enc_cd)); 9448c2ecf20Sopenharmony_ci memset(ctx->dec_cd, 0, sizeof(*ctx->dec_cd)); 9458c2ecf20Sopenharmony_ci memset(&ctx->enc_fw_req, 0, sizeof(ctx->enc_fw_req)); 9468c2ecf20Sopenharmony_ci memset(&ctx->dec_fw_req, 0, sizeof(ctx->dec_fw_req)); 9478c2ecf20Sopenharmony_ci 9488c2ecf20Sopenharmony_ci return qat_alg_skcipher_init_sessions(ctx, key, keylen, mode); 9498c2ecf20Sopenharmony_ci} 9508c2ecf20Sopenharmony_ci 9518c2ecf20Sopenharmony_cistatic int qat_alg_skcipher_newkey(struct qat_alg_skcipher_ctx *ctx, 9528c2ecf20Sopenharmony_ci const u8 *key, unsigned int keylen, 9538c2ecf20Sopenharmony_ci int mode) 9548c2ecf20Sopenharmony_ci{ 9558c2ecf20Sopenharmony_ci struct qat_crypto_instance *inst = NULL; 9568c2ecf20Sopenharmony_ci struct device *dev; 9578c2ecf20Sopenharmony_ci int node = get_current_node(); 9588c2ecf20Sopenharmony_ci int ret; 9598c2ecf20Sopenharmony_ci 9608c2ecf20Sopenharmony_ci inst = qat_crypto_get_instance_node(node); 9618c2ecf20Sopenharmony_ci if (!inst) 9628c2ecf20Sopenharmony_ci return -EINVAL; 9638c2ecf20Sopenharmony_ci dev = &GET_DEV(inst->accel_dev); 9648c2ecf20Sopenharmony_ci ctx->inst = inst; 9658c2ecf20Sopenharmony_ci ctx->enc_cd = dma_alloc_coherent(dev, sizeof(*ctx->enc_cd), 9668c2ecf20Sopenharmony_ci &ctx->enc_cd_paddr, 9678c2ecf20Sopenharmony_ci GFP_ATOMIC); 9688c2ecf20Sopenharmony_ci if (!ctx->enc_cd) { 9698c2ecf20Sopenharmony_ci ret = -ENOMEM; 9708c2ecf20Sopenharmony_ci goto out_free_instance; 9718c2ecf20Sopenharmony_ci } 9728c2ecf20Sopenharmony_ci ctx->dec_cd = dma_alloc_coherent(dev, sizeof(*ctx->dec_cd), 9738c2ecf20Sopenharmony_ci &ctx->dec_cd_paddr, 9748c2ecf20Sopenharmony_ci GFP_ATOMIC); 9758c2ecf20Sopenharmony_ci if (!ctx->dec_cd) { 9768c2ecf20Sopenharmony_ci ret = -ENOMEM; 9778c2ecf20Sopenharmony_ci goto out_free_enc; 9788c2ecf20Sopenharmony_ci } 9798c2ecf20Sopenharmony_ci 9808c2ecf20Sopenharmony_ci ret = qat_alg_skcipher_init_sessions(ctx, key, keylen, mode); 9818c2ecf20Sopenharmony_ci if (ret) 9828c2ecf20Sopenharmony_ci goto out_free_all; 9838c2ecf20Sopenharmony_ci 9848c2ecf20Sopenharmony_ci return 0; 9858c2ecf20Sopenharmony_ci 9868c2ecf20Sopenharmony_ciout_free_all: 9878c2ecf20Sopenharmony_ci memset(ctx->dec_cd, 0, sizeof(*ctx->dec_cd)); 9888c2ecf20Sopenharmony_ci dma_free_coherent(dev, sizeof(*ctx->dec_cd), 9898c2ecf20Sopenharmony_ci ctx->dec_cd, ctx->dec_cd_paddr); 9908c2ecf20Sopenharmony_ci ctx->dec_cd = NULL; 9918c2ecf20Sopenharmony_ciout_free_enc: 9928c2ecf20Sopenharmony_ci memset(ctx->enc_cd, 0, sizeof(*ctx->enc_cd)); 9938c2ecf20Sopenharmony_ci dma_free_coherent(dev, sizeof(*ctx->enc_cd), 9948c2ecf20Sopenharmony_ci ctx->enc_cd, ctx->enc_cd_paddr); 9958c2ecf20Sopenharmony_ci ctx->enc_cd = NULL; 9968c2ecf20Sopenharmony_ciout_free_instance: 9978c2ecf20Sopenharmony_ci ctx->inst = NULL; 9988c2ecf20Sopenharmony_ci qat_crypto_put_instance(inst); 9998c2ecf20Sopenharmony_ci return ret; 10008c2ecf20Sopenharmony_ci} 10018c2ecf20Sopenharmony_ci 10028c2ecf20Sopenharmony_cistatic int qat_alg_skcipher_setkey(struct crypto_skcipher *tfm, 10038c2ecf20Sopenharmony_ci const u8 *key, unsigned int keylen, 10048c2ecf20Sopenharmony_ci int mode) 10058c2ecf20Sopenharmony_ci{ 10068c2ecf20Sopenharmony_ci struct qat_alg_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 10078c2ecf20Sopenharmony_ci 10088c2ecf20Sopenharmony_ci if (ctx->enc_cd) 10098c2ecf20Sopenharmony_ci return qat_alg_skcipher_rekey(ctx, key, keylen, mode); 10108c2ecf20Sopenharmony_ci else 10118c2ecf20Sopenharmony_ci return qat_alg_skcipher_newkey(ctx, key, keylen, mode); 10128c2ecf20Sopenharmony_ci} 10138c2ecf20Sopenharmony_ci 10148c2ecf20Sopenharmony_cistatic int qat_alg_skcipher_cbc_setkey(struct crypto_skcipher *tfm, 10158c2ecf20Sopenharmony_ci const u8 *key, unsigned int keylen) 10168c2ecf20Sopenharmony_ci{ 10178c2ecf20Sopenharmony_ci return qat_alg_skcipher_setkey(tfm, key, keylen, 10188c2ecf20Sopenharmony_ci ICP_QAT_HW_CIPHER_CBC_MODE); 10198c2ecf20Sopenharmony_ci} 10208c2ecf20Sopenharmony_ci 10218c2ecf20Sopenharmony_cistatic int qat_alg_skcipher_ctr_setkey(struct crypto_skcipher *tfm, 10228c2ecf20Sopenharmony_ci const u8 *key, unsigned int keylen) 10238c2ecf20Sopenharmony_ci{ 10248c2ecf20Sopenharmony_ci return qat_alg_skcipher_setkey(tfm, key, keylen, 10258c2ecf20Sopenharmony_ci ICP_QAT_HW_CIPHER_CTR_MODE); 10268c2ecf20Sopenharmony_ci} 10278c2ecf20Sopenharmony_ci 10288c2ecf20Sopenharmony_cistatic int qat_alg_skcipher_xts_setkey(struct crypto_skcipher *tfm, 10298c2ecf20Sopenharmony_ci const u8 *key, unsigned int keylen) 10308c2ecf20Sopenharmony_ci{ 10318c2ecf20Sopenharmony_ci struct qat_alg_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 10328c2ecf20Sopenharmony_ci int ret; 10338c2ecf20Sopenharmony_ci 10348c2ecf20Sopenharmony_ci ret = xts_verify_key(tfm, key, keylen); 10358c2ecf20Sopenharmony_ci if (ret) 10368c2ecf20Sopenharmony_ci return ret; 10378c2ecf20Sopenharmony_ci 10388c2ecf20Sopenharmony_ci if (keylen >> 1 == AES_KEYSIZE_192) { 10398c2ecf20Sopenharmony_ci ret = crypto_skcipher_setkey(ctx->ftfm, key, keylen); 10408c2ecf20Sopenharmony_ci if (ret) 10418c2ecf20Sopenharmony_ci return ret; 10428c2ecf20Sopenharmony_ci 10438c2ecf20Sopenharmony_ci ctx->fallback = true; 10448c2ecf20Sopenharmony_ci 10458c2ecf20Sopenharmony_ci return 0; 10468c2ecf20Sopenharmony_ci } 10478c2ecf20Sopenharmony_ci 10488c2ecf20Sopenharmony_ci ctx->fallback = false; 10498c2ecf20Sopenharmony_ci 10508c2ecf20Sopenharmony_ci return qat_alg_skcipher_setkey(tfm, key, keylen, 10518c2ecf20Sopenharmony_ci ICP_QAT_HW_CIPHER_XTS_MODE); 10528c2ecf20Sopenharmony_ci} 10538c2ecf20Sopenharmony_ci 10548c2ecf20Sopenharmony_cistatic int qat_alg_skcipher_encrypt(struct skcipher_request *req) 10558c2ecf20Sopenharmony_ci{ 10568c2ecf20Sopenharmony_ci struct crypto_skcipher *stfm = crypto_skcipher_reqtfm(req); 10578c2ecf20Sopenharmony_ci struct crypto_tfm *tfm = crypto_skcipher_tfm(stfm); 10588c2ecf20Sopenharmony_ci struct qat_alg_skcipher_ctx *ctx = crypto_tfm_ctx(tfm); 10598c2ecf20Sopenharmony_ci struct qat_crypto_request *qat_req = skcipher_request_ctx(req); 10608c2ecf20Sopenharmony_ci struct icp_qat_fw_la_cipher_req_params *cipher_param; 10618c2ecf20Sopenharmony_ci struct icp_qat_fw_la_bulk_req *msg; 10628c2ecf20Sopenharmony_ci struct device *dev = &GET_DEV(ctx->inst->accel_dev); 10638c2ecf20Sopenharmony_ci int ret, ctr = 0; 10648c2ecf20Sopenharmony_ci 10658c2ecf20Sopenharmony_ci if (req->cryptlen == 0) 10668c2ecf20Sopenharmony_ci return 0; 10678c2ecf20Sopenharmony_ci 10688c2ecf20Sopenharmony_ci qat_req->iv = dma_alloc_coherent(dev, AES_BLOCK_SIZE, 10698c2ecf20Sopenharmony_ci &qat_req->iv_paddr, GFP_ATOMIC); 10708c2ecf20Sopenharmony_ci if (!qat_req->iv) 10718c2ecf20Sopenharmony_ci return -ENOMEM; 10728c2ecf20Sopenharmony_ci 10738c2ecf20Sopenharmony_ci ret = qat_alg_sgl_to_bufl(ctx->inst, req->src, req->dst, qat_req); 10748c2ecf20Sopenharmony_ci if (unlikely(ret)) { 10758c2ecf20Sopenharmony_ci dma_free_coherent(dev, AES_BLOCK_SIZE, qat_req->iv, 10768c2ecf20Sopenharmony_ci qat_req->iv_paddr); 10778c2ecf20Sopenharmony_ci return ret; 10788c2ecf20Sopenharmony_ci } 10798c2ecf20Sopenharmony_ci 10808c2ecf20Sopenharmony_ci msg = &qat_req->req; 10818c2ecf20Sopenharmony_ci *msg = ctx->enc_fw_req; 10828c2ecf20Sopenharmony_ci qat_req->skcipher_ctx = ctx; 10838c2ecf20Sopenharmony_ci qat_req->skcipher_req = req; 10848c2ecf20Sopenharmony_ci qat_req->cb = qat_skcipher_alg_callback; 10858c2ecf20Sopenharmony_ci qat_req->req.comn_mid.opaque_data = (u64)(__force long)qat_req; 10868c2ecf20Sopenharmony_ci qat_req->req.comn_mid.src_data_addr = qat_req->buf.blp; 10878c2ecf20Sopenharmony_ci qat_req->req.comn_mid.dest_data_addr = qat_req->buf.bloutp; 10888c2ecf20Sopenharmony_ci cipher_param = (void *)&qat_req->req.serv_specif_rqpars; 10898c2ecf20Sopenharmony_ci cipher_param->cipher_length = req->cryptlen; 10908c2ecf20Sopenharmony_ci cipher_param->cipher_offset = 0; 10918c2ecf20Sopenharmony_ci cipher_param->u.s.cipher_IV_ptr = qat_req->iv_paddr; 10928c2ecf20Sopenharmony_ci memcpy(qat_req->iv, req->iv, AES_BLOCK_SIZE); 10938c2ecf20Sopenharmony_ci do { 10948c2ecf20Sopenharmony_ci ret = adf_send_message(ctx->inst->sym_tx, (u32 *)msg); 10958c2ecf20Sopenharmony_ci } while (ret == -EAGAIN && ctr++ < 10); 10968c2ecf20Sopenharmony_ci 10978c2ecf20Sopenharmony_ci if (ret == -EAGAIN) { 10988c2ecf20Sopenharmony_ci qat_alg_free_bufl(ctx->inst, qat_req); 10998c2ecf20Sopenharmony_ci dma_free_coherent(dev, AES_BLOCK_SIZE, qat_req->iv, 11008c2ecf20Sopenharmony_ci qat_req->iv_paddr); 11018c2ecf20Sopenharmony_ci return -EBUSY; 11028c2ecf20Sopenharmony_ci } 11038c2ecf20Sopenharmony_ci return -EINPROGRESS; 11048c2ecf20Sopenharmony_ci} 11058c2ecf20Sopenharmony_ci 11068c2ecf20Sopenharmony_cistatic int qat_alg_skcipher_blk_encrypt(struct skcipher_request *req) 11078c2ecf20Sopenharmony_ci{ 11088c2ecf20Sopenharmony_ci if (req->cryptlen % AES_BLOCK_SIZE != 0) 11098c2ecf20Sopenharmony_ci return -EINVAL; 11108c2ecf20Sopenharmony_ci 11118c2ecf20Sopenharmony_ci return qat_alg_skcipher_encrypt(req); 11128c2ecf20Sopenharmony_ci} 11138c2ecf20Sopenharmony_ci 11148c2ecf20Sopenharmony_cistatic int qat_alg_skcipher_xts_encrypt(struct skcipher_request *req) 11158c2ecf20Sopenharmony_ci{ 11168c2ecf20Sopenharmony_ci struct crypto_skcipher *stfm = crypto_skcipher_reqtfm(req); 11178c2ecf20Sopenharmony_ci struct qat_alg_skcipher_ctx *ctx = crypto_skcipher_ctx(stfm); 11188c2ecf20Sopenharmony_ci struct skcipher_request *nreq = skcipher_request_ctx(req); 11198c2ecf20Sopenharmony_ci 11208c2ecf20Sopenharmony_ci if (req->cryptlen < XTS_BLOCK_SIZE) 11218c2ecf20Sopenharmony_ci return -EINVAL; 11228c2ecf20Sopenharmony_ci 11238c2ecf20Sopenharmony_ci if (ctx->fallback) { 11248c2ecf20Sopenharmony_ci memcpy(nreq, req, sizeof(*req)); 11258c2ecf20Sopenharmony_ci skcipher_request_set_tfm(nreq, ctx->ftfm); 11268c2ecf20Sopenharmony_ci return crypto_skcipher_encrypt(nreq); 11278c2ecf20Sopenharmony_ci } 11288c2ecf20Sopenharmony_ci 11298c2ecf20Sopenharmony_ci return qat_alg_skcipher_encrypt(req); 11308c2ecf20Sopenharmony_ci} 11318c2ecf20Sopenharmony_ci 11328c2ecf20Sopenharmony_cistatic int qat_alg_skcipher_decrypt(struct skcipher_request *req) 11338c2ecf20Sopenharmony_ci{ 11348c2ecf20Sopenharmony_ci struct crypto_skcipher *stfm = crypto_skcipher_reqtfm(req); 11358c2ecf20Sopenharmony_ci struct crypto_tfm *tfm = crypto_skcipher_tfm(stfm); 11368c2ecf20Sopenharmony_ci struct qat_alg_skcipher_ctx *ctx = crypto_tfm_ctx(tfm); 11378c2ecf20Sopenharmony_ci struct qat_crypto_request *qat_req = skcipher_request_ctx(req); 11388c2ecf20Sopenharmony_ci struct icp_qat_fw_la_cipher_req_params *cipher_param; 11398c2ecf20Sopenharmony_ci struct icp_qat_fw_la_bulk_req *msg; 11408c2ecf20Sopenharmony_ci struct device *dev = &GET_DEV(ctx->inst->accel_dev); 11418c2ecf20Sopenharmony_ci int ret, ctr = 0; 11428c2ecf20Sopenharmony_ci 11438c2ecf20Sopenharmony_ci if (req->cryptlen == 0) 11448c2ecf20Sopenharmony_ci return 0; 11458c2ecf20Sopenharmony_ci 11468c2ecf20Sopenharmony_ci qat_req->iv = dma_alloc_coherent(dev, AES_BLOCK_SIZE, 11478c2ecf20Sopenharmony_ci &qat_req->iv_paddr, GFP_ATOMIC); 11488c2ecf20Sopenharmony_ci if (!qat_req->iv) 11498c2ecf20Sopenharmony_ci return -ENOMEM; 11508c2ecf20Sopenharmony_ci 11518c2ecf20Sopenharmony_ci ret = qat_alg_sgl_to_bufl(ctx->inst, req->src, req->dst, qat_req); 11528c2ecf20Sopenharmony_ci if (unlikely(ret)) { 11538c2ecf20Sopenharmony_ci dma_free_coherent(dev, AES_BLOCK_SIZE, qat_req->iv, 11548c2ecf20Sopenharmony_ci qat_req->iv_paddr); 11558c2ecf20Sopenharmony_ci return ret; 11568c2ecf20Sopenharmony_ci } 11578c2ecf20Sopenharmony_ci 11588c2ecf20Sopenharmony_ci msg = &qat_req->req; 11598c2ecf20Sopenharmony_ci *msg = ctx->dec_fw_req; 11608c2ecf20Sopenharmony_ci qat_req->skcipher_ctx = ctx; 11618c2ecf20Sopenharmony_ci qat_req->skcipher_req = req; 11628c2ecf20Sopenharmony_ci qat_req->cb = qat_skcipher_alg_callback; 11638c2ecf20Sopenharmony_ci qat_req->req.comn_mid.opaque_data = (u64)(__force long)qat_req; 11648c2ecf20Sopenharmony_ci qat_req->req.comn_mid.src_data_addr = qat_req->buf.blp; 11658c2ecf20Sopenharmony_ci qat_req->req.comn_mid.dest_data_addr = qat_req->buf.bloutp; 11668c2ecf20Sopenharmony_ci cipher_param = (void *)&qat_req->req.serv_specif_rqpars; 11678c2ecf20Sopenharmony_ci cipher_param->cipher_length = req->cryptlen; 11688c2ecf20Sopenharmony_ci cipher_param->cipher_offset = 0; 11698c2ecf20Sopenharmony_ci cipher_param->u.s.cipher_IV_ptr = qat_req->iv_paddr; 11708c2ecf20Sopenharmony_ci memcpy(qat_req->iv, req->iv, AES_BLOCK_SIZE); 11718c2ecf20Sopenharmony_ci do { 11728c2ecf20Sopenharmony_ci ret = adf_send_message(ctx->inst->sym_tx, (u32 *)msg); 11738c2ecf20Sopenharmony_ci } while (ret == -EAGAIN && ctr++ < 10); 11748c2ecf20Sopenharmony_ci 11758c2ecf20Sopenharmony_ci if (ret == -EAGAIN) { 11768c2ecf20Sopenharmony_ci qat_alg_free_bufl(ctx->inst, qat_req); 11778c2ecf20Sopenharmony_ci dma_free_coherent(dev, AES_BLOCK_SIZE, qat_req->iv, 11788c2ecf20Sopenharmony_ci qat_req->iv_paddr); 11798c2ecf20Sopenharmony_ci return -EBUSY; 11808c2ecf20Sopenharmony_ci } 11818c2ecf20Sopenharmony_ci return -EINPROGRESS; 11828c2ecf20Sopenharmony_ci} 11838c2ecf20Sopenharmony_ci 11848c2ecf20Sopenharmony_cistatic int qat_alg_skcipher_blk_decrypt(struct skcipher_request *req) 11858c2ecf20Sopenharmony_ci{ 11868c2ecf20Sopenharmony_ci if (req->cryptlen % AES_BLOCK_SIZE != 0) 11878c2ecf20Sopenharmony_ci return -EINVAL; 11888c2ecf20Sopenharmony_ci 11898c2ecf20Sopenharmony_ci return qat_alg_skcipher_decrypt(req); 11908c2ecf20Sopenharmony_ci} 11918c2ecf20Sopenharmony_ci 11928c2ecf20Sopenharmony_cistatic int qat_alg_skcipher_xts_decrypt(struct skcipher_request *req) 11938c2ecf20Sopenharmony_ci{ 11948c2ecf20Sopenharmony_ci struct crypto_skcipher *stfm = crypto_skcipher_reqtfm(req); 11958c2ecf20Sopenharmony_ci struct qat_alg_skcipher_ctx *ctx = crypto_skcipher_ctx(stfm); 11968c2ecf20Sopenharmony_ci struct skcipher_request *nreq = skcipher_request_ctx(req); 11978c2ecf20Sopenharmony_ci 11988c2ecf20Sopenharmony_ci if (req->cryptlen < XTS_BLOCK_SIZE) 11998c2ecf20Sopenharmony_ci return -EINVAL; 12008c2ecf20Sopenharmony_ci 12018c2ecf20Sopenharmony_ci if (ctx->fallback) { 12028c2ecf20Sopenharmony_ci memcpy(nreq, req, sizeof(*req)); 12038c2ecf20Sopenharmony_ci skcipher_request_set_tfm(nreq, ctx->ftfm); 12048c2ecf20Sopenharmony_ci return crypto_skcipher_decrypt(nreq); 12058c2ecf20Sopenharmony_ci } 12068c2ecf20Sopenharmony_ci 12078c2ecf20Sopenharmony_ci return qat_alg_skcipher_decrypt(req); 12088c2ecf20Sopenharmony_ci} 12098c2ecf20Sopenharmony_ci 12108c2ecf20Sopenharmony_cistatic int qat_alg_aead_init(struct crypto_aead *tfm, 12118c2ecf20Sopenharmony_ci enum icp_qat_hw_auth_algo hash, 12128c2ecf20Sopenharmony_ci const char *hash_name) 12138c2ecf20Sopenharmony_ci{ 12148c2ecf20Sopenharmony_ci struct qat_alg_aead_ctx *ctx = crypto_aead_ctx(tfm); 12158c2ecf20Sopenharmony_ci 12168c2ecf20Sopenharmony_ci ctx->hash_tfm = crypto_alloc_shash(hash_name, 0, 0); 12178c2ecf20Sopenharmony_ci if (IS_ERR(ctx->hash_tfm)) 12188c2ecf20Sopenharmony_ci return PTR_ERR(ctx->hash_tfm); 12198c2ecf20Sopenharmony_ci ctx->qat_hash_alg = hash; 12208c2ecf20Sopenharmony_ci crypto_aead_set_reqsize(tfm, sizeof(struct qat_crypto_request)); 12218c2ecf20Sopenharmony_ci return 0; 12228c2ecf20Sopenharmony_ci} 12238c2ecf20Sopenharmony_ci 12248c2ecf20Sopenharmony_cistatic int qat_alg_aead_sha1_init(struct crypto_aead *tfm) 12258c2ecf20Sopenharmony_ci{ 12268c2ecf20Sopenharmony_ci return qat_alg_aead_init(tfm, ICP_QAT_HW_AUTH_ALGO_SHA1, "sha1"); 12278c2ecf20Sopenharmony_ci} 12288c2ecf20Sopenharmony_ci 12298c2ecf20Sopenharmony_cistatic int qat_alg_aead_sha256_init(struct crypto_aead *tfm) 12308c2ecf20Sopenharmony_ci{ 12318c2ecf20Sopenharmony_ci return qat_alg_aead_init(tfm, ICP_QAT_HW_AUTH_ALGO_SHA256, "sha256"); 12328c2ecf20Sopenharmony_ci} 12338c2ecf20Sopenharmony_ci 12348c2ecf20Sopenharmony_cistatic int qat_alg_aead_sha512_init(struct crypto_aead *tfm) 12358c2ecf20Sopenharmony_ci{ 12368c2ecf20Sopenharmony_ci return qat_alg_aead_init(tfm, ICP_QAT_HW_AUTH_ALGO_SHA512, "sha512"); 12378c2ecf20Sopenharmony_ci} 12388c2ecf20Sopenharmony_ci 12398c2ecf20Sopenharmony_cistatic void qat_alg_aead_exit(struct crypto_aead *tfm) 12408c2ecf20Sopenharmony_ci{ 12418c2ecf20Sopenharmony_ci struct qat_alg_aead_ctx *ctx = crypto_aead_ctx(tfm); 12428c2ecf20Sopenharmony_ci struct qat_crypto_instance *inst = ctx->inst; 12438c2ecf20Sopenharmony_ci struct device *dev; 12448c2ecf20Sopenharmony_ci 12458c2ecf20Sopenharmony_ci crypto_free_shash(ctx->hash_tfm); 12468c2ecf20Sopenharmony_ci 12478c2ecf20Sopenharmony_ci if (!inst) 12488c2ecf20Sopenharmony_ci return; 12498c2ecf20Sopenharmony_ci 12508c2ecf20Sopenharmony_ci dev = &GET_DEV(inst->accel_dev); 12518c2ecf20Sopenharmony_ci if (ctx->enc_cd) { 12528c2ecf20Sopenharmony_ci memset(ctx->enc_cd, 0, sizeof(struct qat_alg_cd)); 12538c2ecf20Sopenharmony_ci dma_free_coherent(dev, sizeof(struct qat_alg_cd), 12548c2ecf20Sopenharmony_ci ctx->enc_cd, ctx->enc_cd_paddr); 12558c2ecf20Sopenharmony_ci } 12568c2ecf20Sopenharmony_ci if (ctx->dec_cd) { 12578c2ecf20Sopenharmony_ci memset(ctx->dec_cd, 0, sizeof(struct qat_alg_cd)); 12588c2ecf20Sopenharmony_ci dma_free_coherent(dev, sizeof(struct qat_alg_cd), 12598c2ecf20Sopenharmony_ci ctx->dec_cd, ctx->dec_cd_paddr); 12608c2ecf20Sopenharmony_ci } 12618c2ecf20Sopenharmony_ci qat_crypto_put_instance(inst); 12628c2ecf20Sopenharmony_ci} 12638c2ecf20Sopenharmony_ci 12648c2ecf20Sopenharmony_cistatic int qat_alg_skcipher_init_tfm(struct crypto_skcipher *tfm) 12658c2ecf20Sopenharmony_ci{ 12668c2ecf20Sopenharmony_ci crypto_skcipher_set_reqsize(tfm, sizeof(struct qat_crypto_request)); 12678c2ecf20Sopenharmony_ci return 0; 12688c2ecf20Sopenharmony_ci} 12698c2ecf20Sopenharmony_ci 12708c2ecf20Sopenharmony_cistatic int qat_alg_skcipher_init_xts_tfm(struct crypto_skcipher *tfm) 12718c2ecf20Sopenharmony_ci{ 12728c2ecf20Sopenharmony_ci struct qat_alg_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 12738c2ecf20Sopenharmony_ci int reqsize; 12748c2ecf20Sopenharmony_ci 12758c2ecf20Sopenharmony_ci ctx->ftfm = crypto_alloc_skcipher("xts(aes)", 0, 12768c2ecf20Sopenharmony_ci CRYPTO_ALG_NEED_FALLBACK); 12778c2ecf20Sopenharmony_ci if (IS_ERR(ctx->ftfm)) 12788c2ecf20Sopenharmony_ci return PTR_ERR(ctx->ftfm); 12798c2ecf20Sopenharmony_ci 12808c2ecf20Sopenharmony_ci reqsize = max(sizeof(struct qat_crypto_request), 12818c2ecf20Sopenharmony_ci sizeof(struct skcipher_request) + 12828c2ecf20Sopenharmony_ci crypto_skcipher_reqsize(ctx->ftfm)); 12838c2ecf20Sopenharmony_ci crypto_skcipher_set_reqsize(tfm, reqsize); 12848c2ecf20Sopenharmony_ci 12858c2ecf20Sopenharmony_ci return 0; 12868c2ecf20Sopenharmony_ci} 12878c2ecf20Sopenharmony_ci 12888c2ecf20Sopenharmony_cistatic void qat_alg_skcipher_exit_tfm(struct crypto_skcipher *tfm) 12898c2ecf20Sopenharmony_ci{ 12908c2ecf20Sopenharmony_ci struct qat_alg_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 12918c2ecf20Sopenharmony_ci struct qat_crypto_instance *inst = ctx->inst; 12928c2ecf20Sopenharmony_ci struct device *dev; 12938c2ecf20Sopenharmony_ci 12948c2ecf20Sopenharmony_ci if (!inst) 12958c2ecf20Sopenharmony_ci return; 12968c2ecf20Sopenharmony_ci 12978c2ecf20Sopenharmony_ci dev = &GET_DEV(inst->accel_dev); 12988c2ecf20Sopenharmony_ci if (ctx->enc_cd) { 12998c2ecf20Sopenharmony_ci memset(ctx->enc_cd, 0, 13008c2ecf20Sopenharmony_ci sizeof(struct icp_qat_hw_cipher_algo_blk)); 13018c2ecf20Sopenharmony_ci dma_free_coherent(dev, 13028c2ecf20Sopenharmony_ci sizeof(struct icp_qat_hw_cipher_algo_blk), 13038c2ecf20Sopenharmony_ci ctx->enc_cd, ctx->enc_cd_paddr); 13048c2ecf20Sopenharmony_ci } 13058c2ecf20Sopenharmony_ci if (ctx->dec_cd) { 13068c2ecf20Sopenharmony_ci memset(ctx->dec_cd, 0, 13078c2ecf20Sopenharmony_ci sizeof(struct icp_qat_hw_cipher_algo_blk)); 13088c2ecf20Sopenharmony_ci dma_free_coherent(dev, 13098c2ecf20Sopenharmony_ci sizeof(struct icp_qat_hw_cipher_algo_blk), 13108c2ecf20Sopenharmony_ci ctx->dec_cd, ctx->dec_cd_paddr); 13118c2ecf20Sopenharmony_ci } 13128c2ecf20Sopenharmony_ci qat_crypto_put_instance(inst); 13138c2ecf20Sopenharmony_ci} 13148c2ecf20Sopenharmony_ci 13158c2ecf20Sopenharmony_cistatic void qat_alg_skcipher_exit_xts_tfm(struct crypto_skcipher *tfm) 13168c2ecf20Sopenharmony_ci{ 13178c2ecf20Sopenharmony_ci struct qat_alg_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm); 13188c2ecf20Sopenharmony_ci 13198c2ecf20Sopenharmony_ci if (ctx->ftfm) 13208c2ecf20Sopenharmony_ci crypto_free_skcipher(ctx->ftfm); 13218c2ecf20Sopenharmony_ci 13228c2ecf20Sopenharmony_ci qat_alg_skcipher_exit_tfm(tfm); 13238c2ecf20Sopenharmony_ci} 13248c2ecf20Sopenharmony_ci 13258c2ecf20Sopenharmony_cistatic struct aead_alg qat_aeads[] = { { 13268c2ecf20Sopenharmony_ci .base = { 13278c2ecf20Sopenharmony_ci .cra_name = "authenc(hmac(sha1),cbc(aes))", 13288c2ecf20Sopenharmony_ci .cra_driver_name = "qat_aes_cbc_hmac_sha1", 13298c2ecf20Sopenharmony_ci .cra_priority = 4001, 13308c2ecf20Sopenharmony_ci .cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY, 13318c2ecf20Sopenharmony_ci .cra_blocksize = AES_BLOCK_SIZE, 13328c2ecf20Sopenharmony_ci .cra_ctxsize = sizeof(struct qat_alg_aead_ctx), 13338c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 13348c2ecf20Sopenharmony_ci }, 13358c2ecf20Sopenharmony_ci .init = qat_alg_aead_sha1_init, 13368c2ecf20Sopenharmony_ci .exit = qat_alg_aead_exit, 13378c2ecf20Sopenharmony_ci .setkey = qat_alg_aead_setkey, 13388c2ecf20Sopenharmony_ci .decrypt = qat_alg_aead_dec, 13398c2ecf20Sopenharmony_ci .encrypt = qat_alg_aead_enc, 13408c2ecf20Sopenharmony_ci .ivsize = AES_BLOCK_SIZE, 13418c2ecf20Sopenharmony_ci .maxauthsize = SHA1_DIGEST_SIZE, 13428c2ecf20Sopenharmony_ci}, { 13438c2ecf20Sopenharmony_ci .base = { 13448c2ecf20Sopenharmony_ci .cra_name = "authenc(hmac(sha256),cbc(aes))", 13458c2ecf20Sopenharmony_ci .cra_driver_name = "qat_aes_cbc_hmac_sha256", 13468c2ecf20Sopenharmony_ci .cra_priority = 4001, 13478c2ecf20Sopenharmony_ci .cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY, 13488c2ecf20Sopenharmony_ci .cra_blocksize = AES_BLOCK_SIZE, 13498c2ecf20Sopenharmony_ci .cra_ctxsize = sizeof(struct qat_alg_aead_ctx), 13508c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 13518c2ecf20Sopenharmony_ci }, 13528c2ecf20Sopenharmony_ci .init = qat_alg_aead_sha256_init, 13538c2ecf20Sopenharmony_ci .exit = qat_alg_aead_exit, 13548c2ecf20Sopenharmony_ci .setkey = qat_alg_aead_setkey, 13558c2ecf20Sopenharmony_ci .decrypt = qat_alg_aead_dec, 13568c2ecf20Sopenharmony_ci .encrypt = qat_alg_aead_enc, 13578c2ecf20Sopenharmony_ci .ivsize = AES_BLOCK_SIZE, 13588c2ecf20Sopenharmony_ci .maxauthsize = SHA256_DIGEST_SIZE, 13598c2ecf20Sopenharmony_ci}, { 13608c2ecf20Sopenharmony_ci .base = { 13618c2ecf20Sopenharmony_ci .cra_name = "authenc(hmac(sha512),cbc(aes))", 13628c2ecf20Sopenharmony_ci .cra_driver_name = "qat_aes_cbc_hmac_sha512", 13638c2ecf20Sopenharmony_ci .cra_priority = 4001, 13648c2ecf20Sopenharmony_ci .cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY, 13658c2ecf20Sopenharmony_ci .cra_blocksize = AES_BLOCK_SIZE, 13668c2ecf20Sopenharmony_ci .cra_ctxsize = sizeof(struct qat_alg_aead_ctx), 13678c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 13688c2ecf20Sopenharmony_ci }, 13698c2ecf20Sopenharmony_ci .init = qat_alg_aead_sha512_init, 13708c2ecf20Sopenharmony_ci .exit = qat_alg_aead_exit, 13718c2ecf20Sopenharmony_ci .setkey = qat_alg_aead_setkey, 13728c2ecf20Sopenharmony_ci .decrypt = qat_alg_aead_dec, 13738c2ecf20Sopenharmony_ci .encrypt = qat_alg_aead_enc, 13748c2ecf20Sopenharmony_ci .ivsize = AES_BLOCK_SIZE, 13758c2ecf20Sopenharmony_ci .maxauthsize = SHA512_DIGEST_SIZE, 13768c2ecf20Sopenharmony_ci} }; 13778c2ecf20Sopenharmony_ci 13788c2ecf20Sopenharmony_cistatic struct skcipher_alg qat_skciphers[] = { { 13798c2ecf20Sopenharmony_ci .base.cra_name = "cbc(aes)", 13808c2ecf20Sopenharmony_ci .base.cra_driver_name = "qat_aes_cbc", 13818c2ecf20Sopenharmony_ci .base.cra_priority = 4001, 13828c2ecf20Sopenharmony_ci .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY, 13838c2ecf20Sopenharmony_ci .base.cra_blocksize = AES_BLOCK_SIZE, 13848c2ecf20Sopenharmony_ci .base.cra_ctxsize = sizeof(struct qat_alg_skcipher_ctx), 13858c2ecf20Sopenharmony_ci .base.cra_alignmask = 0, 13868c2ecf20Sopenharmony_ci .base.cra_module = THIS_MODULE, 13878c2ecf20Sopenharmony_ci 13888c2ecf20Sopenharmony_ci .init = qat_alg_skcipher_init_tfm, 13898c2ecf20Sopenharmony_ci .exit = qat_alg_skcipher_exit_tfm, 13908c2ecf20Sopenharmony_ci .setkey = qat_alg_skcipher_cbc_setkey, 13918c2ecf20Sopenharmony_ci .decrypt = qat_alg_skcipher_blk_decrypt, 13928c2ecf20Sopenharmony_ci .encrypt = qat_alg_skcipher_blk_encrypt, 13938c2ecf20Sopenharmony_ci .min_keysize = AES_MIN_KEY_SIZE, 13948c2ecf20Sopenharmony_ci .max_keysize = AES_MAX_KEY_SIZE, 13958c2ecf20Sopenharmony_ci .ivsize = AES_BLOCK_SIZE, 13968c2ecf20Sopenharmony_ci}, { 13978c2ecf20Sopenharmony_ci .base.cra_name = "ctr(aes)", 13988c2ecf20Sopenharmony_ci .base.cra_driver_name = "qat_aes_ctr", 13998c2ecf20Sopenharmony_ci .base.cra_priority = 4001, 14008c2ecf20Sopenharmony_ci .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_ALLOCATES_MEMORY, 14018c2ecf20Sopenharmony_ci .base.cra_blocksize = 1, 14028c2ecf20Sopenharmony_ci .base.cra_ctxsize = sizeof(struct qat_alg_skcipher_ctx), 14038c2ecf20Sopenharmony_ci .base.cra_alignmask = 0, 14048c2ecf20Sopenharmony_ci .base.cra_module = THIS_MODULE, 14058c2ecf20Sopenharmony_ci 14068c2ecf20Sopenharmony_ci .init = qat_alg_skcipher_init_tfm, 14078c2ecf20Sopenharmony_ci .exit = qat_alg_skcipher_exit_tfm, 14088c2ecf20Sopenharmony_ci .setkey = qat_alg_skcipher_ctr_setkey, 14098c2ecf20Sopenharmony_ci .decrypt = qat_alg_skcipher_decrypt, 14108c2ecf20Sopenharmony_ci .encrypt = qat_alg_skcipher_encrypt, 14118c2ecf20Sopenharmony_ci .min_keysize = AES_MIN_KEY_SIZE, 14128c2ecf20Sopenharmony_ci .max_keysize = AES_MAX_KEY_SIZE, 14138c2ecf20Sopenharmony_ci .ivsize = AES_BLOCK_SIZE, 14148c2ecf20Sopenharmony_ci}, { 14158c2ecf20Sopenharmony_ci .base.cra_name = "xts(aes)", 14168c2ecf20Sopenharmony_ci .base.cra_driver_name = "qat_aes_xts", 14178c2ecf20Sopenharmony_ci .base.cra_priority = 4001, 14188c2ecf20Sopenharmony_ci .base.cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK | 14198c2ecf20Sopenharmony_ci CRYPTO_ALG_ALLOCATES_MEMORY, 14208c2ecf20Sopenharmony_ci .base.cra_blocksize = AES_BLOCK_SIZE, 14218c2ecf20Sopenharmony_ci .base.cra_ctxsize = sizeof(struct qat_alg_skcipher_ctx), 14228c2ecf20Sopenharmony_ci .base.cra_alignmask = 0, 14238c2ecf20Sopenharmony_ci .base.cra_module = THIS_MODULE, 14248c2ecf20Sopenharmony_ci 14258c2ecf20Sopenharmony_ci .init = qat_alg_skcipher_init_xts_tfm, 14268c2ecf20Sopenharmony_ci .exit = qat_alg_skcipher_exit_xts_tfm, 14278c2ecf20Sopenharmony_ci .setkey = qat_alg_skcipher_xts_setkey, 14288c2ecf20Sopenharmony_ci .decrypt = qat_alg_skcipher_xts_decrypt, 14298c2ecf20Sopenharmony_ci .encrypt = qat_alg_skcipher_xts_encrypt, 14308c2ecf20Sopenharmony_ci .min_keysize = 2 * AES_MIN_KEY_SIZE, 14318c2ecf20Sopenharmony_ci .max_keysize = 2 * AES_MAX_KEY_SIZE, 14328c2ecf20Sopenharmony_ci .ivsize = AES_BLOCK_SIZE, 14338c2ecf20Sopenharmony_ci} }; 14348c2ecf20Sopenharmony_ci 14358c2ecf20Sopenharmony_ciint qat_algs_register(void) 14368c2ecf20Sopenharmony_ci{ 14378c2ecf20Sopenharmony_ci int ret = 0; 14388c2ecf20Sopenharmony_ci 14398c2ecf20Sopenharmony_ci mutex_lock(&algs_lock); 14408c2ecf20Sopenharmony_ci if (++active_devs != 1) 14418c2ecf20Sopenharmony_ci goto unlock; 14428c2ecf20Sopenharmony_ci 14438c2ecf20Sopenharmony_ci ret = crypto_register_skciphers(qat_skciphers, 14448c2ecf20Sopenharmony_ci ARRAY_SIZE(qat_skciphers)); 14458c2ecf20Sopenharmony_ci if (ret) 14468c2ecf20Sopenharmony_ci goto unlock; 14478c2ecf20Sopenharmony_ci 14488c2ecf20Sopenharmony_ci ret = crypto_register_aeads(qat_aeads, ARRAY_SIZE(qat_aeads)); 14498c2ecf20Sopenharmony_ci if (ret) 14508c2ecf20Sopenharmony_ci goto unreg_algs; 14518c2ecf20Sopenharmony_ci 14528c2ecf20Sopenharmony_ciunlock: 14538c2ecf20Sopenharmony_ci mutex_unlock(&algs_lock); 14548c2ecf20Sopenharmony_ci return ret; 14558c2ecf20Sopenharmony_ci 14568c2ecf20Sopenharmony_ciunreg_algs: 14578c2ecf20Sopenharmony_ci crypto_unregister_skciphers(qat_skciphers, ARRAY_SIZE(qat_skciphers)); 14588c2ecf20Sopenharmony_ci goto unlock; 14598c2ecf20Sopenharmony_ci} 14608c2ecf20Sopenharmony_ci 14618c2ecf20Sopenharmony_civoid qat_algs_unregister(void) 14628c2ecf20Sopenharmony_ci{ 14638c2ecf20Sopenharmony_ci mutex_lock(&algs_lock); 14648c2ecf20Sopenharmony_ci if (--active_devs != 0) 14658c2ecf20Sopenharmony_ci goto unlock; 14668c2ecf20Sopenharmony_ci 14678c2ecf20Sopenharmony_ci crypto_unregister_aeads(qat_aeads, ARRAY_SIZE(qat_aeads)); 14688c2ecf20Sopenharmony_ci crypto_unregister_skciphers(qat_skciphers, ARRAY_SIZE(qat_skciphers)); 14698c2ecf20Sopenharmony_ci 14708c2ecf20Sopenharmony_ciunlock: 14718c2ecf20Sopenharmony_ci mutex_unlock(&algs_lock); 14728c2ecf20Sopenharmony_ci} 1473