2 * AMCC SoC PPC4xx Crypto Driver
4 * Copyright (c) 2008 Applied Micro Circuits Corporation.
5 * All rights reserved. James Hsiao <jhsiao@amcc.com>
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.
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.
17 * This is the header file for AMCC Crypto offload Linux device driver for
18 * use with Linux CryptoAPI.
22 #ifndef __CRYPTO4XX_CORE_H__
23 #define __CRYPTO4XX_CORE_H__
25 #include <linux/ratelimit.h>
26 #include <linux/mutex.h>
27 #include <crypto/internal/hash.h>
28 #include <crypto/internal/aead.h>
29 #include <crypto/internal/rng.h>
30 #include <crypto/internal/skcipher.h>
31 #include "crypto4xx_reg_def.h"
32 #include "crypto4xx_sa.h"
34 #define PPC460SX_SDR0_SRST 0x201
35 #define PPC405EX_SDR0_SRST 0x200
36 #define PPC460EX_SDR0_SRST 0x201
37 #define PPC460EX_CE_RESET 0x08000000
38 #define PPC460SX_CE_RESET 0x20000000
39 #define PPC405EX_CE_RESET 0x00000008
41 #define CRYPTO4XX_CRYPTO_PRIORITY 300
42 #define PPC4XX_NUM_PD 256
43 #define PPC4XX_LAST_PD (PPC4XX_NUM_PD - 1)
44 #define PPC4XX_NUM_GD 1024
45 #define PPC4XX_LAST_GD (PPC4XX_NUM_GD - 1)
46 #define PPC4XX_NUM_SD 256
47 #define PPC4XX_LAST_SD (PPC4XX_NUM_SD - 1)
48 #define PPC4XX_SD_BUFFER_SIZE 2048
50 #define PD_ENTRY_BUSY BIT(1)
51 #define PD_ENTRY_INUSE BIT(0)
52 #define PD_ENTRY_FREE 0
53 #define ERING_WAS_FULL 0xffffffff
55 struct crypto4xx_device;
58 struct dynamic_sa_ctl sa;
60 /* alloc 256 bytes which is enough for any kind of dynamic sa */
65 struct crypto4xx_device *dev;
67 u32 first_gd; /* first gather discriptor
68 used by this packet */
69 u32 num_gd; /* number of gather discriptor
70 used by this packet */
71 u32 first_sd; /* first scatter discriptor
72 used by this packet */
73 u32 num_sd; /* number of scatter discriptors
74 used by this packet */
75 struct dynamic_sa_ctl *sa_va; /* shadow sa */
76 struct sa_state_record *sr_va; /* state record for shadow sa */
78 struct scatterlist *dest_va;
79 struct crypto_async_request *async_req; /* base crypto request
83 struct crypto4xx_device {
84 struct crypto4xx_core_device *core_dev;
85 void __iomem *ce_base;
86 void __iomem *trng_base;
88 struct ce_pd *pdr; /* base address of packet descriptor ring */
89 dma_addr_t pdr_pa; /* physical address of pdr_base_register */
90 struct ce_gd *gdr; /* gather descriptor ring */
91 dma_addr_t gdr_pa; /* physical address of gdr_base_register */
92 struct ce_sd *sdr; /* scatter descriptor ring */
93 dma_addr_t sdr_pa; /* physical address of sdr_base_register */
94 void *scatter_buffer_va;
95 dma_addr_t scatter_buffer_pa;
97 union shadow_sa_buf *shadow_sa_pool;
98 dma_addr_t shadow_sa_pool_pa;
99 struct sa_state_record *shadow_sr_pool;
100 dma_addr_t shadow_sr_pool_pa;
107 struct pd_uinfo *pdr_uinfo;
108 struct list_head alg_list; /* List of algorithm supported
110 struct ratelimit_state aead_ratelimit;
114 struct crypto4xx_core_device {
115 struct device *device;
116 struct platform_device *ofdev;
117 struct crypto4xx_device *dev;
121 struct tasklet_struct tasklet;
123 struct mutex rng_lock;
126 struct crypto4xx_ctx {
127 struct crypto4xx_device *dev;
128 struct dynamic_sa_ctl *sa_in;
129 struct dynamic_sa_ctl *sa_out;
133 struct crypto_sync_skcipher *cipher;
134 struct crypto_aead *aead;
138 struct crypto4xx_aead_reqctx {
139 struct scatterlist dst[2];
142 struct crypto4xx_alg_common {
145 struct skcipher_alg cipher;
146 struct ahash_alg hash;
147 struct aead_alg aead;
152 struct crypto4xx_alg {
153 struct list_head entry;
154 struct crypto4xx_alg_common alg;
155 struct crypto4xx_device *dev;
158 int crypto4xx_alloc_sa(struct crypto4xx_ctx *ctx, u32 size);
159 void crypto4xx_free_sa(struct crypto4xx_ctx *ctx);
160 void crypto4xx_free_ctx(struct crypto4xx_ctx *ctx);
161 int crypto4xx_build_pd(struct crypto_async_request *req,
162 struct crypto4xx_ctx *ctx,
163 struct scatterlist *src,
164 struct scatterlist *dst,
165 const unsigned int datalen,
166 const __le32 *iv, const u32 iv_len,
167 const struct dynamic_sa_ctl *sa,
168 const unsigned int sa_len,
169 const unsigned int assoclen,
170 struct scatterlist *dst_tmp);
171 int crypto4xx_setkey_aes_cbc(struct crypto_skcipher *cipher,
172 const u8 *key, unsigned int keylen);
173 int crypto4xx_setkey_aes_cfb(struct crypto_skcipher *cipher,
174 const u8 *key, unsigned int keylen);
175 int crypto4xx_setkey_aes_ctr(struct crypto_skcipher *cipher,
176 const u8 *key, unsigned int keylen);
177 int crypto4xx_setkey_aes_ecb(struct crypto_skcipher *cipher,
178 const u8 *key, unsigned int keylen);
179 int crypto4xx_setkey_aes_ofb(struct crypto_skcipher *cipher,
180 const u8 *key, unsigned int keylen);
181 int crypto4xx_setkey_rfc3686(struct crypto_skcipher *cipher,
182 const u8 *key, unsigned int keylen);
183 int crypto4xx_encrypt_ctr(struct skcipher_request *req);
184 int crypto4xx_decrypt_ctr(struct skcipher_request *req);
185 int crypto4xx_encrypt_iv(struct skcipher_request *req);
186 int crypto4xx_decrypt_iv(struct skcipher_request *req);
187 int crypto4xx_encrypt_noiv(struct skcipher_request *req);
188 int crypto4xx_decrypt_noiv(struct skcipher_request *req);
189 int crypto4xx_rfc3686_encrypt(struct skcipher_request *req);
190 int crypto4xx_rfc3686_decrypt(struct skcipher_request *req);
191 int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm);
192 int crypto4xx_hash_digest(struct ahash_request *req);
193 int crypto4xx_hash_final(struct ahash_request *req);
194 int crypto4xx_hash_update(struct ahash_request *req);
195 int crypto4xx_hash_init(struct ahash_request *req);
198 * Note: Only use this function to copy items that is word aligned.
200 static inline void crypto4xx_memcpy_swab32(u32 *dst, const void *buf,
203 for (; len >= 4; buf += 4, len -= 4)
204 *dst++ = __swab32p((u32 *) buf);
207 const u8 *tmp = (u8 *)buf;
211 *dst = (tmp[2] << 16) |
216 *dst = (tmp[1] << 8) |
228 static inline void crypto4xx_memcpy_from_le32(u32 *dst, const void *buf,
231 crypto4xx_memcpy_swab32(dst, buf, len);
234 static inline void crypto4xx_memcpy_to_le32(__le32 *dst, const void *buf,
237 crypto4xx_memcpy_swab32((u32 *)dst, buf, len);
240 int crypto4xx_setauthsize_aead(struct crypto_aead *ciper,
241 unsigned int authsize);
242 int crypto4xx_setkey_aes_ccm(struct crypto_aead *cipher,
243 const u8 *key, unsigned int keylen);
244 int crypto4xx_encrypt_aes_ccm(struct aead_request *req);
245 int crypto4xx_decrypt_aes_ccm(struct aead_request *req);
246 int crypto4xx_setkey_aes_gcm(struct crypto_aead *cipher,
247 const u8 *key, unsigned int keylen);
248 int crypto4xx_encrypt_aes_gcm(struct aead_request *req);
249 int crypto4xx_decrypt_aes_gcm(struct aead_request *req);