From patchwork Mon Mar 16 15:00:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Andrey Smirnov X-Patchwork-Id: 197983 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 631E6C0044D for ; Mon, 16 Mar 2020 15:01:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 383402071C for ; Mon, 16 Mar 2020 15:01:04 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hjPfR7YF" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731630AbgCPPBD (ORCPT ); Mon, 16 Mar 2020 11:01:03 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:36290 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731556AbgCPPBD (ORCPT ); Mon, 16 Mar 2020 11:01:03 -0400 Received: by mail-pf1-f193.google.com with SMTP id i13so10096904pfe.3; Mon, 16 Mar 2020 08:01:02 -0700 (PDT) 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=y0Kh2+LS4t0Gn18HgPrQ7Dx9B87FOQXhtsCkrbuPHSE=; b=hjPfR7YFCSspiBssrhLO0118tojUGw3XN/gHbYr6h68xMxP1AIK3OzsjEW+WldfUmX 23ozeQ3xUQ12mypSaeK/7tATUdxjberBMjqtjiSEoDjTnvDzS9XOGlQpyrsuTEogEJlU GzcW9h/CQiK+ryvFs7org0XtQO+tDklbU8HC8N7BskP2fNP9kSftiw4D+RtITDho0CYw yvaxxXbqhDiUD2PBBOdlU3n/iqqiumNGmAgg2BaGFj0mHUxnAr/KpI00n9FhlI5P+a4i HEArAtJqvx3t+8bRz9uj0tfYWPqi+VfYpx0TnkWbbVKISfa6KC9ywwGWlHWxuD4eHBub dimA== 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=y0Kh2+LS4t0Gn18HgPrQ7Dx9B87FOQXhtsCkrbuPHSE=; b=OaVh8hUmM5tPZXxB44RllmLGNA79cyNcVt1iBAz/pqXGkDkbVAor4yOhmkjhD7cv0f 6rlXHXwRYGchEbnUW5NvD6dZ7XwUub97cFeZeThiH2eh3CF4cf1G/UeYdV9apSjVOTNC Dhg+2qXoH2XuMyU1cu7YSImWPrLVj8iGCflF+rrjIP+b1prUEoljj9CWqBfQ6MyQe5kl 5LCZjNbzmZCDDhCOITGtMWBD4+1Xc+sauEPhU3rswUe7lKoWH7UXd/XFOj8hbzWUhObN mBZ2+UYt2xVM6j+zc994yVFnYwzHYO4ZzsYwRnB7iPiURzlH055m9HTEh1Y16mW9LBVX OUXg== X-Gm-Message-State: ANhLgQ2jt2ROtkV3YjiEaboK6vBBPO1sI7+EbE0f1aNcdlA7bK9w+67k v2Bg1zKCVKVuXmg+VUF5FSYJ8nDl X-Google-Smtp-Source: ADFU+vsTTtN/HPWOdFHcvalqUd9XsRBQ4rGiHfxHr+Mlsa53oZIthzk7icK6ml93nCkxEhv4+XNkYg== X-Received: by 2002:a62:5c07:: with SMTP id q7mr67445pfb.200.1584370861806; Mon, 16 Mar 2020 08:01:01 -0700 (PDT) Received: from localhost.localdomain (c-67-165-113-11.hsd1.wa.comcast.net. [67.165.113.11]) by smtp.gmail.com with ESMTPSA id o128sm256354pfg.5.2020.03.16.08.01.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Mar 2020 08:01:00 -0700 (PDT) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , Chris Healy , Lucas Stach , =?utf-8?q?Horia_Geant?= =?utf-8?b?xIM=?= , Herbert Xu , Iuliana Prodan , linux-imx@nxp.com, linux-kernel@vger.kernel.org Subject: [PATCH v8 1/8] crypto: caam - allocate RNG instantiation descriptor with GFP_DMA Date: Mon, 16 Mar 2020 08:00:40 -0700 Message-Id: <20200316150047.30828-2-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200316150047.30828-1-andrew.smirnov@gmail.com> References: <20200316150047.30828-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 Be consistent with the rest of the codebase and use GFP_DMA when allocating memory for a CAAM JR descriptor. Signed-off-by: Andrey Smirnov Cc: Chris Healy Cc: Lucas Stach Cc: Horia Geantă Cc: Herbert Xu Cc: Iuliana Prodan Cc: linux-imx@nxp.com Cc: linux-crypto@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/crypto/caam/ctrl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/crypto/caam/ctrl.c b/drivers/crypto/caam/ctrl.c index 7139366da016..7f7f2960b0cc 100644 --- a/drivers/crypto/caam/ctrl.c +++ b/drivers/crypto/caam/ctrl.c @@ -196,7 +196,7 @@ static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask) u32 *desc, status; int sh_idx, ret = 0; - desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL); + desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL | GFP_DMA); if (!desc) return -ENOMEM; @@ -273,7 +273,7 @@ static int instantiate_rng(struct device *ctrldev, int state_handle_mask, int ret = 0, sh_idx; ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl; - desc = kmalloc(CAAM_CMD_SZ * 7, GFP_KERNEL); + desc = kmalloc(CAAM_CMD_SZ * 7, GFP_KERNEL | GFP_DMA); if (!desc) return -ENOMEM; From patchwork Mon Mar 16 15:00: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: 197979 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 64E94C0044D for ; Mon, 16 Mar 2020 15:01:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3B1EB20767 for ; Mon, 16 Mar 2020 15:01:37 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EHHqpAlE" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731712AbgCPPBH (ORCPT ); Mon, 16 Mar 2020 11:01:07 -0400 Received: from mail-pj1-f66.google.com ([209.85.216.66]:54844 "EHLO mail-pj1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731539AbgCPPBH (ORCPT ); Mon, 16 Mar 2020 11:01:07 -0400 Received: by mail-pj1-f66.google.com with SMTP id np9so1839522pjb.4; Mon, 16 Mar 2020 08:01:06 -0700 (PDT) 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=2ATfd/evJNtMJU02hUmWNanYOGwfjBYhe+ZX93KVWo4=; b=EHHqpAlEmQ7ZBI08E0QY+UdSchMBjEjbZ/EeZ9VgTLbhnT4rSOoVUwNNrROHDzTaF5 b8NY8rWQuAf7XUogvzvPfiQxNxkwbG78pXoEwGD3UrOraWrh2/I79Q4OHyzitn8KW422 pm0ZMTcDgmWDIn95qBD9ck4ym13DP5QUxk2LPJ8XVZjGETU25zRWUJsb71GuZrsxRcs+ 5I9X7CHbe87+qq/LFDhfazz31iI9ITc52oX/j4vn/TV8Y2WOEmJ0fMW1nlD1VAIewf75 EX5V5yNeB9yiBa00T/HE+VC3cbzMGxYGnmELsLrWdxNOwkopFNBJPrdcRqlNQtkgoK/9 o63A== 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=2ATfd/evJNtMJU02hUmWNanYOGwfjBYhe+ZX93KVWo4=; b=PmoutzMQG/CVOyXe10QRq2T9Jbz5GUmE324hRUqgEJBFx1yy8B3/QWEUdRtmRaLB4I DyfQt3m3jWSQnH548PzU+86V4BsiU1vlvMi9RtVZlMDdkuGGhTjlfVqg+UxHui+1o1kL JJ/f/UaQByNLmJXs7pI6bmnWAp3mrLALwKIWosX7O5iDL04BoB4LpFLBkHV+M6/57IKW UXXxZlMTgQMji5Z9dA1w8dv0vrVJhTdq2QGjnzhkN1794oJH1JyX99krNUJQilihYiZ8 SZJxrtg+qaA49tdPDEDI5ovylCUp+K1yoiafmgo+YuEGoZcEBC8YlM3/G2OqBb5xtp/L PsrA== X-Gm-Message-State: ANhLgQ3oKLN9I3vOs+z25Bhir1soz+TgwhFF4CRUusx4c6PCtdOd818j W+3zfbISPBx4LS33RzOHYlT2Xpj1 X-Google-Smtp-Source: ADFU+vsbml5LQOiFqBkGpU6oQqMxdzw8MA7Cwh5UvFvmAnkAxez1rnPnDhjPTgRm3/qNMtGsAqI+BQ== X-Received: by 2002:a17:902:8f8e:: with SMTP id z14mr27402067plo.195.1584370865206; Mon, 16 Mar 2020 08:01:05 -0700 (PDT) Received: from localhost.localdomain (c-67-165-113-11.hsd1.wa.comcast.net. [67.165.113.11]) by smtp.gmail.com with ESMTPSA id o128sm256354pfg.5.2020.03.16.08.01.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Mar 2020 08:01:02 -0700 (PDT) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , =?utf-8?q?Horia_Geant=C4=83?= , Chris Healy , Lucas Stach , Herbert Xu , Iuliana Prodan , linux-kernel@vger.kernel.org, linux-imx@nxp.com Subject: [PATCH v8 2/8] crypto: caam - use struct hwrng's .init for initialization Date: Mon, 16 Mar 2020 08:00:41 -0700 Message-Id: <20200316150047.30828-3-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200316150047.30828-1-andrew.smirnov@gmail.com> References: <20200316150047.30828-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 Reviewed-by: Horia Geantă 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 34cbb4a56f05..69a02ac5de54 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 Mon Mar 16 15:00: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: 197982 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BCDD2C0044D for ; Mon, 16 Mar 2020 15:01:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 7CFC32071C for ; Mon, 16 Mar 2020 15:01:13 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JbwYli8P" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731773AbgCPPBL (ORCPT ); Mon, 16 Mar 2020 11:01:11 -0400 Received: from mail-pg1-f195.google.com ([209.85.215.195]:44229 "EHLO mail-pg1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731754AbgCPPBL (ORCPT ); Mon, 16 Mar 2020 11:01:11 -0400 Received: by mail-pg1-f195.google.com with SMTP id 37so9894068pgm.11; Mon, 16 Mar 2020 08:01:10 -0700 (PDT) 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=MPrOSWSrtPAEMOuF4bEaFRsvrmYiw/O9v/DoKWrsQLE=; b=JbwYli8PzqFdYTJQePcHzbahKJtd0nkhWDma1aA1fH0s7FG0EotH22QWY+b7U/h5tc fCsDswcX2eyeqW1a+EKf9GoCwJ5Dlt5cUaXAkpuk75KAv/cOPK2n/8UcWZEpQyIftkzF MmcPS44qbOdoRKyLZUMgH/LuBTJGnw+wvXyQWqNHPGCnOgAH6Rx41laMicA3E0A0PjWj bp9FXm6ReTlUAkY5DaSihVwF3PrB4XYcyjpVxVUYDhm0Km0zrqZB0StrXi7ct/NFplkL /kug/f9ZfRAkS3SJ+v2cb1afjXQYuXZpHZW46728yfKcCaRreyPYrW9vQc3LLW3DklXt m3hA== 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=MPrOSWSrtPAEMOuF4bEaFRsvrmYiw/O9v/DoKWrsQLE=; b=f9mq185VL8jhM2GXFBBFhmjppSeGl6HUXHjtGLYlVxMLjaJNkbP1rG3AzAyaiafln6 N+K58zx7fWXKmZbRfrloTs8jRwWRCOgi4MXrqHw5mzxkhtaUjiPoaHPGlWH/SjdMuUtH AYsQ/BfXfec0x4c9hjb7WRheEPwO3wbvU+R8g/UlrfRUgWW67sU8x7JC9Q1sPbgF1Iui LUl/Qb2lpHKNz5+51CPnCtm5YCkMbEOnbpmkPwmC4Eblo/W9brqdea9pHRHLabLhdMkk PI3LpEHBC23oXmCOFR7HpfCAcyrTQw0dG7pV0ULy1qDFlaQTgVXQQeHSUDBVTI3braw5 WNlg== X-Gm-Message-State: ANhLgQ0VBbfvDNrhjhNioAUQ0ZR4OGDjeT+VaL6ucI/KL2DPu//rmx2Q oh1/eVtTTsTQp/9Ksb3VNtwJa7uc X-Google-Smtp-Source: ADFU+vtdgycAdmnsDPdTKhjsHfcdNXjwrsCQYDikc4md2en4GNEbUYiiO7lwxRQnYYlMmyq3Xh5ZLw== X-Received: by 2002:aa7:9250:: with SMTP id 16mr10562pfp.205.1584370868842; Mon, 16 Mar 2020 08:01:08 -0700 (PDT) Received: from localhost.localdomain (c-67-165-113-11.hsd1.wa.comcast.net. [67.165.113.11]) by smtp.gmail.com with ESMTPSA id o128sm256354pfg.5.2020.03.16.08.01.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Mar 2020 08:01:07 -0700 (PDT) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , Chris Healy , Lucas Stach , =?utf-8?q?Horia_Geant?= =?utf-8?b?xIM=?= , Herbert Xu , Iuliana Prodan , linux-kernel@vger.kernel.org, linux-imx@nxp.com Subject: [PATCH v8 4/8] crypto: caam - simplify RNG implementation Date: Mon, 16 Mar 2020 08:00:43 -0700 Message-Id: <20200316150047.30828-5-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200316150047.30828-1-andrew.smirnov@gmail.com> References: <20200316150047.30828-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 | 323 ++++++++++++---------------------- 1 file changed, 108 insertions(+), 215 deletions(-) diff --git a/drivers/crypto/caam/caamrng.c b/drivers/crypto/caam/caamrng.c index 753625f2b2c0..b3b92e5944cd 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,26 @@ #include "jr.h" #include "error.h" +#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) + * Length of used descriptors, see caam_init_desc() */ -#define RN_BUF_SIZE (0xffff / L1_CACHE_BYTES * \ - L1_CACHE_BYTES) - -/* 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; -}; +#define CAAM_RNG_DESC_LEN (CAAM_CMD_SZ + \ + CAAM_CMD_SZ + \ + CAAM_CMD_SZ + CAAM_PTR_SZ_MAX) /* 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; + void *desc_async; + void *desc_sync; + struct work_struct worker; + struct kfifo fifo; }; static struct caam_rng_ctx *to_caam_rng_ctx(struct hwrng *r) @@ -84,228 +49,153 @@ static struct caam_rng_ctx *to_caam_rng_ctx(struct hwrng *r) return (struct caam_rng_ctx *)r->priv; } -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: + 1 cmd_sz + caam_ptr_sz */ + 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 != -EINPROGRESS) - 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 != -EINPROGRESS) - 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 == -EINPROGRESS) { + wait_for_completion(done); + err = 0; + } - /* 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_fill_async(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; - } - - print_hex_dump_debug("rng shdesc@: ", DUMP_PREFIX_ADDRESS, 16, 4, - desc, desc_bytes(desc), 1); + 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_async, + &done); + if (len < 0) + return; + + kfifo_dma_in_finish(&ctx->fifo, len); +} - 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_sync, &done); } - append_seq_out_ptr_intlen(desc, bd->addr, RN_BUF_SIZE, 0); + out = kfifo_out(&ctx->fifo, dst, max); + if (kfifo_len(&ctx->fifo) <= CAAM_RNG_FIFO_LEN / 2) + schedule_work(&ctx->worker); - 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; + ctx->desc_sync = devm_kzalloc(ctx->ctrldev, CAAM_RNG_DESC_LEN, + GFP_DMA | GFP_KERNEL); + if (!ctx->desc_sync) + return -ENOMEM; - atomic_set(&bd->empty, BUF_EMPTY); - submit_job(ctx, buf_id == ctx->current_buf); - wait_for_completion(&bd->filled); + ctx->desc_async = devm_kzalloc(ctx->ctrldev, CAAM_RNG_DESC_LEN, + GFP_DMA | GFP_KERNEL); + if (!ctx->desc_async) + 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); @@ -335,15 +225,18 @@ int caam_rng_init(struct device *ctrldev) if (!devres_open_group(ctrldev, caam_rng_init, GFP_KERNEL)) return -ENOMEM; - 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; ctx->rng.read = caam_read; ctx->rng.priv = (unsigned long)ctx; + ctx->rng.quality = 1024; dev_info(ctrldev, "registering rng-caam\n"); From patchwork Mon Mar 16 15:00: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: 197980 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7075EC2BB1D for ; Mon, 16 Mar 2020 15:01:33 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 48BC920767 for ; Mon, 16 Mar 2020 15:01:33 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fwyF7Kt0" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731799AbgCPPBQ (ORCPT ); Mon, 16 Mar 2020 11:01:16 -0400 Received: from mail-pj1-f67.google.com ([209.85.216.67]:51033 "EHLO mail-pj1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731781AbgCPPBN (ORCPT ); Mon, 16 Mar 2020 11:01:13 -0400 Received: by mail-pj1-f67.google.com with SMTP id o23so2735880pjp.0; Mon, 16 Mar 2020 08:01:13 -0700 (PDT) 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=4kfpZWrpoE5n8JP08S8IQsd4cy/AH7i9JnMmCEqutX0=; b=fwyF7Kt0CowLhs2uqr/b27uoCrPuAngqF/uAW33LyjPc/Q5+aULI3590R0C0nVELyP SPTrd+k06bxbFdqRGsJHxpsAAyBvuTtE4l4oER7AZpOletFvxjTzJmo9O509kNIm8+NS mIDpV1Jv/sircGlRxDZyajm9t2vMGy1YuM4oYUrsM85tGYkv/ZsZUgGrCX6mCynJXX52 2j9pFaHzrBNb1LFbSkJVLc+YpjR+eHQrjZyo8jOt4P7bakKp2dQNinGxczkxAqwvISGZ pCsDlivKltZlNseeIvcYdtTJn4YIkJl1p5UljW+R1OajNIgD1jT3sgEhXqP8/jvsP0hH gipg== 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=4kfpZWrpoE5n8JP08S8IQsd4cy/AH7i9JnMmCEqutX0=; b=W6ALCEEiTJfqkQVY/gOj6fHrVo85+D8LvoKI2QZUo7H4yQgrGVQ6o6IDdVU6tM84wS z3BlcbRYj7Isyvr9BZiRMfGeaMlUFlDdyDOmyo0GHCDVpWqqEy9QBud6V3F6KB63CSfi jFf92f8dUwZKhGrCAk2N3uN7f6IuwVifyk9TSxjqrQyIiXC/S+XqDbzusEGw/Rq2Cyw7 9wmS8q2qLPiypHVXa4nemw+FWQMpU+9Z2IKQfixhxjLEQNjjIUXnrHKK60iwfCA5K88X ZZKcFSqMyA7MJ5LV8djQyqmyygUQ9UUd6TWL6c+UvAy7aNDnctPmbhbk6yvAIOCZu7FI U9DQ== X-Gm-Message-State: ANhLgQ0Vxt9qRgrLS2gyOchl+P2FZrY7YhVys9PH4Q5tXIXuu94804yf Qs4WnMVIESY+uNcvWrhC+ALoxEfH X-Google-Smtp-Source: ADFU+vv7Mb877pIIE5+vpncehbvrRG2ZZBqSVEaFXJ4pMft6McCiOQ0GYzIuVb6YYP3Ipsj0kYa8RA== X-Received: by 2002:a17:902:c1cb:: with SMTP id c11mr27658473plc.80.1584370872214; Mon, 16 Mar 2020 08:01:12 -0700 (PDT) Received: from localhost.localdomain (c-67-165-113-11.hsd1.wa.comcast.net. [67.165.113.11]) by smtp.gmail.com with ESMTPSA id o128sm256354pfg.5.2020.03.16.08.01.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Mar 2020 08:01:11 -0700 (PDT) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , Aymen Sghaier , Vipul Kumar , =?utf-8?q?Horia_Geant?= =?utf-8?b?xIM=?= , Chris Healy , Lucas Stach , Herbert Xu , Iuliana Prodan , linux-kernel@vger.kernel.org, linux-imx@nxp.com Subject: [PATCH v8 6/8] crypto: caam - invalidate entropy register during RNG initialization Date: Mon, 16 Mar 2020 08:00:45 -0700 Message-Id: <20200316150047.30828-7-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200316150047.30828-1-andrew.smirnov@gmail.com> References: <20200316150047.30828-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 Reviewed-by: Horia Geantă 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 7f7f2960b0cc..b278471f4013 100644 --- a/drivers/crypto/caam/ctrl.c +++ b/drivers/crypto/caam/ctrl.c @@ -341,8 +341,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 @@ -372,7 +376,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 Mon Mar 16 15:00: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: 197981 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, UNWANTED_LANGUAGE_BODY, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 48B0FC0044D for ; Mon, 16 Mar 2020 15:01:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 16C332073E for ; Mon, 16 Mar 2020 15:01:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="SCifVSsX" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731807AbgCPPBY (ORCPT ); Mon, 16 Mar 2020 11:01:24 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:39527 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731823AbgCPPBS (ORCPT ); Mon, 16 Mar 2020 11:01:18 -0400 Received: by mail-pf1-f193.google.com with SMTP id d25so2081990pfn.6; Mon, 16 Mar 2020 08:01:16 -0700 (PDT) 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=6FSGRqvWIrV1AnR+eNOcmfjvZaVEwGuCJbbdcQTrqeA=; b=SCifVSsXYGYFePDAubNpRPpOOD5WkSnD9w88D/7K+mYZNxj+5Zwm4drhBTnVdWA8HV Sh3abG8AvOaX/Vv5iLPRSXKGUxTKluBGEmJDmxCUNGkwyw0NZ1+LeGioYh2yD9wAwSAB AQ4YzbGplhRBFA2AbhnwKXPq5eTLxmLYA0GvdIrPu3znAFa6qNIkEwelLFz/DwHDeQDf zn8gRXC64aae5ZRQzJoonYy211eEXAOsKObw8cjTBSaW0ZVVMI+yQbx20+l2SRxjWM9S TspP+r9pWqRmAjwKNfRsWvFwgwn6RrjRYs0fdZUD8glo3BBDaZy7ZLs2UCylLclDh2tr KsUQ== 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=6FSGRqvWIrV1AnR+eNOcmfjvZaVEwGuCJbbdcQTrqeA=; b=R4xNfiMV2FFNEFUmhkYqkjgKT/Or17NWtUzZWgx+1Ro1/ugvpwG+2EghYBsCU8vA4O 9FYlTfcOI1NUz8i451LfXb0gSBJO5GKPJZDJIDwMtogcg64kMiLlTgluGWK+LM9PTqfd K0pWhu1giQR53dBpxIHNlHaGYIYwG2KmQMPMTJFejbQXZKVosGHrOoNeMYzuH0FMYbXx XsC+8xHgKvsgC92Cwj2aO9ZrpOuho6W2Shijpi6EeIrQtYl6QhFr7L3LNjR9LUi82l9c AIwx2tuzRm3h36g3wuvDfz0Gs8yB1Iqvy+xqajGW33wHzO8NtnkUy9D+jnN4rJTmBN5+ 3yBg== X-Gm-Message-State: ANhLgQ0buPVgFiROAAfIq87hmiaRsHFL8RqcpMaZrClK9cAySY55Ioec CRrsv6VtcZ303DyK0mcB9t6mKgWm X-Google-Smtp-Source: ADFU+vtR0JV4vkxPY1U/B35BsEtpeY7gjYv1FzdAARr9vqVT1MatpZtC/avVrGNXXNOnr7h9qgQ1ag== X-Received: by 2002:a65:5181:: with SMTP id h1mr323912pgq.62.1584370875444; Mon, 16 Mar 2020 08:01:15 -0700 (PDT) Received: from localhost.localdomain (c-67-165-113-11.hsd1.wa.comcast.net. [67.165.113.11]) by smtp.gmail.com with ESMTPSA id o128sm256354pfg.5.2020.03.16.08.01.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Mar 2020 08:01:14 -0700 (PDT) From: Andrey Smirnov To: linux-crypto@vger.kernel.org Cc: Andrey Smirnov , Chris Healy , Lucas Stach , =?utf-8?q?Horia_Geant?= =?utf-8?b?xIM=?= , Herbert Xu , Iuliana Prodan , linux-kernel@vger.kernel.org, linux-imx@nxp.com Subject: [PATCH v8 8/8] crypto: caam - limit single JD RNG output to maximum of 16 bytes Date: Mon, 16 Mar 2020 08:00:47 -0700 Message-Id: <20200316150047.30828-9-andrew.smirnov@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200316150047.30828-1-andrew.smirnov@gmail.com> References: <20200316150047.30828-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 1024 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 | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/drivers/crypto/caam/caamrng.c b/drivers/crypto/caam/caamrng.c index cffa6604f726..77d048dfe5d0 100644 --- a/drivers/crypto/caam/caamrng.c +++ b/drivers/crypto/caam/caamrng.c @@ -22,9 +22,7 @@ #include "jr.h" #include "error.h" -#define CAAM_RNG_MAX_FIFO_STORE_SIZE U16_MAX - -#define CAAM_RNG_FIFO_LEN SZ_32K /* Must be a multiple of 2 */ +#define CAAM_RNG_MAX_FIFO_STORE_SIZE 16 /* * Length of used descriptors, see caam_init_desc() @@ -65,14 +63,15 @@ static void caam_rng_done(struct device *jrdev, u32 *desc, u32 err, complete(jctx->done); } -static u32 *caam_init_desc(u32 *desc, dma_addr_t dst_dma, int len) +static u32 *caam_init_desc(u32 *desc, dma_addr_t dst_dma) { 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 | OP_ALG_PR_ON); /* Store bytes: + 1 cmd_sz + caam_ptr_sz */ - append_fifo_store(desc, dst_dma, len, FIFOST_TYPE_RNGSTORE); + append_fifo_store(desc, dst_dma, + CAAM_RNG_MAX_FIFO_STORE_SIZE, FIFOST_TYPE_RNGSTORE); print_hex_dump_debug("rng job desc@: ", DUMP_PREFIX_ADDRESS, 16, 4, desc, desc_bytes(desc), 1); @@ -92,7 +91,7 @@ static int caam_rng_read_one(struct device *jrdev, .err = &ret, }; - len = min_t(int, len, CAAM_RNG_MAX_FIFO_STORE_SIZE); + len = CAAM_RNG_MAX_FIFO_STORE_SIZE; dst_dma = dma_map_single(jrdev, dst, len, DMA_FROM_DEVICE); if (dma_mapping_error(jrdev, dst_dma)) { @@ -102,7 +101,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_init_desc(desc, dst_dma), caam_rng_done, &jctx); if (err == -EINPROGRESS) { wait_for_completion(done); @@ -122,7 +121,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; @@ -156,7 +155,7 @@ static int caam_read(struct hwrng *rng, void *dst, size_t max, bool wait) } out = kfifo_out(&ctx->fifo, dst, max); - if (kfifo_len(&ctx->fifo) <= CAAM_RNG_FIFO_LEN / 2) + if (kfifo_is_empty(&ctx->fifo)) schedule_work(&ctx->worker); return out; @@ -186,7 +185,8 @@ static int caam_init(struct hwrng *rng) if (!ctx->desc_async) return -ENOMEM; - if (kfifo_alloc(&ctx->fifo, CAAM_RNG_FIFO_LEN, GFP_DMA | GFP_KERNEL)) + if (kfifo_alloc(&ctx->fifo, CAAM_RNG_MAX_FIFO_STORE_SIZE, + GFP_DMA | GFP_KERNEL)) return -ENOMEM; INIT_WORK(&ctx->worker, caam_rng_worker);