2 * Copyright (c) 2010-2011 Picochip Ltd., Jamie Iles
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.
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.
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
18 #include <crypto/internal/aead.h>
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>
35 #include <linux/list.h>
36 #include <linux/module.h>
38 #include <linux/platform_device.h>
40 #include <linux/rtnetlink.h>
41 #include <linux/scatterlist.h>
42 #include <linux/sched.h>
43 #include <linux/sizes.h>
44 #include <linux/slab.h>
45 #include <linux/timer.h>
47 #include "picoxcell_crypto_regs.h"
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.
54 #define CMD0_IRQ_THRESHOLD 1
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.
63 #define PACKET_TIMEOUT 1
65 /* The priority to register each algorithm with. */
66 #define SPACC_CRYPTO_ALG_PRIORITY 10000
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
78 #define MAX_DDT_LEN 16
80 /* DDT format. This must match the hardware DDT format exactly. */
87 * Asynchronous crypto request structure.
89 * This structure defines a request that is either queued for processing or
93 struct list_head list;
94 struct spacc_engine *engine;
95 struct crypto_async_request *req;
99 dma_addr_t src_addr, dst_addr;
100 struct spacc_ddt *src_ddt, *dst_ddt;
101 void (*complete)(struct spacc_req *req);
103 /* AEAD specific bits. */
109 struct spacc_engine {
111 struct list_head pending;
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;
123 struct list_head registered_algs;
130 struct timer_list packet_timeout;
131 unsigned stat_irq_thresh;
132 struct dma_pool *req_pool;
135 /* Algorithm type mask. */
136 #define SPACC_CRYPTO_ALG_MASK 0x7
138 /* SPACC definition of a crypto algorithm. */
140 unsigned long ctrl_default;
142 struct crypto_alg alg;
143 struct spacc_engine *engine;
144 struct list_head entry;
149 /* Generic context structure for any algorithm type. */
150 struct spacc_generic_ctx {
151 struct spacc_engine *engine;
157 /* Block cipher context. */
158 struct spacc_ablk_ctx {
159 struct spacc_generic_ctx generic;
160 u8 key[AES_MAX_KEY_SIZE];
163 * The fallback cipher. If the operation can't be done in hardware,
164 * fallback to a software version.
166 struct crypto_ablkcipher *sw_cipher;
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];
176 struct crypto_aead *sw_cipher;
178 u8 salt[AES_BLOCK_SIZE];
181 static int spacc_ablk_submit(struct spacc_req *req);
183 static inline struct spacc_alg *to_spacc_alg(struct crypto_alg *alg)
185 return alg ? container_of(alg, struct spacc_alg, alg) : NULL;
188 static inline int spacc_fifo_cmd_full(struct spacc_engine *engine)
190 u32 fifo_stat = readl(engine->regs + SPA_FIFO_STAT_REG_OFFSET);
192 return fifo_stat & SPA_FIFO_CMD_FULL;
196 * Given a cipher context, and a context number, get the base address of the
199 * Returns the address of the context page where the key/context may
202 static inline void __iomem *spacc_ctx_page_addr(struct spacc_generic_ctx *ctx,
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);
211 /* The context pages can only be written with 32-bit accesses. */
212 static inline void memcpy_toio32(u32 __iomem *dst, const void *src,
215 const u32 *src32 = (const u32 *) src;
218 writel(*src32++, dst++);
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)
225 void __iomem *key_ptr = page_addr + ctx->key_offs;
226 void __iomem *iv_ptr = page_addr + ctx->iv_offs;
228 memcpy_toio32(key_ptr, key, key_len / 4);
229 memcpy_toio32(iv_ptr, iv, iv_len / 4);
233 * Load a context into the engines context memory.
235 * Returns the index of the context page where the context was loaded.
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,
242 unsigned indx = ctx->engine->next_ctx++;
243 void __iomem *ciph_page_addr, *hash_page_addr;
245 ciph_page_addr = spacc_ctx_page_addr(ctx, indx, 1);
246 hash_page_addr = spacc_ctx_page_addr(ctx, indx, 0);
248 ctx->engine->next_ctx &= ctx->engine->fifo_sz - 1;
249 spacc_cipher_write_ctx(ctx, ciph_page_addr, ciph_key, ciph_len, iv,
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);
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);
264 /* Count the number of scatterlist entries in a scatterlist. */
265 static inline int sg_count(struct scatterlist *sg_list, int nbytes)
267 return sg_nents_for_len(sg_list, nbytes);
270 static inline void ddt_set(struct spacc_ddt *ddt, dma_addr_t phys, size_t len)
277 * Take a crypto request and scatterlists for the data and turn them into DDTs
278 * for passing to the crypto engines. This also DMA maps the data so that the
279 * crypto engines can DMA to/from them.
281 static struct spacc_ddt *spacc_sg_to_ddt(struct spacc_engine *engine,
282 struct scatterlist *payload,
284 enum dma_data_direction dir,
285 dma_addr_t *ddt_phys)
287 unsigned nents, mapped_ents;
288 struct scatterlist *cur;
289 struct spacc_ddt *ddt;
292 nents = sg_count(payload, nbytes);
293 mapped_ents = dma_map_sg(engine->dev, payload, nents, dir);
295 if (mapped_ents + 1 > MAX_DDT_LEN)
298 ddt = dma_pool_alloc(engine->req_pool, GFP_ATOMIC, ddt_phys);
302 for_each_sg(payload, cur, mapped_ents, i)
303 ddt_set(&ddt[i], sg_dma_address(cur), sg_dma_len(cur));
304 ddt_set(&ddt[mapped_ents], 0, 0);
309 dma_unmap_sg(engine->dev, payload, nents, dir);
313 static int spacc_aead_make_ddts(struct spacc_req *req, u8 *giv)
315 struct aead_request *areq = container_of(req->req, struct aead_request,
317 struct spacc_engine *engine = req->engine;
318 struct spacc_ddt *src_ddt, *dst_ddt;
319 unsigned ivsize = crypto_aead_ivsize(crypto_aead_reqtfm(areq));
320 unsigned nents = sg_count(areq->src, areq->cryptlen);
322 struct scatterlist *cur;
323 int i, dst_ents, src_ents, assoc_ents;
324 u8 *iv = giv ? giv : areq->iv;
326 src_ddt = dma_pool_alloc(engine->req_pool, GFP_ATOMIC, &req->src_addr);
330 dst_ddt = dma_pool_alloc(engine->req_pool, GFP_ATOMIC, &req->dst_addr);
332 dma_pool_free(engine->req_pool, src_ddt, req->src_addr);
336 req->src_ddt = src_ddt;
337 req->dst_ddt = dst_ddt;
339 assoc_ents = dma_map_sg(engine->dev, areq->assoc,
340 sg_count(areq->assoc, areq->assoclen), DMA_TO_DEVICE);
341 if (areq->src != areq->dst) {
342 src_ents = dma_map_sg(engine->dev, areq->src, nents,
344 dst_ents = dma_map_sg(engine->dev, areq->dst, nents,
347 src_ents = dma_map_sg(engine->dev, areq->src, nents,
353 * Map the IV/GIV. For the GIV it needs to be bidirectional as it is
354 * formed by the crypto block and sent as the ESP IV for IPSEC.
356 iv_addr = dma_map_single(engine->dev, iv, ivsize,
357 giv ? DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
358 req->giv_pa = iv_addr;
361 * Map the associated data. For decryption we don't copy the
364 for_each_sg(areq->assoc, cur, assoc_ents, i) {
365 ddt_set(src_ddt++, sg_dma_address(cur), sg_dma_len(cur));
367 ddt_set(dst_ddt++, sg_dma_address(cur),
370 ddt_set(src_ddt++, iv_addr, ivsize);
372 if (giv || req->is_encrypt)
373 ddt_set(dst_ddt++, iv_addr, ivsize);
376 * Now map in the payload for the source and destination and terminate
377 * with the NULL pointers.
379 for_each_sg(areq->src, cur, src_ents, i) {
380 ddt_set(src_ddt++, sg_dma_address(cur), sg_dma_len(cur));
381 if (areq->src == areq->dst)
382 ddt_set(dst_ddt++, sg_dma_address(cur),
386 for_each_sg(areq->dst, cur, dst_ents, i)
387 ddt_set(dst_ddt++, sg_dma_address(cur),
390 ddt_set(src_ddt, 0, 0);
391 ddt_set(dst_ddt, 0, 0);
396 static void spacc_aead_free_ddts(struct spacc_req *req)
398 struct aead_request *areq = container_of(req->req, struct aead_request,
400 struct spacc_alg *alg = to_spacc_alg(req->req->tfm->__crt_alg);
401 struct spacc_ablk_ctx *aead_ctx = crypto_tfm_ctx(req->req->tfm);
402 struct spacc_engine *engine = aead_ctx->generic.engine;
403 unsigned ivsize = alg->alg.cra_aead.ivsize;
404 unsigned nents = sg_count(areq->src, areq->cryptlen);
406 if (areq->src != areq->dst) {
407 dma_unmap_sg(engine->dev, areq->src, nents, DMA_TO_DEVICE);
408 dma_unmap_sg(engine->dev, areq->dst,
409 sg_count(areq->dst, areq->cryptlen),
412 dma_unmap_sg(engine->dev, areq->src, nents, DMA_BIDIRECTIONAL);
414 dma_unmap_sg(engine->dev, areq->assoc,
415 sg_count(areq->assoc, areq->assoclen), DMA_TO_DEVICE);
417 dma_unmap_single(engine->dev, req->giv_pa, ivsize, DMA_BIDIRECTIONAL);
419 dma_pool_free(engine->req_pool, req->src_ddt, req->src_addr);
420 dma_pool_free(engine->req_pool, req->dst_ddt, req->dst_addr);
423 static void spacc_free_ddt(struct spacc_req *req, struct spacc_ddt *ddt,
424 dma_addr_t ddt_addr, struct scatterlist *payload,
425 unsigned nbytes, enum dma_data_direction dir)
427 unsigned nents = sg_count(payload, nbytes);
429 dma_unmap_sg(req->engine->dev, payload, nents, dir);
430 dma_pool_free(req->engine->req_pool, ddt, ddt_addr);
434 * Set key for a DES operation in an AEAD cipher. This also performs weak key
435 * checking if required.
437 static int spacc_aead_des_setkey(struct crypto_aead *aead, const u8 *key,
440 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
441 struct spacc_aead_ctx *ctx = crypto_tfm_ctx(tfm);
442 u32 tmp[DES_EXPKEY_WORDS];
444 if (unlikely(!des_ekey(tmp, key)) &&
445 (crypto_aead_get_flags(aead)) & CRYPTO_TFM_REQ_WEAK_KEY) {
446 tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
450 memcpy(ctx->cipher_key, key, len);
451 ctx->cipher_key_len = len;
456 /* Set the key for the AES block cipher component of the AEAD transform. */
457 static int spacc_aead_aes_setkey(struct crypto_aead *aead, const u8 *key,
460 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
461 struct spacc_aead_ctx *ctx = crypto_tfm_ctx(tfm);
464 * IPSec engine only supports 128 and 256 bit AES keys. If we get a
465 * request for any other size (192 bits) then we need to do a software
468 if (len != AES_KEYSIZE_128 && len != AES_KEYSIZE_256) {
470 * Set the fallback transform to use the same request flags as
471 * the hardware transform.
473 ctx->sw_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
474 ctx->sw_cipher->base.crt_flags |=
475 tfm->crt_flags & CRYPTO_TFM_REQ_MASK;
476 return crypto_aead_setkey(ctx->sw_cipher, key, len);
479 memcpy(ctx->cipher_key, key, len);
480 ctx->cipher_key_len = len;
485 static int spacc_aead_setkey(struct crypto_aead *tfm, const u8 *key,
488 struct spacc_aead_ctx *ctx = crypto_aead_ctx(tfm);
489 struct spacc_alg *alg = to_spacc_alg(tfm->base.__crt_alg);
490 struct crypto_authenc_keys keys;
493 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
496 if (keys.enckeylen > AES_MAX_KEY_SIZE)
499 if (keys.authkeylen > sizeof(ctx->hash_ctx))
502 if ((alg->ctrl_default & SPACC_CRYPTO_ALG_MASK) ==
503 SPA_CTRL_CIPH_ALG_AES)
504 err = spacc_aead_aes_setkey(tfm, keys.enckey, keys.enckeylen);
506 err = spacc_aead_des_setkey(tfm, keys.enckey, keys.enckeylen);
511 memcpy(ctx->hash_ctx, keys.authkey, keys.authkeylen);
512 ctx->hash_key_len = keys.authkeylen;
517 crypto_aead_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
521 static int spacc_aead_setauthsize(struct crypto_aead *tfm,
522 unsigned int authsize)
524 struct spacc_aead_ctx *ctx = crypto_tfm_ctx(crypto_aead_tfm(tfm));
526 ctx->auth_size = authsize;
532 * Check if an AEAD request requires a fallback operation. Some requests can't
533 * be completed in hardware because the hardware may not support certain key
534 * sizes. In these cases we need to complete the request in software.
536 static int spacc_aead_need_fallback(struct spacc_req *req)
538 struct aead_request *aead_req;
539 struct crypto_tfm *tfm = req->req->tfm;
540 struct crypto_alg *alg = req->req->tfm->__crt_alg;
541 struct spacc_alg *spacc_alg = to_spacc_alg(alg);
542 struct spacc_aead_ctx *ctx = crypto_tfm_ctx(tfm);
544 aead_req = container_of(req->req, struct aead_request, base);
546 * If we have a non-supported key-length, then we need to do a
549 if ((spacc_alg->ctrl_default & SPACC_CRYPTO_ALG_MASK) ==
550 SPA_CTRL_CIPH_ALG_AES &&
551 ctx->cipher_key_len != AES_KEYSIZE_128 &&
552 ctx->cipher_key_len != AES_KEYSIZE_256)
558 static int spacc_aead_do_fallback(struct aead_request *req, unsigned alg_type,
561 struct crypto_tfm *old_tfm = crypto_aead_tfm(crypto_aead_reqtfm(req));
562 struct spacc_aead_ctx *ctx = crypto_tfm_ctx(old_tfm);
565 if (ctx->sw_cipher) {
567 * Change the request to use the software fallback transform,
568 * and once the ciphering has completed, put the old transform
569 * back into the request.
571 aead_request_set_tfm(req, ctx->sw_cipher);
572 err = is_encrypt ? crypto_aead_encrypt(req) :
573 crypto_aead_decrypt(req);
574 aead_request_set_tfm(req, __crypto_aead_cast(old_tfm));
581 static void spacc_aead_complete(struct spacc_req *req)
583 spacc_aead_free_ddts(req);
584 req->req->complete(req->req, req->result);
587 static int spacc_aead_submit(struct spacc_req *req)
589 struct crypto_tfm *tfm = req->req->tfm;
590 struct spacc_aead_ctx *ctx = crypto_tfm_ctx(tfm);
591 struct crypto_alg *alg = req->req->tfm->__crt_alg;
592 struct spacc_alg *spacc_alg = to_spacc_alg(alg);
593 struct spacc_engine *engine = ctx->generic.engine;
594 u32 ctrl, proc_len, assoc_len;
595 struct aead_request *aead_req =
596 container_of(req->req, struct aead_request, base);
598 req->result = -EINPROGRESS;
599 req->ctx_id = spacc_load_ctx(&ctx->generic, ctx->cipher_key,
600 ctx->cipher_key_len, aead_req->iv, alg->cra_aead.ivsize,
601 ctx->hash_ctx, ctx->hash_key_len);
603 /* Set the source and destination DDT pointers. */
604 writel(req->src_addr, engine->regs + SPA_SRC_PTR_REG_OFFSET);
605 writel(req->dst_addr, engine->regs + SPA_DST_PTR_REG_OFFSET);
606 writel(0, engine->regs + SPA_OFFSET_REG_OFFSET);
608 assoc_len = aead_req->assoclen;
609 proc_len = aead_req->cryptlen + assoc_len;
612 * If we aren't generating an IV, then we need to include the IV in the
613 * associated data so that it is included in the hash.
616 assoc_len += crypto_aead_ivsize(crypto_aead_reqtfm(aead_req));
617 proc_len += crypto_aead_ivsize(crypto_aead_reqtfm(aead_req));
619 proc_len += req->giv_len;
622 * If we are decrypting, we need to take the length of the ICV out of
623 * the processing length.
625 if (!req->is_encrypt)
626 proc_len -= ctx->auth_size;
628 writel(proc_len, engine->regs + SPA_PROC_LEN_REG_OFFSET);
629 writel(assoc_len, engine->regs + SPA_AAD_LEN_REG_OFFSET);
630 writel(ctx->auth_size, engine->regs + SPA_ICV_LEN_REG_OFFSET);
631 writel(0, engine->regs + SPA_ICV_OFFSET_REG_OFFSET);
632 writel(0, engine->regs + SPA_AUX_INFO_REG_OFFSET);
634 ctrl = spacc_alg->ctrl_default | (req->ctx_id << SPA_CTRL_CTX_IDX) |
635 (1 << SPA_CTRL_ICV_APPEND);
637 ctrl |= (1 << SPA_CTRL_ENCRYPT_IDX) | (1 << SPA_CTRL_AAD_COPY);
639 ctrl |= (1 << SPA_CTRL_KEY_EXP);
641 mod_timer(&engine->packet_timeout, jiffies + PACKET_TIMEOUT);
643 writel(ctrl, engine->regs + SPA_CTRL_REG_OFFSET);
648 static int spacc_req_submit(struct spacc_req *req);
650 static void spacc_push(struct spacc_engine *engine)
652 struct spacc_req *req;
654 while (!list_empty(&engine->pending) &&
655 engine->in_flight + 1 <= engine->fifo_sz) {
658 req = list_first_entry(&engine->pending, struct spacc_req,
660 list_move_tail(&req->list, &engine->in_progress);
662 req->result = spacc_req_submit(req);
667 * Setup an AEAD request for processing. This will configure the engine, load
668 * the context and then start the packet processing.
670 * @giv Pointer to destination address for a generated IV. If the
671 * request does not need to generate an IV then this should be set to NULL.
673 static int spacc_aead_setup(struct aead_request *req, u8 *giv,
674 unsigned alg_type, bool is_encrypt)
676 struct crypto_alg *alg = req->base.tfm->__crt_alg;
677 struct spacc_engine *engine = to_spacc_alg(alg)->engine;
678 struct spacc_req *dev_req = aead_request_ctx(req);
679 int err = -EINPROGRESS;
681 unsigned ivsize = crypto_aead_ivsize(crypto_aead_reqtfm(req));
684 dev_req->giv_len = ivsize;
685 dev_req->req = &req->base;
686 dev_req->is_encrypt = is_encrypt;
687 dev_req->result = -EBUSY;
688 dev_req->engine = engine;
689 dev_req->complete = spacc_aead_complete;
691 if (unlikely(spacc_aead_need_fallback(dev_req)))
692 return spacc_aead_do_fallback(req, alg_type, is_encrypt);
694 spacc_aead_make_ddts(dev_req, dev_req->giv);
697 spin_lock_irqsave(&engine->hw_lock, flags);
698 if (unlikely(spacc_fifo_cmd_full(engine)) ||
699 engine->in_flight + 1 > engine->fifo_sz) {
700 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
702 spin_unlock_irqrestore(&engine->hw_lock, flags);
705 list_add_tail(&dev_req->list, &engine->pending);
707 list_add_tail(&dev_req->list, &engine->pending);
710 spin_unlock_irqrestore(&engine->hw_lock, flags);
715 spacc_aead_free_ddts(dev_req);
720 static int spacc_aead_encrypt(struct aead_request *req)
722 struct crypto_aead *aead = crypto_aead_reqtfm(req);
723 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
724 struct spacc_alg *alg = to_spacc_alg(tfm->__crt_alg);
726 return spacc_aead_setup(req, NULL, alg->type, 1);
729 static int spacc_aead_givencrypt(struct aead_givcrypt_request *req)
731 struct crypto_aead *tfm = aead_givcrypt_reqtfm(req);
732 struct spacc_aead_ctx *ctx = crypto_aead_ctx(tfm);
733 size_t ivsize = crypto_aead_ivsize(tfm);
734 struct spacc_alg *alg = to_spacc_alg(tfm->base.__crt_alg);
738 memcpy(req->areq.iv, ctx->salt, ivsize);
740 if (ivsize > sizeof(u64)) {
741 memset(req->giv, 0, ivsize - sizeof(u64));
744 seq = cpu_to_be64(req->seq);
745 memcpy(req->giv + ivsize - len, &seq, len);
747 return spacc_aead_setup(&req->areq, req->giv, alg->type, 1);
750 static int spacc_aead_decrypt(struct aead_request *req)
752 struct crypto_aead *aead = crypto_aead_reqtfm(req);
753 struct crypto_tfm *tfm = crypto_aead_tfm(aead);
754 struct spacc_alg *alg = to_spacc_alg(tfm->__crt_alg);
756 return spacc_aead_setup(req, NULL, alg->type, 0);
760 * Initialise a new AEAD context. This is responsible for allocating the
761 * fallback cipher and initialising the context.
763 static int spacc_aead_cra_init(struct crypto_tfm *tfm)
765 struct spacc_aead_ctx *ctx = crypto_tfm_ctx(tfm);
766 struct crypto_alg *alg = tfm->__crt_alg;
767 struct spacc_alg *spacc_alg = to_spacc_alg(alg);
768 struct spacc_engine *engine = spacc_alg->engine;
770 ctx->generic.flags = spacc_alg->type;
771 ctx->generic.engine = engine;
772 ctx->sw_cipher = crypto_alloc_aead(alg->cra_name, 0,
774 CRYPTO_ALG_NEED_FALLBACK);
775 if (IS_ERR(ctx->sw_cipher)) {
776 dev_warn(engine->dev, "failed to allocate fallback for %s\n",
778 ctx->sw_cipher = NULL;
780 ctx->generic.key_offs = spacc_alg->key_offs;
781 ctx->generic.iv_offs = spacc_alg->iv_offs;
783 get_random_bytes(ctx->salt, sizeof(ctx->salt));
785 crypto_aead_set_reqsize(__crypto_aead_cast(tfm),
786 sizeof(struct spacc_req));
792 * Destructor for an AEAD context. This is called when the transform is freed
793 * and must free the fallback cipher.
795 static void spacc_aead_cra_exit(struct crypto_tfm *tfm)
797 struct spacc_aead_ctx *ctx = crypto_tfm_ctx(tfm);
800 crypto_free_aead(ctx->sw_cipher);
801 ctx->sw_cipher = NULL;
805 * Set the DES key for a block cipher transform. This also performs weak key
806 * checking if the transform has requested it.
808 static int spacc_des_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
811 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
812 struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
813 u32 tmp[DES_EXPKEY_WORDS];
815 if (len > DES3_EDE_KEY_SIZE) {
816 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
820 if (unlikely(!des_ekey(tmp, key)) &&
821 (crypto_ablkcipher_get_flags(cipher) & CRYPTO_TFM_REQ_WEAK_KEY)) {
822 tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
826 memcpy(ctx->key, key, len);
833 * Set the key for an AES block cipher. Some key lengths are not supported in
834 * hardware so this must also check whether a fallback is needed.
836 static int spacc_aes_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
839 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
840 struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
843 if (len > AES_MAX_KEY_SIZE) {
844 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
849 * IPSec engine only supports 128 and 256 bit AES keys. If we get a
850 * request for any other size (192 bits) then we need to do a software
853 if (len != AES_KEYSIZE_128 && len != AES_KEYSIZE_256 &&
856 * Set the fallback transform to use the same request flags as
857 * the hardware transform.
859 ctx->sw_cipher->base.crt_flags &= ~CRYPTO_TFM_REQ_MASK;
860 ctx->sw_cipher->base.crt_flags |=
861 cipher->base.crt_flags & CRYPTO_TFM_REQ_MASK;
863 err = crypto_ablkcipher_setkey(ctx->sw_cipher, key, len);
865 goto sw_setkey_failed;
866 } else if (len != AES_KEYSIZE_128 && len != AES_KEYSIZE_256 &&
870 memcpy(ctx->key, key, len);
874 if (err && ctx->sw_cipher) {
875 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
877 ctx->sw_cipher->base.crt_flags & CRYPTO_TFM_RES_MASK;
883 static int spacc_kasumi_f8_setkey(struct crypto_ablkcipher *cipher,
884 const u8 *key, unsigned int len)
886 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
887 struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
890 if (len > AES_MAX_KEY_SIZE) {
891 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
896 memcpy(ctx->key, key, len);
903 static int spacc_ablk_need_fallback(struct spacc_req *req)
905 struct spacc_ablk_ctx *ctx;
906 struct crypto_tfm *tfm = req->req->tfm;
907 struct crypto_alg *alg = req->req->tfm->__crt_alg;
908 struct spacc_alg *spacc_alg = to_spacc_alg(alg);
910 ctx = crypto_tfm_ctx(tfm);
912 return (spacc_alg->ctrl_default & SPACC_CRYPTO_ALG_MASK) ==
913 SPA_CTRL_CIPH_ALG_AES &&
914 ctx->key_len != AES_KEYSIZE_128 &&
915 ctx->key_len != AES_KEYSIZE_256;
918 static void spacc_ablk_complete(struct spacc_req *req)
920 struct ablkcipher_request *ablk_req =
921 container_of(req->req, struct ablkcipher_request, base);
923 if (ablk_req->src != ablk_req->dst) {
924 spacc_free_ddt(req, req->src_ddt, req->src_addr, ablk_req->src,
925 ablk_req->nbytes, DMA_TO_DEVICE);
926 spacc_free_ddt(req, req->dst_ddt, req->dst_addr, ablk_req->dst,
927 ablk_req->nbytes, DMA_FROM_DEVICE);
929 spacc_free_ddt(req, req->dst_ddt, req->dst_addr, ablk_req->dst,
930 ablk_req->nbytes, DMA_BIDIRECTIONAL);
932 req->req->complete(req->req, req->result);
935 static int spacc_ablk_submit(struct spacc_req *req)
937 struct crypto_tfm *tfm = req->req->tfm;
938 struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
939 struct ablkcipher_request *ablk_req = ablkcipher_request_cast(req->req);
940 struct crypto_alg *alg = req->req->tfm->__crt_alg;
941 struct spacc_alg *spacc_alg = to_spacc_alg(alg);
942 struct spacc_engine *engine = ctx->generic.engine;
945 req->ctx_id = spacc_load_ctx(&ctx->generic, ctx->key,
946 ctx->key_len, ablk_req->info, alg->cra_ablkcipher.ivsize,
949 writel(req->src_addr, engine->regs + SPA_SRC_PTR_REG_OFFSET);
950 writel(req->dst_addr, engine->regs + SPA_DST_PTR_REG_OFFSET);
951 writel(0, engine->regs + SPA_OFFSET_REG_OFFSET);
953 writel(ablk_req->nbytes, engine->regs + SPA_PROC_LEN_REG_OFFSET);
954 writel(0, engine->regs + SPA_ICV_OFFSET_REG_OFFSET);
955 writel(0, engine->regs + SPA_AUX_INFO_REG_OFFSET);
956 writel(0, engine->regs + SPA_AAD_LEN_REG_OFFSET);
958 ctrl = spacc_alg->ctrl_default | (req->ctx_id << SPA_CTRL_CTX_IDX) |
959 (req->is_encrypt ? (1 << SPA_CTRL_ENCRYPT_IDX) :
960 (1 << SPA_CTRL_KEY_EXP));
962 mod_timer(&engine->packet_timeout, jiffies + PACKET_TIMEOUT);
964 writel(ctrl, engine->regs + SPA_CTRL_REG_OFFSET);
969 static int spacc_ablk_do_fallback(struct ablkcipher_request *req,
970 unsigned alg_type, bool is_encrypt)
972 struct crypto_tfm *old_tfm =
973 crypto_ablkcipher_tfm(crypto_ablkcipher_reqtfm(req));
974 struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(old_tfm);
981 * Change the request to use the software fallback transform, and once
982 * the ciphering has completed, put the old transform back into the
985 ablkcipher_request_set_tfm(req, ctx->sw_cipher);
986 err = is_encrypt ? crypto_ablkcipher_encrypt(req) :
987 crypto_ablkcipher_decrypt(req);
988 ablkcipher_request_set_tfm(req, __crypto_ablkcipher_cast(old_tfm));
993 static int spacc_ablk_setup(struct ablkcipher_request *req, unsigned alg_type,
996 struct crypto_alg *alg = req->base.tfm->__crt_alg;
997 struct spacc_engine *engine = to_spacc_alg(alg)->engine;
998 struct spacc_req *dev_req = ablkcipher_request_ctx(req);
1002 dev_req->req = &req->base;
1003 dev_req->is_encrypt = is_encrypt;
1004 dev_req->engine = engine;
1005 dev_req->complete = spacc_ablk_complete;
1006 dev_req->result = -EINPROGRESS;
1008 if (unlikely(spacc_ablk_need_fallback(dev_req)))
1009 return spacc_ablk_do_fallback(req, alg_type, is_encrypt);
1012 * Create the DDT's for the engine. If we share the same source and
1013 * destination then we can optimize by reusing the DDT's.
1015 if (req->src != req->dst) {
1016 dev_req->src_ddt = spacc_sg_to_ddt(engine, req->src,
1017 req->nbytes, DMA_TO_DEVICE, &dev_req->src_addr);
1018 if (!dev_req->src_ddt)
1021 dev_req->dst_ddt = spacc_sg_to_ddt(engine, req->dst,
1022 req->nbytes, DMA_FROM_DEVICE, &dev_req->dst_addr);
1023 if (!dev_req->dst_ddt)
1026 dev_req->dst_ddt = spacc_sg_to_ddt(engine, req->dst,
1027 req->nbytes, DMA_BIDIRECTIONAL, &dev_req->dst_addr);
1028 if (!dev_req->dst_ddt)
1031 dev_req->src_ddt = NULL;
1032 dev_req->src_addr = dev_req->dst_addr;
1036 spin_lock_irqsave(&engine->hw_lock, flags);
1038 * Check if the engine will accept the operation now. If it won't then
1039 * we either stick it on the end of a pending list if we can backlog,
1040 * or bailout with an error if not.
1042 if (unlikely(spacc_fifo_cmd_full(engine)) ||
1043 engine->in_flight + 1 > engine->fifo_sz) {
1044 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
1046 spin_unlock_irqrestore(&engine->hw_lock, flags);
1049 list_add_tail(&dev_req->list, &engine->pending);
1051 list_add_tail(&dev_req->list, &engine->pending);
1054 spin_unlock_irqrestore(&engine->hw_lock, flags);
1059 spacc_free_ddt(dev_req, dev_req->dst_ddt, dev_req->dst_addr, req->dst,
1060 req->nbytes, req->src == req->dst ?
1061 DMA_BIDIRECTIONAL : DMA_FROM_DEVICE);
1063 if (req->src != req->dst)
1064 spacc_free_ddt(dev_req, dev_req->src_ddt, dev_req->src_addr,
1065 req->src, req->nbytes, DMA_TO_DEVICE);
1070 static int spacc_ablk_cra_init(struct crypto_tfm *tfm)
1072 struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
1073 struct crypto_alg *alg = tfm->__crt_alg;
1074 struct spacc_alg *spacc_alg = to_spacc_alg(alg);
1075 struct spacc_engine *engine = spacc_alg->engine;
1077 ctx->generic.flags = spacc_alg->type;
1078 ctx->generic.engine = engine;
1079 if (alg->cra_flags & CRYPTO_ALG_NEED_FALLBACK) {
1080 ctx->sw_cipher = crypto_alloc_ablkcipher(alg->cra_name, 0,
1081 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK);
1082 if (IS_ERR(ctx->sw_cipher)) {
1083 dev_warn(engine->dev, "failed to allocate fallback for %s\n",
1085 ctx->sw_cipher = NULL;
1088 ctx->generic.key_offs = spacc_alg->key_offs;
1089 ctx->generic.iv_offs = spacc_alg->iv_offs;
1091 tfm->crt_ablkcipher.reqsize = sizeof(struct spacc_req);
1096 static void spacc_ablk_cra_exit(struct crypto_tfm *tfm)
1098 struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
1101 crypto_free_ablkcipher(ctx->sw_cipher);
1102 ctx->sw_cipher = NULL;
1105 static int spacc_ablk_encrypt(struct ablkcipher_request *req)
1107 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(req);
1108 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
1109 struct spacc_alg *alg = to_spacc_alg(tfm->__crt_alg);
1111 return spacc_ablk_setup(req, alg->type, 1);
1114 static int spacc_ablk_decrypt(struct ablkcipher_request *req)
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);
1120 return spacc_ablk_setup(req, alg->type, 0);
1123 static inline int spacc_fifo_stat_empty(struct spacc_engine *engine)
1125 return readl(engine->regs + SPA_FIFO_STAT_REG_OFFSET) &
1126 SPA_FIFO_STAT_EMPTY;
1129 static void spacc_process_done(struct spacc_engine *engine)
1131 struct spacc_req *req;
1132 unsigned long flags;
1134 spin_lock_irqsave(&engine->hw_lock, flags);
1136 while (!spacc_fifo_stat_empty(engine)) {
1137 req = list_first_entry(&engine->in_progress, struct spacc_req,
1139 list_move_tail(&req->list, &engine->completed);
1140 --engine->in_flight;
1142 /* POP the status register. */
1143 writel(~0, engine->regs + SPA_STAT_POP_REG_OFFSET);
1144 req->result = (readl(engine->regs + SPA_STATUS_REG_OFFSET) &
1145 SPA_STATUS_RES_CODE_MASK) >> SPA_STATUS_RES_CODE_OFFSET;
1148 * Convert the SPAcc error status into the standard POSIX error
1151 if (unlikely(req->result)) {
1152 switch (req->result) {
1153 case SPA_STATUS_ICV_FAIL:
1154 req->result = -EBADMSG;
1157 case SPA_STATUS_MEMORY_ERROR:
1158 dev_warn(engine->dev,
1159 "memory error triggered\n");
1160 req->result = -EFAULT;
1163 case SPA_STATUS_BLOCK_ERROR:
1164 dev_warn(engine->dev,
1165 "block error triggered\n");
1172 tasklet_schedule(&engine->complete);
1174 spin_unlock_irqrestore(&engine->hw_lock, flags);
1177 static irqreturn_t spacc_spacc_irq(int irq, void *dev)
1179 struct spacc_engine *engine = (struct spacc_engine *)dev;
1180 u32 spacc_irq_stat = readl(engine->regs + SPA_IRQ_STAT_REG_OFFSET);
1182 writel(spacc_irq_stat, engine->regs + SPA_IRQ_STAT_REG_OFFSET);
1183 spacc_process_done(engine);
1188 static void spacc_packet_timeout(unsigned long data)
1190 struct spacc_engine *engine = (struct spacc_engine *)data;
1192 spacc_process_done(engine);
1195 static int spacc_req_submit(struct spacc_req *req)
1197 struct crypto_alg *alg = req->req->tfm->__crt_alg;
1199 if (CRYPTO_ALG_TYPE_AEAD == (CRYPTO_ALG_TYPE_MASK & alg->cra_flags))
1200 return spacc_aead_submit(req);
1202 return spacc_ablk_submit(req);
1205 static void spacc_spacc_complete(unsigned long data)
1207 struct spacc_engine *engine = (struct spacc_engine *)data;
1208 struct spacc_req *req, *tmp;
1209 unsigned long flags;
1210 LIST_HEAD(completed);
1212 spin_lock_irqsave(&engine->hw_lock, flags);
1214 list_splice_init(&engine->completed, &completed);
1216 if (engine->in_flight)
1217 mod_timer(&engine->packet_timeout, jiffies + PACKET_TIMEOUT);
1219 spin_unlock_irqrestore(&engine->hw_lock, flags);
1221 list_for_each_entry_safe(req, tmp, &completed, list) {
1222 list_del(&req->list);
1228 static int spacc_suspend(struct device *dev)
1230 struct platform_device *pdev = to_platform_device(dev);
1231 struct spacc_engine *engine = platform_get_drvdata(pdev);
1234 * We only support standby mode. All we have to do is gate the clock to
1235 * the spacc. The hardware will preserve state until we turn it back
1238 clk_disable(engine->clk);
1243 static int spacc_resume(struct device *dev)
1245 struct platform_device *pdev = to_platform_device(dev);
1246 struct spacc_engine *engine = platform_get_drvdata(pdev);
1248 return clk_enable(engine->clk);
1251 static const struct dev_pm_ops spacc_pm_ops = {
1252 .suspend = spacc_suspend,
1253 .resume = spacc_resume,
1255 #endif /* CONFIG_PM */
1257 static inline struct spacc_engine *spacc_dev_to_engine(struct device *dev)
1259 return dev ? platform_get_drvdata(to_platform_device(dev)) : NULL;
1262 static ssize_t spacc_stat_irq_thresh_show(struct device *dev,
1263 struct device_attribute *attr,
1266 struct spacc_engine *engine = spacc_dev_to_engine(dev);
1268 return snprintf(buf, PAGE_SIZE, "%u\n", engine->stat_irq_thresh);
1271 static ssize_t spacc_stat_irq_thresh_store(struct device *dev,
1272 struct device_attribute *attr,
1273 const char *buf, size_t len)
1275 struct spacc_engine *engine = spacc_dev_to_engine(dev);
1276 unsigned long thresh;
1278 if (kstrtoul(buf, 0, &thresh))
1281 thresh = clamp(thresh, 1UL, engine->fifo_sz - 1);
1283 engine->stat_irq_thresh = thresh;
1284 writel(engine->stat_irq_thresh << SPA_IRQ_CTRL_STAT_CNT_OFFSET,
1285 engine->regs + SPA_IRQ_CTRL_REG_OFFSET);
1289 static DEVICE_ATTR(stat_irq_thresh, 0644, spacc_stat_irq_thresh_show,
1290 spacc_stat_irq_thresh_store);
1292 static struct spacc_alg ipsec_engine_algs[] = {
1294 .ctrl_default = SPA_CTRL_CIPH_ALG_AES | SPA_CTRL_CIPH_MODE_CBC,
1296 .iv_offs = AES_MAX_KEY_SIZE,
1298 .cra_name = "cbc(aes)",
1299 .cra_driver_name = "cbc-aes-picoxcell",
1300 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1301 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
1302 CRYPTO_ALG_KERN_DRIVER_ONLY |
1304 CRYPTO_ALG_NEED_FALLBACK,
1305 .cra_blocksize = AES_BLOCK_SIZE,
1306 .cra_ctxsize = sizeof(struct spacc_ablk_ctx),
1307 .cra_type = &crypto_ablkcipher_type,
1308 .cra_module = THIS_MODULE,
1310 .setkey = spacc_aes_setkey,
1311 .encrypt = spacc_ablk_encrypt,
1312 .decrypt = spacc_ablk_decrypt,
1313 .min_keysize = AES_MIN_KEY_SIZE,
1314 .max_keysize = AES_MAX_KEY_SIZE,
1315 .ivsize = AES_BLOCK_SIZE,
1317 .cra_init = spacc_ablk_cra_init,
1318 .cra_exit = spacc_ablk_cra_exit,
1323 .iv_offs = AES_MAX_KEY_SIZE,
1324 .ctrl_default = SPA_CTRL_CIPH_ALG_AES | SPA_CTRL_CIPH_MODE_ECB,
1326 .cra_name = "ecb(aes)",
1327 .cra_driver_name = "ecb-aes-picoxcell",
1328 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1329 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
1330 CRYPTO_ALG_KERN_DRIVER_ONLY |
1331 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK,
1332 .cra_blocksize = AES_BLOCK_SIZE,
1333 .cra_ctxsize = sizeof(struct spacc_ablk_ctx),
1334 .cra_type = &crypto_ablkcipher_type,
1335 .cra_module = THIS_MODULE,
1337 .setkey = spacc_aes_setkey,
1338 .encrypt = spacc_ablk_encrypt,
1339 .decrypt = spacc_ablk_decrypt,
1340 .min_keysize = AES_MIN_KEY_SIZE,
1341 .max_keysize = AES_MAX_KEY_SIZE,
1343 .cra_init = spacc_ablk_cra_init,
1344 .cra_exit = spacc_ablk_cra_exit,
1348 .key_offs = DES_BLOCK_SIZE,
1350 .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_CBC,
1352 .cra_name = "cbc(des)",
1353 .cra_driver_name = "cbc-des-picoxcell",
1354 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1355 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
1357 CRYPTO_ALG_KERN_DRIVER_ONLY,
1358 .cra_blocksize = DES_BLOCK_SIZE,
1359 .cra_ctxsize = sizeof(struct spacc_ablk_ctx),
1360 .cra_type = &crypto_ablkcipher_type,
1361 .cra_module = THIS_MODULE,
1363 .setkey = spacc_des_setkey,
1364 .encrypt = spacc_ablk_encrypt,
1365 .decrypt = spacc_ablk_decrypt,
1366 .min_keysize = DES_KEY_SIZE,
1367 .max_keysize = DES_KEY_SIZE,
1368 .ivsize = DES_BLOCK_SIZE,
1370 .cra_init = spacc_ablk_cra_init,
1371 .cra_exit = spacc_ablk_cra_exit,
1375 .key_offs = DES_BLOCK_SIZE,
1377 .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_ECB,
1379 .cra_name = "ecb(des)",
1380 .cra_driver_name = "ecb-des-picoxcell",
1381 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1382 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
1384 CRYPTO_ALG_KERN_DRIVER_ONLY,
1385 .cra_blocksize = DES_BLOCK_SIZE,
1386 .cra_ctxsize = sizeof(struct spacc_ablk_ctx),
1387 .cra_type = &crypto_ablkcipher_type,
1388 .cra_module = THIS_MODULE,
1390 .setkey = spacc_des_setkey,
1391 .encrypt = spacc_ablk_encrypt,
1392 .decrypt = spacc_ablk_decrypt,
1393 .min_keysize = DES_KEY_SIZE,
1394 .max_keysize = DES_KEY_SIZE,
1396 .cra_init = spacc_ablk_cra_init,
1397 .cra_exit = spacc_ablk_cra_exit,
1401 .key_offs = DES_BLOCK_SIZE,
1403 .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_CBC,
1405 .cra_name = "cbc(des3_ede)",
1406 .cra_driver_name = "cbc-des3-ede-picoxcell",
1407 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1408 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
1410 CRYPTO_ALG_KERN_DRIVER_ONLY,
1411 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1412 .cra_ctxsize = sizeof(struct spacc_ablk_ctx),
1413 .cra_type = &crypto_ablkcipher_type,
1414 .cra_module = THIS_MODULE,
1416 .setkey = spacc_des_setkey,
1417 .encrypt = spacc_ablk_encrypt,
1418 .decrypt = spacc_ablk_decrypt,
1419 .min_keysize = DES3_EDE_KEY_SIZE,
1420 .max_keysize = DES3_EDE_KEY_SIZE,
1421 .ivsize = DES3_EDE_BLOCK_SIZE,
1423 .cra_init = spacc_ablk_cra_init,
1424 .cra_exit = spacc_ablk_cra_exit,
1428 .key_offs = DES_BLOCK_SIZE,
1430 .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_ECB,
1432 .cra_name = "ecb(des3_ede)",
1433 .cra_driver_name = "ecb-des3-ede-picoxcell",
1434 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1435 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
1437 CRYPTO_ALG_KERN_DRIVER_ONLY,
1438 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1439 .cra_ctxsize = sizeof(struct spacc_ablk_ctx),
1440 .cra_type = &crypto_ablkcipher_type,
1441 .cra_module = THIS_MODULE,
1443 .setkey = spacc_des_setkey,
1444 .encrypt = spacc_ablk_encrypt,
1445 .decrypt = spacc_ablk_decrypt,
1446 .min_keysize = DES3_EDE_KEY_SIZE,
1447 .max_keysize = DES3_EDE_KEY_SIZE,
1449 .cra_init = spacc_ablk_cra_init,
1450 .cra_exit = spacc_ablk_cra_exit,
1454 .ctrl_default = SPA_CTRL_CIPH_ALG_AES | SPA_CTRL_CIPH_MODE_CBC |
1455 SPA_CTRL_HASH_ALG_SHA | SPA_CTRL_HASH_MODE_HMAC,
1457 .iv_offs = AES_MAX_KEY_SIZE,
1459 .cra_name = "authenc(hmac(sha1),cbc(aes))",
1460 .cra_driver_name = "authenc-hmac-sha1-cbc-aes-picoxcell",
1461 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1462 .cra_flags = CRYPTO_ALG_TYPE_AEAD |
1464 CRYPTO_ALG_KERN_DRIVER_ONLY,
1465 .cra_blocksize = AES_BLOCK_SIZE,
1466 .cra_ctxsize = sizeof(struct spacc_aead_ctx),
1467 .cra_type = &crypto_aead_type,
1468 .cra_module = THIS_MODULE,
1470 .setkey = spacc_aead_setkey,
1471 .setauthsize = spacc_aead_setauthsize,
1472 .encrypt = spacc_aead_encrypt,
1473 .decrypt = spacc_aead_decrypt,
1474 .givencrypt = spacc_aead_givencrypt,
1475 .ivsize = AES_BLOCK_SIZE,
1476 .maxauthsize = SHA1_DIGEST_SIZE,
1478 .cra_init = spacc_aead_cra_init,
1479 .cra_exit = spacc_aead_cra_exit,
1483 .ctrl_default = SPA_CTRL_CIPH_ALG_AES | SPA_CTRL_CIPH_MODE_CBC |
1484 SPA_CTRL_HASH_ALG_SHA256 |
1485 SPA_CTRL_HASH_MODE_HMAC,
1487 .iv_offs = AES_MAX_KEY_SIZE,
1489 .cra_name = "authenc(hmac(sha256),cbc(aes))",
1490 .cra_driver_name = "authenc-hmac-sha256-cbc-aes-picoxcell",
1491 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1492 .cra_flags = CRYPTO_ALG_TYPE_AEAD |
1494 CRYPTO_ALG_KERN_DRIVER_ONLY,
1495 .cra_blocksize = AES_BLOCK_SIZE,
1496 .cra_ctxsize = sizeof(struct spacc_aead_ctx),
1497 .cra_type = &crypto_aead_type,
1498 .cra_module = THIS_MODULE,
1500 .setkey = spacc_aead_setkey,
1501 .setauthsize = spacc_aead_setauthsize,
1502 .encrypt = spacc_aead_encrypt,
1503 .decrypt = spacc_aead_decrypt,
1504 .givencrypt = spacc_aead_givencrypt,
1505 .ivsize = AES_BLOCK_SIZE,
1506 .maxauthsize = SHA256_DIGEST_SIZE,
1508 .cra_init = spacc_aead_cra_init,
1509 .cra_exit = spacc_aead_cra_exit,
1514 .iv_offs = AES_MAX_KEY_SIZE,
1515 .ctrl_default = SPA_CTRL_CIPH_ALG_AES | SPA_CTRL_CIPH_MODE_CBC |
1516 SPA_CTRL_HASH_ALG_MD5 | SPA_CTRL_HASH_MODE_HMAC,
1518 .cra_name = "authenc(hmac(md5),cbc(aes))",
1519 .cra_driver_name = "authenc-hmac-md5-cbc-aes-picoxcell",
1520 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1521 .cra_flags = CRYPTO_ALG_TYPE_AEAD |
1523 CRYPTO_ALG_KERN_DRIVER_ONLY,
1524 .cra_blocksize = AES_BLOCK_SIZE,
1525 .cra_ctxsize = sizeof(struct spacc_aead_ctx),
1526 .cra_type = &crypto_aead_type,
1527 .cra_module = THIS_MODULE,
1529 .setkey = spacc_aead_setkey,
1530 .setauthsize = spacc_aead_setauthsize,
1531 .encrypt = spacc_aead_encrypt,
1532 .decrypt = spacc_aead_decrypt,
1533 .givencrypt = spacc_aead_givencrypt,
1534 .ivsize = AES_BLOCK_SIZE,
1535 .maxauthsize = MD5_DIGEST_SIZE,
1537 .cra_init = spacc_aead_cra_init,
1538 .cra_exit = spacc_aead_cra_exit,
1542 .key_offs = DES_BLOCK_SIZE,
1544 .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_CBC |
1545 SPA_CTRL_HASH_ALG_SHA | SPA_CTRL_HASH_MODE_HMAC,
1547 .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
1548 .cra_driver_name = "authenc-hmac-sha1-cbc-3des-picoxcell",
1549 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1550 .cra_flags = CRYPTO_ALG_TYPE_AEAD |
1552 CRYPTO_ALG_KERN_DRIVER_ONLY,
1553 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1554 .cra_ctxsize = sizeof(struct spacc_aead_ctx),
1555 .cra_type = &crypto_aead_type,
1556 .cra_module = THIS_MODULE,
1558 .setkey = spacc_aead_setkey,
1559 .setauthsize = spacc_aead_setauthsize,
1560 .encrypt = spacc_aead_encrypt,
1561 .decrypt = spacc_aead_decrypt,
1562 .givencrypt = spacc_aead_givencrypt,
1563 .ivsize = DES3_EDE_BLOCK_SIZE,
1564 .maxauthsize = SHA1_DIGEST_SIZE,
1566 .cra_init = spacc_aead_cra_init,
1567 .cra_exit = spacc_aead_cra_exit,
1571 .key_offs = DES_BLOCK_SIZE,
1573 .ctrl_default = SPA_CTRL_CIPH_ALG_AES | SPA_CTRL_CIPH_MODE_CBC |
1574 SPA_CTRL_HASH_ALG_SHA256 |
1575 SPA_CTRL_HASH_MODE_HMAC,
1577 .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
1578 .cra_driver_name = "authenc-hmac-sha256-cbc-3des-picoxcell",
1579 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1580 .cra_flags = CRYPTO_ALG_TYPE_AEAD |
1582 CRYPTO_ALG_KERN_DRIVER_ONLY,
1583 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1584 .cra_ctxsize = sizeof(struct spacc_aead_ctx),
1585 .cra_type = &crypto_aead_type,
1586 .cra_module = THIS_MODULE,
1588 .setkey = spacc_aead_setkey,
1589 .setauthsize = spacc_aead_setauthsize,
1590 .encrypt = spacc_aead_encrypt,
1591 .decrypt = spacc_aead_decrypt,
1592 .givencrypt = spacc_aead_givencrypt,
1593 .ivsize = DES3_EDE_BLOCK_SIZE,
1594 .maxauthsize = SHA256_DIGEST_SIZE,
1596 .cra_init = spacc_aead_cra_init,
1597 .cra_exit = spacc_aead_cra_exit,
1601 .key_offs = DES_BLOCK_SIZE,
1603 .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_CBC |
1604 SPA_CTRL_HASH_ALG_MD5 | SPA_CTRL_HASH_MODE_HMAC,
1606 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
1607 .cra_driver_name = "authenc-hmac-md5-cbc-3des-picoxcell",
1608 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1609 .cra_flags = CRYPTO_ALG_TYPE_AEAD |
1611 CRYPTO_ALG_KERN_DRIVER_ONLY,
1612 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1613 .cra_ctxsize = sizeof(struct spacc_aead_ctx),
1614 .cra_type = &crypto_aead_type,
1615 .cra_module = THIS_MODULE,
1617 .setkey = spacc_aead_setkey,
1618 .setauthsize = spacc_aead_setauthsize,
1619 .encrypt = spacc_aead_encrypt,
1620 .decrypt = spacc_aead_decrypt,
1621 .givencrypt = spacc_aead_givencrypt,
1622 .ivsize = DES3_EDE_BLOCK_SIZE,
1623 .maxauthsize = MD5_DIGEST_SIZE,
1625 .cra_init = spacc_aead_cra_init,
1626 .cra_exit = spacc_aead_cra_exit,
1631 static struct spacc_alg l2_engine_algs[] = {
1634 .iv_offs = SPACC_CRYPTO_KASUMI_F8_KEY_LEN,
1635 .ctrl_default = SPA_CTRL_CIPH_ALG_KASUMI |
1636 SPA_CTRL_CIPH_MODE_F8,
1638 .cra_name = "f8(kasumi)",
1639 .cra_driver_name = "f8-kasumi-picoxcell",
1640 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1641 .cra_flags = CRYPTO_ALG_TYPE_GIVCIPHER |
1643 CRYPTO_ALG_KERN_DRIVER_ONLY,
1645 .cra_ctxsize = sizeof(struct spacc_ablk_ctx),
1646 .cra_type = &crypto_ablkcipher_type,
1647 .cra_module = THIS_MODULE,
1649 .setkey = spacc_kasumi_f8_setkey,
1650 .encrypt = spacc_ablk_encrypt,
1651 .decrypt = spacc_ablk_decrypt,
1656 .cra_init = spacc_ablk_cra_init,
1657 .cra_exit = spacc_ablk_cra_exit,
1663 static const struct of_device_id spacc_of_id_table[] = {
1664 { .compatible = "picochip,spacc-ipsec" },
1665 { .compatible = "picochip,spacc-l2" },
1668 #endif /* CONFIG_OF */
1670 static bool spacc_is_compatible(struct platform_device *pdev,
1671 const char *spacc_type)
1673 const struct platform_device_id *platid = platform_get_device_id(pdev);
1675 if (platid && !strcmp(platid->name, spacc_type))
1679 if (of_device_is_compatible(pdev->dev.of_node, spacc_type))
1681 #endif /* CONFIG_OF */
1686 static int spacc_probe(struct platform_device *pdev)
1688 int i, err, ret = -EINVAL;
1689 struct resource *mem, *irq;
1690 struct spacc_engine *engine = devm_kzalloc(&pdev->dev, sizeof(*engine),
1695 if (spacc_is_compatible(pdev, "picochip,spacc-ipsec")) {
1696 engine->max_ctxs = SPACC_CRYPTO_IPSEC_MAX_CTXS;
1697 engine->cipher_pg_sz = SPACC_CRYPTO_IPSEC_CIPHER_PG_SZ;
1698 engine->hash_pg_sz = SPACC_CRYPTO_IPSEC_HASH_PG_SZ;
1699 engine->fifo_sz = SPACC_CRYPTO_IPSEC_FIFO_SZ;
1700 engine->algs = ipsec_engine_algs;
1701 engine->num_algs = ARRAY_SIZE(ipsec_engine_algs);
1702 } else if (spacc_is_compatible(pdev, "picochip,spacc-l2")) {
1703 engine->max_ctxs = SPACC_CRYPTO_L2_MAX_CTXS;
1704 engine->cipher_pg_sz = SPACC_CRYPTO_L2_CIPHER_PG_SZ;
1705 engine->hash_pg_sz = SPACC_CRYPTO_L2_HASH_PG_SZ;
1706 engine->fifo_sz = SPACC_CRYPTO_L2_FIFO_SZ;
1707 engine->algs = l2_engine_algs;
1708 engine->num_algs = ARRAY_SIZE(l2_engine_algs);
1713 engine->name = dev_name(&pdev->dev);
1715 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1716 engine->regs = devm_ioremap_resource(&pdev->dev, mem);
1717 if (IS_ERR(engine->regs))
1718 return PTR_ERR(engine->regs);
1720 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1722 dev_err(&pdev->dev, "no memory/irq resource for engine\n");
1726 if (devm_request_irq(&pdev->dev, irq->start, spacc_spacc_irq, 0,
1727 engine->name, engine)) {
1728 dev_err(engine->dev, "failed to request IRQ\n");
1732 engine->dev = &pdev->dev;
1733 engine->cipher_ctx_base = engine->regs + SPA_CIPH_KEY_BASE_REG_OFFSET;
1734 engine->hash_key_base = engine->regs + SPA_HASH_KEY_BASE_REG_OFFSET;
1736 engine->req_pool = dmam_pool_create(engine->name, engine->dev,
1737 MAX_DDT_LEN * sizeof(struct spacc_ddt), 8, SZ_64K);
1738 if (!engine->req_pool)
1741 spin_lock_init(&engine->hw_lock);
1743 engine->clk = clk_get(&pdev->dev, "ref");
1744 if (IS_ERR(engine->clk)) {
1745 dev_info(&pdev->dev, "clk unavailable\n");
1746 device_remove_file(&pdev->dev, &dev_attr_stat_irq_thresh);
1747 return PTR_ERR(engine->clk);
1750 if (clk_enable(engine->clk)) {
1751 dev_info(&pdev->dev, "unable to enable clk\n");
1752 clk_put(engine->clk);
1756 err = device_create_file(&pdev->dev, &dev_attr_stat_irq_thresh);
1758 clk_disable(engine->clk);
1759 clk_put(engine->clk);
1765 * Use an IRQ threshold of 50% as a default. This seems to be a
1766 * reasonable trade off of latency against throughput but can be
1767 * changed at runtime.
1769 engine->stat_irq_thresh = (engine->fifo_sz / 2);
1772 * Configure the interrupts. We only use the STAT_CNT interrupt as we
1773 * only submit a new packet for processing when we complete another in
1774 * the queue. This minimizes time spent in the interrupt handler.
1776 writel(engine->stat_irq_thresh << SPA_IRQ_CTRL_STAT_CNT_OFFSET,
1777 engine->regs + SPA_IRQ_CTRL_REG_OFFSET);
1778 writel(SPA_IRQ_EN_STAT_EN | SPA_IRQ_EN_GLBL_EN,
1779 engine->regs + SPA_IRQ_EN_REG_OFFSET);
1781 setup_timer(&engine->packet_timeout, spacc_packet_timeout,
1782 (unsigned long)engine);
1784 INIT_LIST_HEAD(&engine->pending);
1785 INIT_LIST_HEAD(&engine->completed);
1786 INIT_LIST_HEAD(&engine->in_progress);
1787 engine->in_flight = 0;
1788 tasklet_init(&engine->complete, spacc_spacc_complete,
1789 (unsigned long)engine);
1791 platform_set_drvdata(pdev, engine);
1793 INIT_LIST_HEAD(&engine->registered_algs);
1794 for (i = 0; i < engine->num_algs; ++i) {
1795 engine->algs[i].engine = engine;
1796 err = crypto_register_alg(&engine->algs[i].alg);
1798 list_add_tail(&engine->algs[i].entry,
1799 &engine->registered_algs);
1803 dev_err(engine->dev, "failed to register alg \"%s\"\n",
1804 engine->algs[i].alg.cra_name);
1806 dev_dbg(engine->dev, "registered alg \"%s\"\n",
1807 engine->algs[i].alg.cra_name);
1813 static int spacc_remove(struct platform_device *pdev)
1815 struct spacc_alg *alg, *next;
1816 struct spacc_engine *engine = platform_get_drvdata(pdev);
1818 del_timer_sync(&engine->packet_timeout);
1819 device_remove_file(&pdev->dev, &dev_attr_stat_irq_thresh);
1821 list_for_each_entry_safe(alg, next, &engine->registered_algs, entry) {
1822 list_del(&alg->entry);
1823 crypto_unregister_alg(&alg->alg);
1826 clk_disable(engine->clk);
1827 clk_put(engine->clk);
1832 static const struct platform_device_id spacc_id_table[] = {
1833 { "picochip,spacc-ipsec", },
1834 { "picochip,spacc-l2", },
1838 static struct platform_driver spacc_driver = {
1839 .probe = spacc_probe,
1840 .remove = spacc_remove,
1842 .name = "picochip,spacc",
1844 .pm = &spacc_pm_ops,
1845 #endif /* CONFIG_PM */
1846 .of_match_table = of_match_ptr(spacc_of_id_table),
1848 .id_table = spacc_id_table,
1851 module_platform_driver(spacc_driver);
1853 MODULE_LICENSE("GPL");
1854 MODULE_AUTHOR("Jamie Iles");