18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/* Copyright (c) 2016-2017 Hisilicon Limited. */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#ifndef _SEC_DRV_H_
58c2ecf20Sopenharmony_ci#define _SEC_DRV_H_
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <crypto/algapi.h>
88c2ecf20Sopenharmony_ci#include <linux/kfifo.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#define SEC_MAX_SGE_NUM			64
118c2ecf20Sopenharmony_ci#define SEC_HW_RING_NUM			3
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#define SEC_CMD_RING			0
148c2ecf20Sopenharmony_ci#define SEC_OUTORDER_RING		1
158c2ecf20Sopenharmony_ci#define SEC_DBG_RING			2
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/* A reasonable length to balance memory use against flexibility */
188c2ecf20Sopenharmony_ci#define SEC_QUEUE_LEN			512
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define SEC_MAX_SGE_NUM   64
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistruct sec_bd_info {
238c2ecf20Sopenharmony_ci#define SEC_BD_W0_T_LEN_M			GENMASK(4, 0)
248c2ecf20Sopenharmony_ci#define SEC_BD_W0_T_LEN_S			0
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define SEC_BD_W0_C_WIDTH_M			GENMASK(6, 5)
278c2ecf20Sopenharmony_ci#define SEC_BD_W0_C_WIDTH_S			5
288c2ecf20Sopenharmony_ci#define   SEC_C_WIDTH_AES_128BIT		0
298c2ecf20Sopenharmony_ci#define   SEC_C_WIDTH_AES_8BIT		1
308c2ecf20Sopenharmony_ci#define   SEC_C_WIDTH_AES_1BIT		2
318c2ecf20Sopenharmony_ci#define   SEC_C_WIDTH_DES_64BIT		0
328c2ecf20Sopenharmony_ci#define   SEC_C_WIDTH_DES_8BIT		1
338c2ecf20Sopenharmony_ci#define   SEC_C_WIDTH_DES_1BIT		2
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define SEC_BD_W0_C_MODE_M			GENMASK(9, 7)
368c2ecf20Sopenharmony_ci#define SEC_BD_W0_C_MODE_S			7
378c2ecf20Sopenharmony_ci#define   SEC_C_MODE_ECB			0
388c2ecf20Sopenharmony_ci#define   SEC_C_MODE_CBC			1
398c2ecf20Sopenharmony_ci#define   SEC_C_MODE_CTR			4
408c2ecf20Sopenharmony_ci#define   SEC_C_MODE_CCM			5
418c2ecf20Sopenharmony_ci#define   SEC_C_MODE_GCM			6
428c2ecf20Sopenharmony_ci#define   SEC_C_MODE_XTS			7
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci#define SEC_BD_W0_SEQ				BIT(10)
458c2ecf20Sopenharmony_ci#define SEC_BD_W0_DE				BIT(11)
468c2ecf20Sopenharmony_ci#define SEC_BD_W0_DAT_SKIP_M			GENMASK(13, 12)
478c2ecf20Sopenharmony_ci#define SEC_BD_W0_DAT_SKIP_S			12
488c2ecf20Sopenharmony_ci#define SEC_BD_W0_C_GRAN_SIZE_19_16_M		GENMASK(17, 14)
498c2ecf20Sopenharmony_ci#define SEC_BD_W0_C_GRAN_SIZE_19_16_S		14
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define SEC_BD_W0_CIPHER_M			GENMASK(19, 18)
528c2ecf20Sopenharmony_ci#define SEC_BD_W0_CIPHER_S			18
538c2ecf20Sopenharmony_ci#define   SEC_CIPHER_NULL			0
548c2ecf20Sopenharmony_ci#define   SEC_CIPHER_ENCRYPT			1
558c2ecf20Sopenharmony_ci#define   SEC_CIPHER_DECRYPT			2
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#define SEC_BD_W0_AUTH_M			GENMASK(21, 20)
588c2ecf20Sopenharmony_ci#define SEC_BD_W0_AUTH_S			20
598c2ecf20Sopenharmony_ci#define   SEC_AUTH_NULL				0
608c2ecf20Sopenharmony_ci#define   SEC_AUTH_MAC				1
618c2ecf20Sopenharmony_ci#define   SEC_AUTH_VERIF			2
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#define SEC_BD_W0_AI_GEN			BIT(22)
648c2ecf20Sopenharmony_ci#define SEC_BD_W0_CI_GEN			BIT(23)
658c2ecf20Sopenharmony_ci#define SEC_BD_W0_NO_HPAD			BIT(24)
668c2ecf20Sopenharmony_ci#define SEC_BD_W0_HM_M				GENMASK(26, 25)
678c2ecf20Sopenharmony_ci#define SEC_BD_W0_HM_S				25
688c2ecf20Sopenharmony_ci#define SEC_BD_W0_ICV_OR_SKEY_EN_M		GENMASK(28, 27)
698c2ecf20Sopenharmony_ci#define SEC_BD_W0_ICV_OR_SKEY_EN_S		27
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/* Multi purpose field - gran size bits for send, flag for recv */
728c2ecf20Sopenharmony_ci#define SEC_BD_W0_FLAG_M			GENMASK(30, 29)
738c2ecf20Sopenharmony_ci#define SEC_BD_W0_C_GRAN_SIZE_21_20_M		GENMASK(30, 29)
748c2ecf20Sopenharmony_ci#define SEC_BD_W0_FLAG_S			29
758c2ecf20Sopenharmony_ci#define SEC_BD_W0_C_GRAN_SIZE_21_20_S		29
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define SEC_BD_W0_DONE				BIT(31)
788c2ecf20Sopenharmony_ci	u32 w0;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#define SEC_BD_W1_AUTH_GRAN_SIZE_M		GENMASK(21, 0)
818c2ecf20Sopenharmony_ci#define SEC_BD_W1_AUTH_GRAN_SIZE_S		0
828c2ecf20Sopenharmony_ci#define SEC_BD_W1_M_KEY_EN			BIT(22)
838c2ecf20Sopenharmony_ci#define SEC_BD_W1_BD_INVALID			BIT(23)
848c2ecf20Sopenharmony_ci#define SEC_BD_W1_ADDR_TYPE			BIT(24)
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci#define SEC_BD_W1_A_ALG_M			GENMASK(28, 25)
878c2ecf20Sopenharmony_ci#define SEC_BD_W1_A_ALG_S			25
888c2ecf20Sopenharmony_ci#define   SEC_A_ALG_SHA1			0
898c2ecf20Sopenharmony_ci#define   SEC_A_ALG_SHA256			1
908c2ecf20Sopenharmony_ci#define   SEC_A_ALG_MD5				2
918c2ecf20Sopenharmony_ci#define   SEC_A_ALG_SHA224			3
928c2ecf20Sopenharmony_ci#define   SEC_A_ALG_HMAC_SHA1			8
938c2ecf20Sopenharmony_ci#define   SEC_A_ALG_HMAC_SHA224			10
948c2ecf20Sopenharmony_ci#define   SEC_A_ALG_HMAC_SHA256			11
958c2ecf20Sopenharmony_ci#define   SEC_A_ALG_HMAC_MD5			12
968c2ecf20Sopenharmony_ci#define   SEC_A_ALG_AES_XCBC			13
978c2ecf20Sopenharmony_ci#define   SEC_A_ALG_AES_CMAC			14
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#define SEC_BD_W1_C_ALG_M			GENMASK(31, 29)
1008c2ecf20Sopenharmony_ci#define SEC_BD_W1_C_ALG_S			29
1018c2ecf20Sopenharmony_ci#define   SEC_C_ALG_DES				0
1028c2ecf20Sopenharmony_ci#define   SEC_C_ALG_3DES			1
1038c2ecf20Sopenharmony_ci#define   SEC_C_ALG_AES				2
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	u32 w1;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci#define SEC_BD_W2_C_GRAN_SIZE_15_0_M		GENMASK(15, 0)
1088c2ecf20Sopenharmony_ci#define SEC_BD_W2_C_GRAN_SIZE_15_0_S		0
1098c2ecf20Sopenharmony_ci#define SEC_BD_W2_GRAN_NUM_M			GENMASK(31, 16)
1108c2ecf20Sopenharmony_ci#define SEC_BD_W2_GRAN_NUM_S			16
1118c2ecf20Sopenharmony_ci	u32 w2;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci#define SEC_BD_W3_AUTH_LEN_OFFSET_M		GENMASK(9, 0)
1148c2ecf20Sopenharmony_ci#define SEC_BD_W3_AUTH_LEN_OFFSET_S		0
1158c2ecf20Sopenharmony_ci#define SEC_BD_W3_CIPHER_LEN_OFFSET_M		GENMASK(19, 10)
1168c2ecf20Sopenharmony_ci#define SEC_BD_W3_CIPHER_LEN_OFFSET_S		10
1178c2ecf20Sopenharmony_ci#define SEC_BD_W3_MAC_LEN_M			GENMASK(24, 20)
1188c2ecf20Sopenharmony_ci#define SEC_BD_W3_MAC_LEN_S			20
1198c2ecf20Sopenharmony_ci#define SEC_BD_W3_A_KEY_LEN_M			GENMASK(29, 25)
1208c2ecf20Sopenharmony_ci#define SEC_BD_W3_A_KEY_LEN_S			25
1218c2ecf20Sopenharmony_ci#define SEC_BD_W3_C_KEY_LEN_M			GENMASK(31, 30)
1228c2ecf20Sopenharmony_ci#define SEC_BD_W3_C_KEY_LEN_S			30
1238c2ecf20Sopenharmony_ci#define   SEC_KEY_LEN_AES_128			0
1248c2ecf20Sopenharmony_ci#define   SEC_KEY_LEN_AES_192			1
1258c2ecf20Sopenharmony_ci#define   SEC_KEY_LEN_AES_256			2
1268c2ecf20Sopenharmony_ci#define   SEC_KEY_LEN_DES			1
1278c2ecf20Sopenharmony_ci#define   SEC_KEY_LEN_3DES_3_KEY		1
1288c2ecf20Sopenharmony_ci#define   SEC_KEY_LEN_3DES_2_KEY		3
1298c2ecf20Sopenharmony_ci	u32 w3;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	/* W4,5 */
1328c2ecf20Sopenharmony_ci	union {
1338c2ecf20Sopenharmony_ci		u32 authkey_addr_lo;
1348c2ecf20Sopenharmony_ci		u32 authiv_addr_lo;
1358c2ecf20Sopenharmony_ci	};
1368c2ecf20Sopenharmony_ci	union {
1378c2ecf20Sopenharmony_ci		u32 authkey_addr_hi;
1388c2ecf20Sopenharmony_ci		u32 authiv_addr_hi;
1398c2ecf20Sopenharmony_ci	};
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	/* W6,7 */
1428c2ecf20Sopenharmony_ci	u32 cipher_key_addr_lo;
1438c2ecf20Sopenharmony_ci	u32 cipher_key_addr_hi;
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/* W8,9 */
1468c2ecf20Sopenharmony_ci	u32 cipher_iv_addr_lo;
1478c2ecf20Sopenharmony_ci	u32 cipher_iv_addr_hi;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	/* W10,11 */
1508c2ecf20Sopenharmony_ci	u32 data_addr_lo;
1518c2ecf20Sopenharmony_ci	u32 data_addr_hi;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	/* W12,13 */
1548c2ecf20Sopenharmony_ci	u32 mac_addr_lo;
1558c2ecf20Sopenharmony_ci	u32 mac_addr_hi;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	/* W14,15 */
1588c2ecf20Sopenharmony_ci	u32 cipher_destin_addr_lo;
1598c2ecf20Sopenharmony_ci	u32 cipher_destin_addr_hi;
1608c2ecf20Sopenharmony_ci};
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cienum sec_mem_region {
1638c2ecf20Sopenharmony_ci	SEC_COMMON = 0,
1648c2ecf20Sopenharmony_ci	SEC_SAA,
1658c2ecf20Sopenharmony_ci	SEC_NUM_ADDR_REGIONS
1668c2ecf20Sopenharmony_ci};
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci#define SEC_NAME_SIZE				64
1698c2ecf20Sopenharmony_ci#define SEC_Q_NUM				16
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci/**
1738c2ecf20Sopenharmony_ci * struct sec_queue_ring_cmd - store information about a SEC HW cmd ring
1748c2ecf20Sopenharmony_ci * @used: Local counter used to cheaply establish if the ring is empty.
1758c2ecf20Sopenharmony_ci * @lock: Protect against simultaneous adjusting of the read and write pointers.
1768c2ecf20Sopenharmony_ci * @vaddr: Virtual address for the ram pages used for the ring.
1778c2ecf20Sopenharmony_ci * @paddr: Physical address of the dma mapped region of ram used for the ring.
1788c2ecf20Sopenharmony_ci * @callback: Callback function called on a ring element completing.
1798c2ecf20Sopenharmony_ci */
1808c2ecf20Sopenharmony_cistruct sec_queue_ring_cmd {
1818c2ecf20Sopenharmony_ci	atomic_t used;
1828c2ecf20Sopenharmony_ci	struct mutex lock;
1838c2ecf20Sopenharmony_ci	struct sec_bd_info *vaddr;
1848c2ecf20Sopenharmony_ci	dma_addr_t paddr;
1858c2ecf20Sopenharmony_ci	void (*callback)(struct sec_bd_info *resp, void *ctx);
1868c2ecf20Sopenharmony_ci};
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_cistruct sec_debug_bd_info;
1898c2ecf20Sopenharmony_cistruct sec_queue_ring_db {
1908c2ecf20Sopenharmony_ci	struct sec_debug_bd_info *vaddr;
1918c2ecf20Sopenharmony_ci	dma_addr_t paddr;
1928c2ecf20Sopenharmony_ci};
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistruct sec_out_bd_info;
1958c2ecf20Sopenharmony_cistruct sec_queue_ring_cq {
1968c2ecf20Sopenharmony_ci	struct sec_out_bd_info *vaddr;
1978c2ecf20Sopenharmony_ci	dma_addr_t paddr;
1988c2ecf20Sopenharmony_ci};
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistruct sec_dev_info;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cienum sec_cipher_alg {
2038c2ecf20Sopenharmony_ci	SEC_C_DES_ECB_64,
2048c2ecf20Sopenharmony_ci	SEC_C_DES_CBC_64,
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	SEC_C_3DES_ECB_192_3KEY,
2078c2ecf20Sopenharmony_ci	SEC_C_3DES_ECB_192_2KEY,
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	SEC_C_3DES_CBC_192_3KEY,
2108c2ecf20Sopenharmony_ci	SEC_C_3DES_CBC_192_2KEY,
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	SEC_C_AES_ECB_128,
2138c2ecf20Sopenharmony_ci	SEC_C_AES_ECB_192,
2148c2ecf20Sopenharmony_ci	SEC_C_AES_ECB_256,
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	SEC_C_AES_CBC_128,
2178c2ecf20Sopenharmony_ci	SEC_C_AES_CBC_192,
2188c2ecf20Sopenharmony_ci	SEC_C_AES_CBC_256,
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	SEC_C_AES_CTR_128,
2218c2ecf20Sopenharmony_ci	SEC_C_AES_CTR_192,
2228c2ecf20Sopenharmony_ci	SEC_C_AES_CTR_256,
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	SEC_C_AES_XTS_128,
2258c2ecf20Sopenharmony_ci	SEC_C_AES_XTS_256,
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	SEC_C_NULL,
2288c2ecf20Sopenharmony_ci};
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci/**
2318c2ecf20Sopenharmony_ci * struct sec_alg_tfm_ctx - hardware specific tranformation context
2328c2ecf20Sopenharmony_ci * @cipher_alg: Cipher algorithm enabled include encryption mode.
2338c2ecf20Sopenharmony_ci * @key: Key storage if required.
2348c2ecf20Sopenharmony_ci * @pkey: DMA address for the key storage.
2358c2ecf20Sopenharmony_ci * @req_template: Request template to save time on setup.
2368c2ecf20Sopenharmony_ci * @queue: The hardware queue associated with this tfm context.
2378c2ecf20Sopenharmony_ci * @lock: Protect key and pkey to ensure they are consistent
2388c2ecf20Sopenharmony_ci * @auth_buf: Current context buffer for auth operations.
2398c2ecf20Sopenharmony_ci * @backlog: The backlog queue used for cases where our buffers aren't
2408c2ecf20Sopenharmony_ci * large enough.
2418c2ecf20Sopenharmony_ci */
2428c2ecf20Sopenharmony_cistruct sec_alg_tfm_ctx {
2438c2ecf20Sopenharmony_ci	enum sec_cipher_alg cipher_alg;
2448c2ecf20Sopenharmony_ci	u8 *key;
2458c2ecf20Sopenharmony_ci	dma_addr_t pkey;
2468c2ecf20Sopenharmony_ci	struct sec_bd_info req_template;
2478c2ecf20Sopenharmony_ci	struct sec_queue *queue;
2488c2ecf20Sopenharmony_ci	struct mutex lock;
2498c2ecf20Sopenharmony_ci	u8 *auth_buf;
2508c2ecf20Sopenharmony_ci	struct list_head backlog;
2518c2ecf20Sopenharmony_ci};
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci/**
2548c2ecf20Sopenharmony_ci * struct sec_request - data associate with a single crypto request
2558c2ecf20Sopenharmony_ci * @elements: List of subparts of this request (hardware size restriction)
2568c2ecf20Sopenharmony_ci * @num_elements: The number of subparts (used as an optimization)
2578c2ecf20Sopenharmony_ci * @lock: Protect elements of this structure against concurrent change.
2588c2ecf20Sopenharmony_ci * @tfm_ctx: hardware specific context.
2598c2ecf20Sopenharmony_ci * @len_in: length of in sgl from upper layers
2608c2ecf20Sopenharmony_ci * @len_out: length of out sgl from upper layers
2618c2ecf20Sopenharmony_ci * @dma_iv: initialization vector - phsyical address
2628c2ecf20Sopenharmony_ci * @err: store used to track errors across subelements of this request.
2638c2ecf20Sopenharmony_ci * @req_base: pointer to base element of associate crypto context.
2648c2ecf20Sopenharmony_ci * This is needed to allow shared handling skcipher, ahash etc.
2658c2ecf20Sopenharmony_ci * @cb: completion callback.
2668c2ecf20Sopenharmony_ci * @backlog_head: list head to allow backlog maintenance.
2678c2ecf20Sopenharmony_ci *
2688c2ecf20Sopenharmony_ci * The hardware is limited in the maximum size of data that it can
2698c2ecf20Sopenharmony_ci * process from a single BD.  Typically this is fairly large (32MB)
2708c2ecf20Sopenharmony_ci * but still requires the complexity of splitting the incoming
2718c2ecf20Sopenharmony_ci * skreq up into a number of elements complete with appropriate
2728c2ecf20Sopenharmony_ci * iv chaining.
2738c2ecf20Sopenharmony_ci */
2748c2ecf20Sopenharmony_cistruct sec_request {
2758c2ecf20Sopenharmony_ci	struct list_head elements;
2768c2ecf20Sopenharmony_ci	int num_elements;
2778c2ecf20Sopenharmony_ci	struct mutex lock;
2788c2ecf20Sopenharmony_ci	struct sec_alg_tfm_ctx *tfm_ctx;
2798c2ecf20Sopenharmony_ci	int len_in;
2808c2ecf20Sopenharmony_ci	int len_out;
2818c2ecf20Sopenharmony_ci	dma_addr_t dma_iv;
2828c2ecf20Sopenharmony_ci	int err;
2838c2ecf20Sopenharmony_ci	struct crypto_async_request *req_base;
2848c2ecf20Sopenharmony_ci	void (*cb)(struct sec_bd_info *resp, struct crypto_async_request *req);
2858c2ecf20Sopenharmony_ci	struct list_head backlog_head;
2868c2ecf20Sopenharmony_ci};
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci/**
2898c2ecf20Sopenharmony_ci * struct sec_request_el - A subpart of a request.
2908c2ecf20Sopenharmony_ci * @head: allow us to attach this to the list in the sec_request
2918c2ecf20Sopenharmony_ci * @req: hardware block descriptor corresponding to this request subpart
2928c2ecf20Sopenharmony_ci * @in: hardware sgl for input - virtual address
2938c2ecf20Sopenharmony_ci * @dma_in: hardware sgl for input - physical address
2948c2ecf20Sopenharmony_ci * @sgl_in: scatterlist for this request subpart
2958c2ecf20Sopenharmony_ci * @out: hardware sgl for output - virtual address
2968c2ecf20Sopenharmony_ci * @dma_out: hardware sgl for output - physical address
2978c2ecf20Sopenharmony_ci * @sgl_out: scatterlist for this request subpart
2988c2ecf20Sopenharmony_ci * @sec_req: The request which this subpart forms a part of
2998c2ecf20Sopenharmony_ci * @el_length: Number of bytes in this subpart. Needed to locate
3008c2ecf20Sopenharmony_ci * last ivsize chunk for iv chaining.
3018c2ecf20Sopenharmony_ci */
3028c2ecf20Sopenharmony_cistruct sec_request_el {
3038c2ecf20Sopenharmony_ci	struct list_head head;
3048c2ecf20Sopenharmony_ci	struct sec_bd_info req;
3058c2ecf20Sopenharmony_ci	struct sec_hw_sgl *in;
3068c2ecf20Sopenharmony_ci	dma_addr_t dma_in;
3078c2ecf20Sopenharmony_ci	struct scatterlist *sgl_in;
3088c2ecf20Sopenharmony_ci	struct sec_hw_sgl *out;
3098c2ecf20Sopenharmony_ci	dma_addr_t dma_out;
3108c2ecf20Sopenharmony_ci	struct scatterlist *sgl_out;
3118c2ecf20Sopenharmony_ci	struct sec_request *sec_req;
3128c2ecf20Sopenharmony_ci	size_t el_length;
3138c2ecf20Sopenharmony_ci};
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci/**
3168c2ecf20Sopenharmony_ci * struct sec_queue - All the information about a HW queue
3178c2ecf20Sopenharmony_ci * @dev_info: The parent SEC device to which this queue belongs.
3188c2ecf20Sopenharmony_ci * @task_irq: Completion interrupt for the queue.
3198c2ecf20Sopenharmony_ci * @name: Human readable queue description also used as irq name.
3208c2ecf20Sopenharmony_ci * @ring: The several HW rings associated with one queue.
3218c2ecf20Sopenharmony_ci * @regs: The iomapped device registers
3228c2ecf20Sopenharmony_ci * @queue_id: Index of the queue used for naming and resource selection.
3238c2ecf20Sopenharmony_ci * @in_use: Flag to say if the queue is in use.
3248c2ecf20Sopenharmony_ci * @expected: The next expected element to finish assuming we were in order.
3258c2ecf20Sopenharmony_ci * @uprocessed: A bitmap to track which OoO elements are done but not handled.
3268c2ecf20Sopenharmony_ci * @softqueue: A software queue used when chaining requirements prevent direct
3278c2ecf20Sopenharmony_ci *   use of the hardware queues.
3288c2ecf20Sopenharmony_ci * @havesoftqueue: A flag to say we have a queues - as we may need one for the
3298c2ecf20Sopenharmony_ci *   current mode.
3308c2ecf20Sopenharmony_ci * @queuelock: Protect the soft queue from concurrent changes to avoid some
3318c2ecf20Sopenharmony_ci *   potential loss of data races.
3328c2ecf20Sopenharmony_ci * @shadow: Pointers back to the shadow copy of the hardware ring element
3338c2ecf20Sopenharmony_ci *   need because we can't store any context reference in the bd element.
3348c2ecf20Sopenharmony_ci */
3358c2ecf20Sopenharmony_cistruct sec_queue {
3368c2ecf20Sopenharmony_ci	struct sec_dev_info *dev_info;
3378c2ecf20Sopenharmony_ci	int task_irq;
3388c2ecf20Sopenharmony_ci	char name[SEC_NAME_SIZE];
3398c2ecf20Sopenharmony_ci	struct sec_queue_ring_cmd ring_cmd;
3408c2ecf20Sopenharmony_ci	struct sec_queue_ring_cq ring_cq;
3418c2ecf20Sopenharmony_ci	struct sec_queue_ring_db ring_db;
3428c2ecf20Sopenharmony_ci	void __iomem *regs;
3438c2ecf20Sopenharmony_ci	u32 queue_id;
3448c2ecf20Sopenharmony_ci	bool in_use;
3458c2ecf20Sopenharmony_ci	int expected;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	DECLARE_BITMAP(unprocessed, SEC_QUEUE_LEN);
3488c2ecf20Sopenharmony_ci	DECLARE_KFIFO_PTR(softqueue, typeof(struct sec_request_el *));
3498c2ecf20Sopenharmony_ci	bool havesoftqueue;
3508c2ecf20Sopenharmony_ci	spinlock_t queuelock;
3518c2ecf20Sopenharmony_ci	void *shadow[SEC_QUEUE_LEN];
3528c2ecf20Sopenharmony_ci};
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci/**
3558c2ecf20Sopenharmony_ci * struct sec_hw_sge: Track each of the 64 element SEC HW SGL entries
3568c2ecf20Sopenharmony_ci * @buf: The IOV dma address for this entry.
3578c2ecf20Sopenharmony_ci * @len: Length of this IOV.
3588c2ecf20Sopenharmony_ci * @pad: Reserved space.
3598c2ecf20Sopenharmony_ci */
3608c2ecf20Sopenharmony_cistruct sec_hw_sge {
3618c2ecf20Sopenharmony_ci	dma_addr_t buf;
3628c2ecf20Sopenharmony_ci	unsigned int len;
3638c2ecf20Sopenharmony_ci	unsigned int pad;
3648c2ecf20Sopenharmony_ci};
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci/**
3678c2ecf20Sopenharmony_ci * struct sec_hw_sgl: One hardware SGL entry.
3688c2ecf20Sopenharmony_ci * @next_sgl: The next entry if we need to chain dma address. Null if last.
3698c2ecf20Sopenharmony_ci * @entry_sum_in_chain: The full count of SGEs - only matters for first SGL.
3708c2ecf20Sopenharmony_ci * @entry_sum_in_sgl: The number of SGEs in this SGL element.
3718c2ecf20Sopenharmony_ci * @flag: Unused in skciphers.
3728c2ecf20Sopenharmony_ci * @serial_num: Unsued in skciphers.
3738c2ecf20Sopenharmony_ci * @cpuid: Currently unused.
3748c2ecf20Sopenharmony_ci * @data_bytes_in_sgl: Count of bytes from all SGEs in this SGL.
3758c2ecf20Sopenharmony_ci * @next: Virtual address used to stash the next sgl - useful in completion.
3768c2ecf20Sopenharmony_ci * @reserved: A reserved field not currently used.
3778c2ecf20Sopenharmony_ci * @sge_entries: The (up to) 64 Scatter Gather Entries, representing IOVs.
3788c2ecf20Sopenharmony_ci * @node: Currently unused.
3798c2ecf20Sopenharmony_ci */
3808c2ecf20Sopenharmony_cistruct sec_hw_sgl {
3818c2ecf20Sopenharmony_ci	dma_addr_t next_sgl;
3828c2ecf20Sopenharmony_ci	u16 entry_sum_in_chain;
3838c2ecf20Sopenharmony_ci	u16 entry_sum_in_sgl;
3848c2ecf20Sopenharmony_ci	u32 flag;
3858c2ecf20Sopenharmony_ci	u64 serial_num;
3868c2ecf20Sopenharmony_ci	u32 cpuid;
3878c2ecf20Sopenharmony_ci	u32 data_bytes_in_sgl;
3888c2ecf20Sopenharmony_ci	struct sec_hw_sgl *next;
3898c2ecf20Sopenharmony_ci	u64 reserved;
3908c2ecf20Sopenharmony_ci	struct sec_hw_sge  sge_entries[SEC_MAX_SGE_NUM];
3918c2ecf20Sopenharmony_ci	u8 node[16];
3928c2ecf20Sopenharmony_ci};
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_cistruct dma_pool;
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci/**
3978c2ecf20Sopenharmony_ci * struct sec_dev_info: The full SEC unit comprising queues and processors.
3988c2ecf20Sopenharmony_ci * @sec_id: Index used to track which SEC this is when more than one is present.
3998c2ecf20Sopenharmony_ci * @num_saas: The number of backed processors enabled.
4008c2ecf20Sopenharmony_ci * @regs: iomapped register regions shared by whole SEC unit.
4018c2ecf20Sopenharmony_ci * @dev_lock: Protects concurrent queue allocation / freeing for the SEC.
4028c2ecf20Sopenharmony_ci * @queues: The 16 queues that this SEC instance provides.
4038c2ecf20Sopenharmony_ci * @dev: Device pointer.
4048c2ecf20Sopenharmony_ci * @hw_sgl_pool: DMA pool used to mimise mapping for the scatter gather lists.
4058c2ecf20Sopenharmony_ci */
4068c2ecf20Sopenharmony_cistruct sec_dev_info {
4078c2ecf20Sopenharmony_ci	int sec_id;
4088c2ecf20Sopenharmony_ci	int num_saas;
4098c2ecf20Sopenharmony_ci	void __iomem *regs[SEC_NUM_ADDR_REGIONS];
4108c2ecf20Sopenharmony_ci	struct mutex dev_lock;
4118c2ecf20Sopenharmony_ci	int queues_in_use;
4128c2ecf20Sopenharmony_ci	struct sec_queue queues[SEC_Q_NUM];
4138c2ecf20Sopenharmony_ci	struct device *dev;
4148c2ecf20Sopenharmony_ci	struct dma_pool *hw_sgl_pool;
4158c2ecf20Sopenharmony_ci};
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ciint sec_queue_send(struct sec_queue *queue, struct sec_bd_info *msg, void *ctx);
4188c2ecf20Sopenharmony_cibool sec_queue_can_enqueue(struct sec_queue *queue, int num);
4198c2ecf20Sopenharmony_ciint sec_queue_stop_release(struct sec_queue *queue);
4208c2ecf20Sopenharmony_cistruct sec_queue *sec_queue_alloc_start_safe(void);
4218c2ecf20Sopenharmony_cibool sec_queue_empty(struct sec_queue *queue);
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci/* Algorithm specific elements from sec_algs.c */
4248c2ecf20Sopenharmony_civoid sec_alg_callback(struct sec_bd_info *resp, void *ctx);
4258c2ecf20Sopenharmony_ciint sec_algs_register(void);
4268c2ecf20Sopenharmony_civoid sec_algs_unregister(void);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci#endif /* _SEC_DRV_H_ */
429