2 * talitos - Freescale Integrated Security Engine (SEC) device driver
4 * Copyright (c) 2008-2011 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_address.h>
36 #include <linux/of_irq.h>
37 #include <linux/of_platform.h>
38 #include <linux/dma-mapping.h>
40 #include <linux/spinlock.h>
41 #include <linux/rtnetlink.h>
42 #include <linux/slab.h>
44 #include <crypto/algapi.h>
45 #include <crypto/aes.h>
46 #include <crypto/des.h>
47 #include <crypto/sha.h>
48 #include <crypto/md5.h>
49 #include <crypto/internal/aead.h>
50 #include <crypto/authenc.h>
51 #include <crypto/skcipher.h>
52 #include <crypto/hash.h>
53 #include <crypto/internal/hash.h>
54 #include <crypto/scatterwalk.h>
58 static void to_talitos_ptr(struct talitos_ptr
*ptr
, dma_addr_t dma_addr
,
59 unsigned int len
, bool is_sec1
)
61 ptr
->ptr
= cpu_to_be32(lower_32_bits(dma_addr
));
63 ptr
->len1
= cpu_to_be16(len
);
65 ptr
->len
= cpu_to_be16(len
);
66 ptr
->eptr
= upper_32_bits(dma_addr
);
70 static void copy_talitos_ptr(struct talitos_ptr
*dst_ptr
,
71 struct talitos_ptr
*src_ptr
, bool is_sec1
)
73 dst_ptr
->ptr
= src_ptr
->ptr
;
75 dst_ptr
->len1
= src_ptr
->len1
;
77 dst_ptr
->len
= src_ptr
->len
;
78 dst_ptr
->eptr
= src_ptr
->eptr
;
82 static unsigned short from_talitos_ptr_len(struct talitos_ptr
*ptr
,
86 return be16_to_cpu(ptr
->len1
);
88 return be16_to_cpu(ptr
->len
);
91 static void to_talitos_ptr_ext_set(struct talitos_ptr
*ptr
, u8 val
,
98 static void to_talitos_ptr_ext_or(struct talitos_ptr
*ptr
, u8 val
, bool is_sec1
)
101 ptr
->j_extent
|= val
;
105 * map virtual single (contiguous) pointer to h/w descriptor pointer
107 static void map_single_talitos_ptr(struct device
*dev
,
108 struct talitos_ptr
*ptr
,
109 unsigned int len
, void *data
,
110 enum dma_data_direction dir
)
112 dma_addr_t dma_addr
= dma_map_single(dev
, data
, len
, dir
);
113 struct talitos_private
*priv
= dev_get_drvdata(dev
);
114 bool is_sec1
= has_ftr_sec1(priv
);
116 to_talitos_ptr(ptr
, dma_addr
, len
, is_sec1
);
120 * unmap bus single (contiguous) h/w descriptor pointer
122 static void unmap_single_talitos_ptr(struct device
*dev
,
123 struct talitos_ptr
*ptr
,
124 enum dma_data_direction dir
)
126 struct talitos_private
*priv
= dev_get_drvdata(dev
);
127 bool is_sec1
= has_ftr_sec1(priv
);
129 dma_unmap_single(dev
, be32_to_cpu(ptr
->ptr
),
130 from_talitos_ptr_len(ptr
, is_sec1
), dir
);
133 static int reset_channel(struct device
*dev
, int ch
)
135 struct talitos_private
*priv
= dev_get_drvdata(dev
);
136 unsigned int timeout
= TALITOS_TIMEOUT
;
137 bool is_sec1
= has_ftr_sec1(priv
);
140 setbits32(priv
->chan
[ch
].reg
+ TALITOS_CCCR_LO
,
141 TALITOS1_CCCR_LO_RESET
);
143 while ((in_be32(priv
->chan
[ch
].reg
+ TALITOS_CCCR_LO
) &
144 TALITOS1_CCCR_LO_RESET
) && --timeout
)
147 setbits32(priv
->chan
[ch
].reg
+ TALITOS_CCCR
,
148 TALITOS2_CCCR_RESET
);
150 while ((in_be32(priv
->chan
[ch
].reg
+ TALITOS_CCCR
) &
151 TALITOS2_CCCR_RESET
) && --timeout
)
156 dev_err(dev
, "failed to reset channel %d\n", ch
);
160 /* set 36-bit addressing, done writeback enable and done IRQ enable */
161 setbits32(priv
->chan
[ch
].reg
+ TALITOS_CCCR_LO
, TALITOS_CCCR_LO_EAE
|
162 TALITOS_CCCR_LO_CDWE
| TALITOS_CCCR_LO_CDIE
);
163 /* enable chaining descriptors */
165 setbits32(priv
->chan
[ch
].reg
+ TALITOS_CCCR_LO
,
168 /* and ICCR writeback, if available */
169 if (priv
->features
& TALITOS_FTR_HW_AUTH_CHECK
)
170 setbits32(priv
->chan
[ch
].reg
+ TALITOS_CCCR_LO
,
171 TALITOS_CCCR_LO_IWSE
);
176 static int reset_device(struct device
*dev
)
178 struct talitos_private
*priv
= dev_get_drvdata(dev
);
179 unsigned int timeout
= TALITOS_TIMEOUT
;
180 bool is_sec1
= has_ftr_sec1(priv
);
181 u32 mcr
= is_sec1
? TALITOS1_MCR_SWR
: TALITOS2_MCR_SWR
;
183 setbits32(priv
->reg
+ TALITOS_MCR
, mcr
);
185 while ((in_be32(priv
->reg
+ TALITOS_MCR
) & mcr
)
190 mcr
= TALITOS_MCR_RCA1
| TALITOS_MCR_RCA3
;
191 setbits32(priv
->reg
+ TALITOS_MCR
, mcr
);
195 dev_err(dev
, "failed to reset device\n");
203 * Reset and initialize the device
205 static int init_device(struct device
*dev
)
207 struct talitos_private
*priv
= dev_get_drvdata(dev
);
209 bool is_sec1
= has_ftr_sec1(priv
);
213 * errata documentation: warning: certain SEC interrupts
214 * are not fully cleared by writing the MCR:SWR bit,
215 * set bit twice to completely reset
217 err
= reset_device(dev
);
221 err
= reset_device(dev
);
226 for (ch
= 0; ch
< priv
->num_channels
; ch
++) {
227 err
= reset_channel(dev
, ch
);
232 /* enable channel done and error interrupts */
234 clrbits32(priv
->reg
+ TALITOS_IMR
, TALITOS1_IMR_INIT
);
235 clrbits32(priv
->reg
+ TALITOS_IMR_LO
, TALITOS1_IMR_LO_INIT
);
236 /* disable parity error check in DEU (erroneous? test vect.) */
237 setbits32(priv
->reg_deu
+ TALITOS_EUICR
, TALITOS1_DEUICR_KPE
);
239 setbits32(priv
->reg
+ TALITOS_IMR
, TALITOS2_IMR_INIT
);
240 setbits32(priv
->reg
+ TALITOS_IMR_LO
, TALITOS2_IMR_LO_INIT
);
243 /* disable integrity check error interrupts (use writeback instead) */
244 if (priv
->features
& TALITOS_FTR_HW_AUTH_CHECK
)
245 setbits32(priv
->reg_mdeu
+ TALITOS_EUICR_LO
,
246 TALITOS_MDEUICR_LO_ICE
);
252 * talitos_submit - submits a descriptor to the device for processing
253 * @dev: the SEC device to be used
254 * @ch: the SEC device channel to be used
255 * @desc: the descriptor to be processed by the device
256 * @callback: whom to call when processing is complete
257 * @context: a handle for use by caller (optional)
259 * desc must contain valid dma-mapped (bus physical) address pointers.
260 * callback must check err and feedback in descriptor header
261 * for device processing status.
263 int talitos_submit(struct device
*dev
, int ch
, struct talitos_desc
*desc
,
264 void (*callback
)(struct device
*dev
,
265 struct talitos_desc
*desc
,
266 void *context
, int error
),
269 struct talitos_private
*priv
= dev_get_drvdata(dev
);
270 struct talitos_request
*request
;
273 bool is_sec1
= has_ftr_sec1(priv
);
275 spin_lock_irqsave(&priv
->chan
[ch
].head_lock
, flags
);
277 if (!atomic_inc_not_zero(&priv
->chan
[ch
].submit_count
)) {
278 /* h/w fifo is full */
279 spin_unlock_irqrestore(&priv
->chan
[ch
].head_lock
, flags
);
283 head
= priv
->chan
[ch
].head
;
284 request
= &priv
->chan
[ch
].fifo
[head
];
286 /* map descriptor and save caller data */
288 desc
->hdr1
= desc
->hdr
;
289 request
->dma_desc
= dma_map_single(dev
, &desc
->hdr1
,
293 request
->dma_desc
= dma_map_single(dev
, desc
,
297 request
->callback
= callback
;
298 request
->context
= context
;
300 /* increment fifo head */
301 priv
->chan
[ch
].head
= (priv
->chan
[ch
].head
+ 1) & (priv
->fifo_len
- 1);
304 request
->desc
= desc
;
308 out_be32(priv
->chan
[ch
].reg
+ TALITOS_FF
,
309 upper_32_bits(request
->dma_desc
));
310 out_be32(priv
->chan
[ch
].reg
+ TALITOS_FF_LO
,
311 lower_32_bits(request
->dma_desc
));
313 spin_unlock_irqrestore(&priv
->chan
[ch
].head_lock
, flags
);
317 EXPORT_SYMBOL(talitos_submit
);
320 * process what was done, notify callback of error if not
322 static void flush_channel(struct device
*dev
, int ch
, int error
, int reset_ch
)
324 struct talitos_private
*priv
= dev_get_drvdata(dev
);
325 struct talitos_request
*request
, saved_req
;
328 bool is_sec1
= has_ftr_sec1(priv
);
330 spin_lock_irqsave(&priv
->chan
[ch
].tail_lock
, flags
);
332 tail
= priv
->chan
[ch
].tail
;
333 while (priv
->chan
[ch
].fifo
[tail
].desc
) {
336 request
= &priv
->chan
[ch
].fifo
[tail
];
338 /* descriptors with their done bits set don't get the error */
341 hdr
= request
->desc
->hdr
;
342 else if (request
->desc
->next_desc
)
343 hdr
= (request
->desc
+ 1)->hdr1
;
345 hdr
= request
->desc
->hdr1
;
347 if ((hdr
& DESC_HDR_DONE
) == DESC_HDR_DONE
)
355 dma_unmap_single(dev
, request
->dma_desc
,
359 /* copy entries so we can call callback outside lock */
360 saved_req
.desc
= request
->desc
;
361 saved_req
.callback
= request
->callback
;
362 saved_req
.context
= request
->context
;
364 /* release request entry in fifo */
366 request
->desc
= NULL
;
368 /* increment fifo tail */
369 priv
->chan
[ch
].tail
= (tail
+ 1) & (priv
->fifo_len
- 1);
371 spin_unlock_irqrestore(&priv
->chan
[ch
].tail_lock
, flags
);
373 atomic_dec(&priv
->chan
[ch
].submit_count
);
375 saved_req
.callback(dev
, saved_req
.desc
, saved_req
.context
,
377 /* channel may resume processing in single desc error case */
378 if (error
&& !reset_ch
&& status
== error
)
380 spin_lock_irqsave(&priv
->chan
[ch
].tail_lock
, flags
);
381 tail
= priv
->chan
[ch
].tail
;
384 spin_unlock_irqrestore(&priv
->chan
[ch
].tail_lock
, flags
);
388 * process completed requests for channels that have done status
390 #define DEF_TALITOS1_DONE(name, ch_done_mask) \
391 static void talitos1_done_##name(unsigned long data) \
393 struct device *dev = (struct device *)data; \
394 struct talitos_private *priv = dev_get_drvdata(dev); \
395 unsigned long flags; \
397 if (ch_done_mask & 0x10000000) \
398 flush_channel(dev, 0, 0, 0); \
399 if (ch_done_mask & 0x40000000) \
400 flush_channel(dev, 1, 0, 0); \
401 if (ch_done_mask & 0x00010000) \
402 flush_channel(dev, 2, 0, 0); \
403 if (ch_done_mask & 0x00040000) \
404 flush_channel(dev, 3, 0, 0); \
406 /* At this point, all completed channels have been processed */ \
407 /* Unmask done interrupts for channels completed later on. */ \
408 spin_lock_irqsave(&priv->reg_lock, flags); \
409 clrbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
410 clrbits32(priv->reg + TALITOS_IMR_LO, TALITOS1_IMR_LO_INIT); \
411 spin_unlock_irqrestore(&priv->reg_lock, flags); \
414 DEF_TALITOS1_DONE(4ch
, TALITOS1_ISR_4CHDONE
)
415 DEF_TALITOS1_DONE(ch0
, TALITOS1_ISR_CH_0_DONE
)
417 #define DEF_TALITOS2_DONE(name, ch_done_mask) \
418 static void talitos2_done_##name(unsigned long data) \
420 struct device *dev = (struct device *)data; \
421 struct talitos_private *priv = dev_get_drvdata(dev); \
422 unsigned long flags; \
424 if (ch_done_mask & 1) \
425 flush_channel(dev, 0, 0, 0); \
426 if (ch_done_mask & (1 << 2)) \
427 flush_channel(dev, 1, 0, 0); \
428 if (ch_done_mask & (1 << 4)) \
429 flush_channel(dev, 2, 0, 0); \
430 if (ch_done_mask & (1 << 6)) \
431 flush_channel(dev, 3, 0, 0); \
433 /* At this point, all completed channels have been processed */ \
434 /* Unmask done interrupts for channels completed later on. */ \
435 spin_lock_irqsave(&priv->reg_lock, flags); \
436 setbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
437 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS2_IMR_LO_INIT); \
438 spin_unlock_irqrestore(&priv->reg_lock, flags); \
441 DEF_TALITOS2_DONE(4ch
, TALITOS2_ISR_4CHDONE
)
442 DEF_TALITOS2_DONE(ch0
, TALITOS2_ISR_CH_0_DONE
)
443 DEF_TALITOS2_DONE(ch0_2
, TALITOS2_ISR_CH_0_2_DONE
)
444 DEF_TALITOS2_DONE(ch1_3
, TALITOS2_ISR_CH_1_3_DONE
)
447 * locate current (offending) descriptor
449 static u32
current_desc_hdr(struct device
*dev
, int ch
)
451 struct talitos_private
*priv
= dev_get_drvdata(dev
);
455 cur_desc
= ((u64
)in_be32(priv
->chan
[ch
].reg
+ TALITOS_CDPR
)) << 32;
456 cur_desc
|= in_be32(priv
->chan
[ch
].reg
+ TALITOS_CDPR_LO
);
459 dev_err(dev
, "CDPR is NULL, giving up search for offending descriptor\n");
463 tail
= priv
->chan
[ch
].tail
;
466 while (priv
->chan
[ch
].fifo
[iter
].dma_desc
!= cur_desc
&&
467 priv
->chan
[ch
].fifo
[iter
].desc
->next_desc
!= cur_desc
) {
468 iter
= (iter
+ 1) & (priv
->fifo_len
- 1);
470 dev_err(dev
, "couldn't locate current descriptor\n");
475 if (priv
->chan
[ch
].fifo
[iter
].desc
->next_desc
== cur_desc
)
476 return (priv
->chan
[ch
].fifo
[iter
].desc
+ 1)->hdr
;
478 return priv
->chan
[ch
].fifo
[iter
].desc
->hdr
;
482 * user diagnostics; report root cause of error based on execution unit status
484 static void report_eu_error(struct device
*dev
, int ch
, u32 desc_hdr
)
486 struct talitos_private
*priv
= dev_get_drvdata(dev
);
490 desc_hdr
= in_be32(priv
->chan
[ch
].reg
+ TALITOS_DESCBUF
);
492 switch (desc_hdr
& DESC_HDR_SEL0_MASK
) {
493 case DESC_HDR_SEL0_AFEU
:
494 dev_err(dev
, "AFEUISR 0x%08x_%08x\n",
495 in_be32(priv
->reg_afeu
+ TALITOS_EUISR
),
496 in_be32(priv
->reg_afeu
+ TALITOS_EUISR_LO
));
498 case DESC_HDR_SEL0_DEU
:
499 dev_err(dev
, "DEUISR 0x%08x_%08x\n",
500 in_be32(priv
->reg_deu
+ TALITOS_EUISR
),
501 in_be32(priv
->reg_deu
+ TALITOS_EUISR_LO
));
503 case DESC_HDR_SEL0_MDEUA
:
504 case DESC_HDR_SEL0_MDEUB
:
505 dev_err(dev
, "MDEUISR 0x%08x_%08x\n",
506 in_be32(priv
->reg_mdeu
+ TALITOS_EUISR
),
507 in_be32(priv
->reg_mdeu
+ TALITOS_EUISR_LO
));
509 case DESC_HDR_SEL0_RNG
:
510 dev_err(dev
, "RNGUISR 0x%08x_%08x\n",
511 in_be32(priv
->reg_rngu
+ TALITOS_ISR
),
512 in_be32(priv
->reg_rngu
+ TALITOS_ISR_LO
));
514 case DESC_HDR_SEL0_PKEU
:
515 dev_err(dev
, "PKEUISR 0x%08x_%08x\n",
516 in_be32(priv
->reg_pkeu
+ TALITOS_EUISR
),
517 in_be32(priv
->reg_pkeu
+ TALITOS_EUISR_LO
));
519 case DESC_HDR_SEL0_AESU
:
520 dev_err(dev
, "AESUISR 0x%08x_%08x\n",
521 in_be32(priv
->reg_aesu
+ TALITOS_EUISR
),
522 in_be32(priv
->reg_aesu
+ TALITOS_EUISR_LO
));
524 case DESC_HDR_SEL0_CRCU
:
525 dev_err(dev
, "CRCUISR 0x%08x_%08x\n",
526 in_be32(priv
->reg_crcu
+ TALITOS_EUISR
),
527 in_be32(priv
->reg_crcu
+ TALITOS_EUISR_LO
));
529 case DESC_HDR_SEL0_KEU
:
530 dev_err(dev
, "KEUISR 0x%08x_%08x\n",
531 in_be32(priv
->reg_pkeu
+ TALITOS_EUISR
),
532 in_be32(priv
->reg_pkeu
+ TALITOS_EUISR_LO
));
536 switch (desc_hdr
& DESC_HDR_SEL1_MASK
) {
537 case DESC_HDR_SEL1_MDEUA
:
538 case DESC_HDR_SEL1_MDEUB
:
539 dev_err(dev
, "MDEUISR 0x%08x_%08x\n",
540 in_be32(priv
->reg_mdeu
+ TALITOS_EUISR
),
541 in_be32(priv
->reg_mdeu
+ TALITOS_EUISR_LO
));
543 case DESC_HDR_SEL1_CRCU
:
544 dev_err(dev
, "CRCUISR 0x%08x_%08x\n",
545 in_be32(priv
->reg_crcu
+ TALITOS_EUISR
),
546 in_be32(priv
->reg_crcu
+ TALITOS_EUISR_LO
));
550 for (i
= 0; i
< 8; i
++)
551 dev_err(dev
, "DESCBUF 0x%08x_%08x\n",
552 in_be32(priv
->chan
[ch
].reg
+ TALITOS_DESCBUF
+ 8*i
),
553 in_be32(priv
->chan
[ch
].reg
+ TALITOS_DESCBUF_LO
+ 8*i
));
557 * recover from error interrupts
559 static void talitos_error(struct device
*dev
, u32 isr
, u32 isr_lo
)
561 struct talitos_private
*priv
= dev_get_drvdata(dev
);
562 unsigned int timeout
= TALITOS_TIMEOUT
;
563 int ch
, error
, reset_dev
= 0;
565 bool is_sec1
= has_ftr_sec1(priv
);
566 int reset_ch
= is_sec1
? 1 : 0; /* only SEC2 supports continuation */
568 for (ch
= 0; ch
< priv
->num_channels
; ch
++) {
569 /* skip channels without errors */
571 /* bits 29, 31, 17, 19 */
572 if (!(isr
& (1 << (29 + (ch
& 1) * 2 - (ch
& 2) * 6))))
575 if (!(isr
& (1 << (ch
* 2 + 1))))
581 v_lo
= in_be32(priv
->chan
[ch
].reg
+ TALITOS_CCPSR_LO
);
583 if (v_lo
& TALITOS_CCPSR_LO_DOF
) {
584 dev_err(dev
, "double fetch fifo overflow error\n");
588 if (v_lo
& TALITOS_CCPSR_LO_SOF
) {
589 /* h/w dropped descriptor */
590 dev_err(dev
, "single fetch fifo overflow error\n");
593 if (v_lo
& TALITOS_CCPSR_LO_MDTE
)
594 dev_err(dev
, "master data transfer error\n");
595 if (v_lo
& TALITOS_CCPSR_LO_SGDLZ
)
596 dev_err(dev
, is_sec1
? "pointer not complete error\n"
597 : "s/g data length zero error\n");
598 if (v_lo
& TALITOS_CCPSR_LO_FPZ
)
599 dev_err(dev
, is_sec1
? "parity error\n"
600 : "fetch pointer zero error\n");
601 if (v_lo
& TALITOS_CCPSR_LO_IDH
)
602 dev_err(dev
, "illegal descriptor header error\n");
603 if (v_lo
& TALITOS_CCPSR_LO_IEU
)
604 dev_err(dev
, is_sec1
? "static assignment error\n"
605 : "invalid exec unit error\n");
606 if (v_lo
& TALITOS_CCPSR_LO_EU
)
607 report_eu_error(dev
, ch
, current_desc_hdr(dev
, ch
));
609 if (v_lo
& TALITOS_CCPSR_LO_GB
)
610 dev_err(dev
, "gather boundary error\n");
611 if (v_lo
& TALITOS_CCPSR_LO_GRL
)
612 dev_err(dev
, "gather return/length error\n");
613 if (v_lo
& TALITOS_CCPSR_LO_SB
)
614 dev_err(dev
, "scatter boundary error\n");
615 if (v_lo
& TALITOS_CCPSR_LO_SRL
)
616 dev_err(dev
, "scatter return/length error\n");
619 flush_channel(dev
, ch
, error
, reset_ch
);
622 reset_channel(dev
, ch
);
624 setbits32(priv
->chan
[ch
].reg
+ TALITOS_CCCR
,
626 setbits32(priv
->chan
[ch
].reg
+ TALITOS_CCCR_LO
, 0);
627 while ((in_be32(priv
->chan
[ch
].reg
+ TALITOS_CCCR
) &
628 TALITOS2_CCCR_CONT
) && --timeout
)
631 dev_err(dev
, "failed to restart channel %d\n",
637 if (reset_dev
|| (is_sec1
&& isr
& ~TALITOS1_ISR_4CHERR
) ||
638 (!is_sec1
&& isr
& ~TALITOS2_ISR_4CHERR
) || isr_lo
) {
639 if (is_sec1
&& (isr_lo
& TALITOS1_ISR_TEA_ERR
))
640 dev_err(dev
, "TEA error: ISR 0x%08x_%08x\n",
643 dev_err(dev
, "done overflow, internal time out, or "
644 "rngu error: ISR 0x%08x_%08x\n", isr
, isr_lo
);
646 /* purge request queues */
647 for (ch
= 0; ch
< priv
->num_channels
; ch
++)
648 flush_channel(dev
, ch
, -EIO
, 1);
650 /* reset and reinitialize the device */
655 #define DEF_TALITOS1_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet) \
656 static irqreturn_t talitos1_interrupt_##name(int irq, void *data) \
658 struct device *dev = data; \
659 struct talitos_private *priv = dev_get_drvdata(dev); \
661 unsigned long flags; \
663 spin_lock_irqsave(&priv->reg_lock, flags); \
664 isr = in_be32(priv->reg + TALITOS_ISR); \
665 isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); \
666 /* Acknowledge interrupt */ \
667 out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
668 out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); \
670 if (unlikely(isr & ch_err_mask || isr_lo & TALITOS1_IMR_LO_INIT)) { \
671 spin_unlock_irqrestore(&priv->reg_lock, flags); \
672 talitos_error(dev, isr & ch_err_mask, isr_lo); \
675 if (likely(isr & ch_done_mask)) { \
676 /* mask further done interrupts. */ \
677 setbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
678 /* done_task will unmask done interrupts at exit */ \
679 tasklet_schedule(&priv->done_task[tlet]); \
681 spin_unlock_irqrestore(&priv->reg_lock, flags); \
684 return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED : \
688 DEF_TALITOS1_INTERRUPT(4ch
, TALITOS1_ISR_4CHDONE
, TALITOS1_ISR_4CHERR
, 0)
690 #define DEF_TALITOS2_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet) \
691 static irqreturn_t talitos2_interrupt_##name(int irq, void *data) \
693 struct device *dev = data; \
694 struct talitos_private *priv = dev_get_drvdata(dev); \
696 unsigned long flags; \
698 spin_lock_irqsave(&priv->reg_lock, flags); \
699 isr = in_be32(priv->reg + TALITOS_ISR); \
700 isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); \
701 /* Acknowledge interrupt */ \
702 out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
703 out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); \
705 if (unlikely(isr & ch_err_mask || isr_lo)) { \
706 spin_unlock_irqrestore(&priv->reg_lock, flags); \
707 talitos_error(dev, isr & ch_err_mask, isr_lo); \
710 if (likely(isr & ch_done_mask)) { \
711 /* mask further done interrupts. */ \
712 clrbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
713 /* done_task will unmask done interrupts at exit */ \
714 tasklet_schedule(&priv->done_task[tlet]); \
716 spin_unlock_irqrestore(&priv->reg_lock, flags); \
719 return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED : \
723 DEF_TALITOS2_INTERRUPT(4ch
, TALITOS2_ISR_4CHDONE
, TALITOS2_ISR_4CHERR
, 0)
724 DEF_TALITOS2_INTERRUPT(ch0_2
, TALITOS2_ISR_CH_0_2_DONE
, TALITOS2_ISR_CH_0_2_ERR
,
726 DEF_TALITOS2_INTERRUPT(ch1_3
, TALITOS2_ISR_CH_1_3_DONE
, TALITOS2_ISR_CH_1_3_ERR
,
732 static int talitos_rng_data_present(struct hwrng
*rng
, int wait
)
734 struct device
*dev
= (struct device
*)rng
->priv
;
735 struct talitos_private
*priv
= dev_get_drvdata(dev
);
739 for (i
= 0; i
< 20; i
++) {
740 ofl
= in_be32(priv
->reg_rngu
+ TALITOS_EUSR_LO
) &
741 TALITOS_RNGUSR_LO_OFL
;
750 static int talitos_rng_data_read(struct hwrng
*rng
, u32
*data
)
752 struct device
*dev
= (struct device
*)rng
->priv
;
753 struct talitos_private
*priv
= dev_get_drvdata(dev
);
755 /* rng fifo requires 64-bit accesses */
756 *data
= in_be32(priv
->reg_rngu
+ TALITOS_EU_FIFO
);
757 *data
= in_be32(priv
->reg_rngu
+ TALITOS_EU_FIFO_LO
);
762 static int talitos_rng_init(struct hwrng
*rng
)
764 struct device
*dev
= (struct device
*)rng
->priv
;
765 struct talitos_private
*priv
= dev_get_drvdata(dev
);
766 unsigned int timeout
= TALITOS_TIMEOUT
;
768 setbits32(priv
->reg_rngu
+ TALITOS_EURCR_LO
, TALITOS_RNGURCR_LO_SR
);
769 while (!(in_be32(priv
->reg_rngu
+ TALITOS_EUSR_LO
)
770 & TALITOS_RNGUSR_LO_RD
)
774 dev_err(dev
, "failed to reset rng hw\n");
778 /* start generating */
779 setbits32(priv
->reg_rngu
+ TALITOS_EUDSR_LO
, 0);
784 static int talitos_register_rng(struct device
*dev
)
786 struct talitos_private
*priv
= dev_get_drvdata(dev
);
789 priv
->rng
.name
= dev_driver_string(dev
),
790 priv
->rng
.init
= talitos_rng_init
,
791 priv
->rng
.data_present
= talitos_rng_data_present
,
792 priv
->rng
.data_read
= talitos_rng_data_read
,
793 priv
->rng
.priv
= (unsigned long)dev
;
795 err
= hwrng_register(&priv
->rng
);
797 priv
->rng_registered
= true;
802 static void talitos_unregister_rng(struct device
*dev
)
804 struct talitos_private
*priv
= dev_get_drvdata(dev
);
806 if (!priv
->rng_registered
)
809 hwrng_unregister(&priv
->rng
);
810 priv
->rng_registered
= false;
816 #define TALITOS_CRA_PRIORITY 3000
818 * Defines a priority for doing AEAD with descriptors type
819 * HMAC_SNOOP_NO_AFEA (HSNA) instead of type IPSEC_ESP
821 #define TALITOS_CRA_PRIORITY_AEAD_HSNA (TALITOS_CRA_PRIORITY - 1)
822 #define TALITOS_MAX_KEY_SIZE (AES_MAX_KEY_SIZE + SHA512_BLOCK_SIZE)
823 #define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
828 __be32 desc_hdr_template
;
829 u8 key
[TALITOS_MAX_KEY_SIZE
];
830 u8 iv
[TALITOS_MAX_IV_LENGTH
];
833 unsigned int enckeylen
;
834 unsigned int authkeylen
;
836 dma_addr_t dma_hw_context
;
839 #define HASH_MAX_BLOCK_SIZE SHA512_BLOCK_SIZE
840 #define TALITOS_MDEU_MAX_CONTEXT_SIZE TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
842 struct talitos_ahash_req_ctx
{
843 u32 hw_context
[TALITOS_MDEU_MAX_CONTEXT_SIZE
/ sizeof(u32
)];
844 unsigned int hw_context_size
;
845 u8 buf
[2][HASH_MAX_BLOCK_SIZE
];
850 unsigned int to_hash_later
;
852 struct scatterlist bufsl
[2];
853 struct scatterlist
*psrc
;
856 struct talitos_export_state
{
857 u32 hw_context
[TALITOS_MDEU_MAX_CONTEXT_SIZE
/ sizeof(u32
)];
858 u8 buf
[HASH_MAX_BLOCK_SIZE
];
862 unsigned int to_hash_later
;
866 static int aead_setkey(struct crypto_aead
*authenc
,
867 const u8
*key
, unsigned int keylen
)
869 struct talitos_ctx
*ctx
= crypto_aead_ctx(authenc
);
870 struct device
*dev
= ctx
->dev
;
871 struct crypto_authenc_keys keys
;
873 if (crypto_authenc_extractkeys(&keys
, key
, keylen
) != 0)
876 if (keys
.authkeylen
+ keys
.enckeylen
> TALITOS_MAX_KEY_SIZE
)
880 dma_unmap_single(dev
, ctx
->dma_key
, ctx
->keylen
, DMA_TO_DEVICE
);
882 memcpy(ctx
->key
, keys
.authkey
, keys
.authkeylen
);
883 memcpy(&ctx
->key
[keys
.authkeylen
], keys
.enckey
, keys
.enckeylen
);
885 ctx
->keylen
= keys
.authkeylen
+ keys
.enckeylen
;
886 ctx
->enckeylen
= keys
.enckeylen
;
887 ctx
->authkeylen
= keys
.authkeylen
;
888 ctx
->dma_key
= dma_map_single(dev
, ctx
->key
, ctx
->keylen
,
894 crypto_aead_set_flags(authenc
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
899 * talitos_edesc - s/w-extended descriptor
900 * @src_nents: number of segments in input scatterlist
901 * @dst_nents: number of segments in output scatterlist
902 * @icv_ool: whether ICV is out-of-line
903 * @iv_dma: dma address of iv for checking continuity and link table
904 * @dma_len: length of dma mapped link_tbl space
905 * @dma_link_tbl: bus physical address of link_tbl/buf
906 * @desc: h/w descriptor
907 * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1) (SEC2)
908 * @buf: input and output buffeur (if {src,dst}_nents > 1) (SEC1)
910 * if decrypting (with authcheck), or either one of src_nents or dst_nents
911 * is greater than 1, an integrity check value is concatenated to the end
914 struct talitos_edesc
{
920 dma_addr_t dma_link_tbl
;
921 struct talitos_desc desc
;
923 struct talitos_ptr link_tbl
[0];
928 static void talitos_sg_unmap(struct device
*dev
,
929 struct talitos_edesc
*edesc
,
930 struct scatterlist
*src
,
931 struct scatterlist
*dst
,
932 unsigned int len
, unsigned int offset
)
934 struct talitos_private
*priv
= dev_get_drvdata(dev
);
935 bool is_sec1
= has_ftr_sec1(priv
);
936 unsigned int src_nents
= edesc
->src_nents
? : 1;
937 unsigned int dst_nents
= edesc
->dst_nents
? : 1;
939 if (is_sec1
&& dst
&& dst_nents
> 1) {
940 dma_sync_single_for_device(dev
, edesc
->dma_link_tbl
+ offset
,
941 len
, DMA_FROM_DEVICE
);
942 sg_pcopy_from_buffer(dst
, dst_nents
, edesc
->buf
+ offset
, len
,
946 if (src_nents
== 1 || !is_sec1
)
947 dma_unmap_sg(dev
, src
, src_nents
, DMA_TO_DEVICE
);
949 if (dst
&& (dst_nents
== 1 || !is_sec1
))
950 dma_unmap_sg(dev
, dst
, dst_nents
, DMA_FROM_DEVICE
);
951 } else if (src_nents
== 1 || !is_sec1
) {
952 dma_unmap_sg(dev
, src
, src_nents
, DMA_BIDIRECTIONAL
);
956 static void ipsec_esp_unmap(struct device
*dev
,
957 struct talitos_edesc
*edesc
,
958 struct aead_request
*areq
)
960 struct crypto_aead
*aead
= crypto_aead_reqtfm(areq
);
961 struct talitos_ctx
*ctx
= crypto_aead_ctx(aead
);
962 unsigned int ivsize
= crypto_aead_ivsize(aead
);
963 bool is_ipsec_esp
= edesc
->desc
.hdr
& DESC_HDR_TYPE_IPSEC_ESP
;
964 struct talitos_ptr
*civ_ptr
= &edesc
->desc
.ptr
[is_ipsec_esp
? 2 : 3];
967 unmap_single_talitos_ptr(dev
, &edesc
->desc
.ptr
[6],
969 unmap_single_talitos_ptr(dev
, civ_ptr
, DMA_TO_DEVICE
);
971 talitos_sg_unmap(dev
, edesc
, areq
->src
, areq
->dst
, areq
->cryptlen
,
975 dma_unmap_single(dev
, edesc
->dma_link_tbl
, edesc
->dma_len
,
979 unsigned int dst_nents
= edesc
->dst_nents
? : 1;
981 sg_pcopy_to_buffer(areq
->dst
, dst_nents
, ctx
->iv
, ivsize
,
982 areq
->assoclen
+ areq
->cryptlen
- ivsize
);
987 * ipsec_esp descriptor callbacks
989 static void ipsec_esp_encrypt_done(struct device
*dev
,
990 struct talitos_desc
*desc
, void *context
,
993 struct talitos_private
*priv
= dev_get_drvdata(dev
);
994 bool is_sec1
= has_ftr_sec1(priv
);
995 struct aead_request
*areq
= context
;
996 struct crypto_aead
*authenc
= crypto_aead_reqtfm(areq
);
997 unsigned int authsize
= crypto_aead_authsize(authenc
);
998 unsigned int ivsize
= crypto_aead_ivsize(authenc
);
999 struct talitos_edesc
*edesc
;
1000 struct scatterlist
*sg
;
1003 edesc
= container_of(desc
, struct talitos_edesc
, desc
);
1005 ipsec_esp_unmap(dev
, edesc
, areq
);
1007 /* copy the generated ICV to dst */
1008 if (edesc
->icv_ool
) {
1010 icvdata
= edesc
->buf
+ areq
->assoclen
+ areq
->cryptlen
;
1012 icvdata
= &edesc
->link_tbl
[edesc
->src_nents
+
1013 edesc
->dst_nents
+ 2];
1014 sg
= sg_last(areq
->dst
, edesc
->dst_nents
);
1015 memcpy((char *)sg_virt(sg
) + sg
->length
- authsize
,
1019 dma_unmap_single(dev
, edesc
->iv_dma
, ivsize
, DMA_TO_DEVICE
);
1023 aead_request_complete(areq
, err
);
1026 static void ipsec_esp_decrypt_swauth_done(struct device
*dev
,
1027 struct talitos_desc
*desc
,
1028 void *context
, int err
)
1030 struct aead_request
*req
= context
;
1031 struct crypto_aead
*authenc
= crypto_aead_reqtfm(req
);
1032 unsigned int authsize
= crypto_aead_authsize(authenc
);
1033 struct talitos_edesc
*edesc
;
1034 struct scatterlist
*sg
;
1036 struct talitos_private
*priv
= dev_get_drvdata(dev
);
1037 bool is_sec1
= has_ftr_sec1(priv
);
1039 edesc
= container_of(desc
, struct talitos_edesc
, desc
);
1041 ipsec_esp_unmap(dev
, edesc
, req
);
1045 sg
= sg_last(req
->dst
, edesc
->dst_nents
? : 1);
1046 icv
= (char *)sg_virt(sg
) + sg
->length
- authsize
;
1048 if (edesc
->dma_len
) {
1050 oicv
= (char *)&edesc
->dma_link_tbl
+
1051 req
->assoclen
+ req
->cryptlen
;
1054 &edesc
->link_tbl
[edesc
->src_nents
+
1055 edesc
->dst_nents
+ 2];
1057 icv
= oicv
+ authsize
;
1059 oicv
= (char *)&edesc
->link_tbl
[0];
1061 err
= crypto_memneq(oicv
, icv
, authsize
) ? -EBADMSG
: 0;
1066 aead_request_complete(req
, err
);
1069 static void ipsec_esp_decrypt_hwauth_done(struct device
*dev
,
1070 struct talitos_desc
*desc
,
1071 void *context
, int err
)
1073 struct aead_request
*req
= context
;
1074 struct talitos_edesc
*edesc
;
1076 edesc
= container_of(desc
, struct talitos_edesc
, desc
);
1078 ipsec_esp_unmap(dev
, edesc
, req
);
1080 /* check ICV auth status */
1081 if (!err
&& ((desc
->hdr_lo
& DESC_HDR_LO_ICCR1_MASK
) !=
1082 DESC_HDR_LO_ICCR1_PASS
))
1087 aead_request_complete(req
, err
);
1091 * convert scatterlist to SEC h/w link table format
1092 * stop at cryptlen bytes
1094 static int sg_to_link_tbl_offset(struct scatterlist
*sg
, int sg_count
,
1095 unsigned int offset
, int cryptlen
,
1096 struct talitos_ptr
*link_tbl_ptr
)
1098 int n_sg
= sg_count
;
1101 while (cryptlen
&& sg
&& n_sg
--) {
1102 unsigned int len
= sg_dma_len(sg
);
1104 if (offset
>= len
) {
1114 to_talitos_ptr(link_tbl_ptr
+ count
,
1115 sg_dma_address(sg
) + offset
, len
, 0);
1116 to_talitos_ptr_ext_set(link_tbl_ptr
+ count
, 0, 0);
1125 /* tag end of link table */
1127 to_talitos_ptr_ext_set(link_tbl_ptr
+ count
- 1,
1128 DESC_PTR_LNKTBL_RETURN
, 0);
1133 static int talitos_sg_map(struct device
*dev
, struct scatterlist
*src
,
1134 unsigned int len
, struct talitos_edesc
*edesc
,
1135 struct talitos_ptr
*ptr
,
1136 int sg_count
, unsigned int offset
, int tbl_off
)
1138 struct talitos_private
*priv
= dev_get_drvdata(dev
);
1139 bool is_sec1
= has_ftr_sec1(priv
);
1142 to_talitos_ptr(ptr
, 0, 0, is_sec1
);
1145 if (sg_count
== 1) {
1146 to_talitos_ptr(ptr
, sg_dma_address(src
) + offset
, len
, is_sec1
);
1150 to_talitos_ptr(ptr
, edesc
->dma_link_tbl
+ offset
, len
, is_sec1
);
1153 sg_count
= sg_to_link_tbl_offset(src
, sg_count
, offset
, len
,
1154 &edesc
->link_tbl
[tbl_off
]);
1155 if (sg_count
== 1) {
1156 /* Only one segment now, so no link tbl needed*/
1157 copy_talitos_ptr(ptr
, &edesc
->link_tbl
[tbl_off
], is_sec1
);
1160 to_talitos_ptr(ptr
, edesc
->dma_link_tbl
+
1161 tbl_off
* sizeof(struct talitos_ptr
), len
, is_sec1
);
1162 to_talitos_ptr_ext_or(ptr
, DESC_PTR_LNKTBL_JUMP
, is_sec1
);
1168 * fill in and submit ipsec_esp descriptor
1170 static int ipsec_esp(struct talitos_edesc
*edesc
, struct aead_request
*areq
,
1171 void (*callback
)(struct device
*dev
,
1172 struct talitos_desc
*desc
,
1173 void *context
, int error
))
1175 struct crypto_aead
*aead
= crypto_aead_reqtfm(areq
);
1176 unsigned int authsize
= crypto_aead_authsize(aead
);
1177 struct talitos_ctx
*ctx
= crypto_aead_ctx(aead
);
1178 struct device
*dev
= ctx
->dev
;
1179 struct talitos_desc
*desc
= &edesc
->desc
;
1180 unsigned int cryptlen
= areq
->cryptlen
;
1181 unsigned int ivsize
= crypto_aead_ivsize(aead
);
1184 int sg_link_tbl_len
;
1185 bool sync_needed
= false;
1186 struct talitos_private
*priv
= dev_get_drvdata(dev
);
1187 bool is_sec1
= has_ftr_sec1(priv
);
1188 bool is_ipsec_esp
= desc
->hdr
& DESC_HDR_TYPE_IPSEC_ESP
;
1189 struct talitos_ptr
*civ_ptr
= &desc
->ptr
[is_ipsec_esp
? 2 : 3];
1190 struct talitos_ptr
*ckey_ptr
= &desc
->ptr
[is_ipsec_esp
? 3 : 2];
1193 to_talitos_ptr(&desc
->ptr
[0], ctx
->dma_key
, ctx
->authkeylen
, is_sec1
);
1195 sg_count
= edesc
->src_nents
?: 1;
1196 if (is_sec1
&& sg_count
> 1)
1197 sg_copy_to_buffer(areq
->src
, sg_count
, edesc
->buf
,
1198 areq
->assoclen
+ cryptlen
);
1200 sg_count
= dma_map_sg(dev
, areq
->src
, sg_count
,
1201 (areq
->src
== areq
->dst
) ?
1202 DMA_BIDIRECTIONAL
: DMA_TO_DEVICE
);
1205 ret
= talitos_sg_map(dev
, areq
->src
, areq
->assoclen
, edesc
,
1206 &desc
->ptr
[1], sg_count
, 0, tbl_off
);
1214 to_talitos_ptr(civ_ptr
, edesc
->iv_dma
, ivsize
, is_sec1
);
1217 to_talitos_ptr(ckey_ptr
, ctx
->dma_key
+ ctx
->authkeylen
,
1218 ctx
->enckeylen
, is_sec1
);
1222 * map and adjust cipher len to aead request cryptlen.
1223 * extent is bytes of HMAC postpended to ciphertext,
1224 * typically 12 for ipsec
1226 sg_link_tbl_len
= cryptlen
;
1229 to_talitos_ptr_ext_set(&desc
->ptr
[4], authsize
, is_sec1
);
1231 if (desc
->hdr
& DESC_HDR_MODE1_MDEU_CICV
)
1232 sg_link_tbl_len
+= authsize
;
1235 ret
= talitos_sg_map(dev
, areq
->src
, sg_link_tbl_len
, edesc
,
1236 &desc
->ptr
[4], sg_count
, areq
->assoclen
, tbl_off
);
1244 if (areq
->src
!= areq
->dst
) {
1245 sg_count
= edesc
->dst_nents
? : 1;
1246 if (!is_sec1
|| sg_count
== 1)
1247 dma_map_sg(dev
, areq
->dst
, sg_count
, DMA_FROM_DEVICE
);
1250 ret
= talitos_sg_map(dev
, areq
->dst
, cryptlen
, edesc
, &desc
->ptr
[5],
1251 sg_count
, areq
->assoclen
, tbl_off
);
1254 to_talitos_ptr_ext_or(&desc
->ptr
[5], authsize
, is_sec1
);
1259 edesc
->icv_ool
= true;
1263 struct talitos_ptr
*tbl_ptr
= &edesc
->link_tbl
[tbl_off
];
1264 int offset
= (edesc
->src_nents
+ edesc
->dst_nents
+ 2) *
1265 sizeof(struct talitos_ptr
) + authsize
;
1267 /* Add an entry to the link table for ICV data */
1268 to_talitos_ptr_ext_set(tbl_ptr
- 1, 0, is_sec1
);
1269 to_talitos_ptr_ext_set(tbl_ptr
, DESC_PTR_LNKTBL_RETURN
,
1272 /* icv data follows link tables */
1273 to_talitos_ptr(tbl_ptr
, edesc
->dma_link_tbl
+ offset
,
1276 dma_addr_t addr
= edesc
->dma_link_tbl
;
1279 addr
+= areq
->assoclen
+ cryptlen
;
1281 addr
+= sizeof(struct talitos_ptr
) * tbl_off
;
1283 to_talitos_ptr(&desc
->ptr
[6], addr
, authsize
, is_sec1
);
1285 } else if (!is_ipsec_esp
) {
1286 ret
= talitos_sg_map(dev
, areq
->dst
, authsize
, edesc
,
1287 &desc
->ptr
[6], sg_count
, areq
->assoclen
+
1292 edesc
->icv_ool
= true;
1295 edesc
->icv_ool
= false;
1298 edesc
->icv_ool
= false;
1303 map_single_talitos_ptr(dev
, &desc
->ptr
[6], ivsize
, ctx
->iv
,
1307 dma_sync_single_for_device(dev
, edesc
->dma_link_tbl
,
1311 ret
= talitos_submit(dev
, ctx
->ch
, desc
, callback
, areq
);
1312 if (ret
!= -EINPROGRESS
) {
1313 ipsec_esp_unmap(dev
, edesc
, areq
);
1320 * allocate and map the extended descriptor
1322 static struct talitos_edesc
*talitos_edesc_alloc(struct device
*dev
,
1323 struct scatterlist
*src
,
1324 struct scatterlist
*dst
,
1326 unsigned int assoclen
,
1327 unsigned int cryptlen
,
1328 unsigned int authsize
,
1329 unsigned int ivsize
,
1334 struct talitos_edesc
*edesc
;
1335 int src_nents
, dst_nents
, alloc_len
, dma_len
, src_len
, dst_len
;
1336 dma_addr_t iv_dma
= 0;
1337 gfp_t flags
= cryptoflags
& CRYPTO_TFM_REQ_MAY_SLEEP
? GFP_KERNEL
:
1339 struct talitos_private
*priv
= dev_get_drvdata(dev
);
1340 bool is_sec1
= has_ftr_sec1(priv
);
1341 int max_len
= is_sec1
? TALITOS1_MAX_DATA_LEN
: TALITOS2_MAX_DATA_LEN
;
1344 if (cryptlen
+ authsize
> max_len
) {
1345 dev_err(dev
, "length exceeds h/w max limit\n");
1346 return ERR_PTR(-EINVAL
);
1350 iv_dma
= dma_map_single(dev
, iv
, ivsize
, DMA_TO_DEVICE
);
1352 if (!dst
|| dst
== src
) {
1353 src_len
= assoclen
+ cryptlen
+ authsize
;
1354 src_nents
= sg_nents_for_len(src
, src_len
);
1355 if (src_nents
< 0) {
1356 dev_err(dev
, "Invalid number of src SG.\n");
1357 err
= ERR_PTR(-EINVAL
);
1360 src_nents
= (src_nents
== 1) ? 0 : src_nents
;
1361 dst_nents
= dst
? src_nents
: 0;
1363 } else { /* dst && dst != src*/
1364 src_len
= assoclen
+ cryptlen
+ (encrypt
? 0 : authsize
);
1365 src_nents
= sg_nents_for_len(src
, src_len
);
1366 if (src_nents
< 0) {
1367 dev_err(dev
, "Invalid number of src SG.\n");
1368 err
= ERR_PTR(-EINVAL
);
1371 src_nents
= (src_nents
== 1) ? 0 : src_nents
;
1372 dst_len
= assoclen
+ cryptlen
+ (encrypt
? authsize
: 0);
1373 dst_nents
= sg_nents_for_len(dst
, dst_len
);
1374 if (dst_nents
< 0) {
1375 dev_err(dev
, "Invalid number of dst SG.\n");
1376 err
= ERR_PTR(-EINVAL
);
1379 dst_nents
= (dst_nents
== 1) ? 0 : dst_nents
;
1383 * allocate space for base edesc plus the link tables,
1384 * allowing for two separate entries for AD and generated ICV (+ 2),
1385 * and space for two sets of ICVs (stashed and generated)
1387 alloc_len
= sizeof(struct talitos_edesc
);
1388 if (src_nents
|| dst_nents
) {
1390 dma_len
= (src_nents
? src_len
: 0) +
1391 (dst_nents
? dst_len
: 0);
1393 dma_len
= (src_nents
+ dst_nents
+ 2) *
1394 sizeof(struct talitos_ptr
) + authsize
* 2;
1395 alloc_len
+= dma_len
;
1398 alloc_len
+= icv_stashing
? authsize
: 0;
1401 /* if its a ahash, add space for a second desc next to the first one */
1402 if (is_sec1
&& !dst
)
1403 alloc_len
+= sizeof(struct talitos_desc
);
1405 edesc
= kmalloc(alloc_len
, GFP_DMA
| flags
);
1407 dev_err(dev
, "could not allocate edescriptor\n");
1408 err
= ERR_PTR(-ENOMEM
);
1411 memset(&edesc
->desc
, 0, sizeof(edesc
->desc
));
1413 edesc
->src_nents
= src_nents
;
1414 edesc
->dst_nents
= dst_nents
;
1415 edesc
->iv_dma
= iv_dma
;
1416 edesc
->dma_len
= dma_len
;
1418 void *addr
= &edesc
->link_tbl
[0];
1420 if (is_sec1
&& !dst
)
1421 addr
+= sizeof(struct talitos_desc
);
1422 edesc
->dma_link_tbl
= dma_map_single(dev
, addr
,
1429 dma_unmap_single(dev
, iv_dma
, ivsize
, DMA_TO_DEVICE
);
1433 static struct talitos_edesc
*aead_edesc_alloc(struct aead_request
*areq
, u8
*iv
,
1434 int icv_stashing
, bool encrypt
)
1436 struct crypto_aead
*authenc
= crypto_aead_reqtfm(areq
);
1437 unsigned int authsize
= crypto_aead_authsize(authenc
);
1438 struct talitos_ctx
*ctx
= crypto_aead_ctx(authenc
);
1439 unsigned int ivsize
= crypto_aead_ivsize(authenc
);
1441 return talitos_edesc_alloc(ctx
->dev
, areq
->src
, areq
->dst
,
1442 iv
, areq
->assoclen
, areq
->cryptlen
,
1443 authsize
, ivsize
, icv_stashing
,
1444 areq
->base
.flags
, encrypt
);
1447 static int aead_encrypt(struct aead_request
*req
)
1449 struct crypto_aead
*authenc
= crypto_aead_reqtfm(req
);
1450 struct talitos_ctx
*ctx
= crypto_aead_ctx(authenc
);
1451 struct talitos_edesc
*edesc
;
1453 /* allocate extended descriptor */
1454 edesc
= aead_edesc_alloc(req
, req
->iv
, 0, true);
1456 return PTR_ERR(edesc
);
1459 edesc
->desc
.hdr
= ctx
->desc_hdr_template
| DESC_HDR_MODE0_ENCRYPT
;
1461 return ipsec_esp(edesc
, req
, ipsec_esp_encrypt_done
);
1464 static int aead_decrypt(struct aead_request
*req
)
1466 struct crypto_aead
*authenc
= crypto_aead_reqtfm(req
);
1467 unsigned int authsize
= crypto_aead_authsize(authenc
);
1468 struct talitos_ctx
*ctx
= crypto_aead_ctx(authenc
);
1469 struct talitos_private
*priv
= dev_get_drvdata(ctx
->dev
);
1470 struct talitos_edesc
*edesc
;
1471 struct scatterlist
*sg
;
1474 req
->cryptlen
-= authsize
;
1476 /* allocate extended descriptor */
1477 edesc
= aead_edesc_alloc(req
, req
->iv
, 1, false);
1479 return PTR_ERR(edesc
);
1481 if ((priv
->features
& TALITOS_FTR_HW_AUTH_CHECK
) &&
1482 ((!edesc
->src_nents
&& !edesc
->dst_nents
) ||
1483 priv
->features
& TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT
)) {
1485 /* decrypt and check the ICV */
1486 edesc
->desc
.hdr
= ctx
->desc_hdr_template
|
1487 DESC_HDR_DIR_INBOUND
|
1488 DESC_HDR_MODE1_MDEU_CICV
;
1490 /* reset integrity check result bits */
1492 return ipsec_esp(edesc
, req
, ipsec_esp_decrypt_hwauth_done
);
1495 /* Have to check the ICV with software */
1496 edesc
->desc
.hdr
= ctx
->desc_hdr_template
| DESC_HDR_DIR_INBOUND
;
1498 /* stash incoming ICV for later cmp with ICV generated by the h/w */
1500 icvdata
= (char *)&edesc
->link_tbl
[edesc
->src_nents
+
1501 edesc
->dst_nents
+ 2];
1503 icvdata
= &edesc
->link_tbl
[0];
1505 sg
= sg_last(req
->src
, edesc
->src_nents
? : 1);
1507 memcpy(icvdata
, (char *)sg_virt(sg
) + sg
->length
- authsize
, authsize
);
1509 return ipsec_esp(edesc
, req
, ipsec_esp_decrypt_swauth_done
);
1512 static int ablkcipher_setkey(struct crypto_ablkcipher
*cipher
,
1513 const u8
*key
, unsigned int keylen
)
1515 struct talitos_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
1516 struct device
*dev
= ctx
->dev
;
1517 u32 tmp
[DES_EXPKEY_WORDS
];
1519 if (keylen
> TALITOS_MAX_KEY_SIZE
) {
1520 crypto_ablkcipher_set_flags(cipher
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
1524 if (unlikely(crypto_ablkcipher_get_flags(cipher
) &
1525 CRYPTO_TFM_REQ_WEAK_KEY
) &&
1526 !des_ekey(tmp
, key
)) {
1527 crypto_ablkcipher_set_flags(cipher
, CRYPTO_TFM_RES_WEAK_KEY
);
1532 dma_unmap_single(dev
, ctx
->dma_key
, ctx
->keylen
, DMA_TO_DEVICE
);
1534 memcpy(&ctx
->key
, key
, keylen
);
1535 ctx
->keylen
= keylen
;
1537 ctx
->dma_key
= dma_map_single(dev
, ctx
->key
, keylen
, DMA_TO_DEVICE
);
1542 static void common_nonsnoop_unmap(struct device
*dev
,
1543 struct talitos_edesc
*edesc
,
1544 struct ablkcipher_request
*areq
)
1546 unmap_single_talitos_ptr(dev
, &edesc
->desc
.ptr
[5], DMA_FROM_DEVICE
);
1548 talitos_sg_unmap(dev
, edesc
, areq
->src
, areq
->dst
, areq
->nbytes
, 0);
1549 unmap_single_talitos_ptr(dev
, &edesc
->desc
.ptr
[1], DMA_TO_DEVICE
);
1552 dma_unmap_single(dev
, edesc
->dma_link_tbl
, edesc
->dma_len
,
1556 static void ablkcipher_done(struct device
*dev
,
1557 struct talitos_desc
*desc
, void *context
,
1560 struct ablkcipher_request
*areq
= context
;
1561 struct talitos_edesc
*edesc
;
1563 edesc
= container_of(desc
, struct talitos_edesc
, desc
);
1565 common_nonsnoop_unmap(dev
, edesc
, areq
);
1569 areq
->base
.complete(&areq
->base
, err
);
1572 static int common_nonsnoop(struct talitos_edesc
*edesc
,
1573 struct ablkcipher_request
*areq
,
1574 void (*callback
) (struct device
*dev
,
1575 struct talitos_desc
*desc
,
1576 void *context
, int error
))
1578 struct crypto_ablkcipher
*cipher
= crypto_ablkcipher_reqtfm(areq
);
1579 struct talitos_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
1580 struct device
*dev
= ctx
->dev
;
1581 struct talitos_desc
*desc
= &edesc
->desc
;
1582 unsigned int cryptlen
= areq
->nbytes
;
1583 unsigned int ivsize
= crypto_ablkcipher_ivsize(cipher
);
1585 bool sync_needed
= false;
1586 struct talitos_private
*priv
= dev_get_drvdata(dev
);
1587 bool is_sec1
= has_ftr_sec1(priv
);
1589 /* first DWORD empty */
1592 to_talitos_ptr(&desc
->ptr
[1], edesc
->iv_dma
, ivsize
, is_sec1
);
1595 to_talitos_ptr(&desc
->ptr
[2], ctx
->dma_key
, ctx
->keylen
, is_sec1
);
1597 sg_count
= edesc
->src_nents
?: 1;
1598 if (is_sec1
&& sg_count
> 1)
1599 sg_copy_to_buffer(areq
->src
, sg_count
, edesc
->buf
,
1602 sg_count
= dma_map_sg(dev
, areq
->src
, sg_count
,
1603 (areq
->src
== areq
->dst
) ?
1604 DMA_BIDIRECTIONAL
: DMA_TO_DEVICE
);
1608 sg_count
= talitos_sg_map(dev
, areq
->src
, cryptlen
, edesc
,
1609 &desc
->ptr
[3], sg_count
, 0, 0);
1614 if (areq
->src
!= areq
->dst
) {
1615 sg_count
= edesc
->dst_nents
? : 1;
1616 if (!is_sec1
|| sg_count
== 1)
1617 dma_map_sg(dev
, areq
->dst
, sg_count
, DMA_FROM_DEVICE
);
1620 ret
= talitos_sg_map(dev
, areq
->dst
, cryptlen
, edesc
, &desc
->ptr
[4],
1621 sg_count
, 0, (edesc
->src_nents
+ 1));
1626 map_single_talitos_ptr(dev
, &desc
->ptr
[5], ivsize
, ctx
->iv
,
1629 /* last DWORD empty */
1632 dma_sync_single_for_device(dev
, edesc
->dma_link_tbl
,
1633 edesc
->dma_len
, DMA_BIDIRECTIONAL
);
1635 ret
= talitos_submit(dev
, ctx
->ch
, desc
, callback
, areq
);
1636 if (ret
!= -EINPROGRESS
) {
1637 common_nonsnoop_unmap(dev
, edesc
, areq
);
1643 static struct talitos_edesc
*ablkcipher_edesc_alloc(struct ablkcipher_request
*
1646 struct crypto_ablkcipher
*cipher
= crypto_ablkcipher_reqtfm(areq
);
1647 struct talitos_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
1648 unsigned int ivsize
= crypto_ablkcipher_ivsize(cipher
);
1650 return talitos_edesc_alloc(ctx
->dev
, areq
->src
, areq
->dst
,
1651 areq
->info
, 0, areq
->nbytes
, 0, ivsize
, 0,
1652 areq
->base
.flags
, encrypt
);
1655 static int ablkcipher_encrypt(struct ablkcipher_request
*areq
)
1657 struct crypto_ablkcipher
*cipher
= crypto_ablkcipher_reqtfm(areq
);
1658 struct talitos_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
1659 struct talitos_edesc
*edesc
;
1661 /* allocate extended descriptor */
1662 edesc
= ablkcipher_edesc_alloc(areq
, true);
1664 return PTR_ERR(edesc
);
1667 edesc
->desc
.hdr
= ctx
->desc_hdr_template
| DESC_HDR_MODE0_ENCRYPT
;
1669 return common_nonsnoop(edesc
, areq
, ablkcipher_done
);
1672 static int ablkcipher_decrypt(struct ablkcipher_request
*areq
)
1674 struct crypto_ablkcipher
*cipher
= crypto_ablkcipher_reqtfm(areq
);
1675 struct talitos_ctx
*ctx
= crypto_ablkcipher_ctx(cipher
);
1676 struct talitos_edesc
*edesc
;
1678 /* allocate extended descriptor */
1679 edesc
= ablkcipher_edesc_alloc(areq
, false);
1681 return PTR_ERR(edesc
);
1683 edesc
->desc
.hdr
= ctx
->desc_hdr_template
| DESC_HDR_DIR_INBOUND
;
1685 return common_nonsnoop(edesc
, areq
, ablkcipher_done
);
1688 static void common_nonsnoop_hash_unmap(struct device
*dev
,
1689 struct talitos_edesc
*edesc
,
1690 struct ahash_request
*areq
)
1692 struct talitos_ahash_req_ctx
*req_ctx
= ahash_request_ctx(areq
);
1694 talitos_sg_unmap(dev
, edesc
, req_ctx
->psrc
, NULL
, 0, 0);
1697 dma_unmap_single(dev
, edesc
->dma_link_tbl
, edesc
->dma_len
,
1700 if (edesc
->desc
.next_desc
)
1701 dma_unmap_single(dev
, be32_to_cpu(edesc
->desc
.next_desc
),
1702 TALITOS_DESC_SIZE
, DMA_BIDIRECTIONAL
);
1705 static void ahash_done(struct device
*dev
,
1706 struct talitos_desc
*desc
, void *context
,
1709 struct ahash_request
*areq
= context
;
1710 struct talitos_edesc
*edesc
=
1711 container_of(desc
, struct talitos_edesc
, desc
);
1712 struct talitos_ahash_req_ctx
*req_ctx
= ahash_request_ctx(areq
);
1714 if (!req_ctx
->last
&& req_ctx
->to_hash_later
) {
1715 /* Position any partial block for next update/final/finup */
1716 req_ctx
->buf_idx
= (req_ctx
->buf_idx
+ 1) & 1;
1717 req_ctx
->nbuf
= req_ctx
->to_hash_later
;
1719 common_nonsnoop_hash_unmap(dev
, edesc
, areq
);
1723 areq
->base
.complete(&areq
->base
, err
);
1727 * SEC1 doesn't like hashing of 0 sized message, so we do the padding
1728 * ourself and submit a padded block
1730 static void talitos_handle_buggy_hash(struct talitos_ctx
*ctx
,
1731 struct talitos_edesc
*edesc
,
1732 struct talitos_ptr
*ptr
)
1734 static u8 padded_hash
[64] = {
1735 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1736 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1737 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1738 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1741 pr_err_once("Bug in SEC1, padding ourself\n");
1742 edesc
->desc
.hdr
&= ~DESC_HDR_MODE0_MDEU_PAD
;
1743 map_single_talitos_ptr(ctx
->dev
, ptr
, sizeof(padded_hash
),
1744 (char *)padded_hash
, DMA_TO_DEVICE
);
1747 static int common_nonsnoop_hash(struct talitos_edesc
*edesc
,
1748 struct ahash_request
*areq
, unsigned int length
,
1749 unsigned int offset
,
1750 void (*callback
) (struct device
*dev
,
1751 struct talitos_desc
*desc
,
1752 void *context
, int error
))
1754 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(areq
);
1755 struct talitos_ctx
*ctx
= crypto_ahash_ctx(tfm
);
1756 struct talitos_ahash_req_ctx
*req_ctx
= ahash_request_ctx(areq
);
1757 struct device
*dev
= ctx
->dev
;
1758 struct talitos_desc
*desc
= &edesc
->desc
;
1760 bool sync_needed
= false;
1761 struct talitos_private
*priv
= dev_get_drvdata(dev
);
1762 bool is_sec1
= has_ftr_sec1(priv
);
1765 /* first DWORD empty */
1767 /* hash context in */
1768 if (!req_ctx
->first
|| req_ctx
->swinit
) {
1769 to_talitos_ptr(&desc
->ptr
[1], ctx
->dma_hw_context
,
1770 req_ctx
->hw_context_size
, is_sec1
);
1771 req_ctx
->swinit
= 0;
1773 /* Indicate next op is not the first. */
1778 to_talitos_ptr(&desc
->ptr
[2], ctx
->dma_key
, ctx
->keylen
,
1781 if (is_sec1
&& req_ctx
->nbuf
)
1782 length
-= req_ctx
->nbuf
;
1784 sg_count
= edesc
->src_nents
?: 1;
1785 if (is_sec1
&& sg_count
> 1)
1786 sg_pcopy_to_buffer(req_ctx
->psrc
, sg_count
,
1787 edesc
->buf
+ sizeof(struct talitos_desc
),
1788 length
, req_ctx
->nbuf
);
1790 sg_count
= dma_map_sg(dev
, req_ctx
->psrc
, sg_count
,
1795 if (is_sec1
&& req_ctx
->nbuf
) {
1796 dma_addr_t dma_buf
= ctx
->dma_buf
+ req_ctx
->buf_idx
*
1797 HASH_MAX_BLOCK_SIZE
;
1799 to_talitos_ptr(&desc
->ptr
[3], dma_buf
, req_ctx
->nbuf
, is_sec1
);
1801 sg_count
= talitos_sg_map(dev
, req_ctx
->psrc
, length
, edesc
,
1802 &desc
->ptr
[3], sg_count
, offset
, 0);
1807 /* fifth DWORD empty */
1809 /* hash/HMAC out -or- hash context out */
1811 map_single_talitos_ptr(dev
, &desc
->ptr
[5],
1812 crypto_ahash_digestsize(tfm
),
1813 areq
->result
, DMA_FROM_DEVICE
);
1815 to_talitos_ptr(&desc
->ptr
[5], ctx
->dma_hw_context
,
1816 req_ctx
->hw_context_size
, is_sec1
);
1818 /* last DWORD empty */
1820 if (is_sec1
&& from_talitos_ptr_len(&desc
->ptr
[3], true) == 0)
1821 talitos_handle_buggy_hash(ctx
, edesc
, &desc
->ptr
[3]);
1823 if (is_sec1
&& req_ctx
->nbuf
&& length
) {
1824 struct talitos_desc
*desc2
= desc
+ 1;
1825 dma_addr_t next_desc
;
1827 memset(desc2
, 0, sizeof(*desc2
));
1828 desc2
->hdr
= desc
->hdr
;
1829 desc2
->hdr
&= ~DESC_HDR_MODE0_MDEU_INIT
;
1830 desc2
->hdr1
= desc2
->hdr
;
1831 desc
->hdr
&= ~DESC_HDR_MODE0_MDEU_PAD
;
1832 desc
->hdr
|= DESC_HDR_MODE0_MDEU_CONT
;
1833 desc
->hdr
&= ~DESC_HDR_DONE_NOTIFY
;
1835 to_talitos_ptr(&desc2
->ptr
[1], ctx
->dma_hw_context
,
1836 req_ctx
->hw_context_size
, is_sec1
);
1838 copy_talitos_ptr(&desc2
->ptr
[2], &desc
->ptr
[2], is_sec1
);
1839 sg_count
= talitos_sg_map(dev
, req_ctx
->psrc
, length
, edesc
,
1840 &desc2
->ptr
[3], sg_count
, offset
, 0);
1843 copy_talitos_ptr(&desc2
->ptr
[5], &desc
->ptr
[5], is_sec1
);
1845 to_talitos_ptr(&desc
->ptr
[5], ctx
->dma_hw_context
,
1846 req_ctx
->hw_context_size
, is_sec1
);
1848 next_desc
= dma_map_single(dev
, &desc2
->hdr1
, TALITOS_DESC_SIZE
,
1850 desc
->next_desc
= cpu_to_be32(next_desc
);
1854 dma_sync_single_for_device(dev
, edesc
->dma_link_tbl
,
1855 edesc
->dma_len
, DMA_BIDIRECTIONAL
);
1857 ret
= talitos_submit(dev
, ctx
->ch
, desc
, callback
, areq
);
1858 if (ret
!= -EINPROGRESS
) {
1859 common_nonsnoop_hash_unmap(dev
, edesc
, areq
);
1865 static struct talitos_edesc
*ahash_edesc_alloc(struct ahash_request
*areq
,
1866 unsigned int nbytes
)
1868 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(areq
);
1869 struct talitos_ctx
*ctx
= crypto_ahash_ctx(tfm
);
1870 struct talitos_ahash_req_ctx
*req_ctx
= ahash_request_ctx(areq
);
1871 struct talitos_private
*priv
= dev_get_drvdata(ctx
->dev
);
1872 bool is_sec1
= has_ftr_sec1(priv
);
1875 nbytes
-= req_ctx
->nbuf
;
1877 return talitos_edesc_alloc(ctx
->dev
, req_ctx
->psrc
, NULL
, NULL
, 0,
1878 nbytes
, 0, 0, 0, areq
->base
.flags
, false);
1881 static int ahash_init(struct ahash_request
*areq
)
1883 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(areq
);
1884 struct talitos_ctx
*ctx
= crypto_ahash_ctx(tfm
);
1885 struct device
*dev
= ctx
->dev
;
1886 struct talitos_ahash_req_ctx
*req_ctx
= ahash_request_ctx(areq
);
1888 struct talitos_private
*priv
= dev_get_drvdata(dev
);
1889 bool is_sec1
= has_ftr_sec1(priv
);
1891 /* Initialize the context */
1892 req_ctx
->buf_idx
= 0;
1894 req_ctx
->first
= 1; /* first indicates h/w must init its context */
1895 req_ctx
->swinit
= 0; /* assume h/w init of context */
1896 size
= (crypto_ahash_digestsize(tfm
) <= SHA256_DIGEST_SIZE
)
1897 ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1898 : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
;
1899 req_ctx
->hw_context_size
= size
;
1901 if (ctx
->dma_hw_context
)
1902 dma_unmap_single(dev
, ctx
->dma_hw_context
, size
,
1904 ctx
->dma_hw_context
= dma_map_single(dev
, req_ctx
->hw_context
, size
,
1907 dma_unmap_single(dev
, ctx
->dma_buf
, sizeof(req_ctx
->buf
),
1910 ctx
->dma_buf
= dma_map_single(dev
, req_ctx
->buf
,
1911 sizeof(req_ctx
->buf
),
1917 * on h/w without explicit sha224 support, we initialize h/w context
1918 * manually with sha224 constants, and tell it to run sha256.
1920 static int ahash_init_sha224_swinit(struct ahash_request
*areq
)
1922 struct talitos_ahash_req_ctx
*req_ctx
= ahash_request_ctx(areq
);
1923 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(areq
);
1924 struct talitos_ctx
*ctx
= crypto_ahash_ctx(tfm
);
1925 struct device
*dev
= ctx
->dev
;
1928 req_ctx
->swinit
= 1;/* prevent h/w initting context with sha256 values*/
1930 req_ctx
->hw_context
[0] = SHA224_H0
;
1931 req_ctx
->hw_context
[1] = SHA224_H1
;
1932 req_ctx
->hw_context
[2] = SHA224_H2
;
1933 req_ctx
->hw_context
[3] = SHA224_H3
;
1934 req_ctx
->hw_context
[4] = SHA224_H4
;
1935 req_ctx
->hw_context
[5] = SHA224_H5
;
1936 req_ctx
->hw_context
[6] = SHA224_H6
;
1937 req_ctx
->hw_context
[7] = SHA224_H7
;
1939 /* init 64-bit count */
1940 req_ctx
->hw_context
[8] = 0;
1941 req_ctx
->hw_context
[9] = 0;
1943 dma_sync_single_for_device(dev
, ctx
->dma_hw_context
,
1944 req_ctx
->hw_context_size
, DMA_TO_DEVICE
);
1949 static int ahash_process_req(struct ahash_request
*areq
, unsigned int nbytes
)
1951 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(areq
);
1952 struct talitos_ctx
*ctx
= crypto_ahash_ctx(tfm
);
1953 struct talitos_ahash_req_ctx
*req_ctx
= ahash_request_ctx(areq
);
1954 struct talitos_edesc
*edesc
;
1955 unsigned int blocksize
=
1956 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm
));
1957 unsigned int nbytes_to_hash
;
1958 unsigned int to_hash_later
;
1961 struct device
*dev
= ctx
->dev
;
1962 struct talitos_private
*priv
= dev_get_drvdata(dev
);
1963 bool is_sec1
= has_ftr_sec1(priv
);
1965 u8
*ctx_buf
= req_ctx
->buf
[req_ctx
->buf_idx
];
1967 if (!req_ctx
->last
&& (nbytes
+ req_ctx
->nbuf
<= blocksize
)) {
1968 /* Buffer up to one whole block */
1969 nents
= sg_nents_for_len(areq
->src
, nbytes
);
1971 dev_err(ctx
->dev
, "Invalid number of src SG.\n");
1974 sg_copy_to_buffer(areq
->src
, nents
,
1975 ctx_buf
+ req_ctx
->nbuf
, nbytes
);
1976 req_ctx
->nbuf
+= nbytes
;
1980 /* At least (blocksize + 1) bytes are available to hash */
1981 nbytes_to_hash
= nbytes
+ req_ctx
->nbuf
;
1982 to_hash_later
= nbytes_to_hash
& (blocksize
- 1);
1986 else if (to_hash_later
)
1987 /* There is a partial block. Hash the full block(s) now */
1988 nbytes_to_hash
-= to_hash_later
;
1990 /* Keep one block buffered */
1991 nbytes_to_hash
-= blocksize
;
1992 to_hash_later
= blocksize
;
1995 /* Chain in any previously buffered data */
1996 if (!is_sec1
&& req_ctx
->nbuf
) {
1997 nsg
= (req_ctx
->nbuf
< nbytes_to_hash
) ? 2 : 1;
1998 sg_init_table(req_ctx
->bufsl
, nsg
);
1999 sg_set_buf(req_ctx
->bufsl
, ctx_buf
, req_ctx
->nbuf
);
2001 sg_chain(req_ctx
->bufsl
, 2, areq
->src
);
2002 req_ctx
->psrc
= req_ctx
->bufsl
;
2003 } else if (is_sec1
&& req_ctx
->nbuf
&& req_ctx
->nbuf
< blocksize
) {
2004 if (nbytes_to_hash
> blocksize
)
2005 offset
= blocksize
- req_ctx
->nbuf
;
2007 offset
= nbytes_to_hash
- req_ctx
->nbuf
;
2008 nents
= sg_nents_for_len(areq
->src
, offset
);
2010 dev_err(ctx
->dev
, "Invalid number of src SG.\n");
2013 sg_copy_to_buffer(areq
->src
, nents
,
2014 ctx_buf
+ req_ctx
->nbuf
, offset
);
2015 req_ctx
->nbuf
+= offset
;
2016 req_ctx
->psrc
= areq
->src
;
2018 req_ctx
->psrc
= areq
->src
;
2020 if (to_hash_later
) {
2021 nents
= sg_nents_for_len(areq
->src
, nbytes
);
2023 dev_err(ctx
->dev
, "Invalid number of src SG.\n");
2026 sg_pcopy_to_buffer(areq
->src
, nents
,
2027 req_ctx
->buf
[(req_ctx
->buf_idx
+ 1) & 1],
2029 nbytes
- to_hash_later
);
2031 req_ctx
->to_hash_later
= to_hash_later
;
2033 /* Allocate extended descriptor */
2034 edesc
= ahash_edesc_alloc(areq
, nbytes_to_hash
);
2036 return PTR_ERR(edesc
);
2038 edesc
->desc
.hdr
= ctx
->desc_hdr_template
;
2040 /* On last one, request SEC to pad; otherwise continue */
2042 edesc
->desc
.hdr
|= DESC_HDR_MODE0_MDEU_PAD
;
2044 edesc
->desc
.hdr
|= DESC_HDR_MODE0_MDEU_CONT
;
2046 /* request SEC to INIT hash. */
2047 if (req_ctx
->first
&& !req_ctx
->swinit
)
2048 edesc
->desc
.hdr
|= DESC_HDR_MODE0_MDEU_INIT
;
2050 dma_addr_t dma_buf
= ctx
->dma_buf
+ req_ctx
->buf_idx
*
2051 HASH_MAX_BLOCK_SIZE
;
2053 dma_sync_single_for_device(dev
, dma_buf
,
2054 req_ctx
->nbuf
, DMA_TO_DEVICE
);
2057 /* When the tfm context has a keylen, it's an HMAC.
2058 * A first or last (ie. not middle) descriptor must request HMAC.
2060 if (ctx
->keylen
&& (req_ctx
->first
|| req_ctx
->last
))
2061 edesc
->desc
.hdr
|= DESC_HDR_MODE0_MDEU_HMAC
;
2063 return common_nonsnoop_hash(edesc
, areq
, nbytes_to_hash
, offset
,
2067 static int ahash_update(struct ahash_request
*areq
)
2069 struct talitos_ahash_req_ctx
*req_ctx
= ahash_request_ctx(areq
);
2073 return ahash_process_req(areq
, areq
->nbytes
);
2076 static int ahash_final(struct ahash_request
*areq
)
2078 struct talitos_ahash_req_ctx
*req_ctx
= ahash_request_ctx(areq
);
2082 return ahash_process_req(areq
, 0);
2085 static int ahash_finup(struct ahash_request
*areq
)
2087 struct talitos_ahash_req_ctx
*req_ctx
= ahash_request_ctx(areq
);
2091 return ahash_process_req(areq
, areq
->nbytes
);
2094 static int ahash_digest(struct ahash_request
*areq
)
2096 struct talitos_ahash_req_ctx
*req_ctx
= ahash_request_ctx(areq
);
2097 struct crypto_ahash
*ahash
= crypto_ahash_reqtfm(areq
);
2102 return ahash_process_req(areq
, areq
->nbytes
);
2105 static int ahash_export(struct ahash_request
*areq
, void *out
)
2107 struct talitos_ahash_req_ctx
*req_ctx
= ahash_request_ctx(areq
);
2108 struct talitos_export_state
*export
= out
;
2109 struct crypto_ahash
*ahash
= crypto_ahash_reqtfm(areq
);
2110 struct talitos_ctx
*ctx
= crypto_ahash_ctx(ahash
);
2111 struct device
*dev
= ctx
->dev
;
2113 dma_sync_single_for_cpu(dev
, ctx
->dma_hw_context
,
2114 req_ctx
->hw_context_size
, DMA_FROM_DEVICE
);
2115 memcpy(export
->hw_context
, req_ctx
->hw_context
,
2116 req_ctx
->hw_context_size
);
2117 memcpy(export
->buf
, req_ctx
->buf
[req_ctx
->buf_idx
], req_ctx
->nbuf
);
2118 export
->swinit
= req_ctx
->swinit
;
2119 export
->first
= req_ctx
->first
;
2120 export
->last
= req_ctx
->last
;
2121 export
->to_hash_later
= req_ctx
->to_hash_later
;
2122 export
->nbuf
= req_ctx
->nbuf
;
2127 static int ahash_import(struct ahash_request
*areq
, const void *in
)
2129 struct talitos_ahash_req_ctx
*req_ctx
= ahash_request_ctx(areq
);
2130 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(areq
);
2131 const struct talitos_export_state
*export
= in
;
2133 struct talitos_ctx
*ctx
= crypto_ahash_ctx(tfm
);
2134 struct device
*dev
= ctx
->dev
;
2135 struct talitos_private
*priv
= dev_get_drvdata(dev
);
2136 bool is_sec1
= has_ftr_sec1(priv
);
2138 memset(req_ctx
, 0, sizeof(*req_ctx
));
2139 size
= (crypto_ahash_digestsize(tfm
) <= SHA256_DIGEST_SIZE
)
2140 ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
2141 : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
;
2142 req_ctx
->hw_context_size
= size
;
2143 if (ctx
->dma_hw_context
)
2144 dma_unmap_single(dev
, ctx
->dma_hw_context
, size
,
2147 memcpy(req_ctx
->hw_context
, export
->hw_context
, size
);
2148 ctx
->dma_hw_context
= dma_map_single(dev
, req_ctx
->hw_context
, size
,
2151 dma_unmap_single(dev
, ctx
->dma_buf
, sizeof(req_ctx
->buf
),
2153 memcpy(req_ctx
->buf
[0], export
->buf
, export
->nbuf
);
2155 ctx
->dma_buf
= dma_map_single(dev
, req_ctx
->buf
,
2156 sizeof(req_ctx
->buf
),
2158 req_ctx
->swinit
= export
->swinit
;
2159 req_ctx
->first
= export
->first
;
2160 req_ctx
->last
= export
->last
;
2161 req_ctx
->to_hash_later
= export
->to_hash_later
;
2162 req_ctx
->nbuf
= export
->nbuf
;
2167 static int keyhash(struct crypto_ahash
*tfm
, const u8
*key
, unsigned int keylen
,
2170 struct talitos_ctx
*ctx
= crypto_tfm_ctx(crypto_ahash_tfm(tfm
));
2172 struct scatterlist sg
[1];
2173 struct ahash_request
*req
;
2174 struct crypto_wait wait
;
2177 crypto_init_wait(&wait
);
2179 req
= ahash_request_alloc(tfm
, GFP_KERNEL
);
2183 /* Keep tfm keylen == 0 during hash of the long key */
2185 ahash_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
2186 crypto_req_done
, &wait
);
2188 sg_init_one(&sg
[0], key
, keylen
);
2190 ahash_request_set_crypt(req
, sg
, hash
, keylen
);
2191 ret
= crypto_wait_req(crypto_ahash_digest(req
), &wait
);
2193 ahash_request_free(req
);
2198 static int ahash_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
2199 unsigned int keylen
)
2201 struct talitos_ctx
*ctx
= crypto_tfm_ctx(crypto_ahash_tfm(tfm
));
2202 struct device
*dev
= ctx
->dev
;
2203 unsigned int blocksize
=
2204 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm
));
2205 unsigned int digestsize
= crypto_ahash_digestsize(tfm
);
2206 unsigned int keysize
= keylen
;
2207 u8 hash
[SHA512_DIGEST_SIZE
];
2210 if (keylen
<= blocksize
)
2211 memcpy(ctx
->key
, key
, keysize
);
2213 /* Must get the hash of the long key */
2214 ret
= keyhash(tfm
, key
, keylen
, hash
);
2217 crypto_ahash_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
2221 keysize
= digestsize
;
2222 memcpy(ctx
->key
, hash
, digestsize
);
2226 dma_unmap_single(dev
, ctx
->dma_key
, ctx
->keylen
, DMA_TO_DEVICE
);
2228 ctx
->keylen
= keysize
;
2229 ctx
->dma_key
= dma_map_single(dev
, ctx
->key
, keysize
, DMA_TO_DEVICE
);
2235 struct talitos_alg_template
{
2239 struct crypto_alg crypto
;
2240 struct ahash_alg hash
;
2241 struct aead_alg aead
;
2243 __be32 desc_hdr_template
;
2246 static struct talitos_alg_template driver_algs
[] = {
2247 /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */
2248 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2251 .cra_name
= "authenc(hmac(sha1),cbc(aes))",
2252 .cra_driver_name
= "authenc-hmac-sha1-"
2254 .cra_blocksize
= AES_BLOCK_SIZE
,
2255 .cra_flags
= CRYPTO_ALG_ASYNC
,
2257 .ivsize
= AES_BLOCK_SIZE
,
2258 .maxauthsize
= SHA1_DIGEST_SIZE
,
2260 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
2261 DESC_HDR_SEL0_AESU
|
2262 DESC_HDR_MODE0_AESU_CBC
|
2263 DESC_HDR_SEL1_MDEUA
|
2264 DESC_HDR_MODE1_MDEU_INIT
|
2265 DESC_HDR_MODE1_MDEU_PAD
|
2266 DESC_HDR_MODE1_MDEU_SHA1_HMAC
,
2268 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2269 .priority
= TALITOS_CRA_PRIORITY_AEAD_HSNA
,
2272 .cra_name
= "authenc(hmac(sha1),cbc(aes))",
2273 .cra_driver_name
= "authenc-hmac-sha1-"
2275 .cra_blocksize
= AES_BLOCK_SIZE
,
2276 .cra_flags
= CRYPTO_ALG_ASYNC
,
2278 .ivsize
= AES_BLOCK_SIZE
,
2279 .maxauthsize
= SHA1_DIGEST_SIZE
,
2281 .desc_hdr_template
= DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU
|
2282 DESC_HDR_SEL0_AESU
|
2283 DESC_HDR_MODE0_AESU_CBC
|
2284 DESC_HDR_SEL1_MDEUA
|
2285 DESC_HDR_MODE1_MDEU_INIT
|
2286 DESC_HDR_MODE1_MDEU_PAD
|
2287 DESC_HDR_MODE1_MDEU_SHA1_HMAC
,
2289 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2292 .cra_name
= "authenc(hmac(sha1),"
2294 .cra_driver_name
= "authenc-hmac-sha1-"
2296 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2297 .cra_flags
= CRYPTO_ALG_ASYNC
,
2299 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2300 .maxauthsize
= SHA1_DIGEST_SIZE
,
2302 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
2304 DESC_HDR_MODE0_DEU_CBC
|
2305 DESC_HDR_MODE0_DEU_3DES
|
2306 DESC_HDR_SEL1_MDEUA
|
2307 DESC_HDR_MODE1_MDEU_INIT
|
2308 DESC_HDR_MODE1_MDEU_PAD
|
2309 DESC_HDR_MODE1_MDEU_SHA1_HMAC
,
2311 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2312 .priority
= TALITOS_CRA_PRIORITY_AEAD_HSNA
,
2315 .cra_name
= "authenc(hmac(sha1),"
2317 .cra_driver_name
= "authenc-hmac-sha1-"
2319 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2320 .cra_flags
= CRYPTO_ALG_ASYNC
,
2322 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2323 .maxauthsize
= SHA1_DIGEST_SIZE
,
2325 .desc_hdr_template
= DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU
|
2327 DESC_HDR_MODE0_DEU_CBC
|
2328 DESC_HDR_MODE0_DEU_3DES
|
2329 DESC_HDR_SEL1_MDEUA
|
2330 DESC_HDR_MODE1_MDEU_INIT
|
2331 DESC_HDR_MODE1_MDEU_PAD
|
2332 DESC_HDR_MODE1_MDEU_SHA1_HMAC
,
2334 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2337 .cra_name
= "authenc(hmac(sha224),cbc(aes))",
2338 .cra_driver_name
= "authenc-hmac-sha224-"
2340 .cra_blocksize
= AES_BLOCK_SIZE
,
2341 .cra_flags
= CRYPTO_ALG_ASYNC
,
2343 .ivsize
= AES_BLOCK_SIZE
,
2344 .maxauthsize
= SHA224_DIGEST_SIZE
,
2346 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
2347 DESC_HDR_SEL0_AESU
|
2348 DESC_HDR_MODE0_AESU_CBC
|
2349 DESC_HDR_SEL1_MDEUA
|
2350 DESC_HDR_MODE1_MDEU_INIT
|
2351 DESC_HDR_MODE1_MDEU_PAD
|
2352 DESC_HDR_MODE1_MDEU_SHA224_HMAC
,
2354 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2355 .priority
= TALITOS_CRA_PRIORITY_AEAD_HSNA
,
2358 .cra_name
= "authenc(hmac(sha224),cbc(aes))",
2359 .cra_driver_name
= "authenc-hmac-sha224-"
2361 .cra_blocksize
= AES_BLOCK_SIZE
,
2362 .cra_flags
= CRYPTO_ALG_ASYNC
,
2364 .ivsize
= AES_BLOCK_SIZE
,
2365 .maxauthsize
= SHA224_DIGEST_SIZE
,
2367 .desc_hdr_template
= DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU
|
2368 DESC_HDR_SEL0_AESU
|
2369 DESC_HDR_MODE0_AESU_CBC
|
2370 DESC_HDR_SEL1_MDEUA
|
2371 DESC_HDR_MODE1_MDEU_INIT
|
2372 DESC_HDR_MODE1_MDEU_PAD
|
2373 DESC_HDR_MODE1_MDEU_SHA224_HMAC
,
2375 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2378 .cra_name
= "authenc(hmac(sha224),"
2380 .cra_driver_name
= "authenc-hmac-sha224-"
2382 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2383 .cra_flags
= CRYPTO_ALG_ASYNC
,
2385 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2386 .maxauthsize
= SHA224_DIGEST_SIZE
,
2388 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
2390 DESC_HDR_MODE0_DEU_CBC
|
2391 DESC_HDR_MODE0_DEU_3DES
|
2392 DESC_HDR_SEL1_MDEUA
|
2393 DESC_HDR_MODE1_MDEU_INIT
|
2394 DESC_HDR_MODE1_MDEU_PAD
|
2395 DESC_HDR_MODE1_MDEU_SHA224_HMAC
,
2397 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2398 .priority
= TALITOS_CRA_PRIORITY_AEAD_HSNA
,
2401 .cra_name
= "authenc(hmac(sha224),"
2403 .cra_driver_name
= "authenc-hmac-sha224-"
2405 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2406 .cra_flags
= CRYPTO_ALG_ASYNC
,
2408 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2409 .maxauthsize
= SHA224_DIGEST_SIZE
,
2411 .desc_hdr_template
= DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU
|
2413 DESC_HDR_MODE0_DEU_CBC
|
2414 DESC_HDR_MODE0_DEU_3DES
|
2415 DESC_HDR_SEL1_MDEUA
|
2416 DESC_HDR_MODE1_MDEU_INIT
|
2417 DESC_HDR_MODE1_MDEU_PAD
|
2418 DESC_HDR_MODE1_MDEU_SHA224_HMAC
,
2420 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2423 .cra_name
= "authenc(hmac(sha256),cbc(aes))",
2424 .cra_driver_name
= "authenc-hmac-sha256-"
2426 .cra_blocksize
= AES_BLOCK_SIZE
,
2427 .cra_flags
= CRYPTO_ALG_ASYNC
,
2429 .ivsize
= AES_BLOCK_SIZE
,
2430 .maxauthsize
= SHA256_DIGEST_SIZE
,
2432 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
2433 DESC_HDR_SEL0_AESU
|
2434 DESC_HDR_MODE0_AESU_CBC
|
2435 DESC_HDR_SEL1_MDEUA
|
2436 DESC_HDR_MODE1_MDEU_INIT
|
2437 DESC_HDR_MODE1_MDEU_PAD
|
2438 DESC_HDR_MODE1_MDEU_SHA256_HMAC
,
2440 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2441 .priority
= TALITOS_CRA_PRIORITY_AEAD_HSNA
,
2444 .cra_name
= "authenc(hmac(sha256),cbc(aes))",
2445 .cra_driver_name
= "authenc-hmac-sha256-"
2447 .cra_blocksize
= AES_BLOCK_SIZE
,
2448 .cra_flags
= CRYPTO_ALG_ASYNC
,
2450 .ivsize
= AES_BLOCK_SIZE
,
2451 .maxauthsize
= SHA256_DIGEST_SIZE
,
2453 .desc_hdr_template
= DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU
|
2454 DESC_HDR_SEL0_AESU
|
2455 DESC_HDR_MODE0_AESU_CBC
|
2456 DESC_HDR_SEL1_MDEUA
|
2457 DESC_HDR_MODE1_MDEU_INIT
|
2458 DESC_HDR_MODE1_MDEU_PAD
|
2459 DESC_HDR_MODE1_MDEU_SHA256_HMAC
,
2461 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2464 .cra_name
= "authenc(hmac(sha256),"
2466 .cra_driver_name
= "authenc-hmac-sha256-"
2468 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2469 .cra_flags
= CRYPTO_ALG_ASYNC
,
2471 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2472 .maxauthsize
= SHA256_DIGEST_SIZE
,
2474 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
2476 DESC_HDR_MODE0_DEU_CBC
|
2477 DESC_HDR_MODE0_DEU_3DES
|
2478 DESC_HDR_SEL1_MDEUA
|
2479 DESC_HDR_MODE1_MDEU_INIT
|
2480 DESC_HDR_MODE1_MDEU_PAD
|
2481 DESC_HDR_MODE1_MDEU_SHA256_HMAC
,
2483 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2484 .priority
= TALITOS_CRA_PRIORITY_AEAD_HSNA
,
2487 .cra_name
= "authenc(hmac(sha256),"
2489 .cra_driver_name
= "authenc-hmac-sha256-"
2491 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2492 .cra_flags
= CRYPTO_ALG_ASYNC
,
2494 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2495 .maxauthsize
= SHA256_DIGEST_SIZE
,
2497 .desc_hdr_template
= DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU
|
2499 DESC_HDR_MODE0_DEU_CBC
|
2500 DESC_HDR_MODE0_DEU_3DES
|
2501 DESC_HDR_SEL1_MDEUA
|
2502 DESC_HDR_MODE1_MDEU_INIT
|
2503 DESC_HDR_MODE1_MDEU_PAD
|
2504 DESC_HDR_MODE1_MDEU_SHA256_HMAC
,
2506 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2509 .cra_name
= "authenc(hmac(sha384),cbc(aes))",
2510 .cra_driver_name
= "authenc-hmac-sha384-"
2512 .cra_blocksize
= AES_BLOCK_SIZE
,
2513 .cra_flags
= CRYPTO_ALG_ASYNC
,
2515 .ivsize
= AES_BLOCK_SIZE
,
2516 .maxauthsize
= SHA384_DIGEST_SIZE
,
2518 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
2519 DESC_HDR_SEL0_AESU
|
2520 DESC_HDR_MODE0_AESU_CBC
|
2521 DESC_HDR_SEL1_MDEUB
|
2522 DESC_HDR_MODE1_MDEU_INIT
|
2523 DESC_HDR_MODE1_MDEU_PAD
|
2524 DESC_HDR_MODE1_MDEUB_SHA384_HMAC
,
2526 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2529 .cra_name
= "authenc(hmac(sha384),"
2531 .cra_driver_name
= "authenc-hmac-sha384-"
2533 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2534 .cra_flags
= CRYPTO_ALG_ASYNC
,
2536 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2537 .maxauthsize
= SHA384_DIGEST_SIZE
,
2539 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
2541 DESC_HDR_MODE0_DEU_CBC
|
2542 DESC_HDR_MODE0_DEU_3DES
|
2543 DESC_HDR_SEL1_MDEUB
|
2544 DESC_HDR_MODE1_MDEU_INIT
|
2545 DESC_HDR_MODE1_MDEU_PAD
|
2546 DESC_HDR_MODE1_MDEUB_SHA384_HMAC
,
2548 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2551 .cra_name
= "authenc(hmac(sha512),cbc(aes))",
2552 .cra_driver_name
= "authenc-hmac-sha512-"
2554 .cra_blocksize
= AES_BLOCK_SIZE
,
2555 .cra_flags
= CRYPTO_ALG_ASYNC
,
2557 .ivsize
= AES_BLOCK_SIZE
,
2558 .maxauthsize
= SHA512_DIGEST_SIZE
,
2560 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
2561 DESC_HDR_SEL0_AESU
|
2562 DESC_HDR_MODE0_AESU_CBC
|
2563 DESC_HDR_SEL1_MDEUB
|
2564 DESC_HDR_MODE1_MDEU_INIT
|
2565 DESC_HDR_MODE1_MDEU_PAD
|
2566 DESC_HDR_MODE1_MDEUB_SHA512_HMAC
,
2568 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2571 .cra_name
= "authenc(hmac(sha512),"
2573 .cra_driver_name
= "authenc-hmac-sha512-"
2575 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2576 .cra_flags
= CRYPTO_ALG_ASYNC
,
2578 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2579 .maxauthsize
= SHA512_DIGEST_SIZE
,
2581 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
2583 DESC_HDR_MODE0_DEU_CBC
|
2584 DESC_HDR_MODE0_DEU_3DES
|
2585 DESC_HDR_SEL1_MDEUB
|
2586 DESC_HDR_MODE1_MDEU_INIT
|
2587 DESC_HDR_MODE1_MDEU_PAD
|
2588 DESC_HDR_MODE1_MDEUB_SHA512_HMAC
,
2590 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2593 .cra_name
= "authenc(hmac(md5),cbc(aes))",
2594 .cra_driver_name
= "authenc-hmac-md5-"
2596 .cra_blocksize
= AES_BLOCK_SIZE
,
2597 .cra_flags
= CRYPTO_ALG_ASYNC
,
2599 .ivsize
= AES_BLOCK_SIZE
,
2600 .maxauthsize
= MD5_DIGEST_SIZE
,
2602 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
2603 DESC_HDR_SEL0_AESU
|
2604 DESC_HDR_MODE0_AESU_CBC
|
2605 DESC_HDR_SEL1_MDEUA
|
2606 DESC_HDR_MODE1_MDEU_INIT
|
2607 DESC_HDR_MODE1_MDEU_PAD
|
2608 DESC_HDR_MODE1_MDEU_MD5_HMAC
,
2610 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2611 .priority
= TALITOS_CRA_PRIORITY_AEAD_HSNA
,
2614 .cra_name
= "authenc(hmac(md5),cbc(aes))",
2615 .cra_driver_name
= "authenc-hmac-md5-"
2617 .cra_blocksize
= AES_BLOCK_SIZE
,
2618 .cra_flags
= CRYPTO_ALG_ASYNC
,
2620 .ivsize
= AES_BLOCK_SIZE
,
2621 .maxauthsize
= MD5_DIGEST_SIZE
,
2623 .desc_hdr_template
= DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU
|
2624 DESC_HDR_SEL0_AESU
|
2625 DESC_HDR_MODE0_AESU_CBC
|
2626 DESC_HDR_SEL1_MDEUA
|
2627 DESC_HDR_MODE1_MDEU_INIT
|
2628 DESC_HDR_MODE1_MDEU_PAD
|
2629 DESC_HDR_MODE1_MDEU_MD5_HMAC
,
2631 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2634 .cra_name
= "authenc(hmac(md5),cbc(des3_ede))",
2635 .cra_driver_name
= "authenc-hmac-md5-"
2637 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2638 .cra_flags
= CRYPTO_ALG_ASYNC
,
2640 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2641 .maxauthsize
= MD5_DIGEST_SIZE
,
2643 .desc_hdr_template
= DESC_HDR_TYPE_IPSEC_ESP
|
2645 DESC_HDR_MODE0_DEU_CBC
|
2646 DESC_HDR_MODE0_DEU_3DES
|
2647 DESC_HDR_SEL1_MDEUA
|
2648 DESC_HDR_MODE1_MDEU_INIT
|
2649 DESC_HDR_MODE1_MDEU_PAD
|
2650 DESC_HDR_MODE1_MDEU_MD5_HMAC
,
2652 { .type
= CRYPTO_ALG_TYPE_AEAD
,
2653 .priority
= TALITOS_CRA_PRIORITY_AEAD_HSNA
,
2656 .cra_name
= "authenc(hmac(md5),cbc(des3_ede))",
2657 .cra_driver_name
= "authenc-hmac-md5-"
2659 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2660 .cra_flags
= CRYPTO_ALG_ASYNC
,
2662 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2663 .maxauthsize
= MD5_DIGEST_SIZE
,
2665 .desc_hdr_template
= DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU
|
2667 DESC_HDR_MODE0_DEU_CBC
|
2668 DESC_HDR_MODE0_DEU_3DES
|
2669 DESC_HDR_SEL1_MDEUA
|
2670 DESC_HDR_MODE1_MDEU_INIT
|
2671 DESC_HDR_MODE1_MDEU_PAD
|
2672 DESC_HDR_MODE1_MDEU_MD5_HMAC
,
2674 /* ABLKCIPHER algorithms. */
2675 { .type
= CRYPTO_ALG_TYPE_ABLKCIPHER
,
2677 .cra_name
= "ecb(aes)",
2678 .cra_driver_name
= "ecb-aes-talitos",
2679 .cra_blocksize
= AES_BLOCK_SIZE
,
2680 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
2683 .min_keysize
= AES_MIN_KEY_SIZE
,
2684 .max_keysize
= AES_MAX_KEY_SIZE
,
2685 .ivsize
= AES_BLOCK_SIZE
,
2688 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2691 { .type
= CRYPTO_ALG_TYPE_ABLKCIPHER
,
2693 .cra_name
= "cbc(aes)",
2694 .cra_driver_name
= "cbc-aes-talitos",
2695 .cra_blocksize
= AES_BLOCK_SIZE
,
2696 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
2699 .min_keysize
= AES_MIN_KEY_SIZE
,
2700 .max_keysize
= AES_MAX_KEY_SIZE
,
2701 .ivsize
= AES_BLOCK_SIZE
,
2704 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2705 DESC_HDR_SEL0_AESU
|
2706 DESC_HDR_MODE0_AESU_CBC
,
2708 { .type
= CRYPTO_ALG_TYPE_ABLKCIPHER
,
2710 .cra_name
= "ctr(aes)",
2711 .cra_driver_name
= "ctr-aes-talitos",
2712 .cra_blocksize
= AES_BLOCK_SIZE
,
2713 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
2716 .min_keysize
= AES_MIN_KEY_SIZE
,
2717 .max_keysize
= AES_MAX_KEY_SIZE
,
2718 .ivsize
= AES_BLOCK_SIZE
,
2721 .desc_hdr_template
= DESC_HDR_TYPE_AESU_CTR_NONSNOOP
|
2722 DESC_HDR_SEL0_AESU
|
2723 DESC_HDR_MODE0_AESU_CTR
,
2725 { .type
= CRYPTO_ALG_TYPE_ABLKCIPHER
,
2727 .cra_name
= "ecb(des)",
2728 .cra_driver_name
= "ecb-des-talitos",
2729 .cra_blocksize
= DES_BLOCK_SIZE
,
2730 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
2733 .min_keysize
= DES_KEY_SIZE
,
2734 .max_keysize
= DES_KEY_SIZE
,
2735 .ivsize
= DES_BLOCK_SIZE
,
2738 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2741 { .type
= CRYPTO_ALG_TYPE_ABLKCIPHER
,
2743 .cra_name
= "cbc(des)",
2744 .cra_driver_name
= "cbc-des-talitos",
2745 .cra_blocksize
= DES_BLOCK_SIZE
,
2746 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
2749 .min_keysize
= DES_KEY_SIZE
,
2750 .max_keysize
= DES_KEY_SIZE
,
2751 .ivsize
= DES_BLOCK_SIZE
,
2754 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2756 DESC_HDR_MODE0_DEU_CBC
,
2758 { .type
= CRYPTO_ALG_TYPE_ABLKCIPHER
,
2760 .cra_name
= "ecb(des3_ede)",
2761 .cra_driver_name
= "ecb-3des-talitos",
2762 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2763 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
2766 .min_keysize
= DES3_EDE_KEY_SIZE
,
2767 .max_keysize
= DES3_EDE_KEY_SIZE
,
2768 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2771 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2773 DESC_HDR_MODE0_DEU_3DES
,
2775 { .type
= CRYPTO_ALG_TYPE_ABLKCIPHER
,
2777 .cra_name
= "cbc(des3_ede)",
2778 .cra_driver_name
= "cbc-3des-talitos",
2779 .cra_blocksize
= DES3_EDE_BLOCK_SIZE
,
2780 .cra_flags
= CRYPTO_ALG_TYPE_ABLKCIPHER
|
2783 .min_keysize
= DES3_EDE_KEY_SIZE
,
2784 .max_keysize
= DES3_EDE_KEY_SIZE
,
2785 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2788 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2790 DESC_HDR_MODE0_DEU_CBC
|
2791 DESC_HDR_MODE0_DEU_3DES
,
2793 /* AHASH algorithms. */
2794 { .type
= CRYPTO_ALG_TYPE_AHASH
,
2796 .halg
.digestsize
= MD5_DIGEST_SIZE
,
2797 .halg
.statesize
= sizeof(struct talitos_export_state
),
2800 .cra_driver_name
= "md5-talitos",
2801 .cra_blocksize
= MD5_HMAC_BLOCK_SIZE
,
2802 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
2806 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2807 DESC_HDR_SEL0_MDEUA
|
2808 DESC_HDR_MODE0_MDEU_MD5
,
2810 { .type
= CRYPTO_ALG_TYPE_AHASH
,
2812 .halg
.digestsize
= SHA1_DIGEST_SIZE
,
2813 .halg
.statesize
= sizeof(struct talitos_export_state
),
2816 .cra_driver_name
= "sha1-talitos",
2817 .cra_blocksize
= SHA1_BLOCK_SIZE
,
2818 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
2822 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2823 DESC_HDR_SEL0_MDEUA
|
2824 DESC_HDR_MODE0_MDEU_SHA1
,
2826 { .type
= CRYPTO_ALG_TYPE_AHASH
,
2828 .halg
.digestsize
= SHA224_DIGEST_SIZE
,
2829 .halg
.statesize
= sizeof(struct talitos_export_state
),
2831 .cra_name
= "sha224",
2832 .cra_driver_name
= "sha224-talitos",
2833 .cra_blocksize
= SHA224_BLOCK_SIZE
,
2834 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
2838 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2839 DESC_HDR_SEL0_MDEUA
|
2840 DESC_HDR_MODE0_MDEU_SHA224
,
2842 { .type
= CRYPTO_ALG_TYPE_AHASH
,
2844 .halg
.digestsize
= SHA256_DIGEST_SIZE
,
2845 .halg
.statesize
= sizeof(struct talitos_export_state
),
2847 .cra_name
= "sha256",
2848 .cra_driver_name
= "sha256-talitos",
2849 .cra_blocksize
= SHA256_BLOCK_SIZE
,
2850 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
2854 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2855 DESC_HDR_SEL0_MDEUA
|
2856 DESC_HDR_MODE0_MDEU_SHA256
,
2858 { .type
= CRYPTO_ALG_TYPE_AHASH
,
2860 .halg
.digestsize
= SHA384_DIGEST_SIZE
,
2861 .halg
.statesize
= sizeof(struct talitos_export_state
),
2863 .cra_name
= "sha384",
2864 .cra_driver_name
= "sha384-talitos",
2865 .cra_blocksize
= SHA384_BLOCK_SIZE
,
2866 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
2870 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2871 DESC_HDR_SEL0_MDEUB
|
2872 DESC_HDR_MODE0_MDEUB_SHA384
,
2874 { .type
= CRYPTO_ALG_TYPE_AHASH
,
2876 .halg
.digestsize
= SHA512_DIGEST_SIZE
,
2877 .halg
.statesize
= sizeof(struct talitos_export_state
),
2879 .cra_name
= "sha512",
2880 .cra_driver_name
= "sha512-talitos",
2881 .cra_blocksize
= SHA512_BLOCK_SIZE
,
2882 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
2886 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2887 DESC_HDR_SEL0_MDEUB
|
2888 DESC_HDR_MODE0_MDEUB_SHA512
,
2890 { .type
= CRYPTO_ALG_TYPE_AHASH
,
2892 .halg
.digestsize
= MD5_DIGEST_SIZE
,
2893 .halg
.statesize
= sizeof(struct talitos_export_state
),
2895 .cra_name
= "hmac(md5)",
2896 .cra_driver_name
= "hmac-md5-talitos",
2897 .cra_blocksize
= MD5_HMAC_BLOCK_SIZE
,
2898 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
2902 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2903 DESC_HDR_SEL0_MDEUA
|
2904 DESC_HDR_MODE0_MDEU_MD5
,
2906 { .type
= CRYPTO_ALG_TYPE_AHASH
,
2908 .halg
.digestsize
= SHA1_DIGEST_SIZE
,
2909 .halg
.statesize
= sizeof(struct talitos_export_state
),
2911 .cra_name
= "hmac(sha1)",
2912 .cra_driver_name
= "hmac-sha1-talitos",
2913 .cra_blocksize
= SHA1_BLOCK_SIZE
,
2914 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
2918 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2919 DESC_HDR_SEL0_MDEUA
|
2920 DESC_HDR_MODE0_MDEU_SHA1
,
2922 { .type
= CRYPTO_ALG_TYPE_AHASH
,
2924 .halg
.digestsize
= SHA224_DIGEST_SIZE
,
2925 .halg
.statesize
= sizeof(struct talitos_export_state
),
2927 .cra_name
= "hmac(sha224)",
2928 .cra_driver_name
= "hmac-sha224-talitos",
2929 .cra_blocksize
= SHA224_BLOCK_SIZE
,
2930 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
2934 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2935 DESC_HDR_SEL0_MDEUA
|
2936 DESC_HDR_MODE0_MDEU_SHA224
,
2938 { .type
= CRYPTO_ALG_TYPE_AHASH
,
2940 .halg
.digestsize
= SHA256_DIGEST_SIZE
,
2941 .halg
.statesize
= sizeof(struct talitos_export_state
),
2943 .cra_name
= "hmac(sha256)",
2944 .cra_driver_name
= "hmac-sha256-talitos",
2945 .cra_blocksize
= SHA256_BLOCK_SIZE
,
2946 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
2950 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2951 DESC_HDR_SEL0_MDEUA
|
2952 DESC_HDR_MODE0_MDEU_SHA256
,
2954 { .type
= CRYPTO_ALG_TYPE_AHASH
,
2956 .halg
.digestsize
= SHA384_DIGEST_SIZE
,
2957 .halg
.statesize
= sizeof(struct talitos_export_state
),
2959 .cra_name
= "hmac(sha384)",
2960 .cra_driver_name
= "hmac-sha384-talitos",
2961 .cra_blocksize
= SHA384_BLOCK_SIZE
,
2962 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
2966 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2967 DESC_HDR_SEL0_MDEUB
|
2968 DESC_HDR_MODE0_MDEUB_SHA384
,
2970 { .type
= CRYPTO_ALG_TYPE_AHASH
,
2972 .halg
.digestsize
= SHA512_DIGEST_SIZE
,
2973 .halg
.statesize
= sizeof(struct talitos_export_state
),
2975 .cra_name
= "hmac(sha512)",
2976 .cra_driver_name
= "hmac-sha512-talitos",
2977 .cra_blocksize
= SHA512_BLOCK_SIZE
,
2978 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
|
2982 .desc_hdr_template
= DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
2983 DESC_HDR_SEL0_MDEUB
|
2984 DESC_HDR_MODE0_MDEUB_SHA512
,
2988 struct talitos_crypto_alg
{
2989 struct list_head entry
;
2991 struct talitos_alg_template algt
;
2994 static int talitos_init_common(struct talitos_ctx
*ctx
,
2995 struct talitos_crypto_alg
*talitos_alg
)
2997 struct talitos_private
*priv
;
2999 /* update context with ptr to dev */
3000 ctx
->dev
= talitos_alg
->dev
;
3002 /* assign SEC channel to tfm in round-robin fashion */
3003 priv
= dev_get_drvdata(ctx
->dev
);
3004 ctx
->ch
= atomic_inc_return(&priv
->last_chan
) &
3005 (priv
->num_channels
- 1);
3007 /* copy descriptor header template value */
3008 ctx
->desc_hdr_template
= talitos_alg
->algt
.desc_hdr_template
;
3010 /* select done notification */
3011 ctx
->desc_hdr_template
|= DESC_HDR_DONE_NOTIFY
;
3016 static int talitos_cra_init(struct crypto_tfm
*tfm
)
3018 struct crypto_alg
*alg
= tfm
->__crt_alg
;
3019 struct talitos_crypto_alg
*talitos_alg
;
3020 struct talitos_ctx
*ctx
= crypto_tfm_ctx(tfm
);
3022 if ((alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
) == CRYPTO_ALG_TYPE_AHASH
)
3023 talitos_alg
= container_of(__crypto_ahash_alg(alg
),
3024 struct talitos_crypto_alg
,
3027 talitos_alg
= container_of(alg
, struct talitos_crypto_alg
,
3030 return talitos_init_common(ctx
, talitos_alg
);
3033 static int talitos_cra_init_aead(struct crypto_aead
*tfm
)
3035 struct aead_alg
*alg
= crypto_aead_alg(tfm
);
3036 struct talitos_crypto_alg
*talitos_alg
;
3037 struct talitos_ctx
*ctx
= crypto_aead_ctx(tfm
);
3039 talitos_alg
= container_of(alg
, struct talitos_crypto_alg
,
3042 return talitos_init_common(ctx
, talitos_alg
);
3045 static int talitos_cra_init_ahash(struct crypto_tfm
*tfm
)
3047 struct talitos_ctx
*ctx
= crypto_tfm_ctx(tfm
);
3049 talitos_cra_init(tfm
);
3052 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
3053 sizeof(struct talitos_ahash_req_ctx
));
3058 static void talitos_cra_exit(struct crypto_tfm
*tfm
)
3060 struct talitos_ctx
*ctx
= crypto_tfm_ctx(tfm
);
3061 struct device
*dev
= ctx
->dev
;
3064 dma_unmap_single(dev
, ctx
->dma_key
, ctx
->keylen
, DMA_TO_DEVICE
);
3067 static void talitos_cra_exit_ahash(struct crypto_tfm
*tfm
)
3069 struct talitos_ctx
*ctx
= crypto_tfm_ctx(tfm
);
3070 struct device
*dev
= ctx
->dev
;
3073 talitos_cra_exit(tfm
);
3075 size
= (crypto_ahash_digestsize(__crypto_ahash_cast(tfm
)) <=
3077 ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
3078 : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
;
3080 if (ctx
->dma_hw_context
)
3081 dma_unmap_single(dev
, ctx
->dma_hw_context
, size
,
3084 dma_unmap_single(dev
, ctx
->dma_buf
, HASH_MAX_BLOCK_SIZE
* 2,
3089 * given the alg's descriptor header template, determine whether descriptor
3090 * type and primary/secondary execution units required match the hw
3091 * capabilities description provided in the device tree node.
3093 static int hw_supports(struct device
*dev
, __be32 desc_hdr_template
)
3095 struct talitos_private
*priv
= dev_get_drvdata(dev
);
3098 ret
= (1 << DESC_TYPE(desc_hdr_template
) & priv
->desc_types
) &&
3099 (1 << PRIMARY_EU(desc_hdr_template
) & priv
->exec_units
);
3101 if (SECONDARY_EU(desc_hdr_template
))
3102 ret
= ret
&& (1 << SECONDARY_EU(desc_hdr_template
)
3103 & priv
->exec_units
);
3108 static int talitos_remove(struct platform_device
*ofdev
)
3110 struct device
*dev
= &ofdev
->dev
;
3111 struct talitos_private
*priv
= dev_get_drvdata(dev
);
3112 struct talitos_crypto_alg
*t_alg
, *n
;
3115 list_for_each_entry_safe(t_alg
, n
, &priv
->alg_list
, entry
) {
3116 switch (t_alg
->algt
.type
) {
3117 case CRYPTO_ALG_TYPE_ABLKCIPHER
:
3119 case CRYPTO_ALG_TYPE_AEAD
:
3120 crypto_unregister_aead(&t_alg
->algt
.alg
.aead
);
3121 case CRYPTO_ALG_TYPE_AHASH
:
3122 crypto_unregister_ahash(&t_alg
->algt
.alg
.hash
);
3125 list_del(&t_alg
->entry
);
3128 if (hw_supports(dev
, DESC_HDR_SEL0_RNG
))
3129 talitos_unregister_rng(dev
);
3131 for (i
= 0; i
< 2; i
++)
3133 free_irq(priv
->irq
[i
], dev
);
3134 irq_dispose_mapping(priv
->irq
[i
]);
3137 tasklet_kill(&priv
->done_task
[0]);
3139 tasklet_kill(&priv
->done_task
[1]);
3144 static struct talitos_crypto_alg
*talitos_alg_alloc(struct device
*dev
,
3145 struct talitos_alg_template
3148 struct talitos_private
*priv
= dev_get_drvdata(dev
);
3149 struct talitos_crypto_alg
*t_alg
;
3150 struct crypto_alg
*alg
;
3152 t_alg
= devm_kzalloc(dev
, sizeof(struct talitos_crypto_alg
),
3155 return ERR_PTR(-ENOMEM
);
3157 t_alg
->algt
= *template;
3159 switch (t_alg
->algt
.type
) {
3160 case CRYPTO_ALG_TYPE_ABLKCIPHER
:
3161 alg
= &t_alg
->algt
.alg
.crypto
;
3162 alg
->cra_init
= talitos_cra_init
;
3163 alg
->cra_exit
= talitos_cra_exit
;
3164 alg
->cra_type
= &crypto_ablkcipher_type
;
3165 alg
->cra_ablkcipher
.setkey
= ablkcipher_setkey
;
3166 alg
->cra_ablkcipher
.encrypt
= ablkcipher_encrypt
;
3167 alg
->cra_ablkcipher
.decrypt
= ablkcipher_decrypt
;
3168 alg
->cra_ablkcipher
.geniv
= "eseqiv";
3170 case CRYPTO_ALG_TYPE_AEAD
:
3171 alg
= &t_alg
->algt
.alg
.aead
.base
;
3172 alg
->cra_exit
= talitos_cra_exit
;
3173 t_alg
->algt
.alg
.aead
.init
= talitos_cra_init_aead
;
3174 t_alg
->algt
.alg
.aead
.setkey
= aead_setkey
;
3175 t_alg
->algt
.alg
.aead
.encrypt
= aead_encrypt
;
3176 t_alg
->algt
.alg
.aead
.decrypt
= aead_decrypt
;
3177 if (!(priv
->features
& TALITOS_FTR_SHA224_HWINIT
) &&
3178 !strncmp(alg
->cra_name
, "authenc(hmac(sha224)", 20)) {
3179 devm_kfree(dev
, t_alg
);
3180 return ERR_PTR(-ENOTSUPP
);
3183 case CRYPTO_ALG_TYPE_AHASH
:
3184 alg
= &t_alg
->algt
.alg
.hash
.halg
.base
;
3185 alg
->cra_init
= talitos_cra_init_ahash
;
3186 alg
->cra_exit
= talitos_cra_exit_ahash
;
3187 alg
->cra_type
= &crypto_ahash_type
;
3188 t_alg
->algt
.alg
.hash
.init
= ahash_init
;
3189 t_alg
->algt
.alg
.hash
.update
= ahash_update
;
3190 t_alg
->algt
.alg
.hash
.final
= ahash_final
;
3191 t_alg
->algt
.alg
.hash
.finup
= ahash_finup
;
3192 t_alg
->algt
.alg
.hash
.digest
= ahash_digest
;
3193 if (!strncmp(alg
->cra_name
, "hmac", 4))
3194 t_alg
->algt
.alg
.hash
.setkey
= ahash_setkey
;
3195 t_alg
->algt
.alg
.hash
.import
= ahash_import
;
3196 t_alg
->algt
.alg
.hash
.export
= ahash_export
;
3198 if (!(priv
->features
& TALITOS_FTR_HMAC_OK
) &&
3199 !strncmp(alg
->cra_name
, "hmac", 4)) {
3200 devm_kfree(dev
, t_alg
);
3201 return ERR_PTR(-ENOTSUPP
);
3203 if (!(priv
->features
& TALITOS_FTR_SHA224_HWINIT
) &&
3204 (!strcmp(alg
->cra_name
, "sha224") ||
3205 !strcmp(alg
->cra_name
, "hmac(sha224)"))) {
3206 t_alg
->algt
.alg
.hash
.init
= ahash_init_sha224_swinit
;
3207 t_alg
->algt
.desc_hdr_template
=
3208 DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU
|
3209 DESC_HDR_SEL0_MDEUA
|
3210 DESC_HDR_MODE0_MDEU_SHA256
;
3214 dev_err(dev
, "unknown algorithm type %d\n", t_alg
->algt
.type
);
3215 devm_kfree(dev
, t_alg
);
3216 return ERR_PTR(-EINVAL
);
3219 alg
->cra_module
= THIS_MODULE
;
3220 if (t_alg
->algt
.priority
)
3221 alg
->cra_priority
= t_alg
->algt
.priority
;
3223 alg
->cra_priority
= TALITOS_CRA_PRIORITY
;
3224 alg
->cra_alignmask
= 0;
3225 alg
->cra_ctxsize
= sizeof(struct talitos_ctx
);
3226 alg
->cra_flags
|= CRYPTO_ALG_KERN_DRIVER_ONLY
;
3233 static int talitos_probe_irq(struct platform_device
*ofdev
)
3235 struct device
*dev
= &ofdev
->dev
;
3236 struct device_node
*np
= ofdev
->dev
.of_node
;
3237 struct talitos_private
*priv
= dev_get_drvdata(dev
);
3239 bool is_sec1
= has_ftr_sec1(priv
);
3241 priv
->irq
[0] = irq_of_parse_and_map(np
, 0);
3242 if (!priv
->irq
[0]) {
3243 dev_err(dev
, "failed to map irq\n");
3247 err
= request_irq(priv
->irq
[0], talitos1_interrupt_4ch
, 0,
3248 dev_driver_string(dev
), dev
);
3252 priv
->irq
[1] = irq_of_parse_and_map(np
, 1);
3254 /* get the primary irq line */
3255 if (!priv
->irq
[1]) {
3256 err
= request_irq(priv
->irq
[0], talitos2_interrupt_4ch
, 0,
3257 dev_driver_string(dev
), dev
);
3261 err
= request_irq(priv
->irq
[0], talitos2_interrupt_ch0_2
, 0,
3262 dev_driver_string(dev
), dev
);
3266 /* get the secondary irq line */
3267 err
= request_irq(priv
->irq
[1], talitos2_interrupt_ch1_3
, 0,
3268 dev_driver_string(dev
), dev
);
3270 dev_err(dev
, "failed to request secondary irq\n");
3271 irq_dispose_mapping(priv
->irq
[1]);
3279 dev_err(dev
, "failed to request primary irq\n");
3280 irq_dispose_mapping(priv
->irq
[0]);
3287 static int talitos_probe(struct platform_device
*ofdev
)
3289 struct device
*dev
= &ofdev
->dev
;
3290 struct device_node
*np
= ofdev
->dev
.of_node
;
3291 struct talitos_private
*priv
;
3294 struct resource
*res
;
3296 priv
= devm_kzalloc(dev
, sizeof(struct talitos_private
), GFP_KERNEL
);
3300 INIT_LIST_HEAD(&priv
->alg_list
);
3302 dev_set_drvdata(dev
, priv
);
3304 priv
->ofdev
= ofdev
;
3306 spin_lock_init(&priv
->reg_lock
);
3308 res
= platform_get_resource(ofdev
, IORESOURCE_MEM
, 0);
3311 priv
->reg
= devm_ioremap(dev
, res
->start
, resource_size(res
));
3313 dev_err(dev
, "failed to of_iomap\n");
3318 /* get SEC version capabilities from device tree */
3319 of_property_read_u32(np
, "fsl,num-channels", &priv
->num_channels
);
3320 of_property_read_u32(np
, "fsl,channel-fifo-len", &priv
->chfifo_len
);
3321 of_property_read_u32(np
, "fsl,exec-units-mask", &priv
->exec_units
);
3322 of_property_read_u32(np
, "fsl,descriptor-types-mask",
3325 if (!is_power_of_2(priv
->num_channels
) || !priv
->chfifo_len
||
3326 !priv
->exec_units
|| !priv
->desc_types
) {
3327 dev_err(dev
, "invalid property data in device tree node\n");
3332 if (of_device_is_compatible(np
, "fsl,sec3.0"))
3333 priv
->features
|= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT
;
3335 if (of_device_is_compatible(np
, "fsl,sec2.1"))
3336 priv
->features
|= TALITOS_FTR_HW_AUTH_CHECK
|
3337 TALITOS_FTR_SHA224_HWINIT
|
3338 TALITOS_FTR_HMAC_OK
;
3340 if (of_device_is_compatible(np
, "fsl,sec1.0"))
3341 priv
->features
|= TALITOS_FTR_SEC1
;
3343 if (of_device_is_compatible(np
, "fsl,sec1.2")) {
3344 priv
->reg_deu
= priv
->reg
+ TALITOS12_DEU
;
3345 priv
->reg_aesu
= priv
->reg
+ TALITOS12_AESU
;
3346 priv
->reg_mdeu
= priv
->reg
+ TALITOS12_MDEU
;
3347 stride
= TALITOS1_CH_STRIDE
;
3348 } else if (of_device_is_compatible(np
, "fsl,sec1.0")) {
3349 priv
->reg_deu
= priv
->reg
+ TALITOS10_DEU
;
3350 priv
->reg_aesu
= priv
->reg
+ TALITOS10_AESU
;
3351 priv
->reg_mdeu
= priv
->reg
+ TALITOS10_MDEU
;
3352 priv
->reg_afeu
= priv
->reg
+ TALITOS10_AFEU
;
3353 priv
->reg_rngu
= priv
->reg
+ TALITOS10_RNGU
;
3354 priv
->reg_pkeu
= priv
->reg
+ TALITOS10_PKEU
;
3355 stride
= TALITOS1_CH_STRIDE
;
3357 priv
->reg_deu
= priv
->reg
+ TALITOS2_DEU
;
3358 priv
->reg_aesu
= priv
->reg
+ TALITOS2_AESU
;
3359 priv
->reg_mdeu
= priv
->reg
+ TALITOS2_MDEU
;
3360 priv
->reg_afeu
= priv
->reg
+ TALITOS2_AFEU
;
3361 priv
->reg_rngu
= priv
->reg
+ TALITOS2_RNGU
;
3362 priv
->reg_pkeu
= priv
->reg
+ TALITOS2_PKEU
;
3363 priv
->reg_keu
= priv
->reg
+ TALITOS2_KEU
;
3364 priv
->reg_crcu
= priv
->reg
+ TALITOS2_CRCU
;
3365 stride
= TALITOS2_CH_STRIDE
;
3368 err
= talitos_probe_irq(ofdev
);
3372 if (of_device_is_compatible(np
, "fsl,sec1.0")) {
3373 if (priv
->num_channels
== 1)
3374 tasklet_init(&priv
->done_task
[0], talitos1_done_ch0
,
3375 (unsigned long)dev
);
3377 tasklet_init(&priv
->done_task
[0], talitos1_done_4ch
,
3378 (unsigned long)dev
);
3381 tasklet_init(&priv
->done_task
[0], talitos2_done_ch0_2
,
3382 (unsigned long)dev
);
3383 tasklet_init(&priv
->done_task
[1], talitos2_done_ch1_3
,
3384 (unsigned long)dev
);
3385 } else if (priv
->num_channels
== 1) {
3386 tasklet_init(&priv
->done_task
[0], talitos2_done_ch0
,
3387 (unsigned long)dev
);
3389 tasklet_init(&priv
->done_task
[0], talitos2_done_4ch
,
3390 (unsigned long)dev
);
3394 priv
->chan
= devm_kzalloc(dev
, sizeof(struct talitos_channel
) *
3395 priv
->num_channels
, GFP_KERNEL
);
3397 dev_err(dev
, "failed to allocate channel management space\n");
3402 priv
->fifo_len
= roundup_pow_of_two(priv
->chfifo_len
);
3404 for (i
= 0; i
< priv
->num_channels
; i
++) {
3405 priv
->chan
[i
].reg
= priv
->reg
+ stride
* (i
+ 1);
3406 if (!priv
->irq
[1] || !(i
& 1))
3407 priv
->chan
[i
].reg
+= TALITOS_CH_BASE_OFFSET
;
3409 spin_lock_init(&priv
->chan
[i
].head_lock
);
3410 spin_lock_init(&priv
->chan
[i
].tail_lock
);
3412 priv
->chan
[i
].fifo
= devm_kzalloc(dev
,
3413 sizeof(struct talitos_request
) *
3414 priv
->fifo_len
, GFP_KERNEL
);
3415 if (!priv
->chan
[i
].fifo
) {
3416 dev_err(dev
, "failed to allocate request fifo %d\n", i
);
3421 atomic_set(&priv
->chan
[i
].submit_count
,
3422 -(priv
->chfifo_len
- 1));
3425 dma_set_mask(dev
, DMA_BIT_MASK(36));
3427 /* reset and initialize the h/w */
3428 err
= init_device(dev
);
3430 dev_err(dev
, "failed to initialize device\n");
3434 /* register the RNG, if available */
3435 if (hw_supports(dev
, DESC_HDR_SEL0_RNG
)) {
3436 err
= talitos_register_rng(dev
);
3438 dev_err(dev
, "failed to register hwrng: %d\n", err
);
3441 dev_info(dev
, "hwrng\n");
3444 /* register crypto algorithms the device supports */
3445 for (i
= 0; i
< ARRAY_SIZE(driver_algs
); i
++) {
3446 if (hw_supports(dev
, driver_algs
[i
].desc_hdr_template
)) {
3447 struct talitos_crypto_alg
*t_alg
;
3448 struct crypto_alg
*alg
= NULL
;
3450 t_alg
= talitos_alg_alloc(dev
, &driver_algs
[i
]);
3451 if (IS_ERR(t_alg
)) {
3452 err
= PTR_ERR(t_alg
);
3453 if (err
== -ENOTSUPP
)
3458 switch (t_alg
->algt
.type
) {
3459 case CRYPTO_ALG_TYPE_ABLKCIPHER
:
3460 err
= crypto_register_alg(
3461 &t_alg
->algt
.alg
.crypto
);
3462 alg
= &t_alg
->algt
.alg
.crypto
;
3465 case CRYPTO_ALG_TYPE_AEAD
:
3466 err
= crypto_register_aead(
3467 &t_alg
->algt
.alg
.aead
);
3468 alg
= &t_alg
->algt
.alg
.aead
.base
;
3471 case CRYPTO_ALG_TYPE_AHASH
:
3472 err
= crypto_register_ahash(
3473 &t_alg
->algt
.alg
.hash
);
3474 alg
= &t_alg
->algt
.alg
.hash
.halg
.base
;
3478 dev_err(dev
, "%s alg registration failed\n",
3479 alg
->cra_driver_name
);
3480 devm_kfree(dev
, t_alg
);
3482 list_add_tail(&t_alg
->entry
, &priv
->alg_list
);
3485 if (!list_empty(&priv
->alg_list
))
3486 dev_info(dev
, "%s algorithms registered in /proc/crypto\n",
3487 (char *)of_get_property(np
, "compatible", NULL
));
3492 talitos_remove(ofdev
);
3497 static const struct of_device_id talitos_match
[] = {
3498 #ifdef CONFIG_CRYPTO_DEV_TALITOS1
3500 .compatible
= "fsl,sec1.0",
3503 #ifdef CONFIG_CRYPTO_DEV_TALITOS2
3505 .compatible
= "fsl,sec2.0",
3510 MODULE_DEVICE_TABLE(of
, talitos_match
);
3512 static struct platform_driver talitos_driver
= {
3515 .of_match_table
= talitos_match
,
3517 .probe
= talitos_probe
,
3518 .remove
= talitos_remove
,
3521 module_platform_driver(talitos_driver
);
3523 MODULE_LICENSE("GPL");
3524 MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
3525 MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");