xref: /linux/drivers/crypto/amcc/crypto4xx_core.h (revision dba0bfded433d412506eb674d6af77cdca82e259)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * AMCC SoC PPC4xx Crypto Driver
4  *
5  * Copyright (c) 2008 Applied Micro Circuits Corporation.
6  * All rights reserved. James Hsiao <jhsiao@amcc.com>
7  *
8  * This is the header file for AMCC Crypto offload Linux device driver for
9  * use with Linux CryptoAPI.
10 
11  */
12 
13 #ifndef __CRYPTO4XX_CORE_H__
14 #define __CRYPTO4XX_CORE_H__
15 
16 #include <linux/ratelimit.h>
17 #include <linux/scatterlist.h>
18 #include <crypto/internal/aead.h>
19 #include <crypto/internal/skcipher.h>
20 #include "crypto4xx_reg_def.h"
21 #include "crypto4xx_sa.h"
22 
23 #define PPC460SX_SDR0_SRST                      0x201
24 #define PPC405EX_SDR0_SRST                      0x200
25 #define PPC460EX_SDR0_SRST                      0x201
26 #define PPC460EX_CE_RESET                       0x08000000
27 #define PPC460SX_CE_RESET                       0x20000000
28 #define PPC405EX_CE_RESET                       0x00000008
29 
30 #define CRYPTO4XX_CRYPTO_PRIORITY		300
31 #define PPC4XX_NUM_PD				256
32 #define PPC4XX_LAST_PD				(PPC4XX_NUM_PD - 1)
33 #define PPC4XX_NUM_GD				1024
34 #define PPC4XX_LAST_GD				(PPC4XX_NUM_GD - 1)
35 #define PPC4XX_NUM_SD				256
36 #define PPC4XX_LAST_SD				(PPC4XX_NUM_SD - 1)
37 #define PPC4XX_SD_BUFFER_SIZE			2048
38 
39 #define PD_ENTRY_BUSY				BIT(1)
40 #define PD_ENTRY_INUSE				BIT(0)
41 #define PD_ENTRY_FREE				0
42 #define ERING_WAS_FULL				0xffffffff
43 
44 struct crypto4xx_device;
45 
46 union shadow_sa_buf {
47 	struct dynamic_sa_ctl sa;
48 
49 	/* alloc 256 bytes which is enough for any kind of dynamic sa */
50 	u8 buf[256];
51 } __packed;
52 
53 struct pd_uinfo {
54 	struct crypto4xx_device *dev;
55 	u32   state;
56 	u32 first_gd;		/* first gather discriptor
57 				used by this packet */
58 	u32 num_gd;             /* number of gather discriptor
59 				used by this packet */
60 	u32 first_sd;		/* first scatter discriptor
61 				used by this packet */
62 	u32 num_sd;		/* number of scatter discriptors
63 				used by this packet */
64 	struct dynamic_sa_ctl *sa_va;	/* shadow sa */
65 	struct sa_state_record *sr_va;	/* state record for shadow sa */
66 	u32 sr_pa;
67 	struct scatterlist *dest_va;
68 	struct crypto_async_request *async_req; 	/* base crypto request
69 							for this packet */
70 };
71 
72 struct crypto4xx_device {
73 	struct crypto4xx_core_device *core_dev;
74 	void __iomem *ce_base;
75 	void __iomem *trng_base;
76 
77 	struct ce_pd *pdr;	/* base address of packet descriptor ring */
78 	dma_addr_t pdr_pa;	/* physical address of pdr_base_register */
79 	struct ce_gd *gdr;	/* gather descriptor ring */
80 	dma_addr_t gdr_pa;	/* physical address of gdr_base_register */
81 	struct ce_sd *sdr;	/* scatter descriptor ring */
82 	dma_addr_t sdr_pa;	/* physical address of sdr_base_register */
83 	void *scatter_buffer_va;
84 	dma_addr_t scatter_buffer_pa;
85 
86 	union shadow_sa_buf *shadow_sa_pool;
87 	dma_addr_t shadow_sa_pool_pa;
88 	struct sa_state_record *shadow_sr_pool;
89 	dma_addr_t shadow_sr_pool_pa;
90 	u32 pdr_tail;
91 	u32 pdr_head;
92 	u32 gdr_tail;
93 	u32 gdr_head;
94 	u32 sdr_tail;
95 	u32 sdr_head;
96 	struct pd_uinfo *pdr_uinfo;
97 	struct list_head alg_list;	/* List of algorithm supported
98 					by this device */
99 	struct ratelimit_state aead_ratelimit;
100 	bool is_revb;
101 };
102 
103 struct crypto4xx_core_device {
104 	struct device *device;
105 	struct platform_device *ofdev;
106 	struct crypto4xx_device *dev;
107 	struct hwrng *trng;
108 	u32 int_status;
109 	int irq;
110 	struct tasklet_struct tasklet;
111 	spinlock_t lock;
112 };
113 
114 struct crypto4xx_ctx {
115 	struct crypto4xx_device *dev;
116 	struct dynamic_sa_ctl *sa_in;
117 	struct dynamic_sa_ctl *sa_out;
118 	__le32 iv_nonce;
119 	u32 sa_len;
120 	union {
121 		struct crypto_sync_skcipher *cipher;
122 		struct crypto_aead *aead;
123 	} sw_cipher;
124 };
125 
126 struct crypto4xx_aead_reqctx {
127 	struct scatterlist dst[2];
128 };
129 
130 struct crypto4xx_alg_common {
131 	u32 type;
132 	union {
133 		struct skcipher_alg cipher;
134 		struct aead_alg aead;
135 	} u;
136 };
137 
138 struct crypto4xx_alg {
139 	struct list_head  entry;
140 	struct crypto4xx_alg_common alg;
141 	struct crypto4xx_device *dev;
142 };
143 
144 #if IS_ENABLED(CONFIG_CC_IS_GCC) && CONFIG_GCC_VERSION >= 120000
145 #define BUILD_PD_ACCESS __attribute__((access(read_only, 6, 7)))
146 #else
147 #define BUILD_PD_ACCESS
148 #endif
149 
150 int crypto4xx_alloc_sa(struct crypto4xx_ctx *ctx, u32 size);
151 void crypto4xx_free_sa(struct crypto4xx_ctx *ctx);
152 int crypto4xx_build_pd(struct crypto_async_request *req,
153 		       struct crypto4xx_ctx *ctx,
154 		       struct scatterlist *src,
155 		       struct scatterlist *dst,
156 		       const unsigned int datalen,
157 		       const void *iv, const u32 iv_len,
158 		       const struct dynamic_sa_ctl *sa,
159 		       const unsigned int sa_len,
160 		       const unsigned int assoclen,
161 		       struct scatterlist *dst_tmp) BUILD_PD_ACCESS;
162 int crypto4xx_setkey_aes_cbc(struct crypto_skcipher *cipher,
163 			     const u8 *key, unsigned int keylen);
164 int crypto4xx_setkey_aes_ctr(struct crypto_skcipher *cipher,
165 			     const u8 *key, unsigned int keylen);
166 int crypto4xx_setkey_aes_ecb(struct crypto_skcipher *cipher,
167 			     const u8 *key, unsigned int keylen);
168 int crypto4xx_setkey_rfc3686(struct crypto_skcipher *cipher,
169 			     const u8 *key, unsigned int keylen);
170 int crypto4xx_encrypt_ctr(struct skcipher_request *req);
171 int crypto4xx_decrypt_ctr(struct skcipher_request *req);
172 int crypto4xx_encrypt_iv_stream(struct skcipher_request *req);
173 int crypto4xx_decrypt_iv_stream(struct skcipher_request *req);
174 int crypto4xx_encrypt_iv_block(struct skcipher_request *req);
175 int crypto4xx_decrypt_iv_block(struct skcipher_request *req);
176 int crypto4xx_encrypt_noiv_block(struct skcipher_request *req);
177 int crypto4xx_decrypt_noiv_block(struct skcipher_request *req);
178 int crypto4xx_rfc3686_encrypt(struct skcipher_request *req);
179 int crypto4xx_rfc3686_decrypt(struct skcipher_request *req);
180 
181 /*
182  * Note: Only use this function to copy items that is word aligned.
183  */
184 static inline void crypto4xx_memcpy_swab32(u32 *dst, const void *buf,
185 					   size_t len)
186 {
187 	for (; len >= 4; buf += 4, len -= 4)
188 		*dst++ = __swab32p((u32 *) buf);
189 
190 	if (len) {
191 		const u8 *tmp = (u8 *)buf;
192 
193 		switch (len) {
194 		case 3:
195 			*dst = (tmp[2] << 16) |
196 			       (tmp[1] << 8) |
197 			       tmp[0];
198 			break;
199 		case 2:
200 			*dst = (tmp[1] << 8) |
201 			       tmp[0];
202 			break;
203 		case 1:
204 			*dst = tmp[0];
205 			break;
206 		default:
207 			break;
208 		}
209 	}
210 }
211 
212 static inline void crypto4xx_memcpy_from_le32(u32 *dst, const void *buf,
213 					      size_t len)
214 {
215 	crypto4xx_memcpy_swab32(dst, buf, len);
216 }
217 
218 static inline void crypto4xx_memcpy_to_le32(__le32 *dst, const void *buf,
219 					    size_t len)
220 {
221 	crypto4xx_memcpy_swab32((u32 *)dst, buf, len);
222 }
223 
224 int crypto4xx_setauthsize_aead(struct crypto_aead *ciper,
225 			       unsigned int authsize);
226 int crypto4xx_setkey_aes_ccm(struct crypto_aead *cipher,
227 			     const u8 *key, unsigned int keylen);
228 int crypto4xx_encrypt_aes_ccm(struct aead_request *req);
229 int crypto4xx_decrypt_aes_ccm(struct aead_request *req);
230 int crypto4xx_setkey_aes_gcm(struct crypto_aead *cipher,
231 			     const u8 *key, unsigned int keylen);
232 int crypto4xx_encrypt_aes_gcm(struct aead_request *req);
233 int crypto4xx_decrypt_aes_gcm(struct aead_request *req);
234 
235 #endif
236