xref: /linux/drivers/crypto/talitos.c (revision 2cdba3cf6ffc1fbf880a6fbfa9e7bb757e3d6526)
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 
237*2cdba3cfSKim 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  */
53740405f10SKim Phillips static void talitos_error(unsigned long data, u32 isr, u32 isr_lo)
5389c4a7965SKim Phillips {
5399c4a7965SKim Phillips 	struct device *dev = (struct device *)data;
5409c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
5419c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
5429c4a7965SKim Phillips 	int ch, error, reset_dev = 0, reset_ch = 0;
54340405f10SKim Phillips 	u32 v, v_lo;
5449c4a7965SKim Phillips 
5459c4a7965SKim Phillips 	for (ch = 0; ch < priv->num_channels; ch++) {
5469c4a7965SKim Phillips 		/* skip channels without errors */
5479c4a7965SKim Phillips 		if (!(isr & (1 << (ch * 2 + 1))))
5489c4a7965SKim Phillips 			continue;
5499c4a7965SKim Phillips 
5509c4a7965SKim Phillips 		error = -EINVAL;
5519c4a7965SKim Phillips 
552ad42d5fcSKim Phillips 		v = in_be32(priv->chan[ch].reg + TALITOS_CCPSR);
553ad42d5fcSKim Phillips 		v_lo = in_be32(priv->chan[ch].reg + TALITOS_CCPSR_LO);
5549c4a7965SKim Phillips 
5559c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_DOF) {
5569c4a7965SKim Phillips 			dev_err(dev, "double fetch fifo overflow error\n");
5579c4a7965SKim Phillips 			error = -EAGAIN;
5589c4a7965SKim Phillips 			reset_ch = 1;
5599c4a7965SKim Phillips 		}
5609c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SOF) {
5619c4a7965SKim Phillips 			/* h/w dropped descriptor */
5629c4a7965SKim Phillips 			dev_err(dev, "single fetch fifo overflow error\n");
5639c4a7965SKim Phillips 			error = -EAGAIN;
5649c4a7965SKim Phillips 		}
5659c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_MDTE)
5669c4a7965SKim Phillips 			dev_err(dev, "master data transfer error\n");
5679c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SGDLZ)
5689c4a7965SKim Phillips 			dev_err(dev, "s/g data length zero error\n");
5699c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_FPZ)
5709c4a7965SKim Phillips 			dev_err(dev, "fetch pointer zero error\n");
5719c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_IDH)
5729c4a7965SKim Phillips 			dev_err(dev, "illegal descriptor header error\n");
5739c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_IEU)
5749c4a7965SKim Phillips 			dev_err(dev, "invalid execution unit error\n");
5759c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_EU)
5763e721aebSKim Phillips 			report_eu_error(dev, ch, current_desc_hdr(dev, ch));
5779c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_GB)
5789c4a7965SKim Phillips 			dev_err(dev, "gather boundary error\n");
5799c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_GRL)
5809c4a7965SKim Phillips 			dev_err(dev, "gather return/length error\n");
5819c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SB)
5829c4a7965SKim Phillips 			dev_err(dev, "scatter boundary error\n");
5839c4a7965SKim Phillips 		if (v_lo & TALITOS_CCPSR_LO_SRL)
5849c4a7965SKim Phillips 			dev_err(dev, "scatter return/length error\n");
5859c4a7965SKim Phillips 
5869c4a7965SKim Phillips 		flush_channel(dev, ch, error, reset_ch);
5879c4a7965SKim Phillips 
5889c4a7965SKim Phillips 		if (reset_ch) {
5899c4a7965SKim Phillips 			reset_channel(dev, ch);
5909c4a7965SKim Phillips 		} else {
591ad42d5fcSKim Phillips 			setbits32(priv->chan[ch].reg + TALITOS_CCCR,
5929c4a7965SKim Phillips 				  TALITOS_CCCR_CONT);
593ad42d5fcSKim Phillips 			setbits32(priv->chan[ch].reg + TALITOS_CCCR_LO, 0);
594ad42d5fcSKim Phillips 			while ((in_be32(priv->chan[ch].reg + TALITOS_CCCR) &
5959c4a7965SKim Phillips 			       TALITOS_CCCR_CONT) && --timeout)
5969c4a7965SKim Phillips 				cpu_relax();
5979c4a7965SKim Phillips 			if (timeout == 0) {
5989c4a7965SKim Phillips 				dev_err(dev, "failed to restart channel %d\n",
5999c4a7965SKim Phillips 					ch);
6009c4a7965SKim Phillips 				reset_dev = 1;
6019c4a7965SKim Phillips 			}
6029c4a7965SKim Phillips 		}
6039c4a7965SKim Phillips 	}
604c3e337f8SKim Phillips 	if (reset_dev || isr & ~TALITOS_ISR_4CHERR || isr_lo) {
6059c4a7965SKim Phillips 		dev_err(dev, "done overflow, internal time out, or rngu error: "
6069c4a7965SKim Phillips 		        "ISR 0x%08x_%08x\n", isr, isr_lo);
6079c4a7965SKim Phillips 
6089c4a7965SKim Phillips 		/* purge request queues */
6099c4a7965SKim Phillips 		for (ch = 0; ch < priv->num_channels; ch++)
6109c4a7965SKim Phillips 			flush_channel(dev, ch, -EIO, 1);
6119c4a7965SKim Phillips 
6129c4a7965SKim Phillips 		/* reset and reinitialize the device */
6139c4a7965SKim Phillips 		init_device(dev);
6149c4a7965SKim Phillips 	}
6159c4a7965SKim Phillips }
6169c4a7965SKim Phillips 
617c3e337f8SKim Phillips #define DEF_TALITOS_INTERRUPT(name, ch_done_mask, ch_err_mask, tlet)	       \
618c3e337f8SKim Phillips static irqreturn_t talitos_interrupt_##name(int irq, void *data)	       \
619c3e337f8SKim Phillips {									       \
620c3e337f8SKim Phillips 	struct device *dev = data;					       \
621c3e337f8SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);		       \
622c3e337f8SKim Phillips 	u32 isr, isr_lo;						       \
623c3e337f8SKim Phillips 									       \
624c3e337f8SKim Phillips 	isr = in_be32(priv->reg + TALITOS_ISR);				       \
625c3e337f8SKim Phillips 	isr_lo = in_be32(priv->reg + TALITOS_ISR_LO);			       \
626c3e337f8SKim Phillips 	/* Acknowledge interrupt */					       \
627c3e337f8SKim Phillips 	out_be32(priv->reg + TALITOS_ICR, isr & (ch_done_mask | ch_err_mask)); \
628c3e337f8SKim Phillips 	out_be32(priv->reg + TALITOS_ICR_LO, isr_lo);			       \
629c3e337f8SKim Phillips 									       \
630c3e337f8SKim Phillips 	if (unlikely((isr & ~TALITOS_ISR_4CHDONE) & ch_err_mask || isr_lo))    \
631c3e337f8SKim Phillips 		talitos_error((unsigned long)data, isr, isr_lo);	       \
632c3e337f8SKim Phillips 	else								       \
633c3e337f8SKim Phillips 		if (likely(isr & ch_done_mask)) {			       \
634c3e337f8SKim Phillips 			/* mask further done interrupts. */		       \
635c3e337f8SKim Phillips 			clrbits32(priv->reg + TALITOS_IMR, ch_done_mask);      \
636c3e337f8SKim Phillips 			/* done_task will unmask done interrupts at exit */    \
637c3e337f8SKim Phillips 			tasklet_schedule(&priv->done_task[tlet]);	       \
638c3e337f8SKim Phillips 		}							       \
639c3e337f8SKim Phillips 									       \
640c3e337f8SKim Phillips 	return (isr & (ch_done_mask | ch_err_mask) || isr_lo) ? IRQ_HANDLED :  \
641c3e337f8SKim Phillips 								IRQ_NONE;      \
6421c2e8811SLee Nipper }
643c3e337f8SKim Phillips DEF_TALITOS_INTERRUPT(4ch, TALITOS_ISR_4CHDONE, TALITOS_ISR_4CHERR, 0)
644c3e337f8SKim Phillips DEF_TALITOS_INTERRUPT(ch0_2, TALITOS_ISR_CH_0_2_DONE, TALITOS_ISR_CH_0_2_ERR, 0)
645c3e337f8SKim Phillips DEF_TALITOS_INTERRUPT(ch1_3, TALITOS_ISR_CH_1_3_DONE, TALITOS_ISR_CH_1_3_ERR, 1)
6469c4a7965SKim Phillips 
6479c4a7965SKim Phillips /*
6489c4a7965SKim Phillips  * hwrng
6499c4a7965SKim Phillips  */
6509c4a7965SKim Phillips static int talitos_rng_data_present(struct hwrng *rng, int wait)
6519c4a7965SKim Phillips {
6529c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
6539c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6549c4a7965SKim Phillips 	u32 ofl;
6559c4a7965SKim Phillips 	int i;
6569c4a7965SKim Phillips 
6579c4a7965SKim Phillips 	for (i = 0; i < 20; i++) {
6589c4a7965SKim Phillips 		ofl = in_be32(priv->reg + TALITOS_RNGUSR_LO) &
6599c4a7965SKim Phillips 		      TALITOS_RNGUSR_LO_OFL;
6609c4a7965SKim Phillips 		if (ofl || !wait)
6619c4a7965SKim Phillips 			break;
6629c4a7965SKim Phillips 		udelay(10);
6639c4a7965SKim Phillips 	}
6649c4a7965SKim Phillips 
6659c4a7965SKim Phillips 	return !!ofl;
6669c4a7965SKim Phillips }
6679c4a7965SKim Phillips 
6689c4a7965SKim Phillips static int talitos_rng_data_read(struct hwrng *rng, u32 *data)
6699c4a7965SKim Phillips {
6709c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
6719c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6729c4a7965SKim Phillips 
6739c4a7965SKim Phillips 	/* rng fifo requires 64-bit accesses */
6749c4a7965SKim Phillips 	*data = in_be32(priv->reg + TALITOS_RNGU_FIFO);
6759c4a7965SKim Phillips 	*data = in_be32(priv->reg + TALITOS_RNGU_FIFO_LO);
6769c4a7965SKim Phillips 
6779c4a7965SKim Phillips 	return sizeof(u32);
6789c4a7965SKim Phillips }
6799c4a7965SKim Phillips 
6809c4a7965SKim Phillips static int talitos_rng_init(struct hwrng *rng)
6819c4a7965SKim Phillips {
6829c4a7965SKim Phillips 	struct device *dev = (struct device *)rng->priv;
6839c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
6849c4a7965SKim Phillips 	unsigned int timeout = TALITOS_TIMEOUT;
6859c4a7965SKim Phillips 
6869c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_RNGURCR_LO, TALITOS_RNGURCR_LO_SR);
6879c4a7965SKim Phillips 	while (!(in_be32(priv->reg + TALITOS_RNGUSR_LO) & TALITOS_RNGUSR_LO_RD)
6889c4a7965SKim Phillips 	       && --timeout)
6899c4a7965SKim Phillips 		cpu_relax();
6909c4a7965SKim Phillips 	if (timeout == 0) {
6919c4a7965SKim Phillips 		dev_err(dev, "failed to reset rng hw\n");
6929c4a7965SKim Phillips 		return -ENODEV;
6939c4a7965SKim Phillips 	}
6949c4a7965SKim Phillips 
6959c4a7965SKim Phillips 	/* start generating */
6969c4a7965SKim Phillips 	setbits32(priv->reg + TALITOS_RNGUDSR_LO, 0);
6979c4a7965SKim Phillips 
6989c4a7965SKim Phillips 	return 0;
6999c4a7965SKim Phillips }
7009c4a7965SKim Phillips 
7019c4a7965SKim Phillips static int talitos_register_rng(struct device *dev)
7029c4a7965SKim Phillips {
7039c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
7049c4a7965SKim Phillips 
7059c4a7965SKim Phillips 	priv->rng.name		= dev_driver_string(dev),
7069c4a7965SKim Phillips 	priv->rng.init		= talitos_rng_init,
7079c4a7965SKim Phillips 	priv->rng.data_present	= talitos_rng_data_present,
7089c4a7965SKim Phillips 	priv->rng.data_read	= talitos_rng_data_read,
7099c4a7965SKim Phillips 	priv->rng.priv		= (unsigned long)dev;
7109c4a7965SKim Phillips 
7119c4a7965SKim Phillips 	return hwrng_register(&priv->rng);
7129c4a7965SKim Phillips }
7139c4a7965SKim Phillips 
7149c4a7965SKim Phillips static void talitos_unregister_rng(struct device *dev)
7159c4a7965SKim Phillips {
7169c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
7179c4a7965SKim Phillips 
7189c4a7965SKim Phillips 	hwrng_unregister(&priv->rng);
7199c4a7965SKim Phillips }
7209c4a7965SKim Phillips 
7219c4a7965SKim Phillips /*
7229c4a7965SKim Phillips  * crypto alg
7239c4a7965SKim Phillips  */
7249c4a7965SKim Phillips #define TALITOS_CRA_PRIORITY		3000
7259c4a7965SKim Phillips #define TALITOS_MAX_KEY_SIZE		64
7263952f17eSLee Nipper #define TALITOS_MAX_IV_LENGTH		16 /* max of AES_BLOCK_SIZE, DES3_EDE_BLOCK_SIZE */
72770bcaca7SLee Nipper 
728497f2e6bSLee Nipper #define MD5_BLOCK_SIZE    64
7299c4a7965SKim Phillips 
7309c4a7965SKim Phillips struct talitos_ctx {
7319c4a7965SKim Phillips 	struct device *dev;
7325228f0f7SKim Phillips 	int ch;
7339c4a7965SKim Phillips 	__be32 desc_hdr_template;
7349c4a7965SKim Phillips 	u8 key[TALITOS_MAX_KEY_SIZE];
73570bcaca7SLee Nipper 	u8 iv[TALITOS_MAX_IV_LENGTH];
7369c4a7965SKim Phillips 	unsigned int keylen;
7379c4a7965SKim Phillips 	unsigned int enckeylen;
7389c4a7965SKim Phillips 	unsigned int authkeylen;
7399c4a7965SKim Phillips 	unsigned int authsize;
7409c4a7965SKim Phillips };
7419c4a7965SKim Phillips 
742497f2e6bSLee Nipper #define HASH_MAX_BLOCK_SIZE		SHA512_BLOCK_SIZE
743497f2e6bSLee Nipper #define TALITOS_MDEU_MAX_CONTEXT_SIZE	TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512
744497f2e6bSLee Nipper 
745497f2e6bSLee Nipper struct talitos_ahash_req_ctx {
74660f208d7SKim Phillips 	u32 hw_context[TALITOS_MDEU_MAX_CONTEXT_SIZE / sizeof(u32)];
747497f2e6bSLee Nipper 	unsigned int hw_context_size;
748497f2e6bSLee Nipper 	u8 buf[HASH_MAX_BLOCK_SIZE];
749497f2e6bSLee Nipper 	u8 bufnext[HASH_MAX_BLOCK_SIZE];
75060f208d7SKim Phillips 	unsigned int swinit;
751497f2e6bSLee Nipper 	unsigned int first;
752497f2e6bSLee Nipper 	unsigned int last;
753497f2e6bSLee Nipper 	unsigned int to_hash_later;
7545e833bc4SLee Nipper 	u64 nbuf;
755497f2e6bSLee Nipper 	struct scatterlist bufsl[2];
756497f2e6bSLee Nipper 	struct scatterlist *psrc;
757497f2e6bSLee Nipper };
758497f2e6bSLee Nipper 
75956af8cd4SLee Nipper static int aead_setauthsize(struct crypto_aead *authenc,
7609c4a7965SKim Phillips 			    unsigned int authsize)
7619c4a7965SKim Phillips {
7629c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7639c4a7965SKim Phillips 
7649c4a7965SKim Phillips 	ctx->authsize = authsize;
7659c4a7965SKim Phillips 
7669c4a7965SKim Phillips 	return 0;
7679c4a7965SKim Phillips }
7689c4a7965SKim Phillips 
76956af8cd4SLee Nipper static int aead_setkey(struct crypto_aead *authenc,
7709c4a7965SKim Phillips 		       const u8 *key, unsigned int keylen)
7719c4a7965SKim Phillips {
7729c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
7739c4a7965SKim Phillips 	struct rtattr *rta = (void *)key;
7749c4a7965SKim Phillips 	struct crypto_authenc_key_param *param;
7759c4a7965SKim Phillips 	unsigned int authkeylen;
7769c4a7965SKim Phillips 	unsigned int enckeylen;
7779c4a7965SKim Phillips 
7789c4a7965SKim Phillips 	if (!RTA_OK(rta, keylen))
7799c4a7965SKim Phillips 		goto badkey;
7809c4a7965SKim Phillips 
7819c4a7965SKim Phillips 	if (rta->rta_type != CRYPTO_AUTHENC_KEYA_PARAM)
7829c4a7965SKim Phillips 		goto badkey;
7839c4a7965SKim Phillips 
7849c4a7965SKim Phillips 	if (RTA_PAYLOAD(rta) < sizeof(*param))
7859c4a7965SKim Phillips 		goto badkey;
7869c4a7965SKim Phillips 
7879c4a7965SKim Phillips 	param = RTA_DATA(rta);
7889c4a7965SKim Phillips 	enckeylen = be32_to_cpu(param->enckeylen);
7899c4a7965SKim Phillips 
7909c4a7965SKim Phillips 	key += RTA_ALIGN(rta->rta_len);
7919c4a7965SKim Phillips 	keylen -= RTA_ALIGN(rta->rta_len);
7929c4a7965SKim Phillips 
7939c4a7965SKim Phillips 	if (keylen < enckeylen)
7949c4a7965SKim Phillips 		goto badkey;
7959c4a7965SKim Phillips 
7969c4a7965SKim Phillips 	authkeylen = keylen - enckeylen;
7979c4a7965SKim Phillips 
7989c4a7965SKim Phillips 	if (keylen > TALITOS_MAX_KEY_SIZE)
7999c4a7965SKim Phillips 		goto badkey;
8009c4a7965SKim Phillips 
8019c4a7965SKim Phillips 	memcpy(&ctx->key, key, keylen);
8029c4a7965SKim Phillips 
8039c4a7965SKim Phillips 	ctx->keylen = keylen;
8049c4a7965SKim Phillips 	ctx->enckeylen = enckeylen;
8059c4a7965SKim Phillips 	ctx->authkeylen = authkeylen;
8069c4a7965SKim Phillips 
8079c4a7965SKim Phillips 	return 0;
8089c4a7965SKim Phillips 
8099c4a7965SKim Phillips badkey:
8109c4a7965SKim Phillips 	crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
8119c4a7965SKim Phillips 	return -EINVAL;
8129c4a7965SKim Phillips }
8139c4a7965SKim Phillips 
8149c4a7965SKim Phillips /*
81556af8cd4SLee Nipper  * talitos_edesc - s/w-extended descriptor
8169c4a7965SKim Phillips  * @src_nents: number of segments in input scatterlist
8179c4a7965SKim Phillips  * @dst_nents: number of segments in output scatterlist
8189c4a7965SKim Phillips  * @dma_len: length of dma mapped link_tbl space
8199c4a7965SKim Phillips  * @dma_link_tbl: bus physical address of link_tbl
8209c4a7965SKim Phillips  * @desc: h/w descriptor
8219c4a7965SKim Phillips  * @link_tbl: input and output h/w link tables (if {src,dst}_nents > 1)
8229c4a7965SKim Phillips  *
8239c4a7965SKim Phillips  * if decrypting (with authcheck), or either one of src_nents or dst_nents
8249c4a7965SKim Phillips  * is greater than 1, an integrity check value is concatenated to the end
8259c4a7965SKim Phillips  * of link_tbl data
8269c4a7965SKim Phillips  */
82756af8cd4SLee Nipper struct talitos_edesc {
8289c4a7965SKim Phillips 	int src_nents;
8299c4a7965SKim Phillips 	int dst_nents;
8304de9d0b5SLee Nipper 	int src_is_chained;
8314de9d0b5SLee Nipper 	int dst_is_chained;
8329c4a7965SKim Phillips 	int dma_len;
8339c4a7965SKim Phillips 	dma_addr_t dma_link_tbl;
8349c4a7965SKim Phillips 	struct talitos_desc desc;
8359c4a7965SKim Phillips 	struct talitos_ptr link_tbl[0];
8369c4a7965SKim Phillips };
8379c4a7965SKim Phillips 
8384de9d0b5SLee Nipper static int talitos_map_sg(struct device *dev, struct scatterlist *sg,
8394de9d0b5SLee Nipper 			  unsigned int nents, enum dma_data_direction dir,
8404de9d0b5SLee Nipper 			  int chained)
8414de9d0b5SLee Nipper {
8424de9d0b5SLee Nipper 	if (unlikely(chained))
8434de9d0b5SLee Nipper 		while (sg) {
8444de9d0b5SLee Nipper 			dma_map_sg(dev, sg, 1, dir);
8454de9d0b5SLee Nipper 			sg = scatterwalk_sg_next(sg);
8464de9d0b5SLee Nipper 		}
8474de9d0b5SLee Nipper 	else
8484de9d0b5SLee Nipper 		dma_map_sg(dev, sg, nents, dir);
8494de9d0b5SLee Nipper 	return nents;
8504de9d0b5SLee Nipper }
8514de9d0b5SLee Nipper 
8524de9d0b5SLee Nipper static void talitos_unmap_sg_chain(struct device *dev, struct scatterlist *sg,
8534de9d0b5SLee Nipper 				   enum dma_data_direction dir)
8544de9d0b5SLee Nipper {
8554de9d0b5SLee Nipper 	while (sg) {
8564de9d0b5SLee Nipper 		dma_unmap_sg(dev, sg, 1, dir);
8574de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
8584de9d0b5SLee Nipper 	}
8594de9d0b5SLee Nipper }
8604de9d0b5SLee Nipper 
8614de9d0b5SLee Nipper static void talitos_sg_unmap(struct device *dev,
8624de9d0b5SLee Nipper 			     struct talitos_edesc *edesc,
8634de9d0b5SLee Nipper 			     struct scatterlist *src,
8644de9d0b5SLee Nipper 			     struct scatterlist *dst)
8654de9d0b5SLee Nipper {
8664de9d0b5SLee Nipper 	unsigned int src_nents = edesc->src_nents ? : 1;
8674de9d0b5SLee Nipper 	unsigned int dst_nents = edesc->dst_nents ? : 1;
8684de9d0b5SLee Nipper 
8694de9d0b5SLee Nipper 	if (src != dst) {
8704de9d0b5SLee Nipper 		if (edesc->src_is_chained)
8714de9d0b5SLee Nipper 			talitos_unmap_sg_chain(dev, src, DMA_TO_DEVICE);
8724de9d0b5SLee Nipper 		else
8734de9d0b5SLee Nipper 			dma_unmap_sg(dev, src, src_nents, DMA_TO_DEVICE);
8744de9d0b5SLee Nipper 
875497f2e6bSLee Nipper 		if (dst) {
8764de9d0b5SLee Nipper 			if (edesc->dst_is_chained)
877497f2e6bSLee Nipper 				talitos_unmap_sg_chain(dev, dst,
878497f2e6bSLee Nipper 						       DMA_FROM_DEVICE);
8794de9d0b5SLee Nipper 			else
880497f2e6bSLee Nipper 				dma_unmap_sg(dev, dst, dst_nents,
881497f2e6bSLee Nipper 					     DMA_FROM_DEVICE);
882497f2e6bSLee Nipper 		}
8834de9d0b5SLee Nipper 	} else
8844de9d0b5SLee Nipper 		if (edesc->src_is_chained)
8854de9d0b5SLee Nipper 			talitos_unmap_sg_chain(dev, src, DMA_BIDIRECTIONAL);
8864de9d0b5SLee Nipper 		else
8874de9d0b5SLee Nipper 			dma_unmap_sg(dev, src, src_nents, DMA_BIDIRECTIONAL);
8884de9d0b5SLee Nipper }
8894de9d0b5SLee Nipper 
8909c4a7965SKim Phillips static void ipsec_esp_unmap(struct device *dev,
89156af8cd4SLee Nipper 			    struct talitos_edesc *edesc,
8929c4a7965SKim Phillips 			    struct aead_request *areq)
8939c4a7965SKim Phillips {
8949c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[6], DMA_FROM_DEVICE);
8959c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[3], DMA_TO_DEVICE);
8969c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
8979c4a7965SKim Phillips 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
8989c4a7965SKim Phillips 
8999c4a7965SKim Phillips 	dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE);
9009c4a7965SKim Phillips 
9014de9d0b5SLee Nipper 	talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
9029c4a7965SKim Phillips 
9039c4a7965SKim Phillips 	if (edesc->dma_len)
9049c4a7965SKim Phillips 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
9059c4a7965SKim Phillips 				 DMA_BIDIRECTIONAL);
9069c4a7965SKim Phillips }
9079c4a7965SKim Phillips 
9089c4a7965SKim Phillips /*
9099c4a7965SKim Phillips  * ipsec_esp descriptor callbacks
9109c4a7965SKim Phillips  */
9119c4a7965SKim Phillips static void ipsec_esp_encrypt_done(struct device *dev,
9129c4a7965SKim Phillips 				   struct talitos_desc *desc, void *context,
9139c4a7965SKim Phillips 				   int err)
9149c4a7965SKim Phillips {
9159c4a7965SKim Phillips 	struct aead_request *areq = context;
9169c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
9179c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
91819bbbc63SKim Phillips 	struct talitos_edesc *edesc;
9199c4a7965SKim Phillips 	struct scatterlist *sg;
9209c4a7965SKim Phillips 	void *icvdata;
9219c4a7965SKim Phillips 
92219bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
92319bbbc63SKim Phillips 
9249c4a7965SKim Phillips 	ipsec_esp_unmap(dev, edesc, areq);
9259c4a7965SKim Phillips 
9269c4a7965SKim Phillips 	/* copy the generated ICV to dst */
9279c4a7965SKim Phillips 	if (edesc->dma_len) {
9289c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[edesc->src_nents +
929f3c85bc1SLee Nipper 					   edesc->dst_nents + 2];
9309c4a7965SKim Phillips 		sg = sg_last(areq->dst, edesc->dst_nents);
9319c4a7965SKim Phillips 		memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
9329c4a7965SKim Phillips 		       icvdata, ctx->authsize);
9339c4a7965SKim Phillips 	}
9349c4a7965SKim Phillips 
9359c4a7965SKim Phillips 	kfree(edesc);
9369c4a7965SKim Phillips 
9379c4a7965SKim Phillips 	aead_request_complete(areq, err);
9389c4a7965SKim Phillips }
9399c4a7965SKim Phillips 
940fe5720e2SKim Phillips static void ipsec_esp_decrypt_swauth_done(struct device *dev,
941e938e465SKim Phillips 					  struct talitos_desc *desc,
942e938e465SKim Phillips 					  void *context, int err)
9439c4a7965SKim Phillips {
9449c4a7965SKim Phillips 	struct aead_request *req = context;
9459c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
9469c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
94719bbbc63SKim Phillips 	struct talitos_edesc *edesc;
9489c4a7965SKim Phillips 	struct scatterlist *sg;
9499c4a7965SKim Phillips 	void *icvdata;
9509c4a7965SKim Phillips 
95119bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
95219bbbc63SKim Phillips 
9539c4a7965SKim Phillips 	ipsec_esp_unmap(dev, edesc, req);
9549c4a7965SKim Phillips 
9559c4a7965SKim Phillips 	if (!err) {
9569c4a7965SKim Phillips 		/* auth check */
9579c4a7965SKim Phillips 		if (edesc->dma_len)
9589c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[edesc->src_nents +
959f3c85bc1SLee Nipper 						   edesc->dst_nents + 2];
9609c4a7965SKim Phillips 		else
9619c4a7965SKim Phillips 			icvdata = &edesc->link_tbl[0];
9629c4a7965SKim Phillips 
9639c4a7965SKim Phillips 		sg = sg_last(req->dst, edesc->dst_nents ? : 1);
9649c4a7965SKim Phillips 		err = memcmp(icvdata, (char *)sg_virt(sg) + sg->length -
9659c4a7965SKim Phillips 			     ctx->authsize, ctx->authsize) ? -EBADMSG : 0;
9669c4a7965SKim Phillips 	}
9679c4a7965SKim Phillips 
9689c4a7965SKim Phillips 	kfree(edesc);
9699c4a7965SKim Phillips 
9709c4a7965SKim Phillips 	aead_request_complete(req, err);
9719c4a7965SKim Phillips }
9729c4a7965SKim Phillips 
973fe5720e2SKim Phillips static void ipsec_esp_decrypt_hwauth_done(struct device *dev,
974e938e465SKim Phillips 					  struct talitos_desc *desc,
975e938e465SKim Phillips 					  void *context, int err)
976fe5720e2SKim Phillips {
977fe5720e2SKim Phillips 	struct aead_request *req = context;
97819bbbc63SKim Phillips 	struct talitos_edesc *edesc;
97919bbbc63SKim Phillips 
98019bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
981fe5720e2SKim Phillips 
982fe5720e2SKim Phillips 	ipsec_esp_unmap(dev, edesc, req);
983fe5720e2SKim Phillips 
984fe5720e2SKim Phillips 	/* check ICV auth status */
985e938e465SKim Phillips 	if (!err && ((desc->hdr_lo & DESC_HDR_LO_ICCR1_MASK) !=
986e938e465SKim Phillips 		     DESC_HDR_LO_ICCR1_PASS))
987fe5720e2SKim Phillips 		err = -EBADMSG;
988fe5720e2SKim Phillips 
989fe5720e2SKim Phillips 	kfree(edesc);
990fe5720e2SKim Phillips 
991fe5720e2SKim Phillips 	aead_request_complete(req, err);
992fe5720e2SKim Phillips }
993fe5720e2SKim Phillips 
9949c4a7965SKim Phillips /*
9959c4a7965SKim Phillips  * convert scatterlist to SEC h/w link table format
9969c4a7965SKim Phillips  * stop at cryptlen bytes
9979c4a7965SKim Phillips  */
99870bcaca7SLee Nipper static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
9999c4a7965SKim Phillips 			   int cryptlen, struct talitos_ptr *link_tbl_ptr)
10009c4a7965SKim Phillips {
100170bcaca7SLee Nipper 	int n_sg = sg_count;
100270bcaca7SLee Nipper 
100370bcaca7SLee Nipper 	while (n_sg--) {
100481eb024cSKim Phillips 		to_talitos_ptr(link_tbl_ptr, sg_dma_address(sg));
10059c4a7965SKim Phillips 		link_tbl_ptr->len = cpu_to_be16(sg_dma_len(sg));
10069c4a7965SKim Phillips 		link_tbl_ptr->j_extent = 0;
10079c4a7965SKim Phillips 		link_tbl_ptr++;
10089c4a7965SKim Phillips 		cryptlen -= sg_dma_len(sg);
10094de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
10109c4a7965SKim Phillips 	}
10119c4a7965SKim Phillips 
101270bcaca7SLee Nipper 	/* adjust (decrease) last one (or two) entry's len to cryptlen */
10139c4a7965SKim Phillips 	link_tbl_ptr--;
1014c0e741d4SKim Phillips 	while (be16_to_cpu(link_tbl_ptr->len) <= (-cryptlen)) {
101570bcaca7SLee Nipper 		/* Empty this entry, and move to previous one */
101670bcaca7SLee Nipper 		cryptlen += be16_to_cpu(link_tbl_ptr->len);
101770bcaca7SLee Nipper 		link_tbl_ptr->len = 0;
101870bcaca7SLee Nipper 		sg_count--;
101970bcaca7SLee Nipper 		link_tbl_ptr--;
102070bcaca7SLee Nipper 	}
10219c4a7965SKim Phillips 	link_tbl_ptr->len = cpu_to_be16(be16_to_cpu(link_tbl_ptr->len)
10229c4a7965SKim Phillips 					+ cryptlen);
10239c4a7965SKim Phillips 
10249c4a7965SKim Phillips 	/* tag end of link table */
10259c4a7965SKim Phillips 	link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
102670bcaca7SLee Nipper 
102770bcaca7SLee Nipper 	return sg_count;
10289c4a7965SKim Phillips }
10299c4a7965SKim Phillips 
10309c4a7965SKim Phillips /*
10319c4a7965SKim Phillips  * fill in and submit ipsec_esp descriptor
10329c4a7965SKim Phillips  */
103356af8cd4SLee Nipper static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
10349c4a7965SKim Phillips 		     u8 *giv, u64 seq,
10359c4a7965SKim Phillips 		     void (*callback) (struct device *dev,
10369c4a7965SKim Phillips 				       struct talitos_desc *desc,
10379c4a7965SKim Phillips 				       void *context, int error))
10389c4a7965SKim Phillips {
10399c4a7965SKim Phillips 	struct crypto_aead *aead = crypto_aead_reqtfm(areq);
10409c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(aead);
10419c4a7965SKim Phillips 	struct device *dev = ctx->dev;
10429c4a7965SKim Phillips 	struct talitos_desc *desc = &edesc->desc;
10439c4a7965SKim Phillips 	unsigned int cryptlen = areq->cryptlen;
10449c4a7965SKim Phillips 	unsigned int authsize = ctx->authsize;
1045e41256f1SKim Phillips 	unsigned int ivsize = crypto_aead_ivsize(aead);
1046fa86a267SKim Phillips 	int sg_count, ret;
1047fe5720e2SKim Phillips 	int sg_link_tbl_len;
10489c4a7965SKim Phillips 
10499c4a7965SKim Phillips 	/* hmac key */
10509c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
10519c4a7965SKim Phillips 			       0, DMA_TO_DEVICE);
10529c4a7965SKim Phillips 	/* hmac data */
1053e41256f1SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[1], areq->assoclen + ivsize,
1054e41256f1SKim Phillips 			       sg_virt(areq->assoc), 0, DMA_TO_DEVICE);
10559c4a7965SKim Phillips 	/* cipher iv */
10569c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0,
10579c4a7965SKim Phillips 			       DMA_TO_DEVICE);
10589c4a7965SKim Phillips 
10599c4a7965SKim Phillips 	/* cipher key */
10609c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
10619c4a7965SKim Phillips 			       (char *)&ctx->key + ctx->authkeylen, 0,
10629c4a7965SKim Phillips 			       DMA_TO_DEVICE);
10639c4a7965SKim Phillips 
10649c4a7965SKim Phillips 	/*
10659c4a7965SKim Phillips 	 * cipher in
10669c4a7965SKim Phillips 	 * map and adjust cipher len to aead request cryptlen.
10679c4a7965SKim Phillips 	 * extent is bytes of HMAC postpended to ciphertext,
10689c4a7965SKim Phillips 	 * typically 12 for ipsec
10699c4a7965SKim Phillips 	 */
10709c4a7965SKim Phillips 	desc->ptr[4].len = cpu_to_be16(cryptlen);
10719c4a7965SKim Phillips 	desc->ptr[4].j_extent = authsize;
10729c4a7965SKim Phillips 
1073e938e465SKim Phillips 	sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
1074e938e465SKim Phillips 				  (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
1075e938e465SKim Phillips 							   : DMA_TO_DEVICE,
10764de9d0b5SLee Nipper 				  edesc->src_is_chained);
10779c4a7965SKim Phillips 
10789c4a7965SKim Phillips 	if (sg_count == 1) {
107981eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->src));
10809c4a7965SKim Phillips 	} else {
1081fe5720e2SKim Phillips 		sg_link_tbl_len = cryptlen;
1082fe5720e2SKim Phillips 
1083962a9c99SKim Phillips 		if (edesc->desc.hdr & DESC_HDR_MODE1_MDEU_CICV)
1084fe5720e2SKim Phillips 			sg_link_tbl_len = cryptlen + authsize;
1085e938e465SKim Phillips 
1086fe5720e2SKim Phillips 		sg_count = sg_to_link_tbl(areq->src, sg_count, sg_link_tbl_len,
10879c4a7965SKim Phillips 					  &edesc->link_tbl[0]);
108870bcaca7SLee Nipper 		if (sg_count > 1) {
10899c4a7965SKim Phillips 			desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
109081eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl);
1091e938e465SKim Phillips 			dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1092e938e465SKim Phillips 						   edesc->dma_len,
1093e938e465SKim Phillips 						   DMA_BIDIRECTIONAL);
109470bcaca7SLee Nipper 		} else {
109570bcaca7SLee Nipper 			/* Only one segment now, so no link tbl needed */
109681eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[4],
109781eb024cSKim Phillips 				       sg_dma_address(areq->src));
109870bcaca7SLee Nipper 		}
10999c4a7965SKim Phillips 	}
11009c4a7965SKim Phillips 
11019c4a7965SKim Phillips 	/* cipher out */
11029c4a7965SKim Phillips 	desc->ptr[5].len = cpu_to_be16(cryptlen);
11039c4a7965SKim Phillips 	desc->ptr[5].j_extent = authsize;
11049c4a7965SKim Phillips 
1105e938e465SKim Phillips 	if (areq->src != areq->dst)
11064de9d0b5SLee Nipper 		sg_count = talitos_map_sg(dev, areq->dst,
11074de9d0b5SLee Nipper 					  edesc->dst_nents ? : 1,
11084de9d0b5SLee Nipper 					  DMA_FROM_DEVICE,
11094de9d0b5SLee Nipper 					  edesc->dst_is_chained);
11109c4a7965SKim Phillips 
11119c4a7965SKim Phillips 	if (sg_count == 1) {
111281eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst));
11139c4a7965SKim Phillips 	} else {
11149c4a7965SKim Phillips 		struct talitos_ptr *link_tbl_ptr =
1115f3c85bc1SLee Nipper 			&edesc->link_tbl[edesc->src_nents + 1];
11169c4a7965SKim Phillips 
111781eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
111881eb024cSKim Phillips 			       (edesc->src_nents + 1) *
111981eb024cSKim Phillips 			       sizeof(struct talitos_ptr));
112070bcaca7SLee Nipper 		sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
11219c4a7965SKim Phillips 					  link_tbl_ptr);
1122fe5720e2SKim Phillips 
1123f3c85bc1SLee Nipper 		/* Add an entry to the link table for ICV data */
11249c4a7965SKim Phillips 		link_tbl_ptr += sg_count - 1;
11259c4a7965SKim Phillips 		link_tbl_ptr->j_extent = 0;
1126f3c85bc1SLee Nipper 		sg_count++;
11279c4a7965SKim Phillips 		link_tbl_ptr++;
11289c4a7965SKim Phillips 		link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
11299c4a7965SKim Phillips 		link_tbl_ptr->len = cpu_to_be16(authsize);
11309c4a7965SKim Phillips 
11319c4a7965SKim Phillips 		/* icv data follows link tables */
113281eb024cSKim Phillips 		to_talitos_ptr(link_tbl_ptr, edesc->dma_link_tbl +
113381eb024cSKim Phillips 			       (edesc->src_nents + edesc->dst_nents + 2) *
113481eb024cSKim Phillips 			       sizeof(struct talitos_ptr));
11359c4a7965SKim Phillips 		desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
11369c4a7965SKim Phillips 		dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
11379c4a7965SKim Phillips 					   edesc->dma_len, DMA_BIDIRECTIONAL);
11389c4a7965SKim Phillips 	}
11399c4a7965SKim Phillips 
11409c4a7965SKim Phillips 	/* iv out */
11419c4a7965SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[6], ivsize, ctx->iv, 0,
11429c4a7965SKim Phillips 			       DMA_FROM_DEVICE);
11439c4a7965SKim Phillips 
11445228f0f7SKim Phillips 	ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
1145fa86a267SKim Phillips 	if (ret != -EINPROGRESS) {
1146fa86a267SKim Phillips 		ipsec_esp_unmap(dev, edesc, areq);
1147fa86a267SKim Phillips 		kfree(edesc);
1148fa86a267SKim Phillips 	}
1149fa86a267SKim Phillips 	return ret;
11509c4a7965SKim Phillips }
11519c4a7965SKim Phillips 
11529c4a7965SKim Phillips /*
11539c4a7965SKim Phillips  * derive number of elements in scatterlist
11549c4a7965SKim Phillips  */
11554de9d0b5SLee Nipper static int sg_count(struct scatterlist *sg_list, int nbytes, int *chained)
11569c4a7965SKim Phillips {
11579c4a7965SKim Phillips 	struct scatterlist *sg = sg_list;
11589c4a7965SKim Phillips 	int sg_nents = 0;
11599c4a7965SKim Phillips 
11604de9d0b5SLee Nipper 	*chained = 0;
11614de9d0b5SLee Nipper 	while (nbytes > 0) {
11629c4a7965SKim Phillips 		sg_nents++;
11639c4a7965SKim Phillips 		nbytes -= sg->length;
11644de9d0b5SLee Nipper 		if (!sg_is_last(sg) && (sg + 1)->length == 0)
11654de9d0b5SLee Nipper 			*chained = 1;
11664de9d0b5SLee Nipper 		sg = scatterwalk_sg_next(sg);
11679c4a7965SKim Phillips 	}
11689c4a7965SKim Phillips 
11699c4a7965SKim Phillips 	return sg_nents;
11709c4a7965SKim Phillips }
11719c4a7965SKim Phillips 
1172497f2e6bSLee Nipper /**
1173497f2e6bSLee Nipper  * sg_copy_end_to_buffer - Copy end data from SG list to a linear buffer
1174497f2e6bSLee Nipper  * @sgl:		 The SG list
1175497f2e6bSLee Nipper  * @nents:		 Number of SG entries
1176497f2e6bSLee Nipper  * @buf:		 Where to copy to
1177497f2e6bSLee Nipper  * @buflen:		 The number of bytes to copy
1178497f2e6bSLee Nipper  * @skip:		 The number of bytes to skip before copying.
1179497f2e6bSLee Nipper  *                       Note: skip + buflen should equal SG total size.
1180497f2e6bSLee Nipper  *
1181497f2e6bSLee Nipper  * Returns the number of copied bytes.
1182497f2e6bSLee Nipper  *
1183497f2e6bSLee Nipper  **/
1184497f2e6bSLee Nipper static size_t sg_copy_end_to_buffer(struct scatterlist *sgl, unsigned int nents,
1185497f2e6bSLee Nipper 				    void *buf, size_t buflen, unsigned int skip)
1186497f2e6bSLee Nipper {
1187497f2e6bSLee Nipper 	unsigned int offset = 0;
1188497f2e6bSLee Nipper 	unsigned int boffset = 0;
1189497f2e6bSLee Nipper 	struct sg_mapping_iter miter;
1190497f2e6bSLee Nipper 	unsigned long flags;
1191497f2e6bSLee Nipper 	unsigned int sg_flags = SG_MITER_ATOMIC;
1192497f2e6bSLee Nipper 	size_t total_buffer = buflen + skip;
1193497f2e6bSLee Nipper 
1194497f2e6bSLee Nipper 	sg_flags |= SG_MITER_FROM_SG;
1195497f2e6bSLee Nipper 
1196497f2e6bSLee Nipper 	sg_miter_start(&miter, sgl, nents, sg_flags);
1197497f2e6bSLee Nipper 
1198497f2e6bSLee Nipper 	local_irq_save(flags);
1199497f2e6bSLee Nipper 
1200497f2e6bSLee Nipper 	while (sg_miter_next(&miter) && offset < total_buffer) {
1201497f2e6bSLee Nipper 		unsigned int len;
1202497f2e6bSLee Nipper 		unsigned int ignore;
1203497f2e6bSLee Nipper 
1204497f2e6bSLee Nipper 		if ((offset + miter.length) > skip) {
1205497f2e6bSLee Nipper 			if (offset < skip) {
1206497f2e6bSLee Nipper 				/* Copy part of this segment */
1207497f2e6bSLee Nipper 				ignore = skip - offset;
1208497f2e6bSLee Nipper 				len = miter.length - ignore;
12097260042bSLee Nipper 				if (boffset + len > buflen)
12107260042bSLee Nipper 					len = buflen - boffset;
1211497f2e6bSLee Nipper 				memcpy(buf + boffset, miter.addr + ignore, len);
1212497f2e6bSLee Nipper 			} else {
12137260042bSLee Nipper 				/* Copy all of this segment (up to buflen) */
1214497f2e6bSLee Nipper 				len = miter.length;
12157260042bSLee Nipper 				if (boffset + len > buflen)
12167260042bSLee Nipper 					len = buflen - boffset;
1217497f2e6bSLee Nipper 				memcpy(buf + boffset, miter.addr, len);
1218497f2e6bSLee Nipper 			}
1219497f2e6bSLee Nipper 			boffset += len;
1220497f2e6bSLee Nipper 		}
1221497f2e6bSLee Nipper 		offset += miter.length;
1222497f2e6bSLee Nipper 	}
1223497f2e6bSLee Nipper 
1224497f2e6bSLee Nipper 	sg_miter_stop(&miter);
1225497f2e6bSLee Nipper 
1226497f2e6bSLee Nipper 	local_irq_restore(flags);
1227497f2e6bSLee Nipper 	return boffset;
1228497f2e6bSLee Nipper }
1229497f2e6bSLee Nipper 
12309c4a7965SKim Phillips /*
123156af8cd4SLee Nipper  * allocate and map the extended descriptor
12329c4a7965SKim Phillips  */
12334de9d0b5SLee Nipper static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
12344de9d0b5SLee Nipper 						 struct scatterlist *src,
12354de9d0b5SLee Nipper 						 struct scatterlist *dst,
1236497f2e6bSLee Nipper 						 int hash_result,
12374de9d0b5SLee Nipper 						 unsigned int cryptlen,
12384de9d0b5SLee Nipper 						 unsigned int authsize,
12394de9d0b5SLee Nipper 						 int icv_stashing,
12404de9d0b5SLee Nipper 						 u32 cryptoflags)
12419c4a7965SKim Phillips {
124256af8cd4SLee Nipper 	struct talitos_edesc *edesc;
12439c4a7965SKim Phillips 	int src_nents, dst_nents, alloc_len, dma_len;
12444de9d0b5SLee Nipper 	int src_chained, dst_chained = 0;
12454de9d0b5SLee Nipper 	gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
1246586725f8SKim Phillips 		      GFP_ATOMIC;
12479c4a7965SKim Phillips 
12484de9d0b5SLee Nipper 	if (cryptlen + authsize > TALITOS_MAX_DATA_LEN) {
12494de9d0b5SLee Nipper 		dev_err(dev, "length exceeds h/w max limit\n");
12509c4a7965SKim Phillips 		return ERR_PTR(-EINVAL);
12519c4a7965SKim Phillips 	}
12529c4a7965SKim Phillips 
12534de9d0b5SLee Nipper 	src_nents = sg_count(src, cryptlen + authsize, &src_chained);
12549c4a7965SKim Phillips 	src_nents = (src_nents == 1) ? 0 : src_nents;
12559c4a7965SKim Phillips 
1256497f2e6bSLee Nipper 	if (hash_result) {
1257497f2e6bSLee Nipper 		dst_nents = 0;
1258497f2e6bSLee Nipper 	} else {
12594de9d0b5SLee Nipper 		if (dst == src) {
12609c4a7965SKim Phillips 			dst_nents = src_nents;
12619c4a7965SKim Phillips 		} else {
1262497f2e6bSLee Nipper 			dst_nents = sg_count(dst, cryptlen + authsize,
1263497f2e6bSLee Nipper 					     &dst_chained);
1264695ad589SLee Nipper 			dst_nents = (dst_nents == 1) ? 0 : dst_nents;
12659c4a7965SKim Phillips 		}
1266497f2e6bSLee Nipper 	}
12679c4a7965SKim Phillips 
12689c4a7965SKim Phillips 	/*
12699c4a7965SKim Phillips 	 * allocate space for base edesc plus the link tables,
1270f3c85bc1SLee Nipper 	 * allowing for two separate entries for ICV and generated ICV (+ 2),
12719c4a7965SKim Phillips 	 * and the ICV data itself
12729c4a7965SKim Phillips 	 */
127356af8cd4SLee Nipper 	alloc_len = sizeof(struct talitos_edesc);
12749c4a7965SKim Phillips 	if (src_nents || dst_nents) {
1275f3c85bc1SLee Nipper 		dma_len = (src_nents + dst_nents + 2) *
12764de9d0b5SLee Nipper 				 sizeof(struct talitos_ptr) + authsize;
12779c4a7965SKim Phillips 		alloc_len += dma_len;
12789c4a7965SKim Phillips 	} else {
12799c4a7965SKim Phillips 		dma_len = 0;
12804de9d0b5SLee Nipper 		alloc_len += icv_stashing ? authsize : 0;
12819c4a7965SKim Phillips 	}
12829c4a7965SKim Phillips 
1283586725f8SKim Phillips 	edesc = kmalloc(alloc_len, GFP_DMA | flags);
12849c4a7965SKim Phillips 	if (!edesc) {
12854de9d0b5SLee Nipper 		dev_err(dev, "could not allocate edescriptor\n");
12869c4a7965SKim Phillips 		return ERR_PTR(-ENOMEM);
12879c4a7965SKim Phillips 	}
12889c4a7965SKim Phillips 
12899c4a7965SKim Phillips 	edesc->src_nents = src_nents;
12909c4a7965SKim Phillips 	edesc->dst_nents = dst_nents;
12914de9d0b5SLee Nipper 	edesc->src_is_chained = src_chained;
12924de9d0b5SLee Nipper 	edesc->dst_is_chained = dst_chained;
12939c4a7965SKim Phillips 	edesc->dma_len = dma_len;
1294497f2e6bSLee Nipper 	if (dma_len)
12954de9d0b5SLee Nipper 		edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
1296497f2e6bSLee Nipper 						     edesc->dma_len,
1297497f2e6bSLee Nipper 						     DMA_BIDIRECTIONAL);
12989c4a7965SKim Phillips 
12999c4a7965SKim Phillips 	return edesc;
13009c4a7965SKim Phillips }
13019c4a7965SKim Phillips 
13024de9d0b5SLee Nipper static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq,
13034de9d0b5SLee Nipper 					      int icv_stashing)
13044de9d0b5SLee Nipper {
13054de9d0b5SLee Nipper 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
13064de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
13074de9d0b5SLee Nipper 
1308497f2e6bSLee Nipper 	return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
13094de9d0b5SLee Nipper 				   areq->cryptlen, ctx->authsize, icv_stashing,
13104de9d0b5SLee Nipper 				   areq->base.flags);
13114de9d0b5SLee Nipper }
13124de9d0b5SLee Nipper 
131356af8cd4SLee Nipper static int aead_encrypt(struct aead_request *req)
13149c4a7965SKim Phillips {
13159c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
13169c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
131756af8cd4SLee Nipper 	struct talitos_edesc *edesc;
13189c4a7965SKim Phillips 
13199c4a7965SKim Phillips 	/* allocate extended descriptor */
13204de9d0b5SLee Nipper 	edesc = aead_edesc_alloc(req, 0);
13219c4a7965SKim Phillips 	if (IS_ERR(edesc))
13229c4a7965SKim Phillips 		return PTR_ERR(edesc);
13239c4a7965SKim Phillips 
13249c4a7965SKim Phillips 	/* set encrypt */
132570bcaca7SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
13269c4a7965SKim Phillips 
13279c4a7965SKim Phillips 	return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done);
13289c4a7965SKim Phillips }
13299c4a7965SKim Phillips 
133056af8cd4SLee Nipper static int aead_decrypt(struct aead_request *req)
13319c4a7965SKim Phillips {
13329c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(req);
13339c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
13349c4a7965SKim Phillips 	unsigned int authsize = ctx->authsize;
1335fe5720e2SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(ctx->dev);
133656af8cd4SLee Nipper 	struct talitos_edesc *edesc;
13379c4a7965SKim Phillips 	struct scatterlist *sg;
13389c4a7965SKim Phillips 	void *icvdata;
13399c4a7965SKim Phillips 
13409c4a7965SKim Phillips 	req->cryptlen -= authsize;
13419c4a7965SKim Phillips 
13429c4a7965SKim Phillips 	/* allocate extended descriptor */
13434de9d0b5SLee Nipper 	edesc = aead_edesc_alloc(req, 1);
13449c4a7965SKim Phillips 	if (IS_ERR(edesc))
13459c4a7965SKim Phillips 		return PTR_ERR(edesc);
13469c4a7965SKim Phillips 
1347fe5720e2SKim Phillips 	if ((priv->features & TALITOS_FTR_HW_AUTH_CHECK) &&
1348e938e465SKim Phillips 	    ((!edesc->src_nents && !edesc->dst_nents) ||
1349e938e465SKim Phillips 	     priv->features & TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT)) {
1350fe5720e2SKim Phillips 
1351fe5720e2SKim Phillips 		/* decrypt and check the ICV */
1352e938e465SKim Phillips 		edesc->desc.hdr = ctx->desc_hdr_template |
1353e938e465SKim Phillips 				  DESC_HDR_DIR_INBOUND |
1354fe5720e2SKim Phillips 				  DESC_HDR_MODE1_MDEU_CICV;
1355fe5720e2SKim Phillips 
1356fe5720e2SKim Phillips 		/* reset integrity check result bits */
1357fe5720e2SKim Phillips 		edesc->desc.hdr_lo = 0;
1358fe5720e2SKim Phillips 
1359e938e465SKim Phillips 		return ipsec_esp(edesc, req, NULL, 0,
1360e938e465SKim Phillips 				 ipsec_esp_decrypt_hwauth_done);
1361fe5720e2SKim Phillips 
1362e938e465SKim Phillips 	}
1363fe5720e2SKim Phillips 
1364fe5720e2SKim Phillips 	/* Have to check the ICV with software */
1365fe5720e2SKim Phillips 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
1366fe5720e2SKim Phillips 
13679c4a7965SKim Phillips 	/* stash incoming ICV for later cmp with ICV generated by the h/w */
13689c4a7965SKim Phillips 	if (edesc->dma_len)
13699c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[edesc->src_nents +
1370f3c85bc1SLee Nipper 					   edesc->dst_nents + 2];
13719c4a7965SKim Phillips 	else
13729c4a7965SKim Phillips 		icvdata = &edesc->link_tbl[0];
13739c4a7965SKim Phillips 
13749c4a7965SKim Phillips 	sg = sg_last(req->src, edesc->src_nents ? : 1);
13759c4a7965SKim Phillips 
13769c4a7965SKim Phillips 	memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
13779c4a7965SKim Phillips 	       ctx->authsize);
13789c4a7965SKim Phillips 
1379fe5720e2SKim Phillips 	return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_swauth_done);
1380fe5720e2SKim Phillips }
13819c4a7965SKim Phillips 
138256af8cd4SLee Nipper static int aead_givencrypt(struct aead_givcrypt_request *req)
13839c4a7965SKim Phillips {
13849c4a7965SKim Phillips 	struct aead_request *areq = &req->areq;
13859c4a7965SKim Phillips 	struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
13869c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
138756af8cd4SLee Nipper 	struct talitos_edesc *edesc;
13889c4a7965SKim Phillips 
13899c4a7965SKim Phillips 	/* allocate extended descriptor */
13904de9d0b5SLee Nipper 	edesc = aead_edesc_alloc(areq, 0);
13919c4a7965SKim Phillips 	if (IS_ERR(edesc))
13929c4a7965SKim Phillips 		return PTR_ERR(edesc);
13939c4a7965SKim Phillips 
13949c4a7965SKim Phillips 	/* set encrypt */
139570bcaca7SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
13969c4a7965SKim Phillips 
13979c4a7965SKim Phillips 	memcpy(req->giv, ctx->iv, crypto_aead_ivsize(authenc));
1398ba95487dSKim Phillips 	/* avoid consecutive packets going out with same IV */
1399ba95487dSKim Phillips 	*(__be64 *)req->giv ^= cpu_to_be64(req->seq);
14009c4a7965SKim Phillips 
14019c4a7965SKim Phillips 	return ipsec_esp(edesc, areq, req->giv, req->seq,
14029c4a7965SKim Phillips 			 ipsec_esp_encrypt_done);
14039c4a7965SKim Phillips }
14049c4a7965SKim Phillips 
14054de9d0b5SLee Nipper static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
14064de9d0b5SLee Nipper 			     const u8 *key, unsigned int keylen)
14074de9d0b5SLee Nipper {
14084de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
14094de9d0b5SLee Nipper 
14104de9d0b5SLee Nipper 	memcpy(&ctx->key, key, keylen);
14114de9d0b5SLee Nipper 	ctx->keylen = keylen;
14124de9d0b5SLee Nipper 
14134de9d0b5SLee Nipper 	return 0;
14144de9d0b5SLee Nipper }
14154de9d0b5SLee Nipper 
14164de9d0b5SLee Nipper static void common_nonsnoop_unmap(struct device *dev,
14174de9d0b5SLee Nipper 				  struct talitos_edesc *edesc,
14184de9d0b5SLee Nipper 				  struct ablkcipher_request *areq)
14194de9d0b5SLee Nipper {
14204de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
14214de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
14224de9d0b5SLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1], DMA_TO_DEVICE);
14234de9d0b5SLee Nipper 
14244de9d0b5SLee Nipper 	talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
14254de9d0b5SLee Nipper 
14264de9d0b5SLee Nipper 	if (edesc->dma_len)
14274de9d0b5SLee Nipper 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
14284de9d0b5SLee Nipper 				 DMA_BIDIRECTIONAL);
14294de9d0b5SLee Nipper }
14304de9d0b5SLee Nipper 
14314de9d0b5SLee Nipper static void ablkcipher_done(struct device *dev,
14324de9d0b5SLee Nipper 			    struct talitos_desc *desc, void *context,
14334de9d0b5SLee Nipper 			    int err)
14344de9d0b5SLee Nipper {
14354de9d0b5SLee Nipper 	struct ablkcipher_request *areq = context;
143619bbbc63SKim Phillips 	struct talitos_edesc *edesc;
143719bbbc63SKim Phillips 
143819bbbc63SKim Phillips 	edesc = container_of(desc, struct talitos_edesc, desc);
14394de9d0b5SLee Nipper 
14404de9d0b5SLee Nipper 	common_nonsnoop_unmap(dev, edesc, areq);
14414de9d0b5SLee Nipper 
14424de9d0b5SLee Nipper 	kfree(edesc);
14434de9d0b5SLee Nipper 
14444de9d0b5SLee Nipper 	areq->base.complete(&areq->base, err);
14454de9d0b5SLee Nipper }
14464de9d0b5SLee Nipper 
14474de9d0b5SLee Nipper static int common_nonsnoop(struct talitos_edesc *edesc,
14484de9d0b5SLee Nipper 			   struct ablkcipher_request *areq,
14494de9d0b5SLee Nipper 			   void (*callback) (struct device *dev,
14504de9d0b5SLee Nipper 					     struct talitos_desc *desc,
14514de9d0b5SLee Nipper 					     void *context, int error))
14524de9d0b5SLee Nipper {
14534de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
14544de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
14554de9d0b5SLee Nipper 	struct device *dev = ctx->dev;
14564de9d0b5SLee Nipper 	struct talitos_desc *desc = &edesc->desc;
14574de9d0b5SLee Nipper 	unsigned int cryptlen = areq->nbytes;
14584de9d0b5SLee Nipper 	unsigned int ivsize;
14594de9d0b5SLee Nipper 	int sg_count, ret;
14604de9d0b5SLee Nipper 
14614de9d0b5SLee Nipper 	/* first DWORD empty */
14624de9d0b5SLee Nipper 	desc->ptr[0].len = 0;
146381eb024cSKim Phillips 	to_talitos_ptr(&desc->ptr[0], 0);
14644de9d0b5SLee Nipper 	desc->ptr[0].j_extent = 0;
14654de9d0b5SLee Nipper 
14664de9d0b5SLee Nipper 	/* cipher iv */
14674de9d0b5SLee Nipper 	ivsize = crypto_ablkcipher_ivsize(cipher);
1468febec542SKim Phillips 	map_single_talitos_ptr(dev, &desc->ptr[1], ivsize, areq->info, 0,
14694de9d0b5SLee Nipper 			       DMA_TO_DEVICE);
14704de9d0b5SLee Nipper 
14714de9d0b5SLee Nipper 	/* cipher key */
14724de9d0b5SLee Nipper 	map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
14734de9d0b5SLee Nipper 			       (char *)&ctx->key, 0, DMA_TO_DEVICE);
14744de9d0b5SLee Nipper 
14754de9d0b5SLee Nipper 	/*
14764de9d0b5SLee Nipper 	 * cipher in
14774de9d0b5SLee Nipper 	 */
14784de9d0b5SLee Nipper 	desc->ptr[3].len = cpu_to_be16(cryptlen);
14794de9d0b5SLee Nipper 	desc->ptr[3].j_extent = 0;
14804de9d0b5SLee Nipper 
14814de9d0b5SLee Nipper 	sg_count = talitos_map_sg(dev, areq->src, edesc->src_nents ? : 1,
14824de9d0b5SLee Nipper 				  (areq->src == areq->dst) ? DMA_BIDIRECTIONAL
14834de9d0b5SLee Nipper 							   : DMA_TO_DEVICE,
14844de9d0b5SLee Nipper 				  edesc->src_is_chained);
14854de9d0b5SLee Nipper 
14864de9d0b5SLee Nipper 	if (sg_count == 1) {
148781eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[3], sg_dma_address(areq->src));
14884de9d0b5SLee Nipper 	} else {
14894de9d0b5SLee Nipper 		sg_count = sg_to_link_tbl(areq->src, sg_count, cryptlen,
14904de9d0b5SLee Nipper 					  &edesc->link_tbl[0]);
14914de9d0b5SLee Nipper 		if (sg_count > 1) {
149281eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
14934de9d0b5SLee Nipper 			desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1494e938e465SKim Phillips 			dma_sync_single_for_device(dev, edesc->dma_link_tbl,
1495e938e465SKim Phillips 						   edesc->dma_len,
1496e938e465SKim Phillips 						   DMA_BIDIRECTIONAL);
14974de9d0b5SLee Nipper 		} else {
14984de9d0b5SLee Nipper 			/* Only one segment now, so no link tbl needed */
149981eb024cSKim Phillips 			to_talitos_ptr(&desc->ptr[3],
150081eb024cSKim Phillips 				       sg_dma_address(areq->src));
15014de9d0b5SLee Nipper 		}
15024de9d0b5SLee Nipper 	}
15034de9d0b5SLee Nipper 
15044de9d0b5SLee Nipper 	/* cipher out */
15054de9d0b5SLee Nipper 	desc->ptr[4].len = cpu_to_be16(cryptlen);
15064de9d0b5SLee Nipper 	desc->ptr[4].j_extent = 0;
15074de9d0b5SLee Nipper 
15084de9d0b5SLee Nipper 	if (areq->src != areq->dst)
15094de9d0b5SLee Nipper 		sg_count = talitos_map_sg(dev, areq->dst,
15104de9d0b5SLee Nipper 					  edesc->dst_nents ? : 1,
15114de9d0b5SLee Nipper 					  DMA_FROM_DEVICE,
15124de9d0b5SLee Nipper 					  edesc->dst_is_chained);
15134de9d0b5SLee Nipper 
15144de9d0b5SLee Nipper 	if (sg_count == 1) {
151581eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], sg_dma_address(areq->dst));
15164de9d0b5SLee Nipper 	} else {
15174de9d0b5SLee Nipper 		struct talitos_ptr *link_tbl_ptr =
15184de9d0b5SLee Nipper 			&edesc->link_tbl[edesc->src_nents + 1];
15194de9d0b5SLee Nipper 
152081eb024cSKim Phillips 		to_talitos_ptr(&desc->ptr[4], edesc->dma_link_tbl +
152181eb024cSKim Phillips 					      (edesc->src_nents + 1) *
152281eb024cSKim Phillips 					      sizeof(struct talitos_ptr));
15234de9d0b5SLee Nipper 		desc->ptr[4].j_extent |= DESC_PTR_LNKTBL_JUMP;
15244de9d0b5SLee Nipper 		sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
15254de9d0b5SLee Nipper 					  link_tbl_ptr);
15264de9d0b5SLee Nipper 		dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
15274de9d0b5SLee Nipper 					   edesc->dma_len, DMA_BIDIRECTIONAL);
15284de9d0b5SLee Nipper 	}
15294de9d0b5SLee Nipper 
15304de9d0b5SLee Nipper 	/* iv out */
15314de9d0b5SLee Nipper 	map_single_talitos_ptr(dev, &desc->ptr[5], ivsize, ctx->iv, 0,
15324de9d0b5SLee Nipper 			       DMA_FROM_DEVICE);
15334de9d0b5SLee Nipper 
15344de9d0b5SLee Nipper 	/* last DWORD empty */
15354de9d0b5SLee Nipper 	desc->ptr[6].len = 0;
153681eb024cSKim Phillips 	to_talitos_ptr(&desc->ptr[6], 0);
15374de9d0b5SLee Nipper 	desc->ptr[6].j_extent = 0;
15384de9d0b5SLee Nipper 
15395228f0f7SKim Phillips 	ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
15404de9d0b5SLee Nipper 	if (ret != -EINPROGRESS) {
15414de9d0b5SLee Nipper 		common_nonsnoop_unmap(dev, edesc, areq);
15424de9d0b5SLee Nipper 		kfree(edesc);
15434de9d0b5SLee Nipper 	}
15444de9d0b5SLee Nipper 	return ret;
15454de9d0b5SLee Nipper }
15464de9d0b5SLee Nipper 
1547e938e465SKim Phillips static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
1548e938e465SKim Phillips 						    areq)
15494de9d0b5SLee Nipper {
15504de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
15514de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
15524de9d0b5SLee Nipper 
1553497f2e6bSLee Nipper 	return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, 0,
1554497f2e6bSLee Nipper 				   areq->nbytes, 0, 0, areq->base.flags);
15554de9d0b5SLee Nipper }
15564de9d0b5SLee Nipper 
15574de9d0b5SLee Nipper static int ablkcipher_encrypt(struct ablkcipher_request *areq)
15584de9d0b5SLee Nipper {
15594de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
15604de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
15614de9d0b5SLee Nipper 	struct talitos_edesc *edesc;
15624de9d0b5SLee Nipper 
15634de9d0b5SLee Nipper 	/* allocate extended descriptor */
15644de9d0b5SLee Nipper 	edesc = ablkcipher_edesc_alloc(areq);
15654de9d0b5SLee Nipper 	if (IS_ERR(edesc))
15664de9d0b5SLee Nipper 		return PTR_ERR(edesc);
15674de9d0b5SLee Nipper 
15684de9d0b5SLee Nipper 	/* set encrypt */
15694de9d0b5SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
15704de9d0b5SLee Nipper 
1571febec542SKim Phillips 	return common_nonsnoop(edesc, areq, ablkcipher_done);
15724de9d0b5SLee Nipper }
15734de9d0b5SLee Nipper 
15744de9d0b5SLee Nipper static int ablkcipher_decrypt(struct ablkcipher_request *areq)
15754de9d0b5SLee Nipper {
15764de9d0b5SLee Nipper 	struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
15774de9d0b5SLee Nipper 	struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
15784de9d0b5SLee Nipper 	struct talitos_edesc *edesc;
15794de9d0b5SLee Nipper 
15804de9d0b5SLee Nipper 	/* allocate extended descriptor */
15814de9d0b5SLee Nipper 	edesc = ablkcipher_edesc_alloc(areq);
15824de9d0b5SLee Nipper 	if (IS_ERR(edesc))
15834de9d0b5SLee Nipper 		return PTR_ERR(edesc);
15844de9d0b5SLee Nipper 
15854de9d0b5SLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_DIR_INBOUND;
15864de9d0b5SLee Nipper 
1587febec542SKim Phillips 	return common_nonsnoop(edesc, areq, ablkcipher_done);
15884de9d0b5SLee Nipper }
15894de9d0b5SLee Nipper 
1590497f2e6bSLee Nipper static void common_nonsnoop_hash_unmap(struct device *dev,
1591497f2e6bSLee Nipper 				       struct talitos_edesc *edesc,
1592497f2e6bSLee Nipper 				       struct ahash_request *areq)
1593497f2e6bSLee Nipper {
1594497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1595497f2e6bSLee Nipper 
1596497f2e6bSLee Nipper 	unmap_single_talitos_ptr(dev, &edesc->desc.ptr[5], DMA_FROM_DEVICE);
1597497f2e6bSLee Nipper 
1598497f2e6bSLee Nipper 	/* When using hashctx-in, must unmap it. */
1599497f2e6bSLee Nipper 	if (edesc->desc.ptr[1].len)
1600497f2e6bSLee Nipper 		unmap_single_talitos_ptr(dev, &edesc->desc.ptr[1],
1601497f2e6bSLee Nipper 					 DMA_TO_DEVICE);
1602497f2e6bSLee Nipper 
1603497f2e6bSLee Nipper 	if (edesc->desc.ptr[2].len)
1604497f2e6bSLee Nipper 		unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2],
1605497f2e6bSLee Nipper 					 DMA_TO_DEVICE);
1606497f2e6bSLee Nipper 
1607497f2e6bSLee Nipper 	talitos_sg_unmap(dev, edesc, req_ctx->psrc, NULL);
1608497f2e6bSLee Nipper 
1609497f2e6bSLee Nipper 	if (edesc->dma_len)
1610497f2e6bSLee Nipper 		dma_unmap_single(dev, edesc->dma_link_tbl, edesc->dma_len,
1611497f2e6bSLee Nipper 				 DMA_BIDIRECTIONAL);
1612497f2e6bSLee Nipper 
1613497f2e6bSLee Nipper }
1614497f2e6bSLee Nipper 
1615497f2e6bSLee Nipper static void ahash_done(struct device *dev,
1616497f2e6bSLee Nipper 		       struct talitos_desc *desc, void *context,
1617497f2e6bSLee Nipper 		       int err)
1618497f2e6bSLee Nipper {
1619497f2e6bSLee Nipper 	struct ahash_request *areq = context;
1620497f2e6bSLee Nipper 	struct talitos_edesc *edesc =
1621497f2e6bSLee Nipper 		 container_of(desc, struct talitos_edesc, desc);
1622497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1623497f2e6bSLee Nipper 
1624497f2e6bSLee Nipper 	if (!req_ctx->last && req_ctx->to_hash_later) {
1625497f2e6bSLee Nipper 		/* Position any partial block for next update/final/finup */
1626497f2e6bSLee Nipper 		memcpy(req_ctx->buf, req_ctx->bufnext, req_ctx->to_hash_later);
16275e833bc4SLee Nipper 		req_ctx->nbuf = req_ctx->to_hash_later;
1628497f2e6bSLee Nipper 	}
1629497f2e6bSLee Nipper 	common_nonsnoop_hash_unmap(dev, edesc, areq);
1630497f2e6bSLee Nipper 
1631497f2e6bSLee Nipper 	kfree(edesc);
1632497f2e6bSLee Nipper 
1633497f2e6bSLee Nipper 	areq->base.complete(&areq->base, err);
1634497f2e6bSLee Nipper }
1635497f2e6bSLee Nipper 
1636497f2e6bSLee Nipper static int common_nonsnoop_hash(struct talitos_edesc *edesc,
1637497f2e6bSLee Nipper 				struct ahash_request *areq, unsigned int length,
1638497f2e6bSLee Nipper 				void (*callback) (struct device *dev,
1639497f2e6bSLee Nipper 						  struct talitos_desc *desc,
1640497f2e6bSLee Nipper 						  void *context, int error))
1641497f2e6bSLee Nipper {
1642497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1643497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1644497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1645497f2e6bSLee Nipper 	struct device *dev = ctx->dev;
1646497f2e6bSLee Nipper 	struct talitos_desc *desc = &edesc->desc;
1647497f2e6bSLee Nipper 	int sg_count, ret;
1648497f2e6bSLee Nipper 
1649497f2e6bSLee Nipper 	/* first DWORD empty */
1650497f2e6bSLee Nipper 	desc->ptr[0] = zero_entry;
1651497f2e6bSLee Nipper 
165260f208d7SKim Phillips 	/* hash context in */
165360f208d7SKim Phillips 	if (!req_ctx->first || req_ctx->swinit) {
1654497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[1],
1655497f2e6bSLee Nipper 				       req_ctx->hw_context_size,
1656497f2e6bSLee Nipper 				       (char *)req_ctx->hw_context, 0,
1657497f2e6bSLee Nipper 				       DMA_TO_DEVICE);
165860f208d7SKim Phillips 		req_ctx->swinit = 0;
1659497f2e6bSLee Nipper 	} else {
1660497f2e6bSLee Nipper 		desc->ptr[1] = zero_entry;
1661497f2e6bSLee Nipper 		/* Indicate next op is not the first. */
1662497f2e6bSLee Nipper 		req_ctx->first = 0;
1663497f2e6bSLee Nipper 	}
1664497f2e6bSLee Nipper 
1665497f2e6bSLee Nipper 	/* HMAC key */
1666497f2e6bSLee Nipper 	if (ctx->keylen)
1667497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
1668497f2e6bSLee Nipper 				       (char *)&ctx->key, 0, DMA_TO_DEVICE);
1669497f2e6bSLee Nipper 	else
1670497f2e6bSLee Nipper 		desc->ptr[2] = zero_entry;
1671497f2e6bSLee Nipper 
1672497f2e6bSLee Nipper 	/*
1673497f2e6bSLee Nipper 	 * data in
1674497f2e6bSLee Nipper 	 */
1675497f2e6bSLee Nipper 	desc->ptr[3].len = cpu_to_be16(length);
1676497f2e6bSLee Nipper 	desc->ptr[3].j_extent = 0;
1677497f2e6bSLee Nipper 
1678497f2e6bSLee Nipper 	sg_count = talitos_map_sg(dev, req_ctx->psrc,
1679497f2e6bSLee Nipper 				  edesc->src_nents ? : 1,
1680497f2e6bSLee Nipper 				  DMA_TO_DEVICE,
1681497f2e6bSLee Nipper 				  edesc->src_is_chained);
1682497f2e6bSLee Nipper 
1683497f2e6bSLee Nipper 	if (sg_count == 1) {
1684497f2e6bSLee Nipper 		to_talitos_ptr(&desc->ptr[3], sg_dma_address(req_ctx->psrc));
1685497f2e6bSLee Nipper 	} else {
1686497f2e6bSLee Nipper 		sg_count = sg_to_link_tbl(req_ctx->psrc, sg_count, length,
1687497f2e6bSLee Nipper 					  &edesc->link_tbl[0]);
1688497f2e6bSLee Nipper 		if (sg_count > 1) {
1689497f2e6bSLee Nipper 			desc->ptr[3].j_extent |= DESC_PTR_LNKTBL_JUMP;
1690497f2e6bSLee Nipper 			to_talitos_ptr(&desc->ptr[3], edesc->dma_link_tbl);
1691497f2e6bSLee Nipper 			dma_sync_single_for_device(ctx->dev,
1692497f2e6bSLee Nipper 						   edesc->dma_link_tbl,
1693497f2e6bSLee Nipper 						   edesc->dma_len,
1694497f2e6bSLee Nipper 						   DMA_BIDIRECTIONAL);
1695497f2e6bSLee Nipper 		} else {
1696497f2e6bSLee Nipper 			/* Only one segment now, so no link tbl needed */
1697497f2e6bSLee Nipper 			to_talitos_ptr(&desc->ptr[3],
1698497f2e6bSLee Nipper 				       sg_dma_address(req_ctx->psrc));
1699497f2e6bSLee Nipper 		}
1700497f2e6bSLee Nipper 	}
1701497f2e6bSLee Nipper 
1702497f2e6bSLee Nipper 	/* fifth DWORD empty */
1703497f2e6bSLee Nipper 	desc->ptr[4] = zero_entry;
1704497f2e6bSLee Nipper 
1705497f2e6bSLee Nipper 	/* hash/HMAC out -or- hash context out */
1706497f2e6bSLee Nipper 	if (req_ctx->last)
1707497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[5],
1708497f2e6bSLee Nipper 				       crypto_ahash_digestsize(tfm),
1709497f2e6bSLee Nipper 				       areq->result, 0, DMA_FROM_DEVICE);
1710497f2e6bSLee Nipper 	else
1711497f2e6bSLee Nipper 		map_single_talitos_ptr(dev, &desc->ptr[5],
1712497f2e6bSLee Nipper 				       req_ctx->hw_context_size,
1713497f2e6bSLee Nipper 				       req_ctx->hw_context, 0, DMA_FROM_DEVICE);
1714497f2e6bSLee Nipper 
1715497f2e6bSLee Nipper 	/* last DWORD empty */
1716497f2e6bSLee Nipper 	desc->ptr[6] = zero_entry;
1717497f2e6bSLee Nipper 
17185228f0f7SKim Phillips 	ret = talitos_submit(dev, ctx->ch, desc, callback, areq);
1719497f2e6bSLee Nipper 	if (ret != -EINPROGRESS) {
1720497f2e6bSLee Nipper 		common_nonsnoop_hash_unmap(dev, edesc, areq);
1721497f2e6bSLee Nipper 		kfree(edesc);
1722497f2e6bSLee Nipper 	}
1723497f2e6bSLee Nipper 	return ret;
1724497f2e6bSLee Nipper }
1725497f2e6bSLee Nipper 
1726497f2e6bSLee Nipper static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
1727497f2e6bSLee Nipper 					       unsigned int nbytes)
1728497f2e6bSLee Nipper {
1729497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1730497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1731497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1732497f2e6bSLee Nipper 
1733497f2e6bSLee Nipper 	return talitos_edesc_alloc(ctx->dev, req_ctx->psrc, NULL, 1,
1734497f2e6bSLee Nipper 				   nbytes, 0, 0, areq->base.flags);
1735497f2e6bSLee Nipper }
1736497f2e6bSLee Nipper 
1737497f2e6bSLee Nipper static int ahash_init(struct ahash_request *areq)
1738497f2e6bSLee Nipper {
1739497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1740497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1741497f2e6bSLee Nipper 
1742497f2e6bSLee Nipper 	/* Initialize the context */
17435e833bc4SLee Nipper 	req_ctx->nbuf = 0;
174460f208d7SKim Phillips 	req_ctx->first = 1; /* first indicates h/w must init its context */
174560f208d7SKim Phillips 	req_ctx->swinit = 0; /* assume h/w init of context */
1746497f2e6bSLee Nipper 	req_ctx->hw_context_size =
1747497f2e6bSLee Nipper 		(crypto_ahash_digestsize(tfm) <= SHA256_DIGEST_SIZE)
1748497f2e6bSLee Nipper 			? TALITOS_MDEU_CONTEXT_SIZE_MD5_SHA1_SHA256
1749497f2e6bSLee Nipper 			: TALITOS_MDEU_CONTEXT_SIZE_SHA384_SHA512;
1750497f2e6bSLee Nipper 
1751497f2e6bSLee Nipper 	return 0;
1752497f2e6bSLee Nipper }
1753497f2e6bSLee Nipper 
175460f208d7SKim Phillips /*
175560f208d7SKim Phillips  * on h/w without explicit sha224 support, we initialize h/w context
175660f208d7SKim Phillips  * manually with sha224 constants, and tell it to run sha256.
175760f208d7SKim Phillips  */
175860f208d7SKim Phillips static int ahash_init_sha224_swinit(struct ahash_request *areq)
175960f208d7SKim Phillips {
176060f208d7SKim Phillips 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
176160f208d7SKim Phillips 
176260f208d7SKim Phillips 	ahash_init(areq);
176360f208d7SKim Phillips 	req_ctx->swinit = 1;/* prevent h/w initting context with sha256 values*/
176460f208d7SKim Phillips 
1765a752447aSKim Phillips 	req_ctx->hw_context[0] = SHA224_H0;
1766a752447aSKim Phillips 	req_ctx->hw_context[1] = SHA224_H1;
1767a752447aSKim Phillips 	req_ctx->hw_context[2] = SHA224_H2;
1768a752447aSKim Phillips 	req_ctx->hw_context[3] = SHA224_H3;
1769a752447aSKim Phillips 	req_ctx->hw_context[4] = SHA224_H4;
1770a752447aSKim Phillips 	req_ctx->hw_context[5] = SHA224_H5;
1771a752447aSKim Phillips 	req_ctx->hw_context[6] = SHA224_H6;
1772a752447aSKim Phillips 	req_ctx->hw_context[7] = SHA224_H7;
177360f208d7SKim Phillips 
177460f208d7SKim Phillips 	/* init 64-bit count */
177560f208d7SKim Phillips 	req_ctx->hw_context[8] = 0;
177660f208d7SKim Phillips 	req_ctx->hw_context[9] = 0;
177760f208d7SKim Phillips 
177860f208d7SKim Phillips 	return 0;
177960f208d7SKim Phillips }
178060f208d7SKim Phillips 
1781497f2e6bSLee Nipper static int ahash_process_req(struct ahash_request *areq, unsigned int nbytes)
1782497f2e6bSLee Nipper {
1783497f2e6bSLee Nipper 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
1784497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
1785497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1786497f2e6bSLee Nipper 	struct talitos_edesc *edesc;
1787497f2e6bSLee Nipper 	unsigned int blocksize =
1788497f2e6bSLee Nipper 			crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
1789497f2e6bSLee Nipper 	unsigned int nbytes_to_hash;
1790497f2e6bSLee Nipper 	unsigned int to_hash_later;
17915e833bc4SLee Nipper 	unsigned int nsg;
1792497f2e6bSLee Nipper 	int chained;
1793497f2e6bSLee Nipper 
17945e833bc4SLee Nipper 	if (!req_ctx->last && (nbytes + req_ctx->nbuf <= blocksize)) {
17955e833bc4SLee Nipper 		/* Buffer up to one whole block */
1796497f2e6bSLee Nipper 		sg_copy_to_buffer(areq->src,
1797497f2e6bSLee Nipper 				  sg_count(areq->src, nbytes, &chained),
17985e833bc4SLee Nipper 				  req_ctx->buf + req_ctx->nbuf, nbytes);
17995e833bc4SLee Nipper 		req_ctx->nbuf += nbytes;
1800497f2e6bSLee Nipper 		return 0;
1801497f2e6bSLee Nipper 	}
1802497f2e6bSLee Nipper 
18035e833bc4SLee Nipper 	/* At least (blocksize + 1) bytes are available to hash */
18045e833bc4SLee Nipper 	nbytes_to_hash = nbytes + req_ctx->nbuf;
18055e833bc4SLee Nipper 	to_hash_later = nbytes_to_hash & (blocksize - 1);
18065e833bc4SLee Nipper 
18075e833bc4SLee Nipper 	if (req_ctx->last)
18085e833bc4SLee Nipper 		to_hash_later = 0;
18095e833bc4SLee Nipper 	else if (to_hash_later)
18105e833bc4SLee Nipper 		/* There is a partial block. Hash the full block(s) now */
18115e833bc4SLee Nipper 		nbytes_to_hash -= to_hash_later;
18125e833bc4SLee Nipper 	else {
18135e833bc4SLee Nipper 		/* Keep one block buffered */
18145e833bc4SLee Nipper 		nbytes_to_hash -= blocksize;
18155e833bc4SLee Nipper 		to_hash_later = blocksize;
1816497f2e6bSLee Nipper 	}
18175e833bc4SLee Nipper 
18185e833bc4SLee Nipper 	/* Chain in any previously buffered data */
18195e833bc4SLee Nipper 	if (req_ctx->nbuf) {
18205e833bc4SLee Nipper 		nsg = (req_ctx->nbuf < nbytes_to_hash) ? 2 : 1;
18215e833bc4SLee Nipper 		sg_init_table(req_ctx->bufsl, nsg);
18225e833bc4SLee Nipper 		sg_set_buf(req_ctx->bufsl, req_ctx->buf, req_ctx->nbuf);
18235e833bc4SLee Nipper 		if (nsg > 1)
18245e833bc4SLee Nipper 			scatterwalk_sg_chain(req_ctx->bufsl, 2, areq->src);
18255e833bc4SLee Nipper 		req_ctx->psrc = req_ctx->bufsl;
18265e833bc4SLee Nipper 	} else
18275e833bc4SLee Nipper 		req_ctx->psrc = areq->src;
18285e833bc4SLee Nipper 
1829497f2e6bSLee Nipper 	if (to_hash_later) {
18305e833bc4SLee Nipper 		int nents = sg_count(areq->src, nbytes, &chained);
1831497f2e6bSLee Nipper 		sg_copy_end_to_buffer(areq->src, nents,
1832497f2e6bSLee Nipper 				      req_ctx->bufnext,
1833497f2e6bSLee Nipper 				      to_hash_later,
1834497f2e6bSLee Nipper 				      nbytes - to_hash_later);
1835497f2e6bSLee Nipper 	}
1836497f2e6bSLee Nipper 	req_ctx->to_hash_later = to_hash_later;
1837497f2e6bSLee Nipper 
18385e833bc4SLee Nipper 	/* Allocate extended descriptor */
1839497f2e6bSLee Nipper 	edesc = ahash_edesc_alloc(areq, nbytes_to_hash);
1840497f2e6bSLee Nipper 	if (IS_ERR(edesc))
1841497f2e6bSLee Nipper 		return PTR_ERR(edesc);
1842497f2e6bSLee Nipper 
1843497f2e6bSLee Nipper 	edesc->desc.hdr = ctx->desc_hdr_template;
1844497f2e6bSLee Nipper 
1845497f2e6bSLee Nipper 	/* On last one, request SEC to pad; otherwise continue */
1846497f2e6bSLee Nipper 	if (req_ctx->last)
1847497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_PAD;
1848497f2e6bSLee Nipper 	else
1849497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_CONT;
1850497f2e6bSLee Nipper 
185160f208d7SKim Phillips 	/* request SEC to INIT hash. */
185260f208d7SKim Phillips 	if (req_ctx->first && !req_ctx->swinit)
1853497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_INIT;
1854497f2e6bSLee Nipper 
1855497f2e6bSLee Nipper 	/* When the tfm context has a keylen, it's an HMAC.
1856497f2e6bSLee Nipper 	 * A first or last (ie. not middle) descriptor must request HMAC.
1857497f2e6bSLee Nipper 	 */
1858497f2e6bSLee Nipper 	if (ctx->keylen && (req_ctx->first || req_ctx->last))
1859497f2e6bSLee Nipper 		edesc->desc.hdr |= DESC_HDR_MODE0_MDEU_HMAC;
1860497f2e6bSLee Nipper 
1861497f2e6bSLee Nipper 	return common_nonsnoop_hash(edesc, areq, nbytes_to_hash,
1862497f2e6bSLee Nipper 				    ahash_done);
1863497f2e6bSLee Nipper }
1864497f2e6bSLee Nipper 
1865497f2e6bSLee Nipper static int ahash_update(struct ahash_request *areq)
1866497f2e6bSLee Nipper {
1867497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1868497f2e6bSLee Nipper 
1869497f2e6bSLee Nipper 	req_ctx->last = 0;
1870497f2e6bSLee Nipper 
1871497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1872497f2e6bSLee Nipper }
1873497f2e6bSLee Nipper 
1874497f2e6bSLee Nipper static int ahash_final(struct ahash_request *areq)
1875497f2e6bSLee Nipper {
1876497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1877497f2e6bSLee Nipper 
1878497f2e6bSLee Nipper 	req_ctx->last = 1;
1879497f2e6bSLee Nipper 
1880497f2e6bSLee Nipper 	return ahash_process_req(areq, 0);
1881497f2e6bSLee Nipper }
1882497f2e6bSLee Nipper 
1883497f2e6bSLee Nipper static int ahash_finup(struct ahash_request *areq)
1884497f2e6bSLee Nipper {
1885497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
1886497f2e6bSLee Nipper 
1887497f2e6bSLee Nipper 	req_ctx->last = 1;
1888497f2e6bSLee Nipper 
1889497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1890497f2e6bSLee Nipper }
1891497f2e6bSLee Nipper 
1892497f2e6bSLee Nipper static int ahash_digest(struct ahash_request *areq)
1893497f2e6bSLee Nipper {
1894497f2e6bSLee Nipper 	struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
189560f208d7SKim Phillips 	struct crypto_ahash *ahash = crypto_ahash_reqtfm(areq);
1896497f2e6bSLee Nipper 
189760f208d7SKim Phillips 	ahash->init(areq);
1898497f2e6bSLee Nipper 	req_ctx->last = 1;
1899497f2e6bSLee Nipper 
1900497f2e6bSLee Nipper 	return ahash_process_req(areq, areq->nbytes);
1901497f2e6bSLee Nipper }
1902497f2e6bSLee Nipper 
190379b3a418SLee Nipper struct keyhash_result {
190479b3a418SLee Nipper 	struct completion completion;
190579b3a418SLee Nipper 	int err;
190679b3a418SLee Nipper };
190779b3a418SLee Nipper 
190879b3a418SLee Nipper static void keyhash_complete(struct crypto_async_request *req, int err)
190979b3a418SLee Nipper {
191079b3a418SLee Nipper 	struct keyhash_result *res = req->data;
191179b3a418SLee Nipper 
191279b3a418SLee Nipper 	if (err == -EINPROGRESS)
191379b3a418SLee Nipper 		return;
191479b3a418SLee Nipper 
191579b3a418SLee Nipper 	res->err = err;
191679b3a418SLee Nipper 	complete(&res->completion);
191779b3a418SLee Nipper }
191879b3a418SLee Nipper 
191979b3a418SLee Nipper static int keyhash(struct crypto_ahash *tfm, const u8 *key, unsigned int keylen,
192079b3a418SLee Nipper 		   u8 *hash)
192179b3a418SLee Nipper {
192279b3a418SLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
192379b3a418SLee Nipper 
192479b3a418SLee Nipper 	struct scatterlist sg[1];
192579b3a418SLee Nipper 	struct ahash_request *req;
192679b3a418SLee Nipper 	struct keyhash_result hresult;
192779b3a418SLee Nipper 	int ret;
192879b3a418SLee Nipper 
192979b3a418SLee Nipper 	init_completion(&hresult.completion);
193079b3a418SLee Nipper 
193179b3a418SLee Nipper 	req = ahash_request_alloc(tfm, GFP_KERNEL);
193279b3a418SLee Nipper 	if (!req)
193379b3a418SLee Nipper 		return -ENOMEM;
193479b3a418SLee Nipper 
193579b3a418SLee Nipper 	/* Keep tfm keylen == 0 during hash of the long key */
193679b3a418SLee Nipper 	ctx->keylen = 0;
193779b3a418SLee Nipper 	ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
193879b3a418SLee Nipper 				   keyhash_complete, &hresult);
193979b3a418SLee Nipper 
194079b3a418SLee Nipper 	sg_init_one(&sg[0], key, keylen);
194179b3a418SLee Nipper 
194279b3a418SLee Nipper 	ahash_request_set_crypt(req, sg, hash, keylen);
194379b3a418SLee Nipper 	ret = crypto_ahash_digest(req);
194479b3a418SLee Nipper 	switch (ret) {
194579b3a418SLee Nipper 	case 0:
194679b3a418SLee Nipper 		break;
194779b3a418SLee Nipper 	case -EINPROGRESS:
194879b3a418SLee Nipper 	case -EBUSY:
194979b3a418SLee Nipper 		ret = wait_for_completion_interruptible(
195079b3a418SLee Nipper 			&hresult.completion);
195179b3a418SLee Nipper 		if (!ret)
195279b3a418SLee Nipper 			ret = hresult.err;
195379b3a418SLee Nipper 		break;
195479b3a418SLee Nipper 	default:
195579b3a418SLee Nipper 		break;
195679b3a418SLee Nipper 	}
195779b3a418SLee Nipper 	ahash_request_free(req);
195879b3a418SLee Nipper 
195979b3a418SLee Nipper 	return ret;
196079b3a418SLee Nipper }
196179b3a418SLee Nipper 
196279b3a418SLee Nipper static int ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
196379b3a418SLee Nipper 			unsigned int keylen)
196479b3a418SLee Nipper {
196579b3a418SLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(crypto_ahash_tfm(tfm));
196679b3a418SLee Nipper 	unsigned int blocksize =
196779b3a418SLee Nipper 			crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
196879b3a418SLee Nipper 	unsigned int digestsize = crypto_ahash_digestsize(tfm);
196979b3a418SLee Nipper 	unsigned int keysize = keylen;
197079b3a418SLee Nipper 	u8 hash[SHA512_DIGEST_SIZE];
197179b3a418SLee Nipper 	int ret;
197279b3a418SLee Nipper 
197379b3a418SLee Nipper 	if (keylen <= blocksize)
197479b3a418SLee Nipper 		memcpy(ctx->key, key, keysize);
197579b3a418SLee Nipper 	else {
197679b3a418SLee Nipper 		/* Must get the hash of the long key */
197779b3a418SLee Nipper 		ret = keyhash(tfm, key, keylen, hash);
197879b3a418SLee Nipper 
197979b3a418SLee Nipper 		if (ret) {
198079b3a418SLee Nipper 			crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
198179b3a418SLee Nipper 			return -EINVAL;
198279b3a418SLee Nipper 		}
198379b3a418SLee Nipper 
198479b3a418SLee Nipper 		keysize = digestsize;
198579b3a418SLee Nipper 		memcpy(ctx->key, hash, digestsize);
198679b3a418SLee Nipper 	}
198779b3a418SLee Nipper 
198879b3a418SLee Nipper 	ctx->keylen = keysize;
198979b3a418SLee Nipper 
199079b3a418SLee Nipper 	return 0;
199179b3a418SLee Nipper }
199279b3a418SLee Nipper 
199379b3a418SLee Nipper 
19949c4a7965SKim Phillips struct talitos_alg_template {
1995d5e4aaefSLee Nipper 	u32 type;
1996d5e4aaefSLee Nipper 	union {
1997d5e4aaefSLee Nipper 		struct crypto_alg crypto;
1998acbf7c62SLee Nipper 		struct ahash_alg hash;
1999d5e4aaefSLee Nipper 	} alg;
20009c4a7965SKim Phillips 	__be32 desc_hdr_template;
20019c4a7965SKim Phillips };
20029c4a7965SKim Phillips 
20039c4a7965SKim Phillips static struct talitos_alg_template driver_algs[] = {
200456af8cd4SLee Nipper 	/* AEAD algorithms.  These use a single-pass ipsec_esp descriptor */
2005d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2006d5e4aaefSLee Nipper 		.alg.crypto = {
200756af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha1),cbc(aes))",
200856af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha1-cbc-aes-talitos",
200956af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
201056af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
201156af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
201256af8cd4SLee Nipper 			.cra_aead = {
201356af8cd4SLee Nipper 				.setkey = aead_setkey,
201456af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
201556af8cd4SLee Nipper 				.encrypt = aead_encrypt,
201656af8cd4SLee Nipper 				.decrypt = aead_decrypt,
201756af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
20189c4a7965SKim Phillips 				.geniv = "<built-in>",
20193952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
20203952f17eSLee Nipper 				.maxauthsize = SHA1_DIGEST_SIZE,
202156af8cd4SLee Nipper 			}
20229c4a7965SKim Phillips 		},
20239c4a7965SKim Phillips 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
20249c4a7965SKim Phillips 			             DESC_HDR_SEL0_AESU |
20259c4a7965SKim Phillips 		                     DESC_HDR_MODE0_AESU_CBC |
20269c4a7965SKim Phillips 		                     DESC_HDR_SEL1_MDEUA |
20279c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_INIT |
20289c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_PAD |
20299c4a7965SKim Phillips 		                     DESC_HDR_MODE1_MDEU_SHA1_HMAC,
203070bcaca7SLee Nipper 	},
2031d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2032d5e4aaefSLee Nipper 		.alg.crypto = {
203356af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
203456af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha1-cbc-3des-talitos",
203556af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
203656af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
203756af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
203856af8cd4SLee Nipper 			.cra_aead = {
203956af8cd4SLee Nipper 				.setkey = aead_setkey,
204056af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
204156af8cd4SLee Nipper 				.encrypt = aead_encrypt,
204256af8cd4SLee Nipper 				.decrypt = aead_decrypt,
204356af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
204470bcaca7SLee Nipper 				.geniv = "<built-in>",
20453952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
20463952f17eSLee Nipper 				.maxauthsize = SHA1_DIGEST_SIZE,
204756af8cd4SLee Nipper 			}
204870bcaca7SLee Nipper 		},
204970bcaca7SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
205070bcaca7SLee Nipper 			             DESC_HDR_SEL0_DEU |
205170bcaca7SLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
205270bcaca7SLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
205370bcaca7SLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
205470bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
205570bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
205670bcaca7SLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA1_HMAC,
20573952f17eSLee Nipper 	},
2058d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2059d5e4aaefSLee Nipper 		.alg.crypto = {
206056af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha256),cbc(aes))",
206156af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha256-cbc-aes-talitos",
206256af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
206356af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
206456af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
206556af8cd4SLee Nipper 			.cra_aead = {
206656af8cd4SLee Nipper 				.setkey = aead_setkey,
206756af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
206856af8cd4SLee Nipper 				.encrypt = aead_encrypt,
206956af8cd4SLee Nipper 				.decrypt = aead_decrypt,
207056af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
20713952f17eSLee Nipper 				.geniv = "<built-in>",
20723952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
20733952f17eSLee Nipper 				.maxauthsize = SHA256_DIGEST_SIZE,
207456af8cd4SLee Nipper 			}
20753952f17eSLee Nipper 		},
20763952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
20773952f17eSLee Nipper 			             DESC_HDR_SEL0_AESU |
20783952f17eSLee Nipper 		                     DESC_HDR_MODE0_AESU_CBC |
20793952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
20803952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
20813952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
20823952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA256_HMAC,
20833952f17eSLee Nipper 	},
2084d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2085d5e4aaefSLee Nipper 		.alg.crypto = {
208656af8cd4SLee Nipper 			.cra_name = "authenc(hmac(sha256),cbc(des3_ede))",
208756af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-sha256-cbc-3des-talitos",
208856af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
208956af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
209056af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
209156af8cd4SLee Nipper 			.cra_aead = {
209256af8cd4SLee Nipper 				.setkey = aead_setkey,
209356af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
209456af8cd4SLee Nipper 				.encrypt = aead_encrypt,
209556af8cd4SLee Nipper 				.decrypt = aead_decrypt,
209656af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
20973952f17eSLee Nipper 				.geniv = "<built-in>",
20983952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
20993952f17eSLee Nipper 				.maxauthsize = SHA256_DIGEST_SIZE,
210056af8cd4SLee Nipper 			}
21013952f17eSLee Nipper 		},
21023952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
21033952f17eSLee Nipper 			             DESC_HDR_SEL0_DEU |
21043952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
21053952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
21063952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
21073952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
21083952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
21093952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_SHA256_HMAC,
21103952f17eSLee Nipper 	},
2111d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2112d5e4aaefSLee Nipper 		.alg.crypto = {
211356af8cd4SLee Nipper 			.cra_name = "authenc(hmac(md5),cbc(aes))",
211456af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-md5-cbc-aes-talitos",
211556af8cd4SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
211656af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
211756af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
211856af8cd4SLee Nipper 			.cra_aead = {
211956af8cd4SLee Nipper 				.setkey = aead_setkey,
212056af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
212156af8cd4SLee Nipper 				.encrypt = aead_encrypt,
212256af8cd4SLee Nipper 				.decrypt = aead_decrypt,
212356af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
21243952f17eSLee Nipper 				.geniv = "<built-in>",
21253952f17eSLee Nipper 				.ivsize = AES_BLOCK_SIZE,
21263952f17eSLee Nipper 				.maxauthsize = MD5_DIGEST_SIZE,
212756af8cd4SLee Nipper 			}
21283952f17eSLee Nipper 		},
21293952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
21303952f17eSLee Nipper 			             DESC_HDR_SEL0_AESU |
21313952f17eSLee Nipper 		                     DESC_HDR_MODE0_AESU_CBC |
21323952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
21333952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
21343952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
21353952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_MD5_HMAC,
21363952f17eSLee Nipper 	},
2137d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AEAD,
2138d5e4aaefSLee Nipper 		.alg.crypto = {
213956af8cd4SLee Nipper 			.cra_name = "authenc(hmac(md5),cbc(des3_ede))",
214056af8cd4SLee Nipper 			.cra_driver_name = "authenc-hmac-md5-cbc-3des-talitos",
214156af8cd4SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
214256af8cd4SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_ASYNC,
214356af8cd4SLee Nipper 			.cra_type = &crypto_aead_type,
214456af8cd4SLee Nipper 			.cra_aead = {
214556af8cd4SLee Nipper 				.setkey = aead_setkey,
214656af8cd4SLee Nipper 				.setauthsize = aead_setauthsize,
214756af8cd4SLee Nipper 				.encrypt = aead_encrypt,
214856af8cd4SLee Nipper 				.decrypt = aead_decrypt,
214956af8cd4SLee Nipper 				.givencrypt = aead_givencrypt,
21503952f17eSLee Nipper 				.geniv = "<built-in>",
21513952f17eSLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
21523952f17eSLee Nipper 				.maxauthsize = MD5_DIGEST_SIZE,
215356af8cd4SLee Nipper 			}
21543952f17eSLee Nipper 		},
21553952f17eSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_IPSEC_ESP |
21563952f17eSLee Nipper 			             DESC_HDR_SEL0_DEU |
21573952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
21583952f17eSLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES |
21593952f17eSLee Nipper 		                     DESC_HDR_SEL1_MDEUA |
21603952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_INIT |
21613952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_PAD |
21623952f17eSLee Nipper 		                     DESC_HDR_MODE1_MDEU_MD5_HMAC,
21634de9d0b5SLee Nipper 	},
21644de9d0b5SLee Nipper 	/* ABLKCIPHER algorithms. */
2165d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2166d5e4aaefSLee Nipper 		.alg.crypto = {
21674de9d0b5SLee Nipper 			.cra_name = "cbc(aes)",
21684de9d0b5SLee Nipper 			.cra_driver_name = "cbc-aes-talitos",
21694de9d0b5SLee Nipper 			.cra_blocksize = AES_BLOCK_SIZE,
21704de9d0b5SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
21714de9d0b5SLee Nipper                                      CRYPTO_ALG_ASYNC,
21724de9d0b5SLee Nipper 			.cra_type = &crypto_ablkcipher_type,
21734de9d0b5SLee Nipper 			.cra_ablkcipher = {
21744de9d0b5SLee Nipper 				.setkey = ablkcipher_setkey,
21754de9d0b5SLee Nipper 				.encrypt = ablkcipher_encrypt,
21764de9d0b5SLee Nipper 				.decrypt = ablkcipher_decrypt,
21774de9d0b5SLee Nipper 				.geniv = "eseqiv",
21784de9d0b5SLee Nipper 				.min_keysize = AES_MIN_KEY_SIZE,
21794de9d0b5SLee Nipper 				.max_keysize = AES_MAX_KEY_SIZE,
21804de9d0b5SLee Nipper 				.ivsize = AES_BLOCK_SIZE,
21814de9d0b5SLee Nipper 			}
21824de9d0b5SLee Nipper 		},
21834de9d0b5SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
21844de9d0b5SLee Nipper 				     DESC_HDR_SEL0_AESU |
21854de9d0b5SLee Nipper 				     DESC_HDR_MODE0_AESU_CBC,
21864de9d0b5SLee Nipper 	},
2187d5e4aaefSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_ABLKCIPHER,
2188d5e4aaefSLee Nipper 		.alg.crypto = {
21894de9d0b5SLee Nipper 			.cra_name = "cbc(des3_ede)",
21904de9d0b5SLee Nipper 			.cra_driver_name = "cbc-3des-talitos",
21914de9d0b5SLee Nipper 			.cra_blocksize = DES3_EDE_BLOCK_SIZE,
21924de9d0b5SLee Nipper 			.cra_flags = CRYPTO_ALG_TYPE_ABLKCIPHER |
21934de9d0b5SLee Nipper                                      CRYPTO_ALG_ASYNC,
21944de9d0b5SLee Nipper 			.cra_type = &crypto_ablkcipher_type,
21954de9d0b5SLee Nipper 			.cra_ablkcipher = {
21964de9d0b5SLee Nipper 				.setkey = ablkcipher_setkey,
21974de9d0b5SLee Nipper 				.encrypt = ablkcipher_encrypt,
21984de9d0b5SLee Nipper 				.decrypt = ablkcipher_decrypt,
21994de9d0b5SLee Nipper 				.geniv = "eseqiv",
22004de9d0b5SLee Nipper 				.min_keysize = DES3_EDE_KEY_SIZE,
22014de9d0b5SLee Nipper 				.max_keysize = DES3_EDE_KEY_SIZE,
22024de9d0b5SLee Nipper 				.ivsize = DES3_EDE_BLOCK_SIZE,
22034de9d0b5SLee Nipper 			}
22044de9d0b5SLee Nipper 		},
22054de9d0b5SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
22064de9d0b5SLee Nipper 			             DESC_HDR_SEL0_DEU |
22074de9d0b5SLee Nipper 		                     DESC_HDR_MODE0_DEU_CBC |
22084de9d0b5SLee Nipper 		                     DESC_HDR_MODE0_DEU_3DES,
2209497f2e6bSLee Nipper 	},
2210497f2e6bSLee Nipper 	/* AHASH algorithms. */
2211497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2212497f2e6bSLee Nipper 		.alg.hash = {
2213497f2e6bSLee Nipper 			.init = ahash_init,
2214497f2e6bSLee Nipper 			.update = ahash_update,
2215497f2e6bSLee Nipper 			.final = ahash_final,
2216497f2e6bSLee Nipper 			.finup = ahash_finup,
2217497f2e6bSLee Nipper 			.digest = ahash_digest,
2218497f2e6bSLee Nipper 			.halg.digestsize = MD5_DIGEST_SIZE,
2219497f2e6bSLee Nipper 			.halg.base = {
2220497f2e6bSLee Nipper 				.cra_name = "md5",
2221497f2e6bSLee Nipper 				.cra_driver_name = "md5-talitos",
2222497f2e6bSLee Nipper 				.cra_blocksize = MD5_BLOCK_SIZE,
2223497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2224497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2225497f2e6bSLee Nipper 				.cra_type = &crypto_ahash_type
22269c4a7965SKim Phillips 			}
2227497f2e6bSLee Nipper 		},
2228497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2229497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUA |
2230497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEU_MD5,
2231497f2e6bSLee Nipper 	},
2232497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2233497f2e6bSLee Nipper 		.alg.hash = {
2234497f2e6bSLee Nipper 			.init = ahash_init,
2235497f2e6bSLee Nipper 			.update = ahash_update,
2236497f2e6bSLee Nipper 			.final = ahash_final,
2237497f2e6bSLee Nipper 			.finup = ahash_finup,
2238497f2e6bSLee Nipper 			.digest = ahash_digest,
2239497f2e6bSLee Nipper 			.halg.digestsize = SHA1_DIGEST_SIZE,
2240497f2e6bSLee Nipper 			.halg.base = {
2241497f2e6bSLee Nipper 				.cra_name = "sha1",
2242497f2e6bSLee Nipper 				.cra_driver_name = "sha1-talitos",
2243497f2e6bSLee Nipper 				.cra_blocksize = SHA1_BLOCK_SIZE,
2244497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2245497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2246497f2e6bSLee Nipper 				.cra_type = &crypto_ahash_type
2247497f2e6bSLee Nipper 			}
2248497f2e6bSLee Nipper 		},
2249497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2250497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUA |
2251497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA1,
2252497f2e6bSLee Nipper 	},
2253497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2254497f2e6bSLee Nipper 		.alg.hash = {
2255497f2e6bSLee Nipper 			.init = ahash_init,
2256497f2e6bSLee Nipper 			.update = ahash_update,
2257497f2e6bSLee Nipper 			.final = ahash_final,
2258497f2e6bSLee Nipper 			.finup = ahash_finup,
2259497f2e6bSLee Nipper 			.digest = ahash_digest,
226060f208d7SKim Phillips 			.halg.digestsize = SHA224_DIGEST_SIZE,
226160f208d7SKim Phillips 			.halg.base = {
226260f208d7SKim Phillips 				.cra_name = "sha224",
226360f208d7SKim Phillips 				.cra_driver_name = "sha224-talitos",
226460f208d7SKim Phillips 				.cra_blocksize = SHA224_BLOCK_SIZE,
226560f208d7SKim Phillips 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
226660f208d7SKim Phillips 					     CRYPTO_ALG_ASYNC,
226760f208d7SKim Phillips 				.cra_type = &crypto_ahash_type
226860f208d7SKim Phillips 			}
226960f208d7SKim Phillips 		},
227060f208d7SKim Phillips 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
227160f208d7SKim Phillips 				     DESC_HDR_SEL0_MDEUA |
227260f208d7SKim Phillips 				     DESC_HDR_MODE0_MDEU_SHA224,
227360f208d7SKim Phillips 	},
227460f208d7SKim Phillips 	{	.type = CRYPTO_ALG_TYPE_AHASH,
227560f208d7SKim Phillips 		.alg.hash = {
227660f208d7SKim Phillips 			.init = ahash_init,
227760f208d7SKim Phillips 			.update = ahash_update,
227860f208d7SKim Phillips 			.final = ahash_final,
227960f208d7SKim Phillips 			.finup = ahash_finup,
228060f208d7SKim Phillips 			.digest = ahash_digest,
2281497f2e6bSLee Nipper 			.halg.digestsize = SHA256_DIGEST_SIZE,
2282497f2e6bSLee Nipper 			.halg.base = {
2283497f2e6bSLee Nipper 				.cra_name = "sha256",
2284497f2e6bSLee Nipper 				.cra_driver_name = "sha256-talitos",
2285497f2e6bSLee Nipper 				.cra_blocksize = SHA256_BLOCK_SIZE,
2286497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2287497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2288497f2e6bSLee Nipper 				.cra_type = &crypto_ahash_type
2289497f2e6bSLee Nipper 			}
2290497f2e6bSLee Nipper 		},
2291497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2292497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUA |
2293497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA256,
2294497f2e6bSLee Nipper 	},
2295497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2296497f2e6bSLee Nipper 		.alg.hash = {
2297497f2e6bSLee Nipper 			.init = ahash_init,
2298497f2e6bSLee Nipper 			.update = ahash_update,
2299497f2e6bSLee Nipper 			.final = ahash_final,
2300497f2e6bSLee Nipper 			.finup = ahash_finup,
2301497f2e6bSLee Nipper 			.digest = ahash_digest,
2302497f2e6bSLee Nipper 			.halg.digestsize = SHA384_DIGEST_SIZE,
2303497f2e6bSLee Nipper 			.halg.base = {
2304497f2e6bSLee Nipper 				.cra_name = "sha384",
2305497f2e6bSLee Nipper 				.cra_driver_name = "sha384-talitos",
2306497f2e6bSLee Nipper 				.cra_blocksize = SHA384_BLOCK_SIZE,
2307497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2308497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2309497f2e6bSLee Nipper 				.cra_type = &crypto_ahash_type
2310497f2e6bSLee Nipper 			}
2311497f2e6bSLee Nipper 		},
2312497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2313497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUB |
2314497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA384,
2315497f2e6bSLee Nipper 	},
2316497f2e6bSLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
2317497f2e6bSLee Nipper 		.alg.hash = {
2318497f2e6bSLee Nipper 			.init = ahash_init,
2319497f2e6bSLee Nipper 			.update = ahash_update,
2320497f2e6bSLee Nipper 			.final = ahash_final,
2321497f2e6bSLee Nipper 			.finup = ahash_finup,
2322497f2e6bSLee Nipper 			.digest = ahash_digest,
2323497f2e6bSLee Nipper 			.halg.digestsize = SHA512_DIGEST_SIZE,
2324497f2e6bSLee Nipper 			.halg.base = {
2325497f2e6bSLee Nipper 				.cra_name = "sha512",
2326497f2e6bSLee Nipper 				.cra_driver_name = "sha512-talitos",
2327497f2e6bSLee Nipper 				.cra_blocksize = SHA512_BLOCK_SIZE,
2328497f2e6bSLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
2329497f2e6bSLee Nipper 					     CRYPTO_ALG_ASYNC,
2330497f2e6bSLee Nipper 				.cra_type = &crypto_ahash_type
2331497f2e6bSLee Nipper 			}
2332497f2e6bSLee Nipper 		},
2333497f2e6bSLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
2334497f2e6bSLee Nipper 				     DESC_HDR_SEL0_MDEUB |
2335497f2e6bSLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA512,
2336497f2e6bSLee Nipper 	},
233779b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
233879b3a418SLee Nipper 		.alg.hash = {
233979b3a418SLee Nipper 			.init = ahash_init,
234079b3a418SLee Nipper 			.update = ahash_update,
234179b3a418SLee Nipper 			.final = ahash_final,
234279b3a418SLee Nipper 			.finup = ahash_finup,
234379b3a418SLee Nipper 			.digest = ahash_digest,
234479b3a418SLee Nipper 			.setkey = ahash_setkey,
234579b3a418SLee Nipper 			.halg.digestsize = MD5_DIGEST_SIZE,
234679b3a418SLee Nipper 			.halg.base = {
234779b3a418SLee Nipper 				.cra_name = "hmac(md5)",
234879b3a418SLee Nipper 				.cra_driver_name = "hmac-md5-talitos",
234979b3a418SLee Nipper 				.cra_blocksize = MD5_BLOCK_SIZE,
235079b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
235179b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
235279b3a418SLee Nipper 				.cra_type = &crypto_ahash_type
235379b3a418SLee Nipper 			}
235479b3a418SLee Nipper 		},
235579b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
235679b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUA |
235779b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEU_MD5,
235879b3a418SLee Nipper 	},
235979b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
236079b3a418SLee Nipper 		.alg.hash = {
236179b3a418SLee Nipper 			.init = ahash_init,
236279b3a418SLee Nipper 			.update = ahash_update,
236379b3a418SLee Nipper 			.final = ahash_final,
236479b3a418SLee Nipper 			.finup = ahash_finup,
236579b3a418SLee Nipper 			.digest = ahash_digest,
236679b3a418SLee Nipper 			.setkey = ahash_setkey,
236779b3a418SLee Nipper 			.halg.digestsize = SHA1_DIGEST_SIZE,
236879b3a418SLee Nipper 			.halg.base = {
236979b3a418SLee Nipper 				.cra_name = "hmac(sha1)",
237079b3a418SLee Nipper 				.cra_driver_name = "hmac-sha1-talitos",
237179b3a418SLee Nipper 				.cra_blocksize = SHA1_BLOCK_SIZE,
237279b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
237379b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
237479b3a418SLee Nipper 				.cra_type = &crypto_ahash_type
237579b3a418SLee Nipper 			}
237679b3a418SLee Nipper 		},
237779b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
237879b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUA |
237979b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA1,
238079b3a418SLee Nipper 	},
238179b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
238279b3a418SLee Nipper 		.alg.hash = {
238379b3a418SLee Nipper 			.init = ahash_init,
238479b3a418SLee Nipper 			.update = ahash_update,
238579b3a418SLee Nipper 			.final = ahash_final,
238679b3a418SLee Nipper 			.finup = ahash_finup,
238779b3a418SLee Nipper 			.digest = ahash_digest,
238879b3a418SLee Nipper 			.setkey = ahash_setkey,
238979b3a418SLee Nipper 			.halg.digestsize = SHA224_DIGEST_SIZE,
239079b3a418SLee Nipper 			.halg.base = {
239179b3a418SLee Nipper 				.cra_name = "hmac(sha224)",
239279b3a418SLee Nipper 				.cra_driver_name = "hmac-sha224-talitos",
239379b3a418SLee Nipper 				.cra_blocksize = SHA224_BLOCK_SIZE,
239479b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
239579b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
239679b3a418SLee Nipper 				.cra_type = &crypto_ahash_type
239779b3a418SLee Nipper 			}
239879b3a418SLee Nipper 		},
239979b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
240079b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUA |
240179b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA224,
240279b3a418SLee Nipper 	},
240379b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
240479b3a418SLee Nipper 		.alg.hash = {
240579b3a418SLee Nipper 			.init = ahash_init,
240679b3a418SLee Nipper 			.update = ahash_update,
240779b3a418SLee Nipper 			.final = ahash_final,
240879b3a418SLee Nipper 			.finup = ahash_finup,
240979b3a418SLee Nipper 			.digest = ahash_digest,
241079b3a418SLee Nipper 			.setkey = ahash_setkey,
241179b3a418SLee Nipper 			.halg.digestsize = SHA256_DIGEST_SIZE,
241279b3a418SLee Nipper 			.halg.base = {
241379b3a418SLee Nipper 				.cra_name = "hmac(sha256)",
241479b3a418SLee Nipper 				.cra_driver_name = "hmac-sha256-talitos",
241579b3a418SLee Nipper 				.cra_blocksize = SHA256_BLOCK_SIZE,
241679b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
241779b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
241879b3a418SLee Nipper 				.cra_type = &crypto_ahash_type
241979b3a418SLee Nipper 			}
242079b3a418SLee Nipper 		},
242179b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
242279b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUA |
242379b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEU_SHA256,
242479b3a418SLee Nipper 	},
242579b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
242679b3a418SLee Nipper 		.alg.hash = {
242779b3a418SLee Nipper 			.init = ahash_init,
242879b3a418SLee Nipper 			.update = ahash_update,
242979b3a418SLee Nipper 			.final = ahash_final,
243079b3a418SLee Nipper 			.finup = ahash_finup,
243179b3a418SLee Nipper 			.digest = ahash_digest,
243279b3a418SLee Nipper 			.setkey = ahash_setkey,
243379b3a418SLee Nipper 			.halg.digestsize = SHA384_DIGEST_SIZE,
243479b3a418SLee Nipper 			.halg.base = {
243579b3a418SLee Nipper 				.cra_name = "hmac(sha384)",
243679b3a418SLee Nipper 				.cra_driver_name = "hmac-sha384-talitos",
243779b3a418SLee Nipper 				.cra_blocksize = SHA384_BLOCK_SIZE,
243879b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
243979b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
244079b3a418SLee Nipper 				.cra_type = &crypto_ahash_type
244179b3a418SLee Nipper 			}
244279b3a418SLee Nipper 		},
244379b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
244479b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUB |
244579b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA384,
244679b3a418SLee Nipper 	},
244779b3a418SLee Nipper 	{	.type = CRYPTO_ALG_TYPE_AHASH,
244879b3a418SLee Nipper 		.alg.hash = {
244979b3a418SLee Nipper 			.init = ahash_init,
245079b3a418SLee Nipper 			.update = ahash_update,
245179b3a418SLee Nipper 			.final = ahash_final,
245279b3a418SLee Nipper 			.finup = ahash_finup,
245379b3a418SLee Nipper 			.digest = ahash_digest,
245479b3a418SLee Nipper 			.setkey = ahash_setkey,
245579b3a418SLee Nipper 			.halg.digestsize = SHA512_DIGEST_SIZE,
245679b3a418SLee Nipper 			.halg.base = {
245779b3a418SLee Nipper 				.cra_name = "hmac(sha512)",
245879b3a418SLee Nipper 				.cra_driver_name = "hmac-sha512-talitos",
245979b3a418SLee Nipper 				.cra_blocksize = SHA512_BLOCK_SIZE,
246079b3a418SLee Nipper 				.cra_flags = CRYPTO_ALG_TYPE_AHASH |
246179b3a418SLee Nipper 					     CRYPTO_ALG_ASYNC,
246279b3a418SLee Nipper 				.cra_type = &crypto_ahash_type
246379b3a418SLee Nipper 			}
246479b3a418SLee Nipper 		},
246579b3a418SLee Nipper 		.desc_hdr_template = DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
246679b3a418SLee Nipper 				     DESC_HDR_SEL0_MDEUB |
246779b3a418SLee Nipper 				     DESC_HDR_MODE0_MDEUB_SHA512,
246879b3a418SLee Nipper 	}
24699c4a7965SKim Phillips };
24709c4a7965SKim Phillips 
24719c4a7965SKim Phillips struct talitos_crypto_alg {
24729c4a7965SKim Phillips 	struct list_head entry;
24739c4a7965SKim Phillips 	struct device *dev;
2474acbf7c62SLee Nipper 	struct talitos_alg_template algt;
24759c4a7965SKim Phillips };
24769c4a7965SKim Phillips 
24779c4a7965SKim Phillips static int talitos_cra_init(struct crypto_tfm *tfm)
24789c4a7965SKim Phillips {
24799c4a7965SKim Phillips 	struct crypto_alg *alg = tfm->__crt_alg;
248019bbbc63SKim Phillips 	struct talitos_crypto_alg *talitos_alg;
24819c4a7965SKim Phillips 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
24825228f0f7SKim Phillips 	struct talitos_private *priv;
24839c4a7965SKim Phillips 
2484497f2e6bSLee Nipper 	if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_AHASH)
2485497f2e6bSLee Nipper 		talitos_alg = container_of(__crypto_ahash_alg(alg),
2486497f2e6bSLee Nipper 					   struct talitos_crypto_alg,
2487497f2e6bSLee Nipper 					   algt.alg.hash);
2488497f2e6bSLee Nipper 	else
2489acbf7c62SLee Nipper 		talitos_alg = container_of(alg, struct talitos_crypto_alg,
2490acbf7c62SLee Nipper 					   algt.alg.crypto);
249119bbbc63SKim Phillips 
24929c4a7965SKim Phillips 	/* update context with ptr to dev */
24939c4a7965SKim Phillips 	ctx->dev = talitos_alg->dev;
249419bbbc63SKim Phillips 
24955228f0f7SKim Phillips 	/* assign SEC channel to tfm in round-robin fashion */
24965228f0f7SKim Phillips 	priv = dev_get_drvdata(ctx->dev);
24975228f0f7SKim Phillips 	ctx->ch = atomic_inc_return(&priv->last_chan) &
24985228f0f7SKim Phillips 		  (priv->num_channels - 1);
24995228f0f7SKim Phillips 
25009c4a7965SKim Phillips 	/* copy descriptor header template value */
2501acbf7c62SLee Nipper 	ctx->desc_hdr_template = talitos_alg->algt.desc_hdr_template;
25029c4a7965SKim Phillips 
2503602dba5aSKim Phillips 	/* select done notification */
2504602dba5aSKim Phillips 	ctx->desc_hdr_template |= DESC_HDR_DONE_NOTIFY;
2505602dba5aSKim Phillips 
2506497f2e6bSLee Nipper 	return 0;
2507497f2e6bSLee Nipper }
2508497f2e6bSLee Nipper 
2509497f2e6bSLee Nipper static int talitos_cra_init_aead(struct crypto_tfm *tfm)
2510497f2e6bSLee Nipper {
2511497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2512497f2e6bSLee Nipper 
2513497f2e6bSLee Nipper 	talitos_cra_init(tfm);
25149c4a7965SKim Phillips 
25159c4a7965SKim Phillips 	/* random first IV */
251670bcaca7SLee Nipper 	get_random_bytes(ctx->iv, TALITOS_MAX_IV_LENGTH);
25179c4a7965SKim Phillips 
25189c4a7965SKim Phillips 	return 0;
25199c4a7965SKim Phillips }
25209c4a7965SKim Phillips 
2521497f2e6bSLee Nipper static int talitos_cra_init_ahash(struct crypto_tfm *tfm)
2522497f2e6bSLee Nipper {
2523497f2e6bSLee Nipper 	struct talitos_ctx *ctx = crypto_tfm_ctx(tfm);
2524497f2e6bSLee Nipper 
2525497f2e6bSLee Nipper 	talitos_cra_init(tfm);
2526497f2e6bSLee Nipper 
2527497f2e6bSLee Nipper 	ctx->keylen = 0;
2528497f2e6bSLee Nipper 	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2529497f2e6bSLee Nipper 				 sizeof(struct talitos_ahash_req_ctx));
2530497f2e6bSLee Nipper 
2531497f2e6bSLee Nipper 	return 0;
2532497f2e6bSLee Nipper }
2533497f2e6bSLee Nipper 
25349c4a7965SKim Phillips /*
25359c4a7965SKim Phillips  * given the alg's descriptor header template, determine whether descriptor
25369c4a7965SKim Phillips  * type and primary/secondary execution units required match the hw
25379c4a7965SKim Phillips  * capabilities description provided in the device tree node.
25389c4a7965SKim Phillips  */
25399c4a7965SKim Phillips static int hw_supports(struct device *dev, __be32 desc_hdr_template)
25409c4a7965SKim Phillips {
25419c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
25429c4a7965SKim Phillips 	int ret;
25439c4a7965SKim Phillips 
25449c4a7965SKim Phillips 	ret = (1 << DESC_TYPE(desc_hdr_template) & priv->desc_types) &&
25459c4a7965SKim Phillips 	      (1 << PRIMARY_EU(desc_hdr_template) & priv->exec_units);
25469c4a7965SKim Phillips 
25479c4a7965SKim Phillips 	if (SECONDARY_EU(desc_hdr_template))
25489c4a7965SKim Phillips 		ret = ret && (1 << SECONDARY_EU(desc_hdr_template)
25499c4a7965SKim Phillips 		              & priv->exec_units);
25509c4a7965SKim Phillips 
25519c4a7965SKim Phillips 	return ret;
25529c4a7965SKim Phillips }
25539c4a7965SKim Phillips 
25542dc11581SGrant Likely static int talitos_remove(struct platform_device *ofdev)
25559c4a7965SKim Phillips {
25569c4a7965SKim Phillips 	struct device *dev = &ofdev->dev;
25579c4a7965SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
25589c4a7965SKim Phillips 	struct talitos_crypto_alg *t_alg, *n;
25599c4a7965SKim Phillips 	int i;
25609c4a7965SKim Phillips 
25619c4a7965SKim Phillips 	list_for_each_entry_safe(t_alg, n, &priv->alg_list, entry) {
2562acbf7c62SLee Nipper 		switch (t_alg->algt.type) {
2563acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_ABLKCIPHER:
2564acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_AEAD:
2565acbf7c62SLee Nipper 			crypto_unregister_alg(&t_alg->algt.alg.crypto);
2566acbf7c62SLee Nipper 			break;
2567acbf7c62SLee Nipper 		case CRYPTO_ALG_TYPE_AHASH:
2568acbf7c62SLee Nipper 			crypto_unregister_ahash(&t_alg->algt.alg.hash);
2569acbf7c62SLee Nipper 			break;
2570acbf7c62SLee Nipper 		}
25719c4a7965SKim Phillips 		list_del(&t_alg->entry);
25729c4a7965SKim Phillips 		kfree(t_alg);
25739c4a7965SKim Phillips 	}
25749c4a7965SKim Phillips 
25759c4a7965SKim Phillips 	if (hw_supports(dev, DESC_HDR_SEL0_RNG))
25769c4a7965SKim Phillips 		talitos_unregister_rng(dev);
25779c4a7965SKim Phillips 
25789c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++)
25794b992628SKim Phillips 		kfree(priv->chan[i].fifo);
25809c4a7965SKim Phillips 
25814b992628SKim Phillips 	kfree(priv->chan);
25829c4a7965SKim Phillips 
2583c3e337f8SKim Phillips 	for (i = 0; i < 2; i++)
2584*2cdba3cfSKim Phillips 		if (priv->irq[i]) {
2585c3e337f8SKim Phillips 			free_irq(priv->irq[i], dev);
2586c3e337f8SKim Phillips 			irq_dispose_mapping(priv->irq[i]);
25879c4a7965SKim Phillips 		}
25889c4a7965SKim Phillips 
2589c3e337f8SKim Phillips 	tasklet_kill(&priv->done_task[0]);
2590*2cdba3cfSKim Phillips 	if (priv->irq[1])
2591c3e337f8SKim Phillips 		tasklet_kill(&priv->done_task[1]);
25929c4a7965SKim Phillips 
25939c4a7965SKim Phillips 	iounmap(priv->reg);
25949c4a7965SKim Phillips 
25959c4a7965SKim Phillips 	dev_set_drvdata(dev, NULL);
25969c4a7965SKim Phillips 
25979c4a7965SKim Phillips 	kfree(priv);
25989c4a7965SKim Phillips 
25999c4a7965SKim Phillips 	return 0;
26009c4a7965SKim Phillips }
26019c4a7965SKim Phillips 
26029c4a7965SKim Phillips static struct talitos_crypto_alg *talitos_alg_alloc(struct device *dev,
26039c4a7965SKim Phillips 						    struct talitos_alg_template
26049c4a7965SKim Phillips 						           *template)
26059c4a7965SKim Phillips {
260660f208d7SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
26079c4a7965SKim Phillips 	struct talitos_crypto_alg *t_alg;
26089c4a7965SKim Phillips 	struct crypto_alg *alg;
26099c4a7965SKim Phillips 
26109c4a7965SKim Phillips 	t_alg = kzalloc(sizeof(struct talitos_crypto_alg), GFP_KERNEL);
26119c4a7965SKim Phillips 	if (!t_alg)
26129c4a7965SKim Phillips 		return ERR_PTR(-ENOMEM);
26139c4a7965SKim Phillips 
2614acbf7c62SLee Nipper 	t_alg->algt = *template;
2615acbf7c62SLee Nipper 
2616acbf7c62SLee Nipper 	switch (t_alg->algt.type) {
2617acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_ABLKCIPHER:
2618497f2e6bSLee Nipper 		alg = &t_alg->algt.alg.crypto;
2619497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init;
2620497f2e6bSLee Nipper 		break;
2621acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_AEAD:
2622acbf7c62SLee Nipper 		alg = &t_alg->algt.alg.crypto;
2623497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init_aead;
2624acbf7c62SLee Nipper 		break;
2625acbf7c62SLee Nipper 	case CRYPTO_ALG_TYPE_AHASH:
2626acbf7c62SLee Nipper 		alg = &t_alg->algt.alg.hash.halg.base;
2627497f2e6bSLee Nipper 		alg->cra_init = talitos_cra_init_ahash;
262879b3a418SLee Nipper 		if (!(priv->features & TALITOS_FTR_HMAC_OK) &&
26290b2730d8SKim Phillips 		    !strncmp(alg->cra_name, "hmac", 4)) {
26300b2730d8SKim Phillips 			kfree(t_alg);
263179b3a418SLee Nipper 			return ERR_PTR(-ENOTSUPP);
26320b2730d8SKim Phillips 		}
263360f208d7SKim Phillips 		if (!(priv->features & TALITOS_FTR_SHA224_HWINIT) &&
263479b3a418SLee Nipper 		    (!strcmp(alg->cra_name, "sha224") ||
263579b3a418SLee Nipper 		     !strcmp(alg->cra_name, "hmac(sha224)"))) {
263660f208d7SKim Phillips 			t_alg->algt.alg.hash.init = ahash_init_sha224_swinit;
263760f208d7SKim Phillips 			t_alg->algt.desc_hdr_template =
263860f208d7SKim Phillips 					DESC_HDR_TYPE_COMMON_NONSNOOP_NO_AFEU |
263960f208d7SKim Phillips 					DESC_HDR_SEL0_MDEUA |
264060f208d7SKim Phillips 					DESC_HDR_MODE0_MDEU_SHA256;
264160f208d7SKim Phillips 		}
2642497f2e6bSLee Nipper 		break;
26431d11911aSKim Phillips 	default:
26441d11911aSKim Phillips 		dev_err(dev, "unknown algorithm type %d\n", t_alg->algt.type);
26451d11911aSKim Phillips 		return ERR_PTR(-EINVAL);
2646acbf7c62SLee Nipper 	}
26479c4a7965SKim Phillips 
26489c4a7965SKim Phillips 	alg->cra_module = THIS_MODULE;
26499c4a7965SKim Phillips 	alg->cra_priority = TALITOS_CRA_PRIORITY;
26509c4a7965SKim Phillips 	alg->cra_alignmask = 0;
26519c4a7965SKim Phillips 	alg->cra_ctxsize = sizeof(struct talitos_ctx);
26529c4a7965SKim Phillips 
26539c4a7965SKim Phillips 	t_alg->dev = dev;
26549c4a7965SKim Phillips 
26559c4a7965SKim Phillips 	return t_alg;
26569c4a7965SKim Phillips }
26579c4a7965SKim Phillips 
2658c3e337f8SKim Phillips static int talitos_probe_irq(struct platform_device *ofdev)
2659c3e337f8SKim Phillips {
2660c3e337f8SKim Phillips 	struct device *dev = &ofdev->dev;
2661c3e337f8SKim Phillips 	struct device_node *np = ofdev->dev.of_node;
2662c3e337f8SKim Phillips 	struct talitos_private *priv = dev_get_drvdata(dev);
2663c3e337f8SKim Phillips 	int err;
2664c3e337f8SKim Phillips 
2665c3e337f8SKim Phillips 	priv->irq[0] = irq_of_parse_and_map(np, 0);
2666*2cdba3cfSKim Phillips 	if (!priv->irq[0]) {
2667c3e337f8SKim Phillips 		dev_err(dev, "failed to map irq\n");
2668c3e337f8SKim Phillips 		return -EINVAL;
2669c3e337f8SKim Phillips 	}
2670c3e337f8SKim Phillips 
2671c3e337f8SKim Phillips 	priv->irq[1] = irq_of_parse_and_map(np, 1);
2672c3e337f8SKim Phillips 
2673c3e337f8SKim Phillips 	/* get the primary irq line */
2674*2cdba3cfSKim Phillips 	if (!priv->irq[1]) {
2675c3e337f8SKim Phillips 		err = request_irq(priv->irq[0], talitos_interrupt_4ch, 0,
2676c3e337f8SKim Phillips 				  dev_driver_string(dev), dev);
2677c3e337f8SKim Phillips 		goto primary_out;
2678c3e337f8SKim Phillips 	}
2679c3e337f8SKim Phillips 
2680c3e337f8SKim Phillips 	err = request_irq(priv->irq[0], talitos_interrupt_ch0_2, 0,
2681c3e337f8SKim Phillips 			  dev_driver_string(dev), dev);
2682c3e337f8SKim Phillips 	if (err)
2683c3e337f8SKim Phillips 		goto primary_out;
2684c3e337f8SKim Phillips 
2685c3e337f8SKim Phillips 	/* get the secondary irq line */
2686c3e337f8SKim Phillips 	err = request_irq(priv->irq[1], talitos_interrupt_ch1_3, 0,
2687c3e337f8SKim Phillips 			  dev_driver_string(dev), dev);
2688c3e337f8SKim Phillips 	if (err) {
2689c3e337f8SKim Phillips 		dev_err(dev, "failed to request secondary irq\n");
2690c3e337f8SKim Phillips 		irq_dispose_mapping(priv->irq[1]);
2691*2cdba3cfSKim Phillips 		priv->irq[1] = 0;
2692c3e337f8SKim Phillips 	}
2693c3e337f8SKim Phillips 
2694c3e337f8SKim Phillips 	return err;
2695c3e337f8SKim Phillips 
2696c3e337f8SKim Phillips primary_out:
2697c3e337f8SKim Phillips 	if (err) {
2698c3e337f8SKim Phillips 		dev_err(dev, "failed to request primary irq\n");
2699c3e337f8SKim Phillips 		irq_dispose_mapping(priv->irq[0]);
2700*2cdba3cfSKim Phillips 		priv->irq[0] = 0;
2701c3e337f8SKim Phillips 	}
2702c3e337f8SKim Phillips 
2703c3e337f8SKim Phillips 	return err;
2704c3e337f8SKim Phillips }
2705c3e337f8SKim Phillips 
27061c48a5c9SGrant Likely static int talitos_probe(struct platform_device *ofdev)
27079c4a7965SKim Phillips {
27089c4a7965SKim Phillips 	struct device *dev = &ofdev->dev;
270961c7a080SGrant Likely 	struct device_node *np = ofdev->dev.of_node;
27109c4a7965SKim Phillips 	struct talitos_private *priv;
27119c4a7965SKim Phillips 	const unsigned int *prop;
27129c4a7965SKim Phillips 	int i, err;
27139c4a7965SKim Phillips 
27149c4a7965SKim Phillips 	priv = kzalloc(sizeof(struct talitos_private), GFP_KERNEL);
27159c4a7965SKim Phillips 	if (!priv)
27169c4a7965SKim Phillips 		return -ENOMEM;
27179c4a7965SKim Phillips 
27189c4a7965SKim Phillips 	dev_set_drvdata(dev, priv);
27199c4a7965SKim Phillips 
27209c4a7965SKim Phillips 	priv->ofdev = ofdev;
27219c4a7965SKim Phillips 
2722c3e337f8SKim Phillips 	err = talitos_probe_irq(ofdev);
2723c3e337f8SKim Phillips 	if (err)
2724c3e337f8SKim Phillips 		goto err_out;
2725c3e337f8SKim Phillips 
2726*2cdba3cfSKim Phillips 	if (!priv->irq[1]) {
2727c3e337f8SKim Phillips 		tasklet_init(&priv->done_task[0], talitos_done_4ch,
2728c3e337f8SKim Phillips 			     (unsigned long)dev);
2729c3e337f8SKim Phillips 	} else {
2730c3e337f8SKim Phillips 		tasklet_init(&priv->done_task[0], talitos_done_ch0_2,
2731c3e337f8SKim Phillips 			     (unsigned long)dev);
2732c3e337f8SKim Phillips 		tasklet_init(&priv->done_task[1], talitos_done_ch1_3,
2733c3e337f8SKim Phillips 			     (unsigned long)dev);
2734c3e337f8SKim Phillips 	}
27359c4a7965SKim Phillips 
2736fe5720e2SKim Phillips 	INIT_LIST_HEAD(&priv->alg_list);
2737fe5720e2SKim Phillips 
27389c4a7965SKim Phillips 	priv->reg = of_iomap(np, 0);
27399c4a7965SKim Phillips 	if (!priv->reg) {
27409c4a7965SKim Phillips 		dev_err(dev, "failed to of_iomap\n");
27419c4a7965SKim Phillips 		err = -ENOMEM;
27429c4a7965SKim Phillips 		goto err_out;
27439c4a7965SKim Phillips 	}
27449c4a7965SKim Phillips 
27459c4a7965SKim Phillips 	/* get SEC version capabilities from device tree */
27469c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,num-channels", NULL);
27479c4a7965SKim Phillips 	if (prop)
27489c4a7965SKim Phillips 		priv->num_channels = *prop;
27499c4a7965SKim Phillips 
27509c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,channel-fifo-len", NULL);
27519c4a7965SKim Phillips 	if (prop)
27529c4a7965SKim Phillips 		priv->chfifo_len = *prop;
27539c4a7965SKim Phillips 
27549c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,exec-units-mask", NULL);
27559c4a7965SKim Phillips 	if (prop)
27569c4a7965SKim Phillips 		priv->exec_units = *prop;
27579c4a7965SKim Phillips 
27589c4a7965SKim Phillips 	prop = of_get_property(np, "fsl,descriptor-types-mask", NULL);
27599c4a7965SKim Phillips 	if (prop)
27609c4a7965SKim Phillips 		priv->desc_types = *prop;
27619c4a7965SKim Phillips 
27629c4a7965SKim Phillips 	if (!is_power_of_2(priv->num_channels) || !priv->chfifo_len ||
27639c4a7965SKim Phillips 	    !priv->exec_units || !priv->desc_types) {
27649c4a7965SKim Phillips 		dev_err(dev, "invalid property data in device tree node\n");
27659c4a7965SKim Phillips 		err = -EINVAL;
27669c4a7965SKim Phillips 		goto err_out;
27679c4a7965SKim Phillips 	}
27689c4a7965SKim Phillips 
2769f3c85bc1SLee Nipper 	if (of_device_is_compatible(np, "fsl,sec3.0"))
2770f3c85bc1SLee Nipper 		priv->features |= TALITOS_FTR_SRC_LINK_TBL_LEN_INCLUDES_EXTENT;
2771f3c85bc1SLee Nipper 
2772fe5720e2SKim Phillips 	if (of_device_is_compatible(np, "fsl,sec2.1"))
277360f208d7SKim Phillips 		priv->features |= TALITOS_FTR_HW_AUTH_CHECK |
277479b3a418SLee Nipper 				  TALITOS_FTR_SHA224_HWINIT |
277579b3a418SLee Nipper 				  TALITOS_FTR_HMAC_OK;
2776fe5720e2SKim Phillips 
27774b992628SKim Phillips 	priv->chan = kzalloc(sizeof(struct talitos_channel) *
27784b992628SKim Phillips 			     priv->num_channels, GFP_KERNEL);
27794b992628SKim Phillips 	if (!priv->chan) {
27804b992628SKim Phillips 		dev_err(dev, "failed to allocate channel management space\n");
27819c4a7965SKim Phillips 		err = -ENOMEM;
27829c4a7965SKim Phillips 		goto err_out;
27839c4a7965SKim Phillips 	}
27849c4a7965SKim Phillips 
2785c3e337f8SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
2786c3e337f8SKim Phillips 		priv->chan[i].reg = priv->reg + TALITOS_CH_STRIDE * (i + 1);
2787*2cdba3cfSKim Phillips 		if (!priv->irq[1] || !(i & 1))
2788c3e337f8SKim Phillips 			priv->chan[i].reg += TALITOS_CH_BASE_OFFSET;
2789c3e337f8SKim Phillips 	}
2790ad42d5fcSKim Phillips 
27919c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
27924b992628SKim Phillips 		spin_lock_init(&priv->chan[i].head_lock);
27934b992628SKim Phillips 		spin_lock_init(&priv->chan[i].tail_lock);
27949c4a7965SKim Phillips 	}
27959c4a7965SKim Phillips 
27969c4a7965SKim Phillips 	priv->fifo_len = roundup_pow_of_two(priv->chfifo_len);
27979c4a7965SKim Phillips 
27989c4a7965SKim Phillips 	for (i = 0; i < priv->num_channels; i++) {
27994b992628SKim Phillips 		priv->chan[i].fifo = kzalloc(sizeof(struct talitos_request) *
28009c4a7965SKim Phillips 					     priv->fifo_len, GFP_KERNEL);
28014b992628SKim Phillips 		if (!priv->chan[i].fifo) {
28029c4a7965SKim Phillips 			dev_err(dev, "failed to allocate request fifo %d\n", i);
28039c4a7965SKim Phillips 			err = -ENOMEM;
28049c4a7965SKim Phillips 			goto err_out;
28059c4a7965SKim Phillips 		}
28069c4a7965SKim Phillips 	}
28079c4a7965SKim Phillips 
2808ec6644d6SKim Phillips 	for (i = 0; i < priv->num_channels; i++)
28094b992628SKim Phillips 		atomic_set(&priv->chan[i].submit_count,
28104b992628SKim Phillips 			   -(priv->chfifo_len - 1));
28119c4a7965SKim Phillips 
281281eb024cSKim Phillips 	dma_set_mask(dev, DMA_BIT_MASK(36));
281381eb024cSKim Phillips 
28149c4a7965SKim Phillips 	/* reset and initialize the h/w */
28159c4a7965SKim Phillips 	err = init_device(dev);
28169c4a7965SKim Phillips 	if (err) {
28179c4a7965SKim Phillips 		dev_err(dev, "failed to initialize device\n");
28189c4a7965SKim Phillips 		goto err_out;
28199c4a7965SKim Phillips 	}
28209c4a7965SKim Phillips 
28219c4a7965SKim Phillips 	/* register the RNG, if available */
28229c4a7965SKim Phillips 	if (hw_supports(dev, DESC_HDR_SEL0_RNG)) {
28239c4a7965SKim Phillips 		err = talitos_register_rng(dev);
28249c4a7965SKim Phillips 		if (err) {
28259c4a7965SKim Phillips 			dev_err(dev, "failed to register hwrng: %d\n", err);
28269c4a7965SKim Phillips 			goto err_out;
28279c4a7965SKim Phillips 		} else
28289c4a7965SKim Phillips 			dev_info(dev, "hwrng\n");
28299c4a7965SKim Phillips 	}
28309c4a7965SKim Phillips 
28319c4a7965SKim Phillips 	/* register crypto algorithms the device supports */
28329c4a7965SKim Phillips 	for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
28339c4a7965SKim Phillips 		if (hw_supports(dev, driver_algs[i].desc_hdr_template)) {
28349c4a7965SKim Phillips 			struct talitos_crypto_alg *t_alg;
2835acbf7c62SLee Nipper 			char *name = NULL;
28369c4a7965SKim Phillips 
28379c4a7965SKim Phillips 			t_alg = talitos_alg_alloc(dev, &driver_algs[i]);
28389c4a7965SKim Phillips 			if (IS_ERR(t_alg)) {
28399c4a7965SKim Phillips 				err = PTR_ERR(t_alg);
28400b2730d8SKim Phillips 				if (err == -ENOTSUPP)
284179b3a418SLee Nipper 					continue;
28429c4a7965SKim Phillips 				goto err_out;
28439c4a7965SKim Phillips 			}
28449c4a7965SKim Phillips 
2845acbf7c62SLee Nipper 			switch (t_alg->algt.type) {
2846acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_ABLKCIPHER:
2847acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_AEAD:
2848acbf7c62SLee Nipper 				err = crypto_register_alg(
2849acbf7c62SLee Nipper 						&t_alg->algt.alg.crypto);
2850acbf7c62SLee Nipper 				name = t_alg->algt.alg.crypto.cra_driver_name;
2851acbf7c62SLee Nipper 				break;
2852acbf7c62SLee Nipper 			case CRYPTO_ALG_TYPE_AHASH:
2853acbf7c62SLee Nipper 				err = crypto_register_ahash(
2854acbf7c62SLee Nipper 						&t_alg->algt.alg.hash);
2855acbf7c62SLee Nipper 				name =
2856acbf7c62SLee Nipper 				 t_alg->algt.alg.hash.halg.base.cra_driver_name;
2857acbf7c62SLee Nipper 				break;
2858acbf7c62SLee Nipper 			}
28599c4a7965SKim Phillips 			if (err) {
28609c4a7965SKim Phillips 				dev_err(dev, "%s alg registration failed\n",
2861acbf7c62SLee Nipper 					name);
28629c4a7965SKim Phillips 				kfree(t_alg);
28635b859b6eSKim Phillips 			} else
28649c4a7965SKim Phillips 				list_add_tail(&t_alg->entry, &priv->alg_list);
28659c4a7965SKim Phillips 		}
28669c4a7965SKim Phillips 	}
28675b859b6eSKim Phillips 	if (!list_empty(&priv->alg_list))
28685b859b6eSKim Phillips 		dev_info(dev, "%s algorithms registered in /proc/crypto\n",
28695b859b6eSKim Phillips 			 (char *)of_get_property(np, "compatible", NULL));
28709c4a7965SKim Phillips 
28719c4a7965SKim Phillips 	return 0;
28729c4a7965SKim Phillips 
28739c4a7965SKim Phillips err_out:
28749c4a7965SKim Phillips 	talitos_remove(ofdev);
28759c4a7965SKim Phillips 
28769c4a7965SKim Phillips 	return err;
28779c4a7965SKim Phillips }
28789c4a7965SKim Phillips 
28796c3f975aSMárton Németh static const struct of_device_id talitos_match[] = {
28809c4a7965SKim Phillips 	{
28819c4a7965SKim Phillips 		.compatible = "fsl,sec2.0",
28829c4a7965SKim Phillips 	},
28839c4a7965SKim Phillips 	{},
28849c4a7965SKim Phillips };
28859c4a7965SKim Phillips MODULE_DEVICE_TABLE(of, talitos_match);
28869c4a7965SKim Phillips 
28871c48a5c9SGrant Likely static struct platform_driver talitos_driver = {
28884018294bSGrant Likely 	.driver = {
28899c4a7965SKim Phillips 		.name = "talitos",
28904018294bSGrant Likely 		.owner = THIS_MODULE,
28914018294bSGrant Likely 		.of_match_table = talitos_match,
28924018294bSGrant Likely 	},
28939c4a7965SKim Phillips 	.probe = talitos_probe,
2894596f1034SAl Viro 	.remove = talitos_remove,
28959c4a7965SKim Phillips };
28969c4a7965SKim Phillips 
2897741e8c2dSAxel Lin module_platform_driver(talitos_driver);
28989c4a7965SKim Phillips 
28999c4a7965SKim Phillips MODULE_LICENSE("GPL");
29009c4a7965SKim Phillips MODULE_AUTHOR("Kim Phillips <kim.phillips@freescale.com>");
29019c4a7965SKim Phillips MODULE_DESCRIPTION("Freescale integrated security engine (SEC) driver");
2902