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