From patchwork Wed Jan 8 15:40:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrey Smirnov X-Patchwork-Id: 11323955 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3CC6B1395 for ; Wed, 8 Jan 2020 15:42:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1BED22077B for ; Wed, 8 Jan 2020 15:42:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="knynJ8ba" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729006AbgAHPl6 (ORCPT ); Wed, 8 Jan 2020 10:41:58 -0500 Received: from mail-pg1-f194.google.com ([209.85.215.194]:45565 "EHLO mail-pg1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726363AbgAHPl5 (ORCPT ); Wed, 8 Jan 2020 10:41:57 -0500 Received: by mail-pg1-f194.google.com with SMTP id b9so1747121pgk.12; Wed, 08 Jan 2020 07:41:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TcjeClmRH5R/km2Nzp0idBoWoVhmeZ5+erKgXpUxBBo=; b=knynJ8ba5fZ3R4P8XjkZ1TML44VOpPpxwLv9dGJ0RIRNMWtozzeLvArzdrNnNNAnqb vzyZ0bPXlp+l8BVqd5v3Ywk07VigcGDd15nIS6HCw0lRl2FkVi/GIYB/LQ3jaXBFyNyV rLEd3tHmPfsoLASlv+cmh75bU7LOCJGZsrCKGn8gNjc+gzEPS8JdbfHoezZli8P3a42k lC1EAYDWkzXmprwK2YfWmkSQYL0T8yfk6TAj7Z5nwI/mDMhiF0e/07DIQCdWZbw7I7os cPaK2AUAhDqnZ41308IPuFaO2qEeqj/nuzpKl94NMPjs6lF/qG+KZ+HizBlABMP5ZVDw gagQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TcjeClmRH5R/km2Nzp0idBoWoVhmeZ5+erKgXpUxBBo=; b=k46ht+2W0ezuUX+W4gQLFT7BuoekGyr57tcCStZDlTn3jWEUSTjTy7BvyqEnidfTpv N+Jt2bAnkLyFHJir8CT7YDVkofngLTYEUpQ8U8n8/8c7tG9ctV8UiqdqoCBiNuYzLIeY 7JocIHwBdvP7ElMieG1Lqj+U4MPLPqS9mIskBUlmSAnU+nao+JlT2oI8q/AGh46ePlnC pHcXYnFnR7Toh7zCaibepg5Oie/VEKasGxbzlbHTzhSWDlhP7lQS4qNfP7bSjrw8XcRs cY0g/TecWPYzkqd/tgz5D8ZQIwLvA1Fux60/uWQr77m6tCy8zvvFIb+UlQetD6nmuqm5 NAmA== X-Gm-Message-State: APjAAAUyIuiX+Bf2OvQXjTXERMPIKyioPm7UNEvl4rPq/BK9AMO7v3n6 3ClXHYzxR2AMtwzVwxHv3IF0ddqQ X-Google-Smtp-Source: APXvYqxc7eLrfF3txmj8EG0QDiovMY2NZkrKXm4CAZCmFpdoaeqLTtGGMBP/xOCzhAmUuMtUHy6mvQ== X-Received: by 2002:a63:4b49:: with SMTP id k9mr5812279pgl.269.1578498116399; Wed, 08 Jan 2020 07:41:56 -0800 (PST) Received: from localhost.localdomain (c-67-165-113-11.hsd1.wa.comcast.net. [67.165.113.11]) by smtp.gmail.com with ESMTPSA id e1sm4286640pfl.98.2020.01.08.07.41.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jan 2020 07:41:55 -0800 (PST) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , Chris Healy , Lucas Stach , =?utf-8?q?Horia_Geant=C4=83?= , Herbert Xu , Iuliana Prodan , linux-kernel@vger.kernel.org, linux-imx@nxp.com Subject: [PATCH v6 1/7] crypto: caam - use struct hwrng's .init for initialization Date: Wed, 8 Jan 2020 07:40:41 -0800 Message-Id: <20200108154047.12526-2-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200108154047.12526-1-andrew.smirnov@gmail.com> References: <20200108154047.12526-1-andrew.smirnov@gmail.com> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Make caamrng code a bit more symmetric by moving initialization code to .init hook of struct hwrng. Signed-off-by: Andrey Smirnov Cc: Chris Healy Cc: Lucas Stach Cc: Horia Geantă Cc: Herbert Xu Cc: Iuliana Prodan Cc: linux-crypto@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: linux-imx@nxp.com --- drivers/crypto/caam/caamrng.c | 47 ++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/drivers/crypto/caam/caamrng.c b/drivers/crypto/caam/caamrng.c index e8baacaabe07..1ce7fbd29e85 100644 --- a/drivers/crypto/caam/caamrng.c +++ b/drivers/crypto/caam/caamrng.c @@ -256,6 +256,7 @@ static void caam_cleanup(struct hwrng *rng) } rng_unmap_ctx(rng_ctx); + caam_jr_free(rng_ctx->jrdev); } static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id) @@ -274,28 +275,43 @@ static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id) return 0; } -static int caam_init_rng(struct caam_rng_ctx *ctx, struct device *jrdev) +static int caam_init(struct hwrng *rng) { + struct caam_rng_ctx *ctx = rng_ctx; int err; - ctx->jrdev = jrdev; + ctx->jrdev = caam_jr_alloc(); + err = PTR_ERR_OR_ZERO(ctx->jrdev); + if (err) { + pr_err("Job Ring Device allocation for transform failed\n"); + return err; + } err = rng_create_sh_desc(ctx); if (err) - return err; + goto free_jrdev; ctx->current_buf = 0; ctx->cur_buf_idx = 0; err = caam_init_buf(ctx, 0); if (err) - return err; + goto free_jrdev; + + err = caam_init_buf(ctx, 1); + if (err) + goto free_jrdev; - return caam_init_buf(ctx, 1); + return 0; + +free_jrdev: + caam_jr_free(ctx->jrdev); + return err; } static struct hwrng caam_rng = { .name = "rng-caam", + .init = caam_init, .cleanup = caam_cleanup, .read = caam_read, }; @@ -305,14 +321,12 @@ void caam_rng_exit(void) if (!init_done) return; - caam_jr_free(rng_ctx->jrdev); hwrng_unregister(&caam_rng); kfree(rng_ctx); } int caam_rng_init(struct device *ctrldev) { - struct device *dev; u32 rng_inst; struct caam_drv_private *priv = dev_get_drvdata(ctrldev); int err; @@ -328,21 +342,11 @@ int caam_rng_init(struct device *ctrldev) if (!rng_inst) return 0; - dev = caam_jr_alloc(); - if (IS_ERR(dev)) { - pr_err("Job Ring Device allocation for transform failed\n"); - return PTR_ERR(dev); - } rng_ctx = kmalloc(sizeof(*rng_ctx), GFP_DMA | GFP_KERNEL); - if (!rng_ctx) { - err = -ENOMEM; - goto free_caam_alloc; - } - err = caam_init_rng(rng_ctx, dev); - if (err) - goto free_rng_ctx; + if (!rng_ctx) + return -ENOMEM; - dev_info(dev, "registering rng-caam\n"); + dev_info(ctrldev, "registering rng-caam\n"); err = hwrng_register(&caam_rng); if (!err) { @@ -350,9 +354,6 @@ int caam_rng_init(struct device *ctrldev) return err; } -free_rng_ctx: kfree(rng_ctx); -free_caam_alloc: - caam_jr_free(dev); return err; } From patchwork Wed Jan 8 15:40:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrey Smirnov X-Patchwork-Id: 11323953 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 92ECA139A for ; Wed, 8 Jan 2020 15:42:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 683642077B for ; Wed, 8 Jan 2020 15:42:22 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YG0xVTOG" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729018AbgAHPmA (ORCPT ); Wed, 8 Jan 2020 10:42:00 -0500 Received: from mail-pj1-f66.google.com ([209.85.216.66]:39300 "EHLO mail-pj1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726363AbgAHPl7 (ORCPT ); Wed, 8 Jan 2020 10:41:59 -0500 Received: by mail-pj1-f66.google.com with SMTP id t101so1250176pjb.4; Wed, 08 Jan 2020 07:41:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mJRKn/O/sOX/kyS1VZ/irmvWoHkJDZeTp8EAvr0/qOg=; b=YG0xVTOGwobn7MkYuigBVuI2P5YBHuxt2pujqDSpd244i/mB3gGqUL4KHrZmovON2B zlXrPoIfjTThPnu14evwkJEn6TcoA/8dKnCvr1GHMPVYczWX6nBq6HvukReNPHPyHed1 mrRlg0zS/7KRigfPEO2cxihRdmWxKW3la0LHFggxDFyZTdO+2FouCfijnc/2Vb6Uif3Y 2iAncq5qQtzwvZ9msCaqvtGR2qNXPy/wdfrHbo0Z1Y3tAATkRmeJceuE+MCucM5vluJI nAEVrAuOeOsDFs6qwYdY6OV1GSFwEe4//rKQwyKUtxr19jWGsuH0dVxfnO17KGMFuYs5 Df4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mJRKn/O/sOX/kyS1VZ/irmvWoHkJDZeTp8EAvr0/qOg=; b=JVpMi4A8clHhFIegycZ8SHHIHJJJm2Ni2XaV96Qvzh+DgTwDkQ1EszJrAIjf1mKcx6 12NYGfFXxlUTleRgcvO9qNUYgEHEXS1L/ihFqChxn0TCs9OCOiBwiP1eLhZuywnnZCpJ GLyQUkXlCwvQI/06pvhrlbvm7mkTWUAAMTWc4+dvc8uUtRYvZTsEtv1M4j59kShDjdkB KqnKlUAw41cxHn/CsBQ9FnbmHMLi0/ngb+UYDB1+yS0o1BVigasVmOGu389E/UUHaVJs 9lvlcJ3IBfq4+WoAuDd76yCNuBJ4z21DtiJzvyoc6iqi84KRYwTkNZrjhPxIeiOuLP61 YkOA== X-Gm-Message-State: APjAAAXIdRRvQ4t+KCJ3Enhjsw2YMHv5SEf1WRA17L5KR/J3KOgaiaT5 dGTJyAOKcC3JfESef2C85GRyUOfR X-Google-Smtp-Source: APXvYqzIEdSREN3lBgaCQc0cjuoW4xFgu91Jmy3nEsRiYSH8iUN95iY+Lz5MGfght77Gg04tXjTk0Q== X-Received: by 2002:a17:90a:b002:: with SMTP id x2mr4937065pjq.38.1578498118271; Wed, 08 Jan 2020 07:41:58 -0800 (PST) Received: from localhost.localdomain (c-67-165-113-11.hsd1.wa.comcast.net. [67.165.113.11]) by smtp.gmail.com with ESMTPSA id e1sm4286640pfl.98.2020.01.08.07.41.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jan 2020 07:41:57 -0800 (PST) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , Chris Healy , Lucas Stach , =?utf-8?q?Horia_Geant=C4=83?= , Herbert Xu , Iuliana Prodan , linux-kernel@vger.kernel.org, linux-imx@nxp.com Subject: [PATCH v6 2/7] crypto: caam - drop global context pointer and init_done Date: Wed, 8 Jan 2020 07:40:42 -0800 Message-Id: <20200108154047.12526-3-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200108154047.12526-1-andrew.smirnov@gmail.com> References: <20200108154047.12526-1-andrew.smirnov@gmail.com> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Leverage devres to get rid of code storing global context as well as init_done flag. Signed-off-by: Andrey Smirnov Cc: Chris Healy Cc: Lucas Stach Cc: Horia Geantă Cc: Herbert Xu Cc: Iuliana Prodan Cc: linux-crypto@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: linux-imx@nxp.com --- drivers/crypto/caam/caamrng.c | 60 ++++++++++++----------------------- drivers/crypto/caam/intern.h | 5 --- drivers/crypto/caam/jr.c | 1 - 3 files changed, 20 insertions(+), 46 deletions(-) diff --git a/drivers/crypto/caam/caamrng.c b/drivers/crypto/caam/caamrng.c index 1ce7fbd29e85..fe187db91233 100644 --- a/drivers/crypto/caam/caamrng.c +++ b/drivers/crypto/caam/caamrng.c @@ -70,6 +70,7 @@ struct buf_data { /* rng per-device context */ struct caam_rng_ctx { + struct hwrng rng; struct device *jrdev; dma_addr_t sh_desc_dma; u32 sh_desc[DESC_RNG_LEN]; @@ -78,13 +79,10 @@ struct caam_rng_ctx { struct buf_data bufs[2]; }; -static struct caam_rng_ctx *rng_ctx; - -/* - * Variable used to avoid double free of resources in case - * algorithm registration was unsuccessful - */ -static bool init_done; +static struct caam_rng_ctx *to_caam_rng_ctx(struct hwrng *r) +{ + return container_of(r, struct caam_rng_ctx, rng); +} static inline void rng_unmap_buf(struct device *jrdev, struct buf_data *bd) { @@ -143,7 +141,7 @@ static inline int submit_job(struct caam_rng_ctx *ctx, int to_current) static int caam_read(struct hwrng *rng, void *data, size_t max, bool wait) { - struct caam_rng_ctx *ctx = rng_ctx; + struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng); struct buf_data *bd = &ctx->bufs[ctx->current_buf]; int next_buf_idx, copied_idx; int err; @@ -246,17 +244,18 @@ static inline int rng_create_job_desc(struct caam_rng_ctx *ctx, int buf_id) static void caam_cleanup(struct hwrng *rng) { + struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng); int i; struct buf_data *bd; for (i = 0; i < 2; i++) { - bd = &rng_ctx->bufs[i]; + bd = &ctx->bufs[i]; if (atomic_read(&bd->empty) == BUF_PENDING) wait_for_completion(&bd->filled); } - rng_unmap_ctx(rng_ctx); - caam_jr_free(rng_ctx->jrdev); + rng_unmap_ctx(ctx); + caam_jr_free(ctx->jrdev); } static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id) @@ -277,7 +276,7 @@ static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id) static int caam_init(struct hwrng *rng) { - struct caam_rng_ctx *ctx = rng_ctx; + struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng); int err; ctx->jrdev = caam_jr_alloc(); @@ -309,28 +308,11 @@ static int caam_init(struct hwrng *rng) return err; } -static struct hwrng caam_rng = { - .name = "rng-caam", - .init = caam_init, - .cleanup = caam_cleanup, - .read = caam_read, -}; - -void caam_rng_exit(void) -{ - if (!init_done) - return; - - hwrng_unregister(&caam_rng); - kfree(rng_ctx); -} - int caam_rng_init(struct device *ctrldev) { + struct caam_rng_ctx *ctx; u32 rng_inst; struct caam_drv_private *priv = dev_get_drvdata(ctrldev); - int err; - init_done = false; /* Check for an instantiated RNG before registration */ if (priv->era < 10) @@ -342,18 +324,16 @@ int caam_rng_init(struct device *ctrldev) if (!rng_inst) return 0; - rng_ctx = kmalloc(sizeof(*rng_ctx), GFP_DMA | GFP_KERNEL); - if (!rng_ctx) + ctx = devm_kzalloc(ctrldev, sizeof(*ctx), GFP_DMA | GFP_KERNEL); + if (!ctx) return -ENOMEM; - dev_info(ctrldev, "registering rng-caam\n"); + ctx->rng.name = "rng-caam"; + ctx->rng.init = caam_init; + ctx->rng.cleanup = caam_cleanup; + ctx->rng.read = caam_read; - err = hwrng_register(&caam_rng); - if (!err) { - init_done = true; - return err; - } + dev_info(ctrldev, "registering rng-caam\n"); - kfree(rng_ctx); - return err; + return devm_hwrng_register(ctrldev, &ctx->rng); } diff --git a/drivers/crypto/caam/intern.h b/drivers/crypto/caam/intern.h index c7c10c90464b..6d64931409eb 100644 --- a/drivers/crypto/caam/intern.h +++ b/drivers/crypto/caam/intern.h @@ -161,7 +161,6 @@ static inline void caam_pkc_exit(void) #ifdef CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API int caam_rng_init(struct device *dev); -void caam_rng_exit(void); #else @@ -170,10 +169,6 @@ static inline int caam_rng_init(struct device *dev) return 0; } -static inline void caam_rng_exit(void) -{ -} - #endif /* CONFIG_CRYPTO_DEV_FSL_CAAM_RNG_API */ #ifdef CONFIG_CAAM_QI diff --git a/drivers/crypto/caam/jr.c b/drivers/crypto/caam/jr.c index fc97cde27059..f15d0d92c031 100644 --- a/drivers/crypto/caam/jr.c +++ b/drivers/crypto/caam/jr.c @@ -53,7 +53,6 @@ static void unregister_algs(void) caam_qi_algapi_exit(); - caam_rng_exit(); caam_pkc_exit(); caam_algapi_hash_exit(); caam_algapi_exit(); From patchwork Wed Jan 8 15:40:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrey Smirnov X-Patchwork-Id: 11323951 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2F6F8138C for ; Wed, 8 Jan 2020 15:42:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E193A2077B for ; Wed, 8 Jan 2020 15:42:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Bahr8uv9" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729027AbgAHPmC (ORCPT ); Wed, 8 Jan 2020 10:42:02 -0500 Received: from mail-pg1-f193.google.com ([209.85.215.193]:47058 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729021AbgAHPmB (ORCPT ); Wed, 8 Jan 2020 10:42:01 -0500 Received: by mail-pg1-f193.google.com with SMTP id z124so1744275pgb.13; Wed, 08 Jan 2020 07:42:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=91RVSAxXkP4uK/WUqhqZP7bqN8+kWT8WkXlcQPyqUfE=; b=Bahr8uv91t+5r5N8Buuzzl27gEG6pW7axHVoZz2xUgQJkouX9j9lMBwSYLtkL40pd2 0mNnk2CQu83eHgPaTt+7ZJAR7N/5vsLzP+3w0txQdX9CORPDwMEYLqz+uY5fBpSQx972 lx/vWPXUdE31iajD4RPQWqn9VGLxE/8eUbHDaJZs1i2JgT7YsGMwbqIsqUDBG38CZz59 qDluI5ELBChQclkqTxklpdUhTi1IOs0LGxgD25WRpdps4QMuHmETfD5IlaLOzKlJW94+ fQQ7j98/MuBxNo1owANCllTDnla5yOHA1vNPcMFaR5okc1QDa4SBGGc8TgduWzDtN/nt +eFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=91RVSAxXkP4uK/WUqhqZP7bqN8+kWT8WkXlcQPyqUfE=; b=fQC2j0bRckcq2u6NY9klzK5zZsOuc3U1LqX2TstPgAhh4LfPh99+fxDpEtLlbTR8hS qk1LT5+hFJDy6NHBmHIszYKF+Ewmn3cYBA606WJrmYfIkOlgdmksfDCYaPvA75PRQ270 /mGiENl8li5OforNKVyk9Y/9kVis7WKw1tbN/PgSI9xOlebZTWUJzutWRxUnZVNVgS/H xrv7bpjFWlpbFR5ujs5BtsxhM/ZRFics4uRK/U+RCD0SUrqFDnDCAcSpD2Vi7u988sAR kAHTs9jL3sSQcboxOCJ94yG8DlbA7F+pymcYvAcHdlfYPNK0C/XD9rBbkVkBGn3cItYw zUTA== X-Gm-Message-State: APjAAAXBRMZQj+2bhDjt2CPX1urxWUjcQoaY3HpAOjUXPTgOGNhtufKZ nHeTHkSnjmEpIXPneehh/KEr8gSD X-Google-Smtp-Source: APXvYqyV3S1ELxd/J9tlWzs8MA+vABtQuK6F3X4eWuobP6GF/sFnE62rMi6TKXcMLkb4RrB1UGnocQ== X-Received: by 2002:a63:c207:: with SMTP id b7mr5915303pgd.422.1578498119681; Wed, 08 Jan 2020 07:41:59 -0800 (PST) Received: from localhost.localdomain (c-67-165-113-11.hsd1.wa.comcast.net. [67.165.113.11]) by smtp.gmail.com with ESMTPSA id e1sm4286640pfl.98.2020.01.08.07.41.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jan 2020 07:41:58 -0800 (PST) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , Chris Healy , Lucas Stach , =?utf-8?q?Horia_Geant=C4=83?= , Herbert Xu , Iuliana Prodan , linux-kernel@vger.kernel.org, linux-imx@nxp.com Subject: [PATCH v6 3/7] crypto: caam - simplify RNG implementation Date: Wed, 8 Jan 2020 07:40:43 -0800 Message-Id: <20200108154047.12526-4-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200108154047.12526-1-andrew.smirnov@gmail.com> References: <20200108154047.12526-1-andrew.smirnov@gmail.com> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Rework CAAM RNG implementation as follows: - Make use of the fact that HWRNG supports partial reads and will handle such cases gracefully by removing recursion in caam_read() - Convert blocking caam_read() codepath to do a single blocking job read directly into requested buffer, bypassing any intermediary buffers - Convert async caam_read() codepath into a simple single reader/single writer FIFO use-case, thus simplifying concurrency handling and delegating buffer read/write position management to KFIFO subsystem. - Leverage the same low level RNG data extraction code for both async and blocking caam_read() scenarios, get rid of the shared job descriptor and make non-shared one as a simple as possible (just HEADER + ALGORITHM OPERATION + FIFO STORE) - Split private context from DMA related memory, so that the former could be allocated without GFP_DMA. NOTE: On its face value this commit decreased throughput numbers reported by dd if=/dev/hwrng of=/dev/null bs=1 count=100K [iflag=nonblock] by about 15%, however commits that enable prediction resistance and limit JR total size impact the performance so much and move the bottleneck such as to make this regression irrelevant. NOTE: On the bright side, this commit reduces RNG in kernel DMA buffer memory usage from 2 x RN_BUF_SIZE (~256K) to 32K. Signed-off-by: Andrey Smirnov Cc: Chris Healy Cc: Lucas Stach Cc: Horia Geantă Cc: Herbert Xu Cc: Iuliana Prodan Cc: linux-crypto@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: linux-imx@nxp.com --- drivers/crypto/caam/caamrng.c | 325 ++++++++++++---------------------- 1 file changed, 112 insertions(+), 213 deletions(-) diff --git a/drivers/crypto/caam/caamrng.c b/drivers/crypto/caam/caamrng.c index fe187db91233..3960f5c81c97 100644 --- a/drivers/crypto/caam/caamrng.c +++ b/drivers/crypto/caam/caamrng.c @@ -7,35 +7,12 @@ * * Based on caamalg.c crypto API driver. * - * relationship between job descriptors to shared descriptors: - * - * --------------- -------------- - * | JobDesc #0 |-------------------->| ShareDesc | - * | *(buffer 0) | |------------->| (generate) | - * --------------- | | (move) | - * | | (store) | - * --------------- | -------------- - * | JobDesc #1 |------| - * | *(buffer 1) | - * --------------- - * - * A job desc looks like this: - * - * --------------------- - * | Header | - * | ShareDesc Pointer | - * | SEQ_OUT_PTR | - * | (output buffer) | - * --------------------- - * - * The SharedDesc never changes, and each job descriptor points to one of two - * buffers for each device, from which the data will be copied into the - * requested destination */ #include #include #include +#include #include "compat.h" @@ -45,38 +22,34 @@ #include "jr.h" #include "error.h" +/* length of descriptors */ +#define CAAM_RNG_MAX_FIFO_STORE_SIZE U16_MAX + +#define CAAM_RNG_FIFO_LEN SZ_32K /* Must be a multiple of 2 */ + /* - * Maximum buffer size: maximum number of random, cache-aligned bytes that - * will be generated and moved to seq out ptr (extlen not allowed) + * See caam_init_desc() */ -#define RN_BUF_SIZE (0xffff / L1_CACHE_BYTES * \ - L1_CACHE_BYTES) +#define CAAM_RNG_DESC_LEN (CAAM_CMD_SZ + \ + CAAM_CMD_SZ + \ + CAAM_CMD_SZ + CAAM_PTR_SZ_MAX) -/* length of descriptors */ -#define DESC_JOB_O_LEN (CAAM_CMD_SZ * 2 + CAAM_PTR_SZ_MAX * 2) -#define DESC_RNG_LEN (3 * CAAM_CMD_SZ) - -/* Buffer, its dma address and lock */ -struct buf_data { - u8 buf[RN_BUF_SIZE] ____cacheline_aligned; - dma_addr_t addr; - struct completion filled; - u32 hw_desc[DESC_JOB_O_LEN]; -#define BUF_NOT_EMPTY 0 -#define BUF_EMPTY 1 -#define BUF_PENDING 2 /* Empty, but with job pending --don't submit another */ - atomic_t empty; +typedef u8 caam_rng_desc[CAAM_RNG_DESC_LEN]; + +enum { + DESC_ASYNC, + DESC_SYNC, + DESC_NUM, }; /* rng per-device context */ struct caam_rng_ctx { struct hwrng rng; struct device *jrdev; - dma_addr_t sh_desc_dma; - u32 sh_desc[DESC_RNG_LEN]; - unsigned int cur_buf_idx; - int current_buf; - struct buf_data bufs[2]; + struct device *ctrldev; + caam_rng_desc *desc; + struct work_struct worker; + struct kfifo fifo; }; static struct caam_rng_ctx *to_caam_rng_ctx(struct hwrng *r) @@ -84,228 +57,152 @@ static struct caam_rng_ctx *to_caam_rng_ctx(struct hwrng *r) return container_of(r, struct caam_rng_ctx, rng); } -static inline void rng_unmap_buf(struct device *jrdev, struct buf_data *bd) +static void caam_rng_done(struct device *jrdev, u32 *desc, u32 err, + void *context) { - if (bd->addr) - dma_unmap_single(jrdev, bd->addr, RN_BUF_SIZE, - DMA_FROM_DEVICE); -} - -static inline void rng_unmap_ctx(struct caam_rng_ctx *ctx) -{ - struct device *jrdev = ctx->jrdev; - - if (ctx->sh_desc_dma) - dma_unmap_single(jrdev, ctx->sh_desc_dma, - desc_bytes(ctx->sh_desc), DMA_TO_DEVICE); - rng_unmap_buf(jrdev, &ctx->bufs[0]); - rng_unmap_buf(jrdev, &ctx->bufs[1]); -} - -static void rng_done(struct device *jrdev, u32 *desc, u32 err, void *context) -{ - struct buf_data *bd; - - bd = container_of(desc, struct buf_data, hw_desc[0]); + struct completion *done = context; if (err) caam_jr_strstatus(jrdev, err); - atomic_set(&bd->empty, BUF_NOT_EMPTY); - complete(&bd->filled); - - /* Buffer refilled, invalidate cache */ - dma_sync_single_for_cpu(jrdev, bd->addr, RN_BUF_SIZE, DMA_FROM_DEVICE); - - print_hex_dump_debug("rng refreshed buf@: ", DUMP_PREFIX_ADDRESS, 16, 4, - bd->buf, RN_BUF_SIZE, 1); + complete(done); } -static inline int submit_job(struct caam_rng_ctx *ctx, int to_current) +static u32 *caam_init_desc(u32 *desc, dma_addr_t dst_dma, int len) { - struct buf_data *bd = &ctx->bufs[!(to_current ^ ctx->current_buf)]; - struct device *jrdev = ctx->jrdev; - u32 *desc = bd->hw_desc; - int err; + init_job_desc(desc, 0); /* + 1 cmd_sz */ + /* Generate random bytes: + 1 cmd_sz */ + append_operation(desc, OP_ALG_ALGSEL_RNG | OP_TYPE_CLASS1_ALG); + /* Store bytes */ + append_fifo_store(desc, dst_dma, len, FIFOST_TYPE_RNGSTORE); - dev_dbg(jrdev, "submitting job %d\n", !(to_current ^ ctx->current_buf)); - init_completion(&bd->filled); - err = caam_jr_enqueue(jrdev, desc, rng_done, ctx); - if (err) - complete(&bd->filled); /* don't wait on failed job*/ - else - atomic_inc(&bd->empty); /* note if pending */ + print_hex_dump_debug("rng job desc@: ", DUMP_PREFIX_ADDRESS, + 16, 4, desc, desc_bytes(desc), 1); - return err; + return desc; } -static int caam_read(struct hwrng *rng, void *data, size_t max, bool wait) +static int caam_rng_read_one(struct device *jrdev, + void *dst, int len, + void *desc, + struct completion *done) { - struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng); - struct buf_data *bd = &ctx->bufs[ctx->current_buf]; - int next_buf_idx, copied_idx; + dma_addr_t dst_dma; int err; - if (atomic_read(&bd->empty)) { - /* try to submit job if there wasn't one */ - if (atomic_read(&bd->empty) == BUF_EMPTY) { - err = submit_job(ctx, 1); - /* if can't submit job, can't even wait */ - if (err) - return 0; - } - /* no immediate data, so exit if not waiting */ - if (!wait) - return 0; - - /* waiting for pending job */ - if (atomic_read(&bd->empty)) - wait_for_completion(&bd->filled); - } - - next_buf_idx = ctx->cur_buf_idx + max; - dev_dbg(ctx->jrdev, "%s: start reading at buffer %d, idx %d\n", - __func__, ctx->current_buf, ctx->cur_buf_idx); + len = min_t(int, len, CAAM_RNG_MAX_FIFO_STORE_SIZE); - /* if enough data in current buffer */ - if (next_buf_idx < RN_BUF_SIZE) { - memcpy(data, bd->buf + ctx->cur_buf_idx, max); - ctx->cur_buf_idx = next_buf_idx; - return max; + dst_dma = dma_map_single(jrdev, dst, len, DMA_FROM_DEVICE); + if (dma_mapping_error(jrdev, dst_dma)) { + dev_err(jrdev, "unable to map destination memory\n"); + return -ENOMEM; } - /* else, copy what's left... */ - copied_idx = RN_BUF_SIZE - ctx->cur_buf_idx; - memcpy(data, bd->buf + ctx->cur_buf_idx, copied_idx); - ctx->cur_buf_idx = 0; - atomic_set(&bd->empty, BUF_EMPTY); - - /* ...refill... */ - submit_job(ctx, 1); + init_completion(done); + err = caam_jr_enqueue(jrdev, + caam_init_desc(desc, dst_dma, len), + caam_rng_done, done); + if (!err) + wait_for_completion(done); - /* and use next buffer */ - ctx->current_buf = !ctx->current_buf; - dev_dbg(ctx->jrdev, "switched to buffer %d\n", ctx->current_buf); + dma_unmap_single(jrdev, dst_dma, len, DMA_FROM_DEVICE); - /* since there already is some data read, don't wait */ - return copied_idx + caam_read(rng, data + copied_idx, - max - copied_idx, false); + return err ?: len; } -static inline int rng_create_sh_desc(struct caam_rng_ctx *ctx) +static void caam_rng_maybe_refill_fifo(struct caam_rng_ctx *ctx) { - struct device *jrdev = ctx->jrdev; - u32 *desc = ctx->sh_desc; - - init_sh_desc(desc, HDR_SHARE_SERIAL); - - /* Generate random bytes */ - append_operation(desc, OP_ALG_ALGSEL_RNG | OP_TYPE_CLASS1_ALG); - - /* Store bytes */ - append_seq_fifo_store(desc, RN_BUF_SIZE, FIFOST_TYPE_RNGSTORE); - - ctx->sh_desc_dma = dma_map_single(jrdev, desc, desc_bytes(desc), - DMA_TO_DEVICE); - if (dma_mapping_error(jrdev, ctx->sh_desc_dma)) { - dev_err(jrdev, "unable to map shared descriptor\n"); - return -ENOMEM; - } + if (kfifo_len(&ctx->fifo) <= CAAM_RNG_FIFO_LEN / 2) + schedule_work(&ctx->worker); +} - print_hex_dump_debug("rng shdesc@: ", DUMP_PREFIX_ADDRESS, 16, 4, - desc, desc_bytes(desc), 1); +static void caam_rng_fill_async(struct caam_rng_ctx *ctx) +{ + struct scatterlist sg[1]; + struct completion done; + int len, nents; + + sg_init_table(sg, ARRAY_SIZE(sg)); + nents = kfifo_dma_in_prepare(&ctx->fifo, sg, ARRAY_SIZE(sg), + CAAM_RNG_FIFO_LEN); + if (!nents) + return; + + len = caam_rng_read_one(ctx->jrdev, sg_virt(&sg[0]), + sg[0].length, + &ctx->desc[DESC_ASYNC], + &done); + if (len < 0) + return; + + kfifo_dma_in_finish(&ctx->fifo, len); + caam_rng_maybe_refill_fifo(ctx); +} - return 0; +static void caam_rng_worker(struct work_struct *work) +{ + struct caam_rng_ctx *ctx = container_of(work, struct caam_rng_ctx, + worker); + caam_rng_fill_async(ctx); } -static inline int rng_create_job_desc(struct caam_rng_ctx *ctx, int buf_id) +static int caam_read(struct hwrng *rng, void *dst, size_t max, bool wait) { - struct device *jrdev = ctx->jrdev; - struct buf_data *bd = &ctx->bufs[buf_id]; - u32 *desc = bd->hw_desc; - int sh_len = desc_len(ctx->sh_desc); + struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng); + int out; - init_job_desc_shared(desc, ctx->sh_desc_dma, sh_len, HDR_SHARE_DEFER | - HDR_REVERSE); + if (wait) { + struct completion done; - bd->addr = dma_map_single(jrdev, bd->buf, RN_BUF_SIZE, DMA_FROM_DEVICE); - if (dma_mapping_error(jrdev, bd->addr)) { - dev_err(jrdev, "unable to map dst\n"); - return -ENOMEM; + return caam_rng_read_one(ctx->jrdev, dst, max, + ctx->desc[DESC_SYNC], &done); } - append_seq_out_ptr_intlen(desc, bd->addr, RN_BUF_SIZE, 0); + out = kfifo_out(&ctx->fifo, dst, max); + caam_rng_maybe_refill_fifo(ctx); - print_hex_dump_debug("rng job desc@: ", DUMP_PREFIX_ADDRESS, 16, 4, - desc, desc_bytes(desc), 1); - - return 0; + return out; } static void caam_cleanup(struct hwrng *rng) { struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng); - int i; - struct buf_data *bd; - for (i = 0; i < 2; i++) { - bd = &ctx->bufs[i]; - if (atomic_read(&bd->empty) == BUF_PENDING) - wait_for_completion(&bd->filled); - } - - rng_unmap_ctx(ctx); + flush_work(&ctx->worker); caam_jr_free(ctx->jrdev); + kfifo_free(&ctx->fifo); } -static int caam_init_buf(struct caam_rng_ctx *ctx, int buf_id) +static int caam_init(struct hwrng *rng) { - struct buf_data *bd = &ctx->bufs[buf_id]; + struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng); int err; - err = rng_create_job_desc(ctx, buf_id); - if (err) - return err; - - atomic_set(&bd->empty, BUF_EMPTY); - submit_job(ctx, buf_id == ctx->current_buf); - wait_for_completion(&bd->filled); + ctx->desc = devm_kcalloc(ctx->ctrldev, DESC_NUM, sizeof(*ctx->desc), + GFP_DMA | GFP_KERNEL); + if (!ctx->desc) + return -ENOMEM; - return 0; -} + if (kfifo_alloc(&ctx->fifo, CAAM_RNG_FIFO_LEN, GFP_DMA | GFP_KERNEL)) + return -ENOMEM; -static int caam_init(struct hwrng *rng) -{ - struct caam_rng_ctx *ctx = to_caam_rng_ctx(rng); - int err; + INIT_WORK(&ctx->worker, caam_rng_worker); ctx->jrdev = caam_jr_alloc(); err = PTR_ERR_OR_ZERO(ctx->jrdev); if (err) { + kfifo_free(&ctx->fifo); pr_err("Job Ring Device allocation for transform failed\n"); return err; } - err = rng_create_sh_desc(ctx); - if (err) - goto free_jrdev; - - ctx->current_buf = 0; - ctx->cur_buf_idx = 0; - - err = caam_init_buf(ctx, 0); - if (err) - goto free_jrdev; - - err = caam_init_buf(ctx, 1); - if (err) - goto free_jrdev; + /* + * Fill async buffer to have early randomness data for + * hw_random + */ + caam_rng_fill_async(ctx); return 0; - -free_jrdev: - caam_jr_free(ctx->jrdev); - return err; } int caam_rng_init(struct device *ctrldev) @@ -324,10 +221,12 @@ int caam_rng_init(struct device *ctrldev) if (!rng_inst) return 0; - ctx = devm_kzalloc(ctrldev, sizeof(*ctx), GFP_DMA | GFP_KERNEL); + ctx = devm_kzalloc(ctrldev, sizeof(*ctx), GFP_KERNEL); if (!ctx) return -ENOMEM; + ctx->ctrldev = ctrldev; + ctx->rng.name = "rng-caam"; ctx->rng.init = caam_init; ctx->rng.cleanup = caam_cleanup; From patchwork Wed Jan 8 15:40:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrey Smirnov X-Patchwork-Id: 11323945 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 03648138C for ; Wed, 8 Jan 2020 15:42:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D5AC32072A for ; Wed, 8 Jan 2020 15:42:07 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YsQN8sZD" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729040AbgAHPmG (ORCPT ); Wed, 8 Jan 2020 10:42:06 -0500 Received: from mail-pf1-f193.google.com ([209.85.210.193]:33595 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726363AbgAHPmC (ORCPT ); Wed, 8 Jan 2020 10:42:02 -0500 Received: by mail-pf1-f193.google.com with SMTP id z16so1830490pfk.0; Wed, 08 Jan 2020 07:42:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=e1VydrddQQ2iYB1DThmBo5L3jMtREdgLznIozdRQZ6Q=; b=YsQN8sZDCsVBLyqVqglFYQX3ipzdNfpAUgQ8dxBhxSuL45z81+BFcKCb3k8LSqqBIy 8yjy0b2ow+l/gtoaQcoU+C7T/8s1DL5R6evI3b6cwef3mYlKGvO6gJkO6FnrblnbiEnh e/h96a3ANkKUIaP1TLQBCH05aR7CvW7/zKAl5z7Cp4xBFeJzwDFKNcuFvJtaAXuVg+Qj zxjfuONcUpwegTVDC2p8PvEr/kCvYuVK1LS8nGnAuQb9NPWx8xHvLDpQj0ATfs0itati nUYljY6+1MAWRv+zoIlZq963wyGuJ234s910bSQ+LuovSBGGQiyM2Pqo0eIJFFAtrhp5 N+Bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=e1VydrddQQ2iYB1DThmBo5L3jMtREdgLznIozdRQZ6Q=; b=U+CsERhGGY+BPZGK3MQIc0kjE+JtXJolijv5pXy//XRXoZTASxH2Yz1O17VDNY1tPI snH53SNFCUvgwCmbMRvHJ5DgNO5NQ2IOO/P3DVkqUK6vwzTuM6b2jFy7EH3egvqog4mW 4zbYQ7ZLb5hBeqW4jMlfW4T+HCP2YqzbB79MCEkf5l3jA4wyKR+HkO0LI1qNIJRjomY8 Zpkd0I69lwihWOxPGPdVx/voCpOXnVvSnp1v3onL1d9BL048qUB4ifpA40cipK7T4zAU B8yrJer+nFMnYjmkQ7pp4V7vZeHXzeT5BoWg4WyQsgTm7SZlL46DTyYslscpnezuaHVb dgTg== X-Gm-Message-State: APjAAAU2I8ejS+6Wkei8RRHhT1wjGMh50LIhaiyllTGJLKaGqQYnyiOA qD390nQyuA8tevj07eC38pdD4D9I X-Google-Smtp-Source: APXvYqx3dfQwyBq6j3FDwObhn/gSdqmirlOoClvmjzVDteEIveTxU2sklT6ftL0+rbntMCSOsgYZ9Q== X-Received: by 2002:a62:1d87:: with SMTP id d129mr5551144pfd.87.1578498121397; Wed, 08 Jan 2020 07:42:01 -0800 (PST) Received: from localhost.localdomain (c-67-165-113-11.hsd1.wa.comcast.net. [67.165.113.11]) by smtp.gmail.com with ESMTPSA id e1sm4286640pfl.98.2020.01.08.07.41.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jan 2020 07:42:00 -0800 (PST) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , Chris Healy , Lucas Stach , =?utf-8?q?Horia_Geant=C4=83?= , Herbert Xu , Iuliana Prodan , linux-kernel@vger.kernel.org, linux-imx@nxp.com Subject: [PATCH v6 4/7] crypto: caam - check if RNG job failed Date: Wed, 8 Jan 2020 07:40:44 -0800 Message-Id: <20200108154047.12526-5-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200108154047.12526-1-andrew.smirnov@gmail.com> References: <20200108154047.12526-1-andrew.smirnov@gmail.com> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org We shouldn't stay silent if RNG job fails. Add appropriate code to check for that case and propagate error code up appropriately. Signed-off-by: Andrey Smirnov Cc: Chris Healy Cc: Lucas Stach Cc: Horia Geantă Cc: Herbert Xu Cc: Iuliana Prodan Cc: linux-crypto@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: linux-imx@nxp.com --- drivers/crypto/caam/caamrng.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/drivers/crypto/caam/caamrng.c b/drivers/crypto/caam/caamrng.c index 3960f5c81c97..554aafbd4d11 100644 --- a/drivers/crypto/caam/caamrng.c +++ b/drivers/crypto/caam/caamrng.c @@ -52,6 +52,11 @@ struct caam_rng_ctx { struct kfifo fifo; }; +struct caam_rng_job_ctx { + struct completion *done; + int *err; +}; + static struct caam_rng_ctx *to_caam_rng_ctx(struct hwrng *r) { return container_of(r, struct caam_rng_ctx, rng); @@ -60,12 +65,12 @@ static struct caam_rng_ctx *to_caam_rng_ctx(struct hwrng *r) static void caam_rng_done(struct device *jrdev, u32 *desc, u32 err, void *context) { - struct completion *done = context; + struct caam_rng_job_ctx *jctx = context; if (err) - caam_jr_strstatus(jrdev, err); + *jctx->err = caam_jr_strstatus(jrdev, err); - complete(done); + complete(jctx->done); } static u32 *caam_init_desc(u32 *desc, dma_addr_t dst_dma, int len) @@ -89,6 +94,10 @@ static int caam_rng_read_one(struct device *jrdev, { dma_addr_t dst_dma; int err; + struct caam_rng_job_ctx jctx = { + .done = done, + .err = &err, + }; len = min_t(int, len, CAAM_RNG_MAX_FIFO_STORE_SIZE); @@ -101,7 +110,7 @@ static int caam_rng_read_one(struct device *jrdev, init_completion(done); err = caam_jr_enqueue(jrdev, caam_init_desc(desc, dst_dma, len), - caam_rng_done, done); + caam_rng_done, &jctx); if (!err) wait_for_completion(done); From patchwork Wed Jan 8 15:40:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrey Smirnov X-Patchwork-Id: 11323957 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D7BB71395 for ; Wed, 8 Jan 2020 15:44:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id B646D2072A for ; Wed, 8 Jan 2020 15:44:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KVEziDG5" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726290AbgAHPoI (ORCPT ); Wed, 8 Jan 2020 10:44:08 -0500 Received: from mail-pf1-f196.google.com ([209.85.210.196]:43018 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726900AbgAHPmE (ORCPT ); Wed, 8 Jan 2020 10:42:04 -0500 Received: by mail-pf1-f196.google.com with SMTP id x6so1804105pfo.10; Wed, 08 Jan 2020 07:42:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=CzFFEei+JwG2EZuAl0ehAfcwGBgiwRJjuVINpXuZnf8=; b=KVEziDG5F7/xs1cRyrwrrAtV0AMGoout0aH231tCyCdZx40Nj3RlcgF+KpzPWkvGAQ UkYG9gSgmEi1q4A1563uJwVy3L/M7EwJBcN+BX3aLWU2XFA3j4v/BjKZId1lZR1wD00D EtEwtVD6lkSe9KjuDhJBhV/VT4voOFCbTWoNusW6+S0d2utwR+RphjiC1xg5JZbiadCp IR8sn3t5ibt47otmhn94/JdcLQFYnkkeBLqN7iXtFlDY41l2+WRI6R54nAkfULufB4DM FE6qZTVMqZTT4AdwcLwiZfjpEFOZ6la9kR0EoD9YaogBwNxDwYFNG6z0Z51vJHrlV5td 5XBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CzFFEei+JwG2EZuAl0ehAfcwGBgiwRJjuVINpXuZnf8=; b=awhIHQlXS82M89Ya2PQtOzfO2s8ctWMPY9J76tAnpHky+y/uGrg35f54rUmGfm1fLD 3rEBRdp+eQy96/XEK6Qse2ZujWe/rhsO0+VdCB5H4J0hmxp6Nch4v59Ezc7oAUsrm7ac Z+mGBbl6O8IiKcd+WC3x4f/6fOBAcHGHCgftfsDohkgU0DdMgzUg8HVdPSgTg2bmwcJ+ pg/z3zrpuefjtscYhB1yQi88X6zQ21qNgfh7YVJ36nPNnuNBM7r7MkPLm0Zd6h3JCB9o pIQgIUGiQdaxTqfgp407vM8TbB1w9pKQChlJNxwc2fKtfLbHqNZ9ypFWem8JDzAXdCiB +D7g== X-Gm-Message-State: APjAAAW0H3GVfUngrdVEEeDPLIMYV/SPR87n1R9BPCU4cYBlZe+FRjg1 wxjQPJ5dfI5MnPwgm1qr/YIiYGot X-Google-Smtp-Source: APXvYqyw7/Tfbz/3km4S6vfphbbp3vGQJ4bGCeD8bUUwGESsP7pEwvYkiyKosoafMjrhlNfLVsytXA== X-Received: by 2002:a62:2cc1:: with SMTP id s184mr5807578pfs.111.1578498123143; Wed, 08 Jan 2020 07:42:03 -0800 (PST) Received: from localhost.localdomain (c-67-165-113-11.hsd1.wa.comcast.net. [67.165.113.11]) by smtp.gmail.com with ESMTPSA id e1sm4286640pfl.98.2020.01.08.07.42.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jan 2020 07:42:02 -0800 (PST) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , Aymen Sghaier , Vipul Kumar , Chris Healy , Lucas Stach , =?utf-8?q?Horia_Geant=C4=83?= , Herbert Xu , Iuliana Prodan , linux-kernel@vger.kernel.org, linux-imx@nxp.com Subject: [PATCH v6 5/7] crypto: caam - invalidate entropy register during RNG initialization Date: Wed, 8 Jan 2020 07:40:45 -0800 Message-Id: <20200108154047.12526-6-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200108154047.12526-1-andrew.smirnov@gmail.com> References: <20200108154047.12526-1-andrew.smirnov@gmail.com> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org In order to make sure that we always use non-stale entropy data, change the code to invalidate entropy register during RNG initialization. Signed-off-by: Aymen Sghaier Signed-off-by: Vipul Kumar [andrew.smirnov@gmail.com ported to upstream kernel, rewrote commit msg] Signed-off-by: Andrey Smirnov Cc: Chris Healy Cc: Lucas Stach Cc: Horia Geantă Cc: Herbert Xu Cc: Iuliana Prodan Cc: linux-crypto@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: linux-imx@nxp.com --- drivers/crypto/caam/ctrl.c | 11 ++++++++--- drivers/crypto/caam/regs.h | 3 ++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/drivers/crypto/caam/ctrl.c b/drivers/crypto/caam/ctrl.c index c99a6a3b22de..22d8676dd610 100644 --- a/drivers/crypto/caam/ctrl.c +++ b/drivers/crypto/caam/ctrl.c @@ -338,8 +338,12 @@ static void kick_trng(struct platform_device *pdev, int ent_delay) ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl; r4tst = &ctrl->r4tst[0]; - /* put RNG4 into program mode */ - clrsetbits_32(&r4tst->rtmctl, 0, RTMCTL_PRGM); + /* + * Setting both RTMCTL:PRGM and RTMCTL:TRNG_ACC causes TRNG to + * properly invalidate the entropy in the entropy register and + * force re-generation. + */ + clrsetbits_32(&r4tst->rtmctl, 0, RTMCTL_PRGM | RTMCTL_ACC); /* * Performance-wise, it does not make sense to @@ -369,7 +373,8 @@ static void kick_trng(struct platform_device *pdev, int ent_delay) * select raw sampling in both entropy shifter * and statistical checker; ; put RNG4 into run mode */ - clrsetbits_32(&r4tst->rtmctl, RTMCTL_PRGM, RTMCTL_SAMP_MODE_RAW_ES_SC); + clrsetbits_32(&r4tst->rtmctl, RTMCTL_PRGM | RTMCTL_ACC, + RTMCTL_SAMP_MODE_RAW_ES_SC); } static int caam_get_era_from_hw(struct caam_ctrl __iomem *ctrl) diff --git a/drivers/crypto/caam/regs.h b/drivers/crypto/caam/regs.h index 05127b70527d..c191e8fd0fa7 100644 --- a/drivers/crypto/caam/regs.h +++ b/drivers/crypto/caam/regs.h @@ -487,7 +487,8 @@ struct rngtst { /* RNG4 TRNG test registers */ struct rng4tst { -#define RTMCTL_PRGM 0x00010000 /* 1 -> program mode, 0 -> run mode */ +#define RTMCTL_ACC BIT(5) /* TRNG access mode */ +#define RTMCTL_PRGM BIT(16) /* 1 -> program mode, 0 -> run mode */ #define RTMCTL_SAMP_MODE_VON_NEUMANN_ES_SC 0 /* use von Neumann data in both entropy shifter and statistical checker */ From patchwork Wed Jan 8 15:40:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrey Smirnov X-Patchwork-Id: 11323949 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 03B8C1395 for ; Wed, 8 Jan 2020 15:42:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D5F362073A for ; Wed, 8 Jan 2020 15:42:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="baGZZ/Rm" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727727AbgAHPmO (ORCPT ); Wed, 8 Jan 2020 10:42:14 -0500 Received: from mail-pl1-f193.google.com ([209.85.214.193]:45962 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729021AbgAHPmG (ORCPT ); Wed, 8 Jan 2020 10:42:06 -0500 Received: by mail-pl1-f193.google.com with SMTP id b22so1271306pls.12; Wed, 08 Jan 2020 07:42:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=a0DGQ1NyXBS1YH7DaQs4FMIy+9goKt2JJcyNuNbw5NM=; b=baGZZ/RmRfsQfYAN9D3ZwCTZ49xzk10YRBistCKAbvK22uWGkHYdQyAGXnp5QvjIwl VQiAVxp7yk2JRNOyddB8rzFk3QIE9HM6VSe5P8QsjDRAB7MWGz5VdHt798pAAI+qMkCV 3pNKk4Q4s9dFinUE21pf2CR5Yd1/s0GQrVMGjfKtzb9lGMloCVzhuWMR4/4Y7bEJ4kfA T85urEyXMiwxXGRwFqQy0mM/ffJyOuCRZcSFHKc65JcCHfQcJxflZ7BZSJ1gBKG5zpya SxYs0K8fAdMf1Q2Y0tavcEXEar0SMkc7LrsLJf4iisT+3tRLY6vMCqXPkeokJH4KTz8n wxHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=a0DGQ1NyXBS1YH7DaQs4FMIy+9goKt2JJcyNuNbw5NM=; b=IJzuv1bb+u4W/avCTnRSyfkrHbHh3fofx5ZlNGhO0FdpYlVK4IZ8UCZZwkfxemVEFc WaziKNLRUIXyfeqHnJVvjg9J6LiJwZeK+rWFcHRCZREYE67gQyBU4dP8HWju6oQJ9sHP 4nsbuKmYm5LKGe1pGK1ZCQgtblFIYWG2hpBVUfrnw568seqecIBcCb7bvbW50Tm7JMkt AwpN67qQwiZetG9PMkuvdLCHf3y7KYokhWJDTolt08U47bigh7Mfk2ullpobWdiKmNCW rLh9+zICu8sUYhoPl69gi2ZNunQlOl8kraAWsifOkdeVXdlIUjHyE725WaZMiJW0jk2f FKxg== X-Gm-Message-State: APjAAAV3eZNMZbqCom8zmWZAonD49D3mOvAn9VzM+9Yp4XLY5QnedKeM 9SGxJXCO275J8CpwnDWuEU6JkA+U X-Google-Smtp-Source: APXvYqxefgu5EZ5v1cP9TxmF+1BBmL5QKj+FOMDj+BcprSrAddOwDfj9ipsGJAfNVVXCQIgCjQK+sg== X-Received: by 2002:a17:902:a40c:: with SMTP id p12mr5911072plq.292.1578498124771; Wed, 08 Jan 2020 07:42:04 -0800 (PST) Received: from localhost.localdomain (c-67-165-113-11.hsd1.wa.comcast.net. [67.165.113.11]) by smtp.gmail.com with ESMTPSA id e1sm4286640pfl.98.2020.01.08.07.42.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jan 2020 07:42:03 -0800 (PST) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , Chris Healy , Lucas Stach , =?utf-8?q?Horia_Geant=C4=83?= , Herbert Xu , Iuliana Prodan , linux-kernel@vger.kernel.org, linux-imx@nxp.com Subject: [PATCH v6 6/7] crypto: caam - enable prediction resistance in HRWNG Date: Wed, 8 Jan 2020 07:40:46 -0800 Message-Id: <20200108154047.12526-7-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200108154047.12526-1-andrew.smirnov@gmail.com> References: <20200108154047.12526-1-andrew.smirnov@gmail.com> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Instantiate CAAM RNG with prediction resistance enabled to improve its quality (with PR on DRNG is forced to reseed from TRNG every time random data is generated). Signed-off-by: Andrey Smirnov Cc: Chris Healy Cc: Lucas Stach Cc: Horia Geantă Cc: Herbert Xu Cc: Iuliana Prodan Cc: linux-crypto@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: linux-imx@nxp.com --- drivers/crypto/caam/caamrng.c | 3 ++- drivers/crypto/caam/ctrl.c | 22 ++++++++++++++++++---- drivers/crypto/caam/desc.h | 2 ++ drivers/crypto/caam/regs.h | 4 +++- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/drivers/crypto/caam/caamrng.c b/drivers/crypto/caam/caamrng.c index 554aafbd4d11..91ccde0240fe 100644 --- a/drivers/crypto/caam/caamrng.c +++ b/drivers/crypto/caam/caamrng.c @@ -77,7 +77,8 @@ static u32 *caam_init_desc(u32 *desc, dma_addr_t dst_dma, int len) { init_job_desc(desc, 0); /* + 1 cmd_sz */ /* Generate random bytes: + 1 cmd_sz */ - append_operation(desc, OP_ALG_ALGSEL_RNG | OP_TYPE_CLASS1_ALG); + append_operation(desc, OP_ALG_ALGSEL_RNG | OP_TYPE_CLASS1_ALG | + OP_ALG_PR_ON); /* Store bytes */ append_fifo_store(desc, dst_dma, len, FIFOST_TYPE_RNGSTORE); diff --git a/drivers/crypto/caam/ctrl.c b/drivers/crypto/caam/ctrl.c index 22d8676dd610..85c2e831839a 100644 --- a/drivers/crypto/caam/ctrl.c +++ b/drivers/crypto/caam/ctrl.c @@ -36,7 +36,8 @@ static void build_instantiation_desc(u32 *desc, int handle, int do_sk) init_job_desc(desc, 0); op_flags = OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG | - (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INIT; + (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INIT | + OP_ALG_PR_ON; /* INIT RNG in non-test mode */ append_operation(desc, op_flags); @@ -275,12 +276,25 @@ static int instantiate_rng(struct device *ctrldev, int state_handle_mask, return -ENOMEM; for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) { + const u32 rdsta_if = RDSTA_IF0 << sh_idx; + const u32 rdsta_pr = RDSTA_PR0 << sh_idx; + const u32 rdsta_mask = rdsta_if | rdsta_pr; /* * If the corresponding bit is set, this state handle * was initialized by somebody else, so it's left alone. */ - if ((1 << sh_idx) & state_handle_mask) - continue; + if (rdsta_if & state_handle_mask) { + if (rdsta_pr & state_handle_mask) + continue; + + dev_info(ctrldev, + "RNG4 SH%d was previously instantiated without prediction resistance. Tearing it down\n", + sh_idx); + + ret = deinstantiate_rng(ctrldev, rdsta_if); + if (ret) + break; + } /* Create the descriptor for instantiating RNG State Handle */ build_instantiation_desc(desc, sh_idx, gen_sk); @@ -302,7 +316,7 @@ static int instantiate_rng(struct device *ctrldev, int state_handle_mask, rdsta_val = rd_reg32(&ctrl->r4tst[0].rdsta) & RDSTA_IFMASK; if ((status && status != JRSTA_SSRC_JUMP_HALT_CC) || - !(rdsta_val & (1 << sh_idx))) { + (rdsta_val & rdsta_mask) != rdsta_mask) { ret = -EAGAIN; break; } diff --git a/drivers/crypto/caam/desc.h b/drivers/crypto/caam/desc.h index 4b6854bf896a..e796d3cb9be8 100644 --- a/drivers/crypto/caam/desc.h +++ b/drivers/crypto/caam/desc.h @@ -1254,6 +1254,8 @@ #define OP_ALG_ICV_OFF (0 << OP_ALG_ICV_SHIFT) #define OP_ALG_ICV_ON (1 << OP_ALG_ICV_SHIFT) +#define OP_ALG_PR_ON BIT(1) + #define OP_ALG_DIR_SHIFT 0 #define OP_ALG_DIR_MASK 1 #define OP_ALG_DECRYPT 0 diff --git a/drivers/crypto/caam/regs.h b/drivers/crypto/caam/regs.h index c191e8fd0fa7..fe1f8c1409fd 100644 --- a/drivers/crypto/caam/regs.h +++ b/drivers/crypto/caam/regs.h @@ -524,9 +524,11 @@ struct rng4tst { u32 rsvd1[40]; #define RDSTA_SKVT 0x80000000 #define RDSTA_SKVN 0x40000000 +#define RDSTA_PR0 BIT(4) +#define RDSTA_PR1 BIT(5) #define RDSTA_IF0 0x00000001 #define RDSTA_IF1 0x00000002 -#define RDSTA_IFMASK (RDSTA_IF1 | RDSTA_IF0) +#define RDSTA_IFMASK (RDSTA_PR1 | RDSTA_PR0 | RDSTA_IF1 | RDSTA_IF0) u32 rdsta; u32 rsvd2[15]; }; From patchwork Wed Jan 8 15:40:47 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrey Smirnov X-Patchwork-Id: 11323947 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D1B5A1395 for ; Wed, 8 Jan 2020 15:42:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id AF1C42072A for ; Wed, 8 Jan 2020 15:42:11 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="X695sjDZ" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729051AbgAHPmI (ORCPT ); Wed, 8 Jan 2020 10:42:08 -0500 Received: from mail-pj1-f67.google.com ([209.85.216.67]:52291 "EHLO mail-pj1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729045AbgAHPmH (ORCPT ); Wed, 8 Jan 2020 10:42:07 -0500 Received: by mail-pj1-f67.google.com with SMTP id a6so1236778pjh.2; Wed, 08 Jan 2020 07:42:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NqcNgcBIdsIjxgPBGKJAVyvsIKyRH4Nc8ZAOSc+D0fE=; b=X695sjDZ3I4ZjRLo6I24zQ/zy2+HMnhKMYzkrip6EpfcvthHq8Ax+KZkMQsZxEeUUU wYJvnt0YXY/nrHPafYHkVWd3Upn3aFRB4M3ZqLt16ZC5jtWFU1b922rTKr3Zp7sFnSU/ XP/vOOnJ13BHHu9QXBXe2w3HKY+QbtdcXqtFZiXmYuKsYt84Wd96oZ/98gX4lrZfqzBz P4WyVoM+o+lAN7Az6Ck9gNUiO4X1lzN2RQ5T3Tbh9KQj4NUq9d0C3ZFwZbKTtWWmgAgB oJTzakpiEr1vCInId1dZ1l709PXO/UG7uEJXgGn2WRxilb4D3HmAAnQb70HdqSiYViZi DpBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NqcNgcBIdsIjxgPBGKJAVyvsIKyRH4Nc8ZAOSc+D0fE=; b=iOWbrqtV3gMXLeRGH+iq+3AvCwWIAtOY1p19vdLIqk0j0SPzLUeQq2WG2kiC1R678t ze3Dnzxda78rkTtg8uh9+qCMR9zswAPp3hjVv7wyGuT8jvHTw2OGDZySNt33I7VJL5im xr9v40K7Jhd68jxs6bB8rubjeQebtJqW+LInKk0YUNj4yr7G15dz/c9Z1l9WWhF9lQ1U JZxdKY9I8RJjOjUtehambq3rvSRRbs7etLEWRJ1oSmYeRyb9zM2MA+ETgI3PyS5/nakg QFRV91CtPNK+p8qtRgUWg1OzohZbiW44NdxR2m0v8/qxNsMjPASX3QzA0u6eClsBQKmm 3TZw== X-Gm-Message-State: APjAAAUBEGOsI/A++eVaDT011snrgGa5KnOKyfF3+b83dBKZ2g229rLS 4lngOTjtwDAQiFAhA74Q6PpK4yzt X-Google-Smtp-Source: APXvYqza+Ai3sj9qXmjB+8LuxnlqvhRuDxggGyIJVthk929D3XxHkipgLVT+kgJlJhcVp0zhz0eDrA== X-Received: by 2002:a17:902:7203:: with SMTP id ba3mr5794754plb.249.1578498126366; Wed, 08 Jan 2020 07:42:06 -0800 (PST) Received: from localhost.localdomain (c-67-165-113-11.hsd1.wa.comcast.net. [67.165.113.11]) by smtp.gmail.com with ESMTPSA id e1sm4286640pfl.98.2020.01.08.07.42.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Jan 2020 07:42:05 -0800 (PST) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , Chris Healy , Lucas Stach , =?utf-8?q?Horia_Geant=C4=83?= , Herbert Xu , Iuliana Prodan , linux-kernel@vger.kernel.org, linux-imx@nxp.com Subject: [PATCH v6 7/7] crypto: caam - limit single JD RNG output to maximum of 16 bytes Date: Wed, 8 Jan 2020 07:40:47 -0800 Message-Id: <20200108154047.12526-8-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200108154047.12526-1-andrew.smirnov@gmail.com> References: <20200108154047.12526-1-andrew.smirnov@gmail.com> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org In order to follow recommendation in SP800-90C (section "9.4 The Oversampling-NRBG Construction") limit the output of "generate" JD submitted to CAAM. See https://lore.kernel.org/linux-crypto/VI1PR0402MB3485EF10976A4A69F90E5B0F98580@VI1PR0402MB3485.eurprd04.prod.outlook.com/ for more details. This change should make CAAM's hwrng driver good enough to have 999 quality rating. Signed-off-by: Andrey Smirnov Cc: Chris Healy Cc: Lucas Stach Cc: Horia Geantă Cc: Herbert Xu Cc: Iuliana Prodan Cc: linux-crypto@vger.kernel.org Cc: linux-kernel@vger.kernel.org Cc: linux-imx@nxp.com --- drivers/crypto/caam/caamrng.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/crypto/caam/caamrng.c b/drivers/crypto/caam/caamrng.c index 91ccde0240fe..2b75ffffcac9 100644 --- a/drivers/crypto/caam/caamrng.c +++ b/drivers/crypto/caam/caamrng.c @@ -23,7 +23,7 @@ #include "error.h" /* length of descriptors */ -#define CAAM_RNG_MAX_FIFO_STORE_SIZE U16_MAX +#define CAAM_RNG_MAX_FIFO_STORE_SIZE 16 #define CAAM_RNG_FIFO_LEN SZ_32K /* Must be a multiple of 2 */ @@ -134,7 +134,7 @@ static void caam_rng_fill_async(struct caam_rng_ctx *ctx) sg_init_table(sg, ARRAY_SIZE(sg)); nents = kfifo_dma_in_prepare(&ctx->fifo, sg, ARRAY_SIZE(sg), - CAAM_RNG_FIFO_LEN); + CAAM_RNG_MAX_FIFO_STORE_SIZE); if (!nents) return; @@ -241,6 +241,7 @@ int caam_rng_init(struct device *ctrldev) ctx->rng.init = caam_init; ctx->rng.cleanup = caam_cleanup; ctx->rng.read = caam_read; + ctx->rng.quality = 999; dev_info(ctrldev, "registering rng-caam\n");