Received: by 2002:a05:7412:f584:b0:e2:908c:2ebd with SMTP id eh4csp1471281rdb; Mon, 4 Sep 2023 15:41:16 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFoAYsDHsRBIggA/8xZnA7KlUEA8rw38tBTceTPtgZL1PrJ70hJAGgXUL0ZIYWEqpp27Jwc X-Received: by 2002:a05:6a20:7d9c:b0:149:602e:9239 with SMTP id v28-20020a056a207d9c00b00149602e9239mr14844190pzj.21.1693867275811; Mon, 04 Sep 2023 15:41:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693867275; cv=none; d=google.com; s=arc-20160816; b=MvvYYpssDk82gu45y+eJoopZ6kAp3SQSzPY3UjvM/4F6CW5pDaKoCoXauI3jwpYPgH BJCvrTnY9dX5/KkeYPHANqZJ5DYyi60zdB8a+Ja87TMWChdU3hlvWd2kwb+lxXgnXV1A fyH3qQZVzoFL8eGl8NjbuN1Zj/Tybx7DOs29iRNdzUu/5Ogdwx7Mjzm3coDFo2cOstAB oFHL8NexEOaQcEoY1vOUH7MF8ljmSBzHwH55G+TsxiAaXNbyAozuVNpdtm2DBKi5Ny15 A8B5lMFb7Mx7o5Q2gWIXxDmhyHW0eSYmRPpibr1DiQXAeHdv/pCWM/vMyJJ4G9cORYr1 bAtw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=QZFBxHDh1/QfVDU6enj3BTF7ofMEWBrKLALc8wjjcPs=; fh=ln0nItm0syOMHsiNignx7CG9Qb6sagium675KTXa4B0=; b=nme12Hs3PpShhbx8kvIqWtSdOFb8MMHBqP1azg1X8K5DAmgAogipkL2yobTkbg31kx iOVSKWTp7rGoVnu6T51klzcBH28sq4mu++/qO64l2hS58aKHjGLHQkOMDFq0wYxKexzk RzTq2gMU7IGpYleHvnAhGN671MyxHK/u+SBVQ2Yf1OTCoBwV14EgqddqdoLOcJUYQgMa IyYAhHAjxwoJ98qszUl4p2XRrZLbSGSBjTTUEFcV4T/csE62Y6xJOvidrRvNL72CmXsl mT50Ih0+h/dOCZFdP7ba06zu35WeYf2ZqkxNpGYbooZ21tQsufTGmDIXTeZ74qfd/hXP OkDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=aAzHA3Zg; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f28-20020a63381c000000b00564514df661si8334338pga.322.2023.09.04.15.41.13; Mon, 04 Sep 2023 15:41:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b=aAzHA3Zg; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343713AbjIANPn (ORCPT + 15 others); Fri, 1 Sep 2023 09:15:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229472AbjIANPn (ORCPT ); Fri, 1 Sep 2023 09:15:43 -0400 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D816E7E; Fri, 1 Sep 2023 06:15:40 -0700 (PDT) Received: from pps.filterd (m0279866.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 381CPxIT020102; Fri, 1 Sep 2023 13:15:25 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=QZFBxHDh1/QfVDU6enj3BTF7ofMEWBrKLALc8wjjcPs=; b=aAzHA3ZgvbHrHyA1kNEmJlTN3xNd0E7OyulzC0hDTl4QPgkirFVCck7YrZoF1SFn2Tu2 cZ75p5vDIQ1jADWiZ+ILU6O/6ZCK/lUW47pksnm3j2TmF0JdzkCJ1O+pxSuS6MForVf6 T7+VAi7XfysoYBz/QJiffTYNQ4y3NgwzVY0za5daaVmE/EILx0uFgfa/5FEqN2WiEmtj 0tNBBaJj+W/4y6Cfz3jbCktHkMfaTD0DcP7YIQ39rczp7FB79TvrTneCMvyJC26e/lBG BbfMz5z9KdBPyfgeNx3zf39mJoJrkrM48GDhcVBXjExweUDKeHe+ZqvUcjBbVmlX8UUp UQ== Received: from nalasppmta02.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3stpy9bxds-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 01 Sep 2023 13:15:24 +0000 Received: from nalasex01b.na.qualcomm.com (nalasex01b.na.qualcomm.com [10.47.209.197]) by NALASPPMTA02.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 381DFNqZ007163 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 1 Sep 2023 13:15:24 GMT Received: from hu-omprsing-hyd.qualcomm.com (10.80.80.8) by nalasex01b.na.qualcomm.com (10.47.209.197) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.36; Fri, 1 Sep 2023 06:15:19 -0700 From: Om Prakash Singh To: CC: , , , , , , , , , , , , , , Subject: [PATCH] crypto: qcom-rng: Add hwrng support Date: Fri, 1 Sep 2023 18:45:02 +0530 Message-ID: <20230901131502.1549809-1-quic_omprsing@quicinc.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [10.80.80.8] X-ClientProxiedBy: nasanex01a.na.qualcomm.com (10.52.223.231) To nalasex01b.na.qualcomm.com (10.47.209.197) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: r3gbnOOYR86C7eLLD5H5c0c5RW3Gcydm X-Proofpoint-GUID: r3gbnOOYR86C7eLLD5H5c0c5RW3Gcydm X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-09-01_10,2023-08-31_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 lowpriorityscore=0 spamscore=0 impostorscore=0 suspectscore=0 adultscore=0 mlxlogscore=999 clxscore=1015 malwarescore=0 phishscore=0 bulkscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2308100000 definitions=main-2309010124 X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org This is follow patch on top of [1] to add hwrng support for newer platform with trng capability. [1] https://lore.kernel.org/lkml/20230824-topic-sm8550-rng-v2-4-dfcafbb16a3e@linaro.org/ Signed-off-by: Om Prakash Singh --- drivers/crypto/qcom-rng.c | 72 ++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/drivers/crypto/qcom-rng.c b/drivers/crypto/qcom-rng.c index fb54b8cfc35f..f78ffdcc66ec 100644 --- a/drivers/crypto/qcom-rng.c +++ b/drivers/crypto/qcom-rng.c @@ -13,6 +13,7 @@ #include #include #include +#include /* Device specific register offsets */ #define PRNG_DATA_OUT 0x0000 @@ -32,13 +33,18 @@ struct qcom_rng { struct mutex lock; void __iomem *base; struct clk *clk; - unsigned int skip_init; + struct qcom_rng_of_data *of_data; }; struct qcom_rng_ctx { struct qcom_rng *rng; }; +struct qcom_rng_of_data { + bool skip_init; + bool hwrng_support; +}; + static struct qcom_rng *qcom_rng_dev; static int qcom_rng_read(struct qcom_rng *rng, u8 *data, unsigned int max) @@ -70,7 +76,7 @@ static int qcom_rng_read(struct qcom_rng *rng, u8 *data, unsigned int max) } } while (currsize < max); - return 0; + return currsize; } static int qcom_rng_generate(struct crypto_rng *tfm, @@ -79,7 +85,8 @@ static int qcom_rng_generate(struct crypto_rng *tfm, { struct qcom_rng_ctx *ctx = crypto_rng_ctx(tfm); struct qcom_rng *rng = ctx->rng; - int ret; + int ret = -EFAULT; + int read_size = 0; ret = clk_prepare_enable(rng->clk); if (ret) @@ -87,11 +94,14 @@ static int qcom_rng_generate(struct crypto_rng *tfm, mutex_lock(&rng->lock); - ret = qcom_rng_read(rng, dstn, dlen); + read_size = qcom_rng_read(rng, dstn, dlen); mutex_unlock(&rng->lock); clk_disable_unprepare(rng->clk); + if (read_size == dlen) + ret = 0; + return ret; } @@ -101,6 +111,16 @@ static int qcom_rng_seed(struct crypto_rng *tfm, const u8 *seed, return 0; } +static int qcom_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait) +{ + int ret; + struct qcom_rng *qrng; + + qrng = (struct qcom_rng *)rng->priv; + ret = qcom_rng_read(qrng, data, max); + return ret; +} + static int qcom_rng_enable(struct qcom_rng *rng) { u32 val; @@ -136,7 +156,7 @@ static int qcom_rng_init(struct crypto_tfm *tfm) ctx->rng = qcom_rng_dev; - if (!ctx->rng->skip_init) + if (!ctx->rng->of_data->skip_init) return qcom_rng_enable(ctx->rng); return 0; @@ -157,9 +177,16 @@ static struct rng_alg qcom_rng_alg = { } }; +static struct hwrng qcom_hwrng = { + .name = "qcom-hwrng", + .read = qcom_hwrng_read, + .quality = 1024, +}; + static int qcom_rng_probe(struct platform_device *pdev) { struct qcom_rng *rng; + const struct qcom_rng_of_data *data; int ret; rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL); @@ -177,7 +204,8 @@ static int qcom_rng_probe(struct platform_device *pdev) if (IS_ERR(rng->clk)) return PTR_ERR(rng->clk); - rng->skip_init = (unsigned long)device_get_match_data(&pdev->dev); + data = of_device_get_match_data(&pdev->dev); + rng->of_data = (struct qcom_rng_of_data *)data; qcom_rng_dev = rng; ret = crypto_register_rng(&qcom_rng_alg); @@ -185,6 +213,14 @@ static int qcom_rng_probe(struct platform_device *pdev) dev_err(&pdev->dev, "Register crypto rng failed: %d\n", ret); qcom_rng_dev = NULL; } + if (rng->of_data->hwrng_support) { + qcom_hwrng.priv = (unsigned long)qcom_rng_dev; + ret = hwrng_register(&qcom_hwrng); + if (ret) { + dev_err(&pdev->dev, "Register hwrng failed: %d\n", ret); + qcom_rng_dev = NULL; + } + } return ret; } @@ -193,11 +229,29 @@ static int qcom_rng_remove(struct platform_device *pdev) { crypto_unregister_rng(&qcom_rng_alg); + if (qcom_rng_dev->of_data->hwrng_support) + hwrng_unregister(&qcom_hwrng); + qcom_rng_dev = NULL; return 0; } +struct qcom_rng_of_data qcom_prng_of_data = { + .skip_init = false, + .hwrng_support = false, +}; + +struct qcom_rng_of_data qcom_prng_ee_of_data = { + .skip_init = true, + .hwrng_support = false, +}; + +struct qcom_rng_of_data qcom_trng_of_data = { + .skip_init = true, + .hwrng_support = true, +}; + static const struct acpi_device_id __maybe_unused qcom_rng_acpi_match[] = { { .id = "QCOM8160", .driver_data = 1 }, {} @@ -205,9 +259,9 @@ static const struct acpi_device_id __maybe_unused qcom_rng_acpi_match[] = { MODULE_DEVICE_TABLE(acpi, qcom_rng_acpi_match); static const struct of_device_id __maybe_unused qcom_rng_of_match[] = { - { .compatible = "qcom,prng", .data = (void *)0}, - { .compatible = "qcom,prng-ee", .data = (void *)1}, - { .compatible = "qcom,trng", .data = (void *)1}, + { .compatible = "qcom,prng", .data = &qcom_prng_of_data }, + { .compatible = "qcom,prng-ee", .data = &qcom_prng_ee_of_data }, + { .compatible = "qcom,trng", .data = &qcom_trng_of_data }, {} }; MODULE_DEVICE_TABLE(of, qcom_rng_of_match); -- 2.25.1