xref: /linux/drivers/crypto/talitos.c (revision 5e718a09e5cafc8922f3fe22206423449a2801c9)
19c4a7965SKim Phillips /*
29c4a7965SKim Phillips  * talitos - Freescale Integrated Security Engine (SEC) device driver
39c4a7965SKim Phillips  *
45228f0f7SKim Phillips  * Copyright (c) 2008-2011 Freescale Semiconductor, Inc.
59c4a7965SKim Phillips  *
69c4a7965SKim Phillips  * Scatterlist Crypto API glue code copied from files with the following:
79c4a7965SKim Phillips  * Copyright (c) 2006-2007 Herbert Xu <herbert@gondor.apana.org.au>
89c4a7965SKim Phillips  *
99c4a7965SKim Phillips  * Crypto algorithm registration code copied from hifn driver:
109c4a7965SKim Phillips  * 2007+ Copyright (c) Evgeniy Polyakov <johnpol@2ka.mipt.ru>
119c4a7965SKim Phillips  * All rights reserved.
129c4a7965SKim Phillips  *
139c4a7965SKim Phillips  * This program is free software; you can redistribute it and/or modify
149c4a7965SKim Phillips  * it under the terms of the GNU General Public License as published by
159c4a7965SKim Phillips  * the Free Software Foundation; either version 2 of the License, or
169c4a7965SKim Phillips  * (at your option) any later version.
179c4a7965SKim Phillips  *
189c4a7965SKim Phillips  * This program is distributed in the hope that it will be useful,
199c4a7965SKim Phillips  * but WITHOUT ANY WARRANTY; without even the implied warranty of
209c4a7965SKim Phillips  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
219c4a7965SKim Phillips  * GNU General Public License for more details.
229c4a7965SKim Phillips  *
239c4a7965SKim Phillips  * You should have received a copy of the GNU General Public License
249c4a7965SKim Phillips  * along with this program; if not, write to the Free Software
259c4a7965SKim Phillips  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
269c4a7965SKim Phillips  */
279c4a7965SKim Phillips 
289c4a7965SKim Phillips #include <linux/kernel.h>
299c4a7965SKim Phillips #include <linux/module.h>
309c4a7965SKim Phillips #include <linux/mod_devicetable.h>
319c4a7965SKim Phillips #include <linux/device.h>
329c4a7965SKim Phillips #include <linux/interrupt.h>
339c4a7965SKim Phillips #include <linux/crypto.h>
349c4a7965SKim Phillips #include <linux/hw_random.h>
359c4a7965SKim Phillips #include <linux/of_platform.h>
369c4a7965SKim Phillips #include <linux/dma-mapping.h>
379c4a7965SKim Phillips #include <linux/io.h>
389c4a7965SKim Phillips #include <linux/spinlock.h>
399c4a7965SKim Phillips #include <linux/rtnetlink.h>
405a0e3ad6STejun Heo #include <linux/slab.h>
419c4a7965SKim Phillips 
429c4a7965SKim Phillips #include <crypto/algapi.h>
439c4a7965SKim Phillips #include <crypto/aes.h>
443952f17eSLee Nipper #include <crypto/des.h>
459c4a7965SKim Phillips #include <crypto/sha.h>
46497f2e6bSLee Nipper #include <crypto/md5.h>
479c4a7965SKim Phillips #include <crypto/aead.h>
489c4a7965SKim Phillips #include <crypto/authenc.h>
494de9d0b5SLee Nipper #include <crypto/skcipher.h>
50acbf7c62SLee Nipper #include <crypto/hash.h>
51acbf7c62SLee Nipper #include <crypto/internal/hash.h>
524de9d0b5SLee Nipper #include <crypto/scatterwalk.h>
539c4a7965SKim Phillips 
549c4a7965SKim Phillips #include "talitos.h"
559c4a7965SKim Phillips 
569c4a7965SKim Phillips #define TALITOS_TIMEOUT 100000
579c4a7965SKim Phillips #define TALITOS_MAX_DATA_LEN 65535
589c4a7965SKim Phillips 
599c4a7965SKim Phillips #define DESC_TYPE(desc_hdr) ((be32_to_cpu(desc_hdr) >> 3) & 0x1f)
609c4a7965SKim Phillips #define PRIMARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 28) & 0xf)
619c4a7965SKim Phillips #define SECONDARY_EU(desc_hdr) ((be32_to_cpu(desc_hdr) >> 16) & 0xf)
629c4a7965SKim Phillips 
639c4a7965SKim Phillips /* descriptor pointer entry */
649c4a7965SKim Phillips struct talitos_ptr {
659c4a7965SKim Phillips 	__be16 len;	/* length */
669c4a7965SKim Phillips 	u8 j_extent;	/* jump to sg link table and/or extent */
679c4a7965SKim Phillips 	u8 eptr;	/* extended address */
689c4a7965SKim Phillips 	__be32 ptr;	/* address */
699c4a7965SKim Phillips };
709c4a7965SKim Phillips 
71497f2e6bSLee Nipper static const struct talitos_ptr zero_entry = {
72497f2e6bSLee Nipper 	.len = 0,
73497f2e6bSLee Nipper 	.j_extent = 0,
74497f2e6bSLee Nipper 	.eptr = 0,
75497f2e6bSLee Nipper 	.ptr = 0
76497f2e6bSLee Nipper };
77497f2e6bSLee Nipper 
789c4a7965SKim Phillips /* descriptor */
799c4a7965SKim Phillips struct talitos_desc {
809c4a7965SKim Phillips 	__be32 hdr;			/* header high bits */
819c4a7965SKim Phillips 	__be32 hdr_lo;			/* header low bits */
829c4a7965SKim Phillips 	struct talitos_ptr ptr[7];	/* ptr/len pair array */
839c4a7965SKim Phillips };
849c4a7965SKim Phillips 
859c4a7965SKim Phillips /**
869c4a7965SKim Phillips  * talitos_request - descriptor submission request
879c4a7965SKim Phillips  * @desc: descriptor pointer (kernel virtual)
889c4a7965SKim Phillips  * @dma_desc: descriptor's physical bus address
899c4a7965SKim Phillips  * @callback: whom to call when descriptor processing is done
909c4a7965SKim Phillips  * @context: caller context (optional)
919c4a7965SKim Phillips  */
929c4a7965SKim Phillips struct talitos_request {
939c4a7965SKim Phillips 	struct talitos_desc *desc;
949c4a7965SKim Phillips 	dma_addr_t dma_desc;
959c4a7965SKim Phillips 	void (*callback) (struct device *dev, struct talitos_desc *desc,
969c4a7965SKim Phillips 	                  void *context, int error);
979c4a7965SKim Phillips 	void *context;
989c4a7965SKim Phillips };
999c4a7965SKim Phillips 
1004b992628SKim Phillips /* per-channel fifo management */
1014b992628SKim Phillips struct talitos_channel {
102ad42d5fcSKim Phillips 	void __iomem *reg;
103ad42d5fcSKim Phillips 
1044b992628SKim Phillips 	/* request fifo */
1054b992628SKim Phillips 	struct talitos_request *fifo;
1064b992628SKim Phillips 
1074b992628SKim Phillips 	/* number of requests pending in channel h/w fifo */
1084b992628SKim Phillips 	atomic_t submit_count ____cacheline_aligned;
1094b992628SKim Phillips 
1104b992628SKim Phillips 	/* request submission (head) lock */
1114b992628SKim Phillips 	spinlock_t head_lock ____cacheline_aligned;
1124b992628SKim Phillips 	/* index to next free descriptor request */
1134b992628SKim Phillips 	int head;
1144b992628SKim Phillips 
1154b992628SKim Phillips 	/* request release (tail) lock */
1164b992628SKim Phillips 	spinlock_t tail_lock ____cacheline_aligned;
1174b992628SKim Phillips 	/* index to next in-progress/done descriptor request */
1184b992628SKim Phillips 	int tail;
1194b992628SKim Phillips };
1204b992628SKim Phillips 
1219c4a7965SKim Phillips struct talitos_private {
1229c4a7965SKim Phillips 	struct device *dev;
1232dc11581SGrant Likely 	struct platform_device *ofdev;
1249c4a7965SKim Phillips 	void __iomem *reg;
125c3e337f8SKim Phillips 	int irq[2];
1269c4a7965SKim Phillips 
1279c4a7965SKim Phillips 	/* SEC version geometry (from device tree node) */
1289c4a7965SKim Phillips 	unsigned int num_channels;
1299c4a7965SKim Phillips 	unsigned int chfifo_len;
1309c4a7965SKim Phillips 	unsigned int exec_units;
1319c4a7965SKim Phillips 	unsigned int desc_types;
1329c4a7965SKim Phillips 
133f3c85bc1SLee Nipper 	/* SEC Compatibility info */
134f3c85bc1SLee Nipper 	unsigned long features;
135f3c85bc1SLee Nipper 
1369c4a7965SKim Phillips 	/*
1379c4a7965SKim Phillips 	 * length of the request fifo
1389c4a7965SKim Phillips 	 * fifo_len is chfifo_len rounded up to next power of 2
1399c4a7965SKim Phillips 	 * so we can use bitwise ops to wrap
1409c4a7965SKim Phillips 	 */
1419c4a7965SKim Phillips 	unsigned int fifo_len;
1429c4a7965SKim Phillips 
1434b992628SKim Phillips 	struct talitos_channel *chan;
1449c4a7965SKim Phillips 
1454b992628SKim Phillips 	/* next channel to be assigned next incoming descriptor */
1464b992628SKim Phillips 	atomic_t last_chan ____cacheline_aligned;
1479c4a7965SKim Phillips 
1489c4a7965SKim Phillips 	/* request callback tasklet */
149c3e337f8SKim Phillips 	struct tasklet_struct done_task[2];
1509c4a7965SKim Phillips 
1519c4a7965SKim Phillips 	/* list of registered algorithms */
1529c4a7965SKim Phillips 	struct list_head alg_list;
1539c4a7965SKim Phillips 
1549c4a7965SKim Phillips 	/* hwrng device */
1559c4a7965SKim Phillips 	struct hwrng rng;
1569c4a7965SKim Phillips };
1579c4a7965SKim Phillips 
158f3c85bc1SLee Nipper /* .features flag */
159f3c85bc1SLee Nipper #define TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT 0x00000001
160fe5720e2SKim Phillips #define TALITOS_FTR_HW_AUTH_CHECK 0x00000002
16160f208d7SKim Phillips #define TALITOS_FTR_SHA224_HWINIT 0x00000004
16279b3a418SLee Nipper #define TALITOS_FTR_HMAC_OK 0x00000008
163f3c85bc1SLee Nipper 
16481eb024cSKim Phillips static void to_talitos_ptr(struct talitos_ptr *talitos_ptr, dma_addr_t dma_addr)
16581eb024cSKim Phillips {
16681eb024cSKim Phillips 	talitos_ptr->ptr = cpu_to_be32(lower_32_bits(dma_addr));
167a752447aSKim Phillips 	talitos_ptr->eptr = upper_32_bits(dma_addr);
16881eb024cSKim Phillips }
16981eb024cSKim Phillips 
1709c4a7965SKim Phillips /*
1719c4a7965SKim Phillips  * map virtual single (contiguous) pointer to h/w descriptor pointer
1729c4a7965SKim Phillips  */
1739c4a7965SKim Phillips static void map_single_talitos_ptr(struct device *dev,
1749c4a7965SKim Phillips 				   struct talitos_ptr *talitos_ptr,
1759c4a7965SKim Phillips 				   unsigned short len, void *data,
1769c4a7965SKim Phillips 				   unsigned char extent,
1779c4a7965SKim Phillips 				   enum dma_data_direction dir)
1789c4a7965SKim Phillips {
17981eb024cSKim Phillips 	dma_addr_t dma_addr = dma_map_single(dev, data, len, dir);
18081eb024cSKim Phillips 
1819c4a7965SKim Phillips 	talitos_ptr->len = cpu_to_be16(len);
18281eb024cSKim Phillips 	to_talitos_ptr(talitos_ptr, dma_addr);
1839c4a7965SKim Phillips 	talitos_ptr->j_extent = extent;
1849c4a7965SKim Phillips }
1859c4a7965SKim Phillips 
1869c4a7965SKim Phillips /*
1879c4a7965SKim Phillips  * unmap bus single (contiguous) h/w descriptor pointer
1889c4a7965SKim Phillips  */
1899c4a7965SKim Phillips static void unmap_single_talitos_ptr(struct device *dev,
1909c4a7965SKim Phillips 				     struct talitos_ptr *talitos_ptr,
1919c4a7965SKim Phillips 				     enum dma_data_direction dir)
1929c4a7965SKim Phillips {
1939c4a7965SKim Phillips 	dma_unmap_single(dev, be32_to_cpu(talitos_ptr->ptr),
1949c4a7965SKim Phillips 			 be16_to_cpu(talitos_ptr->len), dir);
1959c4a7965SKim Phillips }
1969c4a7965SKim Phillips 
1979c4a7965SKim Phillips static int reset_channel(struct device *dev, int ch)
1989c4a7965SKim Phillips {
1999c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2009c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
2019c4a7965SKim Phillips 
202ad42d5fcSKim Phillips 	setbits32(priv->chan[ch].reg + TALITOS_CCCR, TALITOS_CCCR_RESET);
2039c4a7965SKim Phillips 
204ad42d5fcSKim Phillips 	while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) & TALITOS_CCCR_RESET)
2059c4a7965SKim Phillips 	       && --timeout)
2069c4a7965SKim Phillips 		cpu_relax();
2079c4a7965SKim Phillips 
2089c4a7965SKim Phillips 	if (timeout == 0) {
2099c4a7965SKim Phillips 		dev_err(dev, "failed to reset channel %d\n", ch);
2109c4a7965SKim Phillips 		return -EIO;
2119c4a7965SKim Phillips 	}
2129c4a7965SKim Phillips 
21381eb024cSKim Phillips 	/* set 36-bit addressing, done writeback enable and done IRQ enable */
214ad42d5fcSKim Phillips 	setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, TALITOS_CCCR_LO_EAE |
21581eb024cSKim Phillips 		  TALITOS_CCCR_LO_CDWE | TALITOS_CCCR_LO_CDIE);
2169c4a7965SKim Phillips 
217fe5720e2SKim Phillips 	/* and ICCR writeback, if available */
218fe5720e2SKim Phillips 	if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
219ad42d5fcSKim Phillips 		setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO,
220fe5720e2SKim Phillips 		          TALITOS_CCCR_LO_IWSE);
221fe5720e2SKim Phillips 
2229c4a7965SKim Phillips 	return 0;
2239c4a7965SKim Phillips }
2249c4a7965SKim Phillips 
2259c4a7965SKim Phillips static int reset_device(struct device *dev)
2269c4a7965SKim Phillips {
2279c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2289c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
229c3e337f8SKim Phillips 	u32 mcr = TALITOS_MCR_SWR;
2309c4a7965SKim Phillips 
231c3e337f8SKim Phillips 	setbits32(priv->reg + TALITOS_MCR, mcr);
2329c4a7965SKim Phillips 
2339c4a7965SKim Phillips 	while ((in_be32(priv->reg + TALITOS_MCR) & TALITOS_MCR_SWR)
2349c4a7965SKim Phillips 	       && --timeout)
2359c4a7965SKim Phillips 		cpu_relax();
2369c4a7965SKim Phillips 
2372cdba3cfSKim Phillips 	if (priv->irq[1]) {
238c3e337f8SKim Phillips 		mcr = TALITOS_MCR_RCA1 | TALITOS_MCR_RCA3;
239c3e337f8SKim Phillips 		setbits32(priv->reg + TALITOS_MCR, mcr);
240c3e337f8SKim Phillips 	}
241c3e337f8SKim Phillips 
2429c4a7965SKim Phillips 	if (timeout == 0) {
2439c4a7965SKim Phillips 		dev_err(dev, "failed to reset device\n");
2449c4a7965SKim Phillips 		return -EIO;
2459c4a7965SKim Phillips 	}
2469c4a7965SKim Phillips 
2479c4a7965SKim Phillips 	return 0;
2489c4a7965SKim Phillips }
2499c4a7965SKim Phillips 
2509c4a7965SKim Phillips /*
2519c4a7965SKim Phillips  * Reset and initialize the device
2529c4a7965SKim Phillips  */
2539c4a7965SKim Phillips static int init_device(struct device *dev)
2549c4a7965SKim Phillips {
2559c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2569c4a7965SKim Phillips 	int ch, err;
2579c4a7965SKim Phillips 
2589c4a7965SKim Phillips 	/*
2599c4a7965SKim Phillips 	 * Master reset
2609c4a7965SKim Phillips 	 * errata documentation: warning: certain SEC interrupts
2619c4a7965SKim Phillips 	 * are not fully cleared by writing the MCR:SWR bit,
2629c4a7965SKim Phillips 	 * set bit twice to completely reset
2639c4a7965SKim Phillips 	 */
2649c4a7965SKim Phillips 	err = reset_device(dev);
2659c4a7965SKim Phillips 	if (err)
2669c4a7965SKim Phillips 		return err;
2679c4a7965SKim Phillips 
2689c4a7965SKim Phillips 	err = reset_device(dev);
2699c4a7965SKim Phillips 	if (err)
2709c4a7965SKim Phillips 		return err;
2719c4a7965SKim Phillips 
2729c4a7965SKim Phillips 	/* reset channels */
2739c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++) {
2749c4a7965SKim Phillips 		err = reset_channel(dev, ch);
2759c4a7965SKim Phillips 		if (err)
2769c4a7965SKim Phillips 			return err;
2779c4a7965SKim Phillips 	}
2789c4a7965SKim Phillips 
2799c4a7965SKim Phillips 	/* enable channel done and error interrupts */
2809c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_IMR, TALITOS_IMR_INIT);
2819c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);
2829c4a7965SKim Phillips 
283fe5720e2SKim Phillips 	/* disable integrity check error interrupts (use writeback instead) */
284fe5720e2SKim Phillips 	if (priv->features & TALITOS_FTR_HW_AUTH_CHECK)
285fe5720e2SKim Phillips 		setbits32(priv->reg + TALITOS_MDEUICR_LO,
286fe5720e2SKim Phillips 		          TALITOS_MDEUICR_LO_ICE);
287fe5720e2SKim Phillips 
2889c4a7965SKim Phillips 	return 0;
2899c4a7965SKim Phillips }
2909c4a7965SKim Phillips 
2919c4a7965SKim Phillips /**
2929c4a7965SKim Phillips  * talitos_submit - submits a descriptor to the device for processing
2939c4a7965SKim Phillips  * @dev:	the SEC device to be used
2945228f0f7SKim Phillips  * @ch:		the SEC device channel to be used
2959c4a7965SKim Phillips  * @desc:	the descriptor to be processed by the device
2969c4a7965SKim Phillips  * @callback:	whom to call when processing is complete
2979c4a7965SKim Phillips  * @context:	a handle for use by caller (optional)
2989c4a7965SKim Phillips  *
2999c4a7965SKim Phillips  * desc must contain valid dma-mapped (bus physical) address pointers.
3009c4a7965SKim Phillips  * callback must check err and feedback in descriptor header
3019c4a7965SKim Phillips  * for device processing status.
3029c4a7965SKim Phillips  */
3035228f0f7SKim Phillips static int talitos_submit(struct device *dev, int ch, struct talitos_desc *desc,
3049c4a7965SKim Phillips 			  void (*callback)(struct device *dev,
3059c4a7965SKim Phillips 					   struct talitos_desc *desc,
3069c4a7965SKim Phillips 					   void *context, int error),
3079c4a7965SKim Phillips 			  void *context)
3089c4a7965SKim Phillips {
3099c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
3109c4a7965SKim Phillips 	struct talitos_request *request;
3115228f0f7SKim Phillips 	unsigned long flags;
3129c4a7965SKim Phillips 	int head;
3139c4a7965SKim Phillips 
3144b992628SKim Phillips 	spin_lock_irqsave(&priv->chan[ch].head_lock, flags);
3159c4a7965SKim Phillips 
3164b992628SKim Phillips 	if (!atomic_inc_not_zero(&priv->chan[ch].submit_count)) {
317ec6644d6SKim Phillips 		/* h/w fifo is full */
3184b992628SKim Phillips 		spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
3199c4a7965SKim Phillips 		return -EAGAIN;
3209c4a7965SKim Phillips 	}
3219c4a7965SKim Phillips 
3224b992628SKim Phillips 	head = priv->chan[ch].head;
3234b992628SKim Phillips 	request = &priv->chan[ch].fifo[head];
324ec6644d6SKim Phillips 
3259c4a7965SKim Phillips 	/* map descriptor and save caller data */
3269c4a7965SKim Phillips 	request->dma_desc = dma_map_single(dev, desc, sizeof(*desc),
3279c4a7965SKim Phillips 					   DMA_BIDIRECTIONAL);
3289c4a7965SKim Phillips 	request->callback = callback;
3299c4a7965SKim Phillips 	request->context = context;
3309c4a7965SKim Phillips 
3319c4a7965SKim Phillips 	/* increment fifo head */
3324b992628SKim Phillips 	priv->chan[ch].head = (priv->chan[ch].head + 1) & (priv->fifo_len - 1);
3339c4a7965SKim Phillips 
3349c4a7965SKim Phillips 	smp_wmb();
3359c4a7965SKim Phillips 	request->desc = desc;
3369c4a7965SKim Phillips 
3379c4a7965SKim Phillips 	/* GO! */
3389c4a7965SKim Phillips 	wmb();
339ad42d5fcSKim Phillips 	out_be32(priv->chan[ch].reg + TALITOS_FF,
340ad42d5fcSKim Phillips 		 upper_32_bits(request->dma_desc));
341ad42d5fcSKim Phillips 	out_be32(priv->chan[ch].reg + TALITOS_FF_LO,
342a752447aSKim Phillips 		 lower_32_bits(request->dma_desc));
3439c4a7965SKim Phillips 
3444b992628SKim Phillips 	spin_unlock_irqrestore(&priv->chan[ch].head_lock, flags);
3459c4a7965SKim Phillips 
3469c4a7965SKim Phillips 	return -EINPROGRESS;
3479c4a7965SKim Phillips }
3489c4a7965SKim Phillips 
3499c4a7965SKim Phillips /*
3509c4a7965SKim Phillips  * process what was done, notify callback of error if not
3519c4a7965SKim Phillips  */
3529c4a7965SKim Phillips static void flush_channel(struct device *dev, int ch, int error, int reset_ch)
3539c4a7965SKim Phillips {
3549c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
3559c4a7965SKim Phillips 	struct talitos_request *request, saved_req;
3569c4a7965SKim Phillips 	unsigned long flags;
3579c4a7965SKim Phillips 	int tail, status;
3589c4a7965SKim Phillips 
3594b992628SKim Phillips 	spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
3609c4a7965SKim Phillips 
3614b992628SKim Phillips 	tail = priv->chan[ch].tail;
3624b992628SKim Phillips 	while (priv->chan[ch].fifo[tail].desc) {
3634b992628SKim Phillips 		request = &priv->chan[ch].fifo[tail];
3649c4a7965SKim Phillips 
3659c4a7965SKim Phillips 		/* descriptors with their done bits set don't get the error */
3669c4a7965SKim Phillips 		rmb();
367ca38a814SLee Nipper 		if ((request->desc->hdr & DESC_HDR_DONE) == DESC_HDR_DONE)
3689c4a7965SKim Phillips 			status = 0;
369ca38a814SLee Nipper 		else
3709c4a7965SKim Phillips 			if (!error)
3719c4a7965SKim Phillips 				break;
3729c4a7965SKim Phillips 			else
3739c4a7965SKim Phillips 				status = error;
3749c4a7965SKim Phillips 
3759c4a7965SKim Phillips 		dma_unmap_single(dev, request->dma_desc,
376e938e465SKim Phillips 				 sizeof(struct talitos_desc),
377e938e465SKim Phillips 				 DMA_BIDIRECTIONAL);
3789c4a7965SKim Phillips 
3799c4a7965SKim Phillips 		/* copy entries so we can call callback outside lock */
3809c4a7965SKim Phillips 		saved_req.desc = request->desc;
3819c4a7965SKim Phillips 		saved_req.callback = request->callback;
3829c4a7965SKim Phillips 		saved_req.context = request->context;
3839c4a7965SKim Phillips 
3849c4a7965SKim Phillips 		/* release request entry in fifo */
3859c4a7965SKim Phillips 		smp_wmb();
3869c4a7965SKim Phillips 		request->desc = NULL;
3879c4a7965SKim Phillips 
3889c4a7965SKim Phillips 		/* increment fifo tail */
3894b992628SKim Phillips 		priv->chan[ch].tail = (tail + 1) & (priv->fifo_len - 1);
3909c4a7965SKim Phillips 
3914b992628SKim Phillips 		spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
392ec6644d6SKim Phillips 
3934b992628SKim Phillips 		atomic_dec(&priv->chan[ch].submit_count);
394ec6644d6SKim Phillips 
3959c4a7965SKim Phillips 		saved_req.callback(dev, saved_req.desc, saved_req.context,
3969c4a7965SKim Phillips 				   status);
3979c4a7965SKim Phillips 		/* channel may resume processing in single desc error case */
3989c4a7965SKim Phillips 		if (error && !reset_ch && status == error)
3999c4a7965SKim Phillips 			return;
4004b992628SKim Phillips 		spin_lock_irqsave(&priv->chan[ch].tail_lock, flags);
4014b992628SKim Phillips 		tail = priv->chan[ch].tail;
4029c4a7965SKim Phillips 	}
4039c4a7965SKim Phillips 
4044b992628SKim Phillips 	spin_unlock_irqrestore(&priv->chan[ch].tail_lock, flags);
4059c4a7965SKim Phillips }
4069c4a7965SKim Phillips 
4079c4a7965SKim Phillips /*
4089c4a7965SKim Phillips  * process completed requests for channels that have done status
4099c4a7965SKim Phillips  */
410c3e337f8SKim Phillips #define DEF_TALITOS_DONE(name, ch_done_mask)				\
411c3e337f8SKim Phillips static void talitos_done_##name(unsigned long data)			\
412c3e337f8SKim Phillips {									\
413c3e337f8SKim Phillips 	struct device *dev = (struct device *)data;			\
414c3e337f8SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);		\
415c3e337f8SKim Phillips 									\
416c3e337f8SKim Phillips 	if (ch_done_mask & 1)						\
417c3e337f8SKim Phillips 		flush_channel(dev, 0, 0, 0);				\
418c3e337f8SKim Phillips 	if (priv->num_channels == 1)					\
419c3e337f8SKim Phillips 		goto out;						\
420c3e337f8SKim Phillips 	if (ch_done_mask & (1 << 2))					\
421c3e337f8SKim Phillips 		flush_channel(dev, 1, 0, 0);				\
422c3e337f8SKim Phillips 	if (ch_done_mask & (1 << 4))					\
423c3e337f8SKim Phillips 		flush_channel(dev, 2, 0, 0);				\
424c3e337f8SKim Phillips 	if (ch_done_mask & (1 << 6))					\
425c3e337f8SKim Phillips 		flush_channel(dev, 3, 0, 0);				\
426c3e337f8SKim Phillips 									\
427c3e337f8SKim Phillips out:									\
428c3e337f8SKim Phillips 	/* At this point, all completed channels have been processed */	\
429c3e337f8SKim Phillips 	/* Unmask done interrupts for channels completed later on. */	\
430c3e337f8SKim Phillips 	setbits32(priv->reg + TALITOS_IMR, ch_done_mask);		\
431c3e337f8SKim Phillips 	setbits32(priv->reg + TALITOS_IMR_LO, TALITOS_IMR_LO_INIT);	\
4329c4a7965SKim Phillips }
433c3e337f8SKim Phillips DEF_TALITOS_DONE(4ch, TALITOS_ISR_4CHDONE)
434c3e337f8SKim Phillips DEF_TALITOS_DONE(ch0_2, TALITOS_ISR_CH_0_2_DONE)
435c3e337f8SKim Phillips DEF_TALITOS_DONE(ch1_3, TALITOS_ISR_CH_1_3_DONE)
4369c4a7965SKim Phillips 
4379c4a7965SKim Phillips /*
4389c4a7965SKim Phillips  * locate current (offending) descriptor
4399c4a7965SKim Phillips  */
4403e721aebSKim Phillips static u32 current_desc_hdr(struct device *dev, int ch)
4419c4a7965SKim Phillips {
4429c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
4434b992628SKim Phillips 	int tail = priv->chan[ch].tail;
4449c4a7965SKim Phillips 	dma_addr_t cur_desc;
4459c4a7965SKim Phillips 
446ad42d5fcSKim Phillips 	cur_desc = in_be32(priv->chan[ch].reg + TALITOS_CDPR_LO);
4479c4a7965SKim Phillips 
4484b992628SKim Phillips 	while (priv->chan[ch].fifo[tail].dma_desc != cur_desc) {
4499c4a7965SKim Phillips 		tail = (tail + 1) & (priv->fifo_len - 1);
4504b992628SKim Phillips 		if (tail == priv->chan[ch].tail) {
4519c4a7965SKim Phillips 			dev_err(dev, "couldn't locate current descriptor\n");
4523e721aebSKim Phillips 			return 0;
4539c4a7965SKim Phillips 		}
4549c4a7965SKim Phillips 	}
4559c4a7965SKim Phillips 
4563e721aebSKim Phillips 	return priv->chan[ch].fifo[tail].desc->hdr;
4579c4a7965SKim Phillips }
4589c4a7965SKim Phillips 
4599c4a7965SKim Phillips /*
4609c4a7965SKim Phillips  * user diagnostics; report root cause of error based on execution unit status
4619c4a7965SKim Phillips  */
4623e721aebSKim Phillips static void report_eu_error(struct device *dev, int ch, u32 desc_hdr)
4639c4a7965SKim Phillips {
4649c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
4659c4a7965SKim Phillips 	int i;
4669c4a7965SKim Phillips 
4673e721aebSKim Phillips 	if (!desc_hdr)
468ad42d5fcSKim Phillips 		desc_hdr = in_be32(priv->chan[ch].reg + TALITOS_DESCBUF);
4693e721aebSKim Phillips 
4703e721aebSKim Phillips 	switch (desc_hdr & DESC_HDR_SEL0_MASK) {
4719c4a7965SKim Phillips 	case DESC_HDR_SEL0_AFEU:
4729c4a7965SKim Phillips 		dev_err(dev, "AFEUISR 0x%08x_%08x\n",
4739c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AFEUISR),
4749c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AFEUISR_LO));
4759c4a7965SKim Phillips 		break;
4769c4a7965SKim Phillips 	case DESC_HDR_SEL0_DEU:
4779c4a7965SKim Phillips 		dev_err(dev, "DEUISR 0x%08x_%08x\n",
4789c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DEUISR),
4799c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_DEUISR_LO));
4809c4a7965SKim Phillips 		break;
4819c4a7965SKim Phillips 	case DESC_HDR_SEL0_MDEUA:
4829c4a7965SKim Phillips 	case DESC_HDR_SEL0_MDEUB:
4839c4a7965SKim Phillips 		dev_err(dev, "MDEUISR 0x%08x_%08x\n",
4849c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR),
4859c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR_LO));
4869c4a7965SKim Phillips 		break;
4879c4a7965SKim Phillips 	case DESC_HDR_SEL0_RNG:
4889c4a7965SKim Phillips 		dev_err(dev, "RNGUISR 0x%08x_%08x\n",
4899c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_RNGUISR),
4909c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_RNGUISR_LO));
4919c4a7965SKim Phillips 		break;
4929c4a7965SKim Phillips 	case DESC_HDR_SEL0_PKEU:
4939c4a7965SKim Phillips 		dev_err(dev, "PKEUISR 0x%08x_%08x\n",
4949c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_PKEUISR),
4959c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_PKEUISR_LO));
4969c4a7965SKim Phillips 		break;
4979c4a7965SKim Phillips 	case DESC_HDR_SEL0_AESU:
4989c4a7965SKim Phillips 		dev_err(dev, "AESUISR 0x%08x_%08x\n",
4999c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AESUISR),
5009c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_AESUISR_LO));
5019c4a7965SKim Phillips 		break;
5029c4a7965SKim Phillips 	case DESC_HDR_SEL0_CRCU:
5039c4a7965SKim Phillips 		dev_err(dev, "CRCUISR 0x%08x_%08x\n",
5049c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR),
5059c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR_LO));
5069c4a7965SKim Phillips 		break;
5079c4a7965SKim Phillips 	case DESC_HDR_SEL0_KEU:
5089c4a7965SKim Phillips 		dev_err(dev, "KEUISR 0x%08x_%08x\n",
5099c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_KEUISR),
5109c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_KEUISR_LO));
5119c4a7965SKim Phillips 		break;
5129c4a7965SKim Phillips 	}
5139c4a7965SKim Phillips 
5143e721aebSKim Phillips 	switch (desc_hdr & DESC_HDR_SEL1_MASK) {
5159c4a7965SKim Phillips 	case DESC_HDR_SEL1_MDEUA:
5169c4a7965SKim Phillips 	case DESC_HDR_SEL1_MDEUB:
5179c4a7965SKim Phillips 		dev_err(dev, "MDEUISR 0x%08x_%08x\n",
5189c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR),
5199c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_MDEUISR_LO));
5209c4a7965SKim Phillips 		break;
5219c4a7965SKim Phillips 	case DESC_HDR_SEL1_CRCU:
5229c4a7965SKim Phillips 		dev_err(dev, "CRCUISR 0x%08x_%08x\n",
5239c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR),
5249c4a7965SKim Phillips 			in_be32(priv->reg + TALITOS_CRCUISR_LO));
5259c4a7965SKim Phillips 		break;
5269c4a7965SKim Phillips 	}
5279c4a7965SKim Phillips 
5289c4a7965SKim Phillips 	for (i = 0; i < 8; i++)
5299c4a7965SKim Phillips 		dev_err(dev, "DESCBUF 0x%08x_%08x\n",
530ad42d5fcSKim Phillips 			in_be32(priv->chan[ch].reg + TALITOS_DESCBUF + 8*i),
531ad42d5fcSKim Phillips 			in_be32(priv->chan[ch].reg + TALITOS_DESCBUF_LO + 8*i));
5329c4a7965SKim Phillips }
5339c4a7965SKim Phillips 
5349c4a7965SKim Phillips /*
5359c4a7965SKim Phillips  * recover from error interrupts
5369c4a7965SKim Phillips  */
537*5e718a09SKim Phillips static void talitos_error(struct device *dev, u32 isr, u32 isr_lo)
5389c4a7965SKim Phillips {
5399c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
5409c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
5419c4a7965SKim Phillips 	int ch, error, reset_dev = 0, reset_ch = 0;
54240405f10SKim Phillips 	u32 v, v_lo;
5439c4a7965SKim Phillips 
5449c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++) {
5459c4a7965SKim Phillips 		/* skip channels without errors */
5469c4a7965SKim Phillips 		if (!(isr & (1 << (ch * 2 + 1))))
5479c4a7965SKim Phillips 			continue;
5489c4a7965SKim Phillips 
5499c4a7965SKim Phillips 		error = -EINVAL;
5509c4a7965SKim Phillips 
551ad42d5fcSKim Phillips 		v = in_be32(priv->chan[ch].reg + TALITOS_CCPSR);
552ad42d5fcSKim Phillips 		v_lo = in_be32(priv->chan[ch].reg + TALITOS_CCPSR_LO);
5539c4a7965SKim Phillips 
5549c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_DOF) {
5559c4a7965SKim Phillips 			dev_err(dev, "double fetch fifo overflow error\n");
5569c4a7965SKim Phillips 			error = -EAGAIN;
5579c4a7965SKim Phillips 			reset_ch = 1;
5589c4a7965SKim Phillips 		}
5599c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SOF) {
5609c4a7965SKim Phillips 			/* h/w dropped descriptor */
5619c4a7965SKim Phillips 			dev_err(dev, "single fetch fifo overflow error\n");
5629c4a7965SKim Phillips 			error = -EAGAIN;
5639c4a7965SKim Phillips 		}
5649c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_MDTE)
5659c4a7965SKim Phillips 			dev_err(dev, "master data transfer error\n");
5669c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
5679c4a7965SKim Phillips 			dev_err(dev, "s/g data length zero error\n");
5689c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_FPZ)
5699c4a7965SKim Phillips 			dev_err(dev, "fetch pointer zero error\n");
5709c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_IDH)
5719c4a7965SKim Phillips 			dev_err(dev, "illegal descriptor header error\n");
5729c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_IEU)
5739c4a7965SKim Phillips 			dev_err(dev, "invalid execution unit error\n");
5749c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_EU)
5753e721aebSKim Phillips 			report_eu_error(dev, ch, current_desc_hdr(dev, ch));
5769c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_GB)
5779c4a7965SKim Phillips 			dev_err(dev, "gather boundary error\n");
5789c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_GRL)
5799c4a7965SKim Phillips 			dev_err(dev, "gather return/length error\n");
5809c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SB)
5819c4a7965SKim Phillips 			dev_err(dev, "scatter boundary error\n");
5829c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SRL)
5839c4a7965SKim Phillips 			dev_err(dev, "scatter return/length error\n");
5849c4a7965SKim Phillips 
5859c4a7965SKim Phillips 		flush_channel(dev, ch, error, reset_ch);
5869c4a7965SKim Phillips 
5879c4a7965SKim Phillips 		if (reset_ch) {
5889c4a7965SKim Phillips 			reset_channel(dev, ch);
5899c4a7965SKim Phillips 		} else {
590ad42d5fcSKim Phillips 			setbits32(priv->chan[ch].reg + TALITOS_CCCR,
5919c4a7965SKim Phillips 				  TALITOS_CCCR_CONT);
592ad42d5fcSKim Phillips 			setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, 0);
593ad42d5fcSKim Phillips 			while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) &
5949c4a7965SKim Phillips 			       TALITOS_CCCR_CONT) && --timeout)
5959c4a7965SKim Phillips 				cpu_relax();
5969c4a7965SKim Phillips 			if (timeout == 0) {
5979c4a7965SKim Phillips 				dev_err(dev, "failed to restart channel %d\n",
5989c4a7965SKim Phillips 					ch);
5999c4a7965SKim Phillips 				reset_dev = 1;
6009c4a7965SKim Phillips 			}
6019c4a7965SKim Phillips 		}
6029c4a7965SKim Phillips 	}
603c3e337f8SKim Phillips 	if (reset_dev || isr & ~TALITOS_ISR_4CHERR || isr_lo) {
6049c4a7965SKim Phillips 		dev_err(dev, "done overflow, internal time out, or rngu error: "
6059c4a7965SKim Phillips 		        "ISR 0x%08x_%08x\n", isr, isr_lo);
6069c4a7965SKim Phillips 
6079c4a7965SKim Phillips 		/* purge request queues */
6089c4a7965SKim Phillips 		for (ch = 0; ch < priv->num_channels; ch++)
6099c4a7965SKim Phillips 			flush_channel(dev, ch, -EIO, 1);
6109c4a7965SKim Phillips 
6119c4a7965SKim Phillips 		/* reset and reinitialize the device */
6129c4a7965SKim Phillips 		init_device(dev);
6139c4a7965SKim Phillips 	}
6149c4a7965SKim Phillips }
6159c4a7965SKim Phillips 
616c3e337f8SKim Phillips #define DEF_TALITOS_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet)	       \
617c3e337f8SKim Phillips static irqreturn_t talitos_interrupt_##name(int irq, void *data)	       \
618c3e337f8SKim Phillips {									       \
619c3e337f8SKim Phillips 	struct device *dev = data;					       \
620c3e337f8SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);		       \
621c3e337f8SKim Phillips 	u32 isr, isr_lo;						       \
622c3e337f8SKim Phillips 									       \
623c3e337f8SKim Phillips 	isr = in_be32(priv->reg + TALITOS_ISR);				       \
624c3e337f8SKim Phillips 	isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);			       \
625c3e337f8SKim Phillips 	/* Acknowledge interrupt */					       \
626c3e337f8SKim Phillips 	out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
627c3e337f8SKim Phillips 	out_be32(priv->reg + TALITOS_ICR_LO, isr_lo);			       \
628c3e337f8SKim Phillips 									       \
629c3e337f8SKim Phillips 	if (unlikely((isr & ~TALITOS_ISR_4CHDONE) & ch_err_mask || isr_lo))    \
630*5e718a09SKim Phillips 		talitos_error(dev, isr, isr_lo);			       \
631c3e337f8SKim Phillips 	else								       \
632c3e337f8SKim Phillips 		if (likely(isr & ch_done_mask)) {			       \
633c3e337f8SKim Phillips 			/* mask further done interrupts. */		       \
634c3e337f8SKim Phillips 			clrbits32(priv->reg + TALITOS_IMR, ch_done_mask);      \
635c3e337f8SKim Phillips 			/* done_task will unmask done interrupts at exit */    \
636c3e337f8SKim Phillips 			tasklet_schedule(&priv->done_task[tlet]);	       \
637c3e337f8SKim Phillips 		}							       \
638c3e337f8SKim Phillips 									       \
639c3e337f8SKim Phillips 	return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED :  \
640c3e337f8SKim Phillips 								IRQ_NONE;      \
6411c2e8811SLee Nipper }
642c3e337f8SKim Phillips DEF_TALITOS_INTERRUPT(4ch, TALITOS_ISR_4CHDONE, TALITOS_ISR_4CHERR, 0)
643c3e337f8SKim Phillips DEF_TALITOS_INTERRUPT(ch0_2, TALITOS_ISR_CH_0_2_DONE, TALITOS_ISR_CH_0_2_ERR, 0)
644c3e337f8SKim Phillips DEF_TALITOS_INTERRUPT(ch1_3, TALITOS_ISR_CH_1_3_DONE, TALITOS_ISR_CH_1_3_ERR, 1)
6459c4a7965SKim Phillips 
6469c4a7965SKim Phillips /*
6479c4a7965SKim Phillips  * hwrng
6489c4a7965SKim Phillips  */
6499c4a7965SKim Phillips static int talitos_rng_data_present(struct hwrng *rng, int wait)
6509c4a7965SKim Phillips {
6519c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
6529c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6539c4a7965SKim Phillips 	u32 ofl;
6549c4a7965SKim Phillips 	int i;
6559c4a7965SKim Phillips 
6569c4a7965SKim Phillips 	for (i = 0; i < 20; i++) {
6579c4a7965SKim Phillips 		ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
6589c4a7965SKim Phillips 		      TALITOS_RNGUSR_LO_OFL;
6599c4a7965SKim Phillips 		if (ofl || !wait)
6609c4a7965SKim Phillips 			break;
6619c4a7965SKim Phillips 		udelay(10);
6629c4a7965SKim Phillips 	}
6639c4a7965SKim Phillips 
6649c4a7965SKim Phillips 	return !!ofl;
6659c4a7965SKim Phillips }
6669c4a7965SKim Phillips 
6679c4a7965SKim Phillips static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
6689c4a7965SKim Phillips {
6699c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
6709c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6719c4a7965SKim Phillips 
6729c4a7965SKim Phillips 	/* rng fifo requires 64-bit accesses */
6739c4a7965SKim Phillips 	*data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
6749c4a7965SKim Phillips 	*data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
6759c4a7965SKim Phillips 
6769c4a7965SKim Phillips 	return sizeof(u32);
6779c4a7965SKim Phillips }
6789c4a7965SKim Phillips 
6799c4a7965SKim Phillips static int talitos_rng_init(struct hwrng *rng)
6809c4a7965SKim Phillips {
6819c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
6829c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6839c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
6849c4a7965SKim Phillips 
6859c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
6869c4a7965SKim Phillips 	while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
6879c4a7965SKim Phillips 	       && --timeout)
6889c4a7965SKim Phillips 		cpu_relax();
6899c4a7965SKim Phillips 	if (timeout == 0) {
6909c4a7965SKim Phillips 		dev_err(dev, "failed to reset rng hw\n");
6919c4a7965SKim Phillips 		return -ENODEV;
6929c4a7965SKim Phillips 	}
6939c4a7965SKim Phillips 
6949c4a7965SKim Phillips 	/* start generating */
6959c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
6969c4a7965SKim Phillips 
6979c4a7965SKim Phillips 	return 0;
6989c4a7965SKim Phillips }
6999c4a7965SKim Phillips 
7009c4a7965SKim Phillips static int talitos_register_rng(struct device *dev)
7019c4a7965SKim Phillips {
7029c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
7039c4a7965SKim Phillips 
7049c4a7965SKim Phillips 	priv->rng.name		= dev_driver_string(dev),
7059c4a7965SKim Phillips 	priv->rng.init		= talitos_rng_init,
7069c4a7965SKim Phillips 	priv->rng.data_present	= talitos_rng_data_present,
7079c4a7965SKim Phillips 	priv->rng.data_read	= talitos_rng_data_read,
7089c4a7965SKim Phillips 	priv->rng.priv		= (unsigned long)dev;
7099c4a7965SKim Phillips 
7109c4a7965SKim Phillips 	return hwrng_register(&priv->rng);
7119c4a7965SKim Phillips }
7129c4a7965SKim Phillips 
7139c4a7965SKim Phillips static void talitos_unregister_rng(struct device *dev)
7149c4a7965SKim Phillips {
7159c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
7169c4a7965SKim Phillips 
7179c4a7965SKim Phillips 	hwrng_unregister(&priv->rng);
7189c4a7965SKim Phillips }
7199c4a7965SKim Phillips 
7209c4a7965SKim Phillips /*
7219c4a7965SKim Phillips  * crypto alg
7229c4a7965SKim Phillips  */
7239c4a7965SKim Phillips #define TALITOS_CRA_PRIORITY		3000
7249c4a7965SKim Phillips #define TALITOS_MAX_KEY_SIZE		64
7253952f17eSLee Nipper #define TALITOS_MAX_IV_LENGTH		16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
72670bcaca7SLee Nipper 
727497f2e6bSLee Nipper #define MD5_BLOCK_SIZE    64
7289c4a7965SKim Phillips 
7299c4a7965SKim Phillips struct talitos_ctx {
7309c4a7965SKim Phillips 	struct device *dev;
7315228f0f7SKim Phillips 	int ch;
7329c4a7965SKim Phillips 	__be32 desc_hdr_template;
7339c4a7965SKim Phillips 	u8 key[TALITOS_MAX_KEY_SIZE];
73470bcaca7SLee Nipper 	u8 iv[TALITOS_MAX_IV_LENGTH];
7359c4a7965SKim Phillips 	unsigned int keylen;
7369c4a7965SKim Phillips 	unsigned int enckeylen;
7379c4a7965SKim Phillips 	unsigned int authkeylen;
7389c4a7965SKim Phillips 	unsigned int authsize;
7399c4a7965SKim Phillips };
7409c4a7965SKim Phillips 
741497f2e6bSLee Nipper #define HASH_MAX_BLOCK_SIZE		SHA512_BLOCK_SIZE
742497f2e6bSLee Nipper #define TALITOS_MDEU_MAX_CONTEXT_SIZE	TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
743497f2e6bSLee Nipper 
744497f2e6bSLee Nipper struct talitos_ahash_req_ctx {
74560f208d7SKim Phillips 	u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
746497f2e6bSLee Nipper 	unsigned int hw_context_size;
747497f2e6bSLee Nipper 	u8 buf[HASH_MAX_BLOCK_SIZE];
748497f2e6bSLee Nipper 	u8 bufnext[HASH_MAX_BLOCK_SIZE];
74960f208d7SKim Phillips 	unsigned int swinit;
750497f2e6bSLee Nipper 	unsigned int first;
751497f2e6bSLee Nipper 	unsigned int last;
752497f2e6bSLee Nipper 	unsigned int to_hash_later;
7535e833bc4SLee Nipper 	u64 nbuf;
754497f2e6bSLee Nipper 	struct scatterlist bufsl[2];
755497f2e6bSLee Nipper 	struct scatterlist *psrc;
756497f2e6bSLee Nipper };
757497f2e6bSLee Nipper 
75856af8cd4SLee Nipper static int aead_setauthsize(struct crypto_aead *authenc,
7599c4a7965SKim Phillips 			    unsigned int authsize)
7609c4a7965SKim Phillips {
7619c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7629c4a7965SKim Phillips 
7639c4a7965SKim Phillips 	ctx->authsize = authsize;
7649c4a7965SKim Phillips 
7659c4a7965SKim Phillips 	return 0;
7669c4a7965SKim Phillips }
7679c4a7965SKim Phillips 
76856af8cd4SLee Nipper static int aead_setkey(struct crypto_aead *authenc,
7699c4a7965SKim Phillips 		       const u8 *key, unsigned int keylen)
7709c4a7965SKim Phillips {
7719c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7729c4a7965SKim Phillips 	struct rtattr *rta = (void *)key;
7739c4a7965SKim Phillips 	struct crypto_authenc_key_param *param;
7749c4a7965SKim Phillips 	unsigned int authkeylen;
7759c4a7965SKim Phillips 	unsigned int enckeylen;
7769c4a7965SKim Phillips 
7779c4a7965SKim Phillips 	if (!RTA_OK(rta, keylen))
7789c4a7965SKim Phillips 		goto badkey;
7799c4a7965SKim Phillips 
7809c4a7965SKim Phillips 	if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
7819c4a7965SKim Phillips 		goto badkey;
7829c4a7965SKim Phillips 
7839c4a7965SKim Phillips 	if (RTA_PAYLOAD(rta) < sizeof(*param))
7849c4a7965SKim Phillips 		goto badkey;
7859c4a7965SKim Phillips 
7869c4a7965SKim Phillips 	param = RTA_DATA(rta);
7879c4a7965SKim Phillips 	enckeylen = be32_to_cpu(param->enckeylen);
7889c4a7965SKim Phillips 
7899c4a7965SKim Phillips 	key += RTA_ALIGN(rta->rta_len);
7909c4a7965SKim Phillips 	keylen -= RTA_ALIGN(rta->rta_len);
7919c4a7965SKim Phillips 
7929c4a7965SKim Phillips 	if (keylen < enckeylen)
7939c4a7965SKim Phillips 		goto badkey;
7949c4a7965SKim Phillips 
7959c4a7965SKim Phillips 	authkeylen = keylen - enckeylen;
7969c4a7965SKim Phillips 
7979c4a7965SKim Phillips 	if (keylen > TALITOS_MAX_KEY_SIZE)
7989c4a7965SKim Phillips 		goto badkey;
7999c4a7965SKim Phillips 
8009c4a7965SKim Phillips 	memcpy(&ctx->key, key, keylen);
8019c4a7965SKim Phillips 
8029c4a7965SKim Phillips 	ctx->keylen = keylen;
8039c4a7965SKim Phillips 	ctx->enckeylen = enckeylen;
8049c4a7965SKim Phillips 	ctx->authkeylen = authkeylen;
8059c4a7965SKim Phillips 
8069c4a7965SKim Phillips 	return 0;
8079c4a7965SKim Phillips 
8089c4a7965SKim Phillips badkey:
8099c4a7965SKim Phillips 	crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
8109c4a7965SKim Phillips 	return -EINVAL;
8119c4a7965SKim Phillips }
8129c4a7965SKim Phillips 
8139c4a7965SKim Phillips /*
81456af8cd4SLee Nipper  * talitos_edesc - s/w-extended descriptor
8159c4a7965SKim Phillips  * @src_nents: number of segments in input scatterlist
8169c4a7965SKim Phillips  * @dst_nents: number of segments in output scatterlist
8179c4a7965SKim Phillips  * @dma_len: length of dma mapped link_tbl space
8189c4a7965SKim Phillips  * @dma_link_tbl: bus physical address of link_tbl
8199c4a7965SKim Phillips  * @desc: h/w descriptor
8209c4a7965SKim Phillips  * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
8219c4a7965SKim Phillips  *
8229c4a7965SKim Phillips  * if decrypting (with authcheck), or either one of src_nents or dst_nents
8239c4a7965SKim Phillips  * is greater than 1, an integrity check value is concatenated to the end
8249c4a7965SKim Phillips  * of link_tbl data
8259c4a7965SKim Phillips  */
82656af8cd4SLee Nipper struct talitos_edesc {
8279c4a7965SKim Phillips 	int src_nents;
8289c4a7965SKim Phillips 	int dst_nents;
8294de9d0b5SLee Nipper 	int src_is_chained;
8304de9d0b5SLee Nipper 	int dst_is_chained;
8319c4a7965SKim Phillips 	int dma_len;
8329c4a7965SKim Phillips 	dma_addr_t dma_link_tbl;
8339c4a7965SKim Phillips 	struct talitos_desc desc;
8349c4a7965SKim Phillips 	struct talitos_ptr link_tbl[0];
8359c4a7965SKim Phillips };
8369c4a7965SKim Phillips 
8374de9d0b5SLee Nipper static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
8384de9d0b5SLee Nipper 			  unsigned int nents, enum dma_data_direction dir,
8394de9d0b5SLee Nipper 			  int chained)
8404de9d0b5SLee Nipper {
8414de9d0b5SLee Nipper 	if (unlikely(chained))
8424de9d0b5SLee Nipper 		while (sg) {
8434de9d0b5SLee Nipper 			dma_map_sg(dev, sg, 1, dir);
8444de9d0b5SLee Nipper 			sg = scatterwalk_sg_next(sg);
8454de9d0b5SLee Nipper 		}
8464de9d0b5SLee Nipper 	else
8474de9d0b5SLee Nipper 		dma_map_sg(dev, sg, nents, dir);
8484de9d0b5SLee Nipper 	return nents;
8494de9d0b5SLee Nipper }
8504de9d0b5SLee Nipper 
8514de9d0b5SLee Nipper static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
8524de9d0b5SLee Nipper 				   enum dma_data_direction dir)
8534de9d0b5SLee Nipper {
8544de9d0b5SLee Nipper 	while (sg) {
8554de9d0b5SLee Nipper 		dma_unmap_sg(dev, sg, 1, dir);
8564de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
8574de9d0b5SLee Nipper 	}
8584de9d0b5SLee Nipper }
8594de9d0b5SLee Nipper 
8604de9d0b5SLee Nipper static void talitos_sg_unmap(struct device *dev,
8614de9d0b5SLee Nipper 			     struct talitos_edesc *edesc,
8624de9d0b5SLee Nipper 			     struct scatterlist *src,
8634de9d0b5SLee Nipper 			     struct scatterlist *dst)
8644de9d0b5SLee Nipper {
8654de9d0b5SLee Nipper 	unsigned int src_nents = edesc->src_nents ? : 1;
8664de9d0b5SLee Nipper 	unsigned int dst_nents = edesc->dst_nents ? : 1;
8674de9d0b5SLee Nipper 
8684de9d0b5SLee Nipper 	if (src != dst) {
8694de9d0b5SLee Nipper 		if (edesc->src_is_chained)
8704de9d0b5SLee Nipper 			talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
8714de9d0b5SLee Nipper 		else
8724de9d0b5SLee Nipper 			dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
8734de9d0b5SLee Nipper 
874497f2e6bSLee Nipper 		if (dst) {
8754de9d0b5SLee Nipper 			if (edesc->dst_is_chained)
876497f2e6bSLee Nipper 				talitos_unmap_sg_chain(dev, dst,
877497f2e6bSLee Nipper 						       DMA_FROM_DEVICE);
8784de9d0b5SLee Nipper 			else
879497f2e6bSLee Nipper 				dma_unmap_sg(dev, dst, dst_nents,
880497f2e6bSLee Nipper 					     DMA_FROM_DEVICE);
881497f2e6bSLee Nipper 		}
8824de9d0b5SLee Nipper 	} else
8834de9d0b5SLee Nipper 		if (edesc->src_is_chained)
8844de9d0b5SLee Nipper 			talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
8854de9d0b5SLee Nipper 		else
8864de9d0b5SLee Nipper 			dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
8874de9d0b5SLee Nipper }
8884de9d0b5SLee Nipper 
8899c4a7965SKim Phillips static void ipsec_esp_unmap(struct device *dev,
89056af8cd4SLee Nipper 			    struct talitos_edesc *edesc,
8919c4a7965SKim Phillips 			    struct aead_request *areq)
8929c4a7965SKim Phillips {
8939c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
8949c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
8959c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
8969c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
8979c4a7965SKim Phillips 
8989c4a7965SKim Phillips 	dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE);
8999c4a7965SKim Phillips 
9004de9d0b5SLee Nipper 	talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
9019c4a7965SKim Phillips 
9029c4a7965SKim Phillips 	if (edesc->dma_len)
9039c4a7965SKim Phillips 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
9049c4a7965SKim Phillips 				 DMA_BIDIRECTIONAL);
9059c4a7965SKim Phillips }
9069c4a7965SKim Phillips 
9079c4a7965SKim Phillips /*
9089c4a7965SKim Phillips  * ipsec_esp descriptor callbacks
9099c4a7965SKim Phillips  */
9109c4a7965SKim Phillips static void ipsec_esp_encrypt_done(struct device *dev,
9119c4a7965SKim Phillips 				   struct talitos_desc *desc, void *context,
9129c4a7965SKim Phillips 				   int err)
9139c4a7965SKim Phillips {
9149c4a7965SKim Phillips 	struct aead_request *areq = context;
9159c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
9169c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
91719bbbc63SKim Phillips 	struct talitos_edesc *edesc;
9189c4a7965SKim Phillips 	struct scatterlist *sg;
9199c4a7965SKim Phillips 	void *icvdata;
9209c4a7965SKim Phillips 
92119bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
92219bbbc63SKim Phillips 
9239c4a7965SKim Phillips 	ipsec_esp_unmap(dev, edesc, areq);
9249c4a7965SKim Phillips 
9259c4a7965SKim Phillips 	/* copy the generated ICV to dst */
9269c4a7965SKim Phillips 	if (edesc->dma_len) {
9279c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[edesc->src_nents +
928f3c85bc1SLee Nipper 					   edesc->dst_nents + 2];
9299c4a7965SKim Phillips 		sg = sg_last(areq->dst, edesc->dst_nents);
9309c4a7965SKim Phillips 		memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
9319c4a7965SKim Phillips 		       icvdata, ctx->authsize);
9329c4a7965SKim Phillips 	}
9339c4a7965SKim Phillips 
9349c4a7965SKim Phillips 	kfree(edesc);
9359c4a7965SKim Phillips 
9369c4a7965SKim Phillips 	aead_request_complete(areq, err);
9379c4a7965SKim Phillips }
9389c4a7965SKim Phillips 
939fe5720e2SKim Phillips static void ipsec_esp_decrypt_swauth_done(struct device *dev,
940e938e465SKim Phillips 					  struct talitos_desc *desc,
941e938e465SKim Phillips 					  void *context, int err)
9429c4a7965SKim Phillips {
9439c4a7965SKim Phillips 	struct aead_request *req = context;
9449c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
9459c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
94619bbbc63SKim Phillips 	struct talitos_edesc *edesc;
9479c4a7965SKim Phillips 	struct scatterlist *sg;
9489c4a7965SKim Phillips 	void *icvdata;
9499c4a7965SKim Phillips 
95019bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
95119bbbc63SKim Phillips 
9529c4a7965SKim Phillips 	ipsec_esp_unmap(dev, edesc, req);
9539c4a7965SKim Phillips 
9549c4a7965SKim Phillips 	if (!err) {
9559c4a7965SKim Phillips 		/* auth check */
9569c4a7965SKim Phillips 		if (edesc->dma_len)
9579c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[edesc->src_nents +
958f3c85bc1SLee Nipper 						   edesc->dst_nents + 2];
9599c4a7965SKim Phillips 		else
9609c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[0];
9619c4a7965SKim Phillips 
9629c4a7965SKim Phillips 		sg = sg_last(req->dst, edesc->dst_nents ? : 1);
9639c4a7965SKim Phillips 		err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
9649c4a7965SKim Phillips 			     ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
9659c4a7965SKim Phillips 	}
9669c4a7965SKim Phillips 
9679c4a7965SKim Phillips 	kfree(edesc);
9689c4a7965SKim Phillips 
9699c4a7965SKim Phillips 	aead_request_complete(req, err);
9709c4a7965SKim Phillips }
9719c4a7965SKim Phillips 
972fe5720e2SKim Phillips static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
973e938e465SKim Phillips 					  struct talitos_desc *desc,
974e938e465SKim Phillips 					  void *context, int err)
975fe5720e2SKim Phillips {
976fe5720e2SKim Phillips 	struct aead_request *req = context;
97719bbbc63SKim Phillips 	struct talitos_edesc *edesc;
97819bbbc63SKim Phillips 
97919bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
980fe5720e2SKim Phillips 
981fe5720e2SKim Phillips 	ipsec_esp_unmap(dev, edesc, req);
982fe5720e2SKim Phillips 
983fe5720e2SKim Phillips 	/* check ICV auth status */
984e938e465SKim Phillips 	if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
985e938e465SKim Phillips 		     DESC_HDR_LO_ICCR1_PASS))
986fe5720e2SKim Phillips 		err = -EBADMSG;
987fe5720e2SKim Phillips 
988fe5720e2SKim Phillips 	kfree(edesc);
989fe5720e2SKim Phillips 
990fe5720e2SKim Phillips 	aead_request_complete(req, err);
991fe5720e2SKim Phillips }
992fe5720e2SKim Phillips 
9939c4a7965SKim Phillips /*
9949c4a7965SKim Phillips  * convert scatterlist to SEC h/w link table format
9959c4a7965SKim Phillips  * stop at cryptlen bytes
9969c4a7965SKim Phillips  */
99770bcaca7SLee Nipper static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
9989c4a7965SKim Phillips 			   int cryptlen, struct talitos_ptr *link_tbl_ptr)
9999c4a7965SKim Phillips {
100070bcaca7SLee Nipper 	int n_sg = sg_count;
100170bcaca7SLee Nipper 
100270bcaca7SLee Nipper 	while (n_sg--) {
100381eb024cSKim Phillips 		to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg));
10049c4a7965SKim Phillips 		link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
10059c4a7965SKim Phillips 		link_tbl_ptr->j_extent = 0;
10069c4a7965SKim Phillips 		link_tbl_ptr++;
10079c4a7965SKim Phillips 		cryptlen -= sg_dma_len(sg);
10084de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
10099c4a7965SKim Phillips 	}
10109c4a7965SKim Phillips 
101170bcaca7SLee Nipper 	/* adjust (decrease) last one (or two) entry's len to cryptlen */
10129c4a7965SKim Phillips 	link_tbl_ptr--;
1013c0e741d4SKim Phillips 	while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
101470bcaca7SLee Nipper 		/* Empty this entry, and move to previous one */
101570bcaca7SLee Nipper 		cryptlen += be16_to_cpu(link_tbl_ptr->len);
101670bcaca7SLee Nipper 		link_tbl_ptr->len = 0;
101770bcaca7SLee Nipper 		sg_count--;
101870bcaca7SLee Nipper 		link_tbl_ptr--;
101970bcaca7SLee Nipper 	}
10209c4a7965SKim Phillips 	link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len)
10219c4a7965SKim Phillips 					+ cryptlen);
10229c4a7965SKim Phillips 
10239c4a7965SKim Phillips 	/* tag end of link table */
10249c4a7965SKim Phillips 	link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
102570bcaca7SLee Nipper 
102670bcaca7SLee Nipper 	return sg_count;
10279c4a7965SKim Phillips }
10289c4a7965SKim Phillips 
10299c4a7965SKim Phillips /*
10309c4a7965SKim Phillips  * fill in and submit ipsec_esp descriptor
10319c4a7965SKim Phillips  */
103256af8cd4SLee Nipper static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
10339c4a7965SKim Phillips 		     u8 *giv, u64 seq,
10349c4a7965SKim Phillips 		     void (*callback) (struct device *dev,
10359c4a7965SKim Phillips 				       struct talitos_desc *desc,
10369c4a7965SKim Phillips 				       void *context, int error))
10379c4a7965SKim Phillips {
10389c4a7965SKim Phillips 	struct crypto_aead *aead = crypto_aead_reqtfm(areq);
10399c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(aead);
10409c4a7965SKim Phillips 	struct device *dev = ctx->dev;
10419c4a7965SKim Phillips 	struct talitos_desc *desc = &edesc->desc;
10429c4a7965SKim Phillips 	unsigned int cryptlen = areq->cryptlen;
10439c4a7965SKim Phillips 	unsigned int authsize = ctx->authsize;
1044e41256f1SKim Phillips 	unsigned int ivsize = crypto_aead_ivsize(aead);
1045fa86a267SKim Phillips 	int sg_count, ret;
1046fe5720e2SKim Phillips 	int sg_link_tbl_len;
10479c4a7965SKim Phillips 
10489c4a7965SKim Phillips 	/* hmac key */
10499c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
10509c4a7965SKim Phillips 			       0, DMA_TO_DEVICE);
10519c4a7965SKim Phillips 	/* hmac data */
1052e41256f1SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[1], areq->assoclen + ivsize,
1053e41256f1SKim Phillips 			       sg_virt(areq->assoc), 0, DMA_TO_DEVICE);
10549c4a7965SKim Phillips 	/* cipher iv */
10559c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0,
10569c4a7965SKim Phillips 			       DMA_TO_DEVICE);
10579c4a7965SKim Phillips 
10589c4a7965SKim Phillips 	/* cipher key */
10599c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
10609c4a7965SKim Phillips 			       (char *)&ctx->key + ctx->authkeylen, 0,
10619c4a7965SKim Phillips 			       DMA_TO_DEVICE);
10629c4a7965SKim Phillips 
10639c4a7965SKim Phillips 	/*
10649c4a7965SKim Phillips 	 * cipher in
10659c4a7965SKim Phillips 	 * map and adjust cipher len to aead request cryptlen.
10669c4a7965SKim Phillips 	 * extent is bytes of HMAC postpended to ciphertext,
10679c4a7965SKim Phillips 	 * typically 12 for ipsec
10689c4a7965SKim Phillips 	 */
10699c4a7965SKim Phillips 	desc->ptr[4].len = cpu_to_be16(cryptlen);
10709c4a7965SKim Phillips 	desc->ptr[4].j_extent = authsize;
10719c4a7965SKim Phillips 
1072e938e465SKim Phillips 	sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1073e938e465SKim Phillips 				  (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1074e938e465SKim Phillips 							   : DMA_TO_DEVICE,
10754de9d0b5SLee Nipper 				  edesc->src_is_chained);
10769c4a7965SKim Phillips 
10779c4a7965SKim Phillips 	if (sg_count == 1) {
107881eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src));
10799c4a7965SKim Phillips 	} else {
1080fe5720e2SKim Phillips 		sg_link_tbl_len = cryptlen;
1081fe5720e2SKim Phillips 
1082962a9c99SKim Phillips 		if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
1083fe5720e2SKim Phillips 			sg_link_tbl_len = cryptlen + authsize;
1084e938e465SKim Phillips 
1085fe5720e2SKim Phillips 		sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
10869c4a7965SKim Phillips 					  &edesc->link_tbl[0]);
108770bcaca7SLee Nipper 		if (sg_count > 1) {
10889c4a7965SKim Phillips 			desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
108981eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl);
1090e938e465SKim Phillips 			dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1091e938e465SKim Phillips 						   edesc->dma_len,
1092e938e465SKim Phillips 						   DMA_BIDIRECTIONAL);
109370bcaca7SLee Nipper 		} else {
109470bcaca7SLee Nipper 			/* Only one segment now, so no link tbl needed */
109581eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[4],
109681eb024cSKim Phillips 				       sg_dma_address(areq->src));
109770bcaca7SLee Nipper 		}
10989c4a7965SKim Phillips 	}
10999c4a7965SKim Phillips 
11009c4a7965SKim Phillips 	/* cipher out */
11019c4a7965SKim Phillips 	desc->ptr[5].len = cpu_to_be16(cryptlen);
11029c4a7965SKim Phillips 	desc->ptr[5].j_extent = authsize;
11039c4a7965SKim Phillips 
1104e938e465SKim Phillips 	if (areq->src != areq->dst)
11054de9d0b5SLee Nipper 		sg_count = talitos_map_sg(dev, areq->dst,
11064de9d0b5SLee Nipper 					  edesc->dst_nents ? : 1,
11074de9d0b5SLee Nipper 					  DMA_FROM_DEVICE,
11084de9d0b5SLee Nipper 					  edesc->dst_is_chained);
11099c4a7965SKim Phillips 
11109c4a7965SKim Phillips 	if (sg_count == 1) {
111181eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst));
11129c4a7965SKim Phillips 	} else {
11139c4a7965SKim Phillips 		struct talitos_ptr *link_tbl_ptr =
1114f3c85bc1SLee Nipper 			&edesc->link_tbl[edesc->src_nents + 1];
11159c4a7965SKim Phillips 
111681eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
111781eb024cSKim Phillips 			       (edesc->src_nents + 1) *
111881eb024cSKim Phillips 			       sizeof(struct talitos_ptr));
111970bcaca7SLee Nipper 		sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
11209c4a7965SKim Phillips 					  link_tbl_ptr);
1121fe5720e2SKim Phillips 
1122f3c85bc1SLee Nipper 		/* Add an entry to the link table for ICV data */
11239c4a7965SKim Phillips 		link_tbl_ptr += sg_count - 1;
11249c4a7965SKim Phillips 		link_tbl_ptr->j_extent = 0;
1125f3c85bc1SLee Nipper 		sg_count++;
11269c4a7965SKim Phillips 		link_tbl_ptr++;
11279c4a7965SKim Phillips 		link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
11289c4a7965SKim Phillips 		link_tbl_ptr->len = cpu_to_be16(authsize);
11299c4a7965SKim Phillips 
11309c4a7965SKim Phillips 		/* icv data follows link tables */
113181eb024cSKim Phillips 		to_talitos_ptr(link_tbl_ptr, edesc->dma_link_tbl +
113281eb024cSKim Phillips 			       (edesc->src_nents + edesc->dst_nents + 2) *
113381eb024cSKim Phillips 			       sizeof(struct talitos_ptr));
11349c4a7965SKim Phillips 		desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
11359c4a7965SKim Phillips 		dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
11369c4a7965SKim Phillips 					   edesc->dma_len, DMA_BIDIRECTIONAL);
11379c4a7965SKim Phillips 	}
11389c4a7965SKim Phillips 
11399c4a7965SKim Phillips 	/* iv out */
11409c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
11419c4a7965SKim Phillips 			       DMA_FROM_DEVICE);
11429c4a7965SKim Phillips 
11435228f0f7SKim Phillips 	ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
1144fa86a267SKim Phillips 	if (ret != -EINPROGRESS) {
1145fa86a267SKim Phillips 		ipsec_esp_unmap(dev, edesc, areq);
1146fa86a267SKim Phillips 		kfree(edesc);
1147fa86a267SKim Phillips 	}
1148fa86a267SKim Phillips 	return ret;
11499c4a7965SKim Phillips }
11509c4a7965SKim Phillips 
11519c4a7965SKim Phillips /*
11529c4a7965SKim Phillips  * derive number of elements in scatterlist
11539c4a7965SKim Phillips  */
11544de9d0b5SLee Nipper static int sg_count(struct scatterlist *sg_list, int nbytes, int *chained)
11559c4a7965SKim Phillips {
11569c4a7965SKim Phillips 	struct scatterlist *sg = sg_list;
11579c4a7965SKim Phillips 	int sg_nents = 0;
11589c4a7965SKim Phillips 
11594de9d0b5SLee Nipper 	*chained = 0;
11604de9d0b5SLee Nipper 	while (nbytes > 0) {
11619c4a7965SKim Phillips 		sg_nents++;
11629c4a7965SKim Phillips 		nbytes -= sg->length;
11634de9d0b5SLee Nipper 		if (!sg_is_last(sg) && (sg + 1)->length == 0)
11644de9d0b5SLee Nipper 			*chained = 1;
11654de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
11669c4a7965SKim Phillips 	}
11679c4a7965SKim Phillips 
11689c4a7965SKim Phillips 	return sg_nents;
11699c4a7965SKim Phillips }
11709c4a7965SKim Phillips 
1171497f2e6bSLee Nipper /**
1172497f2e6bSLee Nipper  * sg_copy_end_to_buffer - Copy end data from SG list to a linear buffer
1173497f2e6bSLee Nipper  * @sgl:		 The SG list
1174497f2e6bSLee Nipper  * @nents:		 Number of SG entries
1175497f2e6bSLee Nipper  * @buf:		 Where to copy to
1176497f2e6bSLee Nipper  * @buflen:		 The number of bytes to copy
1177497f2e6bSLee Nipper  * @skip:		 The number of bytes to skip before copying.
1178497f2e6bSLee Nipper  *                       Note: skip + buflen should equal SG total size.
1179497f2e6bSLee Nipper  *
1180497f2e6bSLee Nipper  * Returns the number of copied bytes.
1181497f2e6bSLee Nipper  *
1182497f2e6bSLee Nipper  **/
1183497f2e6bSLee Nipper static size_t sg_copy_end_to_buffer(struct scatterlist *sgl, unsigned int nents,
1184497f2e6bSLee Nipper 				    void *buf, size_t buflen, unsigned int skip)
1185497f2e6bSLee Nipper {
1186497f2e6bSLee Nipper 	unsigned int offset = 0;
1187497f2e6bSLee Nipper 	unsigned int boffset = 0;
1188497f2e6bSLee Nipper 	struct sg_mapping_iter miter;
1189497f2e6bSLee Nipper 	unsigned long flags;
1190497f2e6bSLee Nipper 	unsigned int sg_flags = SG_MITER_ATOMIC;
1191497f2e6bSLee Nipper 	size_t total_buffer = buflen + skip;
1192497f2e6bSLee Nipper 
1193497f2e6bSLee Nipper 	sg_flags |= SG_MITER_FROM_SG;
1194497f2e6bSLee Nipper 
1195497f2e6bSLee Nipper 	sg_miter_start(&miter, sgl, nents, sg_flags);
1196497f2e6bSLee Nipper 
1197497f2e6bSLee Nipper 	local_irq_save(flags);
1198497f2e6bSLee Nipper 
1199497f2e6bSLee Nipper 	while (sg_miter_next(&miter) && offset < total_buffer) {
1200497f2e6bSLee Nipper 		unsigned int len;
1201497f2e6bSLee Nipper 		unsigned int ignore;
1202497f2e6bSLee Nipper 
1203497f2e6bSLee Nipper 		if ((offset + miter.length) > skip) {
1204497f2e6bSLee Nipper 			if (offset < skip) {
1205497f2e6bSLee Nipper 				/* Copy part of this segment */
1206497f2e6bSLee Nipper 				ignore = skip - offset;
1207497f2e6bSLee Nipper 				len = miter.length - ignore;
12087260042bSLee Nipper 				if (boffset + len > buflen)
12097260042bSLee Nipper 					len = buflen - boffset;
1210497f2e6bSLee Nipper 				memcpy(buf + boffset, miter.addr + ignore, len);
1211497f2e6bSLee Nipper 			} else {
12127260042bSLee Nipper 				/* Copy all of this segment (up to buflen) */
1213497f2e6bSLee Nipper 				len = miter.length;
12147260042bSLee Nipper 				if (boffset + len > buflen)
12157260042bSLee Nipper 					len = buflen - boffset;
1216497f2e6bSLee Nipper 				memcpy(buf + boffset, miter.addr, len);
1217497f2e6bSLee Nipper 			}
1218497f2e6bSLee Nipper 			boffset += len;
1219497f2e6bSLee Nipper 		}
1220497f2e6bSLee Nipper 		offset += miter.length;
1221497f2e6bSLee Nipper 	}
1222497f2e6bSLee Nipper 
1223497f2e6bSLee Nipper 	sg_miter_stop(&miter);
1224497f2e6bSLee Nipper 
1225497f2e6bSLee Nipper 	local_irq_restore(flags);
1226497f2e6bSLee Nipper 	return boffset;
1227497f2e6bSLee Nipper }
1228497f2e6bSLee Nipper 
12299c4a7965SKim Phillips /*
123056af8cd4SLee Nipper  * allocate and map the extended descriptor
12319c4a7965SKim Phillips  */
12324de9d0b5SLee Nipper static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
12334de9d0b5SLee Nipper 						 struct scatterlist *src,
12344de9d0b5SLee Nipper 						 struct scatterlist *dst,
1235497f2e6bSLee Nipper 						 int hash_result,
12364de9d0b5SLee Nipper 						 unsigned int cryptlen,
12374de9d0b5SLee Nipper 						 unsigned int authsize,
12384de9d0b5SLee Nipper 						 int icv_stashing,
12394de9d0b5SLee Nipper 						 u32 cryptoflags)
12409c4a7965SKim Phillips {
124156af8cd4SLee Nipper 	struct talitos_edesc *edesc;
12429c4a7965SKim Phillips 	int src_nents, dst_nents, alloc_len, dma_len;
12434de9d0b5SLee Nipper 	int src_chained, dst_chained = 0;
12444de9d0b5SLee Nipper 	gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
1245586725f8SKim Phillips 		      GFP_ATOMIC;
12469c4a7965SKim Phillips 
12474de9d0b5SLee Nipper 	if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
12484de9d0b5SLee Nipper 		dev_err(dev, "length exceeds h/w max limit\n");
12499c4a7965SKim Phillips 		return ERR_PTR(-EINVAL);
12509c4a7965SKim Phillips 	}
12519c4a7965SKim Phillips 
12524de9d0b5SLee Nipper 	src_nents = sg_count(src, cryptlen + authsize, &src_chained);
12539c4a7965SKim Phillips 	src_nents = (src_nents == 1) ? 0 : src_nents;
12549c4a7965SKim Phillips 
1255497f2e6bSLee Nipper 	if (hash_result) {
1256497f2e6bSLee Nipper 		dst_nents = 0;
1257497f2e6bSLee Nipper 	} else {
12584de9d0b5SLee Nipper 		if (dst == src) {
12599c4a7965SKim Phillips 			dst_nents = src_nents;
12609c4a7965SKim Phillips 		} else {
1261497f2e6bSLee Nipper 			dst_nents = sg_count(dst, cryptlen + authsize,
1262497f2e6bSLee Nipper 					     &dst_chained);
1263695ad589SLee Nipper 			dst_nents = (dst_nents == 1) ? 0 : dst_nents;
12649c4a7965SKim Phillips 		}
1265497f2e6bSLee Nipper 	}
12669c4a7965SKim Phillips 
12679c4a7965SKim Phillips 	/*
12689c4a7965SKim Phillips 	 * allocate space for base edesc plus the link tables,
1269f3c85bc1SLee Nipper 	 * allowing for two separate entries for ICV and generated ICV (+ 2),
12709c4a7965SKim Phillips 	 * and the ICV data itself
12719c4a7965SKim Phillips 	 */
127256af8cd4SLee Nipper 	alloc_len = sizeof(struct talitos_edesc);
12739c4a7965SKim Phillips 	if (src_nents || dst_nents) {
1274f3c85bc1SLee Nipper 		dma_len = (src_nents + dst_nents + 2) *
12754de9d0b5SLee Nipper 				 sizeof(struct talitos_ptr) + authsize;
12769c4a7965SKim Phillips 		alloc_len += dma_len;
12779c4a7965SKim Phillips 	} else {
12789c4a7965SKim Phillips 		dma_len = 0;
12794de9d0b5SLee Nipper 		alloc_len += icv_stashing ? authsize : 0;
12809c4a7965SKim Phillips 	}
12819c4a7965SKim Phillips 
1282586725f8SKim Phillips 	edesc = kmalloc(alloc_len, GFP_DMA | flags);
12839c4a7965SKim Phillips 	if (!edesc) {
12844de9d0b5SLee Nipper 		dev_err(dev, "could not allocate edescriptor\n");
12859c4a7965SKim Phillips 		return ERR_PTR(-ENOMEM);
12869c4a7965SKim Phillips 	}
12879c4a7965SKim Phillips 
12889c4a7965SKim Phillips 	edesc->src_nents = src_nents;
12899c4a7965SKim Phillips 	edesc->dst_nents = dst_nents;
12904de9d0b5SLee Nipper 	edesc->src_is_chained = src_chained;
12914de9d0b5SLee Nipper 	edesc->dst_is_chained = dst_chained;
12929c4a7965SKim Phillips 	edesc->dma_len = dma_len;
1293497f2e6bSLee Nipper 	if (dma_len)
12944de9d0b5SLee Nipper 		edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1295497f2e6bSLee Nipper 						     edesc->dma_len,
1296497f2e6bSLee Nipper 						     DMA_BIDIRECTIONAL);
12979c4a7965SKim Phillips 
12989c4a7965SKim Phillips 	return edesc;
12999c4a7965SKim Phillips }
13009c4a7965SKim Phillips 
13014de9d0b5SLee Nipper static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq,
13024de9d0b5SLee Nipper 					      int icv_stashing)
13034de9d0b5SLee Nipper {
13044de9d0b5SLee Nipper 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
13054de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
13064de9d0b5SLee Nipper 
1307497f2e6bSLee Nipper 	return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
13084de9d0b5SLee Nipper 				   areq->cryptlen, ctx->authsize, icv_stashing,
13094de9d0b5SLee Nipper 				   areq->base.flags);
13104de9d0b5SLee Nipper }
13114de9d0b5SLee Nipper 
131256af8cd4SLee Nipper static int aead_encrypt(struct aead_request *req)
13139c4a7965SKim Phillips {
13149c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
13159c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
131656af8cd4SLee Nipper 	struct talitos_edesc *edesc;
13179c4a7965SKim Phillips 
13189c4a7965SKim Phillips 	/* allocate extended descriptor */
13194de9d0b5SLee Nipper 	edesc = aead_edesc_alloc(req, 0);
13209c4a7965SKim Phillips 	if (IS_ERR(edesc))
13219c4a7965SKim Phillips 		return PTR_ERR(edesc);
13229c4a7965SKim Phillips 
13239c4a7965SKim Phillips 	/* set encrypt */
132470bcaca7SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
13259c4a7965SKim Phillips 
13269c4a7965SKim Phillips 	return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done);
13279c4a7965SKim Phillips }
13289c4a7965SKim Phillips 
132956af8cd4SLee Nipper static int aead_decrypt(struct aead_request *req)
13309c4a7965SKim Phillips {
13319c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
13329c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
13339c4a7965SKim Phillips 	unsigned int authsize = ctx->authsize;
1334fe5720e2SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(ctx->dev);
133556af8cd4SLee Nipper 	struct talitos_edesc *edesc;
13369c4a7965SKim Phillips 	struct scatterlist *sg;
13379c4a7965SKim Phillips 	void *icvdata;
13389c4a7965SKim Phillips 
13399c4a7965SKim Phillips 	req->cryptlen -= authsize;
13409c4a7965SKim Phillips 
13419c4a7965SKim Phillips 	/* allocate extended descriptor */
13424de9d0b5SLee Nipper 	edesc = aead_edesc_alloc(req, 1);
13439c4a7965SKim Phillips 	if (IS_ERR(edesc))
13449c4a7965SKim Phillips 		return PTR_ERR(edesc);
13459c4a7965SKim Phillips 
1346fe5720e2SKim Phillips 	if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
1347e938e465SKim Phillips 	    ((!edesc->src_nents && !edesc->dst_nents) ||
1348e938e465SKim Phillips 	     priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
1349fe5720e2SKim Phillips 
1350fe5720e2SKim Phillips 		/* decrypt and check the ICV */
1351e938e465SKim Phillips 		edesc->desc.hdr = ctx->desc_hdr_template |
1352e938e465SKim Phillips 				  DESC_HDR_DIR_INBOUND |
1353fe5720e2SKim Phillips 				  DESC_HDR_MODE1_MDEU_CICV;
1354fe5720e2SKim Phillips 
1355fe5720e2SKim Phillips 		/* reset integrity check result bits */
1356fe5720e2SKim Phillips 		edesc->desc.hdr_lo = 0;
1357fe5720e2SKim Phillips 
1358e938e465SKim Phillips 		return ipsec_esp(edesc, req, NULL, 0,
1359e938e465SKim Phillips 				 ipsec_esp_decrypt_hwauth_done);
1360fe5720e2SKim Phillips 
1361e938e465SKim Phillips 	}
1362fe5720e2SKim Phillips 
1363fe5720e2SKim Phillips 	/* Have to check the ICV with software */
1364fe5720e2SKim Phillips 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1365fe5720e2SKim Phillips 
13669c4a7965SKim Phillips 	/* stash incoming ICV for later cmp with ICV generated by the h/w */
13679c4a7965SKim Phillips 	if (edesc->dma_len)
13689c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[edesc->src_nents +
1369f3c85bc1SLee Nipper 					   edesc->dst_nents + 2];
13709c4a7965SKim Phillips 	else
13719c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[0];
13729c4a7965SKim Phillips 
13739c4a7965SKim Phillips 	sg = sg_last(req->src, edesc->src_nents ? : 1);
13749c4a7965SKim Phillips 
13759c4a7965SKim Phillips 	memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
13769c4a7965SKim Phillips 	       ctx->authsize);
13779c4a7965SKim Phillips 
1378fe5720e2SKim Phillips 	return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_swauth_done);
1379fe5720e2SKim Phillips }
13809c4a7965SKim Phillips 
138156af8cd4SLee Nipper static int aead_givencrypt(struct aead_givcrypt_request *req)
13829c4a7965SKim Phillips {
13839c4a7965SKim Phillips 	struct aead_request *areq = &req->areq;
13849c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
13859c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
138656af8cd4SLee Nipper 	struct talitos_edesc *edesc;
13879c4a7965SKim Phillips 
13889c4a7965SKim Phillips 	/* allocate extended descriptor */
13894de9d0b5SLee Nipper 	edesc = aead_edesc_alloc(areq, 0);
13909c4a7965SKim Phillips 	if (IS_ERR(edesc))
13919c4a7965SKim Phillips 		return PTR_ERR(edesc);
13929c4a7965SKim Phillips 
13939c4a7965SKim Phillips 	/* set encrypt */
139470bcaca7SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
13959c4a7965SKim Phillips 
13969c4a7965SKim Phillips 	memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
1397ba95487dSKim Phillips 	/* avoid consecutive packets going out with same IV */
1398ba95487dSKim Phillips 	*(__be64 *)req->giv ^= cpu_to_be64(req->seq);
13999c4a7965SKim Phillips 
14009c4a7965SKim Phillips 	return ipsec_esp(edesc, areq, req->giv, req->seq,
14019c4a7965SKim Phillips 			 ipsec_esp_encrypt_done);
14029c4a7965SKim Phillips }
14039c4a7965SKim Phillips 
14044de9d0b5SLee Nipper static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
14054de9d0b5SLee Nipper 			     const u8 *key, unsigned int keylen)
14064de9d0b5SLee Nipper {
14074de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
14084de9d0b5SLee Nipper 
14094de9d0b5SLee Nipper 	memcpy(&ctx->key, key, keylen);
14104de9d0b5SLee Nipper 	ctx->keylen = keylen;
14114de9d0b5SLee Nipper 
14124de9d0b5SLee Nipper 	return 0;
14134de9d0b5SLee Nipper }
14144de9d0b5SLee Nipper 
14154de9d0b5SLee Nipper static void common_nonsnoop_unmap(struct device *dev,
14164de9d0b5SLee Nipper 				  struct talitos_edesc *edesc,
14174de9d0b5SLee Nipper 				  struct ablkcipher_request *areq)
14184de9d0b5SLee Nipper {
14194de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
14204de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
14214de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
14224de9d0b5SLee Nipper 
14234de9d0b5SLee Nipper 	talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
14244de9d0b5SLee Nipper 
14254de9d0b5SLee Nipper 	if (edesc->dma_len)
14264de9d0b5SLee Nipper 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
14274de9d0b5SLee Nipper 				 DMA_BIDIRECTIONAL);
14284de9d0b5SLee Nipper }
14294de9d0b5SLee Nipper 
14304de9d0b5SLee Nipper static void ablkcipher_done(struct device *dev,
14314de9d0b5SLee Nipper 			    struct talitos_desc *desc, void *context,
14324de9d0b5SLee Nipper 			    int err)
14334de9d0b5SLee Nipper {
14344de9d0b5SLee Nipper 	struct ablkcipher_request *areq = context;
143519bbbc63SKim Phillips 	struct talitos_edesc *edesc;
143619bbbc63SKim Phillips 
143719bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
14384de9d0b5SLee Nipper 
14394de9d0b5SLee Nipper 	common_nonsnoop_unmap(dev, edesc, areq);
14404de9d0b5SLee Nipper 
14414de9d0b5SLee Nipper 	kfree(edesc);
14424de9d0b5SLee Nipper 
14434de9d0b5SLee Nipper 	areq->base.complete(&areq->base, err);
14444de9d0b5SLee Nipper }
14454de9d0b5SLee Nipper 
14464de9d0b5SLee Nipper static int common_nonsnoop(struct talitos_edesc *edesc,
14474de9d0b5SLee Nipper 			   struct ablkcipher_request *areq,
14484de9d0b5SLee Nipper 			   void (*callback) (struct device *dev,
14494de9d0b5SLee Nipper 					     struct talitos_desc *desc,
14504de9d0b5SLee Nipper 					     void *context, int error))
14514de9d0b5SLee Nipper {
14524de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
14534de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
14544de9d0b5SLee Nipper 	struct device *dev = ctx->dev;
14554de9d0b5SLee Nipper 	struct talitos_desc *desc = &edesc->desc;
14564de9d0b5SLee Nipper 	unsigned int cryptlen = areq->nbytes;
14574de9d0b5SLee Nipper 	unsigned int ivsize;
14584de9d0b5SLee Nipper 	int sg_count, ret;
14594de9d0b5SLee Nipper 
14604de9d0b5SLee Nipper 	/* first DWORD empty */
14614de9d0b5SLee Nipper 	desc->ptr[0].len = 0;
146281eb024cSKim Phillips 	to_talitos_ptr(&desc->ptr[0], 0);
14634de9d0b5SLee Nipper 	desc->ptr[0].j_extent = 0;
14644de9d0b5SLee Nipper 
14654de9d0b5SLee Nipper 	/* cipher iv */
14664de9d0b5SLee Nipper 	ivsize = crypto_ablkcipher_ivsize(cipher);
1467febec542SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[1], ivsize, areq->info, 0,
14684de9d0b5SLee Nipper 			       DMA_TO_DEVICE);
14694de9d0b5SLee Nipper 
14704de9d0b5SLee Nipper 	/* cipher key */
14714de9d0b5SLee Nipper 	map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
14724de9d0b5SLee Nipper 			       (char *)&ctx->key, 0, DMA_TO_DEVICE);
14734de9d0b5SLee Nipper 
14744de9d0b5SLee Nipper 	/*
14754de9d0b5SLee Nipper 	 * cipher in
14764de9d0b5SLee Nipper 	 */
14774de9d0b5SLee Nipper 	desc->ptr[3].len = cpu_to_be16(cryptlen);
14784de9d0b5SLee Nipper 	desc->ptr[3].j_extent = 0;
14794de9d0b5SLee Nipper 
14804de9d0b5SLee Nipper 	sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
14814de9d0b5SLee Nipper 				  (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
14824de9d0b5SLee Nipper 							   : DMA_TO_DEVICE,
14834de9d0b5SLee Nipper 				  edesc->src_is_chained);
14844de9d0b5SLee Nipper 
14854de9d0b5SLee Nipper 	if (sg_count == 1) {
148681eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[3], sg_dma_address(areq->src));
14874de9d0b5SLee Nipper 	} else {
14884de9d0b5SLee Nipper 		sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
14894de9d0b5SLee Nipper 					  &edesc->link_tbl[0]);
14904de9d0b5SLee Nipper 		if (sg_count > 1) {
149181eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
14924de9d0b5SLee Nipper 			desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1493e938e465SKim Phillips 			dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1494e938e465SKim Phillips 						   edesc->dma_len,
1495e938e465SKim Phillips 						   DMA_BIDIRECTIONAL);
14964de9d0b5SLee Nipper 		} else {
14974de9d0b5SLee Nipper 			/* Only one segment now, so no link tbl needed */
149881eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[3],
149981eb024cSKim Phillips 				       sg_dma_address(areq->src));
15004de9d0b5SLee Nipper 		}
15014de9d0b5SLee Nipper 	}
15024de9d0b5SLee Nipper 
15034de9d0b5SLee Nipper 	/* cipher out */
15044de9d0b5SLee Nipper 	desc->ptr[4].len = cpu_to_be16(cryptlen);
15054de9d0b5SLee Nipper 	desc->ptr[4].j_extent = 0;
15064de9d0b5SLee Nipper 
15074de9d0b5SLee Nipper 	if (areq->src != areq->dst)
15084de9d0b5SLee Nipper 		sg_count = talitos_map_sg(dev, areq->dst,
15094de9d0b5SLee Nipper 					  edesc->dst_nents ? : 1,
15104de9d0b5SLee Nipper 					  DMA_FROM_DEVICE,
15114de9d0b5SLee Nipper 					  edesc->dst_is_chained);
15124de9d0b5SLee Nipper 
15134de9d0b5SLee Nipper 	if (sg_count == 1) {
151481eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->dst));
15154de9d0b5SLee Nipper 	} else {
15164de9d0b5SLee Nipper 		struct talitos_ptr *link_tbl_ptr =
15174de9d0b5SLee Nipper 			&edesc->link_tbl[edesc->src_nents + 1];
15184de9d0b5SLee Nipper 
151981eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl +
152081eb024cSKim Phillips 					      (edesc->src_nents + 1) *
152181eb024cSKim Phillips 					      sizeof(struct talitos_ptr));
15224de9d0b5SLee Nipper 		desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
15234de9d0b5SLee Nipper 		sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
15244de9d0b5SLee Nipper 					  link_tbl_ptr);
15254de9d0b5SLee Nipper 		dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
15264de9d0b5SLee Nipper 					   edesc->dma_len, DMA_BIDIRECTIONAL);
15274de9d0b5SLee Nipper 	}
15284de9d0b5SLee Nipper 
15294de9d0b5SLee Nipper 	/* iv out */
15304de9d0b5SLee Nipper 	map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0,
15314de9d0b5SLee Nipper 			       DMA_FROM_DEVICE);
15324de9d0b5SLee Nipper 
15334de9d0b5SLee Nipper 	/* last DWORD empty */
15344de9d0b5SLee Nipper 	desc->ptr[6].len = 0;
153581eb024cSKim Phillips 	to_talitos_ptr(&desc->ptr[6], 0);
15364de9d0b5SLee Nipper 	desc->ptr[6].j_extent = 0;
15374de9d0b5SLee Nipper 
15385228f0f7SKim Phillips 	ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
15394de9d0b5SLee Nipper 	if (ret != -EINPROGRESS) {
15404de9d0b5SLee Nipper 		common_nonsnoop_unmap(dev, edesc, areq);
15414de9d0b5SLee Nipper 		kfree(edesc);
15424de9d0b5SLee Nipper 	}
15434de9d0b5SLee Nipper 	return ret;
15444de9d0b5SLee Nipper }
15454de9d0b5SLee Nipper 
1546e938e465SKim Phillips static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
1547e938e465SKim Phillips 						    areq)
15484de9d0b5SLee Nipper {
15494de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
15504de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
15514de9d0b5SLee Nipper 
1552497f2e6bSLee Nipper 	return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
1553497f2e6bSLee Nipper 				   areq->nbytes, 0, 0, areq->base.flags);
15544de9d0b5SLee Nipper }
15554de9d0b5SLee Nipper 
15564de9d0b5SLee Nipper static int ablkcipher_encrypt(struct ablkcipher_request *areq)
15574de9d0b5SLee Nipper {
15584de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
15594de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
15604de9d0b5SLee Nipper 	struct talitos_edesc *edesc;
15614de9d0b5SLee Nipper 
15624de9d0b5SLee Nipper 	/* allocate extended descriptor */
15634de9d0b5SLee Nipper 	edesc = ablkcipher_edesc_alloc(areq);
15644de9d0b5SLee Nipper 	if (IS_ERR(edesc))
15654de9d0b5SLee Nipper 		return PTR_ERR(edesc);
15664de9d0b5SLee Nipper 
15674de9d0b5SLee Nipper 	/* set encrypt */
15684de9d0b5SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
15694de9d0b5SLee Nipper 
1570febec542SKim Phillips 	return common_nonsnoop(edesc, areq, ablkcipher_done);
15714de9d0b5SLee Nipper }
15724de9d0b5SLee Nipper 
15734de9d0b5SLee Nipper static int ablkcipher_decrypt(struct ablkcipher_request *areq)
15744de9d0b5SLee Nipper {
15754de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
15764de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
15774de9d0b5SLee Nipper 	struct talitos_edesc *edesc;
15784de9d0b5SLee Nipper 
15794de9d0b5SLee Nipper 	/* allocate extended descriptor */
15804de9d0b5SLee Nipper 	edesc = ablkcipher_edesc_alloc(areq);
15814de9d0b5SLee Nipper 	if (IS_ERR(edesc))
15824de9d0b5SLee Nipper 		return PTR_ERR(edesc);
15834de9d0b5SLee Nipper 
15844de9d0b5SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
15854de9d0b5SLee Nipper 
1586febec542SKim Phillips 	return common_nonsnoop(edesc, areq, ablkcipher_done);
15874de9d0b5SLee Nipper }
15884de9d0b5SLee Nipper 
1589497f2e6bSLee Nipper static void common_nonsnoop_hash_unmap(struct device *dev,
1590497f2e6bSLee Nipper 				       struct talitos_edesc *edesc,
1591497f2e6bSLee Nipper 				       struct ahash_request *areq)
1592497f2e6bSLee Nipper {
1593497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1594497f2e6bSLee Nipper 
1595497f2e6bSLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1596497f2e6bSLee Nipper 
1597497f2e6bSLee Nipper 	/* When using hashctx-in, must unmap it. */
1598497f2e6bSLee Nipper 	if (edesc->desc.ptr[1].len)
1599497f2e6bSLee Nipper 		unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1600497f2e6bSLee Nipper 					 DMA_TO_DEVICE);
1601497f2e6bSLee Nipper 
1602497f2e6bSLee Nipper 	if (edesc->desc.ptr[2].len)
1603497f2e6bSLee Nipper 		unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
1604497f2e6bSLee Nipper 					 DMA_TO_DEVICE);
1605497f2e6bSLee Nipper 
1606497f2e6bSLee Nipper 	talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL);
1607497f2e6bSLee Nipper 
1608497f2e6bSLee Nipper 	if (edesc->dma_len)
1609497f2e6bSLee Nipper 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1610497f2e6bSLee Nipper 				 DMA_BIDIRECTIONAL);
1611497f2e6bSLee Nipper 
1612497f2e6bSLee Nipper }
1613497f2e6bSLee Nipper 
1614497f2e6bSLee Nipper static void ahash_done(struct device *dev,
1615497f2e6bSLee Nipper 		       struct talitos_desc *desc, void *context,
1616497f2e6bSLee Nipper 		       int err)
1617497f2e6bSLee Nipper {
1618497f2e6bSLee Nipper 	struct ahash_request *areq = context;
1619497f2e6bSLee Nipper 	struct talitos_edesc *edesc =
1620497f2e6bSLee Nipper 		 container_of(desc, struct talitos_edesc, desc);
1621497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1622497f2e6bSLee Nipper 
1623497f2e6bSLee Nipper 	if (!req_ctx->last && req_ctx->to_hash_later) {
1624497f2e6bSLee Nipper 		/* Position any partial block for next update/final/finup */
1625497f2e6bSLee Nipper 		memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
16265e833bc4SLee Nipper 		req_ctx->nbuf = req_ctx->to_hash_later;
1627497f2e6bSLee Nipper 	}
1628497f2e6bSLee Nipper 	common_nonsnoop_hash_unmap(dev, edesc, areq);
1629497f2e6bSLee Nipper 
1630497f2e6bSLee Nipper 	kfree(edesc);
1631497f2e6bSLee Nipper 
1632497f2e6bSLee Nipper 	areq->base.complete(&areq->base, err);
1633497f2e6bSLee Nipper }
1634497f2e6bSLee Nipper 
1635497f2e6bSLee Nipper static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1636497f2e6bSLee Nipper 				struct ahash_request *areq, unsigned int length,
1637497f2e6bSLee Nipper 				void (*callback) (struct device *dev,
1638497f2e6bSLee Nipper 						  struct talitos_desc *desc,
1639497f2e6bSLee Nipper 						  void *context, int error))
1640497f2e6bSLee Nipper {
1641497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1642497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1643497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1644497f2e6bSLee Nipper 	struct device *dev = ctx->dev;
1645497f2e6bSLee Nipper 	struct talitos_desc *desc = &edesc->desc;
1646497f2e6bSLee Nipper 	int sg_count, ret;
1647497f2e6bSLee Nipper 
1648497f2e6bSLee Nipper 	/* first DWORD empty */
1649497f2e6bSLee Nipper 	desc->ptr[0] = zero_entry;
1650497f2e6bSLee Nipper 
165160f208d7SKim Phillips 	/* hash context in */
165260f208d7SKim Phillips 	if (!req_ctx->first || req_ctx->swinit) {
1653497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[1],
1654497f2e6bSLee Nipper 				       req_ctx->hw_context_size,
1655497f2e6bSLee Nipper 				       (char *)req_ctx->hw_context, 0,
1656497f2e6bSLee Nipper 				       DMA_TO_DEVICE);
165760f208d7SKim Phillips 		req_ctx->swinit = 0;
1658497f2e6bSLee Nipper 	} else {
1659497f2e6bSLee Nipper 		desc->ptr[1] = zero_entry;
1660497f2e6bSLee Nipper 		/* Indicate next op is not the first. */
1661497f2e6bSLee Nipper 		req_ctx->first = 0;
1662497f2e6bSLee Nipper 	}
1663497f2e6bSLee Nipper 
1664497f2e6bSLee Nipper 	/* HMAC key */
1665497f2e6bSLee Nipper 	if (ctx->keylen)
1666497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1667497f2e6bSLee Nipper 				       (char *)&ctx->key, 0, DMA_TO_DEVICE);
1668497f2e6bSLee Nipper 	else
1669497f2e6bSLee Nipper 		desc->ptr[2] = zero_entry;
1670497f2e6bSLee Nipper 
1671497f2e6bSLee Nipper 	/*
1672497f2e6bSLee Nipper 	 * data in
1673497f2e6bSLee Nipper 	 */
1674497f2e6bSLee Nipper 	desc->ptr[3].len = cpu_to_be16(length);
1675497f2e6bSLee Nipper 	desc->ptr[3].j_extent = 0;
1676497f2e6bSLee Nipper 
1677497f2e6bSLee Nipper 	sg_count = talitos_map_sg(dev, req_ctx->psrc,
1678497f2e6bSLee Nipper 				  edesc->src_nents ? : 1,
1679497f2e6bSLee Nipper 				  DMA_TO_DEVICE,
1680497f2e6bSLee Nipper 				  edesc->src_is_chained);
1681497f2e6bSLee Nipper 
1682497f2e6bSLee Nipper 	if (sg_count == 1) {
1683497f2e6bSLee Nipper 		to_talitos_ptr(&desc->ptr[3], sg_dma_address(req_ctx->psrc));
1684497f2e6bSLee Nipper 	} else {
1685497f2e6bSLee Nipper 		sg_count = sg_to_link_tbl(req_ctx->psrc, sg_count, length,
1686497f2e6bSLee Nipper 					  &edesc->link_tbl[0]);
1687497f2e6bSLee Nipper 		if (sg_count > 1) {
1688497f2e6bSLee Nipper 			desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1689497f2e6bSLee Nipper 			to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
1690497f2e6bSLee Nipper 			dma_sync_single_for_device(ctx->dev,
1691497f2e6bSLee Nipper 						   edesc->dma_link_tbl,
1692497f2e6bSLee Nipper 						   edesc->dma_len,
1693497f2e6bSLee Nipper 						   DMA_BIDIRECTIONAL);
1694497f2e6bSLee Nipper 		} else {
1695497f2e6bSLee Nipper 			/* Only one segment now, so no link tbl needed */
1696497f2e6bSLee Nipper 			to_talitos_ptr(&desc->ptr[3],
1697497f2e6bSLee Nipper 				       sg_dma_address(req_ctx->psrc));
1698497f2e6bSLee Nipper 		}
1699497f2e6bSLee Nipper 	}
1700497f2e6bSLee Nipper 
1701497f2e6bSLee Nipper 	/* fifth DWORD empty */
1702497f2e6bSLee Nipper 	desc->ptr[4] = zero_entry;
1703497f2e6bSLee Nipper 
1704497f2e6bSLee Nipper 	/* hash/HMAC out -or- hash context out */
1705497f2e6bSLee Nipper 	if (req_ctx->last)
1706497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[5],
1707497f2e6bSLee Nipper 				       crypto_ahash_digestsize(tfm),
1708497f2e6bSLee Nipper 				       areq->result, 0, DMA_FROM_DEVICE);
1709497f2e6bSLee Nipper 	else
1710497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[5],
1711497f2e6bSLee Nipper 				       req_ctx->hw_context_size,
1712497f2e6bSLee Nipper 				       req_ctx->hw_context, 0, DMA_FROM_DEVICE);
1713497f2e6bSLee Nipper 
1714497f2e6bSLee Nipper 	/* last DWORD empty */
1715497f2e6bSLee Nipper 	desc->ptr[6] = zero_entry;
1716497f2e6bSLee Nipper 
17175228f0f7SKim Phillips 	ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
1718497f2e6bSLee Nipper 	if (ret != -EINPROGRESS) {
1719497f2e6bSLee Nipper 		common_nonsnoop_hash_unmap(dev, edesc, areq);
1720497f2e6bSLee Nipper 		kfree(edesc);
1721497f2e6bSLee Nipper 	}
1722497f2e6bSLee Nipper 	return ret;
1723497f2e6bSLee Nipper }
1724497f2e6bSLee Nipper 
1725497f2e6bSLee Nipper static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1726497f2e6bSLee Nipper 					       unsigned int nbytes)
1727497f2e6bSLee Nipper {
1728497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1729497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1730497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1731497f2e6bSLee Nipper 
1732497f2e6bSLee Nipper 	return talitos_edesc_alloc(ctx->dev, req_ctx->psrc, NULL, 1,
1733497f2e6bSLee Nipper 				   nbytes, 0, 0, areq->base.flags);
1734497f2e6bSLee Nipper }
1735497f2e6bSLee Nipper 
1736497f2e6bSLee Nipper static int ahash_init(struct ahash_request *areq)
1737497f2e6bSLee Nipper {
1738497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1739497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1740497f2e6bSLee Nipper 
1741497f2e6bSLee Nipper 	/* Initialize the context */
17425e833bc4SLee Nipper 	req_ctx->nbuf = 0;
174360f208d7SKim Phillips 	req_ctx->first = 1; /* first indicates h/w must init its context */
174460f208d7SKim Phillips 	req_ctx->swinit = 0; /* assume h/w init of context */
1745497f2e6bSLee Nipper 	req_ctx->hw_context_size =
1746497f2e6bSLee Nipper 		(crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1747497f2e6bSLee Nipper 			? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1748497f2e6bSLee Nipper 			: TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1749497f2e6bSLee Nipper 
1750497f2e6bSLee Nipper 	return 0;
1751497f2e6bSLee Nipper }
1752497f2e6bSLee Nipper 
175360f208d7SKim Phillips /*
175460f208d7SKim Phillips  * on h/w without explicit sha224 support, we initialize h/w context
175560f208d7SKim Phillips  * manually with sha224 constants, and tell it to run sha256.
175660f208d7SKim Phillips  */
175760f208d7SKim Phillips static int ahash_init_sha224_swinit(struct ahash_request *areq)
175860f208d7SKim Phillips {
175960f208d7SKim Phillips 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
176060f208d7SKim Phillips 
176160f208d7SKim Phillips 	ahash_init(areq);
176260f208d7SKim Phillips 	req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
176360f208d7SKim Phillips 
1764a752447aSKim Phillips 	req_ctx->hw_context[0] = SHA224_H0;
1765a752447aSKim Phillips 	req_ctx->hw_context[1] = SHA224_H1;
1766a752447aSKim Phillips 	req_ctx->hw_context[2] = SHA224_H2;
1767a752447aSKim Phillips 	req_ctx->hw_context[3] = SHA224_H3;
1768a752447aSKim Phillips 	req_ctx->hw_context[4] = SHA224_H4;
1769a752447aSKim Phillips 	req_ctx->hw_context[5] = SHA224_H5;
1770a752447aSKim Phillips 	req_ctx->hw_context[6] = SHA224_H6;
1771a752447aSKim Phillips 	req_ctx->hw_context[7] = SHA224_H7;
177260f208d7SKim Phillips 
177360f208d7SKim Phillips 	/* init 64-bit count */
177460f208d7SKim Phillips 	req_ctx->hw_context[8] = 0;
177560f208d7SKim Phillips 	req_ctx->hw_context[9] = 0;
177660f208d7SKim Phillips 
177760f208d7SKim Phillips 	return 0;
177860f208d7SKim Phillips }
177960f208d7SKim Phillips 
1780497f2e6bSLee Nipper static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1781497f2e6bSLee Nipper {
1782497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1783497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1784497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1785497f2e6bSLee Nipper 	struct talitos_edesc *edesc;
1786497f2e6bSLee Nipper 	unsigned int blocksize =
1787497f2e6bSLee Nipper 			crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1788497f2e6bSLee Nipper 	unsigned int nbytes_to_hash;
1789497f2e6bSLee Nipper 	unsigned int to_hash_later;
17905e833bc4SLee Nipper 	unsigned int nsg;
1791497f2e6bSLee Nipper 	int chained;
1792497f2e6bSLee Nipper 
17935e833bc4SLee Nipper 	if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
17945e833bc4SLee Nipper 		/* Buffer up to one whole block */
1795497f2e6bSLee Nipper 		sg_copy_to_buffer(areq->src,
1796497f2e6bSLee Nipper 				  sg_count(areq->src, nbytes, &chained),
17975e833bc4SLee Nipper 				  req_ctx->buf + req_ctx->nbuf, nbytes);
17985e833bc4SLee Nipper 		req_ctx->nbuf += nbytes;
1799497f2e6bSLee Nipper 		return 0;
1800497f2e6bSLee Nipper 	}
1801497f2e6bSLee Nipper 
18025e833bc4SLee Nipper 	/* At least (blocksize + 1) bytes are available to hash */
18035e833bc4SLee Nipper 	nbytes_to_hash = nbytes + req_ctx->nbuf;
18045e833bc4SLee Nipper 	to_hash_later = nbytes_to_hash & (blocksize - 1);
18055e833bc4SLee Nipper 
18065e833bc4SLee Nipper 	if (req_ctx->last)
18075e833bc4SLee Nipper 		to_hash_later = 0;
18085e833bc4SLee Nipper 	else if (to_hash_later)
18095e833bc4SLee Nipper 		/* There is a partial block. Hash the full block(s) now */
18105e833bc4SLee Nipper 		nbytes_to_hash -= to_hash_later;
18115e833bc4SLee Nipper 	else {
18125e833bc4SLee Nipper 		/* Keep one block buffered */
18135e833bc4SLee Nipper 		nbytes_to_hash -= blocksize;
18145e833bc4SLee Nipper 		to_hash_later = blocksize;
1815497f2e6bSLee Nipper 	}
18165e833bc4SLee Nipper 
18175e833bc4SLee Nipper 	/* Chain in any previously buffered data */
18185e833bc4SLee Nipper 	if (req_ctx->nbuf) {
18195e833bc4SLee Nipper 		nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
18205e833bc4SLee Nipper 		sg_init_table(req_ctx->bufsl, nsg);
18215e833bc4SLee Nipper 		sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
18225e833bc4SLee Nipper 		if (nsg > 1)
18235e833bc4SLee Nipper 			scatterwalk_sg_chain(req_ctx->bufsl, 2, areq->src);
18245e833bc4SLee Nipper 		req_ctx->psrc = req_ctx->bufsl;
18255e833bc4SLee Nipper 	} else
18265e833bc4SLee Nipper 		req_ctx->psrc = areq->src;
18275e833bc4SLee Nipper 
1828497f2e6bSLee Nipper 	if (to_hash_later) {
18295e833bc4SLee Nipper 		int nents = sg_count(areq->src, nbytes, &chained);
1830497f2e6bSLee Nipper 		sg_copy_end_to_buffer(areq->src, nents,
1831497f2e6bSLee Nipper 				      req_ctx->bufnext,
1832497f2e6bSLee Nipper 				      to_hash_later,
1833497f2e6bSLee Nipper 				      nbytes - to_hash_later);
1834497f2e6bSLee Nipper 	}
1835497f2e6bSLee Nipper 	req_ctx->to_hash_later = to_hash_later;
1836497f2e6bSLee Nipper 
18375e833bc4SLee Nipper 	/* Allocate extended descriptor */
1838497f2e6bSLee Nipper 	edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
1839497f2e6bSLee Nipper 	if (IS_ERR(edesc))
1840497f2e6bSLee Nipper 		return PTR_ERR(edesc);
1841497f2e6bSLee Nipper 
1842497f2e6bSLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template;
1843497f2e6bSLee Nipper 
1844497f2e6bSLee Nipper 	/* On last one, request SEC to pad; otherwise continue */
1845497f2e6bSLee Nipper 	if (req_ctx->last)
1846497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
1847497f2e6bSLee Nipper 	else
1848497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
1849497f2e6bSLee Nipper 
185060f208d7SKim Phillips 	/* request SEC to INIT hash. */
185160f208d7SKim Phillips 	if (req_ctx->first && !req_ctx->swinit)
1852497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
1853497f2e6bSLee Nipper 
1854497f2e6bSLee Nipper 	/* When the tfm context has a keylen, it's an HMAC.
1855497f2e6bSLee Nipper 	 * A first or last (ie. not middle) descriptor must request HMAC.
1856497f2e6bSLee Nipper 	 */
1857497f2e6bSLee Nipper 	if (ctx->keylen && (req_ctx->first || req_ctx->last))
1858497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
1859497f2e6bSLee Nipper 
1860497f2e6bSLee Nipper 	return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
1861497f2e6bSLee Nipper 				    ahash_done);
1862497f2e6bSLee Nipper }
1863497f2e6bSLee Nipper 
1864497f2e6bSLee Nipper static int ahash_update(struct ahash_request *areq)
1865497f2e6bSLee Nipper {
1866497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1867497f2e6bSLee Nipper 
1868497f2e6bSLee Nipper 	req_ctx->last = 0;
1869497f2e6bSLee Nipper 
1870497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1871497f2e6bSLee Nipper }
1872497f2e6bSLee Nipper 
1873497f2e6bSLee Nipper static int ahash_final(struct ahash_request *areq)
1874497f2e6bSLee Nipper {
1875497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1876497f2e6bSLee Nipper 
1877497f2e6bSLee Nipper 	req_ctx->last = 1;
1878497f2e6bSLee Nipper 
1879497f2e6bSLee Nipper 	return ahash_process_req(areq, 0);
1880497f2e6bSLee Nipper }
1881497f2e6bSLee Nipper 
1882497f2e6bSLee Nipper static int ahash_finup(struct ahash_request *areq)
1883497f2e6bSLee Nipper {
1884497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1885497f2e6bSLee Nipper 
1886497f2e6bSLee Nipper 	req_ctx->last = 1;
1887497f2e6bSLee Nipper 
1888497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1889497f2e6bSLee Nipper }
1890497f2e6bSLee Nipper 
1891497f2e6bSLee Nipper static int ahash_digest(struct ahash_request *areq)
1892497f2e6bSLee Nipper {
1893497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
189460f208d7SKim Phillips 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
1895497f2e6bSLee Nipper 
189660f208d7SKim Phillips 	ahash->init(areq);
1897497f2e6bSLee Nipper 	req_ctx->last = 1;
1898497f2e6bSLee Nipper 
1899497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1900497f2e6bSLee Nipper }
1901497f2e6bSLee Nipper 
190279b3a418SLee Nipper struct keyhash_result {
190379b3a418SLee Nipper 	struct completion completion;
190479b3a418SLee Nipper 	int err;
190579b3a418SLee Nipper };
190679b3a418SLee Nipper 
190779b3a418SLee Nipper static void keyhash_complete(struct crypto_async_request *req, int err)
190879b3a418SLee Nipper {
190979b3a418SLee Nipper 	struct keyhash_result *res = req->data;
191079b3a418SLee Nipper 
191179b3a418SLee Nipper 	if (err == -EINPROGRESS)
191279b3a418SLee Nipper 		return;
191379b3a418SLee Nipper 
191479b3a418SLee Nipper 	res->err = err;
191579b3a418SLee Nipper 	complete(&res->completion);
191679b3a418SLee Nipper }
191779b3a418SLee Nipper 
191879b3a418SLee Nipper static int keyhash(struct crypto_ahash *tfm, const u8 *key, unsigned int keylen,
191979b3a418SLee Nipper 		   u8 *hash)
192079b3a418SLee Nipper {
192179b3a418SLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
192279b3a418SLee Nipper 
192379b3a418SLee Nipper 	struct scatterlist sg[1];
192479b3a418SLee Nipper 	struct ahash_request *req;
192579b3a418SLee Nipper 	struct keyhash_result hresult;
192679b3a418SLee Nipper 	int ret;
192779b3a418SLee Nipper 
192879b3a418SLee Nipper 	init_completion(&hresult.completion);
192979b3a418SLee Nipper 
193079b3a418SLee Nipper 	req = ahash_request_alloc(tfm, GFP_KERNEL);
193179b3a418SLee Nipper 	if (!req)
193279b3a418SLee Nipper 		return -ENOMEM;
193379b3a418SLee Nipper 
193479b3a418SLee Nipper 	/* Keep tfm keylen == 0 during hash of the long key */
193579b3a418SLee Nipper 	ctx->keylen = 0;
193679b3a418SLee Nipper 	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
193779b3a418SLee Nipper 				   keyhash_complete, &hresult);
193879b3a418SLee Nipper 
193979b3a418SLee Nipper 	sg_init_one(&sg[0], key, keylen);
194079b3a418SLee Nipper 
194179b3a418SLee Nipper 	ahash_request_set_crypt(req, sg, hash, keylen);
194279b3a418SLee Nipper 	ret = crypto_ahash_digest(req);
194379b3a418SLee Nipper 	switch (ret) {
194479b3a418SLee Nipper 	case 0:
194579b3a418SLee Nipper 		break;
194679b3a418SLee Nipper 	case -EINPROGRESS:
194779b3a418SLee Nipper 	case -EBUSY:
194879b3a418SLee Nipper 		ret = wait_for_completion_interruptible(
194979b3a418SLee Nipper 			&hresult.completion);
195079b3a418SLee Nipper 		if (!ret)
195179b3a418SLee Nipper 			ret = hresult.err;
195279b3a418SLee Nipper 		break;
195379b3a418SLee Nipper 	default:
195479b3a418SLee Nipper 		break;
195579b3a418SLee Nipper 	}
195679b3a418SLee Nipper 	ahash_request_free(req);
195779b3a418SLee Nipper 
195879b3a418SLee Nipper 	return ret;
195979b3a418SLee Nipper }
196079b3a418SLee Nipper 
196179b3a418SLee Nipper static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
196279b3a418SLee Nipper 			unsigned int keylen)
196379b3a418SLee Nipper {
196479b3a418SLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
196579b3a418SLee Nipper 	unsigned int blocksize =
196679b3a418SLee Nipper 			crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
196779b3a418SLee Nipper 	unsigned int digestsize = crypto_ahash_digestsize(tfm);
196879b3a418SLee Nipper 	unsigned int keysize = keylen;
196979b3a418SLee Nipper 	u8 hash[SHA512_DIGEST_SIZE];
197079b3a418SLee Nipper 	int ret;
197179b3a418SLee Nipper 
197279b3a418SLee Nipper 	if (keylen <= blocksize)
197379b3a418SLee Nipper 		memcpy(ctx->key, key, keysize);
197479b3a418SLee Nipper 	else {
197579b3a418SLee Nipper 		/* Must get the hash of the long key */
197679b3a418SLee Nipper 		ret = keyhash(tfm, key, keylen, hash);
197779b3a418SLee Nipper 
197879b3a418SLee Nipper 		if (ret) {
197979b3a418SLee Nipper 			crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
198079b3a418SLee Nipper 			return -EINVAL;
198179b3a418SLee Nipper 		}
198279b3a418SLee Nipper 
198379b3a418SLee Nipper 		keysize = digestsize;
198479b3a418SLee Nipper 		memcpy(ctx->key, hash, digestsize);
198579b3a418SLee Nipper 	}
198679b3a418SLee Nipper 
198779b3a418SLee Nipper 	ctx->keylen = keysize;
198879b3a418SLee Nipper 
198979b3a418SLee Nipper 	return 0;
199079b3a418SLee Nipper }
199179b3a418SLee Nipper 
199279b3a418SLee Nipper 
19939c4a7965SKim Phillips struct talitos_alg_template {
1994d5e4aaefSLee Nipper 	u32 type;
1995d5e4aaefSLee Nipper 	union {
1996d5e4aaefSLee Nipper 		struct crypto_alg crypto;
1997acbf7c62SLee Nipper 		struct ahash_alg hash;
1998d5e4aaefSLee Nipper 	} alg;
19999c4a7965SKim Phillips 	__be32 desc_hdr_template;
20009c4a7965SKim Phillips };
20019c4a7965SKim Phillips 
20029c4a7965SKim Phillips static struct talitos_alg_template driver_algs[] = {
200356af8cd4SLee Nipper 	/* AEAD algorithms.  These use a single-pass ipsec_esp descriptor */
2004d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2005d5e4aaefSLee Nipper 		.alg.crypto = {
200656af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha1),cbc(aes))",
200756af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
200856af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
200956af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
201056af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
201156af8cd4SLee Nipper 			.cra_aead = {
201256af8cd4SLee Nipper 				.setkey = aead_setkey,
201356af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
201456af8cd4SLee Nipper 				.encrypt = aead_encrypt,
201556af8cd4SLee Nipper 				.decrypt = aead_decrypt,
201656af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
20179c4a7965SKim Phillips 				.geniv = "<built-in>",
20183952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
20193952f17eSLee Nipper 				.maxauthsize = SHA1_DIGEST_SIZE,
202056af8cd4SLee Nipper 			}
20219c4a7965SKim Phillips 		},
20229c4a7965SKim Phillips 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
20239c4a7965SKim Phillips 			             DESC_HDR_SEL0_AESU |
20249c4a7965SKim Phillips 		                     DESC_HDR_MODE0_AESU_CBC |
20259c4a7965SKim Phillips 		                     DESC_HDR_SEL1_MDEUA |
20269c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_INIT |
20279c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_PAD |
20289c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_SHA1_HMAC,
202970bcaca7SLee Nipper 	},
2030d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2031d5e4aaefSLee Nipper 		.alg.crypto = {
203256af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
203356af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
203456af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
203556af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
203656af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
203756af8cd4SLee Nipper 			.cra_aead = {
203856af8cd4SLee Nipper 				.setkey = aead_setkey,
203956af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
204056af8cd4SLee Nipper 				.encrypt = aead_encrypt,
204156af8cd4SLee Nipper 				.decrypt = aead_decrypt,
204256af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
204370bcaca7SLee Nipper 				.geniv = "<built-in>",
20443952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
20453952f17eSLee Nipper 				.maxauthsize = SHA1_DIGEST_SIZE,
204656af8cd4SLee Nipper 			}
204770bcaca7SLee Nipper 		},
204870bcaca7SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
204970bcaca7SLee Nipper 			             DESC_HDR_SEL0_DEU |
205070bcaca7SLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
205170bcaca7SLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
205270bcaca7SLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
205370bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
205470bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
205570bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA1_HMAC,
20563952f17eSLee Nipper 	},
2057d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2058d5e4aaefSLee Nipper 		.alg.crypto = {
205956af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha256),cbc(aes))",
206056af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
206156af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
206256af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
206356af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
206456af8cd4SLee Nipper 			.cra_aead = {
206556af8cd4SLee Nipper 				.setkey = aead_setkey,
206656af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
206756af8cd4SLee Nipper 				.encrypt = aead_encrypt,
206856af8cd4SLee Nipper 				.decrypt = aead_decrypt,
206956af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
20703952f17eSLee Nipper 				.geniv = "<built-in>",
20713952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
20723952f17eSLee Nipper 				.maxauthsize = SHA256_DIGEST_SIZE,
207356af8cd4SLee Nipper 			}
20743952f17eSLee Nipper 		},
20753952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
20763952f17eSLee Nipper 			             DESC_HDR_SEL0_AESU |
20773952f17eSLee Nipper 		                     DESC_HDR_MODE0_AESU_CBC |
20783952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
20793952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
20803952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
20813952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA256_HMAC,
20823952f17eSLee Nipper 	},
2083d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2084d5e4aaefSLee Nipper 		.alg.crypto = {
208556af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
208656af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
208756af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
208856af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
208956af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
209056af8cd4SLee Nipper 			.cra_aead = {
209156af8cd4SLee Nipper 				.setkey = aead_setkey,
209256af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
209356af8cd4SLee Nipper 				.encrypt = aead_encrypt,
209456af8cd4SLee Nipper 				.decrypt = aead_decrypt,
209556af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
20963952f17eSLee Nipper 				.geniv = "<built-in>",
20973952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
20983952f17eSLee Nipper 				.maxauthsize = SHA256_DIGEST_SIZE,
209956af8cd4SLee Nipper 			}
21003952f17eSLee Nipper 		},
21013952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
21023952f17eSLee Nipper 			             DESC_HDR_SEL0_DEU |
21033952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
21043952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
21053952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
21063952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
21073952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
21083952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA256_HMAC,
21093952f17eSLee Nipper 	},
2110d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2111d5e4aaefSLee Nipper 		.alg.crypto = {
211256af8cd4SLee Nipper 			.cra_name = "authenc(hmac(md5),cbc(aes))",
211356af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
211456af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
211556af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
211656af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
211756af8cd4SLee Nipper 			.cra_aead = {
211856af8cd4SLee Nipper 				.setkey = aead_setkey,
211956af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
212056af8cd4SLee Nipper 				.encrypt = aead_encrypt,
212156af8cd4SLee Nipper 				.decrypt = aead_decrypt,
212256af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
21233952f17eSLee Nipper 				.geniv = "<built-in>",
21243952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
21253952f17eSLee Nipper 				.maxauthsize = MD5_DIGEST_SIZE,
212656af8cd4SLee Nipper 			}
21273952f17eSLee Nipper 		},
21283952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
21293952f17eSLee Nipper 			             DESC_HDR_SEL0_AESU |
21303952f17eSLee Nipper 		                     DESC_HDR_MODE0_AESU_CBC |
21313952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
21323952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
21333952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
21343952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_MD5_HMAC,
21353952f17eSLee Nipper 	},
2136d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2137d5e4aaefSLee Nipper 		.alg.crypto = {
213856af8cd4SLee Nipper 			.cra_name = "authenc(hmac(md5),cbc(des3_ede))",
213956af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
214056af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
214156af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
214256af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
214356af8cd4SLee Nipper 			.cra_aead = {
214456af8cd4SLee Nipper 				.setkey = aead_setkey,
214556af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
214656af8cd4SLee Nipper 				.encrypt = aead_encrypt,
214756af8cd4SLee Nipper 				.decrypt = aead_decrypt,
214856af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
21493952f17eSLee Nipper 				.geniv = "<built-in>",
21503952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
21513952f17eSLee Nipper 				.maxauthsize = MD5_DIGEST_SIZE,
215256af8cd4SLee Nipper 			}
21533952f17eSLee Nipper 		},
21543952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
21553952f17eSLee Nipper 			             DESC_HDR_SEL0_DEU |
21563952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
21573952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
21583952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
21593952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
21603952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
21613952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_MD5_HMAC,
21624de9d0b5SLee Nipper 	},
21634de9d0b5SLee Nipper 	/* ABLKCIPHER algorithms. */
2164d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2165d5e4aaefSLee Nipper 		.alg.crypto = {
21664de9d0b5SLee Nipper 			.cra_name = "cbc(aes)",
21674de9d0b5SLee Nipper 			.cra_driver_name = "cbc-aes-talitos",
21684de9d0b5SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
21694de9d0b5SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
21704de9d0b5SLee Nipper                                      CRYPTO_ALG_ASYNC,
21714de9d0b5SLee Nipper 			.cra_type = &crypto_ablkcipher_type,
21724de9d0b5SLee Nipper 			.cra_ablkcipher = {
21734de9d0b5SLee Nipper 				.setkey = ablkcipher_setkey,
21744de9d0b5SLee Nipper 				.encrypt = ablkcipher_encrypt,
21754de9d0b5SLee Nipper 				.decrypt = ablkcipher_decrypt,
21764de9d0b5SLee Nipper 				.geniv = "eseqiv",
21774de9d0b5SLee Nipper 				.min_keysize = AES_MIN_KEY_SIZE,
21784de9d0b5SLee Nipper 				.max_keysize = AES_MAX_KEY_SIZE,
21794de9d0b5SLee Nipper 				.ivsize = AES_BLOCK_SIZE,
21804de9d0b5SLee Nipper 			}
21814de9d0b5SLee Nipper 		},
21824de9d0b5SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
21834de9d0b5SLee Nipper 				     DESC_HDR_SEL0_AESU |
21844de9d0b5SLee Nipper 				     DESC_HDR_MODE0_AESU_CBC,
21854de9d0b5SLee Nipper 	},
2186d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2187d5e4aaefSLee Nipper 		.alg.crypto = {
21884de9d0b5SLee Nipper 			.cra_name = "cbc(des3_ede)",
21894de9d0b5SLee Nipper 			.cra_driver_name = "cbc-3des-talitos",
21904de9d0b5SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
21914de9d0b5SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
21924de9d0b5SLee Nipper                                      CRYPTO_ALG_ASYNC,
21934de9d0b5SLee Nipper 			.cra_type = &crypto_ablkcipher_type,
21944de9d0b5SLee Nipper 			.cra_ablkcipher = {
21954de9d0b5SLee Nipper 				.setkey = ablkcipher_setkey,
21964de9d0b5SLee Nipper 				.encrypt = ablkcipher_encrypt,
21974de9d0b5SLee Nipper 				.decrypt = ablkcipher_decrypt,
21984de9d0b5SLee Nipper 				.geniv = "eseqiv",
21994de9d0b5SLee Nipper 				.min_keysize = DES3_EDE_KEY_SIZE,
22004de9d0b5SLee Nipper 				.max_keysize = DES3_EDE_KEY_SIZE,
22014de9d0b5SLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
22024de9d0b5SLee Nipper 			}
22034de9d0b5SLee Nipper 		},
22044de9d0b5SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
22054de9d0b5SLee Nipper 			             DESC_HDR_SEL0_DEU |
22064de9d0b5SLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
22074de9d0b5SLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES,
2208497f2e6bSLee Nipper 	},
2209497f2e6bSLee Nipper 	/* AHASH algorithms. */
2210497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2211497f2e6bSLee Nipper 		.alg.hash = {
2212497f2e6bSLee Nipper 			.init = ahash_init,
2213497f2e6bSLee Nipper 			.update = ahash_update,
2214497f2e6bSLee Nipper 			.final = ahash_final,
2215497f2e6bSLee Nipper 			.finup = ahash_finup,
2216497f2e6bSLee Nipper 			.digest = ahash_digest,
2217497f2e6bSLee Nipper 			.halg.digestsize = MD5_DIGEST_SIZE,
2218497f2e6bSLee Nipper 			.halg.base = {
2219497f2e6bSLee Nipper 				.cra_name = "md5",
2220497f2e6bSLee Nipper 				.cra_driver_name = "md5-talitos",
2221497f2e6bSLee Nipper 				.cra_blocksize = MD5_BLOCK_SIZE,
2222497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2223497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2224497f2e6bSLee Nipper 				.cra_type = &crypto_ahash_type
22259c4a7965SKim Phillips 			}
2226497f2e6bSLee Nipper 		},
2227497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2228497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUA |
2229497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEU_MD5,
2230497f2e6bSLee Nipper 	},
2231497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2232497f2e6bSLee Nipper 		.alg.hash = {
2233497f2e6bSLee Nipper 			.init = ahash_init,
2234497f2e6bSLee Nipper 			.update = ahash_update,
2235497f2e6bSLee Nipper 			.final = ahash_final,
2236497f2e6bSLee Nipper 			.finup = ahash_finup,
2237497f2e6bSLee Nipper 			.digest = ahash_digest,
2238497f2e6bSLee Nipper 			.halg.digestsize = SHA1_DIGEST_SIZE,
2239497f2e6bSLee Nipper 			.halg.base = {
2240497f2e6bSLee Nipper 				.cra_name = "sha1",
2241497f2e6bSLee Nipper 				.cra_driver_name = "sha1-talitos",
2242497f2e6bSLee Nipper 				.cra_blocksize = SHA1_BLOCK_SIZE,
2243497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2244497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2245497f2e6bSLee Nipper 				.cra_type = &crypto_ahash_type
2246497f2e6bSLee Nipper 			}
2247497f2e6bSLee Nipper 		},
2248497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2249497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUA |
2250497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA1,
2251497f2e6bSLee Nipper 	},
2252497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2253497f2e6bSLee Nipper 		.alg.hash = {
2254497f2e6bSLee Nipper 			.init = ahash_init,
2255497f2e6bSLee Nipper 			.update = ahash_update,
2256497f2e6bSLee Nipper 			.final = ahash_final,
2257497f2e6bSLee Nipper 			.finup = ahash_finup,
2258497f2e6bSLee Nipper 			.digest = ahash_digest,
225960f208d7SKim Phillips 			.halg.digestsize = SHA224_DIGEST_SIZE,
226060f208d7SKim Phillips 			.halg.base = {
226160f208d7SKim Phillips 				.cra_name = "sha224",
226260f208d7SKim Phillips 				.cra_driver_name = "sha224-talitos",
226360f208d7SKim Phillips 				.cra_blocksize = SHA224_BLOCK_SIZE,
226460f208d7SKim Phillips 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
226560f208d7SKim Phillips 					     CRYPTO_ALG_ASYNC,
226660f208d7SKim Phillips 				.cra_type = &crypto_ahash_type
226760f208d7SKim Phillips 			}
226860f208d7SKim Phillips 		},
226960f208d7SKim Phillips 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
227060f208d7SKim Phillips 				     DESC_HDR_SEL0_MDEUA |
227160f208d7SKim Phillips 				     DESC_HDR_MODE0_MDEU_SHA224,
227260f208d7SKim Phillips 	},
227360f208d7SKim Phillips 	{	.type = CRYPTO_ALG_TYPE_AHASH,
227460f208d7SKim Phillips 		.alg.hash = {
227560f208d7SKim Phillips 			.init = ahash_init,
227660f208d7SKim Phillips 			.update = ahash_update,
227760f208d7SKim Phillips 			.final = ahash_final,
227860f208d7SKim Phillips 			.finup = ahash_finup,
227960f208d7SKim Phillips 			.digest = ahash_digest,
2280497f2e6bSLee Nipper 			.halg.digestsize = SHA256_DIGEST_SIZE,
2281497f2e6bSLee Nipper 			.halg.base = {
2282497f2e6bSLee Nipper 				.cra_name = "sha256",
2283497f2e6bSLee Nipper 				.cra_driver_name = "sha256-talitos",
2284497f2e6bSLee Nipper 				.cra_blocksize = SHA256_BLOCK_SIZE,
2285497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2286497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2287497f2e6bSLee Nipper 				.cra_type = &crypto_ahash_type
2288497f2e6bSLee Nipper 			}
2289497f2e6bSLee Nipper 		},
2290497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2291497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUA |
2292497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA256,
2293497f2e6bSLee Nipper 	},
2294497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2295497f2e6bSLee Nipper 		.alg.hash = {
2296497f2e6bSLee Nipper 			.init = ahash_init,
2297497f2e6bSLee Nipper 			.update = ahash_update,
2298497f2e6bSLee Nipper 			.final = ahash_final,
2299497f2e6bSLee Nipper 			.finup = ahash_finup,
2300497f2e6bSLee Nipper 			.digest = ahash_digest,
2301497f2e6bSLee Nipper 			.halg.digestsize = SHA384_DIGEST_SIZE,
2302497f2e6bSLee Nipper 			.halg.base = {
2303497f2e6bSLee Nipper 				.cra_name = "sha384",
2304497f2e6bSLee Nipper 				.cra_driver_name = "sha384-talitos",
2305497f2e6bSLee Nipper 				.cra_blocksize = SHA384_BLOCK_SIZE,
2306497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2307497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2308497f2e6bSLee Nipper 				.cra_type = &crypto_ahash_type
2309497f2e6bSLee Nipper 			}
2310497f2e6bSLee Nipper 		},
2311497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2312497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUB |
2313497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA384,
2314497f2e6bSLee Nipper 	},
2315497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2316497f2e6bSLee Nipper 		.alg.hash = {
2317497f2e6bSLee Nipper 			.init = ahash_init,
2318497f2e6bSLee Nipper 			.update = ahash_update,
2319497f2e6bSLee Nipper 			.final = ahash_final,
2320497f2e6bSLee Nipper 			.finup = ahash_finup,
2321497f2e6bSLee Nipper 			.digest = ahash_digest,
2322497f2e6bSLee Nipper 			.halg.digestsize = SHA512_DIGEST_SIZE,
2323497f2e6bSLee Nipper 			.halg.base = {
2324497f2e6bSLee Nipper 				.cra_name = "sha512",
2325497f2e6bSLee Nipper 				.cra_driver_name = "sha512-talitos",
2326497f2e6bSLee Nipper 				.cra_blocksize = SHA512_BLOCK_SIZE,
2327497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2328497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2329497f2e6bSLee Nipper 				.cra_type = &crypto_ahash_type
2330497f2e6bSLee Nipper 			}
2331497f2e6bSLee Nipper 		},
2332497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2333497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUB |
2334497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA512,
2335497f2e6bSLee Nipper 	},
233679b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
233779b3a418SLee Nipper 		.alg.hash = {
233879b3a418SLee Nipper 			.init = ahash_init,
233979b3a418SLee Nipper 			.update = ahash_update,
234079b3a418SLee Nipper 			.final = ahash_final,
234179b3a418SLee Nipper 			.finup = ahash_finup,
234279b3a418SLee Nipper 			.digest = ahash_digest,
234379b3a418SLee Nipper 			.setkey = ahash_setkey,
234479b3a418SLee Nipper 			.halg.digestsize = MD5_DIGEST_SIZE,
234579b3a418SLee Nipper 			.halg.base = {
234679b3a418SLee Nipper 				.cra_name = "hmac(md5)",
234779b3a418SLee Nipper 				.cra_driver_name = "hmac-md5-talitos",
234879b3a418SLee Nipper 				.cra_blocksize = MD5_BLOCK_SIZE,
234979b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
235079b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
235179b3a418SLee Nipper 				.cra_type = &crypto_ahash_type
235279b3a418SLee Nipper 			}
235379b3a418SLee Nipper 		},
235479b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
235579b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUA |
235679b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEU_MD5,
235779b3a418SLee Nipper 	},
235879b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
235979b3a418SLee Nipper 		.alg.hash = {
236079b3a418SLee Nipper 			.init = ahash_init,
236179b3a418SLee Nipper 			.update = ahash_update,
236279b3a418SLee Nipper 			.final = ahash_final,
236379b3a418SLee Nipper 			.finup = ahash_finup,
236479b3a418SLee Nipper 			.digest = ahash_digest,
236579b3a418SLee Nipper 			.setkey = ahash_setkey,
236679b3a418SLee Nipper 			.halg.digestsize = SHA1_DIGEST_SIZE,
236779b3a418SLee Nipper 			.halg.base = {
236879b3a418SLee Nipper 				.cra_name = "hmac(sha1)",
236979b3a418SLee Nipper 				.cra_driver_name = "hmac-sha1-talitos",
237079b3a418SLee Nipper 				.cra_blocksize = SHA1_BLOCK_SIZE,
237179b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
237279b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
237379b3a418SLee Nipper 				.cra_type = &crypto_ahash_type
237479b3a418SLee Nipper 			}
237579b3a418SLee Nipper 		},
237679b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
237779b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUA |
237879b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA1,
237979b3a418SLee Nipper 	},
238079b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
238179b3a418SLee Nipper 		.alg.hash = {
238279b3a418SLee Nipper 			.init = ahash_init,
238379b3a418SLee Nipper 			.update = ahash_update,
238479b3a418SLee Nipper 			.final = ahash_final,
238579b3a418SLee Nipper 			.finup = ahash_finup,
238679b3a418SLee Nipper 			.digest = ahash_digest,
238779b3a418SLee Nipper 			.setkey = ahash_setkey,
238879b3a418SLee Nipper 			.halg.digestsize = SHA224_DIGEST_SIZE,
238979b3a418SLee Nipper 			.halg.base = {
239079b3a418SLee Nipper 				.cra_name = "hmac(sha224)",
239179b3a418SLee Nipper 				.cra_driver_name = "hmac-sha224-talitos",
239279b3a418SLee Nipper 				.cra_blocksize = SHA224_BLOCK_SIZE,
239379b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
239479b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
239579b3a418SLee Nipper 				.cra_type = &crypto_ahash_type
239679b3a418SLee Nipper 			}
239779b3a418SLee Nipper 		},
239879b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
239979b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUA |
240079b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA224,
240179b3a418SLee Nipper 	},
240279b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
240379b3a418SLee Nipper 		.alg.hash = {
240479b3a418SLee Nipper 			.init = ahash_init,
240579b3a418SLee Nipper 			.update = ahash_update,
240679b3a418SLee Nipper 			.final = ahash_final,
240779b3a418SLee Nipper 			.finup = ahash_finup,
240879b3a418SLee Nipper 			.digest = ahash_digest,
240979b3a418SLee Nipper 			.setkey = ahash_setkey,
241079b3a418SLee Nipper 			.halg.digestsize = SHA256_DIGEST_SIZE,
241179b3a418SLee Nipper 			.halg.base = {
241279b3a418SLee Nipper 				.cra_name = "hmac(sha256)",
241379b3a418SLee Nipper 				.cra_driver_name = "hmac-sha256-talitos",
241479b3a418SLee Nipper 				.cra_blocksize = SHA256_BLOCK_SIZE,
241579b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
241679b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
241779b3a418SLee Nipper 				.cra_type = &crypto_ahash_type
241879b3a418SLee Nipper 			}
241979b3a418SLee Nipper 		},
242079b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
242179b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUA |
242279b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA256,
242379b3a418SLee Nipper 	},
242479b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
242579b3a418SLee Nipper 		.alg.hash = {
242679b3a418SLee Nipper 			.init = ahash_init,
242779b3a418SLee Nipper 			.update = ahash_update,
242879b3a418SLee Nipper 			.final = ahash_final,
242979b3a418SLee Nipper 			.finup = ahash_finup,
243079b3a418SLee Nipper 			.digest = ahash_digest,
243179b3a418SLee Nipper 			.setkey = ahash_setkey,
243279b3a418SLee Nipper 			.halg.digestsize = SHA384_DIGEST_SIZE,
243379b3a418SLee Nipper 			.halg.base = {
243479b3a418SLee Nipper 				.cra_name = "hmac(sha384)",
243579b3a418SLee Nipper 				.cra_driver_name = "hmac-sha384-talitos",
243679b3a418SLee Nipper 				.cra_blocksize = SHA384_BLOCK_SIZE,
243779b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
243879b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
243979b3a418SLee Nipper 				.cra_type = &crypto_ahash_type
244079b3a418SLee Nipper 			}
244179b3a418SLee Nipper 		},
244279b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
244379b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUB |
244479b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA384,
244579b3a418SLee Nipper 	},
244679b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
244779b3a418SLee Nipper 		.alg.hash = {
244879b3a418SLee Nipper 			.init = ahash_init,
244979b3a418SLee Nipper 			.update = ahash_update,
245079b3a418SLee Nipper 			.final = ahash_final,
245179b3a418SLee Nipper 			.finup = ahash_finup,
245279b3a418SLee Nipper 			.digest = ahash_digest,
245379b3a418SLee Nipper 			.setkey = ahash_setkey,
245479b3a418SLee Nipper 			.halg.digestsize = SHA512_DIGEST_SIZE,
245579b3a418SLee Nipper 			.halg.base = {
245679b3a418SLee Nipper 				.cra_name = "hmac(sha512)",
245779b3a418SLee Nipper 				.cra_driver_name = "hmac-sha512-talitos",
245879b3a418SLee Nipper 				.cra_blocksize = SHA512_BLOCK_SIZE,
245979b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
246079b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
246179b3a418SLee Nipper 				.cra_type = &crypto_ahash_type
246279b3a418SLee Nipper 			}
246379b3a418SLee Nipper 		},
246479b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
246579b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUB |
246679b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA512,
246779b3a418SLee Nipper 	}
24689c4a7965SKim Phillips };
24699c4a7965SKim Phillips 
24709c4a7965SKim Phillips struct talitos_crypto_alg {
24719c4a7965SKim Phillips 	struct list_head entry;
24729c4a7965SKim Phillips 	struct device *dev;
2473acbf7c62SLee Nipper 	struct talitos_alg_template algt;
24749c4a7965SKim Phillips };
24759c4a7965SKim Phillips 
24769c4a7965SKim Phillips static int talitos_cra_init(struct crypto_tfm *tfm)
24779c4a7965SKim Phillips {
24789c4a7965SKim Phillips 	struct crypto_alg *alg = tfm->__crt_alg;
247919bbbc63SKim Phillips 	struct talitos_crypto_alg *talitos_alg;
24809c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
24815228f0f7SKim Phillips 	struct talitos_private *priv;
24829c4a7965SKim Phillips 
2483497f2e6bSLee Nipper 	if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
2484497f2e6bSLee Nipper 		talitos_alg = container_of(__crypto_ahash_alg(alg),
2485497f2e6bSLee Nipper 					   struct talitos_crypto_alg,
2486497f2e6bSLee Nipper 					   algt.alg.hash);
2487497f2e6bSLee Nipper 	else
2488acbf7c62SLee Nipper 		talitos_alg = container_of(alg, struct talitos_crypto_alg,
2489acbf7c62SLee Nipper 					   algt.alg.crypto);
249019bbbc63SKim Phillips 
24919c4a7965SKim Phillips 	/* update context with ptr to dev */
24929c4a7965SKim Phillips 	ctx->dev = talitos_alg->dev;
249319bbbc63SKim Phillips 
24945228f0f7SKim Phillips 	/* assign SEC channel to tfm in round-robin fashion */
24955228f0f7SKim Phillips 	priv = dev_get_drvdata(ctx->dev);
24965228f0f7SKim Phillips 	ctx->ch = atomic_inc_return(&priv->last_chan) &
24975228f0f7SKim Phillips 		  (priv->num_channels - 1);
24985228f0f7SKim Phillips 
24999c4a7965SKim Phillips 	/* copy descriptor header template value */
2500acbf7c62SLee Nipper 	ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
25019c4a7965SKim Phillips 
2502602dba5aSKim Phillips 	/* select done notification */
2503602dba5aSKim Phillips 	ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY;
2504602dba5aSKim Phillips 
2505497f2e6bSLee Nipper 	return 0;
2506497f2e6bSLee Nipper }
2507497f2e6bSLee Nipper 
2508497f2e6bSLee Nipper static int talitos_cra_init_aead(struct crypto_tfm *tfm)
2509497f2e6bSLee Nipper {
2510497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2511497f2e6bSLee Nipper 
2512497f2e6bSLee Nipper 	talitos_cra_init(tfm);
25139c4a7965SKim Phillips 
25149c4a7965SKim Phillips 	/* random first IV */
251570bcaca7SLee Nipper 	get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
25169c4a7965SKim Phillips 
25179c4a7965SKim Phillips 	return 0;
25189c4a7965SKim Phillips }
25199c4a7965SKim Phillips 
2520497f2e6bSLee Nipper static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
2521497f2e6bSLee Nipper {
2522497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2523497f2e6bSLee Nipper 
2524497f2e6bSLee Nipper 	talitos_cra_init(tfm);
2525497f2e6bSLee Nipper 
2526497f2e6bSLee Nipper 	ctx->keylen = 0;
2527497f2e6bSLee Nipper 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2528497f2e6bSLee Nipper 				 sizeof(struct talitos_ahash_req_ctx));
2529497f2e6bSLee Nipper 
2530497f2e6bSLee Nipper 	return 0;
2531497f2e6bSLee Nipper }
2532497f2e6bSLee Nipper 
25339c4a7965SKim Phillips /*
25349c4a7965SKim Phillips  * given the alg's descriptor header template, determine whether descriptor
25359c4a7965SKim Phillips  * type and primary/secondary execution units required match the hw
25369c4a7965SKim Phillips  * capabilities description provided in the device tree node.
25379c4a7965SKim Phillips  */
25389c4a7965SKim Phillips static int hw_supports(struct device *dev, __be32 desc_hdr_template)
25399c4a7965SKim Phillips {
25409c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
25419c4a7965SKim Phillips 	int ret;
25429c4a7965SKim Phillips 
25439c4a7965SKim Phillips 	ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
25449c4a7965SKim Phillips 	      (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
25459c4a7965SKim Phillips 
25469c4a7965SKim Phillips 	if (SECONDARY_EU(desc_hdr_template))
25479c4a7965SKim Phillips 		ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
25489c4a7965SKim Phillips 		              & priv->exec_units);
25499c4a7965SKim Phillips 
25509c4a7965SKim Phillips 	return ret;
25519c4a7965SKim Phillips }
25529c4a7965SKim Phillips 
25532dc11581SGrant Likely static int talitos_remove(struct platform_device *ofdev)
25549c4a7965SKim Phillips {
25559c4a7965SKim Phillips 	struct device *dev = &ofdev->dev;
25569c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
25579c4a7965SKim Phillips 	struct talitos_crypto_alg *t_alg, *n;
25589c4a7965SKim Phillips 	int i;
25599c4a7965SKim Phillips 
25609c4a7965SKim Phillips 	list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
2561acbf7c62SLee Nipper 		switch (t_alg->algt.type) {
2562acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_ABLKCIPHER:
2563acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_AEAD:
2564acbf7c62SLee Nipper 			crypto_unregister_alg(&t_alg->algt.alg.crypto);
2565acbf7c62SLee Nipper 			break;
2566acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_AHASH:
2567acbf7c62SLee Nipper 			crypto_unregister_ahash(&t_alg->algt.alg.hash);
2568acbf7c62SLee Nipper 			break;
2569acbf7c62SLee Nipper 		}
25709c4a7965SKim Phillips 		list_del(&t_alg->entry);
25719c4a7965SKim Phillips 		kfree(t_alg);
25729c4a7965SKim Phillips 	}
25739c4a7965SKim Phillips 
25749c4a7965SKim Phillips 	if (hw_supports(dev, DESC_HDR_SEL0_RNG))
25759c4a7965SKim Phillips 		talitos_unregister_rng(dev);
25769c4a7965SKim Phillips 
25779c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++)
25784b992628SKim Phillips 		kfree(priv->chan[i].fifo);
25799c4a7965SKim Phillips 
25804b992628SKim Phillips 	kfree(priv->chan);
25819c4a7965SKim Phillips 
2582c3e337f8SKim Phillips 	for (i = 0; i < 2; i++)
25832cdba3cfSKim Phillips 		if (priv->irq[i]) {
2584c3e337f8SKim Phillips 			free_irq(priv->irq[i], dev);
2585c3e337f8SKim Phillips 			irq_dispose_mapping(priv->irq[i]);
25869c4a7965SKim Phillips 		}
25879c4a7965SKim Phillips 
2588c3e337f8SKim Phillips 	tasklet_kill(&priv->done_task[0]);
25892cdba3cfSKim Phillips 	if (priv->irq[1])
2590c3e337f8SKim Phillips 		tasklet_kill(&priv->done_task[1]);
25919c4a7965SKim Phillips 
25929c4a7965SKim Phillips 	iounmap(priv->reg);
25939c4a7965SKim Phillips 
25949c4a7965SKim Phillips 	dev_set_drvdata(dev, NULL);
25959c4a7965SKim Phillips 
25969c4a7965SKim Phillips 	kfree(priv);
25979c4a7965SKim Phillips 
25989c4a7965SKim Phillips 	return 0;
25999c4a7965SKim Phillips }
26009c4a7965SKim Phillips 
26019c4a7965SKim Phillips static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
26029c4a7965SKim Phillips 						    struct talitos_alg_template
26039c4a7965SKim Phillips 						           *template)
26049c4a7965SKim Phillips {
260560f208d7SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
26069c4a7965SKim Phillips 	struct talitos_crypto_alg *t_alg;
26079c4a7965SKim Phillips 	struct crypto_alg *alg;
26089c4a7965SKim Phillips 
26099c4a7965SKim Phillips 	t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
26109c4a7965SKim Phillips 	if (!t_alg)
26119c4a7965SKim Phillips 		return ERR_PTR(-ENOMEM);
26129c4a7965SKim Phillips 
2613acbf7c62SLee Nipper 	t_alg->algt = *template;
2614acbf7c62SLee Nipper 
2615acbf7c62SLee Nipper 	switch (t_alg->algt.type) {
2616acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_ABLKCIPHER:
2617497f2e6bSLee Nipper 		alg = &t_alg->algt.alg.crypto;
2618497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init;
2619497f2e6bSLee Nipper 		break;
2620acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_AEAD:
2621acbf7c62SLee Nipper 		alg = &t_alg->algt.alg.crypto;
2622497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init_aead;
2623acbf7c62SLee Nipper 		break;
2624acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_AHASH:
2625acbf7c62SLee Nipper 		alg = &t_alg->algt.alg.hash.halg.base;
2626497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init_ahash;
262779b3a418SLee Nipper 		if (!(priv->features & TALITOS_FTR_HMAC_OK) &&
26280b2730d8SKim Phillips 		    !strncmp(alg->cra_name, "hmac", 4)) {
26290b2730d8SKim Phillips 			kfree(t_alg);
263079b3a418SLee Nipper 			return ERR_PTR(-ENOTSUPP);
26310b2730d8SKim Phillips 		}
263260f208d7SKim Phillips 		if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
263379b3a418SLee Nipper 		    (!strcmp(alg->cra_name, "sha224") ||
263479b3a418SLee Nipper 		     !strcmp(alg->cra_name, "hmac(sha224)"))) {
263560f208d7SKim Phillips 			t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
263660f208d7SKim Phillips 			t_alg->algt.desc_hdr_template =
263760f208d7SKim Phillips 					DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
263860f208d7SKim Phillips 					DESC_HDR_SEL0_MDEUA |
263960f208d7SKim Phillips 					DESC_HDR_MODE0_MDEU_SHA256;
264060f208d7SKim Phillips 		}
2641497f2e6bSLee Nipper 		break;
26421d11911aSKim Phillips 	default:
26431d11911aSKim Phillips 		dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
26441d11911aSKim Phillips 		return ERR_PTR(-EINVAL);
2645acbf7c62SLee Nipper 	}
26469c4a7965SKim Phillips 
26479c4a7965SKim Phillips 	alg->cra_module = THIS_MODULE;
26489c4a7965SKim Phillips 	alg->cra_priority = TALITOS_CRA_PRIORITY;
26499c4a7965SKim Phillips 	alg->cra_alignmask = 0;
26509c4a7965SKim Phillips 	alg->cra_ctxsize = sizeof(struct talitos_ctx);
26519c4a7965SKim Phillips 
26529c4a7965SKim Phillips 	t_alg->dev = dev;
26539c4a7965SKim Phillips 
26549c4a7965SKim Phillips 	return t_alg;
26559c4a7965SKim Phillips }
26569c4a7965SKim Phillips 
2657c3e337f8SKim Phillips static int talitos_probe_irq(struct platform_device *ofdev)
2658c3e337f8SKim Phillips {
2659c3e337f8SKim Phillips 	struct device *dev = &ofdev->dev;
2660c3e337f8SKim Phillips 	struct device_node *np = ofdev->dev.of_node;
2661c3e337f8SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2662c3e337f8SKim Phillips 	int err;
2663c3e337f8SKim Phillips 
2664c3e337f8SKim Phillips 	priv->irq[0] = irq_of_parse_and_map(np, 0);
26652cdba3cfSKim Phillips 	if (!priv->irq[0]) {
2666c3e337f8SKim Phillips 		dev_err(dev, "failed to map irq\n");
2667c3e337f8SKim Phillips 		return -EINVAL;
2668c3e337f8SKim Phillips 	}
2669c3e337f8SKim Phillips 
2670c3e337f8SKim Phillips 	priv->irq[1] = irq_of_parse_and_map(np, 1);
2671c3e337f8SKim Phillips 
2672c3e337f8SKim Phillips 	/* get the primary irq line */
26732cdba3cfSKim Phillips 	if (!priv->irq[1]) {
2674c3e337f8SKim Phillips 		err = request_irq(priv->irq[0], talitos_interrupt_4ch, 0,
2675c3e337f8SKim Phillips 				  dev_driver_string(dev), dev);
2676c3e337f8SKim Phillips 		goto primary_out;
2677c3e337f8SKim Phillips 	}
2678c3e337f8SKim Phillips 
2679c3e337f8SKim Phillips 	err = request_irq(priv->irq[0], talitos_interrupt_ch0_2, 0,
2680c3e337f8SKim Phillips 			  dev_driver_string(dev), dev);
2681c3e337f8SKim Phillips 	if (err)
2682c3e337f8SKim Phillips 		goto primary_out;
2683c3e337f8SKim Phillips 
2684c3e337f8SKim Phillips 	/* get the secondary irq line */
2685c3e337f8SKim Phillips 	err = request_irq(priv->irq[1], talitos_interrupt_ch1_3, 0,
2686c3e337f8SKim Phillips 			  dev_driver_string(dev), dev);
2687c3e337f8SKim Phillips 	if (err) {
2688c3e337f8SKim Phillips 		dev_err(dev, "failed to request secondary irq\n");
2689c3e337f8SKim Phillips 		irq_dispose_mapping(priv->irq[1]);
26902cdba3cfSKim Phillips 		priv->irq[1] = 0;
2691c3e337f8SKim Phillips 	}
2692c3e337f8SKim Phillips 
2693c3e337f8SKim Phillips 	return err;
2694c3e337f8SKim Phillips 
2695c3e337f8SKim Phillips primary_out:
2696c3e337f8SKim Phillips 	if (err) {
2697c3e337f8SKim Phillips 		dev_err(dev, "failed to request primary irq\n");
2698c3e337f8SKim Phillips 		irq_dispose_mapping(priv->irq[0]);
26992cdba3cfSKim Phillips 		priv->irq[0] = 0;
2700c3e337f8SKim Phillips 	}
2701c3e337f8SKim Phillips 
2702c3e337f8SKim Phillips 	return err;
2703c3e337f8SKim Phillips }
2704c3e337f8SKim Phillips 
27051c48a5c9SGrant Likely static int talitos_probe(struct platform_device *ofdev)
27069c4a7965SKim Phillips {
27079c4a7965SKim Phillips 	struct device *dev = &ofdev->dev;
270861c7a080SGrant Likely 	struct device_node *np = ofdev->dev.of_node;
27099c4a7965SKim Phillips 	struct talitos_private *priv;
27109c4a7965SKim Phillips 	const unsigned int *prop;
27119c4a7965SKim Phillips 	int i, err;
27129c4a7965SKim Phillips 
27139c4a7965SKim Phillips 	priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
27149c4a7965SKim Phillips 	if (!priv)
27159c4a7965SKim Phillips 		return -ENOMEM;
27169c4a7965SKim Phillips 
27179c4a7965SKim Phillips 	dev_set_drvdata(dev, priv);
27189c4a7965SKim Phillips 
27199c4a7965SKim Phillips 	priv->ofdev = ofdev;
27209c4a7965SKim Phillips 
2721c3e337f8SKim Phillips 	err = talitos_probe_irq(ofdev);
2722c3e337f8SKim Phillips 	if (err)
2723c3e337f8SKim Phillips 		goto err_out;
2724c3e337f8SKim Phillips 
27252cdba3cfSKim Phillips 	if (!priv->irq[1]) {
2726c3e337f8SKim Phillips 		tasklet_init(&priv->done_task[0], talitos_done_4ch,
2727c3e337f8SKim Phillips 			     (unsigned long)dev);
2728c3e337f8SKim Phillips 	} else {
2729c3e337f8SKim Phillips 		tasklet_init(&priv->done_task[0], talitos_done_ch0_2,
2730c3e337f8SKim Phillips 			     (unsigned long)dev);
2731c3e337f8SKim Phillips 		tasklet_init(&priv->done_task[1], talitos_done_ch1_3,
2732c3e337f8SKim Phillips 			     (unsigned long)dev);
2733c3e337f8SKim Phillips 	}
27349c4a7965SKim Phillips 
2735fe5720e2SKim Phillips 	INIT_LIST_HEAD(&priv->alg_list);
2736fe5720e2SKim Phillips 
27379c4a7965SKim Phillips 	priv->reg = of_iomap(np, 0);
27389c4a7965SKim Phillips 	if (!priv->reg) {
27399c4a7965SKim Phillips 		dev_err(dev, "failed to of_iomap\n");
27409c4a7965SKim Phillips 		err = -ENOMEM;
27419c4a7965SKim Phillips 		goto err_out;
27429c4a7965SKim Phillips 	}
27439c4a7965SKim Phillips 
27449c4a7965SKim Phillips 	/* get SEC version capabilities from device tree */
27459c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,num-channels", NULL);
27469c4a7965SKim Phillips 	if (prop)
27479c4a7965SKim Phillips 		priv->num_channels = *prop;
27489c4a7965SKim Phillips 
27499c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
27509c4a7965SKim Phillips 	if (prop)
27519c4a7965SKim Phillips 		priv->chfifo_len = *prop;
27529c4a7965SKim Phillips 
27539c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,exec-units-mask", NULL);
27549c4a7965SKim Phillips 	if (prop)
27559c4a7965SKim Phillips 		priv->exec_units = *prop;
27569c4a7965SKim Phillips 
27579c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
27589c4a7965SKim Phillips 	if (prop)
27599c4a7965SKim Phillips 		priv->desc_types = *prop;
27609c4a7965SKim Phillips 
27619c4a7965SKim Phillips 	if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
27629c4a7965SKim Phillips 	    !priv->exec_units || !priv->desc_types) {
27639c4a7965SKim Phillips 		dev_err(dev, "invalid property data in device tree node\n");
27649c4a7965SKim Phillips 		err = -EINVAL;
27659c4a7965SKim Phillips 		goto err_out;
27669c4a7965SKim Phillips 	}
27679c4a7965SKim Phillips 
2768f3c85bc1SLee Nipper 	if (of_device_is_compatible(np, "fsl,sec3.0"))
2769f3c85bc1SLee Nipper 		priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
2770f3c85bc1SLee Nipper 
2771fe5720e2SKim Phillips 	if (of_device_is_compatible(np, "fsl,sec2.1"))
277260f208d7SKim Phillips 		priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
277379b3a418SLee Nipper 				  TALITOS_FTR_SHA224_HWINIT |
277479b3a418SLee Nipper 				  TALITOS_FTR_HMAC_OK;
2775fe5720e2SKim Phillips 
27764b992628SKim Phillips 	priv->chan = kzalloc(sizeof(struct talitos_channel) *
27774b992628SKim Phillips 			     priv->num_channels, GFP_KERNEL);
27784b992628SKim Phillips 	if (!priv->chan) {
27794b992628SKim Phillips 		dev_err(dev, "failed to allocate channel management space\n");
27809c4a7965SKim Phillips 		err = -ENOMEM;
27819c4a7965SKim Phillips 		goto err_out;
27829c4a7965SKim Phillips 	}
27839c4a7965SKim Phillips 
2784c3e337f8SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
2785c3e337f8SKim Phillips 		priv->chan[i].reg = priv->reg + TALITOS_CH_STRIDE * (i + 1);
27862cdba3cfSKim Phillips 		if (!priv->irq[1] || !(i & 1))
2787c3e337f8SKim Phillips 			priv->chan[i].reg += TALITOS_CH_BASE_OFFSET;
2788c3e337f8SKim Phillips 	}
2789ad42d5fcSKim Phillips 
27909c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
27914b992628SKim Phillips 		spin_lock_init(&priv->chan[i].head_lock);
27924b992628SKim Phillips 		spin_lock_init(&priv->chan[i].tail_lock);
27939c4a7965SKim Phillips 	}
27949c4a7965SKim Phillips 
27959c4a7965SKim Phillips 	priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
27969c4a7965SKim Phillips 
27979c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
27984b992628SKim Phillips 		priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
27999c4a7965SKim Phillips 					     priv->fifo_len, GFP_KERNEL);
28004b992628SKim Phillips 		if (!priv->chan[i].fifo) {
28019c4a7965SKim Phillips 			dev_err(dev, "failed to allocate request fifo %d\n", i);
28029c4a7965SKim Phillips 			err = -ENOMEM;
28039c4a7965SKim Phillips 			goto err_out;
28049c4a7965SKim Phillips 		}
28059c4a7965SKim Phillips 	}
28069c4a7965SKim Phillips 
2807ec6644d6SKim Phillips 	for (i = 0; i < priv->num_channels; i++)
28084b992628SKim Phillips 		atomic_set(&priv->chan[i].submit_count,
28094b992628SKim Phillips 			   -(priv->chfifo_len - 1));
28109c4a7965SKim Phillips 
281181eb024cSKim Phillips 	dma_set_mask(dev, DMA_BIT_MASK(36));
281281eb024cSKim Phillips 
28139c4a7965SKim Phillips 	/* reset and initialize the h/w */
28149c4a7965SKim Phillips 	err = init_device(dev);
28159c4a7965SKim Phillips 	if (err) {
28169c4a7965SKim Phillips 		dev_err(dev, "failed to initialize device\n");
28179c4a7965SKim Phillips 		goto err_out;
28189c4a7965SKim Phillips 	}
28199c4a7965SKim Phillips 
28209c4a7965SKim Phillips 	/* register the RNG, if available */
28219c4a7965SKim Phillips 	if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
28229c4a7965SKim Phillips 		err = talitos_register_rng(dev);
28239c4a7965SKim Phillips 		if (err) {
28249c4a7965SKim Phillips 			dev_err(dev, "failed to register hwrng: %d\n", err);
28259c4a7965SKim Phillips 			goto err_out;
28269c4a7965SKim Phillips 		} else
28279c4a7965SKim Phillips 			dev_info(dev, "hwrng\n");
28289c4a7965SKim Phillips 	}
28299c4a7965SKim Phillips 
28309c4a7965SKim Phillips 	/* register crypto algorithms the device supports */
28319c4a7965SKim Phillips 	for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
28329c4a7965SKim Phillips 		if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
28339c4a7965SKim Phillips 			struct talitos_crypto_alg *t_alg;
2834acbf7c62SLee Nipper 			char *name = NULL;
28359c4a7965SKim Phillips 
28369c4a7965SKim Phillips 			t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
28379c4a7965SKim Phillips 			if (IS_ERR(t_alg)) {
28389c4a7965SKim Phillips 				err = PTR_ERR(t_alg);
28390b2730d8SKim Phillips 				if (err == -ENOTSUPP)
284079b3a418SLee Nipper 					continue;
28419c4a7965SKim Phillips 				goto err_out;
28429c4a7965SKim Phillips 			}
28439c4a7965SKim Phillips 
2844acbf7c62SLee Nipper 			switch (t_alg->algt.type) {
2845acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_ABLKCIPHER:
2846acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_AEAD:
2847acbf7c62SLee Nipper 				err = crypto_register_alg(
2848acbf7c62SLee Nipper 						&t_alg->algt.alg.crypto);
2849acbf7c62SLee Nipper 				name = t_alg->algt.alg.crypto.cra_driver_name;
2850acbf7c62SLee Nipper 				break;
2851acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_AHASH:
2852acbf7c62SLee Nipper 				err = crypto_register_ahash(
2853acbf7c62SLee Nipper 						&t_alg->algt.alg.hash);
2854acbf7c62SLee Nipper 				name =
2855acbf7c62SLee Nipper 				 t_alg->algt.alg.hash.halg.base.cra_driver_name;
2856acbf7c62SLee Nipper 				break;
2857acbf7c62SLee Nipper 			}
28589c4a7965SKim Phillips 			if (err) {
28599c4a7965SKim Phillips 				dev_err(dev, "%s alg registration failed\n",
2860acbf7c62SLee Nipper 					name);
28619c4a7965SKim Phillips 				kfree(t_alg);
28625b859b6eSKim Phillips 			} else
28639c4a7965SKim Phillips 				list_add_tail(&t_alg->entry, &priv->alg_list);
28649c4a7965SKim Phillips 		}
28659c4a7965SKim Phillips 	}
28665b859b6eSKim Phillips 	if (!list_empty(&priv->alg_list))
28675b859b6eSKim Phillips 		dev_info(dev, "%s algorithms registered in /proc/crypto\n",
28685b859b6eSKim Phillips 			 (char *)of_get_property(np, "compatible", NULL));
28699c4a7965SKim Phillips 
28709c4a7965SKim Phillips 	return 0;
28719c4a7965SKim Phillips 
28729c4a7965SKim Phillips err_out:
28739c4a7965SKim Phillips 	talitos_remove(ofdev);
28749c4a7965SKim Phillips 
28759c4a7965SKim Phillips 	return err;
28769c4a7965SKim Phillips }
28779c4a7965SKim Phillips 
28786c3f975aSMárton Németh static const struct of_device_id talitos_match[] = {
28799c4a7965SKim Phillips 	{
28809c4a7965SKim Phillips 		.compatible = "fsl,sec2.0",
28819c4a7965SKim Phillips 	},
28829c4a7965SKim Phillips 	{},
28839c4a7965SKim Phillips };
28849c4a7965SKim Phillips MODULE_DEVICE_TABLE(of, talitos_match);
28859c4a7965SKim Phillips 
28861c48a5c9SGrant Likely static struct platform_driver talitos_driver = {
28874018294bSGrant Likely 	.driver = {
28889c4a7965SKim Phillips 		.name = "talitos",
28894018294bSGrant Likely 		.owner = THIS_MODULE,
28904018294bSGrant Likely 		.of_match_table = talitos_match,
28914018294bSGrant Likely 	},
28929c4a7965SKim Phillips 	.probe = talitos_probe,
2893596f1034SAl Viro 	.remove = talitos_remove,
28949c4a7965SKim Phillips };
28959c4a7965SKim Phillips 
2896741e8c2dSAxel Lin module_platform_driver(talitos_driver);
28979c4a7965SKim Phillips 
28989c4a7965SKim Phillips MODULE_LICENSE("GPL");
28999c4a7965SKim Phillips MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
29009c4a7965SKim Phillips MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");
2901