From: Kai Ye <yekai13@huawei.com>
To: <herbert@gondor.apana.org.au>
Cc: <linux-crypto@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
<wangzhou1@hisilicon.com>, <yekai13@huawei.com>,
<liulongfang@huawei.com>
Subject: [PATCH v3 1/6] crypto: hisilicon/sec - add ping-pong buffer for ahash
Date: Fri, 22 Oct 2021 17:10:50 +0800 [thread overview]
Message-ID: <20211022091055.15369-2-yekai13@huawei.com> (raw)
In-Reply-To: <20211022091055.15369-1-yekai13@huawei.com>
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 <yekai13@huawei.com>
---
drivers/crypto/hisilicon/sec2/sec.h | 14 ++++
drivers/crypto/hisilicon/sec2/sec_crypto.c | 85 ++++++++++++++++++++++
drivers/crypto/hisilicon/sec2/sec_crypto.h | 3 +
3 files changed, 102 insertions(+)
diff --git a/drivers/crypto/hisilicon/sec2/sec.h b/drivers/crypto/hisilicon/sec2/sec.h
index d97cf02b1df7..0ddfac937ef0 100644
--- a/drivers/crypto/hisilicon/sec2/sec.h
+++ b/drivers/crypto/hisilicon/sec2/sec.h
@@ -130,6 +130,11 @@ enum sec_alg_type {
SEC_AEAD
};
+struct pending_sgl {
+ struct scatterlist sgl[MERGE_SGL_NUM];
+ u32 len;
+};
+
/* SEC Crypto TFM context which defines queue and cipher .etc relatives */
struct sec_ctx {
struct sec_qp_ctx *qp_ctx;
@@ -154,6 +159,15 @@ 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 pending_sgl pingpong_sg[SEC_MAX_STREAMS][PINGPONG_BUF_NUM];
+ u8 pingpong_idx[SEC_MAX_STREAMS];
+ 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..66f461f64288 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_table(ctx->pingpong_sg[j][i].sgl, MERGE_SGL_NUM);
+ sg_set_buf(ctx->pingpong_sg[j][i].sgl, 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].sgl;
+ 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..62add3ebb862 100644
--- a/drivers/crypto/hisilicon/sec2/sec_crypto.h
+++ b/drivers/crypto/hisilicon/sec2/sec_crypto.h
@@ -8,7 +8,10 @@
#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
+#define MERGE_SGL_NUM 2
enum sec_calg {
SEC_CALG_3DES = 0x1,
--
2.33.0
next prev parent reply other threads:[~2021-10-22 9:13 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-22 9:10 [PATCH v3 0/6] crypto: hisilicon - supports hash algorithm for SEC engine Kai Ye
2021-10-22 9:10 ` Kai Ye [this message]
2021-10-22 9:10 ` [PATCH v3 2/6] crypto: hisilicon/sec - add ahash alg features for Kunpeng920 Kai Ye
2021-11-13 6:01 ` Herbert Xu
2021-10-22 9:10 ` [PATCH v3 3/6] crypto: hisilicon/sec - support the larger packets for digest mode Kai Ye
2021-10-22 9:10 ` [PATCH v3 4/6] crypto: hisilicon/sec - ahash adapt to Kunpeng930 SQE Kai Ye
2021-10-22 9:10 ` [PATCH v3 5/6] crypto: hisilicon/sec - add fallback tfm supporting for ahash Kai Ye
2021-10-22 9:10 ` [PATCH v3 6/6] crypto: hisilicon/sec - add sm3/md5 generic selection " Kai Ye
2021-10-30 1:06 ` [PATCH v3 0/6] crypto: hisilicon - supports hash algorithm for SEC engine yekai(A)
2021-11-02 3:52 ` Herbert Xu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211022091055.15369-2-yekai13@huawei.com \
--to=yekai13@huawei.com \
--cc=herbert@gondor.apana.org.au \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=liulongfang@huawei.com \
--cc=wangzhou1@hisilicon.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®