1 /* SPDX-License-Identifier: GPL-2.0
2  *
3  * Copyright (C) 2019 - 2021
4  *
5  * Richard van Schagen <vschagen@icloud.com>
6  * Christian Marangi <ansuelsmth@gmail.com
7  */
8 #ifndef _EIP93_MAIN_H_
9 #define _EIP93_MAIN_H_
10 
11 #include <crypto/internal/aead.h>
12 #include <crypto/internal/hash.h>
13 #include <crypto/internal/skcipher.h>
14 #include <linux/bitfield.h>
15 #include <linux/interrupt.h>
16 
17 #define EIP93_RING_BUSY_DELAY		500
18 
19 #define EIP93_RING_NUM			512
20 #define EIP93_RING_BUSY			32
21 #define EIP93_CRA_PRIORITY		1500
22 
23 #define EIP93_RING_SA_STATE_ADDR(base, idx)	((base) + (idx))
24 #define EIP93_RING_SA_STATE_DMA(dma_base, idx)	((u32 __force)(dma_base) + \
25 						 ((idx) * sizeof(struct sa_state)))
26 
27 /* cipher algorithms */
28 #define EIP93_ALG_DES			BIT(0)
29 #define EIP93_ALG_3DES			BIT(1)
30 #define EIP93_ALG_AES			BIT(2)
31 #define EIP93_ALG_MASK			GENMASK(2, 0)
32 /* hash and hmac algorithms */
33 #define EIP93_HASH_MD5			BIT(3)
34 #define EIP93_HASH_SHA1			BIT(4)
35 #define EIP93_HASH_SHA224			BIT(5)
36 #define EIP93_HASH_SHA256			BIT(6)
37 #define EIP93_HASH_HMAC			BIT(7)
38 #define EIP93_HASH_MASK			GENMASK(6, 3)
39 /* cipher modes */
40 #define EIP93_MODE_CBC			BIT(8)
41 #define EIP93_MODE_ECB			BIT(9)
42 #define EIP93_MODE_CTR			BIT(10)
43 #define EIP93_MODE_RFC3686		BIT(11)
44 #define EIP93_MODE_MASK			GENMASK(10, 8)
45 
46 /* cipher encryption/decryption operations */
47 #define EIP93_ENCRYPT			BIT(12)
48 #define EIP93_DECRYPT			BIT(13)
49 
50 #define EIP93_BUSY			BIT(14)
51 
52 /* descriptor flags */
53 #define EIP93_DESC_DMA_IV			BIT(0)
54 #define EIP93_DESC_IPSEC			BIT(1)
55 #define EIP93_DESC_FINISH			BIT(2)
56 #define EIP93_DESC_LAST			BIT(3)
57 #define EIP93_DESC_FAKE_HMAC		BIT(4)
58 #define EIP93_DESC_PRNG			BIT(5)
59 #define EIP93_DESC_HASH			BIT(6)
60 #define EIP93_DESC_AEAD			BIT(7)
61 #define EIP93_DESC_SKCIPHER		BIT(8)
62 #define EIP93_DESC_ASYNC			BIT(9)
63 
64 #define IS_DMA_IV(desc_flags)		((desc_flags) & EIP93_DESC_DMA_IV)
65 
66 #define IS_DES(flags)			((flags) & EIP93_ALG_DES)
67 #define IS_3DES(flags)			((flags) & EIP93_ALG_3DES)
68 #define IS_AES(flags)			((flags) & EIP93_ALG_AES)
69 
70 #define IS_HASH_MD5(flags)		((flags) & EIP93_HASH_MD5)
71 #define IS_HASH_SHA1(flags)		((flags) & EIP93_HASH_SHA1)
72 #define IS_HASH_SHA224(flags)		((flags) & EIP93_HASH_SHA224)
73 #define IS_HASH_SHA256(flags)		((flags) & EIP93_HASH_SHA256)
74 #define IS_HMAC(flags)			((flags) & EIP93_HASH_HMAC)
75 
76 #define IS_CBC(mode)			((mode) & EIP93_MODE_CBC)
77 #define IS_ECB(mode)			((mode) & EIP93_MODE_ECB)
78 #define IS_CTR(mode)			((mode) & EIP93_MODE_CTR)
79 #define IS_RFC3686(mode)		((mode) & EIP93_MODE_RFC3686)
80 
81 #define IS_BUSY(flags)			((flags) & EIP93_BUSY)
82 
83 #define IS_ENCRYPT(dir)			((dir) & EIP93_ENCRYPT)
84 #define IS_DECRYPT(dir)			((dir) & EIP93_DECRYPT)
85 
86 #define IS_CIPHER(flags)		((flags) & (EIP93_ALG_DES | \
87 						    EIP93_ALG_3DES |  \
88 						    EIP93_ALG_AES))
89 
90 #define IS_HASH(flags)			((flags) & (EIP93_HASH_MD5 |  \
91 						    EIP93_HASH_SHA1 |   \
92 						    EIP93_HASH_SHA224 | \
93 						    EIP93_HASH_SHA256))
94 
95 /**
96  * struct eip93_device - crypto engine device structure
97  */
98 struct eip93_device {
99 	void __iomem		*base;
100 	struct device		*dev;
101 	struct clk		*clk;
102 	int			irq;
103 	struct eip93_ring		*ring;
104 };
105 
106 struct eip93_desc_ring {
107 	void			*base;
108 	void			*base_end;
109 	dma_addr_t		base_dma;
110 	/* write and read pointers */
111 	void			*read;
112 	void			*write;
113 	/* descriptor element offset */
114 	u32			offset;
115 };
116 
117 struct eip93_state_pool {
118 	void			*base;
119 	dma_addr_t		base_dma;
120 };
121 
122 struct eip93_ring {
123 	struct tasklet_struct		done_task;
124 	/* command/result rings */
125 	struct eip93_desc_ring		cdr;
126 	struct eip93_desc_ring		rdr;
127 	spinlock_t			write_lock;
128 	spinlock_t			read_lock;
129 	/* aync idr */
130 	spinlock_t			idr_lock;
131 	struct idr			crypto_async_idr;
132 };
133 
134 enum eip93_alg_type {
135 	EIP93_ALG_TYPE_AEAD,
136 	EIP93_ALG_TYPE_SKCIPHER,
137 	EIP93_ALG_TYPE_HASH,
138 };
139 
140 struct eip93_alg_template {
141 	struct eip93_device	*eip93;
142 	enum eip93_alg_type	type;
143 	u32			flags;
144 	union {
145 		struct aead_alg		aead;
146 		struct skcipher_alg	skcipher;
147 		struct ahash_alg	ahash;
148 	} alg;
149 };
150 
151 #endif /* _EIP93_MAIN_H_ */
152