The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH 00/11] crypto: ccp - add Hygon SM3/SM4 support
@ 2026-11-25  7:45 Depei Yang
  2026-11-25  7:45 ` [PATCH 01/11] crypto: ccp - fix LSB region mask calculation Depei Yang
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: Depei Yang @ 2026-11-25  7:45 UTC (permalink / raw)
  To: linux-crypto
  Cc: Tom Lendacky, Herbert Xu, David S. Miller, Gary R Hook,
	linux-kernel, Depei Yang, Shan Chen, Yongwei Xu, Xiaochen Shen

This series adds SM3 and SM4 offload for Hygon CCP devices, which use
the v5 programming model, and fixes two pre-existing problems in the
queue handling that were found while validating it.

Patches 1-2 are generic fixes that apply to the AMD devices as well:

  1: correct the LSB region mask, which tested the wrong region
  2: acknowledge the queue interrupt before publishing the completion

Patches 3-11 prepare and enable the Hygon support on top of a per-device
capability set instead of unconditional registration:

  3-4: add struct ccp_caps and gate algorithm registration on the
       engines the device implements.  AMD v5 devices keep their
       current set of algorithms; the v3 CCP does not implement DES3
       and no longer advertises it.
  5:   split descriptor submission from running the queue, so that the
       SM engines can submit a whole batch and run the queue once.
  6-7: add the SM3/SM4 command ABI, the Hygon PCI IDs and vdata.
  8:   ignore the empty-queue interrupt status bit, which the Hygon
       CCP reports and the v5 handler must not treat as a completion.
       AMD hardware has not been observed to report it; for the status
       values AMD produces the check makes no difference.
  9:   read the implemented engines from the Hygon version register.
  10-11: add the SM4 skcipher and SM3 ahash front ends, registering
       ecb/cbc/ctr(sm4), sm3 and hmac(sm3).

Every patch was built separately with W=1 and passes checkpatch.  The
driver has been tested on Hygon CCP hardware: the crypto API self-tests
pass, and an out-of-tree test module passes all 17 of its cases.  The
cases cover known-answer vectors, cross-checks against the generic
software implementations, streaming and export state, multi-segment
scatterlists, repeated requests and concurrent requests.

AI assistance: the code and changelogs were produced with AI coding
assistants under human review.  The assistance is recorded here, as the
patches do not carry per-patch Assisted-by tags:

  Assisted-by: Claude:deepseek-v4-flash-1017

The test module is not part of this series.

Depei Yang (11):
  crypto: ccp - fix LSB region mask calculation
  crypto: ccp - acknowledge queue interrupt before waking waiter
  crypto: ccp - add engine capability and version information
  crypto: ccp - register algorithms only for supported engines
  crypto: ccp - split v5 command submission from queue run
  crypto: ccp - add SM3/SM4 command definitions
  crypto: ccp - add Hygon CCP device IDs and vdata
  crypto: ccp - handle only supported interrupts in v5 irq bh
  crypto: ccp - detect the Hygon CCP engines from the version register
  crypto: ccp - add SM4 engine and skcipher algorithms
  crypto: ccp - add SM3 engine and ahash algorithms

 drivers/crypto/ccp/Kconfig           |   1 +
 drivers/crypto/ccp/Makefile          |   4 +-
 drivers/crypto/ccp/ccp-crypto-main.c |  36 +-
 drivers/crypto/ccp/ccp-crypto-sm3.c  | 468 +++++++++++++++++++++++++
 drivers/crypto/ccp/ccp-crypto-sm4.c  | 254 ++++++++++++++
 drivers/crypto/ccp/ccp-crypto.h      |  67 ++++
 drivers/crypto/ccp/ccp-dev-v3.c      |  21 +-
 drivers/crypto/ccp/ccp-dev-v5.c      | 260 +++++++++++++-
 drivers/crypto/ccp/ccp-dev.c         |  37 ++
 drivers/crypto/ccp/ccp-dev.h         |  31 ++
 drivers/crypto/ccp/ccp-ops.c         | 487 +++++++++++++++++++++++++++
 drivers/crypto/ccp/sp-dev.h          |  12 +
 drivers/crypto/ccp/sp-pci.c          |  35 ++
 include/linux/ccp.h                  | 187 ++++++++++
 14 files changed, 1878 insertions(+), 22 deletions(-)
 create mode 100644 drivers/crypto/ccp/ccp-crypto-sm3.c
 create mode 100644 drivers/crypto/ccp/ccp-crypto-sm4.c


base-commit: 05441d697f7f9a927264f4058cbbc6a03dfcac9d
-- 
2.34.1



^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH 01/11] crypto: ccp - fix LSB region mask calculation
  2026-11-25  7:45 [PATCH 00/11] crypto: ccp - add Hygon SM3/SM4 support Depei Yang
@ 2026-11-25  7:45 ` Depei Yang
  2026-11-25  7:46 ` [PATCH 02/11] crypto: ccp - acknowledge queue interrupt before waking waiter Depei Yang
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Depei Yang @ 2026-11-25  7:45 UTC (permalink / raw)
  To: linux-crypto
  Cc: Tom Lendacky, Herbert Xu, David S. Miller, Gary R Hook,
	linux-kernel, Depei Yang, Shan Chen, Yongwei Xu, Xiaochen Shen,
	stable

ccp_find_lsb_regions() tests the queue bit of status before shifting the
register by the region width, so region 0 is examined for region 1 and
the last region is never checked.  Shift the status before the loop so
that each iteration tests the region it records.

Fixes: 4b394a232df7 ("crypto: ccp - Let a v5 CCP provide the same function as v3")
Cc: stable@vger.kernel.org
Reviewed-by: Shan Chen <chenshan@hygon.cn>
Tested-by: Yongwei Xu <xuyongwei@hygon.cn>
Signed-off-by: Depei Yang <yangdepei@hygon.cn>
---
 drivers/crypto/ccp/ccp-dev-v5.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/crypto/ccp/ccp-dev-v5.c b/drivers/crypto/ccp/ccp-dev-v5.c
index dacde9614e8a..4a546224af79 100644
--- a/drivers/crypto/ccp/ccp-dev-v5.c
+++ b/drivers/crypto/ccp/ccp-dev-v5.c
@@ -593,6 +593,7 @@ static int ccp_find_lsb_regions(struct ccp_cmd_queue *cmd_q, u64 status)
 	/* Build a bit mask to know which LSBs this queue has access to.
 	 * Don't bother with segment 0 as it has special privileges.
 	 */
+	status >>= LSB_REGION_WIDTH;
 	for (j = 1; j < MAX_LSB_CNT; j++) {
 		if (status & q_mask)
 			bitmap_set(cmd_q->lsbmask, j, 1);
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 02/11] crypto: ccp - acknowledge queue interrupt before waking waiter
  2026-11-25  7:45 [PATCH 00/11] crypto: ccp - add Hygon SM3/SM4 support Depei Yang
  2026-11-25  7:45 ` [PATCH 01/11] crypto: ccp - fix LSB region mask calculation Depei Yang
@ 2026-11-25  7:46 ` Depei Yang
  2026-11-25  7:46 ` [PATCH 03/11] crypto: ccp - add engine capability and version information Depei Yang
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Depei Yang @ 2026-11-25  7:46 UTC (permalink / raw)
  To: linux-crypto
  Cc: Tom Lendacky, Herbert Xu, David S. Miller, Gary R Hook,
	linux-kernel, Depei Yang, Shan Chen, Yongwei Xu, Xiaochen Shen,
	stable

ccp5_irq_bh() and ccp_irq_bh() set int_rcvd before writing the interrupt
status back to the device.  A waiting queue thread can then start the
next command before the previous completion has been acknowledged; the
stale write-back clears the new completion bit as well, and the thread
waits for an interrupt that will never be raised again.  The wait has no
timeout, so the queue stays stuck.

Acknowledge the interrupt before making int_rcvd visible.  The v5 handler
was observed to wedge a queue thread under concurrent crypto load; the
v3 handler uses the same ordering and gets the same treatment.

Fixes: 4b394a232df7 ("crypto: ccp - Let a v5 CCP provide the same function as v3")
Fixes: 63b945091a07 ("crypto: ccp - CCP device driver and interface support")
Cc: stable@vger.kernel.org
Reviewed-by: Shan Chen <chenshan@hygon.cn>
Tested-by: Yongwei Xu <xuyongwei@hygon.cn>
Signed-off-by: Depei Yang <yangdepei@hygon.cn>
---
 drivers/crypto/ccp/ccp-dev-v3.c | 9 ++++++---
 drivers/crypto/ccp/ccp-dev-v5.c | 9 ++++++---
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/drivers/crypto/ccp/ccp-dev-v3.c b/drivers/crypto/ccp/ccp-dev-v3.c
index d92de4ad31ac..02f7575e8f9b 100644
--- a/drivers/crypto/ccp/ccp-dev-v3.c
+++ b/drivers/crypto/ccp/ccp-dev-v3.c
@@ -343,10 +343,13 @@ static void ccp_irq_bh(unsigned long data)
 			if ((q_int & cmd_q->int_err) && !cmd_q->cmd_error)
 				cmd_q->cmd_error = CMD_Q_ERROR(cmd_q->q_status);
 
-			cmd_q->int_rcvd = 1;
-
-			/* Acknowledge the interrupt and wake the kthread */
+			/*
+			 * Acknowledge the interrupt before waking the kthread,
+			 * otherwise a fast waiter can start a new command while
+			 * the old interrupt is still pending.
+			 */
 			iowrite32(q_int, ccp->io_regs + IRQ_STATUS_REG);
+			cmd_q->int_rcvd = 1;
 			wake_up_interruptible(&cmd_q->int_queue);
 		}
 	}
diff --git a/drivers/crypto/ccp/ccp-dev-v5.c b/drivers/crypto/ccp/ccp-dev-v5.c
index 4a546224af79..bf76743845e3 100644
--- a/drivers/crypto/ccp/ccp-dev-v5.c
+++ b/drivers/crypto/ccp/ccp-dev-v5.c
@@ -754,10 +754,13 @@ static void ccp5_irq_bh(unsigned long data)
 			if ((status & INT_ERROR) && !cmd_q->cmd_error)
 				cmd_q->cmd_error = CMD_Q_ERROR(cmd_q->q_status);
 
-			cmd_q->int_rcvd = 1;
-
-			/* Acknowledge the interrupt and wake the kthread */
+			/*
+			 * Acknowledge the interrupt before waking the kthread,
+			 * otherwise a fast waiter can start a new command while
+			 * the old interrupt is still pending.
+			 */
 			iowrite32(status, cmd_q->reg_interrupt_status);
+			cmd_q->int_rcvd = 1;
 			wake_up_interruptible(&cmd_q->int_queue);
 		}
 	}
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 03/11] crypto: ccp - add engine capability and version information
  2026-11-25  7:45 [PATCH 00/11] crypto: ccp - add Hygon SM3/SM4 support Depei Yang
  2026-11-25  7:45 ` [PATCH 01/11] crypto: ccp - fix LSB region mask calculation Depei Yang
  2026-11-25  7:46 ` [PATCH 02/11] crypto: ccp - acknowledge queue interrupt before waking waiter Depei Yang
@ 2026-11-25  7:46 ` Depei Yang
  2026-11-25  7:46 ` [PATCH 04/11] crypto: ccp - register algorithms only for supported engines Depei Yang
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Depei Yang @ 2026-11-25  7:46 UTC (permalink / raw)
  To: linux-crypto
  Cc: Tom Lendacky, Herbert Xu, David S. Miller, Gary R Hook,
	linux-kernel, Depei Yang, Shan Chen, Yongwei Xu, Xiaochen Shen

Add a struct ccp_caps to struct ccp_device to record which engines a
device implements, together with the CCP and engine versions.  The
versions are not used yet; they are needed to tell features of the same
engine apart, for example whether an SM4 engine can run XTS.

A get_caps callback fills the set at probe time, after the register
mapping and device setup, so a device can derive it from a register
instead of a table.  Capabilities are BIT(enum ccp_engine) behind the
CCP_CAPS_* helpers, so the engine id in a command descriptor and its
capability bit cannot drift apart; ccp_has_cap() requires a single
device to provide every bit in the mask.

No algorithm registration is gated on this yet.

Reviewed-by: Shan Chen <chenshan@hygon.cn>
Tested-by: Yongwei Xu <xuyongwei@hygon.cn>
Signed-off-by: Depei Yang <yangdepei@hygon.cn>
---
 drivers/crypto/ccp/ccp-dev-v3.c | 12 +++++++++++
 drivers/crypto/ccp/ccp-dev-v5.c | 16 ++++++++++++++
 drivers/crypto/ccp/ccp-dev.c    | 37 +++++++++++++++++++++++++++++++++
 drivers/crypto/ccp/ccp-dev.h    |  2 ++
 drivers/crypto/ccp/sp-dev.h     | 12 +++++++++++
 include/linux/ccp.h             | 22 ++++++++++++++++++++
 6 files changed, 101 insertions(+)

diff --git a/drivers/crypto/ccp/ccp-dev-v3.c b/drivers/crypto/ccp/ccp-dev-v3.c
index 02f7575e8f9b..b4715b694b53 100644
--- a/drivers/crypto/ccp/ccp-dev-v3.c
+++ b/drivers/crypto/ccp/ccp-dev-v3.c
@@ -587,12 +587,23 @@ static const struct ccp_actions ccp3_actions = {
 	.irqhandler = ccp_irq_handler,
 };
 
+/* Engines implemented for the v3 CCP.  3DES is not wired up for it. */
+#define CCP3_BASE_ENGINES	(CCP_CAPS_AES | CCP_CAPS_AES_XTS |	\
+				 CCP_CAPS_SHA | CCP_CAPS_RSA |		\
+				 CCP_CAPS_PASSTHRU | CCP_CAPS_ECC)
+
+static void ccp3_get_caps(struct ccp_device *ccp)
+{
+	ccp->caps.engines = CCP3_BASE_ENGINES;
+}
+
 const struct ccp_vdata ccpv3_platform = {
 	.version = CCP_VERSION(3, 0),
 	.setup = NULL,
 	.perform = &ccp3_actions,
 	.offset = 0,
 	.rsamax = CCP_RSA_MAX_WIDTH,
+	.get_caps = ccp3_get_caps,
 };
 
 const struct ccp_vdata ccpv3 = {
@@ -601,4 +612,5 @@ const struct ccp_vdata ccpv3 = {
 	.perform = &ccp3_actions,
 	.offset = 0x20000,
 	.rsamax = CCP_RSA_MAX_WIDTH,
+	.get_caps = ccp3_get_caps,
 };
diff --git a/drivers/crypto/ccp/ccp-dev-v5.c b/drivers/crypto/ccp/ccp-dev-v5.c
index bf76743845e3..372ccf7d1c8b 100644
--- a/drivers/crypto/ccp/ccp-dev-v5.c
+++ b/drivers/crypto/ccp/ccp-dev-v5.c
@@ -1118,12 +1118,27 @@ static const struct ccp_actions ccp5_actions = {
 	.get_free_slots = ccp5_get_free_slots,
 };
 
+/*
+ * Engines implemented by the AMD v5 CCP variants.  They are described
+ * statically, there is no engine presence register to read.
+ */
+#define CCP5_BASE_ENGINES	(CCP_CAPS_AES | CCP_CAPS_AES_XTS |	\
+				 CCP_CAPS_DES3 | CCP_CAPS_SHA |		\
+				 CCP_CAPS_RSA | CCP_CAPS_PASSTHRU |	\
+				 CCP_CAPS_ECC)
+
+static void ccp5_get_caps(struct ccp_device *ccp)
+{
+	ccp->caps.engines = CCP5_BASE_ENGINES;
+}
+
 const struct ccp_vdata ccpv5a = {
 	.version = CCP_VERSION(5, 0),
 	.setup = ccp5_config,
 	.perform = &ccp5_actions,
 	.offset = 0x0,
 	.rsamax = CCP5_RSA_MAX_WIDTH,
+	.get_caps = ccp5_get_caps,
 };
 
 const struct ccp_vdata ccpv5b = {
@@ -1133,4 +1148,5 @@ const struct ccp_vdata ccpv5b = {
 	.perform = &ccp5_actions,
 	.offset = 0x0,
 	.rsamax = CCP5_RSA_MAX_WIDTH,
+	.get_caps = ccp5_get_caps,
 };
diff --git a/drivers/crypto/ccp/ccp-dev.c b/drivers/crypto/ccp/ccp-dev.c
index aff3348b8351..ceaeca3d755a 100644
--- a/drivers/crypto/ccp/ccp-dev.c
+++ b/drivers/crypto/ccp/ccp-dev.c
@@ -242,6 +242,40 @@ int ccp_present(void)
 }
 EXPORT_SYMBOL_GPL(ccp_present);
 
+/**
+ * ccp_has_cap - check whether any CCP device provides a capability
+ *
+ * @cap: mask of BIT(enum ccp_engine) values
+ *
+ * Returns true if at least one registered CCP device provides all the
+ * engines in @cap.
+ *
+ * Commands are dispatched to the devices by round-robin without checking
+ * their capabilities, so a caller may only rely on this result when all
+ * the CCP devices of the system provide the same engines, as they do when
+ * they are instances of the same hardware.  A system mixing devices with
+ * different engines would need per-command capability filtering in the
+ * dispatch path.
+ */
+bool ccp_has_cap(unsigned int cap)
+{
+	struct ccp_device *dp;
+	unsigned long flags;
+	bool ret = false;
+
+	read_lock_irqsave(&ccp_unit_lock, flags);
+	list_for_each_entry(dp, &ccp_units, entry) {
+		if ((dp->caps.engines & cap) == cap) {
+			ret = true;
+			break;
+		}
+	}
+	read_unlock_irqrestore(&ccp_unit_lock, flags);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(ccp_has_cap);
+
 /**
  * ccp_version - get the version of the CCP device
  *
@@ -634,6 +668,9 @@ int ccp_dev_init(struct sp_device *sp)
 	if (ccp->vdata->setup)
 		ccp->vdata->setup(ccp);
 
+	if (ccp->vdata->get_caps)
+		ccp->vdata->get_caps(ccp);
+
 	ret = ccp->vdata->perform->init(ccp);
 	if (ret) {
 		/* A positive number means that the device cannot be initialized,
diff --git a/drivers/crypto/ccp/ccp-dev.h b/drivers/crypto/ccp/ccp-dev.h
index 83350e2d9821..ef88962ff6e6 100644
--- a/drivers/crypto/ccp/ccp-dev.h
+++ b/drivers/crypto/ccp/ccp-dev.h
@@ -23,6 +23,7 @@
 #include <linux/interrupt.h>
 #include <linux/irqreturn.h>
 #include <linux/dmaengine.h>
+#include <linux/ccp.h>
 
 #include "sp-dev.h"
 
@@ -340,6 +341,7 @@ struct ccp_device {
 	struct list_head entry;
 
 	struct ccp_vdata *vdata;
+	struct ccp_caps caps;
 	unsigned int ord;
 	char name[MAX_CCP_NAME_LEN];
 	char rngname[MAX_CCP_NAME_LEN];
diff --git a/drivers/crypto/ccp/sp-dev.h b/drivers/crypto/ccp/sp-dev.h
index a83751cfd006..2513a3e92631 100644
--- a/drivers/crypto/ccp/sp-dev.h
+++ b/drivers/crypto/ccp/sp-dev.h
@@ -35,6 +35,17 @@
 
 /* Structure to hold CCP device data */
 struct ccp_device;
+
+/*
+ * Engines implemented by a CCP device, plus the CCP and engine
+ * versions.  A version of 0 means that it is not known.
+ */
+struct ccp_caps {
+	unsigned int engines;		/* BIT(enum ccp_engine) */
+	unsigned int ccp_version;
+	unsigned int engine_version;
+};
+
 struct ccp_vdata {
 	const unsigned int version;
 	const unsigned int dma_chan_attr;
@@ -42,6 +53,7 @@ struct ccp_vdata {
 	const struct ccp_actions *perform;
 	const unsigned int offset;
 	const unsigned int rsamax;
+	void (*get_caps)(struct ccp_device *ccp);
 };
 
 struct sev_vdata {
diff --git a/include/linux/ccp.h b/include/linux/ccp.h
index e6c599243f11..06c3b14a1eb6 100644
--- a/include/linux/ccp.h
+++ b/include/linux/ccp.h
@@ -11,6 +11,7 @@
 #ifndef __CCP_H__
 #define __CCP_H__
 
+#include <linux/bits.h>
 #include <linux/scatterlist.h>
 #include <linux/workqueue.h>
 #include <linux/list.h>
@@ -30,6 +31,13 @@ struct ccp_cmd;
  */
 int ccp_present(void);
 
+/**
+ * ccp_has_cap - check whether any CCP device provides a capability
+ *
+ * Returns: true if at least one registered CCP device provides it.
+ */
+bool ccp_has_cap(unsigned int cap);
+
 #define	CCP_VSIZE 16
 #define	CCP_VMASK		((unsigned int)((1 << CCP_VSIZE) - 1))
 #define	CCP_VERSION(v, r)	((unsigned int)((v << CCP_VSIZE) \
@@ -79,6 +87,11 @@ static inline unsigned int ccp_version(void)
 	return 0;
 }
 
+static inline bool ccp_has_cap(unsigned int cap)
+{
+	return false;
+}
+
 static inline int ccp_enqueue_cmd(struct ccp_cmd *cmd)
 {
 	return -ENODEV;
@@ -630,6 +643,15 @@ enum ccp_engine {
 	CCP_ENGINE__LAST,
 };
 
+/* Engine capability bits, indexed by enum ccp_engine */
+#define CCP_CAPS_AES		BIT(CCP_ENGINE_AES)
+#define CCP_CAPS_AES_XTS	BIT(CCP_ENGINE_XTS_AES_128)
+#define CCP_CAPS_DES3		BIT(CCP_ENGINE_DES3)
+#define CCP_CAPS_SHA		BIT(CCP_ENGINE_SHA)
+#define CCP_CAPS_RSA		BIT(CCP_ENGINE_RSA)
+#define CCP_CAPS_PASSTHRU	BIT(CCP_ENGINE_PASSTHRU)
+#define CCP_CAPS_ECC		BIT(CCP_ENGINE_ECC)
+
 /* Flag values for flags member of ccp_cmd */
 #define CCP_CMD_MAY_BACKLOG		0x00000001
 #define CCP_CMD_PASSTHRU_NO_DMA_MAP	0x00000002
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 04/11] crypto: ccp - register algorithms only for supported engines
  2026-11-25  7:45 [PATCH 00/11] crypto: ccp - add Hygon SM3/SM4 support Depei Yang
                   ` (2 preceding siblings ...)
  2026-11-25  7:46 ` [PATCH 03/11] crypto: ccp - add engine capability and version information Depei Yang
@ 2026-11-25  7:46 ` Depei Yang
  2026-11-25  7:46 ` [PATCH 05/11] crypto: ccp - split v5 command submission from queue run Depei Yang
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Depei Yang @ 2026-11-25  7:46 UTC (permalink / raw)
  To: linux-crypto
  Cc: Tom Lendacky, Herbert Xu, David S. Miller, Gary R Hook,
	linux-kernel, Depei Yang, Shan Chen, Yongwei Xu, Xiaochen Shen

ccp_register_algs() registers every algorithm family the driver knows
about.  A device that does not implement the matching engine still gets
the algorithms advertised, and requests for them end up on a queue that
cannot execute them.

Gate each family on the engine it is built on, using the capability
information from probe time.  The AMD v5 variants advertise all of their
engines and keep the current set of algorithms; the v3 CCP does not
implement DES3 and no longer advertises des3.

Reviewed-by: Shan Chen <chenshan@hygon.cn>
Tested-by: Yongwei Xu <xuyongwei@hygon.cn>
Signed-off-by: Depei Yang <yangdepei@hygon.cn>
---
 drivers/crypto/ccp/ccp-crypto-main.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/drivers/crypto/ccp/ccp-crypto-main.c b/drivers/crypto/ccp/ccp-crypto-main.c
index 698e39ab107f..388ecb1694e5 100644
--- a/drivers/crypto/ccp/ccp-crypto-main.c
+++ b/drivers/crypto/ccp/ccp-crypto-main.c
@@ -325,7 +325,7 @@ static int ccp_register_algs(void)
 {
 	int ret;
 
-	if (!aes_disable) {
+	if (!aes_disable && ccp_has_cap(CCP_CAPS_AES)) {
 		ret = ccp_register_aes_algs(&skcipher_algs);
 		if (ret)
 			return ret;
@@ -334,28 +334,30 @@ static int ccp_register_algs(void)
 		if (ret)
 			return ret;
 
-		ret = ccp_register_aes_xts_algs(&skcipher_algs);
+		ret = ccp_register_aes_aeads(&aead_algs);
 		if (ret)
 			return ret;
+	}
 
-		ret = ccp_register_aes_aeads(&aead_algs);
+	if (!aes_disable && ccp_has_cap(CCP_CAPS_AES_XTS)) {
+		ret = ccp_register_aes_xts_algs(&skcipher_algs);
 		if (ret)
 			return ret;
 	}
 
-	if (!des3_disable) {
+	if (!des3_disable && ccp_has_cap(CCP_CAPS_DES3)) {
 		ret = ccp_register_des3_algs(&skcipher_algs);
 		if (ret)
 			return ret;
 	}
 
-	if (!sha_disable) {
+	if (!sha_disable && ccp_has_cap(CCP_CAPS_SHA)) {
 		ret = ccp_register_sha_algs(&hash_algs);
 		if (ret)
 			return ret;
 	}
 
-	if (!rsa_disable) {
+	if (!rsa_disable && ccp_has_cap(CCP_CAPS_RSA)) {
 		ret = ccp_register_rsa_algs(&akcipher_algs);
 		if (ret)
 			return ret;
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 05/11] crypto: ccp - split v5 command submission from queue run
  2026-11-25  7:45 [PATCH 00/11] crypto: ccp - add Hygon SM3/SM4 support Depei Yang
                   ` (3 preceding siblings ...)
  2026-11-25  7:46 ` [PATCH 04/11] crypto: ccp - register algorithms only for supported engines Depei Yang
@ 2026-11-25  7:46 ` Depei Yang
  2026-11-25  7:46 ` [PATCH 06/11] crypto: ccp - add SM3/SM4 command definitions Depei Yang
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Depei Yang @ 2026-11-25  7:46 UTC (permalink / raw)
  To: linux-crypto
  Cc: Tom Lendacky, Herbert Xu, David S. Miller, Gary R Hook,
	linux-kernel, Depei Yang, Shan Chen, Yongwei Xu, Xiaochen Shen

ccp5_do_cmd() copies one descriptor into the ring and immediately runs
the queue and waits for the completion.  Some engines prepare several
descriptors for a single operation and only need the queue run once.

Split the descriptor copy from running the queue.  ccp5_do_cmd() keeps
its behaviour by calling both helpers back to back; the SM engines will
run the queue through the new ->run action once a whole batch has been
submitted.

Reviewed-by: Shan Chen <chenshan@hygon.cn>
Tested-by: Yongwei Xu <xuyongwei@hygon.cn>
Signed-off-by: Depei Yang <yangdepei@hygon.cn>
---
 drivers/crypto/ccp/ccp-dev-v5.c | 38 ++++++++++++++++++++++++++-------
 drivers/crypto/ccp/ccp-dev.h    |  1 +
 2 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/drivers/crypto/ccp/ccp-dev-v5.c b/drivers/crypto/ccp/ccp-dev-v5.c
index 372ccf7d1c8b..f7c107b93ccc 100644
--- a/drivers/crypto/ccp/ccp-dev-v5.c
+++ b/drivers/crypto/ccp/ccp-dev-v5.c
@@ -218,14 +218,12 @@ static unsigned int ccp5_get_free_slots(struct ccp_cmd_queue *cmd_q)
 	return n % COMMANDS_PER_QUEUE; /* Always one unused spot */
 }
 
-static int ccp5_do_cmd(struct ccp5_desc *desc,
-		       struct ccp_cmd_queue *cmd_q)
+static void ccp5_submit_cmd(struct ccp5_desc *desc,
+			    struct ccp_cmd_queue *cmd_q)
 {
 	__le32 *mP;
 	u32 *dP;
-	u32 tail;
-	int	i;
-	int ret = 0;
+	int i;
 
 	cmd_q->total_ops++;
 
@@ -233,6 +231,7 @@ static int ccp5_do_cmd(struct ccp5_desc *desc,
 		CCP5_CMD_IOC(desc) = 1;
 		CCP5_CMD_SOC(desc) = 0;
 	}
+
 	mutex_lock(&cmd_q->q_mutex);
 
 	mP = (__le32 *)&cmd_q->qbase[cmd_q->qidx];
@@ -242,6 +241,16 @@ static int ccp5_do_cmd(struct ccp5_desc *desc,
 
 	cmd_q->qidx = (cmd_q->qidx + 1) % COMMANDS_PER_QUEUE;
 
+	mutex_unlock(&cmd_q->q_mutex);
+}
+
+static int ccp5_run_queue(struct ccp_cmd_queue *cmd_q, bool wait)
+{
+	u32 tail;
+	int ret = 0;
+
+	mutex_lock(&cmd_q->q_mutex);
+
 	/* The data used by this command must be flushed to memory */
 	wmb();
 
@@ -253,7 +262,7 @@ static int ccp5_do_cmd(struct ccp5_desc *desc,
 	iowrite32(cmd_q->qcontrol | CMD5_Q_RUN, cmd_q->reg_control);
 	mutex_unlock(&cmd_q->q_mutex);
 
-	if (CCP5_CMD_IOC(desc)) {
+	if (wait) {
 		/* Wait for the job to complete */
 		ret = wait_event_interruptible(cmd_q->int_queue,
 					       cmd_q->int_rcvd);
@@ -262,8 +271,7 @@ static int ccp5_do_cmd(struct ccp5_desc *desc,
 			 * moving the head pointer
 			 */
 			if (cmd_q->cmd_error)
-				ccp_log_error(cmd_q->ccp,
-					      cmd_q->cmd_error);
+				ccp_log_error(cmd_q->ccp, cmd_q->cmd_error);
 			iowrite32(tail, cmd_q->reg_head_lo);
 			if (!ret)
 				ret = -EIO;
@@ -274,6 +282,19 @@ static int ccp5_do_cmd(struct ccp5_desc *desc,
 	return ret;
 }
 
+static int ccp5_run_cmd(struct ccp_op *op)
+{
+	return ccp5_run_queue(op->cmd_q, op->ioc);
+}
+
+static int ccp5_do_cmd(struct ccp5_desc *desc,
+		       struct ccp_cmd_queue *cmd_q)
+{
+	ccp5_submit_cmd(desc, cmd_q);
+
+	return ccp5_run_queue(cmd_q, CCP5_CMD_IOC(desc));
+}
+
 static int ccp5_perform_aes(struct ccp_op *op)
 {
 	struct ccp5_desc desc;
@@ -1111,6 +1132,7 @@ static const struct ccp_actions ccp5_actions = {
 	.rsa = ccp5_perform_rsa,
 	.passthru = ccp5_perform_passthru,
 	.ecc = ccp5_perform_ecc,
+	.run = ccp5_run_cmd,
 	.sballoc = ccp_lsb_alloc,
 	.sbfree = ccp_lsb_free,
 	.init = ccp5_init,
diff --git a/drivers/crypto/ccp/ccp-dev.h b/drivers/crypto/ccp/ccp-dev.h
index ef88962ff6e6..16d5ea7773e2 100644
--- a/drivers/crypto/ccp/ccp-dev.h
+++ b/drivers/crypto/ccp/ccp-dev.h
@@ -659,6 +659,7 @@ struct ccp_actions {
 	int (*rsa)(struct ccp_op *);
 	int (*passthru)(struct ccp_op *);
 	int (*ecc)(struct ccp_op *);
+	int (*run)(struct ccp_op *op);
 	u32 (*sballoc)(struct ccp_cmd_queue *, unsigned int);
 	void (*sbfree)(struct ccp_cmd_queue *, unsigned int, unsigned int);
 	unsigned int (*get_free_slots)(struct ccp_cmd_queue *);
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 06/11] crypto: ccp - add SM3/SM4 command definitions
  2026-11-25  7:45 [PATCH 00/11] crypto: ccp - add Hygon SM3/SM4 support Depei Yang
                   ` (4 preceding siblings ...)
  2026-11-25  7:46 ` [PATCH 05/11] crypto: ccp - split v5 command submission from queue run Depei Yang
@ 2026-11-25  7:46 ` Depei Yang
  2026-11-25  7:46 ` [PATCH 07/11] crypto: ccp - add Hygon CCP device IDs and vdata Depei Yang
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Depei Yang @ 2026-11-25  7:46 UTC (permalink / raw)
  To: linux-crypto
  Cc: Tom Lendacky, Herbert Xu, David S. Miller, Gary R Hook,
	linux-kernel, Depei Yang, Shan Chen, Yongwei Xu, Xiaochen Shen

Add the public SM3/SM4 command structures, the engine identifiers and
the v5 descriptor fields.  No engine or algorithm is wired up yet; this
only describes the interface the SM engine patches will use.

The new engine identifiers come with the matching CCP_CAPS_* bits.  The
SM2 engine number (8) is left reserved.

Reviewed-by: Shan Chen <chenshan@hygon.cn>
Tested-by: Yongwei Xu <xuyongwei@hygon.cn>
Signed-off-by: Depei Yang <yangdepei@hygon.cn>
---
 drivers/crypto/ccp/ccp-crypto.h |  67 +++++++++++++
 drivers/crypto/ccp/ccp-dev-v5.c |  26 +++++
 drivers/crypto/ccp/ccp-dev.h    |  22 +++++
 include/linux/ccp.h             | 165 ++++++++++++++++++++++++++++++++
 4 files changed, 280 insertions(+)

diff --git a/drivers/crypto/ccp/ccp-crypto.h b/drivers/crypto/ccp/ccp-crypto.h
index e42450d07168..dfd16df920a3 100644
--- a/drivers/crypto/ccp/ccp-crypto.h
+++ b/drivers/crypto/ccp/ccp-crypto.h
@@ -21,6 +21,8 @@
 #include <crypto/hash.h>
 #include <crypto/sha1.h>
 #include <crypto/sha2.h>
+#include <crypto/sm3.h>
+#include <crypto/sm4.h>
 #include <crypto/akcipher.h>
 #include <crypto/skcipher.h>
 #include <crypto/internal/rsa.h>
@@ -258,6 +260,67 @@ struct ccp_rsa_req_ctx {
 #define	CCP_RSA_MAXMOD	(4 * 1024 / 8)
 #define	CCP5_RSA_MAXMOD	(16 * 1024 / 8)
 
+/***** SM3 related defines *****/
+struct ccp_sm3_ctx {
+	u32 key_len;
+	u8 key[SM3_BLOCK_SIZE];
+
+	u8 ipad[SM3_BLOCK_SIZE];
+
+	u8 opad[SM3_BLOCK_SIZE];
+	struct scatterlist opad_sg;
+
+	struct crypto_shash *hmac_tfm;
+};
+
+struct ccp_sm3_req_ctx {
+	u64 msg_bits;
+
+	unsigned int first;
+	unsigned int final;
+
+	struct scatterlist *src;
+	u32 nbytes;
+
+	u64 hash_cnt;
+	u32 hash_rem;
+
+	struct sg_table data_sg;
+
+	struct scatterlist ctx_sg;
+	u8 ctx[SM3_DIGEST_SIZE];
+
+	struct scatterlist buf_sg;
+	u32 buf_count;
+	u8 buf[SM3_BLOCK_SIZE];
+
+	struct ccp_cmd cmd;
+};
+
+struct ccp_sm3_exp_ctx {
+	u64 msg_bits;
+
+	u8 ctx[SM3_DIGEST_SIZE];
+
+	u32 buf_count;
+	u8 buf[SM3_BLOCK_SIZE];
+};
+
+/***** SM4 related defines *****/
+struct ccp_sm4_ctx {
+	struct scatterlist key_sg;
+	u8 key[SM4_KEY_SIZE];
+	u32 key_len;
+	u32 mode;
+};
+
+struct ccp_sm4_req_ctx {
+	struct scatterlist iv_sg;
+	u8 iv[SM4_BLOCK_SIZE];
+
+	struct ccp_cmd cmd;
+};
+
 /***** Common Context Structure *****/
 struct ccp_ctx {
 	int (*complete)(struct crypto_async_request *req, int ret);
@@ -267,6 +330,8 @@ struct ccp_ctx {
 		struct ccp_rsa_ctx rsa;
 		struct ccp_sha_ctx sha;
 		struct ccp_des3_ctx des3;
+		struct ccp_sm3_ctx sm3;
+		struct ccp_sm4_ctx sm4;
 	} u;
 };
 
@@ -282,5 +347,7 @@ int ccp_register_aes_aeads(struct list_head *head);
 int ccp_register_sha_algs(struct list_head *head);
 int ccp_register_des3_algs(struct list_head *head);
 int ccp_register_rsa_algs(struct list_head *head);
+int ccp_register_sm3_algs(struct list_head *head);
+int ccp_register_sm4_algs(struct list_head *head);
 
 #endif
diff --git a/drivers/crypto/ccp/ccp-dev-v5.c b/drivers/crypto/ccp/ccp-dev-v5.c
index f7c107b93ccc..9b1593703ec5 100644
--- a/drivers/crypto/ccp/ccp-dev-v5.c
+++ b/drivers/crypto/ccp/ccp-dev-v5.c
@@ -131,6 +131,23 @@ union ccp_function {
 		u16 type:2;
 		u16 mode:3;
 	} ecc;
+	struct {
+		u16 rsvd:10;
+		u16 type:4;
+		u16 rsvd2:1;
+	} sm3;
+	struct {
+		u16 rsvd:7;
+		u16 encrypt:1;
+		u16 mode:4;
+		u16 select:1;
+		u16 rsvd2:2;
+	} sm4;
+	struct {
+		u16 size:7;
+		u16 encrypt:1;
+		u16 step:7;
+	} sm4_ctr;
 	u16 raw;
 };
 
@@ -151,6 +168,13 @@ union ccp_function {
 #define	CCP_PT_BITWISE(p)	((p)->pt.bitwise)
 #define	CCP_ECC_MODE(p)		((p)->ecc.mode)
 #define	CCP_ECC_AFFINE(p)	((p)->ecc.one)
+#define	CCP_SM3_TYPE(p)		((p)->sm3.type)
+#define	CCP_SM4_ENCRYPT(p)	((p)->sm4.encrypt)
+#define	CCP_SM4_MODE(p)		((p)->sm4.mode)
+#define	CCP_SM4_SELECT(p)	((p)->sm4.select)
+#define	CCP_SM4_CTR_ENCRYPT(p)	((p)->sm4_ctr.encrypt)
+#define	CCP_SM4_CTR_STEP(p)	((p)->sm4_ctr.step)
+#define	CCP_SM4_CTR_SIZE(p)	((p)->sm4_ctr.size)
 
 /* Word 0 */
 #define CCP5_CMD_DW0(p)		((p)->dw0)
@@ -186,6 +210,8 @@ union ccp_function {
 #define CCP5_CMD_FIX_DST(p)	((p)->dw5.fields.fixed)
 #define CCP5_CMD_SHA_LO(p)	((p)->dw4.sha_len_lo)
 #define CCP5_CMD_SHA_HI(p)	((p)->dw5.sha_len_hi)
+#define CCP5_CMD_SM3_LO(p)	((p)->dw4.sm3_len_lo)
+#define CCP5_CMD_SM3_HI(p)	((p)->dw5.sm3_len_hi)
 
 /* Word 6/7 */
 #define CCP5_CMD_DW6(p)		((p)->key_lo)
diff --git a/drivers/crypto/ccp/ccp-dev.h b/drivers/crypto/ccp/ccp-dev.h
index 16d5ea7773e2..813a1b20c3c2 100644
--- a/drivers/crypto/ccp/ccp-dev.h
+++ b/drivers/crypto/ccp/ccp-dev.h
@@ -530,6 +530,23 @@ struct ccp_ecc_op {
 	enum ccp_ecc_function function;
 };
 
+struct ccp_sm3_op {
+	enum ccp_sm3_type type;
+	u64 msg_bits;
+};
+
+struct ccp_sm4_op {
+	enum ccp_sm4_action action;
+	enum ccp_sm4_mode mode;
+	u32 select;
+};
+
+struct ccp_sm4_ctr_op {
+	u32 size;
+	enum ccp_sm4_action action;
+	u32 step;
+};
+
 struct ccp_op {
 	struct ccp_cmd_queue *cmd_q;
 
@@ -553,6 +570,9 @@ struct ccp_op {
 		struct ccp_rsa_op rsa;
 		struct ccp_passthru_op passthru;
 		struct ccp_ecc_op ecc;
+		struct ccp_sm3_op sm3;
+		struct ccp_sm4_op sm4;
+		struct ccp_sm4_ctr_op sm4_ctr;
 	} u;
 };
 
@@ -601,6 +621,7 @@ struct dword3 {
 union dword4 {
 	u32 dst_lo;		/* NON-SHA	*/
 	u32 sha_len_lo;		/* SHA		*/
+	u32 sm3_len_lo;		/* SM3		*/
 };
 
 union dword5 {
@@ -611,6 +632,7 @@ union dword5 {
 		unsigned int  fixed:1;
 	} fields;
 	u32 sha_len_hi;
+	u32 sm3_len_hi;
 };
 
 struct dword7 {
diff --git a/include/linux/ccp.h b/include/linux/ccp.h
index 06c3b14a1eb6..43020be71bc4 100644
--- a/include/linux/ccp.h
+++ b/include/linux/ccp.h
@@ -617,6 +617,158 @@ struct ccp_ecc_engine {
 	u16 ecc_result;
 };
 
+/***** SM3 engine *****/
+/**
+ * enum ccp_sm3_type - type of SM3 operation
+ *
+ * @CCP_SM3_TYPE_256: SM3-256 operation
+ */
+enum ccp_sm3_type {
+	CCP_SM3_TYPE_256 = 2,
+};
+
+/**
+ * struct ccp_sm3_engine - CCP SM3 operation
+ * @type: Type of SM3 operation
+ * @ctx: current hash value
+ * @ctx_len: length in bytes of hash value
+ * @src: data to be used for this operation
+ * @src_len: length in bytes of data used for this operation
+ * @opad: data to be used for final HMAC operation
+ * @opad_len: length in bytes of data used for final HMAC operation
+ * @first: indicates first SM3 operation
+ * @final: indicates final SM3 operation
+ * @msg_bits: total length of the message in bits used in the final
+ *	      SM3 operation
+ *
+ * Variables required to be set when calling ccp_enqueue_cmd():
+ *   - type, ctx, ctx_len, src, src_len, first, final
+ *   - opad, opad_len for the final HMAC operation
+ *   - msg_bits when final is set
+ */
+struct ccp_sm3_engine {
+	enum ccp_sm3_type type;
+
+	struct scatterlist *ctx;
+	u32 ctx_len;
+
+	struct scatterlist *src;
+	u64 src_len;
+
+	struct scatterlist *opad;
+	u32 opad_len;
+
+	u32 first;
+	u32 final;
+	u64 msg_bits;
+};
+
+/***** SM4 engine *****/
+/**
+ * enum ccp_sm4_mode - SM4 operation mode
+ *
+ * These values are hardware encodings and must not be changed.
+ *
+ * @CCP_SM4_MODE_ECB: ECB mode
+ * @CCP_SM4_MODE_CBC: CBC mode
+ * @CCP_SM4_MODE_OFB: OFB mode
+ * @CCP_SM4_MODE_CFB: CFB mode
+ * @CCP_SM4_MODE_XTS: XTS mode
+ * @CCP_SM4_MODE_CTR: CTR mode
+ */
+enum ccp_sm4_mode {
+	CCP_SM4_MODE_ECB = 0,
+	CCP_SM4_MODE_CBC,
+	CCP_SM4_MODE_OFB,
+	CCP_SM4_MODE_CFB,
+	CCP_SM4_MODE_XTS,
+	CCP_SM4_MODE_CTR,
+};
+
+/**
+ * enum ccp_sm4_action - SM4 operation
+ *
+ * @CCP_SM4_ACTION_DECRYPT: SM4 decrypt operation
+ * @CCP_SM4_ACTION_ENCRYPT: SM4 encrypt operation
+ */
+enum ccp_sm4_action {
+	CCP_SM4_ACTION_DECRYPT = 0,
+	CCP_SM4_ACTION_ENCRYPT,
+};
+
+/**
+ * struct ccp_sm4_engine - CCP SM4 operation
+ * @mode: SM4 operation mode
+ * @action: SM4 operation (decrypt/encrypt)
+ * @select: indicating that the high-security engine is selected
+ * @key: key to be used for this SM4 operation
+ * @key_len: length in bytes of key
+ * @iv: IV to be used for this SM4 operation
+ * @iv_len: length in bytes of iv
+ * @src: data to be used for this operation
+ * @dst: data produced by this operation
+ * @src_len: length in bytes of data used for this operation
+ *
+ * Variables required to be set when calling ccp_enqueue_cmd():
+ *   - mode, action, key, key_len, src, dst, src_len
+ *   - iv, iv_len for any mode other than ECB
+ *   - key_len and iv_len must be 16B
+ *   - src_len must be a multiple of 16B
+ *
+ * The iv variable is used as both input and output. On completion of the
+ * SM4 operation the new IV overwrites the old IV.
+ */
+struct ccp_sm4_engine {
+	enum ccp_sm4_mode mode;
+	enum ccp_sm4_action action;
+	u32 select;
+
+	struct scatterlist *key;
+	u32 key_len;		/* In bytes */
+
+	struct scatterlist *iv;
+	u32 iv_len;		/* In bytes */
+
+	struct scatterlist *src, *dst;
+	u64 src_len;		/* In bytes */
+};
+
+/***** SM4_CTR engine *****/
+/**
+ * struct ccp_sm4_ctr_engine - CCP SM4_CTR operation
+ * @action: SM4_CTR operation (decrypt/encrypt)
+ * @size: counter bit size
+ * @step: counter increase step
+ * @key: key to be used for this SM4 operation
+ * @key_len: length in bytes of key
+ * @iv: IV to be used for this SM4 operation
+ * @iv_len: length in bytes of iv
+ * @src: data to be used for this operation
+ * @dst: data produced by this operation
+ * @src_len: length in bytes of data used for this operation
+ *
+ * Variables required to be set when calling ccp_enqueue_cmd():
+ *   - action, size, step, key, key_len, iv, iv_len, src, dst, src_len
+ *   - key_len and iv_len must be 16B
+ *
+ * The iv variable is used as both input and output. On completion of the
+ * SM4_CTR operation the new IV overwrites the old IV.
+ */
+struct ccp_sm4_ctr_engine {
+	enum ccp_sm4_action action;
+	u32 size;
+	u32 step;
+
+	struct scatterlist *key;
+	u32 key_len;		/* In bytes */
+
+	struct scatterlist *iv;
+	u32 iv_len;		/* In bytes */
+
+	struct scatterlist *src, *dst;
+	u64 src_len;		/* In bytes */
+};
+
 
 /**
  * enum ccp_engine - CCP operation identifiers
@@ -629,6 +781,9 @@ struct ccp_ecc_engine {
  * @CCP_ENGINE_PASSTHRU: pass-through operation
  * @CCP_ENGINE_ZLIB_DECOMPRESS: unused
  * @CCP_ENGINE_ECC: ECC operation
+ * @CCP_ENGINE_SM3: SM3 operation
+ * @CCP_ENGINE_SM4: SM4 operation
+ * @CCP_ENGINE_SM4_CTR: SM4 CTR operation
  */
 enum ccp_engine {
 	CCP_ENGINE_AES = 0,
@@ -639,6 +794,10 @@ enum ccp_engine {
 	CCP_ENGINE_PASSTHRU,
 	CCP_ENGINE_ZLIB_DECOMPRESS,
 	CCP_ENGINE_ECC,
+	/* 8 is reserved for SM2 */
+	CCP_ENGINE_SM3 = 9,
+	CCP_ENGINE_SM4,
+	CCP_ENGINE_SM4_CTR,
 	/* private: */
 	CCP_ENGINE__LAST,
 };
@@ -651,6 +810,9 @@ enum ccp_engine {
 #define CCP_CAPS_RSA		BIT(CCP_ENGINE_RSA)
 #define CCP_CAPS_PASSTHRU	BIT(CCP_ENGINE_PASSTHRU)
 #define CCP_CAPS_ECC		BIT(CCP_ENGINE_ECC)
+#define CCP_CAPS_SM3		BIT(CCP_ENGINE_SM3)
+#define CCP_CAPS_SM4		BIT(CCP_ENGINE_SM4)
+#define CCP_CAPS_SM4_CTR	BIT(CCP_ENGINE_SM4_CTR)
 
 /* Flag values for flags member of ccp_cmd */
 #define CCP_CMD_MAY_BACKLOG		0x00000001
@@ -697,6 +859,9 @@ struct ccp_cmd {
 		struct ccp_passthru_engine passthru;
 		struct ccp_passthru_nomap_engine passthru_nomap;
 		struct ccp_ecc_engine ecc;
+		struct ccp_sm3_engine sm3;
+		struct ccp_sm4_engine sm4;
+		struct ccp_sm4_ctr_engine sm4_ctr;
 	} u;
 
 	/* Completion callback support */
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 07/11] crypto: ccp - add Hygon CCP device IDs and vdata
  2026-11-25  7:45 [PATCH 00/11] crypto: ccp - add Hygon SM3/SM4 support Depei Yang
                   ` (5 preceding siblings ...)
  2026-11-25  7:46 ` [PATCH 06/11] crypto: ccp - add SM3/SM4 command definitions Depei Yang
@ 2026-11-25  7:46 ` Depei Yang
  2026-11-25  7:46 ` [PATCH 08/11] crypto: ccp - handle only supported interrupts in v5 irq bh Depei Yang
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Depei Yang @ 2026-11-25  7:46 UTC (permalink / raw)
  To: linux-crypto
  Cc: Tom Lendacky, Herbert Xu, David S. Miller, Gary R Hook,
	linux-kernel, Depei Yang, Shan Chen, Yongwei Xu, Xiaochen Shen

Hygon CCP devices use the v5 programming model.  Add the PCI IDs and a
minimal vdata table.  The get_caps callback is a placeholder that
advertises only the SM engines; a later patch replaces it with
register-based detection.

Reviewed-by: Shan Chen <chenshan@hygon.cn>
Tested-by: Yongwei Xu <xuyongwei@hygon.cn>
Signed-off-by: Depei Yang <yangdepei@hygon.cn>
---
 drivers/crypto/ccp/ccp-dev-v5.c | 32 ++++++++++++++++++++++++++++++
 drivers/crypto/ccp/ccp-dev.h    |  2 ++
 drivers/crypto/ccp/sp-pci.c     | 35 +++++++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+)

diff --git a/drivers/crypto/ccp/ccp-dev-v5.c b/drivers/crypto/ccp/ccp-dev-v5.c
index 9b1593703ec5..34832b830785 100644
--- a/drivers/crypto/ccp/ccp-dev-v5.c
+++ b/drivers/crypto/ccp/ccp-dev-v5.c
@@ -1198,3 +1198,35 @@ const struct ccp_vdata ccpv5b = {
 	.rsamax = CCP5_RSA_MAX_WIDTH,
 	.get_caps = ccp5_get_caps,
 };
+
+/*
+ * Hygon CCP is a v5 derivative with the SM3/SM4 engines added.  It
+ * uses the same queue, LSB and interrupt model as the AMD v5 CCP.
+ *
+ * TODO: derive the legacy engines from the Hygon engine register once
+ * its layout is confirmed.  Until then only the engines that cannot be
+ * detected from a register are advertised.
+ */
+static void ccp5_hygon_get_caps(struct ccp_device *ccp)
+{
+	ccp->caps.engines = CCP_CAPS_SM3 | CCP_CAPS_SM4 | CCP_CAPS_SM4_CTR;
+}
+
+const struct ccp_vdata ccpv5a_hygon = {
+	.version = CCP_VERSION(5, 0),
+	.setup = ccp5_config,
+	.perform = &ccp5_actions,
+	.offset = 0x0,
+	.rsamax = CCP5_RSA_MAX_WIDTH,
+	.get_caps = ccp5_hygon_get_caps,
+};
+
+const struct ccp_vdata ccpv5b_hygon = {
+	.version = CCP_VERSION(5, 0),
+	.dma_chan_attr = DMA_PRIVATE,
+	.setup = ccp5other_config,
+	.perform = &ccp5_actions,
+	.offset = 0x0,
+	.rsamax = CCP5_RSA_MAX_WIDTH,
+	.get_caps = ccp5_hygon_get_caps,
+};
diff --git a/drivers/crypto/ccp/ccp-dev.h b/drivers/crypto/ccp/ccp-dev.h
index 813a1b20c3c2..f499e6868d82 100644
--- a/drivers/crypto/ccp/ccp-dev.h
+++ b/drivers/crypto/ccp/ccp-dev.h
@@ -694,5 +694,7 @@ extern const struct ccp_vdata ccpv3_platform;
 extern const struct ccp_vdata ccpv3;
 extern const struct ccp_vdata ccpv5a;
 extern const struct ccp_vdata ccpv5b;
+extern const struct ccp_vdata ccpv5a_hygon;
+extern const struct ccp_vdata ccpv5b_hygon;
 
 #endif
diff --git a/drivers/crypto/ccp/sp-pci.c b/drivers/crypto/ccp/sp-pci.c
index ede6ff9ad0c2..dafd934836dc 100644
--- a/drivers/crypto/ccp/sp-pci.c
+++ b/drivers/crypto/ccp/sp-pci.c
@@ -551,6 +551,33 @@ static const struct sp_dev_vdata dev_vdata[] = {
 	},
 
 };
+
+/*
+ * Hygon secure processor devices.  They are CCP v5 derivatives, so
+ * they reuse the v5 CCP programming model.  The PSP/CSV side is not
+ * described here.
+ */
+static const struct sp_dev_vdata hygon_dev_vdata[] = {
+	{	/* 0 */
+		.bar = 2,
+#ifdef CONFIG_CRYPTO_DEV_SP_CCP
+		.ccp_vdata = &ccpv5a_hygon,
+#endif
+	},
+	{	/* 1 */
+		.bar = 2,
+#ifdef CONFIG_CRYPTO_DEV_SP_CCP
+		.ccp_vdata = &ccpv5b_hygon,
+#endif
+	},
+	{	/* 2 */
+		.bar = 2,
+#ifdef CONFIG_CRYPTO_DEV_SP_CCP
+		.ccp_vdata = &ccpv5a_hygon,
+#endif
+	},
+};
+
 static const struct pci_device_id sp_pci_table[] = {
 	{ PCI_VDEVICE(AMD, 0x1537), .driver_data = (kernel_ulong_t)&dev_vdata[0] },
 	{ PCI_VDEVICE(AMD, 0x1456), .driver_data = (kernel_ulong_t)&dev_vdata[1] },
@@ -565,6 +592,14 @@ static const struct pci_device_id sp_pci_table[] = {
 	{ PCI_VDEVICE(AMD, 0x156E), .driver_data = (kernel_ulong_t)&dev_vdata[8] },
 	{ PCI_VDEVICE(AMD, 0x17D8), .driver_data = (kernel_ulong_t)&dev_vdata[8] },
 	{ PCI_VDEVICE(AMD, 0x115A), .driver_data = (kernel_ulong_t)&dev_vdata[9] },
+	{ PCI_VDEVICE(HYGON, 0x1456), .driver_data = (kernel_ulong_t)&hygon_dev_vdata[0] },
+	{ PCI_VDEVICE(HYGON, 0x1468), .driver_data = (kernel_ulong_t)&hygon_dev_vdata[1] },
+	{ PCI_VDEVICE(HYGON, 0x1486), .driver_data = (kernel_ulong_t)&hygon_dev_vdata[2] },
+	{ PCI_VDEVICE(HYGON, 0x14b8), .driver_data = (kernel_ulong_t)&hygon_dev_vdata[1] },
+	{ PCI_VDEVICE(HYGON, 0x14a6), .driver_data = (kernel_ulong_t)&hygon_dev_vdata[2] },
+	{ PCI_VDEVICE(HYGON, 0x14d8), .driver_data = (kernel_ulong_t)&hygon_dev_vdata[1] },
+	{ PCI_VDEVICE(HYGON, 0x14c6), .driver_data = (kernel_ulong_t)&hygon_dev_vdata[2] },
+	{ PCI_VDEVICE(HYGON, 0x2006), .driver_data = (kernel_ulong_t)&hygon_dev_vdata[2] },
 	/* Last entry must be zero */
 	{ }
 };
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 08/11] crypto: ccp - handle only supported interrupts in v5 irq bh
  2026-11-25  7:45 [PATCH 00/11] crypto: ccp - add Hygon SM3/SM4 support Depei Yang
                   ` (6 preceding siblings ...)
  2026-11-25  7:46 ` [PATCH 07/11] crypto: ccp - add Hygon CCP device IDs and vdata Depei Yang
@ 2026-11-25  7:46 ` Depei Yang
  2026-11-25  7:46 ` [PATCH 09/11] crypto: ccp - detect the Hygon CCP engines from the version register Depei Yang
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: Depei Yang @ 2026-11-25  7:46 UTC (permalink / raw)
  To: linux-crypto
  Cc: Tom Lendacky, Herbert Xu, David S. Miller, Gary R Hook,
	linux-kernel, Depei Yang, Shan Chen, Yongwei Xu, Xiaochen Shen,
	stable

The Hygon CCP reports INT_EMPTY_QUEUE in the interrupt status register,
which SUPPORTED_INTERRUPTS does not include.  Treating it as a
completion makes the command queue thread observe a spurious
completion.  Only process the bits that the driver enables.

AMD hardware has not been observed to report this bit; for the status
values it produces the new check is a no-op.

Fixes: 6263b51eb319 ("crypto: ccp - Change ISR handler method for a v5 CCP")
Cc: stable@vger.kernel.org
Reviewed-by: Shan Chen <chenshan@hygon.cn>
Tested-by: Yongwei Xu <xuyongwei@hygon.cn>
Signed-off-by: Depei Yang <yangdepei@hygon.cn>
---
 drivers/crypto/ccp/ccp-dev-v5.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/crypto/ccp/ccp-dev-v5.c b/drivers/crypto/ccp/ccp-dev-v5.c
index 34832b830785..d2586625700b 100644
--- a/drivers/crypto/ccp/ccp-dev-v5.c
+++ b/drivers/crypto/ccp/ccp-dev-v5.c
@@ -792,7 +792,7 @@ static void ccp5_irq_bh(unsigned long data)
 
 		status = ioread32(cmd_q->reg_interrupt_status);
 
-		if (status) {
+		if (status & SUPPORTED_INTERRUPTS) {
 			cmd_q->int_status = status;
 			cmd_q->q_status = ioread32(cmd_q->reg_status);
 			cmd_q->q_int_status = ioread32(cmd_q->reg_int_status);
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 09/11] crypto: ccp - detect the Hygon CCP engines from the version register
  2026-11-25  7:45 [PATCH 00/11] crypto: ccp - add Hygon SM3/SM4 support Depei Yang
                   ` (7 preceding siblings ...)
  2026-11-25  7:46 ` [PATCH 08/11] crypto: ccp - handle only supported interrupts in v5 irq bh Depei Yang
@ 2026-11-25  7:46 ` Depei Yang
  2026-11-25  7:46 ` [PATCH 10/11] crypto: ccp - add SM4 engine and skcipher algorithms Depei Yang
  2026-11-25  7:46 ` [PATCH 11/11] crypto: ccp - add SM3 engine and ahash algorithms Depei Yang
  10 siblings, 0 replies; 12+ messages in thread
From: Depei Yang @ 2026-11-25  7:46 UTC (permalink / raw)
  To: linux-crypto
  Cc: Tom Lendacky, Herbert Xu, David S. Miller, Gary R Hook,
	linux-kernel, Depei Yang, Shan Chen, Yongwei Xu, Xiaochen Shen

The Hygon CCP reports the engines it implements in the upper bits of the
CCP version register, starting at bit 6: SM2, SM3, SM4, SM4-CTR, ECC,
AES, SHA2, TRNG and SM4-GCM.

Translate the presence bits into engine capabilities in
ccp5_hygon_get_caps().  The same function stores the raw register value
and the engine version read from the new CMD5_PSP_CCP_ENG_VERSION
register; neither is used yet.  Only the SM engines are advertised for
now; the other presence bits are defined but not translated until those
engines are enabled.

Reviewed-by: Shan Chen <chenshan@hygon.cn>
Tested-by: Yongwei Xu <xuyongwei@hygon.cn>
Signed-off-by: Depei Yang <yangdepei@hygon.cn>
---
 drivers/crypto/ccp/ccp-dev-v5.c | 42 +++++++++++++++++++++++++++++----
 drivers/crypto/ccp/ccp-dev.h    |  1 +
 2 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/drivers/crypto/ccp/ccp-dev-v5.c b/drivers/crypto/ccp/ccp-dev-v5.c
index d2586625700b..0284be103ad1 100644
--- a/drivers/crypto/ccp/ccp-dev-v5.c
+++ b/drivers/crypto/ccp/ccp-dev-v5.c
@@ -1203,13 +1203,47 @@ const struct ccp_vdata ccpv5b = {
  * Hygon CCP is a v5 derivative with the SM3/SM4 engines added.  It
  * uses the same queue, LSB and interrupt model as the AMD v5 CCP.
  *
- * TODO: derive the legacy engines from the Hygon engine register once
- * its layout is confirmed.  Until then only the engines that cannot be
- * detected from a register are advertised.
+ * Engine presence is reported by the bits 6 and above of the CCP
+ * version register; the layout differs from the AMD one.
  */
+#define HYGON_RI_SM2		BIT(6)
+#define HYGON_RI_SM3		BIT(7)
+#define HYGON_RI_SM4		BIT(8)
+#define HYGON_RI_SM4_CTR	BIT(9)
+#define HYGON_RI_ECC		BIT(10)
+#define HYGON_RI_AES		BIT(11)
+#define HYGON_RI_SHA2		BIT(12)
+#define HYGON_RI_TRNG		BIT(13)
+#define HYGON_RI_SM4_GCM	BIT(14)
+
 static void ccp5_hygon_get_caps(struct ccp_device *ccp)
 {
-	ccp->caps.engines = CCP_CAPS_SM3 | CCP_CAPS_SM4 | CCP_CAPS_SM4_CTR;
+	/*
+	 * Only the SM engines are advertised for now.  SM2, ECC, AES,
+	 * SHA2, TRNG and SM4-GCM are not enabled for the Hygon CCP yet;
+	 * they will be added to the table once the driver supports them
+	 * on this hardware.
+	 */
+	static const struct {
+		u32 presence;
+		u32 cap;
+	} engines[] = {
+		{ HYGON_RI_SM3,		CCP_CAPS_SM3 },
+		{ HYGON_RI_SM4,		CCP_CAPS_SM4 },
+		{ HYGON_RI_SM4_CTR,	CCP_CAPS_SM4_CTR },
+	};
+	u32 version;
+	unsigned int i;
+
+	version = ioread32(ccp->io_regs + CMD5_PSP_CCP_VERSION);
+	ccp->caps.ccp_version = version;
+	ccp->caps.engine_version =
+		ioread32(ccp->io_regs + CMD5_PSP_CCP_ENG_VERSION);
+	ccp->caps.engines = 0;
+
+	for (i = 0; i < ARRAY_SIZE(engines); i++)
+		if (version & engines[i].presence)
+			ccp->caps.engines |= engines[i].cap;
 }
 
 const struct ccp_vdata ccpv5a_hygon = {
diff --git a/drivers/crypto/ccp/ccp-dev.h b/drivers/crypto/ccp/ccp-dev.h
index f499e6868d82..c2b8068bc123 100644
--- a/drivers/crypto/ccp/ccp-dev.h
+++ b/drivers/crypto/ccp/ccp-dev.h
@@ -71,6 +71,7 @@
 #define LSB_PRIVATE_MASK_LO_OFFSET	0x20
 #define LSB_PRIVATE_MASK_HI_OFFSET	0x24
 #define CMD5_PSP_CCP_VERSION		0x100
+#define CMD5_PSP_CCP_ENG_VERSION	0x104
 
 #define CMD5_Q_CONTROL_BASE		0x0000
 #define CMD5_Q_TAIL_LO_BASE		0x0004
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 10/11] crypto: ccp - add SM4 engine and skcipher algorithms
  2026-11-25  7:45 [PATCH 00/11] crypto: ccp - add Hygon SM3/SM4 support Depei Yang
                   ` (8 preceding siblings ...)
  2026-11-25  7:46 ` [PATCH 09/11] crypto: ccp - detect the Hygon CCP engines from the version register Depei Yang
@ 2026-11-25  7:46 ` Depei Yang
  2026-11-25  7:46 ` [PATCH 11/11] crypto: ccp - add SM3 engine and ahash algorithms Depei Yang
  10 siblings, 0 replies; 12+ messages in thread
From: Depei Yang @ 2026-11-25  7:46 UTC (permalink / raw)
  To: linux-crypto
  Cc: Tom Lendacky, Herbert Xu, David S. Miller, Gary R Hook,
	linux-kernel, Depei Yang, Shan Chen, Yongwei Xu, Xiaochen Shen

Add the SM4 and SM4-CTR command builders, the ccp-ops request handling
and the skcipher front end.  ecb(sm4) and cbc(sm4) are registered when a
device advertises the SM4 engine; ctr(sm4) needs the separately reported
SM4-CTR engine.

Reviewed-by: Shan Chen <chenshan@hygon.cn>
Tested-by: Yongwei Xu <xuyongwei@hygon.cn>
Signed-off-by: Depei Yang <yangdepei@hygon.cn>
---
 drivers/crypto/ccp/Makefile          |   3 +-
 drivers/crypto/ccp/ccp-crypto-main.c |  16 ++
 drivers/crypto/ccp/ccp-crypto-sm4.c  | 254 +++++++++++++++++++++++
 drivers/crypto/ccp/ccp-crypto.h      |   1 +
 drivers/crypto/ccp/ccp-dev-v5.c      |  65 ++++++
 drivers/crypto/ccp/ccp-dev.h         |   2 +
 drivers/crypto/ccp/ccp-ops.c         | 296 +++++++++++++++++++++++++++
 7 files changed, 636 insertions(+), 1 deletion(-)
 create mode 100644 drivers/crypto/ccp/ccp-crypto-sm4.c

diff --git a/drivers/crypto/ccp/Makefile b/drivers/crypto/ccp/Makefile
index 0424e08561ef..dd0177961465 100644
--- a/drivers/crypto/ccp/Makefile
+++ b/drivers/crypto/ccp/Makefile
@@ -28,4 +28,5 @@ ccp-crypto-objs := ccp-crypto-main.o \
 		   ccp-crypto-aes-galois.o \
 		   ccp-crypto-des3.o \
 		   ccp-crypto-rsa.o \
-		   ccp-crypto-sha.o
+		   ccp-crypto-sha.o \
+		   ccp-crypto-sm4.o
diff --git a/drivers/crypto/ccp/ccp-crypto-main.c b/drivers/crypto/ccp/ccp-crypto-main.c
index 388ecb1694e5..e68c843ce3fb 100644
--- a/drivers/crypto/ccp/ccp-crypto-main.c
+++ b/drivers/crypto/ccp/ccp-crypto-main.c
@@ -42,6 +42,10 @@ static unsigned int rsa_disable;
 module_param(rsa_disable, uint, 0444);
 MODULE_PARM_DESC(rsa_disable, "Disable use of RSA - any non-zero value");
 
+static unsigned int sm_disable;
+module_param(sm_disable, uint, 0444);
+MODULE_PARM_DESC(sm_disable, "Disable use of SM3/SM4 - any non-zero value");
+
 /* List heads for the supported algorithms */
 static LIST_HEAD(hash_algs);
 static LIST_HEAD(skcipher_algs);
@@ -363,6 +367,18 @@ static int ccp_register_algs(void)
 			return ret;
 	}
 
+	if (!sm_disable && ccp_has_cap(CCP_CAPS_SM4)) {
+		ret = ccp_register_sm4_algs(&skcipher_algs);
+		if (ret)
+			return ret;
+	}
+
+	if (!sm_disable && ccp_has_cap(CCP_CAPS_SM4_CTR)) {
+		ret = ccp_register_sm4_ctr_algs(&skcipher_algs);
+		if (ret)
+			return ret;
+	}
+
 	return 0;
 }
 
diff --git a/drivers/crypto/ccp/ccp-crypto-sm4.c b/drivers/crypto/ccp/ccp-crypto-sm4.c
new file mode 100644
index 000000000000..fbdba8cd0da0
--- /dev/null
+++ b/drivers/crypto/ccp/ccp-crypto-sm4.c
@@ -0,0 +1,254 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * CCP SM4 crypto API support
+ *
+ * Copyright (C) 2022 Hygon Info Technologies Ltd.
+ */
+
+#include <linux/module.h>
+#include <linux/sched.h>
+#include <linux/delay.h>
+#include <linux/scatterlist.h>
+#include <linux/crypto.h>
+#include <crypto/algapi.h>
+#include <crypto/internal/skcipher.h>
+#include <crypto/scatterwalk.h>
+
+#include "ccp-crypto.h"
+
+enum ccp_sm4_alg_mode {
+	CCP_SM4_ALG_MODE_ECB = CCP_SM4_MODE_ECB,
+	CCP_SM4_ALG_MODE_CBC = CCP_SM4_MODE_CBC,
+	CCP_SM4_ALG_MODE_CTR = CCP_SM4_MODE_CTR,
+};
+
+static int ccp_sm4_complete(struct crypto_async_request *async_req, int ret)
+{
+	struct skcipher_request *req = skcipher_request_cast(async_req);
+	struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(crypto_skcipher_reqtfm(req));
+	struct ccp_sm4_req_ctx *rctx = skcipher_request_ctx_dma(req);
+
+	if (ret)
+		return ret;
+
+	if (ctx->u.sm4.mode != CCP_SM4_ALG_MODE_ECB) {
+		memcpy(req->iv, rctx->iv, SM4_BLOCK_SIZE);
+		memset(rctx->iv, 0, SM4_BLOCK_SIZE);
+	}
+
+	return 0;
+}
+
+static int ccp_sm4_setkey(struct crypto_skcipher *tfm, const u8 *key,
+			  unsigned int key_len)
+{
+	struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
+
+	if (!key || key_len != SM4_KEY_SIZE)
+		return -EINVAL;
+
+	memcpy(ctx->u.sm4.key, key, key_len);
+	sg_init_one(&ctx->u.sm4.key_sg, ctx->u.sm4.key, key_len);
+
+	ctx->u.sm4.key_len = key_len;
+
+	return 0;
+}
+
+static int ccp_sm4_crypt(struct skcipher_request *req, bool encrypt)
+{
+	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
+	struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
+	struct ccp_sm4_req_ctx *rctx = skcipher_request_ctx_dma(req);
+	struct scatterlist *iv_sg = NULL;
+	struct ccp_cmd *cmd = &rctx->cmd;
+	enum ccp_sm4_alg_mode mode = ctx->u.sm4.mode;
+	enum ccp_sm4_action action;
+	int ret;
+
+	if (!ctx->u.sm4.key_len)
+		return -ENOKEY;
+
+	if (mode != CCP_SM4_ALG_MODE_CTR &&
+	    (req->cryptlen & (SM4_BLOCK_SIZE - 1)))
+		return -EINVAL;
+
+	if (mode != CCP_SM4_ALG_MODE_ECB) {
+		if (!req->iv)
+			return -EINVAL;
+
+		memcpy(rctx->iv, req->iv, SM4_BLOCK_SIZE);
+		iv_sg = &rctx->iv_sg;
+		sg_init_one(iv_sg, rctx->iv, SM4_BLOCK_SIZE);
+	}
+
+	memset(cmd, 0, sizeof(*cmd));
+	INIT_LIST_HEAD(&cmd->entry);
+	action = encrypt ? CCP_SM4_ACTION_ENCRYPT : CCP_SM4_ACTION_DECRYPT;
+
+	if (mode == CCP_SM4_ALG_MODE_CTR) {
+		cmd->engine = CCP_ENGINE_SM4_CTR;
+		cmd->u.sm4_ctr.action = action;
+		cmd->u.sm4_ctr.size = 63;
+		cmd->u.sm4_ctr.step = 1;
+
+		cmd->u.sm4_ctr.key = &ctx->u.sm4.key_sg;
+		cmd->u.sm4_ctr.key_len = SM4_KEY_SIZE;
+		cmd->u.sm4_ctr.iv = iv_sg;
+		cmd->u.sm4_ctr.iv_len = SM4_BLOCK_SIZE;
+
+		cmd->u.sm4_ctr.src = req->src;
+		cmd->u.sm4_ctr.dst = req->dst;
+		cmd->u.sm4_ctr.src_len = req->cryptlen;
+	} else {
+		cmd->engine = CCP_ENGINE_SM4;
+		cmd->u.sm4.mode = (enum ccp_sm4_mode)mode;
+		cmd->u.sm4.action = action;
+
+		cmd->u.sm4.key = &ctx->u.sm4.key_sg;
+		cmd->u.sm4.key_len = ctx->u.sm4.key_len;
+		cmd->u.sm4.iv = iv_sg;
+		cmd->u.sm4.iv_len = iv_sg ? SM4_BLOCK_SIZE : 0;
+
+		cmd->u.sm4.src = req->src;
+		cmd->u.sm4.dst = req->dst;
+		cmd->u.sm4.src_len = req->cryptlen;
+	}
+
+	ret = ccp_crypto_enqueue_request(&req->base, cmd);
+
+	return ret;
+}
+
+static int ccp_sm4_encrypt(struct skcipher_request *req)
+{
+	return ccp_sm4_crypt(req, true);
+}
+
+static int ccp_sm4_decrypt(struct skcipher_request *req)
+{
+	return ccp_sm4_crypt(req, false);
+}
+
+static int ccp_sm4_init_tfm(struct crypto_skcipher *tfm)
+{
+	struct ccp_crypto_skcipher_alg *alg = ccp_crypto_skcipher_alg(tfm);
+	struct ccp_ctx *ctx = crypto_skcipher_ctx_dma(tfm);
+
+	ctx->complete = ccp_sm4_complete;
+	ctx->u.sm4.mode = alg->mode;
+
+	crypto_skcipher_set_reqsize_dma(tfm, sizeof(struct ccp_sm4_req_ctx));
+
+	return 0;
+}
+
+static const struct skcipher_alg ccp_sm4_defaults = {
+	.setkey			= ccp_sm4_setkey,
+	.encrypt		= ccp_sm4_encrypt,
+	.decrypt		= ccp_sm4_decrypt,
+	.min_keysize		= SM4_KEY_SIZE,
+	.max_keysize		= SM4_KEY_SIZE,
+	.init			= ccp_sm4_init_tfm,
+
+	.base.cra_flags		= CRYPTO_ALG_ASYNC |
+				  CRYPTO_ALG_ALLOCATES_MEMORY |
+				  CRYPTO_ALG_KERN_DRIVER_ONLY |
+				  CRYPTO_ALG_NEED_FALLBACK,
+	.base.cra_blocksize	= SM4_BLOCK_SIZE,
+	.base.cra_ctxsize	= sizeof(struct ccp_ctx) + CRYPTO_DMA_PADDING,
+	.base.cra_priority	= CCP_CRA_PRIORITY,
+	.base.cra_module	= THIS_MODULE,
+};
+
+struct ccp_sm4_def {
+	enum ccp_sm4_alg_mode mode;
+	const char *name;
+	const char *driver_name;
+	unsigned int blocksize;
+	unsigned int ivsize;
+	const struct skcipher_alg *alg_defaults;
+};
+
+static const struct ccp_sm4_def sm4_algs[] = {
+	{
+		.mode		= CCP_SM4_ALG_MODE_ECB,
+		.name		= "ecb(sm4)",
+		.driver_name	= "ecb-sm4-ccp",
+		.blocksize	= SM4_BLOCK_SIZE,
+		.ivsize		= 0,
+		.alg_defaults	= &ccp_sm4_defaults,
+	},
+	{
+		.mode		= CCP_SM4_ALG_MODE_CBC,
+		.name		= "cbc(sm4)",
+		.driver_name	= "cbc-sm4-ccp",
+		.blocksize	= SM4_BLOCK_SIZE,
+		.ivsize		= SM4_BLOCK_SIZE,
+		.alg_defaults	= &ccp_sm4_defaults,
+	},
+};
+
+/* SM4-CTR is a separate engine on the CCP */
+static const struct ccp_sm4_def sm4_ctr_alg = {
+	.mode		= CCP_SM4_ALG_MODE_CTR,
+	.name		= "ctr(sm4)",
+	.driver_name	= "ctr-sm4-ccp",
+	.blocksize	= 1,
+	.ivsize		= SM4_BLOCK_SIZE,
+	.alg_defaults	= &ccp_sm4_defaults,
+};
+
+static int ccp_register_sm4_alg(struct list_head *head,
+				const struct ccp_sm4_def *def)
+{
+	struct ccp_crypto_skcipher_alg *ccp_alg;
+	struct skcipher_alg *alg;
+	int ret;
+
+	ccp_alg = kzalloc_obj(*ccp_alg);
+	if (!ccp_alg)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&ccp_alg->entry);
+
+	ccp_alg->mode = def->mode;
+
+	/* copy the defaults and override as necessary */
+	alg = &ccp_alg->alg;
+	*alg = *def->alg_defaults;
+	strscpy(alg->base.cra_name, def->name);
+	strscpy(alg->base.cra_driver_name, def->driver_name);
+	alg->base.cra_blocksize = def->blocksize;
+	alg->ivsize = def->ivsize;
+
+	ret = crypto_register_skcipher(alg);
+	if (ret) {
+		pr_err("%s skcipher algorithm registration error (%d)\n",
+		       alg->base.cra_name, ret);
+		kfree(ccp_alg);
+		return ret;
+	}
+
+	list_add(&ccp_alg->entry, head);
+
+	return 0;
+}
+
+int ccp_register_sm4_algs(struct list_head *head)
+{
+	int i, ret;
+
+	for (i = 0; i < ARRAY_SIZE(sm4_algs); i++) {
+		ret = ccp_register_sm4_alg(head, &sm4_algs[i]);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
+int ccp_register_sm4_ctr_algs(struct list_head *head)
+{
+	return ccp_register_sm4_alg(head, &sm4_ctr_alg);
+}
diff --git a/drivers/crypto/ccp/ccp-crypto.h b/drivers/crypto/ccp/ccp-crypto.h
index dfd16df920a3..9d6f2983f39f 100644
--- a/drivers/crypto/ccp/ccp-crypto.h
+++ b/drivers/crypto/ccp/ccp-crypto.h
@@ -349,5 +349,6 @@ int ccp_register_des3_algs(struct list_head *head);
 int ccp_register_rsa_algs(struct list_head *head);
 int ccp_register_sm3_algs(struct list_head *head);
 int ccp_register_sm4_algs(struct list_head *head);
+int ccp_register_sm4_ctr_algs(struct list_head *head);
 
 #endif
diff --git a/drivers/crypto/ccp/ccp-dev-v5.c b/drivers/crypto/ccp/ccp-dev-v5.c
index 0284be103ad1..8cbbb8d81d17 100644
--- a/drivers/crypto/ccp/ccp-dev-v5.c
+++ b/drivers/crypto/ccp/ccp-dev-v5.c
@@ -7,6 +7,7 @@
  * Author: Gary R Hook <gary.hook@amd.com>
  */
 
+#include <crypto/sm4.h>
 #include <linux/kernel.h>
 #include <linux/kthread.h>
 #include <linux/dma-mapping.h>
@@ -631,6 +632,68 @@ static int ccp5_perform_ecc(struct ccp_op *op)
 	return ccp5_do_cmd(&desc, op->cmd_q);
 }
 
+static int ccp5_perform_sm4_common(struct ccp_op *op, enum ccp_engine engine,
+				   u32 function)
+{
+	struct ccp5_desc desc;
+	u32 key_addr = op->sb_ctx * LSB_ITEM_SIZE + SM4_BLOCK_SIZE;
+
+	memset(&desc, 0, Q_DESC_SIZE);
+
+	CCP5_CMD_ENGINE(&desc) = engine;
+
+	CCP5_CMD_SOC(&desc) = op->soc;
+	CCP5_CMD_IOC(&desc) = op->ioc;
+	CCP5_CMD_INIT(&desc) = op->init;
+	CCP5_CMD_EOM(&desc) = op->eom;
+	CCP5_CMD_PROT(&desc) = 0;
+
+	CCP5_CMD_FUNCTION(&desc) = function;
+
+	CCP5_CMD_LEN(&desc) = op->src.u.dma.length;
+
+	CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma);
+	CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma);
+	CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
+	CCP5_CMD_LSB_ID(&desc) = op->sb_ctx;
+
+	CCP5_CMD_DST_LO(&desc) = ccp_addr_lo(&op->dst.u.dma);
+	CCP5_CMD_DST_HI(&desc) = ccp_addr_hi(&op->dst.u.dma);
+	CCP5_CMD_DST_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
+
+	CCP5_CMD_KEY_LO(&desc) = lower_32_bits(key_addr);
+	CCP5_CMD_KEY_HI(&desc) = 0;
+	CCP5_CMD_KEY_MEM(&desc) = CCP_MEMTYPE_SB;
+
+	ccp5_submit_cmd(&desc, op->cmd_q);
+
+	return 0;
+}
+
+static int ccp5_perform_sm4(struct ccp_op *op)
+{
+	union ccp_function function;
+
+	function.raw = 0;
+	CCP_SM4_ENCRYPT(&function) = op->u.sm4.action;
+	CCP_SM4_MODE(&function) = op->u.sm4.mode;
+	CCP_SM4_SELECT(&function) = op->u.sm4.select;
+
+	return ccp5_perform_sm4_common(op, CCP_ENGINE_SM4, function.raw);
+}
+
+static int ccp5_perform_sm4_ctr(struct ccp_op *op)
+{
+	union ccp_function function;
+
+	function.raw = 0;
+	CCP_SM4_CTR_SIZE(&function) = op->u.sm4_ctr.size;
+	CCP_SM4_CTR_ENCRYPT(&function) = op->u.sm4_ctr.action;
+	CCP_SM4_CTR_STEP(&function) = op->u.sm4_ctr.step;
+
+	return ccp5_perform_sm4_common(op, CCP_ENGINE_SM4_CTR, function.raw);
+}
+
 static int ccp_find_lsb_regions(struct ccp_cmd_queue *cmd_q, u64 status)
 {
 	int q_mask = 1 << cmd_q->id;
@@ -1159,6 +1222,8 @@ static const struct ccp_actions ccp5_actions = {
 	.passthru = ccp5_perform_passthru,
 	.ecc = ccp5_perform_ecc,
 	.run = ccp5_run_cmd,
+	.sm4 = ccp5_perform_sm4,
+	.sm4_ctr = ccp5_perform_sm4_ctr,
 	.sballoc = ccp_lsb_alloc,
 	.sbfree = ccp_lsb_free,
 	.init = ccp5_init,
diff --git a/drivers/crypto/ccp/ccp-dev.h b/drivers/crypto/ccp/ccp-dev.h
index c2b8068bc123..544c7079882f 100644
--- a/drivers/crypto/ccp/ccp-dev.h
+++ b/drivers/crypto/ccp/ccp-dev.h
@@ -683,6 +683,8 @@ struct ccp_actions {
 	int (*passthru)(struct ccp_op *);
 	int (*ecc)(struct ccp_op *);
 	int (*run)(struct ccp_op *op);
+	int (*sm4)(struct ccp_op *op);
+	int (*sm4_ctr)(struct ccp_op *op);
 	u32 (*sballoc)(struct ccp_cmd_queue *, unsigned int);
 	void (*sbfree)(struct ccp_cmd_queue *, unsigned int, unsigned int);
 	unsigned int (*get_free_slots)(struct ccp_cmd_queue *);
diff --git a/drivers/crypto/ccp/ccp-ops.c b/drivers/crypto/ccp/ccp-ops.c
index f35803c37ab4..7c8464b1db6b 100644
--- a/drivers/crypto/ccp/ccp-ops.c
+++ b/drivers/crypto/ccp/ccp-ops.c
@@ -10,6 +10,7 @@
 
 #include <crypto/des.h>
 #include <crypto/scatterwalk.h>
+#include <crypto/sm4.h>
 #include <crypto/utils.h>
 #include <linux/ccp.h>
 #include <linux/dma-mapping.h>
@@ -2473,6 +2474,285 @@ ccp_run_ecc_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
 	}
 }
 
+/*
+ * The SM engines queue several descriptors before running the queue.
+ * Keep one descriptor slot free so that the ring never wraps around an
+ * entry that the hardware has not consumed yet.
+ */
+static bool ccp_sm_queue_full(struct ccp_cmd_queue *cmd_q)
+{
+	const struct ccp_actions *perform = cmd_q->ccp->vdata->perform;
+
+	if (!perform->get_free_slots)
+		return true;
+
+	return perform->get_free_slots(cmd_q) <= 1;
+}
+
+static int ccp_run_sm4_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
+{
+	struct ccp_sm4_engine *sm4 = &cmd->u.sm4;
+	struct ccp_dm_workarea iv_key;
+	struct ccp_data src, dst;
+	struct ccp_op op;
+	bool in_place = false;
+	int ret;
+
+	if (!sm4->src || !sm4->dst)
+		return -EINVAL;
+
+	if (!sm4->key || sm4->key_len != SM4_KEY_SIZE)
+		return -EINVAL;
+
+	if (sg_nents_for_len(sm4->key, SM4_KEY_SIZE) < 0)
+		return -EINVAL;
+
+	if (sm4->mode != CCP_SM4_MODE_ECB) {
+		if (!sm4->iv || sm4->iv_len != SM4_BLOCK_SIZE)
+			return -EINVAL;
+
+		if (sg_nents_for_len(sm4->iv, SM4_BLOCK_SIZE) < 0)
+			return -EINVAL;
+	}
+
+	memset(&op, 0, sizeof(op));
+	op.cmd_q = cmd_q;
+	op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
+	op.ioc = 1;
+	op.sb_ctx = cmd_q->sb_ctx;
+	op.init = (sm4->mode == CCP_SM4_MODE_ECB) ? 0 : 1;
+	op.u.sm4.action = sm4->action;
+	op.u.sm4.mode = sm4->mode;
+	op.u.sm4.select = sm4->select;
+
+	/*
+	 * Prepare the input and output data workareas. For in-place
+	 * operations we need to set the dma direction to BIDIRECTIONAL
+	 * and copy the src workarea to the dst workarea.
+	 */
+	if (sg_virt(sm4->src) == sg_virt(sm4->dst))
+		in_place = true;
+
+	ret = ccp_init_data(&src, cmd_q, sm4->src, sm4->src_len,
+			    SM4_BLOCK_SIZE,
+			    in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
+	if (ret)
+		return ret;
+
+	if (in_place) {
+		dst = src;
+	} else {
+		ret = ccp_init_data(&dst, cmd_q, sm4->dst, sm4->src_len,
+				    SM4_BLOCK_SIZE, DMA_FROM_DEVICE);
+		if (ret)
+			goto e_src;
+	}
+
+	/* load iv and key */
+	ret = ccp_init_dm_workarea(&iv_key, cmd_q,
+				   SM4_BLOCK_SIZE + SM4_KEY_SIZE,
+				   DMA_BIDIRECTIONAL);
+	if (ret)
+		goto e_dst;
+
+	if (sm4->mode != CCP_SM4_MODE_ECB)
+		ccp_set_dm_area(&iv_key, 0, sm4->iv, 0, SM4_BLOCK_SIZE);
+
+	ccp_set_dm_area(&iv_key, SM4_BLOCK_SIZE, sm4->key, 0, SM4_KEY_SIZE);
+
+	ret = ccp_copy_to_sb(cmd_q, &iv_key, 0, op.sb_ctx,
+			     CCP_PASSTHRU_BYTESWAP_NOOP);
+	if (ret) {
+		cmd->engine_error = cmd_q->cmd_error;
+		goto e_iv_key;
+	}
+
+	/* send data to the CCP SM4 engine */
+	while (src.sg_wa.bytes_left) {
+		ccp_prepare_data(&src, &dst, &op, SM4_BLOCK_SIZE, true);
+		if (!src.sg_wa.bytes_left)
+			op.eom = 1;
+
+		if (!src.sg_wa.bytes_left || op.soc ||
+		    ccp_sm_queue_full(cmd_q))
+			op.ioc = 1;
+		else
+			op.ioc = 0;
+
+		ret = cmd_q->ccp->vdata->perform->sm4(&op);
+		if (ret) {
+			cmd->engine_error = cmd_q->cmd_error;
+			goto e_iv_key;
+		}
+
+		if (op.ioc) {
+			ret = cmd_q->ccp->vdata->perform->run(&op);
+			if (ret) {
+				cmd->engine_error = cmd_q->cmd_error;
+				goto e_iv_key;
+			}
+		}
+
+		ccp_process_data(&src, &dst, &op);
+	}
+
+	if (sm4->mode != CCP_SM4_MODE_ECB) {
+		/* retrieve the SM4 iv */
+		ret = ccp_copy_from_sb(cmd_q, &iv_key, 0, op.sb_ctx,
+				       CCP_PASSTHRU_BYTESWAP_NOOP);
+		if (ret) {
+			cmd->engine_error = cmd_q->cmd_error;
+			goto e_iv_key;
+		}
+
+		ccp_get_dm_area(&iv_key, 0, sm4->iv, 0, SM4_BLOCK_SIZE);
+	}
+
+e_iv_key:
+	memset(iv_key.address, 0, SM4_BLOCK_SIZE + SM4_KEY_SIZE);
+	ccp_dm_free(&iv_key);
+
+e_dst:
+	if (!in_place)
+		ccp_free_data(&dst, cmd_q);
+
+e_src:
+	ccp_free_data(&src, cmd_q);
+
+	return ret;
+}
+
+static int ccp_run_sm4_ctr_cmd(struct ccp_cmd_queue *cmd_q,
+			       struct ccp_cmd *cmd)
+{
+	struct ccp_sm4_ctr_engine *sm4_ctr = &cmd->u.sm4_ctr;
+	struct ccp_dm_workarea iv_key;
+	struct ccp_data src, dst;
+	struct ccp_op op;
+	bool in_place = false;
+	int ret;
+
+	if (!sm4_ctr->src || !sm4_ctr->dst)
+		return -EINVAL;
+
+	if (!sm4_ctr->key || sm4_ctr->key_len != SM4_KEY_SIZE)
+		return -EINVAL;
+
+	if (sg_nents_for_len(sm4_ctr->key, SM4_KEY_SIZE) < 0)
+		return -EINVAL;
+
+	if (!sm4_ctr->iv || sm4_ctr->iv_len != SM4_BLOCK_SIZE)
+		return -EINVAL;
+
+	if (sg_nents_for_len(sm4_ctr->iv, SM4_BLOCK_SIZE) < 0)
+		return -EINVAL;
+
+	memset(&op, 0, sizeof(op));
+	op.cmd_q = cmd_q;
+	op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
+	op.ioc = 1;
+	op.sb_ctx = cmd_q->sb_ctx;
+	op.init = 1;
+	op.u.sm4_ctr.size = sm4_ctr->size;
+	op.u.sm4_ctr.action = sm4_ctr->action;
+	op.u.sm4_ctr.step = sm4_ctr->step;
+
+	if (sg_virt(sm4_ctr->src) == sg_virt(sm4_ctr->dst))
+		in_place = true;
+
+	ret = ccp_init_data(&src, cmd_q, sm4_ctr->src, sm4_ctr->src_len,
+			    SM4_BLOCK_SIZE,
+			    in_place ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
+	if (ret)
+		return ret;
+
+	if (in_place) {
+		dst = src;
+	} else {
+		ret = ccp_init_data(&dst, cmd_q, sm4_ctr->dst,
+				    sm4_ctr->src_len, SM4_BLOCK_SIZE,
+				    DMA_FROM_DEVICE);
+		if (ret)
+			goto e_src;
+	}
+
+	/* load iv and key */
+	ret = ccp_init_dm_workarea(&iv_key, cmd_q,
+				   SM4_BLOCK_SIZE + SM4_KEY_SIZE,
+				   DMA_BIDIRECTIONAL);
+	if (ret)
+		goto e_dst;
+
+	ccp_set_dm_area(&iv_key, 0, sm4_ctr->iv, 0, SM4_BLOCK_SIZE);
+	ccp_set_dm_area(&iv_key, SM4_BLOCK_SIZE, sm4_ctr->key, 0,
+			SM4_KEY_SIZE);
+
+	ret = ccp_copy_to_sb(cmd_q, &iv_key, 0, op.sb_ctx,
+			     CCP_PASSTHRU_BYTESWAP_NOOP);
+	if (ret) {
+		cmd->engine_error = cmd_q->cmd_error;
+		goto e_iv_key;
+	}
+
+	/* send data to the CCP SM4_CTR engine */
+	while (src.sg_wa.bytes_left) {
+		/*
+		 * The SM4_CTR engine processes a length that is not a
+		 * multiple of the block size, so the last partial block is
+		 * submitted as is instead of being padded to a full block
+		 * the way the block modes do it.
+		 */
+		ccp_prepare_data(&src, &dst, &op, SM4_BLOCK_SIZE, false);
+		if (!src.sg_wa.bytes_left)
+			op.eom = 1;
+
+		if (!src.sg_wa.bytes_left || op.soc ||
+		    ccp_sm_queue_full(cmd_q))
+			op.ioc = 1;
+		else
+			op.ioc = 0;
+
+		ret = cmd_q->ccp->vdata->perform->sm4_ctr(&op);
+		if (ret) {
+			cmd->engine_error = cmd_q->cmd_error;
+			goto e_iv_key;
+		}
+
+		if (op.ioc) {
+			ret = cmd_q->ccp->vdata->perform->run(&op);
+			if (ret) {
+				cmd->engine_error = cmd_q->cmd_error;
+				goto e_iv_key;
+			}
+		}
+
+		ccp_process_data(&src, &dst, &op);
+	}
+
+	/* retrieve the SM4_CTR iv */
+	ret = ccp_copy_from_sb(cmd_q, &iv_key, 0, op.sb_ctx,
+			       CCP_PASSTHRU_BYTESWAP_NOOP);
+	if (ret) {
+		cmd->engine_error = cmd_q->cmd_error;
+		goto e_iv_key;
+	}
+
+	ccp_get_dm_area(&iv_key, 0, sm4_ctr->iv, 0, SM4_BLOCK_SIZE);
+
+e_iv_key:
+	memset(iv_key.address, 0, SM4_BLOCK_SIZE + SM4_KEY_SIZE);
+	ccp_dm_free(&iv_key);
+
+e_dst:
+	if (!in_place)
+		ccp_free_data(&dst, cmd_q);
+
+e_src:
+	ccp_free_data(&src, cmd_q);
+
+	return ret;
+}
+
 int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
 {
 	int ret;
@@ -2517,6 +2797,22 @@ int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
 	case CCP_ENGINE_ECC:
 		ret = ccp_run_ecc_cmd(cmd_q, cmd);
 		break;
+	case CCP_ENGINE_SM4:
+		if (!(cmd_q->ccp->caps.engines & CCP_CAPS_SM4) ||
+		    !cmd_q->ccp->vdata->perform->sm4) {
+			ret = -EINVAL;
+			break;
+		}
+		ret = ccp_run_sm4_cmd(cmd_q, cmd);
+		break;
+	case CCP_ENGINE_SM4_CTR:
+		if (!(cmd_q->ccp->caps.engines & CCP_CAPS_SM4_CTR) ||
+		    !cmd_q->ccp->vdata->perform->sm4_ctr) {
+			ret = -EINVAL;
+			break;
+		}
+		ret = ccp_run_sm4_ctr_cmd(cmd_q, cmd);
+		break;
 	default:
 		ret = -EINVAL;
 	}
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH 11/11] crypto: ccp - add SM3 engine and ahash algorithms
  2026-11-25  7:45 [PATCH 00/11] crypto: ccp - add Hygon SM3/SM4 support Depei Yang
                   ` (9 preceding siblings ...)
  2026-11-25  7:46 ` [PATCH 10/11] crypto: ccp - add SM4 engine and skcipher algorithms Depei Yang
@ 2026-11-25  7:46 ` Depei Yang
  10 siblings, 0 replies; 12+ messages in thread
From: Depei Yang @ 2026-11-25  7:46 UTC (permalink / raw)
  To: linux-crypto
  Cc: Tom Lendacky, Herbert Xu, David S. Miller, Gary R Hook,
	linux-kernel, Depei Yang, Shan Chen, Yongwei Xu, Xiaochen Shen

Add the SM3 command builder, the ccp-ops request handling and the ahash
front end, including hmac(sm3).  sm3 and hmac(sm3) are registered when a
device advertises the SM3 engine.

CRYPTO_DEV_SP_CCP selects CRYPTO_SM3 so that the shash used to hash HMAC
keys longer than the block size is always available, as it does for
CRYPTO_SHA1 and CRYPTO_SHA256.

Reviewed-by: Shan Chen <chenshan@hygon.cn>
Tested-by: Yongwei Xu <xuyongwei@hygon.cn>
Signed-off-by: Depei Yang <yangdepei@hygon.cn>
---
 drivers/crypto/ccp/Kconfig           |   1 +
 drivers/crypto/ccp/Makefile          |   3 +-
 drivers/crypto/ccp/ccp-crypto-main.c |   6 +
 drivers/crypto/ccp/ccp-crypto-sm3.c  | 468 +++++++++++++++++++++++++++
 drivers/crypto/ccp/ccp-crypto.h      |   1 -
 drivers/crypto/ccp/ccp-dev-v5.c      |  37 +++
 drivers/crypto/ccp/ccp-dev.h         |   1 +
 drivers/crypto/ccp/ccp-ops.c         | 191 +++++++++++
 8 files changed, 706 insertions(+), 2 deletions(-)
 create mode 100644 drivers/crypto/ccp/ccp-crypto-sm3.c

diff --git a/drivers/crypto/ccp/Kconfig b/drivers/crypto/ccp/Kconfig
index f16a0f611317..a226720201fe 100644
--- a/drivers/crypto/ccp/Kconfig
+++ b/drivers/crypto/ccp/Kconfig
@@ -15,6 +15,7 @@ config CRYPTO_DEV_SP_CCP
 	select DMA_ENGINE
 	select CRYPTO_SHA1
 	select CRYPTO_SHA256
+	select CRYPTO_SM3
 	help
 	  Provides the support for AMD Cryptographic Coprocessor (CCP) device
 	  which can be used to offload encryption operations such as SHA, AES
diff --git a/drivers/crypto/ccp/Makefile b/drivers/crypto/ccp/Makefile
index dd0177961465..db7630b476e2 100644
--- a/drivers/crypto/ccp/Makefile
+++ b/drivers/crypto/ccp/Makefile
@@ -29,4 +29,5 @@ ccp-crypto-objs := ccp-crypto-main.o \
 		   ccp-crypto-des3.o \
 		   ccp-crypto-rsa.o \
 		   ccp-crypto-sha.o \
-		   ccp-crypto-sm4.o
+		   ccp-crypto-sm4.o \
+		   ccp-crypto-sm3.o
diff --git a/drivers/crypto/ccp/ccp-crypto-main.c b/drivers/crypto/ccp/ccp-crypto-main.c
index e68c843ce3fb..284a78234854 100644
--- a/drivers/crypto/ccp/ccp-crypto-main.c
+++ b/drivers/crypto/ccp/ccp-crypto-main.c
@@ -367,6 +367,12 @@ static int ccp_register_algs(void)
 			return ret;
 	}
 
+	if (!sm_disable && ccp_has_cap(CCP_CAPS_SM3)) {
+		ret = ccp_register_sm3_algs(&hash_algs);
+		if (ret)
+			return ret;
+	}
+
 	if (!sm_disable && ccp_has_cap(CCP_CAPS_SM4)) {
 		ret = ccp_register_sm4_algs(&skcipher_algs);
 		if (ret)
diff --git a/drivers/crypto/ccp/ccp-crypto-sm3.c b/drivers/crypto/ccp/ccp-crypto-sm3.c
new file mode 100644
index 000000000000..10a26a03a524
--- /dev/null
+++ b/drivers/crypto/ccp/ccp-crypto-sm3.c
@@ -0,0 +1,468 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * CCP SM3 crypto API support
+ *
+ * Copyright (C) 2022 Hygon Info Technologies Ltd.
+ */
+
+#include <linux/module.h>
+#include <linux/sched.h>
+#include <linux/delay.h>
+#include <linux/scatterlist.h>
+#include <linux/crypto.h>
+#include <crypto/algapi.h>
+#include <crypto/hash.h>
+#include <crypto/internal/hash.h>
+#include <crypto/scatterwalk.h>
+#include <crypto/hmac.h>
+
+#include "ccp-crypto.h"
+
+static int ccp_sm3_complete(struct crypto_async_request *async_req, int ret)
+{
+	struct ahash_request *req = ahash_request_cast(async_req);
+	struct ccp_sm3_req_ctx *rctx = ahash_request_ctx_dma(req);
+
+	if (ret)
+		goto e_free;
+
+	rctx->msg_bits += (rctx->hash_cnt << 3);
+	if (rctx->hash_rem) {
+		/* save remaining data to buffer */
+		unsigned int offset = rctx->nbytes - rctx->hash_rem;
+
+		scatterwalk_map_and_copy(rctx->buf, rctx->src, offset,
+					 rctx->hash_rem, 0);
+		rctx->buf_count = rctx->hash_rem;
+	} else {
+		rctx->buf_count = 0;
+	}
+
+	if (rctx->final) {
+		if (req->result)
+			memcpy(req->result, rctx->ctx, SM3_DIGEST_SIZE);
+
+		memset(rctx->ctx, 0, SM3_DIGEST_SIZE);
+	}
+
+e_free:
+	sg_free_table(&rctx->data_sg);
+
+	return ret;
+}
+
+static int ccp_do_sm3_update(struct ahash_request *req, unsigned int nbytes,
+			     unsigned int final)
+{
+	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
+	struct ccp_ctx *ctx = crypto_ahash_ctx_dma(tfm);
+	struct ccp_sm3_req_ctx *rctx = ahash_request_ctx_dma(req);
+	struct scatterlist *sg = req->src;
+	struct ccp_sm3_engine *sm3;
+	unsigned int sg_count;
+	gfp_t gfp;
+	u64 len, msg_bits = 0;
+	int nents;
+	int ret;
+
+	if (nbytes) {
+		nents = sg_nents_for_len(req->src, nbytes);
+		if (nents < 0)
+			return -EINVAL;
+	}
+
+	len = (u64)rctx->buf_count + nbytes;
+	if (len <= SM3_BLOCK_SIZE) {
+		scatterwalk_map_and_copy(rctx->buf + rctx->buf_count,
+					 req->src, 0, nbytes, 0);
+		rctx->buf_count += nbytes;
+		if (!final)
+			return 0;
+
+		sg_init_one(&rctx->buf_sg, rctx->buf, rctx->buf_count);
+		sg = &rctx->buf_sg;
+	} else {
+		gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
+			GFP_KERNEL : GFP_ATOMIC;
+
+		if (rctx->buf_count) {
+			/* build the scatterlist: buffered data + new data */
+			sg_count = sg_nents(req->src) + 1;
+			ret = sg_alloc_table(&rctx->data_sg, sg_count, gfp);
+			if (ret)
+				return ret;
+
+			sg_init_one(&rctx->buf_sg, rctx->buf,
+				    rctx->buf_count);
+			sg = ccp_crypto_sg_table_add(&rctx->data_sg,
+						     &rctx->buf_sg);
+			if (!sg) {
+				ret = -EINVAL;
+				goto e_free;
+			}
+			sg = ccp_crypto_sg_table_add(&rctx->data_sg,
+						     req->src);
+			if (!sg) {
+				ret = -EINVAL;
+				goto e_free;
+			}
+			sg_mark_end(sg);
+
+			sg = rctx->data_sg.sgl;
+		} else {
+			sg = req->src;
+		}
+	}
+
+	rctx->final = final;
+	if (final) {
+		rctx->hash_rem = 0;
+		rctx->hash_cnt = len;
+		msg_bits = rctx->msg_bits + (len << 3);
+	} else {
+		rctx->hash_rem = len & (SM3_BLOCK_SIZE - 1);
+		rctx->hash_cnt = len - rctx->hash_rem;
+		rctx->src = req->src;
+		rctx->nbytes = nbytes;
+	}
+
+	memset(&rctx->cmd, 0, sizeof(rctx->cmd));
+	INIT_LIST_HEAD(&rctx->cmd.entry);
+	rctx->cmd.engine = CCP_ENGINE_SM3;
+
+	sm3 = &rctx->cmd.u.sm3;
+	sm3->type = CCP_SM3_TYPE_256;
+	sm3->ctx = &rctx->ctx_sg;
+	sm3->ctx_len = SM3_DIGEST_SIZE;
+	sm3->src = sg;
+	sm3->src_len = rctx->hash_cnt;
+	sm3->first = rctx->msg_bits ? 0 : 1;
+	sm3->final = final;
+	sm3->msg_bits = msg_bits;
+	if (final && ctx->u.sm3.hmac_tfm) {
+		sm3->opad = &ctx->u.sm3.opad_sg;
+		sm3->opad_len = SM3_BLOCK_SIZE;
+	}
+
+	ret = ccp_crypto_enqueue_request(&req->base, &rctx->cmd);
+	if (ret && ret != -EINPROGRESS && ret != -EBUSY)
+		goto e_free;
+
+	return ret;
+
+e_free:
+	sg_free_table(&rctx->data_sg);
+
+	return ret;
+}
+
+static int ccp_sm3_init(struct ahash_request *req)
+{
+	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
+	struct ccp_ctx *ctx = crypto_ahash_ctx_dma(tfm);
+	struct ccp_sm3_req_ctx *rctx = ahash_request_ctx_dma(req);
+
+	if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
+		return -ENOKEY;
+
+	memset(rctx, 0, sizeof(*rctx));
+	if (ctx->u.sm3.hmac_tfm) {
+		/* buffer the HMAC ipad for the first update */
+		memcpy(rctx->buf, ctx->u.sm3.ipad, SM3_BLOCK_SIZE);
+		rctx->buf_count = SM3_BLOCK_SIZE;
+	}
+
+	sg_init_one(&rctx->ctx_sg, rctx->ctx, SM3_DIGEST_SIZE);
+
+	return 0;
+}
+
+static int ccp_sm3_update(struct ahash_request *req)
+{
+	return ccp_do_sm3_update(req, req->nbytes, 0);
+}
+
+static int ccp_sm3_final(struct ahash_request *req)
+{
+	return ccp_do_sm3_update(req, 0, 1);
+}
+
+static int ccp_sm3_finup(struct ahash_request *req)
+{
+	return ccp_do_sm3_update(req, req->nbytes, 1);
+}
+
+static int ccp_sm3_digest(struct ahash_request *req)
+{
+	int ret;
+
+	ret = ccp_sm3_init(req);
+	if (unlikely(ret))
+		return ret;
+
+	return ccp_sm3_finup(req);
+}
+
+static int ccp_sm3_export(struct ahash_request *req, void *out)
+{
+	struct ccp_sm3_req_ctx *rctx = ahash_request_ctx_dma(req);
+	struct ccp_sm3_exp_ctx state;
+
+	if (!out)
+		return -EINVAL;
+
+	/* don't let anything leak to 'out' */
+	memset(&state, 0, sizeof(state));
+
+	state.msg_bits = rctx->msg_bits;
+	memcpy(state.ctx, rctx->ctx, SM3_DIGEST_SIZE);
+	state.buf_count = rctx->buf_count;
+	memcpy(state.buf, rctx->buf, rctx->buf_count);
+
+	/* 'out' may not be aligned so memcpy from a local variable */
+	memcpy(out, &state, sizeof(state));
+	memset(&state, 0, sizeof(state));
+
+	return 0;
+}
+
+static int ccp_sm3_import(struct ahash_request *req, const void *in)
+{
+	struct ccp_sm3_req_ctx *rctx = ahash_request_ctx_dma(req);
+	struct ccp_sm3_exp_ctx state;
+
+	if (!in)
+		return -EINVAL;
+
+	/* 'in' may not be aligned so memcpy to a local variable */
+	memcpy(&state, in, sizeof(state));
+
+	if (state.buf_count > SM3_BLOCK_SIZE)
+		return -EINVAL;
+
+	memset(rctx, 0, sizeof(*rctx));
+	rctx->msg_bits = state.msg_bits;
+	memcpy(rctx->ctx, state.ctx, SM3_DIGEST_SIZE);
+	sg_init_one(&rctx->ctx_sg, rctx->ctx, SM3_DIGEST_SIZE);
+	rctx->buf_count = state.buf_count;
+	memcpy(rctx->buf, state.buf, SM3_BLOCK_SIZE);
+
+	memset(&state, 0, sizeof(state));
+
+	return 0;
+}
+
+static int ccp_sm3_setkey(struct crypto_ahash *tfm, const u8 *key,
+			  unsigned int key_len)
+{
+	struct ccp_ctx *ctx = crypto_ahash_ctx_dma(tfm);
+	struct crypto_shash *shash = ctx->u.sm3.hmac_tfm;
+	SHASH_DESC_ON_STACK(sdesc, shash);
+	int i, ret;
+
+	if (!key)
+		return -EINVAL;
+
+	/*
+	 * clear the key area to provide zero padding for keys smaller
+	 * than the block size
+	 */
+	memset(ctx->u.sm3.key, 0, SM3_BLOCK_SIZE);
+
+	if (key_len > SM3_BLOCK_SIZE) {
+		/* must hash the input key */
+		sdesc->tfm = shash;
+		ret = crypto_shash_digest(sdesc, key, key_len,
+					  ctx->u.sm3.key);
+		if (ret) {
+			crypto_ahash_set_flags(tfm, CRYPTO_TFM_NEED_KEY);
+			return -EINVAL;
+		}
+
+		key_len = SM3_DIGEST_SIZE;
+	} else {
+		memcpy(ctx->u.sm3.key, key, key_len);
+	}
+
+	for (i = 0; i < SM3_BLOCK_SIZE; i++) {
+		ctx->u.sm3.ipad[i] = ctx->u.sm3.key[i] ^ HMAC_IPAD_VALUE;
+		ctx->u.sm3.opad[i] = ctx->u.sm3.key[i] ^ HMAC_OPAD_VALUE;
+	}
+
+	sg_init_one(&ctx->u.sm3.opad_sg, ctx->u.sm3.opad, SM3_BLOCK_SIZE);
+
+	return 0;
+}
+
+static int ccp_sm3_cra_init(struct crypto_tfm *tfm)
+{
+	struct crypto_ahash *ahash = __crypto_ahash_cast(tfm);
+	struct ccp_ctx *ctx = crypto_ahash_ctx_dma(ahash);
+
+	ctx->complete = ccp_sm3_complete;
+	crypto_ahash_set_reqsize_dma(ahash, sizeof(struct ccp_sm3_req_ctx));
+
+	return 0;
+}
+
+static void ccp_sm3_cra_exit(struct crypto_tfm *tfm)
+{
+}
+
+static int ccp_hmac_sm3_cra_init(struct crypto_tfm *tfm)
+{
+	struct ccp_ctx *ctx = crypto_tfm_ctx_dma(tfm);
+	struct ccp_crypto_ahash_alg *alg = ccp_crypto_ahash_alg(tfm);
+	struct crypto_shash *hmac_tfm;
+
+	hmac_tfm = crypto_alloc_shash(alg->child_alg, 0, 0);
+	if (IS_ERR(hmac_tfm)) {
+		pr_warn("could not load driver %s needed for HMAC support\n",
+			alg->child_alg);
+		return PTR_ERR(hmac_tfm);
+	}
+
+	ctx->u.sm3.hmac_tfm = hmac_tfm;
+
+	return ccp_sm3_cra_init(tfm);
+}
+
+static void ccp_hmac_sm3_cra_exit(struct crypto_tfm *tfm)
+{
+	struct ccp_ctx *ctx = crypto_tfm_ctx_dma(tfm);
+
+	if (ctx->u.sm3.hmac_tfm)
+		crypto_free_shash(ctx->u.sm3.hmac_tfm);
+
+	ccp_sm3_cra_exit(tfm);
+}
+
+struct ccp_sm3_def {
+	const char *name;
+	const char *drv_name;
+	enum ccp_sm3_type type;
+	u32 digest_size;
+	u32 block_size;
+};
+
+static const struct ccp_sm3_def sm3_algs[] = {
+	{
+		.name		= "sm3",
+		.drv_name	= "sm3-ccp",
+		.type		= CCP_SM3_TYPE_256,
+		.digest_size	= SM3_DIGEST_SIZE,
+		.block_size	= SM3_BLOCK_SIZE,
+	},
+};
+
+static int ccp_register_hmac_sm3_alg(struct list_head *head,
+				     const struct ccp_sm3_def *def,
+				     const struct ccp_crypto_ahash_alg *base_alg)
+{
+	struct ccp_crypto_ahash_alg *ccp_alg;
+	struct ahash_alg *alg;
+	struct crypto_alg *base;
+	int ret;
+
+	ccp_alg = kzalloc_obj(*ccp_alg);
+	if (!ccp_alg)
+		return -ENOMEM;
+
+	/* copy the base algorithm and only change what's necessary */
+	*ccp_alg = *base_alg;
+	INIT_LIST_HEAD(&ccp_alg->entry);
+
+	strscpy(ccp_alg->child_alg, def->name);
+
+	alg = &ccp_alg->alg;
+	alg->setkey = ccp_sm3_setkey;
+
+	base = &alg->halg.base;
+	snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)", def->name);
+	snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, "hmac-%s",
+		 def->drv_name);
+	base->cra_init = ccp_hmac_sm3_cra_init;
+	base->cra_exit = ccp_hmac_sm3_cra_exit;
+
+	ret = crypto_register_ahash(alg);
+	if (ret) {
+		pr_err("%s ahash algorithm registration error (%d)\n",
+		       base->cra_name, ret);
+		kfree(ccp_alg);
+		return ret;
+	}
+
+	list_add(&ccp_alg->entry, head);
+
+	return 0;
+}
+
+static int ccp_register_sm3_alg(struct list_head *head,
+				const struct ccp_sm3_def *def)
+{
+	struct ccp_crypto_ahash_alg *ccp_alg;
+	struct ahash_alg *alg;
+	struct hash_alg_common *halg;
+	struct crypto_alg *base;
+	int ret;
+
+	ccp_alg = kzalloc_obj(*ccp_alg);
+	if (!ccp_alg)
+		return -ENOMEM;
+
+	INIT_LIST_HEAD(&ccp_alg->entry);
+
+	ccp_alg->type = def->type;
+
+	alg = &ccp_alg->alg;
+	alg->init = ccp_sm3_init;
+	alg->update = ccp_sm3_update;
+	alg->final = ccp_sm3_final;
+	alg->finup = ccp_sm3_finup;
+	alg->digest = ccp_sm3_digest;
+	alg->export = ccp_sm3_export;
+	alg->import = ccp_sm3_import;
+
+	halg = &alg->halg;
+	halg->digestsize = def->digest_size;
+	halg->statesize = sizeof(struct ccp_sm3_exp_ctx);
+
+	base = &halg->base;
+	strscpy(base->cra_name, def->name);
+	strscpy(base->cra_driver_name, def->drv_name);
+	base->cra_flags = CRYPTO_ALG_ASYNC |
+			  CRYPTO_ALG_ALLOCATES_MEMORY |
+			  CRYPTO_ALG_KERN_DRIVER_ONLY |
+			  CRYPTO_ALG_NEED_FALLBACK;
+	base->cra_blocksize = def->block_size;
+	base->cra_ctxsize = sizeof(struct ccp_ctx) + crypto_dma_padding();
+	base->cra_priority = CCP_CRA_PRIORITY;
+	base->cra_init = ccp_sm3_cra_init;
+	base->cra_exit = ccp_sm3_cra_exit;
+	base->cra_module = THIS_MODULE;
+
+	ret = crypto_register_ahash(alg);
+	if (ret) {
+		pr_err("%s ahash algorithm registration error (%d)\n",
+		       base->cra_name, ret);
+		kfree(ccp_alg);
+		return ret;
+	}
+
+	list_add(&ccp_alg->entry, head);
+
+	return ccp_register_hmac_sm3_alg(head, def, ccp_alg);
+}
+
+int ccp_register_sm3_algs(struct list_head *head)
+{
+	int i, ret;
+
+	for (i = 0; i < ARRAY_SIZE(sm3_algs); i++) {
+		ret = ccp_register_sm3_alg(head, &sm3_algs[i]);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
diff --git a/drivers/crypto/ccp/ccp-crypto.h b/drivers/crypto/ccp/ccp-crypto.h
index 9d6f2983f39f..e5036cd5169d 100644
--- a/drivers/crypto/ccp/ccp-crypto.h
+++ b/drivers/crypto/ccp/ccp-crypto.h
@@ -262,7 +262,6 @@ struct ccp_rsa_req_ctx {
 
 /***** SM3 related defines *****/
 struct ccp_sm3_ctx {
-	u32 key_len;
 	u8 key[SM3_BLOCK_SIZE];
 
 	u8 ipad[SM3_BLOCK_SIZE];
diff --git a/drivers/crypto/ccp/ccp-dev-v5.c b/drivers/crypto/ccp/ccp-dev-v5.c
index 8cbbb8d81d17..e8204f6afdd4 100644
--- a/drivers/crypto/ccp/ccp-dev-v5.c
+++ b/drivers/crypto/ccp/ccp-dev-v5.c
@@ -632,6 +632,42 @@ static int ccp5_perform_ecc(struct ccp_op *op)
 	return ccp5_do_cmd(&desc, op->cmd_q);
 }
 
+static int ccp5_perform_sm3(struct ccp_op *op)
+{
+	struct ccp5_desc desc;
+	union ccp_function function;
+
+	memset(&desc, 0, Q_DESC_SIZE);
+
+	CCP5_CMD_ENGINE(&desc) = CCP_ENGINE_SM3;
+
+	CCP5_CMD_SOC(&desc) = op->soc;
+	CCP5_CMD_IOC(&desc) = op->ioc;
+	CCP5_CMD_INIT(&desc) = op->init;
+	CCP5_CMD_EOM(&desc) = op->eom;
+	CCP5_CMD_PROT(&desc) = 0;
+
+	function.raw = 0;
+	CCP_SM3_TYPE(&function) = op->u.sm3.type;
+	CCP5_CMD_FUNCTION(&desc) = function.raw;
+
+	CCP5_CMD_LEN(&desc) = op->src.u.dma.length;
+
+	CCP5_CMD_SRC_LO(&desc) = ccp_addr_lo(&op->src.u.dma);
+	CCP5_CMD_SRC_HI(&desc) = ccp_addr_hi(&op->src.u.dma);
+	CCP5_CMD_SRC_MEM(&desc) = CCP_MEMTYPE_SYSTEM;
+	CCP5_CMD_LSB_ID(&desc) = op->sb_ctx;
+
+	if (op->eom) {
+		CCP5_CMD_SM3_LO(&desc) = lower_32_bits(op->u.sm3.msg_bits);
+		CCP5_CMD_SM3_HI(&desc) = upper_32_bits(op->u.sm3.msg_bits);
+	}
+
+	ccp5_submit_cmd(&desc, op->cmd_q);
+
+	return 0;
+}
+
 static int ccp5_perform_sm4_common(struct ccp_op *op, enum ccp_engine engine,
 				   u32 function)
 {
@@ -1222,6 +1258,7 @@ static const struct ccp_actions ccp5_actions = {
 	.passthru = ccp5_perform_passthru,
 	.ecc = ccp5_perform_ecc,
 	.run = ccp5_run_cmd,
+	.sm3 = ccp5_perform_sm3,
 	.sm4 = ccp5_perform_sm4,
 	.sm4_ctr = ccp5_perform_sm4_ctr,
 	.sballoc = ccp_lsb_alloc,
diff --git a/drivers/crypto/ccp/ccp-dev.h b/drivers/crypto/ccp/ccp-dev.h
index 544c7079882f..2141bffb745f 100644
--- a/drivers/crypto/ccp/ccp-dev.h
+++ b/drivers/crypto/ccp/ccp-dev.h
@@ -683,6 +683,7 @@ struct ccp_actions {
 	int (*passthru)(struct ccp_op *);
 	int (*ecc)(struct ccp_op *);
 	int (*run)(struct ccp_op *op);
+	int (*sm3)(struct ccp_op *op);
 	int (*sm4)(struct ccp_op *op);
 	int (*sm4_ctr)(struct ccp_op *op);
 	u32 (*sballoc)(struct ccp_cmd_queue *, unsigned int);
diff --git a/drivers/crypto/ccp/ccp-ops.c b/drivers/crypto/ccp/ccp-ops.c
index 7c8464b1db6b..cc8c44574bdb 100644
--- a/drivers/crypto/ccp/ccp-ops.c
+++ b/drivers/crypto/ccp/ccp-ops.c
@@ -10,6 +10,7 @@
 
 #include <crypto/des.h>
 #include <crypto/scatterwalk.h>
+#include <crypto/sm3.h>
 #include <crypto/sm4.h>
 #include <crypto/utils.h>
 #include <linux/ccp.h>
@@ -2489,6 +2490,188 @@ static bool ccp_sm_queue_full(struct ccp_cmd_queue *cmd_q)
 	return perform->get_free_slots(cmd_q) <= 1;
 }
 
+static int ccp_run_sm3_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
+{
+	struct ccp_sm3_engine *sm3 = &cmd->u.sm3;
+	struct ccp_dm_workarea ctx;
+	struct ccp_data src;
+	struct ccp_op op;
+	u8 sm3_zero_message_hash[SM3_DIGEST_SIZE] = {
+		0x1A, 0xB2, 0x1D, 0x83, 0x55, 0xCF, 0xA1, 0x7F,
+		0x8e, 0x61, 0x19, 0x48, 0x31, 0xE8, 0x1A, 0x8F,
+		0x22, 0xBE, 0xC8, 0xC7, 0x28, 0xFE, 0xFB, 0x74,
+		0x7E, 0xD0, 0x35, 0xEB, 0x50, 0x82, 0xAA, 0x2B,
+	};
+	int ret;
+
+	if (!sm3->ctx || sm3->ctx_len != SM3_DIGEST_SIZE)
+		return -EINVAL;
+
+	if (sm3->src_len && !sm3->src)
+		return -EINVAL;
+
+	if (!sm3->final && (sm3->src_len & (SM3_BLOCK_SIZE - 1)))
+		return -EINVAL;
+
+	if (sm3->opad &&
+	    (sm3->opad_len != SM3_BLOCK_SIZE ||
+	     sg_nents_for_len(sm3->opad, SM3_BLOCK_SIZE) < 0))
+		return -EINVAL;
+
+	if (sg_nents_for_len(sm3->ctx, SM3_DIGEST_SIZE) < 0)
+		return -EINVAL;
+
+	if (sm3->final && sm3->first && !sm3->src_len) {
+		scatterwalk_map_and_copy(sm3_zero_message_hash, sm3->ctx,
+					 0, SM3_DIGEST_SIZE, 1);
+		return 0;
+	}
+
+	memset(&op, 0, sizeof(op));
+	op.cmd_q = cmd_q;
+	op.jobid = CCP_NEW_JOBID(cmd_q->ccp);
+	op.ioc = 1;
+	op.sb_ctx = cmd_q->sb_ctx;
+	op.init = sm3->first & 0x1;
+	op.u.sm3.type = sm3->type;
+	op.u.sm3.msg_bits = sm3->msg_bits;
+
+	memset(&ctx, 0, sizeof(ctx));
+	ret = ccp_init_dm_workarea(&ctx, cmd_q, SM3_DIGEST_SIZE,
+				   DMA_BIDIRECTIONAL);
+	if (ret)
+		return ret;
+
+	if (!sm3->first) {
+		/* load the previous hash value */
+		ccp_set_dm_area(&ctx, 0, sm3->ctx, 0, SM3_DIGEST_SIZE);
+
+		ret = ccp_copy_to_sb(cmd_q, &ctx, 0, op.sb_ctx,
+				     CCP_PASSTHRU_BYTESWAP_NOOP);
+		if (ret) {
+			cmd->engine_error = cmd_q->cmd_error;
+			goto e_ctx;
+		}
+	}
+
+	ret = ccp_init_data(&src, cmd_q, sm3->src, sm3->src_len,
+			    SM3_BLOCK_SIZE, DMA_TO_DEVICE);
+	if (ret)
+		goto e_ctx;
+
+	if (sm3->src_len) {
+		while (src.sg_wa.bytes_left) {
+			ccp_prepare_data(&src, NULL, &op, SM3_BLOCK_SIZE,
+					 false);
+			if (!src.sg_wa.bytes_left && sm3->final)
+				op.eom = 1;
+
+			if (!src.sg_wa.bytes_left || op.soc ||
+			    ccp_sm_queue_full(cmd_q))
+				op.ioc = 1;
+			else
+				op.ioc = 0;
+
+			ret = cmd_q->ccp->vdata->perform->sm3(&op);
+			if (ret) {
+				cmd->engine_error = cmd_q->cmd_error;
+				goto e_data;
+			}
+
+			if (op.ioc) {
+				ret = cmd_q->ccp->vdata->perform->run(&op);
+				if (ret) {
+					cmd->engine_error = cmd_q->cmd_error;
+					goto e_data;
+				}
+			}
+
+			ccp_process_data(&src, NULL, &op);
+		}
+	} else {
+		/*
+		 * The hardware does not pad, so build the final block in
+		 * software.
+		 */
+		src.dm_wa.address[0] = 0x80;
+		*(__be64 *)&src.dm_wa.address[56] =
+			cpu_to_be64(sm3->msg_bits);
+
+		op.soc = 0;
+		op.ioc = 1;
+		op.eom = 0;
+		op.src.u.dma.address = src.dm_wa.dma.address;
+		op.src.u.dma.offset = 0;
+		op.src.u.dma.length = SM3_BLOCK_SIZE;
+
+		ret = cmd_q->ccp->vdata->perform->sm3(&op);
+		if (ret) {
+			cmd->engine_error = cmd_q->cmd_error;
+			goto e_data;
+		}
+
+		ret = cmd_q->ccp->vdata->perform->run(&op);
+		if (ret) {
+			cmd->engine_error = cmd_q->cmd_error;
+			goto e_data;
+		}
+	}
+
+	ret = ccp_copy_from_sb(cmd_q, &ctx, 0, op.sb_ctx,
+			       CCP_PASSTHRU_BYTESWAP_NOOP);
+	if (ret) {
+		cmd->engine_error = cmd_q->cmd_error;
+		goto e_data;
+	}
+
+	if (sm3->final && sm3->opad) {
+		/* HMAC: run a second SM3 over opad || inner hash */
+		struct ccp_cmd hmac_cmd;
+		struct scatterlist sg;
+		u8 *hmac_buf;
+
+		hmac_buf = kmalloc(SM3_BLOCK_SIZE + SM3_DIGEST_SIZE,
+				   GFP_KERNEL);
+		if (!hmac_buf) {
+			ret = -ENOMEM;
+			goto e_data;
+		}
+		scatterwalk_map_and_copy(hmac_buf, sm3->opad, 0,
+					 SM3_BLOCK_SIZE, 0);
+		memcpy(hmac_buf + SM3_BLOCK_SIZE, ctx.address,
+		       SM3_DIGEST_SIZE);
+		sg_init_one(&sg, hmac_buf, SM3_BLOCK_SIZE + SM3_DIGEST_SIZE);
+
+		memset(&hmac_cmd, 0, sizeof(hmac_cmd));
+		hmac_cmd.engine = CCP_ENGINE_SM3;
+		hmac_cmd.u.sm3.type = sm3->type;
+		hmac_cmd.u.sm3.ctx = sm3->ctx;
+		hmac_cmd.u.sm3.ctx_len = sm3->ctx_len;
+		hmac_cmd.u.sm3.src = &sg;
+		hmac_cmd.u.sm3.src_len = SM3_BLOCK_SIZE + SM3_DIGEST_SIZE;
+		hmac_cmd.u.sm3.first = 1;
+		hmac_cmd.u.sm3.final = 1;
+		hmac_cmd.u.sm3.msg_bits =
+			(SM3_BLOCK_SIZE + SM3_DIGEST_SIZE) << 3;
+
+		ret = ccp_run_sm3_cmd(cmd_q, &hmac_cmd);
+		if (ret)
+			cmd->engine_error = hmac_cmd.engine_error;
+
+		kfree(hmac_buf);
+	} else {
+		ccp_get_dm_area(&ctx, 0, sm3->ctx, 0, SM3_DIGEST_SIZE);
+	}
+
+e_data:
+	ccp_free_data(&src, cmd_q);
+
+e_ctx:
+	ccp_dm_free(&ctx);
+
+	return ret;
+}
+
 static int ccp_run_sm4_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
 {
 	struct ccp_sm4_engine *sm4 = &cmd->u.sm4;
@@ -2797,6 +2980,14 @@ int ccp_run_cmd(struct ccp_cmd_queue *cmd_q, struct ccp_cmd *cmd)
 	case CCP_ENGINE_ECC:
 		ret = ccp_run_ecc_cmd(cmd_q, cmd);
 		break;
+	case CCP_ENGINE_SM3:
+		if (!(cmd_q->ccp->caps.engines & CCP_CAPS_SM3) ||
+		    !cmd_q->ccp->vdata->perform->sm3) {
+			ret = -EINVAL;
+			break;
+		}
+		ret = ccp_run_sm3_cmd(cmd_q, cmd);
+		break;
 	case CCP_ENGINE_SM4:
 		if (!(cmd_q->ccp->caps.engines & CCP_CAPS_SM4) ||
 		    !cmd_q->ccp->vdata->perform->sm4) {
-- 
2.34.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-11-25  7:46 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25  7:45 [PATCH 00/11] crypto: ccp - add Hygon SM3/SM4 support Depei Yang
2026-11-25  7:45 ` [PATCH 01/11] crypto: ccp - fix LSB region mask calculation Depei Yang
2026-11-25  7:46 ` [PATCH 02/11] crypto: ccp - acknowledge queue interrupt before waking waiter Depei Yang
2026-11-25  7:46 ` [PATCH 03/11] crypto: ccp - add engine capability and version information Depei Yang
2026-11-25  7:46 ` [PATCH 04/11] crypto: ccp - register algorithms only for supported engines Depei Yang
2026-11-25  7:46 ` [PATCH 05/11] crypto: ccp - split v5 command submission from queue run Depei Yang
2026-11-25  7:46 ` [PATCH 06/11] crypto: ccp - add SM3/SM4 command definitions Depei Yang
2026-11-25  7:46 ` [PATCH 07/11] crypto: ccp - add Hygon CCP device IDs and vdata Depei Yang
2026-11-25  7:46 ` [PATCH 08/11] crypto: ccp - handle only supported interrupts in v5 irq bh Depei Yang
2026-11-25  7:46 ` [PATCH 09/11] crypto: ccp - detect the Hygon CCP engines from the version register Depei Yang
2026-11-25  7:46 ` [PATCH 10/11] crypto: ccp - add SM4 engine and skcipher algorithms Depei Yang
2026-11-25  7:46 ` [PATCH 11/11] crypto: ccp - add SM3 engine and ahash algorithms Depei Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox