18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved. 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/err.h> 78c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 88c2ecf20Sopenharmony_ci#include <linux/types.h> 98c2ecf20Sopenharmony_ci#include <crypto/scatterwalk.h> 108c2ecf20Sopenharmony_ci#include <crypto/sha.h> 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include "cipher.h" 138c2ecf20Sopenharmony_ci#include "common.h" 148c2ecf20Sopenharmony_ci#include "core.h" 158c2ecf20Sopenharmony_ci#include "regs-v5.h" 168c2ecf20Sopenharmony_ci#include "sha.h" 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_cistatic inline u32 qce_read(struct qce_device *qce, u32 offset) 198c2ecf20Sopenharmony_ci{ 208c2ecf20Sopenharmony_ci return readl(qce->base + offset); 218c2ecf20Sopenharmony_ci} 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_cistatic inline void qce_write(struct qce_device *qce, u32 offset, u32 val) 248c2ecf20Sopenharmony_ci{ 258c2ecf20Sopenharmony_ci writel(val, qce->base + offset); 268c2ecf20Sopenharmony_ci} 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_cistatic inline void qce_write_array(struct qce_device *qce, u32 offset, 298c2ecf20Sopenharmony_ci const u32 *val, unsigned int len) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci int i; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci for (i = 0; i < len; i++) 348c2ecf20Sopenharmony_ci qce_write(qce, offset + i * sizeof(u32), val[i]); 358c2ecf20Sopenharmony_ci} 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_cistatic inline void 388c2ecf20Sopenharmony_ciqce_clear_array(struct qce_device *qce, u32 offset, unsigned int len) 398c2ecf20Sopenharmony_ci{ 408c2ecf20Sopenharmony_ci int i; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci for (i = 0; i < len; i++) 438c2ecf20Sopenharmony_ci qce_write(qce, offset + i * sizeof(u32), 0); 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_cistatic u32 qce_config_reg(struct qce_device *qce, int little) 478c2ecf20Sopenharmony_ci{ 488c2ecf20Sopenharmony_ci u32 beats = (qce->burst_size >> 3) - 1; 498c2ecf20Sopenharmony_ci u32 pipe_pair = qce->pipe_pair_id; 508c2ecf20Sopenharmony_ci u32 config; 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci config = (beats << REQ_SIZE_SHIFT) & REQ_SIZE_MASK; 538c2ecf20Sopenharmony_ci config |= BIT(MASK_DOUT_INTR_SHIFT) | BIT(MASK_DIN_INTR_SHIFT) | 548c2ecf20Sopenharmony_ci BIT(MASK_OP_DONE_INTR_SHIFT) | BIT(MASK_ERR_INTR_SHIFT); 558c2ecf20Sopenharmony_ci config |= (pipe_pair << PIPE_SET_SELECT_SHIFT) & PIPE_SET_SELECT_MASK; 568c2ecf20Sopenharmony_ci config &= ~HIGH_SPD_EN_N_SHIFT; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci if (little) 598c2ecf20Sopenharmony_ci config |= BIT(LITTLE_ENDIAN_MODE_SHIFT); 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci return config; 628c2ecf20Sopenharmony_ci} 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_civoid qce_cpu_to_be32p_array(__be32 *dst, const u8 *src, unsigned int len) 658c2ecf20Sopenharmony_ci{ 668c2ecf20Sopenharmony_ci __be32 *d = dst; 678c2ecf20Sopenharmony_ci const u8 *s = src; 688c2ecf20Sopenharmony_ci unsigned int n; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci n = len / sizeof(u32); 718c2ecf20Sopenharmony_ci for (; n > 0; n--) { 728c2ecf20Sopenharmony_ci *d = cpu_to_be32p((const __u32 *) s); 738c2ecf20Sopenharmony_ci s += sizeof(__u32); 748c2ecf20Sopenharmony_ci d++; 758c2ecf20Sopenharmony_ci } 768c2ecf20Sopenharmony_ci} 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_cistatic void qce_setup_config(struct qce_device *qce) 798c2ecf20Sopenharmony_ci{ 808c2ecf20Sopenharmony_ci u32 config; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci /* get big endianness */ 838c2ecf20Sopenharmony_ci config = qce_config_reg(qce, 0); 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci /* clear status */ 868c2ecf20Sopenharmony_ci qce_write(qce, REG_STATUS, 0); 878c2ecf20Sopenharmony_ci qce_write(qce, REG_CONFIG, config); 888c2ecf20Sopenharmony_ci} 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_cistatic inline void qce_crypto_go(struct qce_device *qce) 918c2ecf20Sopenharmony_ci{ 928c2ecf20Sopenharmony_ci qce_write(qce, REG_GOPROC, BIT(GO_SHIFT) | BIT(RESULTS_DUMP_SHIFT)); 938c2ecf20Sopenharmony_ci} 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci#ifdef CONFIG_CRYPTO_DEV_QCE_SHA 968c2ecf20Sopenharmony_cistatic u32 qce_auth_cfg(unsigned long flags, u32 key_size) 978c2ecf20Sopenharmony_ci{ 988c2ecf20Sopenharmony_ci u32 cfg = 0; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci if (IS_AES(flags) && (IS_CCM(flags) || IS_CMAC(flags))) 1018c2ecf20Sopenharmony_ci cfg |= AUTH_ALG_AES << AUTH_ALG_SHIFT; 1028c2ecf20Sopenharmony_ci else 1038c2ecf20Sopenharmony_ci cfg |= AUTH_ALG_SHA << AUTH_ALG_SHIFT; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci if (IS_CCM(flags) || IS_CMAC(flags)) { 1068c2ecf20Sopenharmony_ci if (key_size == AES_KEYSIZE_128) 1078c2ecf20Sopenharmony_ci cfg |= AUTH_KEY_SZ_AES128 << AUTH_KEY_SIZE_SHIFT; 1088c2ecf20Sopenharmony_ci else if (key_size == AES_KEYSIZE_256) 1098c2ecf20Sopenharmony_ci cfg |= AUTH_KEY_SZ_AES256 << AUTH_KEY_SIZE_SHIFT; 1108c2ecf20Sopenharmony_ci } 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci if (IS_SHA1(flags) || IS_SHA1_HMAC(flags)) 1138c2ecf20Sopenharmony_ci cfg |= AUTH_SIZE_SHA1 << AUTH_SIZE_SHIFT; 1148c2ecf20Sopenharmony_ci else if (IS_SHA256(flags) || IS_SHA256_HMAC(flags)) 1158c2ecf20Sopenharmony_ci cfg |= AUTH_SIZE_SHA256 << AUTH_SIZE_SHIFT; 1168c2ecf20Sopenharmony_ci else if (IS_CMAC(flags)) 1178c2ecf20Sopenharmony_ci cfg |= AUTH_SIZE_ENUM_16_BYTES << AUTH_SIZE_SHIFT; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci if (IS_SHA1(flags) || IS_SHA256(flags)) 1208c2ecf20Sopenharmony_ci cfg |= AUTH_MODE_HASH << AUTH_MODE_SHIFT; 1218c2ecf20Sopenharmony_ci else if (IS_SHA1_HMAC(flags) || IS_SHA256_HMAC(flags) || 1228c2ecf20Sopenharmony_ci IS_CBC(flags) || IS_CTR(flags)) 1238c2ecf20Sopenharmony_ci cfg |= AUTH_MODE_HMAC << AUTH_MODE_SHIFT; 1248c2ecf20Sopenharmony_ci else if (IS_AES(flags) && IS_CCM(flags)) 1258c2ecf20Sopenharmony_ci cfg |= AUTH_MODE_CCM << AUTH_MODE_SHIFT; 1268c2ecf20Sopenharmony_ci else if (IS_AES(flags) && IS_CMAC(flags)) 1278c2ecf20Sopenharmony_ci cfg |= AUTH_MODE_CMAC << AUTH_MODE_SHIFT; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci if (IS_SHA(flags) || IS_SHA_HMAC(flags)) 1308c2ecf20Sopenharmony_ci cfg |= AUTH_POS_BEFORE << AUTH_POS_SHIFT; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci if (IS_CCM(flags)) 1338c2ecf20Sopenharmony_ci cfg |= QCE_MAX_NONCE_WORDS << AUTH_NONCE_NUM_WORDS_SHIFT; 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci if (IS_CBC(flags) || IS_CTR(flags) || IS_CCM(flags) || 1368c2ecf20Sopenharmony_ci IS_CMAC(flags)) 1378c2ecf20Sopenharmony_ci cfg |= BIT(AUTH_LAST_SHIFT) | BIT(AUTH_FIRST_SHIFT); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci return cfg; 1408c2ecf20Sopenharmony_ci} 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_cistatic int qce_setup_regs_ahash(struct crypto_async_request *async_req, 1438c2ecf20Sopenharmony_ci u32 totallen, u32 offset) 1448c2ecf20Sopenharmony_ci{ 1458c2ecf20Sopenharmony_ci struct ahash_request *req = ahash_request_cast(async_req); 1468c2ecf20Sopenharmony_ci struct crypto_ahash *ahash = __crypto_ahash_cast(async_req->tfm); 1478c2ecf20Sopenharmony_ci struct qce_sha_reqctx *rctx = ahash_request_ctx(req); 1488c2ecf20Sopenharmony_ci struct qce_alg_template *tmpl = to_ahash_tmpl(async_req->tfm); 1498c2ecf20Sopenharmony_ci struct qce_device *qce = tmpl->qce; 1508c2ecf20Sopenharmony_ci unsigned int digestsize = crypto_ahash_digestsize(ahash); 1518c2ecf20Sopenharmony_ci unsigned int blocksize = crypto_tfm_alg_blocksize(async_req->tfm); 1528c2ecf20Sopenharmony_ci __be32 auth[SHA256_DIGEST_SIZE / sizeof(__be32)] = {0}; 1538c2ecf20Sopenharmony_ci __be32 mackey[QCE_SHA_HMAC_KEY_SIZE / sizeof(__be32)] = {0}; 1548c2ecf20Sopenharmony_ci u32 auth_cfg = 0, config; 1558c2ecf20Sopenharmony_ci unsigned int iv_words; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci /* if not the last, the size has to be on the block boundary */ 1588c2ecf20Sopenharmony_ci if (!rctx->last_blk && req->nbytes % blocksize) 1598c2ecf20Sopenharmony_ci return -EINVAL; 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci qce_setup_config(qce); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci if (IS_CMAC(rctx->flags)) { 1648c2ecf20Sopenharmony_ci qce_write(qce, REG_AUTH_SEG_CFG, 0); 1658c2ecf20Sopenharmony_ci qce_write(qce, REG_ENCR_SEG_CFG, 0); 1668c2ecf20Sopenharmony_ci qce_write(qce, REG_ENCR_SEG_SIZE, 0); 1678c2ecf20Sopenharmony_ci qce_clear_array(qce, REG_AUTH_IV0, 16); 1688c2ecf20Sopenharmony_ci qce_clear_array(qce, REG_AUTH_KEY0, 16); 1698c2ecf20Sopenharmony_ci qce_clear_array(qce, REG_AUTH_BYTECNT0, 4); 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci auth_cfg = qce_auth_cfg(rctx->flags, rctx->authklen); 1728c2ecf20Sopenharmony_ci } 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci if (IS_SHA_HMAC(rctx->flags) || IS_CMAC(rctx->flags)) { 1758c2ecf20Sopenharmony_ci u32 authkey_words = rctx->authklen / sizeof(u32); 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_ci qce_cpu_to_be32p_array(mackey, rctx->authkey, rctx->authklen); 1788c2ecf20Sopenharmony_ci qce_write_array(qce, REG_AUTH_KEY0, (u32 *)mackey, 1798c2ecf20Sopenharmony_ci authkey_words); 1808c2ecf20Sopenharmony_ci } 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci if (IS_CMAC(rctx->flags)) 1838c2ecf20Sopenharmony_ci goto go_proc; 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci if (rctx->first_blk) 1868c2ecf20Sopenharmony_ci memcpy(auth, rctx->digest, digestsize); 1878c2ecf20Sopenharmony_ci else 1888c2ecf20Sopenharmony_ci qce_cpu_to_be32p_array(auth, rctx->digest, digestsize); 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_ci iv_words = (IS_SHA1(rctx->flags) || IS_SHA1_HMAC(rctx->flags)) ? 5 : 8; 1918c2ecf20Sopenharmony_ci qce_write_array(qce, REG_AUTH_IV0, (u32 *)auth, iv_words); 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci if (rctx->first_blk) 1948c2ecf20Sopenharmony_ci qce_clear_array(qce, REG_AUTH_BYTECNT0, 4); 1958c2ecf20Sopenharmony_ci else 1968c2ecf20Sopenharmony_ci qce_write_array(qce, REG_AUTH_BYTECNT0, 1978c2ecf20Sopenharmony_ci (u32 *)rctx->byte_count, 2); 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci auth_cfg = qce_auth_cfg(rctx->flags, 0); 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci if (rctx->last_blk) 2028c2ecf20Sopenharmony_ci auth_cfg |= BIT(AUTH_LAST_SHIFT); 2038c2ecf20Sopenharmony_ci else 2048c2ecf20Sopenharmony_ci auth_cfg &= ~BIT(AUTH_LAST_SHIFT); 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci if (rctx->first_blk) 2078c2ecf20Sopenharmony_ci auth_cfg |= BIT(AUTH_FIRST_SHIFT); 2088c2ecf20Sopenharmony_ci else 2098c2ecf20Sopenharmony_ci auth_cfg &= ~BIT(AUTH_FIRST_SHIFT); 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_cigo_proc: 2128c2ecf20Sopenharmony_ci qce_write(qce, REG_AUTH_SEG_CFG, auth_cfg); 2138c2ecf20Sopenharmony_ci qce_write(qce, REG_AUTH_SEG_SIZE, req->nbytes); 2148c2ecf20Sopenharmony_ci qce_write(qce, REG_AUTH_SEG_START, 0); 2158c2ecf20Sopenharmony_ci qce_write(qce, REG_ENCR_SEG_CFG, 0); 2168c2ecf20Sopenharmony_ci qce_write(qce, REG_SEG_SIZE, req->nbytes); 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci /* get little endianness */ 2198c2ecf20Sopenharmony_ci config = qce_config_reg(qce, 1); 2208c2ecf20Sopenharmony_ci qce_write(qce, REG_CONFIG, config); 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci qce_crypto_go(qce); 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci return 0; 2258c2ecf20Sopenharmony_ci} 2268c2ecf20Sopenharmony_ci#endif 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci#ifdef CONFIG_CRYPTO_DEV_QCE_SKCIPHER 2298c2ecf20Sopenharmony_cistatic u32 qce_encr_cfg(unsigned long flags, u32 aes_key_size) 2308c2ecf20Sopenharmony_ci{ 2318c2ecf20Sopenharmony_ci u32 cfg = 0; 2328c2ecf20Sopenharmony_ci 2338c2ecf20Sopenharmony_ci if (IS_AES(flags)) { 2348c2ecf20Sopenharmony_ci if (aes_key_size == AES_KEYSIZE_128) 2358c2ecf20Sopenharmony_ci cfg |= ENCR_KEY_SZ_AES128 << ENCR_KEY_SZ_SHIFT; 2368c2ecf20Sopenharmony_ci else if (aes_key_size == AES_KEYSIZE_256) 2378c2ecf20Sopenharmony_ci cfg |= ENCR_KEY_SZ_AES256 << ENCR_KEY_SZ_SHIFT; 2388c2ecf20Sopenharmony_ci } 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci if (IS_AES(flags)) 2418c2ecf20Sopenharmony_ci cfg |= ENCR_ALG_AES << ENCR_ALG_SHIFT; 2428c2ecf20Sopenharmony_ci else if (IS_DES(flags) || IS_3DES(flags)) 2438c2ecf20Sopenharmony_ci cfg |= ENCR_ALG_DES << ENCR_ALG_SHIFT; 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci if (IS_DES(flags)) 2468c2ecf20Sopenharmony_ci cfg |= ENCR_KEY_SZ_DES << ENCR_KEY_SZ_SHIFT; 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci if (IS_3DES(flags)) 2498c2ecf20Sopenharmony_ci cfg |= ENCR_KEY_SZ_3DES << ENCR_KEY_SZ_SHIFT; 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci switch (flags & QCE_MODE_MASK) { 2528c2ecf20Sopenharmony_ci case QCE_MODE_ECB: 2538c2ecf20Sopenharmony_ci cfg |= ENCR_MODE_ECB << ENCR_MODE_SHIFT; 2548c2ecf20Sopenharmony_ci break; 2558c2ecf20Sopenharmony_ci case QCE_MODE_CBC: 2568c2ecf20Sopenharmony_ci cfg |= ENCR_MODE_CBC << ENCR_MODE_SHIFT; 2578c2ecf20Sopenharmony_ci break; 2588c2ecf20Sopenharmony_ci case QCE_MODE_CTR: 2598c2ecf20Sopenharmony_ci cfg |= ENCR_MODE_CTR << ENCR_MODE_SHIFT; 2608c2ecf20Sopenharmony_ci break; 2618c2ecf20Sopenharmony_ci case QCE_MODE_XTS: 2628c2ecf20Sopenharmony_ci cfg |= ENCR_MODE_XTS << ENCR_MODE_SHIFT; 2638c2ecf20Sopenharmony_ci break; 2648c2ecf20Sopenharmony_ci case QCE_MODE_CCM: 2658c2ecf20Sopenharmony_ci cfg |= ENCR_MODE_CCM << ENCR_MODE_SHIFT; 2668c2ecf20Sopenharmony_ci cfg |= LAST_CCM_XFR << LAST_CCM_SHIFT; 2678c2ecf20Sopenharmony_ci break; 2688c2ecf20Sopenharmony_ci default: 2698c2ecf20Sopenharmony_ci return ~0; 2708c2ecf20Sopenharmony_ci } 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci return cfg; 2738c2ecf20Sopenharmony_ci} 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_cistatic void qce_xts_swapiv(__be32 *dst, const u8 *src, unsigned int ivsize) 2768c2ecf20Sopenharmony_ci{ 2778c2ecf20Sopenharmony_ci u8 swap[QCE_AES_IV_LENGTH]; 2788c2ecf20Sopenharmony_ci u32 i, j; 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci if (ivsize > QCE_AES_IV_LENGTH) 2818c2ecf20Sopenharmony_ci return; 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci memset(swap, 0, QCE_AES_IV_LENGTH); 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci for (i = (QCE_AES_IV_LENGTH - ivsize), j = ivsize - 1; 2868c2ecf20Sopenharmony_ci i < QCE_AES_IV_LENGTH; i++, j--) 2878c2ecf20Sopenharmony_ci swap[i] = src[j]; 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci qce_cpu_to_be32p_array(dst, swap, QCE_AES_IV_LENGTH); 2908c2ecf20Sopenharmony_ci} 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_cistatic void qce_xtskey(struct qce_device *qce, const u8 *enckey, 2938c2ecf20Sopenharmony_ci unsigned int enckeylen, unsigned int cryptlen) 2948c2ecf20Sopenharmony_ci{ 2958c2ecf20Sopenharmony_ci u32 xtskey[QCE_MAX_CIPHER_KEY_SIZE / sizeof(u32)] = {0}; 2968c2ecf20Sopenharmony_ci unsigned int xtsklen = enckeylen / (2 * sizeof(u32)); 2978c2ecf20Sopenharmony_ci unsigned int xtsdusize; 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci qce_cpu_to_be32p_array((__be32 *)xtskey, enckey + enckeylen / 2, 3008c2ecf20Sopenharmony_ci enckeylen / 2); 3018c2ecf20Sopenharmony_ci qce_write_array(qce, REG_ENCR_XTS_KEY0, xtskey, xtsklen); 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci /* xts du size 512B */ 3048c2ecf20Sopenharmony_ci xtsdusize = min_t(u32, QCE_SECTOR_SIZE, cryptlen); 3058c2ecf20Sopenharmony_ci qce_write(qce, REG_ENCR_XTS_DU_SIZE, xtsdusize); 3068c2ecf20Sopenharmony_ci} 3078c2ecf20Sopenharmony_ci 3088c2ecf20Sopenharmony_cistatic int qce_setup_regs_skcipher(struct crypto_async_request *async_req, 3098c2ecf20Sopenharmony_ci u32 totallen, u32 offset) 3108c2ecf20Sopenharmony_ci{ 3118c2ecf20Sopenharmony_ci struct skcipher_request *req = skcipher_request_cast(async_req); 3128c2ecf20Sopenharmony_ci struct qce_cipher_reqctx *rctx = skcipher_request_ctx(req); 3138c2ecf20Sopenharmony_ci struct qce_cipher_ctx *ctx = crypto_tfm_ctx(async_req->tfm); 3148c2ecf20Sopenharmony_ci struct qce_alg_template *tmpl = to_cipher_tmpl(crypto_skcipher_reqtfm(req)); 3158c2ecf20Sopenharmony_ci struct qce_device *qce = tmpl->qce; 3168c2ecf20Sopenharmony_ci __be32 enckey[QCE_MAX_CIPHER_KEY_SIZE / sizeof(__be32)] = {0}; 3178c2ecf20Sopenharmony_ci __be32 enciv[QCE_MAX_IV_SIZE / sizeof(__be32)] = {0}; 3188c2ecf20Sopenharmony_ci unsigned int enckey_words, enciv_words; 3198c2ecf20Sopenharmony_ci unsigned int keylen; 3208c2ecf20Sopenharmony_ci u32 encr_cfg = 0, auth_cfg = 0, config; 3218c2ecf20Sopenharmony_ci unsigned int ivsize = rctx->ivsize; 3228c2ecf20Sopenharmony_ci unsigned long flags = rctx->flags; 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci qce_setup_config(qce); 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci if (IS_XTS(flags)) 3278c2ecf20Sopenharmony_ci keylen = ctx->enc_keylen / 2; 3288c2ecf20Sopenharmony_ci else 3298c2ecf20Sopenharmony_ci keylen = ctx->enc_keylen; 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci qce_cpu_to_be32p_array(enckey, ctx->enc_key, keylen); 3328c2ecf20Sopenharmony_ci enckey_words = keylen / sizeof(u32); 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci qce_write(qce, REG_AUTH_SEG_CFG, auth_cfg); 3358c2ecf20Sopenharmony_ci 3368c2ecf20Sopenharmony_ci encr_cfg = qce_encr_cfg(flags, keylen); 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci if (IS_DES(flags)) { 3398c2ecf20Sopenharmony_ci enciv_words = 2; 3408c2ecf20Sopenharmony_ci enckey_words = 2; 3418c2ecf20Sopenharmony_ci } else if (IS_3DES(flags)) { 3428c2ecf20Sopenharmony_ci enciv_words = 2; 3438c2ecf20Sopenharmony_ci enckey_words = 6; 3448c2ecf20Sopenharmony_ci } else if (IS_AES(flags)) { 3458c2ecf20Sopenharmony_ci if (IS_XTS(flags)) 3468c2ecf20Sopenharmony_ci qce_xtskey(qce, ctx->enc_key, ctx->enc_keylen, 3478c2ecf20Sopenharmony_ci rctx->cryptlen); 3488c2ecf20Sopenharmony_ci enciv_words = 4; 3498c2ecf20Sopenharmony_ci } else { 3508c2ecf20Sopenharmony_ci return -EINVAL; 3518c2ecf20Sopenharmony_ci } 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci qce_write_array(qce, REG_ENCR_KEY0, (u32 *)enckey, enckey_words); 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_ci if (!IS_ECB(flags)) { 3568c2ecf20Sopenharmony_ci if (IS_XTS(flags)) 3578c2ecf20Sopenharmony_ci qce_xts_swapiv(enciv, rctx->iv, ivsize); 3588c2ecf20Sopenharmony_ci else 3598c2ecf20Sopenharmony_ci qce_cpu_to_be32p_array(enciv, rctx->iv, ivsize); 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ci qce_write_array(qce, REG_CNTR0_IV0, (u32 *)enciv, enciv_words); 3628c2ecf20Sopenharmony_ci } 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci if (IS_ENCRYPT(flags)) 3658c2ecf20Sopenharmony_ci encr_cfg |= BIT(ENCODE_SHIFT); 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci qce_write(qce, REG_ENCR_SEG_CFG, encr_cfg); 3688c2ecf20Sopenharmony_ci qce_write(qce, REG_ENCR_SEG_SIZE, rctx->cryptlen); 3698c2ecf20Sopenharmony_ci qce_write(qce, REG_ENCR_SEG_START, offset & 0xffff); 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci if (IS_CTR(flags)) { 3728c2ecf20Sopenharmony_ci qce_write(qce, REG_CNTR_MASK, ~0); 3738c2ecf20Sopenharmony_ci qce_write(qce, REG_CNTR_MASK0, ~0); 3748c2ecf20Sopenharmony_ci qce_write(qce, REG_CNTR_MASK1, ~0); 3758c2ecf20Sopenharmony_ci qce_write(qce, REG_CNTR_MASK2, ~0); 3768c2ecf20Sopenharmony_ci } 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci qce_write(qce, REG_SEG_SIZE, totallen); 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci /* get little endianness */ 3818c2ecf20Sopenharmony_ci config = qce_config_reg(qce, 1); 3828c2ecf20Sopenharmony_ci qce_write(qce, REG_CONFIG, config); 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci qce_crypto_go(qce); 3858c2ecf20Sopenharmony_ci 3868c2ecf20Sopenharmony_ci return 0; 3878c2ecf20Sopenharmony_ci} 3888c2ecf20Sopenharmony_ci#endif 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ciint qce_start(struct crypto_async_request *async_req, u32 type, u32 totallen, 3918c2ecf20Sopenharmony_ci u32 offset) 3928c2ecf20Sopenharmony_ci{ 3938c2ecf20Sopenharmony_ci switch (type) { 3948c2ecf20Sopenharmony_ci#ifdef CONFIG_CRYPTO_DEV_QCE_SKCIPHER 3958c2ecf20Sopenharmony_ci case CRYPTO_ALG_TYPE_SKCIPHER: 3968c2ecf20Sopenharmony_ci return qce_setup_regs_skcipher(async_req, totallen, offset); 3978c2ecf20Sopenharmony_ci#endif 3988c2ecf20Sopenharmony_ci#ifdef CONFIG_CRYPTO_DEV_QCE_SHA 3998c2ecf20Sopenharmony_ci case CRYPTO_ALG_TYPE_AHASH: 4008c2ecf20Sopenharmony_ci return qce_setup_regs_ahash(async_req, totallen, offset); 4018c2ecf20Sopenharmony_ci#endif 4028c2ecf20Sopenharmony_ci default: 4038c2ecf20Sopenharmony_ci return -EINVAL; 4048c2ecf20Sopenharmony_ci } 4058c2ecf20Sopenharmony_ci} 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci#define STATUS_ERRORS \ 4088c2ecf20Sopenharmony_ci (BIT(SW_ERR_SHIFT) | BIT(AXI_ERR_SHIFT) | BIT(HSD_ERR_SHIFT)) 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_ciint qce_check_status(struct qce_device *qce, u32 *status) 4118c2ecf20Sopenharmony_ci{ 4128c2ecf20Sopenharmony_ci int ret = 0; 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci *status = qce_read(qce, REG_STATUS); 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci /* 4178c2ecf20Sopenharmony_ci * Don't use result dump status. The operation may not be complete. 4188c2ecf20Sopenharmony_ci * Instead, use the status we just read from device. In case, we need to 4198c2ecf20Sopenharmony_ci * use result_status from result dump the result_status needs to be byte 4208c2ecf20Sopenharmony_ci * swapped, since we set the device to little endian. 4218c2ecf20Sopenharmony_ci */ 4228c2ecf20Sopenharmony_ci if (*status & STATUS_ERRORS || !(*status & BIT(OPERATION_DONE_SHIFT))) 4238c2ecf20Sopenharmony_ci ret = -ENXIO; 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci return ret; 4268c2ecf20Sopenharmony_ci} 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_civoid qce_get_version(struct qce_device *qce, u32 *major, u32 *minor, u32 *step) 4298c2ecf20Sopenharmony_ci{ 4308c2ecf20Sopenharmony_ci u32 val; 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci val = qce_read(qce, REG_VERSION); 4338c2ecf20Sopenharmony_ci *major = (val & CORE_MAJOR_REV_MASK) >> CORE_MAJOR_REV_SHIFT; 4348c2ecf20Sopenharmony_ci *minor = (val & CORE_MINOR_REV_MASK) >> CORE_MINOR_REV_SHIFT; 4358c2ecf20Sopenharmony_ci *step = (val & CORE_STEP_REV_MASK) >> CORE_STEP_REV_SHIFT; 4368c2ecf20Sopenharmony_ci} 437