ltq-deu: make cipher/digest usable by openssl
[openwrt/staging/stintel.git] / package / kernel / lantiq / ltq-deu / src / ifxmips_async_des.c
1 /******************************************************************************
2 **
3 ** FILE NAME : ifxmips_async_des.c
4 ** PROJECT : IFX UEIP
5 ** MODULES : DEU Module
6 **
7 ** DATE : October 11, 2010
8 ** AUTHOR : Mohammad Firdaus
9 ** DESCRIPTION : Data Encryption Unit Driver for DES Algorithm
10 ** COPYRIGHT : Copyright (c) 2010
11 ** Infineon Technologies AG
12 ** Am Campeon 1-12, 85579 Neubiberg, Germany
13 **
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License as published by
16 ** the Free Software Foundation; either version 2 of the License, or
17 ** (at your option) any later version.
18 **
19 ** HISTORY
20 ** $Date $Author $Comment
21 ** 08,Sept 2009 Mohammad Firdaus Initial UEIP release
22 ** 11, Oct 2010 Mohammad Firdaus Kernel Port incl. Async. Ablkcipher mode
23 ** 21,March 2011 Mohammad Firdaus Changes for Kernel 2.6.32 and IPSec integration
24 *******************************************************************************/
25 /*!
26 \defgroup IFX_DEU IFX_DEU_DRIVERS
27 \ingroup API
28 \brief ifx DEU driver module
29 */
30
31 /*!
32 \file ifxmips_async_des.c
33 \ingroup IFX_DEU
34 \brief DES Encryption Driver main file
35 */
36
37 /*!
38 \defgroup IFX_DES_FUNCTIONS IFX_DES_FUNCTIONS
39 \ingroup IFX_DEU
40 \brief IFX DES driver Functions
41 */
42
43 #include <linux/wait.h>
44 #include <linux/crypto.h>
45 #include <linux/kernel.h>
46 #include <linux/interrupt.h>
47 #include <linux/spinlock.h>
48 #include <linux/list.h>
49 #include <crypto/ctr.h>
50 #include <crypto/aes.h>
51 #include <crypto/algapi.h>
52 #include <crypto/scatterwalk.h>
53
54 #include <asm/ifx/ifx_regs.h>
55 #include <asm/ifx/ifx_types.h>
56 #include <asm/ifx/common_routines.h>
57 #include <asm/ifx/irq.h>
58 #include <asm/ifx/ifx_pmu.h>
59 #include <asm/ifx/ifx_gpio.h>
60 #include <asm/kmap_types.h>
61
62 #include "ifxmips_deu.h"
63
64 #if defined(CONFIG_DANUBE)
65 #include "ifxmips_deu_danube.h"
66 extern int ifx_danube_pre_1_4;
67 #elif defined(CONFIG_AR9)
68 #include "ifxmips_deu_ar9.h"
69 #elif defined(CONFIG_VR9) || defined(CONFIG_AR10)
70 #include "ifxmips_deu_vr9.h"
71 #else
72 #error "Unkown platform"
73 #endif
74
75 /* DMA specific header and variables */
76
77 spinlock_t des_lock;
78 #define CRTCL_SECT_INIT spin_lock_init(&des_lock)
79 #define CRTCL_SECT_START spin_lock_irqsave(&des_lock, flag)
80 #define CRTCL_SECT_END spin_unlock_irqrestore(&des_lock, flag)
81
82 /* Preprocessor declerations */
83 #ifdef CRYPTO_DEBUG
84 extern char debug_level;
85 #define DPRINTF(level, format, args...) if (level < debug_level) printk(KERN_INFO "[%s %s %d]: " format, __FILE__, __func__, __LINE__, ##args);
86 #else
87 #define DPRINTF(level, format, args...)
88 #endif
89 //#define DES_3DES_START IFX_DES_CON
90 #define DES_KEY_SIZE 8
91 #define DES_EXPKEY_WORDS 32
92 #define DES_BLOCK_SIZE 8
93 #define DES3_EDE_KEY_SIZE (3 * DES_KEY_SIZE)
94 #define DES3_EDE_EXPKEY_WORDS (3 * DES_EXPKEY_WORDS)
95 #define DES3_EDE_BLOCK_SIZE DES_BLOCK_SIZE
96
97 /* Function Declaration to prevent warning messages */
98 void des_chip_init (void);
99 u32 endian_swap(u32 input);
100 u32 input_swap(u32 input);
101 int aes_memory_allocate(int value);
102 int des_memory_allocate(int value);
103 void memory_release(u32 *buffer);
104 u32* memory_alignment(const u8 *arg, u32 *buff_alloc, int in_out, int nbytes);
105 void aes_dma_memory_copy(u32 *outcopy, u32 *out_dma, u8 *out_arg, int nbytes);
106 void des_dma_memory_copy(u32 *outcopy, u32 *out_dma, u8 *out_arg, int nbytes);
107
108 static int lq_deu_des_core (void *ctx_arg, u8 *out_arg, const u8 *in_arg,
109 u8 *iv_arg, u32 nbytes, int encdec, int mode);
110
111 struct des_ctx {
112 int controlr_M;
113 int key_length;
114 u8 iv[DES_BLOCK_SIZE];
115 u32 expkey[DES3_EDE_EXPKEY_WORDS];
116 };
117
118
119 static int disable_multiblock = 0;
120 module_param(disable_multiblock, int, 0);
121
122 static int disable_deudma = 1;
123
124 struct des_container {
125 u8 *iv;
126 u8 *dst_buf;
127 u8 *src_buf;
128 int mode;
129 int encdec;
130 int complete;
131 int flag;
132
133 u32 bytes_processed;
134 u32 nbytes;
135
136 struct ablkcipher_request arequest;
137 };
138
139 des_priv_t *des_queue;
140 extern deu_drv_priv_t deu_dma_priv;
141
142 void hexdump1(unsigned char *buf, unsigned int len)
143 {
144 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_OFFSET,
145 16, 1,
146 buf, len, false);
147 }
148
149
150 /*! \fn int lq_des_setkey(struct crypto_ablkcipher *tfm, const u8 *key, unsigned int keylen)
151 * \ingroup IFX_DES_FUNCTIONS
152 * \brief sets DES key
153 * \param tfm linux crypto algo transform
154 * \param key input key
155 * \param keylen key length
156 */
157 static int lq_des_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
158 unsigned int keylen)
159 {
160 struct des_ctx *dctx = crypto_ablkcipher_ctx(tfm);
161
162 //printk("setkey in %s\n", __FILE__);
163
164 dctx->controlr_M = 0; // des
165 dctx->key_length = keylen;
166
167 memcpy ((u8 *) (dctx->expkey), key, keylen);
168
169 return 0;
170 }
171
172 /*! \fn int lq_des3_ede_setkey(struct crypto_ablkcipher *tfm, const u8 *key, unsigned int keylen)
173 * \ingroup IFX_DES_FUNCTIONS
174 * \brief sets DES key
175 * \param tfm linux crypto algo transform
176 * \param key input key
177 * \param keylen key length
178 */
179
180 static int lq_des3_ede_setkey(struct crypto_ablkcipher *tfm, const u8 *in_key,
181 unsigned int keylen)
182 {
183 struct des_ctx *dctx = crypto_ablkcipher_ctx(tfm);
184
185 //printk("setkey in %s\n", __FILE__);
186
187 dctx->controlr_M = keylen/8 + 1; // des
188 dctx->key_length = keylen;
189
190 memcpy ((u8 *) (dctx->expkey), in_key, keylen);
191
192 return 0;
193 }
194
195 /*! \fn void ifx_deu_des_core(void *ctx_arg, u8 *out_arg, const u8 *in_arg, u8 *iv_arg, u32 nbytes, int encdec, int mode)
196 * \ingroup IFX_DES_FUNCTIONS
197 * \brief main interface to DES hardware
198 * \param ctx_arg crypto algo context
199 * \param out_arg output bytestream
200 * \param in_arg input bytestream
201 * \param iv_arg initialization vector
202 * \param nbytes length of bytestream
203 * \param encdec 1 for encrypt; 0 for decrypt
204 * \param mode operation mode such as ebc, cbc
205 */
206
207 static int lq_deu_des_core (void *ctx_arg, u8 *out_arg, const u8 *in_arg,
208 u8 *iv_arg, u32 nbytes, int encdec, int mode)
209 {
210 volatile struct des_t *des = (struct des_t *) DES_3DES_START;
211 struct des_ctx *dctx = ctx_arg;
212 u32 *key = dctx->expkey;
213 unsigned long flag;
214
215 int i = 0;
216 int nblocks = 0;
217
218 CRTCL_SECT_START;
219
220 des->controlr.M = dctx->controlr_M;
221 if (dctx->controlr_M == 0) // des
222 {
223 des->K1HR = DEU_ENDIAN_SWAP(*((u32 *) key + 0));
224 des->K1LR = DEU_ENDIAN_SWAP(*((u32 *) key + 1));
225
226 }
227 else {
228 /* Hardware Section */
229 switch (dctx->key_length) {
230 case 24:
231 des->K3HR = DEU_ENDIAN_SWAP(*((u32 *) key + 4));
232 des->K3LR = DEU_ENDIAN_SWAP(*((u32 *) key + 5));
233 /* no break; */
234
235 case 16:
236 des->K2HR = DEU_ENDIAN_SWAP(*((u32 *) key + 2));
237 des->K2LR = DEU_ENDIAN_SWAP(*((u32 *) key + 3));
238
239 /* no break; */
240 case 8:
241 des->K1HR = DEU_ENDIAN_SWAP(*((u32 *) key + 0));
242 des->K1LR = DEU_ENDIAN_SWAP(*((u32 *) key + 1));
243 break;
244
245 default:
246 CRTCL_SECT_END;
247 return -EINVAL;
248 }
249 }
250
251 des->controlr.E_D = !encdec; //encryption
252 des->controlr.O = mode; //0 ECB 1 CBC 2 OFB 3 CFB 4 CTR hexdump(prin,sizeof(*des));
253
254 if (mode > 0) {
255 des->IVHR = DEU_ENDIAN_SWAP(*(u32 *) iv_arg);
256 des->IVLR = DEU_ENDIAN_SWAP(*((u32 *) iv_arg + 1));
257 };
258
259 /* memory alignment issue */
260 dword_mem_aligned_in = (u32 *) DEU_DWORD_REORDERING(in_arg, des_buff_in, BUFFER_IN, nbytes);
261
262 deu_priv->deu_rx_buf = (u32 *) out_arg;
263 deu_priv->deu_rx_len = nbytes;
264
265 dma->controlr.ALGO = 0; //DES
266 des->controlr.DAU = 0;
267 dma->controlr.BS = 0;
268 dma->controlr.EN = 1;
269
270 while (des->controlr.BUS) {
271 };
272
273 wlen = dma_device_write (dma_device, (u8 *) dword_mem_aligned_in, nbytes, NULL);
274 if (wlen != nbytes) {
275 dma->controlr.EN = 0;
276 CRTCL_SECT_END;
277 printk (KERN_ERR "[%s %s %d]: dma_device_write fail!\n", __FILE__, __func__, __LINE__);
278 return -EINVAL;
279 }
280
281
282 /* Prepare Rx buf length used in dma psuedo interrupt */
283 outcopy = (u32 *) DEU_DWORD_REORDERING(out_arg, des_buff_out, BUFFER_OUT, nbytes);
284 deu_priv->outcopy = outcopy;
285 deu_priv->event_src = DES_ASYNC_EVENT;
286
287 if (mode > 0) {
288 *(u32 *) iv_arg = DEU_ENDIAN_SWAP(des->IVHR);
289 *((u32 *) iv_arg + 1) = DEU_ENDIAN_SWAP(des->IVLR);
290 };
291
292 CRTCL_SECT_END;
293
294 return -EINPROGRESS;
295
296 }
297
298 static int count_sgs(struct scatterlist *sl, unsigned int total_bytes)
299 {
300 int i = 0;
301
302 do {
303 total_bytes -= sl[i].length;
304 i++;
305
306 } while (total_bytes > 0);
307
308 return i;
309 }
310
311 /* \fn static inline struct des_container *des_container_cast (
312 * struct scatterlist *dst)
313 * \ingroup IFX_DES_FUNCTIONS
314 * \brief Locate the structure des_container in memory.
315 * \param *areq Pointer to memory location where ablkcipher_request is located
316 * \return *des_cointainer The function pointer to des_container
317 */
318
319 static inline struct des_container *des_container_cast(
320 struct ablkcipher_request *areq)
321 {
322 return container_of(areq, struct des_container, arequest);
323 }
324
325 /* \fn static void lq_sg_complete(struct des_container *des_con)
326 * \ingroup IFX_DES_FUNCTIONS
327 * \brief Free the used up memory after encryt/decrypt.
328 */
329
330 static void lq_sg_complete(struct des_container *des_con)
331 {
332 unsigned long queue_flag;
333
334 spin_lock_irqsave(&des_queue->lock, queue_flag);
335 kfree(des_con);
336 spin_unlock_irqrestore(&des_queue->lock, queue_flag);
337 }
338
339 /* \fn void lq_sg_init(struct scatterlist *src,
340 * struct scatterlist *dst)
341 * \ingroup IFX_DES_FUNCTIONS
342 * \brief Maps the scatterlists into a source/destination page.
343 * \param *src Pointer to the source scatterlist
344 * \param *dst Pointer to the destination scatterlist
345 */
346
347 static void lq_sg_init(struct des_container *des_con, struct scatterlist *src,
348 struct scatterlist *dst)
349 {
350 struct page *dst_page, *src_page;
351
352 src_page = sg_virt(src);
353 des_con->src_buf = (char *) src_page;
354
355 dst_page = sg_virt(dst);
356 des_con->dst_buf = (char *) dst_page;
357 }
358
359 /* \fn static int process_next_packet(struct des_container *des_con, struct ablkcipher_request *areq,
360 * int state)
361 * \ingroup IFX_DES_FUNCTIONS
362 * \brief Process the next packet after dequeuing the packet from crypto queue
363 * \param *des_con Pointer to DES container structure
364 * \param *areq Pointer to ablkcipher_request container
365 * \param state State of the packet (scattered packet or new packet to be processed)
366 * \return -EINVAL: DEU failure, -EINPROGRESS: DEU encrypt/decrypt in progress, 1: no scatterlist left
367 */
368
369 static int process_next_packet(struct des_container *des_con, struct ablkcipher_request *areq,
370 int state)
371 {
372 u8 *iv;
373 int mode, encdec, err = -EINVAL;
374 u32 remain, inc, chunk_size, nbytes;
375 struct scatterlist *src = NULL;
376 struct scatterlist *dst = NULL;
377 struct crypto_ablkcipher *cipher;
378 struct des_ctx *ctx;
379 unsigned long queue_flag;
380
381 spin_lock_irqsave(&des_queue->lock, queue_flag);
382
383 mode = des_con->mode;
384 encdec = des_con->encdec;
385 iv = des_con->iv;
386
387 if (state & PROCESS_SCATTER) {
388 src = scatterwalk_sg_next(areq->src);
389 dst = scatterwalk_sg_next(areq->dst);
390
391 if (!src || !dst) {
392 spin_unlock_irqrestore(&des_queue->lock, queue_flag);
393 return 1;
394 }
395 }
396 else if (state & PROCESS_NEW_PACKET) {
397 src = areq->src;
398 dst = areq->dst;
399 }
400
401 remain = des_con->bytes_processed;
402 chunk_size = src->length;
403
404 //printk("debug ln: %d, func: %s, reqsize: %d, scattersize: %d\n",
405 // __LINE__, __func__, areq->nbytes, chunk_size);
406
407 if (remain > DEU_MAX_PACKET_SIZE)
408 inc = DEU_MAX_PACKET_SIZE;
409 else if(remain > chunk_size)
410 inc = chunk_size;
411 else
412 inc = remain;
413
414 remain -= inc;
415 des_con->nbytes = inc;
416
417 if (state & PROCESS_SCATTER) {
418 des_con->src_buf += des_con->nbytes;
419 des_con->dst_buf += des_con->nbytes;
420 }
421
422 lq_sg_init(des_con, src, dst);
423
424 nbytes = des_con->nbytes;
425
426 cipher = crypto_ablkcipher_reqtfm(areq);
427 ctx = crypto_ablkcipher_ctx(cipher);
428
429 if (des_queue->hw_status == DES_IDLE) {
430 des_queue->hw_status = DES_STARTED;
431 }
432
433 des_con->bytes_processed -= des_con->nbytes;
434 err = ablkcipher_enqueue_request(&des_queue->list, &des_con->arequest);
435 if (err == -EBUSY) {
436 printk("Failed to enqueue request, ln: %d, err: %d\n",
437 __LINE__, err);
438 spin_unlock_irqrestore(&des_queue->lock, queue_flag);
439 return -EINVAL;
440 }
441
442 spin_unlock_irqrestore(&des_queue->lock, queue_flag);
443 err = lq_deu_des_core(ctx, des_con->dst_buf, des_con->src_buf, iv, nbytes, encdec, mode);
444
445 return err;
446 }
447
448 /* \fn static void process_queue(unsigned long data)
449 * \ingroup IFX_DES_FUNCTIONS
450 * \brief Process next packet in queue
451 * \param data not used
452 * \return
453 */
454
455 static void process_queue(unsigned long data)
456 {
457 DEU_WAKEUP_EVENT(deu_dma_priv.deu_thread_wait, DES_ASYNC_EVENT,
458 deu_dma_priv.des_event_flags);
459
460 }
461
462 /* \fn static int des_crypto_thread (void *data)
463 * \ingroup IFX_DES_FUNCTIONS
464 * \brief DES thread that handles crypto requests from upper layer & DMA
465 * \param *data Not used
466 * \return -EINVAL: DEU failure, -EBUSY: DEU HW busy, 0: exit thread
467 */
468
469 static int des_crypto_thread(void *data)
470 {
471 struct des_container *des_con = NULL;
472 struct ablkcipher_request *areq = NULL;
473 int err;
474 unsigned long queue_flag;
475
476 daemonize("lq_des_thread");
477
478 while (1)
479 {
480 DEU_WAIT_EVENT(deu_dma_priv.deu_thread_wait, DES_ASYNC_EVENT,
481 deu_dma_priv.des_event_flags);
482 spin_lock_irqsave(&des_queue->lock, queue_flag);
483
484 /* wait to prevent starting a crypto session before
485 * exiting the dma interrupt thread.
486 */
487
488 if (des_queue->hw_status == DES_STARTED) {
489 areq = ablkcipher_dequeue_request(&des_queue->list);
490 des_con = des_container_cast(areq);
491 des_queue->hw_status = DES_BUSY;
492 }
493 else if (des_queue->hw_status == DES_IDLE) {
494 areq = ablkcipher_dequeue_request(&des_queue->list);
495 des_con = des_container_cast(areq);
496 des_queue->hw_status = DES_STARTED;
497 }
498 else if (des_queue->hw_status == DES_BUSY) {
499 areq = ablkcipher_dequeue_request(&des_queue->list);
500 des_con = des_container_cast(areq);
501 }
502 else if (des_queue->hw_status == DES_COMPLETED) {
503 areq->base.complete(&areq->base, 0);
504 lq_sg_complete(des_con);
505 des_queue->hw_status = DES_IDLE;
506 spin_unlock_irqrestore(&des_queue->lock, queue_flag);
507 return 0;
508 }
509 spin_unlock_irqrestore(&des_queue->lock, queue_flag);
510
511 if ((des_con->bytes_processed == 0)) {
512 goto des_done;
513 }
514
515 if (!des_con) {
516 goto des_done;
517 }
518
519 if (des_con->flag & PROCESS_NEW_PACKET) {
520 des_con->flag = PROCESS_SCATTER;
521 err = process_next_packet(des_con, areq, PROCESS_NEW_PACKET);
522 }
523 else
524 err = process_next_packet(des_con, areq, PROCESS_SCATTER);
525
526 if (err == -EINVAL) {
527 areq->base.complete(&areq->base, err);
528 lq_sg_complete(des_con);
529 printk("src/dst returned -EINVAL in func: %s\n", __func__);
530 }
531 else if (err > 0) {
532 printk("src/dst returned zero in func: %s\n", __func__);
533 goto des_done;
534 }
535
536 continue;
537
538 des_done:
539 //printk("debug line - %d, func: %s, qlen: %d\n", __LINE__, __func__, des_queue->list.qlen);
540 areq->base.complete(&areq->base, 0);
541 lq_sg_complete(des_con);
542
543 spin_lock_irqsave(&des_queue->lock, queue_flag);
544 if (des_queue->list.qlen > 0) {
545 spin_unlock_irqrestore(&des_queue->lock, queue_flag);
546 tasklet_schedule(&des_queue->des_task);
547 }
548 else {
549 des_queue->hw_status = DES_IDLE;
550 spin_unlock_irqrestore(&des_queue->lock, queue_flag);
551 }
552 } // while(1)
553
554 return 0;
555
556 }
557
558 /* \fn static int lq_des_queue_mgr(struct des_ctx *ctx, struct ablkcipher_request *areq,
559 u8 *iv, int encdec, int mode)
560 * \ingroup IFX_DES_FUNCTIONS
561 * \brief starts the process of queuing DEU requests
562 * \param *ctx crypto algo contax
563 * \param *areq Pointer to the balkcipher requests
564 * \param *iv Pointer to intput vector location
565 * \param dir Encrypt/Decrypt
566 * \mode The mode DES algo is running
567 * \return 0 if success
568 */
569
570 static int lq_queue_mgr(struct des_ctx *ctx, struct ablkcipher_request *areq,
571 u8 *iv, int encdec, int mode)
572 {
573 int err = -EINVAL;
574 unsigned long queue_flag;
575 struct scatterlist *src = areq->src;
576 struct scatterlist *dst = areq->dst;
577 struct des_container *des_con = NULL;
578 u32 remain, inc, nbytes = areq->nbytes;
579 u32 chunk_bytes = src->length;
580
581 des_con = (struct des_container *)kmalloc(sizeof(struct des_container),
582 GFP_KERNEL);
583
584 if (!(des_con)) {
585 printk("Cannot allocate memory for AES container, fn %s, ln %d\n",
586 __func__, __LINE__);
587 return -ENOMEM;
588 }
589
590 /* DES encrypt/decrypt mode */
591 if (mode == 5) {
592 nbytes = DES_BLOCK_SIZE;
593 chunk_bytes = DES_BLOCK_SIZE;
594 mode = 0;
595 }
596
597 des_con->bytes_processed = nbytes;
598 des_con->arequest = (*areq);
599 remain = nbytes;
600
601 //printk("debug - Line: %d, func: %s, reqsize: %d, scattersize: %d\n",
602 // __LINE__, __func__, nbytes, chunk_bytes);
603
604 if (remain > DEU_MAX_PACKET_SIZE)
605 inc = DEU_MAX_PACKET_SIZE;
606 else if(remain > chunk_bytes)
607 inc = chunk_bytes;
608 else
609 inc = remain;
610
611 remain -= inc;
612 lq_sg_init(des_con, src, dst);
613
614 if (remain <= 0 ) {
615 des_con->complete = 1;
616 }
617 else
618 des_con->complete = 0;
619
620 des_con->nbytes = inc;
621 des_con->iv = iv;
622 des_con->mode = mode;
623 des_con->encdec = encdec;
624
625 spin_lock_irqsave(&des_queue->lock, queue_flag);
626
627 if (des_queue->hw_status == DES_STARTED || des_queue->hw_status == DES_BUSY ||
628 des_queue->list.qlen > 0) {
629
630 des_con->flag = PROCESS_NEW_PACKET;
631 err = ablkcipher_enqueue_request(&des_queue->list, &des_con->arequest);
632 if (err == -EBUSY) {
633 spin_unlock_irqrestore(&des_queue->lock, queue_flag);
634 printk("Fail to enqueue ablkcipher request ln: %d, err: %d\n",
635 __LINE__, err);
636 return err;
637 }
638
639 spin_unlock_irqrestore(&des_queue->lock, queue_flag);
640 return -EINPROGRESS;
641
642 }
643 else if (des_queue->hw_status == DES_IDLE) {
644 des_queue->hw_status = DES_STARTED;
645 }
646
647 des_con->flag = PROCESS_SCATTER;
648 des_con->bytes_processed -= des_con->nbytes;
649
650 err = ablkcipher_enqueue_request(&des_queue->list, &des_con->arequest);
651 if (err == -EBUSY) {
652 printk("Fail to enqueue ablkcipher request ln: %d, err: %d\n",
653 __LINE__, err);
654
655 spin_unlock_irqrestore(&des_queue->lock, queue_flag);
656 return err;
657 }
658
659 spin_unlock_irqrestore(&des_queue->lock, queue_flag);
660 return lq_deu_des_core(ctx, des_con->dst_buf, des_con->src_buf, iv, inc, encdec, mode);
661
662 }
663
664 /* \fn static int lq_des_encrypt(struct ablkcipher_request *areq)
665 * \ingroup IFX_DES_FUNCTIONS
666 * \brief Decrypt function for DES algo
667 * \param *areq Pointer to ablkcipher request in memory
668 * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure
669 */
670
671 static int lq_des_encrypt(struct ablkcipher_request *areq)
672 {
673 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
674 struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher);
675
676 return lq_queue_mgr(ctx, areq, NULL, CRYPTO_DIR_ENCRYPT, 5);
677
678 }
679
680 /* \fn static int lq_des_decrypt(struct ablkcipher_request *areq)
681 * \ingroup IFX_DES_FUNCTIONS
682 * \brief Decrypt function for DES algo
683 * \param *areq Pointer to ablkcipher request in memory
684 * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure
685 */
686
687 static int lq_des_decrypt(struct ablkcipher_request *areq)
688 {
689 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
690 struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher);
691
692 return lq_queue_mgr(ctx, areq, NULL, CRYPTO_DIR_DECRYPT, 5);
693 }
694
695 /* \fn static int lq_ecb_des_encrypt(struct ablkcipher_request *areq)
696 * \ingroup IFX_DES_FUNCTIONS
697 * \brief Decrypt function for DES algo
698 * \param *areq Pointer to ablkcipher request in memory
699 * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure
700 */
701
702 static int lq_ecb_des_encrypt(struct ablkcipher_request *areq)
703 {
704 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
705 struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher);
706
707 return lq_queue_mgr(ctx, areq, areq->info, CRYPTO_DIR_ENCRYPT, 0);
708 }
709
710 /* \fn static int lq_ecb_des_decrypt(struct ablkcipher_request *areq)
711 * \ingroup IFX_DES_FUNCTIONS
712 * \brief Decrypt function for DES algo
713 * \param *areq Pointer to ablkcipher request in memory
714 * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure
715 */
716 static int lq_ecb_des_decrypt(struct ablkcipher_request *areq)
717 {
718 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
719 struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher);
720
721 return lq_queue_mgr(ctx, areq, areq->info, CRYPTO_DIR_DECRYPT, 0);
722
723 }
724
725 /* \fn static int lq_cbc_ecb_des_encrypt(struct ablkcipher_request *areq)
726 * \ingroup IFX_DES_FUNCTIONS
727 * \brief Decrypt function for DES algo
728 * \param *areq Pointer to ablkcipher request in memory
729 * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure
730 */
731
732 static int lq_cbc_des_encrypt(struct ablkcipher_request *areq)
733 {
734 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
735 struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher);
736
737 return lq_queue_mgr(ctx, areq, areq->info, CRYPTO_DIR_ENCRYPT, 1);
738 }
739 /* \fn static int lq_cbc_des_decrypt(struct ablkcipher_request *areq)
740 * \ingroup IFX_DES_FUNCTIONS
741 * \brief Decrypt function for DES algo
742 * \param *areq Pointer to ablkcipher request in memory
743 * \return 0 is success, -EINPROGRESS if encryting, EINVAL if failure
744 */
745
746 static int lq_cbc_des_decrypt(struct ablkcipher_request *areq)
747 {
748 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
749 struct des_ctx *ctx = crypto_ablkcipher_ctx(cipher);
750
751 return lq_queue_mgr(ctx, areq, areq->info, CRYPTO_DIR_DECRYPT, 1);
752 }
753
754 struct lq_des_alg {
755 struct crypto_alg alg;
756 };
757
758 /* DES Supported algo array */
759 static struct lq_des_alg des_drivers_alg [] = {
760 {
761 .alg = {
762 .cra_name = "des",
763 .cra_driver_name = "lqdeu-des",
764 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
765 .cra_blocksize = DES_BLOCK_SIZE,
766 .cra_ctxsize = sizeof(struct des_ctx),
767 .cra_type = &crypto_ablkcipher_type,
768 .cra_priority = 300,
769 .cra_module = THIS_MODULE,
770 .cra_ablkcipher = {
771 .setkey = lq_des_setkey,
772 .encrypt = lq_des_encrypt,
773 .decrypt = lq_des_decrypt,
774 .geniv = "eseqiv",
775 .min_keysize = DES_KEY_SIZE,
776 .max_keysize = DES_KEY_SIZE,
777 .ivsize = DES_BLOCK_SIZE,
778 }
779 }
780
781 },{
782 .alg = {
783 .cra_name = "ecb(des)",
784 .cra_driver_name = "lqdeu-ecb(des)",
785 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
786 .cra_blocksize = DES_BLOCK_SIZE,
787 .cra_ctxsize = sizeof(struct des_ctx),
788 .cra_type = &crypto_ablkcipher_type,
789 .cra_priority = 400,
790 .cra_module = THIS_MODULE,
791 .cra_ablkcipher = {
792 .setkey = lq_des_setkey,
793 .encrypt = lq_ecb_des_encrypt,
794 .decrypt = lq_ecb_des_decrypt,
795 .geniv = "eseqiv",
796 .min_keysize = DES_KEY_SIZE,
797 .max_keysize = DES_KEY_SIZE,
798 .ivsize = DES_BLOCK_SIZE,
799 }
800 }
801 },{
802 .alg = {
803 .cra_name = "cbc(des)",
804 .cra_driver_name = "lqdeu-cbc(des)",
805 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
806 .cra_blocksize = DES_BLOCK_SIZE,
807 .cra_ctxsize = sizeof(struct des_ctx),
808 .cra_type = &crypto_ablkcipher_type,
809 .cra_priority = 400,
810 .cra_module = THIS_MODULE,
811 .cra_ablkcipher = {
812 .setkey = lq_des_setkey,
813 .encrypt = lq_cbc_des_encrypt,
814 .decrypt = lq_cbc_des_decrypt,
815 .geniv = "eseqiv",
816 .min_keysize = DES3_EDE_KEY_SIZE,
817 .max_keysize = DES3_EDE_KEY_SIZE,
818 .ivsize = DES3_EDE_BLOCK_SIZE,
819 }
820 }
821 },{
822 .alg = {
823 .cra_name = "des3_ede",
824 .cra_driver_name = "lqdeu-des3_ede",
825 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
826 .cra_blocksize = DES_BLOCK_SIZE,
827 .cra_ctxsize = sizeof(struct des_ctx),
828 .cra_type = &crypto_ablkcipher_type,
829 .cra_priority = 300,
830 .cra_module = THIS_MODULE,
831 .cra_ablkcipher = {
832 .setkey = lq_des3_ede_setkey,
833 .encrypt = lq_des_encrypt,
834 .decrypt = lq_des_decrypt,
835 .geniv = "eseqiv",
836 .min_keysize = DES_KEY_SIZE,
837 .max_keysize = DES_KEY_SIZE,
838 .ivsize = DES_BLOCK_SIZE,
839 }
840 }
841 },{
842 .alg = {
843 .cra_name = "ecb(des3_ede)",
844 .cra_driver_name = "lqdeu-ecb(des3_ede)",
845 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
846 .cra_blocksize = DES_BLOCK_SIZE,
847 .cra_ctxsize = sizeof(struct des_ctx),
848 .cra_type = &crypto_ablkcipher_type,
849 .cra_priority = 400,
850 .cra_module = THIS_MODULE,
851 .cra_ablkcipher = {
852 .setkey = lq_des3_ede_setkey,
853 .encrypt = lq_ecb_des_encrypt,
854 .decrypt = lq_ecb_des_decrypt,
855 .geniv = "eseqiv",
856 .min_keysize = DES3_EDE_KEY_SIZE,
857 .max_keysize = DES3_EDE_KEY_SIZE,
858 .ivsize = DES3_EDE_BLOCK_SIZE,
859 }
860 }
861 },{
862 .alg = {
863 .cra_name = "cbc(des3_ede)",
864 .cra_driver_name = "lqdeu-cbc(des3_ede)",
865 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_KERN_DRIVER_ONLY | CRYPTO_ALG_ASYNC,
866 .cra_blocksize = DES_BLOCK_SIZE,
867 .cra_ctxsize = sizeof(struct des_ctx),
868 .cra_type = &crypto_ablkcipher_type,
869 .cra_priority = 400,
870 .cra_module = THIS_MODULE,
871 .cra_ablkcipher = {
872 .setkey = lq_des3_ede_setkey,
873 .encrypt = lq_cbc_des_encrypt,
874 .decrypt = lq_cbc_des_decrypt,
875 .geniv = "eseqiv",
876 .min_keysize = DES3_EDE_KEY_SIZE,
877 .max_keysize = DES3_EDE_KEY_SIZE,
878 .ivsize = DES3_EDE_BLOCK_SIZE,
879 }
880 }
881 }
882 };
883
884 /*! \fn int __init lqdeu_async_des_init (void)
885 * \ingroup IFX_DES_FUNCTIONS
886 * \brief initialize des driver
887 */
888 int __init lqdeu_async_des_init (void)
889 {
890 int i, j, ret = -EINVAL;
891
892 for (i = 0; i < ARRAY_SIZE(des_drivers_alg); i++) {
893 ret = crypto_register_alg(&des_drivers_alg[i].alg);
894 //printk("driver: %s\n", des_drivers_alg[i].alg.cra_name);
895 if (ret)
896 goto des_err;
897 }
898
899 des_chip_init();
900 CRTCL_SECT_INIT;
901
902
903 printk (KERN_NOTICE "IFX DEU DES initialized%s%s.\n", disable_multiblock ? "" : " (multiblock)", disable_deudma ? "" : " (DMA)");
904 return ret;
905
906 des_err:
907 for (j = 0; j < i; j++)
908 crypto_unregister_alg(&des_drivers_alg[i].alg);
909
910 printk(KERN_ERR "Lantiq %s driver initialization failed!\n", (char *)&des_drivers_alg[i].alg.cra_driver_name);
911 return ret;
912
913 cbc_des3_ede_err:
914 for (i = 0; i < ARRAY_SIZE(des_drivers_alg); i++) {
915 if (!strcmp((char *)&des_drivers_alg[i].alg.cra_name, "cbc(des3_ede)"))
916 crypto_unregister_alg(&des_drivers_alg[i].alg);
917 }
918
919 printk(KERN_ERR "Lantiq %s driver initialization failed!\n", (char *)&des_drivers_alg[i].alg.cra_driver_name);
920 return ret;
921 }
922
923 /*! \fn void __exit lqdeu_fini_async_des (void)
924 * \ingroup IFX_DES_FUNCTIONS
925 * \brief unregister des driver
926 */
927 void __exit lqdeu_fini_async_des (void)
928 {
929 int i;
930
931 for (i = 0; i < ARRAY_SIZE(des_drivers_alg); i++)
932 crypto_unregister_alg(&des_drivers_alg[i].alg);
933
934 des_queue->hw_status = DES_COMPLETED;
935 DEU_WAKEUP_EVENT(deu_dma_priv.deu_thread_wait, DES_ASYNC_EVENT,
936 deu_dma_priv.des_event_flags);
937
938 kfree(des_queue);
939 }
940