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