Commit | Line | Data |
---|---|---|
ce921368 JI |
1 | /* |
2 | * Copyright (c) 2010-2011 Picochip Ltd., Jamie Iles | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, | |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
17 | */ | |
2d78db09 | 18 | #include <crypto/internal/aead.h> |
ce921368 JI |
19 | #include <crypto/aes.h> |
20 | #include <crypto/algapi.h> | |
21 | #include <crypto/authenc.h> | |
22 | #include <crypto/des.h> | |
23 | #include <crypto/md5.h> | |
24 | #include <crypto/sha.h> | |
25 | #include <crypto/internal/skcipher.h> | |
26 | #include <linux/clk.h> | |
27 | #include <linux/crypto.h> | |
28 | #include <linux/delay.h> | |
29 | #include <linux/dma-mapping.h> | |
30 | #include <linux/dmapool.h> | |
31 | #include <linux/err.h> | |
32 | #include <linux/init.h> | |
33 | #include <linux/interrupt.h> | |
34 | #include <linux/io.h> | |
35 | #include <linux/list.h> | |
36 | #include <linux/module.h> | |
30343ef1 | 37 | #include <linux/of.h> |
ce921368 JI |
38 | #include <linux/platform_device.h> |
39 | #include <linux/pm.h> | |
40 | #include <linux/rtnetlink.h> | |
41 | #include <linux/scatterlist.h> | |
42 | #include <linux/sched.h> | |
72071fe4 | 43 | #include <linux/sizes.h> |
ce921368 JI |
44 | #include <linux/slab.h> |
45 | #include <linux/timer.h> | |
46 | ||
47 | #include "picoxcell_crypto_regs.h" | |
48 | ||
49 | /* | |
50 | * The threshold for the number of entries in the CMD FIFO available before | |
51 | * the CMD0_CNT interrupt is raised. Increasing this value will reduce the | |
52 | * number of interrupts raised to the CPU. | |
53 | */ | |
54 | #define CMD0_IRQ_THRESHOLD 1 | |
55 | ||
56 | /* | |
57 | * The timeout period (in jiffies) for a PDU. When the the number of PDUs in | |
58 | * flight is greater than the STAT_IRQ_THRESHOLD or 0 the timer is disabled. | |
59 | * When there are packets in flight but lower than the threshold, we enable | |
60 | * the timer and at expiry, attempt to remove any processed packets from the | |
61 | * queue and if there are still packets left, schedule the timer again. | |
62 | */ | |
63 | #define PACKET_TIMEOUT 1 | |
64 | ||
65 | /* The priority to register each algorithm with. */ | |
66 | #define SPACC_CRYPTO_ALG_PRIORITY 10000 | |
67 | ||
68 | #define SPACC_CRYPTO_KASUMI_F8_KEY_LEN 16 | |
69 | #define SPACC_CRYPTO_IPSEC_CIPHER_PG_SZ 64 | |
70 | #define SPACC_CRYPTO_IPSEC_HASH_PG_SZ 64 | |
71 | #define SPACC_CRYPTO_IPSEC_MAX_CTXS 32 | |
72 | #define SPACC_CRYPTO_IPSEC_FIFO_SZ 32 | |
73 | #define SPACC_CRYPTO_L2_CIPHER_PG_SZ 64 | |
74 | #define SPACC_CRYPTO_L2_HASH_PG_SZ 64 | |
75 | #define SPACC_CRYPTO_L2_MAX_CTXS 128 | |
76 | #define SPACC_CRYPTO_L2_FIFO_SZ 128 | |
77 | ||
78 | #define MAX_DDT_LEN 16 | |
79 | ||
80 | /* DDT format. This must match the hardware DDT format exactly. */ | |
81 | struct spacc_ddt { | |
82 | dma_addr_t p; | |
83 | u32 len; | |
84 | }; | |
85 | ||
86 | /* | |
87 | * Asynchronous crypto request structure. | |
88 | * | |
89 | * This structure defines a request that is either queued for processing or | |
90 | * being processed. | |
91 | */ | |
92 | struct spacc_req { | |
93 | struct list_head list; | |
94 | struct spacc_engine *engine; | |
95 | struct crypto_async_request *req; | |
96 | int result; | |
97 | bool is_encrypt; | |
98 | unsigned ctx_id; | |
99 | dma_addr_t src_addr, dst_addr; | |
100 | struct spacc_ddt *src_ddt, *dst_ddt; | |
101 | void (*complete)(struct spacc_req *req); | |
102 | ||
103 | /* AEAD specific bits. */ | |
104 | u8 *giv; | |
105 | size_t giv_len; | |
106 | dma_addr_t giv_pa; | |
107 | }; | |
108 | ||
109 | struct spacc_engine { | |
110 | void __iomem *regs; | |
111 | struct list_head pending; | |
112 | int next_ctx; | |
113 | spinlock_t hw_lock; | |
114 | int in_flight; | |
115 | struct list_head completed; | |
116 | struct list_head in_progress; | |
117 | struct tasklet_struct complete; | |
118 | unsigned long fifo_sz; | |
119 | void __iomem *cipher_ctx_base; | |
120 | void __iomem *hash_key_base; | |
121 | struct spacc_alg *algs; | |
122 | unsigned num_algs; | |
123 | struct list_head registered_algs; | |
124 | size_t cipher_pg_sz; | |
125 | size_t hash_pg_sz; | |
126 | const char *name; | |
127 | struct clk *clk; | |
128 | struct device *dev; | |
129 | unsigned max_ctxs; | |
130 | struct timer_list packet_timeout; | |
131 | unsigned stat_irq_thresh; | |
132 | struct dma_pool *req_pool; | |
133 | }; | |
134 | ||
135 | /* Algorithm type mask. */ | |
136 | #define SPACC_CRYPTO_ALG_MASK 0x7 | |
137 | ||
138 | /* SPACC definition of a crypto algorithm. */ | |
139 | struct spacc_alg { | |
140 | unsigned long ctrl_default; | |
141 | unsigned long type; | |
142 | struct crypto_alg alg; | |
143 | struct spacc_engine *engine; | |
144 | struct list_head entry; | |
145 | int key_offs; | |
146 | int iv_offs; | |
147 | }; | |
148 | ||
149 | /* Generic context structure for any algorithm type. */ | |
150 | struct spacc_generic_ctx { | |
151 | struct spacc_engine *engine; | |
152 | int flags; | |
153 | int key_offs; | |
154 | int iv_offs; | |
155 | }; | |
156 | ||
157 | /* Block cipher context. */ | |
158 | struct spacc_ablk_ctx { | |
159 | struct spacc_generic_ctx generic; | |
160 | u8 key[AES_MAX_KEY_SIZE]; | |
161 | u8 key_len; | |
162 | /* | |
163 | * The fallback cipher. If the operation can't be done in hardware, | |
164 | * fallback to a software version. | |
165 | */ | |
166 | struct crypto_ablkcipher *sw_cipher; | |
167 | }; | |
168 | ||
169 | /* AEAD cipher context. */ | |
170 | struct spacc_aead_ctx { | |
171 | struct spacc_generic_ctx generic; | |
172 | u8 cipher_key[AES_MAX_KEY_SIZE]; | |
173 | u8 hash_ctx[SPACC_CRYPTO_IPSEC_HASH_PG_SZ]; | |
174 | u8 cipher_key_len; | |
175 | u8 hash_key_len; | |
176 | struct crypto_aead *sw_cipher; | |
177 | size_t auth_size; | |
178 | u8 salt[AES_BLOCK_SIZE]; | |
179 | }; | |
180 | ||
40bfc14f JI |
181 | static int spacc_ablk_submit(struct spacc_req *req); |
182 | ||
ce921368 JI |
183 | static inline struct spacc_alg *to_spacc_alg(struct crypto_alg *alg) |
184 | { | |
185 | return alg ? container_of(alg, struct spacc_alg, alg) : NULL; | |
186 | } | |
187 | ||
188 | static inline int spacc_fifo_cmd_full(struct spacc_engine *engine) | |
189 | { | |
190 | u32 fifo_stat = readl(engine->regs + SPA_FIFO_STAT_REG_OFFSET); | |
191 | ||
192 | return fifo_stat & SPA_FIFO_CMD_FULL; | |
193 | } | |
194 | ||
195 | /* | |
196 | * Given a cipher context, and a context number, get the base address of the | |
197 | * context page. | |
198 | * | |
199 | * Returns the address of the context page where the key/context may | |
200 | * be written. | |
201 | */ | |
202 | static inline void __iomem *spacc_ctx_page_addr(struct spacc_generic_ctx *ctx, | |
203 | unsigned indx, | |
204 | bool is_cipher_ctx) | |
205 | { | |
206 | return is_cipher_ctx ? ctx->engine->cipher_ctx_base + | |
207 | (indx * ctx->engine->cipher_pg_sz) : | |
208 | ctx->engine->hash_key_base + (indx * ctx->engine->hash_pg_sz); | |
209 | } | |
210 | ||
211 | /* The context pages can only be written with 32-bit accesses. */ | |
212 | static inline void memcpy_toio32(u32 __iomem *dst, const void *src, | |
213 | unsigned count) | |
214 | { | |
215 | const u32 *src32 = (const u32 *) src; | |
216 | ||
217 | while (count--) | |
218 | writel(*src32++, dst++); | |
219 | } | |
220 | ||
221 | static void spacc_cipher_write_ctx(struct spacc_generic_ctx *ctx, | |
222 | void __iomem *page_addr, const u8 *key, | |
223 | size_t key_len, const u8 *iv, size_t iv_len) | |
224 | { | |
225 | void __iomem *key_ptr = page_addr + ctx->key_offs; | |
226 | void __iomem *iv_ptr = page_addr + ctx->iv_offs; | |
227 | ||
228 | memcpy_toio32(key_ptr, key, key_len / 4); | |
229 | memcpy_toio32(iv_ptr, iv, iv_len / 4); | |
230 | } | |
231 | ||
232 | /* | |
233 | * Load a context into the engines context memory. | |
234 | * | |
235 | * Returns the index of the context page where the context was loaded. | |
236 | */ | |
237 | static unsigned spacc_load_ctx(struct spacc_generic_ctx *ctx, | |
238 | const u8 *ciph_key, size_t ciph_len, | |
239 | const u8 *iv, size_t ivlen, const u8 *hash_key, | |
240 | size_t hash_len) | |
241 | { | |
242 | unsigned indx = ctx->engine->next_ctx++; | |
243 | void __iomem *ciph_page_addr, *hash_page_addr; | |
244 | ||
245 | ciph_page_addr = spacc_ctx_page_addr(ctx, indx, 1); | |
246 | hash_page_addr = spacc_ctx_page_addr(ctx, indx, 0); | |
247 | ||
248 | ctx->engine->next_ctx &= ctx->engine->fifo_sz - 1; | |
249 | spacc_cipher_write_ctx(ctx, ciph_page_addr, ciph_key, ciph_len, iv, | |
250 | ivlen); | |
251 | writel(ciph_len | (indx << SPA_KEY_SZ_CTX_INDEX_OFFSET) | | |
252 | (1 << SPA_KEY_SZ_CIPHER_OFFSET), | |
253 | ctx->engine->regs + SPA_KEY_SZ_REG_OFFSET); | |
254 | ||
255 | if (hash_key) { | |
256 | memcpy_toio32(hash_page_addr, hash_key, hash_len / 4); | |
257 | writel(hash_len | (indx << SPA_KEY_SZ_CTX_INDEX_OFFSET), | |
258 | ctx->engine->regs + SPA_KEY_SZ_REG_OFFSET); | |
259 | } | |
260 | ||
261 | return indx; | |
262 | } | |
263 | ||
264 | /* Count the number of scatterlist entries in a scatterlist. */ | |
265 | static int sg_count(struct scatterlist *sg_list, int nbytes) | |
266 | { | |
267 | struct scatterlist *sg = sg_list; | |
268 | int sg_nents = 0; | |
269 | ||
270 | while (nbytes > 0) { | |
271 | ++sg_nents; | |
272 | nbytes -= sg->length; | |
273 | sg = sg_next(sg); | |
274 | } | |
275 | ||
276 | return sg_nents; | |
277 | } | |
278 | ||
279 | static inline void ddt_set(struct spacc_ddt *ddt, dma_addr_t phys, size_t len) | |
280 | { | |
281 | ddt->p = phys; | |
282 | ddt->len = len; | |
283 | } | |
284 | ||
285 | /* | |
286 | * Take a crypto request and scatterlists for the data and turn them into DDTs | |
287 | * for passing to the crypto engines. This also DMA maps the data so that the | |
288 | * crypto engines can DMA to/from them. | |
289 | */ | |
290 | static struct spacc_ddt *spacc_sg_to_ddt(struct spacc_engine *engine, | |
291 | struct scatterlist *payload, | |
292 | unsigned nbytes, | |
293 | enum dma_data_direction dir, | |
294 | dma_addr_t *ddt_phys) | |
295 | { | |
296 | unsigned nents, mapped_ents; | |
297 | struct scatterlist *cur; | |
298 | struct spacc_ddt *ddt; | |
299 | int i; | |
300 | ||
301 | nents = sg_count(payload, nbytes); | |
302 | mapped_ents = dma_map_sg(engine->dev, payload, nents, dir); | |
303 | ||
304 | if (mapped_ents + 1 > MAX_DDT_LEN) | |
305 | goto out; | |
306 | ||
307 | ddt = dma_pool_alloc(engine->req_pool, GFP_ATOMIC, ddt_phys); | |
308 | if (!ddt) | |
309 | goto out; | |
310 | ||
311 | for_each_sg(payload, cur, mapped_ents, i) | |
312 | ddt_set(&ddt[i], sg_dma_address(cur), sg_dma_len(cur)); | |
313 | ddt_set(&ddt[mapped_ents], 0, 0); | |
314 | ||
315 | return ddt; | |
316 | ||
317 | out: | |
318 | dma_unmap_sg(engine->dev, payload, nents, dir); | |
319 | return NULL; | |
320 | } | |
321 | ||
322 | static int spacc_aead_make_ddts(struct spacc_req *req, u8 *giv) | |
323 | { | |
324 | struct aead_request *areq = container_of(req->req, struct aead_request, | |
325 | base); | |
326 | struct spacc_engine *engine = req->engine; | |
327 | struct spacc_ddt *src_ddt, *dst_ddt; | |
328 | unsigned ivsize = crypto_aead_ivsize(crypto_aead_reqtfm(areq)); | |
329 | unsigned nents = sg_count(areq->src, areq->cryptlen); | |
330 | dma_addr_t iv_addr; | |
331 | struct scatterlist *cur; | |
332 | int i, dst_ents, src_ents, assoc_ents; | |
333 | u8 *iv = giv ? giv : areq->iv; | |
334 | ||
335 | src_ddt = dma_pool_alloc(engine->req_pool, GFP_ATOMIC, &req->src_addr); | |
336 | if (!src_ddt) | |
337 | return -ENOMEM; | |
338 | ||
339 | dst_ddt = dma_pool_alloc(engine->req_pool, GFP_ATOMIC, &req->dst_addr); | |
340 | if (!dst_ddt) { | |
341 | dma_pool_free(engine->req_pool, src_ddt, req->src_addr); | |
342 | return -ENOMEM; | |
343 | } | |
344 | ||
345 | req->src_ddt = src_ddt; | |
346 | req->dst_ddt = dst_ddt; | |
347 | ||
348 | assoc_ents = dma_map_sg(engine->dev, areq->assoc, | |
349 | sg_count(areq->assoc, areq->assoclen), DMA_TO_DEVICE); | |
350 | if (areq->src != areq->dst) { | |
351 | src_ents = dma_map_sg(engine->dev, areq->src, nents, | |
352 | DMA_TO_DEVICE); | |
353 | dst_ents = dma_map_sg(engine->dev, areq->dst, nents, | |
354 | DMA_FROM_DEVICE); | |
355 | } else { | |
356 | src_ents = dma_map_sg(engine->dev, areq->src, nents, | |
357 | DMA_BIDIRECTIONAL); | |
358 | dst_ents = 0; | |
359 | } | |
360 | ||
361 | /* | |
362 | * Map the IV/GIV. For the GIV it needs to be bidirectional as it is | |
363 | * formed by the crypto block and sent as the ESP IV for IPSEC. | |
364 | */ | |
365 | iv_addr = dma_map_single(engine->dev, iv, ivsize, | |
366 | giv ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE); | |
367 | req->giv_pa = iv_addr; | |
368 | ||
369 | /* | |
370 | * Map the associated data. For decryption we don't copy the | |
371 | * associated data. | |
372 | */ | |
373 | for_each_sg(areq->assoc, cur, assoc_ents, i) { | |
374 | ddt_set(src_ddt++, sg_dma_address(cur), sg_dma_len(cur)); | |
375 | if (req->is_encrypt) | |
376 | ddt_set(dst_ddt++, sg_dma_address(cur), | |
377 | sg_dma_len(cur)); | |
378 | } | |
379 | ddt_set(src_ddt++, iv_addr, ivsize); | |
380 | ||
381 | if (giv || req->is_encrypt) | |
382 | ddt_set(dst_ddt++, iv_addr, ivsize); | |
383 | ||
384 | /* | |
385 | * Now map in the payload for the source and destination and terminate | |
386 | * with the NULL pointers. | |
387 | */ | |
388 | for_each_sg(areq->src, cur, src_ents, i) { | |
389 | ddt_set(src_ddt++, sg_dma_address(cur), sg_dma_len(cur)); | |
390 | if (areq->src == areq->dst) | |
391 | ddt_set(dst_ddt++, sg_dma_address(cur), | |
392 | sg_dma_len(cur)); | |
393 | } | |
394 | ||
395 | for_each_sg(areq->dst, cur, dst_ents, i) | |
396 | ddt_set(dst_ddt++, sg_dma_address(cur), | |
397 | sg_dma_len(cur)); | |
398 | ||
399 | ddt_set(src_ddt, 0, 0); | |
400 | ddt_set(dst_ddt, 0, 0); | |
401 | ||
402 | return 0; | |
403 | } | |
404 | ||
405 | static void spacc_aead_free_ddts(struct spacc_req *req) | |
406 | { | |
407 | struct aead_request *areq = container_of(req->req, struct aead_request, | |
408 | base); | |
409 | struct spacc_alg *alg = to_spacc_alg(req->req->tfm->__crt_alg); | |
410 | struct spacc_ablk_ctx *aead_ctx = crypto_tfm_ctx(req->req->tfm); | |
411 | struct spacc_engine *engine = aead_ctx->generic.engine; | |
412 | unsigned ivsize = alg->alg.cra_aead.ivsize; | |
413 | unsigned nents = sg_count(areq->src, areq->cryptlen); | |
414 | ||
415 | if (areq->src != areq->dst) { | |
416 | dma_unmap_sg(engine->dev, areq->src, nents, DMA_TO_DEVICE); | |
417 | dma_unmap_sg(engine->dev, areq->dst, | |
418 | sg_count(areq->dst, areq->cryptlen), | |
419 | DMA_FROM_DEVICE); | |
420 | } else | |
421 | dma_unmap_sg(engine->dev, areq->src, nents, DMA_BIDIRECTIONAL); | |
422 | ||
423 | dma_unmap_sg(engine->dev, areq->assoc, | |
424 | sg_count(areq->assoc, areq->assoclen), DMA_TO_DEVICE); | |
425 | ||
426 | dma_unmap_single(engine->dev, req->giv_pa, ivsize, DMA_BIDIRECTIONAL); | |
427 | ||
428 | dma_pool_free(engine->req_pool, req->src_ddt, req->src_addr); | |
429 | dma_pool_free(engine->req_pool, req->dst_ddt, req->dst_addr); | |
430 | } | |
431 | ||
432 | static void spacc_free_ddt(struct spacc_req *req, struct spacc_ddt *ddt, | |
433 | dma_addr_t ddt_addr, struct scatterlist *payload, | |
434 | unsigned nbytes, enum dma_data_direction dir) | |
435 | { | |
436 | unsigned nents = sg_count(payload, nbytes); | |
437 | ||
438 | dma_unmap_sg(req->engine->dev, payload, nents, dir); | |
439 | dma_pool_free(req->engine->req_pool, ddt, ddt_addr); | |
440 | } | |
441 | ||
442 | /* | |
443 | * Set key for a DES operation in an AEAD cipher. This also performs weak key | |
444 | * checking if required. | |
445 | */ | |
446 | static int spacc_aead_des_setkey(struct crypto_aead *aead, const u8 *key, | |
447 | unsigned int len) | |
448 | { | |
449 | struct crypto_tfm *tfm = crypto_aead_tfm(aead); | |
450 | struct spacc_aead_ctx *ctx = crypto_tfm_ctx(tfm); | |
451 | u32 tmp[DES_EXPKEY_WORDS]; | |
452 | ||
453 | if (unlikely(!des_ekey(tmp, key)) && | |
454 | (crypto_aead_get_flags(aead)) & CRYPTO_TFM_REQ_WEAK_KEY) { | |
455 | tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY; | |
456 | return -EINVAL; | |
457 | } | |
458 | ||
459 | memcpy(ctx->cipher_key, key, len); | |
460 | ctx->cipher_key_len = len; | |
461 | ||
462 | return 0; | |
463 | } | |
464 | ||
465 | /* Set the key for the AES block cipher component of the AEAD transform. */ | |
466 | static int spacc_aead_aes_setkey(struct crypto_aead *aead, const u8 *key, | |
467 | unsigned int len) | |
468 | { | |
469 | struct crypto_tfm *tfm = crypto_aead_tfm(aead); | |
470 | struct spacc_aead_ctx *ctx = crypto_tfm_ctx(tfm); | |
471 | ||
472 | /* | |
473 | * IPSec engine only supports 128 and 256 bit AES keys. If we get a | |
474 | * request for any other size (192 bits) then we need to do a software | |
475 | * fallback. | |
476 | */ | |
477 | if (len != AES_KEYSIZE_128 && len != AES_KEYSIZE_256) { | |
478 | /* | |
479 | * Set the fallback transform to use the same request flags as | |
480 | * the hardware transform. | |
481 | */ | |
482 | ctx->sw_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK; | |
483 | ctx->sw_cipher->base.crt_flags |= | |
484 | tfm->crt_flags & CRYPTO_TFM_REQ_MASK; | |
485 | return crypto_aead_setkey(ctx->sw_cipher, key, len); | |
486 | } | |
487 | ||
488 | memcpy(ctx->cipher_key, key, len); | |
489 | ctx->cipher_key_len = len; | |
490 | ||
491 | return 0; | |
492 | } | |
493 | ||
494 | static int spacc_aead_setkey(struct crypto_aead *tfm, const u8 *key, | |
495 | unsigned int keylen) | |
496 | { | |
497 | struct spacc_aead_ctx *ctx = crypto_aead_ctx(tfm); | |
498 | struct spacc_alg *alg = to_spacc_alg(tfm->base.__crt_alg); | |
ab827fb3 | 499 | struct crypto_authenc_keys keys; |
ce921368 JI |
500 | int err = -EINVAL; |
501 | ||
ab827fb3 | 502 | if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) |
ce921368 JI |
503 | goto badkey; |
504 | ||
ab827fb3 | 505 | if (keys.enckeylen > AES_MAX_KEY_SIZE) |
ce921368 JI |
506 | goto badkey; |
507 | ||
ab827fb3 | 508 | if (keys.authkeylen > sizeof(ctx->hash_ctx)) |
ce921368 JI |
509 | goto badkey; |
510 | ||
511 | if ((alg->ctrl_default & SPACC_CRYPTO_ALG_MASK) == | |
512 | SPA_CTRL_CIPH_ALG_AES) | |
ab827fb3 | 513 | err = spacc_aead_aes_setkey(tfm, keys.enckey, keys.enckeylen); |
ce921368 | 514 | else |
ab827fb3 | 515 | err = spacc_aead_des_setkey(tfm, keys.enckey, keys.enckeylen); |
ce921368 JI |
516 | |
517 | if (err) | |
518 | goto badkey; | |
519 | ||
ab827fb3 MK |
520 | memcpy(ctx->hash_ctx, keys.authkey, keys.authkeylen); |
521 | ctx->hash_key_len = keys.authkeylen; | |
ce921368 JI |
522 | |
523 | return 0; | |
524 | ||
525 | badkey: | |
526 | crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN); | |
527 | return -EINVAL; | |
528 | } | |
529 | ||
530 | static int spacc_aead_setauthsize(struct crypto_aead *tfm, | |
531 | unsigned int authsize) | |
532 | { | |
533 | struct spacc_aead_ctx *ctx = crypto_tfm_ctx(crypto_aead_tfm(tfm)); | |
534 | ||
535 | ctx->auth_size = authsize; | |
536 | ||
537 | return 0; | |
538 | } | |
539 | ||
540 | /* | |
541 | * Check if an AEAD request requires a fallback operation. Some requests can't | |
542 | * be completed in hardware because the hardware may not support certain key | |
543 | * sizes. In these cases we need to complete the request in software. | |
544 | */ | |
545 | static int spacc_aead_need_fallback(struct spacc_req *req) | |
546 | { | |
547 | struct aead_request *aead_req; | |
548 | struct crypto_tfm *tfm = req->req->tfm; | |
549 | struct crypto_alg *alg = req->req->tfm->__crt_alg; | |
550 | struct spacc_alg *spacc_alg = to_spacc_alg(alg); | |
551 | struct spacc_aead_ctx *ctx = crypto_tfm_ctx(tfm); | |
552 | ||
553 | aead_req = container_of(req->req, struct aead_request, base); | |
554 | /* | |
555 | * If we have a non-supported key-length, then we need to do a | |
556 | * software fallback. | |
557 | */ | |
558 | if ((spacc_alg->ctrl_default & SPACC_CRYPTO_ALG_MASK) == | |
559 | SPA_CTRL_CIPH_ALG_AES && | |
560 | ctx->cipher_key_len != AES_KEYSIZE_128 && | |
561 | ctx->cipher_key_len != AES_KEYSIZE_256) | |
562 | return 1; | |
563 | ||
564 | return 0; | |
565 | } | |
566 | ||
567 | static int spacc_aead_do_fallback(struct aead_request *req, unsigned alg_type, | |
568 | bool is_encrypt) | |
569 | { | |
570 | struct crypto_tfm *old_tfm = crypto_aead_tfm(crypto_aead_reqtfm(req)); | |
571 | struct spacc_aead_ctx *ctx = crypto_tfm_ctx(old_tfm); | |
572 | int err; | |
573 | ||
574 | if (ctx->sw_cipher) { | |
575 | /* | |
576 | * Change the request to use the software fallback transform, | |
577 | * and once the ciphering has completed, put the old transform | |
578 | * back into the request. | |
579 | */ | |
580 | aead_request_set_tfm(req, ctx->sw_cipher); | |
581 | err = is_encrypt ? crypto_aead_encrypt(req) : | |
582 | crypto_aead_decrypt(req); | |
583 | aead_request_set_tfm(req, __crypto_aead_cast(old_tfm)); | |
584 | } else | |
585 | err = -EINVAL; | |
586 | ||
587 | return err; | |
588 | } | |
589 | ||
590 | static void spacc_aead_complete(struct spacc_req *req) | |
591 | { | |
592 | spacc_aead_free_ddts(req); | |
593 | req->req->complete(req->req, req->result); | |
594 | } | |
595 | ||
596 | static int spacc_aead_submit(struct spacc_req *req) | |
597 | { | |
598 | struct crypto_tfm *tfm = req->req->tfm; | |
599 | struct spacc_aead_ctx *ctx = crypto_tfm_ctx(tfm); | |
600 | struct crypto_alg *alg = req->req->tfm->__crt_alg; | |
601 | struct spacc_alg *spacc_alg = to_spacc_alg(alg); | |
602 | struct spacc_engine *engine = ctx->generic.engine; | |
603 | u32 ctrl, proc_len, assoc_len; | |
604 | struct aead_request *aead_req = | |
605 | container_of(req->req, struct aead_request, base); | |
606 | ||
607 | req->result = -EINPROGRESS; | |
608 | req->ctx_id = spacc_load_ctx(&ctx->generic, ctx->cipher_key, | |
609 | ctx->cipher_key_len, aead_req->iv, alg->cra_aead.ivsize, | |
610 | ctx->hash_ctx, ctx->hash_key_len); | |
611 | ||
612 | /* Set the source and destination DDT pointers. */ | |
613 | writel(req->src_addr, engine->regs + SPA_SRC_PTR_REG_OFFSET); | |
614 | writel(req->dst_addr, engine->regs + SPA_DST_PTR_REG_OFFSET); | |
615 | writel(0, engine->regs + SPA_OFFSET_REG_OFFSET); | |
616 | ||
617 | assoc_len = aead_req->assoclen; | |
618 | proc_len = aead_req->cryptlen + assoc_len; | |
619 | ||
620 | /* | |
621 | * If we aren't generating an IV, then we need to include the IV in the | |
622 | * associated data so that it is included in the hash. | |
623 | */ | |
624 | if (!req->giv) { | |
625 | assoc_len += crypto_aead_ivsize(crypto_aead_reqtfm(aead_req)); | |
626 | proc_len += crypto_aead_ivsize(crypto_aead_reqtfm(aead_req)); | |
627 | } else | |
628 | proc_len += req->giv_len; | |
629 | ||
630 | /* | |
631 | * If we are decrypting, we need to take the length of the ICV out of | |
632 | * the processing length. | |
633 | */ | |
634 | if (!req->is_encrypt) | |
635 | proc_len -= ctx->auth_size; | |
636 | ||
637 | writel(proc_len, engine->regs + SPA_PROC_LEN_REG_OFFSET); | |
638 | writel(assoc_len, engine->regs + SPA_AAD_LEN_REG_OFFSET); | |
639 | writel(ctx->auth_size, engine->regs + SPA_ICV_LEN_REG_OFFSET); | |
640 | writel(0, engine->regs + SPA_ICV_OFFSET_REG_OFFSET); | |
641 | writel(0, engine->regs + SPA_AUX_INFO_REG_OFFSET); | |
642 | ||
643 | ctrl = spacc_alg->ctrl_default | (req->ctx_id << SPA_CTRL_CTX_IDX) | | |
644 | (1 << SPA_CTRL_ICV_APPEND); | |
645 | if (req->is_encrypt) | |
646 | ctrl |= (1 << SPA_CTRL_ENCRYPT_IDX) | (1 << SPA_CTRL_AAD_COPY); | |
647 | else | |
648 | ctrl |= (1 << SPA_CTRL_KEY_EXP); | |
649 | ||
650 | mod_timer(&engine->packet_timeout, jiffies + PACKET_TIMEOUT); | |
651 | ||
652 | writel(ctrl, engine->regs + SPA_CTRL_REG_OFFSET); | |
653 | ||
654 | return -EINPROGRESS; | |
655 | } | |
656 | ||
40bfc14f JI |
657 | static int spacc_req_submit(struct spacc_req *req); |
658 | ||
659 | static void spacc_push(struct spacc_engine *engine) | |
660 | { | |
661 | struct spacc_req *req; | |
662 | ||
663 | while (!list_empty(&engine->pending) && | |
664 | engine->in_flight + 1 <= engine->fifo_sz) { | |
665 | ||
666 | ++engine->in_flight; | |
667 | req = list_first_entry(&engine->pending, struct spacc_req, | |
668 | list); | |
669 | list_move_tail(&req->list, &engine->in_progress); | |
670 | ||
671 | req->result = spacc_req_submit(req); | |
672 | } | |
673 | } | |
674 | ||
ce921368 JI |
675 | /* |
676 | * Setup an AEAD request for processing. This will configure the engine, load | |
677 | * the context and then start the packet processing. | |
678 | * | |
679 | * @giv Pointer to destination address for a generated IV. If the | |
680 | * request does not need to generate an IV then this should be set to NULL. | |
681 | */ | |
682 | static int spacc_aead_setup(struct aead_request *req, u8 *giv, | |
683 | unsigned alg_type, bool is_encrypt) | |
684 | { | |
685 | struct crypto_alg *alg = req->base.tfm->__crt_alg; | |
686 | struct spacc_engine *engine = to_spacc_alg(alg)->engine; | |
687 | struct spacc_req *dev_req = aead_request_ctx(req); | |
688 | int err = -EINPROGRESS; | |
689 | unsigned long flags; | |
690 | unsigned ivsize = crypto_aead_ivsize(crypto_aead_reqtfm(req)); | |
691 | ||
692 | dev_req->giv = giv; | |
693 | dev_req->giv_len = ivsize; | |
694 | dev_req->req = &req->base; | |
695 | dev_req->is_encrypt = is_encrypt; | |
696 | dev_req->result = -EBUSY; | |
697 | dev_req->engine = engine; | |
698 | dev_req->complete = spacc_aead_complete; | |
699 | ||
700 | if (unlikely(spacc_aead_need_fallback(dev_req))) | |
701 | return spacc_aead_do_fallback(req, alg_type, is_encrypt); | |
702 | ||
703 | spacc_aead_make_ddts(dev_req, dev_req->giv); | |
704 | ||
705 | err = -EINPROGRESS; | |
706 | spin_lock_irqsave(&engine->hw_lock, flags); | |
40bfc14f JI |
707 | if (unlikely(spacc_fifo_cmd_full(engine)) || |
708 | engine->in_flight + 1 > engine->fifo_sz) { | |
ce921368 JI |
709 | if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) { |
710 | err = -EBUSY; | |
711 | spin_unlock_irqrestore(&engine->hw_lock, flags); | |
712 | goto out_free_ddts; | |
713 | } | |
714 | list_add_tail(&dev_req->list, &engine->pending); | |
715 | } else { | |
40bfc14f JI |
716 | list_add_tail(&dev_req->list, &engine->pending); |
717 | spacc_push(engine); | |
ce921368 JI |
718 | } |
719 | spin_unlock_irqrestore(&engine->hw_lock, flags); | |
720 | ||
721 | goto out; | |
722 | ||
723 | out_free_ddts: | |
724 | spacc_aead_free_ddts(dev_req); | |
725 | out: | |
726 | return err; | |
727 | } | |
728 | ||
729 | static int spacc_aead_encrypt(struct aead_request *req) | |
730 | { | |
731 | struct crypto_aead *aead = crypto_aead_reqtfm(req); | |
732 | struct crypto_tfm *tfm = crypto_aead_tfm(aead); | |
733 | struct spacc_alg *alg = to_spacc_alg(tfm->__crt_alg); | |
734 | ||
735 | return spacc_aead_setup(req, NULL, alg->type, 1); | |
736 | } | |
737 | ||
738 | static int spacc_aead_givencrypt(struct aead_givcrypt_request *req) | |
739 | { | |
740 | struct crypto_aead *tfm = aead_givcrypt_reqtfm(req); | |
741 | struct spacc_aead_ctx *ctx = crypto_aead_ctx(tfm); | |
742 | size_t ivsize = crypto_aead_ivsize(tfm); | |
743 | struct spacc_alg *alg = to_spacc_alg(tfm->base.__crt_alg); | |
744 | unsigned len; | |
745 | __be64 seq; | |
746 | ||
747 | memcpy(req->areq.iv, ctx->salt, ivsize); | |
748 | len = ivsize; | |
749 | if (ivsize > sizeof(u64)) { | |
750 | memset(req->giv, 0, ivsize - sizeof(u64)); | |
751 | len = sizeof(u64); | |
752 | } | |
753 | seq = cpu_to_be64(req->seq); | |
754 | memcpy(req->giv + ivsize - len, &seq, len); | |
755 | ||
756 | return spacc_aead_setup(&req->areq, req->giv, alg->type, 1); | |
757 | } | |
758 | ||
759 | static int spacc_aead_decrypt(struct aead_request *req) | |
760 | { | |
761 | struct crypto_aead *aead = crypto_aead_reqtfm(req); | |
762 | struct crypto_tfm *tfm = crypto_aead_tfm(aead); | |
763 | struct spacc_alg *alg = to_spacc_alg(tfm->__crt_alg); | |
764 | ||
765 | return spacc_aead_setup(req, NULL, alg->type, 0); | |
766 | } | |
767 | ||
768 | /* | |
769 | * Initialise a new AEAD context. This is responsible for allocating the | |
770 | * fallback cipher and initialising the context. | |
771 | */ | |
772 | static int spacc_aead_cra_init(struct crypto_tfm *tfm) | |
773 | { | |
774 | struct spacc_aead_ctx *ctx = crypto_tfm_ctx(tfm); | |
775 | struct crypto_alg *alg = tfm->__crt_alg; | |
776 | struct spacc_alg *spacc_alg = to_spacc_alg(alg); | |
777 | struct spacc_engine *engine = spacc_alg->engine; | |
778 | ||
779 | ctx->generic.flags = spacc_alg->type; | |
780 | ctx->generic.engine = engine; | |
781 | ctx->sw_cipher = crypto_alloc_aead(alg->cra_name, 0, | |
782 | CRYPTO_ALG_ASYNC | | |
783 | CRYPTO_ALG_NEED_FALLBACK); | |
784 | if (IS_ERR(ctx->sw_cipher)) { | |
785 | dev_warn(engine->dev, "failed to allocate fallback for %s\n", | |
786 | alg->cra_name); | |
787 | ctx->sw_cipher = NULL; | |
788 | } | |
789 | ctx->generic.key_offs = spacc_alg->key_offs; | |
790 | ctx->generic.iv_offs = spacc_alg->iv_offs; | |
791 | ||
792 | get_random_bytes(ctx->salt, sizeof(ctx->salt)); | |
793 | ||
9611ef63 HX |
794 | crypto_aead_set_reqsize(__crypto_aead_cast(tfm), |
795 | sizeof(struct spacc_req)); | |
ce921368 JI |
796 | |
797 | return 0; | |
798 | } | |
799 | ||
800 | /* | |
801 | * Destructor for an AEAD context. This is called when the transform is freed | |
802 | * and must free the fallback cipher. | |
803 | */ | |
804 | static void spacc_aead_cra_exit(struct crypto_tfm *tfm) | |
805 | { | |
806 | struct spacc_aead_ctx *ctx = crypto_tfm_ctx(tfm); | |
807 | ||
808 | if (ctx->sw_cipher) | |
809 | crypto_free_aead(ctx->sw_cipher); | |
810 | ctx->sw_cipher = NULL; | |
811 | } | |
812 | ||
813 | /* | |
814 | * Set the DES key for a block cipher transform. This also performs weak key | |
815 | * checking if the transform has requested it. | |
816 | */ | |
817 | static int spacc_des_setkey(struct crypto_ablkcipher *cipher, const u8 *key, | |
818 | unsigned int len) | |
819 | { | |
820 | struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher); | |
821 | struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm); | |
822 | u32 tmp[DES_EXPKEY_WORDS]; | |
823 | ||
824 | if (len > DES3_EDE_KEY_SIZE) { | |
825 | crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN); | |
826 | return -EINVAL; | |
827 | } | |
828 | ||
829 | if (unlikely(!des_ekey(tmp, key)) && | |
830 | (crypto_ablkcipher_get_flags(cipher) & CRYPTO_TFM_REQ_WEAK_KEY)) { | |
831 | tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY; | |
832 | return -EINVAL; | |
833 | } | |
834 | ||
835 | memcpy(ctx->key, key, len); | |
836 | ctx->key_len = len; | |
837 | ||
838 | return 0; | |
839 | } | |
840 | ||
841 | /* | |
842 | * Set the key for an AES block cipher. Some key lengths are not supported in | |
843 | * hardware so this must also check whether a fallback is needed. | |
844 | */ | |
845 | static int spacc_aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key, | |
846 | unsigned int len) | |
847 | { | |
848 | struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher); | |
849 | struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm); | |
850 | int err = 0; | |
851 | ||
852 | if (len > AES_MAX_KEY_SIZE) { | |
853 | crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN); | |
854 | return -EINVAL; | |
855 | } | |
856 | ||
857 | /* | |
858 | * IPSec engine only supports 128 and 256 bit AES keys. If we get a | |
859 | * request for any other size (192 bits) then we need to do a software | |
860 | * fallback. | |
861 | */ | |
a9c57a9c | 862 | if (len != AES_KEYSIZE_128 && len != AES_KEYSIZE_256 && |
ce921368 JI |
863 | ctx->sw_cipher) { |
864 | /* | |
865 | * Set the fallback transform to use the same request flags as | |
866 | * the hardware transform. | |
867 | */ | |
868 | ctx->sw_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK; | |
869 | ctx->sw_cipher->base.crt_flags |= | |
870 | cipher->base.crt_flags & CRYPTO_TFM_REQ_MASK; | |
871 | ||
872 | err = crypto_ablkcipher_setkey(ctx->sw_cipher, key, len); | |
873 | if (err) | |
874 | goto sw_setkey_failed; | |
a9c57a9c | 875 | } else if (len != AES_KEYSIZE_128 && len != AES_KEYSIZE_256 && |
ce921368 JI |
876 | !ctx->sw_cipher) |
877 | err = -EINVAL; | |
878 | ||
879 | memcpy(ctx->key, key, len); | |
880 | ctx->key_len = len; | |
881 | ||
882 | sw_setkey_failed: | |
883 | if (err && ctx->sw_cipher) { | |
884 | tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK; | |
885 | tfm->crt_flags |= | |
886 | ctx->sw_cipher->base.crt_flags & CRYPTO_TFM_RES_MASK; | |
887 | } | |
888 | ||
889 | return err; | |
890 | } | |
891 | ||
892 | static int spacc_kasumi_f8_setkey(struct crypto_ablkcipher *cipher, | |
893 | const u8 *key, unsigned int len) | |
894 | { | |
895 | struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher); | |
896 | struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm); | |
897 | int err = 0; | |
898 | ||
899 | if (len > AES_MAX_KEY_SIZE) { | |
900 | crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN); | |
901 | err = -EINVAL; | |
902 | goto out; | |
903 | } | |
904 | ||
905 | memcpy(ctx->key, key, len); | |
906 | ctx->key_len = len; | |
907 | ||
908 | out: | |
909 | return err; | |
910 | } | |
911 | ||
912 | static int spacc_ablk_need_fallback(struct spacc_req *req) | |
913 | { | |
914 | struct spacc_ablk_ctx *ctx; | |
915 | struct crypto_tfm *tfm = req->req->tfm; | |
916 | struct crypto_alg *alg = req->req->tfm->__crt_alg; | |
917 | struct spacc_alg *spacc_alg = to_spacc_alg(alg); | |
918 | ||
919 | ctx = crypto_tfm_ctx(tfm); | |
920 | ||
921 | return (spacc_alg->ctrl_default & SPACC_CRYPTO_ALG_MASK) == | |
922 | SPA_CTRL_CIPH_ALG_AES && | |
923 | ctx->key_len != AES_KEYSIZE_128 && | |
924 | ctx->key_len != AES_KEYSIZE_256; | |
925 | } | |
926 | ||
927 | static void spacc_ablk_complete(struct spacc_req *req) | |
928 | { | |
929 | struct ablkcipher_request *ablk_req = | |
930 | container_of(req->req, struct ablkcipher_request, base); | |
931 | ||
932 | if (ablk_req->src != ablk_req->dst) { | |
933 | spacc_free_ddt(req, req->src_ddt, req->src_addr, ablk_req->src, | |
934 | ablk_req->nbytes, DMA_TO_DEVICE); | |
935 | spacc_free_ddt(req, req->dst_ddt, req->dst_addr, ablk_req->dst, | |
936 | ablk_req->nbytes, DMA_FROM_DEVICE); | |
937 | } else | |
938 | spacc_free_ddt(req, req->dst_ddt, req->dst_addr, ablk_req->dst, | |
939 | ablk_req->nbytes, DMA_BIDIRECTIONAL); | |
940 | ||
941 | req->req->complete(req->req, req->result); | |
942 | } | |
943 | ||
944 | static int spacc_ablk_submit(struct spacc_req *req) | |
945 | { | |
946 | struct crypto_tfm *tfm = req->req->tfm; | |
947 | struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm); | |
948 | struct ablkcipher_request *ablk_req = ablkcipher_request_cast(req->req); | |
949 | struct crypto_alg *alg = req->req->tfm->__crt_alg; | |
950 | struct spacc_alg *spacc_alg = to_spacc_alg(alg); | |
951 | struct spacc_engine *engine = ctx->generic.engine; | |
952 | u32 ctrl; | |
953 | ||
954 | req->ctx_id = spacc_load_ctx(&ctx->generic, ctx->key, | |
955 | ctx->key_len, ablk_req->info, alg->cra_ablkcipher.ivsize, | |
956 | NULL, 0); | |
957 | ||
958 | writel(req->src_addr, engine->regs + SPA_SRC_PTR_REG_OFFSET); | |
959 | writel(req->dst_addr, engine->regs + SPA_DST_PTR_REG_OFFSET); | |
960 | writel(0, engine->regs + SPA_OFFSET_REG_OFFSET); | |
961 | ||
962 | writel(ablk_req->nbytes, engine->regs + SPA_PROC_LEN_REG_OFFSET); | |
963 | writel(0, engine->regs + SPA_ICV_OFFSET_REG_OFFSET); | |
964 | writel(0, engine->regs + SPA_AUX_INFO_REG_OFFSET); | |
965 | writel(0, engine->regs + SPA_AAD_LEN_REG_OFFSET); | |
966 | ||
967 | ctrl = spacc_alg->ctrl_default | (req->ctx_id << SPA_CTRL_CTX_IDX) | | |
968 | (req->is_encrypt ? (1 << SPA_CTRL_ENCRYPT_IDX) : | |
969 | (1 << SPA_CTRL_KEY_EXP)); | |
970 | ||
971 | mod_timer(&engine->packet_timeout, jiffies + PACKET_TIMEOUT); | |
972 | ||
973 | writel(ctrl, engine->regs + SPA_CTRL_REG_OFFSET); | |
974 | ||
975 | return -EINPROGRESS; | |
976 | } | |
977 | ||
978 | static int spacc_ablk_do_fallback(struct ablkcipher_request *req, | |
979 | unsigned alg_type, bool is_encrypt) | |
980 | { | |
981 | struct crypto_tfm *old_tfm = | |
982 | crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req)); | |
983 | struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(old_tfm); | |
984 | int err; | |
985 | ||
986 | if (!ctx->sw_cipher) | |
987 | return -EINVAL; | |
988 | ||
989 | /* | |
990 | * Change the request to use the software fallback transform, and once | |
991 | * the ciphering has completed, put the old transform back into the | |
992 | * request. | |
993 | */ | |
994 | ablkcipher_request_set_tfm(req, ctx->sw_cipher); | |
995 | err = is_encrypt ? crypto_ablkcipher_encrypt(req) : | |
996 | crypto_ablkcipher_decrypt(req); | |
997 | ablkcipher_request_set_tfm(req, __crypto_ablkcipher_cast(old_tfm)); | |
998 | ||
999 | return err; | |
1000 | } | |
1001 | ||
1002 | static int spacc_ablk_setup(struct ablkcipher_request *req, unsigned alg_type, | |
1003 | bool is_encrypt) | |
1004 | { | |
1005 | struct crypto_alg *alg = req->base.tfm->__crt_alg; | |
1006 | struct spacc_engine *engine = to_spacc_alg(alg)->engine; | |
1007 | struct spacc_req *dev_req = ablkcipher_request_ctx(req); | |
1008 | unsigned long flags; | |
1009 | int err = -ENOMEM; | |
1010 | ||
1011 | dev_req->req = &req->base; | |
1012 | dev_req->is_encrypt = is_encrypt; | |
1013 | dev_req->engine = engine; | |
1014 | dev_req->complete = spacc_ablk_complete; | |
1015 | dev_req->result = -EINPROGRESS; | |
1016 | ||
1017 | if (unlikely(spacc_ablk_need_fallback(dev_req))) | |
1018 | return spacc_ablk_do_fallback(req, alg_type, is_encrypt); | |
1019 | ||
1020 | /* | |
1021 | * Create the DDT's for the engine. If we share the same source and | |
1022 | * destination then we can optimize by reusing the DDT's. | |
1023 | */ | |
1024 | if (req->src != req->dst) { | |
1025 | dev_req->src_ddt = spacc_sg_to_ddt(engine, req->src, | |
1026 | req->nbytes, DMA_TO_DEVICE, &dev_req->src_addr); | |
1027 | if (!dev_req->src_ddt) | |
1028 | goto out; | |
1029 | ||
1030 | dev_req->dst_ddt = spacc_sg_to_ddt(engine, req->dst, | |
1031 | req->nbytes, DMA_FROM_DEVICE, &dev_req->dst_addr); | |
1032 | if (!dev_req->dst_ddt) | |
1033 | goto out_free_src; | |
1034 | } else { | |
1035 | dev_req->dst_ddt = spacc_sg_to_ddt(engine, req->dst, | |
1036 | req->nbytes, DMA_BIDIRECTIONAL, &dev_req->dst_addr); | |
1037 | if (!dev_req->dst_ddt) | |
1038 | goto out; | |
1039 | ||
1040 | dev_req->src_ddt = NULL; | |
1041 | dev_req->src_addr = dev_req->dst_addr; | |
1042 | } | |
1043 | ||
1044 | err = -EINPROGRESS; | |
1045 | spin_lock_irqsave(&engine->hw_lock, flags); | |
1046 | /* | |
1047 | * Check if the engine will accept the operation now. If it won't then | |
1048 | * we either stick it on the end of a pending list if we can backlog, | |
1049 | * or bailout with an error if not. | |
1050 | */ | |
40bfc14f JI |
1051 | if (unlikely(spacc_fifo_cmd_full(engine)) || |
1052 | engine->in_flight + 1 > engine->fifo_sz) { | |
ce921368 JI |
1053 | if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) { |
1054 | err = -EBUSY; | |
1055 | spin_unlock_irqrestore(&engine->hw_lock, flags); | |
1056 | goto out_free_ddts; | |
1057 | } | |
1058 | list_add_tail(&dev_req->list, &engine->pending); | |
1059 | } else { | |
40bfc14f JI |
1060 | list_add_tail(&dev_req->list, &engine->pending); |
1061 | spacc_push(engine); | |
ce921368 JI |
1062 | } |
1063 | spin_unlock_irqrestore(&engine->hw_lock, flags); | |
1064 | ||
1065 | goto out; | |
1066 | ||
1067 | out_free_ddts: | |
1068 | spacc_free_ddt(dev_req, dev_req->dst_ddt, dev_req->dst_addr, req->dst, | |
1069 | req->nbytes, req->src == req->dst ? | |
1070 | DMA_BIDIRECTIONAL : DMA_FROM_DEVICE); | |
1071 | out_free_src: | |
1072 | if (req->src != req->dst) | |
1073 | spacc_free_ddt(dev_req, dev_req->src_ddt, dev_req->src_addr, | |
1074 | req->src, req->nbytes, DMA_TO_DEVICE); | |
1075 | out: | |
1076 | return err; | |
1077 | } | |
1078 | ||
1079 | static int spacc_ablk_cra_init(struct crypto_tfm *tfm) | |
1080 | { | |
1081 | struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm); | |
1082 | struct crypto_alg *alg = tfm->__crt_alg; | |
1083 | struct spacc_alg *spacc_alg = to_spacc_alg(alg); | |
1084 | struct spacc_engine *engine = spacc_alg->engine; | |
1085 | ||
1086 | ctx->generic.flags = spacc_alg->type; | |
1087 | ctx->generic.engine = engine; | |
1088 | if (alg->cra_flags & CRYPTO_ALG_NEED_FALLBACK) { | |
1089 | ctx->sw_cipher = crypto_alloc_ablkcipher(alg->cra_name, 0, | |
1090 | CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK); | |
1091 | if (IS_ERR(ctx->sw_cipher)) { | |
1092 | dev_warn(engine->dev, "failed to allocate fallback for %s\n", | |
1093 | alg->cra_name); | |
1094 | ctx->sw_cipher = NULL; | |
1095 | } | |
1096 | } | |
1097 | ctx->generic.key_offs = spacc_alg->key_offs; | |
1098 | ctx->generic.iv_offs = spacc_alg->iv_offs; | |
1099 | ||
1100 | tfm->crt_ablkcipher.reqsize = sizeof(struct spacc_req); | |
1101 | ||
1102 | return 0; | |
1103 | } | |
1104 | ||
1105 | static void spacc_ablk_cra_exit(struct crypto_tfm *tfm) | |
1106 | { | |
1107 | struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm); | |
1108 | ||
1109 | if (ctx->sw_cipher) | |
1110 | crypto_free_ablkcipher(ctx->sw_cipher); | |
1111 | ctx->sw_cipher = NULL; | |
1112 | } | |
1113 | ||
1114 | static int spacc_ablk_encrypt(struct ablkcipher_request *req) | |
1115 | { | |
1116 | struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(req); | |
1117 | struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher); | |
1118 | struct spacc_alg *alg = to_spacc_alg(tfm->__crt_alg); | |
1119 | ||
1120 | return spacc_ablk_setup(req, alg->type, 1); | |
1121 | } | |
1122 | ||
1123 | static int spacc_ablk_decrypt(struct ablkcipher_request *req) | |
1124 | { | |
1125 | struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(req); | |
1126 | struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher); | |
1127 | struct spacc_alg *alg = to_spacc_alg(tfm->__crt_alg); | |
1128 | ||
1129 | return spacc_ablk_setup(req, alg->type, 0); | |
1130 | } | |
1131 | ||
1132 | static inline int spacc_fifo_stat_empty(struct spacc_engine *engine) | |
1133 | { | |
1134 | return readl(engine->regs + SPA_FIFO_STAT_REG_OFFSET) & | |
1135 | SPA_FIFO_STAT_EMPTY; | |
1136 | } | |
1137 | ||
1138 | static void spacc_process_done(struct spacc_engine *engine) | |
1139 | { | |
1140 | struct spacc_req *req; | |
1141 | unsigned long flags; | |
1142 | ||
1143 | spin_lock_irqsave(&engine->hw_lock, flags); | |
1144 | ||
1145 | while (!spacc_fifo_stat_empty(engine)) { | |
1146 | req = list_first_entry(&engine->in_progress, struct spacc_req, | |
1147 | list); | |
1148 | list_move_tail(&req->list, &engine->completed); | |
40bfc14f | 1149 | --engine->in_flight; |
ce921368 JI |
1150 | |
1151 | /* POP the status register. */ | |
1152 | writel(~0, engine->regs + SPA_STAT_POP_REG_OFFSET); | |
1153 | req->result = (readl(engine->regs + SPA_STATUS_REG_OFFSET) & | |
1154 | SPA_STATUS_RES_CODE_MASK) >> SPA_STATUS_RES_CODE_OFFSET; | |
1155 | ||
1156 | /* | |
1157 | * Convert the SPAcc error status into the standard POSIX error | |
1158 | * codes. | |
1159 | */ | |
1160 | if (unlikely(req->result)) { | |
1161 | switch (req->result) { | |
1162 | case SPA_STATUS_ICV_FAIL: | |
1163 | req->result = -EBADMSG; | |
1164 | break; | |
1165 | ||
1166 | case SPA_STATUS_MEMORY_ERROR: | |
1167 | dev_warn(engine->dev, | |
1168 | "memory error triggered\n"); | |
1169 | req->result = -EFAULT; | |
1170 | break; | |
1171 | ||
1172 | case SPA_STATUS_BLOCK_ERROR: | |
1173 | dev_warn(engine->dev, | |
1174 | "block error triggered\n"); | |
1175 | req->result = -EIO; | |
1176 | break; | |
1177 | } | |
1178 | } | |
1179 | } | |
1180 | ||
1181 | tasklet_schedule(&engine->complete); | |
1182 | ||
1183 | spin_unlock_irqrestore(&engine->hw_lock, flags); | |
1184 | } | |
1185 | ||
1186 | static irqreturn_t spacc_spacc_irq(int irq, void *dev) | |
1187 | { | |
1188 | struct spacc_engine *engine = (struct spacc_engine *)dev; | |
1189 | u32 spacc_irq_stat = readl(engine->regs + SPA_IRQ_STAT_REG_OFFSET); | |
1190 | ||
1191 | writel(spacc_irq_stat, engine->regs + SPA_IRQ_STAT_REG_OFFSET); | |
1192 | spacc_process_done(engine); | |
1193 | ||
1194 | return IRQ_HANDLED; | |
1195 | } | |
1196 | ||
1197 | static void spacc_packet_timeout(unsigned long data) | |
1198 | { | |
1199 | struct spacc_engine *engine = (struct spacc_engine *)data; | |
1200 | ||
1201 | spacc_process_done(engine); | |
1202 | } | |
1203 | ||
1204 | static int spacc_req_submit(struct spacc_req *req) | |
1205 | { | |
1206 | struct crypto_alg *alg = req->req->tfm->__crt_alg; | |
1207 | ||
1208 | if (CRYPTO_ALG_TYPE_AEAD == (CRYPTO_ALG_TYPE_MASK & alg->cra_flags)) | |
1209 | return spacc_aead_submit(req); | |
1210 | else | |
1211 | return spacc_ablk_submit(req); | |
1212 | } | |
1213 | ||
1214 | static void spacc_spacc_complete(unsigned long data) | |
1215 | { | |
1216 | struct spacc_engine *engine = (struct spacc_engine *)data; | |
1217 | struct spacc_req *req, *tmp; | |
1218 | unsigned long flags; | |
ce921368 JI |
1219 | LIST_HEAD(completed); |
1220 | ||
1221 | spin_lock_irqsave(&engine->hw_lock, flags); | |
40bfc14f | 1222 | |
ce921368 | 1223 | list_splice_init(&engine->completed, &completed); |
40bfc14f JI |
1224 | spacc_push(engine); |
1225 | if (engine->in_flight) | |
1226 | mod_timer(&engine->packet_timeout, jiffies + PACKET_TIMEOUT); | |
1227 | ||
ce921368 JI |
1228 | spin_unlock_irqrestore(&engine->hw_lock, flags); |
1229 | ||
1230 | list_for_each_entry_safe(req, tmp, &completed, list) { | |
40bfc14f | 1231 | list_del(&req->list); |
b64dc04b | 1232 | req->complete(req); |
ce921368 | 1233 | } |
ce921368 JI |
1234 | } |
1235 | ||
1236 | #ifdef CONFIG_PM | |
1237 | static int spacc_suspend(struct device *dev) | |
1238 | { | |
1239 | struct platform_device *pdev = to_platform_device(dev); | |
1240 | struct spacc_engine *engine = platform_get_drvdata(pdev); | |
1241 | ||
1242 | /* | |
1243 | * We only support standby mode. All we have to do is gate the clock to | |
1244 | * the spacc. The hardware will preserve state until we turn it back | |
1245 | * on again. | |
1246 | */ | |
1247 | clk_disable(engine->clk); | |
1248 | ||
1249 | return 0; | |
1250 | } | |
1251 | ||
1252 | static int spacc_resume(struct device *dev) | |
1253 | { | |
1254 | struct platform_device *pdev = to_platform_device(dev); | |
1255 | struct spacc_engine *engine = platform_get_drvdata(pdev); | |
1256 | ||
1257 | return clk_enable(engine->clk); | |
1258 | } | |
1259 | ||
1260 | static const struct dev_pm_ops spacc_pm_ops = { | |
1261 | .suspend = spacc_suspend, | |
1262 | .resume = spacc_resume, | |
1263 | }; | |
1264 | #endif /* CONFIG_PM */ | |
1265 | ||
1266 | static inline struct spacc_engine *spacc_dev_to_engine(struct device *dev) | |
1267 | { | |
1268 | return dev ? platform_get_drvdata(to_platform_device(dev)) : NULL; | |
1269 | } | |
1270 | ||
1271 | static ssize_t spacc_stat_irq_thresh_show(struct device *dev, | |
1272 | struct device_attribute *attr, | |
1273 | char *buf) | |
1274 | { | |
1275 | struct spacc_engine *engine = spacc_dev_to_engine(dev); | |
1276 | ||
1277 | return snprintf(buf, PAGE_SIZE, "%u\n", engine->stat_irq_thresh); | |
1278 | } | |
1279 | ||
1280 | static ssize_t spacc_stat_irq_thresh_store(struct device *dev, | |
1281 | struct device_attribute *attr, | |
1282 | const char *buf, size_t len) | |
1283 | { | |
1284 | struct spacc_engine *engine = spacc_dev_to_engine(dev); | |
1285 | unsigned long thresh; | |
1286 | ||
61e2d1a9 | 1287 | if (kstrtoul(buf, 0, &thresh)) |
ce921368 JI |
1288 | return -EINVAL; |
1289 | ||
1290 | thresh = clamp(thresh, 1UL, engine->fifo_sz - 1); | |
1291 | ||
1292 | engine->stat_irq_thresh = thresh; | |
1293 | writel(engine->stat_irq_thresh << SPA_IRQ_CTRL_STAT_CNT_OFFSET, | |
1294 | engine->regs + SPA_IRQ_CTRL_REG_OFFSET); | |
1295 | ||
1296 | return len; | |
1297 | } | |
1298 | static DEVICE_ATTR(stat_irq_thresh, 0644, spacc_stat_irq_thresh_show, | |
1299 | spacc_stat_irq_thresh_store); | |
1300 | ||
1301 | static struct spacc_alg ipsec_engine_algs[] = { | |
1302 | { | |
1303 | .ctrl_default = SPA_CTRL_CIPH_ALG_AES | SPA_CTRL_CIPH_MODE_CBC, | |
1304 | .key_offs = 0, | |
1305 | .iv_offs = AES_MAX_KEY_SIZE, | |
1306 | .alg = { | |
1307 | .cra_name = "cbc(aes)", | |
1308 | .cra_driver_name = "cbc-aes-picoxcell", | |
1309 | .cra_priority = SPACC_CRYPTO_ALG_PRIORITY, | |
1310 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | | |
d912bb76 | 1311 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
ce921368 JI |
1312 | CRYPTO_ALG_ASYNC | |
1313 | CRYPTO_ALG_NEED_FALLBACK, | |
1314 | .cra_blocksize = AES_BLOCK_SIZE, | |
1315 | .cra_ctxsize = sizeof(struct spacc_ablk_ctx), | |
1316 | .cra_type = &crypto_ablkcipher_type, | |
1317 | .cra_module = THIS_MODULE, | |
1318 | .cra_ablkcipher = { | |
1319 | .setkey = spacc_aes_setkey, | |
1320 | .encrypt = spacc_ablk_encrypt, | |
1321 | .decrypt = spacc_ablk_decrypt, | |
1322 | .min_keysize = AES_MIN_KEY_SIZE, | |
1323 | .max_keysize = AES_MAX_KEY_SIZE, | |
1324 | .ivsize = AES_BLOCK_SIZE, | |
1325 | }, | |
1326 | .cra_init = spacc_ablk_cra_init, | |
1327 | .cra_exit = spacc_ablk_cra_exit, | |
1328 | }, | |
1329 | }, | |
1330 | { | |
1331 | .key_offs = 0, | |
1332 | .iv_offs = AES_MAX_KEY_SIZE, | |
1333 | .ctrl_default = SPA_CTRL_CIPH_ALG_AES | SPA_CTRL_CIPH_MODE_ECB, | |
1334 | .alg = { | |
1335 | .cra_name = "ecb(aes)", | |
1336 | .cra_driver_name = "ecb-aes-picoxcell", | |
1337 | .cra_priority = SPACC_CRYPTO_ALG_PRIORITY, | |
1338 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | | |
d912bb76 | 1339 | CRYPTO_ALG_KERN_DRIVER_ONLY | |
ce921368 JI |
1340 | CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK, |
1341 | .cra_blocksize = AES_BLOCK_SIZE, | |
1342 | .cra_ctxsize = sizeof(struct spacc_ablk_ctx), | |
1343 | .cra_type = &crypto_ablkcipher_type, | |
1344 | .cra_module = THIS_MODULE, | |
1345 | .cra_ablkcipher = { | |
1346 | .setkey = spacc_aes_setkey, | |
1347 | .encrypt = spacc_ablk_encrypt, | |
1348 | .decrypt = spacc_ablk_decrypt, | |
1349 | .min_keysize = AES_MIN_KEY_SIZE, | |
1350 | .max_keysize = AES_MAX_KEY_SIZE, | |
1351 | }, | |
1352 | .cra_init = spacc_ablk_cra_init, | |
1353 | .cra_exit = spacc_ablk_cra_exit, | |
1354 | }, | |
1355 | }, | |
1356 | { | |
1357 | .key_offs = DES_BLOCK_SIZE, | |
1358 | .iv_offs = 0, | |
1359 | .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_CBC, | |
1360 | .alg = { | |
1361 | .cra_name = "cbc(des)", | |
1362 | .cra_driver_name = "cbc-des-picoxcell", | |
1363 | .cra_priority = SPACC_CRYPTO_ALG_PRIORITY, | |
d912bb76 NM |
1364 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
1365 | CRYPTO_ALG_ASYNC | | |
1366 | CRYPTO_ALG_KERN_DRIVER_ONLY, | |
ce921368 JI |
1367 | .cra_blocksize = DES_BLOCK_SIZE, |
1368 | .cra_ctxsize = sizeof(struct spacc_ablk_ctx), | |
1369 | .cra_type = &crypto_ablkcipher_type, | |
1370 | .cra_module = THIS_MODULE, | |
1371 | .cra_ablkcipher = { | |
1372 | .setkey = spacc_des_setkey, | |
1373 | .encrypt = spacc_ablk_encrypt, | |
1374 | .decrypt = spacc_ablk_decrypt, | |
1375 | .min_keysize = DES_KEY_SIZE, | |
1376 | .max_keysize = DES_KEY_SIZE, | |
1377 | .ivsize = DES_BLOCK_SIZE, | |
1378 | }, | |
1379 | .cra_init = spacc_ablk_cra_init, | |
1380 | .cra_exit = spacc_ablk_cra_exit, | |
1381 | }, | |
1382 | }, | |
1383 | { | |
1384 | .key_offs = DES_BLOCK_SIZE, | |
1385 | .iv_offs = 0, | |
1386 | .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_ECB, | |
1387 | .alg = { | |
1388 | .cra_name = "ecb(des)", | |
1389 | .cra_driver_name = "ecb-des-picoxcell", | |
1390 | .cra_priority = SPACC_CRYPTO_ALG_PRIORITY, | |
d912bb76 NM |
1391 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
1392 | CRYPTO_ALG_ASYNC | | |
1393 | CRYPTO_ALG_KERN_DRIVER_ONLY, | |
ce921368 JI |
1394 | .cra_blocksize = DES_BLOCK_SIZE, |
1395 | .cra_ctxsize = sizeof(struct spacc_ablk_ctx), | |
1396 | .cra_type = &crypto_ablkcipher_type, | |
1397 | .cra_module = THIS_MODULE, | |
1398 | .cra_ablkcipher = { | |
1399 | .setkey = spacc_des_setkey, | |
1400 | .encrypt = spacc_ablk_encrypt, | |
1401 | .decrypt = spacc_ablk_decrypt, | |
1402 | .min_keysize = DES_KEY_SIZE, | |
1403 | .max_keysize = DES_KEY_SIZE, | |
1404 | }, | |
1405 | .cra_init = spacc_ablk_cra_init, | |
1406 | .cra_exit = spacc_ablk_cra_exit, | |
1407 | }, | |
1408 | }, | |
1409 | { | |
1410 | .key_offs = DES_BLOCK_SIZE, | |
1411 | .iv_offs = 0, | |
1412 | .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_CBC, | |
1413 | .alg = { | |
1414 | .cra_name = "cbc(des3_ede)", | |
1415 | .cra_driver_name = "cbc-des3-ede-picoxcell", | |
1416 | .cra_priority = SPACC_CRYPTO_ALG_PRIORITY, | |
d912bb76 NM |
1417 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
1418 | CRYPTO_ALG_ASYNC | | |
1419 | CRYPTO_ALG_KERN_DRIVER_ONLY, | |
ce921368 JI |
1420 | .cra_blocksize = DES3_EDE_BLOCK_SIZE, |
1421 | .cra_ctxsize = sizeof(struct spacc_ablk_ctx), | |
1422 | .cra_type = &crypto_ablkcipher_type, | |
1423 | .cra_module = THIS_MODULE, | |
1424 | .cra_ablkcipher = { | |
1425 | .setkey = spacc_des_setkey, | |
1426 | .encrypt = spacc_ablk_encrypt, | |
1427 | .decrypt = spacc_ablk_decrypt, | |
1428 | .min_keysize = DES3_EDE_KEY_SIZE, | |
1429 | .max_keysize = DES3_EDE_KEY_SIZE, | |
1430 | .ivsize = DES3_EDE_BLOCK_SIZE, | |
1431 | }, | |
1432 | .cra_init = spacc_ablk_cra_init, | |
1433 | .cra_exit = spacc_ablk_cra_exit, | |
1434 | }, | |
1435 | }, | |
1436 | { | |
1437 | .key_offs = DES_BLOCK_SIZE, | |
1438 | .iv_offs = 0, | |
1439 | .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_ECB, | |
1440 | .alg = { | |
1441 | .cra_name = "ecb(des3_ede)", | |
1442 | .cra_driver_name = "ecb-des3-ede-picoxcell", | |
1443 | .cra_priority = SPACC_CRYPTO_ALG_PRIORITY, | |
d912bb76 NM |
1444 | .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | |
1445 | CRYPTO_ALG_ASYNC | | |
1446 | CRYPTO_ALG_KERN_DRIVER_ONLY, | |
ce921368 JI |
1447 | .cra_blocksize = DES3_EDE_BLOCK_SIZE, |
1448 | .cra_ctxsize = sizeof(struct spacc_ablk_ctx), | |
1449 | .cra_type = &crypto_ablkcipher_type, | |
1450 | .cra_module = THIS_MODULE, | |
1451 | .cra_ablkcipher = { | |
1452 | .setkey = spacc_des_setkey, | |
1453 | .encrypt = spacc_ablk_encrypt, | |
1454 | .decrypt = spacc_ablk_decrypt, | |
1455 | .min_keysize = DES3_EDE_KEY_SIZE, | |
1456 | .max_keysize = DES3_EDE_KEY_SIZE, | |
1457 | }, | |
1458 | .cra_init = spacc_ablk_cra_init, | |
1459 | .cra_exit = spacc_ablk_cra_exit, | |
1460 | }, | |
1461 | }, | |
1462 | { | |
1463 | .ctrl_default = SPA_CTRL_CIPH_ALG_AES | SPA_CTRL_CIPH_MODE_CBC | | |
1464 | SPA_CTRL_HASH_ALG_SHA | SPA_CTRL_HASH_MODE_HMAC, | |
1465 | .key_offs = 0, | |
1466 | .iv_offs = AES_MAX_KEY_SIZE, | |
1467 | .alg = { | |
1468 | .cra_name = "authenc(hmac(sha1),cbc(aes))", | |
1469 | .cra_driver_name = "authenc-hmac-sha1-cbc-aes-picoxcell", | |
1470 | .cra_priority = SPACC_CRYPTO_ALG_PRIORITY, | |
d912bb76 NM |
1471 | .cra_flags = CRYPTO_ALG_TYPE_AEAD | |
1472 | CRYPTO_ALG_ASYNC | | |
1473 | CRYPTO_ALG_KERN_DRIVER_ONLY, | |
ce921368 JI |
1474 | .cra_blocksize = AES_BLOCK_SIZE, |
1475 | .cra_ctxsize = sizeof(struct spacc_aead_ctx), | |
1476 | .cra_type = &crypto_aead_type, | |
1477 | .cra_module = THIS_MODULE, | |
1478 | .cra_aead = { | |
1479 | .setkey = spacc_aead_setkey, | |
1480 | .setauthsize = spacc_aead_setauthsize, | |
1481 | .encrypt = spacc_aead_encrypt, | |
1482 | .decrypt = spacc_aead_decrypt, | |
1483 | .givencrypt = spacc_aead_givencrypt, | |
1484 | .ivsize = AES_BLOCK_SIZE, | |
1485 | .maxauthsize = SHA1_DIGEST_SIZE, | |
1486 | }, | |
1487 | .cra_init = spacc_aead_cra_init, | |
1488 | .cra_exit = spacc_aead_cra_exit, | |
1489 | }, | |
1490 | }, | |
1491 | { | |
1492 | .ctrl_default = SPA_CTRL_CIPH_ALG_AES | SPA_CTRL_CIPH_MODE_CBC | | |
1493 | SPA_CTRL_HASH_ALG_SHA256 | | |
1494 | SPA_CTRL_HASH_MODE_HMAC, | |
1495 | .key_offs = 0, | |
1496 | .iv_offs = AES_MAX_KEY_SIZE, | |
1497 | .alg = { | |
1498 | .cra_name = "authenc(hmac(sha256),cbc(aes))", | |
1499 | .cra_driver_name = "authenc-hmac-sha256-cbc-aes-picoxcell", | |
1500 | .cra_priority = SPACC_CRYPTO_ALG_PRIORITY, | |
d912bb76 NM |
1501 | .cra_flags = CRYPTO_ALG_TYPE_AEAD | |
1502 | CRYPTO_ALG_ASYNC | | |
1503 | CRYPTO_ALG_KERN_DRIVER_ONLY, | |
ce921368 JI |
1504 | .cra_blocksize = AES_BLOCK_SIZE, |
1505 | .cra_ctxsize = sizeof(struct spacc_aead_ctx), | |
1506 | .cra_type = &crypto_aead_type, | |
1507 | .cra_module = THIS_MODULE, | |
1508 | .cra_aead = { | |
1509 | .setkey = spacc_aead_setkey, | |
1510 | .setauthsize = spacc_aead_setauthsize, | |
1511 | .encrypt = spacc_aead_encrypt, | |
1512 | .decrypt = spacc_aead_decrypt, | |
1513 | .givencrypt = spacc_aead_givencrypt, | |
1514 | .ivsize = AES_BLOCK_SIZE, | |
1515 | .maxauthsize = SHA256_DIGEST_SIZE, | |
1516 | }, | |
1517 | .cra_init = spacc_aead_cra_init, | |
1518 | .cra_exit = spacc_aead_cra_exit, | |
1519 | }, | |
1520 | }, | |
1521 | { | |
1522 | .key_offs = 0, | |
1523 | .iv_offs = AES_MAX_KEY_SIZE, | |
1524 | .ctrl_default = SPA_CTRL_CIPH_ALG_AES | SPA_CTRL_CIPH_MODE_CBC | | |
1525 | SPA_CTRL_HASH_ALG_MD5 | SPA_CTRL_HASH_MODE_HMAC, | |
1526 | .alg = { | |
1527 | .cra_name = "authenc(hmac(md5),cbc(aes))", | |
1528 | .cra_driver_name = "authenc-hmac-md5-cbc-aes-picoxcell", | |
1529 | .cra_priority = SPACC_CRYPTO_ALG_PRIORITY, | |
d912bb76 NM |
1530 | .cra_flags = CRYPTO_ALG_TYPE_AEAD | |
1531 | CRYPTO_ALG_ASYNC | | |
1532 | CRYPTO_ALG_KERN_DRIVER_ONLY, | |
ce921368 JI |
1533 | .cra_blocksize = AES_BLOCK_SIZE, |
1534 | .cra_ctxsize = sizeof(struct spacc_aead_ctx), | |
1535 | .cra_type = &crypto_aead_type, | |
1536 | .cra_module = THIS_MODULE, | |
1537 | .cra_aead = { | |
1538 | .setkey = spacc_aead_setkey, | |
1539 | .setauthsize = spacc_aead_setauthsize, | |
1540 | .encrypt = spacc_aead_encrypt, | |
1541 | .decrypt = spacc_aead_decrypt, | |
1542 | .givencrypt = spacc_aead_givencrypt, | |
1543 | .ivsize = AES_BLOCK_SIZE, | |
1544 | .maxauthsize = MD5_DIGEST_SIZE, | |
1545 | }, | |
1546 | .cra_init = spacc_aead_cra_init, | |
1547 | .cra_exit = spacc_aead_cra_exit, | |
1548 | }, | |
1549 | }, | |
1550 | { | |
1551 | .key_offs = DES_BLOCK_SIZE, | |
1552 | .iv_offs = 0, | |
1553 | .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_CBC | | |
1554 | SPA_CTRL_HASH_ALG_SHA | SPA_CTRL_HASH_MODE_HMAC, | |
1555 | .alg = { | |
1556 | .cra_name = "authenc(hmac(sha1),cbc(des3_ede))", | |
1557 | .cra_driver_name = "authenc-hmac-sha1-cbc-3des-picoxcell", | |
1558 | .cra_priority = SPACC_CRYPTO_ALG_PRIORITY, | |
d912bb76 NM |
1559 | .cra_flags = CRYPTO_ALG_TYPE_AEAD | |
1560 | CRYPTO_ALG_ASYNC | | |
1561 | CRYPTO_ALG_KERN_DRIVER_ONLY, | |
ce921368 JI |
1562 | .cra_blocksize = DES3_EDE_BLOCK_SIZE, |
1563 | .cra_ctxsize = sizeof(struct spacc_aead_ctx), | |
1564 | .cra_type = &crypto_aead_type, | |
1565 | .cra_module = THIS_MODULE, | |
1566 | .cra_aead = { | |
1567 | .setkey = spacc_aead_setkey, | |
1568 | .setauthsize = spacc_aead_setauthsize, | |
1569 | .encrypt = spacc_aead_encrypt, | |
1570 | .decrypt = spacc_aead_decrypt, | |
1571 | .givencrypt = spacc_aead_givencrypt, | |
1572 | .ivsize = DES3_EDE_BLOCK_SIZE, | |
1573 | .maxauthsize = SHA1_DIGEST_SIZE, | |
1574 | }, | |
1575 | .cra_init = spacc_aead_cra_init, | |
1576 | .cra_exit = spacc_aead_cra_exit, | |
1577 | }, | |
1578 | }, | |
1579 | { | |
1580 | .key_offs = DES_BLOCK_SIZE, | |
1581 | .iv_offs = 0, | |
1582 | .ctrl_default = SPA_CTRL_CIPH_ALG_AES | SPA_CTRL_CIPH_MODE_CBC | | |
1583 | SPA_CTRL_HASH_ALG_SHA256 | | |
1584 | SPA_CTRL_HASH_MODE_HMAC, | |
1585 | .alg = { | |
1586 | .cra_name = "authenc(hmac(sha256),cbc(des3_ede))", | |
1587 | .cra_driver_name = "authenc-hmac-sha256-cbc-3des-picoxcell", | |
1588 | .cra_priority = SPACC_CRYPTO_ALG_PRIORITY, | |
d912bb76 NM |
1589 | .cra_flags = CRYPTO_ALG_TYPE_AEAD | |
1590 | CRYPTO_ALG_ASYNC | | |
1591 | CRYPTO_ALG_KERN_DRIVER_ONLY, | |
ce921368 JI |
1592 | .cra_blocksize = DES3_EDE_BLOCK_SIZE, |
1593 | .cra_ctxsize = sizeof(struct spacc_aead_ctx), | |
1594 | .cra_type = &crypto_aead_type, | |
1595 | .cra_module = THIS_MODULE, | |
1596 | .cra_aead = { | |
1597 | .setkey = spacc_aead_setkey, | |
1598 | .setauthsize = spacc_aead_setauthsize, | |
1599 | .encrypt = spacc_aead_encrypt, | |
1600 | .decrypt = spacc_aead_decrypt, | |
1601 | .givencrypt = spacc_aead_givencrypt, | |
1602 | .ivsize = DES3_EDE_BLOCK_SIZE, | |
1603 | .maxauthsize = SHA256_DIGEST_SIZE, | |
1604 | }, | |
1605 | .cra_init = spacc_aead_cra_init, | |
1606 | .cra_exit = spacc_aead_cra_exit, | |
1607 | }, | |
1608 | }, | |
1609 | { | |
1610 | .key_offs = DES_BLOCK_SIZE, | |
1611 | .iv_offs = 0, | |
1612 | .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_CBC | | |
1613 | SPA_CTRL_HASH_ALG_MD5 | SPA_CTRL_HASH_MODE_HMAC, | |
1614 | .alg = { | |
1615 | .cra_name = "authenc(hmac(md5),cbc(des3_ede))", | |
1616 | .cra_driver_name = "authenc-hmac-md5-cbc-3des-picoxcell", | |
1617 | .cra_priority = SPACC_CRYPTO_ALG_PRIORITY, | |
d912bb76 NM |
1618 | .cra_flags = CRYPTO_ALG_TYPE_AEAD | |
1619 | CRYPTO_ALG_ASYNC | | |
1620 | CRYPTO_ALG_KERN_DRIVER_ONLY, | |
ce921368 JI |
1621 | .cra_blocksize = DES3_EDE_BLOCK_SIZE, |
1622 | .cra_ctxsize = sizeof(struct spacc_aead_ctx), | |
1623 | .cra_type = &crypto_aead_type, | |
1624 | .cra_module = THIS_MODULE, | |
1625 | .cra_aead = { | |
1626 | .setkey = spacc_aead_setkey, | |
1627 | .setauthsize = spacc_aead_setauthsize, | |
1628 | .encrypt = spacc_aead_encrypt, | |
1629 | .decrypt = spacc_aead_decrypt, | |
1630 | .givencrypt = spacc_aead_givencrypt, | |
1631 | .ivsize = DES3_EDE_BLOCK_SIZE, | |
1632 | .maxauthsize = MD5_DIGEST_SIZE, | |
1633 | }, | |
1634 | .cra_init = spacc_aead_cra_init, | |
1635 | .cra_exit = spacc_aead_cra_exit, | |
1636 | }, | |
1637 | }, | |
1638 | }; | |
1639 | ||
1640 | static struct spacc_alg l2_engine_algs[] = { | |
1641 | { | |
1642 | .key_offs = 0, | |
1643 | .iv_offs = SPACC_CRYPTO_KASUMI_F8_KEY_LEN, | |
1644 | .ctrl_default = SPA_CTRL_CIPH_ALG_KASUMI | | |
1645 | SPA_CTRL_CIPH_MODE_F8, | |
1646 | .alg = { | |
1647 | .cra_name = "f8(kasumi)", | |
1648 | .cra_driver_name = "f8-kasumi-picoxcell", | |
1649 | .cra_priority = SPACC_CRYPTO_ALG_PRIORITY, | |
d912bb76 NM |
1650 | .cra_flags = CRYPTO_ALG_TYPE_GIVCIPHER | |
1651 | CRYPTO_ALG_ASYNC | | |
1652 | CRYPTO_ALG_KERN_DRIVER_ONLY, | |
ce921368 JI |
1653 | .cra_blocksize = 8, |
1654 | .cra_ctxsize = sizeof(struct spacc_ablk_ctx), | |
1655 | .cra_type = &crypto_ablkcipher_type, | |
1656 | .cra_module = THIS_MODULE, | |
1657 | .cra_ablkcipher = { | |
1658 | .setkey = spacc_kasumi_f8_setkey, | |
1659 | .encrypt = spacc_ablk_encrypt, | |
1660 | .decrypt = spacc_ablk_decrypt, | |
1661 | .min_keysize = 16, | |
1662 | .max_keysize = 16, | |
1663 | .ivsize = 8, | |
1664 | }, | |
1665 | .cra_init = spacc_ablk_cra_init, | |
1666 | .cra_exit = spacc_ablk_cra_exit, | |
1667 | }, | |
1668 | }, | |
1669 | }; | |
1670 | ||
30343ef1 JI |
1671 | #ifdef CONFIG_OF |
1672 | static const struct of_device_id spacc_of_id_table[] = { | |
1673 | { .compatible = "picochip,spacc-ipsec" }, | |
1674 | { .compatible = "picochip,spacc-l2" }, | |
1675 | {} | |
1676 | }; | |
30343ef1 JI |
1677 | #endif /* CONFIG_OF */ |
1678 | ||
1679 | static bool spacc_is_compatible(struct platform_device *pdev, | |
1680 | const char *spacc_type) | |
1681 | { | |
1682 | const struct platform_device_id *platid = platform_get_device_id(pdev); | |
1683 | ||
1684 | if (platid && !strcmp(platid->name, spacc_type)) | |
1685 | return true; | |
1686 | ||
1687 | #ifdef CONFIG_OF | |
1688 | if (of_device_is_compatible(pdev->dev.of_node, spacc_type)) | |
1689 | return true; | |
1690 | #endif /* CONFIG_OF */ | |
1691 | ||
1692 | return false; | |
1693 | } | |
1694 | ||
49cfe4db | 1695 | static int spacc_probe(struct platform_device *pdev) |
ce921368 JI |
1696 | { |
1697 | int i, err, ret = -EINVAL; | |
1698 | struct resource *mem, *irq; | |
1699 | struct spacc_engine *engine = devm_kzalloc(&pdev->dev, sizeof(*engine), | |
1700 | GFP_KERNEL); | |
1701 | if (!engine) | |
1702 | return -ENOMEM; | |
1703 | ||
30343ef1 | 1704 | if (spacc_is_compatible(pdev, "picochip,spacc-ipsec")) { |
c3f4200f JI |
1705 | engine->max_ctxs = SPACC_CRYPTO_IPSEC_MAX_CTXS; |
1706 | engine->cipher_pg_sz = SPACC_CRYPTO_IPSEC_CIPHER_PG_SZ; | |
1707 | engine->hash_pg_sz = SPACC_CRYPTO_IPSEC_HASH_PG_SZ; | |
1708 | engine->fifo_sz = SPACC_CRYPTO_IPSEC_FIFO_SZ; | |
1709 | engine->algs = ipsec_engine_algs; | |
1710 | engine->num_algs = ARRAY_SIZE(ipsec_engine_algs); | |
30343ef1 | 1711 | } else if (spacc_is_compatible(pdev, "picochip,spacc-l2")) { |
c3f4200f JI |
1712 | engine->max_ctxs = SPACC_CRYPTO_L2_MAX_CTXS; |
1713 | engine->cipher_pg_sz = SPACC_CRYPTO_L2_CIPHER_PG_SZ; | |
1714 | engine->hash_pg_sz = SPACC_CRYPTO_L2_HASH_PG_SZ; | |
1715 | engine->fifo_sz = SPACC_CRYPTO_L2_FIFO_SZ; | |
1716 | engine->algs = l2_engine_algs; | |
1717 | engine->num_algs = ARRAY_SIZE(l2_engine_algs); | |
1718 | } else { | |
1719 | return -EINVAL; | |
1720 | } | |
1721 | ||
1722 | engine->name = dev_name(&pdev->dev); | |
ce921368 JI |
1723 | |
1724 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
32af1e18 JH |
1725 | engine->regs = devm_ioremap_resource(&pdev->dev, mem); |
1726 | if (IS_ERR(engine->regs)) | |
1727 | return PTR_ERR(engine->regs); | |
1728 | ||
ce921368 | 1729 | irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
32af1e18 | 1730 | if (!irq) { |
ce921368 JI |
1731 | dev_err(&pdev->dev, "no memory/irq resource for engine\n"); |
1732 | return -ENXIO; | |
1733 | } | |
1734 | ||
ce921368 JI |
1735 | if (devm_request_irq(&pdev->dev, irq->start, spacc_spacc_irq, 0, |
1736 | engine->name, engine)) { | |
1737 | dev_err(engine->dev, "failed to request IRQ\n"); | |
1738 | return -EBUSY; | |
1739 | } | |
1740 | ||
1741 | engine->dev = &pdev->dev; | |
1742 | engine->cipher_ctx_base = engine->regs + SPA_CIPH_KEY_BASE_REG_OFFSET; | |
1743 | engine->hash_key_base = engine->regs + SPA_HASH_KEY_BASE_REG_OFFSET; | |
1744 | ||
1745 | engine->req_pool = dmam_pool_create(engine->name, engine->dev, | |
1746 | MAX_DDT_LEN * sizeof(struct spacc_ddt), 8, SZ_64K); | |
1747 | if (!engine->req_pool) | |
1748 | return -ENOMEM; | |
1749 | ||
1750 | spin_lock_init(&engine->hw_lock); | |
1751 | ||
4efae8c9 | 1752 | engine->clk = clk_get(&pdev->dev, "ref"); |
ce921368 JI |
1753 | if (IS_ERR(engine->clk)) { |
1754 | dev_info(&pdev->dev, "clk unavailable\n"); | |
1755 | device_remove_file(&pdev->dev, &dev_attr_stat_irq_thresh); | |
1756 | return PTR_ERR(engine->clk); | |
1757 | } | |
1758 | ||
1759 | if (clk_enable(engine->clk)) { | |
1760 | dev_info(&pdev->dev, "unable to enable clk\n"); | |
1761 | clk_put(engine->clk); | |
1762 | return -EIO; | |
1763 | } | |
1764 | ||
1765 | err = device_create_file(&pdev->dev, &dev_attr_stat_irq_thresh); | |
1766 | if (err) { | |
1767 | clk_disable(engine->clk); | |
1768 | clk_put(engine->clk); | |
1769 | return err; | |
1770 | } | |
1771 | ||
1772 | ||
1773 | /* | |
1774 | * Use an IRQ threshold of 50% as a default. This seems to be a | |
1775 | * reasonable trade off of latency against throughput but can be | |
1776 | * changed at runtime. | |
1777 | */ | |
1778 | engine->stat_irq_thresh = (engine->fifo_sz / 2); | |
1779 | ||
1780 | /* | |
1781 | * Configure the interrupts. We only use the STAT_CNT interrupt as we | |
1782 | * only submit a new packet for processing when we complete another in | |
1783 | * the queue. This minimizes time spent in the interrupt handler. | |
1784 | */ | |
1785 | writel(engine->stat_irq_thresh << SPA_IRQ_CTRL_STAT_CNT_OFFSET, | |
1786 | engine->regs + SPA_IRQ_CTRL_REG_OFFSET); | |
1787 | writel(SPA_IRQ_EN_STAT_EN | SPA_IRQ_EN_GLBL_EN, | |
1788 | engine->regs + SPA_IRQ_EN_REG_OFFSET); | |
1789 | ||
1790 | setup_timer(&engine->packet_timeout, spacc_packet_timeout, | |
1791 | (unsigned long)engine); | |
1792 | ||
1793 | INIT_LIST_HEAD(&engine->pending); | |
1794 | INIT_LIST_HEAD(&engine->completed); | |
1795 | INIT_LIST_HEAD(&engine->in_progress); | |
1796 | engine->in_flight = 0; | |
1797 | tasklet_init(&engine->complete, spacc_spacc_complete, | |
1798 | (unsigned long)engine); | |
1799 | ||
1800 | platform_set_drvdata(pdev, engine); | |
1801 | ||
1802 | INIT_LIST_HEAD(&engine->registered_algs); | |
1803 | for (i = 0; i < engine->num_algs; ++i) { | |
1804 | engine->algs[i].engine = engine; | |
1805 | err = crypto_register_alg(&engine->algs[i].alg); | |
1806 | if (!err) { | |
1807 | list_add_tail(&engine->algs[i].entry, | |
1808 | &engine->registered_algs); | |
1809 | ret = 0; | |
1810 | } | |
1811 | if (err) | |
1812 | dev_err(engine->dev, "failed to register alg \"%s\"\n", | |
1813 | engine->algs[i].alg.cra_name); | |
1814 | else | |
1815 | dev_dbg(engine->dev, "registered alg \"%s\"\n", | |
1816 | engine->algs[i].alg.cra_name); | |
1817 | } | |
1818 | ||
1819 | return ret; | |
1820 | } | |
1821 | ||
49cfe4db | 1822 | static int spacc_remove(struct platform_device *pdev) |
ce921368 JI |
1823 | { |
1824 | struct spacc_alg *alg, *next; | |
1825 | struct spacc_engine *engine = platform_get_drvdata(pdev); | |
1826 | ||
1827 | del_timer_sync(&engine->packet_timeout); | |
1828 | device_remove_file(&pdev->dev, &dev_attr_stat_irq_thresh); | |
1829 | ||
1830 | list_for_each_entry_safe(alg, next, &engine->registered_algs, entry) { | |
1831 | list_del(&alg->entry); | |
1832 | crypto_unregister_alg(&alg->alg); | |
1833 | } | |
1834 | ||
1835 | clk_disable(engine->clk); | |
1836 | clk_put(engine->clk); | |
1837 | ||
1838 | return 0; | |
1839 | } | |
1840 | ||
c3f4200f JI |
1841 | static const struct platform_device_id spacc_id_table[] = { |
1842 | { "picochip,spacc-ipsec", }, | |
1843 | { "picochip,spacc-l2", }, | |
14198dd6 | 1844 | { } |
ce921368 JI |
1845 | }; |
1846 | ||
c3f4200f JI |
1847 | static struct platform_driver spacc_driver = { |
1848 | .probe = spacc_probe, | |
49cfe4db | 1849 | .remove = spacc_remove, |
ce921368 | 1850 | .driver = { |
c3f4200f | 1851 | .name = "picochip,spacc", |
ce921368 JI |
1852 | #ifdef CONFIG_PM |
1853 | .pm = &spacc_pm_ops, | |
1854 | #endif /* CONFIG_PM */ | |
5cec26e9 | 1855 | .of_match_table = of_match_ptr(spacc_of_id_table), |
ce921368 | 1856 | }, |
c3f4200f | 1857 | .id_table = spacc_id_table, |
ce921368 JI |
1858 | }; |
1859 | ||
741e8c2d | 1860 | module_platform_driver(spacc_driver); |
ce921368 JI |
1861 | |
1862 | MODULE_LICENSE("GPL"); | |
1863 | MODULE_AUTHOR("Jamie Iles"); |