18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef __MARVELL_CESA_H__
38c2ecf20Sopenharmony_ci#define __MARVELL_CESA_H__
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h>
68c2ecf20Sopenharmony_ci#include <crypto/internal/skcipher.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/dma-direction.h>
98c2ecf20Sopenharmony_ci#include <linux/dmapool.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#define CESA_ENGINE_OFF(i)			(((i) * 0x2000))
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define CESA_TDMA_BYTE_CNT			0x800
148c2ecf20Sopenharmony_ci#define CESA_TDMA_SRC_ADDR			0x810
158c2ecf20Sopenharmony_ci#define CESA_TDMA_DST_ADDR			0x820
168c2ecf20Sopenharmony_ci#define CESA_TDMA_NEXT_ADDR			0x830
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define CESA_TDMA_CONTROL			0x840
198c2ecf20Sopenharmony_ci#define CESA_TDMA_DST_BURST			GENMASK(2, 0)
208c2ecf20Sopenharmony_ci#define CESA_TDMA_DST_BURST_32B			3
218c2ecf20Sopenharmony_ci#define CESA_TDMA_DST_BURST_128B		4
228c2ecf20Sopenharmony_ci#define CESA_TDMA_OUT_RD_EN			BIT(4)
238c2ecf20Sopenharmony_ci#define CESA_TDMA_SRC_BURST			GENMASK(8, 6)
248c2ecf20Sopenharmony_ci#define CESA_TDMA_SRC_BURST_32B			(3 << 6)
258c2ecf20Sopenharmony_ci#define CESA_TDMA_SRC_BURST_128B		(4 << 6)
268c2ecf20Sopenharmony_ci#define CESA_TDMA_CHAIN				BIT(9)
278c2ecf20Sopenharmony_ci#define CESA_TDMA_BYTE_SWAP			BIT(11)
288c2ecf20Sopenharmony_ci#define CESA_TDMA_NO_BYTE_SWAP			BIT(11)
298c2ecf20Sopenharmony_ci#define CESA_TDMA_EN				BIT(12)
308c2ecf20Sopenharmony_ci#define CESA_TDMA_FETCH_ND			BIT(13)
318c2ecf20Sopenharmony_ci#define CESA_TDMA_ACT				BIT(14)
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define CESA_TDMA_CUR				0x870
348c2ecf20Sopenharmony_ci#define CESA_TDMA_ERROR_CAUSE			0x8c8
358c2ecf20Sopenharmony_ci#define CESA_TDMA_ERROR_MSK			0x8cc
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define CESA_TDMA_WINDOW_BASE(x)		(((x) * 0x8) + 0xa00)
388c2ecf20Sopenharmony_ci#define CESA_TDMA_WINDOW_CTRL(x)		(((x) * 0x8) + 0xa04)
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define CESA_IVDIG(x)				(0xdd00 + ((x) * 4) +	\
418c2ecf20Sopenharmony_ci						 (((x) < 5) ? 0 : 0x14))
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci#define CESA_SA_CMD				0xde00
448c2ecf20Sopenharmony_ci#define CESA_SA_CMD_EN_CESA_SA_ACCL0		BIT(0)
458c2ecf20Sopenharmony_ci#define CESA_SA_CMD_EN_CESA_SA_ACCL1		BIT(1)
468c2ecf20Sopenharmony_ci#define CESA_SA_CMD_DISABLE_SEC			BIT(2)
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define CESA_SA_DESC_P0				0xde04
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define CESA_SA_DESC_P1				0xde14
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci#define CESA_SA_CFG				0xde08
538c2ecf20Sopenharmony_ci#define CESA_SA_CFG_STOP_DIG_ERR		GENMASK(1, 0)
548c2ecf20Sopenharmony_ci#define CESA_SA_CFG_DIG_ERR_CONT		0
558c2ecf20Sopenharmony_ci#define CESA_SA_CFG_DIG_ERR_SKIP		1
568c2ecf20Sopenharmony_ci#define CESA_SA_CFG_DIG_ERR_STOP		3
578c2ecf20Sopenharmony_ci#define CESA_SA_CFG_CH0_W_IDMA			BIT(7)
588c2ecf20Sopenharmony_ci#define CESA_SA_CFG_CH1_W_IDMA			BIT(8)
598c2ecf20Sopenharmony_ci#define CESA_SA_CFG_ACT_CH0_IDMA		BIT(9)
608c2ecf20Sopenharmony_ci#define CESA_SA_CFG_ACT_CH1_IDMA		BIT(10)
618c2ecf20Sopenharmony_ci#define CESA_SA_CFG_MULTI_PKT			BIT(11)
628c2ecf20Sopenharmony_ci#define CESA_SA_CFG_PARA_DIS			BIT(13)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define CESA_SA_ACCEL_STATUS			0xde0c
658c2ecf20Sopenharmony_ci#define CESA_SA_ST_ACT_0			BIT(0)
668c2ecf20Sopenharmony_ci#define CESA_SA_ST_ACT_1			BIT(1)
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/*
698c2ecf20Sopenharmony_ci * CESA_SA_FPGA_INT_STATUS looks like a FPGA leftover and is documented only
708c2ecf20Sopenharmony_ci * in Errata 4.12. It looks like that it was part of an IRQ-controller in FPGA
718c2ecf20Sopenharmony_ci * and someone forgot to remove  it while switching to the core and moving to
728c2ecf20Sopenharmony_ci * CESA_SA_INT_STATUS.
738c2ecf20Sopenharmony_ci */
748c2ecf20Sopenharmony_ci#define CESA_SA_FPGA_INT_STATUS			0xdd68
758c2ecf20Sopenharmony_ci#define CESA_SA_INT_STATUS			0xde20
768c2ecf20Sopenharmony_ci#define CESA_SA_INT_AUTH_DONE			BIT(0)
778c2ecf20Sopenharmony_ci#define CESA_SA_INT_DES_E_DONE			BIT(1)
788c2ecf20Sopenharmony_ci#define CESA_SA_INT_AES_E_DONE			BIT(2)
798c2ecf20Sopenharmony_ci#define CESA_SA_INT_AES_D_DONE			BIT(3)
808c2ecf20Sopenharmony_ci#define CESA_SA_INT_ENC_DONE			BIT(4)
818c2ecf20Sopenharmony_ci#define CESA_SA_INT_ACCEL0_DONE			BIT(5)
828c2ecf20Sopenharmony_ci#define CESA_SA_INT_ACCEL1_DONE			BIT(6)
838c2ecf20Sopenharmony_ci#define CESA_SA_INT_ACC0_IDMA_DONE		BIT(7)
848c2ecf20Sopenharmony_ci#define CESA_SA_INT_ACC1_IDMA_DONE		BIT(8)
858c2ecf20Sopenharmony_ci#define CESA_SA_INT_IDMA_DONE			BIT(9)
868c2ecf20Sopenharmony_ci#define CESA_SA_INT_IDMA_OWN_ERR		BIT(10)
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#define CESA_SA_INT_MSK				0xde24
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_OP_MAC_ONLY		0
918c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_OP_CRYPT_ONLY		1
928c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_OP_MAC_CRYPT		2
938c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_OP_CRYPT_MAC		3
948c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_OP_MSK			GENMASK(1, 0)
958c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_MACM_SHA256		(1 << 4)
968c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_MACM_HMAC_SHA256	(3 << 4)
978c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_MACM_MD5		(4 << 4)
988c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_MACM_SHA1		(5 << 4)
998c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_MACM_HMAC_MD5		(6 << 4)
1008c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_MACM_HMAC_SHA1		(7 << 4)
1018c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_MACM_MSK		GENMASK(6, 4)
1028c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_CRYPTM_DES		(1 << 8)
1038c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_CRYPTM_3DES		(2 << 8)
1048c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_CRYPTM_AES		(3 << 8)
1058c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_CRYPTM_MSK		GENMASK(9, 8)
1068c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_DIR_ENC		(0 << 12)
1078c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_DIR_DEC		(1 << 12)
1088c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_CRYPTCM_ECB		(0 << 16)
1098c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_CRYPTCM_CBC		(1 << 16)
1108c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_CRYPTCM_MSK		BIT(16)
1118c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_3DES_EEE		(0 << 20)
1128c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_3DES_EDE		(1 << 20)
1138c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_AES_LEN_128		(0 << 24)
1148c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_AES_LEN_192		(1 << 24)
1158c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_AES_LEN_256		(2 << 24)
1168c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_AES_LEN_MSK		GENMASK(25, 24)
1178c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_NOT_FRAG		(0 << 30)
1188c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_FIRST_FRAG		(1 << 30)
1198c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_LAST_FRAG		(2 << 30)
1208c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_MID_FRAG		(3 << 30)
1218c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CFG_FRAG_MSK		GENMASK(31, 30)
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/*
1248c2ecf20Sopenharmony_ci * /-----------\ 0
1258c2ecf20Sopenharmony_ci * | ACCEL CFG |	4 * 8
1268c2ecf20Sopenharmony_ci * |-----------| 0x20
1278c2ecf20Sopenharmony_ci * | CRYPT KEY |	8 * 4
1288c2ecf20Sopenharmony_ci * |-----------| 0x40
1298c2ecf20Sopenharmony_ci * |  IV   IN  |	4 * 4
1308c2ecf20Sopenharmony_ci * |-----------| 0x40 (inplace)
1318c2ecf20Sopenharmony_ci * |  IV BUF   |	4 * 4
1328c2ecf20Sopenharmony_ci * |-----------| 0x80
1338c2ecf20Sopenharmony_ci * |  DATA IN  |	16 * x (max ->max_req_size)
1348c2ecf20Sopenharmony_ci * |-----------| 0x80 (inplace operation)
1358c2ecf20Sopenharmony_ci * |  DATA OUT |	16 * x (max ->max_req_size)
1368c2ecf20Sopenharmony_ci * \-----------/ SRAM size
1378c2ecf20Sopenharmony_ci */
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci/*
1408c2ecf20Sopenharmony_ci * Hashing memory map:
1418c2ecf20Sopenharmony_ci * /-----------\ 0
1428c2ecf20Sopenharmony_ci * | ACCEL CFG |        4 * 8
1438c2ecf20Sopenharmony_ci * |-----------| 0x20
1448c2ecf20Sopenharmony_ci * | Inner IV  |        8 * 4
1458c2ecf20Sopenharmony_ci * |-----------| 0x40
1468c2ecf20Sopenharmony_ci * | Outer IV  |        8 * 4
1478c2ecf20Sopenharmony_ci * |-----------| 0x60
1488c2ecf20Sopenharmony_ci * | Output BUF|        8 * 4
1498c2ecf20Sopenharmony_ci * |-----------| 0x80
1508c2ecf20Sopenharmony_ci * |  DATA IN  |        64 * x (max ->max_req_size)
1518c2ecf20Sopenharmony_ci * \-----------/ SRAM size
1528c2ecf20Sopenharmony_ci */
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci#define CESA_SA_CFG_SRAM_OFFSET			0x00
1558c2ecf20Sopenharmony_ci#define CESA_SA_DATA_SRAM_OFFSET		0x80
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci#define CESA_SA_CRYPT_KEY_SRAM_OFFSET		0x20
1588c2ecf20Sopenharmony_ci#define CESA_SA_CRYPT_IV_SRAM_OFFSET		0x40
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci#define CESA_SA_MAC_IIV_SRAM_OFFSET		0x20
1618c2ecf20Sopenharmony_ci#define CESA_SA_MAC_OIV_SRAM_OFFSET		0x40
1628c2ecf20Sopenharmony_ci#define CESA_SA_MAC_DIG_SRAM_OFFSET		0x60
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CRYPT_DATA(offset)					\
1658c2ecf20Sopenharmony_ci	cpu_to_le32((CESA_SA_DATA_SRAM_OFFSET + (offset)) |		\
1668c2ecf20Sopenharmony_ci		    ((CESA_SA_DATA_SRAM_OFFSET + (offset)) << 16))
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CRYPT_IV(offset)					\
1698c2ecf20Sopenharmony_ci	cpu_to_le32((CESA_SA_CRYPT_IV_SRAM_OFFSET + (offset)) |	\
1708c2ecf20Sopenharmony_ci		    ((CESA_SA_CRYPT_IV_SRAM_OFFSET + (offset)) << 16))
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci#define CESA_SA_DESC_CRYPT_KEY(offset)					\
1738c2ecf20Sopenharmony_ci	cpu_to_le32(CESA_SA_CRYPT_KEY_SRAM_OFFSET + (offset))
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci#define CESA_SA_DESC_MAC_DATA(offset)					\
1768c2ecf20Sopenharmony_ci	cpu_to_le32(CESA_SA_DATA_SRAM_OFFSET + (offset))
1778c2ecf20Sopenharmony_ci#define CESA_SA_DESC_MAC_DATA_MSK		cpu_to_le32(GENMASK(15, 0))
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci#define CESA_SA_DESC_MAC_TOTAL_LEN(total_len)	cpu_to_le32((total_len) << 16)
1808c2ecf20Sopenharmony_ci#define CESA_SA_DESC_MAC_TOTAL_LEN_MSK		cpu_to_le32(GENMASK(31, 16))
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci#define CESA_SA_DESC_MAC_SRC_TOTAL_LEN_MAX	0xffff
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci#define CESA_SA_DESC_MAC_DIGEST(offset)					\
1858c2ecf20Sopenharmony_ci	cpu_to_le32(CESA_SA_MAC_DIG_SRAM_OFFSET + (offset))
1868c2ecf20Sopenharmony_ci#define CESA_SA_DESC_MAC_DIGEST_MSK		cpu_to_le32(GENMASK(15, 0))
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci#define CESA_SA_DESC_MAC_FRAG_LEN(frag_len)	cpu_to_le32((frag_len) << 16)
1898c2ecf20Sopenharmony_ci#define CESA_SA_DESC_MAC_FRAG_LEN_MSK		cpu_to_le32(GENMASK(31, 16))
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci#define CESA_SA_DESC_MAC_IV(offset)					\
1928c2ecf20Sopenharmony_ci	cpu_to_le32((CESA_SA_MAC_IIV_SRAM_OFFSET + (offset)) |		\
1938c2ecf20Sopenharmony_ci		    ((CESA_SA_MAC_OIV_SRAM_OFFSET + (offset)) << 16))
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci#define CESA_SA_SRAM_SIZE			2048
1968c2ecf20Sopenharmony_ci#define CESA_SA_SRAM_PAYLOAD_SIZE		(cesa_dev->sram_size - \
1978c2ecf20Sopenharmony_ci						 CESA_SA_DATA_SRAM_OFFSET)
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci#define CESA_SA_DEFAULT_SRAM_SIZE		2048
2008c2ecf20Sopenharmony_ci#define CESA_SA_MIN_SRAM_SIZE			1024
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci#define CESA_SA_SRAM_MSK			(2048 - 1)
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci#define CESA_MAX_HASH_BLOCK_SIZE		64
2058c2ecf20Sopenharmony_ci#define CESA_HASH_BLOCK_SIZE_MSK		(CESA_MAX_HASH_BLOCK_SIZE - 1)
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci/**
2088c2ecf20Sopenharmony_ci * struct mv_cesa_sec_accel_desc - security accelerator descriptor
2098c2ecf20Sopenharmony_ci * @config:	engine config
2108c2ecf20Sopenharmony_ci * @enc_p:	input and output data pointers for a cipher operation
2118c2ecf20Sopenharmony_ci * @enc_len:	cipher operation length
2128c2ecf20Sopenharmony_ci * @enc_key_p:	cipher key pointer
2138c2ecf20Sopenharmony_ci * @enc_iv:	cipher IV pointers
2148c2ecf20Sopenharmony_ci * @mac_src_p:	input pointer and total hash length
2158c2ecf20Sopenharmony_ci * @mac_digest:	digest pointer and hash operation length
2168c2ecf20Sopenharmony_ci * @mac_iv:	hmac IV pointers
2178c2ecf20Sopenharmony_ci *
2188c2ecf20Sopenharmony_ci * Structure passed to the CESA engine to describe the crypto operation
2198c2ecf20Sopenharmony_ci * to be executed.
2208c2ecf20Sopenharmony_ci */
2218c2ecf20Sopenharmony_cistruct mv_cesa_sec_accel_desc {
2228c2ecf20Sopenharmony_ci	__le32 config;
2238c2ecf20Sopenharmony_ci	__le32 enc_p;
2248c2ecf20Sopenharmony_ci	__le32 enc_len;
2258c2ecf20Sopenharmony_ci	__le32 enc_key_p;
2268c2ecf20Sopenharmony_ci	__le32 enc_iv;
2278c2ecf20Sopenharmony_ci	__le32 mac_src_p;
2288c2ecf20Sopenharmony_ci	__le32 mac_digest;
2298c2ecf20Sopenharmony_ci	__le32 mac_iv;
2308c2ecf20Sopenharmony_ci};
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci/**
2338c2ecf20Sopenharmony_ci * struct mv_cesa_skcipher_op_ctx - cipher operation context
2348c2ecf20Sopenharmony_ci * @key:	cipher key
2358c2ecf20Sopenharmony_ci * @iv:		cipher IV
2368c2ecf20Sopenharmony_ci *
2378c2ecf20Sopenharmony_ci * Context associated to a cipher operation.
2388c2ecf20Sopenharmony_ci */
2398c2ecf20Sopenharmony_cistruct mv_cesa_skcipher_op_ctx {
2408c2ecf20Sopenharmony_ci	__le32 key[8];
2418c2ecf20Sopenharmony_ci	u32 iv[4];
2428c2ecf20Sopenharmony_ci};
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci/**
2458c2ecf20Sopenharmony_ci * struct mv_cesa_hash_op_ctx - hash or hmac operation context
2468c2ecf20Sopenharmony_ci * @key:	cipher key
2478c2ecf20Sopenharmony_ci * @iv:		cipher IV
2488c2ecf20Sopenharmony_ci *
2498c2ecf20Sopenharmony_ci * Context associated to an hash or hmac operation.
2508c2ecf20Sopenharmony_ci */
2518c2ecf20Sopenharmony_cistruct mv_cesa_hash_op_ctx {
2528c2ecf20Sopenharmony_ci	u32 iv[16];
2538c2ecf20Sopenharmony_ci	__le32 hash[8];
2548c2ecf20Sopenharmony_ci};
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci/**
2578c2ecf20Sopenharmony_ci * struct mv_cesa_op_ctx - crypto operation context
2588c2ecf20Sopenharmony_ci * @desc:	CESA descriptor
2598c2ecf20Sopenharmony_ci * @ctx:	context associated to the crypto operation
2608c2ecf20Sopenharmony_ci *
2618c2ecf20Sopenharmony_ci * Context associated to a crypto operation.
2628c2ecf20Sopenharmony_ci */
2638c2ecf20Sopenharmony_cistruct mv_cesa_op_ctx {
2648c2ecf20Sopenharmony_ci	struct mv_cesa_sec_accel_desc desc;
2658c2ecf20Sopenharmony_ci	union {
2668c2ecf20Sopenharmony_ci		struct mv_cesa_skcipher_op_ctx skcipher;
2678c2ecf20Sopenharmony_ci		struct mv_cesa_hash_op_ctx hash;
2688c2ecf20Sopenharmony_ci	} ctx;
2698c2ecf20Sopenharmony_ci};
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci/* TDMA descriptor flags */
2728c2ecf20Sopenharmony_ci#define CESA_TDMA_DST_IN_SRAM			BIT(31)
2738c2ecf20Sopenharmony_ci#define CESA_TDMA_SRC_IN_SRAM			BIT(30)
2748c2ecf20Sopenharmony_ci#define CESA_TDMA_END_OF_REQ			BIT(29)
2758c2ecf20Sopenharmony_ci#define CESA_TDMA_BREAK_CHAIN			BIT(28)
2768c2ecf20Sopenharmony_ci#define CESA_TDMA_SET_STATE			BIT(27)
2778c2ecf20Sopenharmony_ci#define CESA_TDMA_TYPE_MSK			GENMASK(26, 0)
2788c2ecf20Sopenharmony_ci#define CESA_TDMA_DUMMY				0
2798c2ecf20Sopenharmony_ci#define CESA_TDMA_DATA				1
2808c2ecf20Sopenharmony_ci#define CESA_TDMA_OP				2
2818c2ecf20Sopenharmony_ci#define CESA_TDMA_RESULT			3
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci/**
2848c2ecf20Sopenharmony_ci * struct mv_cesa_tdma_desc - TDMA descriptor
2858c2ecf20Sopenharmony_ci * @byte_cnt:	number of bytes to transfer
2868c2ecf20Sopenharmony_ci * @src:	DMA address of the source
2878c2ecf20Sopenharmony_ci * @dst:	DMA address of the destination
2888c2ecf20Sopenharmony_ci * @next_dma:	DMA address of the next TDMA descriptor
2898c2ecf20Sopenharmony_ci * @cur_dma:	DMA address of this TDMA descriptor
2908c2ecf20Sopenharmony_ci * @next:	pointer to the next TDMA descriptor
2918c2ecf20Sopenharmony_ci * @op:		CESA operation attached to this TDMA descriptor
2928c2ecf20Sopenharmony_ci * @data:	raw data attached to this TDMA descriptor
2938c2ecf20Sopenharmony_ci * @flags:	flags describing the TDMA transfer. See the
2948c2ecf20Sopenharmony_ci *		"TDMA descriptor flags" section above
2958c2ecf20Sopenharmony_ci *
2968c2ecf20Sopenharmony_ci * TDMA descriptor used to create a transfer chain describing a crypto
2978c2ecf20Sopenharmony_ci * operation.
2988c2ecf20Sopenharmony_ci */
2998c2ecf20Sopenharmony_cistruct mv_cesa_tdma_desc {
3008c2ecf20Sopenharmony_ci	__le32 byte_cnt;
3018c2ecf20Sopenharmony_ci	union {
3028c2ecf20Sopenharmony_ci		__le32 src;
3038c2ecf20Sopenharmony_ci		u32 src_dma;
3048c2ecf20Sopenharmony_ci	};
3058c2ecf20Sopenharmony_ci	union {
3068c2ecf20Sopenharmony_ci		__le32 dst;
3078c2ecf20Sopenharmony_ci		u32 dst_dma;
3088c2ecf20Sopenharmony_ci	};
3098c2ecf20Sopenharmony_ci	__le32 next_dma;
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	/* Software state */
3128c2ecf20Sopenharmony_ci	dma_addr_t cur_dma;
3138c2ecf20Sopenharmony_ci	struct mv_cesa_tdma_desc *next;
3148c2ecf20Sopenharmony_ci	union {
3158c2ecf20Sopenharmony_ci		struct mv_cesa_op_ctx *op;
3168c2ecf20Sopenharmony_ci		void *data;
3178c2ecf20Sopenharmony_ci	};
3188c2ecf20Sopenharmony_ci	u32 flags;
3198c2ecf20Sopenharmony_ci};
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci/**
3228c2ecf20Sopenharmony_ci * struct mv_cesa_sg_dma_iter - scatter-gather iterator
3238c2ecf20Sopenharmony_ci * @dir:	transfer direction
3248c2ecf20Sopenharmony_ci * @sg:		scatter list
3258c2ecf20Sopenharmony_ci * @offset:	current position in the scatter list
3268c2ecf20Sopenharmony_ci * @op_offset:	current position in the crypto operation
3278c2ecf20Sopenharmony_ci *
3288c2ecf20Sopenharmony_ci * Iterator used to iterate over a scatterlist while creating a TDMA chain for
3298c2ecf20Sopenharmony_ci * a crypto operation.
3308c2ecf20Sopenharmony_ci */
3318c2ecf20Sopenharmony_cistruct mv_cesa_sg_dma_iter {
3328c2ecf20Sopenharmony_ci	enum dma_data_direction dir;
3338c2ecf20Sopenharmony_ci	struct scatterlist *sg;
3348c2ecf20Sopenharmony_ci	unsigned int offset;
3358c2ecf20Sopenharmony_ci	unsigned int op_offset;
3368c2ecf20Sopenharmony_ci};
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci/**
3398c2ecf20Sopenharmony_ci * struct mv_cesa_dma_iter - crypto operation iterator
3408c2ecf20Sopenharmony_ci * @len:	the crypto operation length
3418c2ecf20Sopenharmony_ci * @offset:	current position in the crypto operation
3428c2ecf20Sopenharmony_ci * @op_len:	sub-operation length (the crypto engine can only act on 2kb
3438c2ecf20Sopenharmony_ci *		chunks)
3448c2ecf20Sopenharmony_ci *
3458c2ecf20Sopenharmony_ci * Iterator used to create a TDMA chain for a given crypto operation.
3468c2ecf20Sopenharmony_ci */
3478c2ecf20Sopenharmony_cistruct mv_cesa_dma_iter {
3488c2ecf20Sopenharmony_ci	unsigned int len;
3498c2ecf20Sopenharmony_ci	unsigned int offset;
3508c2ecf20Sopenharmony_ci	unsigned int op_len;
3518c2ecf20Sopenharmony_ci};
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci/**
3548c2ecf20Sopenharmony_ci * struct mv_cesa_tdma_chain - TDMA chain
3558c2ecf20Sopenharmony_ci * @first:	first entry in the TDMA chain
3568c2ecf20Sopenharmony_ci * @last:	last entry in the TDMA chain
3578c2ecf20Sopenharmony_ci *
3588c2ecf20Sopenharmony_ci * Stores a TDMA chain for a specific crypto operation.
3598c2ecf20Sopenharmony_ci */
3608c2ecf20Sopenharmony_cistruct mv_cesa_tdma_chain {
3618c2ecf20Sopenharmony_ci	struct mv_cesa_tdma_desc *first;
3628c2ecf20Sopenharmony_ci	struct mv_cesa_tdma_desc *last;
3638c2ecf20Sopenharmony_ci};
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_cistruct mv_cesa_engine;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci/**
3688c2ecf20Sopenharmony_ci * struct mv_cesa_caps - CESA device capabilities
3698c2ecf20Sopenharmony_ci * @engines:		number of engines
3708c2ecf20Sopenharmony_ci * @has_tdma:		whether this device has a TDMA block
3718c2ecf20Sopenharmony_ci * @cipher_algs:	supported cipher algorithms
3728c2ecf20Sopenharmony_ci * @ncipher_algs:	number of supported cipher algorithms
3738c2ecf20Sopenharmony_ci * @ahash_algs:		supported hash algorithms
3748c2ecf20Sopenharmony_ci * @nahash_algs:	number of supported hash algorithms
3758c2ecf20Sopenharmony_ci *
3768c2ecf20Sopenharmony_ci * Structure used to describe CESA device capabilities.
3778c2ecf20Sopenharmony_ci */
3788c2ecf20Sopenharmony_cistruct mv_cesa_caps {
3798c2ecf20Sopenharmony_ci	int nengines;
3808c2ecf20Sopenharmony_ci	bool has_tdma;
3818c2ecf20Sopenharmony_ci	struct skcipher_alg **cipher_algs;
3828c2ecf20Sopenharmony_ci	int ncipher_algs;
3838c2ecf20Sopenharmony_ci	struct ahash_alg **ahash_algs;
3848c2ecf20Sopenharmony_ci	int nahash_algs;
3858c2ecf20Sopenharmony_ci};
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci/**
3888c2ecf20Sopenharmony_ci * struct mv_cesa_dev_dma - DMA pools
3898c2ecf20Sopenharmony_ci * @tdma_desc_pool:	TDMA desc pool
3908c2ecf20Sopenharmony_ci * @op_pool:		crypto operation pool
3918c2ecf20Sopenharmony_ci * @cache_pool:		data cache pool (used by hash implementation when the
3928c2ecf20Sopenharmony_ci *			hash request is smaller than the hash block size)
3938c2ecf20Sopenharmony_ci * @padding_pool:	padding pool (used by hash implementation when hardware
3948c2ecf20Sopenharmony_ci *			padding cannot be used)
3958c2ecf20Sopenharmony_ci *
3968c2ecf20Sopenharmony_ci * Structure containing the different DMA pools used by this driver.
3978c2ecf20Sopenharmony_ci */
3988c2ecf20Sopenharmony_cistruct mv_cesa_dev_dma {
3998c2ecf20Sopenharmony_ci	struct dma_pool *tdma_desc_pool;
4008c2ecf20Sopenharmony_ci	struct dma_pool *op_pool;
4018c2ecf20Sopenharmony_ci	struct dma_pool *cache_pool;
4028c2ecf20Sopenharmony_ci	struct dma_pool *padding_pool;
4038c2ecf20Sopenharmony_ci};
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci/**
4068c2ecf20Sopenharmony_ci * struct mv_cesa_dev - CESA device
4078c2ecf20Sopenharmony_ci * @caps:	device capabilities
4088c2ecf20Sopenharmony_ci * @regs:	device registers
4098c2ecf20Sopenharmony_ci * @sram_size:	usable SRAM size
4108c2ecf20Sopenharmony_ci * @lock:	device lock
4118c2ecf20Sopenharmony_ci * @engines:	array of engines
4128c2ecf20Sopenharmony_ci * @dma:	dma pools
4138c2ecf20Sopenharmony_ci *
4148c2ecf20Sopenharmony_ci * Structure storing CESA device information.
4158c2ecf20Sopenharmony_ci */
4168c2ecf20Sopenharmony_cistruct mv_cesa_dev {
4178c2ecf20Sopenharmony_ci	const struct mv_cesa_caps *caps;
4188c2ecf20Sopenharmony_ci	void __iomem *regs;
4198c2ecf20Sopenharmony_ci	struct device *dev;
4208c2ecf20Sopenharmony_ci	unsigned int sram_size;
4218c2ecf20Sopenharmony_ci	spinlock_t lock;
4228c2ecf20Sopenharmony_ci	struct mv_cesa_engine *engines;
4238c2ecf20Sopenharmony_ci	struct mv_cesa_dev_dma *dma;
4248c2ecf20Sopenharmony_ci};
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci/**
4278c2ecf20Sopenharmony_ci * struct mv_cesa_engine - CESA engine
4288c2ecf20Sopenharmony_ci * @id:			engine id
4298c2ecf20Sopenharmony_ci * @regs:		engine registers
4308c2ecf20Sopenharmony_ci * @sram:		SRAM memory region
4318c2ecf20Sopenharmony_ci * @sram_dma:		DMA address of the SRAM memory region
4328c2ecf20Sopenharmony_ci * @lock:		engine lock
4338c2ecf20Sopenharmony_ci * @req:		current crypto request
4348c2ecf20Sopenharmony_ci * @clk:		engine clk
4358c2ecf20Sopenharmony_ci * @zclk:		engine zclk
4368c2ecf20Sopenharmony_ci * @max_req_len:	maximum chunk length (useful to create the TDMA chain)
4378c2ecf20Sopenharmony_ci * @int_mask:		interrupt mask cache
4388c2ecf20Sopenharmony_ci * @pool:		memory pool pointing to the memory region reserved in
4398c2ecf20Sopenharmony_ci *			SRAM
4408c2ecf20Sopenharmony_ci * @queue:		fifo of the pending crypto requests
4418c2ecf20Sopenharmony_ci * @load:		engine load counter, useful for load balancing
4428c2ecf20Sopenharmony_ci * @chain:		list of the current tdma descriptors being processed
4438c2ecf20Sopenharmony_ci *			by this engine.
4448c2ecf20Sopenharmony_ci * @complete_queue:	fifo of the processed requests by the engine
4458c2ecf20Sopenharmony_ci *
4468c2ecf20Sopenharmony_ci * Structure storing CESA engine information.
4478c2ecf20Sopenharmony_ci */
4488c2ecf20Sopenharmony_cistruct mv_cesa_engine {
4498c2ecf20Sopenharmony_ci	int id;
4508c2ecf20Sopenharmony_ci	void __iomem *regs;
4518c2ecf20Sopenharmony_ci	void __iomem *sram;
4528c2ecf20Sopenharmony_ci	dma_addr_t sram_dma;
4538c2ecf20Sopenharmony_ci	spinlock_t lock;
4548c2ecf20Sopenharmony_ci	struct crypto_async_request *req;
4558c2ecf20Sopenharmony_ci	struct clk *clk;
4568c2ecf20Sopenharmony_ci	struct clk *zclk;
4578c2ecf20Sopenharmony_ci	size_t max_req_len;
4588c2ecf20Sopenharmony_ci	u32 int_mask;
4598c2ecf20Sopenharmony_ci	struct gen_pool *pool;
4608c2ecf20Sopenharmony_ci	struct crypto_queue queue;
4618c2ecf20Sopenharmony_ci	atomic_t load;
4628c2ecf20Sopenharmony_ci	struct mv_cesa_tdma_chain chain;
4638c2ecf20Sopenharmony_ci	struct list_head complete_queue;
4648c2ecf20Sopenharmony_ci	int irq;
4658c2ecf20Sopenharmony_ci};
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci/**
4688c2ecf20Sopenharmony_ci * struct mv_cesa_req_ops - CESA request operations
4698c2ecf20Sopenharmony_ci * @process:	process a request chunk result (should return 0 if the
4708c2ecf20Sopenharmony_ci *		operation, -EINPROGRESS if it needs more steps or an error
4718c2ecf20Sopenharmony_ci *		code)
4728c2ecf20Sopenharmony_ci * @step:	launch the crypto operation on the next chunk
4738c2ecf20Sopenharmony_ci * @cleanup:	cleanup the crypto request (release associated data)
4748c2ecf20Sopenharmony_ci * @complete:	complete the request, i.e copy result or context from sram when
4758c2ecf20Sopenharmony_ci *		needed.
4768c2ecf20Sopenharmony_ci */
4778c2ecf20Sopenharmony_cistruct mv_cesa_req_ops {
4788c2ecf20Sopenharmony_ci	int (*process)(struct crypto_async_request *req, u32 status);
4798c2ecf20Sopenharmony_ci	void (*step)(struct crypto_async_request *req);
4808c2ecf20Sopenharmony_ci	void (*cleanup)(struct crypto_async_request *req);
4818c2ecf20Sopenharmony_ci	void (*complete)(struct crypto_async_request *req);
4828c2ecf20Sopenharmony_ci};
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci/**
4858c2ecf20Sopenharmony_ci * struct mv_cesa_ctx - CESA operation context
4868c2ecf20Sopenharmony_ci * @ops:	crypto operations
4878c2ecf20Sopenharmony_ci *
4888c2ecf20Sopenharmony_ci * Base context structure inherited by operation specific ones.
4898c2ecf20Sopenharmony_ci */
4908c2ecf20Sopenharmony_cistruct mv_cesa_ctx {
4918c2ecf20Sopenharmony_ci	const struct mv_cesa_req_ops *ops;
4928c2ecf20Sopenharmony_ci};
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci/**
4958c2ecf20Sopenharmony_ci * struct mv_cesa_hash_ctx - CESA hash operation context
4968c2ecf20Sopenharmony_ci * @base:	base context structure
4978c2ecf20Sopenharmony_ci *
4988c2ecf20Sopenharmony_ci * Hash context structure.
4998c2ecf20Sopenharmony_ci */
5008c2ecf20Sopenharmony_cistruct mv_cesa_hash_ctx {
5018c2ecf20Sopenharmony_ci	struct mv_cesa_ctx base;
5028c2ecf20Sopenharmony_ci};
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci/**
5058c2ecf20Sopenharmony_ci * struct mv_cesa_hash_ctx - CESA hmac operation context
5068c2ecf20Sopenharmony_ci * @base:	base context structure
5078c2ecf20Sopenharmony_ci * @iv:		initialization vectors
5088c2ecf20Sopenharmony_ci *
5098c2ecf20Sopenharmony_ci * HMAC context structure.
5108c2ecf20Sopenharmony_ci */
5118c2ecf20Sopenharmony_cistruct mv_cesa_hmac_ctx {
5128c2ecf20Sopenharmony_ci	struct mv_cesa_ctx base;
5138c2ecf20Sopenharmony_ci	__be32 iv[16];
5148c2ecf20Sopenharmony_ci};
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci/**
5178c2ecf20Sopenharmony_ci * enum mv_cesa_req_type - request type definitions
5188c2ecf20Sopenharmony_ci * @CESA_STD_REQ:	standard request
5198c2ecf20Sopenharmony_ci * @CESA_DMA_REQ:	DMA request
5208c2ecf20Sopenharmony_ci */
5218c2ecf20Sopenharmony_cienum mv_cesa_req_type {
5228c2ecf20Sopenharmony_ci	CESA_STD_REQ,
5238c2ecf20Sopenharmony_ci	CESA_DMA_REQ,
5248c2ecf20Sopenharmony_ci};
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci/**
5278c2ecf20Sopenharmony_ci * struct mv_cesa_req - CESA request
5288c2ecf20Sopenharmony_ci * @engine:	engine associated with this request
5298c2ecf20Sopenharmony_ci * @chain:	list of tdma descriptors associated  with this request
5308c2ecf20Sopenharmony_ci */
5318c2ecf20Sopenharmony_cistruct mv_cesa_req {
5328c2ecf20Sopenharmony_ci	struct mv_cesa_engine *engine;
5338c2ecf20Sopenharmony_ci	struct mv_cesa_tdma_chain chain;
5348c2ecf20Sopenharmony_ci};
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci/**
5378c2ecf20Sopenharmony_ci * struct mv_cesa_sg_std_iter - CESA scatter-gather iterator for standard
5388c2ecf20Sopenharmony_ci *				requests
5398c2ecf20Sopenharmony_ci * @iter:	sg mapping iterator
5408c2ecf20Sopenharmony_ci * @offset:	current offset in the SG entry mapped in memory
5418c2ecf20Sopenharmony_ci */
5428c2ecf20Sopenharmony_cistruct mv_cesa_sg_std_iter {
5438c2ecf20Sopenharmony_ci	struct sg_mapping_iter iter;
5448c2ecf20Sopenharmony_ci	unsigned int offset;
5458c2ecf20Sopenharmony_ci};
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci/**
5488c2ecf20Sopenharmony_ci * struct mv_cesa_skcipher_std_req - cipher standard request
5498c2ecf20Sopenharmony_ci * @op:		operation context
5508c2ecf20Sopenharmony_ci * @offset:	current operation offset
5518c2ecf20Sopenharmony_ci * @size:	size of the crypto operation
5528c2ecf20Sopenharmony_ci */
5538c2ecf20Sopenharmony_cistruct mv_cesa_skcipher_std_req {
5548c2ecf20Sopenharmony_ci	struct mv_cesa_op_ctx op;
5558c2ecf20Sopenharmony_ci	unsigned int offset;
5568c2ecf20Sopenharmony_ci	unsigned int size;
5578c2ecf20Sopenharmony_ci	bool skip_ctx;
5588c2ecf20Sopenharmony_ci};
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci/**
5618c2ecf20Sopenharmony_ci * struct mv_cesa_skcipher_req - cipher request
5628c2ecf20Sopenharmony_ci * @req:	type specific request information
5638c2ecf20Sopenharmony_ci * @src_nents:	number of entries in the src sg list
5648c2ecf20Sopenharmony_ci * @dst_nents:	number of entries in the dest sg list
5658c2ecf20Sopenharmony_ci */
5668c2ecf20Sopenharmony_cistruct mv_cesa_skcipher_req {
5678c2ecf20Sopenharmony_ci	struct mv_cesa_req base;
5688c2ecf20Sopenharmony_ci	struct mv_cesa_skcipher_std_req std;
5698c2ecf20Sopenharmony_ci	int src_nents;
5708c2ecf20Sopenharmony_ci	int dst_nents;
5718c2ecf20Sopenharmony_ci};
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci/**
5748c2ecf20Sopenharmony_ci * struct mv_cesa_ahash_std_req - standard hash request
5758c2ecf20Sopenharmony_ci * @offset:	current operation offset
5768c2ecf20Sopenharmony_ci */
5778c2ecf20Sopenharmony_cistruct mv_cesa_ahash_std_req {
5788c2ecf20Sopenharmony_ci	unsigned int offset;
5798c2ecf20Sopenharmony_ci};
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci/**
5828c2ecf20Sopenharmony_ci * struct mv_cesa_ahash_dma_req - DMA hash request
5838c2ecf20Sopenharmony_ci * @padding:		padding buffer
5848c2ecf20Sopenharmony_ci * @padding_dma:	DMA address of the padding buffer
5858c2ecf20Sopenharmony_ci * @cache_dma:		DMA address of the cache buffer
5868c2ecf20Sopenharmony_ci */
5878c2ecf20Sopenharmony_cistruct mv_cesa_ahash_dma_req {
5888c2ecf20Sopenharmony_ci	u8 *padding;
5898c2ecf20Sopenharmony_ci	dma_addr_t padding_dma;
5908c2ecf20Sopenharmony_ci	u8 *cache;
5918c2ecf20Sopenharmony_ci	dma_addr_t cache_dma;
5928c2ecf20Sopenharmony_ci};
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci/**
5958c2ecf20Sopenharmony_ci * struct mv_cesa_ahash_req - hash request
5968c2ecf20Sopenharmony_ci * @req:		type specific request information
5978c2ecf20Sopenharmony_ci * @cache:		cache buffer
5988c2ecf20Sopenharmony_ci * @cache_ptr:		write pointer in the cache buffer
5998c2ecf20Sopenharmony_ci * @len:		hash total length
6008c2ecf20Sopenharmony_ci * @src_nents:		number of entries in the scatterlist
6018c2ecf20Sopenharmony_ci * @last_req:		define whether the current operation is the last one
6028c2ecf20Sopenharmony_ci *			or not
6038c2ecf20Sopenharmony_ci * @state:		hash state
6048c2ecf20Sopenharmony_ci */
6058c2ecf20Sopenharmony_cistruct mv_cesa_ahash_req {
6068c2ecf20Sopenharmony_ci	struct mv_cesa_req base;
6078c2ecf20Sopenharmony_ci	union {
6088c2ecf20Sopenharmony_ci		struct mv_cesa_ahash_dma_req dma;
6098c2ecf20Sopenharmony_ci		struct mv_cesa_ahash_std_req std;
6108c2ecf20Sopenharmony_ci	} req;
6118c2ecf20Sopenharmony_ci	struct mv_cesa_op_ctx op_tmpl;
6128c2ecf20Sopenharmony_ci	u8 cache[CESA_MAX_HASH_BLOCK_SIZE];
6138c2ecf20Sopenharmony_ci	unsigned int cache_ptr;
6148c2ecf20Sopenharmony_ci	u64 len;
6158c2ecf20Sopenharmony_ci	int src_nents;
6168c2ecf20Sopenharmony_ci	bool last_req;
6178c2ecf20Sopenharmony_ci	bool algo_le;
6188c2ecf20Sopenharmony_ci	u32 state[8];
6198c2ecf20Sopenharmony_ci};
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci/* CESA functions */
6228c2ecf20Sopenharmony_ci
6238c2ecf20Sopenharmony_ciextern struct mv_cesa_dev *cesa_dev;
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_cistatic inline void
6278c2ecf20Sopenharmony_cimv_cesa_engine_enqueue_complete_request(struct mv_cesa_engine *engine,
6288c2ecf20Sopenharmony_ci					struct crypto_async_request *req)
6298c2ecf20Sopenharmony_ci{
6308c2ecf20Sopenharmony_ci	list_add_tail(&req->list, &engine->complete_queue);
6318c2ecf20Sopenharmony_ci}
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_cistatic inline struct crypto_async_request *
6348c2ecf20Sopenharmony_cimv_cesa_engine_dequeue_complete_request(struct mv_cesa_engine *engine)
6358c2ecf20Sopenharmony_ci{
6368c2ecf20Sopenharmony_ci	struct crypto_async_request *req;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	req = list_first_entry_or_null(&engine->complete_queue,
6398c2ecf20Sopenharmony_ci				       struct crypto_async_request,
6408c2ecf20Sopenharmony_ci				       list);
6418c2ecf20Sopenharmony_ci	if (req)
6428c2ecf20Sopenharmony_ci		list_del(&req->list);
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	return req;
6458c2ecf20Sopenharmony_ci}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_cistatic inline enum mv_cesa_req_type
6498c2ecf20Sopenharmony_cimv_cesa_req_get_type(struct mv_cesa_req *req)
6508c2ecf20Sopenharmony_ci{
6518c2ecf20Sopenharmony_ci	return req->chain.first ? CESA_DMA_REQ : CESA_STD_REQ;
6528c2ecf20Sopenharmony_ci}
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_cistatic inline void mv_cesa_update_op_cfg(struct mv_cesa_op_ctx *op,
6558c2ecf20Sopenharmony_ci					 u32 cfg, u32 mask)
6568c2ecf20Sopenharmony_ci{
6578c2ecf20Sopenharmony_ci	op->desc.config &= cpu_to_le32(~mask);
6588c2ecf20Sopenharmony_ci	op->desc.config |= cpu_to_le32(cfg);
6598c2ecf20Sopenharmony_ci}
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_cistatic inline u32 mv_cesa_get_op_cfg(const struct mv_cesa_op_ctx *op)
6628c2ecf20Sopenharmony_ci{
6638c2ecf20Sopenharmony_ci	return le32_to_cpu(op->desc.config);
6648c2ecf20Sopenharmony_ci}
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_cistatic inline void mv_cesa_set_op_cfg(struct mv_cesa_op_ctx *op, u32 cfg)
6678c2ecf20Sopenharmony_ci{
6688c2ecf20Sopenharmony_ci	op->desc.config = cpu_to_le32(cfg);
6698c2ecf20Sopenharmony_ci}
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_cistatic inline void mv_cesa_adjust_op(struct mv_cesa_engine *engine,
6728c2ecf20Sopenharmony_ci				     struct mv_cesa_op_ctx *op)
6738c2ecf20Sopenharmony_ci{
6748c2ecf20Sopenharmony_ci	u32 offset = engine->sram_dma & CESA_SA_SRAM_MSK;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	op->desc.enc_p = CESA_SA_DESC_CRYPT_DATA(offset);
6778c2ecf20Sopenharmony_ci	op->desc.enc_key_p = CESA_SA_DESC_CRYPT_KEY(offset);
6788c2ecf20Sopenharmony_ci	op->desc.enc_iv = CESA_SA_DESC_CRYPT_IV(offset);
6798c2ecf20Sopenharmony_ci	op->desc.mac_src_p &= ~CESA_SA_DESC_MAC_DATA_MSK;
6808c2ecf20Sopenharmony_ci	op->desc.mac_src_p |= CESA_SA_DESC_MAC_DATA(offset);
6818c2ecf20Sopenharmony_ci	op->desc.mac_digest &= ~CESA_SA_DESC_MAC_DIGEST_MSK;
6828c2ecf20Sopenharmony_ci	op->desc.mac_digest |= CESA_SA_DESC_MAC_DIGEST(offset);
6838c2ecf20Sopenharmony_ci	op->desc.mac_iv = CESA_SA_DESC_MAC_IV(offset);
6848c2ecf20Sopenharmony_ci}
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_cistatic inline void mv_cesa_set_crypt_op_len(struct mv_cesa_op_ctx *op, int len)
6878c2ecf20Sopenharmony_ci{
6888c2ecf20Sopenharmony_ci	op->desc.enc_len = cpu_to_le32(len);
6898c2ecf20Sopenharmony_ci}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_cistatic inline void mv_cesa_set_mac_op_total_len(struct mv_cesa_op_ctx *op,
6928c2ecf20Sopenharmony_ci						int len)
6938c2ecf20Sopenharmony_ci{
6948c2ecf20Sopenharmony_ci	op->desc.mac_src_p &= ~CESA_SA_DESC_MAC_TOTAL_LEN_MSK;
6958c2ecf20Sopenharmony_ci	op->desc.mac_src_p |= CESA_SA_DESC_MAC_TOTAL_LEN(len);
6968c2ecf20Sopenharmony_ci}
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_cistatic inline void mv_cesa_set_mac_op_frag_len(struct mv_cesa_op_ctx *op,
6998c2ecf20Sopenharmony_ci					       int len)
7008c2ecf20Sopenharmony_ci{
7018c2ecf20Sopenharmony_ci	op->desc.mac_digest &= ~CESA_SA_DESC_MAC_FRAG_LEN_MSK;
7028c2ecf20Sopenharmony_ci	op->desc.mac_digest |= CESA_SA_DESC_MAC_FRAG_LEN(len);
7038c2ecf20Sopenharmony_ci}
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_cistatic inline void mv_cesa_set_int_mask(struct mv_cesa_engine *engine,
7068c2ecf20Sopenharmony_ci					u32 int_mask)
7078c2ecf20Sopenharmony_ci{
7088c2ecf20Sopenharmony_ci	if (int_mask == engine->int_mask)
7098c2ecf20Sopenharmony_ci		return;
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	writel_relaxed(int_mask, engine->regs + CESA_SA_INT_MSK);
7128c2ecf20Sopenharmony_ci	engine->int_mask = int_mask;
7138c2ecf20Sopenharmony_ci}
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_cistatic inline u32 mv_cesa_get_int_mask(struct mv_cesa_engine *engine)
7168c2ecf20Sopenharmony_ci{
7178c2ecf20Sopenharmony_ci	return engine->int_mask;
7188c2ecf20Sopenharmony_ci}
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_cistatic inline bool mv_cesa_mac_op_is_first_frag(const struct mv_cesa_op_ctx *op)
7218c2ecf20Sopenharmony_ci{
7228c2ecf20Sopenharmony_ci	return (mv_cesa_get_op_cfg(op) & CESA_SA_DESC_CFG_FRAG_MSK) ==
7238c2ecf20Sopenharmony_ci		CESA_SA_DESC_CFG_FIRST_FRAG;
7248c2ecf20Sopenharmony_ci}
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ciint mv_cesa_queue_req(struct crypto_async_request *req,
7278c2ecf20Sopenharmony_ci		      struct mv_cesa_req *creq);
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_cistruct crypto_async_request *
7308c2ecf20Sopenharmony_cimv_cesa_dequeue_req_locked(struct mv_cesa_engine *engine,
7318c2ecf20Sopenharmony_ci			   struct crypto_async_request **backlog);
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_cistatic inline struct mv_cesa_engine *mv_cesa_select_engine(int weight)
7348c2ecf20Sopenharmony_ci{
7358c2ecf20Sopenharmony_ci	int i;
7368c2ecf20Sopenharmony_ci	u32 min_load = U32_MAX;
7378c2ecf20Sopenharmony_ci	struct mv_cesa_engine *selected = NULL;
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci	for (i = 0; i < cesa_dev->caps->nengines; i++) {
7408c2ecf20Sopenharmony_ci		struct mv_cesa_engine *engine = cesa_dev->engines + i;
7418c2ecf20Sopenharmony_ci		u32 load = atomic_read(&engine->load);
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci		if (load < min_load) {
7448c2ecf20Sopenharmony_ci			min_load = load;
7458c2ecf20Sopenharmony_ci			selected = engine;
7468c2ecf20Sopenharmony_ci		}
7478c2ecf20Sopenharmony_ci	}
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci	atomic_add(weight, &selected->load);
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	return selected;
7528c2ecf20Sopenharmony_ci}
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci/*
7558c2ecf20Sopenharmony_ci * Helper function that indicates whether a crypto request needs to be
7568c2ecf20Sopenharmony_ci * cleaned up or not after being enqueued using mv_cesa_queue_req().
7578c2ecf20Sopenharmony_ci */
7588c2ecf20Sopenharmony_cistatic inline int mv_cesa_req_needs_cleanup(struct crypto_async_request *req,
7598c2ecf20Sopenharmony_ci					    int ret)
7608c2ecf20Sopenharmony_ci{
7618c2ecf20Sopenharmony_ci	/*
7628c2ecf20Sopenharmony_ci	 * The queue still had some space, the request was queued
7638c2ecf20Sopenharmony_ci	 * normally, so there's no need to clean it up.
7648c2ecf20Sopenharmony_ci	 */
7658c2ecf20Sopenharmony_ci	if (ret == -EINPROGRESS)
7668c2ecf20Sopenharmony_ci		return false;
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	/*
7698c2ecf20Sopenharmony_ci	 * The queue had not space left, but since the request is
7708c2ecf20Sopenharmony_ci	 * flagged with CRYPTO_TFM_REQ_MAY_BACKLOG, it was added to
7718c2ecf20Sopenharmony_ci	 * the backlog and will be processed later. There's no need to
7728c2ecf20Sopenharmony_ci	 * clean it up.
7738c2ecf20Sopenharmony_ci	 */
7748c2ecf20Sopenharmony_ci	if (ret == -EBUSY)
7758c2ecf20Sopenharmony_ci		return false;
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	/* Request wasn't queued, we need to clean it up */
7788c2ecf20Sopenharmony_ci	return true;
7798c2ecf20Sopenharmony_ci}
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci/* TDMA functions */
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_cistatic inline void mv_cesa_req_dma_iter_init(struct mv_cesa_dma_iter *iter,
7848c2ecf20Sopenharmony_ci					     unsigned int len)
7858c2ecf20Sopenharmony_ci{
7868c2ecf20Sopenharmony_ci	iter->len = len;
7878c2ecf20Sopenharmony_ci	iter->op_len = min(len, CESA_SA_SRAM_PAYLOAD_SIZE);
7888c2ecf20Sopenharmony_ci	iter->offset = 0;
7898c2ecf20Sopenharmony_ci}
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_cistatic inline void mv_cesa_sg_dma_iter_init(struct mv_cesa_sg_dma_iter *iter,
7928c2ecf20Sopenharmony_ci					    struct scatterlist *sg,
7938c2ecf20Sopenharmony_ci					    enum dma_data_direction dir)
7948c2ecf20Sopenharmony_ci{
7958c2ecf20Sopenharmony_ci	iter->op_offset = 0;
7968c2ecf20Sopenharmony_ci	iter->offset = 0;
7978c2ecf20Sopenharmony_ci	iter->sg = sg;
7988c2ecf20Sopenharmony_ci	iter->dir = dir;
7998c2ecf20Sopenharmony_ci}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_cistatic inline unsigned int
8028c2ecf20Sopenharmony_cimv_cesa_req_dma_iter_transfer_len(struct mv_cesa_dma_iter *iter,
8038c2ecf20Sopenharmony_ci				  struct mv_cesa_sg_dma_iter *sgiter)
8048c2ecf20Sopenharmony_ci{
8058c2ecf20Sopenharmony_ci	return min(iter->op_len - sgiter->op_offset,
8068c2ecf20Sopenharmony_ci		   sg_dma_len(sgiter->sg) - sgiter->offset);
8078c2ecf20Sopenharmony_ci}
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_cibool mv_cesa_req_dma_iter_next_transfer(struct mv_cesa_dma_iter *chain,
8108c2ecf20Sopenharmony_ci					struct mv_cesa_sg_dma_iter *sgiter,
8118c2ecf20Sopenharmony_ci					unsigned int len);
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_cistatic inline bool mv_cesa_req_dma_iter_next_op(struct mv_cesa_dma_iter *iter)
8148c2ecf20Sopenharmony_ci{
8158c2ecf20Sopenharmony_ci	iter->offset += iter->op_len;
8168c2ecf20Sopenharmony_ci	iter->op_len = min(iter->len - iter->offset,
8178c2ecf20Sopenharmony_ci			   CESA_SA_SRAM_PAYLOAD_SIZE);
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	return iter->op_len;
8208c2ecf20Sopenharmony_ci}
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_civoid mv_cesa_dma_step(struct mv_cesa_req *dreq);
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_cistatic inline int mv_cesa_dma_process(struct mv_cesa_req *dreq,
8258c2ecf20Sopenharmony_ci				      u32 status)
8268c2ecf20Sopenharmony_ci{
8278c2ecf20Sopenharmony_ci	if (!(status & CESA_SA_INT_ACC0_IDMA_DONE))
8288c2ecf20Sopenharmony_ci		return -EINPROGRESS;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	if (status & CESA_SA_INT_IDMA_OWN_ERR)
8318c2ecf20Sopenharmony_ci		return -EINVAL;
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	return 0;
8348c2ecf20Sopenharmony_ci}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_civoid mv_cesa_dma_prepare(struct mv_cesa_req *dreq,
8378c2ecf20Sopenharmony_ci			 struct mv_cesa_engine *engine);
8388c2ecf20Sopenharmony_civoid mv_cesa_dma_cleanup(struct mv_cesa_req *dreq);
8398c2ecf20Sopenharmony_civoid mv_cesa_tdma_chain(struct mv_cesa_engine *engine,
8408c2ecf20Sopenharmony_ci			struct mv_cesa_req *dreq);
8418c2ecf20Sopenharmony_ciint mv_cesa_tdma_process(struct mv_cesa_engine *engine, u32 status);
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_cistatic inline void
8458c2ecf20Sopenharmony_cimv_cesa_tdma_desc_iter_init(struct mv_cesa_tdma_chain *chain)
8468c2ecf20Sopenharmony_ci{
8478c2ecf20Sopenharmony_ci	memset(chain, 0, sizeof(*chain));
8488c2ecf20Sopenharmony_ci}
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ciint mv_cesa_dma_add_result_op(struct mv_cesa_tdma_chain *chain, dma_addr_t src,
8518c2ecf20Sopenharmony_ci			  u32 size, u32 flags, gfp_t gfp_flags);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_cistruct mv_cesa_op_ctx *mv_cesa_dma_add_op(struct mv_cesa_tdma_chain *chain,
8548c2ecf20Sopenharmony_ci					const struct mv_cesa_op_ctx *op_templ,
8558c2ecf20Sopenharmony_ci					bool skip_ctx,
8568c2ecf20Sopenharmony_ci					gfp_t flags);
8578c2ecf20Sopenharmony_ci
8588c2ecf20Sopenharmony_ciint mv_cesa_dma_add_data_transfer(struct mv_cesa_tdma_chain *chain,
8598c2ecf20Sopenharmony_ci				  dma_addr_t dst, dma_addr_t src, u32 size,
8608c2ecf20Sopenharmony_ci				  u32 flags, gfp_t gfp_flags);
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ciint mv_cesa_dma_add_dummy_launch(struct mv_cesa_tdma_chain *chain, gfp_t flags);
8638c2ecf20Sopenharmony_ciint mv_cesa_dma_add_dummy_end(struct mv_cesa_tdma_chain *chain, gfp_t flags);
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ciint mv_cesa_dma_add_op_transfers(struct mv_cesa_tdma_chain *chain,
8668c2ecf20Sopenharmony_ci				 struct mv_cesa_dma_iter *dma_iter,
8678c2ecf20Sopenharmony_ci				 struct mv_cesa_sg_dma_iter *sgiter,
8688c2ecf20Sopenharmony_ci				 gfp_t gfp_flags);
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci/* Algorithm definitions */
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_ciextern struct ahash_alg mv_md5_alg;
8738c2ecf20Sopenharmony_ciextern struct ahash_alg mv_sha1_alg;
8748c2ecf20Sopenharmony_ciextern struct ahash_alg mv_sha256_alg;
8758c2ecf20Sopenharmony_ciextern struct ahash_alg mv_ahmac_md5_alg;
8768c2ecf20Sopenharmony_ciextern struct ahash_alg mv_ahmac_sha1_alg;
8778c2ecf20Sopenharmony_ciextern struct ahash_alg mv_ahmac_sha256_alg;
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ciextern struct skcipher_alg mv_cesa_ecb_des_alg;
8808c2ecf20Sopenharmony_ciextern struct skcipher_alg mv_cesa_cbc_des_alg;
8818c2ecf20Sopenharmony_ciextern struct skcipher_alg mv_cesa_ecb_des3_ede_alg;
8828c2ecf20Sopenharmony_ciextern struct skcipher_alg mv_cesa_cbc_des3_ede_alg;
8838c2ecf20Sopenharmony_ciextern struct skcipher_alg mv_cesa_ecb_aes_alg;
8848c2ecf20Sopenharmony_ciextern struct skcipher_alg mv_cesa_cbc_aes_alg;
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci#endif /* __MARVELL_CESA_H__ */
887