xref: /openbmc/linux/drivers/crypto/omap-des.c (revision 1d1997db)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Support for OMAP DES and Triple DES HW acceleration.
4  *
5  * Copyright (c) 2013 Texas Instruments Incorporated
6  * Author: Joel Fernandes <joelf@ti.com>
7  */
8 
9 #define pr_fmt(fmt) "%s: " fmt, __func__
10 
11 #ifdef DEBUG
12 #define prn(num) printk(#num "=%d\n", num)
13 #define prx(num) printk(#num "=%x\n", num)
14 #else
15 #define prn(num) do { } while (0)
16 #define prx(num)  do { } while (0)
17 #endif
18 
19 #include <linux/err.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/errno.h>
23 #include <linux/kernel.h>
24 #include <linux/platform_device.h>
25 #include <linux/scatterlist.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/dmaengine.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/of.h>
30 #include <linux/of_device.h>
31 #include <linux/of_address.h>
32 #include <linux/io.h>
33 #include <linux/crypto.h>
34 #include <linux/interrupt.h>
35 #include <crypto/scatterwalk.h>
36 #include <crypto/internal/des.h>
37 #include <crypto/internal/skcipher.h>
38 #include <crypto/algapi.h>
39 #include <crypto/engine.h>
40 
41 #include "omap-crypto.h"
42 
43 #define DST_MAXBURST			2
44 
45 #define DES_BLOCK_WORDS		(DES_BLOCK_SIZE >> 2)
46 
47 #define _calc_walked(inout) (dd->inout##_walk.offset - dd->inout##_sg->offset)
48 
49 #define DES_REG_KEY(dd, x)		((dd)->pdata->key_ofs - \
50 						((x ^ 0x01) * 0x04))
51 
52 #define DES_REG_IV(dd, x)		((dd)->pdata->iv_ofs + ((x) * 0x04))
53 
54 #define DES_REG_CTRL(dd)		((dd)->pdata->ctrl_ofs)
55 #define DES_REG_CTRL_CBC		BIT(4)
56 #define DES_REG_CTRL_TDES		BIT(3)
57 #define DES_REG_CTRL_DIRECTION		BIT(2)
58 #define DES_REG_CTRL_INPUT_READY	BIT(1)
59 #define DES_REG_CTRL_OUTPUT_READY	BIT(0)
60 
61 #define DES_REG_DATA_N(dd, x)		((dd)->pdata->data_ofs + ((x) * 0x04))
62 
63 #define DES_REG_REV(dd)			((dd)->pdata->rev_ofs)
64 
65 #define DES_REG_MASK(dd)		((dd)->pdata->mask_ofs)
66 
67 #define DES_REG_LENGTH_N(x)		(0x24 + ((x) * 0x04))
68 
69 #define DES_REG_IRQ_STATUS(dd)         ((dd)->pdata->irq_status_ofs)
70 #define DES_REG_IRQ_ENABLE(dd)         ((dd)->pdata->irq_enable_ofs)
71 #define DES_REG_IRQ_DATA_IN            BIT(1)
72 #define DES_REG_IRQ_DATA_OUT           BIT(2)
73 
74 #define FLAGS_MODE_MASK		0x000f
75 #define FLAGS_ENCRYPT		BIT(0)
76 #define FLAGS_CBC		BIT(1)
77 #define FLAGS_INIT		BIT(4)
78 #define FLAGS_BUSY		BIT(6)
79 
80 #define DEFAULT_AUTOSUSPEND_DELAY	1000
81 
82 #define FLAGS_IN_DATA_ST_SHIFT	8
83 #define FLAGS_OUT_DATA_ST_SHIFT	10
84 
85 struct omap_des_ctx {
86 	struct crypto_engine_ctx enginectx;
87 	struct omap_des_dev *dd;
88 
89 	int		keylen;
90 	u32		key[(3 * DES_KEY_SIZE) / sizeof(u32)];
91 	unsigned long	flags;
92 };
93 
94 struct omap_des_reqctx {
95 	unsigned long mode;
96 };
97 
98 #define OMAP_DES_QUEUE_LENGTH	1
99 #define OMAP_DES_CACHE_SIZE	0
100 
101 struct omap_des_algs_info {
102 	struct skcipher_alg	*algs_list;
103 	unsigned int		size;
104 	unsigned int		registered;
105 };
106 
107 struct omap_des_pdata {
108 	struct omap_des_algs_info	*algs_info;
109 	unsigned int	algs_info_size;
110 
111 	void		(*trigger)(struct omap_des_dev *dd, int length);
112 
113 	u32		key_ofs;
114 	u32		iv_ofs;
115 	u32		ctrl_ofs;
116 	u32		data_ofs;
117 	u32		rev_ofs;
118 	u32		mask_ofs;
119 	u32             irq_enable_ofs;
120 	u32             irq_status_ofs;
121 
122 	u32		dma_enable_in;
123 	u32		dma_enable_out;
124 	u32		dma_start;
125 
126 	u32		major_mask;
127 	u32		major_shift;
128 	u32		minor_mask;
129 	u32		minor_shift;
130 };
131 
132 struct omap_des_dev {
133 	struct list_head	list;
134 	unsigned long		phys_base;
135 	void __iomem		*io_base;
136 	struct omap_des_ctx	*ctx;
137 	struct device		*dev;
138 	unsigned long		flags;
139 	int			err;
140 
141 	struct tasklet_struct	done_task;
142 
143 	struct skcipher_request	*req;
144 	struct crypto_engine		*engine;
145 	/*
146 	 * total is used by PIO mode for book keeping so introduce
147 	 * variable total_save as need it to calc page_order
148 	 */
149 	size_t                          total;
150 	size_t                          total_save;
151 
152 	struct scatterlist		*in_sg;
153 	struct scatterlist		*out_sg;
154 
155 	/* Buffers for copying for unaligned cases */
156 	struct scatterlist		in_sgl;
157 	struct scatterlist		out_sgl;
158 	struct scatterlist		*orig_out;
159 
160 	struct scatter_walk		in_walk;
161 	struct scatter_walk		out_walk;
162 	struct dma_chan		*dma_lch_in;
163 	struct dma_chan		*dma_lch_out;
164 	int			in_sg_len;
165 	int			out_sg_len;
166 	int			pio_only;
167 	const struct omap_des_pdata	*pdata;
168 };
169 
170 /* keep registered devices data here */
171 static LIST_HEAD(dev_list);
172 static DEFINE_SPINLOCK(list_lock);
173 
174 #ifdef DEBUG
175 #define omap_des_read(dd, offset)                               \
176 	({                                                              \
177 	 int _read_ret;                                          \
178 	 _read_ret = __raw_readl(dd->io_base + offset);          \
179 	 pr_err("omap_des_read(" #offset "=%#x)= %#x\n",       \
180 		 offset, _read_ret);                            \
181 	 _read_ret;                                              \
182 	 })
183 #else
184 static inline u32 omap_des_read(struct omap_des_dev *dd, u32 offset)
185 {
186 	return __raw_readl(dd->io_base + offset);
187 }
188 #endif
189 
190 #ifdef DEBUG
191 #define omap_des_write(dd, offset, value)                               \
192 	do {                                                            \
193 		pr_err("omap_des_write(" #offset "=%#x) value=%#x\n", \
194 				offset, value);                                \
195 		__raw_writel(value, dd->io_base + offset);              \
196 	} while (0)
197 #else
198 static inline void omap_des_write(struct omap_des_dev *dd, u32 offset,
199 		u32 value)
200 {
201 	__raw_writel(value, dd->io_base + offset);
202 }
203 #endif
204 
205 static inline void omap_des_write_mask(struct omap_des_dev *dd, u32 offset,
206 					u32 value, u32 mask)
207 {
208 	u32 val;
209 
210 	val = omap_des_read(dd, offset);
211 	val &= ~mask;
212 	val |= value;
213 	omap_des_write(dd, offset, val);
214 }
215 
216 static void omap_des_write_n(struct omap_des_dev *dd, u32 offset,
217 					u32 *value, int count)
218 {
219 	for (; count--; value++, offset += 4)
220 		omap_des_write(dd, offset, *value);
221 }
222 
223 static int omap_des_hw_init(struct omap_des_dev *dd)
224 {
225 	int err;
226 
227 	/*
228 	 * clocks are enabled when request starts and disabled when finished.
229 	 * It may be long delays between requests.
230 	 * Device might go to off mode to save power.
231 	 */
232 	err = pm_runtime_get_sync(dd->dev);
233 	if (err < 0) {
234 		pm_runtime_put_noidle(dd->dev);
235 		dev_err(dd->dev, "%s: failed to get_sync(%d)\n", __func__, err);
236 		return err;
237 	}
238 
239 	if (!(dd->flags & FLAGS_INIT)) {
240 		dd->flags |= FLAGS_INIT;
241 		dd->err = 0;
242 	}
243 
244 	return 0;
245 }
246 
247 static int omap_des_write_ctrl(struct omap_des_dev *dd)
248 {
249 	unsigned int key32;
250 	int i, err;
251 	u32 val = 0, mask = 0;
252 
253 	err = omap_des_hw_init(dd);
254 	if (err)
255 		return err;
256 
257 	key32 = dd->ctx->keylen / sizeof(u32);
258 
259 	/* it seems a key should always be set even if it has not changed */
260 	for (i = 0; i < key32; i++) {
261 		omap_des_write(dd, DES_REG_KEY(dd, i),
262 			       __le32_to_cpu(dd->ctx->key[i]));
263 	}
264 
265 	if ((dd->flags & FLAGS_CBC) && dd->req->iv)
266 		omap_des_write_n(dd, DES_REG_IV(dd, 0), (void *)dd->req->iv, 2);
267 
268 	if (dd->flags & FLAGS_CBC)
269 		val |= DES_REG_CTRL_CBC;
270 	if (dd->flags & FLAGS_ENCRYPT)
271 		val |= DES_REG_CTRL_DIRECTION;
272 	if (key32 == 6)
273 		val |= DES_REG_CTRL_TDES;
274 
275 	mask |= DES_REG_CTRL_CBC | DES_REG_CTRL_DIRECTION | DES_REG_CTRL_TDES;
276 
277 	omap_des_write_mask(dd, DES_REG_CTRL(dd), val, mask);
278 
279 	return 0;
280 }
281 
282 static void omap_des_dma_trigger_omap4(struct omap_des_dev *dd, int length)
283 {
284 	u32 mask, val;
285 
286 	omap_des_write(dd, DES_REG_LENGTH_N(0), length);
287 
288 	val = dd->pdata->dma_start;
289 
290 	if (dd->dma_lch_out != NULL)
291 		val |= dd->pdata->dma_enable_out;
292 	if (dd->dma_lch_in != NULL)
293 		val |= dd->pdata->dma_enable_in;
294 
295 	mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
296 	       dd->pdata->dma_start;
297 
298 	omap_des_write_mask(dd, DES_REG_MASK(dd), val, mask);
299 }
300 
301 static void omap_des_dma_stop(struct omap_des_dev *dd)
302 {
303 	u32 mask;
304 
305 	mask = dd->pdata->dma_enable_out | dd->pdata->dma_enable_in |
306 	       dd->pdata->dma_start;
307 
308 	omap_des_write_mask(dd, DES_REG_MASK(dd), 0, mask);
309 }
310 
311 static struct omap_des_dev *omap_des_find_dev(struct omap_des_ctx *ctx)
312 {
313 	struct omap_des_dev *dd = NULL, *tmp;
314 
315 	spin_lock_bh(&list_lock);
316 	if (!ctx->dd) {
317 		list_for_each_entry(tmp, &dev_list, list) {
318 			/* FIXME: take fist available des core */
319 			dd = tmp;
320 			break;
321 		}
322 		ctx->dd = dd;
323 	} else {
324 		/* already found before */
325 		dd = ctx->dd;
326 	}
327 	spin_unlock_bh(&list_lock);
328 
329 	return dd;
330 }
331 
332 static void omap_des_dma_out_callback(void *data)
333 {
334 	struct omap_des_dev *dd = data;
335 
336 	/* dma_lch_out - completed */
337 	tasklet_schedule(&dd->done_task);
338 }
339 
340 static int omap_des_dma_init(struct omap_des_dev *dd)
341 {
342 	int err;
343 
344 	dd->dma_lch_out = NULL;
345 	dd->dma_lch_in = NULL;
346 
347 	dd->dma_lch_in = dma_request_chan(dd->dev, "rx");
348 	if (IS_ERR(dd->dma_lch_in)) {
349 		dev_err(dd->dev, "Unable to request in DMA channel\n");
350 		return PTR_ERR(dd->dma_lch_in);
351 	}
352 
353 	dd->dma_lch_out = dma_request_chan(dd->dev, "tx");
354 	if (IS_ERR(dd->dma_lch_out)) {
355 		dev_err(dd->dev, "Unable to request out DMA channel\n");
356 		err = PTR_ERR(dd->dma_lch_out);
357 		goto err_dma_out;
358 	}
359 
360 	return 0;
361 
362 err_dma_out:
363 	dma_release_channel(dd->dma_lch_in);
364 
365 	return err;
366 }
367 
368 static void omap_des_dma_cleanup(struct omap_des_dev *dd)
369 {
370 	if (dd->pio_only)
371 		return;
372 
373 	dma_release_channel(dd->dma_lch_out);
374 	dma_release_channel(dd->dma_lch_in);
375 }
376 
377 static int omap_des_crypt_dma(struct crypto_tfm *tfm,
378 		struct scatterlist *in_sg, struct scatterlist *out_sg,
379 		int in_sg_len, int out_sg_len)
380 {
381 	struct omap_des_ctx *ctx = crypto_tfm_ctx(tfm);
382 	struct omap_des_dev *dd = ctx->dd;
383 	struct dma_async_tx_descriptor *tx_in, *tx_out;
384 	struct dma_slave_config cfg;
385 	int ret;
386 
387 	if (dd->pio_only) {
388 		scatterwalk_start(&dd->in_walk, dd->in_sg);
389 		scatterwalk_start(&dd->out_walk, dd->out_sg);
390 
391 		/* Enable DATAIN interrupt and let it take
392 		   care of the rest */
393 		omap_des_write(dd, DES_REG_IRQ_ENABLE(dd), 0x2);
394 		return 0;
395 	}
396 
397 	dma_sync_sg_for_device(dd->dev, dd->in_sg, in_sg_len, DMA_TO_DEVICE);
398 
399 	memset(&cfg, 0, sizeof(cfg));
400 
401 	cfg.src_addr = dd->phys_base + DES_REG_DATA_N(dd, 0);
402 	cfg.dst_addr = dd->phys_base + DES_REG_DATA_N(dd, 0);
403 	cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
404 	cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
405 	cfg.src_maxburst = DST_MAXBURST;
406 	cfg.dst_maxburst = DST_MAXBURST;
407 
408 	/* IN */
409 	ret = dmaengine_slave_config(dd->dma_lch_in, &cfg);
410 	if (ret) {
411 		dev_err(dd->dev, "can't configure IN dmaengine slave: %d\n",
412 			ret);
413 		return ret;
414 	}
415 
416 	tx_in = dmaengine_prep_slave_sg(dd->dma_lch_in, in_sg, in_sg_len,
417 					DMA_MEM_TO_DEV,
418 					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
419 	if (!tx_in) {
420 		dev_err(dd->dev, "IN prep_slave_sg() failed\n");
421 		return -EINVAL;
422 	}
423 
424 	/* No callback necessary */
425 	tx_in->callback_param = dd;
426 
427 	/* OUT */
428 	ret = dmaengine_slave_config(dd->dma_lch_out, &cfg);
429 	if (ret) {
430 		dev_err(dd->dev, "can't configure OUT dmaengine slave: %d\n",
431 			ret);
432 		return ret;
433 	}
434 
435 	tx_out = dmaengine_prep_slave_sg(dd->dma_lch_out, out_sg, out_sg_len,
436 					DMA_DEV_TO_MEM,
437 					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
438 	if (!tx_out) {
439 		dev_err(dd->dev, "OUT prep_slave_sg() failed\n");
440 		return -EINVAL;
441 	}
442 
443 	tx_out->callback = omap_des_dma_out_callback;
444 	tx_out->callback_param = dd;
445 
446 	dmaengine_submit(tx_in);
447 	dmaengine_submit(tx_out);
448 
449 	dma_async_issue_pending(dd->dma_lch_in);
450 	dma_async_issue_pending(dd->dma_lch_out);
451 
452 	/* start DMA */
453 	dd->pdata->trigger(dd, dd->total);
454 
455 	return 0;
456 }
457 
458 static int omap_des_crypt_dma_start(struct omap_des_dev *dd)
459 {
460 	struct crypto_tfm *tfm = crypto_skcipher_tfm(
461 					crypto_skcipher_reqtfm(dd->req));
462 	int err;
463 
464 	pr_debug("total: %d\n", dd->total);
465 
466 	if (!dd->pio_only) {
467 		err = dma_map_sg(dd->dev, dd->in_sg, dd->in_sg_len,
468 				 DMA_TO_DEVICE);
469 		if (!err) {
470 			dev_err(dd->dev, "dma_map_sg() error\n");
471 			return -EINVAL;
472 		}
473 
474 		err = dma_map_sg(dd->dev, dd->out_sg, dd->out_sg_len,
475 				 DMA_FROM_DEVICE);
476 		if (!err) {
477 			dev_err(dd->dev, "dma_map_sg() error\n");
478 			return -EINVAL;
479 		}
480 	}
481 
482 	err = omap_des_crypt_dma(tfm, dd->in_sg, dd->out_sg, dd->in_sg_len,
483 				 dd->out_sg_len);
484 	if (err && !dd->pio_only) {
485 		dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
486 		dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
487 			     DMA_FROM_DEVICE);
488 	}
489 
490 	return err;
491 }
492 
493 static void omap_des_finish_req(struct omap_des_dev *dd, int err)
494 {
495 	struct skcipher_request *req = dd->req;
496 
497 	pr_debug("err: %d\n", err);
498 
499 	crypto_finalize_skcipher_request(dd->engine, req, err);
500 
501 	pm_runtime_mark_last_busy(dd->dev);
502 	pm_runtime_put_autosuspend(dd->dev);
503 }
504 
505 static int omap_des_crypt_dma_stop(struct omap_des_dev *dd)
506 {
507 	pr_debug("total: %d\n", dd->total);
508 
509 	omap_des_dma_stop(dd);
510 
511 	dmaengine_terminate_all(dd->dma_lch_in);
512 	dmaengine_terminate_all(dd->dma_lch_out);
513 
514 	return 0;
515 }
516 
517 static int omap_des_handle_queue(struct omap_des_dev *dd,
518 				 struct skcipher_request *req)
519 {
520 	if (req)
521 		return crypto_transfer_skcipher_request_to_engine(dd->engine, req);
522 
523 	return 0;
524 }
525 
526 static int omap_des_prepare_req(struct crypto_engine *engine,
527 				void *areq)
528 {
529 	struct skcipher_request *req = container_of(areq, struct skcipher_request, base);
530 	struct omap_des_ctx *ctx = crypto_skcipher_ctx(
531 			crypto_skcipher_reqtfm(req));
532 	struct omap_des_dev *dd = omap_des_find_dev(ctx);
533 	struct omap_des_reqctx *rctx;
534 	int ret;
535 	u16 flags;
536 
537 	if (!dd)
538 		return -ENODEV;
539 
540 	/* assign new request to device */
541 	dd->req = req;
542 	dd->total = req->cryptlen;
543 	dd->total_save = req->cryptlen;
544 	dd->in_sg = req->src;
545 	dd->out_sg = req->dst;
546 	dd->orig_out = req->dst;
547 
548 	flags = OMAP_CRYPTO_COPY_DATA;
549 	if (req->src == req->dst)
550 		flags |= OMAP_CRYPTO_FORCE_COPY;
551 
552 	ret = omap_crypto_align_sg(&dd->in_sg, dd->total, DES_BLOCK_SIZE,
553 				   &dd->in_sgl, flags,
554 				   FLAGS_IN_DATA_ST_SHIFT, &dd->flags);
555 	if (ret)
556 		return ret;
557 
558 	ret = omap_crypto_align_sg(&dd->out_sg, dd->total, DES_BLOCK_SIZE,
559 				   &dd->out_sgl, 0,
560 				   FLAGS_OUT_DATA_ST_SHIFT, &dd->flags);
561 	if (ret)
562 		return ret;
563 
564 	dd->in_sg_len = sg_nents_for_len(dd->in_sg, dd->total);
565 	if (dd->in_sg_len < 0)
566 		return dd->in_sg_len;
567 
568 	dd->out_sg_len = sg_nents_for_len(dd->out_sg, dd->total);
569 	if (dd->out_sg_len < 0)
570 		return dd->out_sg_len;
571 
572 	rctx = skcipher_request_ctx(req);
573 	ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
574 	rctx->mode &= FLAGS_MODE_MASK;
575 	dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
576 
577 	dd->ctx = ctx;
578 	ctx->dd = dd;
579 
580 	return omap_des_write_ctrl(dd);
581 }
582 
583 static int omap_des_crypt_req(struct crypto_engine *engine,
584 			      void *areq)
585 {
586 	struct skcipher_request *req = container_of(areq, struct skcipher_request, base);
587 	struct omap_des_ctx *ctx = crypto_skcipher_ctx(
588 			crypto_skcipher_reqtfm(req));
589 	struct omap_des_dev *dd = omap_des_find_dev(ctx);
590 
591 	if (!dd)
592 		return -ENODEV;
593 
594 	return omap_des_crypt_dma_start(dd);
595 }
596 
597 static void omap_des_done_task(unsigned long data)
598 {
599 	struct omap_des_dev *dd = (struct omap_des_dev *)data;
600 
601 	pr_debug("enter done_task\n");
602 
603 	if (!dd->pio_only) {
604 		dma_sync_sg_for_device(dd->dev, dd->out_sg, dd->out_sg_len,
605 				       DMA_FROM_DEVICE);
606 		dma_unmap_sg(dd->dev, dd->in_sg, dd->in_sg_len, DMA_TO_DEVICE);
607 		dma_unmap_sg(dd->dev, dd->out_sg, dd->out_sg_len,
608 			     DMA_FROM_DEVICE);
609 		omap_des_crypt_dma_stop(dd);
610 	}
611 
612 	omap_crypto_cleanup(&dd->in_sgl, NULL, 0, dd->total_save,
613 			    FLAGS_IN_DATA_ST_SHIFT, dd->flags);
614 
615 	omap_crypto_cleanup(&dd->out_sgl, dd->orig_out, 0, dd->total_save,
616 			    FLAGS_OUT_DATA_ST_SHIFT, dd->flags);
617 
618 	omap_des_finish_req(dd, 0);
619 
620 	pr_debug("exit\n");
621 }
622 
623 static int omap_des_crypt(struct skcipher_request *req, unsigned long mode)
624 {
625 	struct omap_des_ctx *ctx = crypto_skcipher_ctx(
626 			crypto_skcipher_reqtfm(req));
627 	struct omap_des_reqctx *rctx = skcipher_request_ctx(req);
628 	struct omap_des_dev *dd;
629 
630 	pr_debug("nbytes: %d, enc: %d, cbc: %d\n", req->cryptlen,
631 		 !!(mode & FLAGS_ENCRYPT),
632 		 !!(mode & FLAGS_CBC));
633 
634 	if (!IS_ALIGNED(req->cryptlen, DES_BLOCK_SIZE)) {
635 		pr_err("request size is not exact amount of DES blocks\n");
636 		return -EINVAL;
637 	}
638 
639 	dd = omap_des_find_dev(ctx);
640 	if (!dd)
641 		return -ENODEV;
642 
643 	rctx->mode = mode;
644 
645 	return omap_des_handle_queue(dd, req);
646 }
647 
648 /* ********************** ALG API ************************************ */
649 
650 static int omap_des_setkey(struct crypto_skcipher *cipher, const u8 *key,
651 			   unsigned int keylen)
652 {
653 	struct omap_des_ctx *ctx = crypto_skcipher_ctx(cipher);
654 	int err;
655 
656 	pr_debug("enter, keylen: %d\n", keylen);
657 
658 	err = verify_skcipher_des_key(cipher, key);
659 	if (err)
660 		return err;
661 
662 	memcpy(ctx->key, key, keylen);
663 	ctx->keylen = keylen;
664 
665 	return 0;
666 }
667 
668 static int omap_des3_setkey(struct crypto_skcipher *cipher, const u8 *key,
669 			    unsigned int keylen)
670 {
671 	struct omap_des_ctx *ctx = crypto_skcipher_ctx(cipher);
672 	int err;
673 
674 	pr_debug("enter, keylen: %d\n", keylen);
675 
676 	err = verify_skcipher_des3_key(cipher, key);
677 	if (err)
678 		return err;
679 
680 	memcpy(ctx->key, key, keylen);
681 	ctx->keylen = keylen;
682 
683 	return 0;
684 }
685 
686 static int omap_des_ecb_encrypt(struct skcipher_request *req)
687 {
688 	return omap_des_crypt(req, FLAGS_ENCRYPT);
689 }
690 
691 static int omap_des_ecb_decrypt(struct skcipher_request *req)
692 {
693 	return omap_des_crypt(req, 0);
694 }
695 
696 static int omap_des_cbc_encrypt(struct skcipher_request *req)
697 {
698 	return omap_des_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
699 }
700 
701 static int omap_des_cbc_decrypt(struct skcipher_request *req)
702 {
703 	return omap_des_crypt(req, FLAGS_CBC);
704 }
705 
706 static int omap_des_prepare_req(struct crypto_engine *engine,
707 				void *areq);
708 static int omap_des_crypt_req(struct crypto_engine *engine,
709 			      void *areq);
710 
711 static int omap_des_init_tfm(struct crypto_skcipher *tfm)
712 {
713 	struct omap_des_ctx *ctx = crypto_skcipher_ctx(tfm);
714 
715 	pr_debug("enter\n");
716 
717 	crypto_skcipher_set_reqsize(tfm, sizeof(struct omap_des_reqctx));
718 
719 	ctx->enginectx.op.prepare_request = omap_des_prepare_req;
720 	ctx->enginectx.op.unprepare_request = NULL;
721 	ctx->enginectx.op.do_one_request = omap_des_crypt_req;
722 
723 	return 0;
724 }
725 
726 /* ********************** ALGS ************************************ */
727 
728 static struct skcipher_alg algs_ecb_cbc[] = {
729 {
730 	.base.cra_name		= "ecb(des)",
731 	.base.cra_driver_name	= "ecb-des-omap",
732 	.base.cra_priority	= 100,
733 	.base.cra_flags		= CRYPTO_ALG_KERN_DRIVER_ONLY |
734 				  CRYPTO_ALG_ASYNC,
735 	.base.cra_blocksize	= DES_BLOCK_SIZE,
736 	.base.cra_ctxsize	= sizeof(struct omap_des_ctx),
737 	.base.cra_module	= THIS_MODULE,
738 
739 	.min_keysize		= DES_KEY_SIZE,
740 	.max_keysize		= DES_KEY_SIZE,
741 	.setkey			= omap_des_setkey,
742 	.encrypt		= omap_des_ecb_encrypt,
743 	.decrypt		= omap_des_ecb_decrypt,
744 	.init			= omap_des_init_tfm,
745 },
746 {
747 	.base.cra_name		= "cbc(des)",
748 	.base.cra_driver_name	= "cbc-des-omap",
749 	.base.cra_priority	= 100,
750 	.base.cra_flags		= CRYPTO_ALG_KERN_DRIVER_ONLY |
751 				  CRYPTO_ALG_ASYNC,
752 	.base.cra_blocksize	= DES_BLOCK_SIZE,
753 	.base.cra_ctxsize	= sizeof(struct omap_des_ctx),
754 	.base.cra_module	= THIS_MODULE,
755 
756 	.min_keysize		= DES_KEY_SIZE,
757 	.max_keysize		= DES_KEY_SIZE,
758 	.ivsize			= DES_BLOCK_SIZE,
759 	.setkey			= omap_des_setkey,
760 	.encrypt		= omap_des_cbc_encrypt,
761 	.decrypt		= omap_des_cbc_decrypt,
762 	.init			= omap_des_init_tfm,
763 },
764 {
765 	.base.cra_name		= "ecb(des3_ede)",
766 	.base.cra_driver_name	= "ecb-des3-omap",
767 	.base.cra_priority	= 100,
768 	.base.cra_flags		= CRYPTO_ALG_KERN_DRIVER_ONLY |
769 				  CRYPTO_ALG_ASYNC,
770 	.base.cra_blocksize	= DES3_EDE_BLOCK_SIZE,
771 	.base.cra_ctxsize	= sizeof(struct omap_des_ctx),
772 	.base.cra_module	= THIS_MODULE,
773 
774 	.min_keysize		= DES3_EDE_KEY_SIZE,
775 	.max_keysize		= DES3_EDE_KEY_SIZE,
776 	.setkey			= omap_des3_setkey,
777 	.encrypt		= omap_des_ecb_encrypt,
778 	.decrypt		= omap_des_ecb_decrypt,
779 	.init			= omap_des_init_tfm,
780 },
781 {
782 	.base.cra_name		= "cbc(des3_ede)",
783 	.base.cra_driver_name	= "cbc-des3-omap",
784 	.base.cra_priority	= 100,
785 	.base.cra_flags		= CRYPTO_ALG_KERN_DRIVER_ONLY |
786 				  CRYPTO_ALG_ASYNC,
787 	.base.cra_blocksize	= DES3_EDE_BLOCK_SIZE,
788 	.base.cra_ctxsize	= sizeof(struct omap_des_ctx),
789 	.base.cra_module	= THIS_MODULE,
790 
791 	.min_keysize		= DES3_EDE_KEY_SIZE,
792 	.max_keysize		= DES3_EDE_KEY_SIZE,
793 	.ivsize			= DES3_EDE_BLOCK_SIZE,
794 	.setkey			= omap_des3_setkey,
795 	.encrypt		= omap_des_cbc_encrypt,
796 	.decrypt		= omap_des_cbc_decrypt,
797 	.init			= omap_des_init_tfm,
798 }
799 };
800 
801 static struct omap_des_algs_info omap_des_algs_info_ecb_cbc[] = {
802 	{
803 		.algs_list	= algs_ecb_cbc,
804 		.size		= ARRAY_SIZE(algs_ecb_cbc),
805 	},
806 };
807 
808 #ifdef CONFIG_OF
809 static const struct omap_des_pdata omap_des_pdata_omap4 = {
810 	.algs_info	= omap_des_algs_info_ecb_cbc,
811 	.algs_info_size	= ARRAY_SIZE(omap_des_algs_info_ecb_cbc),
812 	.trigger	= omap_des_dma_trigger_omap4,
813 	.key_ofs	= 0x14,
814 	.iv_ofs		= 0x18,
815 	.ctrl_ofs	= 0x20,
816 	.data_ofs	= 0x28,
817 	.rev_ofs	= 0x30,
818 	.mask_ofs	= 0x34,
819 	.irq_status_ofs = 0x3c,
820 	.irq_enable_ofs = 0x40,
821 	.dma_enable_in	= BIT(5),
822 	.dma_enable_out	= BIT(6),
823 	.major_mask	= 0x0700,
824 	.major_shift	= 8,
825 	.minor_mask	= 0x003f,
826 	.minor_shift	= 0,
827 };
828 
829 static irqreturn_t omap_des_irq(int irq, void *dev_id)
830 {
831 	struct omap_des_dev *dd = dev_id;
832 	u32 status, i;
833 	u32 *src, *dst;
834 
835 	status = omap_des_read(dd, DES_REG_IRQ_STATUS(dd));
836 	if (status & DES_REG_IRQ_DATA_IN) {
837 		omap_des_write(dd, DES_REG_IRQ_ENABLE(dd), 0x0);
838 
839 		BUG_ON(!dd->in_sg);
840 
841 		BUG_ON(_calc_walked(in) > dd->in_sg->length);
842 
843 		src = sg_virt(dd->in_sg) + _calc_walked(in);
844 
845 		for (i = 0; i < DES_BLOCK_WORDS; i++) {
846 			omap_des_write(dd, DES_REG_DATA_N(dd, i), *src);
847 
848 			scatterwalk_advance(&dd->in_walk, 4);
849 			if (dd->in_sg->length == _calc_walked(in)) {
850 				dd->in_sg = sg_next(dd->in_sg);
851 				if (dd->in_sg) {
852 					scatterwalk_start(&dd->in_walk,
853 							  dd->in_sg);
854 					src = sg_virt(dd->in_sg) +
855 					      _calc_walked(in);
856 				}
857 			} else {
858 				src++;
859 			}
860 		}
861 
862 		/* Clear IRQ status */
863 		status &= ~DES_REG_IRQ_DATA_IN;
864 		omap_des_write(dd, DES_REG_IRQ_STATUS(dd), status);
865 
866 		/* Enable DATA_OUT interrupt */
867 		omap_des_write(dd, DES_REG_IRQ_ENABLE(dd), 0x4);
868 
869 	} else if (status & DES_REG_IRQ_DATA_OUT) {
870 		omap_des_write(dd, DES_REG_IRQ_ENABLE(dd), 0x0);
871 
872 		BUG_ON(!dd->out_sg);
873 
874 		BUG_ON(_calc_walked(out) > dd->out_sg->length);
875 
876 		dst = sg_virt(dd->out_sg) + _calc_walked(out);
877 
878 		for (i = 0; i < DES_BLOCK_WORDS; i++) {
879 			*dst = omap_des_read(dd, DES_REG_DATA_N(dd, i));
880 			scatterwalk_advance(&dd->out_walk, 4);
881 			if (dd->out_sg->length == _calc_walked(out)) {
882 				dd->out_sg = sg_next(dd->out_sg);
883 				if (dd->out_sg) {
884 					scatterwalk_start(&dd->out_walk,
885 							  dd->out_sg);
886 					dst = sg_virt(dd->out_sg) +
887 					      _calc_walked(out);
888 				}
889 			} else {
890 				dst++;
891 			}
892 		}
893 
894 		BUG_ON(dd->total < DES_BLOCK_SIZE);
895 
896 		dd->total -= DES_BLOCK_SIZE;
897 
898 		/* Clear IRQ status */
899 		status &= ~DES_REG_IRQ_DATA_OUT;
900 		omap_des_write(dd, DES_REG_IRQ_STATUS(dd), status);
901 
902 		if (!dd->total)
903 			/* All bytes read! */
904 			tasklet_schedule(&dd->done_task);
905 		else
906 			/* Enable DATA_IN interrupt for next block */
907 			omap_des_write(dd, DES_REG_IRQ_ENABLE(dd), 0x2);
908 	}
909 
910 	return IRQ_HANDLED;
911 }
912 
913 static const struct of_device_id omap_des_of_match[] = {
914 	{
915 		.compatible	= "ti,omap4-des",
916 		.data		= &omap_des_pdata_omap4,
917 	},
918 	{},
919 };
920 MODULE_DEVICE_TABLE(of, omap_des_of_match);
921 
922 static int omap_des_get_of(struct omap_des_dev *dd,
923 		struct platform_device *pdev)
924 {
925 
926 	dd->pdata = of_device_get_match_data(&pdev->dev);
927 	if (!dd->pdata) {
928 		dev_err(&pdev->dev, "no compatible OF match\n");
929 		return -EINVAL;
930 	}
931 
932 	return 0;
933 }
934 #else
935 static int omap_des_get_of(struct omap_des_dev *dd,
936 		struct device *dev)
937 {
938 	return -EINVAL;
939 }
940 #endif
941 
942 static int omap_des_get_pdev(struct omap_des_dev *dd,
943 		struct platform_device *pdev)
944 {
945 	/* non-DT devices get pdata from pdev */
946 	dd->pdata = pdev->dev.platform_data;
947 
948 	return 0;
949 }
950 
951 static int omap_des_probe(struct platform_device *pdev)
952 {
953 	struct device *dev = &pdev->dev;
954 	struct omap_des_dev *dd;
955 	struct skcipher_alg *algp;
956 	struct resource *res;
957 	int err = -ENOMEM, i, j, irq = -1;
958 	u32 reg;
959 
960 	dd = devm_kzalloc(dev, sizeof(struct omap_des_dev), GFP_KERNEL);
961 	if (dd == NULL) {
962 		dev_err(dev, "unable to alloc data struct.\n");
963 		goto err_data;
964 	}
965 	dd->dev = dev;
966 	platform_set_drvdata(pdev, dd);
967 
968 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
969 	if (!res) {
970 		dev_err(dev, "no MEM resource info\n");
971 		goto err_res;
972 	}
973 
974 	err = (dev->of_node) ? omap_des_get_of(dd, pdev) :
975 			       omap_des_get_pdev(dd, pdev);
976 	if (err)
977 		goto err_res;
978 
979 	dd->io_base = devm_ioremap_resource(dev, res);
980 	if (IS_ERR(dd->io_base)) {
981 		err = PTR_ERR(dd->io_base);
982 		goto err_res;
983 	}
984 	dd->phys_base = res->start;
985 
986 	pm_runtime_use_autosuspend(dev);
987 	pm_runtime_set_autosuspend_delay(dev, DEFAULT_AUTOSUSPEND_DELAY);
988 
989 	pm_runtime_enable(dev);
990 	err = pm_runtime_get_sync(dev);
991 	if (err < 0) {
992 		pm_runtime_put_noidle(dev);
993 		dev_err(dd->dev, "%s: failed to get_sync(%d)\n", __func__, err);
994 		goto err_get;
995 	}
996 
997 	omap_des_dma_stop(dd);
998 
999 	reg = omap_des_read(dd, DES_REG_REV(dd));
1000 
1001 	pm_runtime_put_sync(dev);
1002 
1003 	dev_info(dev, "OMAP DES hw accel rev: %u.%u\n",
1004 		 (reg & dd->pdata->major_mask) >> dd->pdata->major_shift,
1005 		 (reg & dd->pdata->minor_mask) >> dd->pdata->minor_shift);
1006 
1007 	tasklet_init(&dd->done_task, omap_des_done_task, (unsigned long)dd);
1008 
1009 	err = omap_des_dma_init(dd);
1010 	if (err == -EPROBE_DEFER) {
1011 		goto err_irq;
1012 	} else if (err && DES_REG_IRQ_STATUS(dd) && DES_REG_IRQ_ENABLE(dd)) {
1013 		dd->pio_only = 1;
1014 
1015 		irq = platform_get_irq(pdev, 0);
1016 		if (irq < 0) {
1017 			err = irq;
1018 			goto err_irq;
1019 		}
1020 
1021 		err = devm_request_irq(dev, irq, omap_des_irq, 0,
1022 				dev_name(dev), dd);
1023 		if (err) {
1024 			dev_err(dev, "Unable to grab omap-des IRQ\n");
1025 			goto err_irq;
1026 		}
1027 	}
1028 
1029 
1030 	INIT_LIST_HEAD(&dd->list);
1031 	spin_lock(&list_lock);
1032 	list_add_tail(&dd->list, &dev_list);
1033 	spin_unlock(&list_lock);
1034 
1035 	/* Initialize des crypto engine */
1036 	dd->engine = crypto_engine_alloc_init(dev, 1);
1037 	if (!dd->engine) {
1038 		err = -ENOMEM;
1039 		goto err_engine;
1040 	}
1041 
1042 	err = crypto_engine_start(dd->engine);
1043 	if (err)
1044 		goto err_engine;
1045 
1046 	for (i = 0; i < dd->pdata->algs_info_size; i++) {
1047 		for (j = 0; j < dd->pdata->algs_info[i].size; j++) {
1048 			algp = &dd->pdata->algs_info[i].algs_list[j];
1049 
1050 			pr_debug("reg alg: %s\n", algp->base.cra_name);
1051 
1052 			err = crypto_register_skcipher(algp);
1053 			if (err)
1054 				goto err_algs;
1055 
1056 			dd->pdata->algs_info[i].registered++;
1057 		}
1058 	}
1059 
1060 	return 0;
1061 
1062 err_algs:
1063 	for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1064 		for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1065 			crypto_unregister_skcipher(
1066 					&dd->pdata->algs_info[i].algs_list[j]);
1067 
1068 err_engine:
1069 	if (dd->engine)
1070 		crypto_engine_exit(dd->engine);
1071 
1072 	omap_des_dma_cleanup(dd);
1073 err_irq:
1074 	tasklet_kill(&dd->done_task);
1075 err_get:
1076 	pm_runtime_disable(dev);
1077 err_res:
1078 	dd = NULL;
1079 err_data:
1080 	dev_err(dev, "initialization failed.\n");
1081 	return err;
1082 }
1083 
1084 static int omap_des_remove(struct platform_device *pdev)
1085 {
1086 	struct omap_des_dev *dd = platform_get_drvdata(pdev);
1087 	int i, j;
1088 
1089 	if (!dd)
1090 		return -ENODEV;
1091 
1092 	spin_lock(&list_lock);
1093 	list_del(&dd->list);
1094 	spin_unlock(&list_lock);
1095 
1096 	for (i = dd->pdata->algs_info_size - 1; i >= 0; i--)
1097 		for (j = dd->pdata->algs_info[i].registered - 1; j >= 0; j--)
1098 			crypto_unregister_skcipher(
1099 					&dd->pdata->algs_info[i].algs_list[j]);
1100 
1101 	tasklet_kill(&dd->done_task);
1102 	omap_des_dma_cleanup(dd);
1103 	pm_runtime_disable(dd->dev);
1104 	dd = NULL;
1105 
1106 	return 0;
1107 }
1108 
1109 #ifdef CONFIG_PM_SLEEP
1110 static int omap_des_suspend(struct device *dev)
1111 {
1112 	pm_runtime_put_sync(dev);
1113 	return 0;
1114 }
1115 
1116 static int omap_des_resume(struct device *dev)
1117 {
1118 	int err;
1119 
1120 	err = pm_runtime_get_sync(dev);
1121 	if (err < 0) {
1122 		pm_runtime_put_noidle(dev);
1123 		dev_err(dev, "%s: failed to get_sync(%d)\n", __func__, err);
1124 		return err;
1125 	}
1126 	return 0;
1127 }
1128 #endif
1129 
1130 static SIMPLE_DEV_PM_OPS(omap_des_pm_ops, omap_des_suspend, omap_des_resume);
1131 
1132 static struct platform_driver omap_des_driver = {
1133 	.probe	= omap_des_probe,
1134 	.remove	= omap_des_remove,
1135 	.driver	= {
1136 		.name	= "omap-des",
1137 		.pm	= &omap_des_pm_ops,
1138 		.of_match_table	= of_match_ptr(omap_des_of_match),
1139 	},
1140 };
1141 
1142 module_platform_driver(omap_des_driver);
1143 
1144 MODULE_DESCRIPTION("OMAP DES hw acceleration support.");
1145 MODULE_LICENSE("GPL v2");
1146 MODULE_AUTHOR("Joel Fernandes <joelf@ti.com>");
1147