crypto: talitos - Refactor the sg in/out chain allocation
[linux/fpc-iii.git] / drivers / crypto / talitos.c
blob5a7e345c523b43763156229ebdb7e2178e6a0d7d
1 /*
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>
39 #include <linux/io.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/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>
56 #include "talitos.h"
58 static void to_talitos_ptr(struct talitos_ptr *talitos_ptr, dma_addr_t dma_addr)
60 talitos_ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr));
61 talitos_ptr->eptr = upper_32_bits(dma_addr);
65 * map virtual single (contiguous) pointer to h/w descriptor pointer
67 static void map_single_talitos_ptr(struct device *dev,
68 struct talitos_ptr *talitos_ptr,
69 unsigned short len, void *data,
70 unsigned char extent,
71 enum dma_data_direction dir)
73 dma_addr_t dma_addr = dma_map_single(dev, data, len, dir);
75 talitos_ptr->len = cpu_to_be16(len);
76 to_talitos_ptr(talitos_ptr, dma_addr);
77 talitos_ptr->j_extent = extent;
81 * unmap bus single (contiguous) h/w descriptor pointer
83 static void unmap_single_talitos_ptr(struct device *dev,
84 struct talitos_ptr *talitos_ptr,
85 enum dma_data_direction dir)
87 dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
88 be16_to_cpu(talitos_ptr->len), dir);
91 static int reset_channel(struct device *dev, int ch)
93 struct talitos_private *priv = dev_get_drvdata(dev);
94 unsigned int timeout = TALITOS_TIMEOUT;
96 setbits32(priv->chan[ch].reg + TALITOS_CCCR, TALITOS_CCCR_RESET);
98 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) & TALITOS_CCCR_RESET)
99 && --timeout)
100 cpu_relax();
102 if (timeout == 0) {
103 dev_err(dev, "failed to reset channel %d\n", ch);
104 return -EIO;
107 /* set 36-bit addressing, done writeback enable and done IRQ enable */
108 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, TALITOS_CCCR_LO_EAE |
109 TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
111 /* and ICCR writeback, if available */
112 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
113 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO,
114 TALITOS_CCCR_LO_IWSE);
116 return 0;
119 static int reset_device(struct device *dev)
121 struct talitos_private *priv = dev_get_drvdata(dev);
122 unsigned int timeout = TALITOS_TIMEOUT;
123 u32 mcr = TALITOS_MCR_SWR;
125 setbits32(priv->reg + TALITOS_MCR, mcr);
127 while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
128 && --timeout)
129 cpu_relax();
131 if (priv->irq[1]) {
132 mcr = TALITOS_MCR_RCA1 | TALITOS_MCR_RCA3;
133 setbits32(priv->reg + TALITOS_MCR, mcr);
136 if (timeout == 0) {
137 dev_err(dev, "failed to reset device\n");
138 return -EIO;
141 return 0;
145 * Reset and initialize the device
147 static int init_device(struct device *dev)
149 struct talitos_private *priv = dev_get_drvdata(dev);
150 int ch, err;
153 * Master reset
154 * errata documentation: warning: certain SEC interrupts
155 * are not fully cleared by writing the MCR:SWR bit,
156 * set bit twice to completely reset
158 err = reset_device(dev);
159 if (err)
160 return err;
162 err = reset_device(dev);
163 if (err)
164 return err;
166 /* reset channels */
167 for (ch = 0; ch < priv->num_channels; ch++) {
168 err = reset_channel(dev, ch);
169 if (err)
170 return err;
173 /* enable channel done and error interrupts */
174 setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
175 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
177 /* disable integrity check error interrupts (use writeback instead) */
178 if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
179 setbits32(priv->reg + TALITOS_MDEUICR_LO,
180 TALITOS_MDEUICR_LO_ICE);
182 return 0;
186 * talitos_submit - submits a descriptor to the device for processing
187 * @dev: the SEC device to be used
188 * @ch: the SEC device channel to be used
189 * @desc: the descriptor to be processed by the device
190 * @callback: whom to call when processing is complete
191 * @context: a handle for use by caller (optional)
193 * desc must contain valid dma-mapped (bus physical) address pointers.
194 * callback must check err and feedback in descriptor header
195 * for device processing status.
197 int talitos_submit(struct device *dev, int ch, struct talitos_desc *desc,
198 void (*callback)(struct device *dev,
199 struct talitos_desc *desc,
200 void *context, int error),
201 void *context)
203 struct talitos_private *priv = dev_get_drvdata(dev);
204 struct talitos_request *request;
205 unsigned long flags;
206 int head;
208 spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
210 if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
211 /* h/w fifo is full */
212 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
213 return -EAGAIN;
216 head = priv->chan[ch].head;
217 request = &priv->chan[ch].fifo[head];
219 /* map descriptor and save caller data */
220 request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
221 DMA_BIDIRECTIONAL);
222 request->callback = callback;
223 request->context = context;
225 /* increment fifo head */
226 priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
228 smp_wmb();
229 request->desc = desc;
231 /* GO! */
232 wmb();
233 out_be32(priv->chan[ch].reg + TALITOS_FF,
234 upper_32_bits(request->dma_desc));
235 out_be32(priv->chan[ch].reg + TALITOS_FF_LO,
236 lower_32_bits(request->dma_desc));
238 spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
240 return -EINPROGRESS;
242 EXPORT_SYMBOL(talitos_submit);
245 * process what was done, notify callback of error if not
247 static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
249 struct talitos_private *priv = dev_get_drvdata(dev);
250 struct talitos_request *request, saved_req;
251 unsigned long flags;
252 int tail, status;
254 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
256 tail = priv->chan[ch].tail;
257 while (priv->chan[ch].fifo[tail].desc) {
258 request = &priv->chan[ch].fifo[tail];
260 /* descriptors with their done bits set don't get the error */
261 rmb();
262 if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
263 status = 0;
264 else
265 if (!error)
266 break;
267 else
268 status = error;
270 dma_unmap_single(dev, request->dma_desc,
271 sizeof(struct talitos_desc),
272 DMA_BIDIRECTIONAL);
274 /* copy entries so we can call callback outside lock */
275 saved_req.desc = request->desc;
276 saved_req.callback = request->callback;
277 saved_req.context = request->context;
279 /* release request entry in fifo */
280 smp_wmb();
281 request->desc = NULL;
283 /* increment fifo tail */
284 priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
286 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
288 atomic_dec(&priv->chan[ch].submit_count);
290 saved_req.callback(dev, saved_req.desc, saved_req.context,
291 status);
292 /* channel may resume processing in single desc error case */
293 if (error && !reset_ch && status == error)
294 return;
295 spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
296 tail = priv->chan[ch].tail;
299 spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
303 * process completed requests for channels that have done status
305 #define DEF_TALITOS_DONE(name, ch_done_mask) \
306 static void talitos_done_##name(unsigned long data) \
308 struct device *dev = (struct device *)data; \
309 struct talitos_private *priv = dev_get_drvdata(dev); \
310 unsigned long flags; \
312 if (ch_done_mask & 1) \
313 flush_channel(dev, 0, 0, 0); \
314 if (priv->num_channels == 1) \
315 goto out; \
316 if (ch_done_mask & (1 << 2)) \
317 flush_channel(dev, 1, 0, 0); \
318 if (ch_done_mask & (1 << 4)) \
319 flush_channel(dev, 2, 0, 0); \
320 if (ch_done_mask & (1 << 6)) \
321 flush_channel(dev, 3, 0, 0); \
323 out: \
324 /* At this point, all completed channels have been processed */ \
325 /* Unmask done interrupts for channels completed later on. */ \
326 spin_lock_irqsave(&priv->reg_lock, flags); \
327 setbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
328 setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT); \
329 spin_unlock_irqrestore(&priv->reg_lock, flags); \
331 DEF_TALITOS_DONE(4ch, TALITOS_ISR_4CHDONE)
332 DEF_TALITOS_DONE(ch0_2, TALITOS_ISR_CH_0_2_DONE)
333 DEF_TALITOS_DONE(ch1_3, TALITOS_ISR_CH_1_3_DONE)
336 * locate current (offending) descriptor
338 static u32 current_desc_hdr(struct device *dev, int ch)
340 struct talitos_private *priv = dev_get_drvdata(dev);
341 int tail, iter;
342 dma_addr_t cur_desc;
344 cur_desc = ((u64)in_be32(priv->chan[ch].reg + TALITOS_CDPR)) << 32;
345 cur_desc |= in_be32(priv->chan[ch].reg + TALITOS_CDPR_LO);
347 if (!cur_desc) {
348 dev_err(dev, "CDPR is NULL, giving up search for offending descriptor\n");
349 return 0;
352 tail = priv->chan[ch].tail;
354 iter = tail;
355 while (priv->chan[ch].fifo[iter].dma_desc != cur_desc) {
356 iter = (iter + 1) & (priv->fifo_len - 1);
357 if (iter == tail) {
358 dev_err(dev, "couldn't locate current descriptor\n");
359 return 0;
363 return priv->chan[ch].fifo[iter].desc->hdr;
367 * user diagnostics; report root cause of error based on execution unit status
369 static void report_eu_error(struct device *dev, int ch, u32 desc_hdr)
371 struct talitos_private *priv = dev_get_drvdata(dev);
372 int i;
374 if (!desc_hdr)
375 desc_hdr = in_be32(priv->chan[ch].reg + TALITOS_DESCBUF);
377 switch (desc_hdr & DESC_HDR_SEL0_MASK) {
378 case DESC_HDR_SEL0_AFEU:
379 dev_err(dev, "AFEUISR 0x%08x_%08x\n",
380 in_be32(priv->reg + TALITOS_AFEUISR),
381 in_be32(priv->reg + TALITOS_AFEUISR_LO));
382 break;
383 case DESC_HDR_SEL0_DEU:
384 dev_err(dev, "DEUISR 0x%08x_%08x\n",
385 in_be32(priv->reg + TALITOS_DEUISR),
386 in_be32(priv->reg + TALITOS_DEUISR_LO));
387 break;
388 case DESC_HDR_SEL0_MDEUA:
389 case DESC_HDR_SEL0_MDEUB:
390 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
391 in_be32(priv->reg + TALITOS_MDEUISR),
392 in_be32(priv->reg + TALITOS_MDEUISR_LO));
393 break;
394 case DESC_HDR_SEL0_RNG:
395 dev_err(dev, "RNGUISR 0x%08x_%08x\n",
396 in_be32(priv->reg + TALITOS_RNGUISR),
397 in_be32(priv->reg + TALITOS_RNGUISR_LO));
398 break;
399 case DESC_HDR_SEL0_PKEU:
400 dev_err(dev, "PKEUISR 0x%08x_%08x\n",
401 in_be32(priv->reg + TALITOS_PKEUISR),
402 in_be32(priv->reg + TALITOS_PKEUISR_LO));
403 break;
404 case DESC_HDR_SEL0_AESU:
405 dev_err(dev, "AESUISR 0x%08x_%08x\n",
406 in_be32(priv->reg + TALITOS_AESUISR),
407 in_be32(priv->reg + TALITOS_AESUISR_LO));
408 break;
409 case DESC_HDR_SEL0_CRCU:
410 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
411 in_be32(priv->reg + TALITOS_CRCUISR),
412 in_be32(priv->reg + TALITOS_CRCUISR_LO));
413 break;
414 case DESC_HDR_SEL0_KEU:
415 dev_err(dev, "KEUISR 0x%08x_%08x\n",
416 in_be32(priv->reg + TALITOS_KEUISR),
417 in_be32(priv->reg + TALITOS_KEUISR_LO));
418 break;
421 switch (desc_hdr & DESC_HDR_SEL1_MASK) {
422 case DESC_HDR_SEL1_MDEUA:
423 case DESC_HDR_SEL1_MDEUB:
424 dev_err(dev, "MDEUISR 0x%08x_%08x\n",
425 in_be32(priv->reg + TALITOS_MDEUISR),
426 in_be32(priv->reg + TALITOS_MDEUISR_LO));
427 break;
428 case DESC_HDR_SEL1_CRCU:
429 dev_err(dev, "CRCUISR 0x%08x_%08x\n",
430 in_be32(priv->reg + TALITOS_CRCUISR),
431 in_be32(priv->reg + TALITOS_CRCUISR_LO));
432 break;
435 for (i = 0; i < 8; i++)
436 dev_err(dev, "DESCBUF 0x%08x_%08x\n",
437 in_be32(priv->chan[ch].reg + TALITOS_DESCBUF + 8*i),
438 in_be32(priv->chan[ch].reg + TALITOS_DESCBUF_LO + 8*i));
442 * recover from error interrupts
444 static void talitos_error(struct device *dev, u32 isr, u32 isr_lo)
446 struct talitos_private *priv = dev_get_drvdata(dev);
447 unsigned int timeout = TALITOS_TIMEOUT;
448 int ch, error, reset_dev = 0, reset_ch = 0;
449 u32 v, v_lo;
451 for (ch = 0; ch < priv->num_channels; ch++) {
452 /* skip channels without errors */
453 if (!(isr & (1 << (ch * 2 + 1))))
454 continue;
456 error = -EINVAL;
458 v = in_be32(priv->chan[ch].reg + TALITOS_CCPSR);
459 v_lo = in_be32(priv->chan[ch].reg + TALITOS_CCPSR_LO);
461 if (v_lo & TALITOS_CCPSR_LO_DOF) {
462 dev_err(dev, "double fetch fifo overflow error\n");
463 error = -EAGAIN;
464 reset_ch = 1;
466 if (v_lo & TALITOS_CCPSR_LO_SOF) {
467 /* h/w dropped descriptor */
468 dev_err(dev, "single fetch fifo overflow error\n");
469 error = -EAGAIN;
471 if (v_lo & TALITOS_CCPSR_LO_MDTE)
472 dev_err(dev, "master data transfer error\n");
473 if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
474 dev_err(dev, "s/g data length zero error\n");
475 if (v_lo & TALITOS_CCPSR_LO_FPZ)
476 dev_err(dev, "fetch pointer zero error\n");
477 if (v_lo & TALITOS_CCPSR_LO_IDH)
478 dev_err(dev, "illegal descriptor header error\n");
479 if (v_lo & TALITOS_CCPSR_LO_IEU)
480 dev_err(dev, "invalid execution unit error\n");
481 if (v_lo & TALITOS_CCPSR_LO_EU)
482 report_eu_error(dev, ch, current_desc_hdr(dev, ch));
483 if (v_lo & TALITOS_CCPSR_LO_GB)
484 dev_err(dev, "gather boundary error\n");
485 if (v_lo & TALITOS_CCPSR_LO_GRL)
486 dev_err(dev, "gather return/length error\n");
487 if (v_lo & TALITOS_CCPSR_LO_SB)
488 dev_err(dev, "scatter boundary error\n");
489 if (v_lo & TALITOS_CCPSR_LO_SRL)
490 dev_err(dev, "scatter return/length error\n");
492 flush_channel(dev, ch, error, reset_ch);
494 if (reset_ch) {
495 reset_channel(dev, ch);
496 } else {
497 setbits32(priv->chan[ch].reg + TALITOS_CCCR,
498 TALITOS_CCCR_CONT);
499 setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, 0);
500 while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) &
501 TALITOS_CCCR_CONT) && --timeout)
502 cpu_relax();
503 if (timeout == 0) {
504 dev_err(dev, "failed to restart channel %d\n",
505 ch);
506 reset_dev = 1;
510 if (reset_dev || isr & ~TALITOS_ISR_4CHERR || isr_lo) {
511 dev_err(dev, "done overflow, internal time out, or rngu error: "
512 "ISR 0x%08x_%08x\n", isr, isr_lo);
514 /* purge request queues */
515 for (ch = 0; ch < priv->num_channels; ch++)
516 flush_channel(dev, ch, -EIO, 1);
518 /* reset and reinitialize the device */
519 init_device(dev);
523 #define DEF_TALITOS_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet) \
524 static irqreturn_t talitos_interrupt_##name(int irq, void *data) \
526 struct device *dev = data; \
527 struct talitos_private *priv = dev_get_drvdata(dev); \
528 u32 isr, isr_lo; \
529 unsigned long flags; \
531 spin_lock_irqsave(&priv->reg_lock, flags); \
532 isr = in_be32(priv->reg + TALITOS_ISR); \
533 isr_lo = in_be32(priv->reg + TALITOS_ISR_LO); \
534 /* Acknowledge interrupt */ \
535 out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
536 out_be32(priv->reg + TALITOS_ICR_LO, isr_lo); \
538 if (unlikely(isr & ch_err_mask || isr_lo)) { \
539 spin_unlock_irqrestore(&priv->reg_lock, flags); \
540 talitos_error(dev, isr & ch_err_mask, isr_lo); \
542 else { \
543 if (likely(isr & ch_done_mask)) { \
544 /* mask further done interrupts. */ \
545 clrbits32(priv->reg + TALITOS_IMR, ch_done_mask); \
546 /* done_task will unmask done interrupts at exit */ \
547 tasklet_schedule(&priv->done_task[tlet]); \
549 spin_unlock_irqrestore(&priv->reg_lock, flags); \
552 return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED : \
553 IRQ_NONE; \
555 DEF_TALITOS_INTERRUPT(4ch, TALITOS_ISR_4CHDONE, TALITOS_ISR_4CHERR, 0)
556 DEF_TALITOS_INTERRUPT(ch0_2, TALITOS_ISR_CH_0_2_DONE, TALITOS_ISR_CH_0_2_ERR, 0)
557 DEF_TALITOS_INTERRUPT(ch1_3, TALITOS_ISR_CH_1_3_DONE, TALITOS_ISR_CH_1_3_ERR, 1)
560 * hwrng
562 static int talitos_rng_data_present(struct hwrng *rng, int wait)
564 struct device *dev = (struct device *)rng->priv;
565 struct talitos_private *priv = dev_get_drvdata(dev);
566 u32 ofl;
567 int i;
569 for (i = 0; i < 20; i++) {
570 ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
571 TALITOS_RNGUSR_LO_OFL;
572 if (ofl || !wait)
573 break;
574 udelay(10);
577 return !!ofl;
580 static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
582 struct device *dev = (struct device *)rng->priv;
583 struct talitos_private *priv = dev_get_drvdata(dev);
585 /* rng fifo requires 64-bit accesses */
586 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
587 *data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
589 return sizeof(u32);
592 static int talitos_rng_init(struct hwrng *rng)
594 struct device *dev = (struct device *)rng->priv;
595 struct talitos_private *priv = dev_get_drvdata(dev);
596 unsigned int timeout = TALITOS_TIMEOUT;
598 setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
599 while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
600 && --timeout)
601 cpu_relax();
602 if (timeout == 0) {
603 dev_err(dev, "failed to reset rng hw\n");
604 return -ENODEV;
607 /* start generating */
608 setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
610 return 0;
613 static int talitos_register_rng(struct device *dev)
615 struct talitos_private *priv = dev_get_drvdata(dev);
617 priv->rng.name = dev_driver_string(dev),
618 priv->rng.init = talitos_rng_init,
619 priv->rng.data_present = talitos_rng_data_present,
620 priv->rng.data_read = talitos_rng_data_read,
621 priv->rng.priv = (unsigned long)dev;
623 return hwrng_register(&priv->rng);
626 static void talitos_unregister_rng(struct device *dev)
628 struct talitos_private *priv = dev_get_drvdata(dev);
630 hwrng_unregister(&priv->rng);
634 * crypto alg
636 #define TALITOS_CRA_PRIORITY 3000
637 #define TALITOS_MAX_KEY_SIZE 96
638 #define TALITOS_MAX_IV_LENGTH 16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
640 struct talitos_ctx {
641 struct device *dev;
642 int ch;
643 __be32 desc_hdr_template;
644 u8 key[TALITOS_MAX_KEY_SIZE];
645 u8 iv[TALITOS_MAX_IV_LENGTH];
646 unsigned int keylen;
647 unsigned int enckeylen;
648 unsigned int authkeylen;
649 unsigned int authsize;
652 #define HASH_MAX_BLOCK_SIZE SHA512_BLOCK_SIZE
653 #define TALITOS_MDEU_MAX_CONTEXT_SIZE TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
655 struct talitos_ahash_req_ctx {
656 u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
657 unsigned int hw_context_size;
658 u8 buf[HASH_MAX_BLOCK_SIZE];
659 u8 bufnext[HASH_MAX_BLOCK_SIZE];
660 unsigned int swinit;
661 unsigned int first;
662 unsigned int last;
663 unsigned int to_hash_later;
664 u64 nbuf;
665 struct scatterlist bufsl[2];
666 struct scatterlist *psrc;
669 static int aead_setauthsize(struct crypto_aead *authenc,
670 unsigned int authsize)
672 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
674 ctx->authsize = authsize;
676 return 0;
679 static int aead_setkey(struct crypto_aead *authenc,
680 const u8 *key, unsigned int keylen)
682 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
683 struct crypto_authenc_keys keys;
685 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
686 goto badkey;
688 if (keys.authkeylen + keys.enckeylen > TALITOS_MAX_KEY_SIZE)
689 goto badkey;
691 memcpy(ctx->key, keys.authkey, keys.authkeylen);
692 memcpy(&ctx->key[keys.authkeylen], keys.enckey, keys.enckeylen);
694 ctx->keylen = keys.authkeylen + keys.enckeylen;
695 ctx->enckeylen = keys.enckeylen;
696 ctx->authkeylen = keys.authkeylen;
698 return 0;
700 badkey:
701 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
702 return -EINVAL;
706 * talitos_edesc - s/w-extended descriptor
707 * @assoc_nents: number of segments in associated data scatterlist
708 * @src_nents: number of segments in input scatterlist
709 * @dst_nents: number of segments in output scatterlist
710 * @assoc_chained: whether assoc is chained or not
711 * @src_chained: whether src is chained or not
712 * @dst_chained: whether dst is chained or not
713 * @iv_dma: dma address of iv for checking continuity and link table
714 * @dma_len: length of dma mapped link_tbl space
715 * @dma_link_tbl: bus physical address of link_tbl
716 * @desc: h/w descriptor
717 * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
719 * if decrypting (with authcheck), or either one of src_nents or dst_nents
720 * is greater than 1, an integrity check value is concatenated to the end
721 * of link_tbl data
723 struct talitos_edesc {
724 int assoc_nents;
725 int src_nents;
726 int dst_nents;
727 bool assoc_chained;
728 bool src_chained;
729 bool dst_chained;
730 dma_addr_t iv_dma;
731 int dma_len;
732 dma_addr_t dma_link_tbl;
733 struct talitos_desc desc;
734 struct talitos_ptr link_tbl[0];
737 static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
738 unsigned int nents, enum dma_data_direction dir,
739 bool chained)
741 if (unlikely(chained))
742 while (sg) {
743 dma_map_sg(dev, sg, 1, dir);
744 sg = sg_next(sg);
746 else
747 dma_map_sg(dev, sg, nents, dir);
748 return nents;
751 static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
752 enum dma_data_direction dir)
754 while (sg) {
755 dma_unmap_sg(dev, sg, 1, dir);
756 sg = sg_next(sg);
760 static void talitos_sg_unmap(struct device *dev,
761 struct talitos_edesc *edesc,
762 struct scatterlist *src,
763 struct scatterlist *dst)
765 unsigned int src_nents = edesc->src_nents ? : 1;
766 unsigned int dst_nents = edesc->dst_nents ? : 1;
768 if (src != dst) {
769 if (edesc->src_chained)
770 talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
771 else
772 dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
774 if (dst) {
775 if (edesc->dst_chained)
776 talitos_unmap_sg_chain(dev, dst,
777 DMA_FROM_DEVICE);
778 else
779 dma_unmap_sg(dev, dst, dst_nents,
780 DMA_FROM_DEVICE);
782 } else
783 if (edesc->src_chained)
784 talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
785 else
786 dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
789 static void ipsec_esp_unmap(struct device *dev,
790 struct talitos_edesc *edesc,
791 struct aead_request *areq)
793 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
794 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
795 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
796 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
798 if (edesc->assoc_chained)
799 talitos_unmap_sg_chain(dev, areq->assoc, DMA_TO_DEVICE);
800 else if (areq->assoclen)
801 /* assoc_nents counts also for IV in non-contiguous cases */
802 dma_unmap_sg(dev, areq->assoc,
803 edesc->assoc_nents ? edesc->assoc_nents - 1 : 1,
804 DMA_TO_DEVICE);
806 talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
808 if (edesc->dma_len)
809 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
810 DMA_BIDIRECTIONAL);
814 * ipsec_esp descriptor callbacks
816 static void ipsec_esp_encrypt_done(struct device *dev,
817 struct talitos_desc *desc, void *context,
818 int err)
820 struct aead_request *areq = context;
821 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
822 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
823 struct talitos_edesc *edesc;
824 struct scatterlist *sg;
825 void *icvdata;
827 edesc = container_of(desc, struct talitos_edesc, desc);
829 ipsec_esp_unmap(dev, edesc, areq);
831 /* copy the generated ICV to dst */
832 if (edesc->dst_nents) {
833 icvdata = &edesc->link_tbl[edesc->src_nents +
834 edesc->dst_nents + 2 +
835 edesc->assoc_nents];
836 sg = sg_last(areq->dst, edesc->dst_nents);
837 memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
838 icvdata, ctx->authsize);
841 kfree(edesc);
843 aead_request_complete(areq, err);
846 static void ipsec_esp_decrypt_swauth_done(struct device *dev,
847 struct talitos_desc *desc,
848 void *context, int err)
850 struct aead_request *req = context;
851 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
852 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
853 struct talitos_edesc *edesc;
854 struct scatterlist *sg;
855 void *icvdata;
857 edesc = container_of(desc, struct talitos_edesc, desc);
859 ipsec_esp_unmap(dev, edesc, req);
861 if (!err) {
862 /* auth check */
863 if (edesc->dma_len)
864 icvdata = &edesc->link_tbl[edesc->src_nents +
865 edesc->dst_nents + 2 +
866 edesc->assoc_nents];
867 else
868 icvdata = &edesc->link_tbl[0];
870 sg = sg_last(req->dst, edesc->dst_nents ? : 1);
871 err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
872 ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
875 kfree(edesc);
877 aead_request_complete(req, err);
880 static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
881 struct talitos_desc *desc,
882 void *context, int err)
884 struct aead_request *req = context;
885 struct talitos_edesc *edesc;
887 edesc = container_of(desc, struct talitos_edesc, desc);
889 ipsec_esp_unmap(dev, edesc, req);
891 /* check ICV auth status */
892 if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
893 DESC_HDR_LO_ICCR1_PASS))
894 err = -EBADMSG;
896 kfree(edesc);
898 aead_request_complete(req, err);
902 * convert scatterlist to SEC h/w link table format
903 * stop at cryptlen bytes
905 static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
906 int cryptlen, struct talitos_ptr *link_tbl_ptr)
908 int n_sg = sg_count;
910 while (n_sg--) {
911 to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg));
912 link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
913 link_tbl_ptr->j_extent = 0;
914 link_tbl_ptr++;
915 cryptlen -= sg_dma_len(sg);
916 sg = sg_next(sg);
919 /* adjust (decrease) last one (or two) entry's len to cryptlen */
920 link_tbl_ptr--;
921 while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
922 /* Empty this entry, and move to previous one */
923 cryptlen += be16_to_cpu(link_tbl_ptr->len);
924 link_tbl_ptr->len = 0;
925 sg_count--;
926 link_tbl_ptr--;
928 be16_add_cpu(&link_tbl_ptr->len, cryptlen);
930 /* tag end of link table */
931 link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
933 return sg_count;
937 * fill in and submit ipsec_esp descriptor
939 static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
940 u64 seq, void (*callback) (struct device *dev,
941 struct talitos_desc *desc,
942 void *context, int error))
944 struct crypto_aead *aead = crypto_aead_reqtfm(areq);
945 struct talitos_ctx *ctx = crypto_aead_ctx(aead);
946 struct device *dev = ctx->dev;
947 struct talitos_desc *desc = &edesc->desc;
948 unsigned int cryptlen = areq->cryptlen;
949 unsigned int authsize = ctx->authsize;
950 unsigned int ivsize = crypto_aead_ivsize(aead);
951 int sg_count, ret;
952 int sg_link_tbl_len;
954 /* hmac key */
955 map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
956 0, DMA_TO_DEVICE);
958 /* hmac data */
959 desc->ptr[1].len = cpu_to_be16(areq->assoclen + ivsize);
960 if (edesc->assoc_nents) {
961 int tbl_off = edesc->src_nents + edesc->dst_nents + 2;
962 struct talitos_ptr *tbl_ptr = &edesc->link_tbl[tbl_off];
964 to_talitos_ptr(&desc->ptr[1], edesc->dma_link_tbl + tbl_off *
965 sizeof(struct talitos_ptr));
966 desc->ptr[1].j_extent = DESC_PTR_LNKTBL_JUMP;
968 /* assoc_nents - 1 entries for assoc, 1 for IV */
969 sg_count = sg_to_link_tbl(areq->assoc, edesc->assoc_nents - 1,
970 areq->assoclen, tbl_ptr);
972 /* add IV to link table */
973 tbl_ptr += sg_count - 1;
974 tbl_ptr->j_extent = 0;
975 tbl_ptr++;
976 to_talitos_ptr(tbl_ptr, edesc->iv_dma);
977 tbl_ptr->len = cpu_to_be16(ivsize);
978 tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
980 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
981 edesc->dma_len, DMA_BIDIRECTIONAL);
982 } else {
983 if (areq->assoclen)
984 to_talitos_ptr(&desc->ptr[1],
985 sg_dma_address(areq->assoc));
986 else
987 to_talitos_ptr(&desc->ptr[1], edesc->iv_dma);
988 desc->ptr[1].j_extent = 0;
991 /* cipher iv */
992 to_talitos_ptr(&desc->ptr[2], edesc->iv_dma);
993 desc->ptr[2].len = cpu_to_be16(ivsize);
994 desc->ptr[2].j_extent = 0;
995 /* Sync needed for the aead_givencrypt case */
996 dma_sync_single_for_device(dev, edesc->iv_dma, ivsize, DMA_TO_DEVICE);
998 /* cipher key */
999 map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
1000 (char *)&ctx->key + ctx->authkeylen, 0,
1001 DMA_TO_DEVICE);
1004 * cipher in
1005 * map and adjust cipher len to aead request cryptlen.
1006 * extent is bytes of HMAC postpended to ciphertext,
1007 * typically 12 for ipsec
1009 desc->ptr[4].len = cpu_to_be16(cryptlen);
1010 desc->ptr[4].j_extent = authsize;
1012 sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1013 (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1014 : DMA_TO_DEVICE,
1015 edesc->src_chained);
1017 if (sg_count == 1) {
1018 to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src));
1019 } else {
1020 sg_link_tbl_len = cryptlen;
1022 if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
1023 sg_link_tbl_len = cryptlen + authsize;
1025 sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
1026 &edesc->link_tbl[0]);
1027 if (sg_count > 1) {
1028 desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
1029 to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl);
1030 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1031 edesc->dma_len,
1032 DMA_BIDIRECTIONAL);
1033 } else {
1034 /* Only one segment now, so no link tbl needed */
1035 to_talitos_ptr(&desc->ptr[4],
1036 sg_dma_address(areq->src));
1040 /* cipher out */
1041 desc->ptr[5].len = cpu_to_be16(cryptlen);
1042 desc->ptr[5].j_extent = authsize;
1044 if (areq->src != areq->dst)
1045 sg_count = talitos_map_sg(dev, areq->dst,
1046 edesc->dst_nents ? : 1,
1047 DMA_FROM_DEVICE, edesc->dst_chained);
1049 if (sg_count == 1) {
1050 to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst));
1051 } else {
1052 int tbl_off = edesc->src_nents + 1;
1053 struct talitos_ptr *tbl_ptr = &edesc->link_tbl[tbl_off];
1055 to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
1056 tbl_off * sizeof(struct talitos_ptr));
1057 sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
1058 tbl_ptr);
1060 /* Add an entry to the link table for ICV data */
1061 tbl_ptr += sg_count - 1;
1062 tbl_ptr->j_extent = 0;
1063 tbl_ptr++;
1064 tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
1065 tbl_ptr->len = cpu_to_be16(authsize);
1067 /* icv data follows link tables */
1068 to_talitos_ptr(tbl_ptr, edesc->dma_link_tbl +
1069 (tbl_off + edesc->dst_nents + 1 +
1070 edesc->assoc_nents) *
1071 sizeof(struct talitos_ptr));
1072 desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
1073 dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
1074 edesc->dma_len, DMA_BIDIRECTIONAL);
1077 /* iv out */
1078 map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
1079 DMA_FROM_DEVICE);
1081 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
1082 if (ret != -EINPROGRESS) {
1083 ipsec_esp_unmap(dev, edesc, areq);
1084 kfree(edesc);
1086 return ret;
1090 * derive number of elements in scatterlist
1092 static int sg_count(struct scatterlist *sg_list, int nbytes, bool *chained)
1094 struct scatterlist *sg = sg_list;
1095 int sg_nents = 0;
1097 *chained = false;
1098 while (nbytes > 0) {
1099 sg_nents++;
1100 nbytes -= sg->length;
1101 if (!sg_is_last(sg) && (sg + 1)->length == 0)
1102 *chained = true;
1103 sg = sg_next(sg);
1106 return sg_nents;
1110 * allocate and map the extended descriptor
1112 static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
1113 struct scatterlist *assoc,
1114 struct scatterlist *src,
1115 struct scatterlist *dst,
1116 u8 *iv,
1117 unsigned int assoclen,
1118 unsigned int cryptlen,
1119 unsigned int authsize,
1120 unsigned int ivsize,
1121 int icv_stashing,
1122 u32 cryptoflags,
1123 bool encrypt)
1125 struct talitos_edesc *edesc;
1126 int assoc_nents = 0, src_nents, dst_nents, alloc_len, dma_len;
1127 bool assoc_chained = false, src_chained = false, dst_chained = false;
1128 dma_addr_t iv_dma = 0;
1129 gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
1130 GFP_ATOMIC;
1132 if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
1133 dev_err(dev, "length exceeds h/w max limit\n");
1134 return ERR_PTR(-EINVAL);
1137 if (ivsize)
1138 iv_dma = dma_map_single(dev, iv, ivsize, DMA_TO_DEVICE);
1140 if (assoclen) {
1142 * Currently it is assumed that iv is provided whenever assoc
1143 * is.
1145 BUG_ON(!iv);
1147 assoc_nents = sg_count(assoc, assoclen, &assoc_chained);
1148 talitos_map_sg(dev, assoc, assoc_nents, DMA_TO_DEVICE,
1149 assoc_chained);
1150 assoc_nents = (assoc_nents == 1) ? 0 : assoc_nents;
1152 if (assoc_nents || sg_dma_address(assoc) + assoclen != iv_dma)
1153 assoc_nents = assoc_nents ? assoc_nents + 1 : 2;
1156 if (!dst || dst == src) {
1157 src_nents = sg_count(src, cryptlen + authsize, &src_chained);
1158 src_nents = (src_nents == 1) ? 0 : src_nents;
1159 dst_nents = dst ? src_nents : 0;
1160 } else { /* dst && dst != src*/
1161 src_nents = sg_count(src, cryptlen + (encrypt ? 0 : authsize),
1162 &src_chained);
1163 src_nents = (src_nents == 1) ? 0 : src_nents;
1164 dst_nents = sg_count(dst, cryptlen + (encrypt ? authsize : 0),
1165 &dst_chained);
1166 dst_nents = (dst_nents == 1) ? 0 : dst_nents;
1170 * allocate space for base edesc plus the link tables,
1171 * allowing for two separate entries for ICV and generated ICV (+ 2),
1172 * and the ICV data itself
1174 alloc_len = sizeof(struct talitos_edesc);
1175 if (assoc_nents || src_nents || dst_nents) {
1176 dma_len = (src_nents + dst_nents + 2 + assoc_nents) *
1177 sizeof(struct talitos_ptr) + authsize;
1178 alloc_len += dma_len;
1179 } else {
1180 dma_len = 0;
1181 alloc_len += icv_stashing ? authsize : 0;
1184 edesc = kmalloc(alloc_len, GFP_DMA | flags);
1185 if (!edesc) {
1186 if (assoc_chained)
1187 talitos_unmap_sg_chain(dev, assoc, DMA_TO_DEVICE);
1188 else if (assoclen)
1189 dma_unmap_sg(dev, assoc,
1190 assoc_nents ? assoc_nents - 1 : 1,
1191 DMA_TO_DEVICE);
1193 if (iv_dma)
1194 dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE);
1196 dev_err(dev, "could not allocate edescriptor\n");
1197 return ERR_PTR(-ENOMEM);
1200 edesc->assoc_nents = assoc_nents;
1201 edesc->src_nents = src_nents;
1202 edesc->dst_nents = dst_nents;
1203 edesc->assoc_chained = assoc_chained;
1204 edesc->src_chained = src_chained;
1205 edesc->dst_chained = dst_chained;
1206 edesc->iv_dma = iv_dma;
1207 edesc->dma_len = dma_len;
1208 if (dma_len)
1209 edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1210 edesc->dma_len,
1211 DMA_BIDIRECTIONAL);
1213 return edesc;
1216 static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq, u8 *iv,
1217 int icv_stashing, bool encrypt)
1219 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1220 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1221 unsigned int ivsize = crypto_aead_ivsize(authenc);
1223 return talitos_edesc_alloc(ctx->dev, areq->assoc, areq->src, areq->dst,
1224 iv, areq->assoclen, areq->cryptlen,
1225 ctx->authsize, ivsize, icv_stashing,
1226 areq->base.flags, encrypt);
1229 static int aead_encrypt(struct aead_request *req)
1231 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1232 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1233 struct talitos_edesc *edesc;
1235 /* allocate extended descriptor */
1236 edesc = aead_edesc_alloc(req, req->iv, 0, true);
1237 if (IS_ERR(edesc))
1238 return PTR_ERR(edesc);
1240 /* set encrypt */
1241 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
1243 return ipsec_esp(edesc, req, 0, ipsec_esp_encrypt_done);
1246 static int aead_decrypt(struct aead_request *req)
1248 struct crypto_aead *authenc = crypto_aead_reqtfm(req);
1249 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1250 unsigned int authsize = ctx->authsize;
1251 struct talitos_private *priv = dev_get_drvdata(ctx->dev);
1252 struct talitos_edesc *edesc;
1253 struct scatterlist *sg;
1254 void *icvdata;
1256 req->cryptlen -= authsize;
1258 /* allocate extended descriptor */
1259 edesc = aead_edesc_alloc(req, req->iv, 1, false);
1260 if (IS_ERR(edesc))
1261 return PTR_ERR(edesc);
1263 if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
1264 ((!edesc->src_nents && !edesc->dst_nents) ||
1265 priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
1267 /* decrypt and check the ICV */
1268 edesc->desc.hdr = ctx->desc_hdr_template |
1269 DESC_HDR_DIR_INBOUND |
1270 DESC_HDR_MODE1_MDEU_CICV;
1272 /* reset integrity check result bits */
1273 edesc->desc.hdr_lo = 0;
1275 return ipsec_esp(edesc, req, 0, ipsec_esp_decrypt_hwauth_done);
1278 /* Have to check the ICV with software */
1279 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1281 /* stash incoming ICV for later cmp with ICV generated by the h/w */
1282 if (edesc->dma_len)
1283 icvdata = &edesc->link_tbl[edesc->src_nents +
1284 edesc->dst_nents + 2 +
1285 edesc->assoc_nents];
1286 else
1287 icvdata = &edesc->link_tbl[0];
1289 sg = sg_last(req->src, edesc->src_nents ? : 1);
1291 memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
1292 ctx->authsize);
1294 return ipsec_esp(edesc, req, 0, ipsec_esp_decrypt_swauth_done);
1297 static int aead_givencrypt(struct aead_givcrypt_request *req)
1299 struct aead_request *areq = &req->areq;
1300 struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
1301 struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1302 struct talitos_edesc *edesc;
1304 /* allocate extended descriptor */
1305 edesc = aead_edesc_alloc(areq, req->giv, 0, true);
1306 if (IS_ERR(edesc))
1307 return PTR_ERR(edesc);
1309 /* set encrypt */
1310 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
1312 memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
1313 /* avoid consecutive packets going out with same IV */
1314 *(__be64 *)req->giv ^= cpu_to_be64(req->seq);
1316 return ipsec_esp(edesc, areq, req->seq, ipsec_esp_encrypt_done);
1319 static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
1320 const u8 *key, unsigned int keylen)
1322 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1324 memcpy(&ctx->key, key, keylen);
1325 ctx->keylen = keylen;
1327 return 0;
1330 static void unmap_sg_talitos_ptr(struct device *dev, struct scatterlist *src,
1331 struct scatterlist *dst, unsigned int len,
1332 struct talitos_edesc *edesc)
1334 talitos_sg_unmap(dev, edesc, src, dst);
1337 static void common_nonsnoop_unmap(struct device *dev,
1338 struct talitos_edesc *edesc,
1339 struct ablkcipher_request *areq)
1341 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1343 unmap_sg_talitos_ptr(dev, areq->src, areq->dst, areq->nbytes, edesc);
1344 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
1345 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
1347 if (edesc->dma_len)
1348 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1349 DMA_BIDIRECTIONAL);
1352 static void ablkcipher_done(struct device *dev,
1353 struct talitos_desc *desc, void *context,
1354 int err)
1356 struct ablkcipher_request *areq = context;
1357 struct talitos_edesc *edesc;
1359 edesc = container_of(desc, struct talitos_edesc, desc);
1361 common_nonsnoop_unmap(dev, edesc, areq);
1363 kfree(edesc);
1365 areq->base.complete(&areq->base, err);
1368 int map_sg_in_talitos_ptr(struct device *dev, struct scatterlist *src,
1369 unsigned int len, struct talitos_edesc *edesc,
1370 enum dma_data_direction dir, struct talitos_ptr *ptr)
1372 int sg_count;
1374 ptr->len = cpu_to_be16(len);
1375 ptr->j_extent = 0;
1377 sg_count = talitos_map_sg(dev, src, edesc->src_nents ? : 1, dir,
1378 edesc->src_chained);
1380 if (sg_count == 1) {
1381 to_talitos_ptr(ptr, sg_dma_address(src));
1382 } else {
1383 sg_count = sg_to_link_tbl(src, sg_count, len,
1384 &edesc->link_tbl[0]);
1385 if (sg_count > 1) {
1386 to_talitos_ptr(ptr, edesc->dma_link_tbl);
1387 ptr->j_extent |= DESC_PTR_LNKTBL_JUMP;
1388 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1389 edesc->dma_len,
1390 DMA_BIDIRECTIONAL);
1391 } else {
1392 /* Only one segment now, so no link tbl needed */
1393 to_talitos_ptr(ptr, sg_dma_address(src));
1396 return sg_count;
1399 void map_sg_out_talitos_ptr(struct device *dev, struct scatterlist *dst,
1400 unsigned int len, struct talitos_edesc *edesc,
1401 enum dma_data_direction dir,
1402 struct talitos_ptr *ptr, int sg_count)
1404 ptr->len = cpu_to_be16(len);
1405 ptr->j_extent = 0;
1407 if (dir != DMA_NONE)
1408 sg_count = talitos_map_sg(dev, dst, edesc->dst_nents ? : 1,
1409 dir, edesc->dst_chained);
1411 if (sg_count == 1) {
1412 to_talitos_ptr(ptr, sg_dma_address(dst));
1413 } else {
1414 struct talitos_ptr *link_tbl_ptr =
1415 &edesc->link_tbl[edesc->src_nents + 1];
1417 to_talitos_ptr(ptr, edesc->dma_link_tbl +
1418 (edesc->src_nents + 1) *
1419 sizeof(struct talitos_ptr));
1420 ptr->j_extent |= DESC_PTR_LNKTBL_JUMP;
1421 sg_count = sg_to_link_tbl(dst, sg_count, len, link_tbl_ptr);
1422 dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1423 edesc->dma_len, DMA_BIDIRECTIONAL);
1427 static int common_nonsnoop(struct talitos_edesc *edesc,
1428 struct ablkcipher_request *areq,
1429 void (*callback) (struct device *dev,
1430 struct talitos_desc *desc,
1431 void *context, int error))
1433 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1434 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1435 struct device *dev = ctx->dev;
1436 struct talitos_desc *desc = &edesc->desc;
1437 unsigned int cryptlen = areq->nbytes;
1438 unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
1439 int sg_count, ret;
1441 /* first DWORD empty */
1442 desc->ptr[0] = zero_entry;
1444 /* cipher iv */
1445 to_talitos_ptr(&desc->ptr[1], edesc->iv_dma);
1446 desc->ptr[1].len = cpu_to_be16(ivsize);
1447 desc->ptr[1].j_extent = 0;
1449 /* cipher key */
1450 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1451 (char *)&ctx->key, 0, DMA_TO_DEVICE);
1454 * cipher in
1456 sg_count = map_sg_in_talitos_ptr(dev, areq->src, cryptlen, edesc,
1457 (areq->src == areq->dst) ?
1458 DMA_BIDIRECTIONAL : DMA_TO_DEVICE,
1459 &desc->ptr[3]);
1461 /* cipher out */
1462 map_sg_out_talitos_ptr(dev, areq->dst, cryptlen, edesc,
1463 (areq->src == areq->dst) ? DMA_NONE
1464 : DMA_FROM_DEVICE,
1465 &desc->ptr[4], sg_count);
1467 /* iv out */
1468 map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0,
1469 DMA_FROM_DEVICE);
1471 /* last DWORD empty */
1472 desc->ptr[6] = zero_entry;
1474 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
1475 if (ret != -EINPROGRESS) {
1476 common_nonsnoop_unmap(dev, edesc, areq);
1477 kfree(edesc);
1479 return ret;
1482 static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
1483 areq, bool encrypt)
1485 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1486 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1487 unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
1489 return talitos_edesc_alloc(ctx->dev, NULL, areq->src, areq->dst,
1490 areq->info, 0, areq->nbytes, 0, ivsize, 0,
1491 areq->base.flags, encrypt);
1494 static int ablkcipher_encrypt(struct ablkcipher_request *areq)
1496 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1497 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1498 struct talitos_edesc *edesc;
1500 /* allocate extended descriptor */
1501 edesc = ablkcipher_edesc_alloc(areq, true);
1502 if (IS_ERR(edesc))
1503 return PTR_ERR(edesc);
1505 /* set encrypt */
1506 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
1508 return common_nonsnoop(edesc, areq, ablkcipher_done);
1511 static int ablkcipher_decrypt(struct ablkcipher_request *areq)
1513 struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
1514 struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1515 struct talitos_edesc *edesc;
1517 /* allocate extended descriptor */
1518 edesc = ablkcipher_edesc_alloc(areq, false);
1519 if (IS_ERR(edesc))
1520 return PTR_ERR(edesc);
1522 edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1524 return common_nonsnoop(edesc, areq, ablkcipher_done);
1527 static void common_nonsnoop_hash_unmap(struct device *dev,
1528 struct talitos_edesc *edesc,
1529 struct ahash_request *areq)
1531 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1533 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1535 unmap_sg_talitos_ptr(dev, req_ctx->psrc, NULL, 0, edesc);
1537 /* When using hashctx-in, must unmap it. */
1538 if (edesc->desc.ptr[1].len)
1539 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1540 DMA_TO_DEVICE);
1542 if (edesc->desc.ptr[2].len)
1543 unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
1544 DMA_TO_DEVICE);
1546 if (edesc->dma_len)
1547 dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1548 DMA_BIDIRECTIONAL);
1552 static void ahash_done(struct device *dev,
1553 struct talitos_desc *desc, void *context,
1554 int err)
1556 struct ahash_request *areq = context;
1557 struct talitos_edesc *edesc =
1558 container_of(desc, struct talitos_edesc, desc);
1559 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1561 if (!req_ctx->last && req_ctx->to_hash_later) {
1562 /* Position any partial block for next update/final/finup */
1563 memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
1564 req_ctx->nbuf = req_ctx->to_hash_later;
1566 common_nonsnoop_hash_unmap(dev, edesc, areq);
1568 kfree(edesc);
1570 areq->base.complete(&areq->base, err);
1573 static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1574 struct ahash_request *areq, unsigned int length,
1575 void (*callback) (struct device *dev,
1576 struct talitos_desc *desc,
1577 void *context, int error))
1579 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1580 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1581 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1582 struct device *dev = ctx->dev;
1583 struct talitos_desc *desc = &edesc->desc;
1584 int ret;
1586 /* first DWORD empty */
1587 desc->ptr[0] = zero_entry;
1589 /* hash context in */
1590 if (!req_ctx->first || req_ctx->swinit) {
1591 map_single_talitos_ptr(dev, &desc->ptr[1],
1592 req_ctx->hw_context_size,
1593 (char *)req_ctx->hw_context, 0,
1594 DMA_TO_DEVICE);
1595 req_ctx->swinit = 0;
1596 } else {
1597 desc->ptr[1] = zero_entry;
1598 /* Indicate next op is not the first. */
1599 req_ctx->first = 0;
1602 /* HMAC key */
1603 if (ctx->keylen)
1604 map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1605 (char *)&ctx->key, 0, DMA_TO_DEVICE);
1606 else
1607 desc->ptr[2] = zero_entry;
1610 * data in
1612 map_sg_in_talitos_ptr(dev, req_ctx->psrc, length, edesc,
1613 DMA_TO_DEVICE, &desc->ptr[3]);
1615 /* fifth DWORD empty */
1616 desc->ptr[4] = zero_entry;
1618 /* hash/HMAC out -or- hash context out */
1619 if (req_ctx->last)
1620 map_single_talitos_ptr(dev, &desc->ptr[5],
1621 crypto_ahash_digestsize(tfm),
1622 areq->result, 0, DMA_FROM_DEVICE);
1623 else
1624 map_single_talitos_ptr(dev, &desc->ptr[5],
1625 req_ctx->hw_context_size,
1626 req_ctx->hw_context, 0, DMA_FROM_DEVICE);
1628 /* last DWORD empty */
1629 desc->ptr[6] = zero_entry;
1631 ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
1632 if (ret != -EINPROGRESS) {
1633 common_nonsnoop_hash_unmap(dev, edesc, areq);
1634 kfree(edesc);
1636 return ret;
1639 static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1640 unsigned int nbytes)
1642 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1643 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1644 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1646 return talitos_edesc_alloc(ctx->dev, NULL, req_ctx->psrc, NULL, NULL, 0,
1647 nbytes, 0, 0, 0, areq->base.flags, false);
1650 static int ahash_init(struct ahash_request *areq)
1652 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1653 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1655 /* Initialize the context */
1656 req_ctx->nbuf = 0;
1657 req_ctx->first = 1; /* first indicates h/w must init its context */
1658 req_ctx->swinit = 0; /* assume h/w init of context */
1659 req_ctx->hw_context_size =
1660 (crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1661 ? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1662 : TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1664 return 0;
1668 * on h/w without explicit sha224 support, we initialize h/w context
1669 * manually with sha224 constants, and tell it to run sha256.
1671 static int ahash_init_sha224_swinit(struct ahash_request *areq)
1673 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1675 ahash_init(areq);
1676 req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
1678 req_ctx->hw_context[0] = SHA224_H0;
1679 req_ctx->hw_context[1] = SHA224_H1;
1680 req_ctx->hw_context[2] = SHA224_H2;
1681 req_ctx->hw_context[3] = SHA224_H3;
1682 req_ctx->hw_context[4] = SHA224_H4;
1683 req_ctx->hw_context[5] = SHA224_H5;
1684 req_ctx->hw_context[6] = SHA224_H6;
1685 req_ctx->hw_context[7] = SHA224_H7;
1687 /* init 64-bit count */
1688 req_ctx->hw_context[8] = 0;
1689 req_ctx->hw_context[9] = 0;
1691 return 0;
1694 static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1696 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1697 struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1698 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1699 struct talitos_edesc *edesc;
1700 unsigned int blocksize =
1701 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1702 unsigned int nbytes_to_hash;
1703 unsigned int to_hash_later;
1704 unsigned int nsg;
1705 bool chained;
1707 if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
1708 /* Buffer up to one whole block */
1709 sg_copy_to_buffer(areq->src,
1710 sg_count(areq->src, nbytes, &chained),
1711 req_ctx->buf + req_ctx->nbuf, nbytes);
1712 req_ctx->nbuf += nbytes;
1713 return 0;
1716 /* At least (blocksize + 1) bytes are available to hash */
1717 nbytes_to_hash = nbytes + req_ctx->nbuf;
1718 to_hash_later = nbytes_to_hash & (blocksize - 1);
1720 if (req_ctx->last)
1721 to_hash_later = 0;
1722 else if (to_hash_later)
1723 /* There is a partial block. Hash the full block(s) now */
1724 nbytes_to_hash -= to_hash_later;
1725 else {
1726 /* Keep one block buffered */
1727 nbytes_to_hash -= blocksize;
1728 to_hash_later = blocksize;
1731 /* Chain in any previously buffered data */
1732 if (req_ctx->nbuf) {
1733 nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
1734 sg_init_table(req_ctx->bufsl, nsg);
1735 sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
1736 if (nsg > 1)
1737 scatterwalk_sg_chain(req_ctx->bufsl, 2, areq->src);
1738 req_ctx->psrc = req_ctx->bufsl;
1739 } else
1740 req_ctx->psrc = areq->src;
1742 if (to_hash_later) {
1743 int nents = sg_count(areq->src, nbytes, &chained);
1744 sg_pcopy_to_buffer(areq->src, nents,
1745 req_ctx->bufnext,
1746 to_hash_later,
1747 nbytes - to_hash_later);
1749 req_ctx->to_hash_later = to_hash_later;
1751 /* Allocate extended descriptor */
1752 edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
1753 if (IS_ERR(edesc))
1754 return PTR_ERR(edesc);
1756 edesc->desc.hdr = ctx->desc_hdr_template;
1758 /* On last one, request SEC to pad; otherwise continue */
1759 if (req_ctx->last)
1760 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
1761 else
1762 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
1764 /* request SEC to INIT hash. */
1765 if (req_ctx->first && !req_ctx->swinit)
1766 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
1768 /* When the tfm context has a keylen, it's an HMAC.
1769 * A first or last (ie. not middle) descriptor must request HMAC.
1771 if (ctx->keylen && (req_ctx->first || req_ctx->last))
1772 edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
1774 return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
1775 ahash_done);
1778 static int ahash_update(struct ahash_request *areq)
1780 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1782 req_ctx->last = 0;
1784 return ahash_process_req(areq, areq->nbytes);
1787 static int ahash_final(struct ahash_request *areq)
1789 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1791 req_ctx->last = 1;
1793 return ahash_process_req(areq, 0);
1796 static int ahash_finup(struct ahash_request *areq)
1798 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1800 req_ctx->last = 1;
1802 return ahash_process_req(areq, areq->nbytes);
1805 static int ahash_digest(struct ahash_request *areq)
1807 struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1808 struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
1810 ahash->init(areq);
1811 req_ctx->last = 1;
1813 return ahash_process_req(areq, areq->nbytes);
1816 struct keyhash_result {
1817 struct completion completion;
1818 int err;
1821 static void keyhash_complete(struct crypto_async_request *req, int err)
1823 struct keyhash_result *res = req->data;
1825 if (err == -EINPROGRESS)
1826 return;
1828 res->err = err;
1829 complete(&res->completion);
1832 static int keyhash(struct crypto_ahash *tfm, const u8 *key, unsigned int keylen,
1833 u8 *hash)
1835 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1837 struct scatterlist sg[1];
1838 struct ahash_request *req;
1839 struct keyhash_result hresult;
1840 int ret;
1842 init_completion(&hresult.completion);
1844 req = ahash_request_alloc(tfm, GFP_KERNEL);
1845 if (!req)
1846 return -ENOMEM;
1848 /* Keep tfm keylen == 0 during hash of the long key */
1849 ctx->keylen = 0;
1850 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
1851 keyhash_complete, &hresult);
1853 sg_init_one(&sg[0], key, keylen);
1855 ahash_request_set_crypt(req, sg, hash, keylen);
1856 ret = crypto_ahash_digest(req);
1857 switch (ret) {
1858 case 0:
1859 break;
1860 case -EINPROGRESS:
1861 case -EBUSY:
1862 ret = wait_for_completion_interruptible(
1863 &hresult.completion);
1864 if (!ret)
1865 ret = hresult.err;
1866 break;
1867 default:
1868 break;
1870 ahash_request_free(req);
1872 return ret;
1875 static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
1876 unsigned int keylen)
1878 struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
1879 unsigned int blocksize =
1880 crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1881 unsigned int digestsize = crypto_ahash_digestsize(tfm);
1882 unsigned int keysize = keylen;
1883 u8 hash[SHA512_DIGEST_SIZE];
1884 int ret;
1886 if (keylen <= blocksize)
1887 memcpy(ctx->key, key, keysize);
1888 else {
1889 /* Must get the hash of the long key */
1890 ret = keyhash(tfm, key, keylen, hash);
1892 if (ret) {
1893 crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
1894 return -EINVAL;
1897 keysize = digestsize;
1898 memcpy(ctx->key, hash, digestsize);
1901 ctx->keylen = keysize;
1903 return 0;
1907 struct talitos_alg_template {
1908 u32 type;
1909 union {
1910 struct crypto_alg crypto;
1911 struct ahash_alg hash;
1912 } alg;
1913 __be32 desc_hdr_template;
1916 static struct talitos_alg_template driver_algs[] = {
1917 /* AEAD algorithms. These use a single-pass ipsec_esp descriptor */
1918 { .type = CRYPTO_ALG_TYPE_AEAD,
1919 .alg.crypto = {
1920 .cra_name = "authenc(hmac(sha1),cbc(aes))",
1921 .cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
1922 .cra_blocksize = AES_BLOCK_SIZE,
1923 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1924 .cra_aead = {
1925 .ivsize = AES_BLOCK_SIZE,
1926 .maxauthsize = SHA1_DIGEST_SIZE,
1929 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1930 DESC_HDR_SEL0_AESU |
1931 DESC_HDR_MODE0_AESU_CBC |
1932 DESC_HDR_SEL1_MDEUA |
1933 DESC_HDR_MODE1_MDEU_INIT |
1934 DESC_HDR_MODE1_MDEU_PAD |
1935 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
1937 { .type = CRYPTO_ALG_TYPE_AEAD,
1938 .alg.crypto = {
1939 .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
1940 .cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
1941 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1942 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1943 .cra_aead = {
1944 .ivsize = DES3_EDE_BLOCK_SIZE,
1945 .maxauthsize = SHA1_DIGEST_SIZE,
1948 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1949 DESC_HDR_SEL0_DEU |
1950 DESC_HDR_MODE0_DEU_CBC |
1951 DESC_HDR_MODE0_DEU_3DES |
1952 DESC_HDR_SEL1_MDEUA |
1953 DESC_HDR_MODE1_MDEU_INIT |
1954 DESC_HDR_MODE1_MDEU_PAD |
1955 DESC_HDR_MODE1_MDEU_SHA1_HMAC,
1957 { .type = CRYPTO_ALG_TYPE_AEAD,
1958 .alg.crypto = {
1959 .cra_name = "authenc(hmac(sha224),cbc(aes))",
1960 .cra_driver_name = "authenc-hmac-sha224-cbc-aes-talitos",
1961 .cra_blocksize = AES_BLOCK_SIZE,
1962 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1963 .cra_aead = {
1964 .ivsize = AES_BLOCK_SIZE,
1965 .maxauthsize = SHA224_DIGEST_SIZE,
1968 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1969 DESC_HDR_SEL0_AESU |
1970 DESC_HDR_MODE0_AESU_CBC |
1971 DESC_HDR_SEL1_MDEUA |
1972 DESC_HDR_MODE1_MDEU_INIT |
1973 DESC_HDR_MODE1_MDEU_PAD |
1974 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
1976 { .type = CRYPTO_ALG_TYPE_AEAD,
1977 .alg.crypto = {
1978 .cra_name = "authenc(hmac(sha224),cbc(des3_ede))",
1979 .cra_driver_name = "authenc-hmac-sha224-cbc-3des-talitos",
1980 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1981 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
1982 .cra_aead = {
1983 .ivsize = DES3_EDE_BLOCK_SIZE,
1984 .maxauthsize = SHA224_DIGEST_SIZE,
1987 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
1988 DESC_HDR_SEL0_DEU |
1989 DESC_HDR_MODE0_DEU_CBC |
1990 DESC_HDR_MODE0_DEU_3DES |
1991 DESC_HDR_SEL1_MDEUA |
1992 DESC_HDR_MODE1_MDEU_INIT |
1993 DESC_HDR_MODE1_MDEU_PAD |
1994 DESC_HDR_MODE1_MDEU_SHA224_HMAC,
1996 { .type = CRYPTO_ALG_TYPE_AEAD,
1997 .alg.crypto = {
1998 .cra_name = "authenc(hmac(sha256),cbc(aes))",
1999 .cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
2000 .cra_blocksize = AES_BLOCK_SIZE,
2001 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2002 .cra_aead = {
2003 .ivsize = AES_BLOCK_SIZE,
2004 .maxauthsize = SHA256_DIGEST_SIZE,
2007 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2008 DESC_HDR_SEL0_AESU |
2009 DESC_HDR_MODE0_AESU_CBC |
2010 DESC_HDR_SEL1_MDEUA |
2011 DESC_HDR_MODE1_MDEU_INIT |
2012 DESC_HDR_MODE1_MDEU_PAD |
2013 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2015 { .type = CRYPTO_ALG_TYPE_AEAD,
2016 .alg.crypto = {
2017 .cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
2018 .cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
2019 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2020 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2021 .cra_aead = {
2022 .ivsize = DES3_EDE_BLOCK_SIZE,
2023 .maxauthsize = SHA256_DIGEST_SIZE,
2026 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2027 DESC_HDR_SEL0_DEU |
2028 DESC_HDR_MODE0_DEU_CBC |
2029 DESC_HDR_MODE0_DEU_3DES |
2030 DESC_HDR_SEL1_MDEUA |
2031 DESC_HDR_MODE1_MDEU_INIT |
2032 DESC_HDR_MODE1_MDEU_PAD |
2033 DESC_HDR_MODE1_MDEU_SHA256_HMAC,
2035 { .type = CRYPTO_ALG_TYPE_AEAD,
2036 .alg.crypto = {
2037 .cra_name = "authenc(hmac(sha384),cbc(aes))",
2038 .cra_driver_name = "authenc-hmac-sha384-cbc-aes-talitos",
2039 .cra_blocksize = AES_BLOCK_SIZE,
2040 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2041 .cra_aead = {
2042 .ivsize = AES_BLOCK_SIZE,
2043 .maxauthsize = SHA384_DIGEST_SIZE,
2046 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2047 DESC_HDR_SEL0_AESU |
2048 DESC_HDR_MODE0_AESU_CBC |
2049 DESC_HDR_SEL1_MDEUB |
2050 DESC_HDR_MODE1_MDEU_INIT |
2051 DESC_HDR_MODE1_MDEU_PAD |
2052 DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2054 { .type = CRYPTO_ALG_TYPE_AEAD,
2055 .alg.crypto = {
2056 .cra_name = "authenc(hmac(sha384),cbc(des3_ede))",
2057 .cra_driver_name = "authenc-hmac-sha384-cbc-3des-talitos",
2058 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2059 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2060 .cra_aead = {
2061 .ivsize = DES3_EDE_BLOCK_SIZE,
2062 .maxauthsize = SHA384_DIGEST_SIZE,
2065 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2066 DESC_HDR_SEL0_DEU |
2067 DESC_HDR_MODE0_DEU_CBC |
2068 DESC_HDR_MODE0_DEU_3DES |
2069 DESC_HDR_SEL1_MDEUB |
2070 DESC_HDR_MODE1_MDEU_INIT |
2071 DESC_HDR_MODE1_MDEU_PAD |
2072 DESC_HDR_MODE1_MDEUB_SHA384_HMAC,
2074 { .type = CRYPTO_ALG_TYPE_AEAD,
2075 .alg.crypto = {
2076 .cra_name = "authenc(hmac(sha512),cbc(aes))",
2077 .cra_driver_name = "authenc-hmac-sha512-cbc-aes-talitos",
2078 .cra_blocksize = AES_BLOCK_SIZE,
2079 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2080 .cra_aead = {
2081 .ivsize = AES_BLOCK_SIZE,
2082 .maxauthsize = SHA512_DIGEST_SIZE,
2085 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2086 DESC_HDR_SEL0_AESU |
2087 DESC_HDR_MODE0_AESU_CBC |
2088 DESC_HDR_SEL1_MDEUB |
2089 DESC_HDR_MODE1_MDEU_INIT |
2090 DESC_HDR_MODE1_MDEU_PAD |
2091 DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2093 { .type = CRYPTO_ALG_TYPE_AEAD,
2094 .alg.crypto = {
2095 .cra_name = "authenc(hmac(sha512),cbc(des3_ede))",
2096 .cra_driver_name = "authenc-hmac-sha512-cbc-3des-talitos",
2097 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2098 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2099 .cra_aead = {
2100 .ivsize = DES3_EDE_BLOCK_SIZE,
2101 .maxauthsize = SHA512_DIGEST_SIZE,
2104 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2105 DESC_HDR_SEL0_DEU |
2106 DESC_HDR_MODE0_DEU_CBC |
2107 DESC_HDR_MODE0_DEU_3DES |
2108 DESC_HDR_SEL1_MDEUB |
2109 DESC_HDR_MODE1_MDEU_INIT |
2110 DESC_HDR_MODE1_MDEU_PAD |
2111 DESC_HDR_MODE1_MDEUB_SHA512_HMAC,
2113 { .type = CRYPTO_ALG_TYPE_AEAD,
2114 .alg.crypto = {
2115 .cra_name = "authenc(hmac(md5),cbc(aes))",
2116 .cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
2117 .cra_blocksize = AES_BLOCK_SIZE,
2118 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2119 .cra_aead = {
2120 .ivsize = AES_BLOCK_SIZE,
2121 .maxauthsize = MD5_DIGEST_SIZE,
2124 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2125 DESC_HDR_SEL0_AESU |
2126 DESC_HDR_MODE0_AESU_CBC |
2127 DESC_HDR_SEL1_MDEUA |
2128 DESC_HDR_MODE1_MDEU_INIT |
2129 DESC_HDR_MODE1_MDEU_PAD |
2130 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2132 { .type = CRYPTO_ALG_TYPE_AEAD,
2133 .alg.crypto = {
2134 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
2135 .cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
2136 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2137 .cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
2138 .cra_aead = {
2139 .ivsize = DES3_EDE_BLOCK_SIZE,
2140 .maxauthsize = MD5_DIGEST_SIZE,
2143 .desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
2144 DESC_HDR_SEL0_DEU |
2145 DESC_HDR_MODE0_DEU_CBC |
2146 DESC_HDR_MODE0_DEU_3DES |
2147 DESC_HDR_SEL1_MDEUA |
2148 DESC_HDR_MODE1_MDEU_INIT |
2149 DESC_HDR_MODE1_MDEU_PAD |
2150 DESC_HDR_MODE1_MDEU_MD5_HMAC,
2152 /* ABLKCIPHER algorithms. */
2153 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2154 .alg.crypto = {
2155 .cra_name = "cbc(aes)",
2156 .cra_driver_name = "cbc-aes-talitos",
2157 .cra_blocksize = AES_BLOCK_SIZE,
2158 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2159 CRYPTO_ALG_ASYNC,
2160 .cra_ablkcipher = {
2161 .min_keysize = AES_MIN_KEY_SIZE,
2162 .max_keysize = AES_MAX_KEY_SIZE,
2163 .ivsize = AES_BLOCK_SIZE,
2166 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2167 DESC_HDR_SEL0_AESU |
2168 DESC_HDR_MODE0_AESU_CBC,
2170 { .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2171 .alg.crypto = {
2172 .cra_name = "cbc(des3_ede)",
2173 .cra_driver_name = "cbc-3des-talitos",
2174 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
2175 .cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
2176 CRYPTO_ALG_ASYNC,
2177 .cra_ablkcipher = {
2178 .min_keysize = DES3_EDE_KEY_SIZE,
2179 .max_keysize = DES3_EDE_KEY_SIZE,
2180 .ivsize = DES3_EDE_BLOCK_SIZE,
2183 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2184 DESC_HDR_SEL0_DEU |
2185 DESC_HDR_MODE0_DEU_CBC |
2186 DESC_HDR_MODE0_DEU_3DES,
2188 /* AHASH algorithms. */
2189 { .type = CRYPTO_ALG_TYPE_AHASH,
2190 .alg.hash = {
2191 .halg.digestsize = MD5_DIGEST_SIZE,
2192 .halg.base = {
2193 .cra_name = "md5",
2194 .cra_driver_name = "md5-talitos",
2195 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
2196 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2197 CRYPTO_ALG_ASYNC,
2200 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2201 DESC_HDR_SEL0_MDEUA |
2202 DESC_HDR_MODE0_MDEU_MD5,
2204 { .type = CRYPTO_ALG_TYPE_AHASH,
2205 .alg.hash = {
2206 .halg.digestsize = SHA1_DIGEST_SIZE,
2207 .halg.base = {
2208 .cra_name = "sha1",
2209 .cra_driver_name = "sha1-talitos",
2210 .cra_blocksize = SHA1_BLOCK_SIZE,
2211 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2212 CRYPTO_ALG_ASYNC,
2215 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2216 DESC_HDR_SEL0_MDEUA |
2217 DESC_HDR_MODE0_MDEU_SHA1,
2219 { .type = CRYPTO_ALG_TYPE_AHASH,
2220 .alg.hash = {
2221 .halg.digestsize = SHA224_DIGEST_SIZE,
2222 .halg.base = {
2223 .cra_name = "sha224",
2224 .cra_driver_name = "sha224-talitos",
2225 .cra_blocksize = SHA224_BLOCK_SIZE,
2226 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2227 CRYPTO_ALG_ASYNC,
2230 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2231 DESC_HDR_SEL0_MDEUA |
2232 DESC_HDR_MODE0_MDEU_SHA224,
2234 { .type = CRYPTO_ALG_TYPE_AHASH,
2235 .alg.hash = {
2236 .halg.digestsize = SHA256_DIGEST_SIZE,
2237 .halg.base = {
2238 .cra_name = "sha256",
2239 .cra_driver_name = "sha256-talitos",
2240 .cra_blocksize = SHA256_BLOCK_SIZE,
2241 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2242 CRYPTO_ALG_ASYNC,
2245 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2246 DESC_HDR_SEL0_MDEUA |
2247 DESC_HDR_MODE0_MDEU_SHA256,
2249 { .type = CRYPTO_ALG_TYPE_AHASH,
2250 .alg.hash = {
2251 .halg.digestsize = SHA384_DIGEST_SIZE,
2252 .halg.base = {
2253 .cra_name = "sha384",
2254 .cra_driver_name = "sha384-talitos",
2255 .cra_blocksize = SHA384_BLOCK_SIZE,
2256 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2257 CRYPTO_ALG_ASYNC,
2260 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2261 DESC_HDR_SEL0_MDEUB |
2262 DESC_HDR_MODE0_MDEUB_SHA384,
2264 { .type = CRYPTO_ALG_TYPE_AHASH,
2265 .alg.hash = {
2266 .halg.digestsize = SHA512_DIGEST_SIZE,
2267 .halg.base = {
2268 .cra_name = "sha512",
2269 .cra_driver_name = "sha512-talitos",
2270 .cra_blocksize = SHA512_BLOCK_SIZE,
2271 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2272 CRYPTO_ALG_ASYNC,
2275 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2276 DESC_HDR_SEL0_MDEUB |
2277 DESC_HDR_MODE0_MDEUB_SHA512,
2279 { .type = CRYPTO_ALG_TYPE_AHASH,
2280 .alg.hash = {
2281 .halg.digestsize = MD5_DIGEST_SIZE,
2282 .halg.base = {
2283 .cra_name = "hmac(md5)",
2284 .cra_driver_name = "hmac-md5-talitos",
2285 .cra_blocksize = MD5_HMAC_BLOCK_SIZE,
2286 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2287 CRYPTO_ALG_ASYNC,
2290 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2291 DESC_HDR_SEL0_MDEUA |
2292 DESC_HDR_MODE0_MDEU_MD5,
2294 { .type = CRYPTO_ALG_TYPE_AHASH,
2295 .alg.hash = {
2296 .halg.digestsize = SHA1_DIGEST_SIZE,
2297 .halg.base = {
2298 .cra_name = "hmac(sha1)",
2299 .cra_driver_name = "hmac-sha1-talitos",
2300 .cra_blocksize = SHA1_BLOCK_SIZE,
2301 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2302 CRYPTO_ALG_ASYNC,
2305 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2306 DESC_HDR_SEL0_MDEUA |
2307 DESC_HDR_MODE0_MDEU_SHA1,
2309 { .type = CRYPTO_ALG_TYPE_AHASH,
2310 .alg.hash = {
2311 .halg.digestsize = SHA224_DIGEST_SIZE,
2312 .halg.base = {
2313 .cra_name = "hmac(sha224)",
2314 .cra_driver_name = "hmac-sha224-talitos",
2315 .cra_blocksize = SHA224_BLOCK_SIZE,
2316 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2317 CRYPTO_ALG_ASYNC,
2320 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2321 DESC_HDR_SEL0_MDEUA |
2322 DESC_HDR_MODE0_MDEU_SHA224,
2324 { .type = CRYPTO_ALG_TYPE_AHASH,
2325 .alg.hash = {
2326 .halg.digestsize = SHA256_DIGEST_SIZE,
2327 .halg.base = {
2328 .cra_name = "hmac(sha256)",
2329 .cra_driver_name = "hmac-sha256-talitos",
2330 .cra_blocksize = SHA256_BLOCK_SIZE,
2331 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2332 CRYPTO_ALG_ASYNC,
2335 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2336 DESC_HDR_SEL0_MDEUA |
2337 DESC_HDR_MODE0_MDEU_SHA256,
2339 { .type = CRYPTO_ALG_TYPE_AHASH,
2340 .alg.hash = {
2341 .halg.digestsize = SHA384_DIGEST_SIZE,
2342 .halg.base = {
2343 .cra_name = "hmac(sha384)",
2344 .cra_driver_name = "hmac-sha384-talitos",
2345 .cra_blocksize = SHA384_BLOCK_SIZE,
2346 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2347 CRYPTO_ALG_ASYNC,
2350 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2351 DESC_HDR_SEL0_MDEUB |
2352 DESC_HDR_MODE0_MDEUB_SHA384,
2354 { .type = CRYPTO_ALG_TYPE_AHASH,
2355 .alg.hash = {
2356 .halg.digestsize = SHA512_DIGEST_SIZE,
2357 .halg.base = {
2358 .cra_name = "hmac(sha512)",
2359 .cra_driver_name = "hmac-sha512-talitos",
2360 .cra_blocksize = SHA512_BLOCK_SIZE,
2361 .cra_flags = CRYPTO_ALG_TYPE_AHASH |
2362 CRYPTO_ALG_ASYNC,
2365 .desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2366 DESC_HDR_SEL0_MDEUB |
2367 DESC_HDR_MODE0_MDEUB_SHA512,
2371 struct talitos_crypto_alg {
2372 struct list_head entry;
2373 struct device *dev;
2374 struct talitos_alg_template algt;
2377 static int talitos_cra_init(struct crypto_tfm *tfm)
2379 struct crypto_alg *alg = tfm->__crt_alg;
2380 struct talitos_crypto_alg *talitos_alg;
2381 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2382 struct talitos_private *priv;
2384 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
2385 talitos_alg = container_of(__crypto_ahash_alg(alg),
2386 struct talitos_crypto_alg,
2387 algt.alg.hash);
2388 else
2389 talitos_alg = container_of(alg, struct talitos_crypto_alg,
2390 algt.alg.crypto);
2392 /* update context with ptr to dev */
2393 ctx->dev = talitos_alg->dev;
2395 /* assign SEC channel to tfm in round-robin fashion */
2396 priv = dev_get_drvdata(ctx->dev);
2397 ctx->ch = atomic_inc_return(&priv->last_chan) &
2398 (priv->num_channels - 1);
2400 /* copy descriptor header template value */
2401 ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
2403 /* select done notification */
2404 ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY;
2406 return 0;
2409 static int talitos_cra_init_aead(struct crypto_tfm *tfm)
2411 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2413 talitos_cra_init(tfm);
2415 /* random first IV */
2416 get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
2418 return 0;
2421 static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
2423 struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2425 talitos_cra_init(tfm);
2427 ctx->keylen = 0;
2428 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2429 sizeof(struct talitos_ahash_req_ctx));
2431 return 0;
2435 * given the alg's descriptor header template, determine whether descriptor
2436 * type and primary/secondary execution units required match the hw
2437 * capabilities description provided in the device tree node.
2439 static int hw_supports(struct device *dev, __be32 desc_hdr_template)
2441 struct talitos_private *priv = dev_get_drvdata(dev);
2442 int ret;
2444 ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
2445 (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
2447 if (SECONDARY_EU(desc_hdr_template))
2448 ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
2449 & priv->exec_units);
2451 return ret;
2454 static int talitos_remove(struct platform_device *ofdev)
2456 struct device *dev = &ofdev->dev;
2457 struct talitos_private *priv = dev_get_drvdata(dev);
2458 struct talitos_crypto_alg *t_alg, *n;
2459 int i;
2461 list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
2462 switch (t_alg->algt.type) {
2463 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2464 case CRYPTO_ALG_TYPE_AEAD:
2465 crypto_unregister_alg(&t_alg->algt.alg.crypto);
2466 break;
2467 case CRYPTO_ALG_TYPE_AHASH:
2468 crypto_unregister_ahash(&t_alg->algt.alg.hash);
2469 break;
2471 list_del(&t_alg->entry);
2472 kfree(t_alg);
2475 if (hw_supports(dev, DESC_HDR_SEL0_RNG))
2476 talitos_unregister_rng(dev);
2478 for (i = 0; i < priv->num_channels; i++)
2479 kfree(priv->chan[i].fifo);
2481 kfree(priv->chan);
2483 for (i = 0; i < 2; i++)
2484 if (priv->irq[i]) {
2485 free_irq(priv->irq[i], dev);
2486 irq_dispose_mapping(priv->irq[i]);
2489 tasklet_kill(&priv->done_task[0]);
2490 if (priv->irq[1])
2491 tasklet_kill(&priv->done_task[1]);
2493 iounmap(priv->reg);
2495 kfree(priv);
2497 return 0;
2500 static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
2501 struct talitos_alg_template
2502 *template)
2504 struct talitos_private *priv = dev_get_drvdata(dev);
2505 struct talitos_crypto_alg *t_alg;
2506 struct crypto_alg *alg;
2508 t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
2509 if (!t_alg)
2510 return ERR_PTR(-ENOMEM);
2512 t_alg->algt = *template;
2514 switch (t_alg->algt.type) {
2515 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2516 alg = &t_alg->algt.alg.crypto;
2517 alg->cra_init = talitos_cra_init;
2518 alg->cra_type = &crypto_ablkcipher_type;
2519 alg->cra_ablkcipher.setkey = ablkcipher_setkey;
2520 alg->cra_ablkcipher.encrypt = ablkcipher_encrypt;
2521 alg->cra_ablkcipher.decrypt = ablkcipher_decrypt;
2522 alg->cra_ablkcipher.geniv = "eseqiv";
2523 break;
2524 case CRYPTO_ALG_TYPE_AEAD:
2525 alg = &t_alg->algt.alg.crypto;
2526 alg->cra_init = talitos_cra_init_aead;
2527 alg->cra_type = &crypto_aead_type;
2528 alg->cra_aead.setkey = aead_setkey;
2529 alg->cra_aead.setauthsize = aead_setauthsize;
2530 alg->cra_aead.encrypt = aead_encrypt;
2531 alg->cra_aead.decrypt = aead_decrypt;
2532 alg->cra_aead.givencrypt = aead_givencrypt;
2533 alg->cra_aead.geniv = "<built-in>";
2534 break;
2535 case CRYPTO_ALG_TYPE_AHASH:
2536 alg = &t_alg->algt.alg.hash.halg.base;
2537 alg->cra_init = talitos_cra_init_ahash;
2538 alg->cra_type = &crypto_ahash_type;
2539 t_alg->algt.alg.hash.init = ahash_init;
2540 t_alg->algt.alg.hash.update = ahash_update;
2541 t_alg->algt.alg.hash.final = ahash_final;
2542 t_alg->algt.alg.hash.finup = ahash_finup;
2543 t_alg->algt.alg.hash.digest = ahash_digest;
2544 t_alg->algt.alg.hash.setkey = ahash_setkey;
2546 if (!(priv->features & TALITOS_FTR_HMAC_OK) &&
2547 !strncmp(alg->cra_name, "hmac", 4)) {
2548 kfree(t_alg);
2549 return ERR_PTR(-ENOTSUPP);
2551 if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
2552 (!strcmp(alg->cra_name, "sha224") ||
2553 !strcmp(alg->cra_name, "hmac(sha224)"))) {
2554 t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
2555 t_alg->algt.desc_hdr_template =
2556 DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2557 DESC_HDR_SEL0_MDEUA |
2558 DESC_HDR_MODE0_MDEU_SHA256;
2560 break;
2561 default:
2562 dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
2563 return ERR_PTR(-EINVAL);
2566 alg->cra_module = THIS_MODULE;
2567 alg->cra_priority = TALITOS_CRA_PRIORITY;
2568 alg->cra_alignmask = 0;
2569 alg->cra_ctxsize = sizeof(struct talitos_ctx);
2570 alg->cra_flags |= CRYPTO_ALG_KERN_DRIVER_ONLY;
2572 t_alg->dev = dev;
2574 return t_alg;
2577 static int talitos_probe_irq(struct platform_device *ofdev)
2579 struct device *dev = &ofdev->dev;
2580 struct device_node *np = ofdev->dev.of_node;
2581 struct talitos_private *priv = dev_get_drvdata(dev);
2582 int err;
2584 priv->irq[0] = irq_of_parse_and_map(np, 0);
2585 if (!priv->irq[0]) {
2586 dev_err(dev, "failed to map irq\n");
2587 return -EINVAL;
2590 priv->irq[1] = irq_of_parse_and_map(np, 1);
2592 /* get the primary irq line */
2593 if (!priv->irq[1]) {
2594 err = request_irq(priv->irq[0], talitos_interrupt_4ch, 0,
2595 dev_driver_string(dev), dev);
2596 goto primary_out;
2599 err = request_irq(priv->irq[0], talitos_interrupt_ch0_2, 0,
2600 dev_driver_string(dev), dev);
2601 if (err)
2602 goto primary_out;
2604 /* get the secondary irq line */
2605 err = request_irq(priv->irq[1], talitos_interrupt_ch1_3, 0,
2606 dev_driver_string(dev), dev);
2607 if (err) {
2608 dev_err(dev, "failed to request secondary irq\n");
2609 irq_dispose_mapping(priv->irq[1]);
2610 priv->irq[1] = 0;
2613 return err;
2615 primary_out:
2616 if (err) {
2617 dev_err(dev, "failed to request primary irq\n");
2618 irq_dispose_mapping(priv->irq[0]);
2619 priv->irq[0] = 0;
2622 return err;
2625 static int talitos_probe(struct platform_device *ofdev)
2627 struct device *dev = &ofdev->dev;
2628 struct device_node *np = ofdev->dev.of_node;
2629 struct talitos_private *priv;
2630 const unsigned int *prop;
2631 int i, err;
2633 priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
2634 if (!priv)
2635 return -ENOMEM;
2637 INIT_LIST_HEAD(&priv->alg_list);
2639 dev_set_drvdata(dev, priv);
2641 priv->ofdev = ofdev;
2643 spin_lock_init(&priv->reg_lock);
2645 err = talitos_probe_irq(ofdev);
2646 if (err)
2647 goto err_out;
2649 if (!priv->irq[1]) {
2650 tasklet_init(&priv->done_task[0], talitos_done_4ch,
2651 (unsigned long)dev);
2652 } else {
2653 tasklet_init(&priv->done_task[0], talitos_done_ch0_2,
2654 (unsigned long)dev);
2655 tasklet_init(&priv->done_task[1], talitos_done_ch1_3,
2656 (unsigned long)dev);
2659 priv->reg = of_iomap(np, 0);
2660 if (!priv->reg) {
2661 dev_err(dev, "failed to of_iomap\n");
2662 err = -ENOMEM;
2663 goto err_out;
2666 /* get SEC version capabilities from device tree */
2667 prop = of_get_property(np, "fsl,num-channels", NULL);
2668 if (prop)
2669 priv->num_channels = *prop;
2671 prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
2672 if (prop)
2673 priv->chfifo_len = *prop;
2675 prop = of_get_property(np, "fsl,exec-units-mask", NULL);
2676 if (prop)
2677 priv->exec_units = *prop;
2679 prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
2680 if (prop)
2681 priv->desc_types = *prop;
2683 if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
2684 !priv->exec_units || !priv->desc_types) {
2685 dev_err(dev, "invalid property data in device tree node\n");
2686 err = -EINVAL;
2687 goto err_out;
2690 if (of_device_is_compatible(np, "fsl,sec3.0"))
2691 priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
2693 if (of_device_is_compatible(np, "fsl,sec2.1"))
2694 priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
2695 TALITOS_FTR_SHA224_HWINIT |
2696 TALITOS_FTR_HMAC_OK;
2698 priv->chan = kzalloc(sizeof(struct talitos_channel) *
2699 priv->num_channels, GFP_KERNEL);
2700 if (!priv->chan) {
2701 dev_err(dev, "failed to allocate channel management space\n");
2702 err = -ENOMEM;
2703 goto err_out;
2706 priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
2708 for (i = 0; i < priv->num_channels; i++) {
2709 priv->chan[i].reg = priv->reg + TALITOS_CH_STRIDE * (i + 1);
2710 if (!priv->irq[1] || !(i & 1))
2711 priv->chan[i].reg += TALITOS_CH_BASE_OFFSET;
2713 spin_lock_init(&priv->chan[i].head_lock);
2714 spin_lock_init(&priv->chan[i].tail_lock);
2716 priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
2717 priv->fifo_len, GFP_KERNEL);
2718 if (!priv->chan[i].fifo) {
2719 dev_err(dev, "failed to allocate request fifo %d\n", i);
2720 err = -ENOMEM;
2721 goto err_out;
2724 atomic_set(&priv->chan[i].submit_count,
2725 -(priv->chfifo_len - 1));
2728 dma_set_mask(dev, DMA_BIT_MASK(36));
2730 /* reset and initialize the h/w */
2731 err = init_device(dev);
2732 if (err) {
2733 dev_err(dev, "failed to initialize device\n");
2734 goto err_out;
2737 /* register the RNG, if available */
2738 if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
2739 err = talitos_register_rng(dev);
2740 if (err) {
2741 dev_err(dev, "failed to register hwrng: %d\n", err);
2742 goto err_out;
2743 } else
2744 dev_info(dev, "hwrng\n");
2747 /* register crypto algorithms the device supports */
2748 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
2749 if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
2750 struct talitos_crypto_alg *t_alg;
2751 char *name = NULL;
2753 t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
2754 if (IS_ERR(t_alg)) {
2755 err = PTR_ERR(t_alg);
2756 if (err == -ENOTSUPP)
2757 continue;
2758 goto err_out;
2761 switch (t_alg->algt.type) {
2762 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2763 case CRYPTO_ALG_TYPE_AEAD:
2764 err = crypto_register_alg(
2765 &t_alg->algt.alg.crypto);
2766 name = t_alg->algt.alg.crypto.cra_driver_name;
2767 break;
2768 case CRYPTO_ALG_TYPE_AHASH:
2769 err = crypto_register_ahash(
2770 &t_alg->algt.alg.hash);
2771 name =
2772 t_alg->algt.alg.hash.halg.base.cra_driver_name;
2773 break;
2775 if (err) {
2776 dev_err(dev, "%s alg registration failed\n",
2777 name);
2778 kfree(t_alg);
2779 } else
2780 list_add_tail(&t_alg->entry, &priv->alg_list);
2783 if (!list_empty(&priv->alg_list))
2784 dev_info(dev, "%s algorithms registered in /proc/crypto\n",
2785 (char *)of_get_property(np, "compatible", NULL));
2787 return 0;
2789 err_out:
2790 talitos_remove(ofdev);
2792 return err;
2795 static const struct of_device_id talitos_match[] = {
2797 .compatible = "fsl,sec2.0",
2801 MODULE_DEVICE_TABLE(of, talitos_match);
2803 static struct platform_driver talitos_driver = {
2804 .driver = {
2805 .name = "talitos",
2806 .of_match_table = talitos_match,
2808 .probe = talitos_probe,
2809 .remove = talitos_remove,
2812 module_platform_driver(talitos_driver);
2814 MODULE_LICENSE("GPL");
2815 MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
2816 MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");