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,
113 dma_addr_t dma_addr = dma_map_single_attrs(dev, data, len, dir, attrs);
114 struct talitos_private *priv = dev_get_drvdata(dev);
115 bool is_sec1 = has_ftr_sec1(priv);
117 to_talitos_ptr(ptr, dma_addr, len, is_sec1);
120 static void map_single_talitos_ptr(struct device *dev,
121 struct talitos_ptr *ptr,
122 unsigned int len, void *data,
123 enum dma_data_direction dir)
125 __map_single_talitos_ptr(dev, ptr, len, data, dir, 0);
128 static void map_single_talitos_ptr_nosync(struct device *dev,
129 struct talitos_ptr *ptr,
130 unsigned int len, void *data,
131 enum dma_data_direction dir)
133 __map_single_talitos_ptr(dev, ptr, len, data, dir,
134 DMA_ATTR_SKIP_CPU_SYNC);
138 * unmap bus single (contiguous) h/w descriptor pointer
140 static void unmap_single_talitos_ptr(struct device *dev,
141 struct talitos_ptr *ptr,
142 enum dma_data_direction dir)
144 struct talitos_private *priv = dev_get_drvdata(dev);
145 bool is_sec1 = has_ftr_sec1(priv);
147 dma_unmap_single(dev, be32_to_cpu(ptr->ptr),
148 from_talitos_ptr_len(ptr, is_sec1), dir);
151 static int reset_channel(struct device *dev, int ch)
153 struct talitos_private *priv = dev_get_drvdata(dev);
154 unsigned int timeout = TALITOS_TIMEOUT;
155 bool is_sec1 = has_ftr_sec1(priv);
158 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO,
159 TALITOS1_CCCR_LO_RESET);
161 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR_LO) &
162 TALITOS1_CCCR_LO_RESET) && --timeout)
165 setbits32(priv->chan[ch].reg + TALITOS_CCCR,
166 TALITOS2_CCCR_RESET);
168 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) &
169 TALITOS2_CCCR_RESET) && --timeout)
174 dev_err(dev, "failed to reset channel %d\n", ch);
178 /* set 36-bit addressing, done writeback enable and done IRQ enable */
179 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, TALITOS_CCCR_LO_EAE |
180 TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
181 /* enable chaining descriptors */
183 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO,
186 /* and ICCR writeback, if available */
187 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
188 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO,
189 TALITOS_CCCR_LO_IWSE);
194 static int reset_device(struct device *dev)
196 struct talitos_private *priv = dev_get_drvdata(dev);
197 unsigned int timeout = TALITOS_TIMEOUT;
198 bool is_sec1 = has_ftr_sec1(priv);
199 u32 mcr = is_sec1 ? TALITOS1_MCR_SWR : TALITOS2_MCR_SWR;
201 setbits32(priv->reg + TALITOS_MCR, mcr);
203 while ((in_be32(priv->reg + TALITOS_MCR) & mcr)
208 mcr = TALITOS_MCR_RCA1 | TALITOS_MCR_RCA3;
209 setbits32(priv->reg + TALITOS_MCR, mcr);
213 dev_err(dev, "failed to reset device\n");
221 * Reset and initialize the device
223 static int init_device(struct device *dev)
225 struct talitos_private *priv = dev_get_drvdata(dev);
227 bool is_sec1 = has_ftr_sec1(priv);
231 * errata documentation: warning: certain SEC interrupts
232 * are not fully cleared by writing the MCR:SWR bit,
233 * set bit twice to completely reset
235 err = reset_device(dev);
239 err = reset_device(dev);
244 for (ch = 0; ch < priv->num_channels; ch++) {
245 err = reset_channel(dev, ch);
250 /* enable channel done and error interrupts */
252 clrbits32(priv->reg + TALITOS_IMR, TALITOS1_IMR_INIT);
253 clrbits32(priv->reg + TALITOS_IMR_LO, TALITOS1_IMR_LO_INIT);
254 /* disable parity error check in DEU (erroneous? test vect.) */
255 setbits32(priv->reg_deu + TALITOS_EUICR, TALITOS1_DEUICR_KPE);
257 setbits32(priv->reg + TALITOS_IMR, TALITOS2_IMR_INIT);
258 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS2_IMR_LO_INIT);
261 /* disable integrity check error interrupts (use writeback instead) */
262 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
263 setbits32(priv->reg_mdeu + TALITOS_EUICR_LO,
264 TALITOS_MDEUICR_LO_ICE);
270 * talitos_submit - submits a descriptor to the device for processing
271 * @dev: the SEC device to be used
272 * @ch: the SEC device channel to be used
273 * @desc: the descriptor to be processed by the device
274 * @callback: whom to call when processing is complete
275 * @context: a handle for use by caller (optional)
277 * desc must contain valid dma-mapped (bus physical) address pointers.
278 * callback must check err and feedback in descriptor header
279 * for device processing status.
281 int talitos_submit(struct device *dev, int ch, struct talitos_desc *desc,
282 void (*callback)(struct device *dev,
283 struct talitos_desc *desc,
284 void *context, int error),
287 struct talitos_private *priv = dev_get_drvdata(dev);
288 struct talitos_request *request;
291 bool is_sec1 = has_ftr_sec1(priv);
293 spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
295 if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
296 /* h/w fifo is full */
297 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
301 head = priv->chan[ch].head;
302 request = &priv->chan[ch].fifo[head];
304 /* map descriptor and save caller data */
306 desc->hdr1 = desc->hdr;
307 request->dma_desc = dma_map_single(dev, &desc->hdr1,
311 request->dma_desc = dma_map_single(dev, desc,
315 request->callback = callback;
316 request->context = context;
318 /* increment fifo head */
319 priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
322 request->desc = desc;
326 out_be32(priv->chan[ch].reg + TALITOS_FF,
327 upper_32_bits(request->dma_desc));
328 out_be32(priv->chan[ch].reg + TALITOS_FF_LO,
329 lower_32_bits(request->dma_desc));
331 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
335 EXPORT_SYMBOL(talitos_submit);
337 static __be32 get_request_hdr(struct talitos_request *request, bool is_sec1)
339 struct talitos_edesc *edesc;
342 return request->desc->hdr;
344 if (!request->desc->next_desc)
345 return request->desc->hdr1;
347 edesc = container_of(request->desc, struct talitos_edesc, desc);
349 return ((struct talitos_desc *)(edesc->buf + edesc->dma_len))->hdr1;
353 * process what was done, notify callback of error if not
355 static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
357 struct talitos_private *priv = dev_get_drvdata(dev);
358 struct talitos_request *request, saved_req;
361 bool is_sec1 = has_ftr_sec1(priv);
363 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
365 tail = priv->chan[ch].tail;
366 while (priv->chan[ch].fifo[tail].desc) {
369 request = &priv->chan[ch].fifo[tail];
371 /* descriptors with their done bits set don't get the error */
373 hdr = get_request_hdr(request, is_sec1);
375 if ((hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
383 dma_unmap_single(dev, request->dma_desc,
387 /* copy entries so we can call callback outside lock */
388 saved_req.desc = request->desc;
389 saved_req.callback = request->callback;
390 saved_req.context = request->context;
392 /* release request entry in fifo */
394 request->desc = NULL;
396 /* increment fifo tail */
397 priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
399 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
401 atomic_dec(&priv->chan[ch].submit_count);
403 saved_req.callback(dev, saved_req.desc, saved_req.context,
405 /* channel may resume processing in single desc error case */
406 if (error && !reset_ch && status == error)
408 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
409 tail = priv->chan[ch].tail;
412 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
416 * process completed requests for channels that have done status
418 #define DEF_TALITOS1_DONE(name, ch_done_mask) \
419 static void talitos1_done_##name(unsigned long data) \
421 struct device *dev = (struct device *)data; \
422 struct talitos_private *priv = dev_get_drvdata(dev); \
423 unsigned long flags; \
425 if (ch_done_mask & 0x10000000) \
426 flush_channel(dev, 0, 0, 0); \
427 if (ch_done_mask & 0x40000000) \
428 flush_channel(dev, 1, 0, 0); \
429 if (ch_done_mask & 0x00010000) \
430 flush_channel(dev, 2, 0, 0); \
431 if (ch_done_mask & 0x00040000) \
432 flush_channel(dev, 3, 0, 0); \
434 /* At this point, all completed channels have been processed */ \
435 /* Unmask done interrupts for channels completed later on. */ \
436 spin_lock_irqsave(&priv->reg_lock, flags); \
437 clrbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
438 clrbits32(priv->reg + TALITOS_IMR_LO, TALITOS1_IMR_LO_INIT); \
439 spin_unlock_irqrestore(&priv->reg_lock, flags); \
442 DEF_TALITOS1_DONE(4ch, TALITOS1_ISR_4CHDONE)
443 DEF_TALITOS1_DONE(ch0, TALITOS1_ISR_CH_0_DONE)
445 #define DEF_TALITOS2_DONE(name, ch_done_mask) \
446 static void talitos2_done_##name(unsigned long data) \
448 struct device *dev = (struct device *)data; \
449 struct talitos_private *priv = dev_get_drvdata(dev); \
450 unsigned long flags; \
452 if (ch_done_mask & 1) \
453 flush_channel(dev, 0, 0, 0); \
454 if (ch_done_mask & (1 << 2)) \
455 flush_channel(dev, 1, 0, 0); \
456 if (ch_done_mask & (1 << 4)) \
457 flush_channel(dev, 2, 0, 0); \
458 if (ch_done_mask & (1 << 6)) \
459 flush_channel(dev, 3, 0, 0); \
461 /* At this point, all completed channels have been processed */ \
462 /* Unmask done interrupts for channels completed later on. */ \
463 spin_lock_irqsave(&priv->reg_lock, flags); \
464 setbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
465 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS2_IMR_LO_INIT); \
466 spin_unlock_irqrestore(&priv->reg_lock, flags); \
469 DEF_TALITOS2_DONE(4ch, TALITOS2_ISR_4CHDONE)
470 DEF_TALITOS2_DONE(ch0, TALITOS2_ISR_CH_0_DONE)
471 DEF_TALITOS2_DONE(ch0_2, TALITOS2_ISR_CH_0_2_DONE)
472 DEF_TALITOS2_DONE(ch1_3, TALITOS2_ISR_CH_1_3_DONE)
475 * locate current (offending) descriptor
477 static __be32 current_desc_hdr(struct device *dev, int ch)
479 struct talitos_private *priv = dev_get_drvdata(dev);
483 cur_desc = ((u64)in_be32(priv->chan[ch].reg + TALITOS_CDPR)) << 32;
484 cur_desc |= in_be32(priv->chan[ch].reg + TALITOS_CDPR_LO);
487 dev_err(dev, "CDPR is NULL, giving up search for offending descriptor\n");
491 tail = priv->chan[ch].tail;
494 while (priv->chan[ch].fifo[iter].dma_desc != cur_desc &&
495 priv->chan[ch].fifo[iter].desc->next_desc != cpu_to_be32(cur_desc)) {
496 iter = (iter + 1) & (priv->fifo_len - 1);
498 dev_err(dev, "couldn't locate current descriptor\n");
503 if (priv->chan[ch].fifo[iter].desc->next_desc == cpu_to_be32(cur_desc)) {
504 struct talitos_edesc *edesc;
506 edesc = container_of(priv->chan[ch].fifo[iter].desc,
507 struct talitos_edesc, desc);
508 return ((struct talitos_desc *)
509 (edesc->buf + edesc->dma_len))->hdr;
512 return priv->chan[ch].fifo[iter].desc->hdr;
516 * user diagnostics; report root cause of error based on execution unit status
518 static void report_eu_error(struct device *dev, int ch, __be32 desc_hdr)
520 struct talitos_private *priv = dev_get_drvdata(dev);
524 desc_hdr = cpu_to_be32(in_be32(priv->chan[ch].reg + TALITOS_DESCBUF));
526 switch (desc_hdr & DESC_HDR_SEL0_MASK) {
527 case DESC_HDR_SEL0_AFEU:
528 dev_err(dev, "AFEUISR 0x%08x_%08x\n",
529 in_be32(priv->reg_afeu + TALITOS_EUISR),
530 in_be32(priv->reg_afeu + TALITOS_EUISR_LO));
532 case DESC_HDR_SEL0_DEU:
533 dev_err(dev, "DEUISR 0x%08x_%08x\n",
534 in_be32(priv->reg_deu + TALITOS_EUISR),
535 in_be32(priv->reg_deu + TALITOS_EUISR_LO));
537 case DESC_HDR_SEL0_MDEUA:
538 case DESC_HDR_SEL0_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_SEL0_RNG:
544 dev_err(dev, "RNGUISR 0x%08x_%08x\n",
545 in_be32(priv->reg_rngu + TALITOS_ISR),
546 in_be32(priv->reg_rngu + TALITOS_ISR_LO));
548 case DESC_HDR_SEL0_PKEU:
549 dev_err(dev, "PKEUISR 0x%08x_%08x\n",
550 in_be32(priv->reg_pkeu + TALITOS_EUISR),
551 in_be32(priv->reg_pkeu + TALITOS_EUISR_LO));
553 case DESC_HDR_SEL0_AESU:
554 dev_err(dev, "AESUISR 0x%08x_%08x\n",
555 in_be32(priv->reg_aesu + TALITOS_EUISR),
556 in_be32(priv->reg_aesu + TALITOS_EUISR_LO));
558 case DESC_HDR_SEL0_CRCU:
559 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
560 in_be32(priv->reg_crcu + TALITOS_EUISR),
561 in_be32(priv->reg_crcu + TALITOS_EUISR_LO));
563 case DESC_HDR_SEL0_KEU:
564 dev_err(dev, "KEUISR 0x%08x_%08x\n",
565 in_be32(priv->reg_pkeu + TALITOS_EUISR),
566 in_be32(priv->reg_pkeu + TALITOS_EUISR_LO));
570 switch (desc_hdr & DESC_HDR_SEL1_MASK) {
571 case DESC_HDR_SEL1_MDEUA:
572 case DESC_HDR_SEL1_MDEUB:
573 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
574 in_be32(priv->reg_mdeu + TALITOS_EUISR),
575 in_be32(priv->reg_mdeu + TALITOS_EUISR_LO));
577 case DESC_HDR_SEL1_CRCU:
578 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
579 in_be32(priv->reg_crcu + TALITOS_EUISR),
580 in_be32(priv->reg_crcu + TALITOS_EUISR_LO));
584 for (i = 0; i < 8; i++)
585 dev_err(dev, "DESCBUF 0x%08x_%08x\n",
586 in_be32(priv->chan[ch].reg + TALITOS_DESCBUF + 8*i),
587 in_be32(priv->chan[ch].reg + TALITOS_DESCBUF_LO + 8*i));
591 * recover from error interrupts
593 static void talitos_error(struct device *dev, u32 isr, u32 isr_lo)
595 struct talitos_private *priv = dev_get_drvdata(dev);
596 unsigned int timeout = TALITOS_TIMEOUT;
597 int ch, error, reset_dev = 0;
599 bool is_sec1 = has_ftr_sec1(priv);
600 int reset_ch = is_sec1 ? 1 : 0; /* only SEC2 supports continuation */
602 for (ch = 0; ch < priv->num_channels; ch++) {
603 /* skip channels without errors */
605 /* bits 29, 31, 17, 19 */
606 if (!(isr & (1 << (29 + (ch & 1) * 2 - (ch & 2) * 6))))
609 if (!(isr & (1 << (ch * 2 + 1))))
615 v_lo = in_be32(priv->chan[ch].reg + TALITOS_CCPSR_LO);
617 if (v_lo & TALITOS_CCPSR_LO_DOF) {
618 dev_err(dev, "double fetch fifo overflow error\n");
622 if (v_lo & TALITOS_CCPSR_LO_SOF) {
623 /* h/w dropped descriptor */
624 dev_err(dev, "single fetch fifo overflow error\n");
627 if (v_lo & TALITOS_CCPSR_LO_MDTE)
628 dev_err(dev, "master data transfer error\n");
629 if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
630 dev_err(dev, is_sec1 ? "pointer not complete error\n"
631 : "s/g data length zero error\n");
632 if (v_lo & TALITOS_CCPSR_LO_FPZ)
633 dev_err(dev, is_sec1 ? "parity error\n"
634 : "fetch pointer zero error\n");
635 if (v_lo & TALITOS_CCPSR_LO_IDH)
636 dev_err(dev, "illegal descriptor header error\n");
637 if (v_lo & TALITOS_CCPSR_LO_IEU)
638 dev_err(dev, is_sec1 ? "static assignment error\n"
639 : "invalid exec unit error\n");
640 if (v_lo & TALITOS_CCPSR_LO_EU)
641 report_eu_error(dev, ch, current_desc_hdr(dev, ch));
643 if (v_lo & TALITOS_CCPSR_LO_GB)
644 dev_err(dev, "gather boundary error\n");
645 if (v_lo & TALITOS_CCPSR_LO_GRL)
646 dev_err(dev, "gather return/length error\n");
647 if (v_lo & TALITOS_CCPSR_LO_SB)
648 dev_err(dev, "scatter boundary error\n");
649 if (v_lo & TALITOS_CCPSR_LO_SRL)
650 dev_err(dev, "scatter return/length error\n");
653 flush_channel(dev, ch, error, reset_ch);
656 reset_channel(dev, ch);
658 setbits32(priv->chan[ch].reg + TALITOS_CCCR,
660 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, 0);
661 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) &
662 TALITOS2_CCCR_CONT) && --timeout)
665 dev_err(dev, "failed to restart channel %d\n",
671 if (reset_dev || (is_sec1 && isr & ~TALITOS1_ISR_4CHERR) ||
672 (!is_sec1 && isr & ~TALITOS2_ISR_4CHERR) || isr_lo) {
673 if (is_sec1 && (isr_lo & TALITOS1_ISR_TEA_ERR))
674 dev_err(dev, "TEA error: ISR 0x%08x_%08x\n",
677 dev_err(dev, "done overflow, internal time out, or "
678 "rngu error: ISR 0x%08x_%08x\n", isr, isr_lo);
680 /* purge request queues */
681 for (ch = 0; ch < priv->num_channels; ch++)
682 flush_channel(dev, ch, -EIO, 1);
684 /* reset and reinitialize the device */
689 #define DEF_TALITOS1_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet) \
690 static irqreturn_t talitos1_interrupt_##name(int irq, void *data) \
692 struct device *dev = data; \
693 struct talitos_private *priv = dev_get_drvdata(dev); \
695 unsigned long flags; \
697 spin_lock_irqsave(&priv->reg_lock, flags); \
698 isr = in_be32(priv->reg + TALITOS_ISR); \
699 isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); \
700 /* Acknowledge interrupt */ \
701 out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
702 out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); \
704 if (unlikely(isr & ch_err_mask || isr_lo & TALITOS1_IMR_LO_INIT)) { \
705 spin_unlock_irqrestore(&priv->reg_lock, flags); \
706 talitos_error(dev, isr & ch_err_mask, isr_lo); \
709 if (likely(isr & ch_done_mask)) { \
710 /* mask further done interrupts. */ \
711 setbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
712 /* done_task will unmask done interrupts at exit */ \
713 tasklet_schedule(&priv->done_task[tlet]); \
715 spin_unlock_irqrestore(&priv->reg_lock, flags); \
718 return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED : \
722 DEF_TALITOS1_INTERRUPT(4ch, TALITOS1_ISR_4CHDONE, TALITOS1_ISR_4CHERR, 0)
724 #define DEF_TALITOS2_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet) \
725 static irqreturn_t talitos2_interrupt_##name(int irq, void *data) \
727 struct device *dev = data; \
728 struct talitos_private *priv = dev_get_drvdata(dev); \
730 unsigned long flags; \
732 spin_lock_irqsave(&priv->reg_lock, flags); \
733 isr = in_be32(priv->reg + TALITOS_ISR); \
734 isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); \
735 /* Acknowledge interrupt */ \
736 out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
737 out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); \
739 if (unlikely(isr & ch_err_mask || isr_lo)) { \
740 spin_unlock_irqrestore(&priv->reg_lock, flags); \
741 talitos_error(dev, isr & ch_err_mask, isr_lo); \
744 if (likely(isr & ch_done_mask)) { \
745 /* mask further done interrupts. */ \
746 clrbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
747 /* done_task will unmask done interrupts at exit */ \
748 tasklet_schedule(&priv->done_task[tlet]); \
750 spin_unlock_irqrestore(&priv->reg_lock, flags); \
753 return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED : \
757 DEF_TALITOS2_INTERRUPT(4ch, TALITOS2_ISR_4CHDONE, TALITOS2_ISR_4CHERR, 0)
758 DEF_TALITOS2_INTERRUPT(ch0_2, TALITOS2_ISR_CH_0_2_DONE, TALITOS2_ISR_CH_0_2_ERR,
760 DEF_TALITOS2_INTERRUPT(ch1_3, TALITOS2_ISR_CH_1_3_DONE, TALITOS2_ISR_CH_1_3_ERR,
766 static int talitos_rng_data_present(struct hwrng *rng, int wait)
768 struct device *dev = (struct device *)rng->priv;
769 struct talitos_private *priv = dev_get_drvdata(dev);
773 for (i = 0; i < 20; i++) {
774 ofl = in_be32(priv->reg_rngu + TALITOS_EUSR_LO) &
775 TALITOS_RNGUSR_LO_OFL;
784 static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
786 struct device *dev = (struct device *)rng->priv;
787 struct talitos_private *priv = dev_get_drvdata(dev);
789 /* rng fifo requires 64-bit accesses */
790 *data = in_be32(priv->reg_rngu + TALITOS_EU_FIFO);
791 *data = in_be32(priv->reg_rngu + TALITOS_EU_FIFO_LO);
796 static int talitos_rng_init(struct hwrng *rng)
798 struct device *dev = (struct device *)rng->priv;
799 struct talitos_private *priv = dev_get_drvdata(dev);
800 unsigned int timeout = TALITOS_TIMEOUT;
802 setbits32(priv->reg_rngu + TALITOS_EURCR_LO, TALITOS_RNGURCR_LO_SR);
803 while (!(in_be32(priv->reg_rngu + TALITOS_EUSR_LO)
804 & TALITOS_RNGUSR_LO_RD)
808 dev_err(dev, "failed to reset rng hw\n");
812 /* start generating */
813 setbits32(priv->reg_rngu + TALITOS_EUDSR_LO, 0);
818 static int talitos_register_rng(struct device *dev)
820 struct talitos_private *priv = dev_get_drvdata(dev);
823 priv->rng.name = dev_driver_string(dev),
824 priv->rng.init = talitos_rng_init,
825 priv->rng.data_present = talitos_rng_data_present,
826 priv->rng.data_read = talitos_rng_data_read,
827 priv->rng.priv = (unsigned long)dev;
829 err = hwrng_register(&priv->rng);
831 priv->rng_registered = true;
836 static void talitos_unregister_rng(struct device *dev)
838 struct talitos_private *priv = dev_get_drvdata(dev);
840 if (!priv->rng_registered)
843 hwrng_unregister(&priv->rng);
844 priv->rng_registered = false;
850 #define TALITOS_CRA_PRIORITY 3000
852 * Defines a priority for doing AEAD with descriptors type
853 * HMAC_SNOOP_NO_AFEA (HSNA) instead of type IPSEC_ESP
855 #define TALITOS_CRA_PRIORITY_AEAD_HSNA (TALITOS_CRA_PRIORITY - 1)
856 #ifdef CONFIG_CRYPTO_DEV_TALITOS2
857 #define TALITOS_MAX_KEY_SIZE (AES_MAX_KEY_SIZE + SHA512_BLOCK_SIZE)
859 #define TALITOS_MAX_KEY_SIZE (AES_MAX_KEY_SIZE + SHA256_BLOCK_SIZE)
861 #define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
866 __be32 desc_hdr_template;
867 u8 key[TALITOS_MAX_KEY_SIZE];
868 u8 iv[TALITOS_MAX_IV_LENGTH];
871 unsigned int enckeylen;
872 unsigned int authkeylen;
875 #define HASH_MAX_BLOCK_SIZE SHA512_BLOCK_SIZE
876 #define TALITOS_MDEU_MAX_CONTEXT_SIZE TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
878 struct talitos_ahash_req_ctx {
879 u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
880 unsigned int hw_context_size;
881 u8 buf[2][HASH_MAX_BLOCK_SIZE];
886 unsigned int to_hash_later;
888 struct scatterlist bufsl[2];
889 struct scatterlist *psrc;
892 struct talitos_export_state {
893 u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
894 u8 buf[HASH_MAX_BLOCK_SIZE];
898 unsigned int to_hash_later;
902 static int aead_setkey(struct crypto_aead *authenc,
903 const u8 *key, unsigned int keylen)
905 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
906 struct device *dev = ctx->dev;
907 struct crypto_authenc_keys keys;
909 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
912 if (keys.authkeylen + keys.enckeylen > TALITOS_MAX_KEY_SIZE)
916 dma_unmap_single(dev, ctx->dma_key, ctx->keylen, DMA_TO_DEVICE);
918 memcpy(ctx->key, keys.authkey, keys.authkeylen);
919 memcpy(&ctx->key[keys.authkeylen], keys.enckey, keys.enckeylen);
921 ctx->keylen = keys.authkeylen + keys.enckeylen;
922 ctx->enckeylen = keys.enckeylen;
923 ctx->authkeylen = keys.authkeylen;
924 ctx->dma_key = dma_map_single(dev, ctx->key, ctx->keylen,
927 memzero_explicit(&keys, sizeof(keys));
931 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
932 memzero_explicit(&keys, sizeof(keys));
936 static void talitos_sg_unmap(struct device *dev,
937 struct talitos_edesc *edesc,
938 struct scatterlist *src,
939 struct scatterlist *dst,
940 unsigned int len, unsigned int offset)
942 struct talitos_private *priv = dev_get_drvdata(dev);
943 bool is_sec1 = has_ftr_sec1(priv);
944 unsigned int src_nents = edesc->src_nents ? : 1;
945 unsigned int dst_nents = edesc->dst_nents ? : 1;
947 if (is_sec1 && dst && dst_nents > 1) {
948 dma_sync_single_for_device(dev, edesc->dma_link_tbl + offset,
949 len, DMA_FROM_DEVICE);
950 sg_pcopy_from_buffer(dst, dst_nents, edesc->buf + offset, len,
954 if (src_nents == 1 || !is_sec1)
955 dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
957 if (dst && (dst_nents == 1 || !is_sec1))
958 dma_unmap_sg(dev, dst, dst_nents, DMA_FROM_DEVICE);
959 } else if (src_nents == 1 || !is_sec1) {
960 dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
964 static void ipsec_esp_unmap(struct device *dev,
965 struct talitos_edesc *edesc,
966 struct aead_request *areq, bool encrypt)
968 struct crypto_aead *aead = crypto_aead_reqtfm(areq);
969 struct talitos_ctx *ctx = crypto_aead_ctx(aead);
970 unsigned int ivsize = crypto_aead_ivsize(aead);
971 unsigned int authsize = crypto_aead_authsize(aead);
972 unsigned int cryptlen = areq->cryptlen - (encrypt ? 0 : authsize);
973 bool is_ipsec_esp = edesc->desc.hdr & DESC_HDR_TYPE_IPSEC_ESP;
974 struct talitos_ptr *civ_ptr = &edesc->desc.ptr[is_ipsec_esp ? 2 : 3];
977 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6],
979 unmap_single_talitos_ptr(dev, civ_ptr, DMA_TO_DEVICE);
981 talitos_sg_unmap(dev, edesc, areq->src, areq->dst,
982 cryptlen + authsize, areq->assoclen);
985 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
989 unsigned int dst_nents = edesc->dst_nents ? : 1;
991 sg_pcopy_to_buffer(areq->dst, dst_nents, ctx->iv, ivsize,
992 areq->assoclen + cryptlen - ivsize);
997 * ipsec_esp descriptor callbacks
999 static void ipsec_esp_encrypt_done(struct device *dev,
1000 struct talitos_desc *desc, void *context,
1003 struct aead_request *areq = context;
1004 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1005 unsigned int ivsize = crypto_aead_ivsize(authenc);
1006 struct talitos_edesc *edesc;
1008 edesc = container_of(desc, struct talitos_edesc, desc);
1010 ipsec_esp_unmap(dev, edesc, areq, true);
1012 dma_unmap_single(dev, edesc->iv_dma, ivsize, DMA_TO_DEVICE);
1016 aead_request_complete(areq, err);
1019 static void ipsec_esp_decrypt_swauth_done(struct device *dev,
1020 struct talitos_desc *desc,
1021 void *context, int err)
1023 struct aead_request *req = context;
1024 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1025 unsigned int authsize = crypto_aead_authsize(authenc);
1026 struct talitos_edesc *edesc;
1029 edesc = container_of(desc, struct talitos_edesc, desc);
1031 ipsec_esp_unmap(dev, edesc, req, false);
1035 oicv = edesc->buf + edesc->dma_len;
1036 icv = oicv - authsize;
1038 err = crypto_memneq(oicv, icv, authsize) ? -EBADMSG : 0;
1043 aead_request_complete(req, err);
1046 static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
1047 struct talitos_desc *desc,
1048 void *context, int err)
1050 struct aead_request *req = context;
1051 struct talitos_edesc *edesc;
1053 edesc = container_of(desc, struct talitos_edesc, desc);
1055 ipsec_esp_unmap(dev, edesc, req, false);
1057 /* check ICV auth status */
1058 if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
1059 DESC_HDR_LO_ICCR1_PASS))
1064 aead_request_complete(req, err);
1068 * convert scatterlist to SEC h/w link table format
1069 * stop at cryptlen bytes
1071 static int sg_to_link_tbl_offset(struct scatterlist *sg, int sg_count,
1072 unsigned int offset, int datalen, int elen,
1073 struct talitos_ptr *link_tbl_ptr, int align)
1075 int n_sg = elen ? sg_count + 1 : sg_count;
1077 int cryptlen = datalen + elen;
1078 int padding = ALIGN(cryptlen, align) - cryptlen;
1080 while (cryptlen && sg && n_sg--) {
1081 unsigned int len = sg_dma_len(sg);
1083 if (offset >= len) {
1093 if (datalen > 0 && len > datalen) {
1094 to_talitos_ptr(link_tbl_ptr + count,
1095 sg_dma_address(sg) + offset, datalen, 0);
1096 to_talitos_ptr_ext_set(link_tbl_ptr + count, 0, 0);
1101 to_talitos_ptr(link_tbl_ptr + count,
1102 sg_dma_address(sg) + offset, sg_next(sg) ? len : len + padding, 0);
1103 to_talitos_ptr_ext_set(link_tbl_ptr + count, 0, 0);
1113 /* tag end of link table */
1115 to_talitos_ptr_ext_set(link_tbl_ptr + count - 1,
1116 DESC_PTR_LNKTBL_RET, 0);
1121 static int talitos_sg_map_ext(struct device *dev, struct scatterlist *src,
1122 unsigned int len, struct talitos_edesc *edesc,
1123 struct talitos_ptr *ptr, int sg_count,
1124 unsigned int offset, int tbl_off, int elen,
1125 bool force, int align)
1127 struct talitos_private *priv = dev_get_drvdata(dev);
1128 bool is_sec1 = has_ftr_sec1(priv);
1129 int aligned_len = ALIGN(len, align);
1132 to_talitos_ptr(ptr, 0, 0, is_sec1);
1135 to_talitos_ptr_ext_set(ptr, elen, is_sec1);
1136 if (sg_count == 1 && !force) {
1137 to_talitos_ptr(ptr, sg_dma_address(src) + offset, aligned_len, is_sec1);
1141 to_talitos_ptr(ptr, edesc->dma_link_tbl + offset, aligned_len, is_sec1);
1144 sg_count = sg_to_link_tbl_offset(src, sg_count, offset, len, elen,
1145 &edesc->link_tbl[tbl_off], align);
1146 if (sg_count == 1 && !force) {
1147 /* Only one segment now, so no link tbl needed*/
1148 copy_talitos_ptr(ptr, &edesc->link_tbl[tbl_off], is_sec1);
1151 to_talitos_ptr(ptr, edesc->dma_link_tbl +
1152 tbl_off * sizeof(struct talitos_ptr), aligned_len, is_sec1);
1153 to_talitos_ptr_ext_or(ptr, DESC_PTR_LNKTBL_JUMP, is_sec1);
1158 static int talitos_sg_map(struct device *dev, struct scatterlist *src,
1159 unsigned int len, struct talitos_edesc *edesc,
1160 struct talitos_ptr *ptr, int sg_count,
1161 unsigned int offset, int tbl_off)
1163 return talitos_sg_map_ext(dev, src, len, edesc, ptr, sg_count, offset,
1164 tbl_off, 0, false, 1);
1168 * fill in and submit ipsec_esp descriptor
1170 static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
1172 void (*callback)(struct device *dev,
1173 struct talitos_desc *desc,
1174 void *context, int error))
1176 struct crypto_aead *aead = crypto_aead_reqtfm(areq);
1177 unsigned int authsize = crypto_aead_authsize(aead);
1178 struct talitos_ctx *ctx = crypto_aead_ctx(aead);
1179 struct device *dev = ctx->dev;
1180 struct talitos_desc *desc = &edesc->desc;
1181 unsigned int cryptlen = areq->cryptlen - (encrypt ? 0 : authsize);
1182 unsigned int ivsize = crypto_aead_ivsize(aead);
1186 bool sync_needed = false;
1187 struct talitos_private *priv = dev_get_drvdata(dev);
1188 bool is_sec1 = has_ftr_sec1(priv);
1189 bool is_ipsec_esp = desc->hdr & DESC_HDR_TYPE_IPSEC_ESP;
1190 struct talitos_ptr *civ_ptr = &desc->ptr[is_ipsec_esp ? 2 : 3];
1191 struct talitos_ptr *ckey_ptr = &desc->ptr[is_ipsec_esp ? 3 : 2];
1192 dma_addr_t dma_icv = edesc->dma_link_tbl + edesc->dma_len - authsize;
1195 to_talitos_ptr(&desc->ptr[0], ctx->dma_key, ctx->authkeylen, is_sec1);
1197 sg_count = edesc->src_nents ?: 1;
1198 if (is_sec1 && sg_count > 1)
1199 sg_copy_to_buffer(areq->src, sg_count, edesc->buf,
1200 areq->assoclen + cryptlen);
1202 sg_count = dma_map_sg(dev, areq->src, sg_count,
1203 (areq->src == areq->dst) ?
1204 DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1207 ret = talitos_sg_map(dev, areq->src, areq->assoclen, edesc,
1208 &desc->ptr[1], sg_count, 0, tbl_off);
1216 to_talitos_ptr(civ_ptr, edesc->iv_dma, ivsize, is_sec1);
1219 to_talitos_ptr(ckey_ptr, ctx->dma_key + ctx->authkeylen,
1220 ctx->enckeylen, is_sec1);
1224 * map and adjust cipher len to aead request cryptlen.
1225 * extent is bytes of HMAC postpended to ciphertext,
1226 * typically 12 for ipsec
1228 if (is_ipsec_esp && (desc->hdr & DESC_HDR_MODE1_MDEU_CICV))
1231 ret = talitos_sg_map_ext(dev, areq->src, cryptlen, edesc, &desc->ptr[4],
1232 sg_count, areq->assoclen, tbl_off, elen,
1241 if (areq->src != areq->dst) {
1242 sg_count = edesc->dst_nents ? : 1;
1243 if (!is_sec1 || sg_count == 1)
1244 dma_map_sg(dev, areq->dst, sg_count, DMA_FROM_DEVICE);
1247 if (is_ipsec_esp && encrypt)
1251 ret = talitos_sg_map_ext(dev, areq->dst, cryptlen, edesc, &desc->ptr[5],
1252 sg_count, areq->assoclen, tbl_off, elen,
1253 is_ipsec_esp && !encrypt, 1);
1257 edesc->icv_ool = !encrypt;
1259 if (!encrypt && is_ipsec_esp) {
1260 struct talitos_ptr *tbl_ptr = &edesc->link_tbl[tbl_off];
1262 /* Add an entry to the link table for ICV data */
1263 to_talitos_ptr_ext_set(tbl_ptr - 1, 0, is_sec1);
1264 to_talitos_ptr_ext_set(tbl_ptr, DESC_PTR_LNKTBL_RET, is_sec1);
1266 /* icv data follows link tables */
1267 to_talitos_ptr(tbl_ptr, dma_icv, authsize, is_sec1);
1268 to_talitos_ptr_ext_or(&desc->ptr[5], authsize, is_sec1);
1270 } else if (!encrypt) {
1271 to_talitos_ptr(&desc->ptr[6], dma_icv, authsize, is_sec1);
1273 } else if (!is_ipsec_esp) {
1274 talitos_sg_map(dev, areq->dst, authsize, edesc, &desc->ptr[6],
1275 sg_count, areq->assoclen + cryptlen, tbl_off);
1280 map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv,
1284 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1288 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
1289 if (ret != -EINPROGRESS) {
1290 ipsec_esp_unmap(dev, edesc, areq, encrypt);
1297 * allocate and map the extended descriptor
1299 static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
1300 struct scatterlist *src,
1301 struct scatterlist *dst,
1303 unsigned int assoclen,
1304 unsigned int cryptlen,
1305 unsigned int authsize,
1306 unsigned int ivsize,
1311 struct talitos_edesc *edesc;
1312 int src_nents, dst_nents, alloc_len, dma_len, src_len, dst_len;
1313 dma_addr_t iv_dma = 0;
1314 gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
1316 struct talitos_private *priv = dev_get_drvdata(dev);
1317 bool is_sec1 = has_ftr_sec1(priv);
1318 int max_len = is_sec1 ? TALITOS1_MAX_DATA_LEN : TALITOS2_MAX_DATA_LEN;
1320 if (cryptlen + authsize > max_len) {
1321 dev_err(dev, "length exceeds h/w max limit\n");
1322 return ERR_PTR(-EINVAL);
1325 if (!dst || dst == src) {
1326 src_len = assoclen + cryptlen + authsize;
1327 src_nents = sg_nents_for_len(src, src_len);
1328 if (src_nents < 0) {
1329 dev_err(dev, "Invalid number of src SG.\n");
1330 return ERR_PTR(-EINVAL);
1332 src_nents = (src_nents == 1) ? 0 : src_nents;
1333 dst_nents = dst ? src_nents : 0;
1335 } else { /* dst && dst != src*/
1336 src_len = assoclen + cryptlen + (encrypt ? 0 : authsize);
1337 src_nents = sg_nents_for_len(src, src_len);
1338 if (src_nents < 0) {
1339 dev_err(dev, "Invalid number of src SG.\n");
1340 return ERR_PTR(-EINVAL);
1342 src_nents = (src_nents == 1) ? 0 : src_nents;
1343 dst_len = assoclen + cryptlen + (encrypt ? authsize : 0);
1344 dst_nents = sg_nents_for_len(dst, dst_len);
1345 if (dst_nents < 0) {
1346 dev_err(dev, "Invalid number of dst SG.\n");
1347 return ERR_PTR(-EINVAL);
1349 dst_nents = (dst_nents == 1) ? 0 : dst_nents;
1353 * allocate space for base edesc plus the link tables,
1354 * allowing for two separate entries for AD and generated ICV (+ 2),
1355 * and space for two sets of ICVs (stashed and generated)
1357 alloc_len = sizeof(struct talitos_edesc);
1358 if (src_nents || dst_nents || !encrypt) {
1360 dma_len = (src_nents ? src_len : 0) +
1361 (dst_nents ? dst_len : 0) + authsize;
1363 dma_len = (src_nents + dst_nents + 2) *
1364 sizeof(struct talitos_ptr) + authsize;
1365 alloc_len += dma_len;
1369 alloc_len += icv_stashing ? authsize : 0;
1371 /* if its a ahash, add space for a second desc next to the first one */
1372 if (is_sec1 && !dst)
1373 alloc_len += sizeof(struct talitos_desc);
1374 alloc_len += ivsize;
1376 edesc = kmalloc(alloc_len, GFP_DMA | flags);
1378 return ERR_PTR(-ENOMEM);
1380 iv = memcpy(((u8 *)edesc) + alloc_len - ivsize, iv, ivsize);
1381 iv_dma = dma_map_single(dev, iv, ivsize, DMA_TO_DEVICE);
1383 memset(&edesc->desc, 0, sizeof(edesc->desc));
1385 edesc->src_nents = src_nents;
1386 edesc->dst_nents = dst_nents;
1387 edesc->iv_dma = iv_dma;
1388 edesc->dma_len = dma_len;
1390 edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1397 static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq, u8 *iv,
1398 int icv_stashing, bool encrypt)
1400 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1401 unsigned int authsize = crypto_aead_authsize(authenc);
1402 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1403 unsigned int ivsize = crypto_aead_ivsize(authenc);
1404 unsigned int cryptlen = areq->cryptlen - (encrypt ? 0 : authsize);
1406 return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst,
1407 iv, areq->assoclen, cryptlen,
1408 authsize, ivsize, icv_stashing,
1409 areq->base.flags, encrypt);
1412 static int aead_encrypt(struct aead_request *req)
1414 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1415 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1416 struct talitos_edesc *edesc;
1418 /* allocate extended descriptor */
1419 edesc = aead_edesc_alloc(req, req->iv, 0, true);
1421 return PTR_ERR(edesc);
1424 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
1426 return ipsec_esp(edesc, req, true, ipsec_esp_encrypt_done);
1429 static int aead_decrypt(struct aead_request *req)
1431 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1432 unsigned int authsize = crypto_aead_authsize(authenc);
1433 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1434 struct talitos_private *priv = dev_get_drvdata(ctx->dev);
1435 struct talitos_edesc *edesc;
1438 /* allocate extended descriptor */
1439 edesc = aead_edesc_alloc(req, req->iv, 1, false);
1441 return PTR_ERR(edesc);
1443 if ((edesc->desc.hdr & DESC_HDR_TYPE_IPSEC_ESP) &&
1444 (priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
1445 ((!edesc->src_nents && !edesc->dst_nents) ||
1446 priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
1448 /* decrypt and check the ICV */
1449 edesc->desc.hdr = ctx->desc_hdr_template |
1450 DESC_HDR_DIR_INBOUND |
1451 DESC_HDR_MODE1_MDEU_CICV;
1453 /* reset integrity check result bits */
1455 return ipsec_esp(edesc, req, false,
1456 ipsec_esp_decrypt_hwauth_done);
1459 /* Have to check the ICV with software */
1460 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1462 /* stash incoming ICV for later cmp with ICV generated by the h/w */
1463 icvdata = edesc->buf + edesc->dma_len;
1465 sg_pcopy_to_buffer(req->src, edesc->src_nents ? : 1, icvdata, authsize,
1466 req->assoclen + req->cryptlen - authsize);
1468 return ipsec_esp(edesc, req, false, ipsec_esp_decrypt_swauth_done);
1471 static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
1472 const u8 *key, unsigned int keylen)
1474 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1475 struct device *dev = ctx->dev;
1476 u32 tmp[DES_EXPKEY_WORDS];
1478 if (keylen > TALITOS_MAX_KEY_SIZE) {
1479 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
1483 if (unlikely(crypto_ablkcipher_get_flags(cipher) &
1484 CRYPTO_TFM_REQ_WEAK_KEY) &&
1485 !des_ekey(tmp, key)) {
1486 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_WEAK_KEY);
1491 dma_unmap_single(dev, ctx->dma_key, ctx->keylen, DMA_TO_DEVICE);
1493 memcpy(&ctx->key, key, keylen);
1494 ctx->keylen = keylen;
1496 ctx->dma_key = dma_map_single(dev, ctx->key, keylen, DMA_TO_DEVICE);
1501 static int ablkcipher_aes_setkey(struct crypto_ablkcipher *cipher,
1502 const u8 *key, unsigned int keylen)
1504 if (keylen == AES_KEYSIZE_128 || keylen == AES_KEYSIZE_192 ||
1505 keylen == AES_KEYSIZE_256)
1506 return ablkcipher_setkey(cipher, key, keylen);
1508 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
1513 static void common_nonsnoop_unmap(struct device *dev,
1514 struct talitos_edesc *edesc,
1515 struct ablkcipher_request *areq)
1517 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1519 talitos_sg_unmap(dev, edesc, areq->src, areq->dst, areq->nbytes, 0);
1520 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
1523 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1527 static void ablkcipher_done(struct device *dev,
1528 struct talitos_desc *desc, void *context,
1531 struct ablkcipher_request *areq = context;
1532 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1533 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1534 unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
1535 struct talitos_edesc *edesc;
1537 edesc = container_of(desc, struct talitos_edesc, desc);
1539 common_nonsnoop_unmap(dev, edesc, areq);
1540 memcpy(areq->info, ctx->iv, ivsize);
1544 areq->base.complete(&areq->base, err);
1547 static int common_nonsnoop(struct talitos_edesc *edesc,
1548 struct ablkcipher_request *areq,
1549 void (*callback) (struct device *dev,
1550 struct talitos_desc *desc,
1551 void *context, int error))
1553 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1554 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1555 struct device *dev = ctx->dev;
1556 struct talitos_desc *desc = &edesc->desc;
1557 unsigned int cryptlen = areq->nbytes;
1558 unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
1560 bool sync_needed = false;
1561 struct talitos_private *priv = dev_get_drvdata(dev);
1562 bool is_sec1 = has_ftr_sec1(priv);
1563 bool is_ctr = (desc->hdr & DESC_HDR_SEL0_MASK) == DESC_HDR_SEL0_AESU &&
1564 (desc->hdr & DESC_HDR_MODE0_AESU_MASK) == DESC_HDR_MODE0_AESU_CTR;
1566 /* first DWORD empty */
1569 to_talitos_ptr(&desc->ptr[1], edesc->iv_dma, ivsize, is_sec1);
1572 to_talitos_ptr(&desc->ptr[2], ctx->dma_key, ctx->keylen, is_sec1);
1574 sg_count = edesc->src_nents ?: 1;
1575 if (is_sec1 && sg_count > 1)
1576 sg_copy_to_buffer(areq->src, sg_count, edesc->buf,
1579 sg_count = dma_map_sg(dev, areq->src, sg_count,
1580 (areq->src == areq->dst) ?
1581 DMA_BIDIRECTIONAL : DMA_TO_DEVICE);
1585 sg_count = talitos_sg_map_ext(dev, areq->src, cryptlen, edesc, &desc->ptr[3],
1586 sg_count, 0, 0, 0, false, is_ctr ? 16 : 1);
1591 if (areq->src != areq->dst) {
1592 sg_count = edesc->dst_nents ? : 1;
1593 if (!is_sec1 || sg_count == 1)
1594 dma_map_sg(dev, areq->dst, sg_count, DMA_FROM_DEVICE);
1597 ret = talitos_sg_map(dev, areq->dst, cryptlen, edesc, &desc->ptr[4],
1598 sg_count, 0, (edesc->src_nents + 1));
1603 map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv,
1606 /* last DWORD empty */
1609 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1610 edesc->dma_len, DMA_BIDIRECTIONAL);
1612 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
1613 if (ret != -EINPROGRESS) {
1614 common_nonsnoop_unmap(dev, edesc, areq);
1620 static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
1623 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1624 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1625 unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
1627 return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst,
1628 areq->info, 0, areq->nbytes, 0, ivsize, 0,
1629 areq->base.flags, encrypt);
1632 static int ablkcipher_encrypt(struct ablkcipher_request *areq)
1634 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1635 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1636 struct talitos_edesc *edesc;
1637 unsigned int blocksize =
1638 crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(cipher));
1643 if (areq->nbytes % blocksize)
1646 /* allocate extended descriptor */
1647 edesc = ablkcipher_edesc_alloc(areq, true);
1649 return PTR_ERR(edesc);
1652 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
1654 return common_nonsnoop(edesc, areq, ablkcipher_done);
1657 static int ablkcipher_decrypt(struct ablkcipher_request *areq)
1659 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1660 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1661 struct talitos_edesc *edesc;
1662 unsigned int blocksize =
1663 crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(cipher));
1668 if (areq->nbytes % blocksize)
1671 /* allocate extended descriptor */
1672 edesc = ablkcipher_edesc_alloc(areq, false);
1674 return PTR_ERR(edesc);
1676 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1678 return common_nonsnoop(edesc, areq, ablkcipher_done);
1681 static void common_nonsnoop_hash_unmap(struct device *dev,
1682 struct talitos_edesc *edesc,
1683 struct ahash_request *areq)
1685 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1686 struct talitos_private *priv = dev_get_drvdata(dev);
1687 bool is_sec1 = has_ftr_sec1(priv);
1688 struct talitos_desc *desc = &edesc->desc;
1689 struct talitos_desc *desc2 = (struct talitos_desc *)
1690 (edesc->buf + edesc->dma_len);
1692 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1693 if (desc->next_desc &&
1694 desc->ptr[5].ptr != desc2->ptr[5].ptr)
1695 unmap_single_talitos_ptr(dev, &desc2->ptr[5], DMA_FROM_DEVICE);
1698 talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL, 0, 0);
1700 /* When using hashctx-in, must unmap it. */
1701 if (from_talitos_ptr_len(&edesc->desc.ptr[1], is_sec1))
1702 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1704 else if (desc->next_desc)
1705 unmap_single_talitos_ptr(dev, &desc2->ptr[1],
1708 if (is_sec1 && req_ctx->nbuf)
1709 unmap_single_talitos_ptr(dev, &desc->ptr[3],
1713 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1716 if (edesc->desc.next_desc)
1717 dma_unmap_single(dev, be32_to_cpu(edesc->desc.next_desc),
1718 TALITOS_DESC_SIZE, DMA_BIDIRECTIONAL);
1721 static void ahash_done(struct device *dev,
1722 struct talitos_desc *desc, void *context,
1725 struct ahash_request *areq = context;
1726 struct talitos_edesc *edesc =
1727 container_of(desc, struct talitos_edesc, desc);
1728 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1730 if (!req_ctx->last && req_ctx->to_hash_later) {
1731 /* Position any partial block for next update/final/finup */
1732 req_ctx->buf_idx = (req_ctx->buf_idx + 1) & 1;
1733 req_ctx->nbuf = req_ctx->to_hash_later;
1735 common_nonsnoop_hash_unmap(dev, edesc, areq);
1739 areq->base.complete(&areq->base, err);
1743 * SEC1 doesn't like hashing of 0 sized message, so we do the padding
1744 * ourself and submit a padded block
1746 static void talitos_handle_buggy_hash(struct talitos_ctx *ctx,
1747 struct talitos_edesc *edesc,
1748 struct talitos_ptr *ptr)
1750 static u8 padded_hash[64] = {
1751 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1752 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1753 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1754 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1757 pr_err_once("Bug in SEC1, padding ourself\n");
1758 edesc->desc.hdr &= ~DESC_HDR_MODE0_MDEU_PAD;
1759 map_single_talitos_ptr(ctx->dev, ptr, sizeof(padded_hash),
1760 (char *)padded_hash, DMA_TO_DEVICE);
1763 static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1764 struct ahash_request *areq, unsigned int length,
1765 void (*callback) (struct device *dev,
1766 struct talitos_desc *desc,
1767 void *context, int error))
1769 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1770 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1771 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1772 struct device *dev = ctx->dev;
1773 struct talitos_desc *desc = &edesc->desc;
1775 bool sync_needed = false;
1776 struct talitos_private *priv = dev_get_drvdata(dev);
1777 bool is_sec1 = has_ftr_sec1(priv);
1780 /* first DWORD empty */
1782 /* hash context in */
1783 if (!req_ctx->first || req_ctx->swinit) {
1784 map_single_talitos_ptr_nosync(dev, &desc->ptr[1],
1785 req_ctx->hw_context_size,
1786 req_ctx->hw_context,
1788 req_ctx->swinit = 0;
1790 /* Indicate next op is not the first. */
1795 to_talitos_ptr(&desc->ptr[2], ctx->dma_key, ctx->keylen,
1798 if (is_sec1 && req_ctx->nbuf)
1799 length -= req_ctx->nbuf;
1801 sg_count = edesc->src_nents ?: 1;
1802 if (is_sec1 && sg_count > 1)
1803 sg_copy_to_buffer(req_ctx->psrc, sg_count, edesc->buf, length);
1805 sg_count = dma_map_sg(dev, req_ctx->psrc, sg_count,
1810 if (is_sec1 && req_ctx->nbuf) {
1811 map_single_talitos_ptr(dev, &desc->ptr[3], req_ctx->nbuf,
1812 req_ctx->buf[req_ctx->buf_idx],
1815 sg_count = talitos_sg_map(dev, req_ctx->psrc, length, edesc,
1816 &desc->ptr[3], sg_count, 0, 0);
1821 /* fifth DWORD empty */
1823 /* hash/HMAC out -or- hash context out */
1825 map_single_talitos_ptr(dev, &desc->ptr[5],
1826 crypto_ahash_digestsize(tfm),
1827 areq->result, DMA_FROM_DEVICE);
1829 map_single_talitos_ptr_nosync(dev, &desc->ptr[5],
1830 req_ctx->hw_context_size,
1831 req_ctx->hw_context,
1834 /* last DWORD empty */
1836 if (is_sec1 && from_talitos_ptr_len(&desc->ptr[3], true) == 0)
1837 talitos_handle_buggy_hash(ctx, edesc, &desc->ptr[3]);
1839 if (is_sec1 && req_ctx->nbuf && length) {
1840 struct talitos_desc *desc2 = (struct talitos_desc *)
1841 (edesc->buf + edesc->dma_len);
1842 dma_addr_t next_desc;
1844 memset(desc2, 0, sizeof(*desc2));
1845 desc2->hdr = desc->hdr;
1846 desc2->hdr &= ~DESC_HDR_MODE0_MDEU_INIT;
1847 desc2->hdr1 = desc2->hdr;
1848 desc->hdr &= ~DESC_HDR_MODE0_MDEU_PAD;
1849 desc->hdr |= DESC_HDR_MODE0_MDEU_CONT;
1850 desc->hdr &= ~DESC_HDR_DONE_NOTIFY;
1852 if (desc->ptr[1].ptr)
1853 copy_talitos_ptr(&desc2->ptr[1], &desc->ptr[1],
1856 map_single_talitos_ptr_nosync(dev, &desc2->ptr[1],
1857 req_ctx->hw_context_size,
1858 req_ctx->hw_context,
1860 copy_talitos_ptr(&desc2->ptr[2], &desc->ptr[2], is_sec1);
1861 sg_count = talitos_sg_map(dev, req_ctx->psrc, length, edesc,
1862 &desc2->ptr[3], sg_count, 0, 0);
1865 copy_talitos_ptr(&desc2->ptr[5], &desc->ptr[5], is_sec1);
1867 map_single_talitos_ptr_nosync(dev, &desc->ptr[5],
1868 req_ctx->hw_context_size,
1869 req_ctx->hw_context,
1872 next_desc = dma_map_single(dev, &desc2->hdr1, TALITOS_DESC_SIZE,
1874 desc->next_desc = cpu_to_be32(next_desc);
1878 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1879 edesc->dma_len, DMA_BIDIRECTIONAL);
1881 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
1882 if (ret != -EINPROGRESS) {
1883 common_nonsnoop_hash_unmap(dev, edesc, areq);
1889 static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1890 unsigned int nbytes)
1892 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1893 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1894 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1895 struct talitos_private *priv = dev_get_drvdata(ctx->dev);
1896 bool is_sec1 = has_ftr_sec1(priv);
1899 nbytes -= req_ctx->nbuf;
1901 return talitos_edesc_alloc(ctx->dev, req_ctx->psrc, NULL, NULL, 0,
1902 nbytes, 0, 0, 0, areq->base.flags, false);
1905 static int ahash_init(struct ahash_request *areq)
1907 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1908 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1909 struct device *dev = ctx->dev;
1910 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1914 /* Initialize the context */
1915 req_ctx->buf_idx = 0;
1917 req_ctx->first = 1; /* first indicates h/w must init its context */
1918 req_ctx->swinit = 0; /* assume h/w init of context */
1919 size = (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1920 ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1921 : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1922 req_ctx->hw_context_size = size;
1924 dma = dma_map_single(dev, req_ctx->hw_context, req_ctx->hw_context_size,
1926 dma_unmap_single(dev, dma, req_ctx->hw_context_size, DMA_TO_DEVICE);
1932 * on h/w without explicit sha224 support, we initialize h/w context
1933 * manually with sha224 constants, and tell it to run sha256.
1935 static int ahash_init_sha224_swinit(struct ahash_request *areq)
1937 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1939 req_ctx->hw_context[0] = SHA224_H0;
1940 req_ctx->hw_context[1] = SHA224_H1;
1941 req_ctx->hw_context[2] = SHA224_H2;
1942 req_ctx->hw_context[3] = SHA224_H3;
1943 req_ctx->hw_context[4] = SHA224_H4;
1944 req_ctx->hw_context[5] = SHA224_H5;
1945 req_ctx->hw_context[6] = SHA224_H6;
1946 req_ctx->hw_context[7] = SHA224_H7;
1948 /* init 64-bit count */
1949 req_ctx->hw_context[8] = 0;
1950 req_ctx->hw_context[9] = 0;
1953 req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
1958 static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1960 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1961 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1962 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1963 struct talitos_edesc *edesc;
1964 unsigned int blocksize =
1965 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1966 unsigned int nbytes_to_hash;
1967 unsigned int to_hash_later;
1970 struct device *dev = ctx->dev;
1971 struct talitos_private *priv = dev_get_drvdata(dev);
1972 bool is_sec1 = has_ftr_sec1(priv);
1973 u8 *ctx_buf = req_ctx->buf[req_ctx->buf_idx];
1975 if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
1976 /* Buffer up to one whole block */
1977 nents = sg_nents_for_len(areq->src, nbytes);
1979 dev_err(ctx->dev, "Invalid number of src SG.\n");
1982 sg_copy_to_buffer(areq->src, nents,
1983 ctx_buf + req_ctx->nbuf, nbytes);
1984 req_ctx->nbuf += nbytes;
1988 /* At least (blocksize + 1) bytes are available to hash */
1989 nbytes_to_hash = nbytes + req_ctx->nbuf;
1990 to_hash_later = nbytes_to_hash & (blocksize - 1);
1994 else if (to_hash_later)
1995 /* There is a partial block. Hash the full block(s) now */
1996 nbytes_to_hash -= to_hash_later;
1998 /* Keep one block buffered */
1999 nbytes_to_hash -= blocksize;
2000 to_hash_later = blocksize;
2003 /* Chain in any previously buffered data */
2004 if (!is_sec1 && req_ctx->nbuf) {
2005 nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
2006 sg_init_table(req_ctx->bufsl, nsg);
2007 sg_set_buf(req_ctx->bufsl, ctx_buf, req_ctx->nbuf);
2009 sg_chain(req_ctx->bufsl, 2, areq->src);
2010 req_ctx->psrc = req_ctx->bufsl;
2011 } else if (is_sec1 && req_ctx->nbuf && req_ctx->nbuf < blocksize) {
2014 if (nbytes_to_hash > blocksize)
2015 offset = blocksize - req_ctx->nbuf;
2017 offset = nbytes_to_hash - req_ctx->nbuf;
2018 nents = sg_nents_for_len(areq->src, offset);
2020 dev_err(ctx->dev, "Invalid number of src SG.\n");
2023 sg_copy_to_buffer(areq->src, nents,
2024 ctx_buf + req_ctx->nbuf, offset);
2025 req_ctx->nbuf += offset;
2026 req_ctx->psrc = scatterwalk_ffwd(req_ctx->bufsl, areq->src,
2029 req_ctx->psrc = areq->src;
2031 if (to_hash_later) {
2032 nents = sg_nents_for_len(areq->src, nbytes);
2034 dev_err(ctx->dev, "Invalid number of src SG.\n");
2037 sg_pcopy_to_buffer(areq->src, nents,
2038 req_ctx->buf[(req_ctx->buf_idx + 1) & 1],
2040 nbytes - to_hash_later);
2042 req_ctx->to_hash_later = to_hash_later;
2044 /* Allocate extended descriptor */
2045 edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
2047 return PTR_ERR(edesc);
2049 edesc->desc.hdr = ctx->desc_hdr_template;
2051 /* On last one, request SEC to pad; otherwise continue */
2053 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
2055 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
2057 /* request SEC to INIT hash. */
2058 if (req_ctx->first && !req_ctx->swinit)
2059 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
2061 /* When the tfm context has a keylen, it's an HMAC.
2062 * A first or last (ie. not middle) descriptor must request HMAC.
2064 if (ctx->keylen && (req_ctx->first || req_ctx->last))
2065 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
2067 return common_nonsnoop_hash(edesc, areq, nbytes_to_hash, ahash_done);
2070 static int ahash_update(struct ahash_request *areq)
2072 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2076 return ahash_process_req(areq, areq->nbytes);
2079 static int ahash_final(struct ahash_request *areq)
2081 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2085 return ahash_process_req(areq, 0);
2088 static int ahash_finup(struct ahash_request *areq)
2090 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2094 return ahash_process_req(areq, areq->nbytes);
2097 static int ahash_digest(struct ahash_request *areq)
2099 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2100 struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
2105 return ahash_process_req(areq, areq->nbytes);
2108 static int ahash_export(struct ahash_request *areq, void *out)
2110 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2111 struct talitos_export_state *export = out;
2112 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
2113 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
2114 struct device *dev = ctx->dev;
2117 dma = dma_map_single(dev, req_ctx->hw_context, req_ctx->hw_context_size,
2119 dma_unmap_single(dev, dma, req_ctx->hw_context_size, DMA_FROM_DEVICE);
2121 memcpy(export->hw_context, req_ctx->hw_context,
2122 req_ctx->hw_context_size);
2123 memcpy(export->buf, req_ctx->buf[req_ctx->buf_idx], req_ctx->nbuf);
2124 export->swinit = req_ctx->swinit;
2125 export->first = req_ctx->first;
2126 export->last = req_ctx->last;
2127 export->to_hash_later = req_ctx->to_hash_later;
2128 export->nbuf = req_ctx->nbuf;
2133 static int ahash_import(struct ahash_request *areq, const void *in)
2135 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2136 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
2137 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
2138 struct device *dev = ctx->dev;
2139 const struct talitos_export_state *export = in;
2143 memset(req_ctx, 0, sizeof(*req_ctx));
2144 size = (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
2145 ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
2146 : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
2147 req_ctx->hw_context_size = size;
2148 memcpy(req_ctx->hw_context, export->hw_context, size);
2149 memcpy(req_ctx->buf[0], export->buf, export->nbuf);
2150 req_ctx->swinit = export->swinit;
2151 req_ctx->first = export->first;
2152 req_ctx->last = export->last;
2153 req_ctx->to_hash_later = export->to_hash_later;
2154 req_ctx->nbuf = export->nbuf;
2156 dma = dma_map_single(dev, req_ctx->hw_context, req_ctx->hw_context_size,
2158 dma_unmap_single(dev, dma, req_ctx->hw_context_size, DMA_TO_DEVICE);
2163 static int keyhash(struct crypto_ahash *tfm, const u8 *key, unsigned int keylen,
2166 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
2168 struct scatterlist sg[1];
2169 struct ahash_request *req;
2170 struct crypto_wait wait;
2173 crypto_init_wait(&wait);
2175 req = ahash_request_alloc(tfm, GFP_KERNEL);
2179 /* Keep tfm keylen == 0 during hash of the long key */
2181 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
2182 crypto_req_done, &wait);
2184 sg_init_one(&sg[0], key, keylen);
2186 ahash_request_set_crypt(req, sg, hash, keylen);
2187 ret = crypto_wait_req(crypto_ahash_digest(req), &wait);
2189 ahash_request_free(req);
2194 static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
2195 unsigned int keylen)
2197 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
2198 struct device *dev = ctx->dev;
2199 unsigned int blocksize =
2200 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
2201 unsigned int digestsize = crypto_ahash_digestsize(tfm);
2202 unsigned int keysize = keylen;
2203 u8 hash[SHA512_DIGEST_SIZE];
2206 if (keylen <= blocksize)
2207 memcpy(ctx->key, key, keysize);
2209 /* Must get the hash of the long key */
2210 ret = keyhash(tfm, key, keylen, hash);
2213 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
2217 keysize = digestsize;
2218 memcpy(ctx->key, hash, digestsize);
2222 dma_unmap_single(dev, ctx->dma_key, ctx->keylen, DMA_TO_DEVICE);
2224 ctx->keylen = keysize;
2225 ctx->dma_key = dma_map_single(dev, ctx->key, keysize, DMA_TO_DEVICE);
2231 struct talitos_alg_template {
2235 struct crypto_alg crypto;
2236 struct ahash_alg hash;
2237 struct aead_alg aead;
2239 __be32 desc_hdr_template;
2242 static struct talitos_alg_template driver_algs[] = {
2243 /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */
2244 { .type = CRYPTO_ALG_TYPE_AEAD,
2247 .cra_name = "authenc(hmac(sha1),cbc(aes))",
2248 .cra_driver_name = "authenc-hmac-sha1-"
2250 .cra_blocksize = AES_BLOCK_SIZE,
2251 .cra_flags = CRYPTO_ALG_ASYNC,
2253 .ivsize = AES_BLOCK_SIZE,
2254 .maxauthsize = SHA1_DIGEST_SIZE,
2256 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2257 DESC_HDR_SEL0_AESU |
2258 DESC_HDR_MODE0_AESU_CBC |
2259 DESC_HDR_SEL1_MDEUA |
2260 DESC_HDR_MODE1_MDEU_INIT |
2261 DESC_HDR_MODE1_MDEU_PAD |
2262 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
2264 { .type = CRYPTO_ALG_TYPE_AEAD,
2265 .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
2268 .cra_name = "authenc(hmac(sha1),cbc(aes))",
2269 .cra_driver_name = "authenc-hmac-sha1-"
2270 "cbc-aes-talitos-hsna",
2271 .cra_blocksize = AES_BLOCK_SIZE,
2272 .cra_flags = CRYPTO_ALG_ASYNC,
2274 .ivsize = AES_BLOCK_SIZE,
2275 .maxauthsize = SHA1_DIGEST_SIZE,
2277 .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
2278 DESC_HDR_SEL0_AESU |
2279 DESC_HDR_MODE0_AESU_CBC |
2280 DESC_HDR_SEL1_MDEUA |
2281 DESC_HDR_MODE1_MDEU_INIT |
2282 DESC_HDR_MODE1_MDEU_PAD |
2283 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
2285 { .type = CRYPTO_ALG_TYPE_AEAD,
2288 .cra_name = "authenc(hmac(sha1),"
2290 .cra_driver_name = "authenc-hmac-sha1-"
2292 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2293 .cra_flags = CRYPTO_ALG_ASYNC,
2295 .ivsize = DES3_EDE_BLOCK_SIZE,
2296 .maxauthsize = SHA1_DIGEST_SIZE,
2298 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2300 DESC_HDR_MODE0_DEU_CBC |
2301 DESC_HDR_MODE0_DEU_3DES |
2302 DESC_HDR_SEL1_MDEUA |
2303 DESC_HDR_MODE1_MDEU_INIT |
2304 DESC_HDR_MODE1_MDEU_PAD |
2305 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
2307 { .type = CRYPTO_ALG_TYPE_AEAD,
2308 .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
2311 .cra_name = "authenc(hmac(sha1),"
2313 .cra_driver_name = "authenc-hmac-sha1-"
2314 "cbc-3des-talitos-hsna",
2315 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2316 .cra_flags = CRYPTO_ALG_ASYNC,
2318 .ivsize = DES3_EDE_BLOCK_SIZE,
2319 .maxauthsize = SHA1_DIGEST_SIZE,
2321 .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
2323 DESC_HDR_MODE0_DEU_CBC |
2324 DESC_HDR_MODE0_DEU_3DES |
2325 DESC_HDR_SEL1_MDEUA |
2326 DESC_HDR_MODE1_MDEU_INIT |
2327 DESC_HDR_MODE1_MDEU_PAD |
2328 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
2330 { .type = CRYPTO_ALG_TYPE_AEAD,
2333 .cra_name = "authenc(hmac(sha224),cbc(aes))",
2334 .cra_driver_name = "authenc-hmac-sha224-"
2336 .cra_blocksize = AES_BLOCK_SIZE,
2337 .cra_flags = CRYPTO_ALG_ASYNC,
2339 .ivsize = AES_BLOCK_SIZE,
2340 .maxauthsize = SHA224_DIGEST_SIZE,
2342 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2343 DESC_HDR_SEL0_AESU |
2344 DESC_HDR_MODE0_AESU_CBC |
2345 DESC_HDR_SEL1_MDEUA |
2346 DESC_HDR_MODE1_MDEU_INIT |
2347 DESC_HDR_MODE1_MDEU_PAD |
2348 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
2350 { .type = CRYPTO_ALG_TYPE_AEAD,
2351 .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
2354 .cra_name = "authenc(hmac(sha224),cbc(aes))",
2355 .cra_driver_name = "authenc-hmac-sha224-"
2356 "cbc-aes-talitos-hsna",
2357 .cra_blocksize = AES_BLOCK_SIZE,
2358 .cra_flags = CRYPTO_ALG_ASYNC,
2360 .ivsize = AES_BLOCK_SIZE,
2361 .maxauthsize = SHA224_DIGEST_SIZE,
2363 .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
2364 DESC_HDR_SEL0_AESU |
2365 DESC_HDR_MODE0_AESU_CBC |
2366 DESC_HDR_SEL1_MDEUA |
2367 DESC_HDR_MODE1_MDEU_INIT |
2368 DESC_HDR_MODE1_MDEU_PAD |
2369 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
2371 { .type = CRYPTO_ALG_TYPE_AEAD,
2374 .cra_name = "authenc(hmac(sha224),"
2376 .cra_driver_name = "authenc-hmac-sha224-"
2378 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2379 .cra_flags = CRYPTO_ALG_ASYNC,
2381 .ivsize = DES3_EDE_BLOCK_SIZE,
2382 .maxauthsize = SHA224_DIGEST_SIZE,
2384 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2386 DESC_HDR_MODE0_DEU_CBC |
2387 DESC_HDR_MODE0_DEU_3DES |
2388 DESC_HDR_SEL1_MDEUA |
2389 DESC_HDR_MODE1_MDEU_INIT |
2390 DESC_HDR_MODE1_MDEU_PAD |
2391 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
2393 { .type = CRYPTO_ALG_TYPE_AEAD,
2394 .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
2397 .cra_name = "authenc(hmac(sha224),"
2399 .cra_driver_name = "authenc-hmac-sha224-"
2400 "cbc-3des-talitos-hsna",
2401 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2402 .cra_flags = CRYPTO_ALG_ASYNC,
2404 .ivsize = DES3_EDE_BLOCK_SIZE,
2405 .maxauthsize = SHA224_DIGEST_SIZE,
2407 .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
2409 DESC_HDR_MODE0_DEU_CBC |
2410 DESC_HDR_MODE0_DEU_3DES |
2411 DESC_HDR_SEL1_MDEUA |
2412 DESC_HDR_MODE1_MDEU_INIT |
2413 DESC_HDR_MODE1_MDEU_PAD |
2414 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
2416 { .type = CRYPTO_ALG_TYPE_AEAD,
2419 .cra_name = "authenc(hmac(sha256),cbc(aes))",
2420 .cra_driver_name = "authenc-hmac-sha256-"
2422 .cra_blocksize = AES_BLOCK_SIZE,
2423 .cra_flags = CRYPTO_ALG_ASYNC,
2425 .ivsize = AES_BLOCK_SIZE,
2426 .maxauthsize = SHA256_DIGEST_SIZE,
2428 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2429 DESC_HDR_SEL0_AESU |
2430 DESC_HDR_MODE0_AESU_CBC |
2431 DESC_HDR_SEL1_MDEUA |
2432 DESC_HDR_MODE1_MDEU_INIT |
2433 DESC_HDR_MODE1_MDEU_PAD |
2434 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2436 { .type = CRYPTO_ALG_TYPE_AEAD,
2437 .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
2440 .cra_name = "authenc(hmac(sha256),cbc(aes))",
2441 .cra_driver_name = "authenc-hmac-sha256-"
2442 "cbc-aes-talitos-hsna",
2443 .cra_blocksize = AES_BLOCK_SIZE,
2444 .cra_flags = CRYPTO_ALG_ASYNC,
2446 .ivsize = AES_BLOCK_SIZE,
2447 .maxauthsize = SHA256_DIGEST_SIZE,
2449 .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
2450 DESC_HDR_SEL0_AESU |
2451 DESC_HDR_MODE0_AESU_CBC |
2452 DESC_HDR_SEL1_MDEUA |
2453 DESC_HDR_MODE1_MDEU_INIT |
2454 DESC_HDR_MODE1_MDEU_PAD |
2455 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2457 { .type = CRYPTO_ALG_TYPE_AEAD,
2460 .cra_name = "authenc(hmac(sha256),"
2462 .cra_driver_name = "authenc-hmac-sha256-"
2464 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2465 .cra_flags = CRYPTO_ALG_ASYNC,
2467 .ivsize = DES3_EDE_BLOCK_SIZE,
2468 .maxauthsize = SHA256_DIGEST_SIZE,
2470 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2472 DESC_HDR_MODE0_DEU_CBC |
2473 DESC_HDR_MODE0_DEU_3DES |
2474 DESC_HDR_SEL1_MDEUA |
2475 DESC_HDR_MODE1_MDEU_INIT |
2476 DESC_HDR_MODE1_MDEU_PAD |
2477 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2479 { .type = CRYPTO_ALG_TYPE_AEAD,
2480 .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
2483 .cra_name = "authenc(hmac(sha256),"
2485 .cra_driver_name = "authenc-hmac-sha256-"
2486 "cbc-3des-talitos-hsna",
2487 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2488 .cra_flags = CRYPTO_ALG_ASYNC,
2490 .ivsize = DES3_EDE_BLOCK_SIZE,
2491 .maxauthsize = SHA256_DIGEST_SIZE,
2493 .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
2495 DESC_HDR_MODE0_DEU_CBC |
2496 DESC_HDR_MODE0_DEU_3DES |
2497 DESC_HDR_SEL1_MDEUA |
2498 DESC_HDR_MODE1_MDEU_INIT |
2499 DESC_HDR_MODE1_MDEU_PAD |
2500 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2502 { .type = CRYPTO_ALG_TYPE_AEAD,
2505 .cra_name = "authenc(hmac(sha384),cbc(aes))",
2506 .cra_driver_name = "authenc-hmac-sha384-"
2508 .cra_blocksize = AES_BLOCK_SIZE,
2509 .cra_flags = CRYPTO_ALG_ASYNC,
2511 .ivsize = AES_BLOCK_SIZE,
2512 .maxauthsize = SHA384_DIGEST_SIZE,
2514 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2515 DESC_HDR_SEL0_AESU |
2516 DESC_HDR_MODE0_AESU_CBC |
2517 DESC_HDR_SEL1_MDEUB |
2518 DESC_HDR_MODE1_MDEU_INIT |
2519 DESC_HDR_MODE1_MDEU_PAD |
2520 DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2522 { .type = CRYPTO_ALG_TYPE_AEAD,
2525 .cra_name = "authenc(hmac(sha384),"
2527 .cra_driver_name = "authenc-hmac-sha384-"
2529 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2530 .cra_flags = CRYPTO_ALG_ASYNC,
2532 .ivsize = DES3_EDE_BLOCK_SIZE,
2533 .maxauthsize = SHA384_DIGEST_SIZE,
2535 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2537 DESC_HDR_MODE0_DEU_CBC |
2538 DESC_HDR_MODE0_DEU_3DES |
2539 DESC_HDR_SEL1_MDEUB |
2540 DESC_HDR_MODE1_MDEU_INIT |
2541 DESC_HDR_MODE1_MDEU_PAD |
2542 DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2544 { .type = CRYPTO_ALG_TYPE_AEAD,
2547 .cra_name = "authenc(hmac(sha512),cbc(aes))",
2548 .cra_driver_name = "authenc-hmac-sha512-"
2550 .cra_blocksize = AES_BLOCK_SIZE,
2551 .cra_flags = CRYPTO_ALG_ASYNC,
2553 .ivsize = AES_BLOCK_SIZE,
2554 .maxauthsize = SHA512_DIGEST_SIZE,
2556 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2557 DESC_HDR_SEL0_AESU |
2558 DESC_HDR_MODE0_AESU_CBC |
2559 DESC_HDR_SEL1_MDEUB |
2560 DESC_HDR_MODE1_MDEU_INIT |
2561 DESC_HDR_MODE1_MDEU_PAD |
2562 DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2564 { .type = CRYPTO_ALG_TYPE_AEAD,
2567 .cra_name = "authenc(hmac(sha512),"
2569 .cra_driver_name = "authenc-hmac-sha512-"
2571 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2572 .cra_flags = CRYPTO_ALG_ASYNC,
2574 .ivsize = DES3_EDE_BLOCK_SIZE,
2575 .maxauthsize = SHA512_DIGEST_SIZE,
2577 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2579 DESC_HDR_MODE0_DEU_CBC |
2580 DESC_HDR_MODE0_DEU_3DES |
2581 DESC_HDR_SEL1_MDEUB |
2582 DESC_HDR_MODE1_MDEU_INIT |
2583 DESC_HDR_MODE1_MDEU_PAD |
2584 DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2586 { .type = CRYPTO_ALG_TYPE_AEAD,
2589 .cra_name = "authenc(hmac(md5),cbc(aes))",
2590 .cra_driver_name = "authenc-hmac-md5-"
2592 .cra_blocksize = AES_BLOCK_SIZE,
2593 .cra_flags = CRYPTO_ALG_ASYNC,
2595 .ivsize = AES_BLOCK_SIZE,
2596 .maxauthsize = MD5_DIGEST_SIZE,
2598 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2599 DESC_HDR_SEL0_AESU |
2600 DESC_HDR_MODE0_AESU_CBC |
2601 DESC_HDR_SEL1_MDEUA |
2602 DESC_HDR_MODE1_MDEU_INIT |
2603 DESC_HDR_MODE1_MDEU_PAD |
2604 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2606 { .type = CRYPTO_ALG_TYPE_AEAD,
2607 .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
2610 .cra_name = "authenc(hmac(md5),cbc(aes))",
2611 .cra_driver_name = "authenc-hmac-md5-"
2612 "cbc-aes-talitos-hsna",
2613 .cra_blocksize = AES_BLOCK_SIZE,
2614 .cra_flags = CRYPTO_ALG_ASYNC,
2616 .ivsize = AES_BLOCK_SIZE,
2617 .maxauthsize = MD5_DIGEST_SIZE,
2619 .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
2620 DESC_HDR_SEL0_AESU |
2621 DESC_HDR_MODE0_AESU_CBC |
2622 DESC_HDR_SEL1_MDEUA |
2623 DESC_HDR_MODE1_MDEU_INIT |
2624 DESC_HDR_MODE1_MDEU_PAD |
2625 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2627 { .type = CRYPTO_ALG_TYPE_AEAD,
2630 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
2631 .cra_driver_name = "authenc-hmac-md5-"
2633 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2634 .cra_flags = CRYPTO_ALG_ASYNC,
2636 .ivsize = DES3_EDE_BLOCK_SIZE,
2637 .maxauthsize = MD5_DIGEST_SIZE,
2639 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2641 DESC_HDR_MODE0_DEU_CBC |
2642 DESC_HDR_MODE0_DEU_3DES |
2643 DESC_HDR_SEL1_MDEUA |
2644 DESC_HDR_MODE1_MDEU_INIT |
2645 DESC_HDR_MODE1_MDEU_PAD |
2646 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2648 { .type = CRYPTO_ALG_TYPE_AEAD,
2649 .priority = TALITOS_CRA_PRIORITY_AEAD_HSNA,
2652 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
2653 .cra_driver_name = "authenc-hmac-md5-"
2654 "cbc-3des-talitos-hsna",
2655 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2656 .cra_flags = CRYPTO_ALG_ASYNC,
2658 .ivsize = DES3_EDE_BLOCK_SIZE,
2659 .maxauthsize = MD5_DIGEST_SIZE,
2661 .desc_hdr_template = DESC_HDR_TYPE_HMAC_SNOOP_NO_AFEU |
2663 DESC_HDR_MODE0_DEU_CBC |
2664 DESC_HDR_MODE0_DEU_3DES |
2665 DESC_HDR_SEL1_MDEUA |
2666 DESC_HDR_MODE1_MDEU_INIT |
2667 DESC_HDR_MODE1_MDEU_PAD |
2668 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2670 /* ABLKCIPHER algorithms. */
2671 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2673 .cra_name = "ecb(aes)",
2674 .cra_driver_name = "ecb-aes-talitos",
2675 .cra_blocksize = AES_BLOCK_SIZE,
2676 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2679 .min_keysize = AES_MIN_KEY_SIZE,
2680 .max_keysize = AES_MAX_KEY_SIZE,
2683 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2686 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2688 .cra_name = "cbc(aes)",
2689 .cra_driver_name = "cbc-aes-talitos",
2690 .cra_blocksize = AES_BLOCK_SIZE,
2691 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2694 .min_keysize = AES_MIN_KEY_SIZE,
2695 .max_keysize = AES_MAX_KEY_SIZE,
2696 .ivsize = AES_BLOCK_SIZE,
2697 .setkey = ablkcipher_aes_setkey,
2700 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2701 DESC_HDR_SEL0_AESU |
2702 DESC_HDR_MODE0_AESU_CBC,
2704 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2706 .cra_name = "ctr(aes)",
2707 .cra_driver_name = "ctr-aes-talitos",
2709 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2712 .min_keysize = AES_MIN_KEY_SIZE,
2713 .max_keysize = AES_MAX_KEY_SIZE,
2714 .ivsize = AES_BLOCK_SIZE,
2715 .setkey = ablkcipher_aes_setkey,
2718 .desc_hdr_template = DESC_HDR_TYPE_AESU_CTR_NONSNOOP |
2719 DESC_HDR_SEL0_AESU |
2720 DESC_HDR_MODE0_AESU_CTR,
2722 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2724 .cra_name = "ecb(des)",
2725 .cra_driver_name = "ecb-des-talitos",
2726 .cra_blocksize = DES_BLOCK_SIZE,
2727 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2730 .min_keysize = DES_KEY_SIZE,
2731 .max_keysize = DES_KEY_SIZE,
2732 .ivsize = DES_BLOCK_SIZE,
2735 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2738 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2740 .cra_name = "cbc(des)",
2741 .cra_driver_name = "cbc-des-talitos",
2742 .cra_blocksize = DES_BLOCK_SIZE,
2743 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2746 .min_keysize = DES_KEY_SIZE,
2747 .max_keysize = DES_KEY_SIZE,
2748 .ivsize = DES_BLOCK_SIZE,
2751 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2753 DESC_HDR_MODE0_DEU_CBC,
2755 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2757 .cra_name = "ecb(des3_ede)",
2758 .cra_driver_name = "ecb-3des-talitos",
2759 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2760 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2763 .min_keysize = DES3_EDE_KEY_SIZE,
2764 .max_keysize = DES3_EDE_KEY_SIZE,
2765 .ivsize = DES3_EDE_BLOCK_SIZE,
2768 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2770 DESC_HDR_MODE0_DEU_3DES,
2772 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2774 .cra_name = "cbc(des3_ede)",
2775 .cra_driver_name = "cbc-3des-talitos",
2776 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2777 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2780 .min_keysize = DES3_EDE_KEY_SIZE,
2781 .max_keysize = DES3_EDE_KEY_SIZE,
2782 .ivsize = DES3_EDE_BLOCK_SIZE,
2785 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2787 DESC_HDR_MODE0_DEU_CBC |
2788 DESC_HDR_MODE0_DEU_3DES,
2790 /* AHASH algorithms. */
2791 { .type = CRYPTO_ALG_TYPE_AHASH,
2793 .halg.digestsize = MD5_DIGEST_SIZE,
2794 .halg.statesize = sizeof(struct talitos_export_state),
2797 .cra_driver_name = "md5-talitos",
2798 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
2799 .cra_flags = CRYPTO_ALG_ASYNC,
2802 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2803 DESC_HDR_SEL0_MDEUA |
2804 DESC_HDR_MODE0_MDEU_MD5,
2806 { .type = CRYPTO_ALG_TYPE_AHASH,
2808 .halg.digestsize = SHA1_DIGEST_SIZE,
2809 .halg.statesize = sizeof(struct talitos_export_state),
2812 .cra_driver_name = "sha1-talitos",
2813 .cra_blocksize = SHA1_BLOCK_SIZE,
2814 .cra_flags = CRYPTO_ALG_ASYNC,
2817 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2818 DESC_HDR_SEL0_MDEUA |
2819 DESC_HDR_MODE0_MDEU_SHA1,
2821 { .type = CRYPTO_ALG_TYPE_AHASH,
2823 .halg.digestsize = SHA224_DIGEST_SIZE,
2824 .halg.statesize = sizeof(struct talitos_export_state),
2826 .cra_name = "sha224",
2827 .cra_driver_name = "sha224-talitos",
2828 .cra_blocksize = SHA224_BLOCK_SIZE,
2829 .cra_flags = CRYPTO_ALG_ASYNC,
2832 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2833 DESC_HDR_SEL0_MDEUA |
2834 DESC_HDR_MODE0_MDEU_SHA224,
2836 { .type = CRYPTO_ALG_TYPE_AHASH,
2838 .halg.digestsize = SHA256_DIGEST_SIZE,
2839 .halg.statesize = sizeof(struct talitos_export_state),
2841 .cra_name = "sha256",
2842 .cra_driver_name = "sha256-talitos",
2843 .cra_blocksize = SHA256_BLOCK_SIZE,
2844 .cra_flags = CRYPTO_ALG_ASYNC,
2847 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2848 DESC_HDR_SEL0_MDEUA |
2849 DESC_HDR_MODE0_MDEU_SHA256,
2851 { .type = CRYPTO_ALG_TYPE_AHASH,
2853 .halg.digestsize = SHA384_DIGEST_SIZE,
2854 .halg.statesize = sizeof(struct talitos_export_state),
2856 .cra_name = "sha384",
2857 .cra_driver_name = "sha384-talitos",
2858 .cra_blocksize = SHA384_BLOCK_SIZE,
2859 .cra_flags = CRYPTO_ALG_ASYNC,
2862 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2863 DESC_HDR_SEL0_MDEUB |
2864 DESC_HDR_MODE0_MDEUB_SHA384,
2866 { .type = CRYPTO_ALG_TYPE_AHASH,
2868 .halg.digestsize = SHA512_DIGEST_SIZE,
2869 .halg.statesize = sizeof(struct talitos_export_state),
2871 .cra_name = "sha512",
2872 .cra_driver_name = "sha512-talitos",
2873 .cra_blocksize = SHA512_BLOCK_SIZE,
2874 .cra_flags = CRYPTO_ALG_ASYNC,
2877 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2878 DESC_HDR_SEL0_MDEUB |
2879 DESC_HDR_MODE0_MDEUB_SHA512,
2881 { .type = CRYPTO_ALG_TYPE_AHASH,
2883 .halg.digestsize = MD5_DIGEST_SIZE,
2884 .halg.statesize = sizeof(struct talitos_export_state),
2886 .cra_name = "hmac(md5)",
2887 .cra_driver_name = "hmac-md5-talitos",
2888 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
2889 .cra_flags = CRYPTO_ALG_ASYNC,
2892 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2893 DESC_HDR_SEL0_MDEUA |
2894 DESC_HDR_MODE0_MDEU_MD5,
2896 { .type = CRYPTO_ALG_TYPE_AHASH,
2898 .halg.digestsize = SHA1_DIGEST_SIZE,
2899 .halg.statesize = sizeof(struct talitos_export_state),
2901 .cra_name = "hmac(sha1)",
2902 .cra_driver_name = "hmac-sha1-talitos",
2903 .cra_blocksize = SHA1_BLOCK_SIZE,
2904 .cra_flags = CRYPTO_ALG_ASYNC,
2907 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2908 DESC_HDR_SEL0_MDEUA |
2909 DESC_HDR_MODE0_MDEU_SHA1,
2911 { .type = CRYPTO_ALG_TYPE_AHASH,
2913 .halg.digestsize = SHA224_DIGEST_SIZE,
2914 .halg.statesize = sizeof(struct talitos_export_state),
2916 .cra_name = "hmac(sha224)",
2917 .cra_driver_name = "hmac-sha224-talitos",
2918 .cra_blocksize = SHA224_BLOCK_SIZE,
2919 .cra_flags = CRYPTO_ALG_ASYNC,
2922 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2923 DESC_HDR_SEL0_MDEUA |
2924 DESC_HDR_MODE0_MDEU_SHA224,
2926 { .type = CRYPTO_ALG_TYPE_AHASH,
2928 .halg.digestsize = SHA256_DIGEST_SIZE,
2929 .halg.statesize = sizeof(struct talitos_export_state),
2931 .cra_name = "hmac(sha256)",
2932 .cra_driver_name = "hmac-sha256-talitos",
2933 .cra_blocksize = SHA256_BLOCK_SIZE,
2934 .cra_flags = CRYPTO_ALG_ASYNC,
2937 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2938 DESC_HDR_SEL0_MDEUA |
2939 DESC_HDR_MODE0_MDEU_SHA256,
2941 { .type = CRYPTO_ALG_TYPE_AHASH,
2943 .halg.digestsize = SHA384_DIGEST_SIZE,
2944 .halg.statesize = sizeof(struct talitos_export_state),
2946 .cra_name = "hmac(sha384)",
2947 .cra_driver_name = "hmac-sha384-talitos",
2948 .cra_blocksize = SHA384_BLOCK_SIZE,
2949 .cra_flags = CRYPTO_ALG_ASYNC,
2952 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2953 DESC_HDR_SEL0_MDEUB |
2954 DESC_HDR_MODE0_MDEUB_SHA384,
2956 { .type = CRYPTO_ALG_TYPE_AHASH,
2958 .halg.digestsize = SHA512_DIGEST_SIZE,
2959 .halg.statesize = sizeof(struct talitos_export_state),
2961 .cra_name = "hmac(sha512)",
2962 .cra_driver_name = "hmac-sha512-talitos",
2963 .cra_blocksize = SHA512_BLOCK_SIZE,
2964 .cra_flags = CRYPTO_ALG_ASYNC,
2967 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2968 DESC_HDR_SEL0_MDEUB |
2969 DESC_HDR_MODE0_MDEUB_SHA512,
2973 struct talitos_crypto_alg {
2974 struct list_head entry;
2976 struct talitos_alg_template algt;
2979 static int talitos_init_common(struct talitos_ctx *ctx,
2980 struct talitos_crypto_alg *talitos_alg)
2982 struct talitos_private *priv;
2984 /* update context with ptr to dev */
2985 ctx->dev = talitos_alg->dev;
2987 /* assign SEC channel to tfm in round-robin fashion */
2988 priv = dev_get_drvdata(ctx->dev);
2989 ctx->ch = atomic_inc_return(&priv->last_chan) &
2990 (priv->num_channels - 1);
2992 /* copy descriptor header template value */
2993 ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
2995 /* select done notification */
2996 ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY;
3001 static int talitos_cra_init(struct crypto_tfm *tfm)
3003 struct crypto_alg *alg = tfm->__crt_alg;
3004 struct talitos_crypto_alg *talitos_alg;
3005 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
3007 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
3008 talitos_alg = container_of(__crypto_ahash_alg(alg),
3009 struct talitos_crypto_alg,
3012 talitos_alg = container_of(alg, struct talitos_crypto_alg,
3015 return talitos_init_common(ctx, talitos_alg);
3018 static int talitos_cra_init_aead(struct crypto_aead *tfm)
3020 struct aead_alg *alg = crypto_aead_alg(tfm);
3021 struct talitos_crypto_alg *talitos_alg;
3022 struct talitos_ctx *ctx = crypto_aead_ctx(tfm);
3024 talitos_alg = container_of(alg, struct talitos_crypto_alg,
3027 return talitos_init_common(ctx, talitos_alg);
3030 static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
3032 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
3034 talitos_cra_init(tfm);
3037 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
3038 sizeof(struct talitos_ahash_req_ctx));
3043 static void talitos_cra_exit(struct crypto_tfm *tfm)
3045 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
3046 struct device *dev = ctx->dev;
3049 dma_unmap_single(dev, ctx->dma_key, ctx->keylen, DMA_TO_DEVICE);
3053 * given the alg's descriptor header template, determine whether descriptor
3054 * type and primary/secondary execution units required match the hw
3055 * capabilities description provided in the device tree node.
3057 static int hw_supports(struct device *dev, __be32 desc_hdr_template)
3059 struct talitos_private *priv = dev_get_drvdata(dev);
3062 ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
3063 (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
3065 if (SECONDARY_EU(desc_hdr_template))
3066 ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
3067 & priv->exec_units);
3072 static int talitos_remove(struct platform_device *ofdev)
3074 struct device *dev = &ofdev->dev;
3075 struct talitos_private *priv = dev_get_drvdata(dev);
3076 struct talitos_crypto_alg *t_alg, *n;
3079 list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
3080 switch (t_alg->algt.type) {
3081 case CRYPTO_ALG_TYPE_ABLKCIPHER:
3083 case CRYPTO_ALG_TYPE_AEAD:
3084 crypto_unregister_aead(&t_alg->algt.alg.aead);
3086 case CRYPTO_ALG_TYPE_AHASH:
3087 crypto_unregister_ahash(&t_alg->algt.alg.hash);
3090 list_del(&t_alg->entry);
3093 if (hw_supports(dev, DESC_HDR_SEL0_RNG))
3094 talitos_unregister_rng(dev);
3096 for (i = 0; i < 2; i++)
3098 free_irq(priv->irq[i], dev);
3099 irq_dispose_mapping(priv->irq[i]);
3102 tasklet_kill(&priv->done_task[0]);
3104 tasklet_kill(&priv->done_task[1]);
3109 static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
3110 struct talitos_alg_template
3113 struct talitos_private *priv = dev_get_drvdata(dev);
3114 struct talitos_crypto_alg *t_alg;
3115 struct crypto_alg *alg;
3117 t_alg = devm_kzalloc(dev, sizeof(struct talitos_crypto_alg),
3120 return ERR_PTR(-ENOMEM);
3122 t_alg->algt = *template;
3124 switch (t_alg->algt.type) {
3125 case CRYPTO_ALG_TYPE_ABLKCIPHER:
3126 alg = &t_alg->algt.alg.crypto;
3127 alg->cra_init = talitos_cra_init;
3128 alg->cra_exit = talitos_cra_exit;
3129 alg->cra_type = &crypto_ablkcipher_type;
3130 alg->cra_ablkcipher.setkey = ablkcipher_setkey;
3131 alg->cra_ablkcipher.encrypt = ablkcipher_encrypt;
3132 alg->cra_ablkcipher.decrypt = ablkcipher_decrypt;
3133 alg->cra_ablkcipher.geniv = "eseqiv";
3135 case CRYPTO_ALG_TYPE_AEAD:
3136 alg = &t_alg->algt.alg.aead.base;
3137 alg->cra_exit = talitos_cra_exit;
3138 t_alg->algt.alg.aead.init = talitos_cra_init_aead;
3139 t_alg->algt.alg.aead.setkey = aead_setkey;
3140 t_alg->algt.alg.aead.encrypt = aead_encrypt;
3141 t_alg->algt.alg.aead.decrypt = aead_decrypt;
3142 if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
3143 !strncmp(alg->cra_name, "authenc(hmac(sha224)", 20)) {
3144 devm_kfree(dev, t_alg);
3145 return ERR_PTR(-ENOTSUPP);
3148 case CRYPTO_ALG_TYPE_AHASH:
3149 alg = &t_alg->algt.alg.hash.halg.base;
3150 alg->cra_init = talitos_cra_init_ahash;
3151 alg->cra_exit = talitos_cra_exit;
3152 t_alg->algt.alg.hash.init = ahash_init;
3153 t_alg->algt.alg.hash.update = ahash_update;
3154 t_alg->algt.alg.hash.final = ahash_final;
3155 t_alg->algt.alg.hash.finup = ahash_finup;
3156 t_alg->algt.alg.hash.digest = ahash_digest;
3157 if (!strncmp(alg->cra_name, "hmac", 4))
3158 t_alg->algt.alg.hash.setkey = ahash_setkey;
3159 t_alg->algt.alg.hash.import = ahash_import;
3160 t_alg->algt.alg.hash.export = ahash_export;
3162 if (!(priv->features & TALITOS_FTR_HMAC_OK) &&
3163 !strncmp(alg->cra_name, "hmac", 4)) {
3164 devm_kfree(dev, t_alg);
3165 return ERR_PTR(-ENOTSUPP);
3167 if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
3168 (!strcmp(alg->cra_name, "sha224") ||
3169 !strcmp(alg->cra_name, "hmac(sha224)"))) {
3170 t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
3171 t_alg->algt.desc_hdr_template =
3172 DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
3173 DESC_HDR_SEL0_MDEUA |
3174 DESC_HDR_MODE0_MDEU_SHA256;
3178 dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
3179 devm_kfree(dev, t_alg);
3180 return ERR_PTR(-EINVAL);
3183 alg->cra_module = THIS_MODULE;
3184 if (t_alg->algt.priority)
3185 alg->cra_priority = t_alg->algt.priority;
3187 alg->cra_priority = TALITOS_CRA_PRIORITY;
3188 if (has_ftr_sec1(priv))
3189 alg->cra_alignmask = 3;
3191 alg->cra_alignmask = 0;
3192 alg->cra_ctxsize = sizeof(struct talitos_ctx);
3193 alg->cra_flags |= CRYPTO_ALG_KERN_DRIVER_ONLY;
3200 static int talitos_probe_irq(struct platform_device *ofdev)
3202 struct device *dev = &ofdev->dev;
3203 struct device_node *np = ofdev->dev.of_node;
3204 struct talitos_private *priv = dev_get_drvdata(dev);
3206 bool is_sec1 = has_ftr_sec1(priv);
3208 priv->irq[0] = irq_of_parse_and_map(np, 0);
3209 if (!priv->irq[0]) {
3210 dev_err(dev, "failed to map irq\n");
3214 err = request_irq(priv->irq[0], talitos1_interrupt_4ch, 0,
3215 dev_driver_string(dev), dev);
3219 priv->irq[1] = irq_of_parse_and_map(np, 1);
3221 /* get the primary irq line */
3222 if (!priv->irq[1]) {
3223 err = request_irq(priv->irq[0], talitos2_interrupt_4ch, 0,
3224 dev_driver_string(dev), dev);
3228 err = request_irq(priv->irq[0], talitos2_interrupt_ch0_2, 0,
3229 dev_driver_string(dev), dev);
3233 /* get the secondary irq line */
3234 err = request_irq(priv->irq[1], talitos2_interrupt_ch1_3, 0,
3235 dev_driver_string(dev), dev);
3237 dev_err(dev, "failed to request secondary irq\n");
3238 irq_dispose_mapping(priv->irq[1]);
3246 dev_err(dev, "failed to request primary irq\n");
3247 irq_dispose_mapping(priv->irq[0]);
3254 static int talitos_probe(struct platform_device *ofdev)
3256 struct device *dev = &ofdev->dev;
3257 struct device_node *np = ofdev->dev.of_node;
3258 struct talitos_private *priv;
3261 struct resource *res;
3263 priv = devm_kzalloc(dev, sizeof(struct talitos_private), GFP_KERNEL);
3267 INIT_LIST_HEAD(&priv->alg_list);
3269 dev_set_drvdata(dev, priv);
3271 priv->ofdev = ofdev;
3273 spin_lock_init(&priv->reg_lock);
3275 res = platform_get_resource(ofdev, IORESOURCE_MEM, 0);
3278 priv->reg = devm_ioremap(dev, res->start, resource_size(res));
3280 dev_err(dev, "failed to of_iomap\n");
3285 /* get SEC version capabilities from device tree */
3286 of_property_read_u32(np, "fsl,num-channels", &priv->num_channels);
3287 of_property_read_u32(np, "fsl,channel-fifo-len", &priv->chfifo_len);
3288 of_property_read_u32(np, "fsl,exec-units-mask", &priv->exec_units);
3289 of_property_read_u32(np, "fsl,descriptor-types-mask",
3292 if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
3293 !priv->exec_units || !priv->desc_types) {
3294 dev_err(dev, "invalid property data in device tree node\n");
3299 if (of_device_is_compatible(np, "fsl,sec3.0"))
3300 priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
3302 if (of_device_is_compatible(np, "fsl,sec2.1"))
3303 priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
3304 TALITOS_FTR_SHA224_HWINIT |
3305 TALITOS_FTR_HMAC_OK;
3307 if (of_device_is_compatible(np, "fsl,sec1.0"))
3308 priv->features |= TALITOS_FTR_SEC1;
3310 if (of_device_is_compatible(np, "fsl,sec1.2")) {
3311 priv->reg_deu = priv->reg + TALITOS12_DEU;
3312 priv->reg_aesu = priv->reg + TALITOS12_AESU;
3313 priv->reg_mdeu = priv->reg + TALITOS12_MDEU;
3314 stride = TALITOS1_CH_STRIDE;
3315 } else if (of_device_is_compatible(np, "fsl,sec1.0")) {
3316 priv->reg_deu = priv->reg + TALITOS10_DEU;
3317 priv->reg_aesu = priv->reg + TALITOS10_AESU;
3318 priv->reg_mdeu = priv->reg + TALITOS10_MDEU;
3319 priv->reg_afeu = priv->reg + TALITOS10_AFEU;
3320 priv->reg_rngu = priv->reg + TALITOS10_RNGU;
3321 priv->reg_pkeu = priv->reg + TALITOS10_PKEU;
3322 stride = TALITOS1_CH_STRIDE;
3324 priv->reg_deu = priv->reg + TALITOS2_DEU;
3325 priv->reg_aesu = priv->reg + TALITOS2_AESU;
3326 priv->reg_mdeu = priv->reg + TALITOS2_MDEU;
3327 priv->reg_afeu = priv->reg + TALITOS2_AFEU;
3328 priv->reg_rngu = priv->reg + TALITOS2_RNGU;
3329 priv->reg_pkeu = priv->reg + TALITOS2_PKEU;
3330 priv->reg_keu = priv->reg + TALITOS2_KEU;
3331 priv->reg_crcu = priv->reg + TALITOS2_CRCU;
3332 stride = TALITOS2_CH_STRIDE;
3335 err = talitos_probe_irq(ofdev);
3339 if (of_device_is_compatible(np, "fsl,sec1.0")) {
3340 if (priv->num_channels == 1)
3341 tasklet_init(&priv->done_task[0], talitos1_done_ch0,
3342 (unsigned long)dev);
3344 tasklet_init(&priv->done_task[0], talitos1_done_4ch,
3345 (unsigned long)dev);
3348 tasklet_init(&priv->done_task[0], talitos2_done_ch0_2,
3349 (unsigned long)dev);
3350 tasklet_init(&priv->done_task[1], talitos2_done_ch1_3,
3351 (unsigned long)dev);
3352 } else if (priv->num_channels == 1) {
3353 tasklet_init(&priv->done_task[0], talitos2_done_ch0,
3354 (unsigned long)dev);
3356 tasklet_init(&priv->done_task[0], talitos2_done_4ch,
3357 (unsigned long)dev);
3361 priv->chan = devm_kcalloc(dev,
3363 sizeof(struct talitos_channel),
3366 dev_err(dev, "failed to allocate channel management space\n");
3371 priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
3373 for (i = 0; i < priv->num_channels; i++) {
3374 priv->chan[i].reg = priv->reg + stride * (i + 1);
3375 if (!priv->irq[1] || !(i & 1))
3376 priv->chan[i].reg += TALITOS_CH_BASE_OFFSET;
3378 spin_lock_init(&priv->chan[i].head_lock);
3379 spin_lock_init(&priv->chan[i].tail_lock);
3381 priv->chan[i].fifo = devm_kcalloc(dev,
3383 sizeof(struct talitos_request),
3385 if (!priv->chan[i].fifo) {
3386 dev_err(dev, "failed to allocate request fifo %d\n", i);
3391 atomic_set(&priv->chan[i].submit_count,
3392 -(priv->chfifo_len - 1));
3395 dma_set_mask(dev, DMA_BIT_MASK(36));
3397 /* reset and initialize the h/w */
3398 err = init_device(dev);
3400 dev_err(dev, "failed to initialize device\n");
3404 /* register the RNG, if available */
3405 if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
3406 err = talitos_register_rng(dev);
3408 dev_err(dev, "failed to register hwrng: %d\n", err);
3411 dev_info(dev, "hwrng\n");
3414 /* register crypto algorithms the device supports */
3415 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
3416 if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
3417 struct talitos_crypto_alg *t_alg;
3418 struct crypto_alg *alg = NULL;
3420 t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
3421 if (IS_ERR(t_alg)) {
3422 err = PTR_ERR(t_alg);
3423 if (err == -ENOTSUPP)
3428 switch (t_alg->algt.type) {
3429 case CRYPTO_ALG_TYPE_ABLKCIPHER:
3430 err = crypto_register_alg(
3431 &t_alg->algt.alg.crypto);
3432 alg = &t_alg->algt.alg.crypto;
3435 case CRYPTO_ALG_TYPE_AEAD:
3436 err = crypto_register_aead(
3437 &t_alg->algt.alg.aead);
3438 alg = &t_alg->algt.alg.aead.base;
3441 case CRYPTO_ALG_TYPE_AHASH:
3442 err = crypto_register_ahash(
3443 &t_alg->algt.alg.hash);
3444 alg = &t_alg->algt.alg.hash.halg.base;
3448 dev_err(dev, "%s alg registration failed\n",
3449 alg->cra_driver_name);
3450 devm_kfree(dev, t_alg);
3452 list_add_tail(&t_alg->entry, &priv->alg_list);
3455 if (!list_empty(&priv->alg_list))
3456 dev_info(dev, "%s algorithms registered in /proc/crypto\n",
3457 (char *)of_get_property(np, "compatible", NULL));
3462 talitos_remove(ofdev);
3467 static const struct of_device_id talitos_match[] = {
3468 #ifdef CONFIG_CRYPTO_DEV_TALITOS1
3470 .compatible = "fsl,sec1.0",
3473 #ifdef CONFIG_CRYPTO_DEV_TALITOS2
3475 .compatible = "fsl,sec2.0",
3480 MODULE_DEVICE_TABLE(of, talitos_match);
3482 static struct platform_driver talitos_driver = {
3485 .of_match_table = talitos_match,
3487 .probe = talitos_probe,
3488 .remove = talitos_remove,
3491 module_platform_driver(talitos_driver);
3493 MODULE_LICENSE("GPL");
3494 MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
3495 MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");