From nobody Sat Oct 4 11:13:55 2025 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B8CE27A133; Mon, 18 Aug 2025 06:57:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755500241; cv=none; b=BcQ95d37JP4j+R9v4Xf5CVnfqH+DtGf/Sigpc6vR7N6UioqkR7horghe40sMmaqRilcJM68RITohYfxrLgzP+6QeYqttBzslINXXu/IgAQwdEUo9RLW0CP7J9kwqON7D+B3DYZ1obaC8cyF03A428A6dL2f9LFCknmRddQy7fmU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755500241; c=relaxed/simple; bh=2Cw9VI5VxvYaqFG2/ZVeKaqKVO1Yna/O+5zgTQgi1qY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=O+pOusHGwUKpDEdvN43eDQTvTdKjxBKWUAw45MHNyJQQZFG89O8wqIyA5K7U1pZwiqmQO4c2mQgfhYM9hSw21VGSe3+A6PSI+oaDf1UzmHU7fXOTW+BvVsE3NGzcTwhtx3qETSp9toRfZW6QtQtnBYAAByxnnJnLOm+zBHDnLLk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.174]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4c53Rs42tnzvX2B; Mon, 18 Aug 2025 14:57:13 +0800 (CST) Received: from dggemv705-chm.china.huawei.com (unknown [10.3.19.32]) by mail.maildlp.com (Postfix) with ESMTPS id 69C3E140158; Mon, 18 Aug 2025 14:57:16 +0800 (CST) Received: from kwepemq200001.china.huawei.com (7.202.195.16) by dggemv705-chm.china.huawei.com (10.3.19.32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 18 Aug 2025 14:57:16 +0800 Received: from localhost.huawei.com (10.90.31.46) by kwepemq200001.china.huawei.com (7.202.195.16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 18 Aug 2025 14:57:15 +0800 From: Chenghai Huang To: , CC: , , , , , Subject: [PATCH v2 1/3] crypto: hisilicon/zip - support fallback for zip Date: Mon, 18 Aug 2025 14:57:12 +0800 Message-ID: <20250818065714.1916898-2-huangchenghai2@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250818065714.1916898-1-huangchenghai2@huawei.com> References: <20250818065714.1916898-1-huangchenghai2@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: kwepems500002.china.huawei.com (7.221.188.17) To kwepemq200001.china.huawei.com (7.202.195.16) Content-Type: text/plain; charset="utf-8" When the hardware queue resource or memery alloc fail in initialization of acomp_alg, use soft algorithm to complete the work. Signed-off-by: Chenghai Huang Reviewed-by: Longfang Liu --- drivers/crypto/hisilicon/Kconfig | 1 + drivers/crypto/hisilicon/zip/zip_crypto.c | 52 +++++++++++++++++++---- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/drivers/crypto/hisilicon/Kconfig b/drivers/crypto/hisilicon/Kc= onfig index 4137a8bf131f..0d3e32ba6f61 100644 --- a/drivers/crypto/hisilicon/Kconfig +++ b/drivers/crypto/hisilicon/Kconfig @@ -57,6 +57,7 @@ config CRYPTO_DEV_HISI_ZIP depends on UACCE || UACCE=3Dn depends on ACPI select CRYPTO_DEV_HISI_QM + select CRYPTO_DEFLATE help Support for HiSilicon ZIP Driver =20 diff --git a/drivers/crypto/hisilicon/zip/zip_crypto.c b/drivers/crypto/his= ilicon/zip/zip_crypto.c index b97513981a3b..dfc8b59b07ac 100644 --- a/drivers/crypto/hisilicon/zip/zip_crypto.c +++ b/drivers/crypto/hisilicon/zip/zip_crypto.c @@ -82,6 +82,7 @@ struct hisi_zip_sqe_ops { struct hisi_zip_ctx { struct hisi_zip_qp_ctx qp_ctx[HZIP_CTX_Q_NUM]; const struct hisi_zip_sqe_ops *ops; + bool fallback; }; =20 static int sgl_sge_nr_set(const char *val, const struct kernel_param *kp) @@ -108,6 +109,24 @@ static u16 sgl_sge_nr =3D HZIP_SGL_SGE_NR; module_param_cb(sgl_sge_nr, &sgl_sge_nr_ops, &sgl_sge_nr, 0444); MODULE_PARM_DESC(sgl_sge_nr, "Number of sge in sgl(1-255)"); =20 +static int hisi_zip_fallback_do_work(struct acomp_req *acomp_req, bool is_= decompress) +{ + ACOMP_FBREQ_ON_STACK(fbreq, acomp_req); + int ret; + + if (!is_decompress) + ret =3D crypto_acomp_compress(fbreq); + else + ret =3D crypto_acomp_decompress(fbreq); + if (ret) { + pr_err("failed to do fallback work, ret=3D%d\n", ret); + return ret; + } + + acomp_req->dlen =3D fbreq->dlen; + return ret; +} + static struct hisi_zip_req *hisi_zip_create_req(struct hisi_zip_qp_ctx *qp= _ctx, struct acomp_req *req) { @@ -319,10 +338,15 @@ static int hisi_zip_acompress(struct acomp_req *acomp= _req) { struct hisi_zip_ctx *ctx =3D crypto_tfm_ctx(acomp_req->base.tfm); struct hisi_zip_qp_ctx *qp_ctx =3D &ctx->qp_ctx[HZIP_QPC_COMP]; - struct device *dev =3D &qp_ctx->qp->qm->pdev->dev; struct hisi_zip_req *req; + struct device *dev; int ret; =20 + if (ctx->fallback) + return hisi_zip_fallback_do_work(acomp_req, 0); + + dev =3D &qp_ctx->qp->qm->pdev->dev; + req =3D hisi_zip_create_req(qp_ctx, acomp_req); if (IS_ERR(req)) return PTR_ERR(req); @@ -340,10 +364,15 @@ static int hisi_zip_adecompress(struct acomp_req *aco= mp_req) { struct hisi_zip_ctx *ctx =3D crypto_tfm_ctx(acomp_req->base.tfm); struct hisi_zip_qp_ctx *qp_ctx =3D &ctx->qp_ctx[HZIP_QPC_DECOMP]; - struct device *dev =3D &qp_ctx->qp->qm->pdev->dev; struct hisi_zip_req *req; + struct device *dev; int ret; =20 + if (ctx->fallback) + return hisi_zip_fallback_do_work(acomp_req, 1); + + dev =3D &qp_ctx->qp->qm->pdev->dev; + req =3D hisi_zip_create_req(qp_ctx, acomp_req); if (IS_ERR(req)) return PTR_ERR(req); @@ -549,7 +578,7 @@ static int hisi_zip_acomp_init(struct crypto_acomp *tfm) ret =3D hisi_zip_ctx_init(ctx, COMP_NAME_TO_TYPE(alg_name), tfm->base.nod= e); if (ret) { pr_err("failed to init ctx (%d)!\n", ret); - return ret; + goto switch_to_soft; } =20 dev =3D &ctx->qp_ctx[0].qp->qm->pdev->dev; @@ -574,17 +603,21 @@ static int hisi_zip_acomp_init(struct crypto_acomp *t= fm) hisi_zip_release_req_q(ctx); err_ctx_exit: hisi_zip_ctx_exit(ctx); - return ret; +switch_to_soft: + ctx->fallback =3D true; + return 0; } =20 static void hisi_zip_acomp_exit(struct crypto_acomp *tfm) { struct hisi_zip_ctx *ctx =3D crypto_tfm_ctx(&tfm->base); =20 - hisi_zip_set_acomp_cb(ctx, NULL); - hisi_zip_release_sgl_pool(ctx); - hisi_zip_release_req_q(ctx); - hisi_zip_ctx_exit(ctx); + if (!ctx->fallback) { + hisi_zip_set_acomp_cb(ctx, NULL); + hisi_zip_release_sgl_pool(ctx); + hisi_zip_release_req_q(ctx); + hisi_zip_ctx_exit(ctx); + } } =20 static struct acomp_alg hisi_zip_acomp_deflate =3D { @@ -595,7 +628,8 @@ static struct acomp_alg hisi_zip_acomp_deflate =3D { .base =3D { .cra_name =3D "deflate", .cra_driver_name =3D "hisi-deflate-acomp", - .cra_flags =3D CRYPTO_ALG_ASYNC, + .cra_flags =3D CRYPTO_ALG_ASYNC | + CRYPTO_ALG_NEED_FALLBACK, .cra_module =3D THIS_MODULE, .cra_priority =3D HZIP_ALG_PRIORITY, .cra_ctxsize =3D sizeof(struct hisi_zip_ctx), --=20 2.33.0 From nobody Sat Oct 4 11:13:55 2025 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B85F279DDC; Mon, 18 Aug 2025 06:57:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755500242; cv=none; b=scS01knU9jGH54FiBIJG0O9DnuJGMeyP2qHltUHG9zDO6QQ9LgeijdTBTfIZyWUoTIFC7EjmthoyYI2bOp2MA5IPPGz2BtRjkJQyfABdXZehS+Jm6amV424GuQul2A5XqGcdyJEqI+1OjyKiwh74EgBWQxdaCI6aJEnzorD/PHA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755500242; c=relaxed/simple; bh=IBxfXjVVwSqaw92wUvWBUkPEKBBI5AeWSyFFTLRvVeE=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=KVbu4YAg8CZBR8sBVlFL1bgsLzkxfA2vd1b1BDKQJ2cs4bjk0ey/w7qYs13d5m3+JDFdPCB74CIzWjPnSxTDdJfzDCWstiNTQqD2HmCF0vISYg07uCLy3BJ1w9xgGyJKeg0CRaMpj8Hha+wTWaUuzUqST6EM2jV6oCAhbOuc/4o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.163.252]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4c53Mt2LP6z13NCY; Mon, 18 Aug 2025 14:53:46 +0800 (CST) Received: from dggemv706-chm.china.huawei.com (unknown [10.3.19.33]) by mail.maildlp.com (Postfix) with ESMTPS id D7898180B66; Mon, 18 Aug 2025 14:57:16 +0800 (CST) Received: from kwepemq200001.china.huawei.com (7.202.195.16) by dggemv706-chm.china.huawei.com (10.3.19.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 18 Aug 2025 14:57:16 +0800 Received: from localhost.huawei.com (10.90.31.46) by kwepemq200001.china.huawei.com (7.202.195.16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 18 Aug 2025 14:57:15 +0800 From: Chenghai Huang To: , CC: , , , , , Subject: [PATCH v2 2/3] crypto: hisilicon/hpre - support the hpre algorithm fallback Date: Mon, 18 Aug 2025 14:57:13 +0800 Message-ID: <20250818065714.1916898-3-huangchenghai2@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250818065714.1916898-1-huangchenghai2@huawei.com> References: <20250818065714.1916898-1-huangchenghai2@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: kwepems500002.china.huawei.com (7.221.188.17) To kwepemq200001.china.huawei.com (7.202.195.16) Content-Type: text/plain; charset="utf-8" From: Weili Qian There is an upper limit on the number of hardware queues. When all hardware queues are busy, new processes fail to apply for queues. To avoid affecting tasks, support fallback mechanism when hardware queues are unavailable. HPRE driver supports DH algorithm, limited to prime numbers up to 4K. It supports prime numbers larger than 4K via fallback mechanism. Fixes: 90274769cf79 ("crypto: hisilicon/hpre - add 'CURVE25519' algorithm") Signed-off-by: Weili Qian Signed-off-by: Chenghai Huang Reviewed-by: Longfang Liu --- drivers/crypto/hisilicon/hpre/hpre_crypto.c | 314 +++++++++++++++++--- drivers/crypto/hisilicon/qm.c | 4 +- 2 files changed, 269 insertions(+), 49 deletions(-) diff --git a/drivers/crypto/hisilicon/hpre/hpre_crypto.c b/drivers/crypto/h= isilicon/hpre/hpre_crypto.c index 1550c3818383..531cd59adbb6 100644 --- a/drivers/crypto/hisilicon/hpre/hpre_crypto.c +++ b/drivers/crypto/hisilicon/hpre/hpre_crypto.c @@ -94,6 +94,7 @@ struct hpre_dh_ctx { =20 char *g; /* m */ dma_addr_t dma_g; + struct crypto_kpp *soft_tfm; }; =20 struct hpre_ecdh_ctx { @@ -104,6 +105,7 @@ struct hpre_ecdh_ctx { /* low address: x->y */ unsigned char *g; dma_addr_t dma_g; + struct crypto_kpp *soft_tfm; }; =20 struct hpre_curve25519_ctx { @@ -114,6 +116,7 @@ struct hpre_curve25519_ctx { /* gx coordinate */ unsigned char *g; dma_addr_t dma_g; + struct crypto_kpp *soft_tfm; }; =20 struct hpre_ctx { @@ -135,6 +138,7 @@ struct hpre_ctx { unsigned int curve_id; /* for high performance core */ u8 enable_hpcore; + bool fallback; }; =20 struct hpre_asym_request { @@ -224,10 +228,8 @@ static struct hisi_qp *hpre_get_qp_and_start(u8 type) int ret; =20 qp =3D hpre_create_qp(type); - if (!qp) { - pr_err("Can not create hpre qp!\n"); + if (!qp) return ERR_PTR(-ENODEV); - } =20 ret =3D hisi_qm_start_qp(qp, 0); if (ret < 0) { @@ -505,8 +507,10 @@ static int hpre_ctx_init(struct hpre_ctx *ctx, u8 type) int ret; =20 qp =3D hpre_get_qp_and_start(type); - if (IS_ERR(qp)) - return PTR_ERR(qp); + if (IS_ERR(qp)) { + ctx->qp =3D NULL; + return -ENODEV; + } =20 qp->qp_ctx =3D ctx; qp->req_cb =3D hpre_alg_cb; @@ -638,6 +642,48 @@ static int hpre_dh_compute_value(struct kpp_request *r= eq) return ret; } =20 +static struct kpp_request *hpre_dh_prepare_fb_req(struct kpp_request *req) +{ + struct kpp_request *fb_req =3D kpp_request_ctx(req); + struct crypto_kpp *tfm =3D crypto_kpp_reqtfm(req); + struct hpre_ctx *ctx =3D kpp_tfm_ctx(tfm); + + kpp_request_set_tfm(fb_req, ctx->dh.soft_tfm); + kpp_request_set_callback(fb_req, req->base.flags, req->base.complete, req= ->base.data); + kpp_request_set_input(fb_req, req->src, req->src_len); + kpp_request_set_output(fb_req, req->dst, req->dst_len); + + return fb_req; +} + +static int hpre_dh_generate_public_key(struct kpp_request *req) +{ + struct crypto_kpp *tfm =3D crypto_kpp_reqtfm(req); + struct hpre_ctx *ctx =3D kpp_tfm_ctx(tfm); + struct kpp_request *fb_req; + + if (ctx->fallback) { + fb_req =3D hpre_dh_prepare_fb_req(req); + return crypto_kpp_generate_public_key(fb_req); + } + + return hpre_dh_compute_value(req); +} + +static int hpre_dh_compute_shared_secret(struct kpp_request *req) +{ + struct crypto_kpp *tfm =3D crypto_kpp_reqtfm(req); + struct hpre_ctx *ctx =3D kpp_tfm_ctx(tfm); + struct kpp_request *fb_req; + + if (ctx->fallback) { + fb_req =3D hpre_dh_prepare_fb_req(req); + return crypto_kpp_compute_shared_secret(fb_req); + } + + return hpre_dh_compute_value(req); +} + static int hpre_is_dh_params_length_valid(unsigned int key_sz) { #define _HPRE_DH_GRP1 768 @@ -664,13 +710,6 @@ static int hpre_dh_set_params(struct hpre_ctx *ctx, st= ruct dh *params) struct device *dev =3D ctx->dev; unsigned int sz; =20 - if (params->p_size > HPRE_DH_MAX_P_SZ) - return -EINVAL; - - if (hpre_is_dh_params_length_valid(params->p_size << - HPRE_BITS_2_BYTES_SHIFT)) - return -EINVAL; - sz =3D ctx->key_sz =3D params->p_size; ctx->dh.xa_p =3D dma_alloc_coherent(dev, sz << 1, &ctx->dh.dma_xa_p, GFP_KERNEL); @@ -703,6 +742,9 @@ static void hpre_dh_clear_ctx(struct hpre_ctx *ctx, boo= l is_clear_all) struct device *dev =3D ctx->dev; unsigned int sz =3D ctx->key_sz; =20 + if (!ctx->qp) + return; + if (is_clear_all) hisi_qm_stop_qp(ctx->qp); =20 @@ -731,6 +773,13 @@ static int hpre_dh_set_secret(struct crypto_kpp *tfm, = const void *buf, if (crypto_dh_decode_key(buf, len, ¶ms) < 0) return -EINVAL; =20 + if (!ctx->qp) + goto set_soft_secret; + + if (hpre_is_dh_params_length_valid(params.p_size << + HPRE_BITS_2_BYTES_SHIFT)) + goto set_soft_secret; + /* Free old secret if any */ hpre_dh_clear_ctx(ctx, false); =20 @@ -741,27 +790,55 @@ static int hpre_dh_set_secret(struct crypto_kpp *tfm,= const void *buf, memcpy(ctx->dh.xa_p + (ctx->key_sz - params.key_size), params.key, params.key_size); =20 + ctx->fallback =3D false; return 0; =20 err_clear_ctx: hpre_dh_clear_ctx(ctx, false); return ret; +set_soft_secret: + ctx->fallback =3D true; + return crypto_kpp_set_secret(ctx->dh.soft_tfm, buf, len); } =20 static unsigned int hpre_dh_max_size(struct crypto_kpp *tfm) { struct hpre_ctx *ctx =3D kpp_tfm_ctx(tfm); =20 + if (ctx->fallback) + return crypto_kpp_maxsize(ctx->dh.soft_tfm); + return ctx->key_sz; } =20 static int hpre_dh_init_tfm(struct crypto_kpp *tfm) { struct hpre_ctx *ctx =3D kpp_tfm_ctx(tfm); + const char *alg =3D kpp_alg_name(tfm); + unsigned int reqsize; + int ret; =20 - kpp_set_reqsize(tfm, sizeof(struct hpre_asym_request) + hpre_align_pd()); + ctx->dh.soft_tfm =3D crypto_alloc_kpp(alg, 0, CRYPTO_ALG_NEED_FALLBACK); + if (IS_ERR(ctx->dh.soft_tfm)) { + pr_err("Failed to alloc dh tfm!\n"); + return PTR_ERR(ctx->dh.soft_tfm); + } =20 - return hpre_ctx_init(ctx, HPRE_V2_ALG_TYPE); + crypto_kpp_set_flags(ctx->dh.soft_tfm, crypto_kpp_get_flags(tfm)); + + reqsize =3D max(sizeof(struct hpre_asym_request) + hpre_align_pd(), + sizeof(struct kpp_request) + crypto_kpp_reqsize(ctx->dh.soft_tfm)); + kpp_set_reqsize(tfm, reqsize); + + ret =3D hpre_ctx_init(ctx, HPRE_V2_ALG_TYPE); + if (ret && ret !=3D -ENODEV) { + crypto_free_kpp(ctx->dh.soft_tfm); + return ret; + } else if (ret =3D=3D -ENODEV) { + ctx->fallback =3D true; + } + + return 0; } =20 static void hpre_dh_exit_tfm(struct crypto_kpp *tfm) @@ -769,6 +846,7 @@ static void hpre_dh_exit_tfm(struct crypto_kpp *tfm) struct hpre_ctx *ctx =3D kpp_tfm_ctx(tfm); =20 hpre_dh_clear_ctx(ctx, true); + crypto_free_kpp(ctx->dh.soft_tfm); } =20 static void hpre_rsa_drop_leading_zeros(const char **ptr, size_t *len) @@ -808,9 +886,8 @@ static int hpre_rsa_enc(struct akcipher_request *req) struct hpre_sqe *msg =3D &hpre_req->req; int ret; =20 - /* For 512 and 1536 bits key size, use soft tfm instead */ - if (ctx->key_sz =3D=3D HPRE_RSA_512BITS_KSZ || - ctx->key_sz =3D=3D HPRE_RSA_1536BITS_KSZ) { + /* For unsupported key size and unavailable devices, use soft tfm instead= */ + if (ctx->fallback) { akcipher_request_set_tfm(req, ctx->rsa.soft_tfm); ret =3D crypto_akcipher_encrypt(req); akcipher_request_set_tfm(req, tfm); @@ -856,9 +933,8 @@ static int hpre_rsa_dec(struct akcipher_request *req) struct hpre_sqe *msg =3D &hpre_req->req; int ret; =20 - /* For 512 and 1536 bits key size, use soft tfm instead */ - if (ctx->key_sz =3D=3D HPRE_RSA_512BITS_KSZ || - ctx->key_sz =3D=3D HPRE_RSA_1536BITS_KSZ) { + /* For unsupported key size and unavailable devices, use soft tfm instead= */ + if (ctx->fallback) { akcipher_request_set_tfm(req, ctx->rsa.soft_tfm); ret =3D crypto_akcipher_decrypt(req); akcipher_request_set_tfm(req, tfm); @@ -912,8 +988,10 @@ static int hpre_rsa_set_n(struct hpre_ctx *ctx, const = char *value, ctx->key_sz =3D vlen; =20 /* if invalid key size provided, we use software tfm */ - if (!hpre_rsa_key_size_is_support(ctx->key_sz)) + if (!hpre_rsa_key_size_is_support(ctx->key_sz)) { + ctx->fallback =3D true; return 0; + } =20 ctx->rsa.pubkey =3D dma_alloc_coherent(ctx->dev, vlen << 1, &ctx->rsa.dma_pubkey, @@ -1048,6 +1126,9 @@ static void hpre_rsa_clear_ctx(struct hpre_ctx *ctx, = bool is_clear_all) unsigned int half_key_sz =3D ctx->key_sz >> 1; struct device *dev =3D ctx->dev; =20 + if (!ctx->qp) + return; + if (is_clear_all) hisi_qm_stop_qp(ctx->qp); =20 @@ -1130,6 +1211,7 @@ static int hpre_rsa_setkey(struct hpre_ctx *ctx, cons= t void *key, goto free; } =20 + ctx->fallback =3D false; return 0; =20 free: @@ -1147,6 +1229,9 @@ static int hpre_rsa_setpubkey(struct crypto_akcipher = *tfm, const void *key, if (ret) return ret; =20 + if (!ctx->qp) + return 0; + return hpre_rsa_setkey(ctx, key, keylen, false); } =20 @@ -1160,6 +1245,9 @@ static int hpre_rsa_setprivkey(struct crypto_akcipher= *tfm, const void *key, if (ret) return ret; =20 + if (!ctx->qp) + return 0; + return hpre_rsa_setkey(ctx, key, keylen, true); } =20 @@ -1167,9 +1255,8 @@ static unsigned int hpre_rsa_max_size(struct crypto_a= kcipher *tfm) { struct hpre_ctx *ctx =3D akcipher_tfm_ctx(tfm); =20 - /* For 512 and 1536 bits key size, use soft tfm instead */ - if (ctx->key_sz =3D=3D HPRE_RSA_512BITS_KSZ || - ctx->key_sz =3D=3D HPRE_RSA_1536BITS_KSZ) + /* For unsupported key size and unavailable devices, use soft tfm instead= */ + if (ctx->fallback) return crypto_akcipher_maxsize(ctx->rsa.soft_tfm); =20 return ctx->key_sz; @@ -1190,10 +1277,14 @@ static int hpre_rsa_init_tfm(struct crypto_akcipher= *tfm) hpre_align_pd()); =20 ret =3D hpre_ctx_init(ctx, HPRE_V2_ALG_TYPE); - if (ret) + if (ret && ret !=3D -ENODEV) { crypto_free_akcipher(ctx->rsa.soft_tfm); + return ret; + } else if (ret =3D=3D -ENODEV) { + ctx->fallback =3D true; + } =20 - return ret; + return 0; } =20 static void hpre_rsa_exit_tfm(struct crypto_akcipher *tfm) @@ -1407,6 +1498,9 @@ static int hpre_ecdh_set_secret(struct crypto_kpp *tf= m, const void *buf, struct ecdh params; int ret; =20 + if (ctx->fallback) + return crypto_kpp_set_secret(ctx->ecdh.soft_tfm, buf, len); + if (crypto_ecdh_decode_key(buf, len, ¶ms) < 0) { dev_err(dev, "failed to decode ecdh key!\n"); return -EINVAL; @@ -1637,23 +1731,82 @@ static int hpre_ecdh_compute_value(struct kpp_reque= st *req) return ret; } =20 +static int hpre_ecdh_generate_public_key(struct kpp_request *req) +{ + struct crypto_kpp *tfm =3D crypto_kpp_reqtfm(req); + struct hpre_ctx *ctx =3D kpp_tfm_ctx(tfm); + int ret; + + if (ctx->fallback) { + kpp_request_set_tfm(req, ctx->ecdh.soft_tfm); + ret =3D crypto_kpp_generate_public_key(req); + kpp_request_set_tfm(req, tfm); + return ret; + } + + return hpre_ecdh_compute_value(req); +} + +static int hpre_ecdh_compute_shared_secret(struct kpp_request *req) +{ + struct crypto_kpp *tfm =3D crypto_kpp_reqtfm(req); + struct hpre_ctx *ctx =3D kpp_tfm_ctx(tfm); + int ret; + + if (ctx->fallback) { + kpp_request_set_tfm(req, ctx->ecdh.soft_tfm); + ret =3D crypto_kpp_compute_shared_secret(req); + kpp_request_set_tfm(req, tfm); + return ret; + } + + return hpre_ecdh_compute_value(req); +} + static unsigned int hpre_ecdh_max_size(struct crypto_kpp *tfm) { struct hpre_ctx *ctx =3D kpp_tfm_ctx(tfm); =20 + if (ctx->fallback) + return crypto_kpp_maxsize(ctx->ecdh.soft_tfm); + /* max size is the pub_key_size, include x and y */ return ctx->key_sz << 1; } =20 +static int hpre_ecdh_init_tfm(struct crypto_kpp *tfm) +{ + struct hpre_ctx *ctx =3D kpp_tfm_ctx(tfm); + const char *alg =3D kpp_alg_name(tfm); + int ret; + + ret =3D hpre_ctx_init(ctx, HPRE_V3_ECC_ALG_TYPE); + if (!ret) { + kpp_set_reqsize(tfm, sizeof(struct hpre_asym_request) + hpre_align_pd()); + return 0; + } else if (ret && ret !=3D -ENODEV) { + return ret; + } + + ctx->ecdh.soft_tfm =3D crypto_alloc_kpp(alg, 0, CRYPTO_ALG_NEED_FALLBACK); + if (IS_ERR(ctx->ecdh.soft_tfm)) { + pr_err("Failed to alloc %s tfm!\n", alg); + return PTR_ERR(ctx->ecdh.soft_tfm); + } + + crypto_kpp_set_flags(ctx->ecdh.soft_tfm, crypto_kpp_get_flags(tfm)); + ctx->fallback =3D true; + + return 0; +} + static int hpre_ecdh_nist_p192_init_tfm(struct crypto_kpp *tfm) { struct hpre_ctx *ctx =3D kpp_tfm_ctx(tfm); =20 ctx->curve_id =3D ECC_CURVE_NIST_P192; =20 - kpp_set_reqsize(tfm, sizeof(struct hpre_asym_request) + hpre_align_pd()); - - return hpre_ctx_init(ctx, HPRE_V3_ECC_ALG_TYPE); + return hpre_ecdh_init_tfm(tfm); } =20 static int hpre_ecdh_nist_p256_init_tfm(struct crypto_kpp *tfm) @@ -1663,9 +1816,7 @@ static int hpre_ecdh_nist_p256_init_tfm(struct crypto= _kpp *tfm) ctx->curve_id =3D ECC_CURVE_NIST_P256; ctx->enable_hpcore =3D 1; =20 - kpp_set_reqsize(tfm, sizeof(struct hpre_asym_request) + hpre_align_pd()); - - return hpre_ctx_init(ctx, HPRE_V3_ECC_ALG_TYPE); + return hpre_ecdh_init_tfm(tfm); } =20 static int hpre_ecdh_nist_p384_init_tfm(struct crypto_kpp *tfm) @@ -1674,15 +1825,18 @@ static int hpre_ecdh_nist_p384_init_tfm(struct cryp= to_kpp *tfm) =20 ctx->curve_id =3D ECC_CURVE_NIST_P384; =20 - kpp_set_reqsize(tfm, sizeof(struct hpre_asym_request) + hpre_align_pd()); - - return hpre_ctx_init(ctx, HPRE_V3_ECC_ALG_TYPE); + return hpre_ecdh_init_tfm(tfm); } =20 static void hpre_ecdh_exit_tfm(struct crypto_kpp *tfm) { struct hpre_ctx *ctx =3D kpp_tfm_ctx(tfm); =20 + if (ctx->fallback) { + crypto_free_kpp(ctx->ecdh.soft_tfm); + return; + } + hpre_ecc_clear_ctx(ctx, true, true); } =20 @@ -1748,6 +1902,9 @@ static int hpre_curve25519_set_secret(struct crypto_k= pp *tfm, const void *buf, struct device *dev =3D ctx->dev; int ret =3D -EINVAL; =20 + if (ctx->fallback) + return crypto_kpp_set_secret(ctx->curve25519.soft_tfm, buf, len); + if (len !=3D CURVE25519_KEY_SIZE || !crypto_memneq(buf, curve25519_null_point, CURVE25519_KEY_SIZE)) { dev_err(dev, "key is null or key len is not 32bytes!\n"); @@ -1993,26 +2150,83 @@ static int hpre_curve25519_compute_value(struct kpp= _request *req) return ret; } =20 +static int hpre_curve25519_generate_public_key(struct kpp_request *req) +{ + struct crypto_kpp *tfm =3D crypto_kpp_reqtfm(req); + struct hpre_ctx *ctx =3D kpp_tfm_ctx(tfm); + int ret; + + if (ctx->fallback) { + kpp_request_set_tfm(req, ctx->curve25519.soft_tfm); + ret =3D crypto_kpp_generate_public_key(req); + kpp_request_set_tfm(req, tfm); + return ret; + } + + return hpre_curve25519_compute_value(req); +} + +static int hpre_curve25519_compute_shared_secret(struct kpp_request *req) +{ + struct crypto_kpp *tfm =3D crypto_kpp_reqtfm(req); + struct hpre_ctx *ctx =3D kpp_tfm_ctx(tfm); + int ret; + + if (ctx->fallback) { + kpp_request_set_tfm(req, ctx->curve25519.soft_tfm); + ret =3D crypto_kpp_compute_shared_secret(req); + kpp_request_set_tfm(req, tfm); + return ret; + } + + return hpre_curve25519_compute_value(req); +} + static unsigned int hpre_curve25519_max_size(struct crypto_kpp *tfm) { struct hpre_ctx *ctx =3D kpp_tfm_ctx(tfm); =20 + if (ctx->fallback) + return crypto_kpp_maxsize(ctx->curve25519.soft_tfm); + return ctx->key_sz; } =20 static int hpre_curve25519_init_tfm(struct crypto_kpp *tfm) { struct hpre_ctx *ctx =3D kpp_tfm_ctx(tfm); + const char *alg =3D kpp_alg_name(tfm); + int ret; + + ret =3D hpre_ctx_init(ctx, HPRE_V3_ECC_ALG_TYPE); + if (!ret) { + kpp_set_reqsize(tfm, sizeof(struct hpre_asym_request) + hpre_align_pd()); + return 0; + } else if (ret && ret !=3D -ENODEV) { + return ret; + } + + ctx->curve25519.soft_tfm =3D crypto_alloc_kpp(alg, 0, CRYPTO_ALG_NEED_FAL= LBACK); + if (IS_ERR(ctx->curve25519.soft_tfm)) { + pr_err("Failed to alloc curve25519 tfm!\n"); + return PTR_ERR(ctx->curve25519.soft_tfm); + } =20 - kpp_set_reqsize(tfm, sizeof(struct hpre_asym_request) + hpre_align_pd()); + crypto_kpp_set_flags(ctx->curve25519.soft_tfm, crypto_kpp_get_flags(tfm)); + ctx->fallback =3D true; =20 - return hpre_ctx_init(ctx, HPRE_V3_ECC_ALG_TYPE); + return 0; } =20 static void hpre_curve25519_exit_tfm(struct crypto_kpp *tfm) { struct hpre_ctx *ctx =3D kpp_tfm_ctx(tfm); =20 + if (ctx->fallback) { + crypto_free_kpp(ctx->curve25519.soft_tfm); + return; + } + hpre_ecc_clear_ctx(ctx, true, false); } =20 @@ -2030,13 +2244,14 @@ static struct akcipher_alg rsa =3D { .cra_name =3D "rsa", .cra_driver_name =3D "hpre-rsa", .cra_module =3D THIS_MODULE, + .cra_flags =3D CRYPTO_ALG_NEED_FALLBACK, }, }; =20 static struct kpp_alg dh =3D { .set_secret =3D hpre_dh_set_secret, - .generate_public_key =3D hpre_dh_compute_value, - .compute_shared_secret =3D hpre_dh_compute_value, + .generate_public_key =3D hpre_dh_generate_public_key, + .compute_shared_secret =3D hpre_dh_compute_shared_secret, .max_size =3D hpre_dh_max_size, .init =3D hpre_dh_init_tfm, .exit =3D hpre_dh_exit_tfm, @@ -2046,14 +2261,15 @@ static struct kpp_alg dh =3D { .cra_name =3D "dh", .cra_driver_name =3D "hpre-dh", .cra_module =3D THIS_MODULE, + .cra_flags =3D CRYPTO_ALG_NEED_FALLBACK, }, }; =20 static struct kpp_alg ecdh_curves[] =3D { { .set_secret =3D hpre_ecdh_set_secret, - .generate_public_key =3D hpre_ecdh_compute_value, - .compute_shared_secret =3D hpre_ecdh_compute_value, + .generate_public_key =3D hpre_ecdh_generate_public_key, + .compute_shared_secret =3D hpre_ecdh_compute_shared_secret, .max_size =3D hpre_ecdh_max_size, .init =3D hpre_ecdh_nist_p192_init_tfm, .exit =3D hpre_ecdh_exit_tfm, @@ -2063,11 +2279,12 @@ static struct kpp_alg ecdh_curves[] =3D { .cra_name =3D "ecdh-nist-p192", .cra_driver_name =3D "hpre-ecdh-nist-p192", .cra_module =3D THIS_MODULE, + .cra_flags =3D CRYPTO_ALG_NEED_FALLBACK, }, }, { .set_secret =3D hpre_ecdh_set_secret, - .generate_public_key =3D hpre_ecdh_compute_value, - .compute_shared_secret =3D hpre_ecdh_compute_value, + .generate_public_key =3D hpre_ecdh_generate_public_key, + .compute_shared_secret =3D hpre_ecdh_compute_shared_secret, .max_size =3D hpre_ecdh_max_size, .init =3D hpre_ecdh_nist_p256_init_tfm, .exit =3D hpre_ecdh_exit_tfm, @@ -2077,11 +2294,12 @@ static struct kpp_alg ecdh_curves[] =3D { .cra_name =3D "ecdh-nist-p256", .cra_driver_name =3D "hpre-ecdh-nist-p256", .cra_module =3D THIS_MODULE, + .cra_flags =3D CRYPTO_ALG_NEED_FALLBACK, }, }, { .set_secret =3D hpre_ecdh_set_secret, - .generate_public_key =3D hpre_ecdh_compute_value, - .compute_shared_secret =3D hpre_ecdh_compute_value, + .generate_public_key =3D hpre_ecdh_generate_public_key, + .compute_shared_secret =3D hpre_ecdh_compute_shared_secret, .max_size =3D hpre_ecdh_max_size, .init =3D hpre_ecdh_nist_p384_init_tfm, .exit =3D hpre_ecdh_exit_tfm, @@ -2091,14 +2309,15 @@ static struct kpp_alg ecdh_curves[] =3D { .cra_name =3D "ecdh-nist-p384", .cra_driver_name =3D "hpre-ecdh-nist-p384", .cra_module =3D THIS_MODULE, + .cra_flags =3D CRYPTO_ALG_NEED_FALLBACK, }, } }; =20 static struct kpp_alg curve25519_alg =3D { .set_secret =3D hpre_curve25519_set_secret, - .generate_public_key =3D hpre_curve25519_compute_value, - .compute_shared_secret =3D hpre_curve25519_compute_value, + .generate_public_key =3D hpre_curve25519_generate_public_key, + .compute_shared_secret =3D hpre_curve25519_compute_shared_secret, .max_size =3D hpre_curve25519_max_size, .init =3D hpre_curve25519_init_tfm, .exit =3D hpre_curve25519_exit_tfm, @@ -2108,6 +2327,7 @@ static struct kpp_alg curve25519_alg =3D { .cra_name =3D "curve25519", .cra_driver_name =3D "hpre-curve25519", .cra_module =3D THIS_MODULE, + .cra_flags =3D CRYPTO_ALG_NEED_FALLBACK, }, }; =20 diff --git a/drivers/crypto/hisilicon/qm.c b/drivers/crypto/hisilicon/qm.c index 2e4ee7ecfdfb..8d45febb98dd 100644 --- a/drivers/crypto/hisilicon/qm.c +++ b/drivers/crypto/hisilicon/qm.c @@ -3561,8 +3561,8 @@ int hisi_qm_alloc_qps_node(struct hisi_qm_list *qm_li= st, int qp_num, =20 mutex_unlock(&qm_list->lock); if (ret) - pr_info("Failed to create qps, node[%d], alg[%u], qp[%d]!\n", - node, alg_type, qp_num); + pr_info_ratelimited("Too busy to create qps, node[%d], alg[%u], qp[%d]!\= n", + node, alg_type, qp_num); =20 err: free_list(&head); --=20 2.33.0 From nobody Sat Oct 4 11:13:55 2025 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EA294279DCE; Mon, 18 Aug 2025 06:57:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.188 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755500240; cv=none; b=HthZNCeMAyF3RmR6w1PK7AHahhegXoKqa9r84FsIsi+yVnwg3+swbMqG+p/KYQxYtEJEGo1UfVj6nJLOaeESwCSSKQZ7ZZRixzC6KSXJ2MX4/16j2Gqr2aIL3FLJ1V5XhbV/TfN5lXtowV1EbSX5S5KJvoWa9neNzFwCb/vI/r4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755500240; c=relaxed/simple; bh=VZsuGeZRdPCWNg6T6HQEKhZN8ss8knjbtAqlaoTDdA8=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VB8fFdm+lzFAlN6zR3XhMP1SKXsSQqO5ULOf0xC/FGSsGhkZp/2MDj9kKHLn2JnHONc01eRCZBXFA+Q5IckUJT2l3SyAkTFVoLXI01H2c1mbGJUfI1lR1EeJ73jJPM2LNSl7ypnLsHghxZK2CAgeVjyGTQSMUY54KtOaelKpfwI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.188 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.88.105]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4c53Ld1PpFzFrjQ; Mon, 18 Aug 2025 14:52:41 +0800 (CST) Received: from dggemv706-chm.china.huawei.com (unknown [10.3.19.33]) by mail.maildlp.com (Postfix) with ESMTPS id F0F1C140132; Mon, 18 Aug 2025 14:57:16 +0800 (CST) Received: from kwepemq200001.china.huawei.com (7.202.195.16) by dggemv706-chm.china.huawei.com (10.3.19.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 18 Aug 2025 14:57:16 +0800 Received: from localhost.huawei.com (10.90.31.46) by kwepemq200001.china.huawei.com (7.202.195.16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Mon, 18 Aug 2025 14:57:16 +0800 From: Chenghai Huang To: , CC: , , , , , Subject: [PATCH v2 3/3] crypto: hisilicon/sec2 - support skcipher/aead fallback for hardware queue unavailable Date: Mon, 18 Aug 2025 14:57:14 +0800 Message-ID: <20250818065714.1916898-4-huangchenghai2@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20250818065714.1916898-1-huangchenghai2@huawei.com> References: <20250818065714.1916898-1-huangchenghai2@huawei.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: kwepems500002.china.huawei.com (7.221.188.17) To kwepemq200001.china.huawei.com (7.202.195.16) Content-Type: text/plain; charset="utf-8" From: Qi Tao There is an upper limit on the number of hardware queues. When all hardware queues are busy, new processes fail to apply for queues. To avoid affecting tasks, support fallback mechanism when hardware queues are unavailable. Signed-off-by: Qi Tao Signed-off-by: Chenghai Huang Reviewed-by: Longfang Liu --- drivers/crypto/hisilicon/sec2/sec_crypto.c | 62 ++++++++++++++++------ 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/drivers/crypto/hisilicon/sec2/sec_crypto.c b/drivers/crypto/hi= silicon/sec2/sec_crypto.c index d044ded0f290..8c983c561053 100644 --- a/drivers/crypto/hisilicon/sec2/sec_crypto.c +++ b/drivers/crypto/hisilicon/sec2/sec_crypto.c @@ -669,10 +669,8 @@ static int sec_ctx_base_init(struct sec_ctx *ctx) int i, ret; =20 ctx->qps =3D sec_create_qps(); - if (!ctx->qps) { - pr_err("Can not create sec qps!\n"); + if (!ctx->qps) return -ENODEV; - } =20 sec =3D container_of(ctx->qps[0]->qm, struct sec_dev, qm); ctx->sec =3D sec; @@ -708,6 +706,9 @@ static void sec_ctx_base_uninit(struct sec_ctx *ctx) { int i; =20 + if (!ctx->qps) + return; + for (i =3D 0; i < ctx->sec->ctx_q_num; i++) sec_release_qp_ctx(ctx, &ctx->qp_ctx[i]); =20 @@ -719,6 +720,9 @@ static int sec_cipher_init(struct sec_ctx *ctx) { struct sec_cipher_ctx *c_ctx =3D &ctx->c_ctx; =20 + if (!ctx->qps) + return 0; + c_ctx->c_key =3D dma_alloc_coherent(ctx->dev, SEC_MAX_KEY_SIZE, &c_ctx->c_key_dma, GFP_KERNEL); if (!c_ctx->c_key) @@ -731,6 +735,9 @@ static void sec_cipher_uninit(struct sec_ctx *ctx) { struct sec_cipher_ctx *c_ctx =3D &ctx->c_ctx; =20 + if (!ctx->qps) + return; + memzero_explicit(c_ctx->c_key, SEC_MAX_KEY_SIZE); dma_free_coherent(ctx->dev, SEC_MAX_KEY_SIZE, c_ctx->c_key, c_ctx->c_key_dma); @@ -752,6 +759,9 @@ static void sec_auth_uninit(struct sec_ctx *ctx) { struct sec_auth_ctx *a_ctx =3D &ctx->a_ctx; =20 + if (!ctx->qps) + return; + memzero_explicit(a_ctx->a_key, SEC_MAX_AKEY_SIZE); dma_free_coherent(ctx->dev, SEC_MAX_AKEY_SIZE, a_ctx->a_key, a_ctx->a_key_dma); @@ -789,7 +799,7 @@ static int sec_skcipher_init(struct crypto_skcipher *tf= m) } =20 ret =3D sec_ctx_base_init(ctx); - if (ret) + if (ret && ret !=3D -ENODEV) return ret; =20 ret =3D sec_cipher_init(ctx); @@ -898,6 +908,9 @@ static int sec_skcipher_setkey(struct crypto_skcipher *= tfm, const u8 *key, struct device *dev =3D ctx->dev; int ret; =20 + if (!ctx->qps) + goto set_soft_key; + if (c_mode =3D=3D SEC_CMODE_XTS) { ret =3D xts_verify_key(tfm, key, keylen); if (ret) { @@ -928,13 +941,14 @@ static int sec_skcipher_setkey(struct crypto_skcipher= *tfm, const u8 *key, } =20 memcpy(c_ctx->c_key, key, keylen); - if (c_ctx->fbtfm) { - ret =3D crypto_sync_skcipher_setkey(c_ctx->fbtfm, key, keylen); - if (ret) { - dev_err(dev, "failed to set fallback skcipher key!\n"); - return ret; - } + +set_soft_key: + ret =3D crypto_sync_skcipher_setkey(c_ctx->fbtfm, key, keylen); + if (ret) { + dev_err(dev, "failed to set fallback skcipher key!\n"); + return ret; } + return 0; } =20 @@ -1398,6 +1412,9 @@ static int sec_aead_setkey(struct crypto_aead *tfm, c= onst u8 *key, struct crypto_authenc_keys keys; int ret; =20 + if (!ctx->qps) + return sec_aead_fallback_setkey(a_ctx, tfm, key, keylen); + ctx->a_ctx.a_alg =3D a_alg; ctx->c_ctx.c_alg =3D c_alg; c_ctx->c_mode =3D c_mode; @@ -2057,6 +2074,9 @@ static int sec_skcipher_ctx_init(struct crypto_skciph= er *tfm) if (ret) return ret; =20 + if (!ctx->qps) + return 0; + if (ctx->sec->qm.ver < QM_HW_V3) { ctx->type_supported =3D SEC_BD_TYPE2; ctx->req_op =3D &sec_skcipher_req_ops; @@ -2065,7 +2085,7 @@ static int sec_skcipher_ctx_init(struct crypto_skciph= er *tfm) ctx->req_op =3D &sec_skcipher_req_ops_v3; } =20 - return ret; + return 0; } =20 static void sec_skcipher_ctx_exit(struct crypto_skcipher *tfm) @@ -2133,7 +2153,7 @@ static int sec_aead_ctx_init(struct crypto_aead *tfm,= const char *hash_name) int ret; =20 ret =3D sec_aead_init(tfm); - if (ret) { + if (ret && ret !=3D -ENODEV) { pr_err("hisi_sec2: aead init error!\n"); return ret; } @@ -2175,7 +2195,7 @@ static int sec_aead_xcm_ctx_init(struct crypto_aead *= tfm) int ret; =20 ret =3D sec_aead_init(tfm); - if (ret) { + if (ret && ret !=3D -ENODEV) { dev_err(ctx->dev, "hisi_sec2: aead xcm init error!\n"); return ret; } @@ -2320,6 +2340,9 @@ static int sec_skcipher_crypto(struct skcipher_reques= t *sk_req, bool encrypt) bool need_fallback =3D false; int ret; =20 + if (!ctx->qps) + goto soft_crypto; + if (!sk_req->cryptlen) { if (ctx->c_ctx.c_mode =3D=3D SEC_CMODE_XTS) return -EINVAL; @@ -2337,9 +2360,12 @@ static int sec_skcipher_crypto(struct skcipher_reque= st *sk_req, bool encrypt) return -EINVAL; =20 if (unlikely(ctx->c_ctx.fallback || need_fallback)) - return sec_skcipher_soft_crypto(ctx, sk_req, encrypt); + goto soft_crypto; =20 return ctx->req_op->process(ctx, req); + +soft_crypto: + return sec_skcipher_soft_crypto(ctx, sk_req, encrypt); } =20 static int sec_skcipher_encrypt(struct skcipher_request *sk_req) @@ -2547,6 +2573,9 @@ static int sec_aead_crypto(struct aead_request *a_req= , bool encrypt) bool need_fallback =3D false; int ret; =20 + if (!ctx->qps) + goto soft_crypto; + req->flag =3D a_req->base.flags; req->aead_req.aead_req =3D a_req; req->c_req.encrypt =3D encrypt; @@ -2557,11 +2586,14 @@ static int sec_aead_crypto(struct aead_request *a_r= eq, bool encrypt) ret =3D sec_aead_param_check(ctx, req, &need_fallback); if (unlikely(ret)) { if (need_fallback) - return sec_aead_soft_crypto(ctx, a_req, encrypt); + goto soft_crypto; return -EINVAL; } =20 return ctx->req_op->process(ctx, req); + +soft_crypto: + return sec_aead_soft_crypto(ctx, a_req, encrypt); } =20 static int sec_aead_encrypt(struct aead_request *a_req) --=20 2.33.0