2 * talitos - Freescale Integrated Security Engine (SEC) device driver
4 * Copyright (c) 2008 Freescale Semiconductor, Inc.
6 * Scatterlist Crypto API glue code copied from files with the following:
7 * Copyright (c) 2006-2007 Herbert Xu <herbert@gondor.apana.org.au>
9 * Crypto algorithm registration code copied from hifn driver:
10 * 2007+ Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
11 * All rights reserved.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/mod_devicetable.h>
31 #include <linux/device.h>
32 #include <linux/interrupt.h>
33 #include <linux/crypto.h>
34 #include <linux/hw_random.h>
35 #include <linux/of_platform.h>
36 #include <linux/dma-mapping.h>
38 #include <linux/spinlock.h>
39 #include <linux/rtnetlink.h>
41 #include <crypto/algapi.h>
42 #include <crypto/aes.h>
43 #include <crypto/des.h>
44 #include <crypto/sha.h>
45 #include <crypto/aead.h>
46 #include <crypto/authenc.h>
47 #include <crypto/skcipher.h>
48 #include <crypto/scatterwalk.h>
52 #define TALITOS_TIMEOUT 100000
53 #define TALITOS_MAX_DATA_LEN 65535
55 #define DESC_TYPE(desc_hdr) ((be32_to_cpu(desc_hdr) >> 3) & 0x1f)
56 #define PRIMARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 28) & 0xf)
57 #define SECONDARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 16) & 0xf)
59 /* descriptor pointer entry */
61 __be16 len
; /* length */
62 u8 j_extent
; /* jump to sg link table and/or extent */
63 u8 eptr
; /* extended address */
64 __be32 ptr
; /* address */
69 __be32 hdr
; /* header high bits */
70 __be32 hdr_lo
; /* header low bits */
71 struct talitos_ptr ptr
[7]; /* ptr/len pair array */
75 * talitos_request - descriptor submission request
76 * @desc: descriptor pointer (kernel virtual)
77 * @dma_desc: descriptor's physical bus address
78 * @callback: whom to call when descriptor processing is done
79 * @context: caller context (optional)
81 struct talitos_request
{
82 struct talitos_desc
*desc
;
84 void (*callback
) (struct device
*dev
, struct talitos_desc
*desc
,
85 void *context
, int error
);
89 /* per-channel fifo management */
90 struct talitos_channel
{
92 struct talitos_request
*fifo
;
94 /* number of requests pending in channel h/w fifo */
95 atomic_t submit_count ____cacheline_aligned
;
97 /* request submission (head) lock */
98 spinlock_t head_lock ____cacheline_aligned
;
99 /* index to next free descriptor request */
102 /* request release (tail) lock */
103 spinlock_t tail_lock ____cacheline_aligned
;
104 /* index to next in-progress/done descriptor request */
108 struct talitos_private
{
110 struct of_device
*ofdev
;
114 /* SEC version geometry (from device tree node) */
115 unsigned int num_channels
;
116 unsigned int chfifo_len
;
117 unsigned int exec_units
;
118 unsigned int desc_types
;
120 /* SEC Compatibility info */
121 unsigned long features
;
124 * length of the request fifo
125 * fifo_len is chfifo_len rounded up to next power of 2
126 * so we can use bitwise ops to wrap
128 unsigned int fifo_len
;
130 struct talitos_channel
*chan
;
132 /* next channel to be assigned next incoming descriptor */
133 atomic_t last_chan ____cacheline_aligned
;
135 /* request callback tasklet */
136 struct tasklet_struct done_task
;
138 /* list of registered algorithms */
139 struct list_head alg_list
;
146 #define TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT 0x00000001
147 #define TALITOS_FTR_HW_AUTH_CHECK 0x00000002
149 static void to_talitos_ptr(struct talitos_ptr
*talitos_ptr
, dma_addr_t dma_addr
)
151 talitos_ptr
->ptr
= cpu_to_be32(lower_32_bits(dma_addr
));
152 talitos_ptr
->eptr
= cpu_to_be32(upper_32_bits(dma_addr
));
156 * map virtual single (contiguous) pointer to h/w descriptor pointer
158 static void map_single_talitos_ptr(struct device
*dev
,
159 struct talitos_ptr
*talitos_ptr
,
160 unsigned short len
, void *data
,
161 unsigned char extent
,
162 enum dma_data_direction dir
)
164 dma_addr_t dma_addr
= dma_map_single(dev
, data
, len
, dir
);
166 talitos_ptr
->len
= cpu_to_be16(len
);
167 to_talitos_ptr(talitos_ptr
, dma_addr
);
168 talitos_ptr
->j_extent
= extent
;
172 * unmap bus single (contiguous) h/w descriptor pointer
174 static void unmap_single_talitos_ptr(struct device
*dev
,
175 struct talitos_ptr
*talitos_ptr
,
176 enum dma_data_direction dir
)
178 dma_unmap_single(dev
, be32_to_cpu(talitos_ptr
->ptr
),
179 be16_to_cpu(talitos_ptr
->len
), dir
);
182 static int reset_channel(struct device
*dev
, int ch
)
184 struct talitos_private
*priv
= dev_get_drvdata(dev
);
185 unsigned int timeout
= TALITOS_TIMEOUT
;
187 setbits32(priv
->reg
+ TALITOS_CCCR(ch
), TALITOS_CCCR_RESET
);
189 while ((in_be32(priv
->reg
+ TALITOS_CCCR(ch
)) & TALITOS_CCCR_RESET
)
194 dev_err(dev
, "failed to reset channel %d\n", ch
);
198 /* set 36-bit addressing, done writeback enable and done IRQ enable */
199 setbits32(priv
->reg
+ TALITOS_CCCR_LO(ch
), TALITOS_CCCR_LO_EAE
|
200 TALITOS_CCCR_LO_CDWE
| TALITOS_CCCR_LO_CDIE
);
202 /* and ICCR writeback, if available */
203 if (priv
->features
& TALITOS_FTR_HW_AUTH_CHECK
)
204 setbits32(priv
->reg
+ TALITOS_CCCR_LO(ch
),
205 TALITOS_CCCR_LO_IWSE
);
210 static int reset_device(struct device
*dev
)
212 struct talitos_private
*priv
= dev_get_drvdata(dev
);
213 unsigned int timeout
= TALITOS_TIMEOUT
;
215 setbits32(priv
->reg
+ TALITOS_MCR
, TALITOS_MCR_SWR
);
217 while ((in_be32(priv
->reg
+ TALITOS_MCR
) & TALITOS_MCR_SWR
)
222 dev_err(dev
, "failed to reset device\n");
230 * Reset and initialize the device
232 static int init_device(struct device
*dev
)
234 struct talitos_private
*priv
= dev_get_drvdata(dev
);
239 * errata documentation: warning: certain SEC interrupts
240 * are not fully cleared by writing the MCR:SWR bit,
241 * set bit twice to completely reset
243 err
= reset_device(dev
);
247 err
= reset_device(dev
);
252 for (ch
= 0; ch
< priv
->num_channels
; ch
++) {
253 err
= reset_channel(dev
, ch
);
258 /* enable channel done and error interrupts */
259 setbits32(priv
->reg
+ TALITOS_IMR
, TALITOS_IMR_INIT
);
260 setbits32(priv
->reg
+ TALITOS_IMR_LO
, TALITOS_IMR_LO_INIT
);
262 /* disable integrity check error interrupts (use writeback instead) */
263 if (priv
->features
& TALITOS_FTR_HW_AUTH_CHECK
)
264 setbits32(priv
->reg
+ TALITOS_MDEUICR_LO
,
265 TALITOS_MDEUICR_LO_ICE
);
271 * talitos_submit - submits a descriptor to the device for processing
272 * @dev: the SEC device to be used
273 * @desc: the descriptor to be processed by the device
274 * @callback: whom to call when processing is complete
275 * @context: a handle for use by caller (optional)
277 * desc must contain valid dma-mapped (bus physical) address pointers.
278 * callback must check err and feedback in descriptor header
279 * for device processing status.
281 static int talitos_submit(struct device
*dev
, struct talitos_desc
*desc
,
282 void (*callback
)(struct device
*dev
,
283 struct talitos_desc
*desc
,
284 void *context
, int error
),
287 struct talitos_private
*priv
= dev_get_drvdata(dev
);
288 struct talitos_request
*request
;
289 unsigned long flags
, ch
;
292 /* select done notification */
293 desc
->hdr
|= DESC_HDR_DONE_NOTIFY
;
295 /* emulate SEC's round-robin channel fifo polling scheme */
296 ch
= atomic_inc_return(&priv
->last_chan
) & (priv
->num_channels
- 1);
298 spin_lock_irqsave(&priv
->chan
[ch
].head_lock
, flags
);
300 if (!atomic_inc_not_zero(&priv
->chan
[ch
].submit_count
)) {
301 /* h/w fifo is full */
302 spin_unlock_irqrestore(&priv
->chan
[ch
].head_lock
, flags
);
306 head
= priv
->chan
[ch
].head
;
307 request
= &priv
->chan
[ch
].fifo
[head
];
309 /* map descriptor and save caller data */
310 request
->dma_desc
= dma_map_single(dev
, desc
, sizeof(*desc
),
312 request
->callback
= callback
;
313 request
->context
= context
;
315 /* increment fifo head */
316 priv
->chan
[ch
].head
= (priv
->chan
[ch
].head
+ 1) & (priv
->fifo_len
- 1);
319 request
->desc
= desc
;
323 out_be32(priv
->reg
+ TALITOS_FF(ch
),
324 cpu_to_be32(upper_32_bits(request
->dma_desc
)));
325 out_be32(priv
->reg
+ TALITOS_FF_LO(ch
),
326 cpu_to_be32(lower_32_bits(request
->dma_desc
)));
328 spin_unlock_irqrestore(&priv
->chan
[ch
].head_lock
, flags
);
334 * process what was done, notify callback of error if not
336 static void flush_channel(struct device
*dev
, int ch
, int error
, int reset_ch
)
338 struct talitos_private
*priv
= dev_get_drvdata(dev
);
339 struct talitos_request
*request
, saved_req
;
343 spin_lock_irqsave(&priv
->chan
[ch
].tail_lock
, flags
);
345 tail
= priv
->chan
[ch
].tail
;
346 while (priv
->chan
[ch
].fifo
[tail
].desc
) {
347 request
= &priv
->chan
[ch
].fifo
[tail
];
349 /* descriptors with their done bits set don't get the error */
351 if ((request
->desc
->hdr
& DESC_HDR_DONE
) == DESC_HDR_DONE
)
359 dma_unmap_single(dev
, request
->dma_desc
,
360 sizeof(struct talitos_desc
),
363 /* copy entries so we can call callback outside lock */
364 saved_req
.desc
= request
->desc
;
365 saved_req
.callback
= request
->callback
;
366 saved_req
.context
= request
->context
;
368 /* release request entry in fifo */
370 request
->desc
= NULL
;
372 /* increment fifo tail */
373 priv
->chan
[ch
].tail
= (tail
+ 1) & (priv
->fifo_len
- 1);
375 spin_unlock_irqrestore(&priv
->chan
[ch
].tail_lock
, flags
);
377 atomic_dec(&priv
->chan
[ch
].submit_count
);
379 saved_req
.callback(dev
, saved_req
.desc
, saved_req
.context
,
381 /* channel may resume processing in single desc error case */
382 if (error
&& !reset_ch
&& status
== error
)
384 spin_lock_irqsave(&priv
->chan
[ch
].tail_lock
, flags
);
385 tail
= priv
->chan
[ch
].tail
;
388 spin_unlock_irqrestore(&priv
->chan
[ch
].tail_lock
, flags
);
392 * process completed requests for channels that have done status
394 static void talitos_done(unsigned long data
)
396 struct device
*dev
= (struct device
*)data
;
397 struct talitos_private
*priv
= dev_get_drvdata(dev
);
400 for (ch
= 0; ch
< priv
->num_channels
; ch
++)
401 flush_channel(dev
, ch
, 0, 0);
403 /* At this point, all completed channels have been processed.
404 * Unmask done interrupts for channels completed later on.
406 setbits32(priv
->reg
+ TALITOS_IMR
, TALITOS_IMR_INIT
);
407 setbits32(priv
->reg
+ TALITOS_IMR_LO
, TALITOS_IMR_LO_INIT
);
411 * locate current (offending) descriptor
413 static struct talitos_desc
*current_desc(struct device
*dev
, int ch
)
415 struct talitos_private
*priv
= dev_get_drvdata(dev
);
416 int tail
= priv
->chan
[ch
].tail
;
419 cur_desc
= in_be32(priv
->reg
+ TALITOS_CDPR_LO(ch
));
421 while (priv
->chan
[ch
].fifo
[tail
].dma_desc
!= cur_desc
) {
422 tail
= (tail
+ 1) & (priv
->fifo_len
- 1);
423 if (tail
== priv
->chan
[ch
].tail
) {
424 dev_err(dev
, "couldn't locate current descriptor\n");
429 return priv
->chan
[ch
].fifo
[tail
].desc
;
433 * user diagnostics; report root cause of error based on execution unit status
435 static void report_eu_error(struct device
*dev
, int ch
,
436 struct talitos_desc
*desc
)
438 struct talitos_private
*priv
= dev_get_drvdata(dev
);
441 switch (desc
->hdr
& DESC_HDR_SEL0_MASK
) {
442 case DESC_HDR_SEL0_AFEU
:
443 dev_err(dev
, "AFEUISR 0x%08x_%08x\n",
444 in_be32(priv
->reg
+ TALITOS_AFEUISR
),
445 in_be32(priv
->reg
+ TALITOS_AFEUISR_LO
));
447 case DESC_HDR_SEL0_DEU
:
448 dev_err(dev
, "DEUISR 0x%08x_%08x\n",
449 in_be32(priv
->reg
+ TALITOS_DEUISR
),
450 in_be32(priv
->reg
+ TALITOS_DEUISR_LO
));
452 case DESC_HDR_SEL0_MDEUA
:
453 case DESC_HDR_SEL0_MDEUB
:
454 dev_err(dev
, "MDEUISR 0x%08x_%08x\n",
455 in_be32(priv
->reg
+ TALITOS_MDEUISR
),
456 in_be32(priv
->reg
+ TALITOS_MDEUISR_LO
));
458 case DESC_HDR_SEL0_RNG
:
459 dev_err(dev
, "RNGUISR 0x%08x_%08x\n",
460 in_be32(priv
->reg
+ TALITOS_RNGUISR
),
461 in_be32(priv
->reg
+ TALITOS_RNGUISR_LO
));
463 case DESC_HDR_SEL0_PKEU
:
464 dev_err(dev
, "PKEUISR 0x%08x_%08x\n",
465 in_be32(priv
->reg
+ TALITOS_PKEUISR
),
466 in_be32(priv
->reg
+ TALITOS_PKEUISR_LO
));
468 case DESC_HDR_SEL0_AESU
:
469 dev_err(dev
, "AESUISR 0x%08x_%08x\n",
470 in_be32(priv
->reg
+ TALITOS_AESUISR
),
471 in_be32(priv
->reg
+ TALITOS_AESUISR_LO
));
473 case DESC_HDR_SEL0_CRCU
:
474 dev_err(dev
, "CRCUISR 0x%08x_%08x\n",
475 in_be32(priv
->reg
+ TALITOS_CRCUISR
),
476 in_be32(priv
->reg
+ TALITOS_CRCUISR_LO
));
478 case DESC_HDR_SEL0_KEU
:
479 dev_err(dev
, "KEUISR 0x%08x_%08x\n",
480 in_be32(priv
->reg
+ TALITOS_KEUISR
),
481 in_be32(priv
->reg
+ TALITOS_KEUISR_LO
));
485 switch (desc
->hdr
& DESC_HDR_SEL1_MASK
) {
486 case DESC_HDR_SEL1_MDEUA
:
487 case DESC_HDR_SEL1_MDEUB
:
488 dev_err(dev
, "MDEUISR 0x%08x_%08x\n",
489 in_be32(priv
->reg
+ TALITOS_MDEUISR
),
490 in_be32(priv
->reg
+ TALITOS_MDEUISR_LO
));
492 case DESC_HDR_SEL1_CRCU
:
493 dev_err(dev
, "CRCUISR 0x%08x_%08x\n",
494 in_be32(priv
->reg
+ TALITOS_CRCUISR
),
495 in_be32(priv
->reg
+ TALITOS_CRCUISR_LO
));
499 for (i
= 0; i
< 8; i
++)
500 dev_err(dev
, "DESCBUF 0x%08x_%08x\n",
501 in_be32(priv
->reg
+ TALITOS_DESCBUF(ch
) + 8*i
),
502 in_be32(priv
->reg
+ TALITOS_DESCBUF_LO(ch
) + 8*i
));
506 * recover from error interrupts
508 static void talitos_error(unsigned long data
, u32 isr
, u32 isr_lo
)
510 struct device
*dev
= (struct device
*)data
;
511 struct talitos_private
*priv
= dev_get_drvdata(dev
);
512 unsigned int timeout
= TALITOS_TIMEOUT
;
513 int ch
, error
, reset_dev
= 0, reset_ch
= 0;
516 for (ch
= 0; ch
< priv
->num_channels
; ch
++) {
517 /* skip channels without errors */
518 if (!(isr
& (1 << (ch
* 2 + 1))))
523 v
= in_be32(priv
->reg
+ TALITOS_CCPSR(ch
));
524 v_lo
= in_be32(priv
->reg
+ TALITOS_CCPSR_LO(ch
));
526 if (v_lo
& TALITOS_CCPSR_LO_DOF
) {
527 dev_err(dev
, "double fetch fifo overflow error\n");
531 if (v_lo
& TALITOS_CCPSR_LO_SOF
) {
532 /* h/w dropped descriptor */
533 dev_err(dev
, "single fetch fifo overflow error\n");
536 if (v_lo
& TALITOS_CCPSR_LO_MDTE
)
537 dev_err(dev
, "master data transfer error\n");
538 if (v_lo
& TALITOS_CCPSR_LO_SGDLZ
)
539 dev_err(dev
, "s/g data length zero error\n");
540 if (v_lo
& TALITOS_CCPSR_LO_FPZ
)
541 dev_err(dev
, "fetch pointer zero error\n");
542 if (v_lo
& TALITOS_CCPSR_LO_IDH
)
543 dev_err(dev
, "illegal descriptor header error\n");
544 if (v_lo
& TALITOS_CCPSR_LO_IEU
)
545 dev_err(dev
, "invalid execution unit error\n");
546 if (v_lo
& TALITOS_CCPSR_LO_EU
)
547 report_eu_error(dev
, ch
, current_desc(dev
, ch
));
548 if (v_lo
& TALITOS_CCPSR_LO_GB
)
549 dev_err(dev
, "gather boundary error\n");
550 if (v_lo
& TALITOS_CCPSR_LO_GRL
)
551 dev_err(dev
, "gather return/length error\n");
552 if (v_lo
& TALITOS_CCPSR_LO_SB
)
553 dev_err(dev
, "scatter boundary error\n");
554 if (v_lo
& TALITOS_CCPSR_LO_SRL
)
555 dev_err(dev
, "scatter return/length error\n");
557 flush_channel(dev
, ch
, error
, reset_ch
);
560 reset_channel(dev
, ch
);
562 setbits32(priv
->reg
+ TALITOS_CCCR(ch
),
564 setbits32(priv
->reg
+ TALITOS_CCCR_LO(ch
), 0);
565 while ((in_be32(priv
->reg
+ TALITOS_CCCR(ch
)) &
566 TALITOS_CCCR_CONT
) && --timeout
)
569 dev_err(dev
, "failed to restart channel %d\n",
575 if (reset_dev
|| isr
& ~TALITOS_ISR_CHERR
|| isr_lo
) {
576 dev_err(dev
, "done overflow, internal time out, or rngu error: "
577 "ISR 0x%08x_%08x\n", isr
, isr_lo
);
579 /* purge request queues */
580 for (ch
= 0; ch
< priv
->num_channels
; ch
++)
581 flush_channel(dev
, ch
, -EIO
, 1);
583 /* reset and reinitialize the device */
588 static irqreturn_t
talitos_interrupt(int irq
, void *data
)
590 struct device
*dev
= data
;
591 struct talitos_private
*priv
= dev_get_drvdata(dev
);
594 isr
= in_be32(priv
->reg
+ TALITOS_ISR
);
595 isr_lo
= in_be32(priv
->reg
+ TALITOS_ISR_LO
);
596 /* Acknowledge interrupt */
597 out_be32(priv
->reg
+ TALITOS_ICR
, isr
);
598 out_be32(priv
->reg
+ TALITOS_ICR_LO
, isr_lo
);
600 if (unlikely((isr
& ~TALITOS_ISR_CHDONE
) || isr_lo
))
601 talitos_error((unsigned long)data
, isr
, isr_lo
);
603 if (likely(isr
& TALITOS_ISR_CHDONE
)) {
604 /* mask further done interrupts. */
605 clrbits32(priv
->reg
+ TALITOS_IMR
, TALITOS_IMR_DONE
);
606 /* done_task will unmask done interrupts at exit */
607 tasklet_schedule(&priv
->done_task
);
610 return (isr
|| isr_lo
) ? IRQ_HANDLED
: IRQ_NONE
;
616 static int talitos_rng_data_present(struct hwrng
*rng
, int wait
)
618 struct device
*dev
= (struct device
*)rng
->priv
;
619 struct talitos_private
*priv
= dev_get_drvdata(dev
);
623 for (i
= 0; i
< 20; i
++) {
624 ofl
= in_be32(priv
->reg
+ TALITOS_RNGUSR_LO
) &
625 TALITOS_RNGUSR_LO_OFL
;
634 static int talitos_rng_data_read(struct hwrng
*rng
, u32
*data
)
636 struct device
*dev
= (struct device
*)rng
->priv
;
637 struct talitos_private
*priv
= dev_get_drvdata(dev
);
639 /* rng fifo requires 64-bit accesses */
640 *data
= in_be32(priv
->reg
+ TALITOS_RNGU_FIFO
);
641 *data
= in_be32(priv
->reg
+ TALITOS_RNGU_FIFO_LO
);
646 static int talitos_rng_init(struct hwrng
*rng
)
648 struct device
*dev
= (struct device
*)rng
->priv
;
649 struct talitos_private
*priv
= dev_get_drvdata(dev
);
650 unsigned int timeout
= TALITOS_TIMEOUT
;
652 setbits32(priv
->reg
+ TALITOS_RNGURCR_LO
, TALITOS_RNGURCR_LO_SR
);
653 while (!(in_be32(priv
->reg
+ TALITOS_RNGUSR_LO
) & TALITOS_RNGUSR_LO_RD
)
657 dev_err(dev
, "failed to reset rng hw\n");
661 /* start generating */
662 setbits32(priv
->reg
+ TALITOS_RNGUDSR_LO
, 0);
667 static int talitos_register_rng(struct device
*dev
)
669 struct talitos_private
*priv
= dev_get_drvdata(dev
);
671 priv
->rng
.name
= dev_driver_string(dev
),
672 priv
->rng
.init
= talitos_rng_init
,
673 priv
->rng
.data_present
= talitos_rng_data_present
,
674 priv
->rng
.data_read
= talitos_rng_data_read
,
675 priv
->rng
.priv
= (unsigned long)dev
;
677 return hwrng_register(&priv
->rng
);
680 static void talitos_unregister_rng(struct device
*dev
)
682 struct talitos_private
*priv
= dev_get_drvdata(dev
);
684 hwrng_unregister(&priv
->rng
);
690 #define TALITOS_CRA_PRIORITY 3000
691 #define TALITOS_MAX_KEY_SIZE 64
692 #define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
694 #define MD5_DIGEST_SIZE 16
698 __be32 desc_hdr_template
;
699 u8 key
[TALITOS_MAX_KEY_SIZE
];
700 u8 iv
[TALITOS_MAX_IV_LENGTH
];
702 unsigned int enckeylen
;
703 unsigned int authkeylen
;
704 unsigned int authsize
;
707 static int aead_setauthsize(struct crypto_aead
*authenc
,
708 unsigned int authsize
)
710 struct talitos_ctx
*ctx
= crypto_aead_ctx(authenc
);
712 ctx
->authsize
= authsize
;
717 static int aead_setkey(struct crypto_aead
*authenc
,
718 const u8
*key
, unsigned int keylen
)
720 struct talitos_ctx
*ctx
= crypto_aead_ctx(authenc
);
721 struct rtattr
*rta
= (void *)key
;
722 struct crypto_authenc_key_param
*param
;
723 unsigned int authkeylen
;
724 unsigned int enckeylen
;
726 if (!RTA_OK(rta
, keylen
))
729 if (rta
->rta_type
!= CRYPTO_AUTHENC_KEYA_PARAM
)
732 if (RTA_PAYLOAD(rta
) < sizeof(*param
))
735 param
= RTA_DATA(rta
);
736 enckeylen
= be32_to_cpu(param
->enckeylen
);
738 key
+= RTA_ALIGN(rta
->rta_len
);
739 keylen
-= RTA_ALIGN(rta
->rta_len
);
741 if (keylen
< enckeylen
)
744 authkeylen
= keylen
- enckeylen
;
746 if (keylen
> TALITOS_MAX_KEY_SIZE
)
749 memcpy(&ctx
->key
, key
, keylen
);
751 ctx
->keylen
= keylen
;
752 ctx
->enckeylen
= enckeylen
;
753 ctx
->authkeylen
= authkeylen
;
758 crypto_aead_set_flags(authenc
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
763 * talitos_edesc - s/w-extended descriptor
764 * @src_nents: number of segments in input scatterlist
765 * @dst_nents: number of segments in output scatterlist
766 * @dma_len: length of dma mapped link_tbl space
767 * @dma_link_tbl: bus physical address of link_tbl
768 * @desc: h/w descriptor
769 * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
771 * if decrypting (with authcheck), or either one of src_nents or dst_nents
772 * is greater than 1, an integrity check value is concatenated to the end
775 struct talitos_edesc
{
781 dma_addr_t dma_link_tbl
;
782 struct talitos_desc desc
;
783 struct talitos_ptr link_tbl
[0];
786 static int talitos_map_sg(struct device
*dev
, struct scatterlist
*sg
,
787 unsigned int nents
, enum dma_data_direction dir
,
790 if (unlikely(chained
))
792 dma_map_sg(dev
, sg
, 1, dir
);
793 sg
= scatterwalk_sg_next(sg
);
796 dma_map_sg(dev
, sg
, nents
, dir
);
800 static void talitos_unmap_sg_chain(struct device
*dev
, struct scatterlist
*sg
,
801 enum dma_data_direction dir
)
804 dma_unmap_sg(dev
, sg
, 1, dir
);
805 sg
= scatterwalk_sg_next(sg
);
809 static void talitos_sg_unmap(struct device
*dev
,
810 struct talitos_edesc
*edesc
,
811 struct scatterlist
*src
,
812 struct scatterlist
*dst
)
814 unsigned int src_nents
= edesc
->src_nents
? : 1;
815 unsigned int dst_nents
= edesc
->dst_nents
? : 1;
818 if (edesc
->src_is_chained
)
819 talitos_unmap_sg_chain(dev
, src
, DMA_TO_DEVICE
);
821 dma_unmap_sg(dev
, src
, src_nents
, DMA_TO_DEVICE
);
823 if (edesc
->dst_is_chained
)
824 talitos_unmap_sg_chain(dev
, dst
, DMA_FROM_DEVICE
);
826 dma_unmap_sg(dev
, dst
, dst_nents
, DMA_FROM_DEVICE
);
828 if (edesc
->src_is_chained
)
829 talitos_unmap_sg_chain(dev
, src
, DMA_BIDIRECTIONAL
);
831 dma_unmap_sg(dev
, src
, src_nents
, DMA_BIDIRECTIONAL
);
834 static void ipsec_esp_unmap(struct device
*dev
,
835 struct talitos_edesc
*edesc
,
836 struct aead_request
*areq
)
838 unmap_single_talitos_ptr(dev
, &edesc
->desc
.ptr
[6], DMA_FROM_DEVICE
);
839 unmap_single_talitos_ptr(dev
, &edesc
->desc
.ptr
[3], DMA_TO_DEVICE
);
840 unmap_single_talitos_ptr(dev
, &edesc
->desc
.ptr
[2], DMA_TO_DEVICE
);
841 unmap_single_talitos_ptr(dev
, &edesc
->desc
.ptr
[0], DMA_TO_DEVICE
);
843 dma_unmap_sg(dev
, areq
->assoc
, 1, DMA_TO_DEVICE
);
845 talitos_sg_unmap(dev
, edesc
, areq
->src
, areq
->dst
);
848 dma_unmap_single(dev
, edesc
->dma_link_tbl
, edesc
->dma_len
,
853 * ipsec_esp descriptor callbacks
855 static void ipsec_esp_encrypt_done(struct device
*dev
,
856 struct talitos_desc
*desc
, void *context
,
859 struct aead_request
*areq
= context
;
860 struct crypto_aead
*authenc
= crypto_aead_reqtfm(areq
);
861 struct talitos_ctx
*ctx
= crypto_aead_ctx(authenc
);
862 struct talitos_edesc
*edesc
;
863 struct scatterlist
*sg
;
866 edesc
= container_of(desc
, struct talitos_edesc
, desc
);
868 ipsec_esp_unmap(dev
, edesc
, areq
);
870 /* copy the generated ICV to dst */
871 if (edesc
->dma_len
) {
872 icvdata
= &edesc
->link_tbl
[edesc
->src_nents
+
873 edesc
->dst_nents
+ 2];
874 sg
= sg_last(areq
->dst
, edesc
->dst_nents
);
875 memcpy((char *)sg_virt(sg
) + sg
->length
- ctx
->authsize
,
876 icvdata
, ctx
->authsize
);
881 aead_request_complete(areq
, err
);
884 static void ipsec_esp_decrypt_swauth_done(struct device
*dev
,
885 struct talitos_desc
*desc
,
886 void *context
, int err
)
888 struct aead_request
*req
= context
;
889 struct crypto_aead
*authenc
= crypto_aead_reqtfm(req
);
890 struct talitos_ctx
*ctx
= crypto_aead_ctx(authenc
);
891 struct talitos_edesc
*edesc
;
892 struct scatterlist
*sg
;
895 edesc
= container_of(desc
, struct talitos_edesc
, desc
);
897 ipsec_esp_unmap(dev
, edesc
, req
);
902 icvdata
= &edesc
->link_tbl
[edesc
->src_nents
+
903 edesc
->dst_nents
+ 2];
905 icvdata
= &edesc
->link_tbl
[0];
907 sg
= sg_last(req
->dst
, edesc
->dst_nents
? : 1);
908 err
= memcmp(icvdata
, (char *)sg_virt(sg
) + sg
->length
-
909 ctx
->authsize
, ctx
->authsize
) ? -EBADMSG
: 0;
914 aead_request_complete(req
, err
);
917 static void ipsec_esp_decrypt_hwauth_done(struct device
*dev
,
918 struct talitos_desc
*desc
,
919 void *context
, int err
)
921 struct aead_request
*req
= context
;
922 struct talitos_edesc
*edesc
;
924 edesc
= container_of(desc
, struct talitos_edesc
, desc
);
926 ipsec_esp_unmap(dev
, edesc
, req
);
928 /* check ICV auth status */
929 if (!err
&& ((desc
->hdr_lo
& DESC_HDR_LO_ICCR1_MASK
) !=
930 DESC_HDR_LO_ICCR1_PASS
))
935 aead_request_complete(req
, err
);
939 * convert scatterlist to SEC h/w link table format
940 * stop at cryptlen bytes
942 static int sg_to_link_tbl(struct scatterlist
*sg
, int sg_count
,
943 int cryptlen
, struct talitos_ptr
*link_tbl_ptr
)
948 to_talitos_ptr(link_tbl_ptr
, sg_dma_address(sg
));
949 link_tbl_ptr
->len
= cpu_to_be16(sg_dma_len(sg
));
950 link_tbl_ptr
->j_extent
= 0;
952 cryptlen
-= sg_dma_len(sg
);
953 sg
= scatterwalk_sg_next(sg
);
956 /* adjust (decrease) last one (or two) entry's len to cryptlen */
958 while (be16_to_cpu(link_tbl_ptr
->len
) <= (-cryptlen
)) {
959 /* Empty this entry, and move to previous one */
960 cryptlen
+= be16_to_cpu(link_tbl_ptr
->len
);
961 link_tbl_ptr
->len
= 0;
965 link_tbl_ptr
->len
= cpu_to_be16(be16_to_cpu(link_tbl_ptr
->len
)
968 /* tag end of link table */
969 link_tbl_ptr
->j_extent
= DESC_PTR_LNKTBL_RETURN
;
975 * fill in and submit ipsec_esp descriptor
977 static int ipsec_esp(struct talitos_edesc
*edesc
, struct aead_request
*areq
,
979 void (*callback
) (struct device
*dev
,
980 struct talitos_desc
*desc
,
981 void *context
, int error
))
983 struct crypto_aead
*aead
= crypto_aead_reqtfm(areq
);
984 struct talitos_ctx
*ctx
= crypto_aead_ctx(aead
);
985 struct device
*dev
= ctx
->dev
;
986 struct talitos_desc
*desc
= &edesc
->desc
;
987 unsigned int cryptlen
= areq
->cryptlen
;
988 unsigned int authsize
= ctx
->authsize
;
989 unsigned int ivsize
= crypto_aead_ivsize(aead
);
994 map_single_talitos_ptr(dev
, &desc
->ptr
[0], ctx
->authkeylen
, &ctx
->key
,
997 map_single_talitos_ptr(dev
, &desc
->ptr
[1], areq
->assoclen
+ ivsize
,
998 sg_virt(areq
->assoc
), 0, DMA_TO_DEVICE
);
1000 map_single_talitos_ptr(dev
, &desc
->ptr
[2], ivsize
, giv
?: areq
->iv
, 0,
1004 map_single_talitos_ptr(dev
, &desc
->ptr
[3], ctx
->enckeylen
,
1005 (char *)&ctx
->key
+ ctx
->authkeylen
, 0,
1010 * map and adjust cipher len to aead request cryptlen.
1011 * extent is bytes of HMAC postpended to ciphertext,
1012 * typically 12 for ipsec
1014 desc
->ptr
[4].len
= cpu_to_be16(cryptlen
);
1015 desc
->ptr
[4].j_extent
= authsize
;
1017 sg_count
= talitos_map_sg(dev
, areq
->src
, edesc
->src_nents
? : 1,
1018 (areq
->src
== areq
->dst
) ? DMA_BIDIRECTIONAL
1020 edesc
->src_is_chained
);
1022 if (sg_count
== 1) {
1023 to_talitos_ptr(&desc
->ptr
[4], sg_dma_address(areq
->src
));
1025 sg_link_tbl_len
= cryptlen
;
1027 if (edesc
->desc
.hdr
& DESC_HDR_MODE1_MDEU_CICV
)
1028 sg_link_tbl_len
= cryptlen
+ authsize
;
1030 sg_count
= sg_to_link_tbl(areq
->src
, sg_count
, sg_link_tbl_len
,
1031 &edesc
->link_tbl
[0]);
1033 desc
->ptr
[4].j_extent
|= DESC_PTR_LNKTBL_JUMP
;
1034 to_talitos_ptr(&desc
->ptr
[4], edesc
->dma_link_tbl
);
1035 dma_sync_single_for_device(dev
, edesc
->dma_link_tbl
,
1039 /* Only one segment now, so no link tbl needed */
1040 to_talitos_ptr(&desc
->ptr
[4],
1041 sg_dma_address(areq
->src
));
1046 desc
->ptr
[5].len
= cpu_to_be16(cryptlen
);
1047 desc
->ptr
[5].j_extent
= authsize
;
1049 if (areq
->src
!= areq
->dst
)
1050 sg_count
= talitos_map_sg(dev
, areq
->dst
,
1051 edesc
->dst_nents
? : 1,
1053 edesc
->dst_is_chained
);
1055 if (sg_count
== 1) {
1056 to_talitos_ptr(&desc
->ptr
[5], sg_dma_address(areq
->dst
));
1058 struct talitos_ptr
*link_tbl_ptr
=
1059 &edesc
->link_tbl
[edesc
->src_nents
+ 1];
1061 to_talitos_ptr(&desc
->ptr
[5], edesc
->dma_link_tbl
+
1062 (edesc
->src_nents
+ 1) *
1063 sizeof(struct talitos_ptr
));
1064 sg_count
= sg_to_link_tbl(areq
->dst
, sg_count
, cryptlen
,
1067 /* Add an entry to the link table for ICV data */
1068 link_tbl_ptr
+= sg_count
- 1;
1069 link_tbl_ptr
->j_extent
= 0;
1072 link_tbl_ptr
->j_extent
= DESC_PTR_LNKTBL_RETURN
;
1073 link_tbl_ptr
->len
= cpu_to_be16(authsize
);
1075 /* icv data follows link tables */
1076 to_talitos_ptr(link_tbl_ptr
, edesc
->dma_link_tbl
+
1077 (edesc
->src_nents
+ edesc
->dst_nents
+ 2) *
1078 sizeof(struct talitos_ptr
));
1079 desc
->ptr
[5].j_extent
|= DESC_PTR_LNKTBL_JUMP
;
1080 dma_sync_single_for_device(ctx
->dev
, edesc
->dma_link_tbl
,
1081 edesc
->dma_len
, DMA_BIDIRECTIONAL
);
1085 map_single_talitos_ptr(dev
, &desc
->ptr
[6], ivsize
, ctx
->iv
, 0,
1088 ret
= talitos_submit(dev
, desc
, callback
, areq
);
1089 if (ret
!= -EINPROGRESS
) {
1090 ipsec_esp_unmap(dev
, edesc
, areq
);
1097 * derive number of elements in scatterlist
1099 static int sg_count(struct scatterlist
*sg_list
, int nbytes
, int *chained
)
1101 struct scatterlist
*sg
= sg_list
;
1105 while (nbytes
> 0) {
1107 nbytes
-= sg
->length
;
1108 if (!sg_is_last(sg
) && (sg
+ 1)->length
== 0)
1110 sg
= scatterwalk_sg_next(sg
);
1117 * allocate and map the extended descriptor
1119 static struct talitos_edesc
*talitos_edesc_alloc(struct device
*dev
,
1120 struct scatterlist
*src
,
1121 struct scatterlist
*dst
,
1122 unsigned int cryptlen
,
1123 unsigned int authsize
,
1127 struct talitos_edesc
*edesc
;
1128 int src_nents
, dst_nents
, alloc_len
, dma_len
;
1129 int src_chained
, dst_chained
= 0;
1130 gfp_t flags
= cryptoflags
& CRYPTO_TFM_REQ_MAY_SLEEP
? GFP_KERNEL
:
1133 if (cryptlen
+ authsize
> TALITOS_MAX_DATA_LEN
) {
1134 dev_err(dev
, "length exceeds h/w max limit\n");
1135 return ERR_PTR(-EINVAL
);
1138 src_nents
= sg_count(src
, cryptlen
+ authsize
, &src_chained
);
1139 src_nents
= (src_nents
== 1) ? 0 : src_nents
;
1142 dst_nents
= src_nents
;
1144 dst_nents
= sg_count(dst
, cryptlen
+ authsize
, &dst_chained
);
1145 dst_nents
= (dst_nents
== 1) ? 0 : dst_nents
;
1149 * allocate space for base edesc plus the link tables,
1150 * allowing for two separate entries for ICV and generated ICV (+ 2),
1151 * and the ICV data itself
1153 alloc_len
= sizeof(struct talitos_edesc
);
1154 if (src_nents
|| dst_nents
) {
1155 dma_len
= (src_nents
+ dst_nents
+ 2) *
1156 sizeof(struct talitos_ptr
) + authsize
;
1157 alloc_len
+= dma_len
;
1160 alloc_len
+= icv_stashing
? authsize
: 0;
1163 edesc
= kmalloc(alloc_len
, GFP_DMA
| flags
);
1165 dev_err(dev
, "could not allocate edescriptor\n");
1166 return ERR_PTR(-ENOMEM
);
1169 edesc
->src_nents
= src_nents
;
1170 edesc
->dst_nents
= dst_nents
;
1171 edesc
->src_is_chained
= src_chained
;
1172 edesc
->dst_is_chained
= dst_chained
;
1173 edesc
->dma_len
= dma_len
;
1174 edesc
->dma_link_tbl
= dma_map_single(dev
, &edesc
->link_tbl
[0],
1175 edesc
->dma_len
, DMA_BIDIRECTIONAL
);
1180 static struct talitos_edesc
*aead_edesc_alloc(struct aead_request
*areq
,
1183 struct crypto_aead
*authenc
= crypto_aead_reqtfm(areq
);
1184 struct talitos_ctx
*ctx
= crypto_aead_ctx(authenc
);
1186 return talitos_edesc_alloc(ctx
->dev
, areq
->src
, areq
->dst
,
1187 areq
->cryptlen
, ctx
->authsize
, icv_stashing
,
1191 static int aead_encrypt(struct aead_request
*req
)
1193 struct crypto_aead
*authenc
= crypto_aead_reqtfm(req
);
1194 struct talitos_ctx
*ctx
= crypto_aead_ctx(authenc
);
1195 struct talitos_edesc
*edesc
;
1197 /* allocate extended descriptor */
1198 edesc
= aead_edesc_alloc(req
, 0);
1200 return PTR_ERR(edesc
);
1203 edesc
->desc
.hdr
= ctx
->desc_hdr_template
| DESC_HDR_MODE0_ENCRYPT
;
1205 return ipsec_esp(edesc
, req
, NULL
, 0, ipsec_esp_encrypt_done
);
1208 static int aead_decrypt(struct aead_request
*req
)
1210 struct crypto_aead
*authenc
= crypto_aead_reqtfm(req
);
1211 struct talitos_ctx
*ctx
= crypto_aead_ctx(authenc
);
1212 unsigned int authsize
= ctx
->authsize
;
1213 struct talitos_private
*priv
= dev_get_drvdata(ctx
->dev
);
1214 struct talitos_edesc
*edesc
;
1215 struct scatterlist
*sg
;
1218 req
->cryptlen
-= authsize
;
1220 /* allocate extended descriptor */
1221 edesc
= aead_edesc_alloc(req
, 1);
1223 return PTR_ERR(edesc
);
1225 if ((priv
->features
& TALITOS_FTR_HW_AUTH_CHECK
) &&
1226 ((!edesc
->src_nents
&& !edesc
->dst_nents
) ||
1227 priv
->features
& TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT
)) {
1229 /* decrypt and check the ICV */
1230 edesc
->desc
.hdr
= ctx
->desc_hdr_template
|
1231 DESC_HDR_DIR_INBOUND
|
1232 DESC_HDR_MODE1_MDEU_CICV
;
1234 /* reset integrity check result bits */
1235 edesc
->desc
.hdr_lo
= 0;
1237 return ipsec_esp(edesc
, req
, NULL
, 0,
1238 ipsec_esp_decrypt_hwauth_done
);
1242 /* Have to check the ICV with software */
1243 edesc
->desc
.hdr
= ctx
->desc_hdr_template
| DESC_HDR_DIR_INBOUND
;
1245 /* stash incoming ICV for later cmp with ICV generated by the h/w */
1247 icvdata
= &edesc
->link_tbl
[edesc
->src_nents
+
1248 edesc
->dst_nents
+ 2];
1250 icvdata
= &edesc
->link_tbl
[0];
1252 sg
= sg_last(req
->src
, edesc
->src_nents
? : 1);
1254 memcpy(icvdata
, (char *)sg_virt(sg
) + sg
->length
- ctx
->authsize
,
1257 return ipsec_esp(edesc
, req
, NULL
, 0, ipsec_esp_decrypt_swauth_done
);
1260 static int aead_givencrypt(struct aead_givcrypt_request
*req
)
1262 struct aead_request
*areq
= &req
->areq
;
1263 struct crypto_aead
*authenc
= crypto_aead_reqtfm(areq
);
1264 struct talitos_ctx
*ctx
= crypto_aead_ctx(authenc
);
1265 struct talitos_edesc
*edesc
;
1267 /* allocate extended descriptor */
1268 edesc
= aead_edesc_alloc(areq
, 0);
1270 return PTR_ERR(edesc
);
1273 edesc
->desc
.hdr
= ctx
->desc_hdr_template
| DESC_HDR_MODE0_ENCRYPT
;
1275 memcpy(req
->giv
, ctx
->iv
, crypto_aead_ivsize(authenc
));
1276 /* avoid consecutive packets going out with same IV */
1277 *(__be64
*)req
->giv
^= cpu_to_be64(req
->seq
);
1279 return ipsec_esp(edesc
, areq
, req
->giv
, req
->seq
,
1280 ipsec_esp_encrypt_done
);
1283 static int ablkcipher_setkey(struct crypto_ablkcipher
*cipher
,
1284 const u8
*key
, unsigned int keylen
)
1286 struct talitos_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
1287 struct ablkcipher_alg
*alg
= crypto_ablkcipher_alg(cipher
);
1289 if (keylen
> TALITOS_MAX_KEY_SIZE
)
1292 if (keylen
< alg
->min_keysize
|| keylen
> alg
->max_keysize
)
1295 memcpy(&ctx
->key
, key
, keylen
);
1296 ctx
->keylen
= keylen
;
1301 crypto_ablkcipher_set_flags(cipher
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
1305 static void common_nonsnoop_unmap(struct device
*dev
,
1306 struct talitos_edesc
*edesc
,
1307 struct ablkcipher_request
*areq
)
1309 unmap_single_talitos_ptr(dev
, &edesc
->desc
.ptr
[5], DMA_FROM_DEVICE
);
1310 unmap_single_talitos_ptr(dev
, &edesc
->desc
.ptr
[2], DMA_TO_DEVICE
);
1311 unmap_single_talitos_ptr(dev
, &edesc
->desc
.ptr
[1], DMA_TO_DEVICE
);
1313 talitos_sg_unmap(dev
, edesc
, areq
->src
, areq
->dst
);
1316 dma_unmap_single(dev
, edesc
->dma_link_tbl
, edesc
->dma_len
,
1320 static void ablkcipher_done(struct device
*dev
,
1321 struct talitos_desc
*desc
, void *context
,
1324 struct ablkcipher_request
*areq
= context
;
1325 struct talitos_edesc
*edesc
;
1327 edesc
= container_of(desc
, struct talitos_edesc
, desc
);
1329 common_nonsnoop_unmap(dev
, edesc
, areq
);
1333 areq
->base
.complete(&areq
->base
, err
);
1336 static int common_nonsnoop(struct talitos_edesc
*edesc
,
1337 struct ablkcipher_request
*areq
,
1339 void (*callback
) (struct device
*dev
,
1340 struct talitos_desc
*desc
,
1341 void *context
, int error
))
1343 struct crypto_ablkcipher
*cipher
= crypto_ablkcipher_reqtfm(areq
);
1344 struct talitos_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
1345 struct device
*dev
= ctx
->dev
;
1346 struct talitos_desc
*desc
= &edesc
->desc
;
1347 unsigned int cryptlen
= areq
->nbytes
;
1348 unsigned int ivsize
;
1351 /* first DWORD empty */
1352 desc
->ptr
[0].len
= 0;
1353 to_talitos_ptr(&desc
->ptr
[0], 0);
1354 desc
->ptr
[0].j_extent
= 0;
1357 ivsize
= crypto_ablkcipher_ivsize(cipher
);
1358 map_single_talitos_ptr(dev
, &desc
->ptr
[1], ivsize
, giv
?: areq
->info
, 0,
1362 map_single_talitos_ptr(dev
, &desc
->ptr
[2], ctx
->keylen
,
1363 (char *)&ctx
->key
, 0, DMA_TO_DEVICE
);
1368 desc
->ptr
[3].len
= cpu_to_be16(cryptlen
);
1369 desc
->ptr
[3].j_extent
= 0;
1371 sg_count
= talitos_map_sg(dev
, areq
->src
, edesc
->src_nents
? : 1,
1372 (areq
->src
== areq
->dst
) ? DMA_BIDIRECTIONAL
1374 edesc
->src_is_chained
);
1376 if (sg_count
== 1) {
1377 to_talitos_ptr(&desc
->ptr
[3], sg_dma_address(areq
->src
));
1379 sg_count
= sg_to_link_tbl(areq
->src
, sg_count
, cryptlen
,
1380 &edesc
->link_tbl
[0]);
1382 to_talitos_ptr(&desc
->ptr
[3], edesc
->dma_link_tbl
);
1383 desc
->ptr
[3].j_extent
|= DESC_PTR_LNKTBL_JUMP
;
1384 dma_sync_single_for_device(dev
, edesc
->dma_link_tbl
,
1388 /* Only one segment now, so no link tbl needed */
1389 to_talitos_ptr(&desc
->ptr
[3],
1390 sg_dma_address(areq
->src
));
1395 desc
->ptr
[4].len
= cpu_to_be16(cryptlen
);
1396 desc
->ptr
[4].j_extent
= 0;
1398 if (areq
->src
!= areq
->dst
)
1399 sg_count
= talitos_map_sg(dev
, areq
->dst
,
1400 edesc
->dst_nents
? : 1,
1402 edesc
->dst_is_chained
);
1404 if (sg_count
== 1) {
1405 to_talitos_ptr(&desc
->ptr
[4], sg_dma_address(areq
->dst
));
1407 struct talitos_ptr
*link_tbl_ptr
=
1408 &edesc
->link_tbl
[edesc
->src_nents
+ 1];
1410 to_talitos_ptr(&desc
->ptr
[4], edesc
->dma_link_tbl
+
1411 (edesc
->src_nents
+ 1) *
1412 sizeof(struct talitos_ptr
));
1413 desc
->ptr
[4].j_extent
|= DESC_PTR_LNKTBL_JUMP
;
1414 sg_count
= sg_to_link_tbl(areq
->dst
, sg_count
, cryptlen
,
1416 dma_sync_single_for_device(ctx
->dev
, edesc
->dma_link_tbl
,
1417 edesc
->dma_len
, DMA_BIDIRECTIONAL
);
1421 map_single_talitos_ptr(dev
, &desc
->ptr
[5], ivsize
, ctx
->iv
, 0,
1424 /* last DWORD empty */
1425 desc
->ptr
[6].len
= 0;
1426 to_talitos_ptr(&desc
->ptr
[6], 0);
1427 desc
->ptr
[6].j_extent
= 0;
1429 ret
= talitos_submit(dev
, desc
, callback
, areq
);
1430 if (ret
!= -EINPROGRESS
) {
1431 common_nonsnoop_unmap(dev
, edesc
, areq
);
1437 static struct talitos_edesc
*ablkcipher_edesc_alloc(struct ablkcipher_request
*
1440 struct crypto_ablkcipher
*cipher
= crypto_ablkcipher_reqtfm(areq
);
1441 struct talitos_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
1443 return talitos_edesc_alloc(ctx
->dev
, areq
->src
, areq
->dst
, areq
->nbytes
,
1444 0, 0, areq
->base
.flags
);
1447 static int ablkcipher_encrypt(struct ablkcipher_request
*areq
)
1449 struct crypto_ablkcipher
*cipher
= crypto_ablkcipher_reqtfm(areq
);
1450 struct talitos_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
1451 struct talitos_edesc
*edesc
;
1453 /* allocate extended descriptor */
1454 edesc
= ablkcipher_edesc_alloc(areq
);
1456 return PTR_ERR(edesc
);
1459 edesc
->desc
.hdr
= ctx
->desc_hdr_template
| DESC_HDR_MODE0_ENCRYPT
;
1461 return common_nonsnoop(edesc
, areq
, NULL
, ablkcipher_done
);
1464 static int ablkcipher_decrypt(struct ablkcipher_request
*areq
)
1466 struct crypto_ablkcipher
*cipher
= crypto_ablkcipher_reqtfm(areq
);
1467 struct talitos_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
1468 struct talitos_edesc
*edesc
;
1470 /* allocate extended descriptor */
1471 edesc
= ablkcipher_edesc_alloc(areq
);
1473 return PTR_ERR(edesc
);
1475 edesc
->desc
.hdr
= ctx
->desc_hdr_template
| DESC_HDR_DIR_INBOUND
;
1477 return common_nonsnoop(edesc
, areq
, NULL
, ablkcipher_done
);
1480 struct talitos_alg_template
{
1481 struct crypto_alg alg
;
1482 __be32 desc_hdr_template
;
1485 static struct talitos_alg_template driver_algs
[] = {
1486 /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */
1489 .cra_name
= "authenc(hmac(sha1),cbc(aes))",
1490 .cra_driver_name
= "authenc-hmac-sha1-cbc-aes-talitos",
1491 .cra_blocksize
= AES_BLOCK_SIZE
,
1492 .cra_flags
= CRYPTO_ALG_TYPE_AEAD
| CRYPTO_ALG_ASYNC
,
1493 .cra_type
= &crypto_aead_type
,
1495 .setkey
= aead_setkey
,
1496 .setauthsize
= aead_setauthsize
,
1497 .encrypt
= aead_encrypt
,
1498 .decrypt
= aead_decrypt
,
1499 .givencrypt
= aead_givencrypt
,
1500 .geniv
= "<built-in>",
1501 .ivsize
= AES_BLOCK_SIZE
,
1502 .maxauthsize
= SHA1_DIGEST_SIZE
,
1505 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
1506 DESC_HDR_SEL0_AESU
|
1507 DESC_HDR_MODE0_AESU_CBC
|
1508 DESC_HDR_SEL1_MDEUA
|
1509 DESC_HDR_MODE1_MDEU_INIT
|
1510 DESC_HDR_MODE1_MDEU_PAD
|
1511 DESC_HDR_MODE1_MDEU_SHA1_HMAC
,
1515 .cra_name
= "authenc(hmac(sha1),cbc(des3_ede))",
1516 .cra_driver_name
= "authenc-hmac-sha1-cbc-3des-talitos",
1517 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
1518 .cra_flags
= CRYPTO_ALG_TYPE_AEAD
| CRYPTO_ALG_ASYNC
,
1519 .cra_type
= &crypto_aead_type
,
1521 .setkey
= aead_setkey
,
1522 .setauthsize
= aead_setauthsize
,
1523 .encrypt
= aead_encrypt
,
1524 .decrypt
= aead_decrypt
,
1525 .givencrypt
= aead_givencrypt
,
1526 .geniv
= "<built-in>",
1527 .ivsize
= DES3_EDE_BLOCK_SIZE
,
1528 .maxauthsize
= SHA1_DIGEST_SIZE
,
1531 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
1533 DESC_HDR_MODE0_DEU_CBC
|
1534 DESC_HDR_MODE0_DEU_3DES
|
1535 DESC_HDR_SEL1_MDEUA
|
1536 DESC_HDR_MODE1_MDEU_INIT
|
1537 DESC_HDR_MODE1_MDEU_PAD
|
1538 DESC_HDR_MODE1_MDEU_SHA1_HMAC
,
1542 .cra_name
= "authenc(hmac(sha256),cbc(aes))",
1543 .cra_driver_name
= "authenc-hmac-sha256-cbc-aes-talitos",
1544 .cra_blocksize
= AES_BLOCK_SIZE
,
1545 .cra_flags
= CRYPTO_ALG_TYPE_AEAD
| CRYPTO_ALG_ASYNC
,
1546 .cra_type
= &crypto_aead_type
,
1548 .setkey
= aead_setkey
,
1549 .setauthsize
= aead_setauthsize
,
1550 .encrypt
= aead_encrypt
,
1551 .decrypt
= aead_decrypt
,
1552 .givencrypt
= aead_givencrypt
,
1553 .geniv
= "<built-in>",
1554 .ivsize
= AES_BLOCK_SIZE
,
1555 .maxauthsize
= SHA256_DIGEST_SIZE
,
1558 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
1559 DESC_HDR_SEL0_AESU
|
1560 DESC_HDR_MODE0_AESU_CBC
|
1561 DESC_HDR_SEL1_MDEUA
|
1562 DESC_HDR_MODE1_MDEU_INIT
|
1563 DESC_HDR_MODE1_MDEU_PAD
|
1564 DESC_HDR_MODE1_MDEU_SHA256_HMAC
,
1568 .cra_name
= "authenc(hmac(sha256),cbc(des3_ede))",
1569 .cra_driver_name
= "authenc-hmac-sha256-cbc-3des-talitos",
1570 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
1571 .cra_flags
= CRYPTO_ALG_TYPE_AEAD
| CRYPTO_ALG_ASYNC
,
1572 .cra_type
= &crypto_aead_type
,
1574 .setkey
= aead_setkey
,
1575 .setauthsize
= aead_setauthsize
,
1576 .encrypt
= aead_encrypt
,
1577 .decrypt
= aead_decrypt
,
1578 .givencrypt
= aead_givencrypt
,
1579 .geniv
= "<built-in>",
1580 .ivsize
= DES3_EDE_BLOCK_SIZE
,
1581 .maxauthsize
= SHA256_DIGEST_SIZE
,
1584 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
1586 DESC_HDR_MODE0_DEU_CBC
|
1587 DESC_HDR_MODE0_DEU_3DES
|
1588 DESC_HDR_SEL1_MDEUA
|
1589 DESC_HDR_MODE1_MDEU_INIT
|
1590 DESC_HDR_MODE1_MDEU_PAD
|
1591 DESC_HDR_MODE1_MDEU_SHA256_HMAC
,
1595 .cra_name
= "authenc(hmac(md5),cbc(aes))",
1596 .cra_driver_name
= "authenc-hmac-md5-cbc-aes-talitos",
1597 .cra_blocksize
= AES_BLOCK_SIZE
,
1598 .cra_flags
= CRYPTO_ALG_TYPE_AEAD
| CRYPTO_ALG_ASYNC
,
1599 .cra_type
= &crypto_aead_type
,
1601 .setkey
= aead_setkey
,
1602 .setauthsize
= aead_setauthsize
,
1603 .encrypt
= aead_encrypt
,
1604 .decrypt
= aead_decrypt
,
1605 .givencrypt
= aead_givencrypt
,
1606 .geniv
= "<built-in>",
1607 .ivsize
= AES_BLOCK_SIZE
,
1608 .maxauthsize
= MD5_DIGEST_SIZE
,
1611 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
1612 DESC_HDR_SEL0_AESU
|
1613 DESC_HDR_MODE0_AESU_CBC
|
1614 DESC_HDR_SEL1_MDEUA
|
1615 DESC_HDR_MODE1_MDEU_INIT
|
1616 DESC_HDR_MODE1_MDEU_PAD
|
1617 DESC_HDR_MODE1_MDEU_MD5_HMAC
,
1621 .cra_name
= "authenc(hmac(md5),cbc(des3_ede))",
1622 .cra_driver_name
= "authenc-hmac-md5-cbc-3des-talitos",
1623 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
1624 .cra_flags
= CRYPTO_ALG_TYPE_AEAD
| CRYPTO_ALG_ASYNC
,
1625 .cra_type
= &crypto_aead_type
,
1627 .setkey
= aead_setkey
,
1628 .setauthsize
= aead_setauthsize
,
1629 .encrypt
= aead_encrypt
,
1630 .decrypt
= aead_decrypt
,
1631 .givencrypt
= aead_givencrypt
,
1632 .geniv
= "<built-in>",
1633 .ivsize
= DES3_EDE_BLOCK_SIZE
,
1634 .maxauthsize
= MD5_DIGEST_SIZE
,
1637 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
1639 DESC_HDR_MODE0_DEU_CBC
|
1640 DESC_HDR_MODE0_DEU_3DES
|
1641 DESC_HDR_SEL1_MDEUA
|
1642 DESC_HDR_MODE1_MDEU_INIT
|
1643 DESC_HDR_MODE1_MDEU_PAD
|
1644 DESC_HDR_MODE1_MDEU_MD5_HMAC
,
1646 /* ABLKCIPHER algorithms. */
1649 .cra_name
= "cbc(aes)",
1650 .cra_driver_name
= "cbc-aes-talitos",
1651 .cra_blocksize
= AES_BLOCK_SIZE
,
1652 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1654 .cra_type
= &crypto_ablkcipher_type
,
1656 .setkey
= ablkcipher_setkey
,
1657 .encrypt
= ablkcipher_encrypt
,
1658 .decrypt
= ablkcipher_decrypt
,
1660 .min_keysize
= AES_MIN_KEY_SIZE
,
1661 .max_keysize
= AES_MAX_KEY_SIZE
,
1662 .ivsize
= AES_BLOCK_SIZE
,
1665 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
1666 DESC_HDR_SEL0_AESU
|
1667 DESC_HDR_MODE0_AESU_CBC
,
1671 .cra_name
= "cbc(des3_ede)",
1672 .cra_driver_name
= "cbc-3des-talitos",
1673 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
1674 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
1676 .cra_type
= &crypto_ablkcipher_type
,
1678 .setkey
= ablkcipher_setkey
,
1679 .encrypt
= ablkcipher_encrypt
,
1680 .decrypt
= ablkcipher_decrypt
,
1682 .min_keysize
= DES3_EDE_KEY_SIZE
,
1683 .max_keysize
= DES3_EDE_KEY_SIZE
,
1684 .ivsize
= DES3_EDE_BLOCK_SIZE
,
1687 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
1689 DESC_HDR_MODE0_DEU_CBC
|
1690 DESC_HDR_MODE0_DEU_3DES
,
1694 struct talitos_crypto_alg
{
1695 struct list_head entry
;
1697 __be32 desc_hdr_template
;
1698 struct crypto_alg crypto_alg
;
1701 static int talitos_cra_init(struct crypto_tfm
*tfm
)
1703 struct crypto_alg
*alg
= tfm
->__crt_alg
;
1704 struct talitos_crypto_alg
*talitos_alg
;
1705 struct talitos_ctx
*ctx
= crypto_tfm_ctx(tfm
);
1707 talitos_alg
= container_of(alg
, struct talitos_crypto_alg
, crypto_alg
);
1709 /* update context with ptr to dev */
1710 ctx
->dev
= talitos_alg
->dev
;
1712 /* copy descriptor header template value */
1713 ctx
->desc_hdr_template
= talitos_alg
->desc_hdr_template
;
1715 /* random first IV */
1716 get_random_bytes(ctx
->iv
, TALITOS_MAX_IV_LENGTH
);
1722 * given the alg's descriptor header template, determine whether descriptor
1723 * type and primary/secondary execution units required match the hw
1724 * capabilities description provided in the device tree node.
1726 static int hw_supports(struct device
*dev
, __be32 desc_hdr_template
)
1728 struct talitos_private
*priv
= dev_get_drvdata(dev
);
1731 ret
= (1 << DESC_TYPE(desc_hdr_template
) & priv
->desc_types
) &&
1732 (1 << PRIMARY_EU(desc_hdr_template
) & priv
->exec_units
);
1734 if (SECONDARY_EU(desc_hdr_template
))
1735 ret
= ret
&& (1 << SECONDARY_EU(desc_hdr_template
)
1736 & priv
->exec_units
);
1741 static int talitos_remove(struct of_device
*ofdev
)
1743 struct device
*dev
= &ofdev
->dev
;
1744 struct talitos_private
*priv
= dev_get_drvdata(dev
);
1745 struct talitos_crypto_alg
*t_alg
, *n
;
1748 list_for_each_entry_safe(t_alg
, n
, &priv
->alg_list
, entry
) {
1749 crypto_unregister_alg(&t_alg
->crypto_alg
);
1750 list_del(&t_alg
->entry
);
1754 if (hw_supports(dev
, DESC_HDR_SEL0_RNG
))
1755 talitos_unregister_rng(dev
);
1757 for (i
= 0; i
< priv
->num_channels
; i
++)
1758 if (priv
->chan
[i
].fifo
)
1759 kfree(priv
->chan
[i
].fifo
);
1763 if (priv
->irq
!= NO_IRQ
) {
1764 free_irq(priv
->irq
, dev
);
1765 irq_dispose_mapping(priv
->irq
);
1768 tasklet_kill(&priv
->done_task
);
1772 dev_set_drvdata(dev
, NULL
);
1779 static struct talitos_crypto_alg
*talitos_alg_alloc(struct device
*dev
,
1780 struct talitos_alg_template
1783 struct talitos_crypto_alg
*t_alg
;
1784 struct crypto_alg
*alg
;
1786 t_alg
= kzalloc(sizeof(struct talitos_crypto_alg
), GFP_KERNEL
);
1788 return ERR_PTR(-ENOMEM
);
1790 alg
= &t_alg
->crypto_alg
;
1791 *alg
= template->alg
;
1793 alg
->cra_module
= THIS_MODULE
;
1794 alg
->cra_init
= talitos_cra_init
;
1795 alg
->cra_priority
= TALITOS_CRA_PRIORITY
;
1796 alg
->cra_alignmask
= 0;
1797 alg
->cra_ctxsize
= sizeof(struct talitos_ctx
);
1799 t_alg
->desc_hdr_template
= template->desc_hdr_template
;
1805 static int talitos_probe(struct of_device
*ofdev
,
1806 const struct of_device_id
*match
)
1808 struct device
*dev
= &ofdev
->dev
;
1809 struct device_node
*np
= ofdev
->node
;
1810 struct talitos_private
*priv
;
1811 const unsigned int *prop
;
1814 priv
= kzalloc(sizeof(struct talitos_private
), GFP_KERNEL
);
1818 dev_set_drvdata(dev
, priv
);
1820 priv
->ofdev
= ofdev
;
1822 tasklet_init(&priv
->done_task
, talitos_done
, (unsigned long)dev
);
1824 INIT_LIST_HEAD(&priv
->alg_list
);
1826 priv
->irq
= irq_of_parse_and_map(np
, 0);
1828 if (priv
->irq
== NO_IRQ
) {
1829 dev_err(dev
, "failed to map irq\n");
1834 /* get the irq line */
1835 err
= request_irq(priv
->irq
, talitos_interrupt
, 0,
1836 dev_driver_string(dev
), dev
);
1838 dev_err(dev
, "failed to request irq %d\n", priv
->irq
);
1839 irq_dispose_mapping(priv
->irq
);
1844 priv
->reg
= of_iomap(np
, 0);
1846 dev_err(dev
, "failed to of_iomap\n");
1851 /* get SEC version capabilities from device tree */
1852 prop
= of_get_property(np
, "fsl,num-channels", NULL
);
1854 priv
->num_channels
= *prop
;
1856 prop
= of_get_property(np
, "fsl,channel-fifo-len", NULL
);
1858 priv
->chfifo_len
= *prop
;
1860 prop
= of_get_property(np
, "fsl,exec-units-mask", NULL
);
1862 priv
->exec_units
= *prop
;
1864 prop
= of_get_property(np
, "fsl,descriptor-types-mask", NULL
);
1866 priv
->desc_types
= *prop
;
1868 if (!is_power_of_2(priv
->num_channels
) || !priv
->chfifo_len
||
1869 !priv
->exec_units
|| !priv
->desc_types
) {
1870 dev_err(dev
, "invalid property data in device tree node\n");
1875 if (of_device_is_compatible(np
, "fsl,sec3.0"))
1876 priv
->features
|= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT
;
1878 if (of_device_is_compatible(np
, "fsl,sec2.1"))
1879 priv
->features
|= TALITOS_FTR_HW_AUTH_CHECK
;
1881 priv
->chan
= kzalloc(sizeof(struct talitos_channel
) *
1882 priv
->num_channels
, GFP_KERNEL
);
1884 dev_err(dev
, "failed to allocate channel management space\n");
1889 for (i
= 0; i
< priv
->num_channels
; i
++) {
1890 spin_lock_init(&priv
->chan
[i
].head_lock
);
1891 spin_lock_init(&priv
->chan
[i
].tail_lock
);
1894 priv
->fifo_len
= roundup_pow_of_two(priv
->chfifo_len
);
1896 for (i
= 0; i
< priv
->num_channels
; i
++) {
1897 priv
->chan
[i
].fifo
= kzalloc(sizeof(struct talitos_request
) *
1898 priv
->fifo_len
, GFP_KERNEL
);
1899 if (!priv
->chan
[i
].fifo
) {
1900 dev_err(dev
, "failed to allocate request fifo %d\n", i
);
1906 for (i
= 0; i
< priv
->num_channels
; i
++)
1907 atomic_set(&priv
->chan
[i
].submit_count
,
1908 -(priv
->chfifo_len
- 1));
1910 dma_set_mask(dev
, DMA_BIT_MASK(36));
1912 /* reset and initialize the h/w */
1913 err
= init_device(dev
);
1915 dev_err(dev
, "failed to initialize device\n");
1919 /* register the RNG, if available */
1920 if (hw_supports(dev
, DESC_HDR_SEL0_RNG
)) {
1921 err
= talitos_register_rng(dev
);
1923 dev_err(dev
, "failed to register hwrng: %d\n", err
);
1926 dev_info(dev
, "hwrng\n");
1929 /* register crypto algorithms the device supports */
1930 for (i
= 0; i
< ARRAY_SIZE(driver_algs
); i
++) {
1931 if (hw_supports(dev
, driver_algs
[i
].desc_hdr_template
)) {
1932 struct talitos_crypto_alg
*t_alg
;
1934 t_alg
= talitos_alg_alloc(dev
, &driver_algs
[i
]);
1935 if (IS_ERR(t_alg
)) {
1936 err
= PTR_ERR(t_alg
);
1940 err
= crypto_register_alg(&t_alg
->crypto_alg
);
1942 dev_err(dev
, "%s alg registration failed\n",
1943 t_alg
->crypto_alg
.cra_driver_name
);
1946 list_add_tail(&t_alg
->entry
, &priv
->alg_list
);
1947 dev_info(dev
, "%s\n",
1948 t_alg
->crypto_alg
.cra_driver_name
);
1956 talitos_remove(ofdev
);
1961 static struct of_device_id talitos_match
[] = {
1963 .compatible
= "fsl,sec2.0",
1967 MODULE_DEVICE_TABLE(of
, talitos_match
);
1969 static struct of_platform_driver talitos_driver
= {
1971 .match_table
= talitos_match
,
1972 .probe
= talitos_probe
,
1973 .remove
= talitos_remove
,
1976 static int __init
talitos_init(void)
1978 return of_register_platform_driver(&talitos_driver
);
1980 module_init(talitos_init
);
1982 static void __exit
talitos_exit(void)
1984 of_unregister_platform_driver(&talitos_driver
);
1986 module_exit(talitos_exit
);
1988 MODULE_LICENSE("GPL");
1989 MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
1990 MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");