Blame view

drivers/crypto/mxs-dcp.c 29.3 KB
fcaf20360   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
15b59e7c3   Marek Vasut   crypto: mxs - Add...
2
3
4
5
  /*
   * Freescale i.MX23/i.MX28 Data Co-Processor driver
   *
   * Copyright (C) 2013 Marek Vasut <marex@denx.de>
15b59e7c3   Marek Vasut   crypto: mxs - Add...
6
   */
15b59e7c3   Marek Vasut   crypto: mxs - Add...
7
8
9
10
11
12
13
14
15
  #include <linux/dma-mapping.h>
  #include <linux/interrupt.h>
  #include <linux/io.h>
  #include <linux/kernel.h>
  #include <linux/kthread.h>
  #include <linux/module.h>
  #include <linux/of.h>
  #include <linux/platform_device.h>
  #include <linux/stmp_device.h>
57f002891   Leonard Crestez   crypto: mxs-dcp -...
16
  #include <linux/clk.h>
15b59e7c3   Marek Vasut   crypto: mxs - Add...
17
18
19
20
  
  #include <crypto/aes.h>
  #include <crypto/sha.h>
  #include <crypto/internal/hash.h>
29406bb92   Herbert Xu   crypto: mxs-dcp -...
21
  #include <crypto/internal/skcipher.h>
eacec15f5   Rosioru Dragos   LF-717: crypto: d...
22
  #include <crypto/scatterwalk.h>
15b59e7c3   Marek Vasut   crypto: mxs - Add...
23
24
25
  
  #define DCP_MAX_CHANS	4
  #define DCP_BUF_SZ	PAGE_SIZE
c709eebaf   Radu Solea   crypto: mxs-dcp -...
26
  #define DCP_SHA_PAY_SZ  64
15b59e7c3   Marek Vasut   crypto: mxs - Add...
27

1a7c68561   Marek Vasut   crypto: mxs-dcp -...
28
  #define DCP_ALIGNMENT	64
c709eebaf   Radu Solea   crypto: mxs-dcp -...
29
30
31
32
  /*
   * Null hashes to align with hw behavior on imx6sl and ull
   * these are flipped for consistency with hw output
   */
ce4e45842   Wei Yongjun   crypto: mxs-dcp -...
33
  static const uint8_t sha1_null_hash[] =
c709eebaf   Radu Solea   crypto: mxs-dcp -...
34
35
  	"\x09\x07\xd8\xaf\x90\x18\x60\x95\xef\xbf"
  	"\x55\x32\x0d\x4b\x6b\x5e\xee\xa3\x39\xda";
ce4e45842   Wei Yongjun   crypto: mxs-dcp -...
36
  static const uint8_t sha256_null_hash[] =
c709eebaf   Radu Solea   crypto: mxs-dcp -...
37
38
39
40
  	"\x55\xb8\x52\x78\x1b\x99\x95\xa4"
  	"\x4c\x93\x9b\x64\xe4\x41\xae\x27"
  	"\x24\xb9\x6f\x99\xc8\xf4\xfb\x9a"
  	"\x14\x1c\xfc\x98\x42\xc4\xb0\xe3";
15b59e7c3   Marek Vasut   crypto: mxs - Add...
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  /* DCP DMA descriptor. */
  struct dcp_dma_desc {
  	uint32_t	next_cmd_addr;
  	uint32_t	control0;
  	uint32_t	control1;
  	uint32_t	source;
  	uint32_t	destination;
  	uint32_t	size;
  	uint32_t	payload;
  	uint32_t	status;
  };
  
  /* Coherent aligned block for bounce buffering. */
  struct dcp_coherent_block {
  	uint8_t			aes_in_buf[DCP_BUF_SZ];
  	uint8_t			aes_out_buf[DCP_BUF_SZ];
  	uint8_t			sha_in_buf[DCP_BUF_SZ];
c709eebaf   Radu Solea   crypto: mxs-dcp -...
58
  	uint8_t			sha_out_buf[DCP_SHA_PAY_SZ];
15b59e7c3   Marek Vasut   crypto: mxs - Add...
59
60
  
  	uint8_t			aes_key[2 * AES_KEYSIZE_128];
15b59e7c3   Marek Vasut   crypto: mxs - Add...
61
62
63
64
65
66
67
68
69
70
71
72
73
  
  	struct dcp_dma_desc	desc[DCP_MAX_CHANS];
  };
  
  struct dcp {
  	struct device			*dev;
  	void __iomem			*base;
  
  	uint32_t			caps;
  
  	struct dcp_coherent_block	*coh;
  
  	struct completion		completion[DCP_MAX_CHANS];
d80771c08   Leonard Crestez   crypto: mxs-dcp -...
74
  	spinlock_t			lock[DCP_MAX_CHANS];
15b59e7c3   Marek Vasut   crypto: mxs - Add...
75
76
  	struct task_struct		*thread[DCP_MAX_CHANS];
  	struct crypto_queue		queue[DCP_MAX_CHANS];
57f002891   Leonard Crestez   crypto: mxs-dcp -...
77
  	struct clk			*dcp_clk;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  };
  
  enum dcp_chan {
  	DCP_CHAN_HASH_SHA	= 0,
  	DCP_CHAN_CRYPTO		= 2,
  };
  
  struct dcp_async_ctx {
  	/* Common context */
  	enum dcp_chan	chan;
  	uint32_t	fill;
  
  	/* SHA Hash-specific context */
  	struct mutex			mutex;
  	uint32_t			alg;
  	unsigned int			hot:1;
  
  	/* Crypto-specific context */
f805f59d1   Kees Cook   crypto: mxs-dcp -...
96
  	struct crypto_sync_skcipher	*fallback;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
97
98
99
  	unsigned int			key_len;
  	uint8_t				key[AES_KEYSIZE_128];
  };
2021abaa0   Marek Vasut   crypto: dcp - Mov...
100
101
102
103
  struct dcp_aes_req_ctx {
  	unsigned int	enc:1;
  	unsigned int	ecb:1;
  };
15b59e7c3   Marek Vasut   crypto: mxs - Add...
104
105
106
107
  struct dcp_sha_req_ctx {
  	unsigned int	init:1;
  	unsigned int	fini:1;
  };
ea9e7568f   Dan Douglass   crypto: mxs-dcp -...
108
109
110
111
  struct dcp_export_state {
  	struct dcp_sha_req_ctx req_ctx;
  	struct dcp_async_ctx async_ctx;
  };
15b59e7c3   Marek Vasut   crypto: mxs - Add...
112
113
114
115
116
  /*
   * There can even be only one instance of the MXS DCP due to the
   * design of Linux Crypto API.
   */
  static struct dcp *global_sdcp;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  
  /* DCP register layout. */
  #define MXS_DCP_CTRL				0x00
  #define MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES	(1 << 23)
  #define MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING	(1 << 22)
  
  #define MXS_DCP_STAT				0x10
  #define MXS_DCP_STAT_CLR			0x18
  #define MXS_DCP_STAT_IRQ_MASK			0xf
  
  #define MXS_DCP_CHANNELCTRL			0x20
  #define MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK	0xff
  
  #define MXS_DCP_CAPABILITY1			0x40
  #define MXS_DCP_CAPABILITY1_SHA256		(4 << 16)
  #define MXS_DCP_CAPABILITY1_SHA1		(1 << 16)
  #define MXS_DCP_CAPABILITY1_AES128		(1 << 0)
  
  #define MXS_DCP_CONTEXT				0x50
  
  #define MXS_DCP_CH_N_CMDPTR(n)			(0x100 + ((n) * 0x40))
  
  #define MXS_DCP_CH_N_SEMA(n)			(0x110 + ((n) * 0x40))
  
  #define MXS_DCP_CH_N_STAT(n)			(0x120 + ((n) * 0x40))
  #define MXS_DCP_CH_N_STAT_CLR(n)		(0x128 + ((n) * 0x40))
  
  /* DMA descriptor bits. */
  #define MXS_DCP_CONTROL0_HASH_TERM		(1 << 13)
  #define MXS_DCP_CONTROL0_HASH_INIT		(1 << 12)
  #define MXS_DCP_CONTROL0_PAYLOAD_KEY		(1 << 11)
  #define MXS_DCP_CONTROL0_CIPHER_ENCRYPT		(1 << 8)
  #define MXS_DCP_CONTROL0_CIPHER_INIT		(1 << 9)
  #define MXS_DCP_CONTROL0_ENABLE_HASH		(1 << 6)
  #define MXS_DCP_CONTROL0_ENABLE_CIPHER		(1 << 5)
  #define MXS_DCP_CONTROL0_DECR_SEMAPHORE		(1 << 1)
  #define MXS_DCP_CONTROL0_INTERRUPT		(1 << 0)
  
  #define MXS_DCP_CONTROL1_HASH_SELECT_SHA256	(2 << 16)
  #define MXS_DCP_CONTROL1_HASH_SELECT_SHA1	(0 << 16)
  #define MXS_DCP_CONTROL1_CIPHER_MODE_CBC	(1 << 4)
  #define MXS_DCP_CONTROL1_CIPHER_MODE_ECB	(0 << 4)
  #define MXS_DCP_CONTROL1_CIPHER_SELECT_AES128	(0 << 0)
  
  static int mxs_dcp_start_dma(struct dcp_async_ctx *actx)
  {
  	struct dcp *sdcp = global_sdcp;
  	const int chan = actx->chan;
  	uint32_t stat;
dd0fff8db   Nicholas Mc Guire   crypto: mxs-dcp -...
166
  	unsigned long ret;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
  	struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
  
  	dma_addr_t desc_phys = dma_map_single(sdcp->dev, desc, sizeof(*desc),
  					      DMA_TO_DEVICE);
  
  	reinit_completion(&sdcp->completion[chan]);
  
  	/* Clear status register. */
  	writel(0xffffffff, sdcp->base + MXS_DCP_CH_N_STAT_CLR(chan));
  
  	/* Load the DMA descriptor. */
  	writel(desc_phys, sdcp->base + MXS_DCP_CH_N_CMDPTR(chan));
  
  	/* Increment the semaphore to start the DMA transfer. */
  	writel(1, sdcp->base + MXS_DCP_CH_N_SEMA(chan));
  
  	ret = wait_for_completion_timeout(&sdcp->completion[chan],
  					  msecs_to_jiffies(1000));
  	if (!ret) {
  		dev_err(sdcp->dev, "Channel %i timeout (DCP_STAT=0x%08x)
  ",
  			chan, readl(sdcp->base + MXS_DCP_STAT));
  		return -ETIMEDOUT;
  	}
  
  	stat = readl(sdcp->base + MXS_DCP_CH_N_STAT(chan));
  	if (stat & 0xff) {
  		dev_err(sdcp->dev, "Channel %i error (CH_STAT=0x%08x)
  ",
  			chan, stat);
  		return -EINVAL;
  	}
  
  	dma_unmap_single(sdcp->dev, desc_phys, sizeof(*desc), DMA_TO_DEVICE);
  
  	return 0;
  }
  
  /*
   * Encryption (AES128)
   */
2021abaa0   Marek Vasut   crypto: dcp - Mov...
208
209
  static int mxs_dcp_run_aes(struct dcp_async_ctx *actx,
  			   struct ablkcipher_request *req, int init)
15b59e7c3   Marek Vasut   crypto: mxs - Add...
210
211
212
  {
  	struct dcp *sdcp = global_sdcp;
  	struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
2021abaa0   Marek Vasut   crypto: dcp - Mov...
213
  	struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
214
215
216
217
218
219
220
221
222
  	int ret;
  
  	dma_addr_t key_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_key,
  					     2 * AES_KEYSIZE_128,
  					     DMA_TO_DEVICE);
  	dma_addr_t src_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_in_buf,
  					     DCP_BUF_SZ, DMA_TO_DEVICE);
  	dma_addr_t dst_phys = dma_map_single(sdcp->dev, sdcp->coh->aes_out_buf,
  					     DCP_BUF_SZ, DMA_FROM_DEVICE);
fadd7a6e6   Radu Solea   crypto: mxs-dcp -...
223
224
225
226
227
228
  	if (actx->fill % AES_BLOCK_SIZE) {
  		dev_err(sdcp->dev, "Invalid block size!
  ");
  		ret = -EINVAL;
  		goto aes_done_run;
  	}
15b59e7c3   Marek Vasut   crypto: mxs - Add...
229
230
231
232
233
234
235
  	/* Fill in the DMA descriptor. */
  	desc->control0 = MXS_DCP_CONTROL0_DECR_SEMAPHORE |
  		    MXS_DCP_CONTROL0_INTERRUPT |
  		    MXS_DCP_CONTROL0_ENABLE_CIPHER;
  
  	/* Payload contains the key. */
  	desc->control0 |= MXS_DCP_CONTROL0_PAYLOAD_KEY;
2021abaa0   Marek Vasut   crypto: dcp - Mov...
236
  	if (rctx->enc)
15b59e7c3   Marek Vasut   crypto: mxs - Add...
237
238
239
240
241
  		desc->control0 |= MXS_DCP_CONTROL0_CIPHER_ENCRYPT;
  	if (init)
  		desc->control0 |= MXS_DCP_CONTROL0_CIPHER_INIT;
  
  	desc->control1 = MXS_DCP_CONTROL1_CIPHER_SELECT_AES128;
2021abaa0   Marek Vasut   crypto: dcp - Mov...
242
  	if (rctx->ecb)
15b59e7c3   Marek Vasut   crypto: mxs - Add...
243
244
245
246
247
248
249
250
251
252
253
254
  		desc->control1 |= MXS_DCP_CONTROL1_CIPHER_MODE_ECB;
  	else
  		desc->control1 |= MXS_DCP_CONTROL1_CIPHER_MODE_CBC;
  
  	desc->next_cmd_addr = 0;
  	desc->source = src_phys;
  	desc->destination = dst_phys;
  	desc->size = actx->fill;
  	desc->payload = key_phys;
  	desc->status = 0;
  
  	ret = mxs_dcp_start_dma(actx);
fadd7a6e6   Radu Solea   crypto: mxs-dcp -...
255
  aes_done_run:
15b59e7c3   Marek Vasut   crypto: mxs - Add...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
  	dma_unmap_single(sdcp->dev, key_phys, 2 * AES_KEYSIZE_128,
  			 DMA_TO_DEVICE);
  	dma_unmap_single(sdcp->dev, src_phys, DCP_BUF_SZ, DMA_TO_DEVICE);
  	dma_unmap_single(sdcp->dev, dst_phys, DCP_BUF_SZ, DMA_FROM_DEVICE);
  
  	return ret;
  }
  
  static int mxs_dcp_aes_block_crypt(struct crypto_async_request *arq)
  {
  	struct dcp *sdcp = global_sdcp;
  
  	struct ablkcipher_request *req = ablkcipher_request_cast(arq);
  	struct dcp_async_ctx *actx = crypto_tfm_ctx(arq->tfm);
2021abaa0   Marek Vasut   crypto: dcp - Mov...
270
  	struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
271
272
273
274
275
276
277
278
279
280
281
  
  	struct scatterlist *dst = req->dst;
  	struct scatterlist *src = req->src;
  	const int nents = sg_nents(req->src);
  
  	const int out_off = DCP_BUF_SZ;
  	uint8_t *in_buf = sdcp->coh->aes_in_buf;
  	uint8_t *out_buf = sdcp->coh->aes_out_buf;
  
  	uint8_t *out_tmp, *src_buf, *dst_buf = NULL;
  	uint32_t dst_off = 0;
fadd7a6e6   Radu Solea   crypto: mxs-dcp -...
282
  	uint32_t last_out_len = 0;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
283
284
285
286
287
  
  	uint8_t *key = sdcp->coh->aes_key;
  
  	int ret = 0;
  	int split = 0;
fadd7a6e6   Radu Solea   crypto: mxs-dcp -...
288
  	unsigned int i, len, clen, rem = 0, tlen = 0;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
289
  	int init = 0;
fadd7a6e6   Radu Solea   crypto: mxs-dcp -...
290
  	bool limit_hit = false;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
291

7c2588e59   Horia Geantă   crypto: dcp - che...
292
293
  	if (!req->nbytes)
  		return 0;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
294
295
296
297
  	actx->fill = 0;
  
  	/* Copy the key from the temporary location. */
  	memcpy(key, actx->key, actx->key_len);
2021abaa0   Marek Vasut   crypto: dcp - Mov...
298
  	if (!rctx->ecb) {
15b59e7c3   Marek Vasut   crypto: mxs - Add...
299
300
301
302
303
304
305
306
307
308
309
  		/* Copy the CBC IV just past the key. */
  		memcpy(key + AES_KEYSIZE_128, req->info, AES_KEYSIZE_128);
  		/* CBC needs the INIT set. */
  		init = 1;
  	} else {
  		memset(key + AES_KEYSIZE_128, 0, AES_KEYSIZE_128);
  	}
  
  	for_each_sg(req->src, src, nents, i) {
  		src_buf = sg_virt(src);
  		len = sg_dma_len(src);
fadd7a6e6   Radu Solea   crypto: mxs-dcp -...
310
311
312
313
314
  		tlen += len;
  		limit_hit = tlen > req->nbytes;
  
  		if (limit_hit)
  			len = req->nbytes - (tlen - len);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
  
  		do {
  			if (actx->fill + len > out_off)
  				clen = out_off - actx->fill;
  			else
  				clen = len;
  
  			memcpy(in_buf + actx->fill, src_buf, clen);
  			len -= clen;
  			src_buf += clen;
  			actx->fill += clen;
  
  			/*
  			 * If we filled the buffer or this is the last SG,
  			 * submit the buffer.
  			 */
fadd7a6e6   Radu Solea   crypto: mxs-dcp -...
331
332
  			if (actx->fill == out_off || sg_is_last(src) ||
  				limit_hit) {
2021abaa0   Marek Vasut   crypto: dcp - Mov...
333
  				ret = mxs_dcp_run_aes(actx, req, init);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
334
335
336
337
338
  				if (ret)
  					return ret;
  				init = 0;
  
  				out_tmp = out_buf;
fadd7a6e6   Radu Solea   crypto: mxs-dcp -...
339
  				last_out_len = actx->fill;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
  				while (dst && actx->fill) {
  					if (!split) {
  						dst_buf = sg_virt(dst);
  						dst_off = 0;
  					}
  					rem = min(sg_dma_len(dst) - dst_off,
  						  actx->fill);
  
  					memcpy(dst_buf + dst_off, out_tmp, rem);
  					out_tmp += rem;
  					dst_off += rem;
  					actx->fill -= rem;
  
  					if (dst_off == sg_dma_len(dst)) {
  						dst = sg_next(dst);
  						split = 0;
  					} else {
  						split = 1;
  					}
  				}
  			}
  		} while (len);
fadd7a6e6   Radu Solea   crypto: mxs-dcp -...
362
363
364
365
366
367
368
369
370
371
372
373
374
  
  		if (limit_hit)
  			break;
  	}
  
  	/* Copy the IV for CBC for chaining */
  	if (!rctx->ecb) {
  		if (rctx->enc)
  			memcpy(req->info, out_buf+(last_out_len-AES_BLOCK_SIZE),
  				AES_BLOCK_SIZE);
  		else
  			memcpy(req->info, in_buf+(last_out_len-AES_BLOCK_SIZE),
  				AES_BLOCK_SIZE);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
375
376
377
378
379
380
381
382
383
384
385
386
387
388
  	}
  
  	return ret;
  }
  
  static int dcp_chan_thread_aes(void *data)
  {
  	struct dcp *sdcp = global_sdcp;
  	const int chan = DCP_CHAN_CRYPTO;
  
  	struct crypto_async_request *backlog;
  	struct crypto_async_request *arq;
  
  	int ret;
d80771c08   Leonard Crestez   crypto: mxs-dcp -...
389
390
  	while (!kthread_should_stop()) {
  		set_current_state(TASK_INTERRUPTIBLE);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
391

d80771c08   Leonard Crestez   crypto: mxs-dcp -...
392
  		spin_lock(&sdcp->lock[chan]);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
393
394
  		backlog = crypto_get_backlog(&sdcp->queue[chan]);
  		arq = crypto_dequeue_request(&sdcp->queue[chan]);
d80771c08   Leonard Crestez   crypto: mxs-dcp -...
395
396
397
398
399
400
401
402
  		spin_unlock(&sdcp->lock[chan]);
  
  		if (!backlog && !arq) {
  			schedule();
  			continue;
  		}
  
  		set_current_state(TASK_RUNNING);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
403
404
405
406
407
408
409
  
  		if (backlog)
  			backlog->complete(backlog, -EINPROGRESS);
  
  		if (arq) {
  			ret = mxs_dcp_aes_block_crypt(arq);
  			arq->complete(arq, ret);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
410
  		}
d80771c08   Leonard Crestez   crypto: mxs-dcp -...
411
  	}
15b59e7c3   Marek Vasut   crypto: mxs - Add...
412
413
414
415
416
417
  
  	return 0;
  }
  
  static int mxs_dcp_block_fallback(struct ablkcipher_request *req, int enc)
  {
29406bb92   Herbert Xu   crypto: mxs-dcp -...
418
419
  	struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
  	struct dcp_async_ctx *ctx = crypto_ablkcipher_ctx(tfm);
f805f59d1   Kees Cook   crypto: mxs-dcp -...
420
  	SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
421
  	int ret;
f805f59d1   Kees Cook   crypto: mxs-dcp -...
422
  	skcipher_request_set_sync_tfm(subreq, ctx->fallback);
29406bb92   Herbert Xu   crypto: mxs-dcp -...
423
424
425
  	skcipher_request_set_callback(subreq, req->base.flags, NULL, NULL);
  	skcipher_request_set_crypt(subreq, req->src, req->dst,
  				   req->nbytes, req->info);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
426
427
  
  	if (enc)
29406bb92   Herbert Xu   crypto: mxs-dcp -...
428
  		ret = crypto_skcipher_encrypt(subreq);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
429
  	else
29406bb92   Herbert Xu   crypto: mxs-dcp -...
430
  		ret = crypto_skcipher_decrypt(subreq);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
431

29406bb92   Herbert Xu   crypto: mxs-dcp -...
432
  	skcipher_request_zero(subreq);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
433
434
435
436
437
438
439
440
441
  
  	return ret;
  }
  
  static int mxs_dcp_aes_enqueue(struct ablkcipher_request *req, int enc, int ecb)
  {
  	struct dcp *sdcp = global_sdcp;
  	struct crypto_async_request *arq = &req->base;
  	struct dcp_async_ctx *actx = crypto_tfm_ctx(arq->tfm);
2021abaa0   Marek Vasut   crypto: dcp - Mov...
442
  	struct dcp_aes_req_ctx *rctx = ablkcipher_request_ctx(req);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
443
444
445
446
  	int ret;
  
  	if (unlikely(actx->key_len != AES_KEYSIZE_128))
  		return mxs_dcp_block_fallback(req, enc);
2021abaa0   Marek Vasut   crypto: dcp - Mov...
447
448
  	rctx->enc = enc;
  	rctx->ecb = ecb;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
449
  	actx->chan = DCP_CHAN_CRYPTO;
d80771c08   Leonard Crestez   crypto: mxs-dcp -...
450
  	spin_lock(&sdcp->lock[actx->chan]);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
451
  	ret = crypto_enqueue_request(&sdcp->queue[actx->chan], &req->base);
d80771c08   Leonard Crestez   crypto: mxs-dcp -...
452
  	spin_unlock(&sdcp->lock[actx->chan]);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
453
454
  
  	wake_up_process(sdcp->thread[actx->chan]);
dbbaffefd   YueHaibing   crypto: mxs-dcp -...
455
  	return ret;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
  }
  
  static int mxs_dcp_aes_ecb_decrypt(struct ablkcipher_request *req)
  {
  	return mxs_dcp_aes_enqueue(req, 0, 1);
  }
  
  static int mxs_dcp_aes_ecb_encrypt(struct ablkcipher_request *req)
  {
  	return mxs_dcp_aes_enqueue(req, 1, 1);
  }
  
  static int mxs_dcp_aes_cbc_decrypt(struct ablkcipher_request *req)
  {
  	return mxs_dcp_aes_enqueue(req, 0, 0);
  }
  
  static int mxs_dcp_aes_cbc_encrypt(struct ablkcipher_request *req)
  {
  	return mxs_dcp_aes_enqueue(req, 1, 0);
  }
  
  static int mxs_dcp_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  			      unsigned int len)
  {
  	struct dcp_async_ctx *actx = crypto_ablkcipher_ctx(tfm);
  	unsigned int ret;
  
  	/*
  	 * AES 128 is supposed by the hardware, store key into temporary
  	 * buffer and exit. We must use the temporary buffer here, since
  	 * there can still be an operation in progress.
  	 */
  	actx->key_len = len;
  	if (len == AES_KEYSIZE_128) {
  		memcpy(actx->key, key, len);
  		return 0;
  	}
15b59e7c3   Marek Vasut   crypto: mxs - Add...
494
495
496
497
498
  	/*
  	 * If the requested AES key size is not supported by the hardware,
  	 * but is supported by in-kernel software implementation, we use
  	 * software fallback.
  	 */
f805f59d1   Kees Cook   crypto: mxs-dcp -...
499
500
  	crypto_sync_skcipher_clear_flags(actx->fallback, CRYPTO_TFM_REQ_MASK);
  	crypto_sync_skcipher_set_flags(actx->fallback,
29406bb92   Herbert Xu   crypto: mxs-dcp -...
501
  				  tfm->base.crt_flags & CRYPTO_TFM_REQ_MASK);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
502

f805f59d1   Kees Cook   crypto: mxs-dcp -...
503
  	ret = crypto_sync_skcipher_setkey(actx->fallback, key, len);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
504
505
506
507
  	if (!ret)
  		return 0;
  
  	tfm->base.crt_flags &= ~CRYPTO_TFM_RES_MASK;
f805f59d1   Kees Cook   crypto: mxs-dcp -...
508
  	tfm->base.crt_flags |= crypto_sync_skcipher_get_flags(actx->fallback) &
29406bb92   Herbert Xu   crypto: mxs-dcp -...
509
  			       CRYPTO_TFM_RES_MASK;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
510
511
512
513
514
515
  
  	return ret;
  }
  
  static int mxs_dcp_aes_fallback_init(struct crypto_tfm *tfm)
  {
2231204b5   Marek Vasut   crypto: dcp - tfm...
516
  	const char *name = crypto_tfm_alg_name(tfm);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
517
  	struct dcp_async_ctx *actx = crypto_tfm_ctx(tfm);
f805f59d1   Kees Cook   crypto: mxs-dcp -...
518
  	struct crypto_sync_skcipher *blk;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
519

f805f59d1   Kees Cook   crypto: mxs-dcp -...
520
  	blk = crypto_alloc_sync_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
521
522
523
524
  	if (IS_ERR(blk))
  		return PTR_ERR(blk);
  
  	actx->fallback = blk;
2021abaa0   Marek Vasut   crypto: dcp - Mov...
525
  	tfm->crt_ablkcipher.reqsize = sizeof(struct dcp_aes_req_ctx);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
526
527
528
529
530
531
  	return 0;
  }
  
  static void mxs_dcp_aes_fallback_exit(struct crypto_tfm *tfm)
  {
  	struct dcp_async_ctx *actx = crypto_tfm_ctx(tfm);
f805f59d1   Kees Cook   crypto: mxs-dcp -...
532
  	crypto_free_sync_skcipher(actx->fallback);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
533
534
535
536
537
538
539
540
541
542
543
544
545
  }
  
  /*
   * Hashing (SHA1/SHA256)
   */
  static int mxs_dcp_run_sha(struct ahash_request *req)
  {
  	struct dcp *sdcp = global_sdcp;
  	int ret;
  
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
  	struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
546
  	struct dcp_dma_desc *desc = &sdcp->coh->desc[actx->chan];
15b59e7c3   Marek Vasut   crypto: mxs - Add...
547

04d088cc0   Marek Vasut   crypto: mxs-dcp -...
548
  	dma_addr_t digest_phys = 0;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
  	dma_addr_t buf_phys = dma_map_single(sdcp->dev, sdcp->coh->sha_in_buf,
  					     DCP_BUF_SZ, DMA_TO_DEVICE);
  
  	/* Fill in the DMA descriptor. */
  	desc->control0 = MXS_DCP_CONTROL0_DECR_SEMAPHORE |
  		    MXS_DCP_CONTROL0_INTERRUPT |
  		    MXS_DCP_CONTROL0_ENABLE_HASH;
  	if (rctx->init)
  		desc->control0 |= MXS_DCP_CONTROL0_HASH_INIT;
  
  	desc->control1 = actx->alg;
  	desc->next_cmd_addr = 0;
  	desc->source = buf_phys;
  	desc->destination = 0;
  	desc->size = actx->fill;
  	desc->payload = 0;
  	desc->status = 0;
c709eebaf   Radu Solea   crypto: mxs-dcp -...
566
567
568
569
570
571
572
573
574
575
576
577
  	/*
  	 * Align driver with hw behavior when generating null hashes
  	 */
  	if (rctx->init && rctx->fini && desc->size == 0) {
  		struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
  		const uint8_t *sha_buf =
  			(actx->alg == MXS_DCP_CONTROL1_HASH_SELECT_SHA1) ?
  			sha1_null_hash : sha256_null_hash;
  		memcpy(sdcp->coh->sha_out_buf, sha_buf, halg->digestsize);
  		ret = 0;
  		goto done_run;
  	}
15b59e7c3   Marek Vasut   crypto: mxs - Add...
578
579
  	/* Set HASH_TERM bit for last transfer block. */
  	if (rctx->fini) {
c709eebaf   Radu Solea   crypto: mxs-dcp -...
580
581
  		digest_phys = dma_map_single(sdcp->dev, sdcp->coh->sha_out_buf,
  					     DCP_SHA_PAY_SZ, DMA_FROM_DEVICE);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
582
583
584
585
586
  		desc->control0 |= MXS_DCP_CONTROL0_HASH_TERM;
  		desc->payload = digest_phys;
  	}
  
  	ret = mxs_dcp_start_dma(actx);
04d088cc0   Marek Vasut   crypto: mxs-dcp -...
587
  	if (rctx->fini)
c709eebaf   Radu Solea   crypto: mxs-dcp -...
588
  		dma_unmap_single(sdcp->dev, digest_phys, DCP_SHA_PAY_SZ,
04d088cc0   Marek Vasut   crypto: mxs-dcp -...
589
  				 DMA_FROM_DEVICE);
c709eebaf   Radu Solea   crypto: mxs-dcp -...
590
  done_run:
15b59e7c3   Marek Vasut   crypto: mxs - Add...
591
592
593
594
595
596
597
598
599
600
601
602
603
604
  	dma_unmap_single(sdcp->dev, buf_phys, DCP_BUF_SZ, DMA_TO_DEVICE);
  
  	return ret;
  }
  
  static int dcp_sha_req_to_buf(struct crypto_async_request *arq)
  {
  	struct dcp *sdcp = global_sdcp;
  
  	struct ahash_request *req = ahash_request_cast(arq);
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
  	struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
  	struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
605

15b59e7c3   Marek Vasut   crypto: mxs - Add...
606
  	uint8_t *in_buf = sdcp->coh->sha_in_buf;
c709eebaf   Radu Solea   crypto: mxs-dcp -...
607
  	uint8_t *out_buf = sdcp->coh->sha_out_buf;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
608

15b59e7c3   Marek Vasut   crypto: mxs - Add...
609
  	struct scatterlist *src;
eacec15f5   Rosioru Dragos   LF-717: crypto: d...
610
  	unsigned int i, len, clen, oft = 0;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
611
612
613
614
615
  	int ret;
  
  	int fin = rctx->fini;
  	if (fin)
  		rctx->fini = 0;
eacec15f5   Rosioru Dragos   LF-717: crypto: d...
616
617
  	src = req->src;
  	len = req->nbytes;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
618

eacec15f5   Rosioru Dragos   LF-717: crypto: d...
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
  	while (len) {
  		if (actx->fill + len > DCP_BUF_SZ)
  			clen = DCP_BUF_SZ - actx->fill;
  		else
  			clen = len;
  
  		scatterwalk_map_and_copy(in_buf + actx->fill, src, oft, clen,
  					 0);
  
  		len -= clen;
  		oft += clen;
  		actx->fill += clen;
  
  		/*
  		 * If we filled the buffer and still have some
  		 * more data, submit the buffer.
  		 */
  		if (len && actx->fill == DCP_BUF_SZ) {
  			ret = mxs_dcp_run_sha(req);
  			if (ret)
  				return ret;
  			actx->fill = 0;
  			rctx->init = 0;
  		}
15b59e7c3   Marek Vasut   crypto: mxs - Add...
643
644
645
646
647
648
  	}
  
  	if (fin) {
  		rctx->fini = 1;
  
  		/* Submit whatever is left. */
04d088cc0   Marek Vasut   crypto: mxs-dcp -...
649
650
  		if (!req->result)
  			return -EINVAL;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
651
  		ret = mxs_dcp_run_sha(req);
04d088cc0   Marek Vasut   crypto: mxs-dcp -...
652
  		if (ret)
15b59e7c3   Marek Vasut   crypto: mxs - Add...
653
  			return ret;
04d088cc0   Marek Vasut   crypto: mxs-dcp -...
654

15b59e7c3   Marek Vasut   crypto: mxs - Add...
655
  		actx->fill = 0;
c709eebaf   Radu Solea   crypto: mxs-dcp -...
656
657
658
  		/* For some reason the result is flipped */
  		for (i = 0; i < halg->digestsize; i++)
  			req->result[i] = out_buf[halg->digestsize - i - 1];
15b59e7c3   Marek Vasut   crypto: mxs - Add...
659
660
661
662
663
664
665
666
667
668
669
670
  	}
  
  	return 0;
  }
  
  static int dcp_chan_thread_sha(void *data)
  {
  	struct dcp *sdcp = global_sdcp;
  	const int chan = DCP_CHAN_HASH_SHA;
  
  	struct crypto_async_request *backlog;
  	struct crypto_async_request *arq;
11fe71f14   YueHaibing   crypto: mxs-dcp -...
671
  	int ret;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
672

d80771c08   Leonard Crestez   crypto: mxs-dcp -...
673
674
  	while (!kthread_should_stop()) {
  		set_current_state(TASK_INTERRUPTIBLE);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
675

d80771c08   Leonard Crestez   crypto: mxs-dcp -...
676
  		spin_lock(&sdcp->lock[chan]);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
677
678
  		backlog = crypto_get_backlog(&sdcp->queue[chan]);
  		arq = crypto_dequeue_request(&sdcp->queue[chan]);
d80771c08   Leonard Crestez   crypto: mxs-dcp -...
679
680
681
682
683
684
685
686
  		spin_unlock(&sdcp->lock[chan]);
  
  		if (!backlog && !arq) {
  			schedule();
  			continue;
  		}
  
  		set_current_state(TASK_RUNNING);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
687
688
689
690
691
  
  		if (backlog)
  			backlog->complete(backlog, -EINPROGRESS);
  
  		if (arq) {
15b59e7c3   Marek Vasut   crypto: mxs - Add...
692
  			ret = dcp_sha_req_to_buf(arq);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
693
  			arq->complete(arq, ret);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
694
  		}
d80771c08   Leonard Crestez   crypto: mxs-dcp -...
695
  	}
15b59e7c3   Marek Vasut   crypto: mxs - Add...
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
  
  	return 0;
  }
  
  static int dcp_sha_init(struct ahash_request *req)
  {
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
  
  	struct hash_alg_common *halg = crypto_hash_alg_common(tfm);
  
  	/*
  	 * Start hashing session. The code below only inits the
  	 * hashing session context, nothing more.
  	 */
  	memset(actx, 0, sizeof(*actx));
  
  	if (strcmp(halg->base.cra_name, "sha1") == 0)
  		actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA1;
  	else
  		actx->alg = MXS_DCP_CONTROL1_HASH_SELECT_SHA256;
  
  	actx->fill = 0;
  	actx->hot = 0;
  	actx->chan = DCP_CHAN_HASH_SHA;
  
  	mutex_init(&actx->mutex);
  
  	return 0;
  }
  
  static int dcp_sha_update_fx(struct ahash_request *req, int fini)
  {
  	struct dcp *sdcp = global_sdcp;
  
  	struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
  
  	int ret;
  
  	/*
  	 * Ignore requests that have no data in them and are not
  	 * the trailing requests in the stream of requests.
  	 */
  	if (!req->nbytes && !fini)
  		return 0;
  
  	mutex_lock(&actx->mutex);
  
  	rctx->fini = fini;
  
  	if (!actx->hot) {
  		actx->hot = 1;
  		rctx->init = 1;
  	}
d80771c08   Leonard Crestez   crypto: mxs-dcp -...
752
  	spin_lock(&sdcp->lock[actx->chan]);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
753
  	ret = crypto_enqueue_request(&sdcp->queue[actx->chan], &req->base);
d80771c08   Leonard Crestez   crypto: mxs-dcp -...
754
  	spin_unlock(&sdcp->lock[actx->chan]);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
755
756
757
  
  	wake_up_process(sdcp->thread[actx->chan]);
  	mutex_unlock(&actx->mutex);
dbbaffefd   YueHaibing   crypto: mxs-dcp -...
758
  	return ret;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
  }
  
  static int dcp_sha_update(struct ahash_request *req)
  {
  	return dcp_sha_update_fx(req, 0);
  }
  
  static int dcp_sha_final(struct ahash_request *req)
  {
  	ahash_request_set_crypt(req, NULL, req->result, 0);
  	req->nbytes = 0;
  	return dcp_sha_update_fx(req, 1);
  }
  
  static int dcp_sha_finup(struct ahash_request *req)
  {
  	return dcp_sha_update_fx(req, 1);
  }
  
  static int dcp_sha_digest(struct ahash_request *req)
  {
  	int ret;
  
  	ret = dcp_sha_init(req);
  	if (ret)
  		return ret;
  
  	return dcp_sha_finup(req);
  }
ea9e7568f   Dan Douglass   crypto: mxs-dcp -...
788
  static int dcp_sha_import(struct ahash_request *req, const void *in)
9190b6fd5   Kamil Konieczny   crypto: mxs-dcp -...
789
  {
ea9e7568f   Dan Douglass   crypto: mxs-dcp -...
790
791
792
793
794
795
796
797
798
799
800
  	struct dcp_sha_req_ctx *rctx = ahash_request_ctx(req);
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct dcp_async_ctx *actx = crypto_ahash_ctx(tfm);
  	const struct dcp_export_state *export = in;
  
  	memset(rctx, 0, sizeof(struct dcp_sha_req_ctx));
  	memset(actx, 0, sizeof(struct dcp_async_ctx));
  	memcpy(rctx, &export->req_ctx, sizeof(struct dcp_sha_req_ctx));
  	memcpy(actx, &export->async_ctx, sizeof(struct dcp_async_ctx));
  
  	return 0;
9190b6fd5   Kamil Konieczny   crypto: mxs-dcp -...
801
  }
ea9e7568f   Dan Douglass   crypto: mxs-dcp -...
802
  static int dcp_sha_export(struct ahash_request *req, void *out)
9190b6fd5   Kamil Konieczny   crypto: mxs-dcp -...
803
  {
ea9e7568f   Dan Douglass   crypto: mxs-dcp -...
804
805
806
807
808
809
810
811
812
  	struct dcp_sha_req_ctx *rctx_state = ahash_request_ctx(req);
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct dcp_async_ctx *actx_state = crypto_ahash_ctx(tfm);
  	struct dcp_export_state *export = out;
  
  	memcpy(&export->req_ctx, rctx_state, sizeof(struct dcp_sha_req_ctx));
  	memcpy(&export->async_ctx, actx_state, sizeof(struct dcp_async_ctx));
  
  	return 0;
9190b6fd5   Kamil Konieczny   crypto: mxs-dcp -...
813
  }
15b59e7c3   Marek Vasut   crypto: mxs - Add...
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
  static int dcp_sha_cra_init(struct crypto_tfm *tfm)
  {
  	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  				 sizeof(struct dcp_sha_req_ctx));
  	return 0;
  }
  
  static void dcp_sha_cra_exit(struct crypto_tfm *tfm)
  {
  }
  
  /* AES 128 ECB and AES 128 CBC */
  static struct crypto_alg dcp_aes_algs[] = {
  	{
  		.cra_name		= "ecb(aes)",
  		.cra_driver_name	= "ecb-aes-dcp",
  		.cra_priority		= 400,
  		.cra_alignmask		= 15,
  		.cra_flags		= CRYPTO_ALG_TYPE_ABLKCIPHER |
  					  CRYPTO_ALG_ASYNC |
  					  CRYPTO_ALG_NEED_FALLBACK,
  		.cra_init		= mxs_dcp_aes_fallback_init,
  		.cra_exit		= mxs_dcp_aes_fallback_exit,
  		.cra_blocksize		= AES_BLOCK_SIZE,
  		.cra_ctxsize		= sizeof(struct dcp_async_ctx),
  		.cra_type		= &crypto_ablkcipher_type,
  		.cra_module		= THIS_MODULE,
  		.cra_u	= {
  			.ablkcipher = {
  				.min_keysize	= AES_MIN_KEY_SIZE,
  				.max_keysize	= AES_MAX_KEY_SIZE,
  				.setkey		= mxs_dcp_aes_setkey,
  				.encrypt	= mxs_dcp_aes_ecb_encrypt,
  				.decrypt	= mxs_dcp_aes_ecb_decrypt
  			},
  		},
  	}, {
  		.cra_name		= "cbc(aes)",
  		.cra_driver_name	= "cbc-aes-dcp",
  		.cra_priority		= 400,
  		.cra_alignmask		= 15,
  		.cra_flags		= CRYPTO_ALG_TYPE_ABLKCIPHER |
  					  CRYPTO_ALG_ASYNC |
  					  CRYPTO_ALG_NEED_FALLBACK,
  		.cra_init		= mxs_dcp_aes_fallback_init,
  		.cra_exit		= mxs_dcp_aes_fallback_exit,
  		.cra_blocksize		= AES_BLOCK_SIZE,
  		.cra_ctxsize		= sizeof(struct dcp_async_ctx),
  		.cra_type		= &crypto_ablkcipher_type,
  		.cra_module		= THIS_MODULE,
  		.cra_u = {
  			.ablkcipher = {
  				.min_keysize	= AES_MIN_KEY_SIZE,
  				.max_keysize	= AES_MAX_KEY_SIZE,
  				.setkey		= mxs_dcp_aes_setkey,
  				.encrypt	= mxs_dcp_aes_cbc_encrypt,
  				.decrypt	= mxs_dcp_aes_cbc_decrypt,
  				.ivsize		= AES_BLOCK_SIZE,
  			},
  		},
  	},
  };
  
  /* SHA1 */
  static struct ahash_alg dcp_sha1_alg = {
  	.init	= dcp_sha_init,
  	.update	= dcp_sha_update,
  	.final	= dcp_sha_final,
  	.finup	= dcp_sha_finup,
  	.digest	= dcp_sha_digest,
ea9e7568f   Dan Douglass   crypto: mxs-dcp -...
884
885
  	.import = dcp_sha_import,
  	.export = dcp_sha_export,
15b59e7c3   Marek Vasut   crypto: mxs - Add...
886
887
  	.halg	= {
  		.digestsize	= SHA1_DIGEST_SIZE,
ea9e7568f   Dan Douglass   crypto: mxs-dcp -...
888
  		.statesize	= sizeof(struct dcp_export_state),
15b59e7c3   Marek Vasut   crypto: mxs - Add...
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
  		.base		= {
  			.cra_name		= "sha1",
  			.cra_driver_name	= "sha1-dcp",
  			.cra_priority		= 400,
  			.cra_alignmask		= 63,
  			.cra_flags		= CRYPTO_ALG_ASYNC,
  			.cra_blocksize		= SHA1_BLOCK_SIZE,
  			.cra_ctxsize		= sizeof(struct dcp_async_ctx),
  			.cra_module		= THIS_MODULE,
  			.cra_init		= dcp_sha_cra_init,
  			.cra_exit		= dcp_sha_cra_exit,
  		},
  	},
  };
  
  /* SHA256 */
  static struct ahash_alg dcp_sha256_alg = {
  	.init	= dcp_sha_init,
  	.update	= dcp_sha_update,
  	.final	= dcp_sha_final,
  	.finup	= dcp_sha_finup,
  	.digest	= dcp_sha_digest,
ea9e7568f   Dan Douglass   crypto: mxs-dcp -...
911
912
  	.import = dcp_sha_import,
  	.export = dcp_sha_export,
15b59e7c3   Marek Vasut   crypto: mxs - Add...
913
914
  	.halg	= {
  		.digestsize	= SHA256_DIGEST_SIZE,
ea9e7568f   Dan Douglass   crypto: mxs-dcp -...
915
  		.statesize	= sizeof(struct dcp_export_state),
15b59e7c3   Marek Vasut   crypto: mxs - Add...
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
  		.base		= {
  			.cra_name		= "sha256",
  			.cra_driver_name	= "sha256-dcp",
  			.cra_priority		= 400,
  			.cra_alignmask		= 63,
  			.cra_flags		= CRYPTO_ALG_ASYNC,
  			.cra_blocksize		= SHA256_BLOCK_SIZE,
  			.cra_ctxsize		= sizeof(struct dcp_async_ctx),
  			.cra_module		= THIS_MODULE,
  			.cra_init		= dcp_sha_cra_init,
  			.cra_exit		= dcp_sha_cra_exit,
  		},
  	},
  };
  
  static irqreturn_t mxs_dcp_irq(int irq, void *context)
  {
  	struct dcp *sdcp = context;
  	uint32_t stat;
  	int i;
  
  	stat = readl(sdcp->base + MXS_DCP_STAT);
  	stat &= MXS_DCP_STAT_IRQ_MASK;
  	if (!stat)
  		return IRQ_NONE;
  
  	/* Clear the interrupts. */
  	writel(stat, sdcp->base + MXS_DCP_STAT_CLR);
  
  	/* Complete the DMA requests that finished. */
  	for (i = 0; i < DCP_MAX_CHANS; i++)
  		if (stat & (1 << i))
  			complete(&sdcp->completion[i]);
  
  	return IRQ_HANDLED;
  }
  
  static int mxs_dcp_probe(struct platform_device *pdev)
  {
  	struct device *dev = &pdev->dev;
  	struct dcp *sdcp = NULL;
  	int i, ret;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
958
  	int dcp_vmi_irq, dcp_irq;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
959
960
961
  	if (global_sdcp) {
  		dev_err(dev, "Only one DCP instance allowed!
  ");
5fc8005ba   Fabio Estevam   crypto: mxs-dcp -...
962
  		return -ENODEV;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
963
  	}
15b59e7c3   Marek Vasut   crypto: mxs - Add...
964
  	dcp_vmi_irq = platform_get_irq(pdev, 0);
514838e92   Stephen Boyd   crypto: drivers -...
965
  	if (dcp_vmi_irq < 0)
5fc8005ba   Fabio Estevam   crypto: mxs-dcp -...
966
  		return dcp_vmi_irq;
d9588f874   Fabio Estevam   crypto: mxs-dcp -...
967

15b59e7c3   Marek Vasut   crypto: mxs - Add...
968
  	dcp_irq = platform_get_irq(pdev, 1);
514838e92   Stephen Boyd   crypto: drivers -...
969
  	if (dcp_irq < 0)
5fc8005ba   Fabio Estevam   crypto: mxs-dcp -...
970
  		return dcp_irq;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
971
972
  
  	sdcp = devm_kzalloc(dev, sizeof(*sdcp), GFP_KERNEL);
5fc8005ba   Fabio Estevam   crypto: mxs-dcp -...
973
974
  	if (!sdcp)
  		return -ENOMEM;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
975
976
  
  	sdcp->dev = dev;
cec1caaf3   Fabio Estevam   crypto: mxs-dcp -...
977
  	sdcp->base = devm_platform_ioremap_resource(pdev, 0);
5fc8005ba   Fabio Estevam   crypto: mxs-dcp -...
978
979
  	if (IS_ERR(sdcp->base))
  		return PTR_ERR(sdcp->base);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
980
981
982
983
984
985
  
  	ret = devm_request_irq(dev, dcp_vmi_irq, mxs_dcp_irq, 0,
  			       "dcp-vmi-irq", sdcp);
  	if (ret) {
  		dev_err(dev, "Failed to claim DCP VMI IRQ!
  ");
5fc8005ba   Fabio Estevam   crypto: mxs-dcp -...
986
  		return ret;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
987
988
989
990
991
992
993
  	}
  
  	ret = devm_request_irq(dev, dcp_irq, mxs_dcp_irq, 0,
  			       "dcp-irq", sdcp);
  	if (ret) {
  		dev_err(dev, "Failed to claim DCP IRQ!
  ");
5fc8005ba   Fabio Estevam   crypto: mxs-dcp -...
994
  		return ret;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
995
996
997
  	}
  
  	/* Allocate coherent helper block. */
1a7c68561   Marek Vasut   crypto: mxs-dcp -...
998
999
  	sdcp->coh = devm_kzalloc(dev, sizeof(*sdcp->coh) + DCP_ALIGNMENT,
  				   GFP_KERNEL);
5fc8005ba   Fabio Estevam   crypto: mxs-dcp -...
1000
1001
  	if (!sdcp->coh)
  		return -ENOMEM;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
1002

1a7c68561   Marek Vasut   crypto: mxs-dcp -...
1003
1004
  	/* Re-align the structure so it fits the DCP constraints. */
  	sdcp->coh = PTR_ALIGN(sdcp->coh, DCP_ALIGNMENT);
57f002891   Leonard Crestez   crypto: mxs-dcp -...
1005
1006
1007
1008
1009
1010
1011
1012
  	/* DCP clock is optional, only used on some SOCs */
  	sdcp->dcp_clk = devm_clk_get(dev, "dcp");
  	if (IS_ERR(sdcp->dcp_clk)) {
  		if (sdcp->dcp_clk != ERR_PTR(-ENOENT))
  			return PTR_ERR(sdcp->dcp_clk);
  		sdcp->dcp_clk = NULL;
  	}
  	ret = clk_prepare_enable(sdcp->dcp_clk);
fecfd7f7e   Fabio Estevam   crypto: mxs-dcp: ...
1013
  	if (ret)
5fc8005ba   Fabio Estevam   crypto: mxs-dcp -...
1014
  		return ret;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
1015

57f002891   Leonard Crestez   crypto: mxs-dcp -...
1016
1017
1018
1019
1020
1021
1022
  	/* Restart the DCP block. */
  	ret = stmp_reset_block(sdcp->base);
  	if (ret) {
  		dev_err(dev, "Failed reset
  ");
  		goto err_disable_unprepare_clk;
  	}
15b59e7c3   Marek Vasut   crypto: mxs - Add...
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
  	/* Initialize control register. */
  	writel(MXS_DCP_CTRL_GATHER_RESIDUAL_WRITES |
  	       MXS_DCP_CTRL_ENABLE_CONTEXT_CACHING | 0xf,
  	       sdcp->base + MXS_DCP_CTRL);
  
  	/* Enable all DCP DMA channels. */
  	writel(MXS_DCP_CHANNELCTRL_ENABLE_CHANNEL_MASK,
  	       sdcp->base + MXS_DCP_CHANNELCTRL);
  
  	/*
  	 * We do not enable context switching. Give the context buffer a
  	 * pointer to an illegal address so if context switching is
  	 * inadvertantly enabled, the DCP will return an error instead of
  	 * trashing good memory. The DCP DMA cannot access ROM, so any ROM
  	 * address will do.
  	 */
  	writel(0xffff0000, sdcp->base + MXS_DCP_CONTEXT);
  	for (i = 0; i < DCP_MAX_CHANS; i++)
  		writel(0xffffffff, sdcp->base + MXS_DCP_CH_N_STAT_CLR(i));
  	writel(0xffffffff, sdcp->base + MXS_DCP_STAT_CLR);
  
  	global_sdcp = sdcp;
  
  	platform_set_drvdata(pdev, sdcp);
  
  	for (i = 0; i < DCP_MAX_CHANS; i++) {
d80771c08   Leonard Crestez   crypto: mxs-dcp -...
1049
  		spin_lock_init(&sdcp->lock[i]);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
  		init_completion(&sdcp->completion[i]);
  		crypto_init_queue(&sdcp->queue[i], 50);
  	}
  
  	/* Create the SHA and AES handler threads. */
  	sdcp->thread[DCP_CHAN_HASH_SHA] = kthread_run(dcp_chan_thread_sha,
  						      NULL, "mxs_dcp_chan/sha");
  	if (IS_ERR(sdcp->thread[DCP_CHAN_HASH_SHA])) {
  		dev_err(dev, "Error starting SHA thread!
  ");
57f002891   Leonard Crestez   crypto: mxs-dcp -...
1060
1061
  		ret = PTR_ERR(sdcp->thread[DCP_CHAN_HASH_SHA]);
  		goto err_disable_unprepare_clk;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
  	}
  
  	sdcp->thread[DCP_CHAN_CRYPTO] = kthread_run(dcp_chan_thread_aes,
  						    NULL, "mxs_dcp_chan/aes");
  	if (IS_ERR(sdcp->thread[DCP_CHAN_CRYPTO])) {
  		dev_err(dev, "Error starting SHA thread!
  ");
  		ret = PTR_ERR(sdcp->thread[DCP_CHAN_CRYPTO]);
  		goto err_destroy_sha_thread;
  	}
  
  	/* Register the various crypto algorithms. */
  	sdcp->caps = readl(sdcp->base + MXS_DCP_CAPABILITY1);
  
  	if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128) {
  		ret = crypto_register_algs(dcp_aes_algs,
  					   ARRAY_SIZE(dcp_aes_algs));
  		if (ret) {
  			/* Failed to register algorithm. */
  			dev_err(dev, "Failed to register AES crypto!
  ");
  			goto err_destroy_aes_thread;
  		}
  	}
  
  	if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1) {
  		ret = crypto_register_ahash(&dcp_sha1_alg);
  		if (ret) {
  			dev_err(dev, "Failed to register %s hash!
  ",
  				dcp_sha1_alg.halg.base.cra_name);
  			goto err_unregister_aes;
  		}
  	}
  
  	if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256) {
  		ret = crypto_register_ahash(&dcp_sha256_alg);
  		if (ret) {
  			dev_err(dev, "Failed to register %s hash!
  ",
  				dcp_sha256_alg.halg.base.cra_name);
  			goto err_unregister_sha1;
  		}
  	}
  
  	return 0;
  
  err_unregister_sha1:
  	if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1)
  		crypto_unregister_ahash(&dcp_sha1_alg);
  
  err_unregister_aes:
  	if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128)
  		crypto_unregister_algs(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs));
  
  err_destroy_aes_thread:
  	kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]);
  
  err_destroy_sha_thread:
  	kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]);
57f002891   Leonard Crestez   crypto: mxs-dcp -...
1122
1123
1124
  
  err_disable_unprepare_clk:
  	clk_disable_unprepare(sdcp->dcp_clk);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
1125
1126
1127
1128
1129
1130
  	return ret;
  }
  
  static int mxs_dcp_remove(struct platform_device *pdev)
  {
  	struct dcp *sdcp = platform_get_drvdata(pdev);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
  	if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA256)
  		crypto_unregister_ahash(&dcp_sha256_alg);
  
  	if (sdcp->caps & MXS_DCP_CAPABILITY1_SHA1)
  		crypto_unregister_ahash(&dcp_sha1_alg);
  
  	if (sdcp->caps & MXS_DCP_CAPABILITY1_AES128)
  		crypto_unregister_algs(dcp_aes_algs, ARRAY_SIZE(dcp_aes_algs));
  
  	kthread_stop(sdcp->thread[DCP_CHAN_HASH_SHA]);
  	kthread_stop(sdcp->thread[DCP_CHAN_CRYPTO]);
57f002891   Leonard Crestez   crypto: mxs-dcp -...
1142
  	clk_disable_unprepare(sdcp->dcp_clk);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
1143
  	platform_set_drvdata(pdev, NULL);
15b59e7c3   Marek Vasut   crypto: mxs - Add...
1144
  	global_sdcp = NULL;
15b59e7c3   Marek Vasut   crypto: mxs - Add...
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
  
  	return 0;
  }
  
  static const struct of_device_id mxs_dcp_dt_ids[] = {
  	{ .compatible = "fsl,imx23-dcp", .data = NULL, },
  	{ .compatible = "fsl,imx28-dcp", .data = NULL, },
  	{ /* sentinel */ }
  };
  
  MODULE_DEVICE_TABLE(of, mxs_dcp_dt_ids);
  
  static struct platform_driver mxs_dcp_driver = {
  	.probe	= mxs_dcp_probe,
  	.remove	= mxs_dcp_remove,
  	.driver	= {
  		.name		= "mxs-dcp",
15b59e7c3   Marek Vasut   crypto: mxs - Add...
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
  		.of_match_table	= mxs_dcp_dt_ids,
  	},
  };
  
  module_platform_driver(mxs_dcp_driver);
  
  MODULE_AUTHOR("Marek Vasut <marex@denx.de>");
  MODULE_DESCRIPTION("Freescale MXS DCP Driver");
  MODULE_LICENSE("GPL");
  MODULE_ALIAS("platform:mxs-dcp");