Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp2022581rdh; Tue, 26 Sep 2023 09:59:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFf0JJNpJW5ZpxnVhbdJ8+5K2EciNDPZw/ZlPDb84Q2nLjGIDvd8vWpBz6kxNbALEI0qS7T X-Received: by 2002:a05:6830:459b:b0:6c4:7516:f2cf with SMTP id az27-20020a056830459b00b006c47516f2cfmr9268247otb.2.1695747548934; Tue, 26 Sep 2023 09:59:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695747548; cv=none; d=google.com; s=arc-20160816; b=p09VKEcUpJ2Qpui6ejA9jASUlFhoFFEZjmuGkig75mJIGP0t9hFM/iGG3NAuGLXQqR euFV1p07J7lUYitaezepQ+v8Q9HrMbJCderWyw8jLM1AJ05jp2Qe6kdgcferx+zaQAbJ NIwUZ3JoXEnqqDE+lej0QZrE4XGhykMsjye5w3Xw/FGt3ljR43SvPJlwWPBcGk7l81vE XzsP211cf+lHjvF/TgXkP2GcoJR7CK/Oip491mc7xHsF42SCTmVathWfTLg47jbjGrZz uKnUoq0xt/apPNYoMHgAqwAzpTMkVdDckuRyQkrIFgTPyCto4Y3AhGoDiq6MVw0mopLV 2izA== 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=SoO76paamv0PioszEiN3AVES7Unte5nzNg2ivgsCr64=; fh=ln0nItm0syOMHsiNignx7CG9Qb6sagium675KTXa4B0=; b=wWp3gXaee/otv77um3CDdPF92hB6PbJnw4GaF77zhXBvTtqa8HIqiFh4ZKnjuYOtcY HEOSHkJ+46eMUhxxk1Qsblix2xmAj3zioAE+IdiEGjIeV4CGz4WRJYytmN8laDB6e4Qj +CxSRPoGlweOIpq5yMIe5YEUmmDhFYA0pJ09qmK2rL0/Tm1q24vb+1zZxoClmXsx06Uc IbIFnJCaFruu/AUgt+4JQvtrBJof5BnED4YXsCn8jIcMFvixgqigvRrVoIIAWI+AqJuk dmiJszCyiZ4C+Z9BylJdXHrD2b8oxQ3DoaSs8DhLnS9Wm7D2o9Rz8aB/C07PeNbZ7Qae KMwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b="UG9/fOY+"; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id be9-20020a056a001f0900b0068e0fcf8c28si12278355pfb.336.2023.09.26.09.59.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 09:59:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@quicinc.com header.s=qcppdkim1 header.b="UG9/fOY+"; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=quicinc.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id D701B812044D; Tue, 26 Sep 2023 03:20:46 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233201AbjIZKUv (ORCPT + 99 others); Tue, 26 Sep 2023 06:20:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231182AbjIZKUu (ORCPT ); Tue, 26 Sep 2023 06:20:50 -0400 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5582EB3; Tue, 26 Sep 2023 03:20:40 -0700 (PDT) Received: from pps.filterd (m0279864.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 38Q8xw6X005613; Tue, 26 Sep 2023 10:20:26 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=SoO76paamv0PioszEiN3AVES7Unte5nzNg2ivgsCr64=; b=UG9/fOY+tX3tq1GkQmr/3r0yGvQCVDJDA91Bzn0eIs0hmQOUaRPCkZFY2lHzmfRPtwWg hr3FUy8udPWbx0adbSAieLtDjADpRaKgLocipNDvU1GZ8OCba29FZ3DonsBi7ji8cjlo v4o5iv/Snbm1ZZJWNpOORNJajFUALiSuz2ITc99b9rG2q4GX4SldaYZoW+t4jILqNpG+ OpWdT+cvpLlrSpmX9RN3q79zuC6oHf7OoD1qNZnucR+F8ePenRWcnw1NyCVyd0RkGFDT gpVZon00FUlUFQrn18GHwoiEfaOKJYJpFTa8N1fvzwiOfps8klME9OKALuFFPs4qps3d 5w== Received: from nalasppmta01.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3tbmwws2ns-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 26 Sep 2023 10:20:26 +0000 Received: from nalasex01b.na.qualcomm.com (nalasex01b.na.qualcomm.com [10.47.209.197]) by NALASPPMTA01.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 38QAKPXl031276 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 26 Sep 2023 10:20:25 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; Tue, 26 Sep 2023 03:20:20 -0700 From: Om Prakash Singh To: CC: , , , , , , , , , , , , , , Subject: [PATCH V4] crypto: qcom-rng - Add hw_random interface support Date: Tue, 26 Sep 2023 15:50:05 +0530 Message-ID: <20230926102005.3277045-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-GUID: CVvXhIafNzcgd_uXEkF23H0mV34gnzl2 X-Proofpoint-ORIG-GUID: CVvXhIafNzcgd_uXEkF23H0mV34gnzl2 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-26_07,2023-09-25_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 adultscore=0 bulkscore=0 impostorscore=0 malwarescore=0 lowpriorityscore=0 phishscore=0 mlxlogscore=999 spamscore=0 priorityscore=1501 clxscore=1015 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2309180000 definitions=main-2309260090 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Tue, 26 Sep 2023 03:20:47 -0700 (PDT) Add hw_random interface support in qcom-rng driver as new IP block in Qualcomm SoC has inbuilt NIST SP800 90B compliant entropic source to generate true random number. Keeping current rng_alg interface as well for random number generation using Kernel Crypto API. Signed-off-by: Om Prakash Singh --- Changes in V4: - Fixed email address of author - Removed extra space added in qcom_hwrng_read() Changes in V3: - Fix qcom_rng_read() implementation to account for remaining bytes read - Update qcom_rng_generate() return condition for success case - Added hwrng structure as part of qcom_rng structure Changes in V2: - Updated patch to fix the return value from qcom_rng_generate() to be consistent with current implementation - Updated patch to make it more concise - Removed unnecessary use local variable and it's initialization - Updated patch to use devm_hwrng_register() instead of hwrng_register() - Updated subject line of the patch This patch is depends on [1] [1] https://lore.kernel.org/lkml/20230824-topic-sm8550-rng-v2-4-dfcafbb16a3e@linaro.org/ drivers/crypto/qcom-rng.c | 66 ++++++++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 8 deletions(-) diff --git a/drivers/crypto/qcom-rng.c b/drivers/crypto/qcom-rng.c index fb54b8cfc35f..8b506abb934c 100644 --- a/drivers/crypto/qcom-rng.c +++ b/drivers/crypto/qcom-rng.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -28,17 +29,25 @@ #define WORD_SZ 4 +#define QCOM_TRNG_QUALITY 1024 + struct qcom_rng { struct mutex lock; void __iomem *base; struct clk *clk; - unsigned int skip_init; + struct hwrng hwrng; + 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) @@ -66,11 +75,11 @@ static int qcom_rng_read(struct qcom_rng *rng, u8 *data, unsigned int max) } else { /* copy only remaining bytes */ memcpy(data, &val, max - currsize); - break; + currsize = max; } } while (currsize < max); - return 0; + return currsize; } static int qcom_rng_generate(struct crypto_rng *tfm, @@ -92,6 +101,9 @@ static int qcom_rng_generate(struct crypto_rng *tfm, mutex_unlock(&rng->lock); clk_disable_unprepare(rng->clk); + if (ret >= 0) + ret = 0; + return ret; } @@ -101,6 +113,13 @@ static int qcom_rng_seed(struct crypto_rng *tfm, const u8 *seed, return 0; } +static int qcom_hwrng_read(struct hwrng *hwrng, void *data, size_t max, bool wait) +{ + struct qcom_rng *qrng = container_of(hwrng, struct qcom_rng, hwrng); + + return qcom_rng_read(qrng, data, max); +} + static int qcom_rng_enable(struct qcom_rng *rng) { u32 val; @@ -136,7 +155,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; @@ -177,15 +196,31 @@ 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); + rng->of_data = (struct qcom_rng_of_data *)of_device_get_match_data(&pdev->dev); qcom_rng_dev = rng; ret = crypto_register_rng(&qcom_rng_alg); if (ret) { dev_err(&pdev->dev, "Register crypto rng failed: %d\n", ret); qcom_rng_dev = NULL; + return ret; + } + + if (rng->of_data->hwrng_support) { + rng->hwrng.name = "qcom_hwrng"; + rng->hwrng.read = qcom_hwrng_read; + rng->hwrng.quality = QCOM_TRNG_QUALITY; + ret = devm_hwrng_register(&pdev->dev, &rng->hwrng); + if (ret) { + dev_err(&pdev->dev, "Register hwrng failed: %d\n", ret); + qcom_rng_dev = NULL; + goto fail; + } } + return ret; +fail: + crypto_unregister_rng(&qcom_rng_alg); return ret; } @@ -198,6 +233,21 @@ static int qcom_rng_remove(struct platform_device *pdev) 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 +255,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