Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp1348430pxb; Fri, 24 Sep 2021 02:29:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwmSbwHVdCS8QH18NCrkWKsHwy7nYEwvIR15AhOQszXbmByTyJFqOQcZNPGY7nDMMYY5lfr X-Received: by 2002:a05:6e02:c88:: with SMTP id b8mr7408611ile.300.1632475751995; Fri, 24 Sep 2021 02:29:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632475751; cv=none; d=google.com; s=arc-20160816; b=Pdc008gKsDEYoNm1lFhov01E49KiK7wwT27lA20ARjVqvKUdNVIS34oaj5ZjRKRsuZ VAAxCF1TBdq2F1KhJ7/TxpBV18/K+WiavjlyWLc3nujewte3uVBlBJQ6uIubwGZRjE0R ndX8SiR0HLx1lZ/V3Go+X0yeMeKR/+Er+37TBAu8mYTnJx0GjPRAqmG9acZ5m7NJuYu9 wAhHJBaTpWJcMJC/bEYzBaLbIZw50gDpdwRNq9yCV20qR9wd9wHlRMvLRyqnHQO4/i2m qeeFB7NyVevXYeHkTYyPfKHgYiFszmEz7dJcAhecn6kBFu1NrS9N5L7Rec/gSfVQ65Su gj0Q== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=pkVionF3zDMnKSgF5B/4d8BrSjQoz+/suS5WimbD02g=; b=vldm9s0OleMCwiYQIU8ojCxZqz1PEX+gd0HPqxyvNjmuZznjWGnHAajcDVUjCjWkKp DhbYqQFDPlREDIikZzW8EJI/kEHiEqPsKCZJgvcQK9OpVzxP6OPdPUd3i7Vhtz7i+aPV 9aL4RpjMRY9XKzNXiTKRbUjGB4L9wn29aV2+1zHWDwajzLvkl+A4Km5BJsCp0W9xWzk1 iC6czb5VgMvL2+hor4Vj4tMKI8L1c82cnolZzQJnEdnkGZwxpP1HwDRZYJ1+R7APL0h4 MKV5bYca8pTaCWRDvCqTUQexCWmHdr0VljidhcxAMDoihB5YYnCGkQ2JazC5lt/XZbH9 Dhnw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s17si9929855ios.107.2021.09.24.02.28.52; Fri, 24 Sep 2021 02:29:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-crypto-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-crypto-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237056AbhIXJaY (ORCPT + 99 others); Fri, 24 Sep 2021 05:30:24 -0400 Received: from szxga02-in.huawei.com ([45.249.212.188]:16381 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244968AbhIXJaX (ORCPT ); Fri, 24 Sep 2021 05:30:23 -0400 Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4HG6764MdSzRPnV; Fri, 24 Sep 2021 17:24:34 +0800 (CST) Received: from dggpeml500012.china.huawei.com (7.185.36.15) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Fri, 24 Sep 2021 17:28:47 +0800 Received: from huawei.com (10.69.192.56) by dggpeml500012.china.huawei.com (7.185.36.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.8; Fri, 24 Sep 2021 17:28:47 +0800 From: Kai Ye To: CC: , , , Subject: [PATCH v2 1/6] crypto: hisilicon/sec - add ping-pong buffer for ahash Date: Fri, 24 Sep 2021 17:27:11 +0800 Message-ID: <20210924092716.12848-2-yekai13@huawei.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210924092716.12848-1-yekai13@huawei.com> References: <20210924092716.12848-1-yekai13@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.69.192.56] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpeml500012.china.huawei.com (7.185.36.15) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org For stream mode operations, we don't send data into HW everytime. We use the pingpong buffer to hold some small packets. The length of ping-pong buffer is PAGE_SIZE. As well as one TFM supports maximum of 128 streams. Signed-off-by: Kai Ye --- drivers/crypto/hisilicon/sec2/sec.h | 10 +++ drivers/crypto/hisilicon/sec2/sec_crypto.c | 85 ++++++++++++++++++++++ drivers/crypto/hisilicon/sec2/sec_crypto.h | 2 + 3 files changed, 97 insertions(+) diff --git a/drivers/crypto/hisilicon/sec2/sec.h b/drivers/crypto/hisilicon/sec2/sec.h index d97cf02b1df7..281f38de64e9 100644 --- a/drivers/crypto/hisilicon/sec2/sec.h +++ b/drivers/crypto/hisilicon/sec2/sec.h @@ -154,6 +154,16 @@ struct sec_ctx { struct sec_cipher_ctx c_ctx; struct sec_auth_ctx a_ctx; u8 type_supported; + + /* + * For stream mode operations, we don't send data into HW everytime. + * now ahash uses this. To avoid multiple copy, use pingpong buffers. + */ + struct scatterlist pingpong_sg[SEC_MAX_STREAMS][PINGPONG_BUF_NUM]; + u8 pingpong_idx[SEC_MAX_STREAMS]; + u32 pp_data_len[SEC_MAX_STREAMS][PINGPONG_BUF_NUM]; + struct idr stream_idr; + struct mutex stream_idr_lock; struct device *dev; }; diff --git a/drivers/crypto/hisilicon/sec2/sec_crypto.c b/drivers/crypto/hisilicon/sec2/sec_crypto.c index 6a45bd23b363..afbe36549e83 100644 --- a/drivers/crypto/hisilicon/sec2/sec_crypto.c +++ b/drivers/crypto/hisilicon/sec2/sec_crypto.c @@ -77,6 +77,8 @@ #define SEC_TOTAL_PBUF_SZ (PAGE_SIZE * SEC_PBUF_PAGE_NUM + \ SEC_PBUF_LEFT_SZ) +#define SEC_SID_BUF_LEN (PAGE_SIZE) +#define SEC_MAX_AD_SG_NENT 8 #define SEC_SQE_LEN_RATE 4 #define SEC_SQE_CFLAG 2 #define SEC_SQE_AEAD_FLAG 3 @@ -158,6 +160,89 @@ static void sec_free_req_id(struct sec_req *req) mutex_unlock(&qp_ctx->req_lock); } +static int sec_alloc_stream_id(struct sec_ctx *ctx) +{ + int s_id; + + mutex_lock(&ctx->stream_idr_lock); + + s_id = idr_alloc_cyclic(&ctx->stream_idr, NULL, + 0, SEC_MAX_STREAMS, GFP_ATOMIC); + + mutex_unlock(&ctx->stream_idr_lock); + if (unlikely(s_id < 0)) + dev_err(ctx->dev, "alloc stream id fail!\n"); + + return s_id; +} + +static void sec_free_stream_id(struct sec_ctx *ctx, int stream_id) +{ + if (unlikely(stream_id < 0 || stream_id >= SEC_MAX_STREAMS)) { + dev_err(ctx->dev, "free stream id invalid!\n"); + return; + } + + mutex_lock(&ctx->stream_idr_lock); + idr_remove(&ctx->stream_idr, stream_id); + mutex_unlock(&ctx->stream_idr_lock); +} + +/* For stream mode, we pre-create buffer to hold some small packets */ +static int sec_stream_mode_init(struct sec_ctx *ctx) +{ + u32 buf_len = SEC_MAX_STREAMS * SEC_SID_BUF_LEN * PINGPONG_BUF_NUM; + u32 buf_step = SEC_SID_BUF_LEN * PINGPONG_BUF_NUM; + struct device *dev = ctx->dev; + unsigned int order, i, j; + unsigned long buf; + void *temp_buf; + + mutex_init(&ctx->stream_idr_lock); + idr_init(&ctx->stream_idr); + + order = get_order(buf_len); + if (order > MAX_ORDER) { + dev_err(dev, "too large order %u for pingpong buf!\n", order); + return -ENOMEM; + } + + buf = __get_free_pages(GFP_KERNEL, order); + if (!buf) { + dev_err(dev, "fail to get pingpong pages!\n"); + return -ENOMEM; + } + + /* + * stream 0: | ping buf | pong buf + * stream 1: | ping buf | pong buf + * stream xx: | ping buf | pong buf + * scatterlist ping-pong buffer initiation + */ + for (j = 0; j < SEC_MAX_STREAMS; j++) { + for (i = 0; i < PINGPONG_BUF_NUM; i++) { + temp_buf = (void *)(uintptr_t)buf + SEC_SID_BUF_LEN * i; + + sg_init_one(&ctx->pingpong_sg[j][i], temp_buf, SEC_SID_BUF_LEN); + } + buf = buf + buf_step; + } + + return 0; +} + +static void sec_stream_mode_uninit(struct sec_ctx *ctx) +{ + struct scatterlist *sgl = &ctx->pingpong_sg[0][0]; + unsigned int order; + + order = get_order(SEC_SID_BUF_LEN * SEC_MAX_STREAMS * PINGPONG_BUF_NUM); + + free_pages((unsigned long)(uintptr_t)sg_virt(sgl), order); + + idr_destroy(&ctx->stream_idr); +} + static u8 pre_parse_finished_bd(struct bd_status *status, void *resp) { struct sec_sqe *bd = resp; diff --git a/drivers/crypto/hisilicon/sec2/sec_crypto.h b/drivers/crypto/hisilicon/sec2/sec_crypto.h index 9f71c358a6d3..6efe473660eb 100644 --- a/drivers/crypto/hisilicon/sec2/sec_crypto.h +++ b/drivers/crypto/hisilicon/sec2/sec_crypto.h @@ -8,6 +8,8 @@ #define SEC_IV_SIZE 24 #define SEC_MAX_KEY_SIZE 64 #define SEC_COMM_SCENE 0 +#define SEC_MAX_STREAMS 128 +#define PINGPONG_BUF_NUM 2 #define SEC_MIN_BLOCK_SZ 1 enum sec_calg { -- 2.33.0