c042e52348fcce8d13de5d8f6cbb47e1f5af697f
[linux-2.6.git] / drivers / crypto / tegra-aes.c
1 /*
2  * drivers/crypto/tegra-aes.c
3  *
4  * aes driver for NVIDIA tegra aes hardware
5  *
6  * Copyright (c) 2010-2011, NVIDIA Corporation.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  */
22
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/errno.h>
26 #include <linux/kernel.h>
27 #include <linux/clk.h>
28 #include <linux/platform_device.h>
29 #include <linux/scatterlist.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/io.h>
32 #include <linux/mutex.h>
33 #include <linux/interrupt.h>
34 #include <linux/completion.h>
35 #include <linux/workqueue.h>
36
37 #include <mach/arb_sema.h>
38 #include <mach/clk.h>
39 #include "../video/tegra/nvmap/nvmap.h"
40
41 #include <crypto/scatterwalk.h>
42 #include <crypto/aes.h>
43 #include <crypto/internal/rng.h>
44
45 #include "tegra-aes.h"
46
47 #define FLAGS_MODE_MASK         0x00ff
48 #define FLAGS_ENCRYPT           BIT(0)
49 #define FLAGS_CBC               BIT(1)
50 #define FLAGS_GIV               BIT(2)
51 #define FLAGS_RNG               BIT(3)
52 #define FLAGS_OFB               BIT(4)
53 #define FLAGS_INIT              BIT(5)
54 #define FLAGS_BUSY              1
55
56 /*
57  * Defines AES engine Max process bytes size in one go, which takes 1 msec.
58  * AES engine spends about 176 cycles/16-bytes or 11 cycles/byte
59  * The duration CPU can use the BSE to 1 msec, then the number of available
60  * cycles of AVP/BSE is 216K. In this duration, AES can process 216/11 ~= 19KB
61  * Based on this AES_HW_DMA_BUFFER_SIZE_BYTES is configured to 16KB.
62  */
63 #define AES_HW_DMA_BUFFER_SIZE_BYTES    0x4000
64
65 /*
66  * The key table length is 64 bytes
67  * (This includes first upto 32 bytes key + 16 bytes original initial vector
68  * and 16 bytes updated initial vector)
69  */
70 #define AES_HW_KEY_TABLE_LENGTH_BYTES   64
71
72 #define AES_HW_IV_SIZE  16
73 #define AES_HW_KEYSCHEDULE_LEN  256
74 #define ARB_SEMA_TIMEOUT        500
75
76 /*
77  * The memory being used is divides as follows:
78  * 1. Key - 32 bytes
79  * 2. Original IV - 16 bytes
80  * 3. Updated IV - 16 bytes
81  * 4. Key schedule - 256 bytes
82  *
83  * 1+2+3 constitute the hw key table.
84  */
85 #define AES_IVKEY_SIZE (AES_HW_KEY_TABLE_LENGTH_BYTES + AES_HW_KEYSCHEDULE_LEN)
86
87 #define DEFAULT_RNG_BLK_SZ      16
88
89 /* As of now only 5 commands are USED for AES encryption/Decryption */
90 #define AES_HW_MAX_ICQ_LENGTH   4
91
92 #define ICQBITSHIFT_BLKCNT      0
93
94 /* memdma_vd command */
95 #define MEMDMA_DIR_DTOVRAM      0
96 #define MEMDMA_DIR_VTODRAM      1
97 #define MEMDMABITSHIFT_DIR      25
98 #define MEMDMABITSHIFT_NUM_WORDS        12
99
100 /* Define AES Interactive command Queue commands Bit positions */
101 enum {
102         ICQBITSHIFT_KEYTABLEADDR = 0,
103         ICQBITSHIFT_KEYTABLEID = 17,
104         ICQBITSHIFT_VRAMSEL = 23,
105         ICQBITSHIFT_TABLESEL = 24,
106         ICQBITSHIFT_OPCODE = 26,
107 };
108
109 /* Define Ucq opcodes required for AES operation */
110 enum {
111         UCQOPCODE_BLKSTARTENGINE = 0x0E,
112         UCQOPCODE_DMASETUP = 0x10,
113         UCQOPCODE_DMACOMPLETE = 0x11,
114         UCQOPCODE_SETTABLE = 0x15,
115         UCQOPCODE_MEMDMAVD = 0x22,
116 };
117
118 /* Define Aes command values */
119 enum {
120         UCQCMD_VRAM_SEL = 0x1,
121         UCQCMD_CRYPTO_TABLESEL = 0x3,
122         UCQCMD_KEYSCHEDTABLESEL = 0x4,
123         UCQCMD_KEYTABLESEL = 0x8,
124 };
125
126 #define UCQCMD_KEYTABLEADDRMASK 0x1FFFF
127
128 #define AES_NR_KEYSLOTS 8
129 #define SSK_SLOT_NUM    4
130
131 struct tegra_aes_slot {
132         struct list_head node;
133         int slot_num;
134         bool available;
135 };
136
137 struct tegra_aes_reqctx {
138         unsigned long mode;
139 };
140
141 #define TEGRA_AES_QUEUE_LENGTH  500
142
143 struct tegra_aes_engine {
144         struct tegra_aes_dev *dd;
145         struct tegra_aes_ctx *ctx;
146         struct clk *iclk;
147         struct clk *pclk;
148         struct ablkcipher_request *req;
149         struct scatterlist *in_sg;
150         struct completion op_complete;
151         struct scatterlist *out_sg;
152         void __iomem *io_base;
153         void __iomem *ivkey_base;
154         unsigned long phys_base;
155         unsigned long iram_phys;
156         void *iram_virt;
157         dma_addr_t ivkey_phys_base;
158         dma_addr_t dma_buf_in;
159         dma_addr_t dma_buf_out;
160         size_t total;
161         size_t in_offset;
162         size_t out_offset;
163         u32 engine_offset;
164         u32 *buf_in;
165         u32 *buf_out;
166         int res_id;
167         unsigned long busy;
168         u8 irq;
169         bool new_key;
170         u32 status;
171 };
172
173 struct tegra_aes_dev {
174         struct device *dev;
175         struct tegra_aes_slot *slots;
176         struct tegra_aes_engine bsev;
177         struct tegra_aes_engine bsea;
178         struct nvmap_client *client;
179         struct nvmap_handle_ref *h_ref;
180         struct crypto_queue queue;
181         spinlock_t lock;
182         u64 ctr;
183         unsigned long flags;
184         u8 dt[DEFAULT_RNG_BLK_SZ];
185 };
186
187 static struct tegra_aes_dev *aes_dev;
188
189 struct tegra_aes_ctx {
190         struct tegra_aes_dev *dd;
191         struct tegra_aes_engine *eng;
192         struct tegra_aes_slot *slot;
193         int key[AES_MAX_KEY_SIZE];
194         int keylen;
195         bool use_ssk;
196         u8 dt[DEFAULT_RNG_BLK_SZ];
197 };
198
199 static struct tegra_aes_ctx rng_ctx;
200
201 /* keep registered devices data here */
202 static LIST_HEAD(slot_list);
203 static DEFINE_SPINLOCK(list_lock);
204
205 /* Engine specific work queues */
206 static void bsev_workqueue_handler(struct work_struct *work);
207 static void bsea_workqueue_handler(struct work_struct *work);
208
209 static DECLARE_WORK(bsev_work, bsev_workqueue_handler);
210 static DECLARE_WORK(bsea_work, bsea_workqueue_handler);
211
212 static struct workqueue_struct *bsev_wq;
213 static struct workqueue_struct *bsea_wq;
214
215 extern unsigned long long tegra_chip_uid(void);
216
217 static inline u32 aes_readl(struct tegra_aes_engine *engine, u32 offset)
218 {
219         return readl(engine->io_base + offset);
220 }
221
222 static inline void aes_writel(struct tegra_aes_engine *engine,
223         u32 val, u32 offset)
224 {
225         writel(val, engine->io_base + offset);
226 }
227
228 static int alloc_iram(struct tegra_aes_dev *dd)
229 {
230         size_t size;
231         int err;
232
233         dd->h_ref = NULL;
234
235         /* [key+iv+u-iv=64B] * 8 = 512Bytes */
236         size = AES_MAX_KEY_SIZE;
237         dd->client = nvmap_create_client(nvmap_dev, "aes_bsea");
238         if (IS_ERR(dd->client)) {
239                 dev_err(dd->dev, "nvmap_create_client failed\n");
240                 goto out;
241         }
242
243         dd->h_ref = nvmap_create_handle(dd->client, size);
244         if (IS_ERR(dd->h_ref)) {
245                 dev_err(dd->dev, "nvmap_create_handle failed\n");
246                 goto out;
247         }
248
249         /* Allocate memory in the iram */
250         err = nvmap_alloc_handle_id(dd->client, nvmap_ref_to_id(dd->h_ref),
251                 NVMAP_HEAP_CARVEOUT_IRAM, size, 0);
252         if (err) {
253                 dev_err(dd->dev, "nvmap_alloc_handle_id failed\n");
254                 nvmap_free_handle_id(dd->client, nvmap_ref_to_id(dd->h_ref));
255                 goto out;
256         }
257         dd->bsea.iram_phys = nvmap_handle_address(dd->client,
258                                         nvmap_ref_to_id(dd->h_ref));
259
260         dd->bsea.iram_virt = nvmap_mmap(dd->h_ref);     /* get virtual address */
261         if (!dd->bsea.iram_virt) {
262                 dev_err(dd->dev, "%s: no mem, BSEA IRAM alloc failure\n",
263                         __func__);
264                 goto out;
265         }
266
267         memset(dd->bsea.iram_virt, 0, size);
268         return 0;
269
270 out:
271         if (dd->bsea.iram_virt)
272                 nvmap_munmap(dd->h_ref, dd->bsea.iram_virt);
273
274         if (dd->client) {
275                 nvmap_free_handle_id(dd->client, nvmap_ref_to_id(dd->h_ref));
276                 nvmap_client_put(dd->client);
277         }
278
279         return -ENOMEM;
280 }
281
282 static void free_iram(struct tegra_aes_dev *dd)
283 {
284         if (dd->bsea.iram_virt)
285                 nvmap_munmap(dd->h_ref, dd->bsea.iram_virt);
286
287         if (dd->client) {
288                 nvmap_free_handle_id(dd->client, nvmap_ref_to_id(dd->h_ref));
289                 nvmap_client_put(dd->client);
290         }
291 }
292
293 static int aes_hw_init(struct tegra_aes_engine *engine)
294 {
295         struct tegra_aes_dev *dd = aes_dev;
296         int ret = 0;
297
298         if (engine->pclk) {
299                 ret = clk_enable(engine->pclk);
300                 if (ret < 0) {
301                         dev_err(dd->dev, "%s: pclock enable fail(%d)\n",
302                         __func__, ret);
303                         return ret;
304                 }
305         }
306
307         if (engine->iclk) {
308                 ret = clk_enable(engine->iclk);
309                 if (ret < 0) {
310                         dev_err(dd->dev, "%s: iclock enable fail(%d)\n",
311                         __func__, ret);
312                         if (engine->pclk)
313                                 clk_disable(engine->pclk);
314                         return ret;
315                 }
316         }
317
318         return ret;
319 }
320
321 static void aes_hw_deinit(struct tegra_aes_engine *engine)
322 {
323         if (engine->pclk)
324                 clk_disable(engine->pclk);
325
326         if (engine->iclk)
327                 clk_disable(engine->iclk);
328 }
329
330 #define MIN_RETRIES     3
331 static int aes_start_crypt(struct tegra_aes_engine *eng, u32 in_addr,
332         u32 out_addr, int nblocks, int mode, bool upd_iv)
333 {
334         u32 cmdq[AES_HW_MAX_ICQ_LENGTH];
335         int qlen = 0, i, eng_busy, icq_empty, ret;
336         u32 value;
337         int retries = MIN_RETRIES;
338
339 start:
340         do {
341                 value = aes_readl(eng, INTR_STATUS);
342                 eng_busy = value & BIT(0);
343                 icq_empty = value & BIT(3);
344         } while (eng_busy || (!icq_empty));
345
346         aes_writel(eng, 0xFFFFFFFF, INTR_STATUS);
347
348         /* error, dma xfer complete */
349         aes_writel(eng, 0x33, INT_ENB);
350         enable_irq(eng->irq);
351
352         cmdq[qlen++] = UCQOPCODE_DMASETUP << ICQBITSHIFT_OPCODE;
353         cmdq[qlen++] = in_addr;
354         cmdq[qlen++] = UCQOPCODE_BLKSTARTENGINE << ICQBITSHIFT_OPCODE |
355                 (nblocks-1) << ICQBITSHIFT_BLKCNT;
356         cmdq[qlen++] = UCQOPCODE_DMACOMPLETE << ICQBITSHIFT_OPCODE;
357
358         value = aes_readl(eng, CMDQUE_CONTROL);
359         /* access SDRAM through AHB */
360         value &= (~CMDQ_CTRL_SRC_STM_SEL_FIELD & ~CMDQ_CTRL_DST_STM_SEL_FIELD);
361         value |= (CMDQ_CTRL_SRC_STM_SEL_FIELD | CMDQ_CTRL_DST_STM_SEL_FIELD |
362                 CMDQ_CTRL_ICMDQEN_FIELD | CMDQ_CTRL_ERROR_FLUSH_ENB);
363         aes_writel(eng, value, CMDQUE_CONTROL);
364
365         value = 0;
366         if (mode & FLAGS_CBC) {
367                 value = ((0x1 << SECURE_INPUT_ALG_SEL_SHIFT) |
368                         ((eng->ctx->keylen * 8) << SECURE_INPUT_KEY_LEN_SHIFT) |
369                         ((u32)upd_iv << SECURE_IV_SELECT_SHIFT) |
370                         (((mode & FLAGS_ENCRYPT) ? 2 : 3)
371                                 << SECURE_XOR_POS_SHIFT) |
372                         (0 << SECURE_INPUT_SEL_SHIFT) |
373                         (((mode & FLAGS_ENCRYPT) ? 2 : 3)
374                                 << SECURE_VCTRAM_SEL_SHIFT) |
375                         ((mode & FLAGS_ENCRYPT) ? 1 : 0)
376                                 << SECURE_CORE_SEL_SHIFT |
377                         (0 << SECURE_RNG_ENB_SHIFT) |
378                         (0 << SECURE_HASH_ENB_SHIFT));
379         } else if (mode & FLAGS_OFB) {
380                 value = ((0x1 << SECURE_INPUT_ALG_SEL_SHIFT) |
381                         ((eng->ctx->keylen * 8) << SECURE_INPUT_KEY_LEN_SHIFT) |
382                         ((u32)upd_iv << SECURE_IV_SELECT_SHIFT) |
383                         ((u32)0 << SECURE_IV_SELECT_SHIFT) |
384                         (SECURE_XOR_POS_FIELD) |
385                         (2 << SECURE_INPUT_SEL_SHIFT) |
386                         (0 << SECURE_VCTRAM_SEL_SHIFT) |
387                         (SECURE_CORE_SEL_FIELD) |
388                         (0 << SECURE_RNG_ENB_SHIFT) |
389                         (0 << SECURE_HASH_ENB_SHIFT));
390         } else if (mode & FLAGS_RNG){
391                 value = ((0x1 << SECURE_INPUT_ALG_SEL_SHIFT) |
392                         ((eng->ctx->keylen * 8) << SECURE_INPUT_KEY_LEN_SHIFT) |
393                         ((u32)upd_iv << SECURE_IV_SELECT_SHIFT) |
394                         (0 << SECURE_XOR_POS_SHIFT) |
395                         (0 << SECURE_INPUT_SEL_SHIFT) |
396                         ((mode & FLAGS_ENCRYPT) ? 1 : 0)
397                                 << SECURE_CORE_SEL_SHIFT |
398                         (1 << SECURE_RNG_ENB_SHIFT) |
399                         (0 << SECURE_HASH_ENB_SHIFT));
400         } else {
401                 value = ((0x1 << SECURE_INPUT_ALG_SEL_SHIFT) |
402                         ((eng->ctx->keylen * 8) << SECURE_INPUT_KEY_LEN_SHIFT) |
403                         ((u32)upd_iv << SECURE_IV_SELECT_SHIFT) |
404                         (0 << SECURE_XOR_POS_SHIFT) |
405                         (0 << SECURE_INPUT_SEL_SHIFT) |
406                         (((mode & FLAGS_ENCRYPT) ? 1 : 0)
407                                 << SECURE_CORE_SEL_SHIFT) |
408                         (0 << SECURE_RNG_ENB_SHIFT) |
409                                 (0 << SECURE_HASH_ENB_SHIFT));
410         }
411         aes_writel(eng, value, SECURE_INPUT_SELECT);
412
413         aes_writel(eng, out_addr, SECURE_DEST_ADDR);
414         INIT_COMPLETION(eng->op_complete);
415
416         for (i = 0; i < qlen - 1; i++) {
417                 do {
418                         value = aes_readl(eng, INTR_STATUS);
419                         eng_busy = value & BIT(0);
420                         icq_empty = value & BIT(3);
421                 } while (eng_busy || (!icq_empty));
422                 aes_writel(eng, cmdq[i], ICMDQUE_WR);
423         }
424
425         ret = wait_for_completion_timeout(&eng->op_complete,
426                 msecs_to_jiffies(150));
427         if (ret == 0) {
428                 dev_err(aes_dev->dev, "engine%d timed out (0x%x)\n",
429                         eng->res_id, aes_readl(eng, INTR_STATUS));
430                 disable_irq(eng->irq);
431                 return -ETIMEDOUT;
432         }
433
434         disable_irq(eng->irq);
435         aes_writel(eng, cmdq[qlen - 1], ICMDQUE_WR);
436
437         if ((eng->status != 0) && (retries-- > 0)) {
438                 qlen = 0;
439                 goto start;
440         }
441
442         return 0;
443 }
444
445 static void aes_release_key_slot(struct tegra_aes_ctx *ctx)
446 {
447         spin_lock(&list_lock);
448         ctx->slot->available = true;
449         ctx->slot = NULL;
450         spin_unlock(&list_lock);
451 }
452
453 static struct tegra_aes_slot *aes_find_key_slot(struct tegra_aes_dev *dd)
454 {
455         struct tegra_aes_slot *slot = NULL;
456         bool found = 0;
457
458         spin_lock(&list_lock);
459         list_for_each_entry(slot, &slot_list, node) {
460                 dev_dbg(dd->dev, "empty:%d, num:%d\n", slot->available,
461                         slot->slot_num);
462                 if (slot->available) {
463                         slot->available = false;
464                         found = 1;
465                         break;
466                 }
467         }
468
469         spin_unlock(&list_lock);
470         return found ? slot : NULL;
471 }
472
473 static int aes_set_key(struct tegra_aes_engine *eng, int slot_num)
474 {
475         struct tegra_aes_dev *dd = aes_dev;
476         u32 value, cmdq[2];
477         int i, eng_busy, icq_empty, dma_busy;
478
479         if (!eng) {
480                 dev_err(dd->dev, "%s: context invalid\n", __func__);
481                 return -EINVAL;
482         }
483
484         /* enable key schedule generation in hardware */
485         value = aes_readl(eng, SECURE_CONFIG_EXT);
486         value &= ~SECURE_KEY_SCH_DIS_FIELD;
487         aes_writel(eng, value, SECURE_CONFIG_EXT);
488
489         /* select the key slot */
490         value = aes_readl(eng, SECURE_CONFIG);
491         value &= ~SECURE_KEY_INDEX_FIELD;
492         value |= (slot_num << SECURE_KEY_INDEX_SHIFT);
493         aes_writel(eng, value, SECURE_CONFIG);
494
495         if (slot_num == SSK_SLOT_NUM)
496                 goto out;
497
498         if (eng->res_id == TEGRA_ARB_BSEV) {
499                 memset(dd->bsev.ivkey_base, 0, AES_HW_KEY_TABLE_LENGTH_BYTES);
500                 memcpy(dd->bsev.ivkey_base, eng->ctx->key, eng->ctx->keylen);
501
502                 /* copy the key table from sdram to vram */
503                 cmdq[0] = 0;
504                 cmdq[0] = UCQOPCODE_MEMDMAVD << ICQBITSHIFT_OPCODE |
505                                 (MEMDMA_DIR_DTOVRAM << MEMDMABITSHIFT_DIR) |
506                         (AES_HW_KEY_TABLE_LENGTH_BYTES/sizeof(u32))
507                         << MEMDMABITSHIFT_NUM_WORDS;
508                 cmdq[1] = (u32)eng->ivkey_phys_base;
509                 for (i = 0; i < ARRAY_SIZE(cmdq); i++)
510                         aes_writel(eng, cmdq[i], ICMDQUE_WR);
511                 do {
512                         value = aes_readl(eng, INTR_STATUS);
513                         eng_busy = value & BIT(0);
514                         icq_empty = value & BIT(3);
515                         dma_busy = value & BIT(23);
516                 } while (eng_busy & (!icq_empty) & dma_busy);
517
518                 /* settable command to get key into internal registers */
519                 value = 0;
520                 value = UCQOPCODE_SETTABLE << ICQBITSHIFT_OPCODE |
521                         UCQCMD_CRYPTO_TABLESEL << ICQBITSHIFT_TABLESEL |
522                         UCQCMD_VRAM_SEL << ICQBITSHIFT_VRAMSEL |
523                         (UCQCMD_KEYTABLESEL | slot_num)
524                         << ICQBITSHIFT_KEYTABLEID;
525                 aes_writel(eng, value, ICMDQUE_WR);
526                 do {
527                         value = aes_readl(eng, INTR_STATUS);
528                         eng_busy = value & BIT(0);
529                         icq_empty = value & BIT(3);
530                 } while (eng_busy & (!icq_empty));
531         } else {
532                 memset(dd->bsea.iram_virt, 0, AES_HW_KEY_TABLE_LENGTH_BYTES);
533                 memcpy(dd->bsea.iram_virt, eng->ctx->key, eng->ctx->keylen);
534
535                 /* set iram access cfg bit 0 if address >128K */
536                 if (dd->bsea.iram_phys > 0x00020000)
537                         aes_writel(eng, BIT(0), IRAM_ACCESS_CFG);
538                 else
539                         aes_writel(eng, 0, IRAM_ACCESS_CFG);
540
541                 /* settable command to get key into internal registers */
542                 value = 0;
543                 value = UCQOPCODE_SETTABLE << ICQBITSHIFT_OPCODE |
544                         UCQCMD_CRYPTO_TABLESEL << ICQBITSHIFT_TABLESEL |
545                         (UCQCMD_KEYTABLESEL | slot_num)
546                         << ICQBITSHIFT_KEYTABLEID |
547                         dd->bsea.iram_phys >> 2;
548                         aes_writel(eng, value, ICMDQUE_WR);
549                 do {
550                         value = aes_readl(eng, INTR_STATUS);
551                         eng_busy = value & BIT(0);
552                         icq_empty = value & BIT(3);
553                 } while (eng_busy & (!icq_empty));
554         }
555
556 out:
557         return 0;
558 }
559
560 static int tegra_aes_handle_req(struct tegra_aes_engine *eng)
561 {
562         struct tegra_aes_dev *dd = aes_dev;
563         struct tegra_aes_ctx *ctx;
564         struct crypto_async_request *async_req, *backlog;
565         struct tegra_aes_reqctx *rctx;
566         struct ablkcipher_request *req;
567         unsigned long irq_flags;
568         int dma_max = AES_HW_DMA_BUFFER_SIZE_BYTES;
569         int nblocks, total, ret = 0, count = 0;
570         dma_addr_t addr_in, addr_out;
571         struct scatterlist *in_sg, *out_sg;
572
573         spin_lock_irqsave(&dd->lock, irq_flags);
574         backlog = crypto_get_backlog(&dd->queue);
575         async_req = crypto_dequeue_request(&dd->queue);
576         if (!async_req)
577                 clear_bit(FLAGS_BUSY, &eng->busy);
578         spin_unlock_irqrestore(&dd->lock, irq_flags);
579
580         if (!async_req)
581                 return -ENODATA;
582
583         if (backlog)
584                 backlog->complete(backlog, -EINPROGRESS);
585
586         req = ablkcipher_request_cast(async_req);
587         dev_dbg(dd->dev, "%s: get new req (engine #%d)\n", __func__,
588                 eng->res_id);
589
590         if (!req->src || !req->dst)
591                 return -EINVAL;
592
593         /* take the hardware semaphore */
594         if (tegra_arb_mutex_lock_timeout(eng->res_id, ARB_SEMA_TIMEOUT) < 0) {
595                 dev_err(dd->dev, "aes hardware (%d) not available\n",
596                 eng->res_id);
597                 return -EBUSY;
598         }
599
600         /* assign new request to device */
601         eng->req = req;
602         eng->total = req->nbytes;
603         eng->in_offset = 0;
604         eng->in_sg = req->src;
605         eng->out_offset = 0;
606         eng->out_sg = req->dst;
607
608         in_sg = eng->in_sg;
609         out_sg = eng->out_sg;
610         total = eng->total;
611
612         rctx = ablkcipher_request_ctx(req);
613         ctx = crypto_ablkcipher_ctx(crypto_ablkcipher_reqtfm(req));
614         rctx->mode &= FLAGS_MODE_MASK;
615         dd->flags = (dd->flags & ~FLAGS_MODE_MASK) | rctx->mode;
616         eng->ctx = ctx;
617
618         if (eng->new_key) {
619                 if (ctx->use_ssk)
620                         aes_set_key(eng, SSK_SLOT_NUM);
621                 else
622                         aes_set_key(eng, ctx->slot->slot_num);
623
624                 eng->new_key = false;
625         }
626
627         if (((dd->flags & FLAGS_CBC) || (dd->flags & FLAGS_OFB)) && req->info) {
628                 /* set iv to the aes hw slot
629                  * Hw generates updated iv only after iv is set in slot.
630                  * So key and iv is passed asynchronously.
631                 */
632                 memcpy(eng->buf_in, (u8 *)req->info, AES_BLOCK_SIZE);
633
634                 ret = aes_start_crypt(eng, (u32)eng->dma_buf_in,
635                         (u32)eng->dma_buf_out, 1, FLAGS_CBC, false);
636                 if (ret < 0) {
637                         dev_err(dd->dev, "aes_start_crypt fail(%d)\n", ret);
638                         goto out;
639                 }
640         }
641
642         while (total) {
643                 dev_dbg(dd->dev, "remain: %d\n", total);
644                 ret = dma_map_sg(dd->dev, in_sg, 1, DMA_TO_DEVICE);
645                 if (!ret) {
646                         dev_err(dd->dev, "dma_map_sg() error\n");
647                         goto out;
648                 }
649
650                 ret = dma_map_sg(dd->dev, out_sg, 1, DMA_FROM_DEVICE);
651                 if (!ret) {
652                         dev_err(dd->dev, "dma_map_sg() error\n");
653                         dma_unmap_sg(dd->dev, eng->in_sg,
654                                 1, DMA_TO_DEVICE);
655                         goto out;
656                 }
657
658                 addr_in = sg_dma_address(in_sg);
659                 addr_out = sg_dma_address(out_sg);
660                 count = min((int)sg_dma_len(in_sg), (int)dma_max);
661                 WARN_ON(sg_dma_len(in_sg) != sg_dma_len(out_sg));
662                 nblocks = DIV_ROUND_UP(count, AES_BLOCK_SIZE);
663
664                 ret = aes_start_crypt(eng, addr_in, addr_out, nblocks,
665                         dd->flags, true);
666
667                 dma_unmap_sg(dd->dev, out_sg, 1, DMA_FROM_DEVICE);
668                 dma_unmap_sg(dd->dev, in_sg, 1, DMA_TO_DEVICE);
669                 if (ret < 0) {
670                         dev_err(dd->dev, "aes_start_crypt fail(%d)\n", ret);
671                         goto out;
672                 }
673
674                 dev_dbg(dd->dev, "out: copied %d\n", count);
675                 total -= count;
676                 in_sg = sg_next(in_sg);
677                 out_sg = sg_next(out_sg);
678                 WARN_ON(((total != 0) && (!in_sg || !out_sg)));
679         }
680
681 out:
682         /* release the hardware semaphore */
683         tegra_arb_mutex_unlock(eng->res_id);
684         eng->total = total;
685
686         if (eng->req->base.complete)
687                 eng->req->base.complete(&eng->req->base, ret);
688
689         dev_dbg(dd->dev, "%s: exit\n", __func__);
690         return ret;
691 }
692
693 static int tegra_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
694         unsigned int keylen)
695 {
696         struct tegra_aes_ctx *ctx = crypto_ablkcipher_ctx(tfm);
697         struct tegra_aes_dev *dd = aes_dev;
698         struct tegra_aes_slot *key_slot;
699
700         if (!ctx || !dd) {
701                 pr_err("ctx=0x%x, dd=0x%x\n",
702                         (unsigned int)ctx, (unsigned int)dd);
703                 return -EINVAL;
704         }
705
706         if ((keylen != AES_KEYSIZE_128) && (keylen != AES_KEYSIZE_192) &&
707                 (keylen != AES_KEYSIZE_256)) {
708                 dev_err(dd->dev, "unsupported key size\n");
709                 return -EINVAL;
710         }
711
712         dev_dbg(dd->dev, "keylen: %d\n", keylen);
713
714         ctx->dd = dd;
715
716         if (key) {
717                 if (!ctx->slot) {
718                         key_slot = aes_find_key_slot(dd);
719                         if (!key_slot) {
720                                 dev_err(dd->dev, "no empty slot\n");
721                                 return -ENOMEM;
722                         }
723                         ctx->slot = key_slot;
724                 }
725
726                 /* copy the key to the proper slot */
727                 memset(ctx->key, 0, AES_MAX_KEY_SIZE);
728                 memcpy(ctx->key, key, keylen);
729                 ctx->keylen = keylen;
730                 ctx->use_ssk = false;
731         } else {
732                 ctx->use_ssk = true;
733                 ctx->keylen = AES_KEYSIZE_128;
734         }
735
736         dd->bsev.new_key = true;
737         dd->bsea.new_key = true;
738         dev_dbg(dd->dev, "done\n");
739         return 0;
740 }
741
742 static void bsev_workqueue_handler(struct work_struct *work)
743 {
744         struct tegra_aes_dev *dd = aes_dev;
745         struct tegra_aes_engine *engine = &dd->bsev;
746         int ret;
747
748         aes_hw_init(engine);
749
750         /* empty the crypto queue and then return */
751         do {
752                 ret = tegra_aes_handle_req(engine);
753         } while (!ret);
754
755         aes_hw_deinit(engine);
756 }
757
758 static void bsea_workqueue_handler(struct work_struct *work)
759 {
760         struct tegra_aes_dev *dd = aes_dev;
761         struct tegra_aes_engine *engine = &dd->bsea;
762         int ret;
763
764         aes_hw_init(engine);
765
766         /* empty the crypto queue and then return */
767         do {
768                 ret = tegra_aes_handle_req(engine);
769         } while (!ret);
770
771         aes_hw_deinit(engine);
772 }
773
774 #define INT_ERROR_MASK  0xFFF000
775 static irqreturn_t aes_bsev_irq(int irq, void *dev_id)
776 {
777         struct tegra_aes_dev *dd = (struct tegra_aes_dev *)dev_id;
778         u32 value = aes_readl(&dd->bsev, INTR_STATUS);
779
780         dev_dbg(dd->dev, "bsev irq_stat: 0x%x", value);
781         dd->bsev.status = 0;
782         if (value & INT_ERROR_MASK) {
783                 aes_writel(&dd->bsev, INT_ERROR_MASK, INTR_STATUS);
784                 dd->bsev.status = value & INT_ERROR_MASK;
785         }
786
787         value = aes_readl(&dd->bsev, INTR_STATUS);
788         if (!(value & ENGINE_BUSY_FIELD))
789                 complete(&dd->bsev.op_complete);
790
791         return IRQ_HANDLED;
792 }
793
794 static irqreturn_t aes_bsea_irq(int irq, void *dev_id)
795 {
796         struct tegra_aes_dev *dd = (struct tegra_aes_dev *)dev_id;
797         u32 value = aes_readl(&dd->bsea, INTR_STATUS);
798
799         dev_dbg(dd->dev, "bsea irq_stat: 0x%x", value);
800         dd->bsea.status = 0;
801         if (value & INT_ERROR_MASK) {
802                 aes_writel(&dd->bsea, INT_ERROR_MASK, INTR_STATUS);
803                 dd->bsea.status = value & INT_ERROR_MASK;
804         }
805
806         value = aes_readl(&dd->bsea, INTR_STATUS);
807         if (!(value & ENGINE_BUSY_FIELD))
808                 complete(&dd->bsea.op_complete);
809
810         return IRQ_HANDLED;
811 }
812
813 static int tegra_aes_crypt(struct ablkcipher_request *req, unsigned long mode)
814 {
815         struct tegra_aes_reqctx *rctx = ablkcipher_request_ctx(req);
816         struct tegra_aes_dev *dd = aes_dev;
817         unsigned long flags;
818         int err = 0;
819         int bsev_busy;
820         int bsea_busy;
821
822         dev_dbg(dd->dev, "nbytes: %d, enc: %d, cbc: %d, ofb: %d\n", req->nbytes,
823                 !!(mode & FLAGS_ENCRYPT),
824                 !!(mode & FLAGS_CBC),
825                 !!(mode & FLAGS_OFB));
826
827         rctx->mode = mode;
828
829         spin_lock_irqsave(&dd->lock, flags);
830         err = ablkcipher_enqueue_request(&dd->queue, req);
831         bsev_busy = test_and_set_bit(FLAGS_BUSY, &dd->bsev.busy);
832         bsea_busy = test_and_set_bit(FLAGS_BUSY, &dd->bsea.busy);
833         spin_unlock_irqrestore(&dd->lock, flags);
834
835         if (!bsev_busy)
836                 queue_work(bsev_wq, &bsev_work);
837         if (!bsea_busy)
838                 queue_work(bsea_wq, &bsea_work);
839
840         return err;
841 }
842
843 static int tegra_aes_ecb_encrypt(struct ablkcipher_request *req)
844 {
845         return tegra_aes_crypt(req, FLAGS_ENCRYPT);
846 }
847
848 static int tegra_aes_ecb_decrypt(struct ablkcipher_request *req)
849 {
850         return tegra_aes_crypt(req, 0);
851 }
852
853 static int tegra_aes_cbc_encrypt(struct ablkcipher_request *req)
854 {
855         return tegra_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_CBC);
856 }
857
858 static int tegra_aes_cbc_decrypt(struct ablkcipher_request *req)
859 {
860         return tegra_aes_crypt(req, FLAGS_CBC);
861 }
862 static int tegra_aes_ofb_encrypt(struct ablkcipher_request *req)
863 {
864         return tegra_aes_crypt(req, FLAGS_ENCRYPT | FLAGS_OFB);
865 }
866
867 static int tegra_aes_ofb_decrypt(struct ablkcipher_request *req)
868 {
869         return tegra_aes_crypt(req, FLAGS_OFB);
870 }
871
872 static int tegra_aes_get_random(struct crypto_rng *tfm, u8 *rdata,
873         unsigned int dlen)
874 {
875         struct tegra_aes_dev *dd = aes_dev;
876         struct tegra_aes_engine *eng = rng_ctx.eng;
877         unsigned long flags;
878         int ret, i;
879         u8 *dest = rdata, *dt = rng_ctx.dt;
880
881         /* take the hardware semaphore */
882         if (tegra_arb_mutex_lock_timeout(eng->res_id, ARB_SEMA_TIMEOUT) < 0) {
883                 dev_err(dd->dev, "aes hardware (%d) not available\n",
884                 eng->res_id);
885                 return -EBUSY;
886         }
887
888         ret = aes_hw_init(eng);
889         if (ret < 0) {
890                 dev_err(dd->dev, "%s: hw init fail(%d)\n", __func__, ret);
891                 dlen = ret;
892                 goto fail;
893         }
894
895         memset(eng->buf_in, 0, AES_BLOCK_SIZE);
896         memcpy(eng->buf_in, dt, DEFAULT_RNG_BLK_SZ);
897
898         ret = aes_start_crypt(eng, (u32)eng->dma_buf_in, (u32)eng->dma_buf_out,
899                 1, FLAGS_ENCRYPT | FLAGS_RNG, true);
900         if (ret < 0) {
901                 dev_err(dd->dev, "aes_start_crypt fail(%d)\n", ret);
902                 dlen = ret;
903                 goto out;
904         }
905         memcpy(dest, eng->buf_out, dlen);
906
907         /* update the DT */
908         for (i = DEFAULT_RNG_BLK_SZ - 1; i >= 0; i--) {
909                 dt[i] += 1;
910                 if (dt[i] != 0)
911                         break;
912         }
913
914 out:
915         aes_hw_deinit(eng);
916
917         spin_lock_irqsave(&dd->lock, flags);
918         clear_bit(FLAGS_BUSY, &eng->busy);
919         spin_unlock_irqrestore(&dd->lock, flags);
920
921 fail:
922         /* release the hardware semaphore */
923         tegra_arb_mutex_unlock(eng->res_id);
924         dev_dbg(dd->dev, "%s: done\n", __func__);
925         return dlen;
926 }
927
928 static int tegra_aes_rng_reset(struct crypto_rng *tfm, u8 *seed,
929         unsigned int slen)
930 {
931         struct tegra_aes_dev *dd = aes_dev;
932         struct tegra_aes_ctx *ctx = &rng_ctx;
933         struct tegra_aes_engine *eng = NULL;
934         struct tegra_aes_slot *key_slot;
935         int bsea_busy = false;
936         unsigned long flags;
937         struct timespec ts;
938         u64 nsec, tmp[2];
939         int ret = 0;
940         u8 *dt;
941
942         if (!dd)
943                 return -EINVAL;
944
945         if (slen < (DEFAULT_RNG_BLK_SZ + AES_KEYSIZE_128)) {
946                 return -ENOMEM;
947         }
948
949         spin_lock_irqsave(&dd->lock, flags);
950         bsea_busy = test_and_set_bit(FLAGS_BUSY, &dd->bsea.busy);
951         spin_unlock_irqrestore(&dd->lock, flags);
952
953         if (!bsea_busy)
954                 eng = &dd->bsea;
955         else
956                 return -EBUSY;
957
958         ctx->eng = eng;
959         dd->flags = FLAGS_ENCRYPT | FLAGS_RNG;
960
961         if (!ctx->slot) {
962                 key_slot = aes_find_key_slot(dd);
963                 if (!key_slot) {
964                         dev_err(dd->dev, "no empty slot\n");
965                         return -ENOMEM;
966                 }
967                 ctx->slot = key_slot;
968         }
969
970         /* take the hardware semaphore */
971         if (tegra_arb_mutex_lock_timeout(eng->res_id, ARB_SEMA_TIMEOUT) < 0) {
972                 dev_err(dd->dev, "aes hardware (%d) not available\n",
973                 eng->res_id);
974                 return -EBUSY;
975         }
976
977         ret = aes_hw_init(eng);
978         if (ret < 0) {
979                 dev_err(dd->dev, "%s: hw init fail(%d)\n", __func__, ret);
980                 goto fail;
981         }
982
983         memcpy(ctx->key, seed + DEFAULT_RNG_BLK_SZ, AES_KEYSIZE_128);
984
985         eng->ctx = ctx;
986         eng->ctx->keylen = AES_KEYSIZE_128;
987         aes_set_key(eng, ctx->slot->slot_num);
988
989         /* set seed to the aes hw slot */
990         memset(eng->buf_in, 0, AES_BLOCK_SIZE);
991         memcpy(eng->buf_in, seed, DEFAULT_RNG_BLK_SZ);
992         ret = aes_start_crypt(eng, (u32)eng->dma_buf_in,
993           (u32)eng->dma_buf_out, 1, FLAGS_CBC, false);
994         if (ret < 0) {
995                 dev_err(dd->dev, "aes_start_crypt fail(%d)\n", ret);
996                 goto out;
997         }
998
999         if (slen >= (2 * DEFAULT_RNG_BLK_SZ + AES_KEYSIZE_128)) {
1000                 dt = seed + DEFAULT_RNG_BLK_SZ + AES_KEYSIZE_128;
1001         } else {
1002                 getnstimeofday(&ts);
1003                 nsec = timespec_to_ns(&ts);
1004                 do_div(nsec, 1000);
1005                 nsec ^= dd->ctr << 56;
1006                 dd->ctr++;
1007                 tmp[0] = nsec;
1008                 tmp[1] = tegra_chip_uid();
1009                 dt = (u8 *)tmp;
1010         }
1011         memcpy(ctx->dt, dt, DEFAULT_RNG_BLK_SZ);
1012
1013 out:
1014         aes_hw_deinit(eng);
1015
1016 fail:
1017         /* release the hardware semaphore */
1018         tegra_arb_mutex_unlock(eng->res_id);
1019
1020         dev_dbg(dd->dev, "%s: done\n", __func__);
1021         return ret;
1022 }
1023
1024 static int tegra_aes_cra_init(struct crypto_tfm *tfm)
1025 {
1026         tfm->crt_ablkcipher.reqsize = sizeof(struct tegra_aes_reqctx);
1027         return 0;
1028 }
1029
1030 void tegra_aes_cra_exit(struct crypto_tfm *tfm)
1031 {
1032         struct tegra_aes_ctx *ctx = crypto_ablkcipher_ctx((struct crypto_ablkcipher *)tfm);
1033
1034         if (ctx && ctx->slot)
1035                 aes_release_key_slot(ctx);
1036 }
1037
1038 static struct crypto_alg algs[] = {
1039         {
1040                 .cra_name = "disabled_ecb(aes)",
1041                 .cra_driver_name = "ecb-aes-tegra",
1042                 .cra_priority = 100,
1043                 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1044                 .cra_blocksize = AES_BLOCK_SIZE,
1045                 .cra_ctxsize = sizeof(struct tegra_aes_ctx),
1046                 .cra_alignmask = 3,
1047                 .cra_type = &crypto_ablkcipher_type,
1048                 .cra_module = THIS_MODULE,
1049                 .cra_init = tegra_aes_cra_init,
1050                 .cra_exit = tegra_aes_cra_exit,
1051                 .cra_u.ablkcipher = {
1052                         .min_keysize = AES_MIN_KEY_SIZE,
1053                         .max_keysize = AES_MAX_KEY_SIZE,
1054                         .setkey = tegra_aes_setkey,
1055                         .encrypt = tegra_aes_ecb_encrypt,
1056                         .decrypt = tegra_aes_ecb_decrypt,
1057                 },
1058         }, {
1059                 .cra_name = "disabled_cbc(aes)",
1060                 .cra_driver_name = "cbc-aes-tegra",
1061                 .cra_priority = 100,
1062                 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1063                 .cra_blocksize = AES_BLOCK_SIZE,
1064                 .cra_ctxsize  = sizeof(struct tegra_aes_ctx),
1065                 .cra_alignmask = 3,
1066                 .cra_type = &crypto_ablkcipher_type,
1067                 .cra_module = THIS_MODULE,
1068                 .cra_init = tegra_aes_cra_init,
1069                 .cra_exit = tegra_aes_cra_exit,
1070                 .cra_u.ablkcipher = {
1071                         .min_keysize = AES_MIN_KEY_SIZE,
1072                         .max_keysize = AES_MAX_KEY_SIZE,
1073                         .ivsize = AES_MIN_KEY_SIZE,
1074                         .setkey = tegra_aes_setkey,
1075                         .encrypt = tegra_aes_cbc_encrypt,
1076                         .decrypt = tegra_aes_cbc_decrypt,
1077                 }
1078         }, {
1079                 .cra_name = "disabled_ofb(aes)",
1080                 .cra_driver_name = "ofb-aes-tegra",
1081                 .cra_priority = 100,
1082                 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
1083                 .cra_blocksize = AES_BLOCK_SIZE,
1084                 .cra_ctxsize  = sizeof(struct tegra_aes_ctx),
1085                 .cra_alignmask = 3,
1086                 .cra_type = &crypto_ablkcipher_type,
1087                 .cra_module = THIS_MODULE,
1088                 .cra_init = tegra_aes_cra_init,
1089                 .cra_exit = tegra_aes_cra_exit,
1090                 .cra_u.ablkcipher = {
1091                         .min_keysize = AES_MIN_KEY_SIZE,
1092                         .max_keysize = AES_MAX_KEY_SIZE,
1093                         .ivsize = AES_MIN_KEY_SIZE,
1094                         .setkey = tegra_aes_setkey,
1095                         .encrypt = tegra_aes_ofb_encrypt,
1096                         .decrypt = tegra_aes_ofb_decrypt,
1097                 }
1098         }, {
1099                 .cra_name = "disabled_ansi_cprng",
1100                 .cra_driver_name = "rng-aes-tegra",
1101                 .cra_priority = 100,
1102                 .cra_flags = CRYPTO_ALG_TYPE_RNG,
1103                 .cra_ctxsize = sizeof(struct tegra_aes_ctx),
1104                 .cra_type = &crypto_rng_type,
1105                 .cra_module = THIS_MODULE,
1106                 .cra_init = tegra_aes_cra_init,
1107                 .cra_exit = tegra_aes_cra_exit,
1108                 .cra_u.rng = {
1109                         .rng_make_random = tegra_aes_get_random,
1110                         .rng_reset = tegra_aes_rng_reset,
1111                         .seedsize = AES_KEYSIZE_128 + (2 * DEFAULT_RNG_BLK_SZ),
1112                 }
1113         }
1114 };
1115
1116 static int tegra_aes_probe(struct platform_device *pdev)
1117 {
1118         struct device *dev = &pdev->dev;
1119         struct tegra_aes_dev *dd;
1120         struct resource *res[2];
1121         int err = -ENOMEM, i = 0, j;
1122
1123         if (aes_dev)
1124                 return -EEXIST;
1125
1126         dd = kzalloc(sizeof(struct tegra_aes_dev), GFP_KERNEL);
1127         if (dd == NULL) {
1128                 dev_err(dev, "unable to alloc data struct.\n");
1129                 return -ENOMEM;;
1130         }
1131         dd->dev = dev;
1132         platform_set_drvdata(pdev, dd);
1133
1134         dd->slots = kzalloc(sizeof(struct tegra_aes_slot) * AES_NR_KEYSLOTS,
1135                 GFP_KERNEL);
1136         if (dd->slots == NULL) {
1137                 dev_err(dev, "unable to alloc slot struct.\n");
1138                 goto out;
1139         }
1140
1141         spin_lock_init(&dd->lock);
1142         crypto_init_queue(&dd->queue, TEGRA_AES_QUEUE_LENGTH);
1143
1144         /* Get the module base address */
1145         res[0] = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1146         res[1] = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1147         if (!res[0] || !res[1]) {
1148                 dev_err(dev, "invalid resource type: base\n");
1149                 err = -ENODEV;
1150                 goto out;
1151         }
1152         dd->bsev.phys_base = res[0]->start;
1153         dd->bsev.io_base = ioremap(dd->bsev.phys_base, resource_size(res[0]));
1154         dd->bsea.phys_base = res[1]->start;
1155         dd->bsea.io_base = ioremap(dd->bsea.phys_base, resource_size(res[1]));
1156
1157         if (!dd->bsev.io_base || !dd->bsea.io_base) {
1158                 dev_err(dev, "can't ioremap phys_base\n");
1159                 err = -ENOMEM;
1160                 goto out;
1161         }
1162
1163         err = alloc_iram(dd);
1164         if (err < 0) {
1165                 dev_err(dev, "Failed to allocate IRAM for BSEA\n");
1166                 goto out;
1167         }
1168
1169         dd->bsev.res_id = TEGRA_ARB_BSEV;
1170         dd->bsea.res_id = TEGRA_ARB_BSEA;
1171
1172         dd->bsev.pclk = clk_get(dev, "bsev");
1173         if (IS_ERR(dd->bsev.pclk)) {
1174                 dev_err(dev, "v: pclock intialization failed.\n");
1175                 err = -ENODEV;
1176                 goto out;
1177         }
1178
1179         dd->bsev.iclk = clk_get(dev, "vde");
1180         if (IS_ERR(dd->bsev.iclk)) {
1181                 dev_err(dev, "v: iclock intialization failed.\n");
1182                 err = -ENODEV;
1183                 goto out;
1184         }
1185
1186         dd->bsea.pclk = clk_get(dev, "bsea");
1187         if (IS_ERR(dd->bsea.pclk)) {
1188                 dev_err(dev, "a: pclock intialization failed.\n");
1189                 err = -ENODEV;
1190                 goto out;
1191         }
1192
1193         dd->bsea.iclk = clk_get(dev, "sclk");
1194         if (IS_ERR(dd->bsea.iclk)) {
1195                 dev_err(dev, "a: iclock intialization failed.\n");
1196                 err = -ENODEV;
1197                 goto out;
1198         }
1199
1200         err = clk_set_rate(dd->bsev.iclk, ULONG_MAX);
1201         if (err) {
1202                 dev_err(dd->dev, "bsev iclk set_rate fail(%d)\n", err);
1203                 goto out;
1204         }
1205
1206         err = clk_set_rate(dd->bsea.iclk, ULONG_MAX);
1207         if (err) {
1208                 dev_err(dd->dev, "bsea iclk set_rate fail(%d)\n", err);
1209                 goto out;
1210         }
1211
1212         /*
1213          * the foll contiguous memory is allocated as follows -
1214          * - hardware key table
1215          * - key schedule
1216          */
1217         dd->bsea.ivkey_base = NULL;
1218         dd->bsev.ivkey_base = dma_alloc_coherent(dev, AES_MAX_KEY_SIZE,
1219                 &dd->bsev.ivkey_phys_base, GFP_KERNEL);
1220         if (!dd->bsev.ivkey_base) {
1221                 dev_err(dev, "can not allocate iv/key buffer for BSEV\n");
1222                 err = -ENOMEM;
1223                 goto out;
1224         }
1225         memset(dd->bsev.ivkey_base, 0, AES_MAX_KEY_SIZE);
1226
1227         dd->bsev.buf_in = dma_alloc_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1228                 &dd->bsev.dma_buf_in, GFP_KERNEL);
1229         dd->bsea.buf_in = dma_alloc_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1230                 &dd->bsea.dma_buf_in, GFP_KERNEL);
1231         if (!dd->bsev.buf_in || !dd->bsea.buf_in) {
1232                 dev_err(dev, "can not allocate dma-in buffer\n");
1233                 err = -ENOMEM;
1234                 goto out;
1235         }
1236
1237         dd->bsev.buf_out = dma_alloc_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1238                 &dd->bsev.dma_buf_out, GFP_KERNEL);
1239         dd->bsea.buf_out = dma_alloc_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1240                 &dd->bsea.dma_buf_out, GFP_KERNEL);
1241         if (!dd->bsev.buf_out || !dd->bsea.buf_out) {
1242                 dev_err(dev, "can not allocate dma-out buffer\n");
1243                 err = -ENOMEM;
1244                 goto out;
1245         }
1246
1247         init_completion(&dd->bsev.op_complete);
1248         init_completion(&dd->bsea.op_complete);
1249
1250         bsev_wq = alloc_workqueue("bsev_wq", WQ_HIGHPRI | WQ_UNBOUND, 1);
1251         bsea_wq = alloc_workqueue("bsea_wq", WQ_HIGHPRI | WQ_UNBOUND, 1);
1252         if (!bsev_wq || !bsea_wq) {
1253                 dev_err(dev, "alloc_workqueue failed\n");
1254                 goto out;
1255         }
1256
1257         /* get the irq */
1258         dd->bsev.irq = INT_VDE_BSE_V;
1259         err = request_irq(dd->bsev.irq, aes_bsev_irq, IRQF_TRIGGER_HIGH,
1260                 "tegra-aes", dd);
1261         if (err) {
1262                 dev_err(dev, "request_irq failed fir BSEV Engine\n");
1263                 goto out;
1264         }
1265         disable_irq(dd->bsev.irq);
1266
1267         dd->bsea.irq = INT_VDE_BSE_A;
1268         err = request_irq(dd->bsea.irq, aes_bsea_irq, IRQF_TRIGGER_HIGH,
1269                 "tegra-aes", dd);
1270         if (err) {
1271                 dev_err(dev, "request_irq failed for BSEA Engine\n");
1272                 goto out;
1273         }
1274         disable_irq(dd->bsea.irq);
1275
1276         spin_lock_init(&list_lock);
1277         spin_lock(&list_lock);
1278         for (i = 0; i < AES_NR_KEYSLOTS; i++) {
1279                 if (i == SSK_SLOT_NUM)
1280                         continue;
1281                 dd->slots[i].available = true;
1282                 dd->slots[i].slot_num = i;
1283                 INIT_LIST_HEAD(&dd->slots[i].node);
1284                 list_add_tail(&dd->slots[i].node, &slot_list);
1285         }
1286         spin_unlock(&list_lock);
1287
1288         aes_dev = dd;
1289
1290         for (i = 0; i < ARRAY_SIZE(algs); i++) {
1291                 INIT_LIST_HEAD(&algs[i].cra_list);
1292                 err = crypto_register_alg(&algs[i]);
1293                 if (err)
1294                         goto out;
1295         }
1296
1297         dev_info(dev, "registered");
1298         return 0;
1299
1300 out:
1301         for (j = 0; j < i; j++)
1302                 crypto_unregister_alg(&algs[j]);
1303
1304         free_iram(dd);
1305
1306         if (dd->bsev.ivkey_base) {
1307                 dma_free_coherent(dev, SZ_512, dd->bsev.ivkey_base,
1308                         dd->bsev.ivkey_phys_base);
1309         }
1310
1311         if (dd->bsev.buf_in && dd->bsea.buf_in) {
1312                 dma_free_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1313                         dd->bsev.buf_in, dd->bsev.dma_buf_in);
1314                 dma_free_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1315                         dd->bsea.buf_in, dd->bsea.dma_buf_in);
1316         }
1317
1318         if (dd->bsev.buf_out && dd->bsea.buf_out) {
1319                 dma_free_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1320                         dd->bsev.buf_out, dd->bsev.dma_buf_out);
1321                 dma_free_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES,
1322                         dd->bsea.buf_out, dd->bsea.dma_buf_out);
1323         }
1324
1325         if (dd->bsev.io_base && dd->bsea.io_base) {
1326                 iounmap(dd->bsev.io_base);
1327                 iounmap(dd->bsea.io_base);
1328         }
1329
1330         if (dd->bsev.pclk)
1331                 clk_put(dd->bsev.pclk);
1332
1333         if (dd->bsev.iclk)
1334                 clk_put(dd->bsev.iclk);
1335
1336         if (dd->bsea.pclk)
1337                 clk_put(dd->bsea.pclk);
1338
1339         if (bsev_wq)
1340                 destroy_workqueue(bsev_wq);
1341
1342         if (bsea_wq)
1343                 destroy_workqueue(bsea_wq);
1344
1345         if (dd->bsev.irq)
1346                 free_irq(dd->bsev.irq, dd);
1347
1348         if (dd->bsea.irq)
1349                 free_irq(dd->bsea.irq, dd);
1350
1351         spin_lock(&list_lock);
1352         list_del(&slot_list);
1353         spin_unlock(&list_lock);
1354
1355         kfree(dd->slots);
1356         kfree(dd);
1357         aes_dev = NULL;
1358
1359         dev_err(dev, "%s: initialization failed.\n", __func__);
1360         return err;
1361 }
1362
1363 static int __devexit tegra_aes_remove(struct platform_device *pdev)
1364 {
1365         struct device *dev = &pdev->dev;
1366         struct tegra_aes_dev *dd = platform_get_drvdata(pdev);
1367         int i;
1368
1369         if (!dd)
1370                 return -ENODEV;
1371
1372         cancel_work_sync(&bsev_work);
1373         cancel_work_sync(&bsea_work);
1374         destroy_workqueue(bsev_wq);
1375         destroy_workqueue(bsea_wq);
1376         free_irq(dd->bsev.irq, dd);
1377         free_irq(dd->bsea.irq, dd);
1378         spin_lock(&list_lock);
1379         list_del(&slot_list);
1380         spin_unlock(&list_lock);
1381
1382         for (i = 0; i < ARRAY_SIZE(algs); i++)
1383                 crypto_unregister_alg(&algs[i]);
1384
1385         free_iram(dd);
1386         dma_free_coherent(dev, SZ_512, dd->bsev.ivkey_base,
1387                 dd->bsev.ivkey_phys_base);
1388         dma_free_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES, dd->bsev.buf_in,
1389                 dd->bsev.dma_buf_in);
1390         dma_free_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES, dd->bsea.buf_in,
1391                 dd->bsea.dma_buf_in);
1392         dma_free_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES, dd->bsev.buf_out,
1393                 dd->bsev.dma_buf_out);
1394         dma_free_coherent(dev, AES_HW_DMA_BUFFER_SIZE_BYTES, dd->bsea.buf_out,
1395                 dd->bsea.dma_buf_out);
1396
1397         iounmap(dd->bsev.io_base);
1398         iounmap(dd->bsea.io_base);
1399         clk_put(dd->bsev.iclk);
1400         clk_put(dd->bsev.pclk);
1401         clk_put(dd->bsea.pclk);
1402         kfree(dd->slots);
1403         kfree(dd);
1404         aes_dev = NULL;
1405
1406         return 0;
1407 }
1408
1409 static struct platform_driver tegra_aes_driver = {
1410         .probe  = tegra_aes_probe,
1411         .remove = __devexit_p(tegra_aes_remove),
1412         .driver = {
1413                 .name   = "tegra-aes",
1414                 .owner  = THIS_MODULE,
1415         },
1416 };
1417
1418 static int __init tegra_aes_mod_init(void)
1419 {
1420         INIT_LIST_HEAD(&slot_list);
1421         return  platform_driver_register(&tegra_aes_driver);
1422 }
1423
1424 static void __exit tegra_aes_mod_exit(void)
1425 {
1426         platform_driver_unregister(&tegra_aes_driver);
1427 }
1428
1429 module_init(tegra_aes_mod_init);
1430 module_exit(tegra_aes_mod_exit);
1431
1432 MODULE_DESCRIPTION("Tegra AES hw acceleration support.");
1433 MODULE_AUTHOR("NVIDIA Corporation");
1434 MODULE_LICENSE("GPLv2");